blob: 50a233ddb5cc55658686ae3a94c3dfbe4d026373 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
Paul Bakker5121ce52009-01-03 21:22:43 +000020 * http://www.ietf.org/rfc/rfc2246.txt
21 * http://www.ietf.org/rfc/rfc4346.txt
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Jerry Yub476a442022-01-21 18:14:45 +080028#include <assert.h>
29
SimonBd5800b72016-04-26 07:43:27 +010030#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010033#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010034#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000035#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040036
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/debug.h"
38#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010040#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020041#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020042
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <string.h>
44
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050045#if defined(MBEDTLS_USE_PSA_CRYPTO)
46#include "mbedtls/psa_util.h"
47#include "psa/crypto.h"
48#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020049#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050050
Janos Follath23bdca02016-10-07 14:47:14 +010051#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020053#endif
54
Ronald Cronad8c17b2022-06-10 17:18:09 +020055#if defined(MBEDTLS_TEST_HOOKS)
56static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
57
58void mbedtls_ssl_set_chk_buf_ptr_fail_args(
59 const uint8_t *cur, const uint8_t *end, size_t need )
60{
61 chk_buf_ptr_fail_args.cur = cur;
62 chk_buf_ptr_fail_args.end = end;
63 chk_buf_ptr_fail_args.need = need;
64}
65
66void mbedtls_ssl_reset_chk_buf_ptr_fail_args( void )
67{
68 memset( &chk_buf_ptr_fail_args, 0, sizeof( chk_buf_ptr_fail_args ) );
69}
70
71int mbedtls_ssl_cmp_chk_buf_ptr_fail_args( mbedtls_ssl_chk_buf_ptr_args *args )
72{
73 return( ( chk_buf_ptr_fail_args.cur != args->cur ) ||
74 ( chk_buf_ptr_fail_args.end != args->end ) ||
75 ( chk_buf_ptr_fail_args.need != args->need ) );
76}
77#endif /* MBEDTLS_TEST_HOOKS */
78
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010080
Hanno Beckera0e20d02019-05-15 14:03:01 +010081#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010082/* Top-level Connection ID API */
83
Hanno Becker8367ccc2019-05-14 11:30:10 +010084int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
85 size_t len,
86 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +010087{
88 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
89 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
90
Hanno Becker611ac772019-05-14 11:45:26 +010091 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
92 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
93 {
94 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
95 }
96
97 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010098 conf->cid_len = len;
99 return( 0 );
100}
101
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100102int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
103 int enable,
104 unsigned char const *own_cid,
105 size_t own_cid_len )
106{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100107 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
109
Hanno Beckerca092242019-04-25 16:01:49 +0100110 ssl->negotiate_cid = enable;
111 if( enable == MBEDTLS_SSL_CID_DISABLED )
112 {
113 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
114 return( 0 );
115 }
116 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100117 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100118
Hanno Beckerad4a1372019-05-03 13:06:44 +0100119 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100120 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
122 (unsigned) own_cid_len,
123 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
125 }
126
127 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100128 /* Truncation is not an issue here because
129 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
130 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100131
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100132 return( 0 );
133}
134
Paul Elliott0113cf12022-03-11 20:26:47 +0000135int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl,
136 int *enabled,
137 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
138 size_t *own_cid_len )
139{
140 *enabled = MBEDTLS_SSL_CID_DISABLED;
141
142 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
144
145 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
146 * zero as this is indistinguishable from not requesting to use
147 * the CID extension. */
148 if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
149 return( 0 );
150
151 if( own_cid_len != NULL )
152 {
153 *own_cid_len = ssl->own_cid_len;
154 if( own_cid != NULL )
155 memcpy( own_cid, ssl->own_cid, ssl->own_cid_len );
156 }
157
158 *enabled = MBEDTLS_SSL_CID_ENABLED;
159
160 return( 0 );
161}
162
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100163int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
164 int *enabled,
165 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
166 size_t *peer_cid_len )
167{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100168 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100169
Hanno Becker76a79ab2019-05-03 14:38:32 +0100170 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Paul Elliott27b0d942022-03-18 21:55:32 +0000171 mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Hanno Becker76a79ab2019-05-03 14:38:32 +0100172 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100173 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100174 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100175
Hanno Beckerc5f24222019-05-03 12:54:52 +0100176 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
177 * were used, but client and server requested the empty CID.
178 * This is indistinguishable from not using the CID extension
179 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100180 if( ssl->transform_in->in_cid_len == 0 &&
181 ssl->transform_in->out_cid_len == 0 )
182 {
183 return( 0 );
184 }
185
Hanno Becker615ef172019-05-22 16:50:35 +0100186 if( peer_cid_len != NULL )
187 {
188 *peer_cid_len = ssl->transform_in->out_cid_len;
189 if( peer_cid != NULL )
190 {
191 memcpy( peer_cid, ssl->transform_in->out_cid,
192 ssl->transform_in->out_cid_len );
193 }
194 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100195
196 *enabled = MBEDTLS_SSL_CID_ENABLED;
197
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100198 return( 0 );
199}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100200#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200205/*
206 * Convert max_fragment_length codes to length.
207 * RFC 6066 says:
208 * enum{
209 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
210 * } MaxFragmentLength;
211 * and we add 0 -> extension unused
212 */
Angus Grattond8213d02016-05-25 20:56:48 +1000213static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200214{
Angus Grattond8213d02016-05-25 20:56:48 +1000215 switch( mfl )
216 {
217 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
218 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
219 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
220 return 512;
221 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
222 return 1024;
223 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
224 return 2048;
225 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
226 return 4096;
227 default:
228 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
229 }
230}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200232
Hanno Becker52055ae2019-02-06 14:30:46 +0000233int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
234 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_ssl_session_free( dst );
237 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
吴敬辉0b716112021-11-29 10:46:35 +0800238#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
239 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000240#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
241 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000242 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800243#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000244#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000247
248#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200249 if( src->peer_cert != NULL )
250 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000251 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200252
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200253 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200254 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200255 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200260 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263 dst->peer_cert = NULL;
264 return( ret );
265 }
266 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000267#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000268 if( src->peer_cert_digest != NULL )
269 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000270 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000271 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000272 if( dst->peer_cert_digest == NULL )
273 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
274
275 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
276 src->peer_cert_digest_len );
277 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000278 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000279 }
280#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200283
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200284#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200285 if( src->ticket != NULL )
286 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200287 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200288 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200289 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200290
291 memcpy( dst->ticket, src->ticket, src->ticket_len );
292 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000293
294#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
295 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
296 if( src->endpoint == MBEDTLS_SSL_IS_CLIENT )
297 {
298 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
299 ret = mbedtls_ssl_session_set_hostname( dst, src->hostname );
300 if( ret != 0 )
301 return ( ret );
302 }
303#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
304 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200305#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200306
307 return( 0 );
308}
309
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500310#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200311MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500312static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
313{
314 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
315 if( resized_buffer == NULL )
316 return -1;
317
318 /* We want to copy len_new bytes when downsizing the buffer, and
319 * len_old bytes when upsizing, so we choose the smaller of two sizes,
320 * to fit one buffer into another. Size checks, ensuring that no data is
321 * lost, are done outside of this function. */
322 memcpy( resized_buffer, *buffer,
323 ( len_new < *len_old ) ? len_new : *len_old );
324 mbedtls_platform_zeroize( *buffer, *len_old );
325 mbedtls_free( *buffer );
326
327 *buffer = resized_buffer;
328 *len_old = len_new;
329
330 return 0;
331}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200332
333static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500334 size_t in_buf_new_len,
335 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200336{
337 int modified = 0;
338 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
339 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
340 if( ssl->in_buf != NULL )
341 {
342 written_in = ssl->in_msg - ssl->in_buf;
343 iv_offset_in = ssl->in_iv - ssl->in_buf;
344 len_offset_in = ssl->in_len - ssl->in_buf;
345 if( downsizing ?
346 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
347 ssl->in_buf_len < in_buf_new_len )
348 {
349 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
350 {
351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
352 }
353 else
354 {
Paul Elliottb7449902021-03-10 18:14:58 +0000355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
356 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200357 modified = 1;
358 }
359 }
360 }
361
362 if( ssl->out_buf != NULL )
363 {
364 written_out = ssl->out_msg - ssl->out_buf;
365 iv_offset_out = ssl->out_iv - ssl->out_buf;
366 len_offset_out = ssl->out_len - ssl->out_buf;
367 if( downsizing ?
368 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
369 ssl->out_buf_len < out_buf_new_len )
370 {
371 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
372 {
373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
374 }
375 else
376 {
Paul Elliottb7449902021-03-10 18:14:58 +0000377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
378 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200379 modified = 1;
380 }
381 }
382 }
383 if( modified )
384 {
385 /* Update pointers here to avoid doing it twice. */
386 mbedtls_ssl_reset_in_out_pointers( ssl );
387 /* Fields below might not be properly updated with record
388 * splitting or with CID, so they are manually updated here. */
389 ssl->out_msg = ssl->out_buf + written_out;
390 ssl->out_len = ssl->out_buf + len_offset_out;
391 ssl->out_iv = ssl->out_buf + iv_offset_out;
392
393 ssl->in_msg = ssl->in_buf + written_in;
394 ssl->in_len = ssl->in_buf + len_offset_in;
395 ssl->in_iv = ssl->in_buf + iv_offset_in;
396 }
397}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500398#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
399
Jerry Yudb8c48a2022-01-27 14:54:54 +0800400#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800401
402#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
403typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
404 const char *label,
405 const unsigned char *random, size_t rlen,
406 unsigned char *dstbuf, size_t dlen );
407
408static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
409
410#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
411
412/* Type for the TLS PRF */
413typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
414 const unsigned char *, size_t,
415 unsigned char *, size_t);
416
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200417MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800418static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
419 int ciphersuite,
420 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200421#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yued14c932022-02-17 13:40:45 +0800422 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200423#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yued14c932022-02-17 13:40:45 +0800424 ssl_tls_prf_t tls_prf,
425 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -0400426 mbedtls_ssl_protocol_version tls_version,
Jerry Yued14c932022-02-17 13:40:45 +0800427 unsigned endpoint,
428 const mbedtls_ssl_context *ssl );
429
Andrzej Kurek25f27152022-08-17 16:09:31 -0400430#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200431MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800432static int tls_prf_sha256( const unsigned char *secret, size_t slen,
433 const char *label,
434 const unsigned char *random, size_t rlen,
435 unsigned char *dstbuf, size_t dlen );
436static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
437static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
438
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400439#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800440
Andrzej Kurek25f27152022-08-17 16:09:31 -0400441#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200442MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800443static int tls_prf_sha384( const unsigned char *secret, size_t slen,
444 const char *label,
445 const unsigned char *random, size_t rlen,
446 unsigned char *dstbuf, size_t dlen );
447
448static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
449static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400450#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800451
Jerry Yu438ddd82022-07-07 06:55:50 +0000452static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800453 unsigned char *buf,
454 size_t buf_len );
Jerry Yu251a12e2022-07-13 15:15:48 +0800455
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200456MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +0000457static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800458 const unsigned char *buf,
459 size_t len );
460#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
461
Jerry Yu53d23e22022-02-09 16:25:09 +0800462static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
463
Andrzej Kurek25f27152022-08-17 16:09:31 -0400464#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800465static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400466#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudb8c48a2022-01-27 14:54:54 +0800467
Andrzej Kurek25f27152022-08-17 16:09:31 -0400468#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800469static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400470#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Paul Bakker1ef83d62012-04-11 12:09:53 +0000471
Ron Eldor51d3ab52019-05-12 14:54:30 +0300472int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
473 const unsigned char *secret, size_t slen,
474 const char *label,
475 const unsigned char *random, size_t rlen,
476 unsigned char *dstbuf, size_t dlen )
477{
478 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
479
480 switch( prf )
481 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300482#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400483#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300484 case MBEDTLS_SSL_TLS_PRF_SHA384:
485 tls_prf = tls_prf_sha384;
486 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400487#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400488#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300489 case MBEDTLS_SSL_TLS_PRF_SHA256:
490 tls_prf = tls_prf_sha256;
491 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400492#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300493#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300494 default:
495 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
496 }
497
498 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
499}
500
Jerry Yuc73c6182022-02-08 20:29:25 +0800501#if defined(MBEDTLS_X509_CRT_PARSE_C)
502static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
503{
504#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
505 if( session->peer_cert != NULL )
506 {
507 mbedtls_x509_crt_free( session->peer_cert );
508 mbedtls_free( session->peer_cert );
509 session->peer_cert = NULL;
510 }
511#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
512 if( session->peer_cert_digest != NULL )
513 {
514 /* Zeroization is not necessary. */
515 mbedtls_free( session->peer_cert_digest );
516 session->peer_cert_digest = NULL;
517 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
518 session->peer_cert_digest_len = 0;
519 }
520#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
521}
522#endif /* MBEDTLS_X509_CRT_PARSE_C */
523
524void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
525 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
526{
527 ((void) ciphersuite_info);
528
Andrzej Kurek25f27152022-08-17 16:09:31 -0400529#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800530 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
531 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
532 else
533#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400534#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800535 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
536 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
537 else
538#endif
539 {
540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
541 return;
542 }
543}
544
XiaokangQianadab9a62022-07-18 07:41:26 +0000545void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
546 unsigned hs_type,
547 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100548{
549 unsigned char hs_hdr[4];
550
551 /* Build HS header for checksum update. */
552 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
553 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
554 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
555 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
556
557 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
558}
559
560void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
561 unsigned hs_type,
562 unsigned char const *msg,
563 size_t msg_len )
564{
565 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
566 ssl->handshake->update_checksum( ssl, msg, msg_len );
567}
568
Jerry Yuc73c6182022-02-08 20:29:25 +0800569void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
570{
571 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400572#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800573#if defined(MBEDTLS_USE_PSA_CRYPTO)
574 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
575 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
576#else
577 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
578#endif
579#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400580#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800581#if defined(MBEDTLS_USE_PSA_CRYPTO)
582 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
583 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
584#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400585 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800586#endif
587#endif
588}
589
590static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
591 const unsigned char *buf, size_t len )
592{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400593#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800594#if defined(MBEDTLS_USE_PSA_CRYPTO)
595 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
596#else
597 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
598#endif
599#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400600#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800601#if defined(MBEDTLS_USE_PSA_CRYPTO)
602 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
603#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400604 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800605#endif
606#endif
607}
608
Andrzej Kurek25f27152022-08-17 16:09:31 -0400609#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800610static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
611 const unsigned char *buf, size_t len )
612{
613#if defined(MBEDTLS_USE_PSA_CRYPTO)
614 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
615#else
616 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
617#endif
618}
619#endif
620
Andrzej Kurek25f27152022-08-17 16:09:31 -0400621#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800622static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
623 const unsigned char *buf, size_t len )
624{
625#if defined(MBEDTLS_USE_PSA_CRYPTO)
626 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
627#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400628 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800629#endif
630}
631#endif
632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200634{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200636
Andrzej Kurek25f27152022-08-17 16:09:31 -0400637#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500638#if defined(MBEDTLS_USE_PSA_CRYPTO)
639 handshake->fin_sha256_psa = psa_hash_operation_init();
640 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
641#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200643 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200644#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500645#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400646#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500647#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500648 handshake->fin_sha384_psa = psa_hash_operation_init();
649 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500650#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400651 mbedtls_sha512_init( &handshake->fin_sha384 );
652 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200653#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500654#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200655
656 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#if defined(MBEDTLS_DHM_C)
659 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200660#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200661#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200663#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200664#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200665 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200666#if defined(MBEDTLS_SSL_CLI_C)
667 handshake->ecjpake_cache = NULL;
668 handshake->ecjpake_cache_len = 0;
669#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200670#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200671
Gilles Peskineeccd8882020-03-10 12:19:08 +0100672#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200673 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200674#endif
675
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200676#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
677 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
678#endif
Hanno Becker75173122019-02-06 16:18:31 +0000679
680#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
681 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
682 mbedtls_pk_init( &handshake->peer_pubkey );
683#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200684}
685
Hanno Beckera18d1322018-01-03 14:27:32 +0000686void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200687{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200689
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100690#if defined(MBEDTLS_USE_PSA_CRYPTO)
691 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
692 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100693#else
694 mbedtls_cipher_init( &transform->cipher_ctx_enc );
695 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100696#endif
697
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000698#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100699#if defined(MBEDTLS_USE_PSA_CRYPTO)
700 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
701 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100702#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_md_init( &transform->md_ctx_enc );
704 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000705#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100706#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200707}
708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200710{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200712}
713
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200714MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000716{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200717 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000718 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200720 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200722 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200723 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200724
725 /*
726 * Either the pointers are now NULL or cleared properly and can be freed.
727 * Now allocate missing structures.
728 */
729 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200730 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200731 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200732 }
Paul Bakker48916f92012-09-16 19:57:18 +0000733
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200734 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200735 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200736 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200737 }
Paul Bakker48916f92012-09-16 19:57:18 +0000738
Paul Bakker82788fb2014-10-20 13:59:19 +0200739 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200740 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200741 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200742 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500743#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
744 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400745
Andrzej Kurek4a063792020-10-21 15:08:44 +0200746 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
747 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500748#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000749
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200750 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000751 if( ssl->handshake == NULL ||
752 ssl->transform_negotiate == NULL ||
753 ssl->session_negotiate == NULL )
754 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200755 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_free( ssl->handshake );
758 mbedtls_free( ssl->transform_negotiate );
759 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200760
761 ssl->handshake = NULL;
762 ssl->transform_negotiate = NULL;
763 ssl->session_negotiate = NULL;
764
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200765 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000766 }
767
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200768 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000770 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200771 ssl_handshake_params_init( ssl->handshake );
772
Jerry Yud0766ec2022-09-22 10:46:57 +0800773#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
774 defined(MBEDTLS_SSL_SRV_C) && \
775 defined(MBEDTLS_SSL_SESSION_TICKETS)
776 ssl->handshake->new_session_tickets_count =
777 ssl->conf->new_session_tickets_count ;
778#endif
779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200781 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
782 {
783 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200784
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200785 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
786 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
787 else
788 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200789
Hanno Becker0f57a652020-02-05 10:37:26 +0000790 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200791 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200792#endif
793
Brett Warrene0edc842021-08-17 09:53:13 +0100794/*
795 * curve_list is translated to IANA TLS group identifiers here because
796 * mbedtls_ssl_conf_curves returns void and so can't return
797 * any error codes.
798 */
799#if defined(MBEDTLS_ECP_C)
800#if !defined(MBEDTLS_DEPRECATED_REMOVED)
801 /* Heap allocate and translate curve_list from internal to IANA group ids */
802 if ( ssl->conf->curve_list != NULL )
803 {
804 size_t length;
805 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
806
807 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
808 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
809
810 /* Leave room for zero termination */
811 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
812 if ( group_list == NULL )
813 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
814
815 for( size_t i = 0; i < length; i++ )
816 {
817 const mbedtls_ecp_curve_info *info =
818 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
819 if ( info == NULL )
820 {
821 mbedtls_free( group_list );
822 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
823 }
824 group_list[i] = info->tls_id;
825 }
826
827 group_list[length] = 0;
828
829 ssl->handshake->group_list = group_list;
830 ssl->handshake->group_list_heap_allocated = 1;
831 }
832 else
833 {
834 ssl->handshake->group_list = ssl->conf->group_list;
835 ssl->handshake->group_list_heap_allocated = 0;
836 }
837#endif /* MBEDTLS_DEPRECATED_REMOVED */
838#endif /* MBEDTLS_ECP_C */
839
Jerry Yuf017ee42022-01-12 15:49:48 +0800840#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
841#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800842#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800843 /* Heap allocate and translate sig_hashes from internal hash identifiers to
844 signature algorithms IANA identifiers. */
845 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800846 ssl->conf->sig_hashes != NULL )
847 {
848 const int *md;
849 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800850 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800851 uint16_t *p;
852
Jerry Yub476a442022-01-21 18:14:45 +0800853#if defined(static_assert)
854 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
855 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
856 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800857#endif
858
Jerry Yuf017ee42022-01-12 15:49:48 +0800859 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
860 {
861 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
862 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800863#if defined(MBEDTLS_ECDSA_C)
864 sig_algs_len += sizeof( uint16_t );
865#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800866
Jerry Yub476a442022-01-21 18:14:45 +0800867#if defined(MBEDTLS_RSA_C)
868 sig_algs_len += sizeof( uint16_t );
869#endif
870 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
871 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800872 }
873
Jerry Yub476a442022-01-21 18:14:45 +0800874 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800875 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
876
Jerry Yub476a442022-01-21 18:14:45 +0800877 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
878 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800879 if( ssl->handshake->sig_algs == NULL )
880 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
881
882 p = (uint16_t *)ssl->handshake->sig_algs;
883 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
884 {
885 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
886 if( hash == MBEDTLS_SSL_HASH_NONE )
887 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800888#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800889 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
890 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800891#endif
892#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800893 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
894 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800895#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800896 }
Gabor Mezei15b95a62022-05-09 16:37:58 +0200897 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +0800898 ssl->handshake->sig_algs_heap_allocated = 1;
899 }
900 else
Jerry Yua69269a2022-01-17 21:06:01 +0800901#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800902 {
Jerry Yuf017ee42022-01-12 15:49:48 +0800903 ssl->handshake->sig_algs_heap_allocated = 0;
904 }
Jerry Yucc539102022-06-27 16:27:35 +0800905#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yuf017ee42022-01-12 15:49:48 +0800906#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000907 return( 0 );
908}
909
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200910#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200911/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200912MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200913static int ssl_cookie_write_dummy( void *ctx,
914 unsigned char **p, unsigned char *end,
915 const unsigned char *cli_id, size_t cli_id_len )
916{
917 ((void) ctx);
918 ((void) p);
919 ((void) end);
920 ((void) cli_id);
921 ((void) cli_id_len);
922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200924}
925
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200926MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200927static int ssl_cookie_check_dummy( void *ctx,
928 const unsigned char *cookie, size_t cookie_len,
929 const unsigned char *cli_id, size_t cli_id_len )
930{
931 ((void) ctx);
932 ((void) cookie);
933 ((void) cookie_len);
934 ((void) cli_id);
935 ((void) cli_id_len);
936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200938}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200939#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200940
Paul Bakker5121ce52009-01-03 21:22:43 +0000941/*
942 * Initialize an SSL context
943 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200944void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
945{
946 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
947}
948
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200949MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800950static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
951{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100952 const mbedtls_ssl_config *conf = ssl->conf;
953
Ronald Cron6f135e12021-12-08 16:57:54 +0100954#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100955 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
956 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000957 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
958 {
959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
960 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
961 }
962
Jerry Yu60835a82021-08-04 10:13:52 +0800963 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
964 return( 0 );
965 }
966#endif
967
968#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100969 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800970 {
971 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
972 return( 0 );
973 }
974#endif
975
Ronald Cron6f135e12021-12-08 16:57:54 +0100976#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100977 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800978 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000979 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
980 {
981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
982 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
983 }
984
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000985 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
986 {
987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
988 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
989 }
990
991
Ronald Crone1d3f062022-02-10 14:50:54 +0100992 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
993 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +0800994 }
995#endif
996
997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
998 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
999}
1000
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001001MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001002static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1003{
1004 int ret;
1005 ret = ssl_conf_version_check( ssl );
1006 if( ret != 0 )
1007 return( ret );
1008
1009 /* Space for further checks */
1010
1011 return( 0 );
1012}
1013
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001014/*
1015 * Setup an SSL context
1016 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001017
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001018int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001019 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001020{
Janos Follath865b3eb2019-12-16 11:46:15 +00001021 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001022 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1023 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001024
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001025 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001026
Jerry Yu60835a82021-08-04 10:13:52 +08001027 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1028 return( ret );
1029
Paul Bakker62f2dee2012-09-28 07:31:51 +00001030 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001031 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001032 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001033
1034 /* Set to NULL in case of an error condition */
1035 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001036
Darryl Greenb33cc762019-11-28 14:29:44 +00001037#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1038 ssl->in_buf_len = in_buf_len;
1039#endif
1040 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001041 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001042 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001044 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001045 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001046 }
1047
Darryl Greenb33cc762019-11-28 14:29:44 +00001048#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1049 ssl->out_buf_len = out_buf_len;
1050#endif
1051 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001052 if( ssl->out_buf == NULL )
1053 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001055 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001056 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001057 }
1058
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001059 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001060
Johan Pascalb62bb512015-12-03 21:56:45 +01001061#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001062 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001063#endif
1064
Paul Bakker48916f92012-09-16 19:57:18 +00001065 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001066 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001067
1068 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001069
1070error:
1071 mbedtls_free( ssl->in_buf );
1072 mbedtls_free( ssl->out_buf );
1073
1074 ssl->conf = NULL;
1075
Darryl Greenb33cc762019-11-28 14:29:44 +00001076#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1077 ssl->in_buf_len = 0;
1078 ssl->out_buf_len = 0;
1079#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001080 ssl->in_buf = NULL;
1081 ssl->out_buf = NULL;
1082
1083 ssl->in_hdr = NULL;
1084 ssl->in_ctr = NULL;
1085 ssl->in_len = NULL;
1086 ssl->in_iv = NULL;
1087 ssl->in_msg = NULL;
1088
1089 ssl->out_hdr = NULL;
1090 ssl->out_ctr = NULL;
1091 ssl->out_len = NULL;
1092 ssl->out_iv = NULL;
1093 ssl->out_msg = NULL;
1094
k-stachowiak9f7798e2018-07-31 16:52:32 +02001095 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001096}
1097
1098/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001099 * Reset an initialized and used SSL context for re-use while retaining
1100 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001101 *
1102 * If partial is non-zero, keep data in the input buffer and client ID.
1103 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001104 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001105void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1106 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001107{
Darryl Greenb33cc762019-11-28 14:29:44 +00001108#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1109 size_t in_buf_len = ssl->in_buf_len;
1110 size_t out_buf_len = ssl->out_buf_len;
1111#else
1112 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1113 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1114#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001115
Hanno Beckerb0302c42021-08-03 09:39:42 +01001116#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1117 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001118#endif
1119
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001120 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001121 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001122
Hanno Beckerb0302c42021-08-03 09:39:42 +01001123 mbedtls_ssl_reset_in_out_pointers( ssl );
1124
1125 /* Reset incoming message parsing */
1126 ssl->in_offt = NULL;
1127 ssl->nb_zero = 0;
1128 ssl->in_msgtype = 0;
1129 ssl->in_msglen = 0;
1130 ssl->in_hslen = 0;
1131 ssl->keep_current_message = 0;
1132 ssl->transform_in = NULL;
1133
1134#if defined(MBEDTLS_SSL_PROTO_DTLS)
1135 ssl->next_record_offset = 0;
1136 ssl->in_epoch = 0;
1137#endif
1138
1139 /* Keep current datagram if partial == 1 */
1140 if( partial == 0 )
1141 {
1142 ssl->in_left = 0;
1143 memset( ssl->in_buf, 0, in_buf_len );
1144 }
1145
Ronald Cronad8c17b2022-06-10 17:18:09 +02001146 ssl->send_alert = 0;
1147
Hanno Beckerb0302c42021-08-03 09:39:42 +01001148 /* Reset outgoing message writing */
1149 ssl->out_msgtype = 0;
1150 ssl->out_msglen = 0;
1151 ssl->out_left = 0;
1152 memset( ssl->out_buf, 0, out_buf_len );
1153 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1154 ssl->transform_out = NULL;
1155
1156#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1157 mbedtls_ssl_dtls_replay_reset( ssl );
1158#endif
1159
XiaokangQian2b01dc32022-01-21 02:53:13 +00001160#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001161 if( ssl->transform )
1162 {
1163 mbedtls_ssl_transform_free( ssl->transform );
1164 mbedtls_free( ssl->transform );
1165 ssl->transform = NULL;
1166 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001167#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1168
XiaokangQian2b01dc32022-01-21 02:53:13 +00001169#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1170 mbedtls_ssl_transform_free( ssl->transform_application );
1171 mbedtls_free( ssl->transform_application );
1172 ssl->transform_application = NULL;
1173
1174 if( ssl->handshake != NULL )
1175 {
1176 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1177 mbedtls_free( ssl->handshake->transform_earlydata );
1178 ssl->handshake->transform_earlydata = NULL;
1179
1180 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1181 mbedtls_free( ssl->handshake->transform_handshake );
1182 ssl->handshake->transform_handshake = NULL;
1183 }
1184
XiaokangQian2b01dc32022-01-21 02:53:13 +00001185#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001186}
1187
1188int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1189{
1190 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1191
1192 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1193
XiaokangQian78b1fa72022-01-19 06:56:30 +00001194 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001195
1196 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197#if defined(MBEDTLS_SSL_RENEGOTIATION)
1198 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001199 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001200
1201 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1203 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001204#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001206
Hanno Beckerb0302c42021-08-03 09:39:42 +01001207 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001208 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001209 if( ssl->session )
1210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 mbedtls_ssl_session_free( ssl->session );
1212 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001213 ssl->session = NULL;
1214 }
1215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001217 ssl->alpn_chosen = NULL;
1218#endif
1219
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001220#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01001221#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001222 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01001223#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001224 {
1225 mbedtls_free( ssl->cli_id );
1226 ssl->cli_id = NULL;
1227 ssl->cli_id_len = 0;
1228 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001229#endif
1230
Paul Bakker48916f92012-09-16 19:57:18 +00001231 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1232 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001233
1234 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001235}
1236
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001237/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001238 * Reset an initialized and used SSL context for re-use while retaining
1239 * all application-set variables, function pointers and data.
1240 */
1241int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1242{
Hanno Becker43aefe22020-02-05 10:44:56 +00001243 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001244}
1245
1246/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001247 * SSL set accessors
1248 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001249void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001250{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001251 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001252}
1253
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001254void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001255{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001256 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001257}
1258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001260void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001261{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001262 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001263}
1264#endif
1265
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001266void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001267{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001268 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001269}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001272
Hanno Becker1841b0a2018-08-24 11:13:57 +01001273void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1274 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001275{
1276 ssl->disable_datagram_packing = !allow_packing;
1277}
1278
1279void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1280 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001281{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001282 conf->hs_timeout_min = min;
1283 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001284}
1285#endif
1286
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001287void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001288{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001289 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001290}
1291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001293void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001294 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001295 void *p_vrfy )
1296{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001297 conf->f_vrfy = f_vrfy;
1298 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001299}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001301
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001302void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001303 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001304 void *p_rng )
1305{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001306 conf->f_rng = f_rng;
1307 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001308}
1309
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001310void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001311 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001312 void *p_dbg )
1313{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001314 conf->f_dbg = f_dbg;
1315 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001316}
1317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001319 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001320 mbedtls_ssl_send_t *f_send,
1321 mbedtls_ssl_recv_t *f_recv,
1322 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001323{
1324 ssl->p_bio = p_bio;
1325 ssl->f_send = f_send;
1326 ssl->f_recv = f_recv;
1327 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001328}
1329
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001330#if defined(MBEDTLS_SSL_PROTO_DTLS)
1331void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1332{
1333 ssl->mtu = mtu;
1334}
1335#endif
1336
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001337void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001338{
1339 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001340}
1341
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001342void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1343 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001344 mbedtls_ssl_set_timer_t *f_set_timer,
1345 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001346{
1347 ssl->p_timer = p_timer;
1348 ssl->f_set_timer = f_set_timer;
1349 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001350
1351 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001352 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001353}
1354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001356void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001357 void *p_cache,
1358 mbedtls_ssl_cache_get_t *f_get_cache,
1359 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001360{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001361 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001362 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001363 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001364}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#if defined(MBEDTLS_SSL_CLI_C)
1368int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001369{
Janos Follath865b3eb2019-12-16 11:46:15 +00001370 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001371
1372 if( ssl == NULL ||
1373 session == NULL ||
1374 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001375 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001378 }
1379
Hanno Beckere810bbc2021-05-14 16:01:05 +01001380 if( ssl->handshake->resume == 1 )
1381 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1382
Jerry Yu21092062022-10-10 21:21:31 +08001383#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1384 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001385 {
Jerry Yu21092062022-10-10 21:21:31 +08001386 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1387 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1388
1389 if( mbedtls_ssl_validate_ciphersuite(
1390 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1391 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1392 {
1393 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1394 session->ciphersuite ) );
1395 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1396 }
Jerry Yu40afab62022-10-08 10:42:13 +08001397 }
Jerry Yu21092062022-10-10 21:21:31 +08001398#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001399
Hanno Becker52055ae2019-02-06 14:30:46 +00001400 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1401 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001402 return( ret );
1403
Paul Bakker0a597072012-09-25 21:55:46 +00001404 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001405
1406 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001407}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001409
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001410void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1411 const int *ciphersuites )
1412{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001413 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001414}
1415
Ronald Cron6f135e12021-12-08 16:57:54 +01001416#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001417void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001418 const int kex_modes )
1419{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001420 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001421}
Ronald Cron6f135e12021-12-08 16:57:54 +01001422#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001425void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001426 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001427{
1428 conf->cert_profile = profile;
1429}
1430
Glenn Strauss36872db2022-01-22 05:06:31 -05001431static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1432{
1433 mbedtls_ssl_key_cert *cur = key_cert, *next;
1434
1435 while( cur != NULL )
1436 {
1437 next = cur->next;
1438 mbedtls_free( cur );
1439 cur = next;
1440 }
1441}
1442
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001443/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001444MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001445static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1446 mbedtls_x509_crt *cert,
1447 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001448{
niisato8ee24222018-06-25 19:05:48 +09001449 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001450
Glenn Strauss36872db2022-01-22 05:06:31 -05001451 if( cert == NULL )
1452 {
1453 /* Free list if cert is null */
1454 ssl_key_cert_free( *head );
1455 *head = NULL;
1456 return( 0 );
1457 }
1458
niisato8ee24222018-06-25 19:05:48 +09001459 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1460 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001461 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001462
niisato8ee24222018-06-25 19:05:48 +09001463 new_cert->cert = cert;
1464 new_cert->key = key;
1465 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001466
Glenn Strauss36872db2022-01-22 05:06:31 -05001467 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001468 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001469 {
niisato8ee24222018-06-25 19:05:48 +09001470 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001471 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001472 else
1473 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001474 mbedtls_ssl_key_cert *cur = *head;
1475 while( cur->next != NULL )
1476 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001477 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001478 }
1479
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001480 return( 0 );
1481}
1482
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001483int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001484 mbedtls_x509_crt *own_cert,
1485 mbedtls_pk_context *pk_key )
1486{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001487 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001488}
1489
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001490void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001491 mbedtls_x509_crt *ca_chain,
1492 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001493{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001494 conf->ca_chain = ca_chain;
1495 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001496
1497#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1498 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1499 * cannot be used together. */
1500 conf->f_ca_cb = NULL;
1501 conf->p_ca_cb = NULL;
1502#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001503}
Hanno Becker5adaad92019-03-27 16:54:37 +00001504
1505#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1506void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001507 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001508 void *p_ca_cb )
1509{
1510 conf->f_ca_cb = f_ca_cb;
1511 conf->p_ca_cb = p_ca_cb;
1512
1513 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1514 * cannot be used together. */
1515 conf->ca_chain = NULL;
1516 conf->ca_crl = NULL;
1517}
1518#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001520
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001521#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001522const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1523 size_t *name_len )
1524{
1525 *name_len = ssl->handshake->sni_name_len;
1526 return( ssl->handshake->sni_name );
1527}
1528
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001529int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1530 mbedtls_x509_crt *own_cert,
1531 mbedtls_pk_context *pk_key )
1532{
1533 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1534 own_cert, pk_key ) );
1535}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001536
1537void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1538 mbedtls_x509_crt *ca_chain,
1539 mbedtls_x509_crl *ca_crl )
1540{
1541 ssl->handshake->sni_ca_chain = ca_chain;
1542 ssl->handshake->sni_ca_crl = ca_crl;
1543}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001544
Glenn Strauss999ef702022-03-11 01:37:23 -05001545#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1546void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1547 const mbedtls_x509_crt *crt)
1548{
1549 ssl->handshake->dn_hints = crt;
1550}
1551#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1552
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001553void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1554 int authmode )
1555{
1556 ssl->handshake->sni_authmode = authmode;
1557}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001558#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1559
Hanno Becker8927c832019-04-03 12:52:50 +01001560#if defined(MBEDTLS_X509_CRT_PARSE_C)
1561void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1562 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1563 void *p_vrfy )
1564{
1565 ssl->f_vrfy = f_vrfy;
1566 ssl->p_vrfy = p_vrfy;
1567}
1568#endif
1569
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001570#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001571/*
1572 * Set EC J-PAKE password for current handshake
1573 */
1574int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1575 const unsigned char *pw,
1576 size_t pw_len )
1577{
1578 mbedtls_ecjpake_role role;
1579
Janos Follath8eb64132016-06-03 15:40:57 +01001580 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001581 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1582
1583 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1584 role = MBEDTLS_ECJPAKE_SERVER;
1585 else
1586 role = MBEDTLS_ECJPAKE_CLIENT;
1587
1588 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1589 role,
1590 MBEDTLS_MD_SHA256,
1591 MBEDTLS_ECP_DP_SECP256R1,
1592 pw, pw_len ) );
1593}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001594#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001595
Gilles Peskineeccd8882020-03-10 12:19:08 +01001596#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001597
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001598MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001599static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1600{
1601#if defined(MBEDTLS_USE_PSA_CRYPTO)
1602 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1603 return( 1 );
1604#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001605 if( conf->psk != NULL )
1606 return( 1 );
1607
1608 return( 0 );
1609}
1610
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001611static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1612{
1613 /* Remove reference to existing PSK, if any. */
1614#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001615 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001616 {
1617 /* The maintenance of the PSK key slot is the
1618 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001619 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001620 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001621#endif /* MBEDTLS_USE_PSA_CRYPTO */
1622 if( conf->psk != NULL )
1623 {
1624 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1625
1626 mbedtls_free( conf->psk );
1627 conf->psk = NULL;
1628 conf->psk_len = 0;
1629 }
1630
1631 /* Remove reference to PSK identity, if any. */
1632 if( conf->psk_identity != NULL )
1633 {
1634 mbedtls_free( conf->psk_identity );
1635 conf->psk_identity = NULL;
1636 conf->psk_identity_len = 0;
1637 }
1638}
1639
Hanno Becker7390c712018-11-15 13:33:04 +00001640/* This function assumes that PSK identity in the SSL config is unset.
1641 * It checks that the provided identity is well-formed and attempts
1642 * to make a copy of it in the SSL config.
1643 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001644MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001645static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1646 unsigned char const *psk_identity,
1647 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001648{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001649 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001650 if( psk_identity == NULL ||
1651 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001652 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001653 {
1654 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1655 }
1656
Hanno Becker7390c712018-11-15 13:33:04 +00001657 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1658 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001659 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001660
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001661 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001662 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001663
1664 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001665}
1666
Hanno Becker7390c712018-11-15 13:33:04 +00001667int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1668 const unsigned char *psk, size_t psk_len,
1669 const unsigned char *psk_identity, size_t psk_identity_len )
1670{
Janos Follath865b3eb2019-12-16 11:46:15 +00001671 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001672
1673 /* We currently only support one PSK, raw or opaque. */
1674 if( ssl_conf_psk_is_configured( conf ) )
1675 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001676
1677 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001678 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001679 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001680 if( psk_len == 0 )
1681 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1682 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1683 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1684
Hanno Becker7390c712018-11-15 13:33:04 +00001685 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1686 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1687 conf->psk_len = psk_len;
1688 memcpy( conf->psk, psk, conf->psk_len );
1689
1690 /* Check and set PSK Identity */
1691 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1692 if( ret != 0 )
1693 ssl_conf_remove_psk( conf );
1694
1695 return( ret );
1696}
1697
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001698static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1699{
1700#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001701 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001702 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001703 /* The maintenance of the external PSK key slot is the
1704 * user's responsibility. */
1705 if( ssl->handshake->psk_opaque_is_internal )
1706 {
1707 psa_destroy_key( ssl->handshake->psk_opaque );
1708 ssl->handshake->psk_opaque_is_internal = 0;
1709 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001710 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001711 }
Neil Armstronge952a302022-05-03 10:22:14 +02001712#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001713 if( ssl->handshake->psk != NULL )
1714 {
1715 mbedtls_platform_zeroize( ssl->handshake->psk,
1716 ssl->handshake->psk_len );
1717 mbedtls_free( ssl->handshake->psk );
1718 ssl->handshake->psk_len = 0;
1719 }
Neil Armstronge952a302022-05-03 10:22:14 +02001720#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001721}
1722
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001723int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1724 const unsigned char *psk, size_t psk_len )
1725{
Neil Armstrong501c9322022-05-03 09:35:09 +02001726#if defined(MBEDTLS_USE_PSA_CRYPTO)
1727 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001728 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001729 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001730 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001731#endif /* MBEDTLS_USE_PSA_CRYPTO */
1732
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001733 if( psk == NULL || ssl->handshake == NULL )
1734 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1735
1736 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1737 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1738
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001739 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001740
Neil Armstrong501c9322022-05-03 09:35:09 +02001741#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001742#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1743 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1744 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001745 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001746 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1747 else
1748 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1749 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1750 }
1751#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001752
Jerry Yu568ec252022-07-22 21:27:34 +08001753#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001754 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1755 {
1756 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1757 psa_set_key_usage_flags( &key_attributes,
1758 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1759 }
1760#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1761
Neil Armstrong501c9322022-05-03 09:35:09 +02001762 psa_set_key_algorithm( &key_attributes, alg );
1763 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1764
1765 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1766 if( status != PSA_SUCCESS )
1767 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1768
1769 /* Allow calling psa_destroy_key() on psk remove */
1770 ssl->handshake->psk_opaque_is_internal = 1;
1771 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1772#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001773 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001774 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001775
1776 ssl->handshake->psk_len = psk_len;
1777 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1778
1779 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001780#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001781}
1782
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001783#if defined(MBEDTLS_USE_PSA_CRYPTO)
1784int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001785 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001786 const unsigned char *psk_identity,
1787 size_t psk_identity_len )
1788{
Janos Follath865b3eb2019-12-16 11:46:15 +00001789 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001790
1791 /* We currently only support one PSK, raw or opaque. */
1792 if( ssl_conf_psk_is_configured( conf ) )
1793 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001794
Hanno Becker7390c712018-11-15 13:33:04 +00001795 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001796 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001797 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001798 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001799
1800 /* Check and set PSK Identity */
1801 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1802 psk_identity_len );
1803 if( ret != 0 )
1804 ssl_conf_remove_psk( conf );
1805
1806 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001807}
1808
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001809int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1810 mbedtls_svc_key_id_t psk )
1811{
1812 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1813 ( ssl->handshake == NULL ) )
1814 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1815
1816 ssl_remove_psk( ssl );
1817 ssl->handshake->psk_opaque = psk;
1818 return( 0 );
1819}
1820#endif /* MBEDTLS_USE_PSA_CRYPTO */
1821
Jerry Yu8897c072022-08-12 13:56:53 +08001822#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001823void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1824 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1825 size_t),
1826 void *p_psk )
1827{
1828 conf->f_psk = f_psk;
1829 conf->p_psk = p_psk;
1830}
Jerry Yu8897c072022-08-12 13:56:53 +08001831#endif /* MBEDTLS_SSL_SRV_C */
1832
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001833#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1834
1835#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001836static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1837 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001838{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001839#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001840 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001841 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001842#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001843 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001844 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001845 return( MBEDTLS_SSL_MODE_STREAM );
1846}
1847
1848#else /* MBEDTLS_USE_PSA_CRYPTO */
1849
1850static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1851 mbedtls_cipher_mode_t mode )
1852{
1853#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1854 if( mode == MBEDTLS_MODE_CBC )
1855 return( MBEDTLS_SSL_MODE_CBC );
1856#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1857
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001858#if defined(MBEDTLS_GCM_C) || \
1859 defined(MBEDTLS_CCM_C) || \
1860 defined(MBEDTLS_CHACHAPOLY_C)
1861 if( mode == MBEDTLS_MODE_GCM ||
1862 mode == MBEDTLS_MODE_CCM ||
1863 mode == MBEDTLS_MODE_CHACHAPOLY )
1864 return( MBEDTLS_SSL_MODE_AEAD );
1865#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001866
1867 return( MBEDTLS_SSL_MODE_STREAM );
1868}
Gilles Peskine301711e2022-04-26 16:57:05 +02001869#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001870
Gilles Peskinee108d982022-04-26 16:50:40 +02001871static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1872 mbedtls_ssl_mode_t base_mode,
1873 int encrypt_then_mac )
1874{
1875#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1876 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1877 base_mode == MBEDTLS_SSL_MODE_CBC )
1878 {
1879 return( MBEDTLS_SSL_MODE_CBC_ETM );
1880 }
1881#else
1882 (void) encrypt_then_mac;
1883#endif
1884 return( base_mode );
1885}
1886
Neil Armstrongab555e02022-04-04 11:07:59 +02001887mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001888 const mbedtls_ssl_transform *transform )
1889{
Gilles Peskinee108d982022-04-26 16:50:40 +02001890 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001891#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001892 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001893#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001894 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1895#endif
1896 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001897
Gilles Peskinee108d982022-04-26 16:50:40 +02001898 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001899#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001900 encrypt_then_mac = transform->encrypt_then_mac;
1901#endif
1902 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001903}
1904
Neil Armstrongab555e02022-04-04 11:07:59 +02001905mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001906#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001907 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001908#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001909 const mbedtls_ssl_ciphersuite_t *suite )
1910{
Gilles Peskinee108d982022-04-26 16:50:40 +02001911 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1912
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001913#if defined(MBEDTLS_USE_PSA_CRYPTO)
1914 psa_status_t status;
1915 psa_algorithm_t alg;
1916 psa_key_type_t type;
1917 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001918 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1919 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001920 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001921#else
1922 const mbedtls_cipher_info_t *cipher =
1923 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001924 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001925 {
1926 base_mode =
1927 mbedtls_ssl_get_base_mode(
1928 mbedtls_cipher_info_get_mode( cipher ) );
1929 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001930#endif /* MBEDTLS_USE_PSA_CRYPTO */
1931
Gilles Peskinee108d982022-04-26 16:50:40 +02001932#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1933 int encrypt_then_mac = 0;
1934#endif
1935 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001936}
1937
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001938#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001939
1940#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001941/* Serialization of TLS 1.3 sessions:
1942 *
1943 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00001944 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001945 * uint64 ticket_received;
1946 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08001947 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001948 * } ClientOnlyData;
1949 *
1950 * struct {
1951 * uint8 endpoint;
1952 * uint8 ciphersuite[2];
1953 * uint32 ticket_age_add;
1954 * uint8 ticket_flags;
1955 * opaque resumption_key<0..255>;
1956 * select ( endpoint ) {
1957 * case client: ClientOnlyData;
1958 * case server: uint64 start_time;
1959 * };
1960 * } serialized_session_tls13;
1961 *
1962 */
1963#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08001964MBEDTLS_CHECK_RETURN_CRITICAL
1965static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
1966 unsigned char *buf,
1967 size_t buf_len,
1968 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00001969{
1970 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00001971#if defined(MBEDTLS_SSL_CLI_C) && \
1972 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1973 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00001974 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00001975#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08001976 size_t needed = 1 /* endpoint */
1977 + 2 /* ciphersuite */
1978 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08001979 + 1 /* ticket_flags */
1980 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08001981 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08001982
1983 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
1984 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1985 needed += session->resumption_key_len; /* resumption_key */
1986
Jerry Yu438ddd82022-07-07 06:55:50 +00001987#if defined(MBEDTLS_HAVE_TIME)
1988 needed += 8; /* start_time or ticket_received */
1989#endif
1990
1991#if defined(MBEDTLS_SSL_CLI_C)
1992 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1993 {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00001994#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00001995 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00001996 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00001997#endif
1998
Jerry Yu438ddd82022-07-07 06:55:50 +00001999 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002000 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002001
2002 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002003 if( session->ticket_len > SIZE_MAX - needed )
2004 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002005
Jerry Yue28d9742022-08-18 15:44:03 +08002006 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002007 }
2008#endif /* MBEDTLS_SSL_CLI_C */
2009
Jerry Yue36fdd62022-08-17 21:31:36 +08002010 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002011 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002012 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002013
2014 p[0] = session->endpoint;
2015 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2016 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2017 p[7] = session->ticket_flags;
2018
2019 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002020 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002021 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002022 memcpy( p, session->resumption_key, session->resumption_key_len );
2023 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002024
2025#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2026 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2027 {
2028 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2029 p += 8;
2030 }
2031#endif /* MBEDTLS_HAVE_TIME */
2032
2033#if defined(MBEDTLS_SSL_CLI_C)
2034 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2035 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002036#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2037 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2038 p += 2;
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002039 if( hostname_len > 0 )
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002040 {
2041 /* save host name */
2042 memcpy( p, session->hostname, hostname_len );
2043 p += hostname_len;
2044 }
2045#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2046
Jerry Yu438ddd82022-07-07 06:55:50 +00002047#if defined(MBEDTLS_HAVE_TIME)
2048 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2049 p += 8;
2050#endif
2051 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2052 p += 4;
2053
2054 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2055 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002056
2057 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002058 {
2059 memcpy( p, session->ticket, session->ticket_len );
2060 p += session->ticket_len;
2061 }
2062 }
2063#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002064 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002065}
2066
2067MBEDTLS_CHECK_RETURN_CRITICAL
2068static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2069 const unsigned char *buf,
2070 size_t len )
2071{
2072 const unsigned char *p = buf;
2073 const unsigned char *end = buf + len;
2074
2075 if( end - p < 9 )
2076 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2077 session->endpoint = p[0];
2078 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2079 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2080 session->ticket_flags = p[7];
2081
2082 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002083 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002084 p += 9;
2085
Jerry Yubc7c1a42022-07-21 22:57:37 +08002086 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002087 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2088
Jerry Yubc7c1a42022-07-21 22:57:37 +08002089 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002090 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002091 memcpy( session->resumption_key, p, session->resumption_key_len );
2092 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002093
2094#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2095 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2096 {
2097 if( end - p < 8 )
2098 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2099 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2100 p += 8;
2101 }
2102#endif /* MBEDTLS_HAVE_TIME */
2103
2104#if defined(MBEDTLS_SSL_CLI_C)
2105 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2106 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002107#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2108 defined(MBEDTLS_SSL_SESSION_TICKETS)
2109 size_t hostname_len;
2110 /* load host name */
2111 if( end - p < 2 )
2112 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2113 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2114 p += 2;
2115
2116 if( end - p < ( long int )hostname_len )
2117 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2118 if( hostname_len > 0 )
2119 {
2120 session->hostname = mbedtls_calloc( 1, hostname_len );
2121 if( session->hostname == NULL )
2122 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2123 memcpy( session->hostname, p, hostname_len );
2124 p += hostname_len;
2125 }
2126#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2127 MBEDTLS_SSL_SESSION_TICKETS */
2128
Jerry Yu438ddd82022-07-07 06:55:50 +00002129#if defined(MBEDTLS_HAVE_TIME)
2130 if( end - p < 8 )
2131 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2132 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2133 p += 8;
2134#endif
2135 if( end - p < 4 )
2136 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2137 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2138 p += 4;
2139
2140 if( end - p < 2 )
2141 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2142 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2143 p += 2;
2144
2145 if( end - p < ( long int )session->ticket_len )
2146 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2147 if( session->ticket_len > 0 )
2148 {
2149 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2150 if( session->ticket == NULL )
2151 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2152 memcpy( session->ticket, p, session->ticket_len );
2153 p += session->ticket_len;
2154 }
2155 }
2156#endif /* MBEDTLS_SSL_CLI_C */
2157
2158 return( 0 );
2159
2160}
2161#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002162MBEDTLS_CHECK_RETURN_CRITICAL
2163static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2164 unsigned char *buf,
2165 size_t buf_len,
2166 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002167{
2168 ((void) session);
2169 ((void) buf);
2170 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002171 *olen = 0;
2172 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002173}
Jerry Yu438ddd82022-07-07 06:55:50 +00002174
2175static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2176 unsigned char *buf,
2177 size_t buf_len )
2178{
2179 ((void) session);
2180 ((void) buf);
2181 ((void) buf_len);
2182 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2183}
2184#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002185#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2186
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002187psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002188 size_t taglen,
2189 psa_algorithm_t *alg,
2190 psa_key_type_t *key_type,
2191 size_t *key_size )
2192{
2193 switch ( mbedtls_cipher_type )
2194 {
2195 case MBEDTLS_CIPHER_AES_128_CBC:
2196 *alg = PSA_ALG_CBC_NO_PADDING;
2197 *key_type = PSA_KEY_TYPE_AES;
2198 *key_size = 128;
2199 break;
2200 case MBEDTLS_CIPHER_AES_128_CCM:
2201 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2202 *key_type = PSA_KEY_TYPE_AES;
2203 *key_size = 128;
2204 break;
2205 case MBEDTLS_CIPHER_AES_128_GCM:
2206 *alg = PSA_ALG_GCM;
2207 *key_type = PSA_KEY_TYPE_AES;
2208 *key_size = 128;
2209 break;
2210 case MBEDTLS_CIPHER_AES_192_CCM:
2211 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2212 *key_type = PSA_KEY_TYPE_AES;
2213 *key_size = 192;
2214 break;
2215 case MBEDTLS_CIPHER_AES_192_GCM:
2216 *alg = PSA_ALG_GCM;
2217 *key_type = PSA_KEY_TYPE_AES;
2218 *key_size = 192;
2219 break;
2220 case MBEDTLS_CIPHER_AES_256_CBC:
2221 *alg = PSA_ALG_CBC_NO_PADDING;
2222 *key_type = PSA_KEY_TYPE_AES;
2223 *key_size = 256;
2224 break;
2225 case MBEDTLS_CIPHER_AES_256_CCM:
2226 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2227 *key_type = PSA_KEY_TYPE_AES;
2228 *key_size = 256;
2229 break;
2230 case MBEDTLS_CIPHER_AES_256_GCM:
2231 *alg = PSA_ALG_GCM;
2232 *key_type = PSA_KEY_TYPE_AES;
2233 *key_size = 256;
2234 break;
2235 case MBEDTLS_CIPHER_ARIA_128_CBC:
2236 *alg = PSA_ALG_CBC_NO_PADDING;
2237 *key_type = PSA_KEY_TYPE_ARIA;
2238 *key_size = 128;
2239 break;
2240 case MBEDTLS_CIPHER_ARIA_128_CCM:
2241 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2242 *key_type = PSA_KEY_TYPE_ARIA;
2243 *key_size = 128;
2244 break;
2245 case MBEDTLS_CIPHER_ARIA_128_GCM:
2246 *alg = PSA_ALG_GCM;
2247 *key_type = PSA_KEY_TYPE_ARIA;
2248 *key_size = 128;
2249 break;
2250 case MBEDTLS_CIPHER_ARIA_192_CCM:
2251 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2252 *key_type = PSA_KEY_TYPE_ARIA;
2253 *key_size = 192;
2254 break;
2255 case MBEDTLS_CIPHER_ARIA_192_GCM:
2256 *alg = PSA_ALG_GCM;
2257 *key_type = PSA_KEY_TYPE_ARIA;
2258 *key_size = 192;
2259 break;
2260 case MBEDTLS_CIPHER_ARIA_256_CBC:
2261 *alg = PSA_ALG_CBC_NO_PADDING;
2262 *key_type = PSA_KEY_TYPE_ARIA;
2263 *key_size = 256;
2264 break;
2265 case MBEDTLS_CIPHER_ARIA_256_CCM:
2266 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2267 *key_type = PSA_KEY_TYPE_ARIA;
2268 *key_size = 256;
2269 break;
2270 case MBEDTLS_CIPHER_ARIA_256_GCM:
2271 *alg = PSA_ALG_GCM;
2272 *key_type = PSA_KEY_TYPE_ARIA;
2273 *key_size = 256;
2274 break;
2275 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2276 *alg = PSA_ALG_CBC_NO_PADDING;
2277 *key_type = PSA_KEY_TYPE_CAMELLIA;
2278 *key_size = 128;
2279 break;
2280 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2281 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2282 *key_type = PSA_KEY_TYPE_CAMELLIA;
2283 *key_size = 128;
2284 break;
2285 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2286 *alg = PSA_ALG_GCM;
2287 *key_type = PSA_KEY_TYPE_CAMELLIA;
2288 *key_size = 128;
2289 break;
2290 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2291 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2292 *key_type = PSA_KEY_TYPE_CAMELLIA;
2293 *key_size = 192;
2294 break;
2295 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2296 *alg = PSA_ALG_GCM;
2297 *key_type = PSA_KEY_TYPE_CAMELLIA;
2298 *key_size = 192;
2299 break;
2300 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2301 *alg = PSA_ALG_CBC_NO_PADDING;
2302 *key_type = PSA_KEY_TYPE_CAMELLIA;
2303 *key_size = 256;
2304 break;
2305 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2306 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2307 *key_type = PSA_KEY_TYPE_CAMELLIA;
2308 *key_size = 256;
2309 break;
2310 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2311 *alg = PSA_ALG_GCM;
2312 *key_type = PSA_KEY_TYPE_CAMELLIA;
2313 *key_size = 256;
2314 break;
2315 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2316 *alg = PSA_ALG_CHACHA20_POLY1305;
2317 *key_type = PSA_KEY_TYPE_CHACHA20;
2318 *key_size = 256;
2319 break;
2320 case MBEDTLS_CIPHER_NULL:
2321 *alg = MBEDTLS_SSL_NULL_CIPHER;
2322 *key_type = 0;
2323 *key_size = 0;
2324 break;
2325 default:
2326 return PSA_ERROR_NOT_SUPPORTED;
2327 }
2328
2329 return PSA_SUCCESS;
2330}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002331#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002332
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002333#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002334int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2335 const unsigned char *dhm_P, size_t P_len,
2336 const unsigned char *dhm_G, size_t G_len )
2337{
Janos Follath865b3eb2019-12-16 11:46:15 +00002338 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002339
Glenn Strausscee11292021-12-20 01:43:17 -05002340 mbedtls_mpi_free( &conf->dhm_P );
2341 mbedtls_mpi_free( &conf->dhm_G );
2342
Hanno Beckera90658f2017-10-04 15:29:08 +01002343 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2344 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2345 {
2346 mbedtls_mpi_free( &conf->dhm_P );
2347 mbedtls_mpi_free( &conf->dhm_G );
2348 return( ret );
2349 }
2350
2351 return( 0 );
2352}
Paul Bakker5121ce52009-01-03 21:22:43 +00002353
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002354int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002355{
Janos Follath865b3eb2019-12-16 11:46:15 +00002356 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002357
Glenn Strausscee11292021-12-20 01:43:17 -05002358 mbedtls_mpi_free( &conf->dhm_P );
2359 mbedtls_mpi_free( &conf->dhm_G );
2360
Gilles Peskinee5702482021-06-11 21:59:08 +02002361 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2362 &conf->dhm_P ) ) != 0 ||
2363 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2364 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002365 {
2366 mbedtls_mpi_free( &conf->dhm_P );
2367 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002368 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002369 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002370
2371 return( 0 );
2372}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002373#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002374
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002375#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2376/*
2377 * Set the minimum length for Diffie-Hellman parameters
2378 */
2379void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2380 unsigned int bitlen )
2381{
2382 conf->dhm_min_bitlen = bitlen;
2383}
2384#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2385
Gilles Peskineeccd8882020-03-10 12:19:08 +01002386#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002387#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002388/*
2389 * Set allowed/preferred hashes for handshake signatures
2390 */
2391void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2392 const int *hashes )
2393{
2394 conf->sig_hashes = hashes;
2395}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002396#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002397
Jerry Yuf017ee42022-01-12 15:49:48 +08002398/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002399void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2400 const uint16_t* sig_algs )
2401{
Jerry Yuf017ee42022-01-12 15:49:48 +08002402#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2403 conf->sig_hashes = NULL;
2404#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2405 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002406}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002407#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002408
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002409#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002410#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002411/*
2412 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002413 *
2414 * mbedtls_ssl_setup() takes the provided list
2415 * and translates it to a list of IANA TLS group identifiers,
2416 * stored in ssl->handshake->group_list.
2417 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002418 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002419void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002420 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002421{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002422 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002423 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002424}
Brett Warrene0edc842021-08-17 09:53:13 +01002425#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002426#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002427
Brett Warrene0edc842021-08-17 09:53:13 +01002428/*
2429 * Set the allowed groups
2430 */
2431void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2432 const uint16_t *group_list )
2433{
2434#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2435 conf->curve_list = NULL;
2436#endif
2437 conf->group_list = group_list;
2438}
2439
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002440#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002442{
Hanno Becker947194e2017-04-07 13:25:49 +01002443 /* Initialize to suppress unnecessary compiler warning */
2444 size_t hostname_len = 0;
2445
2446 /* Check if new hostname is valid before
2447 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002448 if( hostname != NULL )
2449 {
2450 hostname_len = strlen( hostname );
2451
2452 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2453 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2454 }
2455
2456 /* Now it's clear that we will overwrite the old hostname,
2457 * so we can free it safely */
2458
2459 if( ssl->hostname != NULL )
2460 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002461 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002462 mbedtls_free( ssl->hostname );
2463 }
2464
2465 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002466
Paul Bakker5121ce52009-01-03 21:22:43 +00002467 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002468 {
2469 ssl->hostname = NULL;
2470 }
2471 else
2472 {
2473 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002474 if( ssl->hostname == NULL )
2475 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002476
Hanno Becker947194e2017-04-07 13:25:49 +01002477 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002478
Hanno Becker947194e2017-04-07 13:25:49 +01002479 ssl->hostname[hostname_len] = '\0';
2480 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002481
2482 return( 0 );
2483}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002484#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002485
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002486#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002487void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002489 const unsigned char *, size_t),
2490 void *p_sni )
2491{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002492 conf->f_sni = f_sni;
2493 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002494}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002498int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002499{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002500 size_t cur_len, tot_len;
2501 const char **p;
2502
2503 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002504 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2505 * MUST NOT be truncated."
2506 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002507 */
2508 tot_len = 0;
2509 for( p = protos; *p != NULL; p++ )
2510 {
2511 cur_len = strlen( *p );
2512 tot_len += cur_len;
2513
Ronald Cron8216dd32020-04-23 16:41:44 +02002514 if( ( cur_len == 0 ) ||
2515 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2516 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002518 }
2519
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002520 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002521
2522 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002523}
2524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002526{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002527 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002528}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002530
Johan Pascalb62bb512015-12-03 21:56:45 +01002531#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002532void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2533 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002534{
2535 conf->dtls_srtp_mki_support = support_mki_value;
2536}
2537
Ron Eldoref72faf2018-07-12 11:54:20 +03002538int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2539 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002540 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002541{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002542 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002543 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002544 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002545 }
Ron Eldor591f1622018-01-22 12:30:04 +02002546
2547 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002548 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002549 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002550 }
Ron Eldor591f1622018-01-22 12:30:04 +02002551
2552 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2553 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002554 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002555}
2556
Ron Eldoref72faf2018-07-12 11:54:20 +03002557int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002558 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002559{
Johan Pascal253d0262020-09-22 13:04:45 +02002560 const mbedtls_ssl_srtp_profile *p;
2561 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002562
Johan Pascal253d0262020-09-22 13:04:45 +02002563 /* check the profiles list: all entry must be valid,
2564 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002565 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2566 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2567 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002568 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002569 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002570 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002571 list_size++;
2572 }
2573 else
2574 {
2575 /* unsupported value, stop parsing and set the size to an error value */
2576 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002577 }
2578 }
2579
Johan Pascal5ef72d22020-10-28 17:05:47 +01002580 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002581 {
Johan Pascal253d0262020-09-22 13:04:45 +02002582 conf->dtls_srtp_profile_list = NULL;
2583 conf->dtls_srtp_profile_list_len = 0;
2584 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2585 }
2586
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002587 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002588 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002589
2590 return( 0 );
2591}
2592
Johan Pascal5ef72d22020-10-28 17:05:47 +01002593void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2594 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002595{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002596 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2597 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002598 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002599 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002600 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002601 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002602 else
2603 {
2604 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002605 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2606 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002607 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002608}
Johan Pascalb62bb512015-12-03 21:56:45 +01002609#endif /* MBEDTLS_SSL_DTLS_SRTP */
2610
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302611#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002612void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002613{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002614 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002615}
2616
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002617void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002618{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002619 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002620}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302621#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002622
Janos Follath088ce432017-04-10 12:42:31 +01002623#if defined(MBEDTLS_SSL_SRV_C)
2624void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2625 char cert_req_ca_list )
2626{
2627 conf->cert_req_ca_list = cert_req_ca_list;
2628}
2629#endif
2630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002632void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002633{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002634 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002635}
2636#endif
2637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002639void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002640{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002641 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002642}
2643#endif
2644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002646int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002647{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002649 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002652 }
2653
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002654 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002655
2656 return( 0 );
2657}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002659
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002660void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002661{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002662 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002663}
2664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002666void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002667{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002668 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002669}
2670
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002671void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002672{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002673 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002674}
2675
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002676void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002677 const unsigned char period[8] )
2678{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002679 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002680}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002684#if defined(MBEDTLS_SSL_CLI_C)
2685void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002686{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002687 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002688}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002689#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002690
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002691#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002692
Jerry Yud0766ec2022-09-22 10:46:57 +08002693#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002694void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2695 uint16_t num_tickets )
2696{
Jerry Yud0766ec2022-09-22 10:46:57 +08002697 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002698}
2699#endif
2700
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002701void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2702 mbedtls_ssl_ticket_write_t *f_ticket_write,
2703 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2704 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002705{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002706 conf->f_ticket_write = f_ticket_write;
2707 conf->f_ticket_parse = f_ticket_parse;
2708 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002709}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002710#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002712
Hanno Becker7e6c1782021-06-08 09:24:55 +01002713void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2714 mbedtls_ssl_export_keys_t *f_export_keys,
2715 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002716{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002717 ssl->f_export_keys = f_export_keys;
2718 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002719}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002720
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002721#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002722void mbedtls_ssl_conf_async_private_cb(
2723 mbedtls_ssl_config *conf,
2724 mbedtls_ssl_async_sign_t *f_async_sign,
2725 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2726 mbedtls_ssl_async_resume_t *f_async_resume,
2727 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002728 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002729{
2730 conf->f_async_sign_start = f_async_sign;
2731 conf->f_async_decrypt_start = f_async_decrypt;
2732 conf->f_async_resume = f_async_resume;
2733 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002734 conf->p_async_config_data = async_config_data;
2735}
2736
Gilles Peskine8f97af72018-04-26 11:46:10 +02002737void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2738{
2739 return( conf->p_async_config_data );
2740}
2741
Gilles Peskine1febfef2018-04-30 11:54:39 +02002742void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002743{
2744 if( ssl->handshake == NULL )
2745 return( NULL );
2746 else
2747 return( ssl->handshake->user_async_ctx );
2748}
2749
Gilles Peskine1febfef2018-04-30 11:54:39 +02002750void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002751 void *ctx )
2752{
2753 if( ssl->handshake != NULL )
2754 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002755}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002756#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002757
Paul Bakker5121ce52009-01-03 21:22:43 +00002758/*
2759 * SSL get accessors
2760 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002761uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002762{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002763 if( ssl->session != NULL )
2764 return( ssl->session->verify_result );
2765
2766 if( ssl->session_negotiate != NULL )
2767 return( ssl->session_negotiate->verify_result );
2768
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002769 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002770}
2771
Glenn Strauss8f526902022-01-13 00:04:49 -05002772int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2773{
2774 if( ssl == NULL || ssl->session == NULL )
2775 return( 0 );
2776
2777 return( ssl->session->ciphersuite );
2778}
2779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002781{
Paul Bakker926c8e42013-03-06 10:23:34 +01002782 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002783 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002786}
2787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002789{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002791 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002792 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002793 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002794 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002795 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002796 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002797 default:
2798 return( "unknown (DTLS)" );
2799 }
2800 }
2801#endif
2802
Glenn Strauss60bfe602022-03-14 19:04:24 -04002803 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002804 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002805 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002806 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002807 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002808 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002809 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002810 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002811 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002812}
2813
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002814#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002815size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2816{
David Horstmann95d516f2021-05-04 18:36:56 +01002817 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002818 size_t read_mfl;
2819
2820 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2821 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2822 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2823 {
2824 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2825 }
2826
2827 /* Check if a smaller max length was negotiated */
2828 if( ssl->session_out != NULL )
2829 {
2830 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2831 if( read_mfl < max_len )
2832 {
2833 max_len = read_mfl;
2834 }
2835 }
2836
2837 // During a handshake, use the value being negotiated
2838 if( ssl->session_negotiate != NULL )
2839 {
2840 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2841 if( read_mfl < max_len )
2842 {
2843 max_len = read_mfl;
2844 }
2845 }
2846
2847 return( max_len );
2848}
2849
2850size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002851{
2852 size_t max_len;
2853
2854 /*
2855 * Assume mfl_code is correct since it was checked when set
2856 */
Angus Grattond8213d02016-05-25 20:56:48 +10002857 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002858
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002859 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002860 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002861 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002862 {
Angus Grattond8213d02016-05-25 20:56:48 +10002863 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002864 }
2865
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002866 /* During a handshake, use the value being negotiated */
2867 if( ssl->session_negotiate != NULL &&
2868 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2869 {
2870 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2871 }
2872
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002873 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002874}
2875#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2876
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002877#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002878size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002879{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002880 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2881 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2882 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2883 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2884 return ( 0 );
2885
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002886 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2887 return( ssl->mtu );
2888
2889 if( ssl->mtu == 0 )
2890 return( ssl->handshake->mtu );
2891
2892 return( ssl->mtu < ssl->handshake->mtu ?
2893 ssl->mtu : ssl->handshake->mtu );
2894}
2895#endif /* MBEDTLS_SSL_PROTO_DTLS */
2896
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002897int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2898{
2899 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2900
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002901#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2902 !defined(MBEDTLS_SSL_PROTO_DTLS)
2903 (void) ssl;
2904#endif
2905
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002906#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002907 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002908
2909 if( max_len > mfl )
2910 max_len = mfl;
2911#endif
2912
2913#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002914 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002915 {
Hanno Becker89490712020-02-05 10:50:12 +00002916 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002917 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2918 const size_t overhead = (size_t) ret;
2919
2920 if( ret < 0 )
2921 return( ret );
2922
2923 if( mtu <= overhead )
2924 {
2925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2926 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2927 }
2928
2929 if( max_len > mtu - overhead )
2930 max_len = mtu - overhead;
2931 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002932#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002933
Hanno Becker0defedb2018-08-10 12:35:02 +01002934#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2935 !defined(MBEDTLS_SSL_PROTO_DTLS)
2936 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002937#endif
2938
2939 return( (int) max_len );
2940}
2941
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002942int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2943{
2944 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2945
2946#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2947 (void) ssl;
2948#endif
2949
2950#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2951 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2952
2953 if( max_len > mfl )
2954 max_len = mfl;
2955#endif
2956
2957 return( (int) max_len );
2958}
2959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960#if defined(MBEDTLS_X509_CRT_PARSE_C)
2961const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002962{
2963 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002964 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002965
Hanno Beckere6824572019-02-07 13:18:46 +00002966#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002967 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002968#else
2969 return( NULL );
2970#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002971}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002975int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2976 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002977{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002978 int ret;
2979
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002980 if( ssl == NULL ||
2981 dst == NULL ||
2982 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002983 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002986 }
2987
Hanno Beckere810bbc2021-05-14 16:01:05 +01002988 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2989 * idempotent: Each session can only be exported once.
2990 *
2991 * (This is in preparation for TLS 1.3 support where we will
2992 * need the ability to export multiple sessions (aka tickets),
2993 * which will be achieved by calling mbedtls_ssl_get_session()
2994 * multiple times until it fails.)
2995 *
2996 * Check whether we have already exported the current session,
2997 * and fail if so.
2998 */
2999 if( ssl->session->exported == 1 )
3000 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3001
3002 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3003 if( ret != 0 )
3004 return( ret );
3005
3006 /* Remember that we've exported the session. */
3007 ssl->session->exported = 1;
3008 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003009}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003011
Paul Bakker5121ce52009-01-03 21:22:43 +00003012/*
Hanno Beckera835da52019-05-16 12:39:07 +01003013 * Define ticket header determining Mbed TLS version
3014 * and structure of the ticket.
3015 */
3016
Hanno Becker94ef3b32019-05-16 12:50:45 +01003017/*
Hanno Becker50b59662019-05-28 14:30:45 +01003018 * Define bitflag determining compile-time settings influencing
3019 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003020 */
3021
Hanno Becker50b59662019-05-28 14:30:45 +01003022#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003023#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003024#else
Hanno Becker3e088662019-05-29 11:10:18 +01003025#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003026#endif /* MBEDTLS_HAVE_TIME */
3027
3028#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003029#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003030#else
Hanno Becker3e088662019-05-29 11:10:18 +01003031#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003032#endif /* MBEDTLS_X509_CRT_PARSE_C */
3033
3034#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003035#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003036#else
Hanno Becker3e088662019-05-29 11:10:18 +01003037#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003038#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3039
3040#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003041#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003042#else
Hanno Becker3e088662019-05-29 11:10:18 +01003043#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003044#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3045
Hanno Becker94ef3b32019-05-16 12:50:45 +01003046#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003047#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003048#else
Hanno Becker3e088662019-05-29 11:10:18 +01003049#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003050#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3051
Hanno Becker94ef3b32019-05-16 12:50:45 +01003052#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3053#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3054#else
3055#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3056#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3057
Hanno Becker3e088662019-05-29 11:10:18 +01003058#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3059#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3060#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3061#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003062#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3063#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003064
Hanno Becker50b59662019-05-28 14:30:45 +01003065#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003066 ( (uint16_t) ( \
3067 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3068 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3069 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3070 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003071 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003072 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003073
Hanno Beckerf8787072019-05-16 12:41:07 +01003074static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003075 MBEDTLS_VERSION_MAJOR,
3076 MBEDTLS_VERSION_MINOR,
3077 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003078 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3079 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003080};
Hanno Beckera835da52019-05-16 12:39:07 +01003081
3082/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003083 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003084 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003085 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003086 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003087 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003088 * opaque mbedtls_version[3]; // library version: major, minor, patch
3089 * opaque session_format[2]; // library-version specific 16-bit field
3090 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003091 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003092 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003093 * Note: When updating the format, remember to keep
3094 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003095 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003096 * // In this version, `session_format` determines
3097 * // the setting of those compile-time
3098 * // configuration options which influence
3099 * // the structure of mbedtls_ssl_session.
3100 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003101 * uint8_t minor_ver; // Protocol minor version. Possible values:
3102 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003103 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003104 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003105 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003106 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003107 * serialized_session_tls12 data;
3108 *
3109 * };
3110 *
3111 * } serialized_session;
3112 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003113 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003114
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003115MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003116static int ssl_session_save( const mbedtls_ssl_session *session,
3117 unsigned char omit_header,
3118 unsigned char *buf,
3119 size_t buf_len,
3120 size_t *olen )
3121{
3122 unsigned char *p = buf;
3123 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003124 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003125#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3126 size_t out_len;
3127 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3128#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003129 if( session == NULL )
3130 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3131
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003132 if( !omit_header )
3133 {
3134 /*
3135 * Add Mbed TLS version identifier
3136 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003137 used += sizeof( ssl_serialized_session_header );
3138
3139 if( used <= buf_len )
3140 {
3141 memcpy( p, ssl_serialized_session_header,
3142 sizeof( ssl_serialized_session_header ) );
3143 p += sizeof( ssl_serialized_session_header );
3144 }
3145 }
3146
3147 /*
3148 * TLS version identifier
3149 */
3150 used += 1;
3151 if( used <= buf_len )
3152 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003153 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003154 }
3155
3156 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003157 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003158 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003159 {
3160#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003161 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003162 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003163 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003164#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3165
Jerry Yu251a12e2022-07-13 15:15:48 +08003166#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3167 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003168 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3169 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003170 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003171 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003172 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003173#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3174
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003175 default:
3176 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3177 }
3178
3179 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003180 if( used > buf_len )
3181 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003182
3183 return( 0 );
3184}
3185
3186/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003187 * Public wrapper for ssl_session_save()
3188 */
3189int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3190 unsigned char *buf,
3191 size_t buf_len,
3192 size_t *olen )
3193{
3194 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3195}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003196
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003197/*
3198 * Deserialize session, see mbedtls_ssl_session_save() for format.
3199 *
3200 * This internal version is wrapped by a public function that cleans up in
3201 * case of error, and has an extra option omit_header.
3202 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003203MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003204static int ssl_session_load( mbedtls_ssl_session *session,
3205 unsigned char omit_header,
3206 const unsigned char *buf,
3207 size_t len )
3208{
3209 const unsigned char *p = buf;
3210 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003211 size_t remaining_len;
3212
3213
3214 if( session == NULL )
3215 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003216
3217 if( !omit_header )
3218 {
3219 /*
3220 * Check Mbed TLS version identifier
3221 */
3222
3223 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3224 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3225
3226 if( memcmp( p, ssl_serialized_session_header,
3227 sizeof( ssl_serialized_session_header ) ) != 0 )
3228 {
3229 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3230 }
3231 p += sizeof( ssl_serialized_session_header );
3232 }
3233
3234 /*
3235 * TLS version identifier
3236 */
3237 if( 1 > (size_t)( end - p ) )
3238 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003239 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003240
3241 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003242 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003243 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003244 {
3245#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003246 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003247 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003248#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3249
Jerry Yu438ddd82022-07-07 06:55:50 +00003250#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3251 case MBEDTLS_SSL_VERSION_TLS1_3:
3252 return( ssl_tls13_session_load( session, p, remaining_len ) );
3253#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3254
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003255 default:
3256 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3257 }
3258}
3259
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003260/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003261 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003262 */
3263int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3264 const unsigned char *buf,
3265 size_t len )
3266{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003267 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003268
3269 if( ret != 0 )
3270 mbedtls_ssl_session_free( session );
3271
3272 return( ret );
3273}
3274
3275/*
Paul Bakker1961b702013-01-25 14:49:24 +01003276 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003277 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003278MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003279static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3280{
3281 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3282
Ronald Cron66dbf912022-02-02 15:33:46 +01003283 /*
3284 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003285 * that were written into the output buffer by the previous handshake step,
3286 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003287 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3288 * We proceed to the next handshake step only when all data from the
3289 * previous one have been sent to the peer, thus we make sure that this is
3290 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3291 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3292 * we have to wait before to go ahead.
3293 * In the case of TLS 1.3, handshake step handlers do not send data to the
3294 * peer. Data are only sent here and through
3295 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003296 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003297 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003298 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3299 return( ret );
3300
3301#if defined(MBEDTLS_SSL_PROTO_DTLS)
3302 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3303 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3304 {
3305 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3306 return( ret );
3307 }
3308#endif /* MBEDTLS_SSL_PROTO_DTLS */
3309
3310 return( ret );
3311}
3312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003314{
Hanno Becker41934dd2021-08-07 19:13:43 +01003315 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003316
Hanno Becker41934dd2021-08-07 19:13:43 +01003317 if( ssl == NULL ||
3318 ssl->conf == NULL ||
3319 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003320 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003321 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003322 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003323 }
3324
3325 ret = ssl_prepare_handshake_step( ssl );
3326 if( ret != 0 )
3327 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003328
Jerry Yue7047812021-09-13 19:26:39 +08003329 ret = mbedtls_ssl_handle_pending_alert( ssl );
3330 if( ret != 0 )
3331 goto cleanup;
3332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003333#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003334 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003335 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3337 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003338
Ronald Cron9f0fba32022-02-10 16:45:15 +01003339 switch( ssl->state )
3340 {
3341 case MBEDTLS_SSL_HELLO_REQUEST:
3342 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3343 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003344
Ronald Cron9f0fba32022-02-10 16:45:15 +01003345 case MBEDTLS_SSL_CLIENT_HELLO:
3346 ret = mbedtls_ssl_write_client_hello( ssl );
3347 break;
3348
3349 default:
3350#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003351 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003352 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3353 else
3354 ret = mbedtls_ssl_handshake_client_step( ssl );
3355#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3356 ret = mbedtls_ssl_handshake_client_step( ssl );
3357#else
3358 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3359#endif
3360 }
Jerry Yub9930e72021-08-06 17:11:51 +08003361 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003362#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003364 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003365 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003366#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003367 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003368 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003369#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003370
3371#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3372 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3373 ret = mbedtls_ssl_handshake_server_step( ssl );
3374#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3375 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003376#endif
3377
Jerry Yue7047812021-09-13 19:26:39 +08003378 if( ret != 0 )
3379 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003380 /* handshake_step return error. And it is same
3381 * with alert_reason.
3382 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003383 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003384 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003385 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003386 goto cleanup;
3387 }
3388 }
3389
3390cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003391 return( ret );
3392}
3393
3394/*
3395 * Perform the SSL handshake
3396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003398{
3399 int ret = 0;
3400
Hanno Beckera817ea42020-10-20 15:20:23 +01003401 /* Sanity checks */
3402
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003403 if( ssl == NULL || ssl->conf == NULL )
3404 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3405
Hanno Beckera817ea42020-10-20 15:20:23 +01003406#if defined(MBEDTLS_SSL_PROTO_DTLS)
3407 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3408 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3409 {
3410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3411 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3412 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3413 }
3414#endif /* MBEDTLS_SSL_PROTO_DTLS */
3415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003417
Hanno Beckera817ea42020-10-20 15:20:23 +01003418 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003419 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003422
3423 if( ret != 0 )
3424 break;
3425 }
3426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003428
3429 return( ret );
3430}
3431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432#if defined(MBEDTLS_SSL_RENEGOTIATION)
3433#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003434/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003435 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003436 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003437MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003439{
Janos Follath865b3eb2019-12-16 11:46:15 +00003440 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003443
3444 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3446 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003447
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003448 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003449 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003450 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003451 return( ret );
3452 }
3453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003455
3456 return( 0 );
3457}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003458#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003459
3460/*
3461 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462 * - any side: calling mbedtls_ssl_renegotiate(),
3463 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3464 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003465 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003466 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003468 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003469int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003470{
Janos Follath865b3eb2019-12-16 11:46:15 +00003471 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003474
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003475 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3476 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003477
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003478 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3479 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003481 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003483 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003484 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003485 ssl->handshake->out_msg_seq = 1;
3486 else
3487 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003488 }
3489#endif
3490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003491 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3492 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003496 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003497 return( ret );
3498 }
3499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003501
3502 return( 0 );
3503}
3504
3505/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003506 * Renegotiate current connection on client,
3507 * or request renegotiation on server
3508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003509int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003510{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003512
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003513 if( ssl == NULL || ssl->conf == NULL )
3514 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003517 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003518 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003519 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003520 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003524
3525 /* Did we already try/start sending HelloRequest? */
3526 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003528
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003529 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003530 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003531#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003534 /*
3535 * On client, either start the renegotiation process or,
3536 * if already in progress, continue the handshake
3537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003538 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003539 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003540 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003541 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003542
Hanno Becker40cdaa12020-02-05 10:48:27 +00003543 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003544 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003545 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003546 return( ret );
3547 }
3548 }
3549 else
3550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003551 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003553 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003554 return( ret );
3555 }
3556 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003557#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003558
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003559 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003560}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003562
Gilles Peskine9b562d52018-04-25 20:32:43 +02003563void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003564{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003565 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3566
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003567 if( handshake == NULL )
3568 return;
3569
Brett Warrene0edc842021-08-17 09:53:13 +01003570#if defined(MBEDTLS_ECP_C)
3571#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3572 if ( ssl->handshake->group_list_heap_allocated )
3573 mbedtls_free( (void*) handshake->group_list );
3574 handshake->group_list = NULL;
3575#endif /* MBEDTLS_DEPRECATED_REMOVED */
3576#endif /* MBEDTLS_ECP_C */
3577
Jerry Yuf017ee42022-01-12 15:49:48 +08003578#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3579#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3580 if ( ssl->handshake->sig_algs_heap_allocated )
3581 mbedtls_free( (void*) handshake->sig_algs );
3582 handshake->sig_algs = NULL;
3583#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003584#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3585 if( ssl->handshake->certificate_request_context )
3586 {
3587 mbedtls_free( (void*) handshake->certificate_request_context );
3588 }
3589#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003590#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3591
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003592#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3593 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3594 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003595 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003596 handshake->async_in_progress = 0;
3597 }
3598#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3599
Andrzej Kurek25f27152022-08-17 16:09:31 -04003600#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003601#if defined(MBEDTLS_USE_PSA_CRYPTO)
3602 psa_hash_abort( &handshake->fin_sha256_psa );
3603#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003604 mbedtls_sha256_free( &handshake->fin_sha256 );
3605#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003606#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003607#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003608#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003609 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003610#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003611 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003612#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003613#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003615#if defined(MBEDTLS_DHM_C)
3616 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003617#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003618#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003620#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003621#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003622 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003623#if defined(MBEDTLS_SSL_CLI_C)
3624 mbedtls_free( handshake->ecjpake_cache );
3625 handshake->ecjpake_cache = NULL;
3626 handshake->ecjpake_cache_len = 0;
3627#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003628#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003629
Janos Follath4ae5c292016-02-10 11:27:43 +00003630#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3631 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003632 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003634#endif
3635
Gilles Peskineeccd8882020-03-10 12:19:08 +01003636#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003637#if defined(MBEDTLS_USE_PSA_CRYPTO)
3638 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3639 {
3640 /* The maintenance of the external PSK key slot is the
3641 * user's responsibility. */
3642 if( ssl->handshake->psk_opaque_is_internal )
3643 {
3644 psa_destroy_key( ssl->handshake->psk_opaque );
3645 ssl->handshake->psk_opaque_is_internal = 0;
3646 }
3647 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3648 }
Neil Armstronge952a302022-05-03 10:22:14 +02003649#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003650 if( handshake->psk != NULL )
3651 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003652 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003653 mbedtls_free( handshake->psk );
3654 }
Neil Armstronge952a302022-05-03 10:22:14 +02003655#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003656#endif
3657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003658#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3659 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003660 /*
3661 * Free only the linked list wrapper, not the keys themselves
3662 * since the belong to the SNI callback
3663 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003664 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003666
Gilles Peskineeccd8882020-03-10 12:19:08 +01003667#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003668 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003669 if( handshake->ecrs_peer_cert != NULL )
3670 {
3671 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3672 mbedtls_free( handshake->ecrs_peer_cert );
3673 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003674#endif
3675
Hanno Becker75173122019-02-06 16:18:31 +00003676#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3677 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3678 mbedtls_pk_free( &handshake->peer_pubkey );
3679#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3680
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003681#if defined(MBEDTLS_SSL_CLI_C) && \
3682 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3683 mbedtls_free( handshake->cookie );
3684#endif /* MBEDTLS_SSL_CLI_C &&
3685 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003686
3687#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003688 mbedtls_ssl_flight_free( handshake->flight );
3689 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003690#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003691
Ronald Cronf12b81d2022-03-15 10:42:41 +01003692#if defined(MBEDTLS_ECDH_C) && \
3693 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003694 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003695 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003696#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3697
Ronald Cron6f135e12021-12-08 16:57:54 +01003698#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003699 mbedtls_ssl_transform_free( handshake->transform_handshake );
3700 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003701 mbedtls_free( handshake->transform_earlydata );
3702 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003703#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003704
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003705
3706#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3707 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3708 * processes datagrams and the fact that a datagram is allowed to have
3709 * several records in it, it is possible that the I/O buffers are not
3710 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003711 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3712 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003713#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003714
Jerry Yuba9c7272021-10-30 11:54:10 +08003715 /* mbedtls_platform_zeroize MUST be last one in this function */
3716 mbedtls_platform_zeroize( handshake,
3717 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003718}
3719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003720void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003721{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003722 if( session == NULL )
3723 return;
3724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003726 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003727#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003728
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003729#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00003730#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
3731 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00003732 mbedtls_free( session->hostname );
3733#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003734 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003735#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003736
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003737 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003738}
3739
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003740#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003741
3742#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3743#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3744#else
3745#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3746#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3747
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003748#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003749
3750#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3751#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3752#else
3753#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3754#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3755
3756#if defined(MBEDTLS_SSL_ALPN)
3757#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3758#else
3759#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3760#endif /* MBEDTLS_SSL_ALPN */
3761
3762#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3763#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3764#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3765#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3766
3767#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3768 ( (uint32_t) ( \
3769 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3770 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3771 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3772 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3773 0u ) )
3774
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003775static unsigned char ssl_serialized_context_header[] = {
3776 MBEDTLS_VERSION_MAJOR,
3777 MBEDTLS_VERSION_MINOR,
3778 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003779 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3780 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3781 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3782 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3783 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003784};
3785
Paul Bakker5121ce52009-01-03 21:22:43 +00003786/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003787 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003788 *
3789 * The format of the serialized data is:
3790 * (in the presentation language of TLS, RFC 8446 section 3)
3791 *
3792 * // header
3793 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003794 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003795 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003796 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003797 * Note: When updating the format, remember to keep these
3798 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003799 *
3800 * // session sub-structure
3801 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3802 * // transform sub-structure
3803 * uint8 random[64]; // ServerHello.random+ClientHello.random
3804 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3805 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3806 * // fields from ssl_context
3807 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3808 * uint64 in_window_top; // DTLS: last validated record seq_num
3809 * uint64 in_window; // DTLS: bitmask for replay protection
3810 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3811 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3812 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3813 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3814 *
3815 * Note that many fields of the ssl_context or sub-structures are not
3816 * serialized, as they fall in one of the following categories:
3817 *
3818 * 1. forced value (eg in_left must be 0)
3819 * 2. pointer to dynamically-allocated memory (eg session, transform)
3820 * 3. value can be re-derived from other data (eg session keys from MS)
3821 * 4. value was temporary (eg content of input buffer)
3822 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003823 */
3824int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3825 unsigned char *buf,
3826 size_t buf_len,
3827 size_t *olen )
3828{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003829 unsigned char *p = buf;
3830 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003831 size_t session_len;
3832 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003833
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003834 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003835 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3836 * this function's documentation.
3837 *
3838 * These are due to assumptions/limitations in the implementation. Some of
3839 * them are likely to stay (no handshake in progress) some might go away
3840 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003841 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003842 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003843 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003844 {
3845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003846 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003847 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003848 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003849 {
3850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003851 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003852 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003853 /* Double-check that sub-structures are indeed ready */
3854 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003855 {
3856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003857 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003858 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003859 /* There must be no pending incoming or outgoing data */
3860 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003861 {
3862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003863 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003864 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003865 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003866 {
3867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003868 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003869 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003870 /* Protocol must be DLTS, not TLS */
3871 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003872 {
3873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003874 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003875 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003876 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003877 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003878 {
3879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003880 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003881 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003882 /* We must be using an AEAD ciphersuite */
3883 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003884 {
3885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003886 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003887 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003888 /* Renegotiation must not be enabled */
3889#if defined(MBEDTLS_SSL_RENEGOTIATION)
3890 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003891 {
3892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003893 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003894 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003895#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003896
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003897 /*
3898 * Version and format identifier
3899 */
3900 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003901
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003902 if( used <= buf_len )
3903 {
3904 memcpy( p, ssl_serialized_context_header,
3905 sizeof( ssl_serialized_context_header ) );
3906 p += sizeof( ssl_serialized_context_header );
3907 }
3908
3909 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003910 * Session (length + data)
3911 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003912 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003913 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3914 return( ret );
3915
3916 used += 4 + session_len;
3917 if( used <= buf_len )
3918 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003919 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3920 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003921
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003922 ret = ssl_session_save( ssl->session, 1,
3923 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003924 if( ret != 0 )
3925 return( ret );
3926
3927 p += session_len;
3928 }
3929
3930 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003931 * Transform
3932 */
3933 used += sizeof( ssl->transform->randbytes );
3934 if( used <= buf_len )
3935 {
3936 memcpy( p, ssl->transform->randbytes,
3937 sizeof( ssl->transform->randbytes ) );
3938 p += sizeof( ssl->transform->randbytes );
3939 }
3940
3941#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3942 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3943 if( used <= buf_len )
3944 {
3945 *p++ = ssl->transform->in_cid_len;
3946 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3947 p += ssl->transform->in_cid_len;
3948
3949 *p++ = ssl->transform->out_cid_len;
3950 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3951 p += ssl->transform->out_cid_len;
3952 }
3953#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3954
3955 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003956 * Saved fields from top-level ssl_context structure
3957 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003958 used += 4;
3959 if( used <= buf_len )
3960 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003961 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3962 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003963 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003964
3965#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3966 used += 16;
3967 if( used <= buf_len )
3968 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003969 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3970 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003971
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003972 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3973 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003974 }
3975#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3976
3977#if defined(MBEDTLS_SSL_PROTO_DTLS)
3978 used += 1;
3979 if( used <= buf_len )
3980 {
3981 *p++ = ssl->disable_datagram_packing;
3982 }
3983#endif /* MBEDTLS_SSL_PROTO_DTLS */
3984
Jerry Yuae0b2e22021-10-08 15:21:19 +08003985 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003986 if( used <= buf_len )
3987 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003988 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3989 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003990 }
3991
3992#if defined(MBEDTLS_SSL_PROTO_DTLS)
3993 used += 2;
3994 if( used <= buf_len )
3995 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003996 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
3997 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003998 }
3999#endif /* MBEDTLS_SSL_PROTO_DTLS */
4000
4001#if defined(MBEDTLS_SSL_ALPN)
4002 {
4003 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004004 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004005 : 0;
4006
4007 used += 1 + alpn_len;
4008 if( used <= buf_len )
4009 {
4010 *p++ = alpn_len;
4011
4012 if( ssl->alpn_chosen != NULL )
4013 {
4014 memcpy( p, ssl->alpn_chosen, alpn_len );
4015 p += alpn_len;
4016 }
4017 }
4018 }
4019#endif /* MBEDTLS_SSL_ALPN */
4020
4021 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004022 * Done
4023 */
4024 *olen = used;
4025
4026 if( used > buf_len )
4027 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004028
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004029 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4030
Hanno Becker43aefe22020-02-05 10:44:56 +00004031 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004032}
4033
4034/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004035 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004036 *
4037 * This internal version is wrapped by a public function that cleans up in
4038 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004039 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004040MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004041static int ssl_context_load( mbedtls_ssl_context *ssl,
4042 const unsigned char *buf,
4043 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004044{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004045 const unsigned char *p = buf;
4046 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004047 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004048 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek894edde2022-09-29 06:31:14 -04004049 tls_prf_fn prf_func = NULL;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004050
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004051 /*
4052 * The context should have been freshly setup or reset.
4053 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004054 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004055 * renegotiating, or if the user mistakenly loaded a session first.)
4056 */
4057 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4058 ssl->session != NULL )
4059 {
4060 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4061 }
4062
4063 /*
4064 * We can't check that the config matches the initial one, but we can at
4065 * least check it matches the requirements for serializing.
4066 */
4067 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004068 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4069 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004070#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004071 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004072#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004073 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004074 {
4075 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4076 }
4077
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004078 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4079
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004080 /*
4081 * Check version identifier
4082 */
4083 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4084 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4085
4086 if( memcmp( p, ssl_serialized_context_header,
4087 sizeof( ssl_serialized_context_header ) ) != 0 )
4088 {
4089 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4090 }
4091 p += sizeof( ssl_serialized_context_header );
4092
4093 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004094 * Session
4095 */
4096 if( (size_t)( end - p ) < 4 )
4097 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4098
4099 session_len = ( (size_t) p[0] << 24 ) |
4100 ( (size_t) p[1] << 16 ) |
4101 ( (size_t) p[2] << 8 ) |
4102 ( (size_t) p[3] );
4103 p += 4;
4104
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004105 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004106 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004107 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004108 ssl->session_in = ssl->session;
4109 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004110 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004111
4112 if( (size_t)( end - p ) < session_len )
4113 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4114
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004115 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004116 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004117 {
4118 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004119 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004120 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004121
4122 p += session_len;
4123
4124 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004125 * Transform
4126 */
4127
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004128 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004129 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004130 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004131 ssl->transform_in = ssl->transform;
4132 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004133 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004134
Andrzej Kurek894edde2022-09-29 06:31:14 -04004135 prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite );
4136 if( prf_func == NULL )
4137 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4138
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004139 /* Read random bytes and populate structure */
4140 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4141 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08004142#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00004143 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004144 ssl->session->ciphersuite,
4145 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004146#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004147 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004148#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Andrzej Kurek894edde2022-09-29 06:31:14 -04004149 prf_func,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004150 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004151 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004152 ssl->conf->endpoint,
4153 ssl );
4154 if( ret != 0 )
4155 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004156#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004157 p += sizeof( ssl->transform->randbytes );
4158
4159#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4160 /* Read connection IDs and store them */
4161 if( (size_t)( end - p ) < 1 )
4162 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4163
4164 ssl->transform->in_cid_len = *p++;
4165
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004166 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004167 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4168
4169 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4170 p += ssl->transform->in_cid_len;
4171
4172 ssl->transform->out_cid_len = *p++;
4173
4174 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4175 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4176
4177 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4178 p += ssl->transform->out_cid_len;
4179#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4180
4181 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004182 * Saved fields from top-level ssl_context structure
4183 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004184 if( (size_t)( end - p ) < 4 )
4185 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4186
4187 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4188 ( (uint32_t) p[1] << 16 ) |
4189 ( (uint32_t) p[2] << 8 ) |
4190 ( (uint32_t) p[3] );
4191 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004192
4193#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4194 if( (size_t)( end - p ) < 16 )
4195 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4196
4197 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4198 ( (uint64_t) p[1] << 48 ) |
4199 ( (uint64_t) p[2] << 40 ) |
4200 ( (uint64_t) p[3] << 32 ) |
4201 ( (uint64_t) p[4] << 24 ) |
4202 ( (uint64_t) p[5] << 16 ) |
4203 ( (uint64_t) p[6] << 8 ) |
4204 ( (uint64_t) p[7] );
4205 p += 8;
4206
4207 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4208 ( (uint64_t) p[1] << 48 ) |
4209 ( (uint64_t) p[2] << 40 ) |
4210 ( (uint64_t) p[3] << 32 ) |
4211 ( (uint64_t) p[4] << 24 ) |
4212 ( (uint64_t) p[5] << 16 ) |
4213 ( (uint64_t) p[6] << 8 ) |
4214 ( (uint64_t) p[7] );
4215 p += 8;
4216#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4217
4218#if defined(MBEDTLS_SSL_PROTO_DTLS)
4219 if( (size_t)( end - p ) < 1 )
4220 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4221
4222 ssl->disable_datagram_packing = *p++;
4223#endif /* MBEDTLS_SSL_PROTO_DTLS */
4224
Jerry Yud9a94fe2021-09-28 18:58:59 +08004225 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004226 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004227 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4228 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004229
4230#if defined(MBEDTLS_SSL_PROTO_DTLS)
4231 if( (size_t)( end - p ) < 2 )
4232 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4233
4234 ssl->mtu = ( p[0] << 8 ) | p[1];
4235 p += 2;
4236#endif /* MBEDTLS_SSL_PROTO_DTLS */
4237
4238#if defined(MBEDTLS_SSL_ALPN)
4239 {
4240 uint8_t alpn_len;
4241 const char **cur;
4242
4243 if( (size_t)( end - p ) < 1 )
4244 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4245
4246 alpn_len = *p++;
4247
4248 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4249 {
4250 /* alpn_chosen should point to an item in the configured list */
4251 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4252 {
4253 if( strlen( *cur ) == alpn_len &&
4254 memcmp( p, cur, alpn_len ) == 0 )
4255 {
4256 ssl->alpn_chosen = *cur;
4257 break;
4258 }
4259 }
4260 }
4261
4262 /* can only happen on conf mismatch */
4263 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4264 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4265
4266 p += alpn_len;
4267 }
4268#endif /* MBEDTLS_SSL_ALPN */
4269
4270 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004271 * Forced fields from top-level ssl_context structure
4272 *
4273 * Most of them already set to the correct value by mbedtls_ssl_init() and
4274 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4275 */
4276 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004277 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004278
Hanno Becker361b10d2019-08-30 10:42:49 +01004279 /* Adjust pointers for header fields of outgoing records to
4280 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004281 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004282
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004283#if defined(MBEDTLS_SSL_PROTO_DTLS)
4284 ssl->in_epoch = 1;
4285#endif
4286
4287 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4288 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004289 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4290 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004291 if( ssl->handshake != NULL )
4292 {
4293 mbedtls_ssl_handshake_free( ssl );
4294 mbedtls_free( ssl->handshake );
4295 ssl->handshake = NULL;
4296 }
4297
4298 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004299 * Done - should have consumed entire buffer
4300 */
4301 if( p != end )
4302 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004303
4304 return( 0 );
4305}
4306
4307/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004308 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004309 */
4310int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4311 const unsigned char *buf,
4312 size_t len )
4313{
4314 int ret = ssl_context_load( context, buf, len );
4315
4316 if( ret != 0 )
4317 mbedtls_ssl_free( context );
4318
4319 return( ret );
4320}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004321#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004322
4323/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004324 * Free an SSL context
4325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004326void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004327{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004328 if( ssl == NULL )
4329 return;
4330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004332
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004333 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004334 {
sander-visserb8aa2072020-05-06 22:05:13 +02004335#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4336 size_t out_buf_len = ssl->out_buf_len;
4337#else
4338 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4339#endif
4340
Darryl Greenb33cc762019-11-28 14:29:44 +00004341 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004342 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004343 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004344 }
4345
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004346 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004347 {
sander-visserb8aa2072020-05-06 22:05:13 +02004348#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4349 size_t in_buf_len = ssl->in_buf_len;
4350#else
4351 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4352#endif
4353
Darryl Greenb33cc762019-11-28 14:29:44 +00004354 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004355 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004356 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004357 }
4358
Paul Bakker48916f92012-09-16 19:57:18 +00004359 if( ssl->transform )
4360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004361 mbedtls_ssl_transform_free( ssl->transform );
4362 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004363 }
4364
4365 if( ssl->handshake )
4366 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004367 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004368 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4369 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004371 mbedtls_free( ssl->handshake );
4372 mbedtls_free( ssl->transform_negotiate );
4373 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004374 }
4375
Ronald Cron6f135e12021-12-08 16:57:54 +01004376#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004377 mbedtls_ssl_transform_free( ssl->transform_application );
4378 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004379#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004380
Paul Bakkerc0463502013-02-14 11:19:38 +01004381 if( ssl->session )
4382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004383 mbedtls_ssl_session_free( ssl->session );
4384 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004385 }
4386
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004387#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004388 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004389 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004390 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004391 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004392 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004393#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004394
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004395#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004396 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004397#endif
4398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004399 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004400
Paul Bakker86f04f42013-02-14 11:20:09 +01004401 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004402 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004403}
4404
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004405/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004406 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004407 */
4408void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4409{
4410 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4411}
4412
Gilles Peskineae270bf2021-06-02 00:05:29 +02004413/* The selection should be the same as mbedtls_x509_crt_profile_default in
4414 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004415 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004416 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004417 * about this list.
4418 */
Brett Warrene0edc842021-08-17 09:53:13 +01004419static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004420#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004421 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004422#endif
4423#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004424 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004425#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004426#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004427 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004428#endif
4429#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004430 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004431#endif
4432#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004433 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004434#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004435#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004436 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004437#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004438#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004439 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004440#endif
4441#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004442 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004443#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004444 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004445};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004446
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004447static int ssl_preset_suiteb_ciphersuites[] = {
4448 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4449 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4450 0
4451};
4452
Gilles Peskineeccd8882020-03-10 12:19:08 +01004453#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004454
Jerry Yu909df7b2022-01-22 11:56:27 +08004455/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004456 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004457 * rules SHOULD be upheld.
4458 * - No duplicate entries.
4459 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004460 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004461 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004462 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004463static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004464
Andrzej Kurek25f27152022-08-17 16:09:31 -04004465#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 +08004466 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4467 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004468#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004469 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004470
Andrzej Kurek25f27152022-08-17 16:09:31 -04004471#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 +08004472 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4473 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004474#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004475 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4476
Andrzej Kurek25f27152022-08-17 16:09:31 -04004477#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 +08004478 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4479 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004480#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004481 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004482
Andrzej Kurek25f27152022-08-17 16:09:31 -04004483#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 +08004484 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004485#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 +08004486
Andrzej Kurek25f27152022-08-17 16:09:31 -04004487#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 +08004488 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004489#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 +08004490
Andrzej Kurek25f27152022-08-17 16:09:31 -04004491#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 +08004492 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004493#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 +08004494
Andrzej Kurek25f27152022-08-17 16:09:31 -04004495#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 +08004496 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4497#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4498
Andrzej Kurek25f27152022-08-17 16:09:31 -04004499#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 +08004500 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4501#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4502
Andrzej Kurek25f27152022-08-17 16:09:31 -04004503#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 +08004504 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4505#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4506
Gabor Mezei15b95a62022-05-09 16:37:58 +02004507 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004508};
4509
4510/* NOTICE: see above */
4511#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4512static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004513#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004514#if defined(MBEDTLS_ECDSA_C)
4515 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004516#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004517#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4518 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4519#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004520#if defined(MBEDTLS_RSA_C)
4521 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4522#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004523#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004524#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004525#if defined(MBEDTLS_ECDSA_C)
4526 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004527#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004528#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4529 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4530#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004531#if defined(MBEDTLS_RSA_C)
4532 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4533#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004534#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004535#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004536#if defined(MBEDTLS_ECDSA_C)
4537 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004538#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004539#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4540 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4541#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004542#if defined(MBEDTLS_RSA_C)
4543 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4544#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004545#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004546 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004547};
4548#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4549/* NOTICE: see above */
4550static uint16_t ssl_preset_suiteb_sig_algs[] = {
4551
Andrzej Kurek25f27152022-08-17 16:09:31 -04004552#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 +08004553 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4554 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004555#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004556 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4557
Andrzej Kurek25f27152022-08-17 16:09:31 -04004558#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 +08004559 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4560 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004561#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004562 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004563
Andrzej Kurek25f27152022-08-17 16:09:31 -04004564#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 +08004565 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004566#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 +08004567
Andrzej Kurek25f27152022-08-17 16:09:31 -04004568#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 +08004569 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004570#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004571
Gabor Mezei15b95a62022-05-09 16:37:58 +02004572 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004573};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004574
Jerry Yu909df7b2022-01-22 11:56:27 +08004575/* NOTICE: see above */
4576#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4577static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004578#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004579#if defined(MBEDTLS_ECDSA_C)
4580 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004581#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004582#if defined(MBEDTLS_RSA_C)
4583 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4584#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004585#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004586#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004587#if defined(MBEDTLS_ECDSA_C)
4588 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004589#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004590#if defined(MBEDTLS_RSA_C)
4591 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4592#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004593#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004594 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004595};
Jerry Yu909df7b2022-01-22 11:56:27 +08004596#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4597
Jerry Yu1a8b4812022-01-20 17:56:50 +08004598#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004599
Brett Warrene0edc842021-08-17 09:53:13 +01004600static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004601#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004602 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004603#endif
4604#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004605 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004606#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004607 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004608};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004609
Jerry Yu1a8b4812022-01-20 17:56:50 +08004610#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004611/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004612 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004613MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004614static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004615{
4616 size_t i, j;
4617 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004618
Gabor Mezei15b95a62022-05-09 16:37:58 +02004619 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004620 {
Jerry Yuf377d642022-01-25 10:43:59 +08004621 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004622 {
Jerry Yuf377d642022-01-25 10:43:59 +08004623 if( sig_algs[i] != sig_algs[j] )
4624 continue;
4625 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4626 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4627 sig_algs[i], j, i );
4628 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004629 }
4630 }
4631 return( ret );
4632}
4633
4634#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4635
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004636/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004637 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004638 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004639int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004640 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004641{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004642#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004643 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004644#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004645
Jerry Yu1a8b4812022-01-20 17:56:50 +08004646#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004647 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004648 {
4649 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4650 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4651 }
4652
Jerry Yuf377d642022-01-25 10:43:59 +08004653 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004654 {
4655 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4656 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4657 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004658
4659#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004660 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004661 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004662 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004663 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4664 }
4665
Jerry Yuf377d642022-01-25 10:43:59 +08004666 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004667 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004668 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004669 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4670 }
4671#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004672#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4673
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004674 /* Use the functions here so that they are covered in tests,
4675 * but otherwise access member directly for efficiency */
4676 mbedtls_ssl_conf_endpoint( conf, endpoint );
4677 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004678
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004679 /*
4680 * Things that are common to all presets
4681 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004682#if defined(MBEDTLS_SSL_CLI_C)
4683 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4684 {
4685 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4686#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4687 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4688#endif
4689 }
4690#endif
4691
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004692#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4693 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4694#endif
4695
4696#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4697 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4698#endif
4699
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004700#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004701 conf->f_cookie_write = ssl_cookie_write_dummy;
4702 conf->f_cookie_check = ssl_cookie_check_dummy;
4703#endif
4704
4705#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4706 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4707#endif
4708
Janos Follath088ce432017-04-10 12:42:31 +01004709#if defined(MBEDTLS_SSL_SRV_C)
4710 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004711 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004712#endif
4713
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004714#if defined(MBEDTLS_SSL_PROTO_DTLS)
4715 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4716 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4717#endif
4718
4719#if defined(MBEDTLS_SSL_RENEGOTIATION)
4720 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004721 memset( conf->renego_period, 0x00, 2 );
4722 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004723#endif
4724
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004725#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004726 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4727 {
4728 const unsigned char dhm_p[] =
4729 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4730 const unsigned char dhm_g[] =
4731 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004732
Hanno Beckere2defad2021-07-24 05:59:17 +01004733 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4734 dhm_p, sizeof( dhm_p ),
4735 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4736 {
4737 return( ret );
4738 }
4739 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004740#endif
4741
Ronald Cron6f135e12021-12-08 16:57:54 +01004742#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004743#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004744 mbedtls_ssl_conf_new_session_tickets(
4745 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4746#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004747 /*
4748 * Allow all TLS 1.3 key exchange modes by default.
4749 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004750 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004751#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004752
XiaokangQian4d3a6042022-04-21 13:46:17 +00004753 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4754 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004755#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004756 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004757 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004758#else
XiaokangQian060d8672022-04-21 09:24:56 +00004759 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004760#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004761 }
4762 else
4763 {
4764#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4765 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4766 {
4767 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4768 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4769 }
4770 else
4771 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4772 {
4773 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4774 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4775 }
4776#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4777 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4778 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4779#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4780 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4781 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4782#else
4783 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4784#endif
4785 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004786
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004787 /*
4788 * Preset-specific defaults
4789 */
4790 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004791 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004792 /*
4793 * NSA Suite B
4794 */
4795 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004796
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004797 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004798
4799#if defined(MBEDTLS_X509_CRT_PARSE_C)
4800 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004801#endif
4802
Gilles Peskineeccd8882020-03-10 12:19:08 +01004803#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004804#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4805 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4806 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4807 else
4808#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4809 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004810#endif
4811
Brett Warrene0edc842021-08-17 09:53:13 +01004812#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4813 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004814#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004815 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004816 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004817
4818 /*
4819 * Default
4820 */
4821 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004822
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004823 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004824
4825#if defined(MBEDTLS_X509_CRT_PARSE_C)
4826 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4827#endif
4828
Gilles Peskineeccd8882020-03-10 12:19:08 +01004829#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004830#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4831 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4832 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4833 else
4834#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4835 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004836#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004837
Brett Warrene0edc842021-08-17 09:53:13 +01004838#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4839 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004840#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004841 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004842
4843#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4844 conf->dhm_min_bitlen = 1024;
4845#endif
4846 }
4847
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004848 return( 0 );
4849}
4850
4851/*
4852 * Free mbedtls_ssl_config
4853 */
4854void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4855{
4856#if defined(MBEDTLS_DHM_C)
4857 mbedtls_mpi_free( &conf->dhm_P );
4858 mbedtls_mpi_free( &conf->dhm_G );
4859#endif
4860
Gilles Peskineeccd8882020-03-10 12:19:08 +01004861#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004862#if defined(MBEDTLS_USE_PSA_CRYPTO)
4863 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4864 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004865 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4866 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004867#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004868 if( conf->psk != NULL )
4869 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004870 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004871 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004872 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004873 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004874 }
4875
4876 if( conf->psk_identity != NULL )
4877 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004878 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004879 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004880 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004881 conf->psk_identity_len = 0;
4882 }
4883#endif
4884
4885#if defined(MBEDTLS_X509_CRT_PARSE_C)
4886 ssl_key_cert_free( conf->key_cert );
4887#endif
4888
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004889 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004890}
4891
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004892#if defined(MBEDTLS_PK_C) && \
4893 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004894/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004895 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004896 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004897unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004898{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004899#if defined(MBEDTLS_RSA_C)
4900 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4901 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004902#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004903#if defined(MBEDTLS_ECDSA_C)
4904 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4905 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004906#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004907 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004908}
4909
Hanno Becker7e5437a2017-04-28 17:15:26 +01004910unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4911{
4912 switch( type ) {
4913 case MBEDTLS_PK_RSA:
4914 return( MBEDTLS_SSL_SIG_RSA );
4915 case MBEDTLS_PK_ECDSA:
4916 case MBEDTLS_PK_ECKEY:
4917 return( MBEDTLS_SSL_SIG_ECDSA );
4918 default:
4919 return( MBEDTLS_SSL_SIG_ANON );
4920 }
4921}
4922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004923mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004924{
4925 switch( sig )
4926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004927#if defined(MBEDTLS_RSA_C)
4928 case MBEDTLS_SSL_SIG_RSA:
4929 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004930#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004931#if defined(MBEDTLS_ECDSA_C)
4932 case MBEDTLS_SSL_SIG_ECDSA:
4933 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004934#endif
4935 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004936 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004937 }
4938}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004939#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004940
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004941/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004942 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004943 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004944mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004945{
4946 switch( hash )
4947 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004948#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004949 case MBEDTLS_SSL_HASH_MD5:
4950 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004951#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004952#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004953 case MBEDTLS_SSL_HASH_SHA1:
4954 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004955#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004956#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004957 case MBEDTLS_SSL_HASH_SHA224:
4958 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004959#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004960#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004961 case MBEDTLS_SSL_HASH_SHA256:
4962 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004963#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004964#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004965 case MBEDTLS_SSL_HASH_SHA384:
4966 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004967#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004968#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004969 case MBEDTLS_SSL_HASH_SHA512:
4970 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004971#endif
4972 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004973 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004974 }
4975}
4976
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004977/*
4978 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4979 */
4980unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4981{
4982 switch( md )
4983 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004984#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004985 case MBEDTLS_MD_MD5:
4986 return( MBEDTLS_SSL_HASH_MD5 );
4987#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004988#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004989 case MBEDTLS_MD_SHA1:
4990 return( MBEDTLS_SSL_HASH_SHA1 );
4991#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004992#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004993 case MBEDTLS_MD_SHA224:
4994 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004995#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004996#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004997 case MBEDTLS_MD_SHA256:
4998 return( MBEDTLS_SSL_HASH_SHA256 );
4999#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005000#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005001 case MBEDTLS_MD_SHA384:
5002 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005003#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005004#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005005 case MBEDTLS_MD_SHA512:
5006 return( MBEDTLS_SSL_HASH_SHA512 );
5007#endif
5008 default:
5009 return( MBEDTLS_SSL_HASH_NONE );
5010 }
5011}
5012
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005013/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005014 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005015 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005016 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005017int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005018{
Brett Warrene0edc842021-08-17 09:53:13 +01005019 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005020
Brett Warrene0edc842021-08-17 09:53:13 +01005021 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005022 return( -1 );
5023
Brett Warrene0edc842021-08-17 09:53:13 +01005024 for( ; *group_list != 0; group_list++ )
5025 {
5026 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005027 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005028 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005029
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005030 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005031}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005032
5033#if defined(MBEDTLS_ECP_C)
5034/*
5035 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5036 */
5037int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5038{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005039 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005040 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005041
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005042 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005043 return -1;
5044
5045 uint16_t tls_id = grp_info->tls_id;
5046
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005047 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5048}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005049#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005051#if defined(MBEDTLS_X509_CRT_PARSE_C)
5052int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5053 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005054 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005055 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005056{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005057 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005058 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005059 const char *ext_oid;
5060 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005062 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005063 {
5064 /* Server part of the key exchange */
5065 switch( ciphersuite->key_exchange )
5066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005067 case MBEDTLS_KEY_EXCHANGE_RSA:
5068 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005069 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005070 break;
5071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005072 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5073 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5074 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5075 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005076 break;
5077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005078 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5079 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005080 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005081 break;
5082
5083 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005084 case MBEDTLS_KEY_EXCHANGE_NONE:
5085 case MBEDTLS_KEY_EXCHANGE_PSK:
5086 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5087 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005088 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005089 usage = 0;
5090 }
5091 }
5092 else
5093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005094 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5095 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005096 }
5097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005098 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005099 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005100 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005101 ret = -1;
5102 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005104 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005106 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5107 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005108 }
5109 else
5110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005111 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5112 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005113 }
5114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005115 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005116 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005117 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005118 ret = -1;
5119 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005120
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005121 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005122}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005123#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005124
Jerry Yu148165c2021-09-24 23:20:59 +08005125#if defined(MBEDTLS_USE_PSA_CRYPTO)
5126int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5127 const mbedtls_md_type_t md,
5128 unsigned char *dst,
5129 size_t dst_len,
5130 size_t *olen )
5131{
Ronald Cronf6893e12022-01-07 22:09:01 +01005132 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5133 psa_hash_operation_t *hash_operation_to_clone;
5134 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5135
Jerry Yu148165c2021-09-24 23:20:59 +08005136 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005137
5138 switch( md )
5139 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005140#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005141 case MBEDTLS_MD_SHA384:
5142 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5143 break;
5144#endif
5145
Andrzej Kurek25f27152022-08-17 16:09:31 -04005146#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005147 case MBEDTLS_MD_SHA256:
5148 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5149 break;
5150#endif
5151
5152 default:
5153 goto exit;
5154 }
5155
5156 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5157 if( status != PSA_SUCCESS )
5158 goto exit;
5159
5160 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5161 if( status != PSA_SUCCESS )
5162 goto exit;
5163
5164exit:
Ronald Cronb788c042022-02-15 09:14:15 +01005165 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005166}
5167#else /* MBEDTLS_USE_PSA_CRYPTO */
5168
Andrzej Kurek25f27152022-08-17 16:09:31 -04005169#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005170MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005171static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5172 unsigned char *dst,
5173 size_t dst_len,
5174 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005175{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005176 int ret;
5177 mbedtls_sha512_context sha512;
5178
5179 if( dst_len < 48 )
5180 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5181
5182 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005183 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005184
5185 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5186 {
5187 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5188 goto exit;
5189 }
5190
5191 *olen = 48;
5192
5193exit:
5194
5195 mbedtls_sha512_free( &sha512 );
5196 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005197}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005198#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005199
Andrzej Kurek25f27152022-08-17 16:09:31 -04005200#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005201MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005202static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5203 unsigned char *dst,
5204 size_t dst_len,
5205 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005206{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005207 int ret;
5208 mbedtls_sha256_context sha256;
5209
5210 if( dst_len < 32 )
5211 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5212
5213 mbedtls_sha256_init( &sha256 );
5214 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005215
Jerry Yu24c0ec32021-09-09 14:21:07 +08005216 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5217 {
5218 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5219 goto exit;
5220 }
5221
5222 *olen = 32;
5223
5224exit:
5225
5226 mbedtls_sha256_free( &sha256 );
5227 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005228}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005229#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005230
Jerry Yu000f9762021-09-14 11:12:51 +08005231int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5232 const mbedtls_md_type_t md,
5233 unsigned char *dst,
5234 size_t dst_len,
5235 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005236{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005237 switch( md )
5238 {
5239
Andrzej Kurek25f27152022-08-17 16:09:31 -04005240#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005241 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005242 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005243#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005244
Andrzej Kurek25f27152022-08-17 16:09:31 -04005245#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005246 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005247 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005248#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005249
5250 default:
5251 break;
5252 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005253 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005254}
XiaokangQian647719a2021-12-07 09:16:29 +00005255
Jerry Yu148165c2021-09-24 23:20:59 +08005256#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005257
Gabor Mezei078e8032022-04-27 21:17:56 +02005258#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5259/* mbedtls_ssl_parse_sig_alg_ext()
5260 *
5261 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5262 * value (TLS 1.3 RFC8446):
5263 * enum {
5264 * ....
5265 * ecdsa_secp256r1_sha256( 0x0403 ),
5266 * ecdsa_secp384r1_sha384( 0x0503 ),
5267 * ecdsa_secp521r1_sha512( 0x0603 ),
5268 * ....
5269 * } SignatureScheme;
5270 *
5271 * struct {
5272 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5273 * } SignatureSchemeList;
5274 *
5275 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5276 * value (TLS 1.2 RFC5246):
5277 * enum {
5278 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5279 * sha512(6), (255)
5280 * } HashAlgorithm;
5281 *
5282 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5283 * SignatureAlgorithm;
5284 *
5285 * struct {
5286 * HashAlgorithm hash;
5287 * SignatureAlgorithm signature;
5288 * } SignatureAndHashAlgorithm;
5289 *
5290 * SignatureAndHashAlgorithm
5291 * supported_signature_algorithms<2..2^16-2>;
5292 *
5293 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5294 * generalization of the TLS 1.2 signature algorithm extension.
5295 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5296 * `SignatureScheme` field of TLS 1.3
5297 *
5298 */
5299int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5300 const unsigned char *buf,
5301 const unsigned char *end )
5302{
5303 const unsigned char *p = buf;
5304 size_t supported_sig_algs_len = 0;
5305 const unsigned char *supported_sig_algs_end;
5306 uint16_t sig_alg;
5307 uint32_t common_idx = 0;
5308
5309 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5310 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5311 p += 2;
5312
5313 memset( ssl->handshake->received_sig_algs, 0,
5314 sizeof(ssl->handshake->received_sig_algs) );
5315
5316 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5317 supported_sig_algs_end = p + supported_sig_algs_len;
5318 while( p < supported_sig_algs_end )
5319 {
5320 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5321 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5322 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005323 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5324 sig_alg,
5325 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005326#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005327 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005328 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5329 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5330 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005331 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005332 }
5333#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005334
Jerry Yuf3b46b52022-06-19 16:52:27 +08005335 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5336 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005337
5338 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5339 {
5340 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5341 common_idx += 1;
5342 }
5343 }
5344 /* Check that we consumed all the message. */
5345 if( p != end )
5346 {
5347 MBEDTLS_SSL_DEBUG_MSG( 1,
5348 ( "Signature algorithms extension length misaligned" ) );
5349 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5350 MBEDTLS_ERR_SSL_DECODE_ERROR );
5351 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5352 }
5353
5354 if( common_idx == 0 )
5355 {
5356 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5357 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5358 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5359 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5360 }
5361
Gabor Mezei15b95a62022-05-09 16:37:58 +02005362 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005363 return( 0 );
5364}
5365
5366#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5367
Jerry Yudc7bd172022-02-17 13:44:15 +08005368#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5369
5370#if defined(MBEDTLS_USE_PSA_CRYPTO)
5371
5372static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5373 mbedtls_svc_key_id_t key,
5374 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005375 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005376 const unsigned char* seed, size_t seed_length,
5377 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005378 const unsigned char* other_secret,
5379 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005380 size_t capacity )
5381{
5382 psa_status_t status;
5383
5384 status = psa_key_derivation_setup( derivation, alg );
5385 if( status != PSA_SUCCESS )
5386 return( status );
5387
5388 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5389 {
5390 status = psa_key_derivation_input_bytes( derivation,
5391 PSA_KEY_DERIVATION_INPUT_SEED,
5392 seed, seed_length );
5393 if( status != PSA_SUCCESS )
5394 return( status );
5395
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005396 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005397 {
5398 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005399 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5400 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005401 if( status != PSA_SUCCESS )
5402 return( status );
5403 }
5404
Jerry Yudc7bd172022-02-17 13:44:15 +08005405 if( mbedtls_svc_key_id_is_null( key ) )
5406 {
5407 status = psa_key_derivation_input_bytes(
5408 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005409 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005410 }
5411 else
5412 {
5413 status = psa_key_derivation_input_key(
5414 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5415 }
5416 if( status != PSA_SUCCESS )
5417 return( status );
5418
5419 status = psa_key_derivation_input_bytes( derivation,
5420 PSA_KEY_DERIVATION_INPUT_LABEL,
5421 label, label_length );
5422 if( status != PSA_SUCCESS )
5423 return( status );
5424 }
5425 else
5426 {
5427 return( PSA_ERROR_NOT_SUPPORTED );
5428 }
5429
5430 status = psa_key_derivation_set_capacity( derivation, capacity );
5431 if( status != PSA_SUCCESS )
5432 return( status );
5433
5434 return( PSA_SUCCESS );
5435}
5436
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005437MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005438static int tls_prf_generic( mbedtls_md_type_t md_type,
5439 const unsigned char *secret, size_t slen,
5440 const char *label,
5441 const unsigned char *random, size_t rlen,
5442 unsigned char *dstbuf, size_t dlen )
5443{
5444 psa_status_t status;
5445 psa_algorithm_t alg;
5446 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5447 psa_key_derivation_operation_t derivation =
5448 PSA_KEY_DERIVATION_OPERATION_INIT;
5449
5450 if( md_type == MBEDTLS_MD_SHA384 )
5451 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5452 else
5453 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5454
5455 /* Normally a "secret" should be long enough to be impossible to
5456 * find by brute force, and in particular should not be empty. But
5457 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5458 * and for this use case it makes sense to have a 0-length "secret".
5459 * Since the key API doesn't allow importing a key of length 0,
5460 * keep master_key=0, which setup_psa_key_derivation() understands
5461 * to mean a 0-length "secret" input. */
5462 if( slen != 0 )
5463 {
5464 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5465 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5466 psa_set_key_algorithm( &key_attributes, alg );
5467 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5468
5469 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5470 if( status != PSA_SUCCESS )
5471 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5472 }
5473
5474 status = setup_psa_key_derivation( &derivation,
5475 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005476 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005477 random, rlen,
5478 (unsigned char const *) label,
5479 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005480 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005481 dlen );
5482 if( status != PSA_SUCCESS )
5483 {
5484 psa_key_derivation_abort( &derivation );
5485 psa_destroy_key( master_key );
5486 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5487 }
5488
5489 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5490 if( status != PSA_SUCCESS )
5491 {
5492 psa_key_derivation_abort( &derivation );
5493 psa_destroy_key( master_key );
5494 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5495 }
5496
5497 status = psa_key_derivation_abort( &derivation );
5498 if( status != PSA_SUCCESS )
5499 {
5500 psa_destroy_key( master_key );
5501 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5502 }
5503
5504 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5505 status = psa_destroy_key( master_key );
5506 if( status != PSA_SUCCESS )
5507 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5508
5509 return( 0 );
5510}
5511
5512#else /* MBEDTLS_USE_PSA_CRYPTO */
5513
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005514MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005515static int tls_prf_generic( mbedtls_md_type_t md_type,
5516 const unsigned char *secret, size_t slen,
5517 const char *label,
5518 const unsigned char *random, size_t rlen,
5519 unsigned char *dstbuf, size_t dlen )
5520{
5521 size_t nb;
5522 size_t i, j, k, md_len;
5523 unsigned char *tmp;
5524 size_t tmp_len = 0;
5525 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5526 const mbedtls_md_info_t *md_info;
5527 mbedtls_md_context_t md_ctx;
5528 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5529
5530 mbedtls_md_init( &md_ctx );
5531
5532 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5533 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5534
5535 md_len = mbedtls_md_get_size( md_info );
5536
5537 tmp_len = md_len + strlen( label ) + rlen;
5538 tmp = mbedtls_calloc( 1, tmp_len );
5539 if( tmp == NULL )
5540 {
5541 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5542 goto exit;
5543 }
5544
5545 nb = strlen( label );
5546 memcpy( tmp + md_len, label, nb );
5547 memcpy( tmp + md_len + nb, random, rlen );
5548 nb += rlen;
5549
5550 /*
5551 * Compute P_<hash>(secret, label + random)[0..dlen]
5552 */
5553 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5554 goto exit;
5555
5556 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5557 if( ret != 0 )
5558 goto exit;
5559 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5560 if( ret != 0 )
5561 goto exit;
5562 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5563 if( ret != 0 )
5564 goto exit;
5565
5566 for( i = 0; i < dlen; i += md_len )
5567 {
5568 ret = mbedtls_md_hmac_reset ( &md_ctx );
5569 if( ret != 0 )
5570 goto exit;
5571 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5572 if( ret != 0 )
5573 goto exit;
5574 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5575 if( ret != 0 )
5576 goto exit;
5577
5578 ret = mbedtls_md_hmac_reset ( &md_ctx );
5579 if( ret != 0 )
5580 goto exit;
5581 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5582 if( ret != 0 )
5583 goto exit;
5584 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5585 if( ret != 0 )
5586 goto exit;
5587
5588 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5589
5590 for( j = 0; j < k; j++ )
5591 dstbuf[i + j] = h_i[j];
5592 }
5593
5594exit:
5595 mbedtls_md_free( &md_ctx );
5596
5597 mbedtls_platform_zeroize( tmp, tmp_len );
5598 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5599
5600 mbedtls_free( tmp );
5601
5602 return( ret );
5603}
5604#endif /* MBEDTLS_USE_PSA_CRYPTO */
5605
Andrzej Kurek25f27152022-08-17 16:09:31 -04005606#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005607MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005608static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5609 const char *label,
5610 const unsigned char *random, size_t rlen,
5611 unsigned char *dstbuf, size_t dlen )
5612{
5613 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5614 label, random, rlen, dstbuf, dlen ) );
5615}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005616#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005617
Andrzej Kurek25f27152022-08-17 16:09:31 -04005618#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005619MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005620static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5621 const char *label,
5622 const unsigned char *random, size_t rlen,
5623 unsigned char *dstbuf, size_t dlen )
5624{
5625 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5626 label, random, rlen, dstbuf, dlen ) );
5627}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005628#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005629
Jerry Yuf009d862022-02-17 14:01:37 +08005630/*
5631 * Set appropriate PRF function and other SSL / TLS1.2 functions
5632 *
5633 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005634 * - hash associated with the ciphersuite (only used by TLS 1.2)
5635 *
5636 * Outputs:
5637 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5638 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005639MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005640static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005641 mbedtls_md_type_t hash )
5642{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005643#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005644 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005645 {
5646 handshake->tls_prf = tls_prf_sha384;
5647 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5648 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5649 }
5650 else
5651#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005652#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005653 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005654 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005655 handshake->tls_prf = tls_prf_sha256;
5656 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5657 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5658 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005659#else
Jerry Yuf009d862022-02-17 14:01:37 +08005660 {
Jerry Yuf009d862022-02-17 14:01:37 +08005661 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005662 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005663 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5664 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005665#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005666
5667 return( 0 );
5668}
Jerry Yud6ab2352022-02-17 14:03:43 +08005669
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005670/*
5671 * Compute master secret if needed
5672 *
5673 * Parameters:
5674 * [in/out] handshake
5675 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5676 * (PSA-PSK) ciphersuite_info, psk_opaque
5677 * [out] premaster (cleared)
5678 * [out] master
5679 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5680 * debug: conf->f_dbg, conf->p_dbg
5681 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005682 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005683 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005684MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005685static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5686 unsigned char *master,
5687 const mbedtls_ssl_context *ssl )
5688{
5689 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5690
5691 /* cf. RFC 5246, Section 8.1:
5692 * "The master secret is always exactly 48 bytes in length." */
5693 size_t const master_secret_len = 48;
5694
5695#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5696 unsigned char session_hash[48];
5697#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5698
5699 /* The label for the KDF used for key expansion.
5700 * This is either "master secret" or "extended master secret"
5701 * depending on whether the Extended Master Secret extension
5702 * is used. */
5703 char const *lbl = "master secret";
5704
Przemek Stekielae4ed302022-04-05 17:15:55 +02005705 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005706 * - If the Extended Master Secret extension is not used,
5707 * this is ClientHello.Random + ServerHello.Random
5708 * (see Sect. 8.1 in RFC 5246).
5709 * - If the Extended Master Secret extension is used,
5710 * this is the transcript of the handshake so far.
5711 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005712 unsigned char const *seed = handshake->randbytes;
5713 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005714
5715#if !defined(MBEDTLS_DEBUG_C) && \
5716 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5717 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5718 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5719 ssl = NULL; /* make sure we don't use it except for those cases */
5720 (void) ssl;
5721#endif
5722
5723 if( handshake->resume != 0 )
5724 {
5725 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5726 return( 0 );
5727 }
5728
5729#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5730 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5731 {
5732 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005733 seed = session_hash;
5734 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005735
5736 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005737 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005738 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005739#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005740
Przemek Stekiel99114f32022-04-22 11:20:09 +02005741#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005742 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005743 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005744 {
5745 /* Perform PSK-to-MS expansion in a single step. */
5746 psa_status_t status;
5747 psa_algorithm_t alg;
5748 mbedtls_svc_key_id_t psk;
5749 psa_key_derivation_operation_t derivation =
5750 PSA_KEY_DERIVATION_OPERATION_INIT;
5751 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5752
5753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5754
5755 psk = mbedtls_ssl_get_opaque_psk( ssl );
5756
5757 if( hash_alg == MBEDTLS_MD_SHA384 )
5758 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5759 else
5760 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5761
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005762 size_t other_secret_len = 0;
5763 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005764
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005765 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005766 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005767 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005768 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005769 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005770 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005771 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005772 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005773 other_secret_len = 48;
5774 other_secret = handshake->premaster + 2;
5775 break;
5776 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005777 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005778 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5779 other_secret = handshake->premaster + 2;
5780 break;
5781 default:
5782 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005783 }
5784
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005785 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005786 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005787 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005788 (unsigned char const *) lbl,
5789 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005790 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005791 master_secret_len );
5792 if( status != PSA_SUCCESS )
5793 {
5794 psa_key_derivation_abort( &derivation );
5795 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5796 }
5797
5798 status = psa_key_derivation_output_bytes( &derivation,
5799 master,
5800 master_secret_len );
5801 if( status != PSA_SUCCESS )
5802 {
5803 psa_key_derivation_abort( &derivation );
5804 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5805 }
5806
5807 status = psa_key_derivation_abort( &derivation );
5808 if( status != PSA_SUCCESS )
5809 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5810 }
5811 else
5812#endif
5813 {
5814 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005815 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005816 master,
5817 master_secret_len );
5818 if( ret != 0 )
5819 {
5820 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5821 return( ret );
5822 }
5823
5824 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5825 handshake->premaster,
5826 handshake->pmslen );
5827
5828 mbedtls_platform_zeroize( handshake->premaster,
5829 sizeof(handshake->premaster) );
5830 }
5831
5832 return( 0 );
5833}
5834
Jerry Yud62f87e2022-02-17 14:09:02 +08005835int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5836{
5837 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5838 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5839 ssl->handshake->ciphersuite_info;
5840
5841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5842
5843 /* Set PRF, calc_verify and calc_finished function pointers */
5844 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005845 ciphersuite_info->mac );
5846 if( ret != 0 )
5847 {
5848 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5849 return( ret );
5850 }
5851
5852 /* Compute master secret if needed */
5853 ret = ssl_compute_master( ssl->handshake,
5854 ssl->session_negotiate->master,
5855 ssl );
5856 if( ret != 0 )
5857 {
5858 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5859 return( ret );
5860 }
5861
5862 /* Swap the client and server random values:
5863 * - MS derivation wanted client+server (RFC 5246 8.1)
5864 * - key derivation wants server+client (RFC 5246 6.3) */
5865 {
5866 unsigned char tmp[64];
5867 memcpy( tmp, ssl->handshake->randbytes, 64 );
5868 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5869 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5870 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5871 }
5872
5873 /* Populate transform structure */
5874 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5875 ssl->session_negotiate->ciphersuite,
5876 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005877#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005878 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005879#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005880 ssl->handshake->tls_prf,
5881 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005882 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005883 ssl->conf->endpoint,
5884 ssl );
5885 if( ret != 0 )
5886 {
5887 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5888 return( ret );
5889 }
5890
5891 /* We no longer need Server/ClientHello.random values */
5892 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5893 sizeof( ssl->handshake->randbytes ) );
5894
5895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5896
5897 return( 0 );
5898}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005899
Ronald Cron4dcbca92022-03-07 10:21:40 +01005900int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5901{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005902 switch( md )
5903 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005904#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005905 case MBEDTLS_SSL_HASH_SHA384:
5906 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5907 break;
5908#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005909#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005910 case MBEDTLS_SSL_HASH_SHA256:
5911 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5912 break;
5913#endif
5914 default:
5915 return( -1 );
5916 }
5917
Ronald Cronc2f13a02022-03-07 10:25:24 +01005918 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005919}
5920
Andrzej Kurek25f27152022-08-17 16:09:31 -04005921#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005922void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5923 unsigned char *hash,
5924 size_t *hlen )
5925{
5926#if defined(MBEDTLS_USE_PSA_CRYPTO)
5927 size_t hash_size;
5928 psa_status_t status;
5929 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5930
5931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5932 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5933 if( status != PSA_SUCCESS )
5934 {
5935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5936 return;
5937 }
5938
5939 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5940 if( status != PSA_SUCCESS )
5941 {
5942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5943 return;
5944 }
5945
5946 *hlen = 32;
5947 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5948 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5949#else
5950 mbedtls_sha256_context sha256;
5951
5952 mbedtls_sha256_init( &sha256 );
5953
5954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5955
5956 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5957 mbedtls_sha256_finish( &sha256, hash );
5958
5959 *hlen = 32;
5960
5961 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5962 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5963
5964 mbedtls_sha256_free( &sha256 );
5965#endif /* MBEDTLS_USE_PSA_CRYPTO */
5966 return;
5967}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005968#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08005969
Andrzej Kurek25f27152022-08-17 16:09:31 -04005970#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08005971void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5972 unsigned char *hash,
5973 size_t *hlen )
5974{
5975#if defined(MBEDTLS_USE_PSA_CRYPTO)
5976 size_t hash_size;
5977 psa_status_t status;
5978 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5979
5980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5981 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5982 if( status != PSA_SUCCESS )
5983 {
5984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5985 return;
5986 }
5987
5988 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5989 if( status != PSA_SUCCESS )
5990 {
5991 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5992 return;
5993 }
5994
5995 *hlen = 48;
5996 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5998#else
5999 mbedtls_sha512_context sha512;
6000
6001 mbedtls_sha512_init( &sha512 );
6002
6003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6004
Andrzej Kureka242e832022-08-11 10:03:14 -04006005 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006006 mbedtls_sha512_finish( &sha512, hash );
6007
6008 *hlen = 48;
6009
6010 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6011 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6012
6013 mbedtls_sha512_free( &sha512 );
6014#endif /* MBEDTLS_USE_PSA_CRYPTO */
6015 return;
6016}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006017#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006018
Neil Armstrong80f6f322022-05-03 17:56:38 +02006019#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6020 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006021int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6022{
6023 unsigned char *p = ssl->handshake->premaster;
6024 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6025 const unsigned char *psk = NULL;
6026 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006027 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006028
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006029 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006030 {
6031 /*
6032 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006033 * checked before calling this function.
6034 *
6035 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006036 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006037 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006038 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6039 {
6040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6041 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6042 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006043 }
6044
6045 /*
6046 * PMS = struct {
6047 * opaque other_secret<0..2^16-1>;
6048 * opaque psk<0..2^16-1>;
6049 * };
6050 * with "other_secret" depending on the particular key exchange
6051 */
6052#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6053 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6054 {
6055 if( end - p < 2 )
6056 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6057
6058 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6059 p += 2;
6060
6061 if( end < p || (size_t)( end - p ) < psk_len )
6062 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6063
6064 memset( p, 0, psk_len );
6065 p += psk_len;
6066 }
6067 else
6068#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6069#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6070 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6071 {
6072 /*
6073 * other_secret already set by the ClientKeyExchange message,
6074 * and is 48 bytes long
6075 */
6076 if( end - p < 2 )
6077 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6078
6079 *p++ = 0;
6080 *p++ = 48;
6081 p += 48;
6082 }
6083 else
6084#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6085#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6086 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6087 {
6088 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6089 size_t len;
6090
6091 /* Write length only when we know the actual value */
6092 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6093 p + 2, end - ( p + 2 ), &len,
6094 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6095 {
6096 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6097 return( ret );
6098 }
6099 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6100 p += 2 + len;
6101
6102 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6103 }
6104 else
6105#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006106#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006107 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6108 {
6109 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6110 size_t zlen;
6111
6112 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6113 p + 2, end - ( p + 2 ),
6114 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6115 {
6116 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6117 return( ret );
6118 }
6119
6120 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6121 p += 2 + zlen;
6122
6123 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6124 MBEDTLS_DEBUG_ECDH_Z );
6125 }
6126 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006127#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006128 {
6129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6130 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6131 }
6132
6133 /* opaque psk<0..2^16-1>; */
6134 if( end - p < 2 )
6135 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6136
6137 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6138 p += 2;
6139
6140 if( end < p || (size_t)( end - p ) < psk_len )
6141 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6142
6143 memcpy( p, psk, psk_len );
6144 p += psk_len;
6145
6146 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6147
6148 return( 0 );
6149}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006150#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006151
6152#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006153MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006154static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6155
6156#if defined(MBEDTLS_SSL_PROTO_DTLS)
6157int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6158{
6159 /* If renegotiation is not enforced, retransmit until we would reach max
6160 * timeout if we were using the usual handshake doubling scheme */
6161 if( ssl->conf->renego_max_records < 0 )
6162 {
6163 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6164 unsigned char doublings = 1;
6165
6166 while( ratio != 0 )
6167 {
6168 ++doublings;
6169 ratio >>= 1;
6170 }
6171
6172 if( ++ssl->renego_records_seen > doublings )
6173 {
6174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6175 return( 0 );
6176 }
6177 }
6178
6179 return( ssl_write_hello_request( ssl ) );
6180}
6181#endif
6182#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006183
Jerry Yud9526692022-02-17 14:23:47 +08006184/*
6185 * Handshake functions
6186 */
6187#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6188/* No certificate support -> dummy functions */
6189int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6190{
6191 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6192 ssl->handshake->ciphersuite_info;
6193
6194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6195
6196 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6197 {
6198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6199 ssl->state++;
6200 return( 0 );
6201 }
6202
6203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6204 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6205}
6206
6207int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6208{
6209 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6210 ssl->handshake->ciphersuite_info;
6211
6212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6213
6214 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6215 {
6216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6217 ssl->state++;
6218 return( 0 );
6219 }
6220
6221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6222 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6223}
6224
6225#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6226/* Some certificate support -> implement write and parse */
6227
6228int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6229{
6230 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6231 size_t i, n;
6232 const mbedtls_x509_crt *crt;
6233 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6234 ssl->handshake->ciphersuite_info;
6235
6236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6237
6238 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6239 {
6240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6241 ssl->state++;
6242 return( 0 );
6243 }
6244
6245#if defined(MBEDTLS_SSL_CLI_C)
6246 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6247 {
6248 if( ssl->handshake->client_auth == 0 )
6249 {
6250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6251 ssl->state++;
6252 return( 0 );
6253 }
6254 }
6255#endif /* MBEDTLS_SSL_CLI_C */
6256#if defined(MBEDTLS_SSL_SRV_C)
6257 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6258 {
6259 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6260 {
6261 /* Should never happen because we shouldn't have picked the
6262 * ciphersuite if we don't have a certificate. */
6263 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6264 }
6265 }
6266#endif
6267
6268 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6269
6270 /*
6271 * 0 . 0 handshake type
6272 * 1 . 3 handshake length
6273 * 4 . 6 length of all certs
6274 * 7 . 9 length of cert. 1
6275 * 10 . n-1 peer certificate
6276 * n . n+2 length of cert. 2
6277 * n+3 . ... upper level cert, etc.
6278 */
6279 i = 7;
6280 crt = mbedtls_ssl_own_cert( ssl );
6281
6282 while( crt != NULL )
6283 {
6284 n = crt->raw.len;
6285 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6286 {
6287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6288 " > %" MBEDTLS_PRINTF_SIZET,
6289 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6290 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6291 }
6292
6293 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6294 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6295 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6296
6297 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6298 i += n; crt = crt->next;
6299 }
6300
6301 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6302 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6303 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6304
6305 ssl->out_msglen = i;
6306 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6307 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6308
6309 ssl->state++;
6310
6311 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6312 {
6313 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6314 return( ret );
6315 }
6316
6317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6318
6319 return( ret );
6320}
6321
6322#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6323
6324#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006325MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006326static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6327 unsigned char *crt_buf,
6328 size_t crt_buf_len )
6329{
6330 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6331
6332 if( peer_crt == NULL )
6333 return( -1 );
6334
6335 if( peer_crt->raw.len != crt_buf_len )
6336 return( -1 );
6337
6338 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6339}
6340#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006341MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006342static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6343 unsigned char *crt_buf,
6344 size_t crt_buf_len )
6345{
6346 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6347 unsigned char const * const peer_cert_digest =
6348 ssl->session->peer_cert_digest;
6349 mbedtls_md_type_t const peer_cert_digest_type =
6350 ssl->session->peer_cert_digest_type;
6351 mbedtls_md_info_t const * const digest_info =
6352 mbedtls_md_info_from_type( peer_cert_digest_type );
6353 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6354 size_t digest_len;
6355
6356 if( peer_cert_digest == NULL || digest_info == NULL )
6357 return( -1 );
6358
6359 digest_len = mbedtls_md_get_size( digest_info );
6360 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6361 return( -1 );
6362
6363 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6364 if( ret != 0 )
6365 return( -1 );
6366
6367 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6368}
6369#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6370#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6371
6372/*
6373 * Once the certificate message is read, parse it into a cert chain and
6374 * perform basic checks, but leave actual verification to the caller
6375 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006376MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006377static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6378 mbedtls_x509_crt *chain )
6379{
6380 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6381#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6382 int crt_cnt=0;
6383#endif
6384 size_t i, n;
6385 uint8_t alert;
6386
6387 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6388 {
6389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6390 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6391 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6392 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6393 }
6394
6395 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6396 {
6397 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6398 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6399 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6400 }
6401
6402 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6403 {
6404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6405 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6406 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6407 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6408 }
6409
6410 i = mbedtls_ssl_hs_hdr_len( ssl );
6411
6412 /*
6413 * Same message structure as in mbedtls_ssl_write_certificate()
6414 */
6415 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6416
6417 if( ssl->in_msg[i] != 0 ||
6418 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6419 {
6420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6421 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6422 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6423 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6424 }
6425
6426 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6427 i += 3;
6428
6429 /* Iterate through and parse the CRTs in the provided chain. */
6430 while( i < ssl->in_hslen )
6431 {
6432 /* Check that there's room for the next CRT's length fields. */
6433 if ( i + 3 > ssl->in_hslen ) {
6434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6435 mbedtls_ssl_send_alert_message( ssl,
6436 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6437 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6438 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6439 }
6440 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6441 * anything beyond 2**16 ~ 64K. */
6442 if( ssl->in_msg[i] != 0 )
6443 {
6444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6445 mbedtls_ssl_send_alert_message( ssl,
6446 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6447 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6448 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6449 }
6450
6451 /* Read length of the next CRT in the chain. */
6452 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6453 | (unsigned int) ssl->in_msg[i + 2];
6454 i += 3;
6455
6456 if( n < 128 || i + n > ssl->in_hslen )
6457 {
6458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6459 mbedtls_ssl_send_alert_message( ssl,
6460 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6461 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6462 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6463 }
6464
6465 /* Check if we're handling the first CRT in the chain. */
6466#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6467 if( crt_cnt++ == 0 &&
6468 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6469 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6470 {
6471 /* During client-side renegotiation, check that the server's
6472 * end-CRTs hasn't changed compared to the initial handshake,
6473 * mitigating the triple handshake attack. On success, reuse
6474 * the original end-CRT instead of parsing it again. */
6475 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6476 if( ssl_check_peer_crt_unchanged( ssl,
6477 &ssl->in_msg[i],
6478 n ) != 0 )
6479 {
6480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6481 mbedtls_ssl_send_alert_message( ssl,
6482 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6483 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6484 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6485 }
6486
6487 /* Now we can safely free the original chain. */
6488 ssl_clear_peer_cert( ssl->session );
6489 }
6490#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6491
6492 /* Parse the next certificate in the chain. */
6493#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6494 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6495#else
6496 /* If we don't need to store the CRT chain permanently, parse
6497 * it in-place from the input buffer instead of making a copy. */
6498 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6499#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6500 switch( ret )
6501 {
6502 case 0: /*ok*/
6503 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6504 /* Ignore certificate with an unknown algorithm: maybe a
6505 prior certificate was already trusted. */
6506 break;
6507
6508 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6509 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6510 goto crt_parse_der_failed;
6511
6512 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6513 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6514 goto crt_parse_der_failed;
6515
6516 default:
6517 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6518 crt_parse_der_failed:
6519 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6520 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6521 return( ret );
6522 }
6523
6524 i += n;
6525 }
6526
6527 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6528 return( 0 );
6529}
6530
6531#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006532MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006533static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6534{
6535 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6536 return( -1 );
6537
6538 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6539 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6540 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6541 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6542 {
Ronald Cron19385882022-06-15 16:26:13 +02006543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006544 return( 0 );
6545 }
6546 return( -1 );
6547}
6548#endif /* MBEDTLS_SSL_SRV_C */
6549
6550/* Check if a certificate message is expected.
6551 * Return either
6552 * - SSL_CERTIFICATE_EXPECTED, or
6553 * - SSL_CERTIFICATE_SKIP
6554 * indicating whether a Certificate message is expected or not.
6555 */
6556#define SSL_CERTIFICATE_EXPECTED 0
6557#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006558MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006559static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6560 int authmode )
6561{
6562 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6563 ssl->handshake->ciphersuite_info;
6564
6565 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6566 return( SSL_CERTIFICATE_SKIP );
6567
6568#if defined(MBEDTLS_SSL_SRV_C)
6569 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6570 {
6571 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6572 return( SSL_CERTIFICATE_SKIP );
6573
6574 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6575 {
6576 ssl->session_negotiate->verify_result =
6577 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6578 return( SSL_CERTIFICATE_SKIP );
6579 }
6580 }
6581#else
6582 ((void) authmode);
6583#endif /* MBEDTLS_SSL_SRV_C */
6584
6585 return( SSL_CERTIFICATE_EXPECTED );
6586}
6587
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006588MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006589static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6590 int authmode,
6591 mbedtls_x509_crt *chain,
6592 void *rs_ctx )
6593{
6594 int ret = 0;
6595 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6596 ssl->handshake->ciphersuite_info;
6597 int have_ca_chain = 0;
6598
6599 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6600 void *p_vrfy;
6601
6602 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6603 return( 0 );
6604
6605 if( ssl->f_vrfy != NULL )
6606 {
6607 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6608 f_vrfy = ssl->f_vrfy;
6609 p_vrfy = ssl->p_vrfy;
6610 }
6611 else
6612 {
6613 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6614 f_vrfy = ssl->conf->f_vrfy;
6615 p_vrfy = ssl->conf->p_vrfy;
6616 }
6617
6618 /*
6619 * Main check: verify certificate
6620 */
6621#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6622 if( ssl->conf->f_ca_cb != NULL )
6623 {
6624 ((void) rs_ctx);
6625 have_ca_chain = 1;
6626
6627 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6628 ret = mbedtls_x509_crt_verify_with_ca_cb(
6629 chain,
6630 ssl->conf->f_ca_cb,
6631 ssl->conf->p_ca_cb,
6632 ssl->conf->cert_profile,
6633 ssl->hostname,
6634 &ssl->session_negotiate->verify_result,
6635 f_vrfy, p_vrfy );
6636 }
6637 else
6638#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6639 {
6640 mbedtls_x509_crt *ca_chain;
6641 mbedtls_x509_crl *ca_crl;
6642
6643#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6644 if( ssl->handshake->sni_ca_chain != NULL )
6645 {
6646 ca_chain = ssl->handshake->sni_ca_chain;
6647 ca_crl = ssl->handshake->sni_ca_crl;
6648 }
6649 else
6650#endif
6651 {
6652 ca_chain = ssl->conf->ca_chain;
6653 ca_crl = ssl->conf->ca_crl;
6654 }
6655
6656 if( ca_chain != NULL )
6657 have_ca_chain = 1;
6658
6659 ret = mbedtls_x509_crt_verify_restartable(
6660 chain,
6661 ca_chain, ca_crl,
6662 ssl->conf->cert_profile,
6663 ssl->hostname,
6664 &ssl->session_negotiate->verify_result,
6665 f_vrfy, p_vrfy, rs_ctx );
6666 }
6667
6668 if( ret != 0 )
6669 {
6670 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6671 }
6672
6673#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6674 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6675 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6676#endif
6677
6678 /*
6679 * Secondary checks: always done, but change 'ret' only if it was 0
6680 */
6681
6682#if defined(MBEDTLS_ECP_C)
6683 {
6684 const mbedtls_pk_context *pk = &chain->pk;
6685
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006686 /* If certificate uses an EC key, make sure the curve is OK.
6687 * This is a public key, so it can't be opaque, so can_do() is a good
6688 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006689 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006690 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006691 /* and in the unlikely case the above assumption no longer holds
6692 * we are making sure that pk_ec() here does not return a NULL
6693 */
6694 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6695 if( ec == NULL )
6696 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006698 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6699 }
Jerry Yud9526692022-02-17 14:23:47 +08006700
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006701 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6702 {
6703 ssl->session_negotiate->verify_result |=
6704 MBEDTLS_X509_BADCERT_BAD_KEY;
6705
6706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6707 if( ret == 0 )
6708 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6709 }
Jerry Yud9526692022-02-17 14:23:47 +08006710 }
6711 }
6712#endif /* MBEDTLS_ECP_C */
6713
6714 if( mbedtls_ssl_check_cert_usage( chain,
6715 ciphersuite_info,
6716 ! ssl->conf->endpoint,
6717 &ssl->session_negotiate->verify_result ) != 0 )
6718 {
6719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6720 if( ret == 0 )
6721 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6722 }
6723
6724 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6725 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6726 * with details encoded in the verification flags. All other kinds
6727 * of error codes, including those from the user provided f_vrfy
6728 * functions, are treated as fatal and lead to a failure of
6729 * ssl_parse_certificate even if verification was optional. */
6730 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6731 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6732 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6733 {
6734 ret = 0;
6735 }
6736
6737 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6738 {
6739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6740 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6741 }
6742
6743 if( ret != 0 )
6744 {
6745 uint8_t alert;
6746
6747 /* The certificate may have been rejected for several reasons.
6748 Pick one and send the corresponding alert. Which alert to send
6749 may be a subject of debate in some cases. */
6750 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6751 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6752 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6753 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6754 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6755 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6756 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6757 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6758 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6759 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6760 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6761 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6762 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6763 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6764 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6765 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6766 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6767 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6768 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6769 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6770 else
6771 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6772 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6773 alert );
6774 }
6775
6776#if defined(MBEDTLS_DEBUG_C)
6777 if( ssl->session_negotiate->verify_result != 0 )
6778 {
6779 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6780 (unsigned int) ssl->session_negotiate->verify_result ) );
6781 }
6782 else
6783 {
6784 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6785 }
6786#endif /* MBEDTLS_DEBUG_C */
6787
6788 return( ret );
6789}
6790
6791#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006792MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006793static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6794 unsigned char *start, size_t len )
6795{
6796 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6797 /* Remember digest of the peer's end-CRT. */
6798 ssl->session_negotiate->peer_cert_digest =
6799 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6800 if( ssl->session_negotiate->peer_cert_digest == NULL )
6801 {
6802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6803 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6804 mbedtls_ssl_send_alert_message( ssl,
6805 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6806 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6807
6808 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6809 }
6810
6811 ret = mbedtls_md( mbedtls_md_info_from_type(
6812 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6813 start, len,
6814 ssl->session_negotiate->peer_cert_digest );
6815
6816 ssl->session_negotiate->peer_cert_digest_type =
6817 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6818 ssl->session_negotiate->peer_cert_digest_len =
6819 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6820
6821 return( ret );
6822}
6823
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006824MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006825static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6826 unsigned char *start, size_t len )
6827{
6828 unsigned char *end = start + len;
6829 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6830
6831 /* Make a copy of the peer's raw public key. */
6832 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6833 ret = mbedtls_pk_parse_subpubkey( &start, end,
6834 &ssl->handshake->peer_pubkey );
6835 if( ret != 0 )
6836 {
6837 /* We should have parsed the public key before. */
6838 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6839 }
6840
6841 return( 0 );
6842}
6843#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6844
6845int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6846{
6847 int ret = 0;
6848 int crt_expected;
6849#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6850 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6851 ? ssl->handshake->sni_authmode
6852 : ssl->conf->authmode;
6853#else
6854 const int authmode = ssl->conf->authmode;
6855#endif
6856 void *rs_ctx = NULL;
6857 mbedtls_x509_crt *chain = NULL;
6858
6859 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6860
6861 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6862 if( crt_expected == SSL_CERTIFICATE_SKIP )
6863 {
6864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6865 goto exit;
6866 }
6867
6868#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6869 if( ssl->handshake->ecrs_enabled &&
6870 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6871 {
6872 chain = ssl->handshake->ecrs_peer_cert;
6873 ssl->handshake->ecrs_peer_cert = NULL;
6874 goto crt_verify;
6875 }
6876#endif
6877
6878 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6879 {
6880 /* mbedtls_ssl_read_record may have sent an alert already. We
6881 let it decide whether to alert. */
6882 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6883 goto exit;
6884 }
6885
6886#if defined(MBEDTLS_SSL_SRV_C)
6887 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6888 {
6889 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6890
6891 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6892 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6893
6894 goto exit;
6895 }
6896#endif /* MBEDTLS_SSL_SRV_C */
6897
6898 /* Clear existing peer CRT structure in case we tried to
6899 * reuse a session but it failed, and allocate a new one. */
6900 ssl_clear_peer_cert( ssl->session_negotiate );
6901
6902 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6903 if( chain == NULL )
6904 {
6905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6906 sizeof( mbedtls_x509_crt ) ) );
6907 mbedtls_ssl_send_alert_message( ssl,
6908 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6909 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6910
6911 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6912 goto exit;
6913 }
6914 mbedtls_x509_crt_init( chain );
6915
6916 ret = ssl_parse_certificate_chain( ssl, chain );
6917 if( ret != 0 )
6918 goto exit;
6919
6920#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6921 if( ssl->handshake->ecrs_enabled)
6922 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6923
6924crt_verify:
6925 if( ssl->handshake->ecrs_enabled)
6926 rs_ctx = &ssl->handshake->ecrs_ctx;
6927#endif
6928
6929 ret = ssl_parse_certificate_verify( ssl, authmode,
6930 chain, rs_ctx );
6931 if( ret != 0 )
6932 goto exit;
6933
6934#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6935 {
6936 unsigned char *crt_start, *pk_start;
6937 size_t crt_len, pk_len;
6938
6939 /* We parse the CRT chain without copying, so
6940 * these pointers point into the input buffer,
6941 * and are hence still valid after freeing the
6942 * CRT chain. */
6943
6944 crt_start = chain->raw.p;
6945 crt_len = chain->raw.len;
6946
6947 pk_start = chain->pk_raw.p;
6948 pk_len = chain->pk_raw.len;
6949
6950 /* Free the CRT structures before computing
6951 * digest and copying the peer's public key. */
6952 mbedtls_x509_crt_free( chain );
6953 mbedtls_free( chain );
6954 chain = NULL;
6955
6956 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6957 if( ret != 0 )
6958 goto exit;
6959
6960 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6961 if( ret != 0 )
6962 goto exit;
6963 }
6964#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6965 /* Pass ownership to session structure. */
6966 ssl->session_negotiate->peer_cert = chain;
6967 chain = NULL;
6968#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6969
6970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6971
6972exit:
6973
6974 if( ret == 0 )
6975 ssl->state++;
6976
6977#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6978 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6979 {
6980 ssl->handshake->ecrs_peer_cert = chain;
6981 chain = NULL;
6982 }
6983#endif
6984
6985 if( chain != NULL )
6986 {
6987 mbedtls_x509_crt_free( chain );
6988 mbedtls_free( chain );
6989 }
6990
6991 return( ret );
6992}
6993#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6994
Andrzej Kurek25f27152022-08-17 16:09:31 -04006995#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08006996static void ssl_calc_finished_tls_sha256(
6997 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6998{
6999 int len = 12;
7000 const char *sender;
7001 unsigned char padbuf[32];
7002#if defined(MBEDTLS_USE_PSA_CRYPTO)
7003 size_t hash_size;
7004 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7005 psa_status_t status;
7006#else
7007 mbedtls_sha256_context sha256;
7008#endif
7009
7010 mbedtls_ssl_session *session = ssl->session_negotiate;
7011 if( !session )
7012 session = ssl->session;
7013
7014 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7015 ? "client finished"
7016 : "server finished";
7017
7018#if defined(MBEDTLS_USE_PSA_CRYPTO)
7019 sha256_psa = psa_hash_operation_init();
7020
7021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7022
7023 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7024 if( status != PSA_SUCCESS )
7025 {
7026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7027 return;
7028 }
7029
7030 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7031 if( status != PSA_SUCCESS )
7032 {
7033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7034 return;
7035 }
7036 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7037#else
7038
7039 mbedtls_sha256_init( &sha256 );
7040
7041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7042
7043 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7044
7045 /*
7046 * TLSv1.2:
7047 * hash = PRF( master, finished_label,
7048 * Hash( handshake ) )[0.11]
7049 */
7050
7051#if !defined(MBEDTLS_SHA256_ALT)
7052 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7053 sha256.state, sizeof( sha256.state ) );
7054#endif
7055
7056 mbedtls_sha256_finish( &sha256, padbuf );
7057 mbedtls_sha256_free( &sha256 );
7058#endif /* MBEDTLS_USE_PSA_CRYPTO */
7059
7060 ssl->handshake->tls_prf( session->master, 48, sender,
7061 padbuf, 32, buf, len );
7062
7063 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7064
7065 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7066
7067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7068}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007069#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007070
Jerry Yub7ba49e2022-02-17 14:25:53 +08007071
Andrzej Kurek25f27152022-08-17 16:09:31 -04007072#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007073static void ssl_calc_finished_tls_sha384(
7074 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7075{
7076 int len = 12;
7077 const char *sender;
7078 unsigned char padbuf[48];
7079#if defined(MBEDTLS_USE_PSA_CRYPTO)
7080 size_t hash_size;
7081 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7082 psa_status_t status;
7083#else
7084 mbedtls_sha512_context sha512;
7085#endif
7086
7087 mbedtls_ssl_session *session = ssl->session_negotiate;
7088 if( !session )
7089 session = ssl->session;
7090
7091 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7092 ? "client finished"
7093 : "server finished";
7094
7095#if defined(MBEDTLS_USE_PSA_CRYPTO)
7096 sha384_psa = psa_hash_operation_init();
7097
7098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7099
7100 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7101 if( status != PSA_SUCCESS )
7102 {
7103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7104 return;
7105 }
7106
7107 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7108 if( status != PSA_SUCCESS )
7109 {
7110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7111 return;
7112 }
7113 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7114#else
7115 mbedtls_sha512_init( &sha512 );
7116
7117 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7118
Andrzej Kureka242e832022-08-11 10:03:14 -04007119 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007120
7121 /*
7122 * TLSv1.2:
7123 * hash = PRF( master, finished_label,
7124 * Hash( handshake ) )[0.11]
7125 */
7126
7127#if !defined(MBEDTLS_SHA512_ALT)
7128 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7129 sha512.state, sizeof( sha512.state ) );
7130#endif
7131 mbedtls_sha512_finish( &sha512, padbuf );
7132
7133 mbedtls_sha512_free( &sha512 );
7134#endif
7135
7136 ssl->handshake->tls_prf( session->master, 48, sender,
7137 padbuf, 48, buf, len );
7138
7139 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7140
7141 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7142
7143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7144}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007145#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007146
Jerry Yuaef00152022-02-17 14:27:31 +08007147void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7148{
7149 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7150
7151 /*
7152 * Free our handshake params
7153 */
7154 mbedtls_ssl_handshake_free( ssl );
7155 mbedtls_free( ssl->handshake );
7156 ssl->handshake = NULL;
7157
7158 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007159 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007160 */
7161 if( ssl->transform )
7162 {
7163 mbedtls_ssl_transform_free( ssl->transform );
7164 mbedtls_free( ssl->transform );
7165 }
7166 ssl->transform = ssl->transform_negotiate;
7167 ssl->transform_negotiate = NULL;
7168
7169 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7170}
7171
Jerry Yu2a9fff52022-02-17 14:28:51 +08007172void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7173{
7174 int resume = ssl->handshake->resume;
7175
7176 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7177
7178#if defined(MBEDTLS_SSL_RENEGOTIATION)
7179 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7180 {
7181 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7182 ssl->renego_records_seen = 0;
7183 }
7184#endif
7185
7186 /*
7187 * Free the previous session and switch in the current one
7188 */
7189 if( ssl->session )
7190 {
7191#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7192 /* RFC 7366 3.1: keep the EtM state */
7193 ssl->session_negotiate->encrypt_then_mac =
7194 ssl->session->encrypt_then_mac;
7195#endif
7196
7197 mbedtls_ssl_session_free( ssl->session );
7198 mbedtls_free( ssl->session );
7199 }
7200 ssl->session = ssl->session_negotiate;
7201 ssl->session_negotiate = NULL;
7202
7203 /*
7204 * Add cache entry
7205 */
7206 if( ssl->conf->f_set_cache != NULL &&
7207 ssl->session->id_len != 0 &&
7208 resume == 0 )
7209 {
7210 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7211 ssl->session->id,
7212 ssl->session->id_len,
7213 ssl->session ) != 0 )
7214 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7215 }
7216
7217#if defined(MBEDTLS_SSL_PROTO_DTLS)
7218 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7219 ssl->handshake->flight != NULL )
7220 {
7221 /* Cancel handshake timer */
7222 mbedtls_ssl_set_timer( ssl, 0 );
7223
7224 /* Keep last flight around in case we need to resend it:
7225 * we need the handshake and transform structures for that */
7226 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7227 }
7228 else
7229#endif
7230 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7231
7232 ssl->state++;
7233
7234 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7235}
7236
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007237int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7238{
7239 int ret, hash_len;
7240
7241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7242
7243 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7244
7245 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7246
7247 /*
7248 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7249 * may define some other value. Currently (early 2016), no defined
7250 * ciphersuite does this (and this is unlikely to change as activity has
7251 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7252 */
7253 hash_len = 12;
7254
7255#if defined(MBEDTLS_SSL_RENEGOTIATION)
7256 ssl->verify_data_len = hash_len;
7257 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7258#endif
7259
7260 ssl->out_msglen = 4 + hash_len;
7261 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7262 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7263
7264 /*
7265 * In case of session resuming, invert the client and server
7266 * ChangeCipherSpec messages order.
7267 */
7268 if( ssl->handshake->resume != 0 )
7269 {
7270#if defined(MBEDTLS_SSL_CLI_C)
7271 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7272 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7273#endif
7274#if defined(MBEDTLS_SSL_SRV_C)
7275 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7276 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7277#endif
7278 }
7279 else
7280 ssl->state++;
7281
7282 /*
7283 * Switch to our negotiated transform and session parameters for outbound
7284 * data.
7285 */
7286 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7287
7288#if defined(MBEDTLS_SSL_PROTO_DTLS)
7289 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7290 {
7291 unsigned char i;
7292
7293 /* Remember current epoch settings for resending */
7294 ssl->handshake->alt_transform_out = ssl->transform_out;
7295 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7296 sizeof( ssl->handshake->alt_out_ctr ) );
7297
7298 /* Set sequence_number to zero */
7299 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7300
7301
7302 /* Increment epoch */
7303 for( i = 2; i > 0; i-- )
7304 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7305 break;
7306
7307 /* The loop goes to its end iff the counter is wrapping */
7308 if( i == 0 )
7309 {
7310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7311 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7312 }
7313 }
7314 else
7315#endif /* MBEDTLS_SSL_PROTO_DTLS */
7316 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7317
7318 ssl->transform_out = ssl->transform_negotiate;
7319 ssl->session_out = ssl->session_negotiate;
7320
7321#if defined(MBEDTLS_SSL_PROTO_DTLS)
7322 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7323 mbedtls_ssl_send_flight_completed( ssl );
7324#endif
7325
7326 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7327 {
7328 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7329 return( ret );
7330 }
7331
7332#if defined(MBEDTLS_SSL_PROTO_DTLS)
7333 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7334 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7335 {
7336 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7337 return( ret );
7338 }
7339#endif
7340
7341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7342
7343 return( 0 );
7344}
7345
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007346#define SSL_MAX_HASH_LEN 12
7347
7348int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7349{
7350 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7351 unsigned int hash_len = 12;
7352 unsigned char buf[SSL_MAX_HASH_LEN];
7353
7354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7355
7356 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7357
7358 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7359 {
7360 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7361 goto exit;
7362 }
7363
7364 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7365 {
7366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7367 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7368 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7369 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7370 goto exit;
7371 }
7372
7373 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7374 {
7375 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7376 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7377 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7378 goto exit;
7379 }
7380
7381 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7382 {
7383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7384 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7385 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7386 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7387 goto exit;
7388 }
7389
7390 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7391 buf, hash_len ) != 0 )
7392 {
7393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7394 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7395 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7396 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7397 goto exit;
7398 }
7399
7400#if defined(MBEDTLS_SSL_RENEGOTIATION)
7401 ssl->verify_data_len = hash_len;
7402 memcpy( ssl->peer_verify_data, buf, hash_len );
7403#endif
7404
7405 if( ssl->handshake->resume != 0 )
7406 {
7407#if defined(MBEDTLS_SSL_CLI_C)
7408 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7409 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7410#endif
7411#if defined(MBEDTLS_SSL_SRV_C)
7412 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7413 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7414#endif
7415 }
7416 else
7417 ssl->state++;
7418
7419#if defined(MBEDTLS_SSL_PROTO_DTLS)
7420 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7421 mbedtls_ssl_recv_flight_completed( ssl );
7422#endif
7423
7424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7425
7426exit:
7427 mbedtls_platform_zeroize( buf, hash_len );
7428 return( ret );
7429}
7430
Jerry Yu392112c2022-02-17 14:34:10 +08007431#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7432/*
7433 * Helper to get TLS 1.2 PRF from ciphersuite
7434 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7435 */
Andrzej Kurek894edde2022-09-29 06:31:14 -04007436#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \
7437 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu392112c2022-02-17 14:34:10 +08007438static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7439{
Andrzej Kurek25f27152022-08-17 16:09:31 -04007440#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu392112c2022-02-17 14:34:10 +08007441 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7442 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7443
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007444 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007445 return( tls_prf_sha384 );
Andrzej Kurek894edde2022-09-29 06:31:14 -04007446 else
Jerry Yu392112c2022-02-17 14:34:10 +08007447#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007448#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7449 {
7450 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
7451 return( tls_prf_sha256 );
7452 }
7453#endif
7454#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7455 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7456 (void) ciphersuite_info;
7457#endif
7458 return( NULL );
Jerry Yu392112c2022-02-17 14:34:10 +08007459}
Andrzej Kurek894edde2022-09-29 06:31:14 -04007460#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA ||
7461 MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu392112c2022-02-17 14:34:10 +08007462#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007463
7464static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7465{
7466 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007467#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007468 if( tls_prf == tls_prf_sha384 )
7469 {
7470 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7471 }
7472 else
7473#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007474#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007475 if( tls_prf == tls_prf_sha256 )
7476 {
7477 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7478 }
7479 else
7480#endif
7481 return( MBEDTLS_SSL_TLS_PRF_NONE );
7482}
7483
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007484/*
7485 * Populate a transform structure with session keys and all the other
7486 * necessary information.
7487 *
7488 * Parameters:
7489 * - [in/out]: transform: structure to populate
7490 * [in] must be just initialised with mbedtls_ssl_transform_init()
7491 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7492 * - [in] ciphersuite
7493 * - [in] master
7494 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007495 * - [in] tls_prf: pointer to PRF to use for key derivation
7496 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007497 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007498 * - [in] endpoint: client or server
7499 * - [in] ssl: used for:
7500 * - ssl->conf->{f,p}_export_keys
7501 * [in] optionally used for:
7502 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7503 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007504MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007505static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7506 int ciphersuite,
7507 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007508#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007509 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007510#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007511 ssl_tls_prf_t tls_prf,
7512 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007513 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007514 unsigned endpoint,
7515 const mbedtls_ssl_context *ssl )
7516{
7517 int ret = 0;
7518 unsigned char keyblk[256];
7519 unsigned char *key1;
7520 unsigned char *key2;
7521 unsigned char *mac_enc;
7522 unsigned char *mac_dec;
7523 size_t mac_key_len = 0;
7524 size_t iv_copy_len;
7525 size_t keylen;
7526 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007527 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007528#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007529 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007530 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007531#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007532
7533#if defined(MBEDTLS_USE_PSA_CRYPTO)
7534 psa_key_type_t key_type;
7535 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7536 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007537 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007538 size_t key_bits;
7539 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7540#endif
7541
7542#if !defined(MBEDTLS_DEBUG_C) && \
7543 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7544 if( ssl->f_export_keys == NULL )
7545 {
7546 ssl = NULL; /* make sure we don't use it except for these cases */
7547 (void) ssl;
7548 }
7549#endif
7550
7551 /*
7552 * Some data just needs copying into the structure
7553 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007554#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007555 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007556#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007557 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007558
7559#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7560 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7561#endif
7562
7563#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007564 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007565 {
7566 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7567 * generation separate. This should never happen. */
7568 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7569 }
7570#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7571
7572 /*
7573 * Get various info structures
7574 */
7575 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7576 if( ciphersuite_info == NULL )
7577 {
7578 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7579 ciphersuite ) );
7580 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7581 }
7582
Neil Armstrongab555e02022-04-04 11:07:59 +02007583 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007584#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007585 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007586#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007587 ciphersuite_info );
7588
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007589 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7590 transform->taglen =
7591 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7592
7593#if defined(MBEDTLS_USE_PSA_CRYPTO)
7594 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7595 transform->taglen,
7596 &alg,
7597 &key_type,
7598 &key_bits ) ) != PSA_SUCCESS )
7599 {
7600 ret = psa_ssl_status_to_mbedtls( status );
7601 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7602 goto end;
7603 }
7604#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007605 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7606 if( cipher_info == NULL )
7607 {
7608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7609 ciphersuite_info->cipher ) );
7610 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7611 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007612#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007613
Neil Armstronge4512952022-03-08 09:08:22 +01007614#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007615 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007616 if( mac_alg == 0 )
7617 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007619 (unsigned) ciphersuite_info->mac ) );
7620 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7621 }
7622#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007623 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7624 if( md_info == NULL )
7625 {
7626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7627 (unsigned) ciphersuite_info->mac ) );
7628 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7629 }
Neil Armstronge4512952022-03-08 09:08:22 +01007630#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007631
7632#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7633 /* Copy own and peer's CID if the use of the CID
7634 * extension has been negotiated. */
7635 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7636 {
7637 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7638
7639 transform->in_cid_len = ssl->own_cid_len;
7640 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7641 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7642 transform->in_cid_len );
7643
7644 transform->out_cid_len = ssl->handshake->peer_cid_len;
7645 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7646 ssl->handshake->peer_cid_len );
7647 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7648 transform->out_cid_len );
7649 }
7650#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7651
7652 /*
7653 * Compute key block using the PRF
7654 */
7655 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7656 if( ret != 0 )
7657 {
7658 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7659 return( ret );
7660 }
7661
7662 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7663 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7664 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7665 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7666 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7667
7668 /*
7669 * Determine the appropriate key, IV and MAC length.
7670 */
7671
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007672#if defined(MBEDTLS_USE_PSA_CRYPTO)
7673 keylen = PSA_BITS_TO_BYTES(key_bits);
7674#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007675 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007676#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007677
7678#if defined(MBEDTLS_GCM_C) || \
7679 defined(MBEDTLS_CCM_C) || \
7680 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007681 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007682 {
7683 size_t explicit_ivlen;
7684
7685 transform->maclen = 0;
7686 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007687
7688 /* All modes haves 96-bit IVs, but the length of the static parts vary
7689 * with mode and version:
7690 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7691 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7692 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7693 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7694 * sequence number).
7695 */
7696 transform->ivlen = 12;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007697#if defined(MBEDTLS_USE_PSA_CRYPTO)
7698 if( key_type == PSA_KEY_TYPE_CHACHA20 )
7699#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007700 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007701#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007702 transform->fixed_ivlen = 12;
7703 else
7704 transform->fixed_ivlen = 4;
7705
7706 /* Minimum length of encrypted record */
7707 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7708 transform->minlen = explicit_ivlen + transform->taglen;
7709 }
7710 else
7711#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7712#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007713 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7714 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7715 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007716 {
Neil Armstronge4512952022-03-08 09:08:22 +01007717#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007718 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007719#else
7720 size_t block_size = cipher_info->block_size;
7721#endif /* MBEDTLS_USE_PSA_CRYPTO */
7722
7723#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007724 /* Get MAC length */
7725 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7726#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007727 /* Initialize HMAC contexts */
7728 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7729 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7730 {
7731 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7732 goto end;
7733 }
7734
7735 /* Get MAC length */
7736 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007737#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007738 transform->maclen = mac_key_len;
7739
7740 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007741#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007742 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007743#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007744 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007745#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007746
7747 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007748 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007749 transform->minlen = transform->maclen;
7750 else
7751 {
7752 /*
7753 * GenericBlockCipher:
7754 * 1. if EtM is in use: one block plus MAC
7755 * otherwise: * first multiple of blocklen greater than maclen
7756 * 2. IV
7757 */
7758#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007759 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007760 {
7761 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007762 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007763 }
7764 else
7765#endif
7766 {
7767 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007768 + block_size
7769 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007770 }
7771
Glenn Strauss07c64162022-03-14 12:34:51 -04007772 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007773 {
7774 transform->minlen += transform->ivlen;
7775 }
7776 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007777 {
7778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7779 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7780 goto end;
7781 }
7782 }
7783 }
7784 else
7785#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7786 {
7787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7788 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7789 }
7790
7791 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7792 (unsigned) keylen,
7793 (unsigned) transform->minlen,
7794 (unsigned) transform->ivlen,
7795 (unsigned) transform->maclen ) );
7796
7797 /*
7798 * Finally setup the cipher contexts, IVs and MAC secrets.
7799 */
7800#if defined(MBEDTLS_SSL_CLI_C)
7801 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7802 {
7803 key1 = keyblk + mac_key_len * 2;
7804 key2 = keyblk + mac_key_len * 2 + keylen;
7805
7806 mac_enc = keyblk;
7807 mac_dec = keyblk + mac_key_len;
7808
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007809 iv_copy_len = ( transform->fixed_ivlen ) ?
7810 transform->fixed_ivlen : transform->ivlen;
7811 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7812 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7813 iv_copy_len );
7814 }
7815 else
7816#endif /* MBEDTLS_SSL_CLI_C */
7817#if defined(MBEDTLS_SSL_SRV_C)
7818 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7819 {
7820 key1 = keyblk + mac_key_len * 2 + keylen;
7821 key2 = keyblk + mac_key_len * 2;
7822
7823 mac_enc = keyblk + mac_key_len;
7824 mac_dec = keyblk;
7825
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007826 iv_copy_len = ( transform->fixed_ivlen ) ?
7827 transform->fixed_ivlen : transform->ivlen;
7828 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7829 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7830 iv_copy_len );
7831 }
7832 else
7833#endif /* MBEDTLS_SSL_SRV_C */
7834 {
7835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7836 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7837 goto end;
7838 }
7839
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007840 if( ssl != NULL && ssl->f_export_keys != NULL )
7841 {
7842 ssl->f_export_keys( ssl->p_export_keys,
7843 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7844 master, 48,
7845 randbytes + 32,
7846 randbytes,
7847 tls_prf_get_type( tls_prf ) );
7848 }
7849
7850#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007851 transform->psa_alg = alg;
7852
7853 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7854 {
7855 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7856 psa_set_key_algorithm( &attributes, alg );
7857 psa_set_key_type( &attributes, key_type );
7858
7859 if( ( status = psa_import_key( &attributes,
7860 key1,
7861 PSA_BITS_TO_BYTES( key_bits ),
7862 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7863 {
7864 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7865 ret = psa_ssl_status_to_mbedtls( status );
7866 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7867 goto end;
7868 }
7869
7870 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7871
7872 if( ( status = psa_import_key( &attributes,
7873 key2,
7874 PSA_BITS_TO_BYTES( key_bits ),
7875 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7876 {
7877 ret = psa_ssl_status_to_mbedtls( status );
7878 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7879 goto end;
7880 }
7881 }
7882#else
7883 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7884 cipher_info ) ) != 0 )
7885 {
7886 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7887 goto end;
7888 }
7889
7890 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7891 cipher_info ) ) != 0 )
7892 {
7893 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7894 goto end;
7895 }
7896
7897 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7898 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7899 MBEDTLS_ENCRYPT ) ) != 0 )
7900 {
7901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7902 goto end;
7903 }
7904
7905 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7906 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7907 MBEDTLS_DECRYPT ) ) != 0 )
7908 {
7909 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7910 goto end;
7911 }
7912
7913#if defined(MBEDTLS_CIPHER_MODE_CBC)
7914 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7915 {
7916 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7917 MBEDTLS_PADDING_NONE ) ) != 0 )
7918 {
7919 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7920 goto end;
7921 }
7922
7923 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7924 MBEDTLS_PADDING_NONE ) ) != 0 )
7925 {
7926 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7927 goto end;
7928 }
7929 }
7930#endif /* MBEDTLS_CIPHER_MODE_CBC */
7931#endif /* MBEDTLS_USE_PSA_CRYPTO */
7932
Neil Armstrong29c0c042022-03-17 17:47:28 +01007933#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7934 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7935 For AEAD-based ciphersuites, there is nothing to do here. */
7936 if( mac_key_len != 0 )
7937 {
7938#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007939 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007940
7941 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007942 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007943 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7944
7945 if( ( status = psa_import_key( &attributes,
7946 mac_enc, mac_key_len,
7947 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7948 {
7949 ret = psa_ssl_status_to_mbedtls( status );
7950 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7951 goto end;
7952 }
7953
Ronald Cronfb39f152022-03-25 14:36:28 +01007954 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04007955 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
7956#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
7957 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
7958#endif
7959 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007960 /* mbedtls_ct_hmac() requires the key to be exportable */
7961 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7962 PSA_KEY_USAGE_VERIFY_HASH );
7963 else
7964 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7965
7966 if( ( status = psa_import_key( &attributes,
7967 mac_dec, mac_key_len,
7968 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7969 {
7970 ret = psa_ssl_status_to_mbedtls( status );
7971 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7972 goto end;
7973 }
7974#else
7975 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7976 if( ret != 0 )
7977 goto end;
7978 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7979 if( ret != 0 )
7980 goto end;
7981#endif /* MBEDTLS_USE_PSA_CRYPTO */
7982 }
7983#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7984
7985 ((void) mac_dec);
7986 ((void) mac_enc);
7987
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007988end:
7989 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7990 return( ret );
7991}
7992
Jerry Yuee40f9d2022-02-17 14:55:16 +08007993#if defined(MBEDTLS_USE_PSA_CRYPTO)
7994int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7995 unsigned char *hash, size_t *hashlen,
7996 unsigned char *data, size_t data_len,
7997 mbedtls_md_type_t md_alg )
7998{
7999 psa_status_t status;
8000 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008001 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008002
8003 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8004
8005 if( ( status = psa_hash_setup( &hash_operation,
8006 hash_alg ) ) != PSA_SUCCESS )
8007 {
8008 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8009 goto exit;
8010 }
8011
8012 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8013 64 ) ) != PSA_SUCCESS )
8014 {
8015 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8016 goto exit;
8017 }
8018
8019 if( ( status = psa_hash_update( &hash_operation,
8020 data, data_len ) ) != PSA_SUCCESS )
8021 {
8022 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8023 goto exit;
8024 }
8025
8026 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8027 hashlen ) ) != PSA_SUCCESS )
8028 {
8029 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8030 goto exit;
8031 }
8032
8033exit:
8034 if( status != PSA_SUCCESS )
8035 {
8036 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8037 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8038 switch( status )
8039 {
8040 case PSA_ERROR_NOT_SUPPORTED:
8041 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8042 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8043 case PSA_ERROR_BUFFER_TOO_SMALL:
8044 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8045 case PSA_ERROR_INSUFFICIENT_MEMORY:
8046 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8047 default:
8048 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8049 }
8050 }
8051 return( 0 );
8052}
8053
8054#else
8055
8056int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8057 unsigned char *hash, size_t *hashlen,
8058 unsigned char *data, size_t data_len,
8059 mbedtls_md_type_t md_alg )
8060{
8061 int ret = 0;
8062 mbedtls_md_context_t ctx;
8063 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8064 *hashlen = mbedtls_md_get_size( md_info );
8065
8066 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8067
8068 mbedtls_md_init( &ctx );
8069
8070 /*
8071 * digitally-signed struct {
8072 * opaque client_random[32];
8073 * opaque server_random[32];
8074 * ServerDHParams params;
8075 * };
8076 */
8077 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8078 {
8079 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8080 goto exit;
8081 }
8082 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8083 {
8084 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8085 goto exit;
8086 }
8087 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8088 {
8089 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8090 goto exit;
8091 }
8092 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8093 {
8094 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8095 goto exit;
8096 }
8097 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8098 {
8099 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8100 goto exit;
8101 }
8102
8103exit:
8104 mbedtls_md_free( &ctx );
8105
8106 if( ret != 0 )
8107 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8108 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8109
8110 return( ret );
8111}
8112#endif /* MBEDTLS_USE_PSA_CRYPTO */
8113
Jerry Yud9d91da2022-02-17 14:57:06 +08008114#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8115
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008116/* Find the preferred hash for a given signature algorithm. */
8117unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8118 mbedtls_ssl_context *ssl,
8119 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008120{
Gabor Mezei078e8032022-04-27 21:17:56 +02008121 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008122 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008123
8124 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008125 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008126
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008127 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008128 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008129 unsigned int hash_alg_received =
8130 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8131 received_sig_algs[i] );
8132 unsigned int sig_alg_received =
8133 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8134 received_sig_algs[i] );
8135
8136 if( sig_alg == sig_alg_received )
8137 {
8138#if defined(MBEDTLS_USE_PSA_CRYPTO)
8139 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8140 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008141 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008142 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008143
Neil Armstrong96eceb82022-06-30 18:05:05 +02008144 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8145 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8146 PSA_ALG_ECDSA( psa_hash_alg ),
8147 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008148 continue;
8149
Neil Armstrong96eceb82022-06-30 18:05:05 +02008150 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008151 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8152 PSA_ALG_RSA_PKCS1V15_SIGN(
8153 psa_hash_alg ),
8154 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008155 continue;
8156 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008157#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008158
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008159 return( hash_alg_received );
8160 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008161 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008162
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008163 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008164}
8165
8166#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8167
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008168/* Serialization of TLS 1.2 sessions:
8169 *
8170 * struct {
8171 * uint64 start_time;
8172 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008173 * uint8 session_id_len; // at most 32
8174 * opaque session_id[32];
8175 * opaque master[48]; // fixed length in the standard
8176 * uint32 verify_result;
8177 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8178 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8179 * uint32 ticket_lifetime;
8180 * uint8 mfl_code; // up to 255 according to standard
8181 * uint8 encrypt_then_mac; // 0 or 1
8182 * } serialized_session_tls12;
8183 *
8184 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008185static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008186 unsigned char *buf,
8187 size_t buf_len )
8188{
8189 unsigned char *p = buf;
8190 size_t used = 0;
8191
8192#if defined(MBEDTLS_HAVE_TIME)
8193 uint64_t start;
8194#endif
8195#if defined(MBEDTLS_X509_CRT_PARSE_C)
8196#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8197 size_t cert_len;
8198#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8199#endif /* MBEDTLS_X509_CRT_PARSE_C */
8200
8201 /*
8202 * Time
8203 */
8204#if defined(MBEDTLS_HAVE_TIME)
8205 used += 8;
8206
8207 if( used <= buf_len )
8208 {
8209 start = (uint64_t) session->start;
8210
8211 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8212 p += 8;
8213 }
8214#endif /* MBEDTLS_HAVE_TIME */
8215
8216 /*
8217 * Basic mandatory fields
8218 */
8219 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008220 + 1 /* id_len */
8221 + sizeof( session->id )
8222 + sizeof( session->master )
8223 + 4; /* verify_result */
8224
8225 if( used <= buf_len )
8226 {
8227 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8228 p += 2;
8229
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008230 *p++ = MBEDTLS_BYTE_0( session->id_len );
8231 memcpy( p, session->id, 32 );
8232 p += 32;
8233
8234 memcpy( p, session->master, 48 );
8235 p += 48;
8236
8237 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8238 p += 4;
8239 }
8240
8241 /*
8242 * Peer's end-entity certificate
8243 */
8244#if defined(MBEDTLS_X509_CRT_PARSE_C)
8245#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8246 if( session->peer_cert == NULL )
8247 cert_len = 0;
8248 else
8249 cert_len = session->peer_cert->raw.len;
8250
8251 used += 3 + cert_len;
8252
8253 if( used <= buf_len )
8254 {
8255 *p++ = MBEDTLS_BYTE_2( cert_len );
8256 *p++ = MBEDTLS_BYTE_1( cert_len );
8257 *p++ = MBEDTLS_BYTE_0( cert_len );
8258
8259 if( session->peer_cert != NULL )
8260 {
8261 memcpy( p, session->peer_cert->raw.p, cert_len );
8262 p += cert_len;
8263 }
8264 }
8265#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8266 if( session->peer_cert_digest != NULL )
8267 {
8268 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8269 if( used <= buf_len )
8270 {
8271 *p++ = (unsigned char) session->peer_cert_digest_type;
8272 *p++ = (unsigned char) session->peer_cert_digest_len;
8273 memcpy( p, session->peer_cert_digest,
8274 session->peer_cert_digest_len );
8275 p += session->peer_cert_digest_len;
8276 }
8277 }
8278 else
8279 {
8280 used += 2;
8281 if( used <= buf_len )
8282 {
8283 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8284 *p++ = 0;
8285 }
8286 }
8287#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8288#endif /* MBEDTLS_X509_CRT_PARSE_C */
8289
8290 /*
8291 * Session ticket if any, plus associated data
8292 */
8293#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8294 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8295
8296 if( used <= buf_len )
8297 {
8298 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8299 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8300 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8301
8302 if( session->ticket != NULL )
8303 {
8304 memcpy( p, session->ticket, session->ticket_len );
8305 p += session->ticket_len;
8306 }
8307
8308 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8309 p += 4;
8310 }
8311#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8312
8313 /*
8314 * Misc extension-related info
8315 */
8316#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8317 used += 1;
8318
8319 if( used <= buf_len )
8320 *p++ = session->mfl_code;
8321#endif
8322
8323#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8324 used += 1;
8325
8326 if( used <= buf_len )
8327 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8328#endif
8329
8330 return( used );
8331}
8332
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008333MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008334static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008335 const unsigned char *buf,
8336 size_t len )
8337{
8338#if defined(MBEDTLS_HAVE_TIME)
8339 uint64_t start;
8340#endif
8341#if defined(MBEDTLS_X509_CRT_PARSE_C)
8342#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8343 size_t cert_len;
8344#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8345#endif /* MBEDTLS_X509_CRT_PARSE_C */
8346
8347 const unsigned char *p = buf;
8348 const unsigned char * const end = buf + len;
8349
8350 /*
8351 * Time
8352 */
8353#if defined(MBEDTLS_HAVE_TIME)
8354 if( 8 > (size_t)( end - p ) )
8355 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8356
8357 start = ( (uint64_t) p[0] << 56 ) |
8358 ( (uint64_t) p[1] << 48 ) |
8359 ( (uint64_t) p[2] << 40 ) |
8360 ( (uint64_t) p[3] << 32 ) |
8361 ( (uint64_t) p[4] << 24 ) |
8362 ( (uint64_t) p[5] << 16 ) |
8363 ( (uint64_t) p[6] << 8 ) |
8364 ( (uint64_t) p[7] );
8365 p += 8;
8366
8367 session->start = (time_t) start;
8368#endif /* MBEDTLS_HAVE_TIME */
8369
8370 /*
8371 * Basic mandatory fields
8372 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008373 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008374 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8375
8376 session->ciphersuite = ( p[0] << 8 ) | p[1];
8377 p += 2;
8378
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008379 session->id_len = *p++;
8380 memcpy( session->id, p, 32 );
8381 p += 32;
8382
8383 memcpy( session->master, p, 48 );
8384 p += 48;
8385
8386 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8387 ( (uint32_t) p[1] << 16 ) |
8388 ( (uint32_t) p[2] << 8 ) |
8389 ( (uint32_t) p[3] );
8390 p += 4;
8391
8392 /* Immediately clear invalid pointer values that have been read, in case
8393 * we exit early before we replaced them with valid ones. */
8394#if defined(MBEDTLS_X509_CRT_PARSE_C)
8395#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8396 session->peer_cert = NULL;
8397#else
8398 session->peer_cert_digest = NULL;
8399#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8400#endif /* MBEDTLS_X509_CRT_PARSE_C */
8401#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8402 session->ticket = NULL;
8403#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8404
8405 /*
8406 * Peer certificate
8407 */
8408#if defined(MBEDTLS_X509_CRT_PARSE_C)
8409#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8410 /* Deserialize CRT from the end of the ticket. */
8411 if( 3 > (size_t)( end - p ) )
8412 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8413
8414 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8415 p += 3;
8416
8417 if( cert_len != 0 )
8418 {
8419 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8420
8421 if( cert_len > (size_t)( end - p ) )
8422 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8423
8424 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8425
8426 if( session->peer_cert == NULL )
8427 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8428
8429 mbedtls_x509_crt_init( session->peer_cert );
8430
8431 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8432 p, cert_len ) ) != 0 )
8433 {
8434 mbedtls_x509_crt_free( session->peer_cert );
8435 mbedtls_free( session->peer_cert );
8436 session->peer_cert = NULL;
8437 return( ret );
8438 }
8439
8440 p += cert_len;
8441 }
8442#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8443 /* Deserialize CRT digest from the end of the ticket. */
8444 if( 2 > (size_t)( end - p ) )
8445 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8446
8447 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8448 session->peer_cert_digest_len = (size_t) *p++;
8449
8450 if( session->peer_cert_digest_len != 0 )
8451 {
8452 const mbedtls_md_info_t *md_info =
8453 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8454 if( md_info == NULL )
8455 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8456 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8457 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8458
8459 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8460 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8461
8462 session->peer_cert_digest =
8463 mbedtls_calloc( 1, session->peer_cert_digest_len );
8464 if( session->peer_cert_digest == NULL )
8465 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8466
8467 memcpy( session->peer_cert_digest, p,
8468 session->peer_cert_digest_len );
8469 p += session->peer_cert_digest_len;
8470 }
8471#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8472#endif /* MBEDTLS_X509_CRT_PARSE_C */
8473
8474 /*
8475 * Session ticket and associated data
8476 */
8477#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8478 if( 3 > (size_t)( end - p ) )
8479 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8480
8481 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8482 p += 3;
8483
8484 if( session->ticket_len != 0 )
8485 {
8486 if( session->ticket_len > (size_t)( end - p ) )
8487 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8488
8489 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8490 if( session->ticket == NULL )
8491 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8492
8493 memcpy( session->ticket, p, session->ticket_len );
8494 p += session->ticket_len;
8495 }
8496
8497 if( 4 > (size_t)( end - p ) )
8498 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8499
8500 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8501 ( (uint32_t) p[1] << 16 ) |
8502 ( (uint32_t) p[2] << 8 ) |
8503 ( (uint32_t) p[3] );
8504 p += 4;
8505#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8506
8507 /*
8508 * Misc extension-related info
8509 */
8510#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8511 if( 1 > (size_t)( end - p ) )
8512 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8513
8514 session->mfl_code = *p++;
8515#endif
8516
8517#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8518 if( 1 > (size_t)( end - p ) )
8519 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8520
8521 session->encrypt_then_mac = *p++;
8522#endif
8523
8524 /* Done, should have consumed entire buffer */
8525 if( p != end )
8526 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8527
8528 return( 0 );
8529}
Jerry Yudc7bd172022-02-17 13:44:15 +08008530#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8531
XiaokangQian75d40ef2022-04-20 11:05:24 +00008532int mbedtls_ssl_validate_ciphersuite(
8533 const mbedtls_ssl_context *ssl,
8534 const mbedtls_ssl_ciphersuite_t *suite_info,
8535 mbedtls_ssl_protocol_version min_tls_version,
8536 mbedtls_ssl_protocol_version max_tls_version )
8537{
8538 (void) ssl;
8539
8540 if( suite_info == NULL )
8541 return( -1 );
8542
8543 if( ( suite_info->min_tls_version > max_tls_version ) ||
8544 ( suite_info->max_tls_version < min_tls_version ) )
8545 {
8546 return( -1 );
8547 }
8548
XiaokangQian060d8672022-04-21 09:24:56 +00008549#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008550#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8551 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8552 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8553 {
8554 return( -1 );
8555 }
8556#endif
8557
8558 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8559#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8560 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8561 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8562 {
8563 return( -1 );
8564 }
8565#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8566#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8567
8568 return( 0 );
8569}
8570
XiaokangQianeaf36512022-04-24 09:07:44 +00008571#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8572/*
8573 * Function for writing a signature algorithm extension.
8574 *
8575 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8576 * value (TLS 1.3 RFC8446):
8577 * enum {
8578 * ....
8579 * ecdsa_secp256r1_sha256( 0x0403 ),
8580 * ecdsa_secp384r1_sha384( 0x0503 ),
8581 * ecdsa_secp521r1_sha512( 0x0603 ),
8582 * ....
8583 * } SignatureScheme;
8584 *
8585 * struct {
8586 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8587 * } SignatureSchemeList;
8588 *
8589 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8590 * value (TLS 1.2 RFC5246):
8591 * enum {
8592 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8593 * sha512(6), (255)
8594 * } HashAlgorithm;
8595 *
8596 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8597 * SignatureAlgorithm;
8598 *
8599 * struct {
8600 * HashAlgorithm hash;
8601 * SignatureAlgorithm signature;
8602 * } SignatureAndHashAlgorithm;
8603 *
8604 * SignatureAndHashAlgorithm
8605 * supported_signature_algorithms<2..2^16-2>;
8606 *
8607 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8608 * generalization of the TLS 1.2 signature algorithm extension.
8609 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8610 * `SignatureScheme` field of TLS 1.3
8611 *
8612 */
8613int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8614 const unsigned char *end, size_t *out_len )
8615{
8616 unsigned char *p = buf;
8617 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8618 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8619
8620 *out_len = 0;
8621
8622 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8623
8624 /* Check if we have space for header and length field:
8625 * - extension_type (2 bytes)
8626 * - extension_data_length (2 bytes)
8627 * - supported_signature_algorithms_length (2 bytes)
8628 */
8629 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8630 p += 6;
8631
8632 /*
8633 * Write supported_signature_algorithms
8634 */
8635 supported_sig_alg = p;
8636 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8637 if( sig_alg == NULL )
8638 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8639
8640 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8641 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008642 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8643 *sig_alg,
8644 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008645 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8646 continue;
8647 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8648 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8649 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008650 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008651 *sig_alg,
8652 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008653 }
8654
8655 /* Length of supported_signature_algorithms */
8656 supported_sig_alg_len = p - supported_sig_alg;
8657 if( supported_sig_alg_len == 0 )
8658 {
8659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8660 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8661 }
8662
XiaokangQianeaf36512022-04-24 09:07:44 +00008663 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008664 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008665 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8666
XiaokangQianeaf36512022-04-24 09:07:44 +00008667 *out_len = p - buf;
8668
8669#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8670 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8671#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8672 return( 0 );
8673}
8674#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8675
XiaokangQian40a35232022-05-07 09:02:40 +00008676#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008677/*
8678 * mbedtls_ssl_parse_server_name_ext
8679 *
8680 * Structure of server_name extension:
8681 *
8682 * enum {
8683 * host_name(0), (255)
8684 * } NameType;
8685 * opaque HostName<1..2^16-1>;
8686 *
8687 * struct {
8688 * NameType name_type;
8689 * select (name_type) {
8690 * case host_name: HostName;
8691 * } name;
8692 * } ServerName;
8693 * struct {
8694 * ServerName server_name_list<1..2^16-1>
8695 * } ServerNameList;
8696 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008697MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008698int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8699 const unsigned char *buf,
8700 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008701{
8702 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8703 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008704 size_t server_name_list_len, hostname_len;
8705 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008706
XiaokangQianf2a94202022-05-20 06:44:24 +00008707 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008708
8709 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008710 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008711 p += 2;
8712
XiaokangQian9b2b7712022-05-17 02:57:00 +00008713 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8714 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008715 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008716 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008717 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008718 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008719 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8720 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008721
8722 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8723 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008724 /* sni_name is intended to be used only during the parsing of the
8725 * ClientHello message (it is reset to NULL before the end of
8726 * the message parsing). Thus it is ok to just point to the
8727 * reception buffer and not make a copy of it.
8728 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008729 ssl->handshake->sni_name = p + 3;
8730 ssl->handshake->sni_name_len = hostname_len;
8731 if( ssl->conf->f_sni == NULL )
8732 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008733 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008734 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008735 if( ret != 0 )
8736 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008737 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008738 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8739 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008740 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8741 }
8742 return( 0 );
8743 }
8744
8745 p += hostname_len + 3;
8746 }
8747
8748 return( 0 );
8749}
8750#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8751
XiaokangQianacb39922022-06-17 10:18:48 +00008752#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008753MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008754int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8755 const unsigned char *buf,
8756 const unsigned char *end )
8757{
8758 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008759 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008760 const unsigned char *protocol_name_list;
8761 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008762 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008763
8764 /* If ALPN not configured, just ignore the extension */
8765 if( ssl->conf->alpn_list == NULL )
8766 return( 0 );
8767
8768 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008769 * RFC7301, section 3.1
8770 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008771 *
XiaokangQianc7403452022-06-23 03:24:12 +00008772 * struct {
8773 * ProtocolName protocol_name_list<2..2^16-1>
8774 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008775 */
8776
XiaokangQianc7403452022-06-23 03:24:12 +00008777 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008778 * protocol_name_list_len 2 bytes
8779 * protocol_name_len 1 bytes
8780 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008781 */
XiaokangQianacb39922022-06-17 10:18:48 +00008782 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8783
XiaokangQianc7403452022-06-23 03:24:12 +00008784 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008785 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008786 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008787 protocol_name_list = p;
8788 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008789
8790 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008791 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008792 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008793 protocol_name_len = *p++;
8794 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8795 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008796 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008797 {
8798 MBEDTLS_SSL_PEND_FATAL_ALERT(
8799 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8800 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008801 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008802 }
8803
8804 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008805 }
8806
8807 /* Use our order of preference */
8808 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8809 {
8810 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008811 p = protocol_name_list;
8812 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008813 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008814 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008815 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008816 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008817 {
8818 ssl->alpn_chosen = *alpn;
8819 return( 0 );
8820 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008821
8822 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008823 }
8824 }
8825
XiaokangQian95d5f542022-06-24 02:29:26 +00008826 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008827 MBEDTLS_SSL_PEND_FATAL_ALERT(
8828 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8829 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8830 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8831}
8832
8833int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8834 unsigned char *buf,
8835 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008836 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008837{
8838 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008839 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008840 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008841
8842 if( ssl->alpn_chosen == NULL )
8843 {
8844 return( 0 );
8845 }
8846
XiaokangQian95d5f542022-06-24 02:29:26 +00008847 protocol_name_len = strlen( ssl->alpn_chosen );
8848 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008849
8850 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8851 /*
8852 * 0 . 1 ext identifier
8853 * 2 . 3 ext length
8854 * 4 . 5 protocol list length
8855 * 6 . 6 protocol name length
8856 * 7 . 7+n protocol name
8857 */
8858 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8859
XiaokangQian95d5f542022-06-24 02:29:26 +00008860 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008861
XiaokangQian95d5f542022-06-24 02:29:26 +00008862 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8863 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008864 /* Note: the length of the chosen protocol has been checked to be less
8865 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8866 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008867 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008868
XiaokangQian95d5f542022-06-24 02:29:26 +00008869 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008870 return ( 0 );
8871}
8872#endif /* MBEDTLS_SSL_ALPN */
8873
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008874#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00008875 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00008876 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00008877 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008878int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
8879 const char *hostname )
8880{
8881 /* Initialize to suppress unnecessary compiler warning */
8882 size_t hostname_len = 0;
8883
8884 /* Check if new hostname is valid before
8885 * making any change to current one */
8886 if( hostname != NULL )
8887 {
8888 hostname_len = strlen( hostname );
8889
8890 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8891 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8892 }
8893
8894 /* Now it's clear that we will overwrite the old hostname,
8895 * so we can free it safely */
8896 if( session->hostname != NULL )
8897 {
8898 mbedtls_platform_zeroize( session->hostname,
8899 strlen( session->hostname ) );
8900 mbedtls_free( session->hostname );
8901 }
8902
8903 /* Passing NULL as hostname shall clear the old one */
8904 if( hostname == NULL )
8905 {
8906 session->hostname = NULL;
8907 }
8908 else
8909 {
8910 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
8911 if( session->hostname == NULL )
8912 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8913
8914 memcpy( session->hostname, hostname, hostname_len );
8915 }
8916
8917 return( 0 );
8918}
Xiaokang Qian03409292022-10-12 02:49:52 +00008919#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
8920 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00008921 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00008922 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008924#endif /* MBEDTLS_SSL_TLS_C */