blob: 84b3e40fd1bc4864b977e79f6fff6ecdd5e69c6f [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
Andrzej Kurekeabeb302022-10-17 07:52:51 -0400607#if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
608 !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
609 (void) ssl;
610 (void) buf;
611 (void) len;
612#endif
Jerry Yuc73c6182022-02-08 20:29:25 +0800613}
614
Andrzej Kurek25f27152022-08-17 16:09:31 -0400615#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800616static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
617 const unsigned char *buf, size_t len )
618{
619#if defined(MBEDTLS_USE_PSA_CRYPTO)
620 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
621#else
622 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
623#endif
624}
625#endif
626
Andrzej Kurek25f27152022-08-17 16:09:31 -0400627#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800628static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
629 const unsigned char *buf, size_t len )
630{
631#if defined(MBEDTLS_USE_PSA_CRYPTO)
632 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
633#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400634 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800635#endif
636}
637#endif
638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200640{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200642
Andrzej Kurek25f27152022-08-17 16:09:31 -0400643#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500644#if defined(MBEDTLS_USE_PSA_CRYPTO)
645 handshake->fin_sha256_psa = psa_hash_operation_init();
646 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
647#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200649 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200650#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500651#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400652#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500653#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500654 handshake->fin_sha384_psa = psa_hash_operation_init();
655 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500656#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400657 mbedtls_sha512_init( &handshake->fin_sha384 );
658 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200659#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500660#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200661
662 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664#if defined(MBEDTLS_DHM_C)
665 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200666#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200667#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200669#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200670#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200671 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200672#if defined(MBEDTLS_SSL_CLI_C)
673 handshake->ecjpake_cache = NULL;
674 handshake->ecjpake_cache_len = 0;
675#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200676#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200677
Gilles Peskineeccd8882020-03-10 12:19:08 +0100678#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200679 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200680#endif
681
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200682#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
683 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
684#endif
Hanno Becker75173122019-02-06 16:18:31 +0000685
686#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
687 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
688 mbedtls_pk_init( &handshake->peer_pubkey );
689#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200690}
691
Hanno Beckera18d1322018-01-03 14:27:32 +0000692void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200693{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200695
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100696#if defined(MBEDTLS_USE_PSA_CRYPTO)
697 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
698 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100699#else
700 mbedtls_cipher_init( &transform->cipher_ctx_enc );
701 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100702#endif
703
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000704#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100705#if defined(MBEDTLS_USE_PSA_CRYPTO)
706 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
707 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100708#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_md_init( &transform->md_ctx_enc );
710 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000711#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100712#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200713}
714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200716{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200718}
719
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200720MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000722{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200723 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000724 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200726 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200728 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200729 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200730
731 /*
732 * Either the pointers are now NULL or cleared properly and can be freed.
733 * Now allocate missing structures.
734 */
735 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200736 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200737 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200738 }
Paul Bakker48916f92012-09-16 19:57:18 +0000739
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200740 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200741 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200742 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200743 }
Paul Bakker48916f92012-09-16 19:57:18 +0000744
Paul Bakker82788fb2014-10-20 13:59:19 +0200745 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200746 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200747 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200748 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500749#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
750 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400751
Andrzej Kurek4a063792020-10-21 15:08:44 +0200752 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
753 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500754#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000755
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200756 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000757 if( ssl->handshake == NULL ||
758 ssl->transform_negotiate == NULL ||
759 ssl->session_negotiate == NULL )
760 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 mbedtls_free( ssl->handshake );
764 mbedtls_free( ssl->transform_negotiate );
765 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200766
767 ssl->handshake = NULL;
768 ssl->transform_negotiate = NULL;
769 ssl->session_negotiate = NULL;
770
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200771 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000772 }
773
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200774 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000776 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200777 ssl_handshake_params_init( ssl->handshake );
778
Jerry Yud0766ec2022-09-22 10:46:57 +0800779#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
780 defined(MBEDTLS_SSL_SRV_C) && \
781 defined(MBEDTLS_SSL_SESSION_TICKETS)
782 ssl->handshake->new_session_tickets_count =
783 ssl->conf->new_session_tickets_count ;
784#endif
785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200787 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
788 {
789 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200790
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200791 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
792 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
793 else
794 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200795
Hanno Becker0f57a652020-02-05 10:37:26 +0000796 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200797 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200798#endif
799
Brett Warrene0edc842021-08-17 09:53:13 +0100800/*
801 * curve_list is translated to IANA TLS group identifiers here because
802 * mbedtls_ssl_conf_curves returns void and so can't return
803 * any error codes.
804 */
805#if defined(MBEDTLS_ECP_C)
806#if !defined(MBEDTLS_DEPRECATED_REMOVED)
807 /* Heap allocate and translate curve_list from internal to IANA group ids */
808 if ( ssl->conf->curve_list != NULL )
809 {
810 size_t length;
811 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
812
813 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
814 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
815
816 /* Leave room for zero termination */
817 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
818 if ( group_list == NULL )
819 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
820
821 for( size_t i = 0; i < length; i++ )
822 {
823 const mbedtls_ecp_curve_info *info =
824 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
825 if ( info == NULL )
826 {
827 mbedtls_free( group_list );
828 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
829 }
830 group_list[i] = info->tls_id;
831 }
832
833 group_list[length] = 0;
834
835 ssl->handshake->group_list = group_list;
836 ssl->handshake->group_list_heap_allocated = 1;
837 }
838 else
839 {
840 ssl->handshake->group_list = ssl->conf->group_list;
841 ssl->handshake->group_list_heap_allocated = 0;
842 }
843#endif /* MBEDTLS_DEPRECATED_REMOVED */
844#endif /* MBEDTLS_ECP_C */
845
Ronald Crone68ab4f2022-10-05 12:46:29 +0200846#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +0800847#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800848#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800849 /* Heap allocate and translate sig_hashes from internal hash identifiers to
850 signature algorithms IANA identifiers. */
851 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800852 ssl->conf->sig_hashes != NULL )
853 {
854 const int *md;
855 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800856 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800857 uint16_t *p;
858
Jerry Yub476a442022-01-21 18:14:45 +0800859#if defined(static_assert)
860 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
861 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
862 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800863#endif
864
Jerry Yuf017ee42022-01-12 15:49:48 +0800865 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
866 {
867 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
868 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800869#if defined(MBEDTLS_ECDSA_C)
870 sig_algs_len += sizeof( uint16_t );
871#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800872
Jerry Yub476a442022-01-21 18:14:45 +0800873#if defined(MBEDTLS_RSA_C)
874 sig_algs_len += sizeof( uint16_t );
875#endif
876 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
877 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800878 }
879
Jerry Yub476a442022-01-21 18:14:45 +0800880 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800881 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
882
Jerry Yub476a442022-01-21 18:14:45 +0800883 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
884 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800885 if( ssl->handshake->sig_algs == NULL )
886 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
887
888 p = (uint16_t *)ssl->handshake->sig_algs;
889 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
890 {
891 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
892 if( hash == MBEDTLS_SSL_HASH_NONE )
893 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800894#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800895 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
896 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800897#endif
898#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800899 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
900 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800901#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800902 }
Gabor Mezei15b95a62022-05-09 16:37:58 +0200903 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +0800904 ssl->handshake->sig_algs_heap_allocated = 1;
905 }
906 else
Jerry Yua69269a2022-01-17 21:06:01 +0800907#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800908 {
Jerry Yuf017ee42022-01-12 15:49:48 +0800909 ssl->handshake->sig_algs_heap_allocated = 0;
910 }
Jerry Yucc539102022-06-27 16:27:35 +0800911#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +0200912#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000913 return( 0 );
914}
915
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200916#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200917/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200918MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200919static int ssl_cookie_write_dummy( void *ctx,
920 unsigned char **p, unsigned char *end,
921 const unsigned char *cli_id, size_t cli_id_len )
922{
923 ((void) ctx);
924 ((void) p);
925 ((void) end);
926 ((void) cli_id);
927 ((void) cli_id_len);
928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200930}
931
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200932MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200933static int ssl_cookie_check_dummy( void *ctx,
934 const unsigned char *cookie, size_t cookie_len,
935 const unsigned char *cli_id, size_t cli_id_len )
936{
937 ((void) ctx);
938 ((void) cookie);
939 ((void) cookie_len);
940 ((void) cli_id);
941 ((void) cli_id_len);
942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200944}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200945#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200946
Paul Bakker5121ce52009-01-03 21:22:43 +0000947/*
948 * Initialize an SSL context
949 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200950void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
951{
952 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
953}
954
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200955MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800956static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
957{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100958 const mbedtls_ssl_config *conf = ssl->conf;
959
Ronald Cron6f135e12021-12-08 16:57:54 +0100960#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100961 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
962 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000963 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
964 {
965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
966 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
967 }
968
Jerry Yu60835a82021-08-04 10:13:52 +0800969 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
970 return( 0 );
971 }
972#endif
973
974#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100975 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800976 {
977 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
978 return( 0 );
979 }
980#endif
981
Ronald Cron6f135e12021-12-08 16:57:54 +0100982#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100983 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800984 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000985 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
986 {
987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
988 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
989 }
990
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000991 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
992 {
993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
994 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
995 }
996
997
Ronald Crone1d3f062022-02-10 14:50:54 +0100998 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
999 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +08001000 }
1001#endif
1002
1003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
1004 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1005}
1006
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001007MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001008static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1009{
1010 int ret;
1011 ret = ssl_conf_version_check( ssl );
1012 if( ret != 0 )
1013 return( ret );
1014
1015 /* Space for further checks */
1016
1017 return( 0 );
1018}
1019
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001020/*
1021 * Setup an SSL context
1022 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001023
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001024int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001025 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001026{
Janos Follath865b3eb2019-12-16 11:46:15 +00001027 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001028 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1029 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001030
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001031 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001032
Jerry Yu60835a82021-08-04 10:13:52 +08001033 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1034 return( ret );
1035
Paul Bakker62f2dee2012-09-28 07:31:51 +00001036 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001037 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001038 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001039
1040 /* Set to NULL in case of an error condition */
1041 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001042
Darryl Greenb33cc762019-11-28 14:29:44 +00001043#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1044 ssl->in_buf_len = in_buf_len;
1045#endif
1046 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001047 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001048 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001050 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001051 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001052 }
1053
Darryl Greenb33cc762019-11-28 14:29:44 +00001054#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1055 ssl->out_buf_len = out_buf_len;
1056#endif
1057 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001058 if( ssl->out_buf == NULL )
1059 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001061 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001062 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001063 }
1064
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001065 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001066
Johan Pascalb62bb512015-12-03 21:56:45 +01001067#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001068 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001069#endif
1070
Paul Bakker48916f92012-09-16 19:57:18 +00001071 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001072 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001073
1074 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001075
1076error:
1077 mbedtls_free( ssl->in_buf );
1078 mbedtls_free( ssl->out_buf );
1079
1080 ssl->conf = NULL;
1081
Darryl Greenb33cc762019-11-28 14:29:44 +00001082#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1083 ssl->in_buf_len = 0;
1084 ssl->out_buf_len = 0;
1085#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001086 ssl->in_buf = NULL;
1087 ssl->out_buf = NULL;
1088
1089 ssl->in_hdr = NULL;
1090 ssl->in_ctr = NULL;
1091 ssl->in_len = NULL;
1092 ssl->in_iv = NULL;
1093 ssl->in_msg = NULL;
1094
1095 ssl->out_hdr = NULL;
1096 ssl->out_ctr = NULL;
1097 ssl->out_len = NULL;
1098 ssl->out_iv = NULL;
1099 ssl->out_msg = NULL;
1100
k-stachowiak9f7798e2018-07-31 16:52:32 +02001101 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001102}
1103
1104/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001105 * Reset an initialized and used SSL context for re-use while retaining
1106 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001107 *
1108 * If partial is non-zero, keep data in the input buffer and client ID.
1109 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001110 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001111void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1112 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001113{
Darryl Greenb33cc762019-11-28 14:29:44 +00001114#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1115 size_t in_buf_len = ssl->in_buf_len;
1116 size_t out_buf_len = ssl->out_buf_len;
1117#else
1118 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1119 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1120#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001121
Hanno Beckerb0302c42021-08-03 09:39:42 +01001122#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1123 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001124#endif
1125
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001126 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001127 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001128
Hanno Beckerb0302c42021-08-03 09:39:42 +01001129 mbedtls_ssl_reset_in_out_pointers( ssl );
1130
1131 /* Reset incoming message parsing */
1132 ssl->in_offt = NULL;
1133 ssl->nb_zero = 0;
1134 ssl->in_msgtype = 0;
1135 ssl->in_msglen = 0;
1136 ssl->in_hslen = 0;
1137 ssl->keep_current_message = 0;
1138 ssl->transform_in = NULL;
1139
1140#if defined(MBEDTLS_SSL_PROTO_DTLS)
1141 ssl->next_record_offset = 0;
1142 ssl->in_epoch = 0;
1143#endif
1144
1145 /* Keep current datagram if partial == 1 */
1146 if( partial == 0 )
1147 {
1148 ssl->in_left = 0;
1149 memset( ssl->in_buf, 0, in_buf_len );
1150 }
1151
Ronald Cronad8c17b2022-06-10 17:18:09 +02001152 ssl->send_alert = 0;
1153
Hanno Beckerb0302c42021-08-03 09:39:42 +01001154 /* Reset outgoing message writing */
1155 ssl->out_msgtype = 0;
1156 ssl->out_msglen = 0;
1157 ssl->out_left = 0;
1158 memset( ssl->out_buf, 0, out_buf_len );
1159 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1160 ssl->transform_out = NULL;
1161
1162#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1163 mbedtls_ssl_dtls_replay_reset( ssl );
1164#endif
1165
XiaokangQian2b01dc32022-01-21 02:53:13 +00001166#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001167 if( ssl->transform )
1168 {
1169 mbedtls_ssl_transform_free( ssl->transform );
1170 mbedtls_free( ssl->transform );
1171 ssl->transform = NULL;
1172 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001173#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1174
XiaokangQian2b01dc32022-01-21 02:53:13 +00001175#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1176 mbedtls_ssl_transform_free( ssl->transform_application );
1177 mbedtls_free( ssl->transform_application );
1178 ssl->transform_application = NULL;
1179
1180 if( ssl->handshake != NULL )
1181 {
1182 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1183 mbedtls_free( ssl->handshake->transform_earlydata );
1184 ssl->handshake->transform_earlydata = NULL;
1185
1186 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1187 mbedtls_free( ssl->handshake->transform_handshake );
1188 ssl->handshake->transform_handshake = NULL;
1189 }
1190
XiaokangQian2b01dc32022-01-21 02:53:13 +00001191#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001192}
1193
1194int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1195{
1196 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1197
1198 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1199
XiaokangQian78b1fa72022-01-19 06:56:30 +00001200 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001201
1202 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203#if defined(MBEDTLS_SSL_RENEGOTIATION)
1204 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001205 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001206
1207 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1209 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001210#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001212
Hanno Beckerb0302c42021-08-03 09:39:42 +01001213 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001214 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001215 if( ssl->session )
1216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 mbedtls_ssl_session_free( ssl->session );
1218 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001219 ssl->session = NULL;
1220 }
1221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001223 ssl->alpn_chosen = NULL;
1224#endif
1225
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001226#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001227 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001228#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
David Horstmann3b2276a2022-10-06 14:49:08 +01001229 free_cli_id = ( partial == 0 );
Hanno Becker4ccbf062018-08-10 11:20:38 +01001230#endif
David Horstmann3b2276a2022-10-06 14:49:08 +01001231 if( free_cli_id )
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001232 {
1233 mbedtls_free( ssl->cli_id );
1234 ssl->cli_id = NULL;
1235 ssl->cli_id_len = 0;
1236 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001237#endif
1238
Paul Bakker48916f92012-09-16 19:57:18 +00001239 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1240 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001241
1242 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001243}
1244
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001245/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001246 * Reset an initialized and used SSL context for re-use while retaining
1247 * all application-set variables, function pointers and data.
1248 */
1249int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1250{
Hanno Becker43aefe22020-02-05 10:44:56 +00001251 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001252}
1253
1254/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001255 * SSL set accessors
1256 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001257void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001258{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001259 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001260}
1261
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001262void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001263{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001264 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001265}
1266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001268void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001269{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001270 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001271}
1272#endif
1273
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001274void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001275{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001276 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001277}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001280
Hanno Becker1841b0a2018-08-24 11:13:57 +01001281void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1282 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001283{
1284 ssl->disable_datagram_packing = !allow_packing;
1285}
1286
1287void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1288 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001289{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001290 conf->hs_timeout_min = min;
1291 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001292}
1293#endif
1294
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001295void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001296{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001297 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001298}
1299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001301void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001302 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001303 void *p_vrfy )
1304{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001305 conf->f_vrfy = f_vrfy;
1306 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001307}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001309
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001310void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001311 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001312 void *p_rng )
1313{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001314 conf->f_rng = f_rng;
1315 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001316}
1317
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001318void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001319 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001320 void *p_dbg )
1321{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001322 conf->f_dbg = f_dbg;
1323 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001324}
1325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001327 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001328 mbedtls_ssl_send_t *f_send,
1329 mbedtls_ssl_recv_t *f_recv,
1330 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001331{
1332 ssl->p_bio = p_bio;
1333 ssl->f_send = f_send;
1334 ssl->f_recv = f_recv;
1335 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001336}
1337
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001338#if defined(MBEDTLS_SSL_PROTO_DTLS)
1339void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1340{
1341 ssl->mtu = mtu;
1342}
1343#endif
1344
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001345void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001346{
1347 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001348}
1349
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001350void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1351 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001352 mbedtls_ssl_set_timer_t *f_set_timer,
1353 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001354{
1355 ssl->p_timer = p_timer;
1356 ssl->f_set_timer = f_set_timer;
1357 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001358
1359 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001360 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001361}
1362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001364void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001365 void *p_cache,
1366 mbedtls_ssl_cache_get_t *f_get_cache,
1367 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001368{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001369 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001370 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001371 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001372}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#if defined(MBEDTLS_SSL_CLI_C)
1376int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001377{
Janos Follath865b3eb2019-12-16 11:46:15 +00001378 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001379
1380 if( ssl == NULL ||
1381 session == NULL ||
1382 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001383 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001386 }
1387
Hanno Beckere810bbc2021-05-14 16:01:05 +01001388 if( ssl->handshake->resume == 1 )
1389 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1390
Jerry Yu21092062022-10-10 21:21:31 +08001391#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1392 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001393 {
Jerry Yu21092062022-10-10 21:21:31 +08001394 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1395 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1396
1397 if( mbedtls_ssl_validate_ciphersuite(
1398 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1399 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1400 {
1401 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1402 session->ciphersuite ) );
1403 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1404 }
Jerry Yu40afab62022-10-08 10:42:13 +08001405 }
Jerry Yu21092062022-10-10 21:21:31 +08001406#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001407
Hanno Becker52055ae2019-02-06 14:30:46 +00001408 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1409 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001410 return( ret );
1411
Paul Bakker0a597072012-09-25 21:55:46 +00001412 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001413
1414 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001415}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001417
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001418void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1419 const int *ciphersuites )
1420{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001421 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001422}
1423
Ronald Cron6f135e12021-12-08 16:57:54 +01001424#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001425void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001426 const int kex_modes )
1427{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001428 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001429}
Ronald Cron6f135e12021-12-08 16:57:54 +01001430#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001433void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001434 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001435{
1436 conf->cert_profile = profile;
1437}
1438
Glenn Strauss36872db2022-01-22 05:06:31 -05001439static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1440{
1441 mbedtls_ssl_key_cert *cur = key_cert, *next;
1442
1443 while( cur != NULL )
1444 {
1445 next = cur->next;
1446 mbedtls_free( cur );
1447 cur = next;
1448 }
1449}
1450
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001451/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001452MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001453static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1454 mbedtls_x509_crt *cert,
1455 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001456{
niisato8ee24222018-06-25 19:05:48 +09001457 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001458
Glenn Strauss36872db2022-01-22 05:06:31 -05001459 if( cert == NULL )
1460 {
1461 /* Free list if cert is null */
1462 ssl_key_cert_free( *head );
1463 *head = NULL;
1464 return( 0 );
1465 }
1466
niisato8ee24222018-06-25 19:05:48 +09001467 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1468 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001469 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001470
niisato8ee24222018-06-25 19:05:48 +09001471 new_cert->cert = cert;
1472 new_cert->key = key;
1473 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001474
Glenn Strauss36872db2022-01-22 05:06:31 -05001475 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001476 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001477 {
niisato8ee24222018-06-25 19:05:48 +09001478 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001479 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001480 else
1481 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001482 mbedtls_ssl_key_cert *cur = *head;
1483 while( cur->next != NULL )
1484 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001485 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001486 }
1487
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001488 return( 0 );
1489}
1490
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001491int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001492 mbedtls_x509_crt *own_cert,
1493 mbedtls_pk_context *pk_key )
1494{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001495 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001496}
1497
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001498void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001499 mbedtls_x509_crt *ca_chain,
1500 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001501{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001502 conf->ca_chain = ca_chain;
1503 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001504
1505#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1506 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1507 * cannot be used together. */
1508 conf->f_ca_cb = NULL;
1509 conf->p_ca_cb = NULL;
1510#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001511}
Hanno Becker5adaad92019-03-27 16:54:37 +00001512
1513#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1514void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001515 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001516 void *p_ca_cb )
1517{
1518 conf->f_ca_cb = f_ca_cb;
1519 conf->p_ca_cb = p_ca_cb;
1520
1521 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1522 * cannot be used together. */
1523 conf->ca_chain = NULL;
1524 conf->ca_crl = NULL;
1525}
1526#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001528
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001529#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001530const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1531 size_t *name_len )
1532{
1533 *name_len = ssl->handshake->sni_name_len;
1534 return( ssl->handshake->sni_name );
1535}
1536
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001537int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1538 mbedtls_x509_crt *own_cert,
1539 mbedtls_pk_context *pk_key )
1540{
1541 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1542 own_cert, pk_key ) );
1543}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001544
1545void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1546 mbedtls_x509_crt *ca_chain,
1547 mbedtls_x509_crl *ca_crl )
1548{
1549 ssl->handshake->sni_ca_chain = ca_chain;
1550 ssl->handshake->sni_ca_crl = ca_crl;
1551}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001552
Glenn Strauss999ef702022-03-11 01:37:23 -05001553#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1554void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1555 const mbedtls_x509_crt *crt)
1556{
1557 ssl->handshake->dn_hints = crt;
1558}
1559#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1560
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001561void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1562 int authmode )
1563{
1564 ssl->handshake->sni_authmode = authmode;
1565}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001566#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1567
Hanno Becker8927c832019-04-03 12:52:50 +01001568#if defined(MBEDTLS_X509_CRT_PARSE_C)
1569void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1570 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1571 void *p_vrfy )
1572{
1573 ssl->f_vrfy = f_vrfy;
1574 ssl->p_vrfy = p_vrfy;
1575}
1576#endif
1577
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001578#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001579/*
1580 * Set EC J-PAKE password for current handshake
1581 */
1582int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1583 const unsigned char *pw,
1584 size_t pw_len )
1585{
1586 mbedtls_ecjpake_role role;
1587
Janos Follath8eb64132016-06-03 15:40:57 +01001588 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001589 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1590
1591 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1592 role = MBEDTLS_ECJPAKE_SERVER;
1593 else
1594 role = MBEDTLS_ECJPAKE_CLIENT;
1595
1596 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1597 role,
1598 MBEDTLS_MD_SHA256,
1599 MBEDTLS_ECP_DP_SECP256R1,
1600 pw, pw_len ) );
1601}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001602#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001603
Ronald Cron73fe8df2022-10-05 14:31:43 +02001604#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Ronald Crond29e13e2022-10-19 10:33:48 +02001605int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001606{
Ronald Crond29e13e2022-10-19 10:33:48 +02001607 if( conf->psk_identity == NULL ||
1608 conf->psk_identity_len == 0 )
1609 {
1610 return( 0 );
1611 }
1612
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001613#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Crond29e13e2022-10-19 10:33:48 +02001614 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001615 return( 1 );
1616#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02001617
1618 if( conf->psk != NULL && conf->psk_len != 0 )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001619 return( 1 );
1620
1621 return( 0 );
1622}
1623
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001624static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1625{
1626 /* Remove reference to existing PSK, if any. */
1627#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001628 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001629 {
1630 /* The maintenance of the PSK key slot is the
1631 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001632 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001633 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001634#endif /* MBEDTLS_USE_PSA_CRYPTO */
1635 if( conf->psk != NULL )
1636 {
1637 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1638
1639 mbedtls_free( conf->psk );
1640 conf->psk = NULL;
1641 conf->psk_len = 0;
1642 }
1643
1644 /* Remove reference to PSK identity, if any. */
1645 if( conf->psk_identity != NULL )
1646 {
1647 mbedtls_free( conf->psk_identity );
1648 conf->psk_identity = NULL;
1649 conf->psk_identity_len = 0;
1650 }
1651}
1652
Hanno Becker7390c712018-11-15 13:33:04 +00001653/* This function assumes that PSK identity in the SSL config is unset.
1654 * It checks that the provided identity is well-formed and attempts
1655 * to make a copy of it in the SSL config.
1656 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001657MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001658static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1659 unsigned char const *psk_identity,
1660 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001661{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001662 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001663 if( psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02001664 psk_identity_len == 0 ||
Hanno Becker7390c712018-11-15 13:33:04 +00001665 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001666 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001667 {
1668 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1669 }
1670
Hanno Becker7390c712018-11-15 13:33:04 +00001671 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1672 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001673 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001674
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001675 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001676 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001677
1678 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001679}
1680
Hanno Becker7390c712018-11-15 13:33:04 +00001681int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1682 const unsigned char *psk, size_t psk_len,
1683 const unsigned char *psk_identity, size_t psk_identity_len )
1684{
Janos Follath865b3eb2019-12-16 11:46:15 +00001685 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001686
1687 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02001688 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001689 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001690
1691 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001692 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001693 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001694 if( psk_len == 0 )
1695 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1696 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1697 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1698
Hanno Becker7390c712018-11-15 13:33:04 +00001699 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1700 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1701 conf->psk_len = psk_len;
1702 memcpy( conf->psk, psk, conf->psk_len );
1703
1704 /* Check and set PSK Identity */
1705 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1706 if( ret != 0 )
1707 ssl_conf_remove_psk( conf );
1708
1709 return( ret );
1710}
1711
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001712static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1713{
1714#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001715 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001716 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001717 /* The maintenance of the external PSK key slot is the
1718 * user's responsibility. */
1719 if( ssl->handshake->psk_opaque_is_internal )
1720 {
1721 psa_destroy_key( ssl->handshake->psk_opaque );
1722 ssl->handshake->psk_opaque_is_internal = 0;
1723 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001724 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001725 }
Neil Armstronge952a302022-05-03 10:22:14 +02001726#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001727 if( ssl->handshake->psk != NULL )
1728 {
1729 mbedtls_platform_zeroize( ssl->handshake->psk,
1730 ssl->handshake->psk_len );
1731 mbedtls_free( ssl->handshake->psk );
1732 ssl->handshake->psk_len = 0;
1733 }
Neil Armstronge952a302022-05-03 10:22:14 +02001734#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001735}
1736
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001737int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1738 const unsigned char *psk, size_t psk_len )
1739{
Neil Armstrong501c9322022-05-03 09:35:09 +02001740#if defined(MBEDTLS_USE_PSA_CRYPTO)
1741 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001742 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001743 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001744 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001745#endif /* MBEDTLS_USE_PSA_CRYPTO */
1746
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001747 if( psk == NULL || ssl->handshake == NULL )
1748 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1749
1750 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1751 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1752
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001753 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001754
Neil Armstrong501c9322022-05-03 09:35:09 +02001755#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001756#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1757 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1758 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001759 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001760 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1761 else
1762 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1763 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1764 }
1765#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001766
Jerry Yu568ec252022-07-22 21:27:34 +08001767#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001768 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1769 {
1770 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1771 psa_set_key_usage_flags( &key_attributes,
1772 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1773 }
1774#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1775
Neil Armstrong501c9322022-05-03 09:35:09 +02001776 psa_set_key_algorithm( &key_attributes, alg );
1777 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1778
1779 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1780 if( status != PSA_SUCCESS )
1781 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1782
1783 /* Allow calling psa_destroy_key() on psk remove */
1784 ssl->handshake->psk_opaque_is_internal = 1;
1785 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1786#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001787 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001788 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001789
1790 ssl->handshake->psk_len = psk_len;
1791 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1792
1793 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001794#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001795}
1796
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001797#if defined(MBEDTLS_USE_PSA_CRYPTO)
1798int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001799 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001800 const unsigned char *psk_identity,
1801 size_t psk_identity_len )
1802{
Janos Follath865b3eb2019-12-16 11:46:15 +00001803 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001804
1805 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02001806 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001807 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001808
Hanno Becker7390c712018-11-15 13:33:04 +00001809 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001810 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001811 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001812 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001813
1814 /* Check and set PSK Identity */
1815 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1816 psk_identity_len );
1817 if( ret != 0 )
1818 ssl_conf_remove_psk( conf );
1819
1820 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001821}
1822
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001823int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1824 mbedtls_svc_key_id_t psk )
1825{
1826 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1827 ( ssl->handshake == NULL ) )
1828 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1829
1830 ssl_remove_psk( ssl );
1831 ssl->handshake->psk_opaque = psk;
1832 return( 0 );
1833}
1834#endif /* MBEDTLS_USE_PSA_CRYPTO */
1835
Jerry Yu8897c072022-08-12 13:56:53 +08001836#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001837void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1838 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1839 size_t),
1840 void *p_psk )
1841{
1842 conf->f_psk = f_psk;
1843 conf->p_psk = p_psk;
1844}
Jerry Yu8897c072022-08-12 13:56:53 +08001845#endif /* MBEDTLS_SSL_SRV_C */
1846
Ronald Cron73fe8df2022-10-05 14:31:43 +02001847#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001848
1849#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001850static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1851 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001852{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001853#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001854 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001855 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001856#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001857 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001858 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001859 return( MBEDTLS_SSL_MODE_STREAM );
1860}
1861
1862#else /* MBEDTLS_USE_PSA_CRYPTO */
1863
1864static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1865 mbedtls_cipher_mode_t mode )
1866{
1867#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1868 if( mode == MBEDTLS_MODE_CBC )
1869 return( MBEDTLS_SSL_MODE_CBC );
1870#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1871
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001872#if defined(MBEDTLS_GCM_C) || \
1873 defined(MBEDTLS_CCM_C) || \
1874 defined(MBEDTLS_CHACHAPOLY_C)
1875 if( mode == MBEDTLS_MODE_GCM ||
1876 mode == MBEDTLS_MODE_CCM ||
1877 mode == MBEDTLS_MODE_CHACHAPOLY )
1878 return( MBEDTLS_SSL_MODE_AEAD );
1879#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001880
1881 return( MBEDTLS_SSL_MODE_STREAM );
1882}
Gilles Peskine301711e2022-04-26 16:57:05 +02001883#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001884
Gilles Peskinee108d982022-04-26 16:50:40 +02001885static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1886 mbedtls_ssl_mode_t base_mode,
1887 int encrypt_then_mac )
1888{
1889#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1890 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1891 base_mode == MBEDTLS_SSL_MODE_CBC )
1892 {
1893 return( MBEDTLS_SSL_MODE_CBC_ETM );
1894 }
1895#else
1896 (void) encrypt_then_mac;
1897#endif
1898 return( base_mode );
1899}
1900
Neil Armstrongab555e02022-04-04 11:07:59 +02001901mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001902 const mbedtls_ssl_transform *transform )
1903{
Gilles Peskinee108d982022-04-26 16:50:40 +02001904 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001905#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001906 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001907#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001908 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1909#endif
1910 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001911
Gilles Peskinee108d982022-04-26 16:50:40 +02001912 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001913#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001914 encrypt_then_mac = transform->encrypt_then_mac;
1915#endif
1916 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001917}
1918
Neil Armstrongab555e02022-04-04 11:07:59 +02001919mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001920#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001921 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001922#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001923 const mbedtls_ssl_ciphersuite_t *suite )
1924{
Gilles Peskinee108d982022-04-26 16:50:40 +02001925 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1926
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001927#if defined(MBEDTLS_USE_PSA_CRYPTO)
1928 psa_status_t status;
1929 psa_algorithm_t alg;
1930 psa_key_type_t type;
1931 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001932 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1933 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001934 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001935#else
1936 const mbedtls_cipher_info_t *cipher =
1937 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001938 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001939 {
1940 base_mode =
1941 mbedtls_ssl_get_base_mode(
1942 mbedtls_cipher_info_get_mode( cipher ) );
1943 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001944#endif /* MBEDTLS_USE_PSA_CRYPTO */
1945
Gilles Peskinee108d982022-04-26 16:50:40 +02001946#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1947 int encrypt_then_mac = 0;
1948#endif
1949 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001950}
1951
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001952#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001953
1954#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001955/* Serialization of TLS 1.3 sessions:
1956 *
1957 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00001958 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001959 * uint64 ticket_received;
1960 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08001961 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001962 * } ClientOnlyData;
1963 *
1964 * struct {
1965 * uint8 endpoint;
1966 * uint8 ciphersuite[2];
1967 * uint32 ticket_age_add;
1968 * uint8 ticket_flags;
1969 * opaque resumption_key<0..255>;
1970 * select ( endpoint ) {
1971 * case client: ClientOnlyData;
1972 * case server: uint64 start_time;
1973 * };
1974 * } serialized_session_tls13;
1975 *
1976 */
1977#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08001978MBEDTLS_CHECK_RETURN_CRITICAL
1979static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
1980 unsigned char *buf,
1981 size_t buf_len,
1982 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00001983{
1984 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00001985#if defined(MBEDTLS_SSL_CLI_C) && \
1986 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1987 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00001988 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00001989#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08001990 size_t needed = 1 /* endpoint */
1991 + 2 /* ciphersuite */
1992 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08001993 + 1 /* ticket_flags */
1994 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08001995 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08001996
1997 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
1998 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1999 needed += session->resumption_key_len; /* resumption_key */
2000
Jerry Yu438ddd82022-07-07 06:55:50 +00002001#if defined(MBEDTLS_HAVE_TIME)
2002 needed += 8; /* start_time or ticket_received */
2003#endif
2004
2005#if defined(MBEDTLS_SSL_CLI_C)
2006 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2007 {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002008#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002009 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002010 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002011#endif
2012
Jerry Yu438ddd82022-07-07 06:55:50 +00002013 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002014 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002015
2016 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002017 if( session->ticket_len > SIZE_MAX - needed )
2018 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002019
Jerry Yue28d9742022-08-18 15:44:03 +08002020 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002021 }
2022#endif /* MBEDTLS_SSL_CLI_C */
2023
Jerry Yue36fdd62022-08-17 21:31:36 +08002024 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002025 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002026 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002027
2028 p[0] = session->endpoint;
2029 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2030 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2031 p[7] = session->ticket_flags;
2032
2033 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002034 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002035 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002036 memcpy( p, session->resumption_key, session->resumption_key_len );
2037 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002038
2039#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2040 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2041 {
2042 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2043 p += 8;
2044 }
2045#endif /* MBEDTLS_HAVE_TIME */
2046
2047#if defined(MBEDTLS_SSL_CLI_C)
2048 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2049 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002050#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2051 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2052 p += 2;
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002053 if( hostname_len > 0 )
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002054 {
2055 /* save host name */
2056 memcpy( p, session->hostname, hostname_len );
2057 p += hostname_len;
2058 }
2059#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2060
Jerry Yu438ddd82022-07-07 06:55:50 +00002061#if defined(MBEDTLS_HAVE_TIME)
2062 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2063 p += 8;
2064#endif
2065 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2066 p += 4;
2067
2068 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2069 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002070
2071 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002072 {
2073 memcpy( p, session->ticket, session->ticket_len );
2074 p += session->ticket_len;
2075 }
2076 }
2077#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002078 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002079}
2080
2081MBEDTLS_CHECK_RETURN_CRITICAL
2082static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2083 const unsigned char *buf,
2084 size_t len )
2085{
2086 const unsigned char *p = buf;
2087 const unsigned char *end = buf + len;
2088
2089 if( end - p < 9 )
2090 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2091 session->endpoint = p[0];
2092 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2093 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2094 session->ticket_flags = p[7];
2095
2096 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002097 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002098 p += 9;
2099
Jerry Yubc7c1a42022-07-21 22:57:37 +08002100 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002101 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2102
Jerry Yubc7c1a42022-07-21 22:57:37 +08002103 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002104 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002105 memcpy( session->resumption_key, p, session->resumption_key_len );
2106 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002107
2108#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2109 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2110 {
2111 if( end - p < 8 )
2112 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2113 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2114 p += 8;
2115 }
2116#endif /* MBEDTLS_HAVE_TIME */
2117
2118#if defined(MBEDTLS_SSL_CLI_C)
2119 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2120 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002121#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2122 defined(MBEDTLS_SSL_SESSION_TICKETS)
2123 size_t hostname_len;
2124 /* load host name */
2125 if( end - p < 2 )
2126 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2127 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2128 p += 2;
2129
2130 if( end - p < ( long int )hostname_len )
2131 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2132 if( hostname_len > 0 )
2133 {
2134 session->hostname = mbedtls_calloc( 1, hostname_len );
2135 if( session->hostname == NULL )
2136 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2137 memcpy( session->hostname, p, hostname_len );
2138 p += hostname_len;
2139 }
2140#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2141 MBEDTLS_SSL_SESSION_TICKETS */
2142
Jerry Yu438ddd82022-07-07 06:55:50 +00002143#if defined(MBEDTLS_HAVE_TIME)
2144 if( end - p < 8 )
2145 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2146 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2147 p += 8;
2148#endif
2149 if( end - p < 4 )
2150 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2151 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2152 p += 4;
2153
2154 if( end - p < 2 )
2155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2156 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2157 p += 2;
2158
2159 if( end - p < ( long int )session->ticket_len )
2160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2161 if( session->ticket_len > 0 )
2162 {
2163 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2164 if( session->ticket == NULL )
2165 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2166 memcpy( session->ticket, p, session->ticket_len );
2167 p += session->ticket_len;
2168 }
2169 }
2170#endif /* MBEDTLS_SSL_CLI_C */
2171
2172 return( 0 );
2173
2174}
2175#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002176MBEDTLS_CHECK_RETURN_CRITICAL
2177static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2178 unsigned char *buf,
2179 size_t buf_len,
2180 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002181{
2182 ((void) session);
2183 ((void) buf);
2184 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002185 *olen = 0;
2186 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002187}
Jerry Yu438ddd82022-07-07 06:55:50 +00002188
2189static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2190 unsigned char *buf,
2191 size_t buf_len )
2192{
2193 ((void) session);
2194 ((void) buf);
2195 ((void) buf_len);
2196 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2197}
2198#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002199#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2200
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002201psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002202 size_t taglen,
2203 psa_algorithm_t *alg,
2204 psa_key_type_t *key_type,
2205 size_t *key_size )
2206{
2207 switch ( mbedtls_cipher_type )
2208 {
2209 case MBEDTLS_CIPHER_AES_128_CBC:
2210 *alg = PSA_ALG_CBC_NO_PADDING;
2211 *key_type = PSA_KEY_TYPE_AES;
2212 *key_size = 128;
2213 break;
2214 case MBEDTLS_CIPHER_AES_128_CCM:
2215 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2216 *key_type = PSA_KEY_TYPE_AES;
2217 *key_size = 128;
2218 break;
2219 case MBEDTLS_CIPHER_AES_128_GCM:
2220 *alg = PSA_ALG_GCM;
2221 *key_type = PSA_KEY_TYPE_AES;
2222 *key_size = 128;
2223 break;
2224 case MBEDTLS_CIPHER_AES_192_CCM:
2225 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2226 *key_type = PSA_KEY_TYPE_AES;
2227 *key_size = 192;
2228 break;
2229 case MBEDTLS_CIPHER_AES_192_GCM:
2230 *alg = PSA_ALG_GCM;
2231 *key_type = PSA_KEY_TYPE_AES;
2232 *key_size = 192;
2233 break;
2234 case MBEDTLS_CIPHER_AES_256_CBC:
2235 *alg = PSA_ALG_CBC_NO_PADDING;
2236 *key_type = PSA_KEY_TYPE_AES;
2237 *key_size = 256;
2238 break;
2239 case MBEDTLS_CIPHER_AES_256_CCM:
2240 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2241 *key_type = PSA_KEY_TYPE_AES;
2242 *key_size = 256;
2243 break;
2244 case MBEDTLS_CIPHER_AES_256_GCM:
2245 *alg = PSA_ALG_GCM;
2246 *key_type = PSA_KEY_TYPE_AES;
2247 *key_size = 256;
2248 break;
2249 case MBEDTLS_CIPHER_ARIA_128_CBC:
2250 *alg = PSA_ALG_CBC_NO_PADDING;
2251 *key_type = PSA_KEY_TYPE_ARIA;
2252 *key_size = 128;
2253 break;
2254 case MBEDTLS_CIPHER_ARIA_128_CCM:
2255 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2256 *key_type = PSA_KEY_TYPE_ARIA;
2257 *key_size = 128;
2258 break;
2259 case MBEDTLS_CIPHER_ARIA_128_GCM:
2260 *alg = PSA_ALG_GCM;
2261 *key_type = PSA_KEY_TYPE_ARIA;
2262 *key_size = 128;
2263 break;
2264 case MBEDTLS_CIPHER_ARIA_192_CCM:
2265 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2266 *key_type = PSA_KEY_TYPE_ARIA;
2267 *key_size = 192;
2268 break;
2269 case MBEDTLS_CIPHER_ARIA_192_GCM:
2270 *alg = PSA_ALG_GCM;
2271 *key_type = PSA_KEY_TYPE_ARIA;
2272 *key_size = 192;
2273 break;
2274 case MBEDTLS_CIPHER_ARIA_256_CBC:
2275 *alg = PSA_ALG_CBC_NO_PADDING;
2276 *key_type = PSA_KEY_TYPE_ARIA;
2277 *key_size = 256;
2278 break;
2279 case MBEDTLS_CIPHER_ARIA_256_CCM:
2280 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2281 *key_type = PSA_KEY_TYPE_ARIA;
2282 *key_size = 256;
2283 break;
2284 case MBEDTLS_CIPHER_ARIA_256_GCM:
2285 *alg = PSA_ALG_GCM;
2286 *key_type = PSA_KEY_TYPE_ARIA;
2287 *key_size = 256;
2288 break;
2289 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2290 *alg = PSA_ALG_CBC_NO_PADDING;
2291 *key_type = PSA_KEY_TYPE_CAMELLIA;
2292 *key_size = 128;
2293 break;
2294 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2295 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2296 *key_type = PSA_KEY_TYPE_CAMELLIA;
2297 *key_size = 128;
2298 break;
2299 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2300 *alg = PSA_ALG_GCM;
2301 *key_type = PSA_KEY_TYPE_CAMELLIA;
2302 *key_size = 128;
2303 break;
2304 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2305 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2306 *key_type = PSA_KEY_TYPE_CAMELLIA;
2307 *key_size = 192;
2308 break;
2309 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2310 *alg = PSA_ALG_GCM;
2311 *key_type = PSA_KEY_TYPE_CAMELLIA;
2312 *key_size = 192;
2313 break;
2314 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2315 *alg = PSA_ALG_CBC_NO_PADDING;
2316 *key_type = PSA_KEY_TYPE_CAMELLIA;
2317 *key_size = 256;
2318 break;
2319 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2320 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2321 *key_type = PSA_KEY_TYPE_CAMELLIA;
2322 *key_size = 256;
2323 break;
2324 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2325 *alg = PSA_ALG_GCM;
2326 *key_type = PSA_KEY_TYPE_CAMELLIA;
2327 *key_size = 256;
2328 break;
2329 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2330 *alg = PSA_ALG_CHACHA20_POLY1305;
2331 *key_type = PSA_KEY_TYPE_CHACHA20;
2332 *key_size = 256;
2333 break;
2334 case MBEDTLS_CIPHER_NULL:
2335 *alg = MBEDTLS_SSL_NULL_CIPHER;
2336 *key_type = 0;
2337 *key_size = 0;
2338 break;
2339 default:
2340 return PSA_ERROR_NOT_SUPPORTED;
2341 }
2342
2343 return PSA_SUCCESS;
2344}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002345#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002346
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002347#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002348int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2349 const unsigned char *dhm_P, size_t P_len,
2350 const unsigned char *dhm_G, size_t G_len )
2351{
Janos Follath865b3eb2019-12-16 11:46:15 +00002352 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002353
Glenn Strausscee11292021-12-20 01:43:17 -05002354 mbedtls_mpi_free( &conf->dhm_P );
2355 mbedtls_mpi_free( &conf->dhm_G );
2356
Hanno Beckera90658f2017-10-04 15:29:08 +01002357 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2358 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2359 {
2360 mbedtls_mpi_free( &conf->dhm_P );
2361 mbedtls_mpi_free( &conf->dhm_G );
2362 return( ret );
2363 }
2364
2365 return( 0 );
2366}
Paul Bakker5121ce52009-01-03 21:22:43 +00002367
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002368int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002369{
Janos Follath865b3eb2019-12-16 11:46:15 +00002370 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002371
Glenn Strausscee11292021-12-20 01:43:17 -05002372 mbedtls_mpi_free( &conf->dhm_P );
2373 mbedtls_mpi_free( &conf->dhm_G );
2374
Gilles Peskinee5702482021-06-11 21:59:08 +02002375 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2376 &conf->dhm_P ) ) != 0 ||
2377 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2378 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002379 {
2380 mbedtls_mpi_free( &conf->dhm_P );
2381 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002382 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002383 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002384
2385 return( 0 );
2386}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002387#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002388
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002389#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2390/*
2391 * Set the minimum length for Diffie-Hellman parameters
2392 */
2393void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2394 unsigned int bitlen )
2395{
2396 conf->dhm_min_bitlen = bitlen;
2397}
2398#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2399
Ronald Crone68ab4f2022-10-05 12:46:29 +02002400#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002401#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002402/*
2403 * Set allowed/preferred hashes for handshake signatures
2404 */
2405void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2406 const int *hashes )
2407{
2408 conf->sig_hashes = hashes;
2409}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002410#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002411
Jerry Yuf017ee42022-01-12 15:49:48 +08002412/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002413void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2414 const uint16_t* sig_algs )
2415{
Jerry Yuf017ee42022-01-12 15:49:48 +08002416#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2417 conf->sig_hashes = NULL;
2418#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2419 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002420}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002421#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002422
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002423#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002424#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002425/*
2426 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002427 *
2428 * mbedtls_ssl_setup() takes the provided list
2429 * and translates it to a list of IANA TLS group identifiers,
2430 * stored in ssl->handshake->group_list.
2431 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002432 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002433void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002434 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002435{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002436 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002437 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002438}
Brett Warrene0edc842021-08-17 09:53:13 +01002439#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002440#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002441
Brett Warrene0edc842021-08-17 09:53:13 +01002442/*
2443 * Set the allowed groups
2444 */
2445void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2446 const uint16_t *group_list )
2447{
2448#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2449 conf->curve_list = NULL;
2450#endif
2451 conf->group_list = group_list;
2452}
2453
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002454#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002456{
Hanno Becker947194e2017-04-07 13:25:49 +01002457 /* Initialize to suppress unnecessary compiler warning */
2458 size_t hostname_len = 0;
2459
2460 /* Check if new hostname is valid before
2461 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002462 if( hostname != NULL )
2463 {
2464 hostname_len = strlen( hostname );
2465
2466 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2467 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2468 }
2469
2470 /* Now it's clear that we will overwrite the old hostname,
2471 * so we can free it safely */
2472
2473 if( ssl->hostname != NULL )
2474 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002475 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002476 mbedtls_free( ssl->hostname );
2477 }
2478
2479 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002480
Paul Bakker5121ce52009-01-03 21:22:43 +00002481 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002482 {
2483 ssl->hostname = NULL;
2484 }
2485 else
2486 {
2487 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002488 if( ssl->hostname == NULL )
2489 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002490
Hanno Becker947194e2017-04-07 13:25:49 +01002491 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002492
Hanno Becker947194e2017-04-07 13:25:49 +01002493 ssl->hostname[hostname_len] = '\0';
2494 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002495
2496 return( 0 );
2497}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002498#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002499
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002500#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002501void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002503 const unsigned char *, size_t),
2504 void *p_sni )
2505{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002506 conf->f_sni = f_sni;
2507 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002508}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002512int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002513{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002514 size_t cur_len, tot_len;
2515 const char **p;
2516
2517 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002518 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2519 * MUST NOT be truncated."
2520 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002521 */
2522 tot_len = 0;
2523 for( p = protos; *p != NULL; p++ )
2524 {
2525 cur_len = strlen( *p );
2526 tot_len += cur_len;
2527
Ronald Cron8216dd32020-04-23 16:41:44 +02002528 if( ( cur_len == 0 ) ||
2529 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2530 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002532 }
2533
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002534 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002535
2536 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002537}
2538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002539const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002540{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002541 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002542}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002544
Johan Pascalb62bb512015-12-03 21:56:45 +01002545#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002546void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2547 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002548{
2549 conf->dtls_srtp_mki_support = support_mki_value;
2550}
2551
Ron Eldoref72faf2018-07-12 11:54:20 +03002552int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2553 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002554 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002555{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002556 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002557 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002558 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002559 }
Ron Eldor591f1622018-01-22 12:30:04 +02002560
2561 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002562 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002563 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002564 }
Ron Eldor591f1622018-01-22 12:30:04 +02002565
2566 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2567 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002568 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002569}
2570
Ron Eldoref72faf2018-07-12 11:54:20 +03002571int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002572 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002573{
Johan Pascal253d0262020-09-22 13:04:45 +02002574 const mbedtls_ssl_srtp_profile *p;
2575 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002576
Johan Pascal253d0262020-09-22 13:04:45 +02002577 /* check the profiles list: all entry must be valid,
2578 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002579 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2580 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2581 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002582 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002583 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002584 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002585 list_size++;
2586 }
2587 else
2588 {
2589 /* unsupported value, stop parsing and set the size to an error value */
2590 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002591 }
2592 }
2593
Johan Pascal5ef72d22020-10-28 17:05:47 +01002594 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002595 {
Johan Pascal253d0262020-09-22 13:04:45 +02002596 conf->dtls_srtp_profile_list = NULL;
2597 conf->dtls_srtp_profile_list_len = 0;
2598 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2599 }
2600
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002601 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002602 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002603
2604 return( 0 );
2605}
2606
Johan Pascal5ef72d22020-10-28 17:05:47 +01002607void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2608 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002609{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002610 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2611 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002612 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002613 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002614 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002615 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002616 else
2617 {
2618 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002619 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2620 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002621 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002622}
Johan Pascalb62bb512015-12-03 21:56:45 +01002623#endif /* MBEDTLS_SSL_DTLS_SRTP */
2624
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302625#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002626void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002627{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002628 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002629}
2630
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002631void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002632{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002633 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002634}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302635#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002636
Janos Follath088ce432017-04-10 12:42:31 +01002637#if defined(MBEDTLS_SSL_SRV_C)
2638void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2639 char cert_req_ca_list )
2640{
2641 conf->cert_req_ca_list = cert_req_ca_list;
2642}
2643#endif
2644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002646void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002647{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002648 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002649}
2650#endif
2651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002653void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002654{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002655 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002656}
2657#endif
2658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002660int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002661{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002663 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002666 }
2667
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002668 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002669
2670 return( 0 );
2671}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002673
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002674void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002675{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002676 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002677}
2678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002680void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002681{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002682 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002683}
2684
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002685void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002686{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002687 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002688}
2689
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002690void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002691 const unsigned char period[8] )
2692{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002693 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002694}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002698#if defined(MBEDTLS_SSL_CLI_C)
2699void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002700{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002701 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002702}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002703#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002704
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002705#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002706
Jerry Yud0766ec2022-09-22 10:46:57 +08002707#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002708void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2709 uint16_t num_tickets )
2710{
Jerry Yud0766ec2022-09-22 10:46:57 +08002711 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002712}
2713#endif
2714
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002715void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2716 mbedtls_ssl_ticket_write_t *f_ticket_write,
2717 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2718 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002719{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002720 conf->f_ticket_write = f_ticket_write;
2721 conf->f_ticket_parse = f_ticket_parse;
2722 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002723}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002724#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002726
Hanno Becker7e6c1782021-06-08 09:24:55 +01002727void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2728 mbedtls_ssl_export_keys_t *f_export_keys,
2729 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002730{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002731 ssl->f_export_keys = f_export_keys;
2732 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002733}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002734
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002735#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002736void mbedtls_ssl_conf_async_private_cb(
2737 mbedtls_ssl_config *conf,
2738 mbedtls_ssl_async_sign_t *f_async_sign,
2739 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2740 mbedtls_ssl_async_resume_t *f_async_resume,
2741 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002742 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002743{
2744 conf->f_async_sign_start = f_async_sign;
2745 conf->f_async_decrypt_start = f_async_decrypt;
2746 conf->f_async_resume = f_async_resume;
2747 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002748 conf->p_async_config_data = async_config_data;
2749}
2750
Gilles Peskine8f97af72018-04-26 11:46:10 +02002751void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2752{
2753 return( conf->p_async_config_data );
2754}
2755
Gilles Peskine1febfef2018-04-30 11:54:39 +02002756void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002757{
2758 if( ssl->handshake == NULL )
2759 return( NULL );
2760 else
2761 return( ssl->handshake->user_async_ctx );
2762}
2763
Gilles Peskine1febfef2018-04-30 11:54:39 +02002764void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002765 void *ctx )
2766{
2767 if( ssl->handshake != NULL )
2768 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002769}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002770#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002771
Paul Bakker5121ce52009-01-03 21:22:43 +00002772/*
2773 * SSL get accessors
2774 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002775uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002776{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002777 if( ssl->session != NULL )
2778 return( ssl->session->verify_result );
2779
2780 if( ssl->session_negotiate != NULL )
2781 return( ssl->session_negotiate->verify_result );
2782
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002783 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002784}
2785
Glenn Strauss8f526902022-01-13 00:04:49 -05002786int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2787{
2788 if( ssl == NULL || ssl->session == NULL )
2789 return( 0 );
2790
2791 return( ssl->session->ciphersuite );
2792}
2793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002794const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002795{
Paul Bakker926c8e42013-03-06 10:23:34 +01002796 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002797 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002800}
2801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002803{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002804#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002805 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002806 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002807 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002808 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002809 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002810 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002811 default:
2812 return( "unknown (DTLS)" );
2813 }
2814 }
2815#endif
2816
Glenn Strauss60bfe602022-03-14 19:04:24 -04002817 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002818 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002819 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002820 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002821 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002822 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002823 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002824 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002825 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002826}
2827
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002828#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002829size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2830{
David Horstmann95d516f2021-05-04 18:36:56 +01002831 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002832 size_t read_mfl;
2833
2834 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2835 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2836 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2837 {
2838 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2839 }
2840
2841 /* Check if a smaller max length was negotiated */
2842 if( ssl->session_out != NULL )
2843 {
2844 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2845 if( read_mfl < max_len )
2846 {
2847 max_len = read_mfl;
2848 }
2849 }
2850
2851 // During a handshake, use the value being negotiated
2852 if( ssl->session_negotiate != NULL )
2853 {
2854 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2855 if( read_mfl < max_len )
2856 {
2857 max_len = read_mfl;
2858 }
2859 }
2860
2861 return( max_len );
2862}
2863
2864size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002865{
2866 size_t max_len;
2867
2868 /*
2869 * Assume mfl_code is correct since it was checked when set
2870 */
Angus Grattond8213d02016-05-25 20:56:48 +10002871 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002872
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002873 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002874 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002875 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002876 {
Angus Grattond8213d02016-05-25 20:56:48 +10002877 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002878 }
2879
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002880 /* During a handshake, use the value being negotiated */
2881 if( ssl->session_negotiate != NULL &&
2882 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2883 {
2884 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2885 }
2886
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002887 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002888}
2889#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2890
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002891#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002892size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002893{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002894 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2895 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2896 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2897 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2898 return ( 0 );
2899
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002900 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2901 return( ssl->mtu );
2902
2903 if( ssl->mtu == 0 )
2904 return( ssl->handshake->mtu );
2905
2906 return( ssl->mtu < ssl->handshake->mtu ?
2907 ssl->mtu : ssl->handshake->mtu );
2908}
2909#endif /* MBEDTLS_SSL_PROTO_DTLS */
2910
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002911int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2912{
2913 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2914
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002915#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2916 !defined(MBEDTLS_SSL_PROTO_DTLS)
2917 (void) ssl;
2918#endif
2919
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002920#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002921 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002922
2923 if( max_len > mfl )
2924 max_len = mfl;
2925#endif
2926
2927#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002928 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002929 {
Hanno Becker89490712020-02-05 10:50:12 +00002930 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002931 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2932 const size_t overhead = (size_t) ret;
2933
2934 if( ret < 0 )
2935 return( ret );
2936
2937 if( mtu <= overhead )
2938 {
2939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2940 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2941 }
2942
2943 if( max_len > mtu - overhead )
2944 max_len = mtu - overhead;
2945 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002946#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002947
Hanno Becker0defedb2018-08-10 12:35:02 +01002948#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2949 !defined(MBEDTLS_SSL_PROTO_DTLS)
2950 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002951#endif
2952
2953 return( (int) max_len );
2954}
2955
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002956int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2957{
2958 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2959
2960#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2961 (void) ssl;
2962#endif
2963
2964#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2965 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2966
2967 if( max_len > mfl )
2968 max_len = mfl;
2969#endif
2970
2971 return( (int) max_len );
2972}
2973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974#if defined(MBEDTLS_X509_CRT_PARSE_C)
2975const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002976{
2977 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002978 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002979
Hanno Beckere6824572019-02-07 13:18:46 +00002980#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002981 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002982#else
2983 return( NULL );
2984#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002985}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002989int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2990 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002991{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002992 int ret;
2993
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002994 if( ssl == NULL ||
2995 dst == NULL ||
2996 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002997 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002999 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003000 }
3001
Hanno Beckere810bbc2021-05-14 16:01:05 +01003002 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3003 * idempotent: Each session can only be exported once.
3004 *
3005 * (This is in preparation for TLS 1.3 support where we will
3006 * need the ability to export multiple sessions (aka tickets),
3007 * which will be achieved by calling mbedtls_ssl_get_session()
3008 * multiple times until it fails.)
3009 *
3010 * Check whether we have already exported the current session,
3011 * and fail if so.
3012 */
3013 if( ssl->session->exported == 1 )
3014 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3015
3016 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3017 if( ret != 0 )
3018 return( ret );
3019
3020 /* Remember that we've exported the session. */
3021 ssl->session->exported = 1;
3022 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003023}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003024#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003025
Paul Bakker5121ce52009-01-03 21:22:43 +00003026/*
Hanno Beckera835da52019-05-16 12:39:07 +01003027 * Define ticket header determining Mbed TLS version
3028 * and structure of the ticket.
3029 */
3030
Hanno Becker94ef3b32019-05-16 12:50:45 +01003031/*
Hanno Becker50b59662019-05-28 14:30:45 +01003032 * Define bitflag determining compile-time settings influencing
3033 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003034 */
3035
Hanno Becker50b59662019-05-28 14:30:45 +01003036#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003037#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003038#else
Hanno Becker3e088662019-05-29 11:10:18 +01003039#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003040#endif /* MBEDTLS_HAVE_TIME */
3041
3042#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003043#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003044#else
Hanno Becker3e088662019-05-29 11:10:18 +01003045#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003046#endif /* MBEDTLS_X509_CRT_PARSE_C */
3047
3048#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003049#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003050#else
Hanno Becker3e088662019-05-29 11:10:18 +01003051#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003052#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3053
3054#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003055#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003056#else
Hanno Becker3e088662019-05-29 11:10:18 +01003057#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003058#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3059
Hanno Becker94ef3b32019-05-16 12:50:45 +01003060#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003061#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003062#else
Hanno Becker3e088662019-05-29 11:10:18 +01003063#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003064#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3065
Hanno Becker94ef3b32019-05-16 12:50:45 +01003066#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3067#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3068#else
3069#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3070#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3071
Hanno Becker3e088662019-05-29 11:10:18 +01003072#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3073#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3074#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3075#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003076#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3077#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003078
Hanno Becker50b59662019-05-28 14:30:45 +01003079#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003080 ( (uint16_t) ( \
3081 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3082 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3083 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3084 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003085 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003086 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003087
Hanno Beckerf8787072019-05-16 12:41:07 +01003088static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003089 MBEDTLS_VERSION_MAJOR,
3090 MBEDTLS_VERSION_MINOR,
3091 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003092 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3093 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003094};
Hanno Beckera835da52019-05-16 12:39:07 +01003095
3096/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003097 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003098 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003099 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003100 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003101 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003102 * opaque mbedtls_version[3]; // library version: major, minor, patch
3103 * opaque session_format[2]; // library-version specific 16-bit field
3104 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003105 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003106 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003107 * Note: When updating the format, remember to keep
3108 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003109 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003110 * // In this version, `session_format` determines
3111 * // the setting of those compile-time
3112 * // configuration options which influence
3113 * // the structure of mbedtls_ssl_session.
3114 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003115 * uint8_t minor_ver; // Protocol minor version. Possible values:
3116 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003117 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003118 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003119 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003120 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003121 * serialized_session_tls12 data;
3122 *
3123 * };
3124 *
3125 * } serialized_session;
3126 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003127 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003128
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003129MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003130static int ssl_session_save( const mbedtls_ssl_session *session,
3131 unsigned char omit_header,
3132 unsigned char *buf,
3133 size_t buf_len,
3134 size_t *olen )
3135{
3136 unsigned char *p = buf;
3137 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003138 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003139#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3140 size_t out_len;
3141 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3142#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003143 if( session == NULL )
3144 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3145
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003146 if( !omit_header )
3147 {
3148 /*
3149 * Add Mbed TLS version identifier
3150 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003151 used += sizeof( ssl_serialized_session_header );
3152
3153 if( used <= buf_len )
3154 {
3155 memcpy( p, ssl_serialized_session_header,
3156 sizeof( ssl_serialized_session_header ) );
3157 p += sizeof( ssl_serialized_session_header );
3158 }
3159 }
3160
3161 /*
3162 * TLS version identifier
3163 */
3164 used += 1;
3165 if( used <= buf_len )
3166 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003167 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003168 }
3169
3170 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003171 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003172 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003173 {
3174#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003175 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003176 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003177 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003178#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3179
Jerry Yu251a12e2022-07-13 15:15:48 +08003180#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3181 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003182 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3183 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003184 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003185 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003186 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003187#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3188
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003189 default:
3190 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3191 }
3192
3193 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003194 if( used > buf_len )
3195 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003196
3197 return( 0 );
3198}
3199
3200/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003201 * Public wrapper for ssl_session_save()
3202 */
3203int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3204 unsigned char *buf,
3205 size_t buf_len,
3206 size_t *olen )
3207{
3208 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3209}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003210
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003211/*
3212 * Deserialize session, see mbedtls_ssl_session_save() for format.
3213 *
3214 * This internal version is wrapped by a public function that cleans up in
3215 * case of error, and has an extra option omit_header.
3216 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003217MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003218static int ssl_session_load( mbedtls_ssl_session *session,
3219 unsigned char omit_header,
3220 const unsigned char *buf,
3221 size_t len )
3222{
3223 const unsigned char *p = buf;
3224 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003225 size_t remaining_len;
3226
3227
3228 if( session == NULL )
3229 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003230
3231 if( !omit_header )
3232 {
3233 /*
3234 * Check Mbed TLS version identifier
3235 */
3236
3237 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3238 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3239
3240 if( memcmp( p, ssl_serialized_session_header,
3241 sizeof( ssl_serialized_session_header ) ) != 0 )
3242 {
3243 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3244 }
3245 p += sizeof( ssl_serialized_session_header );
3246 }
3247
3248 /*
3249 * TLS version identifier
3250 */
3251 if( 1 > (size_t)( end - p ) )
3252 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003253 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003254
3255 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003256 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003257 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003258 {
3259#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003260 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003261 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003262#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3263
Jerry Yu438ddd82022-07-07 06:55:50 +00003264#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3265 case MBEDTLS_SSL_VERSION_TLS1_3:
3266 return( ssl_tls13_session_load( session, p, remaining_len ) );
3267#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3268
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003269 default:
3270 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3271 }
3272}
3273
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003274/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003275 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003276 */
3277int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3278 const unsigned char *buf,
3279 size_t len )
3280{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003281 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003282
3283 if( ret != 0 )
3284 mbedtls_ssl_session_free( session );
3285
3286 return( ret );
3287}
3288
3289/*
Paul Bakker1961b702013-01-25 14:49:24 +01003290 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003291 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003292MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003293static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3294{
3295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3296
Ronald Cron66dbf912022-02-02 15:33:46 +01003297 /*
3298 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003299 * that were written into the output buffer by the previous handshake step,
3300 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003301 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3302 * We proceed to the next handshake step only when all data from the
3303 * previous one have been sent to the peer, thus we make sure that this is
3304 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3305 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3306 * we have to wait before to go ahead.
3307 * In the case of TLS 1.3, handshake step handlers do not send data to the
3308 * peer. Data are only sent here and through
3309 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003310 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003311 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003312 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3313 return( ret );
3314
3315#if defined(MBEDTLS_SSL_PROTO_DTLS)
3316 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3317 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3318 {
3319 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3320 return( ret );
3321 }
3322#endif /* MBEDTLS_SSL_PROTO_DTLS */
3323
3324 return( ret );
3325}
3326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003327int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003328{
Hanno Becker41934dd2021-08-07 19:13:43 +01003329 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003330
Hanno Becker41934dd2021-08-07 19:13:43 +01003331 if( ssl == NULL ||
3332 ssl->conf == NULL ||
3333 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003334 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003335 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003336 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003337 }
3338
3339 ret = ssl_prepare_handshake_step( ssl );
3340 if( ret != 0 )
3341 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003342
Jerry Yue7047812021-09-13 19:26:39 +08003343 ret = mbedtls_ssl_handle_pending_alert( ssl );
3344 if( ret != 0 )
3345 goto cleanup;
3346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003348 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003349 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3351 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003352
Ronald Cron9f0fba32022-02-10 16:45:15 +01003353 switch( ssl->state )
3354 {
3355 case MBEDTLS_SSL_HELLO_REQUEST:
3356 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3357 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003358
Ronald Cron9f0fba32022-02-10 16:45:15 +01003359 case MBEDTLS_SSL_CLIENT_HELLO:
3360 ret = mbedtls_ssl_write_client_hello( ssl );
3361 break;
3362
3363 default:
3364#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003365 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003366 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3367 else
3368 ret = mbedtls_ssl_handshake_client_step( ssl );
3369#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3370 ret = mbedtls_ssl_handshake_client_step( ssl );
3371#else
3372 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3373#endif
3374 }
Jerry Yub9930e72021-08-06 17:11:51 +08003375 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003376#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003378 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003379 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003380#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003381 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003382 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003383#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003384
3385#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3386 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3387 ret = mbedtls_ssl_handshake_server_step( ssl );
3388#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3389 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003390#endif
3391
Jerry Yue7047812021-09-13 19:26:39 +08003392 if( ret != 0 )
3393 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003394 /* handshake_step return error. And it is same
3395 * with alert_reason.
3396 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003397 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003398 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003399 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003400 goto cleanup;
3401 }
3402 }
3403
3404cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003405 return( ret );
3406}
3407
3408/*
3409 * Perform the SSL handshake
3410 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003412{
3413 int ret = 0;
3414
Hanno Beckera817ea42020-10-20 15:20:23 +01003415 /* Sanity checks */
3416
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003417 if( ssl == NULL || ssl->conf == NULL )
3418 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3419
Hanno Beckera817ea42020-10-20 15:20:23 +01003420#if defined(MBEDTLS_SSL_PROTO_DTLS)
3421 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3422 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3423 {
3424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3425 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3426 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3427 }
3428#endif /* MBEDTLS_SSL_PROTO_DTLS */
3429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003431
Hanno Beckera817ea42020-10-20 15:20:23 +01003432 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003433 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003436
3437 if( ret != 0 )
3438 break;
3439 }
3440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003442
3443 return( ret );
3444}
3445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003446#if defined(MBEDTLS_SSL_RENEGOTIATION)
3447#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003448/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003449 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003450 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003451MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003453{
Janos Follath865b3eb2019-12-16 11:46:15 +00003454 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003457
3458 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003459 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3460 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003461
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003462 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003463 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003464 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003465 return( ret );
3466 }
3467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003469
3470 return( 0 );
3471}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003473
3474/*
3475 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003476 * - any side: calling mbedtls_ssl_renegotiate(),
3477 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3478 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003479 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003480 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003481 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003482 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003483int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003484{
Janos Follath865b3eb2019-12-16 11:46:15 +00003485 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003488
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003489 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3490 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003491
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003492 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3493 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003495 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003496 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003497 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003498 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003499 ssl->handshake->out_msg_seq = 1;
3500 else
3501 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003502 }
3503#endif
3504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003505 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3506 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003508 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003510 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003511 return( ret );
3512 }
3513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003515
3516 return( 0 );
3517}
3518
3519/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003520 * Renegotiate current connection on client,
3521 * or request renegotiation on server
3522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003524{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003525 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003526
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003527 if( ssl == NULL || ssl->conf == NULL )
3528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003530#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003531 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003532 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003533 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003534 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003535 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003537 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003538
3539 /* Did we already try/start sending HelloRequest? */
3540 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003541 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003542
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003543 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003544 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003547#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003548 /*
3549 * On client, either start the renegotiation process or,
3550 * if already in progress, continue the handshake
3551 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003552 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003553 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003554 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003556
Hanno Becker40cdaa12020-02-05 10:48:27 +00003557 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003558 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003559 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003560 return( ret );
3561 }
3562 }
3563 else
3564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003565 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003568 return( ret );
3569 }
3570 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003571#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003572
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003573 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003574}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003576
Gilles Peskine9b562d52018-04-25 20:32:43 +02003577void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003578{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003579 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3580
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003581 if( handshake == NULL )
3582 return;
3583
Brett Warrene0edc842021-08-17 09:53:13 +01003584#if defined(MBEDTLS_ECP_C)
3585#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3586 if ( ssl->handshake->group_list_heap_allocated )
3587 mbedtls_free( (void*) handshake->group_list );
3588 handshake->group_list = NULL;
3589#endif /* MBEDTLS_DEPRECATED_REMOVED */
3590#endif /* MBEDTLS_ECP_C */
3591
Ronald Crone68ab4f2022-10-05 12:46:29 +02003592#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003593#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3594 if ( ssl->handshake->sig_algs_heap_allocated )
3595 mbedtls_free( (void*) handshake->sig_algs );
3596 handshake->sig_algs = NULL;
3597#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003598#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3599 if( ssl->handshake->certificate_request_context )
3600 {
3601 mbedtls_free( (void*) handshake->certificate_request_context );
3602 }
3603#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02003604#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08003605
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003606#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3607 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3608 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003609 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003610 handshake->async_in_progress = 0;
3611 }
3612#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3613
Andrzej Kurek25f27152022-08-17 16:09:31 -04003614#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003615#if defined(MBEDTLS_USE_PSA_CRYPTO)
3616 psa_hash_abort( &handshake->fin_sha256_psa );
3617#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003618 mbedtls_sha256_free( &handshake->fin_sha256 );
3619#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003620#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003621#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003622#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003623 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003624#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003625 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003626#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003627#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629#if defined(MBEDTLS_DHM_C)
3630 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003631#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003632#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003634#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003635#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003636 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003637#if defined(MBEDTLS_SSL_CLI_C)
3638 mbedtls_free( handshake->ecjpake_cache );
3639 handshake->ecjpake_cache = NULL;
3640 handshake->ecjpake_cache_len = 0;
3641#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003642#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003643
Janos Follath4ae5c292016-02-10 11:27:43 +00003644#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3645 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003646 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003648#endif
3649
Ronald Cron73fe8df2022-10-05 14:31:43 +02003650#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003651#if defined(MBEDTLS_USE_PSA_CRYPTO)
3652 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3653 {
3654 /* The maintenance of the external PSK key slot is the
3655 * user's responsibility. */
3656 if( ssl->handshake->psk_opaque_is_internal )
3657 {
3658 psa_destroy_key( ssl->handshake->psk_opaque );
3659 ssl->handshake->psk_opaque_is_internal = 0;
3660 }
3661 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3662 }
Neil Armstronge952a302022-05-03 10:22:14 +02003663#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003664 if( handshake->psk != NULL )
3665 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003666 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003667 mbedtls_free( handshake->psk );
3668 }
Neil Armstronge952a302022-05-03 10:22:14 +02003669#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02003670#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3673 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003674 /*
3675 * Free only the linked list wrapper, not the keys themselves
3676 * since the belong to the SNI callback
3677 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003678 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003679#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003680
Gilles Peskineeccd8882020-03-10 12:19:08 +01003681#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003682 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003683 if( handshake->ecrs_peer_cert != NULL )
3684 {
3685 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3686 mbedtls_free( handshake->ecrs_peer_cert );
3687 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003688#endif
3689
Hanno Becker75173122019-02-06 16:18:31 +00003690#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3691 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3692 mbedtls_pk_free( &handshake->peer_pubkey );
3693#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3694
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003695#if defined(MBEDTLS_SSL_CLI_C) && \
3696 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3697 mbedtls_free( handshake->cookie );
3698#endif /* MBEDTLS_SSL_CLI_C &&
3699 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003700
3701#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003702 mbedtls_ssl_flight_free( handshake->flight );
3703 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003704#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003705
Ronald Cronf12b81d2022-03-15 10:42:41 +01003706#if defined(MBEDTLS_ECDH_C) && \
3707 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003708 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003709 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003710#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3711
Ronald Cron6f135e12021-12-08 16:57:54 +01003712#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003713 mbedtls_ssl_transform_free( handshake->transform_handshake );
3714 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003715 mbedtls_free( handshake->transform_earlydata );
3716 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003717#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003718
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003719
3720#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3721 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3722 * processes datagrams and the fact that a datagram is allowed to have
3723 * several records in it, it is possible that the I/O buffers are not
3724 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003725 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3726 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003727#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003728
Jerry Yuba9c7272021-10-30 11:54:10 +08003729 /* mbedtls_platform_zeroize MUST be last one in this function */
3730 mbedtls_platform_zeroize( handshake,
3731 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003732}
3733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003734void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003735{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003736 if( session == NULL )
3737 return;
3738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003739#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003740 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003741#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003742
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003743#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00003744#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
3745 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00003746 mbedtls_free( session->hostname );
3747#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003748 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003749#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003750
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003751 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003752}
3753
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003754#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003755
3756#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3757#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3758#else
3759#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3760#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3761
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003762#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003763
3764#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3765#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3766#else
3767#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3768#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3769
3770#if defined(MBEDTLS_SSL_ALPN)
3771#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3772#else
3773#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3774#endif /* MBEDTLS_SSL_ALPN */
3775
3776#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3777#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3778#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3779#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3780
3781#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3782 ( (uint32_t) ( \
3783 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3784 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3785 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3786 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3787 0u ) )
3788
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003789static unsigned char ssl_serialized_context_header[] = {
3790 MBEDTLS_VERSION_MAJOR,
3791 MBEDTLS_VERSION_MINOR,
3792 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003793 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3794 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3795 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3796 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3797 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003798};
3799
Paul Bakker5121ce52009-01-03 21:22:43 +00003800/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003801 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003802 *
3803 * The format of the serialized data is:
3804 * (in the presentation language of TLS, RFC 8446 section 3)
3805 *
3806 * // header
3807 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003808 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003809 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003810 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003811 * Note: When updating the format, remember to keep these
3812 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003813 *
3814 * // session sub-structure
3815 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3816 * // transform sub-structure
3817 * uint8 random[64]; // ServerHello.random+ClientHello.random
3818 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3819 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3820 * // fields from ssl_context
3821 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3822 * uint64 in_window_top; // DTLS: last validated record seq_num
3823 * uint64 in_window; // DTLS: bitmask for replay protection
3824 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3825 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3826 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3827 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3828 *
3829 * Note that many fields of the ssl_context or sub-structures are not
3830 * serialized, as they fall in one of the following categories:
3831 *
3832 * 1. forced value (eg in_left must be 0)
3833 * 2. pointer to dynamically-allocated memory (eg session, transform)
3834 * 3. value can be re-derived from other data (eg session keys from MS)
3835 * 4. value was temporary (eg content of input buffer)
3836 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003837 */
3838int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3839 unsigned char *buf,
3840 size_t buf_len,
3841 size_t *olen )
3842{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003843 unsigned char *p = buf;
3844 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003845 size_t session_len;
3846 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003847
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003848 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003849 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3850 * this function's documentation.
3851 *
3852 * These are due to assumptions/limitations in the implementation. Some of
3853 * them are likely to stay (no handshake in progress) some might go away
3854 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003855 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003856 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003857 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003858 {
3859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003860 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003861 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003862 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003863 {
3864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003865 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003866 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003867 /* Double-check that sub-structures are indeed ready */
3868 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003869 {
3870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003871 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003872 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003873 /* There must be no pending incoming or outgoing data */
3874 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003875 {
3876 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003877 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003878 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003879 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003880 {
3881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003882 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003883 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003884 /* Protocol must be DLTS, not TLS */
3885 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003886 {
3887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003888 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003889 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003890 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003891 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003892 {
3893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003894 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003895 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003896 /* We must be using an AEAD ciphersuite */
3897 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003898 {
3899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003900 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003901 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003902 /* Renegotiation must not be enabled */
3903#if defined(MBEDTLS_SSL_RENEGOTIATION)
3904 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003905 {
3906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003907 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003908 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003909#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003910
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003911 /*
3912 * Version and format identifier
3913 */
3914 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003915
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003916 if( used <= buf_len )
3917 {
3918 memcpy( p, ssl_serialized_context_header,
3919 sizeof( ssl_serialized_context_header ) );
3920 p += sizeof( ssl_serialized_context_header );
3921 }
3922
3923 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003924 * Session (length + data)
3925 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003926 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003927 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3928 return( ret );
3929
3930 used += 4 + session_len;
3931 if( used <= buf_len )
3932 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003933 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3934 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003935
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003936 ret = ssl_session_save( ssl->session, 1,
3937 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003938 if( ret != 0 )
3939 return( ret );
3940
3941 p += session_len;
3942 }
3943
3944 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003945 * Transform
3946 */
3947 used += sizeof( ssl->transform->randbytes );
3948 if( used <= buf_len )
3949 {
3950 memcpy( p, ssl->transform->randbytes,
3951 sizeof( ssl->transform->randbytes ) );
3952 p += sizeof( ssl->transform->randbytes );
3953 }
3954
3955#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3956 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3957 if( used <= buf_len )
3958 {
3959 *p++ = ssl->transform->in_cid_len;
3960 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3961 p += ssl->transform->in_cid_len;
3962
3963 *p++ = ssl->transform->out_cid_len;
3964 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3965 p += ssl->transform->out_cid_len;
3966 }
3967#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3968
3969 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003970 * Saved fields from top-level ssl_context structure
3971 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003972 used += 4;
3973 if( used <= buf_len )
3974 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003975 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3976 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003977 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003978
3979#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3980 used += 16;
3981 if( used <= buf_len )
3982 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003983 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3984 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003985
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003986 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3987 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003988 }
3989#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3990
3991#if defined(MBEDTLS_SSL_PROTO_DTLS)
3992 used += 1;
3993 if( used <= buf_len )
3994 {
3995 *p++ = ssl->disable_datagram_packing;
3996 }
3997#endif /* MBEDTLS_SSL_PROTO_DTLS */
3998
Jerry Yuae0b2e22021-10-08 15:21:19 +08003999 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004000 if( used <= buf_len )
4001 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004002 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
4003 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004004 }
4005
4006#if defined(MBEDTLS_SSL_PROTO_DTLS)
4007 used += 2;
4008 if( used <= buf_len )
4009 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004010 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4011 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004012 }
4013#endif /* MBEDTLS_SSL_PROTO_DTLS */
4014
4015#if defined(MBEDTLS_SSL_ALPN)
4016 {
4017 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004018 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004019 : 0;
4020
4021 used += 1 + alpn_len;
4022 if( used <= buf_len )
4023 {
4024 *p++ = alpn_len;
4025
4026 if( ssl->alpn_chosen != NULL )
4027 {
4028 memcpy( p, ssl->alpn_chosen, alpn_len );
4029 p += alpn_len;
4030 }
4031 }
4032 }
4033#endif /* MBEDTLS_SSL_ALPN */
4034
4035 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004036 * Done
4037 */
4038 *olen = used;
4039
4040 if( used > buf_len )
4041 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004042
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004043 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4044
Hanno Becker43aefe22020-02-05 10:44:56 +00004045 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004046}
4047
4048/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004049 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004050 *
4051 * This internal version is wrapped by a public function that cleans up in
4052 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004053 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004054MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004055static int ssl_context_load( mbedtls_ssl_context *ssl,
4056 const unsigned char *buf,
4057 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004058{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004059 const unsigned char *p = buf;
4060 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004061 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004062 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004063#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004064 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004065#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004066
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004067 /*
4068 * The context should have been freshly setup or reset.
4069 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004070 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004071 * renegotiating, or if the user mistakenly loaded a session first.)
4072 */
4073 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4074 ssl->session != NULL )
4075 {
4076 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4077 }
4078
4079 /*
4080 * We can't check that the config matches the initial one, but we can at
4081 * least check it matches the requirements for serializing.
4082 */
4083 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004084 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4085 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004086#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004087 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004088#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004089 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004090 {
4091 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4092 }
4093
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004094 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4095
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004096 /*
4097 * Check version identifier
4098 */
4099 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4100 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4101
4102 if( memcmp( p, ssl_serialized_context_header,
4103 sizeof( ssl_serialized_context_header ) ) != 0 )
4104 {
4105 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4106 }
4107 p += sizeof( ssl_serialized_context_header );
4108
4109 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004110 * Session
4111 */
4112 if( (size_t)( end - p ) < 4 )
4113 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4114
4115 session_len = ( (size_t) p[0] << 24 ) |
4116 ( (size_t) p[1] << 16 ) |
4117 ( (size_t) p[2] << 8 ) |
4118 ( (size_t) p[3] );
4119 p += 4;
4120
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004121 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004122 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004123 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004124 ssl->session_in = ssl->session;
4125 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004126 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004127
4128 if( (size_t)( end - p ) < session_len )
4129 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4130
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004131 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004132 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004133 {
4134 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004135 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004136 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004137
4138 p += session_len;
4139
4140 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004141 * Transform
4142 */
4143
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004144 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004145 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004146 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004147 ssl->transform_in = ssl->transform;
4148 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004149 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004150
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004151#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004152 prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite );
4153 if( prf_func == NULL )
4154 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4155
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004156 /* Read random bytes and populate structure */
4157 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4158 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004159
Hanno Beckerbd257552021-03-22 06:59:27 +00004160 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004161 ssl->session->ciphersuite,
4162 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004163#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004164 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004165#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Andrzej Kurek894edde2022-09-29 06:31:14 -04004166 prf_func,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004167 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004168 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004169 ssl->conf->endpoint,
4170 ssl );
4171 if( ret != 0 )
4172 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004173#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004174 p += sizeof( ssl->transform->randbytes );
4175
4176#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4177 /* Read connection IDs and store them */
4178 if( (size_t)( end - p ) < 1 )
4179 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4180
4181 ssl->transform->in_cid_len = *p++;
4182
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004183 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004184 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4185
4186 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4187 p += ssl->transform->in_cid_len;
4188
4189 ssl->transform->out_cid_len = *p++;
4190
4191 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4192 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4193
4194 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4195 p += ssl->transform->out_cid_len;
4196#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4197
4198 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004199 * Saved fields from top-level ssl_context structure
4200 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004201 if( (size_t)( end - p ) < 4 )
4202 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4203
4204 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4205 ( (uint32_t) p[1] << 16 ) |
4206 ( (uint32_t) p[2] << 8 ) |
4207 ( (uint32_t) p[3] );
4208 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004209
4210#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4211 if( (size_t)( end - p ) < 16 )
4212 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4213
4214 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4215 ( (uint64_t) p[1] << 48 ) |
4216 ( (uint64_t) p[2] << 40 ) |
4217 ( (uint64_t) p[3] << 32 ) |
4218 ( (uint64_t) p[4] << 24 ) |
4219 ( (uint64_t) p[5] << 16 ) |
4220 ( (uint64_t) p[6] << 8 ) |
4221 ( (uint64_t) p[7] );
4222 p += 8;
4223
4224 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4225 ( (uint64_t) p[1] << 48 ) |
4226 ( (uint64_t) p[2] << 40 ) |
4227 ( (uint64_t) p[3] << 32 ) |
4228 ( (uint64_t) p[4] << 24 ) |
4229 ( (uint64_t) p[5] << 16 ) |
4230 ( (uint64_t) p[6] << 8 ) |
4231 ( (uint64_t) p[7] );
4232 p += 8;
4233#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4234
4235#if defined(MBEDTLS_SSL_PROTO_DTLS)
4236 if( (size_t)( end - p ) < 1 )
4237 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4238
4239 ssl->disable_datagram_packing = *p++;
4240#endif /* MBEDTLS_SSL_PROTO_DTLS */
4241
Jerry Yud9a94fe2021-09-28 18:58:59 +08004242 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004243 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004244 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4245 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004246
4247#if defined(MBEDTLS_SSL_PROTO_DTLS)
4248 if( (size_t)( end - p ) < 2 )
4249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4250
4251 ssl->mtu = ( p[0] << 8 ) | p[1];
4252 p += 2;
4253#endif /* MBEDTLS_SSL_PROTO_DTLS */
4254
4255#if defined(MBEDTLS_SSL_ALPN)
4256 {
4257 uint8_t alpn_len;
4258 const char **cur;
4259
4260 if( (size_t)( end - p ) < 1 )
4261 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4262
4263 alpn_len = *p++;
4264
4265 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4266 {
4267 /* alpn_chosen should point to an item in the configured list */
4268 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4269 {
4270 if( strlen( *cur ) == alpn_len &&
4271 memcmp( p, cur, alpn_len ) == 0 )
4272 {
4273 ssl->alpn_chosen = *cur;
4274 break;
4275 }
4276 }
4277 }
4278
4279 /* can only happen on conf mismatch */
4280 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4281 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4282
4283 p += alpn_len;
4284 }
4285#endif /* MBEDTLS_SSL_ALPN */
4286
4287 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004288 * Forced fields from top-level ssl_context structure
4289 *
4290 * Most of them already set to the correct value by mbedtls_ssl_init() and
4291 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4292 */
4293 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004294 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004295
Hanno Becker361b10d2019-08-30 10:42:49 +01004296 /* Adjust pointers for header fields of outgoing records to
4297 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004298 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004299
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004300#if defined(MBEDTLS_SSL_PROTO_DTLS)
4301 ssl->in_epoch = 1;
4302#endif
4303
4304 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4305 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004306 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4307 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004308 if( ssl->handshake != NULL )
4309 {
4310 mbedtls_ssl_handshake_free( ssl );
4311 mbedtls_free( ssl->handshake );
4312 ssl->handshake = NULL;
4313 }
4314
4315 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004316 * Done - should have consumed entire buffer
4317 */
4318 if( p != end )
4319 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004320
4321 return( 0 );
4322}
4323
4324/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004325 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004326 */
4327int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4328 const unsigned char *buf,
4329 size_t len )
4330{
4331 int ret = ssl_context_load( context, buf, len );
4332
4333 if( ret != 0 )
4334 mbedtls_ssl_free( context );
4335
4336 return( ret );
4337}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004338#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004339
4340/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004341 * Free an SSL context
4342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004343void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004344{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004345 if( ssl == NULL )
4346 return;
4347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004349
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004350 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004351 {
sander-visserb8aa2072020-05-06 22:05:13 +02004352#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4353 size_t out_buf_len = ssl->out_buf_len;
4354#else
4355 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4356#endif
4357
Darryl Greenb33cc762019-11-28 14:29:44 +00004358 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004359 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004360 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004361 }
4362
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004363 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004364 {
sander-visserb8aa2072020-05-06 22:05:13 +02004365#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4366 size_t in_buf_len = ssl->in_buf_len;
4367#else
4368 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4369#endif
4370
Darryl Greenb33cc762019-11-28 14:29:44 +00004371 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004372 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004373 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004374 }
4375
Paul Bakker48916f92012-09-16 19:57:18 +00004376 if( ssl->transform )
4377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004378 mbedtls_ssl_transform_free( ssl->transform );
4379 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004380 }
4381
4382 if( ssl->handshake )
4383 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004384 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004385 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4386 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004388 mbedtls_free( ssl->handshake );
4389 mbedtls_free( ssl->transform_negotiate );
4390 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004391 }
4392
Ronald Cron6f135e12021-12-08 16:57:54 +01004393#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004394 mbedtls_ssl_transform_free( ssl->transform_application );
4395 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004396#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004397
Paul Bakkerc0463502013-02-14 11:19:38 +01004398 if( ssl->session )
4399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004400 mbedtls_ssl_session_free( ssl->session );
4401 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004402 }
4403
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004404#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004405 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004406 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004407 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004408 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004409 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004410#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004411
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004412#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004413 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004414#endif
4415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004417
Paul Bakker86f04f42013-02-14 11:20:09 +01004418 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004419 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004420}
4421
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004422/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004423 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004424 */
4425void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4426{
4427 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4428}
4429
Gilles Peskineae270bf2021-06-02 00:05:29 +02004430/* The selection should be the same as mbedtls_x509_crt_profile_default in
4431 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004432 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004433 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004434 * about this list.
4435 */
Brett Warrene0edc842021-08-17 09:53:13 +01004436static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004437#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004438 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004439#endif
4440#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004441 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004442#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004443#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004444 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004445#endif
4446#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004447 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004448#endif
4449#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004450 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004451#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004452#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004453 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004454#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004455#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004456 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004457#endif
4458#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004459 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004460#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004461 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004462};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004463
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004464static int ssl_preset_suiteb_ciphersuites[] = {
4465 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4466 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4467 0
4468};
4469
Ronald Crone68ab4f2022-10-05 12:46:29 +02004470#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004471
Jerry Yu909df7b2022-01-22 11:56:27 +08004472/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004473 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004474 * rules SHOULD be upheld.
4475 * - No duplicate entries.
4476 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004477 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004478 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004479 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004480static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004481
Andrzej Kurek25f27152022-08-17 16:09:31 -04004482#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 +08004483 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4484 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004485#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004486 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004487
Andrzej Kurek25f27152022-08-17 16:09:31 -04004488#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 +08004489 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4490 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004491#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004492 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4493
Andrzej Kurek25f27152022-08-17 16:09:31 -04004494#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 +08004495 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4496 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004497#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004498 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004499
Andrzej Kurek25f27152022-08-17 16:09:31 -04004500#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 +08004501 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004502#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 +08004503
Andrzej Kurek25f27152022-08-17 16:09:31 -04004504#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 +08004505 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004506#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 +08004507
Andrzej Kurek25f27152022-08-17 16:09:31 -04004508#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 +08004509 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004510#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 +08004511
Andrzej Kurek25f27152022-08-17 16:09:31 -04004512#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 +08004513 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4514#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4515
Andrzej Kurek25f27152022-08-17 16:09:31 -04004516#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 +08004517 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4518#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4519
Andrzej Kurek25f27152022-08-17 16:09:31 -04004520#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 +08004521 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4522#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4523
Gabor Mezei15b95a62022-05-09 16:37:58 +02004524 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004525};
4526
4527/* NOTICE: see above */
4528#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4529static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004530#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004531#if defined(MBEDTLS_ECDSA_C)
4532 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004533#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004534#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4535 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4536#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004537#if defined(MBEDTLS_RSA_C)
4538 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4539#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004540#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004541#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004542#if defined(MBEDTLS_ECDSA_C)
4543 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004544#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004545#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4546 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4547#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004548#if defined(MBEDTLS_RSA_C)
4549 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4550#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004551#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004552#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004553#if defined(MBEDTLS_ECDSA_C)
4554 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004555#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004556#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4557 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4558#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004559#if defined(MBEDTLS_RSA_C)
4560 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4561#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004562#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004563 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004564};
4565#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4566/* NOTICE: see above */
4567static uint16_t ssl_preset_suiteb_sig_algs[] = {
4568
Andrzej Kurek25f27152022-08-17 16:09:31 -04004569#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 +08004570 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4571 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004572#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004573 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4574
Andrzej Kurek25f27152022-08-17 16:09:31 -04004575#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 +08004576 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4577 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004578#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004579 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004580
Andrzej Kurek25f27152022-08-17 16:09:31 -04004581#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 +08004582 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004583#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 +08004584
Andrzej Kurek25f27152022-08-17 16:09:31 -04004585#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 +08004586 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004587#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004588
Gabor Mezei15b95a62022-05-09 16:37:58 +02004589 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004590};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004591
Jerry Yu909df7b2022-01-22 11:56:27 +08004592/* NOTICE: see above */
4593#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4594static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004595#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004596#if defined(MBEDTLS_ECDSA_C)
4597 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004598#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004599#if defined(MBEDTLS_RSA_C)
4600 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4601#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004602#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004603#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004604#if defined(MBEDTLS_ECDSA_C)
4605 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004606#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004607#if defined(MBEDTLS_RSA_C)
4608 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4609#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004610#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004611 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004612};
Jerry Yu909df7b2022-01-22 11:56:27 +08004613#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4614
Ronald Crone68ab4f2022-10-05 12:46:29 +02004615#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004616
Brett Warrene0edc842021-08-17 09:53:13 +01004617static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004618#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004619 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004620#endif
4621#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004622 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004623#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004624 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004625};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004626
Ronald Crone68ab4f2022-10-05 12:46:29 +02004627#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004628/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004629 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004630MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004631static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004632{
4633 size_t i, j;
4634 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004635
Gabor Mezei15b95a62022-05-09 16:37:58 +02004636 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004637 {
Jerry Yuf377d642022-01-25 10:43:59 +08004638 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004639 {
Jerry Yuf377d642022-01-25 10:43:59 +08004640 if( sig_algs[i] != sig_algs[j] )
4641 continue;
4642 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4643 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4644 sig_algs[i], j, i );
4645 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004646 }
4647 }
4648 return( ret );
4649}
4650
Ronald Crone68ab4f2022-10-05 12:46:29 +02004651#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004652
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004653/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004654 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004655 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004656int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004657 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004658{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004659#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004660 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004661#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004662
Ronald Crone68ab4f2022-10-05 12:46:29 +02004663#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004664 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004665 {
4666 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4667 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4668 }
4669
Jerry Yuf377d642022-01-25 10:43:59 +08004670 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004671 {
4672 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4673 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4674 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004675
4676#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004677 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004678 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004679 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004680 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4681 }
4682
Jerry Yuf377d642022-01-25 10:43:59 +08004683 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004684 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004685 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004686 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4687 }
4688#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004689#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004690
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004691 /* Use the functions here so that they are covered in tests,
4692 * but otherwise access member directly for efficiency */
4693 mbedtls_ssl_conf_endpoint( conf, endpoint );
4694 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004695
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004696 /*
4697 * Things that are common to all presets
4698 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004699#if defined(MBEDTLS_SSL_CLI_C)
4700 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4701 {
4702 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4703#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4704 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4705#endif
4706 }
4707#endif
4708
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004709#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4710 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4711#endif
4712
4713#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4714 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4715#endif
4716
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004717#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004718 conf->f_cookie_write = ssl_cookie_write_dummy;
4719 conf->f_cookie_check = ssl_cookie_check_dummy;
4720#endif
4721
4722#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4723 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4724#endif
4725
Janos Follath088ce432017-04-10 12:42:31 +01004726#if defined(MBEDTLS_SSL_SRV_C)
4727 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004728 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004729#endif
4730
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004731#if defined(MBEDTLS_SSL_PROTO_DTLS)
4732 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4733 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4734#endif
4735
4736#if defined(MBEDTLS_SSL_RENEGOTIATION)
4737 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004738 memset( conf->renego_period, 0x00, 2 );
4739 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004740#endif
4741
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004742#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004743 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4744 {
4745 const unsigned char dhm_p[] =
4746 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4747 const unsigned char dhm_g[] =
4748 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004749
Hanno Beckere2defad2021-07-24 05:59:17 +01004750 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4751 dhm_p, sizeof( dhm_p ),
4752 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4753 {
4754 return( ret );
4755 }
4756 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004757#endif
4758
Ronald Cron6f135e12021-12-08 16:57:54 +01004759#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004760#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004761 mbedtls_ssl_conf_new_session_tickets(
4762 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4763#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004764 /*
4765 * Allow all TLS 1.3 key exchange modes by default.
4766 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004767 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004768#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004769
XiaokangQian4d3a6042022-04-21 13:46:17 +00004770 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4771 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004772#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004773 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004774 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004775#else
XiaokangQian060d8672022-04-21 09:24:56 +00004776 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004777#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004778 }
4779 else
4780 {
4781#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4782 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4783 {
4784 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4785 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4786 }
4787 else
4788 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4789 {
4790 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4791 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4792 }
4793#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4794 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4795 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4796#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4797 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4798 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4799#else
4800 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4801#endif
4802 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004803
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004804 /*
4805 * Preset-specific defaults
4806 */
4807 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004808 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004809 /*
4810 * NSA Suite B
4811 */
4812 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004813
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004814 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004815
4816#if defined(MBEDTLS_X509_CRT_PARSE_C)
4817 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004818#endif
4819
Ronald Crone68ab4f2022-10-05 12:46:29 +02004820#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004821#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4822 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4823 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4824 else
4825#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4826 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02004827#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004828
Brett Warrene0edc842021-08-17 09:53:13 +01004829#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4830 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004831#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004832 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004833 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004834
4835 /*
4836 * Default
4837 */
4838 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004839
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004840 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004841
4842#if defined(MBEDTLS_X509_CRT_PARSE_C)
4843 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4844#endif
4845
Ronald Crone68ab4f2022-10-05 12:46:29 +02004846#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004847#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4848 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4849 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4850 else
4851#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4852 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02004853#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004854
Brett Warrene0edc842021-08-17 09:53:13 +01004855#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4856 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004857#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004858 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004859
4860#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4861 conf->dhm_min_bitlen = 1024;
4862#endif
4863 }
4864
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004865 return( 0 );
4866}
4867
4868/*
4869 * Free mbedtls_ssl_config
4870 */
4871void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4872{
4873#if defined(MBEDTLS_DHM_C)
4874 mbedtls_mpi_free( &conf->dhm_P );
4875 mbedtls_mpi_free( &conf->dhm_G );
4876#endif
4877
Ronald Cron73fe8df2022-10-05 14:31:43 +02004878#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004879#if defined(MBEDTLS_USE_PSA_CRYPTO)
4880 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4881 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004882 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4883 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004884#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004885 if( conf->psk != NULL )
4886 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004887 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004888 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004889 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004890 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004891 }
4892
4893 if( conf->psk_identity != NULL )
4894 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004895 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004896 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004897 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004898 conf->psk_identity_len = 0;
4899 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02004900#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004901
4902#if defined(MBEDTLS_X509_CRT_PARSE_C)
4903 ssl_key_cert_free( conf->key_cert );
4904#endif
4905
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004906 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004907}
4908
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004909#if defined(MBEDTLS_PK_C) && \
4910 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004911/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004912 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004913 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004914unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004915{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004916#if defined(MBEDTLS_RSA_C)
4917 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4918 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004919#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004920#if defined(MBEDTLS_ECDSA_C)
4921 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4922 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004923#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004924 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004925}
4926
Hanno Becker7e5437a2017-04-28 17:15:26 +01004927unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4928{
4929 switch( type ) {
4930 case MBEDTLS_PK_RSA:
4931 return( MBEDTLS_SSL_SIG_RSA );
4932 case MBEDTLS_PK_ECDSA:
4933 case MBEDTLS_PK_ECKEY:
4934 return( MBEDTLS_SSL_SIG_ECDSA );
4935 default:
4936 return( MBEDTLS_SSL_SIG_ANON );
4937 }
4938}
4939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004940mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004941{
4942 switch( sig )
4943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004944#if defined(MBEDTLS_RSA_C)
4945 case MBEDTLS_SSL_SIG_RSA:
4946 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004947#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004948#if defined(MBEDTLS_ECDSA_C)
4949 case MBEDTLS_SSL_SIG_ECDSA:
4950 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004951#endif
4952 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004953 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004954 }
4955}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004956#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004957
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004958/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004959 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004960 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004961mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004962{
4963 switch( hash )
4964 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004965#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004966 case MBEDTLS_SSL_HASH_MD5:
4967 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004968#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004969#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004970 case MBEDTLS_SSL_HASH_SHA1:
4971 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004972#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004973#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004974 case MBEDTLS_SSL_HASH_SHA224:
4975 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004976#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004977#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004978 case MBEDTLS_SSL_HASH_SHA256:
4979 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004980#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004981#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004982 case MBEDTLS_SSL_HASH_SHA384:
4983 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004984#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004985#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004986 case MBEDTLS_SSL_HASH_SHA512:
4987 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004988#endif
4989 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004990 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004991 }
4992}
4993
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004994/*
4995 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4996 */
4997unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4998{
4999 switch( md )
5000 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005001#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005002 case MBEDTLS_MD_MD5:
5003 return( MBEDTLS_SSL_HASH_MD5 );
5004#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005005#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005006 case MBEDTLS_MD_SHA1:
5007 return( MBEDTLS_SSL_HASH_SHA1 );
5008#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005009#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005010 case MBEDTLS_MD_SHA224:
5011 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005012#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005013#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005014 case MBEDTLS_MD_SHA256:
5015 return( MBEDTLS_SSL_HASH_SHA256 );
5016#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005017#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005018 case MBEDTLS_MD_SHA384:
5019 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005020#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005021#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005022 case MBEDTLS_MD_SHA512:
5023 return( MBEDTLS_SSL_HASH_SHA512 );
5024#endif
5025 default:
5026 return( MBEDTLS_SSL_HASH_NONE );
5027 }
5028}
5029
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005030/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005031 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005032 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005033 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005034int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005035{
Brett Warrene0edc842021-08-17 09:53:13 +01005036 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005037
Brett Warrene0edc842021-08-17 09:53:13 +01005038 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005039 return( -1 );
5040
Brett Warrene0edc842021-08-17 09:53:13 +01005041 for( ; *group_list != 0; group_list++ )
5042 {
5043 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005044 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005045 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005046
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005047 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005048}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005049
5050#if defined(MBEDTLS_ECP_C)
5051/*
5052 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5053 */
5054int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5055{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005056 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005057 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005058
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005059 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005060 return -1;
5061
5062 uint16_t tls_id = grp_info->tls_id;
5063
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005064 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5065}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005066#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005068#if defined(MBEDTLS_X509_CRT_PARSE_C)
5069int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5070 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005071 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005072 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005073{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005074 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005075 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005076 const char *ext_oid;
5077 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005079 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005080 {
5081 /* Server part of the key exchange */
5082 switch( ciphersuite->key_exchange )
5083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005084 case MBEDTLS_KEY_EXCHANGE_RSA:
5085 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005086 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005087 break;
5088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005089 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5090 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5091 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5092 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005093 break;
5094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005095 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5096 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005097 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005098 break;
5099
5100 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005101 case MBEDTLS_KEY_EXCHANGE_NONE:
5102 case MBEDTLS_KEY_EXCHANGE_PSK:
5103 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5104 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005105 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005106 usage = 0;
5107 }
5108 }
5109 else
5110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005111 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5112 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005113 }
5114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005115 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 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_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005118 ret = -1;
5119 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005121 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005123 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5124 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005125 }
5126 else
5127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005128 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5129 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005130 }
5131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005132 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005133 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005134 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005135 ret = -1;
5136 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005137
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005138 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005139}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005140#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005141
Jerry Yu148165c2021-09-24 23:20:59 +08005142#if defined(MBEDTLS_USE_PSA_CRYPTO)
5143int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5144 const mbedtls_md_type_t md,
5145 unsigned char *dst,
5146 size_t dst_len,
5147 size_t *olen )
5148{
Ronald Cronf6893e12022-01-07 22:09:01 +01005149 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5150 psa_hash_operation_t *hash_operation_to_clone;
5151 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5152
Jerry Yu148165c2021-09-24 23:20:59 +08005153 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005154
5155 switch( md )
5156 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005157#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005158 case MBEDTLS_MD_SHA384:
5159 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5160 break;
5161#endif
5162
Andrzej Kurek25f27152022-08-17 16:09:31 -04005163#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005164 case MBEDTLS_MD_SHA256:
5165 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5166 break;
5167#endif
5168
5169 default:
5170 goto exit;
5171 }
5172
5173 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5174 if( status != PSA_SUCCESS )
5175 goto exit;
5176
5177 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5178 if( status != PSA_SUCCESS )
5179 goto exit;
5180
5181exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005182#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5183 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5184 (void) ssl;
5185#endif
Ronald Cronb788c042022-02-15 09:14:15 +01005186 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005187}
5188#else /* MBEDTLS_USE_PSA_CRYPTO */
5189
Andrzej Kurek25f27152022-08-17 16:09:31 -04005190#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005191MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005192static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5193 unsigned char *dst,
5194 size_t dst_len,
5195 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005196{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005197 int ret;
5198 mbedtls_sha512_context sha512;
5199
5200 if( dst_len < 48 )
5201 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5202
5203 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005204 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005205
5206 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5207 {
5208 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5209 goto exit;
5210 }
5211
5212 *olen = 48;
5213
5214exit:
5215
5216 mbedtls_sha512_free( &sha512 );
5217 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005218}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005219#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005220
Andrzej Kurek25f27152022-08-17 16:09:31 -04005221#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005222MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005223static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5224 unsigned char *dst,
5225 size_t dst_len,
5226 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005227{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005228 int ret;
5229 mbedtls_sha256_context sha256;
5230
5231 if( dst_len < 32 )
5232 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5233
5234 mbedtls_sha256_init( &sha256 );
5235 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005236
Jerry Yu24c0ec32021-09-09 14:21:07 +08005237 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5238 {
5239 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5240 goto exit;
5241 }
5242
5243 *olen = 32;
5244
5245exit:
5246
5247 mbedtls_sha256_free( &sha256 );
5248 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005249}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005250#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005251
Jerry Yu000f9762021-09-14 11:12:51 +08005252int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5253 const mbedtls_md_type_t md,
5254 unsigned char *dst,
5255 size_t dst_len,
5256 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005257{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005258 switch( md )
5259 {
5260
Andrzej Kurek25f27152022-08-17 16:09:31 -04005261#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005262 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005263 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005264#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005265
Andrzej Kurek25f27152022-08-17 16:09:31 -04005266#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005267 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005268 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005269#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005270
5271 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005272#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5273 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5274 (void) ssl;
5275 (void) dst;
5276 (void) dst_len;
5277 (void) olen;
5278#endif
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005279 break;
5280 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005281 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005282}
XiaokangQian647719a2021-12-07 09:16:29 +00005283
Jerry Yu148165c2021-09-24 23:20:59 +08005284#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005285
Ronald Crone68ab4f2022-10-05 12:46:29 +02005286#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005287/* mbedtls_ssl_parse_sig_alg_ext()
5288 *
5289 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5290 * value (TLS 1.3 RFC8446):
5291 * enum {
5292 * ....
5293 * ecdsa_secp256r1_sha256( 0x0403 ),
5294 * ecdsa_secp384r1_sha384( 0x0503 ),
5295 * ecdsa_secp521r1_sha512( 0x0603 ),
5296 * ....
5297 * } SignatureScheme;
5298 *
5299 * struct {
5300 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5301 * } SignatureSchemeList;
5302 *
5303 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5304 * value (TLS 1.2 RFC5246):
5305 * enum {
5306 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5307 * sha512(6), (255)
5308 * } HashAlgorithm;
5309 *
5310 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5311 * SignatureAlgorithm;
5312 *
5313 * struct {
5314 * HashAlgorithm hash;
5315 * SignatureAlgorithm signature;
5316 * } SignatureAndHashAlgorithm;
5317 *
5318 * SignatureAndHashAlgorithm
5319 * supported_signature_algorithms<2..2^16-2>;
5320 *
5321 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5322 * generalization of the TLS 1.2 signature algorithm extension.
5323 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5324 * `SignatureScheme` field of TLS 1.3
5325 *
5326 */
5327int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5328 const unsigned char *buf,
5329 const unsigned char *end )
5330{
5331 const unsigned char *p = buf;
5332 size_t supported_sig_algs_len = 0;
5333 const unsigned char *supported_sig_algs_end;
5334 uint16_t sig_alg;
5335 uint32_t common_idx = 0;
5336
5337 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5338 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5339 p += 2;
5340
5341 memset( ssl->handshake->received_sig_algs, 0,
5342 sizeof(ssl->handshake->received_sig_algs) );
5343
5344 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5345 supported_sig_algs_end = p + supported_sig_algs_len;
5346 while( p < supported_sig_algs_end )
5347 {
5348 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5349 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5350 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005351 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5352 sig_alg,
5353 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005354#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005355 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005356 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5357 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5358 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005359 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005360 }
5361#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005362
Jerry Yuf3b46b52022-06-19 16:52:27 +08005363 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5364 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005365
5366 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5367 {
5368 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5369 common_idx += 1;
5370 }
5371 }
5372 /* Check that we consumed all the message. */
5373 if( p != end )
5374 {
5375 MBEDTLS_SSL_DEBUG_MSG( 1,
5376 ( "Signature algorithms extension length misaligned" ) );
5377 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5378 MBEDTLS_ERR_SSL_DECODE_ERROR );
5379 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5380 }
5381
5382 if( common_idx == 0 )
5383 {
5384 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5385 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5386 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5387 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5388 }
5389
Gabor Mezei15b95a62022-05-09 16:37:58 +02005390 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005391 return( 0 );
5392}
5393
Ronald Crone68ab4f2022-10-05 12:46:29 +02005394#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005395
Jerry Yudc7bd172022-02-17 13:44:15 +08005396#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5397
5398#if defined(MBEDTLS_USE_PSA_CRYPTO)
5399
5400static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5401 mbedtls_svc_key_id_t key,
5402 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005403 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005404 const unsigned char* seed, size_t seed_length,
5405 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005406 const unsigned char* other_secret,
5407 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005408 size_t capacity )
5409{
5410 psa_status_t status;
5411
5412 status = psa_key_derivation_setup( derivation, alg );
5413 if( status != PSA_SUCCESS )
5414 return( status );
5415
5416 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5417 {
5418 status = psa_key_derivation_input_bytes( derivation,
5419 PSA_KEY_DERIVATION_INPUT_SEED,
5420 seed, seed_length );
5421 if( status != PSA_SUCCESS )
5422 return( status );
5423
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005424 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005425 {
5426 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005427 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5428 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005429 if( status != PSA_SUCCESS )
5430 return( status );
5431 }
5432
Jerry Yudc7bd172022-02-17 13:44:15 +08005433 if( mbedtls_svc_key_id_is_null( key ) )
5434 {
5435 status = psa_key_derivation_input_bytes(
5436 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005437 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005438 }
5439 else
5440 {
5441 status = psa_key_derivation_input_key(
5442 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5443 }
5444 if( status != PSA_SUCCESS )
5445 return( status );
5446
5447 status = psa_key_derivation_input_bytes( derivation,
5448 PSA_KEY_DERIVATION_INPUT_LABEL,
5449 label, label_length );
5450 if( status != PSA_SUCCESS )
5451 return( status );
5452 }
5453 else
5454 {
5455 return( PSA_ERROR_NOT_SUPPORTED );
5456 }
5457
5458 status = psa_key_derivation_set_capacity( derivation, capacity );
5459 if( status != PSA_SUCCESS )
5460 return( status );
5461
5462 return( PSA_SUCCESS );
5463}
5464
Andrzej Kurek57d10632022-10-24 10:32:01 -04005465#if defined(PSA_WANT_ALG_SHA_384) || \
5466 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005467MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005468static int tls_prf_generic( mbedtls_md_type_t md_type,
5469 const unsigned char *secret, size_t slen,
5470 const char *label,
5471 const unsigned char *random, size_t rlen,
5472 unsigned char *dstbuf, size_t dlen )
5473{
5474 psa_status_t status;
5475 psa_algorithm_t alg;
5476 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5477 psa_key_derivation_operation_t derivation =
5478 PSA_KEY_DERIVATION_OPERATION_INIT;
5479
5480 if( md_type == MBEDTLS_MD_SHA384 )
5481 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5482 else
5483 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5484
5485 /* Normally a "secret" should be long enough to be impossible to
5486 * find by brute force, and in particular should not be empty. But
5487 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5488 * and for this use case it makes sense to have a 0-length "secret".
5489 * Since the key API doesn't allow importing a key of length 0,
5490 * keep master_key=0, which setup_psa_key_derivation() understands
5491 * to mean a 0-length "secret" input. */
5492 if( slen != 0 )
5493 {
5494 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5495 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5496 psa_set_key_algorithm( &key_attributes, alg );
5497 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5498
5499 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5500 if( status != PSA_SUCCESS )
5501 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5502 }
5503
5504 status = setup_psa_key_derivation( &derivation,
5505 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005506 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005507 random, rlen,
5508 (unsigned char const *) label,
5509 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005510 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005511 dlen );
5512 if( status != PSA_SUCCESS )
5513 {
5514 psa_key_derivation_abort( &derivation );
5515 psa_destroy_key( master_key );
5516 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5517 }
5518
5519 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5520 if( status != PSA_SUCCESS )
5521 {
5522 psa_key_derivation_abort( &derivation );
5523 psa_destroy_key( master_key );
5524 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5525 }
5526
5527 status = psa_key_derivation_abort( &derivation );
5528 if( status != PSA_SUCCESS )
5529 {
5530 psa_destroy_key( master_key );
5531 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5532 }
5533
5534 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5535 status = psa_destroy_key( master_key );
5536 if( status != PSA_SUCCESS )
5537 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5538
5539 return( 0 );
5540}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005541#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08005542#else /* MBEDTLS_USE_PSA_CRYPTO */
5543
Andrzej Kurek57d10632022-10-24 10:32:01 -04005544#if defined(MBEDTLS_MD_C) && \
5545 ( defined(MBEDTLS_SHA256_C) || \
5546 defined(MBEDTLS_SHA384_C) )
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005547MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005548static int tls_prf_generic( mbedtls_md_type_t md_type,
5549 const unsigned char *secret, size_t slen,
5550 const char *label,
5551 const unsigned char *random, size_t rlen,
5552 unsigned char *dstbuf, size_t dlen )
5553{
5554 size_t nb;
5555 size_t i, j, k, md_len;
5556 unsigned char *tmp;
5557 size_t tmp_len = 0;
5558 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5559 const mbedtls_md_info_t *md_info;
5560 mbedtls_md_context_t md_ctx;
5561 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5562
5563 mbedtls_md_init( &md_ctx );
5564
5565 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5566 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5567
5568 md_len = mbedtls_md_get_size( md_info );
5569
5570 tmp_len = md_len + strlen( label ) + rlen;
5571 tmp = mbedtls_calloc( 1, tmp_len );
5572 if( tmp == NULL )
5573 {
5574 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5575 goto exit;
5576 }
5577
5578 nb = strlen( label );
5579 memcpy( tmp + md_len, label, nb );
5580 memcpy( tmp + md_len + nb, random, rlen );
5581 nb += rlen;
5582
5583 /*
5584 * Compute P_<hash>(secret, label + random)[0..dlen]
5585 */
5586 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5587 goto exit;
5588
5589 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5590 if( ret != 0 )
5591 goto exit;
5592 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5593 if( ret != 0 )
5594 goto exit;
5595 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5596 if( ret != 0 )
5597 goto exit;
5598
5599 for( i = 0; i < dlen; i += md_len )
5600 {
5601 ret = mbedtls_md_hmac_reset ( &md_ctx );
5602 if( ret != 0 )
5603 goto exit;
5604 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5605 if( ret != 0 )
5606 goto exit;
5607 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5608 if( ret != 0 )
5609 goto exit;
5610
5611 ret = mbedtls_md_hmac_reset ( &md_ctx );
5612 if( ret != 0 )
5613 goto exit;
5614 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5615 if( ret != 0 )
5616 goto exit;
5617 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5618 if( ret != 0 )
5619 goto exit;
5620
5621 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5622
5623 for( j = 0; j < k; j++ )
5624 dstbuf[i + j] = h_i[j];
5625 }
5626
5627exit:
5628 mbedtls_md_free( &md_ctx );
5629
5630 mbedtls_platform_zeroize( tmp, tmp_len );
5631 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5632
5633 mbedtls_free( tmp );
5634
5635 return( ret );
5636}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005637#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08005638#endif /* MBEDTLS_USE_PSA_CRYPTO */
5639
Andrzej Kurek25f27152022-08-17 16:09:31 -04005640#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005641MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005642static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5643 const char *label,
5644 const unsigned char *random, size_t rlen,
5645 unsigned char *dstbuf, size_t dlen )
5646{
5647 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5648 label, random, rlen, dstbuf, dlen ) );
5649}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005650#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005651
Andrzej Kurek25f27152022-08-17 16:09:31 -04005652#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005653MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005654static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5655 const char *label,
5656 const unsigned char *random, size_t rlen,
5657 unsigned char *dstbuf, size_t dlen )
5658{
5659 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5660 label, random, rlen, dstbuf, dlen ) );
5661}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005662#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005663
Jerry Yuf009d862022-02-17 14:01:37 +08005664/*
5665 * Set appropriate PRF function and other SSL / TLS1.2 functions
5666 *
5667 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005668 * - hash associated with the ciphersuite (only used by TLS 1.2)
5669 *
5670 * Outputs:
5671 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5672 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005673MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005674static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005675 mbedtls_md_type_t hash )
5676{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005677#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005678 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005679 {
5680 handshake->tls_prf = tls_prf_sha384;
5681 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5682 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5683 }
5684 else
5685#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005686#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005687 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005688 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005689 handshake->tls_prf = tls_prf_sha256;
5690 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5691 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5692 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005693#else
Jerry Yuf009d862022-02-17 14:01:37 +08005694 {
Jerry Yuf009d862022-02-17 14:01:37 +08005695 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005696 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005697 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5698 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005699#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005700
5701 return( 0 );
5702}
Jerry Yud6ab2352022-02-17 14:03:43 +08005703
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005704/*
5705 * Compute master secret if needed
5706 *
5707 * Parameters:
5708 * [in/out] handshake
5709 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5710 * (PSA-PSK) ciphersuite_info, psk_opaque
5711 * [out] premaster (cleared)
5712 * [out] master
5713 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5714 * debug: conf->f_dbg, conf->p_dbg
5715 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005716 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005717 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005718MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005719static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5720 unsigned char *master,
5721 const mbedtls_ssl_context *ssl )
5722{
5723 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5724
5725 /* cf. RFC 5246, Section 8.1:
5726 * "The master secret is always exactly 48 bytes in length." */
5727 size_t const master_secret_len = 48;
5728
5729#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5730 unsigned char session_hash[48];
5731#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5732
5733 /* The label for the KDF used for key expansion.
5734 * This is either "master secret" or "extended master secret"
5735 * depending on whether the Extended Master Secret extension
5736 * is used. */
5737 char const *lbl = "master secret";
5738
Przemek Stekielae4ed302022-04-05 17:15:55 +02005739 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005740 * - If the Extended Master Secret extension is not used,
5741 * this is ClientHello.Random + ServerHello.Random
5742 * (see Sect. 8.1 in RFC 5246).
5743 * - If the Extended Master Secret extension is used,
5744 * this is the transcript of the handshake so far.
5745 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005746 unsigned char const *seed = handshake->randbytes;
5747 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005748
5749#if !defined(MBEDTLS_DEBUG_C) && \
5750 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5751 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5752 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5753 ssl = NULL; /* make sure we don't use it except for those cases */
5754 (void) ssl;
5755#endif
5756
5757 if( handshake->resume != 0 )
5758 {
5759 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5760 return( 0 );
5761 }
5762
5763#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5764 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5765 {
5766 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005767 seed = session_hash;
5768 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005769
5770 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005771 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005772 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005773#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005774
Przemek Stekiel99114f32022-04-22 11:20:09 +02005775#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005776 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005777 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005778 {
5779 /* Perform PSK-to-MS expansion in a single step. */
5780 psa_status_t status;
5781 psa_algorithm_t alg;
5782 mbedtls_svc_key_id_t psk;
5783 psa_key_derivation_operation_t derivation =
5784 PSA_KEY_DERIVATION_OPERATION_INIT;
5785 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5786
5787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5788
5789 psk = mbedtls_ssl_get_opaque_psk( ssl );
5790
5791 if( hash_alg == MBEDTLS_MD_SHA384 )
5792 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5793 else
5794 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5795
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005796 size_t other_secret_len = 0;
5797 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005798
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005799 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005800 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005801 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005802 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005803 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005804 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005805 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005806 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005807 other_secret_len = 48;
5808 other_secret = handshake->premaster + 2;
5809 break;
5810 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005811 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005812 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5813 other_secret = handshake->premaster + 2;
5814 break;
5815 default:
5816 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005817 }
5818
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005819 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005820 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005821 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005822 (unsigned char const *) lbl,
5823 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005824 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005825 master_secret_len );
5826 if( status != PSA_SUCCESS )
5827 {
5828 psa_key_derivation_abort( &derivation );
5829 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5830 }
5831
5832 status = psa_key_derivation_output_bytes( &derivation,
5833 master,
5834 master_secret_len );
5835 if( status != PSA_SUCCESS )
5836 {
5837 psa_key_derivation_abort( &derivation );
5838 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5839 }
5840
5841 status = psa_key_derivation_abort( &derivation );
5842 if( status != PSA_SUCCESS )
5843 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5844 }
5845 else
5846#endif
5847 {
5848 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005849 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005850 master,
5851 master_secret_len );
5852 if( ret != 0 )
5853 {
5854 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5855 return( ret );
5856 }
5857
5858 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5859 handshake->premaster,
5860 handshake->pmslen );
5861
5862 mbedtls_platform_zeroize( handshake->premaster,
5863 sizeof(handshake->premaster) );
5864 }
5865
5866 return( 0 );
5867}
5868
Jerry Yud62f87e2022-02-17 14:09:02 +08005869int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5870{
5871 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5872 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5873 ssl->handshake->ciphersuite_info;
5874
5875 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5876
5877 /* Set PRF, calc_verify and calc_finished function pointers */
5878 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005879 ciphersuite_info->mac );
5880 if( ret != 0 )
5881 {
5882 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5883 return( ret );
5884 }
5885
5886 /* Compute master secret if needed */
5887 ret = ssl_compute_master( ssl->handshake,
5888 ssl->session_negotiate->master,
5889 ssl );
5890 if( ret != 0 )
5891 {
5892 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5893 return( ret );
5894 }
5895
5896 /* Swap the client and server random values:
5897 * - MS derivation wanted client+server (RFC 5246 8.1)
5898 * - key derivation wants server+client (RFC 5246 6.3) */
5899 {
5900 unsigned char tmp[64];
5901 memcpy( tmp, ssl->handshake->randbytes, 64 );
5902 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5903 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5904 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5905 }
5906
5907 /* Populate transform structure */
5908 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5909 ssl->session_negotiate->ciphersuite,
5910 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005911#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005912 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005913#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005914 ssl->handshake->tls_prf,
5915 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005916 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005917 ssl->conf->endpoint,
5918 ssl );
5919 if( ret != 0 )
5920 {
5921 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5922 return( ret );
5923 }
5924
5925 /* We no longer need Server/ClientHello.random values */
5926 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5927 sizeof( ssl->handshake->randbytes ) );
5928
5929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5930
5931 return( 0 );
5932}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005933
Ronald Cron4dcbca92022-03-07 10:21:40 +01005934int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5935{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005936 switch( md )
5937 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005938#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005939 case MBEDTLS_SSL_HASH_SHA384:
5940 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5941 break;
5942#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005943#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005944 case MBEDTLS_SSL_HASH_SHA256:
5945 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5946 break;
5947#endif
5948 default:
5949 return( -1 );
5950 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005951#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5952 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5953 (void) ssl;
5954#endif
Ronald Cronc2f13a02022-03-07 10:25:24 +01005955 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005956}
5957
Andrzej Kurek25f27152022-08-17 16:09:31 -04005958#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005959void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5960 unsigned char *hash,
5961 size_t *hlen )
5962{
5963#if defined(MBEDTLS_USE_PSA_CRYPTO)
5964 size_t hash_size;
5965 psa_status_t status;
5966 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5967
5968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5969 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5970 if( status != PSA_SUCCESS )
5971 {
5972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5973 return;
5974 }
5975
5976 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5977 if( status != PSA_SUCCESS )
5978 {
5979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5980 return;
5981 }
5982
5983 *hlen = 32;
5984 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5986#else
5987 mbedtls_sha256_context sha256;
5988
5989 mbedtls_sha256_init( &sha256 );
5990
5991 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5992
5993 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5994 mbedtls_sha256_finish( &sha256, hash );
5995
5996 *hlen = 32;
5997
5998 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6000
6001 mbedtls_sha256_free( &sha256 );
6002#endif /* MBEDTLS_USE_PSA_CRYPTO */
6003 return;
6004}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006005#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006006
Andrzej Kurek25f27152022-08-17 16:09:31 -04006007#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006008void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
6009 unsigned char *hash,
6010 size_t *hlen )
6011{
6012#if defined(MBEDTLS_USE_PSA_CRYPTO)
6013 size_t hash_size;
6014 psa_status_t status;
6015 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6016
6017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6018 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6019 if( status != PSA_SUCCESS )
6020 {
6021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6022 return;
6023 }
6024
6025 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6026 if( status != PSA_SUCCESS )
6027 {
6028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6029 return;
6030 }
6031
6032 *hlen = 48;
6033 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6035#else
6036 mbedtls_sha512_context sha512;
6037
6038 mbedtls_sha512_init( &sha512 );
6039
6040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6041
Andrzej Kureka242e832022-08-11 10:03:14 -04006042 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006043 mbedtls_sha512_finish( &sha512, hash );
6044
6045 *hlen = 48;
6046
6047 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6049
6050 mbedtls_sha512_free( &sha512 );
6051#endif /* MBEDTLS_USE_PSA_CRYPTO */
6052 return;
6053}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006054#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006055
Neil Armstrong80f6f322022-05-03 17:56:38 +02006056#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6057 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006058int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6059{
6060 unsigned char *p = ssl->handshake->premaster;
6061 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6062 const unsigned char *psk = NULL;
6063 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006064 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006065
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006066 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006067 {
6068 /*
6069 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006070 * checked before calling this function.
6071 *
6072 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006073 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006074 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006075 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6076 {
6077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6078 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6079 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006080 }
6081
6082 /*
6083 * PMS = struct {
6084 * opaque other_secret<0..2^16-1>;
6085 * opaque psk<0..2^16-1>;
6086 * };
6087 * with "other_secret" depending on the particular key exchange
6088 */
6089#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6090 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6091 {
6092 if( end - p < 2 )
6093 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6094
6095 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6096 p += 2;
6097
6098 if( end < p || (size_t)( end - p ) < psk_len )
6099 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6100
6101 memset( p, 0, psk_len );
6102 p += psk_len;
6103 }
6104 else
6105#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6106#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6107 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6108 {
6109 /*
6110 * other_secret already set by the ClientKeyExchange message,
6111 * and is 48 bytes long
6112 */
6113 if( end - p < 2 )
6114 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6115
6116 *p++ = 0;
6117 *p++ = 48;
6118 p += 48;
6119 }
6120 else
6121#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6122#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6123 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6124 {
6125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6126 size_t len;
6127
6128 /* Write length only when we know the actual value */
6129 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6130 p + 2, end - ( p + 2 ), &len,
6131 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6132 {
6133 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6134 return( ret );
6135 }
6136 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6137 p += 2 + len;
6138
6139 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6140 }
6141 else
6142#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006143#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006144 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6145 {
6146 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6147 size_t zlen;
6148
6149 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6150 p + 2, end - ( p + 2 ),
6151 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6152 {
6153 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6154 return( ret );
6155 }
6156
6157 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6158 p += 2 + zlen;
6159
6160 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6161 MBEDTLS_DEBUG_ECDH_Z );
6162 }
6163 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006164#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006165 {
6166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6167 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6168 }
6169
6170 /* opaque psk<0..2^16-1>; */
6171 if( end - p < 2 )
6172 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6173
6174 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6175 p += 2;
6176
6177 if( end < p || (size_t)( end - p ) < psk_len )
6178 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6179
6180 memcpy( p, psk, psk_len );
6181 p += psk_len;
6182
6183 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6184
6185 return( 0 );
6186}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006187#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006188
6189#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006190MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006191static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6192
6193#if defined(MBEDTLS_SSL_PROTO_DTLS)
6194int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6195{
6196 /* If renegotiation is not enforced, retransmit until we would reach max
6197 * timeout if we were using the usual handshake doubling scheme */
6198 if( ssl->conf->renego_max_records < 0 )
6199 {
6200 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6201 unsigned char doublings = 1;
6202
6203 while( ratio != 0 )
6204 {
6205 ++doublings;
6206 ratio >>= 1;
6207 }
6208
6209 if( ++ssl->renego_records_seen > doublings )
6210 {
6211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6212 return( 0 );
6213 }
6214 }
6215
6216 return( ssl_write_hello_request( ssl ) );
6217}
6218#endif
6219#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006220
Jerry Yud9526692022-02-17 14:23:47 +08006221/*
6222 * Handshake functions
6223 */
6224#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6225/* No certificate support -> dummy functions */
6226int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6227{
6228 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6229 ssl->handshake->ciphersuite_info;
6230
6231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6232
6233 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6234 {
6235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6236 ssl->state++;
6237 return( 0 );
6238 }
6239
6240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6241 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6242}
6243
6244int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6245{
6246 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6247 ssl->handshake->ciphersuite_info;
6248
6249 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6250
6251 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6252 {
6253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6254 ssl->state++;
6255 return( 0 );
6256 }
6257
6258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6259 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6260}
6261
6262#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6263/* Some certificate support -> implement write and parse */
6264
6265int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6266{
6267 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6268 size_t i, n;
6269 const mbedtls_x509_crt *crt;
6270 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6271 ssl->handshake->ciphersuite_info;
6272
6273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6274
6275 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6276 {
6277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6278 ssl->state++;
6279 return( 0 );
6280 }
6281
6282#if defined(MBEDTLS_SSL_CLI_C)
6283 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6284 {
6285 if( ssl->handshake->client_auth == 0 )
6286 {
6287 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6288 ssl->state++;
6289 return( 0 );
6290 }
6291 }
6292#endif /* MBEDTLS_SSL_CLI_C */
6293#if defined(MBEDTLS_SSL_SRV_C)
6294 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6295 {
6296 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6297 {
6298 /* Should never happen because we shouldn't have picked the
6299 * ciphersuite if we don't have a certificate. */
6300 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6301 }
6302 }
6303#endif
6304
6305 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6306
6307 /*
6308 * 0 . 0 handshake type
6309 * 1 . 3 handshake length
6310 * 4 . 6 length of all certs
6311 * 7 . 9 length of cert. 1
6312 * 10 . n-1 peer certificate
6313 * n . n+2 length of cert. 2
6314 * n+3 . ... upper level cert, etc.
6315 */
6316 i = 7;
6317 crt = mbedtls_ssl_own_cert( ssl );
6318
6319 while( crt != NULL )
6320 {
6321 n = crt->raw.len;
6322 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6323 {
6324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6325 " > %" MBEDTLS_PRINTF_SIZET,
6326 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6327 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6328 }
6329
6330 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6331 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6332 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6333
6334 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6335 i += n; crt = crt->next;
6336 }
6337
6338 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6339 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6340 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6341
6342 ssl->out_msglen = i;
6343 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6344 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6345
6346 ssl->state++;
6347
6348 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6349 {
6350 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6351 return( ret );
6352 }
6353
6354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6355
6356 return( ret );
6357}
6358
6359#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6360
6361#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006362MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006363static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6364 unsigned char *crt_buf,
6365 size_t crt_buf_len )
6366{
6367 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6368
6369 if( peer_crt == NULL )
6370 return( -1 );
6371
6372 if( peer_crt->raw.len != crt_buf_len )
6373 return( -1 );
6374
6375 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6376}
6377#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006378MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006379static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6380 unsigned char *crt_buf,
6381 size_t crt_buf_len )
6382{
6383 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6384 unsigned char const * const peer_cert_digest =
6385 ssl->session->peer_cert_digest;
6386 mbedtls_md_type_t const peer_cert_digest_type =
6387 ssl->session->peer_cert_digest_type;
6388 mbedtls_md_info_t const * const digest_info =
6389 mbedtls_md_info_from_type( peer_cert_digest_type );
6390 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6391 size_t digest_len;
6392
6393 if( peer_cert_digest == NULL || digest_info == NULL )
6394 return( -1 );
6395
6396 digest_len = mbedtls_md_get_size( digest_info );
6397 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6398 return( -1 );
6399
6400 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6401 if( ret != 0 )
6402 return( -1 );
6403
6404 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6405}
6406#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6407#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6408
6409/*
6410 * Once the certificate message is read, parse it into a cert chain and
6411 * perform basic checks, but leave actual verification to the caller
6412 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006413MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006414static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6415 mbedtls_x509_crt *chain )
6416{
6417 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6418#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6419 int crt_cnt=0;
6420#endif
6421 size_t i, n;
6422 uint8_t alert;
6423
6424 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6425 {
6426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6427 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6428 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6429 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6430 }
6431
6432 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6433 {
6434 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6435 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6436 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6437 }
6438
6439 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6440 {
6441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6442 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6443 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6444 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6445 }
6446
6447 i = mbedtls_ssl_hs_hdr_len( ssl );
6448
6449 /*
6450 * Same message structure as in mbedtls_ssl_write_certificate()
6451 */
6452 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6453
6454 if( ssl->in_msg[i] != 0 ||
6455 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6456 {
6457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6458 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6459 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6460 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6461 }
6462
6463 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6464 i += 3;
6465
6466 /* Iterate through and parse the CRTs in the provided chain. */
6467 while( i < ssl->in_hslen )
6468 {
6469 /* Check that there's room for the next CRT's length fields. */
6470 if ( i + 3 > ssl->in_hslen ) {
6471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6472 mbedtls_ssl_send_alert_message( ssl,
6473 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6474 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6475 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6476 }
6477 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6478 * anything beyond 2**16 ~ 64K. */
6479 if( ssl->in_msg[i] != 0 )
6480 {
6481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6482 mbedtls_ssl_send_alert_message( ssl,
6483 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6484 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6485 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6486 }
6487
6488 /* Read length of the next CRT in the chain. */
6489 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6490 | (unsigned int) ssl->in_msg[i + 2];
6491 i += 3;
6492
6493 if( n < 128 || i + n > ssl->in_hslen )
6494 {
6495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6496 mbedtls_ssl_send_alert_message( ssl,
6497 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6498 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6499 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6500 }
6501
6502 /* Check if we're handling the first CRT in the chain. */
6503#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6504 if( crt_cnt++ == 0 &&
6505 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6506 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6507 {
6508 /* During client-side renegotiation, check that the server's
6509 * end-CRTs hasn't changed compared to the initial handshake,
6510 * mitigating the triple handshake attack. On success, reuse
6511 * the original end-CRT instead of parsing it again. */
6512 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6513 if( ssl_check_peer_crt_unchanged( ssl,
6514 &ssl->in_msg[i],
6515 n ) != 0 )
6516 {
6517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6518 mbedtls_ssl_send_alert_message( ssl,
6519 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6520 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6521 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6522 }
6523
6524 /* Now we can safely free the original chain. */
6525 ssl_clear_peer_cert( ssl->session );
6526 }
6527#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6528
6529 /* Parse the next certificate in the chain. */
6530#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6531 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6532#else
6533 /* If we don't need to store the CRT chain permanently, parse
6534 * it in-place from the input buffer instead of making a copy. */
6535 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6536#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6537 switch( ret )
6538 {
6539 case 0: /*ok*/
6540 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6541 /* Ignore certificate with an unknown algorithm: maybe a
6542 prior certificate was already trusted. */
6543 break;
6544
6545 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6546 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6547 goto crt_parse_der_failed;
6548
6549 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6550 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6551 goto crt_parse_der_failed;
6552
6553 default:
6554 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6555 crt_parse_der_failed:
6556 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6557 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6558 return( ret );
6559 }
6560
6561 i += n;
6562 }
6563
6564 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6565 return( 0 );
6566}
6567
6568#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006569MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006570static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6571{
6572 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6573 return( -1 );
6574
6575 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6576 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6577 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6578 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6579 {
Ronald Cron19385882022-06-15 16:26:13 +02006580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006581 return( 0 );
6582 }
6583 return( -1 );
6584}
6585#endif /* MBEDTLS_SSL_SRV_C */
6586
6587/* Check if a certificate message is expected.
6588 * Return either
6589 * - SSL_CERTIFICATE_EXPECTED, or
6590 * - SSL_CERTIFICATE_SKIP
6591 * indicating whether a Certificate message is expected or not.
6592 */
6593#define SSL_CERTIFICATE_EXPECTED 0
6594#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006595MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006596static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6597 int authmode )
6598{
6599 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6600 ssl->handshake->ciphersuite_info;
6601
6602 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6603 return( SSL_CERTIFICATE_SKIP );
6604
6605#if defined(MBEDTLS_SSL_SRV_C)
6606 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6607 {
6608 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6609 return( SSL_CERTIFICATE_SKIP );
6610
6611 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6612 {
6613 ssl->session_negotiate->verify_result =
6614 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6615 return( SSL_CERTIFICATE_SKIP );
6616 }
6617 }
6618#else
6619 ((void) authmode);
6620#endif /* MBEDTLS_SSL_SRV_C */
6621
6622 return( SSL_CERTIFICATE_EXPECTED );
6623}
6624
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006625MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006626static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6627 int authmode,
6628 mbedtls_x509_crt *chain,
6629 void *rs_ctx )
6630{
6631 int ret = 0;
6632 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6633 ssl->handshake->ciphersuite_info;
6634 int have_ca_chain = 0;
6635
6636 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6637 void *p_vrfy;
6638
6639 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6640 return( 0 );
6641
6642 if( ssl->f_vrfy != NULL )
6643 {
6644 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6645 f_vrfy = ssl->f_vrfy;
6646 p_vrfy = ssl->p_vrfy;
6647 }
6648 else
6649 {
6650 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6651 f_vrfy = ssl->conf->f_vrfy;
6652 p_vrfy = ssl->conf->p_vrfy;
6653 }
6654
6655 /*
6656 * Main check: verify certificate
6657 */
6658#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6659 if( ssl->conf->f_ca_cb != NULL )
6660 {
6661 ((void) rs_ctx);
6662 have_ca_chain = 1;
6663
6664 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6665 ret = mbedtls_x509_crt_verify_with_ca_cb(
6666 chain,
6667 ssl->conf->f_ca_cb,
6668 ssl->conf->p_ca_cb,
6669 ssl->conf->cert_profile,
6670 ssl->hostname,
6671 &ssl->session_negotiate->verify_result,
6672 f_vrfy, p_vrfy );
6673 }
6674 else
6675#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6676 {
6677 mbedtls_x509_crt *ca_chain;
6678 mbedtls_x509_crl *ca_crl;
6679
6680#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6681 if( ssl->handshake->sni_ca_chain != NULL )
6682 {
6683 ca_chain = ssl->handshake->sni_ca_chain;
6684 ca_crl = ssl->handshake->sni_ca_crl;
6685 }
6686 else
6687#endif
6688 {
6689 ca_chain = ssl->conf->ca_chain;
6690 ca_crl = ssl->conf->ca_crl;
6691 }
6692
6693 if( ca_chain != NULL )
6694 have_ca_chain = 1;
6695
6696 ret = mbedtls_x509_crt_verify_restartable(
6697 chain,
6698 ca_chain, ca_crl,
6699 ssl->conf->cert_profile,
6700 ssl->hostname,
6701 &ssl->session_negotiate->verify_result,
6702 f_vrfy, p_vrfy, rs_ctx );
6703 }
6704
6705 if( ret != 0 )
6706 {
6707 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6708 }
6709
6710#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6711 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6712 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6713#endif
6714
6715 /*
6716 * Secondary checks: always done, but change 'ret' only if it was 0
6717 */
6718
6719#if defined(MBEDTLS_ECP_C)
6720 {
6721 const mbedtls_pk_context *pk = &chain->pk;
6722
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006723 /* If certificate uses an EC key, make sure the curve is OK.
6724 * This is a public key, so it can't be opaque, so can_do() is a good
6725 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006726 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006727 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006728 /* and in the unlikely case the above assumption no longer holds
6729 * we are making sure that pk_ec() here does not return a NULL
6730 */
6731 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6732 if( ec == NULL )
6733 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006735 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6736 }
Jerry Yud9526692022-02-17 14:23:47 +08006737
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006738 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6739 {
6740 ssl->session_negotiate->verify_result |=
6741 MBEDTLS_X509_BADCERT_BAD_KEY;
6742
6743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6744 if( ret == 0 )
6745 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6746 }
Jerry Yud9526692022-02-17 14:23:47 +08006747 }
6748 }
6749#endif /* MBEDTLS_ECP_C */
6750
6751 if( mbedtls_ssl_check_cert_usage( chain,
6752 ciphersuite_info,
6753 ! ssl->conf->endpoint,
6754 &ssl->session_negotiate->verify_result ) != 0 )
6755 {
6756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6757 if( ret == 0 )
6758 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6759 }
6760
6761 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6762 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6763 * with details encoded in the verification flags. All other kinds
6764 * of error codes, including those from the user provided f_vrfy
6765 * functions, are treated as fatal and lead to a failure of
6766 * ssl_parse_certificate even if verification was optional. */
6767 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6768 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6769 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6770 {
6771 ret = 0;
6772 }
6773
6774 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6775 {
6776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6777 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6778 }
6779
6780 if( ret != 0 )
6781 {
6782 uint8_t alert;
6783
6784 /* The certificate may have been rejected for several reasons.
6785 Pick one and send the corresponding alert. Which alert to send
6786 may be a subject of debate in some cases. */
6787 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6788 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6789 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6790 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6791 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6792 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6793 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6794 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6795 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6796 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6797 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6798 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6799 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6800 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6801 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6802 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6803 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6804 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6805 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6806 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6807 else
6808 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6809 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6810 alert );
6811 }
6812
6813#if defined(MBEDTLS_DEBUG_C)
6814 if( ssl->session_negotiate->verify_result != 0 )
6815 {
6816 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6817 (unsigned int) ssl->session_negotiate->verify_result ) );
6818 }
6819 else
6820 {
6821 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6822 }
6823#endif /* MBEDTLS_DEBUG_C */
6824
6825 return( ret );
6826}
6827
6828#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006829MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006830static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6831 unsigned char *start, size_t len )
6832{
6833 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6834 /* Remember digest of the peer's end-CRT. */
6835 ssl->session_negotiate->peer_cert_digest =
6836 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6837 if( ssl->session_negotiate->peer_cert_digest == NULL )
6838 {
6839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6840 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6841 mbedtls_ssl_send_alert_message( ssl,
6842 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6843 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6844
6845 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6846 }
6847
6848 ret = mbedtls_md( mbedtls_md_info_from_type(
6849 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6850 start, len,
6851 ssl->session_negotiate->peer_cert_digest );
6852
6853 ssl->session_negotiate->peer_cert_digest_type =
6854 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6855 ssl->session_negotiate->peer_cert_digest_len =
6856 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6857
6858 return( ret );
6859}
6860
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006861MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006862static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6863 unsigned char *start, size_t len )
6864{
6865 unsigned char *end = start + len;
6866 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6867
6868 /* Make a copy of the peer's raw public key. */
6869 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6870 ret = mbedtls_pk_parse_subpubkey( &start, end,
6871 &ssl->handshake->peer_pubkey );
6872 if( ret != 0 )
6873 {
6874 /* We should have parsed the public key before. */
6875 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6876 }
6877
6878 return( 0 );
6879}
6880#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6881
6882int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6883{
6884 int ret = 0;
6885 int crt_expected;
6886#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6887 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6888 ? ssl->handshake->sni_authmode
6889 : ssl->conf->authmode;
6890#else
6891 const int authmode = ssl->conf->authmode;
6892#endif
6893 void *rs_ctx = NULL;
6894 mbedtls_x509_crt *chain = NULL;
6895
6896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6897
6898 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6899 if( crt_expected == SSL_CERTIFICATE_SKIP )
6900 {
6901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6902 goto exit;
6903 }
6904
6905#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6906 if( ssl->handshake->ecrs_enabled &&
6907 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6908 {
6909 chain = ssl->handshake->ecrs_peer_cert;
6910 ssl->handshake->ecrs_peer_cert = NULL;
6911 goto crt_verify;
6912 }
6913#endif
6914
6915 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6916 {
6917 /* mbedtls_ssl_read_record may have sent an alert already. We
6918 let it decide whether to alert. */
6919 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6920 goto exit;
6921 }
6922
6923#if defined(MBEDTLS_SSL_SRV_C)
6924 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6925 {
6926 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6927
6928 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6929 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6930
6931 goto exit;
6932 }
6933#endif /* MBEDTLS_SSL_SRV_C */
6934
6935 /* Clear existing peer CRT structure in case we tried to
6936 * reuse a session but it failed, and allocate a new one. */
6937 ssl_clear_peer_cert( ssl->session_negotiate );
6938
6939 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6940 if( chain == NULL )
6941 {
6942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6943 sizeof( mbedtls_x509_crt ) ) );
6944 mbedtls_ssl_send_alert_message( ssl,
6945 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6946 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6947
6948 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6949 goto exit;
6950 }
6951 mbedtls_x509_crt_init( chain );
6952
6953 ret = ssl_parse_certificate_chain( ssl, chain );
6954 if( ret != 0 )
6955 goto exit;
6956
6957#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6958 if( ssl->handshake->ecrs_enabled)
6959 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6960
6961crt_verify:
6962 if( ssl->handshake->ecrs_enabled)
6963 rs_ctx = &ssl->handshake->ecrs_ctx;
6964#endif
6965
6966 ret = ssl_parse_certificate_verify( ssl, authmode,
6967 chain, rs_ctx );
6968 if( ret != 0 )
6969 goto exit;
6970
6971#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6972 {
6973 unsigned char *crt_start, *pk_start;
6974 size_t crt_len, pk_len;
6975
6976 /* We parse the CRT chain without copying, so
6977 * these pointers point into the input buffer,
6978 * and are hence still valid after freeing the
6979 * CRT chain. */
6980
6981 crt_start = chain->raw.p;
6982 crt_len = chain->raw.len;
6983
6984 pk_start = chain->pk_raw.p;
6985 pk_len = chain->pk_raw.len;
6986
6987 /* Free the CRT structures before computing
6988 * digest and copying the peer's public key. */
6989 mbedtls_x509_crt_free( chain );
6990 mbedtls_free( chain );
6991 chain = NULL;
6992
6993 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6994 if( ret != 0 )
6995 goto exit;
6996
6997 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6998 if( ret != 0 )
6999 goto exit;
7000 }
7001#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7002 /* Pass ownership to session structure. */
7003 ssl->session_negotiate->peer_cert = chain;
7004 chain = NULL;
7005#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7006
7007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
7008
7009exit:
7010
7011 if( ret == 0 )
7012 ssl->state++;
7013
7014#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7015 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7016 {
7017 ssl->handshake->ecrs_peer_cert = chain;
7018 chain = NULL;
7019 }
7020#endif
7021
7022 if( chain != NULL )
7023 {
7024 mbedtls_x509_crt_free( chain );
7025 mbedtls_free( chain );
7026 }
7027
7028 return( ret );
7029}
7030#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7031
Andrzej Kurek25f27152022-08-17 16:09:31 -04007032#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007033static void ssl_calc_finished_tls_sha256(
7034 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7035{
7036 int len = 12;
7037 const char *sender;
7038 unsigned char padbuf[32];
7039#if defined(MBEDTLS_USE_PSA_CRYPTO)
7040 size_t hash_size;
7041 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7042 psa_status_t status;
7043#else
7044 mbedtls_sha256_context sha256;
7045#endif
7046
7047 mbedtls_ssl_session *session = ssl->session_negotiate;
7048 if( !session )
7049 session = ssl->session;
7050
7051 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7052 ? "client finished"
7053 : "server finished";
7054
7055#if defined(MBEDTLS_USE_PSA_CRYPTO)
7056 sha256_psa = psa_hash_operation_init();
7057
7058 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7059
7060 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7061 if( status != PSA_SUCCESS )
7062 {
7063 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7064 return;
7065 }
7066
7067 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7068 if( status != PSA_SUCCESS )
7069 {
7070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7071 return;
7072 }
7073 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7074#else
7075
7076 mbedtls_sha256_init( &sha256 );
7077
7078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7079
7080 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7081
7082 /*
7083 * TLSv1.2:
7084 * hash = PRF( master, finished_label,
7085 * Hash( handshake ) )[0.11]
7086 */
7087
7088#if !defined(MBEDTLS_SHA256_ALT)
7089 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7090 sha256.state, sizeof( sha256.state ) );
7091#endif
7092
7093 mbedtls_sha256_finish( &sha256, padbuf );
7094 mbedtls_sha256_free( &sha256 );
7095#endif /* MBEDTLS_USE_PSA_CRYPTO */
7096
7097 ssl->handshake->tls_prf( session->master, 48, sender,
7098 padbuf, 32, buf, len );
7099
7100 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7101
7102 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7103
7104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7105}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007106#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007107
Jerry Yub7ba49e2022-02-17 14:25:53 +08007108
Andrzej Kurek25f27152022-08-17 16:09:31 -04007109#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007110static void ssl_calc_finished_tls_sha384(
7111 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7112{
7113 int len = 12;
7114 const char *sender;
7115 unsigned char padbuf[48];
7116#if defined(MBEDTLS_USE_PSA_CRYPTO)
7117 size_t hash_size;
7118 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7119 psa_status_t status;
7120#else
7121 mbedtls_sha512_context sha512;
7122#endif
7123
7124 mbedtls_ssl_session *session = ssl->session_negotiate;
7125 if( !session )
7126 session = ssl->session;
7127
7128 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7129 ? "client finished"
7130 : "server finished";
7131
7132#if defined(MBEDTLS_USE_PSA_CRYPTO)
7133 sha384_psa = psa_hash_operation_init();
7134
7135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7136
7137 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7138 if( status != PSA_SUCCESS )
7139 {
7140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7141 return;
7142 }
7143
7144 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7145 if( status != PSA_SUCCESS )
7146 {
7147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7148 return;
7149 }
7150 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7151#else
7152 mbedtls_sha512_init( &sha512 );
7153
7154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7155
Andrzej Kureka242e832022-08-11 10:03:14 -04007156 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007157
7158 /*
7159 * TLSv1.2:
7160 * hash = PRF( master, finished_label,
7161 * Hash( handshake ) )[0.11]
7162 */
7163
7164#if !defined(MBEDTLS_SHA512_ALT)
7165 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7166 sha512.state, sizeof( sha512.state ) );
7167#endif
7168 mbedtls_sha512_finish( &sha512, padbuf );
7169
7170 mbedtls_sha512_free( &sha512 );
7171#endif
7172
7173 ssl->handshake->tls_prf( session->master, 48, sender,
7174 padbuf, 48, buf, len );
7175
7176 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7177
7178 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7179
7180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7181}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007182#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007183
Jerry Yuaef00152022-02-17 14:27:31 +08007184void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7185{
7186 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7187
7188 /*
7189 * Free our handshake params
7190 */
7191 mbedtls_ssl_handshake_free( ssl );
7192 mbedtls_free( ssl->handshake );
7193 ssl->handshake = NULL;
7194
7195 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007196 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007197 */
7198 if( ssl->transform )
7199 {
7200 mbedtls_ssl_transform_free( ssl->transform );
7201 mbedtls_free( ssl->transform );
7202 }
7203 ssl->transform = ssl->transform_negotiate;
7204 ssl->transform_negotiate = NULL;
7205
7206 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7207}
7208
Jerry Yu2a9fff52022-02-17 14:28:51 +08007209void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7210{
7211 int resume = ssl->handshake->resume;
7212
7213 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7214
7215#if defined(MBEDTLS_SSL_RENEGOTIATION)
7216 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7217 {
7218 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7219 ssl->renego_records_seen = 0;
7220 }
7221#endif
7222
7223 /*
7224 * Free the previous session and switch in the current one
7225 */
7226 if( ssl->session )
7227 {
7228#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7229 /* RFC 7366 3.1: keep the EtM state */
7230 ssl->session_negotiate->encrypt_then_mac =
7231 ssl->session->encrypt_then_mac;
7232#endif
7233
7234 mbedtls_ssl_session_free( ssl->session );
7235 mbedtls_free( ssl->session );
7236 }
7237 ssl->session = ssl->session_negotiate;
7238 ssl->session_negotiate = NULL;
7239
7240 /*
7241 * Add cache entry
7242 */
7243 if( ssl->conf->f_set_cache != NULL &&
7244 ssl->session->id_len != 0 &&
7245 resume == 0 )
7246 {
7247 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7248 ssl->session->id,
7249 ssl->session->id_len,
7250 ssl->session ) != 0 )
7251 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7252 }
7253
7254#if defined(MBEDTLS_SSL_PROTO_DTLS)
7255 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7256 ssl->handshake->flight != NULL )
7257 {
7258 /* Cancel handshake timer */
7259 mbedtls_ssl_set_timer( ssl, 0 );
7260
7261 /* Keep last flight around in case we need to resend it:
7262 * we need the handshake and transform structures for that */
7263 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7264 }
7265 else
7266#endif
7267 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7268
7269 ssl->state++;
7270
7271 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7272}
7273
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007274int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7275{
7276 int ret, hash_len;
7277
7278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7279
7280 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7281
7282 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7283
7284 /*
7285 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7286 * may define some other value. Currently (early 2016), no defined
7287 * ciphersuite does this (and this is unlikely to change as activity has
7288 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7289 */
7290 hash_len = 12;
7291
7292#if defined(MBEDTLS_SSL_RENEGOTIATION)
7293 ssl->verify_data_len = hash_len;
7294 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7295#endif
7296
7297 ssl->out_msglen = 4 + hash_len;
7298 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7299 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7300
7301 /*
7302 * In case of session resuming, invert the client and server
7303 * ChangeCipherSpec messages order.
7304 */
7305 if( ssl->handshake->resume != 0 )
7306 {
7307#if defined(MBEDTLS_SSL_CLI_C)
7308 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7309 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7310#endif
7311#if defined(MBEDTLS_SSL_SRV_C)
7312 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7313 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7314#endif
7315 }
7316 else
7317 ssl->state++;
7318
7319 /*
7320 * Switch to our negotiated transform and session parameters for outbound
7321 * data.
7322 */
7323 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7324
7325#if defined(MBEDTLS_SSL_PROTO_DTLS)
7326 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7327 {
7328 unsigned char i;
7329
7330 /* Remember current epoch settings for resending */
7331 ssl->handshake->alt_transform_out = ssl->transform_out;
7332 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7333 sizeof( ssl->handshake->alt_out_ctr ) );
7334
7335 /* Set sequence_number to zero */
7336 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7337
7338
7339 /* Increment epoch */
7340 for( i = 2; i > 0; i-- )
7341 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7342 break;
7343
7344 /* The loop goes to its end iff the counter is wrapping */
7345 if( i == 0 )
7346 {
7347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7348 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7349 }
7350 }
7351 else
7352#endif /* MBEDTLS_SSL_PROTO_DTLS */
7353 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7354
7355 ssl->transform_out = ssl->transform_negotiate;
7356 ssl->session_out = ssl->session_negotiate;
7357
7358#if defined(MBEDTLS_SSL_PROTO_DTLS)
7359 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7360 mbedtls_ssl_send_flight_completed( ssl );
7361#endif
7362
7363 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7364 {
7365 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7366 return( ret );
7367 }
7368
7369#if defined(MBEDTLS_SSL_PROTO_DTLS)
7370 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7371 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7372 {
7373 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7374 return( ret );
7375 }
7376#endif
7377
7378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7379
7380 return( 0 );
7381}
7382
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007383#define SSL_MAX_HASH_LEN 12
7384
7385int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7386{
7387 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7388 unsigned int hash_len = 12;
7389 unsigned char buf[SSL_MAX_HASH_LEN];
7390
7391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7392
7393 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7394
7395 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7396 {
7397 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7398 goto exit;
7399 }
7400
7401 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7402 {
7403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7404 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7405 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7406 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7407 goto exit;
7408 }
7409
7410 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7411 {
7412 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7413 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7414 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7415 goto exit;
7416 }
7417
7418 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7419 {
7420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7421 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7422 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7423 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7424 goto exit;
7425 }
7426
7427 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7428 buf, hash_len ) != 0 )
7429 {
7430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7431 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7432 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7433 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7434 goto exit;
7435 }
7436
7437#if defined(MBEDTLS_SSL_RENEGOTIATION)
7438 ssl->verify_data_len = hash_len;
7439 memcpy( ssl->peer_verify_data, buf, hash_len );
7440#endif
7441
7442 if( ssl->handshake->resume != 0 )
7443 {
7444#if defined(MBEDTLS_SSL_CLI_C)
7445 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7446 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7447#endif
7448#if defined(MBEDTLS_SSL_SRV_C)
7449 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7450 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7451#endif
7452 }
7453 else
7454 ssl->state++;
7455
7456#if defined(MBEDTLS_SSL_PROTO_DTLS)
7457 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7458 mbedtls_ssl_recv_flight_completed( ssl );
7459#endif
7460
7461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7462
7463exit:
7464 mbedtls_platform_zeroize( buf, hash_len );
7465 return( ret );
7466}
7467
Jerry Yu392112c2022-02-17 14:34:10 +08007468#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7469/*
7470 * Helper to get TLS 1.2 PRF from ciphersuite
7471 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7472 */
7473static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7474{
Jerry Yu392112c2022-02-17 14:34:10 +08007475 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Andrzej Kurek68327742022-10-03 06:18:18 -04007476 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7477#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007478 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007479 return( tls_prf_sha384 );
Andrzej Kurek894edde2022-09-29 06:31:14 -04007480 else
Jerry Yu392112c2022-02-17 14:34:10 +08007481#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007482#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7483 {
7484 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
7485 return( tls_prf_sha256 );
7486 }
7487#endif
7488#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7489 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7490 (void) ciphersuite_info;
7491#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007492
Andrzej Kurek894edde2022-09-29 06:31:14 -04007493 return( NULL );
Jerry Yu392112c2022-02-17 14:34:10 +08007494}
7495#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007496
7497static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7498{
7499 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007500#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007501 if( tls_prf == tls_prf_sha384 )
7502 {
7503 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7504 }
7505 else
7506#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007507#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007508 if( tls_prf == tls_prf_sha256 )
7509 {
7510 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7511 }
7512 else
7513#endif
7514 return( MBEDTLS_SSL_TLS_PRF_NONE );
7515}
7516
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007517/*
7518 * Populate a transform structure with session keys and all the other
7519 * necessary information.
7520 *
7521 * Parameters:
7522 * - [in/out]: transform: structure to populate
7523 * [in] must be just initialised with mbedtls_ssl_transform_init()
7524 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7525 * - [in] ciphersuite
7526 * - [in] master
7527 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007528 * - [in] tls_prf: pointer to PRF to use for key derivation
7529 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007530 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007531 * - [in] endpoint: client or server
7532 * - [in] ssl: used for:
7533 * - ssl->conf->{f,p}_export_keys
7534 * [in] optionally used for:
7535 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7536 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007537MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007538static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7539 int ciphersuite,
7540 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007541#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007542 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007543#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007544 ssl_tls_prf_t tls_prf,
7545 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007546 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007547 unsigned endpoint,
7548 const mbedtls_ssl_context *ssl )
7549{
7550 int ret = 0;
7551 unsigned char keyblk[256];
7552 unsigned char *key1;
7553 unsigned char *key2;
7554 unsigned char *mac_enc;
7555 unsigned char *mac_dec;
7556 size_t mac_key_len = 0;
7557 size_t iv_copy_len;
7558 size_t keylen;
7559 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007560 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007561#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007562 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007563 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007564#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007565
7566#if defined(MBEDTLS_USE_PSA_CRYPTO)
7567 psa_key_type_t key_type;
7568 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7569 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007570 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007571 size_t key_bits;
7572 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7573#endif
7574
7575#if !defined(MBEDTLS_DEBUG_C) && \
7576 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7577 if( ssl->f_export_keys == NULL )
7578 {
7579 ssl = NULL; /* make sure we don't use it except for these cases */
7580 (void) ssl;
7581 }
7582#endif
7583
7584 /*
7585 * Some data just needs copying into the structure
7586 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007587#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007588 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007589#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007590 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007591
7592#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7593 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7594#endif
7595
7596#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007597 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007598 {
7599 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7600 * generation separate. This should never happen. */
7601 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7602 }
7603#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7604
7605 /*
7606 * Get various info structures
7607 */
7608 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7609 if( ciphersuite_info == NULL )
7610 {
7611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7612 ciphersuite ) );
7613 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7614 }
7615
Neil Armstrongab555e02022-04-04 11:07:59 +02007616 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007617#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007618 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007619#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007620 ciphersuite_info );
7621
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007622 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7623 transform->taglen =
7624 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7625
7626#if defined(MBEDTLS_USE_PSA_CRYPTO)
7627 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7628 transform->taglen,
7629 &alg,
7630 &key_type,
7631 &key_bits ) ) != PSA_SUCCESS )
7632 {
7633 ret = psa_ssl_status_to_mbedtls( status );
7634 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7635 goto end;
7636 }
7637#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007638 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7639 if( cipher_info == NULL )
7640 {
7641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7642 ciphersuite_info->cipher ) );
7643 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7644 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007645#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007646
Neil Armstronge4512952022-03-08 09:08:22 +01007647#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007648 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007649 if( mac_alg == 0 )
7650 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007652 (unsigned) ciphersuite_info->mac ) );
7653 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7654 }
7655#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007656 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7657 if( md_info == NULL )
7658 {
7659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7660 (unsigned) ciphersuite_info->mac ) );
7661 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7662 }
Neil Armstronge4512952022-03-08 09:08:22 +01007663#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007664
7665#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7666 /* Copy own and peer's CID if the use of the CID
7667 * extension has been negotiated. */
7668 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7669 {
7670 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7671
7672 transform->in_cid_len = ssl->own_cid_len;
7673 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7674 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7675 transform->in_cid_len );
7676
7677 transform->out_cid_len = ssl->handshake->peer_cid_len;
7678 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7679 ssl->handshake->peer_cid_len );
7680 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7681 transform->out_cid_len );
7682 }
7683#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7684
7685 /*
7686 * Compute key block using the PRF
7687 */
7688 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7689 if( ret != 0 )
7690 {
7691 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7692 return( ret );
7693 }
7694
7695 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7696 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7697 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7698 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7699 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7700
7701 /*
7702 * Determine the appropriate key, IV and MAC length.
7703 */
7704
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007705#if defined(MBEDTLS_USE_PSA_CRYPTO)
7706 keylen = PSA_BITS_TO_BYTES(key_bits);
7707#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007708 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007709#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007710
7711#if defined(MBEDTLS_GCM_C) || \
7712 defined(MBEDTLS_CCM_C) || \
7713 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007714 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007715 {
7716 size_t explicit_ivlen;
7717
7718 transform->maclen = 0;
7719 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007720
7721 /* All modes haves 96-bit IVs, but the length of the static parts vary
7722 * with mode and version:
7723 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7724 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7725 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7726 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7727 * sequence number).
7728 */
7729 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01007730
7731 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007732#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann3b2276a2022-10-06 14:49:08 +01007733 is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007734#else
David Horstmann3b2276a2022-10-06 14:49:08 +01007735 is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info )
7736 == MBEDTLS_MODE_CHACHAPOLY );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007737#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01007738
7739 if( is_chachapoly )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007740 transform->fixed_ivlen = 12;
7741 else
7742 transform->fixed_ivlen = 4;
7743
7744 /* Minimum length of encrypted record */
7745 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7746 transform->minlen = explicit_ivlen + transform->taglen;
7747 }
7748 else
7749#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7750#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007751 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7752 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7753 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007754 {
Neil Armstronge4512952022-03-08 09:08:22 +01007755#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007756 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007757#else
7758 size_t block_size = cipher_info->block_size;
7759#endif /* MBEDTLS_USE_PSA_CRYPTO */
7760
7761#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007762 /* Get MAC length */
7763 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7764#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007765 /* Initialize HMAC contexts */
7766 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7767 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7768 {
7769 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7770 goto end;
7771 }
7772
7773 /* Get MAC length */
7774 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007775#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007776 transform->maclen = mac_key_len;
7777
7778 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007779#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007780 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007781#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007782 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007783#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007784
7785 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007786 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007787 transform->minlen = transform->maclen;
7788 else
7789 {
7790 /*
7791 * GenericBlockCipher:
7792 * 1. if EtM is in use: one block plus MAC
7793 * otherwise: * first multiple of blocklen greater than maclen
7794 * 2. IV
7795 */
7796#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007797 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007798 {
7799 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007800 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007801 }
7802 else
7803#endif
7804 {
7805 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007806 + block_size
7807 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007808 }
7809
Glenn Strauss07c64162022-03-14 12:34:51 -04007810 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007811 {
7812 transform->minlen += transform->ivlen;
7813 }
7814 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007815 {
7816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7817 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7818 goto end;
7819 }
7820 }
7821 }
7822 else
7823#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7824 {
7825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7826 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7827 }
7828
7829 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7830 (unsigned) keylen,
7831 (unsigned) transform->minlen,
7832 (unsigned) transform->ivlen,
7833 (unsigned) transform->maclen ) );
7834
7835 /*
7836 * Finally setup the cipher contexts, IVs and MAC secrets.
7837 */
7838#if defined(MBEDTLS_SSL_CLI_C)
7839 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7840 {
7841 key1 = keyblk + mac_key_len * 2;
7842 key2 = keyblk + mac_key_len * 2 + keylen;
7843
7844 mac_enc = keyblk;
7845 mac_dec = keyblk + mac_key_len;
7846
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007847 iv_copy_len = ( transform->fixed_ivlen ) ?
7848 transform->fixed_ivlen : transform->ivlen;
7849 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7850 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7851 iv_copy_len );
7852 }
7853 else
7854#endif /* MBEDTLS_SSL_CLI_C */
7855#if defined(MBEDTLS_SSL_SRV_C)
7856 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7857 {
7858 key1 = keyblk + mac_key_len * 2 + keylen;
7859 key2 = keyblk + mac_key_len * 2;
7860
7861 mac_enc = keyblk + mac_key_len;
7862 mac_dec = keyblk;
7863
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007864 iv_copy_len = ( transform->fixed_ivlen ) ?
7865 transform->fixed_ivlen : transform->ivlen;
7866 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7867 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7868 iv_copy_len );
7869 }
7870 else
7871#endif /* MBEDTLS_SSL_SRV_C */
7872 {
7873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7874 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7875 goto end;
7876 }
7877
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007878 if( ssl != NULL && ssl->f_export_keys != NULL )
7879 {
7880 ssl->f_export_keys( ssl->p_export_keys,
7881 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7882 master, 48,
7883 randbytes + 32,
7884 randbytes,
7885 tls_prf_get_type( tls_prf ) );
7886 }
7887
7888#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007889 transform->psa_alg = alg;
7890
7891 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7892 {
7893 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7894 psa_set_key_algorithm( &attributes, alg );
7895 psa_set_key_type( &attributes, key_type );
7896
7897 if( ( status = psa_import_key( &attributes,
7898 key1,
7899 PSA_BITS_TO_BYTES( key_bits ),
7900 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7901 {
7902 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7903 ret = psa_ssl_status_to_mbedtls( status );
7904 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7905 goto end;
7906 }
7907
7908 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7909
7910 if( ( status = psa_import_key( &attributes,
7911 key2,
7912 PSA_BITS_TO_BYTES( key_bits ),
7913 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7914 {
7915 ret = psa_ssl_status_to_mbedtls( status );
7916 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7917 goto end;
7918 }
7919 }
7920#else
7921 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7922 cipher_info ) ) != 0 )
7923 {
7924 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7925 goto end;
7926 }
7927
7928 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7929 cipher_info ) ) != 0 )
7930 {
7931 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7932 goto end;
7933 }
7934
7935 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7936 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7937 MBEDTLS_ENCRYPT ) ) != 0 )
7938 {
7939 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7940 goto end;
7941 }
7942
7943 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7944 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7945 MBEDTLS_DECRYPT ) ) != 0 )
7946 {
7947 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7948 goto end;
7949 }
7950
7951#if defined(MBEDTLS_CIPHER_MODE_CBC)
7952 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7953 {
7954 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7955 MBEDTLS_PADDING_NONE ) ) != 0 )
7956 {
7957 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7958 goto end;
7959 }
7960
7961 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7962 MBEDTLS_PADDING_NONE ) ) != 0 )
7963 {
7964 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7965 goto end;
7966 }
7967 }
7968#endif /* MBEDTLS_CIPHER_MODE_CBC */
7969#endif /* MBEDTLS_USE_PSA_CRYPTO */
7970
Neil Armstrong29c0c042022-03-17 17:47:28 +01007971#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7972 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7973 For AEAD-based ciphersuites, there is nothing to do here. */
7974 if( mac_key_len != 0 )
7975 {
7976#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007977 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007978
7979 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007980 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007981 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7982
7983 if( ( status = psa_import_key( &attributes,
7984 mac_enc, mac_key_len,
7985 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7986 {
7987 ret = psa_ssl_status_to_mbedtls( status );
7988 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7989 goto end;
7990 }
7991
Ronald Cronfb39f152022-03-25 14:36:28 +01007992 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04007993 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
7994#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
7995 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
7996#endif
7997 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007998 /* mbedtls_ct_hmac() requires the key to be exportable */
7999 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
8000 PSA_KEY_USAGE_VERIFY_HASH );
8001 else
8002 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
8003
8004 if( ( status = psa_import_key( &attributes,
8005 mac_dec, mac_key_len,
8006 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
8007 {
8008 ret = psa_ssl_status_to_mbedtls( status );
8009 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8010 goto end;
8011 }
8012#else
8013 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
8014 if( ret != 0 )
8015 goto end;
8016 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
8017 if( ret != 0 )
8018 goto end;
8019#endif /* MBEDTLS_USE_PSA_CRYPTO */
8020 }
8021#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8022
8023 ((void) mac_dec);
8024 ((void) mac_enc);
8025
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008026end:
8027 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
8028 return( ret );
8029}
8030
Jerry Yuee40f9d2022-02-17 14:55:16 +08008031#if defined(MBEDTLS_USE_PSA_CRYPTO)
8032int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8033 unsigned char *hash, size_t *hashlen,
8034 unsigned char *data, size_t data_len,
8035 mbedtls_md_type_t md_alg )
8036{
8037 psa_status_t status;
8038 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008039 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008040
8041 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8042
8043 if( ( status = psa_hash_setup( &hash_operation,
8044 hash_alg ) ) != PSA_SUCCESS )
8045 {
8046 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8047 goto exit;
8048 }
8049
8050 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8051 64 ) ) != PSA_SUCCESS )
8052 {
8053 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8054 goto exit;
8055 }
8056
8057 if( ( status = psa_hash_update( &hash_operation,
8058 data, data_len ) ) != PSA_SUCCESS )
8059 {
8060 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8061 goto exit;
8062 }
8063
8064 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8065 hashlen ) ) != PSA_SUCCESS )
8066 {
8067 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8068 goto exit;
8069 }
8070
8071exit:
8072 if( status != PSA_SUCCESS )
8073 {
8074 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8075 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8076 switch( status )
8077 {
8078 case PSA_ERROR_NOT_SUPPORTED:
8079 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8080 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8081 case PSA_ERROR_BUFFER_TOO_SMALL:
8082 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8083 case PSA_ERROR_INSUFFICIENT_MEMORY:
8084 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8085 default:
8086 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8087 }
8088 }
8089 return( 0 );
8090}
8091
8092#else
8093
8094int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8095 unsigned char *hash, size_t *hashlen,
8096 unsigned char *data, size_t data_len,
8097 mbedtls_md_type_t md_alg )
8098{
8099 int ret = 0;
8100 mbedtls_md_context_t ctx;
8101 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8102 *hashlen = mbedtls_md_get_size( md_info );
8103
8104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8105
8106 mbedtls_md_init( &ctx );
8107
8108 /*
8109 * digitally-signed struct {
8110 * opaque client_random[32];
8111 * opaque server_random[32];
8112 * ServerDHParams params;
8113 * };
8114 */
8115 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8116 {
8117 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8118 goto exit;
8119 }
8120 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8121 {
8122 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8123 goto exit;
8124 }
8125 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8126 {
8127 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8128 goto exit;
8129 }
8130 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8131 {
8132 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8133 goto exit;
8134 }
8135 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8136 {
8137 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8138 goto exit;
8139 }
8140
8141exit:
8142 mbedtls_md_free( &ctx );
8143
8144 if( ret != 0 )
8145 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8146 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8147
8148 return( ret );
8149}
8150#endif /* MBEDTLS_USE_PSA_CRYPTO */
8151
Jerry Yud9d91da2022-02-17 14:57:06 +08008152#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8153
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008154/* Find the preferred hash for a given signature algorithm. */
8155unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8156 mbedtls_ssl_context *ssl,
8157 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008158{
Gabor Mezei078e8032022-04-27 21:17:56 +02008159 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008160 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008161
8162 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008163 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008164
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008165 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008166 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008167 unsigned int hash_alg_received =
8168 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8169 received_sig_algs[i] );
8170 unsigned int sig_alg_received =
8171 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8172 received_sig_algs[i] );
8173
8174 if( sig_alg == sig_alg_received )
8175 {
8176#if defined(MBEDTLS_USE_PSA_CRYPTO)
8177 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8178 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008179 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008180 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008181
Neil Armstrong96eceb82022-06-30 18:05:05 +02008182 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8183 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8184 PSA_ALG_ECDSA( psa_hash_alg ),
8185 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008186 continue;
8187
Neil Armstrong96eceb82022-06-30 18:05:05 +02008188 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008189 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8190 PSA_ALG_RSA_PKCS1V15_SIGN(
8191 psa_hash_alg ),
8192 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008193 continue;
8194 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008195#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008196
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008197 return( hash_alg_received );
8198 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008199 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008200
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008201 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008202}
8203
8204#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8205
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008206/* Serialization of TLS 1.2 sessions:
8207 *
8208 * struct {
8209 * uint64 start_time;
8210 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008211 * uint8 session_id_len; // at most 32
8212 * opaque session_id[32];
8213 * opaque master[48]; // fixed length in the standard
8214 * uint32 verify_result;
8215 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8216 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8217 * uint32 ticket_lifetime;
8218 * uint8 mfl_code; // up to 255 according to standard
8219 * uint8 encrypt_then_mac; // 0 or 1
8220 * } serialized_session_tls12;
8221 *
8222 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008223static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008224 unsigned char *buf,
8225 size_t buf_len )
8226{
8227 unsigned char *p = buf;
8228 size_t used = 0;
8229
8230#if defined(MBEDTLS_HAVE_TIME)
8231 uint64_t start;
8232#endif
8233#if defined(MBEDTLS_X509_CRT_PARSE_C)
8234#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8235 size_t cert_len;
8236#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8237#endif /* MBEDTLS_X509_CRT_PARSE_C */
8238
8239 /*
8240 * Time
8241 */
8242#if defined(MBEDTLS_HAVE_TIME)
8243 used += 8;
8244
8245 if( used <= buf_len )
8246 {
8247 start = (uint64_t) session->start;
8248
8249 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8250 p += 8;
8251 }
8252#endif /* MBEDTLS_HAVE_TIME */
8253
8254 /*
8255 * Basic mandatory fields
8256 */
8257 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008258 + 1 /* id_len */
8259 + sizeof( session->id )
8260 + sizeof( session->master )
8261 + 4; /* verify_result */
8262
8263 if( used <= buf_len )
8264 {
8265 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8266 p += 2;
8267
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008268 *p++ = MBEDTLS_BYTE_0( session->id_len );
8269 memcpy( p, session->id, 32 );
8270 p += 32;
8271
8272 memcpy( p, session->master, 48 );
8273 p += 48;
8274
8275 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8276 p += 4;
8277 }
8278
8279 /*
8280 * Peer's end-entity certificate
8281 */
8282#if defined(MBEDTLS_X509_CRT_PARSE_C)
8283#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8284 if( session->peer_cert == NULL )
8285 cert_len = 0;
8286 else
8287 cert_len = session->peer_cert->raw.len;
8288
8289 used += 3 + cert_len;
8290
8291 if( used <= buf_len )
8292 {
8293 *p++ = MBEDTLS_BYTE_2( cert_len );
8294 *p++ = MBEDTLS_BYTE_1( cert_len );
8295 *p++ = MBEDTLS_BYTE_0( cert_len );
8296
8297 if( session->peer_cert != NULL )
8298 {
8299 memcpy( p, session->peer_cert->raw.p, cert_len );
8300 p += cert_len;
8301 }
8302 }
8303#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8304 if( session->peer_cert_digest != NULL )
8305 {
8306 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8307 if( used <= buf_len )
8308 {
8309 *p++ = (unsigned char) session->peer_cert_digest_type;
8310 *p++ = (unsigned char) session->peer_cert_digest_len;
8311 memcpy( p, session->peer_cert_digest,
8312 session->peer_cert_digest_len );
8313 p += session->peer_cert_digest_len;
8314 }
8315 }
8316 else
8317 {
8318 used += 2;
8319 if( used <= buf_len )
8320 {
8321 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8322 *p++ = 0;
8323 }
8324 }
8325#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8326#endif /* MBEDTLS_X509_CRT_PARSE_C */
8327
8328 /*
8329 * Session ticket if any, plus associated data
8330 */
8331#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8332 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8333
8334 if( used <= buf_len )
8335 {
8336 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8337 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8338 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8339
8340 if( session->ticket != NULL )
8341 {
8342 memcpy( p, session->ticket, session->ticket_len );
8343 p += session->ticket_len;
8344 }
8345
8346 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8347 p += 4;
8348 }
8349#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8350
8351 /*
8352 * Misc extension-related info
8353 */
8354#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8355 used += 1;
8356
8357 if( used <= buf_len )
8358 *p++ = session->mfl_code;
8359#endif
8360
8361#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8362 used += 1;
8363
8364 if( used <= buf_len )
8365 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8366#endif
8367
8368 return( used );
8369}
8370
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008371MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008372static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008373 const unsigned char *buf,
8374 size_t len )
8375{
8376#if defined(MBEDTLS_HAVE_TIME)
8377 uint64_t start;
8378#endif
8379#if defined(MBEDTLS_X509_CRT_PARSE_C)
8380#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8381 size_t cert_len;
8382#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8383#endif /* MBEDTLS_X509_CRT_PARSE_C */
8384
8385 const unsigned char *p = buf;
8386 const unsigned char * const end = buf + len;
8387
8388 /*
8389 * Time
8390 */
8391#if defined(MBEDTLS_HAVE_TIME)
8392 if( 8 > (size_t)( end - p ) )
8393 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8394
8395 start = ( (uint64_t) p[0] << 56 ) |
8396 ( (uint64_t) p[1] << 48 ) |
8397 ( (uint64_t) p[2] << 40 ) |
8398 ( (uint64_t) p[3] << 32 ) |
8399 ( (uint64_t) p[4] << 24 ) |
8400 ( (uint64_t) p[5] << 16 ) |
8401 ( (uint64_t) p[6] << 8 ) |
8402 ( (uint64_t) p[7] );
8403 p += 8;
8404
8405 session->start = (time_t) start;
8406#endif /* MBEDTLS_HAVE_TIME */
8407
8408 /*
8409 * Basic mandatory fields
8410 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008411 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008412 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8413
8414 session->ciphersuite = ( p[0] << 8 ) | p[1];
8415 p += 2;
8416
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008417 session->id_len = *p++;
8418 memcpy( session->id, p, 32 );
8419 p += 32;
8420
8421 memcpy( session->master, p, 48 );
8422 p += 48;
8423
8424 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8425 ( (uint32_t) p[1] << 16 ) |
8426 ( (uint32_t) p[2] << 8 ) |
8427 ( (uint32_t) p[3] );
8428 p += 4;
8429
8430 /* Immediately clear invalid pointer values that have been read, in case
8431 * we exit early before we replaced them with valid ones. */
8432#if defined(MBEDTLS_X509_CRT_PARSE_C)
8433#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8434 session->peer_cert = NULL;
8435#else
8436 session->peer_cert_digest = NULL;
8437#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8438#endif /* MBEDTLS_X509_CRT_PARSE_C */
8439#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8440 session->ticket = NULL;
8441#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8442
8443 /*
8444 * Peer certificate
8445 */
8446#if defined(MBEDTLS_X509_CRT_PARSE_C)
8447#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8448 /* Deserialize CRT from the end of the ticket. */
8449 if( 3 > (size_t)( end - p ) )
8450 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8451
8452 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8453 p += 3;
8454
8455 if( cert_len != 0 )
8456 {
8457 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8458
8459 if( cert_len > (size_t)( end - p ) )
8460 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8461
8462 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8463
8464 if( session->peer_cert == NULL )
8465 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8466
8467 mbedtls_x509_crt_init( session->peer_cert );
8468
8469 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8470 p, cert_len ) ) != 0 )
8471 {
8472 mbedtls_x509_crt_free( session->peer_cert );
8473 mbedtls_free( session->peer_cert );
8474 session->peer_cert = NULL;
8475 return( ret );
8476 }
8477
8478 p += cert_len;
8479 }
8480#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8481 /* Deserialize CRT digest from the end of the ticket. */
8482 if( 2 > (size_t)( end - p ) )
8483 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8484
8485 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8486 session->peer_cert_digest_len = (size_t) *p++;
8487
8488 if( session->peer_cert_digest_len != 0 )
8489 {
8490 const mbedtls_md_info_t *md_info =
8491 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8492 if( md_info == NULL )
8493 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8494 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8495 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8496
8497 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8498 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8499
8500 session->peer_cert_digest =
8501 mbedtls_calloc( 1, session->peer_cert_digest_len );
8502 if( session->peer_cert_digest == NULL )
8503 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8504
8505 memcpy( session->peer_cert_digest, p,
8506 session->peer_cert_digest_len );
8507 p += session->peer_cert_digest_len;
8508 }
8509#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8510#endif /* MBEDTLS_X509_CRT_PARSE_C */
8511
8512 /*
8513 * Session ticket and associated data
8514 */
8515#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8516 if( 3 > (size_t)( end - p ) )
8517 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8518
8519 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8520 p += 3;
8521
8522 if( session->ticket_len != 0 )
8523 {
8524 if( session->ticket_len > (size_t)( end - p ) )
8525 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8526
8527 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8528 if( session->ticket == NULL )
8529 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8530
8531 memcpy( session->ticket, p, session->ticket_len );
8532 p += session->ticket_len;
8533 }
8534
8535 if( 4 > (size_t)( end - p ) )
8536 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8537
8538 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8539 ( (uint32_t) p[1] << 16 ) |
8540 ( (uint32_t) p[2] << 8 ) |
8541 ( (uint32_t) p[3] );
8542 p += 4;
8543#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8544
8545 /*
8546 * Misc extension-related info
8547 */
8548#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8549 if( 1 > (size_t)( end - p ) )
8550 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8551
8552 session->mfl_code = *p++;
8553#endif
8554
8555#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8556 if( 1 > (size_t)( end - p ) )
8557 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8558
8559 session->encrypt_then_mac = *p++;
8560#endif
8561
8562 /* Done, should have consumed entire buffer */
8563 if( p != end )
8564 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8565
8566 return( 0 );
8567}
Jerry Yudc7bd172022-02-17 13:44:15 +08008568#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8569
XiaokangQian75d40ef2022-04-20 11:05:24 +00008570int mbedtls_ssl_validate_ciphersuite(
8571 const mbedtls_ssl_context *ssl,
8572 const mbedtls_ssl_ciphersuite_t *suite_info,
8573 mbedtls_ssl_protocol_version min_tls_version,
8574 mbedtls_ssl_protocol_version max_tls_version )
8575{
8576 (void) ssl;
8577
8578 if( suite_info == NULL )
8579 return( -1 );
8580
8581 if( ( suite_info->min_tls_version > max_tls_version ) ||
8582 ( suite_info->max_tls_version < min_tls_version ) )
8583 {
8584 return( -1 );
8585 }
8586
XiaokangQian060d8672022-04-21 09:24:56 +00008587#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008588#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8589 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8590 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8591 {
8592 return( -1 );
8593 }
8594#endif
8595
8596 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8597#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8598 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8599 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8600 {
8601 return( -1 );
8602 }
8603#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8604#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8605
8606 return( 0 );
8607}
8608
Ronald Crone68ab4f2022-10-05 12:46:29 +02008609#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00008610/*
8611 * Function for writing a signature algorithm extension.
8612 *
8613 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8614 * value (TLS 1.3 RFC8446):
8615 * enum {
8616 * ....
8617 * ecdsa_secp256r1_sha256( 0x0403 ),
8618 * ecdsa_secp384r1_sha384( 0x0503 ),
8619 * ecdsa_secp521r1_sha512( 0x0603 ),
8620 * ....
8621 * } SignatureScheme;
8622 *
8623 * struct {
8624 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8625 * } SignatureSchemeList;
8626 *
8627 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8628 * value (TLS 1.2 RFC5246):
8629 * enum {
8630 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8631 * sha512(6), (255)
8632 * } HashAlgorithm;
8633 *
8634 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8635 * SignatureAlgorithm;
8636 *
8637 * struct {
8638 * HashAlgorithm hash;
8639 * SignatureAlgorithm signature;
8640 * } SignatureAndHashAlgorithm;
8641 *
8642 * SignatureAndHashAlgorithm
8643 * supported_signature_algorithms<2..2^16-2>;
8644 *
8645 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8646 * generalization of the TLS 1.2 signature algorithm extension.
8647 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8648 * `SignatureScheme` field of TLS 1.3
8649 *
8650 */
8651int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8652 const unsigned char *end, size_t *out_len )
8653{
8654 unsigned char *p = buf;
8655 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8656 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8657
8658 *out_len = 0;
8659
8660 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8661
8662 /* Check if we have space for header and length field:
8663 * - extension_type (2 bytes)
8664 * - extension_data_length (2 bytes)
8665 * - supported_signature_algorithms_length (2 bytes)
8666 */
8667 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8668 p += 6;
8669
8670 /*
8671 * Write supported_signature_algorithms
8672 */
8673 supported_sig_alg = p;
8674 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8675 if( sig_alg == NULL )
8676 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8677
8678 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8679 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008680 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8681 *sig_alg,
8682 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008683 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8684 continue;
8685 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8686 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8687 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008688 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008689 *sig_alg,
8690 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008691 }
8692
8693 /* Length of supported_signature_algorithms */
8694 supported_sig_alg_len = p - supported_sig_alg;
8695 if( supported_sig_alg_len == 0 )
8696 {
8697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8698 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8699 }
8700
XiaokangQianeaf36512022-04-24 09:07:44 +00008701 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008702 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008703 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8704
XiaokangQianeaf36512022-04-24 09:07:44 +00008705 *out_len = p - buf;
8706
8707#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8708 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8709#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8710 return( 0 );
8711}
Ronald Crone68ab4f2022-10-05 12:46:29 +02008712#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00008713
XiaokangQian40a35232022-05-07 09:02:40 +00008714#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008715/*
8716 * mbedtls_ssl_parse_server_name_ext
8717 *
8718 * Structure of server_name extension:
8719 *
8720 * enum {
8721 * host_name(0), (255)
8722 * } NameType;
8723 * opaque HostName<1..2^16-1>;
8724 *
8725 * struct {
8726 * NameType name_type;
8727 * select (name_type) {
8728 * case host_name: HostName;
8729 * } name;
8730 * } ServerName;
8731 * struct {
8732 * ServerName server_name_list<1..2^16-1>
8733 * } ServerNameList;
8734 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008735MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008736int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8737 const unsigned char *buf,
8738 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008739{
8740 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8741 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008742 size_t server_name_list_len, hostname_len;
8743 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008744
XiaokangQianf2a94202022-05-20 06:44:24 +00008745 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008746
8747 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008748 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008749 p += 2;
8750
XiaokangQian9b2b7712022-05-17 02:57:00 +00008751 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8752 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008753 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008754 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008755 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008756 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008757 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8758 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008759
8760 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8761 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008762 /* sni_name is intended to be used only during the parsing of the
8763 * ClientHello message (it is reset to NULL before the end of
8764 * the message parsing). Thus it is ok to just point to the
8765 * reception buffer and not make a copy of it.
8766 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008767 ssl->handshake->sni_name = p + 3;
8768 ssl->handshake->sni_name_len = hostname_len;
8769 if( ssl->conf->f_sni == NULL )
8770 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008771 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008772 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008773 if( ret != 0 )
8774 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008775 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008776 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8777 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008778 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8779 }
8780 return( 0 );
8781 }
8782
8783 p += hostname_len + 3;
8784 }
8785
8786 return( 0 );
8787}
8788#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8789
XiaokangQianacb39922022-06-17 10:18:48 +00008790#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008791MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008792int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8793 const unsigned char *buf,
8794 const unsigned char *end )
8795{
8796 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008797 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008798 const unsigned char *protocol_name_list;
8799 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008800 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008801
8802 /* If ALPN not configured, just ignore the extension */
8803 if( ssl->conf->alpn_list == NULL )
8804 return( 0 );
8805
8806 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008807 * RFC7301, section 3.1
8808 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008809 *
XiaokangQianc7403452022-06-23 03:24:12 +00008810 * struct {
8811 * ProtocolName protocol_name_list<2..2^16-1>
8812 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008813 */
8814
XiaokangQianc7403452022-06-23 03:24:12 +00008815 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008816 * protocol_name_list_len 2 bytes
8817 * protocol_name_len 1 bytes
8818 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008819 */
XiaokangQianacb39922022-06-17 10:18:48 +00008820 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8821
XiaokangQianc7403452022-06-23 03:24:12 +00008822 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008823 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008824 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008825 protocol_name_list = p;
8826 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008827
8828 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008829 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008830 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008831 protocol_name_len = *p++;
8832 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8833 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008834 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008835 {
8836 MBEDTLS_SSL_PEND_FATAL_ALERT(
8837 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8838 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008839 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008840 }
8841
8842 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008843 }
8844
8845 /* Use our order of preference */
8846 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8847 {
8848 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008849 p = protocol_name_list;
8850 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008851 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008852 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008853 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008854 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008855 {
8856 ssl->alpn_chosen = *alpn;
8857 return( 0 );
8858 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008859
8860 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008861 }
8862 }
8863
XiaokangQian95d5f542022-06-24 02:29:26 +00008864 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008865 MBEDTLS_SSL_PEND_FATAL_ALERT(
8866 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8867 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8868 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8869}
8870
8871int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8872 unsigned char *buf,
8873 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008874 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008875{
8876 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008877 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008878 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008879
8880 if( ssl->alpn_chosen == NULL )
8881 {
8882 return( 0 );
8883 }
8884
XiaokangQian95d5f542022-06-24 02:29:26 +00008885 protocol_name_len = strlen( ssl->alpn_chosen );
8886 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008887
8888 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8889 /*
8890 * 0 . 1 ext identifier
8891 * 2 . 3 ext length
8892 * 4 . 5 protocol list length
8893 * 6 . 6 protocol name length
8894 * 7 . 7+n protocol name
8895 */
8896 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8897
XiaokangQian95d5f542022-06-24 02:29:26 +00008898 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008899
XiaokangQian95d5f542022-06-24 02:29:26 +00008900 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8901 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008902 /* Note: the length of the chosen protocol has been checked to be less
8903 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8904 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008905 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008906
XiaokangQian95d5f542022-06-24 02:29:26 +00008907 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008908 return ( 0 );
8909}
8910#endif /* MBEDTLS_SSL_ALPN */
8911
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008912#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00008913 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00008914 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00008915 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008916int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
8917 const char *hostname )
8918{
8919 /* Initialize to suppress unnecessary compiler warning */
8920 size_t hostname_len = 0;
8921
8922 /* Check if new hostname is valid before
8923 * making any change to current one */
8924 if( hostname != NULL )
8925 {
8926 hostname_len = strlen( hostname );
8927
8928 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8929 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8930 }
8931
8932 /* Now it's clear that we will overwrite the old hostname,
8933 * so we can free it safely */
8934 if( session->hostname != NULL )
8935 {
8936 mbedtls_platform_zeroize( session->hostname,
8937 strlen( session->hostname ) );
8938 mbedtls_free( session->hostname );
8939 }
8940
8941 /* Passing NULL as hostname shall clear the old one */
8942 if( hostname == NULL )
8943 {
8944 session->hostname = NULL;
8945 }
8946 else
8947 {
8948 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
8949 if( session->hostname == NULL )
8950 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8951
8952 memcpy( session->hostname, hostname, hostname_len );
8953 }
8954
8955 return( 0 );
8956}
Xiaokang Qian03409292022-10-12 02:49:52 +00008957#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
8958 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00008959 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00008960 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008962#endif /* MBEDTLS_SSL_TLS_C */