blob: ebada7a394da10eecdc6ba0d25bed8087d6f375a [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)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200671#if defined(MBEDTLS_USE_PSA_CRYPTO)
672 handshake->psa_pake_ctx = psa_pake_operation_init();
673 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
674#else
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200675 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Neil Armstrongca7d5062022-05-31 14:43:23 +0200676#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200677#if defined(MBEDTLS_SSL_CLI_C)
678 handshake->ecjpake_cache = NULL;
679 handshake->ecjpake_cache_len = 0;
680#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200681#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200682
Gilles Peskineeccd8882020-03-10 12:19:08 +0100683#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200684 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200685#endif
686
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200687#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
688 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
689#endif
Hanno Becker75173122019-02-06 16:18:31 +0000690
691#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
692 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
693 mbedtls_pk_init( &handshake->peer_pubkey );
694#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200695}
696
Hanno Beckera18d1322018-01-03 14:27:32 +0000697void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200698{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200700
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100701#if defined(MBEDTLS_USE_PSA_CRYPTO)
702 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
703 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100704#else
705 mbedtls_cipher_init( &transform->cipher_ctx_enc );
706 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100707#endif
708
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000709#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100710#if defined(MBEDTLS_USE_PSA_CRYPTO)
711 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
712 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100713#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_md_init( &transform->md_ctx_enc );
715 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000716#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100717#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200718}
719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200721{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200723}
724
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200725MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000727{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200728 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000729 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200731 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200733 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200734 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200735
736 /*
737 * Either the pointers are now NULL or cleared properly and can be freed.
738 * Now allocate missing structures.
739 */
740 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200741 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200742 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200743 }
Paul Bakker48916f92012-09-16 19:57:18 +0000744
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200745 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200746 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200747 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200748 }
Paul Bakker48916f92012-09-16 19:57:18 +0000749
Paul Bakker82788fb2014-10-20 13:59:19 +0200750 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200751 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200752 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200753 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500754#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
755 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400756
Andrzej Kurek4a063792020-10-21 15:08:44 +0200757 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
758 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500759#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000760
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200761 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000762 if( ssl->handshake == NULL ||
763 ssl->transform_negotiate == NULL ||
764 ssl->session_negotiate == NULL )
765 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 mbedtls_free( ssl->handshake );
769 mbedtls_free( ssl->transform_negotiate );
770 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200771
772 ssl->handshake = NULL;
773 ssl->transform_negotiate = NULL;
774 ssl->session_negotiate = NULL;
775
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200776 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000777 }
778
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200779 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000781 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200782 ssl_handshake_params_init( ssl->handshake );
783
Jerry Yud0766ec2022-09-22 10:46:57 +0800784#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
785 defined(MBEDTLS_SSL_SRV_C) && \
786 defined(MBEDTLS_SSL_SESSION_TICKETS)
787 ssl->handshake->new_session_tickets_count =
788 ssl->conf->new_session_tickets_count ;
789#endif
790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200792 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
793 {
794 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200795
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200796 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
797 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
798 else
799 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200800
Hanno Becker0f57a652020-02-05 10:37:26 +0000801 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200802 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200803#endif
804
Brett Warrene0edc842021-08-17 09:53:13 +0100805/*
806 * curve_list is translated to IANA TLS group identifiers here because
807 * mbedtls_ssl_conf_curves returns void and so can't return
808 * any error codes.
809 */
810#if defined(MBEDTLS_ECP_C)
811#if !defined(MBEDTLS_DEPRECATED_REMOVED)
812 /* Heap allocate and translate curve_list from internal to IANA group ids */
813 if ( ssl->conf->curve_list != NULL )
814 {
815 size_t length;
816 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
817
818 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
819 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
820
821 /* Leave room for zero termination */
822 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
823 if ( group_list == NULL )
824 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
825
826 for( size_t i = 0; i < length; i++ )
827 {
828 const mbedtls_ecp_curve_info *info =
829 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
830 if ( info == NULL )
831 {
832 mbedtls_free( group_list );
833 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
834 }
835 group_list[i] = info->tls_id;
836 }
837
838 group_list[length] = 0;
839
840 ssl->handshake->group_list = group_list;
841 ssl->handshake->group_list_heap_allocated = 1;
842 }
843 else
844 {
845 ssl->handshake->group_list = ssl->conf->group_list;
846 ssl->handshake->group_list_heap_allocated = 0;
847 }
848#endif /* MBEDTLS_DEPRECATED_REMOVED */
849#endif /* MBEDTLS_ECP_C */
850
Ronald Crone68ab4f2022-10-05 12:46:29 +0200851#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +0800852#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800853#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800854 /* Heap allocate and translate sig_hashes from internal hash identifiers to
855 signature algorithms IANA identifiers. */
856 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800857 ssl->conf->sig_hashes != NULL )
858 {
859 const int *md;
860 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800861 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800862 uint16_t *p;
863
Jerry Yub476a442022-01-21 18:14:45 +0800864#if defined(static_assert)
865 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
866 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
867 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800868#endif
869
Jerry Yuf017ee42022-01-12 15:49:48 +0800870 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
871 {
872 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
873 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800874#if defined(MBEDTLS_ECDSA_C)
875 sig_algs_len += sizeof( uint16_t );
876#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800877
Jerry Yub476a442022-01-21 18:14:45 +0800878#if defined(MBEDTLS_RSA_C)
879 sig_algs_len += sizeof( uint16_t );
880#endif
881 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
882 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800883 }
884
Jerry Yub476a442022-01-21 18:14:45 +0800885 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800886 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
887
Jerry Yub476a442022-01-21 18:14:45 +0800888 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
889 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800890 if( ssl->handshake->sig_algs == NULL )
891 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
892
893 p = (uint16_t *)ssl->handshake->sig_algs;
894 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
895 {
896 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
897 if( hash == MBEDTLS_SSL_HASH_NONE )
898 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800899#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800900 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
901 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800902#endif
903#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800904 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
905 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800906#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800907 }
Gabor Mezei15b95a62022-05-09 16:37:58 +0200908 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +0800909 ssl->handshake->sig_algs_heap_allocated = 1;
910 }
911 else
Jerry Yua69269a2022-01-17 21:06:01 +0800912#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800913 {
Jerry Yuf017ee42022-01-12 15:49:48 +0800914 ssl->handshake->sig_algs_heap_allocated = 0;
915 }
Jerry Yucc539102022-06-27 16:27:35 +0800916#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +0200917#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000918 return( 0 );
919}
920
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200921#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200922/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200923MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200924static int ssl_cookie_write_dummy( void *ctx,
925 unsigned char **p, unsigned char *end,
926 const unsigned char *cli_id, size_t cli_id_len )
927{
928 ((void) ctx);
929 ((void) p);
930 ((void) end);
931 ((void) cli_id);
932 ((void) cli_id_len);
933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200935}
936
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200937MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200938static int ssl_cookie_check_dummy( void *ctx,
939 const unsigned char *cookie, size_t cookie_len,
940 const unsigned char *cli_id, size_t cli_id_len )
941{
942 ((void) ctx);
943 ((void) cookie);
944 ((void) cookie_len);
945 ((void) cli_id);
946 ((void) cli_id_len);
947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200949}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200950#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200951
Paul Bakker5121ce52009-01-03 21:22:43 +0000952/*
953 * Initialize an SSL context
954 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200955void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
956{
957 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
958}
959
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200960MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800961static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
962{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100963 const mbedtls_ssl_config *conf = ssl->conf;
964
Ronald Cron6f135e12021-12-08 16:57:54 +0100965#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100966 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
967 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000968 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
969 {
970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
971 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
972 }
973
Jerry Yu60835a82021-08-04 10:13:52 +0800974 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
975 return( 0 );
976 }
977#endif
978
979#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100980 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800981 {
982 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
983 return( 0 );
984 }
985#endif
986
Ronald Cron6f135e12021-12-08 16:57:54 +0100987#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100988 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800989 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000990 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
991 {
992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
993 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
994 }
995
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000996 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
997 {
998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
999 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1000 }
1001
1002
Ronald Crone1d3f062022-02-10 14:50:54 +01001003 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
1004 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +08001005 }
1006#endif
1007
1008 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
1009 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1010}
1011
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001012MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001013static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1014{
1015 int ret;
1016 ret = ssl_conf_version_check( ssl );
1017 if( ret != 0 )
1018 return( ret );
1019
Jerry Yudef7ae42022-10-30 14:13:19 +08001020#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1021 /* RFC 8446 section 4.4.3
1022 *
1023 * If the verification fails, the receiver MUST terminate the handshake with
1024 * a "decrypt_error" alert.
1025 *
1026 * If the client is configured as TLS 1.3 only with optional verify, return
1027 * bad config.
1028 *
1029 */
1030 if( mbedtls_ssl_conf_tls13_ephemeral_enabled(
1031 (mbedtls_ssl_context *)ssl ) &&
1032 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1033 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1034 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1035 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
1036 {
1037 MBEDTLS_SSL_DEBUG_MSG(
Dave Rodgmane8734d82022-10-31 14:30:24 +00001038 1, ( "Optional verify auth mode "
Jerry Yudef7ae42022-10-30 14:13:19 +08001039 "is not available for TLS 1.3 client" ) );
1040 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1041 }
1042#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1043
Jerry Yu60835a82021-08-04 10:13:52 +08001044 /* Space for further checks */
1045
1046 return( 0 );
1047}
1048
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001049/*
1050 * Setup an SSL context
1051 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001052
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001053int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001054 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001055{
Janos Follath865b3eb2019-12-16 11:46:15 +00001056 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001057 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1058 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001059
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001060 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001061
Jerry Yu60835a82021-08-04 10:13:52 +08001062 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1063 return( ret );
1064
Paul Bakker62f2dee2012-09-28 07:31:51 +00001065 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001066 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001067 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001068
1069 /* Set to NULL in case of an error condition */
1070 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001071
Darryl Greenb33cc762019-11-28 14:29:44 +00001072#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1073 ssl->in_buf_len = in_buf_len;
1074#endif
1075 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001076 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001077 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001079 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001080 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001081 }
1082
Darryl Greenb33cc762019-11-28 14:29:44 +00001083#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1084 ssl->out_buf_len = out_buf_len;
1085#endif
1086 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001087 if( ssl->out_buf == NULL )
1088 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001090 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001091 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001092 }
1093
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001094 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001095
Johan Pascalb62bb512015-12-03 21:56:45 +01001096#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001097 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001098#endif
1099
Paul Bakker48916f92012-09-16 19:57:18 +00001100 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001101 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001102
1103 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001104
1105error:
1106 mbedtls_free( ssl->in_buf );
1107 mbedtls_free( ssl->out_buf );
1108
1109 ssl->conf = NULL;
1110
Darryl Greenb33cc762019-11-28 14:29:44 +00001111#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1112 ssl->in_buf_len = 0;
1113 ssl->out_buf_len = 0;
1114#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001115 ssl->in_buf = NULL;
1116 ssl->out_buf = NULL;
1117
1118 ssl->in_hdr = NULL;
1119 ssl->in_ctr = NULL;
1120 ssl->in_len = NULL;
1121 ssl->in_iv = NULL;
1122 ssl->in_msg = NULL;
1123
1124 ssl->out_hdr = NULL;
1125 ssl->out_ctr = NULL;
1126 ssl->out_len = NULL;
1127 ssl->out_iv = NULL;
1128 ssl->out_msg = NULL;
1129
k-stachowiak9f7798e2018-07-31 16:52:32 +02001130 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001131}
1132
1133/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001134 * Reset an initialized and used SSL context for re-use while retaining
1135 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001136 *
1137 * If partial is non-zero, keep data in the input buffer and client ID.
1138 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001139 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001140void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1141 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001142{
Darryl Greenb33cc762019-11-28 14:29:44 +00001143#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1144 size_t in_buf_len = ssl->in_buf_len;
1145 size_t out_buf_len = ssl->out_buf_len;
1146#else
1147 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1148 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1149#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001150
Hanno Beckerb0302c42021-08-03 09:39:42 +01001151#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1152 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001153#endif
1154
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001155 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001156 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001157
Hanno Beckerb0302c42021-08-03 09:39:42 +01001158 mbedtls_ssl_reset_in_out_pointers( ssl );
1159
1160 /* Reset incoming message parsing */
1161 ssl->in_offt = NULL;
1162 ssl->nb_zero = 0;
1163 ssl->in_msgtype = 0;
1164 ssl->in_msglen = 0;
1165 ssl->in_hslen = 0;
1166 ssl->keep_current_message = 0;
1167 ssl->transform_in = NULL;
1168
1169#if defined(MBEDTLS_SSL_PROTO_DTLS)
1170 ssl->next_record_offset = 0;
1171 ssl->in_epoch = 0;
1172#endif
1173
1174 /* Keep current datagram if partial == 1 */
1175 if( partial == 0 )
1176 {
1177 ssl->in_left = 0;
1178 memset( ssl->in_buf, 0, in_buf_len );
1179 }
1180
Ronald Cronad8c17b2022-06-10 17:18:09 +02001181 ssl->send_alert = 0;
1182
Hanno Beckerb0302c42021-08-03 09:39:42 +01001183 /* Reset outgoing message writing */
1184 ssl->out_msgtype = 0;
1185 ssl->out_msglen = 0;
1186 ssl->out_left = 0;
1187 memset( ssl->out_buf, 0, out_buf_len );
1188 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1189 ssl->transform_out = NULL;
1190
1191#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1192 mbedtls_ssl_dtls_replay_reset( ssl );
1193#endif
1194
XiaokangQian2b01dc32022-01-21 02:53:13 +00001195#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001196 if( ssl->transform )
1197 {
1198 mbedtls_ssl_transform_free( ssl->transform );
1199 mbedtls_free( ssl->transform );
1200 ssl->transform = NULL;
1201 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001202#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1203
XiaokangQian2b01dc32022-01-21 02:53:13 +00001204#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1205 mbedtls_ssl_transform_free( ssl->transform_application );
1206 mbedtls_free( ssl->transform_application );
1207 ssl->transform_application = NULL;
1208
1209 if( ssl->handshake != NULL )
1210 {
1211 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1212 mbedtls_free( ssl->handshake->transform_earlydata );
1213 ssl->handshake->transform_earlydata = NULL;
1214
1215 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1216 mbedtls_free( ssl->handshake->transform_handshake );
1217 ssl->handshake->transform_handshake = NULL;
1218 }
1219
XiaokangQian2b01dc32022-01-21 02:53:13 +00001220#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001221}
1222
1223int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1224{
1225 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1226
1227 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1228
XiaokangQian78b1fa72022-01-19 06:56:30 +00001229 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001230
1231 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232#if defined(MBEDTLS_SSL_RENEGOTIATION)
1233 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001234 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001235
1236 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1238 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001239#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001241
Hanno Beckerb0302c42021-08-03 09:39:42 +01001242 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001243 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001244 if( ssl->session )
1245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 mbedtls_ssl_session_free( ssl->session );
1247 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001248 ssl->session = NULL;
1249 }
1250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001252 ssl->alpn_chosen = NULL;
1253#endif
1254
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001255#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001256 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001257#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
David Horstmann3b2276a2022-10-06 14:49:08 +01001258 free_cli_id = ( partial == 0 );
Hanno Becker4ccbf062018-08-10 11:20:38 +01001259#endif
David Horstmann3b2276a2022-10-06 14:49:08 +01001260 if( free_cli_id )
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001261 {
1262 mbedtls_free( ssl->cli_id );
1263 ssl->cli_id = NULL;
1264 ssl->cli_id_len = 0;
1265 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001266#endif
1267
Paul Bakker48916f92012-09-16 19:57:18 +00001268 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1269 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001270
1271 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001272}
1273
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001274/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001275 * Reset an initialized and used SSL context for re-use while retaining
1276 * all application-set variables, function pointers and data.
1277 */
1278int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1279{
Hanno Becker43aefe22020-02-05 10:44:56 +00001280 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001281}
1282
1283/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001284 * SSL set accessors
1285 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001286void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001287{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001288 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001289}
1290
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001291void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001292{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001293 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001294}
1295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001297void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001298{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001299 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001300}
1301#endif
1302
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001303void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001304{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001305 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001306}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001309
Hanno Becker1841b0a2018-08-24 11:13:57 +01001310void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1311 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001312{
1313 ssl->disable_datagram_packing = !allow_packing;
1314}
1315
1316void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1317 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001318{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001319 conf->hs_timeout_min = min;
1320 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001321}
1322#endif
1323
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001324void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001325{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001326 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001327}
1328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001330void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001331 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001332 void *p_vrfy )
1333{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001334 conf->f_vrfy = f_vrfy;
1335 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001336}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001338
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001339void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001340 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001341 void *p_rng )
1342{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001343 conf->f_rng = f_rng;
1344 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001345}
1346
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001347void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001348 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001349 void *p_dbg )
1350{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001351 conf->f_dbg = f_dbg;
1352 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001353}
1354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001356 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001357 mbedtls_ssl_send_t *f_send,
1358 mbedtls_ssl_recv_t *f_recv,
1359 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001360{
1361 ssl->p_bio = p_bio;
1362 ssl->f_send = f_send;
1363 ssl->f_recv = f_recv;
1364 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001365}
1366
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001367#if defined(MBEDTLS_SSL_PROTO_DTLS)
1368void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1369{
1370 ssl->mtu = mtu;
1371}
1372#endif
1373
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001374void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001375{
1376 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001377}
1378
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001379void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1380 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001381 mbedtls_ssl_set_timer_t *f_set_timer,
1382 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001383{
1384 ssl->p_timer = p_timer;
1385 ssl->f_set_timer = f_set_timer;
1386 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001387
1388 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001389 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001390}
1391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001393void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001394 void *p_cache,
1395 mbedtls_ssl_cache_get_t *f_get_cache,
1396 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001397{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001398 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001399 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001400 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001401}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404#if defined(MBEDTLS_SSL_CLI_C)
1405int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001406{
Janos Follath865b3eb2019-12-16 11:46:15 +00001407 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001408
1409 if( ssl == NULL ||
1410 session == NULL ||
1411 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001412 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001415 }
1416
Hanno Beckere810bbc2021-05-14 16:01:05 +01001417 if( ssl->handshake->resume == 1 )
1418 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1419
Jerry Yu21092062022-10-10 21:21:31 +08001420#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1421 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001422 {
Jerry Yu21092062022-10-10 21:21:31 +08001423 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1424 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1425
1426 if( mbedtls_ssl_validate_ciphersuite(
1427 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1428 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1429 {
1430 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1431 session->ciphersuite ) );
1432 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1433 }
Jerry Yu40afab62022-10-08 10:42:13 +08001434 }
Jerry Yu21092062022-10-10 21:21:31 +08001435#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001436
Hanno Becker52055ae2019-02-06 14:30:46 +00001437 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1438 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001439 return( ret );
1440
Paul Bakker0a597072012-09-25 21:55:46 +00001441 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001442
1443 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001444}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001446
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001447void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1448 const int *ciphersuites )
1449{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001450 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001451}
1452
Ronald Cron6f135e12021-12-08 16:57:54 +01001453#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001454void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001455 const int kex_modes )
1456{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001457 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001458}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001459
1460#if defined(MBEDTLS_SSL_EARLY_DATA)
1461void mbedtls_ssl_tls13_conf_early_data( mbedtls_ssl_config *conf,
Xiaokang Qian72dbfef2022-10-26 06:33:57 +00001462 int early_data_enabled )
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001463{
1464 conf->early_data_enabled = early_data_enabled;
1465}
1466#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001467#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001470void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001471 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001472{
1473 conf->cert_profile = profile;
1474}
1475
Glenn Strauss36872db2022-01-22 05:06:31 -05001476static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1477{
1478 mbedtls_ssl_key_cert *cur = key_cert, *next;
1479
1480 while( cur != NULL )
1481 {
1482 next = cur->next;
1483 mbedtls_free( cur );
1484 cur = next;
1485 }
1486}
1487
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001488/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001489MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001490static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1491 mbedtls_x509_crt *cert,
1492 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001493{
niisato8ee24222018-06-25 19:05:48 +09001494 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001495
Glenn Strauss36872db2022-01-22 05:06:31 -05001496 if( cert == NULL )
1497 {
1498 /* Free list if cert is null */
1499 ssl_key_cert_free( *head );
1500 *head = NULL;
1501 return( 0 );
1502 }
1503
niisato8ee24222018-06-25 19:05:48 +09001504 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1505 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001506 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001507
niisato8ee24222018-06-25 19:05:48 +09001508 new_cert->cert = cert;
1509 new_cert->key = key;
1510 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001511
Glenn Strauss36872db2022-01-22 05:06:31 -05001512 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001513 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001514 {
niisato8ee24222018-06-25 19:05:48 +09001515 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001516 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001517 else
1518 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001519 mbedtls_ssl_key_cert *cur = *head;
1520 while( cur->next != NULL )
1521 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001522 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001523 }
1524
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001525 return( 0 );
1526}
1527
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001528int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001529 mbedtls_x509_crt *own_cert,
1530 mbedtls_pk_context *pk_key )
1531{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001532 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001533}
1534
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001535void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001536 mbedtls_x509_crt *ca_chain,
1537 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001538{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001539 conf->ca_chain = ca_chain;
1540 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001541
1542#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1543 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1544 * cannot be used together. */
1545 conf->f_ca_cb = NULL;
1546 conf->p_ca_cb = NULL;
1547#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001548}
Hanno Becker5adaad92019-03-27 16:54:37 +00001549
1550#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1551void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001552 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001553 void *p_ca_cb )
1554{
1555 conf->f_ca_cb = f_ca_cb;
1556 conf->p_ca_cb = p_ca_cb;
1557
1558 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1559 * cannot be used together. */
1560 conf->ca_chain = NULL;
1561 conf->ca_crl = NULL;
1562}
1563#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001565
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001566#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001567const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1568 size_t *name_len )
1569{
1570 *name_len = ssl->handshake->sni_name_len;
1571 return( ssl->handshake->sni_name );
1572}
1573
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001574int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1575 mbedtls_x509_crt *own_cert,
1576 mbedtls_pk_context *pk_key )
1577{
1578 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1579 own_cert, pk_key ) );
1580}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001581
1582void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1583 mbedtls_x509_crt *ca_chain,
1584 mbedtls_x509_crl *ca_crl )
1585{
1586 ssl->handshake->sni_ca_chain = ca_chain;
1587 ssl->handshake->sni_ca_crl = ca_crl;
1588}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001589
Glenn Strauss999ef702022-03-11 01:37:23 -05001590#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1591void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1592 const mbedtls_x509_crt *crt)
1593{
1594 ssl->handshake->dn_hints = crt;
1595}
1596#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1597
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001598void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1599 int authmode )
1600{
1601 ssl->handshake->sni_authmode = authmode;
1602}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001603#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1604
Hanno Becker8927c832019-04-03 12:52:50 +01001605#if defined(MBEDTLS_X509_CRT_PARSE_C)
1606void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1607 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1608 void *p_vrfy )
1609{
1610 ssl->f_vrfy = f_vrfy;
1611 ssl->p_vrfy = p_vrfy;
1612}
1613#endif
1614
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001615#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001616/*
1617 * Set EC J-PAKE password for current handshake
1618 */
1619int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1620 const unsigned char *pw,
1621 size_t pw_len )
1622{
Neil Armstrongca7d5062022-05-31 14:43:23 +02001623#if defined(MBEDTLS_USE_PSA_CRYPTO)
1624 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1625 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1626 psa_pake_role_t psa_role;
1627 psa_status_t status;
1628#else
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001629 mbedtls_ecjpake_role role;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001630#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001631
Janos Follath8eb64132016-06-03 15:40:57 +01001632 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001633 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1634
Neil Armstrongca7d5062022-05-31 14:43:23 +02001635#if defined(MBEDTLS_USE_PSA_CRYPTO)
1636 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1637 psa_role = PSA_PAKE_ROLE_SERVER;
1638 else
1639 psa_role = PSA_PAKE_ROLE_CLIENT;
1640
1641
1642 if( pw_len > 0 )
1643 {
1644 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
1645 psa_set_key_algorithm( &attributes, PSA_ALG_JPAKE );
1646 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
1647
1648 status = psa_import_key( &attributes, pw, pw_len,
1649 &ssl->handshake->psa_pake_password );
1650 if( status != PSA_SUCCESS )
1651 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1652 }
1653
1654 psa_pake_cs_set_algorithm( &cipher_suite, PSA_ALG_JPAKE );
1655 psa_pake_cs_set_primitive( &cipher_suite,
1656 PSA_PAKE_PRIMITIVE( PSA_PAKE_PRIMITIVE_TYPE_ECC,
1657 PSA_ECC_FAMILY_SECP_R1,
1658 256) );
1659 psa_pake_cs_set_hash( &cipher_suite, PSA_ALG_SHA_256 );
1660
1661 status = psa_pake_setup( &ssl->handshake->psa_pake_ctx, &cipher_suite );
1662 if( status != PSA_SUCCESS )
1663 {
1664 psa_destroy_key( ssl->handshake->psa_pake_password );
1665 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1666 }
1667
1668 status = psa_pake_set_role( &ssl->handshake->psa_pake_ctx, psa_role );
1669 if( status != PSA_SUCCESS )
1670 {
1671 psa_destroy_key( ssl->handshake->psa_pake_password );
1672 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
1673 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1674 }
1675
1676 if( pw_len > 0 )
1677 {
1678 psa_pake_set_password_key( &ssl->handshake->psa_pake_ctx,
1679 ssl->handshake->psa_pake_password );
1680 if( status != PSA_SUCCESS )
1681 {
1682 psa_destroy_key( ssl->handshake->psa_pake_password );
1683 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
1684 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1685 }
1686 }
1687
1688 ssl->handshake->psa_pake_ctx_is_ok = 1;
1689
1690 return( 0 );
1691#else
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001692 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1693 role = MBEDTLS_ECJPAKE_SERVER;
1694 else
1695 role = MBEDTLS_ECJPAKE_CLIENT;
1696
1697 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1698 role,
1699 MBEDTLS_MD_SHA256,
1700 MBEDTLS_ECP_DP_SECP256R1,
1701 pw, pw_len ) );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001702#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001703}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001704#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001705
Ronald Cron73fe8df2022-10-05 14:31:43 +02001706#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Ronald Crond29e13e2022-10-19 10:33:48 +02001707int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001708{
Ronald Crond29e13e2022-10-19 10:33:48 +02001709 if( conf->psk_identity == NULL ||
1710 conf->psk_identity_len == 0 )
1711 {
1712 return( 0 );
1713 }
1714
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001715#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Crond29e13e2022-10-19 10:33:48 +02001716 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001717 return( 1 );
1718#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02001719
1720 if( conf->psk != NULL && conf->psk_len != 0 )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001721 return( 1 );
1722
1723 return( 0 );
1724}
1725
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001726static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1727{
1728 /* Remove reference to existing PSK, if any. */
1729#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001730 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001731 {
1732 /* The maintenance of the PSK key slot is the
1733 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001734 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001735 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001736#endif /* MBEDTLS_USE_PSA_CRYPTO */
1737 if( conf->psk != NULL )
1738 {
1739 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1740
1741 mbedtls_free( conf->psk );
1742 conf->psk = NULL;
1743 conf->psk_len = 0;
1744 }
1745
1746 /* Remove reference to PSK identity, if any. */
1747 if( conf->psk_identity != NULL )
1748 {
1749 mbedtls_free( conf->psk_identity );
1750 conf->psk_identity = NULL;
1751 conf->psk_identity_len = 0;
1752 }
1753}
1754
Hanno Becker7390c712018-11-15 13:33:04 +00001755/* This function assumes that PSK identity in the SSL config is unset.
1756 * It checks that the provided identity is well-formed and attempts
1757 * to make a copy of it in the SSL config.
1758 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001759MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001760static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1761 unsigned char const *psk_identity,
1762 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001763{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001764 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001765 if( psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02001766 psk_identity_len == 0 ||
Hanno Becker7390c712018-11-15 13:33:04 +00001767 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001768 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001769 {
1770 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1771 }
1772
Hanno Becker7390c712018-11-15 13:33:04 +00001773 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1774 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001775 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001776
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001777 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001778 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001779
1780 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001781}
1782
Hanno Becker7390c712018-11-15 13:33:04 +00001783int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1784 const unsigned char *psk, size_t psk_len,
1785 const unsigned char *psk_identity, size_t psk_identity_len )
1786{
Janos Follath865b3eb2019-12-16 11:46:15 +00001787 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001788
1789 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02001790 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001791 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001792
1793 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001794 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001795 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001796 if( psk_len == 0 )
1797 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1798 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1799 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1800
Hanno Becker7390c712018-11-15 13:33:04 +00001801 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1802 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1803 conf->psk_len = psk_len;
1804 memcpy( conf->psk, psk, conf->psk_len );
1805
1806 /* Check and set PSK Identity */
1807 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1808 if( ret != 0 )
1809 ssl_conf_remove_psk( conf );
1810
1811 return( ret );
1812}
1813
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001814static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1815{
1816#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001817 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001818 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001819 /* The maintenance of the external PSK key slot is the
1820 * user's responsibility. */
1821 if( ssl->handshake->psk_opaque_is_internal )
1822 {
1823 psa_destroy_key( ssl->handshake->psk_opaque );
1824 ssl->handshake->psk_opaque_is_internal = 0;
1825 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001826 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001827 }
Neil Armstronge952a302022-05-03 10:22:14 +02001828#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001829 if( ssl->handshake->psk != NULL )
1830 {
1831 mbedtls_platform_zeroize( ssl->handshake->psk,
1832 ssl->handshake->psk_len );
1833 mbedtls_free( ssl->handshake->psk );
1834 ssl->handshake->psk_len = 0;
1835 }
Neil Armstronge952a302022-05-03 10:22:14 +02001836#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001837}
1838
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001839int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1840 const unsigned char *psk, size_t psk_len )
1841{
Neil Armstrong501c9322022-05-03 09:35:09 +02001842#if defined(MBEDTLS_USE_PSA_CRYPTO)
1843 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001844 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001845 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001846 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001847#endif /* MBEDTLS_USE_PSA_CRYPTO */
1848
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001849 if( psk == NULL || ssl->handshake == NULL )
1850 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1851
1852 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1853 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1854
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001855 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001856
Neil Armstrong501c9322022-05-03 09:35:09 +02001857#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001858#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1859 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1860 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001861 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001862 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1863 else
1864 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1865 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1866 }
1867#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001868
Jerry Yu568ec252022-07-22 21:27:34 +08001869#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001870 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1871 {
1872 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1873 psa_set_key_usage_flags( &key_attributes,
1874 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1875 }
1876#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1877
Neil Armstrong501c9322022-05-03 09:35:09 +02001878 psa_set_key_algorithm( &key_attributes, alg );
1879 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1880
1881 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1882 if( status != PSA_SUCCESS )
1883 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1884
1885 /* Allow calling psa_destroy_key() on psk remove */
1886 ssl->handshake->psk_opaque_is_internal = 1;
1887 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1888#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001889 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001890 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001891
1892 ssl->handshake->psk_len = psk_len;
1893 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1894
1895 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001896#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001897}
1898
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001899#if defined(MBEDTLS_USE_PSA_CRYPTO)
1900int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001901 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001902 const unsigned char *psk_identity,
1903 size_t psk_identity_len )
1904{
Janos Follath865b3eb2019-12-16 11:46:15 +00001905 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001906
1907 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02001908 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001909 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001910
Hanno Becker7390c712018-11-15 13:33:04 +00001911 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001912 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001913 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001914 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001915
1916 /* Check and set PSK Identity */
1917 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1918 psk_identity_len );
1919 if( ret != 0 )
1920 ssl_conf_remove_psk( conf );
1921
1922 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001923}
1924
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001925int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1926 mbedtls_svc_key_id_t psk )
1927{
1928 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1929 ( ssl->handshake == NULL ) )
1930 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1931
1932 ssl_remove_psk( ssl );
1933 ssl->handshake->psk_opaque = psk;
1934 return( 0 );
1935}
1936#endif /* MBEDTLS_USE_PSA_CRYPTO */
1937
Jerry Yu8897c072022-08-12 13:56:53 +08001938#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001939void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1940 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1941 size_t),
1942 void *p_psk )
1943{
1944 conf->f_psk = f_psk;
1945 conf->p_psk = p_psk;
1946}
Jerry Yu8897c072022-08-12 13:56:53 +08001947#endif /* MBEDTLS_SSL_SRV_C */
1948
Ronald Cron73fe8df2022-10-05 14:31:43 +02001949#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001950
1951#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001952static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1953 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001954{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001955#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001956 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001957 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001958#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001959 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001960 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001961 return( MBEDTLS_SSL_MODE_STREAM );
1962}
1963
1964#else /* MBEDTLS_USE_PSA_CRYPTO */
1965
1966static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1967 mbedtls_cipher_mode_t mode )
1968{
1969#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1970 if( mode == MBEDTLS_MODE_CBC )
1971 return( MBEDTLS_SSL_MODE_CBC );
1972#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1973
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001974#if defined(MBEDTLS_GCM_C) || \
1975 defined(MBEDTLS_CCM_C) || \
1976 defined(MBEDTLS_CHACHAPOLY_C)
1977 if( mode == MBEDTLS_MODE_GCM ||
1978 mode == MBEDTLS_MODE_CCM ||
1979 mode == MBEDTLS_MODE_CHACHAPOLY )
1980 return( MBEDTLS_SSL_MODE_AEAD );
1981#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001982
1983 return( MBEDTLS_SSL_MODE_STREAM );
1984}
Gilles Peskine301711e2022-04-26 16:57:05 +02001985#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001986
Gilles Peskinee108d982022-04-26 16:50:40 +02001987static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1988 mbedtls_ssl_mode_t base_mode,
1989 int encrypt_then_mac )
1990{
1991#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1992 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1993 base_mode == MBEDTLS_SSL_MODE_CBC )
1994 {
1995 return( MBEDTLS_SSL_MODE_CBC_ETM );
1996 }
1997#else
1998 (void) encrypt_then_mac;
1999#endif
2000 return( base_mode );
2001}
2002
Neil Armstrongab555e02022-04-04 11:07:59 +02002003mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002004 const mbedtls_ssl_transform *transform )
2005{
Gilles Peskinee108d982022-04-26 16:50:40 +02002006 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002007#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02002008 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002009#else
Gilles Peskinee108d982022-04-26 16:50:40 +02002010 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
2011#endif
2012 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002013
Gilles Peskinee108d982022-04-26 16:50:40 +02002014 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002015#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002016 encrypt_then_mac = transform->encrypt_then_mac;
2017#endif
2018 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002019}
2020
Neil Armstrongab555e02022-04-04 11:07:59 +02002021mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002022#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002023 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002024#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002025 const mbedtls_ssl_ciphersuite_t *suite )
2026{
Gilles Peskinee108d982022-04-26 16:50:40 +02002027 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2028
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002029#if defined(MBEDTLS_USE_PSA_CRYPTO)
2030 psa_status_t status;
2031 psa_algorithm_t alg;
2032 psa_key_type_t type;
2033 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002034 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
2035 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02002036 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002037#else
2038 const mbedtls_cipher_info_t *cipher =
2039 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002040 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02002041 {
2042 base_mode =
2043 mbedtls_ssl_get_base_mode(
2044 mbedtls_cipher_info_get_mode( cipher ) );
2045 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002046#endif /* MBEDTLS_USE_PSA_CRYPTO */
2047
Gilles Peskinee108d982022-04-26 16:50:40 +02002048#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2049 int encrypt_then_mac = 0;
2050#endif
2051 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002052}
2053
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002054#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002055
2056#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002057/* Serialization of TLS 1.3 sessions:
2058 *
2059 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002060 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002061 * uint64 ticket_received;
2062 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002063 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002064 * } ClientOnlyData;
2065 *
2066 * struct {
2067 * uint8 endpoint;
2068 * uint8 ciphersuite[2];
2069 * uint32 ticket_age_add;
2070 * uint8 ticket_flags;
2071 * opaque resumption_key<0..255>;
2072 * select ( endpoint ) {
2073 * case client: ClientOnlyData;
2074 * case server: uint64 start_time;
2075 * };
2076 * } serialized_session_tls13;
2077 *
2078 */
2079#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002080MBEDTLS_CHECK_RETURN_CRITICAL
2081static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2082 unsigned char *buf,
2083 size_t buf_len,
2084 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00002085{
2086 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002087#if defined(MBEDTLS_SSL_CLI_C) && \
2088 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2089 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002090 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002091#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002092 size_t needed = 1 /* endpoint */
2093 + 2 /* ciphersuite */
2094 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002095 + 1 /* ticket_flags */
2096 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002097 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002098
2099 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
2100 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2101 needed += session->resumption_key_len; /* resumption_key */
2102
Jerry Yu438ddd82022-07-07 06:55:50 +00002103#if defined(MBEDTLS_HAVE_TIME)
2104 needed += 8; /* start_time or ticket_received */
2105#endif
2106
2107#if defined(MBEDTLS_SSL_CLI_C)
2108 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2109 {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002110#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002111 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002112 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002113#endif
2114
Jerry Yu438ddd82022-07-07 06:55:50 +00002115 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002116 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002117
2118 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002119 if( session->ticket_len > SIZE_MAX - needed )
2120 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002121
Jerry Yue28d9742022-08-18 15:44:03 +08002122 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002123 }
2124#endif /* MBEDTLS_SSL_CLI_C */
2125
Jerry Yue36fdd62022-08-17 21:31:36 +08002126 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002127 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002128 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002129
2130 p[0] = session->endpoint;
2131 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2132 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2133 p[7] = session->ticket_flags;
2134
2135 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002136 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002137 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002138 memcpy( p, session->resumption_key, session->resumption_key_len );
2139 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002140
2141#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2142 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2143 {
2144 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2145 p += 8;
2146 }
2147#endif /* MBEDTLS_HAVE_TIME */
2148
2149#if defined(MBEDTLS_SSL_CLI_C)
2150 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2151 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002152#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2153 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2154 p += 2;
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002155 if( hostname_len > 0 )
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002156 {
2157 /* save host name */
2158 memcpy( p, session->hostname, hostname_len );
2159 p += hostname_len;
2160 }
2161#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2162
Jerry Yu438ddd82022-07-07 06:55:50 +00002163#if defined(MBEDTLS_HAVE_TIME)
2164 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2165 p += 8;
2166#endif
2167 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2168 p += 4;
2169
2170 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2171 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002172
2173 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002174 {
2175 memcpy( p, session->ticket, session->ticket_len );
2176 p += session->ticket_len;
2177 }
2178 }
2179#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002180 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002181}
2182
2183MBEDTLS_CHECK_RETURN_CRITICAL
2184static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2185 const unsigned char *buf,
2186 size_t len )
2187{
2188 const unsigned char *p = buf;
2189 const unsigned char *end = buf + len;
2190
2191 if( end - p < 9 )
2192 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2193 session->endpoint = p[0];
2194 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2195 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2196 session->ticket_flags = p[7];
2197
2198 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002199 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002200 p += 9;
2201
Jerry Yubc7c1a42022-07-21 22:57:37 +08002202 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002203 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2204
Jerry Yubc7c1a42022-07-21 22:57:37 +08002205 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002206 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002207 memcpy( session->resumption_key, p, session->resumption_key_len );
2208 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002209
2210#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2211 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2212 {
2213 if( end - p < 8 )
2214 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2215 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2216 p += 8;
2217 }
2218#endif /* MBEDTLS_HAVE_TIME */
2219
2220#if defined(MBEDTLS_SSL_CLI_C)
2221 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2222 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002223#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2224 defined(MBEDTLS_SSL_SESSION_TICKETS)
2225 size_t hostname_len;
2226 /* load host name */
2227 if( end - p < 2 )
2228 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2229 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2230 p += 2;
2231
2232 if( end - p < ( long int )hostname_len )
2233 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2234 if( hostname_len > 0 )
2235 {
2236 session->hostname = mbedtls_calloc( 1, hostname_len );
2237 if( session->hostname == NULL )
2238 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2239 memcpy( session->hostname, p, hostname_len );
2240 p += hostname_len;
2241 }
2242#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2243 MBEDTLS_SSL_SESSION_TICKETS */
2244
Jerry Yu438ddd82022-07-07 06:55:50 +00002245#if defined(MBEDTLS_HAVE_TIME)
2246 if( end - p < 8 )
2247 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2248 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2249 p += 8;
2250#endif
2251 if( end - p < 4 )
2252 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2253 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2254 p += 4;
2255
2256 if( end - p < 2 )
2257 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2258 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2259 p += 2;
2260
2261 if( end - p < ( long int )session->ticket_len )
2262 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2263 if( session->ticket_len > 0 )
2264 {
2265 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2266 if( session->ticket == NULL )
2267 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2268 memcpy( session->ticket, p, session->ticket_len );
2269 p += session->ticket_len;
2270 }
2271 }
2272#endif /* MBEDTLS_SSL_CLI_C */
2273
2274 return( 0 );
2275
2276}
2277#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002278MBEDTLS_CHECK_RETURN_CRITICAL
2279static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2280 unsigned char *buf,
2281 size_t buf_len,
2282 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002283{
2284 ((void) session);
2285 ((void) buf);
2286 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002287 *olen = 0;
2288 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002289}
Jerry Yu438ddd82022-07-07 06:55:50 +00002290
2291static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2292 unsigned char *buf,
2293 size_t buf_len )
2294{
2295 ((void) session);
2296 ((void) buf);
2297 ((void) buf_len);
2298 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2299}
2300#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002301#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2302
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002303psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002304 size_t taglen,
2305 psa_algorithm_t *alg,
2306 psa_key_type_t *key_type,
2307 size_t *key_size )
2308{
2309 switch ( mbedtls_cipher_type )
2310 {
2311 case MBEDTLS_CIPHER_AES_128_CBC:
2312 *alg = PSA_ALG_CBC_NO_PADDING;
2313 *key_type = PSA_KEY_TYPE_AES;
2314 *key_size = 128;
2315 break;
2316 case MBEDTLS_CIPHER_AES_128_CCM:
2317 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2318 *key_type = PSA_KEY_TYPE_AES;
2319 *key_size = 128;
2320 break;
2321 case MBEDTLS_CIPHER_AES_128_GCM:
2322 *alg = PSA_ALG_GCM;
2323 *key_type = PSA_KEY_TYPE_AES;
2324 *key_size = 128;
2325 break;
2326 case MBEDTLS_CIPHER_AES_192_CCM:
2327 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2328 *key_type = PSA_KEY_TYPE_AES;
2329 *key_size = 192;
2330 break;
2331 case MBEDTLS_CIPHER_AES_192_GCM:
2332 *alg = PSA_ALG_GCM;
2333 *key_type = PSA_KEY_TYPE_AES;
2334 *key_size = 192;
2335 break;
2336 case MBEDTLS_CIPHER_AES_256_CBC:
2337 *alg = PSA_ALG_CBC_NO_PADDING;
2338 *key_type = PSA_KEY_TYPE_AES;
2339 *key_size = 256;
2340 break;
2341 case MBEDTLS_CIPHER_AES_256_CCM:
2342 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2343 *key_type = PSA_KEY_TYPE_AES;
2344 *key_size = 256;
2345 break;
2346 case MBEDTLS_CIPHER_AES_256_GCM:
2347 *alg = PSA_ALG_GCM;
2348 *key_type = PSA_KEY_TYPE_AES;
2349 *key_size = 256;
2350 break;
2351 case MBEDTLS_CIPHER_ARIA_128_CBC:
2352 *alg = PSA_ALG_CBC_NO_PADDING;
2353 *key_type = PSA_KEY_TYPE_ARIA;
2354 *key_size = 128;
2355 break;
2356 case MBEDTLS_CIPHER_ARIA_128_CCM:
2357 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2358 *key_type = PSA_KEY_TYPE_ARIA;
2359 *key_size = 128;
2360 break;
2361 case MBEDTLS_CIPHER_ARIA_128_GCM:
2362 *alg = PSA_ALG_GCM;
2363 *key_type = PSA_KEY_TYPE_ARIA;
2364 *key_size = 128;
2365 break;
2366 case MBEDTLS_CIPHER_ARIA_192_CCM:
2367 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2368 *key_type = PSA_KEY_TYPE_ARIA;
2369 *key_size = 192;
2370 break;
2371 case MBEDTLS_CIPHER_ARIA_192_GCM:
2372 *alg = PSA_ALG_GCM;
2373 *key_type = PSA_KEY_TYPE_ARIA;
2374 *key_size = 192;
2375 break;
2376 case MBEDTLS_CIPHER_ARIA_256_CBC:
2377 *alg = PSA_ALG_CBC_NO_PADDING;
2378 *key_type = PSA_KEY_TYPE_ARIA;
2379 *key_size = 256;
2380 break;
2381 case MBEDTLS_CIPHER_ARIA_256_CCM:
2382 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2383 *key_type = PSA_KEY_TYPE_ARIA;
2384 *key_size = 256;
2385 break;
2386 case MBEDTLS_CIPHER_ARIA_256_GCM:
2387 *alg = PSA_ALG_GCM;
2388 *key_type = PSA_KEY_TYPE_ARIA;
2389 *key_size = 256;
2390 break;
2391 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2392 *alg = PSA_ALG_CBC_NO_PADDING;
2393 *key_type = PSA_KEY_TYPE_CAMELLIA;
2394 *key_size = 128;
2395 break;
2396 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2397 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2398 *key_type = PSA_KEY_TYPE_CAMELLIA;
2399 *key_size = 128;
2400 break;
2401 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2402 *alg = PSA_ALG_GCM;
2403 *key_type = PSA_KEY_TYPE_CAMELLIA;
2404 *key_size = 128;
2405 break;
2406 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2407 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2408 *key_type = PSA_KEY_TYPE_CAMELLIA;
2409 *key_size = 192;
2410 break;
2411 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2412 *alg = PSA_ALG_GCM;
2413 *key_type = PSA_KEY_TYPE_CAMELLIA;
2414 *key_size = 192;
2415 break;
2416 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2417 *alg = PSA_ALG_CBC_NO_PADDING;
2418 *key_type = PSA_KEY_TYPE_CAMELLIA;
2419 *key_size = 256;
2420 break;
2421 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2422 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2423 *key_type = PSA_KEY_TYPE_CAMELLIA;
2424 *key_size = 256;
2425 break;
2426 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2427 *alg = PSA_ALG_GCM;
2428 *key_type = PSA_KEY_TYPE_CAMELLIA;
2429 *key_size = 256;
2430 break;
2431 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2432 *alg = PSA_ALG_CHACHA20_POLY1305;
2433 *key_type = PSA_KEY_TYPE_CHACHA20;
2434 *key_size = 256;
2435 break;
2436 case MBEDTLS_CIPHER_NULL:
2437 *alg = MBEDTLS_SSL_NULL_CIPHER;
2438 *key_type = 0;
2439 *key_size = 0;
2440 break;
2441 default:
2442 return PSA_ERROR_NOT_SUPPORTED;
2443 }
2444
2445 return PSA_SUCCESS;
2446}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002447#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002448
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002449#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002450int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2451 const unsigned char *dhm_P, size_t P_len,
2452 const unsigned char *dhm_G, size_t G_len )
2453{
Janos Follath865b3eb2019-12-16 11:46:15 +00002454 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002455
Glenn Strausscee11292021-12-20 01:43:17 -05002456 mbedtls_mpi_free( &conf->dhm_P );
2457 mbedtls_mpi_free( &conf->dhm_G );
2458
Hanno Beckera90658f2017-10-04 15:29:08 +01002459 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2460 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2461 {
2462 mbedtls_mpi_free( &conf->dhm_P );
2463 mbedtls_mpi_free( &conf->dhm_G );
2464 return( ret );
2465 }
2466
2467 return( 0 );
2468}
Paul Bakker5121ce52009-01-03 21:22:43 +00002469
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002470int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002471{
Janos Follath865b3eb2019-12-16 11:46:15 +00002472 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002473
Glenn Strausscee11292021-12-20 01:43:17 -05002474 mbedtls_mpi_free( &conf->dhm_P );
2475 mbedtls_mpi_free( &conf->dhm_G );
2476
Gilles Peskinee5702482021-06-11 21:59:08 +02002477 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2478 &conf->dhm_P ) ) != 0 ||
2479 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2480 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002481 {
2482 mbedtls_mpi_free( &conf->dhm_P );
2483 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002484 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002485 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002486
2487 return( 0 );
2488}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002489#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002490
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002491#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2492/*
2493 * Set the minimum length for Diffie-Hellman parameters
2494 */
2495void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2496 unsigned int bitlen )
2497{
2498 conf->dhm_min_bitlen = bitlen;
2499}
2500#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2501
Ronald Crone68ab4f2022-10-05 12:46:29 +02002502#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002503#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002504/*
2505 * Set allowed/preferred hashes for handshake signatures
2506 */
2507void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2508 const int *hashes )
2509{
2510 conf->sig_hashes = hashes;
2511}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002512#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002513
Jerry Yuf017ee42022-01-12 15:49:48 +08002514/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002515void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2516 const uint16_t* sig_algs )
2517{
Jerry Yuf017ee42022-01-12 15:49:48 +08002518#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2519 conf->sig_hashes = NULL;
2520#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2521 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002522}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002523#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002524
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002525#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002526#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002527/*
2528 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002529 *
2530 * mbedtls_ssl_setup() takes the provided list
2531 * and translates it to a list of IANA TLS group identifiers,
2532 * stored in ssl->handshake->group_list.
2533 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002534 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002535void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002536 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002537{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002538 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002539 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002540}
Brett Warrene0edc842021-08-17 09:53:13 +01002541#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002542#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002543
Brett Warrene0edc842021-08-17 09:53:13 +01002544/*
2545 * Set the allowed groups
2546 */
2547void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2548 const uint16_t *group_list )
2549{
2550#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2551 conf->curve_list = NULL;
2552#endif
2553 conf->group_list = group_list;
2554}
2555
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002556#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002558{
Hanno Becker947194e2017-04-07 13:25:49 +01002559 /* Initialize to suppress unnecessary compiler warning */
2560 size_t hostname_len = 0;
2561
2562 /* Check if new hostname is valid before
2563 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002564 if( hostname != NULL )
2565 {
2566 hostname_len = strlen( hostname );
2567
2568 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2569 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2570 }
2571
2572 /* Now it's clear that we will overwrite the old hostname,
2573 * so we can free it safely */
2574
2575 if( ssl->hostname != NULL )
2576 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002577 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002578 mbedtls_free( ssl->hostname );
2579 }
2580
2581 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002582
Paul Bakker5121ce52009-01-03 21:22:43 +00002583 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002584 {
2585 ssl->hostname = NULL;
2586 }
2587 else
2588 {
2589 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002590 if( ssl->hostname == NULL )
2591 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002592
Hanno Becker947194e2017-04-07 13:25:49 +01002593 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002594
Hanno Becker947194e2017-04-07 13:25:49 +01002595 ssl->hostname[hostname_len] = '\0';
2596 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002597
2598 return( 0 );
2599}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002600#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002601
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002602#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002603void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002605 const unsigned char *, size_t),
2606 void *p_sni )
2607{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002608 conf->f_sni = f_sni;
2609 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002610}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002614int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002615{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002616 size_t cur_len, tot_len;
2617 const char **p;
2618
2619 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002620 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2621 * MUST NOT be truncated."
2622 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002623 */
2624 tot_len = 0;
2625 for( p = protos; *p != NULL; p++ )
2626 {
2627 cur_len = strlen( *p );
2628 tot_len += cur_len;
2629
Ronald Cron8216dd32020-04-23 16:41:44 +02002630 if( ( cur_len == 0 ) ||
2631 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2632 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002634 }
2635
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002636 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002637
2638 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002639}
2640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002642{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002643 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002644}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002646
Johan Pascalb62bb512015-12-03 21:56:45 +01002647#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002648void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2649 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002650{
2651 conf->dtls_srtp_mki_support = support_mki_value;
2652}
2653
Ron Eldoref72faf2018-07-12 11:54:20 +03002654int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2655 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002656 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002657{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002658 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002659 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002660 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002661 }
Ron Eldor591f1622018-01-22 12:30:04 +02002662
2663 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002664 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002665 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002666 }
Ron Eldor591f1622018-01-22 12:30:04 +02002667
2668 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2669 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002670 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002671}
2672
Ron Eldoref72faf2018-07-12 11:54:20 +03002673int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002674 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002675{
Johan Pascal253d0262020-09-22 13:04:45 +02002676 const mbedtls_ssl_srtp_profile *p;
2677 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002678
Johan Pascal253d0262020-09-22 13:04:45 +02002679 /* check the profiles list: all entry must be valid,
2680 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002681 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2682 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2683 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002684 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002685 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002686 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002687 list_size++;
2688 }
2689 else
2690 {
2691 /* unsupported value, stop parsing and set the size to an error value */
2692 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002693 }
2694 }
2695
Johan Pascal5ef72d22020-10-28 17:05:47 +01002696 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002697 {
Johan Pascal253d0262020-09-22 13:04:45 +02002698 conf->dtls_srtp_profile_list = NULL;
2699 conf->dtls_srtp_profile_list_len = 0;
2700 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2701 }
2702
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002703 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002704 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002705
2706 return( 0 );
2707}
2708
Johan Pascal5ef72d22020-10-28 17:05:47 +01002709void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2710 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002711{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002712 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2713 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002714 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002715 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002716 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002717 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002718 else
2719 {
2720 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002721 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2722 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002723 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002724}
Johan Pascalb62bb512015-12-03 21:56:45 +01002725#endif /* MBEDTLS_SSL_DTLS_SRTP */
2726
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302727#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002728void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002729{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002730 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002731}
2732
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002733void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002734{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002735 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002736}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302737#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002738
Janos Follath088ce432017-04-10 12:42:31 +01002739#if defined(MBEDTLS_SSL_SRV_C)
2740void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2741 char cert_req_ca_list )
2742{
2743 conf->cert_req_ca_list = cert_req_ca_list;
2744}
2745#endif
2746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002748void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002749{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002750 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002751}
2752#endif
2753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002755void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002756{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002757 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002758}
2759#endif
2760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002762int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002763{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002765 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002768 }
2769
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002770 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002771
2772 return( 0 );
2773}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002775
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002776void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002777{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002778 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002779}
2780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002782void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002783{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002784 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002785}
2786
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002787void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002788{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002789 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002790}
2791
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002792void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002793 const unsigned char period[8] )
2794{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002795 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002796}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002800#if defined(MBEDTLS_SSL_CLI_C)
2801void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002802{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002803 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002804}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002805#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002806
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002807#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002808
Jerry Yud0766ec2022-09-22 10:46:57 +08002809#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002810void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2811 uint16_t num_tickets )
2812{
Jerry Yud0766ec2022-09-22 10:46:57 +08002813 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002814}
2815#endif
2816
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002817void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2818 mbedtls_ssl_ticket_write_t *f_ticket_write,
2819 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2820 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002821{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002822 conf->f_ticket_write = f_ticket_write;
2823 conf->f_ticket_parse = f_ticket_parse;
2824 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002825}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002826#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002828
Hanno Becker7e6c1782021-06-08 09:24:55 +01002829void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2830 mbedtls_ssl_export_keys_t *f_export_keys,
2831 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002832{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002833 ssl->f_export_keys = f_export_keys;
2834 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002835}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002836
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002837#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002838void mbedtls_ssl_conf_async_private_cb(
2839 mbedtls_ssl_config *conf,
2840 mbedtls_ssl_async_sign_t *f_async_sign,
2841 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2842 mbedtls_ssl_async_resume_t *f_async_resume,
2843 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002844 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002845{
2846 conf->f_async_sign_start = f_async_sign;
2847 conf->f_async_decrypt_start = f_async_decrypt;
2848 conf->f_async_resume = f_async_resume;
2849 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002850 conf->p_async_config_data = async_config_data;
2851}
2852
Gilles Peskine8f97af72018-04-26 11:46:10 +02002853void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2854{
2855 return( conf->p_async_config_data );
2856}
2857
Gilles Peskine1febfef2018-04-30 11:54:39 +02002858void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002859{
2860 if( ssl->handshake == NULL )
2861 return( NULL );
2862 else
2863 return( ssl->handshake->user_async_ctx );
2864}
2865
Gilles Peskine1febfef2018-04-30 11:54:39 +02002866void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002867 void *ctx )
2868{
2869 if( ssl->handshake != NULL )
2870 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002871}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002872#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002873
Paul Bakker5121ce52009-01-03 21:22:43 +00002874/*
2875 * SSL get accessors
2876 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002877uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002878{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002879 if( ssl->session != NULL )
2880 return( ssl->session->verify_result );
2881
2882 if( ssl->session_negotiate != NULL )
2883 return( ssl->session_negotiate->verify_result );
2884
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002885 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002886}
2887
Glenn Strauss8f526902022-01-13 00:04:49 -05002888int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2889{
2890 if( ssl == NULL || ssl->session == NULL )
2891 return( 0 );
2892
2893 return( ssl->session->ciphersuite );
2894}
2895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002897{
Paul Bakker926c8e42013-03-06 10:23:34 +01002898 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002899 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002902}
2903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002905{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002907 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002908 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002909 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002910 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002911 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002912 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002913 default:
2914 return( "unknown (DTLS)" );
2915 }
2916 }
2917#endif
2918
Glenn Strauss60bfe602022-03-14 19:04:24 -04002919 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002920 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002921 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002922 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002923 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002924 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002925 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002926 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002927 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002928}
2929
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002930#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002931size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2932{
David Horstmann95d516f2021-05-04 18:36:56 +01002933 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002934 size_t read_mfl;
2935
2936 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2937 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2938 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2939 {
2940 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2941 }
2942
2943 /* Check if a smaller max length was negotiated */
2944 if( ssl->session_out != NULL )
2945 {
2946 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2947 if( read_mfl < max_len )
2948 {
2949 max_len = read_mfl;
2950 }
2951 }
2952
2953 // During a handshake, use the value being negotiated
2954 if( ssl->session_negotiate != NULL )
2955 {
2956 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2957 if( read_mfl < max_len )
2958 {
2959 max_len = read_mfl;
2960 }
2961 }
2962
2963 return( max_len );
2964}
2965
2966size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002967{
2968 size_t max_len;
2969
2970 /*
2971 * Assume mfl_code is correct since it was checked when set
2972 */
Angus Grattond8213d02016-05-25 20:56:48 +10002973 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002974
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002975 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002976 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002977 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002978 {
Angus Grattond8213d02016-05-25 20:56:48 +10002979 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002980 }
2981
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002982 /* During a handshake, use the value being negotiated */
2983 if( ssl->session_negotiate != NULL &&
2984 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2985 {
2986 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2987 }
2988
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002989 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002990}
2991#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2992
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002993#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002994size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002995{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002996 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2997 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2998 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2999 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
3000 return ( 0 );
3001
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003002 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
3003 return( ssl->mtu );
3004
3005 if( ssl->mtu == 0 )
3006 return( ssl->handshake->mtu );
3007
3008 return( ssl->mtu < ssl->handshake->mtu ?
3009 ssl->mtu : ssl->handshake->mtu );
3010}
3011#endif /* MBEDTLS_SSL_PROTO_DTLS */
3012
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003013int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
3014{
3015 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3016
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003017#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3018 !defined(MBEDTLS_SSL_PROTO_DTLS)
3019 (void) ssl;
3020#endif
3021
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003022#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003023 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003024
3025 if( max_len > mfl )
3026 max_len = mfl;
3027#endif
3028
3029#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003030 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003031 {
Hanno Becker89490712020-02-05 10:50:12 +00003032 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003033 const int ret = mbedtls_ssl_get_record_expansion( ssl );
3034 const size_t overhead = (size_t) ret;
3035
3036 if( ret < 0 )
3037 return( ret );
3038
3039 if( mtu <= overhead )
3040 {
3041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
3042 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3043 }
3044
3045 if( max_len > mtu - overhead )
3046 max_len = mtu - overhead;
3047 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003048#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003049
Hanno Becker0defedb2018-08-10 12:35:02 +01003050#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3051 !defined(MBEDTLS_SSL_PROTO_DTLS)
3052 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003053#endif
3054
3055 return( (int) max_len );
3056}
3057
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003058int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
3059{
3060 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3061
3062#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3063 (void) ssl;
3064#endif
3065
3066#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3067 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
3068
3069 if( max_len > mfl )
3070 max_len = mfl;
3071#endif
3072
3073 return( (int) max_len );
3074}
3075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076#if defined(MBEDTLS_X509_CRT_PARSE_C)
3077const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003078{
3079 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003080 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00003081
Hanno Beckere6824572019-02-07 13:18:46 +00003082#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003083 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00003084#else
3085 return( NULL );
3086#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003087}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003088#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00003091int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
3092 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003093{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003094 int ret;
3095
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003096 if( ssl == NULL ||
3097 dst == NULL ||
3098 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003099 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003102 }
3103
Hanno Beckere810bbc2021-05-14 16:01:05 +01003104 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3105 * idempotent: Each session can only be exported once.
3106 *
3107 * (This is in preparation for TLS 1.3 support where we will
3108 * need the ability to export multiple sessions (aka tickets),
3109 * which will be achieved by calling mbedtls_ssl_get_session()
3110 * multiple times until it fails.)
3111 *
3112 * Check whether we have already exported the current session,
3113 * and fail if so.
3114 */
3115 if( ssl->session->exported == 1 )
3116 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3117
3118 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3119 if( ret != 0 )
3120 return( ret );
3121
3122 /* Remember that we've exported the session. */
3123 ssl->session->exported = 1;
3124 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003125}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003127
Paul Bakker5121ce52009-01-03 21:22:43 +00003128/*
Hanno Beckera835da52019-05-16 12:39:07 +01003129 * Define ticket header determining Mbed TLS version
3130 * and structure of the ticket.
3131 */
3132
Hanno Becker94ef3b32019-05-16 12:50:45 +01003133/*
Hanno Becker50b59662019-05-28 14:30:45 +01003134 * Define bitflag determining compile-time settings influencing
3135 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003136 */
3137
Hanno Becker50b59662019-05-28 14:30:45 +01003138#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003139#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003140#else
Hanno Becker3e088662019-05-29 11:10:18 +01003141#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003142#endif /* MBEDTLS_HAVE_TIME */
3143
3144#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003145#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003146#else
Hanno Becker3e088662019-05-29 11:10:18 +01003147#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003148#endif /* MBEDTLS_X509_CRT_PARSE_C */
3149
3150#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003151#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003152#else
Hanno Becker3e088662019-05-29 11:10:18 +01003153#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003154#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3155
3156#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003157#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003158#else
Hanno Becker3e088662019-05-29 11:10:18 +01003159#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003160#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3161
Hanno Becker94ef3b32019-05-16 12:50:45 +01003162#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003163#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003164#else
Hanno Becker3e088662019-05-29 11:10:18 +01003165#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003166#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3167
Hanno Becker94ef3b32019-05-16 12:50:45 +01003168#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3169#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3170#else
3171#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3172#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3173
Hanno Becker3e088662019-05-29 11:10:18 +01003174#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3175#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3176#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3177#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003178#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3179#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003180
Hanno Becker50b59662019-05-28 14:30:45 +01003181#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003182 ( (uint16_t) ( \
3183 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3184 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3185 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3186 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003187 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003188 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003189
Hanno Beckerf8787072019-05-16 12:41:07 +01003190static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003191 MBEDTLS_VERSION_MAJOR,
3192 MBEDTLS_VERSION_MINOR,
3193 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003194 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3195 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003196};
Hanno Beckera835da52019-05-16 12:39:07 +01003197
3198/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003199 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003200 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003201 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003202 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003203 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003204 * opaque mbedtls_version[3]; // library version: major, minor, patch
3205 * opaque session_format[2]; // library-version specific 16-bit field
3206 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003207 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003208 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003209 * Note: When updating the format, remember to keep
3210 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003211 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003212 * // In this version, `session_format` determines
3213 * // the setting of those compile-time
3214 * // configuration options which influence
3215 * // the structure of mbedtls_ssl_session.
3216 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003217 * uint8_t minor_ver; // Protocol minor version. Possible values:
3218 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003219 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003220 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003221 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003222 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003223 * serialized_session_tls12 data;
3224 *
3225 * };
3226 *
3227 * } serialized_session;
3228 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003229 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003230
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003231MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003232static int ssl_session_save( const mbedtls_ssl_session *session,
3233 unsigned char omit_header,
3234 unsigned char *buf,
3235 size_t buf_len,
3236 size_t *olen )
3237{
3238 unsigned char *p = buf;
3239 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003240 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003241#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3242 size_t out_len;
3243 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3244#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003245 if( session == NULL )
3246 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3247
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003248 if( !omit_header )
3249 {
3250 /*
3251 * Add Mbed TLS version identifier
3252 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003253 used += sizeof( ssl_serialized_session_header );
3254
3255 if( used <= buf_len )
3256 {
3257 memcpy( p, ssl_serialized_session_header,
3258 sizeof( ssl_serialized_session_header ) );
3259 p += sizeof( ssl_serialized_session_header );
3260 }
3261 }
3262
3263 /*
3264 * TLS version identifier
3265 */
3266 used += 1;
3267 if( used <= buf_len )
3268 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003269 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003270 }
3271
3272 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003273 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003274 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003275 {
3276#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003277 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003278 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003279 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003280#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3281
Jerry Yu251a12e2022-07-13 15:15:48 +08003282#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3283 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003284 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3285 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003286 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003287 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003288 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003289#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3290
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003291 default:
3292 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3293 }
3294
3295 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003296 if( used > buf_len )
3297 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003298
3299 return( 0 );
3300}
3301
3302/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003303 * Public wrapper for ssl_session_save()
3304 */
3305int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3306 unsigned char *buf,
3307 size_t buf_len,
3308 size_t *olen )
3309{
3310 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3311}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003312
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003313/*
3314 * Deserialize session, see mbedtls_ssl_session_save() for format.
3315 *
3316 * This internal version is wrapped by a public function that cleans up in
3317 * case of error, and has an extra option omit_header.
3318 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003319MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003320static int ssl_session_load( mbedtls_ssl_session *session,
3321 unsigned char omit_header,
3322 const unsigned char *buf,
3323 size_t len )
3324{
3325 const unsigned char *p = buf;
3326 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003327 size_t remaining_len;
3328
3329
3330 if( session == NULL )
3331 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003332
3333 if( !omit_header )
3334 {
3335 /*
3336 * Check Mbed TLS version identifier
3337 */
3338
3339 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3340 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3341
3342 if( memcmp( p, ssl_serialized_session_header,
3343 sizeof( ssl_serialized_session_header ) ) != 0 )
3344 {
3345 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3346 }
3347 p += sizeof( ssl_serialized_session_header );
3348 }
3349
3350 /*
3351 * TLS version identifier
3352 */
3353 if( 1 > (size_t)( end - p ) )
3354 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003355 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003356
3357 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003358 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003359 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003360 {
3361#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003362 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003363 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003364#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3365
Jerry Yu438ddd82022-07-07 06:55:50 +00003366#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3367 case MBEDTLS_SSL_VERSION_TLS1_3:
3368 return( ssl_tls13_session_load( session, p, remaining_len ) );
3369#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3370
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003371 default:
3372 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3373 }
3374}
3375
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003376/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003377 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003378 */
3379int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3380 const unsigned char *buf,
3381 size_t len )
3382{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003383 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003384
3385 if( ret != 0 )
3386 mbedtls_ssl_session_free( session );
3387
3388 return( ret );
3389}
3390
3391/*
Paul Bakker1961b702013-01-25 14:49:24 +01003392 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003393 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003394MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003395static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3396{
3397 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3398
Ronald Cron66dbf912022-02-02 15:33:46 +01003399 /*
3400 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003401 * that were written into the output buffer by the previous handshake step,
3402 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003403 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3404 * We proceed to the next handshake step only when all data from the
3405 * previous one have been sent to the peer, thus we make sure that this is
3406 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3407 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3408 * we have to wait before to go ahead.
3409 * In the case of TLS 1.3, handshake step handlers do not send data to the
3410 * peer. Data are only sent here and through
3411 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003412 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003413 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003414 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3415 return( ret );
3416
3417#if defined(MBEDTLS_SSL_PROTO_DTLS)
3418 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3419 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3420 {
3421 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3422 return( ret );
3423 }
3424#endif /* MBEDTLS_SSL_PROTO_DTLS */
3425
3426 return( ret );
3427}
3428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003429int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003430{
Hanno Becker41934dd2021-08-07 19:13:43 +01003431 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003432
Hanno Becker41934dd2021-08-07 19:13:43 +01003433 if( ssl == NULL ||
3434 ssl->conf == NULL ||
3435 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003436 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003437 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003438 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003439 }
3440
3441 ret = ssl_prepare_handshake_step( ssl );
3442 if( ret != 0 )
3443 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003444
Jerry Yue7047812021-09-13 19:26:39 +08003445 ret = mbedtls_ssl_handle_pending_alert( ssl );
3446 if( ret != 0 )
3447 goto cleanup;
3448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003450 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003451 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3453 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003454
Ronald Cron9f0fba32022-02-10 16:45:15 +01003455 switch( ssl->state )
3456 {
3457 case MBEDTLS_SSL_HELLO_REQUEST:
3458 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3459 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003460
Ronald Cron9f0fba32022-02-10 16:45:15 +01003461 case MBEDTLS_SSL_CLIENT_HELLO:
3462 ret = mbedtls_ssl_write_client_hello( ssl );
3463 break;
3464
3465 default:
3466#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003467 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003468 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3469 else
3470 ret = mbedtls_ssl_handshake_client_step( ssl );
3471#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3472 ret = mbedtls_ssl_handshake_client_step( ssl );
3473#else
3474 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3475#endif
3476 }
Jerry Yub9930e72021-08-06 17:11:51 +08003477 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003478#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003480 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003481 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003482#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003483 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003484 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003485#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003486
3487#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3488 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3489 ret = mbedtls_ssl_handshake_server_step( ssl );
3490#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3491 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003492#endif
3493
Jerry Yue7047812021-09-13 19:26:39 +08003494 if( ret != 0 )
3495 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003496 /* handshake_step return error. And it is same
3497 * with alert_reason.
3498 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003499 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003500 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003501 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003502 goto cleanup;
3503 }
3504 }
3505
3506cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003507 return( ret );
3508}
3509
3510/*
3511 * Perform the SSL handshake
3512 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003514{
3515 int ret = 0;
3516
Hanno Beckera817ea42020-10-20 15:20:23 +01003517 /* Sanity checks */
3518
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003519 if( ssl == NULL || ssl->conf == NULL )
3520 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3521
Hanno Beckera817ea42020-10-20 15:20:23 +01003522#if defined(MBEDTLS_SSL_PROTO_DTLS)
3523 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3524 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3525 {
3526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3527 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3529 }
3530#endif /* MBEDTLS_SSL_PROTO_DTLS */
3531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003533
Hanno Beckera817ea42020-10-20 15:20:23 +01003534 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003535 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003537 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003538
3539 if( ret != 0 )
3540 break;
3541 }
3542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003544
3545 return( ret );
3546}
3547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548#if defined(MBEDTLS_SSL_RENEGOTIATION)
3549#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003550/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003551 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003552 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003553MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003554static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003555{
Janos Follath865b3eb2019-12-16 11:46:15 +00003556 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003559
3560 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3562 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003563
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003564 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003565 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003566 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003567 return( ret );
3568 }
3569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003571
3572 return( 0 );
3573}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003575
3576/*
3577 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003578 * - any side: calling mbedtls_ssl_renegotiate(),
3579 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3580 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003581 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003582 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003584 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003585int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003586{
Janos Follath865b3eb2019-12-16 11:46:15 +00003587 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003589 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003590
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003591 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3592 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003593
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003594 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3595 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003597 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003598 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003599 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003600 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003601 ssl->handshake->out_msg_seq = 1;
3602 else
3603 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003604 }
3605#endif
3606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3608 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003613 return( ret );
3614 }
3615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003617
3618 return( 0 );
3619}
3620
3621/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003622 * Renegotiate current connection on client,
3623 * or request renegotiation on server
3624 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003626{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003628
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003629 if( ssl == NULL || ssl->conf == NULL )
3630 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003633 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003634 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003635 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003636 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003639 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003640
3641 /* Did we already try/start sending HelloRequest? */
3642 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003644
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003645 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003646 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003650 /*
3651 * On client, either start the renegotiation process or,
3652 * if already in progress, continue the handshake
3653 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003655 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003656 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003658
Hanno Becker40cdaa12020-02-05 10:48:27 +00003659 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003660 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003661 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003662 return( ret );
3663 }
3664 }
3665 else
3666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003670 return( ret );
3671 }
3672 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003674
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003675 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003676}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003678
Gilles Peskine9b562d52018-04-25 20:32:43 +02003679void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003680{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003681 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3682
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003683 if( handshake == NULL )
3684 return;
3685
Brett Warrene0edc842021-08-17 09:53:13 +01003686#if defined(MBEDTLS_ECP_C)
3687#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3688 if ( ssl->handshake->group_list_heap_allocated )
3689 mbedtls_free( (void*) handshake->group_list );
3690 handshake->group_list = NULL;
3691#endif /* MBEDTLS_DEPRECATED_REMOVED */
3692#endif /* MBEDTLS_ECP_C */
3693
Ronald Crone68ab4f2022-10-05 12:46:29 +02003694#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003695#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3696 if ( ssl->handshake->sig_algs_heap_allocated )
3697 mbedtls_free( (void*) handshake->sig_algs );
3698 handshake->sig_algs = NULL;
3699#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003700#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3701 if( ssl->handshake->certificate_request_context )
3702 {
3703 mbedtls_free( (void*) handshake->certificate_request_context );
3704 }
3705#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02003706#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08003707
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003708#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3709 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3710 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003711 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003712 handshake->async_in_progress = 0;
3713 }
3714#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3715
Andrzej Kurek25f27152022-08-17 16:09:31 -04003716#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003717#if defined(MBEDTLS_USE_PSA_CRYPTO)
3718 psa_hash_abort( &handshake->fin_sha256_psa );
3719#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003720 mbedtls_sha256_free( &handshake->fin_sha256 );
3721#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003722#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003723#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003724#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003725 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003726#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003727 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003728#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003729#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003731#if defined(MBEDTLS_DHM_C)
3732 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003733#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003734#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003735 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003736#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003737#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02003738#if defined(MBEDTLS_USE_PSA_CRYPTO)
3739 psa_pake_abort( &handshake->psa_pake_ctx );
3740 psa_destroy_key( handshake->psa_pake_password );
3741 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
3742#else
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003743 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Neil Armstrongca7d5062022-05-31 14:43:23 +02003744#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003745#if defined(MBEDTLS_SSL_CLI_C)
3746 mbedtls_free( handshake->ecjpake_cache );
3747 handshake->ecjpake_cache = NULL;
3748 handshake->ecjpake_cache_len = 0;
3749#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003750#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003751
Janos Follath4ae5c292016-02-10 11:27:43 +00003752#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3753 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003754 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003755 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003756#endif
3757
Ronald Cron73fe8df2022-10-05 14:31:43 +02003758#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003759#if defined(MBEDTLS_USE_PSA_CRYPTO)
3760 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3761 {
3762 /* The maintenance of the external PSK key slot is the
3763 * user's responsibility. */
3764 if( ssl->handshake->psk_opaque_is_internal )
3765 {
3766 psa_destroy_key( ssl->handshake->psk_opaque );
3767 ssl->handshake->psk_opaque_is_internal = 0;
3768 }
3769 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3770 }
Neil Armstronge952a302022-05-03 10:22:14 +02003771#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003772 if( handshake->psk != NULL )
3773 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003774 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003775 mbedtls_free( handshake->psk );
3776 }
Neil Armstronge952a302022-05-03 10:22:14 +02003777#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02003778#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3781 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003782 /*
3783 * Free only the linked list wrapper, not the keys themselves
3784 * since the belong to the SNI callback
3785 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003786 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003787#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003788
Gilles Peskineeccd8882020-03-10 12:19:08 +01003789#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003790 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003791 if( handshake->ecrs_peer_cert != NULL )
3792 {
3793 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3794 mbedtls_free( handshake->ecrs_peer_cert );
3795 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003796#endif
3797
Hanno Becker75173122019-02-06 16:18:31 +00003798#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3799 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3800 mbedtls_pk_free( &handshake->peer_pubkey );
3801#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3802
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003803#if defined(MBEDTLS_SSL_CLI_C) && \
3804 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3805 mbedtls_free( handshake->cookie );
3806#endif /* MBEDTLS_SSL_CLI_C &&
3807 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003808
3809#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003810 mbedtls_ssl_flight_free( handshake->flight );
3811 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003812#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003813
Ronald Cronf12b81d2022-03-15 10:42:41 +01003814#if defined(MBEDTLS_ECDH_C) && \
3815 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003816 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003817 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003818#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3819
Ronald Cron6f135e12021-12-08 16:57:54 +01003820#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003821 mbedtls_ssl_transform_free( handshake->transform_handshake );
3822 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003823 mbedtls_free( handshake->transform_earlydata );
3824 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003825#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003826
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003827
3828#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3829 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3830 * processes datagrams and the fact that a datagram is allowed to have
3831 * several records in it, it is possible that the I/O buffers are not
3832 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003833 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3834 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003835#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003836
Jerry Yuba9c7272021-10-30 11:54:10 +08003837 /* mbedtls_platform_zeroize MUST be last one in this function */
3838 mbedtls_platform_zeroize( handshake,
3839 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003840}
3841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003843{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003844 if( session == NULL )
3845 return;
3846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003847#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003848 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003849#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003850
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003851#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00003852#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
3853 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00003854 mbedtls_free( session->hostname );
3855#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003857#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003858
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003859 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003860}
3861
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003862#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003863
3864#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3865#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3866#else
3867#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3868#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3869
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003870#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003871
3872#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3873#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3874#else
3875#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3876#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3877
3878#if defined(MBEDTLS_SSL_ALPN)
3879#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3880#else
3881#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3882#endif /* MBEDTLS_SSL_ALPN */
3883
3884#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3885#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3886#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3887#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3888
3889#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3890 ( (uint32_t) ( \
3891 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3892 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3893 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3894 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3895 0u ) )
3896
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003897static unsigned char ssl_serialized_context_header[] = {
3898 MBEDTLS_VERSION_MAJOR,
3899 MBEDTLS_VERSION_MINOR,
3900 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003901 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3902 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3903 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3904 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3905 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003906};
3907
Paul Bakker5121ce52009-01-03 21:22:43 +00003908/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003909 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003910 *
3911 * The format of the serialized data is:
3912 * (in the presentation language of TLS, RFC 8446 section 3)
3913 *
3914 * // header
3915 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003916 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003917 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003918 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003919 * Note: When updating the format, remember to keep these
3920 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003921 *
3922 * // session sub-structure
3923 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3924 * // transform sub-structure
3925 * uint8 random[64]; // ServerHello.random+ClientHello.random
3926 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3927 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3928 * // fields from ssl_context
3929 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3930 * uint64 in_window_top; // DTLS: last validated record seq_num
3931 * uint64 in_window; // DTLS: bitmask for replay protection
3932 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3933 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3934 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3935 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3936 *
3937 * Note that many fields of the ssl_context or sub-structures are not
3938 * serialized, as they fall in one of the following categories:
3939 *
3940 * 1. forced value (eg in_left must be 0)
3941 * 2. pointer to dynamically-allocated memory (eg session, transform)
3942 * 3. value can be re-derived from other data (eg session keys from MS)
3943 * 4. value was temporary (eg content of input buffer)
3944 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003945 */
3946int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3947 unsigned char *buf,
3948 size_t buf_len,
3949 size_t *olen )
3950{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003951 unsigned char *p = buf;
3952 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003953 size_t session_len;
3954 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003955
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003956 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003957 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3958 * this function's documentation.
3959 *
3960 * These are due to assumptions/limitations in the implementation. Some of
3961 * them are likely to stay (no handshake in progress) some might go away
3962 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003963 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003964 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003965 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003966 {
3967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003968 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003969 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003970 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003971 {
3972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003973 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003974 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003975 /* Double-check that sub-structures are indeed ready */
3976 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003977 {
3978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003979 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003980 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003981 /* There must be no pending incoming or outgoing data */
3982 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003983 {
3984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003985 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003986 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003987 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003988 {
3989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003990 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003991 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003992 /* Protocol must be DLTS, not TLS */
3993 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003994 {
3995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003996 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003997 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003998 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003999 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004000 {
4001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004002 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004003 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004004 /* We must be using an AEAD ciphersuite */
4005 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004006 {
4007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004008 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004009 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004010 /* Renegotiation must not be enabled */
4011#if defined(MBEDTLS_SSL_RENEGOTIATION)
4012 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004013 {
4014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004015 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004016 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004017#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004018
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004019 /*
4020 * Version and format identifier
4021 */
4022 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004023
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004024 if( used <= buf_len )
4025 {
4026 memcpy( p, ssl_serialized_context_header,
4027 sizeof( ssl_serialized_context_header ) );
4028 p += sizeof( ssl_serialized_context_header );
4029 }
4030
4031 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004032 * Session (length + data)
4033 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004034 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004035 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
4036 return( ret );
4037
4038 used += 4 + session_len;
4039 if( used <= buf_len )
4040 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004041 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
4042 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004043
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004044 ret = ssl_session_save( ssl->session, 1,
4045 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004046 if( ret != 0 )
4047 return( ret );
4048
4049 p += session_len;
4050 }
4051
4052 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004053 * Transform
4054 */
4055 used += sizeof( ssl->transform->randbytes );
4056 if( used <= buf_len )
4057 {
4058 memcpy( p, ssl->transform->randbytes,
4059 sizeof( ssl->transform->randbytes ) );
4060 p += sizeof( ssl->transform->randbytes );
4061 }
4062
4063#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4064 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
4065 if( used <= buf_len )
4066 {
4067 *p++ = ssl->transform->in_cid_len;
4068 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
4069 p += ssl->transform->in_cid_len;
4070
4071 *p++ = ssl->transform->out_cid_len;
4072 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
4073 p += ssl->transform->out_cid_len;
4074 }
4075#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4076
4077 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004078 * Saved fields from top-level ssl_context structure
4079 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004080 used += 4;
4081 if( used <= buf_len )
4082 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004083 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
4084 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004085 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004086
4087#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4088 used += 16;
4089 if( used <= buf_len )
4090 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004091 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
4092 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004093
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004094 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
4095 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004096 }
4097#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4098
4099#if defined(MBEDTLS_SSL_PROTO_DTLS)
4100 used += 1;
4101 if( used <= buf_len )
4102 {
4103 *p++ = ssl->disable_datagram_packing;
4104 }
4105#endif /* MBEDTLS_SSL_PROTO_DTLS */
4106
Jerry Yuae0b2e22021-10-08 15:21:19 +08004107 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004108 if( used <= buf_len )
4109 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004110 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
4111 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004112 }
4113
4114#if defined(MBEDTLS_SSL_PROTO_DTLS)
4115 used += 2;
4116 if( used <= buf_len )
4117 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004118 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4119 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004120 }
4121#endif /* MBEDTLS_SSL_PROTO_DTLS */
4122
4123#if defined(MBEDTLS_SSL_ALPN)
4124 {
4125 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004126 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004127 : 0;
4128
4129 used += 1 + alpn_len;
4130 if( used <= buf_len )
4131 {
4132 *p++ = alpn_len;
4133
4134 if( ssl->alpn_chosen != NULL )
4135 {
4136 memcpy( p, ssl->alpn_chosen, alpn_len );
4137 p += alpn_len;
4138 }
4139 }
4140 }
4141#endif /* MBEDTLS_SSL_ALPN */
4142
4143 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004144 * Done
4145 */
4146 *olen = used;
4147
4148 if( used > buf_len )
4149 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004150
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004151 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4152
Hanno Becker43aefe22020-02-05 10:44:56 +00004153 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004154}
4155
4156/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004157 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004158 *
4159 * This internal version is wrapped by a public function that cleans up in
4160 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004161 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004162MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004163static int ssl_context_load( mbedtls_ssl_context *ssl,
4164 const unsigned char *buf,
4165 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004166{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004167 const unsigned char *p = buf;
4168 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004169 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004170 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004171#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004172 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004173#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004174
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004175 /*
4176 * The context should have been freshly setup or reset.
4177 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004178 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004179 * renegotiating, or if the user mistakenly loaded a session first.)
4180 */
4181 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4182 ssl->session != NULL )
4183 {
4184 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4185 }
4186
4187 /*
4188 * We can't check that the config matches the initial one, but we can at
4189 * least check it matches the requirements for serializing.
4190 */
4191 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004192 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4193 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004194#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004195 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004196#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004197 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004198 {
4199 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4200 }
4201
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004202 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4203
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004204 /*
4205 * Check version identifier
4206 */
4207 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4208 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4209
4210 if( memcmp( p, ssl_serialized_context_header,
4211 sizeof( ssl_serialized_context_header ) ) != 0 )
4212 {
4213 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4214 }
4215 p += sizeof( ssl_serialized_context_header );
4216
4217 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004218 * Session
4219 */
4220 if( (size_t)( end - p ) < 4 )
4221 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4222
4223 session_len = ( (size_t) p[0] << 24 ) |
4224 ( (size_t) p[1] << 16 ) |
4225 ( (size_t) p[2] << 8 ) |
4226 ( (size_t) p[3] );
4227 p += 4;
4228
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004229 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004230 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004231 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004232 ssl->session_in = ssl->session;
4233 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004234 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004235
4236 if( (size_t)( end - p ) < session_len )
4237 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4238
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004239 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004240 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004241 {
4242 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004243 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004244 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004245
4246 p += session_len;
4247
4248 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004249 * Transform
4250 */
4251
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004252 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004253 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004254 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004255 ssl->transform_in = ssl->transform;
4256 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004257 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004258
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004259#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004260 prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite );
4261 if( prf_func == NULL )
4262 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4263
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004264 /* Read random bytes and populate structure */
4265 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4266 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004267
Hanno Beckerbd257552021-03-22 06:59:27 +00004268 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004269 ssl->session->ciphersuite,
4270 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004271#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004272 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004273#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Andrzej Kurek894edde2022-09-29 06:31:14 -04004274 prf_func,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004275 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004276 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004277 ssl->conf->endpoint,
4278 ssl );
4279 if( ret != 0 )
4280 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004281#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004282 p += sizeof( ssl->transform->randbytes );
4283
4284#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4285 /* Read connection IDs and store them */
4286 if( (size_t)( end - p ) < 1 )
4287 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4288
4289 ssl->transform->in_cid_len = *p++;
4290
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004291 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004292 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4293
4294 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4295 p += ssl->transform->in_cid_len;
4296
4297 ssl->transform->out_cid_len = *p++;
4298
4299 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4300 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4301
4302 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4303 p += ssl->transform->out_cid_len;
4304#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4305
4306 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004307 * Saved fields from top-level ssl_context structure
4308 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004309 if( (size_t)( end - p ) < 4 )
4310 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4311
4312 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4313 ( (uint32_t) p[1] << 16 ) |
4314 ( (uint32_t) p[2] << 8 ) |
4315 ( (uint32_t) p[3] );
4316 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004317
4318#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4319 if( (size_t)( end - p ) < 16 )
4320 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4321
4322 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4323 ( (uint64_t) p[1] << 48 ) |
4324 ( (uint64_t) p[2] << 40 ) |
4325 ( (uint64_t) p[3] << 32 ) |
4326 ( (uint64_t) p[4] << 24 ) |
4327 ( (uint64_t) p[5] << 16 ) |
4328 ( (uint64_t) p[6] << 8 ) |
4329 ( (uint64_t) p[7] );
4330 p += 8;
4331
4332 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4333 ( (uint64_t) p[1] << 48 ) |
4334 ( (uint64_t) p[2] << 40 ) |
4335 ( (uint64_t) p[3] << 32 ) |
4336 ( (uint64_t) p[4] << 24 ) |
4337 ( (uint64_t) p[5] << 16 ) |
4338 ( (uint64_t) p[6] << 8 ) |
4339 ( (uint64_t) p[7] );
4340 p += 8;
4341#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4342
4343#if defined(MBEDTLS_SSL_PROTO_DTLS)
4344 if( (size_t)( end - p ) < 1 )
4345 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4346
4347 ssl->disable_datagram_packing = *p++;
4348#endif /* MBEDTLS_SSL_PROTO_DTLS */
4349
Jerry Yud9a94fe2021-09-28 18:58:59 +08004350 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004351 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004352 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4353 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004354
4355#if defined(MBEDTLS_SSL_PROTO_DTLS)
4356 if( (size_t)( end - p ) < 2 )
4357 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4358
4359 ssl->mtu = ( p[0] << 8 ) | p[1];
4360 p += 2;
4361#endif /* MBEDTLS_SSL_PROTO_DTLS */
4362
4363#if defined(MBEDTLS_SSL_ALPN)
4364 {
4365 uint8_t alpn_len;
4366 const char **cur;
4367
4368 if( (size_t)( end - p ) < 1 )
4369 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4370
4371 alpn_len = *p++;
4372
4373 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4374 {
4375 /* alpn_chosen should point to an item in the configured list */
4376 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4377 {
4378 if( strlen( *cur ) == alpn_len &&
4379 memcmp( p, cur, alpn_len ) == 0 )
4380 {
4381 ssl->alpn_chosen = *cur;
4382 break;
4383 }
4384 }
4385 }
4386
4387 /* can only happen on conf mismatch */
4388 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4389 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4390
4391 p += alpn_len;
4392 }
4393#endif /* MBEDTLS_SSL_ALPN */
4394
4395 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004396 * Forced fields from top-level ssl_context structure
4397 *
4398 * Most of them already set to the correct value by mbedtls_ssl_init() and
4399 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4400 */
4401 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004402 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004403
Hanno Becker361b10d2019-08-30 10:42:49 +01004404 /* Adjust pointers for header fields of outgoing records to
4405 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004406 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004407
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004408#if defined(MBEDTLS_SSL_PROTO_DTLS)
4409 ssl->in_epoch = 1;
4410#endif
4411
4412 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4413 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004414 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4415 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004416 if( ssl->handshake != NULL )
4417 {
4418 mbedtls_ssl_handshake_free( ssl );
4419 mbedtls_free( ssl->handshake );
4420 ssl->handshake = NULL;
4421 }
4422
4423 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004424 * Done - should have consumed entire buffer
4425 */
4426 if( p != end )
4427 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004428
4429 return( 0 );
4430}
4431
4432/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004433 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004434 */
4435int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4436 const unsigned char *buf,
4437 size_t len )
4438{
4439 int ret = ssl_context_load( context, buf, len );
4440
4441 if( ret != 0 )
4442 mbedtls_ssl_free( context );
4443
4444 return( ret );
4445}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004446#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004447
4448/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004449 * Free an SSL context
4450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004451void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004452{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004453 if( ssl == NULL )
4454 return;
4455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004457
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004458 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004459 {
sander-visserb8aa2072020-05-06 22:05:13 +02004460#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4461 size_t out_buf_len = ssl->out_buf_len;
4462#else
4463 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4464#endif
4465
Darryl Greenb33cc762019-11-28 14:29:44 +00004466 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004467 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004468 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004469 }
4470
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004471 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004472 {
sander-visserb8aa2072020-05-06 22:05:13 +02004473#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4474 size_t in_buf_len = ssl->in_buf_len;
4475#else
4476 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4477#endif
4478
Darryl Greenb33cc762019-11-28 14:29:44 +00004479 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004480 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004481 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004482 }
4483
Paul Bakker48916f92012-09-16 19:57:18 +00004484 if( ssl->transform )
4485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004486 mbedtls_ssl_transform_free( ssl->transform );
4487 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004488 }
4489
4490 if( ssl->handshake )
4491 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004492 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004493 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4494 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004496 mbedtls_free( ssl->handshake );
4497 mbedtls_free( ssl->transform_negotiate );
4498 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004499 }
4500
Ronald Cron6f135e12021-12-08 16:57:54 +01004501#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004502 mbedtls_ssl_transform_free( ssl->transform_application );
4503 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004504#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004505
Paul Bakkerc0463502013-02-14 11:19:38 +01004506 if( ssl->session )
4507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004508 mbedtls_ssl_session_free( ssl->session );
4509 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004510 }
4511
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004512#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004513 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004514 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004515 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004516 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004517 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004518#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004519
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004520#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004521 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004522#endif
4523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004525
Paul Bakker86f04f42013-02-14 11:20:09 +01004526 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004527 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004528}
4529
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004530/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004531 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004532 */
4533void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4534{
4535 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4536}
4537
Gilles Peskineae270bf2021-06-02 00:05:29 +02004538/* The selection should be the same as mbedtls_x509_crt_profile_default in
4539 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004540 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004541 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004542 * about this list.
4543 */
Brett Warrene0edc842021-08-17 09:53:13 +01004544static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004545#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004546 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004547#endif
4548#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004549 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004550#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004551#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004552 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004553#endif
4554#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004555 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004556#endif
4557#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004558 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004559#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004560#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004561 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004562#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004563#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004564 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004565#endif
4566#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004567 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004568#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004569 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004570};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004571
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004572static int ssl_preset_suiteb_ciphersuites[] = {
4573 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4574 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4575 0
4576};
4577
Ronald Crone68ab4f2022-10-05 12:46:29 +02004578#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004579
Jerry Yu909df7b2022-01-22 11:56:27 +08004580/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004581 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004582 * rules SHOULD be upheld.
4583 * - No duplicate entries.
4584 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004585 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004586 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004587 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004588static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004589
Andrzej Kurek25f27152022-08-17 16:09:31 -04004590#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 +08004591 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4592 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004593#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004594 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004595
Andrzej Kurek25f27152022-08-17 16:09:31 -04004596#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 +08004597 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4598 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004599#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004600 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4601
Andrzej Kurek25f27152022-08-17 16:09:31 -04004602#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 +08004603 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4604 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004605#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004606 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004607
Andrzej Kurek25f27152022-08-17 16:09:31 -04004608#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 +08004609 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004610#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 +08004611
Andrzej Kurek25f27152022-08-17 16:09:31 -04004612#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 +08004613 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004614#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 +08004615
Andrzej Kurek25f27152022-08-17 16:09:31 -04004616#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 +08004617 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004618#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 +08004619
Andrzej Kurek25f27152022-08-17 16:09:31 -04004620#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 +08004621 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4622#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4623
Andrzej Kurek25f27152022-08-17 16:09:31 -04004624#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 +08004625 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4626#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4627
Andrzej Kurek25f27152022-08-17 16:09:31 -04004628#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 +08004629 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4630#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4631
Gabor Mezei15b95a62022-05-09 16:37:58 +02004632 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004633};
4634
4635/* NOTICE: see above */
4636#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4637static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004638#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004639#if defined(MBEDTLS_ECDSA_C)
4640 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004641#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004642#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4643 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4644#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004645#if defined(MBEDTLS_RSA_C)
4646 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4647#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004648#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004649#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004650#if defined(MBEDTLS_ECDSA_C)
4651 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004652#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004653#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4654 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4655#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004656#if defined(MBEDTLS_RSA_C)
4657 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4658#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004659#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004660#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004661#if defined(MBEDTLS_ECDSA_C)
4662 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004663#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004664#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4665 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4666#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004667#if defined(MBEDTLS_RSA_C)
4668 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4669#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004670#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004671 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004672};
4673#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4674/* NOTICE: see above */
4675static uint16_t ssl_preset_suiteb_sig_algs[] = {
4676
Andrzej Kurek25f27152022-08-17 16:09:31 -04004677#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 +08004678 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4679 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004680#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004681 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4682
Andrzej Kurek25f27152022-08-17 16:09:31 -04004683#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 +08004684 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4685 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004686#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004687 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004688
Andrzej Kurek25f27152022-08-17 16:09:31 -04004689#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 +08004690 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004691#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 +08004692
Andrzej Kurek25f27152022-08-17 16:09:31 -04004693#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 +08004694 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004695#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004696
Gabor Mezei15b95a62022-05-09 16:37:58 +02004697 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004698};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004699
Jerry Yu909df7b2022-01-22 11:56:27 +08004700/* NOTICE: see above */
4701#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4702static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004703#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004704#if defined(MBEDTLS_ECDSA_C)
4705 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004706#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004707#if defined(MBEDTLS_RSA_C)
4708 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4709#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004710#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004711#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004712#if defined(MBEDTLS_ECDSA_C)
4713 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004714#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004715#if defined(MBEDTLS_RSA_C)
4716 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4717#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004718#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004719 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004720};
Jerry Yu909df7b2022-01-22 11:56:27 +08004721#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4722
Ronald Crone68ab4f2022-10-05 12:46:29 +02004723#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004724
Brett Warrene0edc842021-08-17 09:53:13 +01004725static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004726#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004727 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004728#endif
4729#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004730 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004731#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004732 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004733};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004734
Ronald Crone68ab4f2022-10-05 12:46:29 +02004735#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004736/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004737 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004738MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004739static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004740{
4741 size_t i, j;
4742 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004743
Gabor Mezei15b95a62022-05-09 16:37:58 +02004744 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004745 {
Jerry Yuf377d642022-01-25 10:43:59 +08004746 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004747 {
Jerry Yuf377d642022-01-25 10:43:59 +08004748 if( sig_algs[i] != sig_algs[j] )
4749 continue;
4750 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4751 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4752 sig_algs[i], j, i );
4753 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004754 }
4755 }
4756 return( ret );
4757}
4758
Ronald Crone68ab4f2022-10-05 12:46:29 +02004759#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004760
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004761/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004762 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004763 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004764int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004765 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004766{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004767#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004768 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004769#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004770
Ronald Crone68ab4f2022-10-05 12:46:29 +02004771#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004772 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004773 {
4774 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4775 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4776 }
4777
Jerry Yuf377d642022-01-25 10:43:59 +08004778 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004779 {
4780 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4781 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4782 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004783
4784#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004785 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004786 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004787 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004788 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4789 }
4790
Jerry Yuf377d642022-01-25 10:43:59 +08004791 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004792 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004793 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004794 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4795 }
4796#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004797#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004798
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004799 /* Use the functions here so that they are covered in tests,
4800 * but otherwise access member directly for efficiency */
4801 mbedtls_ssl_conf_endpoint( conf, endpoint );
4802 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004803
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004804 /*
4805 * Things that are common to all presets
4806 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004807#if defined(MBEDTLS_SSL_CLI_C)
4808 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4809 {
4810 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4811#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4812 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4813#endif
4814 }
4815#endif
4816
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004817#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4818 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4819#endif
4820
4821#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4822 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4823#endif
4824
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004825#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004826 conf->f_cookie_write = ssl_cookie_write_dummy;
4827 conf->f_cookie_check = ssl_cookie_check_dummy;
4828#endif
4829
4830#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4831 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4832#endif
4833
Janos Follath088ce432017-04-10 12:42:31 +01004834#if defined(MBEDTLS_SSL_SRV_C)
4835 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004836 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004837#endif
4838
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004839#if defined(MBEDTLS_SSL_PROTO_DTLS)
4840 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4841 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4842#endif
4843
4844#if defined(MBEDTLS_SSL_RENEGOTIATION)
4845 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004846 memset( conf->renego_period, 0x00, 2 );
4847 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004848#endif
4849
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004850#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004851 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4852 {
4853 const unsigned char dhm_p[] =
4854 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4855 const unsigned char dhm_g[] =
4856 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004857
Hanno Beckere2defad2021-07-24 05:59:17 +01004858 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4859 dhm_p, sizeof( dhm_p ),
4860 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4861 {
4862 return( ret );
4863 }
4864 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004865#endif
4866
Ronald Cron6f135e12021-12-08 16:57:54 +01004867#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004868#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004869 mbedtls_ssl_conf_new_session_tickets(
4870 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4871#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004872 /*
4873 * Allow all TLS 1.3 key exchange modes by default.
4874 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004875 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004876#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004877
XiaokangQian4d3a6042022-04-21 13:46:17 +00004878 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4879 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004880#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004881 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004882 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004883#else
XiaokangQian060d8672022-04-21 09:24:56 +00004884 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004885#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004886 }
4887 else
4888 {
4889#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4890 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4891 {
4892 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4893 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4894 }
4895 else
4896 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4897 {
4898 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4899 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4900 }
4901#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4902 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4903 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4904#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4905 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4906 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4907#else
4908 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4909#endif
4910 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004911
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004912 /*
4913 * Preset-specific defaults
4914 */
4915 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004916 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004917 /*
4918 * NSA Suite B
4919 */
4920 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004921
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004922 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004923
4924#if defined(MBEDTLS_X509_CRT_PARSE_C)
4925 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004926#endif
4927
Ronald Crone68ab4f2022-10-05 12:46:29 +02004928#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004929#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4930 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4931 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4932 else
4933#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4934 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02004935#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004936
Brett Warrene0edc842021-08-17 09:53:13 +01004937#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4938 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004939#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004940 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004941 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004942
4943 /*
4944 * Default
4945 */
4946 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004947
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004948 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004949
4950#if defined(MBEDTLS_X509_CRT_PARSE_C)
4951 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4952#endif
4953
Ronald Crone68ab4f2022-10-05 12:46:29 +02004954#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004955#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4956 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4957 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4958 else
4959#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4960 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02004961#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004962
Brett Warrene0edc842021-08-17 09:53:13 +01004963#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4964 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004965#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004966 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004967
4968#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4969 conf->dhm_min_bitlen = 1024;
4970#endif
4971 }
4972
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004973 return( 0 );
4974}
4975
4976/*
4977 * Free mbedtls_ssl_config
4978 */
4979void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4980{
4981#if defined(MBEDTLS_DHM_C)
4982 mbedtls_mpi_free( &conf->dhm_P );
4983 mbedtls_mpi_free( &conf->dhm_G );
4984#endif
4985
Ronald Cron73fe8df2022-10-05 14:31:43 +02004986#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004987#if defined(MBEDTLS_USE_PSA_CRYPTO)
4988 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4989 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004990 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4991 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004992#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004993 if( conf->psk != NULL )
4994 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004995 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004996 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004997 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004998 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004999 }
5000
5001 if( conf->psk_identity != NULL )
5002 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005003 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09005004 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00005005 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005006 conf->psk_identity_len = 0;
5007 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005008#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005009
5010#if defined(MBEDTLS_X509_CRT_PARSE_C)
5011 ssl_key_cert_free( conf->key_cert );
5012#endif
5013
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005014 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005015}
5016
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005017#if defined(MBEDTLS_PK_C) && \
5018 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005019/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005020 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005022unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005023{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005024#if defined(MBEDTLS_RSA_C)
5025 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
5026 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005027#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005028#if defined(MBEDTLS_ECDSA_C)
5029 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
5030 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005031#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005032 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005033}
5034
Hanno Becker7e5437a2017-04-28 17:15:26 +01005035unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
5036{
5037 switch( type ) {
5038 case MBEDTLS_PK_RSA:
5039 return( MBEDTLS_SSL_SIG_RSA );
5040 case MBEDTLS_PK_ECDSA:
5041 case MBEDTLS_PK_ECKEY:
5042 return( MBEDTLS_SSL_SIG_ECDSA );
5043 default:
5044 return( MBEDTLS_SSL_SIG_ANON );
5045 }
5046}
5047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005048mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005049{
5050 switch( sig )
5051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005052#if defined(MBEDTLS_RSA_C)
5053 case MBEDTLS_SSL_SIG_RSA:
5054 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005055#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005056#if defined(MBEDTLS_ECDSA_C)
5057 case MBEDTLS_SSL_SIG_ECDSA:
5058 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005059#endif
5060 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005061 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005062 }
5063}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005064#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005065
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005066/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005067 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005068 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005069mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005070{
5071 switch( hash )
5072 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005073#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005074 case MBEDTLS_SSL_HASH_MD5:
5075 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005076#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005077#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005078 case MBEDTLS_SSL_HASH_SHA1:
5079 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005080#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005081#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005082 case MBEDTLS_SSL_HASH_SHA224:
5083 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005084#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005085#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005086 case MBEDTLS_SSL_HASH_SHA256:
5087 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005088#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005089#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005090 case MBEDTLS_SSL_HASH_SHA384:
5091 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005092#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005093#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005094 case MBEDTLS_SSL_HASH_SHA512:
5095 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005096#endif
5097 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005098 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005099 }
5100}
5101
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005102/*
5103 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5104 */
5105unsigned char mbedtls_ssl_hash_from_md_alg( int md )
5106{
5107 switch( md )
5108 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005109#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005110 case MBEDTLS_MD_MD5:
5111 return( MBEDTLS_SSL_HASH_MD5 );
5112#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005113#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005114 case MBEDTLS_MD_SHA1:
5115 return( MBEDTLS_SSL_HASH_SHA1 );
5116#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005117#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005118 case MBEDTLS_MD_SHA224:
5119 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005120#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005121#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005122 case MBEDTLS_MD_SHA256:
5123 return( MBEDTLS_SSL_HASH_SHA256 );
5124#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005125#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005126 case MBEDTLS_MD_SHA384:
5127 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005128#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005129#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005130 case MBEDTLS_MD_SHA512:
5131 return( MBEDTLS_SSL_HASH_SHA512 );
5132#endif
5133 default:
5134 return( MBEDTLS_SSL_HASH_NONE );
5135 }
5136}
5137
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005138/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005139 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005140 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005141 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005142int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005143{
Brett Warrene0edc842021-08-17 09:53:13 +01005144 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005145
Brett Warrene0edc842021-08-17 09:53:13 +01005146 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005147 return( -1 );
5148
Brett Warrene0edc842021-08-17 09:53:13 +01005149 for( ; *group_list != 0; group_list++ )
5150 {
5151 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005152 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005153 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005154
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005155 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005156}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005157
5158#if defined(MBEDTLS_ECP_C)
5159/*
5160 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5161 */
5162int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5163{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005164 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005165 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005166
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005167 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005168 return -1;
5169
5170 uint16_t tls_id = grp_info->tls_id;
5171
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005172 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5173}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005174#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005176#if defined(MBEDTLS_X509_CRT_PARSE_C)
5177int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5178 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005179 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005180 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005181{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005182 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005183 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005184 const char *ext_oid;
5185 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005187 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005188 {
5189 /* Server part of the key exchange */
5190 switch( ciphersuite->key_exchange )
5191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005192 case MBEDTLS_KEY_EXCHANGE_RSA:
5193 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005194 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005195 break;
5196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005197 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5198 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5199 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5200 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005201 break;
5202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005203 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5204 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005205 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005206 break;
5207
5208 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005209 case MBEDTLS_KEY_EXCHANGE_NONE:
5210 case MBEDTLS_KEY_EXCHANGE_PSK:
5211 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5212 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005213 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005214 usage = 0;
5215 }
5216 }
5217 else
5218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005219 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5220 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005221 }
5222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005223 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005224 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005225 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005226 ret = -1;
5227 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005229 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005231 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5232 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005233 }
5234 else
5235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005236 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5237 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005238 }
5239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005240 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005241 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005242 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005243 ret = -1;
5244 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005245
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005246 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005247}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005248#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005249
Jerry Yu148165c2021-09-24 23:20:59 +08005250#if defined(MBEDTLS_USE_PSA_CRYPTO)
5251int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5252 const mbedtls_md_type_t md,
5253 unsigned char *dst,
5254 size_t dst_len,
5255 size_t *olen )
5256{
Ronald Cronf6893e12022-01-07 22:09:01 +01005257 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5258 psa_hash_operation_t *hash_operation_to_clone;
5259 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5260
Jerry Yu148165c2021-09-24 23:20:59 +08005261 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005262
5263 switch( md )
5264 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005265#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005266 case MBEDTLS_MD_SHA384:
5267 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5268 break;
5269#endif
5270
Andrzej Kurek25f27152022-08-17 16:09:31 -04005271#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005272 case MBEDTLS_MD_SHA256:
5273 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5274 break;
5275#endif
5276
5277 default:
5278 goto exit;
5279 }
5280
5281 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5282 if( status != PSA_SUCCESS )
5283 goto exit;
5284
5285 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5286 if( status != PSA_SUCCESS )
5287 goto exit;
5288
5289exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005290#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5291 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5292 (void) ssl;
5293#endif
Ronald Cronb788c042022-02-15 09:14:15 +01005294 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005295}
5296#else /* MBEDTLS_USE_PSA_CRYPTO */
5297
Andrzej Kurek25f27152022-08-17 16:09:31 -04005298#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005299MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005300static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5301 unsigned char *dst,
5302 size_t dst_len,
5303 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005304{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005305 int ret;
5306 mbedtls_sha512_context sha512;
5307
5308 if( dst_len < 48 )
5309 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5310
5311 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005312 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005313
5314 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5315 {
5316 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5317 goto exit;
5318 }
5319
5320 *olen = 48;
5321
5322exit:
5323
5324 mbedtls_sha512_free( &sha512 );
5325 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005326}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005327#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005328
Andrzej Kurek25f27152022-08-17 16:09:31 -04005329#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005330MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005331static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5332 unsigned char *dst,
5333 size_t dst_len,
5334 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005335{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005336 int ret;
5337 mbedtls_sha256_context sha256;
5338
5339 if( dst_len < 32 )
5340 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5341
5342 mbedtls_sha256_init( &sha256 );
5343 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005344
Jerry Yu24c0ec32021-09-09 14:21:07 +08005345 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5346 {
5347 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5348 goto exit;
5349 }
5350
5351 *olen = 32;
5352
5353exit:
5354
5355 mbedtls_sha256_free( &sha256 );
5356 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005357}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005358#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005359
Jerry Yu000f9762021-09-14 11:12:51 +08005360int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5361 const mbedtls_md_type_t md,
5362 unsigned char *dst,
5363 size_t dst_len,
5364 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005365{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005366 switch( md )
5367 {
5368
Andrzej Kurek25f27152022-08-17 16:09:31 -04005369#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005370 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005371 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005372#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005373
Andrzej Kurek25f27152022-08-17 16:09:31 -04005374#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005375 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005376 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005377#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005378
5379 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005380#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5381 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5382 (void) ssl;
5383 (void) dst;
5384 (void) dst_len;
5385 (void) olen;
5386#endif
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005387 break;
5388 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005389 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005390}
XiaokangQian647719a2021-12-07 09:16:29 +00005391
Jerry Yu148165c2021-09-24 23:20:59 +08005392#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005393
Ronald Crone68ab4f2022-10-05 12:46:29 +02005394#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005395/* mbedtls_ssl_parse_sig_alg_ext()
5396 *
5397 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5398 * value (TLS 1.3 RFC8446):
5399 * enum {
5400 * ....
5401 * ecdsa_secp256r1_sha256( 0x0403 ),
5402 * ecdsa_secp384r1_sha384( 0x0503 ),
5403 * ecdsa_secp521r1_sha512( 0x0603 ),
5404 * ....
5405 * } SignatureScheme;
5406 *
5407 * struct {
5408 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5409 * } SignatureSchemeList;
5410 *
5411 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5412 * value (TLS 1.2 RFC5246):
5413 * enum {
5414 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5415 * sha512(6), (255)
5416 * } HashAlgorithm;
5417 *
5418 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5419 * SignatureAlgorithm;
5420 *
5421 * struct {
5422 * HashAlgorithm hash;
5423 * SignatureAlgorithm signature;
5424 * } SignatureAndHashAlgorithm;
5425 *
5426 * SignatureAndHashAlgorithm
5427 * supported_signature_algorithms<2..2^16-2>;
5428 *
5429 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5430 * generalization of the TLS 1.2 signature algorithm extension.
5431 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5432 * `SignatureScheme` field of TLS 1.3
5433 *
5434 */
5435int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5436 const unsigned char *buf,
5437 const unsigned char *end )
5438{
5439 const unsigned char *p = buf;
5440 size_t supported_sig_algs_len = 0;
5441 const unsigned char *supported_sig_algs_end;
5442 uint16_t sig_alg;
5443 uint32_t common_idx = 0;
5444
5445 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5446 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5447 p += 2;
5448
5449 memset( ssl->handshake->received_sig_algs, 0,
5450 sizeof(ssl->handshake->received_sig_algs) );
5451
5452 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5453 supported_sig_algs_end = p + supported_sig_algs_len;
5454 while( p < supported_sig_algs_end )
5455 {
5456 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5457 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5458 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005459 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5460 sig_alg,
5461 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005462#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005463 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005464 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5465 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5466 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005467 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005468 }
5469#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005470
Jerry Yuf3b46b52022-06-19 16:52:27 +08005471 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5472 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005473
5474 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5475 {
5476 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5477 common_idx += 1;
5478 }
5479 }
5480 /* Check that we consumed all the message. */
5481 if( p != end )
5482 {
5483 MBEDTLS_SSL_DEBUG_MSG( 1,
5484 ( "Signature algorithms extension length misaligned" ) );
5485 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5486 MBEDTLS_ERR_SSL_DECODE_ERROR );
5487 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5488 }
5489
5490 if( common_idx == 0 )
5491 {
5492 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5493 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5494 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5495 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5496 }
5497
Gabor Mezei15b95a62022-05-09 16:37:58 +02005498 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005499 return( 0 );
5500}
5501
Ronald Crone68ab4f2022-10-05 12:46:29 +02005502#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005503
Jerry Yudc7bd172022-02-17 13:44:15 +08005504#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5505
5506#if defined(MBEDTLS_USE_PSA_CRYPTO)
5507
5508static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5509 mbedtls_svc_key_id_t key,
5510 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005511 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005512 const unsigned char* seed, size_t seed_length,
5513 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005514 const unsigned char* other_secret,
5515 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005516 size_t capacity )
5517{
5518 psa_status_t status;
5519
5520 status = psa_key_derivation_setup( derivation, alg );
5521 if( status != PSA_SUCCESS )
5522 return( status );
5523
5524 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5525 {
5526 status = psa_key_derivation_input_bytes( derivation,
5527 PSA_KEY_DERIVATION_INPUT_SEED,
5528 seed, seed_length );
5529 if( status != PSA_SUCCESS )
5530 return( status );
5531
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005532 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005533 {
5534 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005535 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5536 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005537 if( status != PSA_SUCCESS )
5538 return( status );
5539 }
5540
Jerry Yudc7bd172022-02-17 13:44:15 +08005541 if( mbedtls_svc_key_id_is_null( key ) )
5542 {
5543 status = psa_key_derivation_input_bytes(
5544 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005545 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005546 }
5547 else
5548 {
5549 status = psa_key_derivation_input_key(
5550 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5551 }
5552 if( status != PSA_SUCCESS )
5553 return( status );
5554
5555 status = psa_key_derivation_input_bytes( derivation,
5556 PSA_KEY_DERIVATION_INPUT_LABEL,
5557 label, label_length );
5558 if( status != PSA_SUCCESS )
5559 return( status );
5560 }
5561 else
5562 {
5563 return( PSA_ERROR_NOT_SUPPORTED );
5564 }
5565
5566 status = psa_key_derivation_set_capacity( derivation, capacity );
5567 if( status != PSA_SUCCESS )
5568 return( status );
5569
5570 return( PSA_SUCCESS );
5571}
5572
Andrzej Kurek57d10632022-10-24 10:32:01 -04005573#if defined(PSA_WANT_ALG_SHA_384) || \
5574 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005575MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005576static int tls_prf_generic( mbedtls_md_type_t md_type,
5577 const unsigned char *secret, size_t slen,
5578 const char *label,
5579 const unsigned char *random, size_t rlen,
5580 unsigned char *dstbuf, size_t dlen )
5581{
5582 psa_status_t status;
5583 psa_algorithm_t alg;
5584 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5585 psa_key_derivation_operation_t derivation =
5586 PSA_KEY_DERIVATION_OPERATION_INIT;
5587
5588 if( md_type == MBEDTLS_MD_SHA384 )
5589 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5590 else
5591 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5592
5593 /* Normally a "secret" should be long enough to be impossible to
5594 * find by brute force, and in particular should not be empty. But
5595 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5596 * and for this use case it makes sense to have a 0-length "secret".
5597 * Since the key API doesn't allow importing a key of length 0,
5598 * keep master_key=0, which setup_psa_key_derivation() understands
5599 * to mean a 0-length "secret" input. */
5600 if( slen != 0 )
5601 {
5602 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5603 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5604 psa_set_key_algorithm( &key_attributes, alg );
5605 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5606
5607 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5608 if( status != PSA_SUCCESS )
5609 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5610 }
5611
5612 status = setup_psa_key_derivation( &derivation,
5613 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005614 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005615 random, rlen,
5616 (unsigned char const *) label,
5617 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005618 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005619 dlen );
5620 if( status != PSA_SUCCESS )
5621 {
5622 psa_key_derivation_abort( &derivation );
5623 psa_destroy_key( master_key );
5624 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5625 }
5626
5627 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5628 if( status != PSA_SUCCESS )
5629 {
5630 psa_key_derivation_abort( &derivation );
5631 psa_destroy_key( master_key );
5632 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5633 }
5634
5635 status = psa_key_derivation_abort( &derivation );
5636 if( status != PSA_SUCCESS )
5637 {
5638 psa_destroy_key( master_key );
5639 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5640 }
5641
5642 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5643 status = psa_destroy_key( master_key );
5644 if( status != PSA_SUCCESS )
5645 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5646
5647 return( 0 );
5648}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005649#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08005650#else /* MBEDTLS_USE_PSA_CRYPTO */
5651
Andrzej Kurek57d10632022-10-24 10:32:01 -04005652#if defined(MBEDTLS_MD_C) && \
5653 ( defined(MBEDTLS_SHA256_C) || \
5654 defined(MBEDTLS_SHA384_C) )
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005655MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005656static int tls_prf_generic( mbedtls_md_type_t md_type,
5657 const unsigned char *secret, size_t slen,
5658 const char *label,
5659 const unsigned char *random, size_t rlen,
5660 unsigned char *dstbuf, size_t dlen )
5661{
5662 size_t nb;
5663 size_t i, j, k, md_len;
5664 unsigned char *tmp;
5665 size_t tmp_len = 0;
5666 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5667 const mbedtls_md_info_t *md_info;
5668 mbedtls_md_context_t md_ctx;
5669 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5670
5671 mbedtls_md_init( &md_ctx );
5672
5673 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5674 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5675
5676 md_len = mbedtls_md_get_size( md_info );
5677
5678 tmp_len = md_len + strlen( label ) + rlen;
5679 tmp = mbedtls_calloc( 1, tmp_len );
5680 if( tmp == NULL )
5681 {
5682 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5683 goto exit;
5684 }
5685
5686 nb = strlen( label );
5687 memcpy( tmp + md_len, label, nb );
5688 memcpy( tmp + md_len + nb, random, rlen );
5689 nb += rlen;
5690
5691 /*
5692 * Compute P_<hash>(secret, label + random)[0..dlen]
5693 */
5694 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5695 goto exit;
5696
5697 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5698 if( ret != 0 )
5699 goto exit;
5700 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5701 if( ret != 0 )
5702 goto exit;
5703 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5704 if( ret != 0 )
5705 goto exit;
5706
5707 for( i = 0; i < dlen; i += md_len )
5708 {
5709 ret = mbedtls_md_hmac_reset ( &md_ctx );
5710 if( ret != 0 )
5711 goto exit;
5712 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5713 if( ret != 0 )
5714 goto exit;
5715 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5716 if( ret != 0 )
5717 goto exit;
5718
5719 ret = mbedtls_md_hmac_reset ( &md_ctx );
5720 if( ret != 0 )
5721 goto exit;
5722 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5723 if( ret != 0 )
5724 goto exit;
5725 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5726 if( ret != 0 )
5727 goto exit;
5728
5729 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5730
5731 for( j = 0; j < k; j++ )
5732 dstbuf[i + j] = h_i[j];
5733 }
5734
5735exit:
5736 mbedtls_md_free( &md_ctx );
5737
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00005738 if ( tmp != NULL )
5739 mbedtls_platform_zeroize( tmp, tmp_len );
5740
Jerry Yudc7bd172022-02-17 13:44:15 +08005741 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5742
5743 mbedtls_free( tmp );
5744
5745 return( ret );
5746}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005747#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08005748#endif /* MBEDTLS_USE_PSA_CRYPTO */
5749
Andrzej Kurek25f27152022-08-17 16:09:31 -04005750#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005751MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005752static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5753 const char *label,
5754 const unsigned char *random, size_t rlen,
5755 unsigned char *dstbuf, size_t dlen )
5756{
5757 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5758 label, random, rlen, dstbuf, dlen ) );
5759}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005760#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005761
Andrzej Kurek25f27152022-08-17 16:09:31 -04005762#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005763MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005764static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5765 const char *label,
5766 const unsigned char *random, size_t rlen,
5767 unsigned char *dstbuf, size_t dlen )
5768{
5769 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5770 label, random, rlen, dstbuf, dlen ) );
5771}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005772#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005773
Jerry Yuf009d862022-02-17 14:01:37 +08005774/*
5775 * Set appropriate PRF function and other SSL / TLS1.2 functions
5776 *
5777 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005778 * - hash associated with the ciphersuite (only used by TLS 1.2)
5779 *
5780 * Outputs:
5781 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5782 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005783MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005784static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005785 mbedtls_md_type_t hash )
5786{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005787#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005788 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005789 {
5790 handshake->tls_prf = tls_prf_sha384;
5791 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5792 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5793 }
5794 else
5795#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005796#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005797 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005798 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005799 handshake->tls_prf = tls_prf_sha256;
5800 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5801 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5802 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005803#else
Jerry Yuf009d862022-02-17 14:01:37 +08005804 {
Jerry Yuf009d862022-02-17 14:01:37 +08005805 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005806 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005807 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5808 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005809#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005810
5811 return( 0 );
5812}
Jerry Yud6ab2352022-02-17 14:03:43 +08005813
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005814/*
5815 * Compute master secret if needed
5816 *
5817 * Parameters:
5818 * [in/out] handshake
5819 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5820 * (PSA-PSK) ciphersuite_info, psk_opaque
5821 * [out] premaster (cleared)
5822 * [out] master
5823 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5824 * debug: conf->f_dbg, conf->p_dbg
5825 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005826 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005827 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005828MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005829static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5830 unsigned char *master,
5831 const mbedtls_ssl_context *ssl )
5832{
5833 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5834
5835 /* cf. RFC 5246, Section 8.1:
5836 * "The master secret is always exactly 48 bytes in length." */
5837 size_t const master_secret_len = 48;
5838
5839#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5840 unsigned char session_hash[48];
5841#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5842
5843 /* The label for the KDF used for key expansion.
5844 * This is either "master secret" or "extended master secret"
5845 * depending on whether the Extended Master Secret extension
5846 * is used. */
5847 char const *lbl = "master secret";
5848
Przemek Stekielae4ed302022-04-05 17:15:55 +02005849 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005850 * - If the Extended Master Secret extension is not used,
5851 * this is ClientHello.Random + ServerHello.Random
5852 * (see Sect. 8.1 in RFC 5246).
5853 * - If the Extended Master Secret extension is used,
5854 * this is the transcript of the handshake so far.
5855 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005856 unsigned char const *seed = handshake->randbytes;
5857 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005858
5859#if !defined(MBEDTLS_DEBUG_C) && \
5860 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5861 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5862 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5863 ssl = NULL; /* make sure we don't use it except for those cases */
5864 (void) ssl;
5865#endif
5866
5867 if( handshake->resume != 0 )
5868 {
5869 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5870 return( 0 );
5871 }
5872
5873#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5874 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5875 {
5876 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005877 seed = session_hash;
5878 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005879
5880 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005881 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005882 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005883#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005884
Przemek Stekiel99114f32022-04-22 11:20:09 +02005885#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005886 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005887 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005888 {
5889 /* Perform PSK-to-MS expansion in a single step. */
5890 psa_status_t status;
5891 psa_algorithm_t alg;
5892 mbedtls_svc_key_id_t psk;
5893 psa_key_derivation_operation_t derivation =
5894 PSA_KEY_DERIVATION_OPERATION_INIT;
5895 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5896
5897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5898
5899 psk = mbedtls_ssl_get_opaque_psk( ssl );
5900
5901 if( hash_alg == MBEDTLS_MD_SHA384 )
5902 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5903 else
5904 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5905
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005906 size_t other_secret_len = 0;
5907 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005908
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005909 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005910 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005911 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005912 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005913 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005914 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005915 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005916 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005917 other_secret_len = 48;
5918 other_secret = handshake->premaster + 2;
5919 break;
5920 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005921 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005922 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5923 other_secret = handshake->premaster + 2;
5924 break;
5925 default:
5926 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005927 }
5928
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005929 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005930 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005931 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005932 (unsigned char const *) lbl,
5933 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005934 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005935 master_secret_len );
5936 if( status != PSA_SUCCESS )
5937 {
5938 psa_key_derivation_abort( &derivation );
5939 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5940 }
5941
5942 status = psa_key_derivation_output_bytes( &derivation,
5943 master,
5944 master_secret_len );
5945 if( status != PSA_SUCCESS )
5946 {
5947 psa_key_derivation_abort( &derivation );
5948 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5949 }
5950
5951 status = psa_key_derivation_abort( &derivation );
5952 if( status != PSA_SUCCESS )
5953 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5954 }
5955 else
5956#endif
5957 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02005958#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
5959 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
5960 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
5961 {
5962 psa_status_t status;
5963 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
5964 psa_key_derivation_operation_t derivation =
5965 PSA_KEY_DERIVATION_OPERATION_INIT;
5966
5967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PMS KDF for ECJPAKE" ) );
5968
5969 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
5970
5971 status = psa_key_derivation_setup( &derivation, alg );
5972 if( status != PSA_SUCCESS )
5973 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5974
5975 status = psa_key_derivation_set_capacity( &derivation,
5976 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE );
5977 if( status != PSA_SUCCESS )
5978 {
5979 psa_key_derivation_abort( &derivation );
5980 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5981 }
5982
5983 status = psa_pake_get_implicit_key( &handshake->psa_pake_ctx,
5984 &derivation );
5985 if( status != PSA_SUCCESS )
5986 {
5987 psa_key_derivation_abort( &derivation );
5988 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5989 }
5990
5991 status = psa_key_derivation_output_bytes( &derivation,
5992 handshake->premaster,
5993 handshake->pmslen );
5994 if( status != PSA_SUCCESS )
5995 {
5996 psa_key_derivation_abort( &derivation );
5997 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5998 }
5999
6000 status = psa_key_derivation_abort( &derivation );
6001 if( status != PSA_SUCCESS )
6002 {
6003 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6004 }
6005 }
6006#endif
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006007 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006008 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006009 master,
6010 master_secret_len );
6011 if( ret != 0 )
6012 {
6013 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
6014 return( ret );
6015 }
6016
6017 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
6018 handshake->premaster,
6019 handshake->pmslen );
6020
6021 mbedtls_platform_zeroize( handshake->premaster,
6022 sizeof(handshake->premaster) );
6023 }
6024
6025 return( 0 );
6026}
6027
Jerry Yud62f87e2022-02-17 14:09:02 +08006028int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
6029{
6030 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6031 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6032 ssl->handshake->ciphersuite_info;
6033
6034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
6035
6036 /* Set PRF, calc_verify and calc_finished function pointers */
6037 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08006038 ciphersuite_info->mac );
6039 if( ret != 0 )
6040 {
6041 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
6042 return( ret );
6043 }
6044
Neil Armstrongca7d5062022-05-31 14:43:23 +02006045
Jerry Yud62f87e2022-02-17 14:09:02 +08006046 /* Compute master secret if needed */
6047 ret = ssl_compute_master( ssl->handshake,
6048 ssl->session_negotiate->master,
6049 ssl );
6050 if( ret != 0 )
6051 {
6052 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
6053 return( ret );
6054 }
6055
6056 /* Swap the client and server random values:
6057 * - MS derivation wanted client+server (RFC 5246 8.1)
6058 * - key derivation wants server+client (RFC 5246 6.3) */
6059 {
6060 unsigned char tmp[64];
6061 memcpy( tmp, ssl->handshake->randbytes, 64 );
6062 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
6063 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
6064 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
6065 }
6066
6067 /* Populate transform structure */
6068 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
6069 ssl->session_negotiate->ciphersuite,
6070 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006071#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08006072 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006073#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08006074 ssl->handshake->tls_prf,
6075 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04006076 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08006077 ssl->conf->endpoint,
6078 ssl );
6079 if( ret != 0 )
6080 {
6081 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
6082 return( ret );
6083 }
6084
6085 /* We no longer need Server/ClientHello.random values */
6086 mbedtls_platform_zeroize( ssl->handshake->randbytes,
6087 sizeof( ssl->handshake->randbytes ) );
6088
6089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
6090
6091 return( 0 );
6092}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006093
Ronald Cron4dcbca92022-03-07 10:21:40 +01006094int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
6095{
Ronald Cron4dcbca92022-03-07 10:21:40 +01006096 switch( md )
6097 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006098#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006099 case MBEDTLS_SSL_HASH_SHA384:
6100 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6101 break;
6102#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006103#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006104 case MBEDTLS_SSL_HASH_SHA256:
6105 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6106 break;
6107#endif
6108 default:
6109 return( -1 );
6110 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006111#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6112 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6113 (void) ssl;
6114#endif
Ronald Cronc2f13a02022-03-07 10:25:24 +01006115 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01006116}
6117
Andrzej Kurek25f27152022-08-17 16:09:31 -04006118#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006119void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
6120 unsigned char *hash,
6121 size_t *hlen )
6122{
6123#if defined(MBEDTLS_USE_PSA_CRYPTO)
6124 size_t hash_size;
6125 psa_status_t status;
6126 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6127
6128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
6129 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6130 if( status != PSA_SUCCESS )
6131 {
6132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6133 return;
6134 }
6135
6136 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
6137 if( status != PSA_SUCCESS )
6138 {
6139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6140 return;
6141 }
6142
6143 *hlen = 32;
6144 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6146#else
6147 mbedtls_sha256_context sha256;
6148
6149 mbedtls_sha256_init( &sha256 );
6150
6151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
6152
6153 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6154 mbedtls_sha256_finish( &sha256, hash );
6155
6156 *hlen = 32;
6157
6158 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6160
6161 mbedtls_sha256_free( &sha256 );
6162#endif /* MBEDTLS_USE_PSA_CRYPTO */
6163 return;
6164}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006165#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006166
Andrzej Kurek25f27152022-08-17 16:09:31 -04006167#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006168void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
6169 unsigned char *hash,
6170 size_t *hlen )
6171{
6172#if defined(MBEDTLS_USE_PSA_CRYPTO)
6173 size_t hash_size;
6174 psa_status_t status;
6175 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6176
6177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6178 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6179 if( status != PSA_SUCCESS )
6180 {
6181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6182 return;
6183 }
6184
6185 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6186 if( status != PSA_SUCCESS )
6187 {
6188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6189 return;
6190 }
6191
6192 *hlen = 48;
6193 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6195#else
6196 mbedtls_sha512_context sha512;
6197
6198 mbedtls_sha512_init( &sha512 );
6199
6200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6201
Andrzej Kureka242e832022-08-11 10:03:14 -04006202 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006203 mbedtls_sha512_finish( &sha512, hash );
6204
6205 *hlen = 48;
6206
6207 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6209
6210 mbedtls_sha512_free( &sha512 );
6211#endif /* MBEDTLS_USE_PSA_CRYPTO */
6212 return;
6213}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006214#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006215
Neil Armstrong80f6f322022-05-03 17:56:38 +02006216#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6217 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006218int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6219{
6220 unsigned char *p = ssl->handshake->premaster;
6221 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6222 const unsigned char *psk = NULL;
6223 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006224 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006225
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006226 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006227 {
6228 /*
6229 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006230 * checked before calling this function.
6231 *
6232 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006233 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006234 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006235 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6236 {
6237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6238 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6239 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006240 }
6241
6242 /*
6243 * PMS = struct {
6244 * opaque other_secret<0..2^16-1>;
6245 * opaque psk<0..2^16-1>;
6246 * };
6247 * with "other_secret" depending on the particular key exchange
6248 */
6249#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6250 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6251 {
6252 if( end - p < 2 )
6253 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6254
6255 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6256 p += 2;
6257
6258 if( end < p || (size_t)( end - p ) < psk_len )
6259 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6260
6261 memset( p, 0, psk_len );
6262 p += psk_len;
6263 }
6264 else
6265#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6266#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6267 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6268 {
6269 /*
6270 * other_secret already set by the ClientKeyExchange message,
6271 * and is 48 bytes long
6272 */
6273 if( end - p < 2 )
6274 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6275
6276 *p++ = 0;
6277 *p++ = 48;
6278 p += 48;
6279 }
6280 else
6281#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6282#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6283 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6284 {
6285 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6286 size_t len;
6287
6288 /* Write length only when we know the actual value */
6289 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6290 p + 2, end - ( p + 2 ), &len,
6291 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6292 {
6293 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6294 return( ret );
6295 }
6296 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6297 p += 2 + len;
6298
6299 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6300 }
6301 else
6302#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006303#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006304 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6305 {
6306 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6307 size_t zlen;
6308
6309 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6310 p + 2, end - ( p + 2 ),
6311 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6312 {
6313 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6314 return( ret );
6315 }
6316
6317 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6318 p += 2 + zlen;
6319
6320 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6321 MBEDTLS_DEBUG_ECDH_Z );
6322 }
6323 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006324#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006325 {
6326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6327 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6328 }
6329
6330 /* opaque psk<0..2^16-1>; */
6331 if( end - p < 2 )
6332 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6333
6334 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6335 p += 2;
6336
6337 if( end < p || (size_t)( end - p ) < psk_len )
6338 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6339
6340 memcpy( p, psk, psk_len );
6341 p += psk_len;
6342
6343 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6344
6345 return( 0 );
6346}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006347#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006348
6349#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006350MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006351static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6352
6353#if defined(MBEDTLS_SSL_PROTO_DTLS)
6354int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6355{
6356 /* If renegotiation is not enforced, retransmit until we would reach max
6357 * timeout if we were using the usual handshake doubling scheme */
6358 if( ssl->conf->renego_max_records < 0 )
6359 {
6360 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6361 unsigned char doublings = 1;
6362
6363 while( ratio != 0 )
6364 {
6365 ++doublings;
6366 ratio >>= 1;
6367 }
6368
6369 if( ++ssl->renego_records_seen > doublings )
6370 {
6371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6372 return( 0 );
6373 }
6374 }
6375
6376 return( ssl_write_hello_request( ssl ) );
6377}
6378#endif
6379#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006380
Jerry Yud9526692022-02-17 14:23:47 +08006381/*
6382 * Handshake functions
6383 */
6384#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6385/* No certificate support -> dummy functions */
6386int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6387{
6388 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6389 ssl->handshake->ciphersuite_info;
6390
6391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6392
6393 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6394 {
6395 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6396 ssl->state++;
6397 return( 0 );
6398 }
6399
6400 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6401 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6402}
6403
6404int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6405{
6406 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6407 ssl->handshake->ciphersuite_info;
6408
6409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6410
6411 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6412 {
6413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6414 ssl->state++;
6415 return( 0 );
6416 }
6417
6418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6419 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6420}
6421
6422#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6423/* Some certificate support -> implement write and parse */
6424
6425int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6426{
6427 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6428 size_t i, n;
6429 const mbedtls_x509_crt *crt;
6430 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6431 ssl->handshake->ciphersuite_info;
6432
6433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6434
6435 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6436 {
6437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6438 ssl->state++;
6439 return( 0 );
6440 }
6441
6442#if defined(MBEDTLS_SSL_CLI_C)
6443 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6444 {
6445 if( ssl->handshake->client_auth == 0 )
6446 {
6447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6448 ssl->state++;
6449 return( 0 );
6450 }
6451 }
6452#endif /* MBEDTLS_SSL_CLI_C */
6453#if defined(MBEDTLS_SSL_SRV_C)
6454 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6455 {
6456 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6457 {
6458 /* Should never happen because we shouldn't have picked the
6459 * ciphersuite if we don't have a certificate. */
6460 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6461 }
6462 }
6463#endif
6464
6465 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6466
6467 /*
6468 * 0 . 0 handshake type
6469 * 1 . 3 handshake length
6470 * 4 . 6 length of all certs
6471 * 7 . 9 length of cert. 1
6472 * 10 . n-1 peer certificate
6473 * n . n+2 length of cert. 2
6474 * n+3 . ... upper level cert, etc.
6475 */
6476 i = 7;
6477 crt = mbedtls_ssl_own_cert( ssl );
6478
6479 while( crt != NULL )
6480 {
6481 n = crt->raw.len;
6482 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6483 {
6484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6485 " > %" MBEDTLS_PRINTF_SIZET,
6486 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6487 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6488 }
6489
6490 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6491 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6492 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6493
6494 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6495 i += n; crt = crt->next;
6496 }
6497
6498 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6499 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6500 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6501
6502 ssl->out_msglen = i;
6503 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6504 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6505
6506 ssl->state++;
6507
6508 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6509 {
6510 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6511 return( ret );
6512 }
6513
6514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6515
6516 return( ret );
6517}
6518
6519#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6520
6521#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006522MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006523static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6524 unsigned char *crt_buf,
6525 size_t crt_buf_len )
6526{
6527 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6528
6529 if( peer_crt == NULL )
6530 return( -1 );
6531
6532 if( peer_crt->raw.len != crt_buf_len )
6533 return( -1 );
6534
6535 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6536}
6537#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006538MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006539static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6540 unsigned char *crt_buf,
6541 size_t crt_buf_len )
6542{
6543 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6544 unsigned char const * const peer_cert_digest =
6545 ssl->session->peer_cert_digest;
6546 mbedtls_md_type_t const peer_cert_digest_type =
6547 ssl->session->peer_cert_digest_type;
6548 mbedtls_md_info_t const * const digest_info =
6549 mbedtls_md_info_from_type( peer_cert_digest_type );
6550 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6551 size_t digest_len;
6552
6553 if( peer_cert_digest == NULL || digest_info == NULL )
6554 return( -1 );
6555
6556 digest_len = mbedtls_md_get_size( digest_info );
6557 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6558 return( -1 );
6559
6560 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6561 if( ret != 0 )
6562 return( -1 );
6563
6564 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6565}
6566#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6567#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6568
6569/*
6570 * Once the certificate message is read, parse it into a cert chain and
6571 * perform basic checks, but leave actual verification to the caller
6572 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006573MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006574static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6575 mbedtls_x509_crt *chain )
6576{
6577 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6578#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6579 int crt_cnt=0;
6580#endif
6581 size_t i, n;
6582 uint8_t alert;
6583
6584 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6585 {
6586 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6587 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6588 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6589 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6590 }
6591
6592 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6593 {
6594 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6595 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6596 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6597 }
6598
6599 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6600 {
6601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6602 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6603 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6604 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6605 }
6606
6607 i = mbedtls_ssl_hs_hdr_len( ssl );
6608
6609 /*
6610 * Same message structure as in mbedtls_ssl_write_certificate()
6611 */
6612 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6613
6614 if( ssl->in_msg[i] != 0 ||
6615 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6616 {
6617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6618 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6619 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6620 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6621 }
6622
6623 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6624 i += 3;
6625
6626 /* Iterate through and parse the CRTs in the provided chain. */
6627 while( i < ssl->in_hslen )
6628 {
6629 /* Check that there's room for the next CRT's length fields. */
6630 if ( i + 3 > ssl->in_hslen ) {
6631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6632 mbedtls_ssl_send_alert_message( ssl,
6633 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6634 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6635 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6636 }
6637 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6638 * anything beyond 2**16 ~ 64K. */
6639 if( ssl->in_msg[i] != 0 )
6640 {
6641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6642 mbedtls_ssl_send_alert_message( ssl,
6643 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6644 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6645 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6646 }
6647
6648 /* Read length of the next CRT in the chain. */
6649 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6650 | (unsigned int) ssl->in_msg[i + 2];
6651 i += 3;
6652
6653 if( n < 128 || i + n > ssl->in_hslen )
6654 {
6655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6656 mbedtls_ssl_send_alert_message( ssl,
6657 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6658 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6659 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6660 }
6661
6662 /* Check if we're handling the first CRT in the chain. */
6663#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6664 if( crt_cnt++ == 0 &&
6665 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6666 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6667 {
6668 /* During client-side renegotiation, check that the server's
6669 * end-CRTs hasn't changed compared to the initial handshake,
6670 * mitigating the triple handshake attack. On success, reuse
6671 * the original end-CRT instead of parsing it again. */
6672 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6673 if( ssl_check_peer_crt_unchanged( ssl,
6674 &ssl->in_msg[i],
6675 n ) != 0 )
6676 {
6677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6678 mbedtls_ssl_send_alert_message( ssl,
6679 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6680 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6681 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6682 }
6683
6684 /* Now we can safely free the original chain. */
6685 ssl_clear_peer_cert( ssl->session );
6686 }
6687#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6688
6689 /* Parse the next certificate in the chain. */
6690#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6691 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6692#else
6693 /* If we don't need to store the CRT chain permanently, parse
6694 * it in-place from the input buffer instead of making a copy. */
6695 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6696#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6697 switch( ret )
6698 {
6699 case 0: /*ok*/
6700 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6701 /* Ignore certificate with an unknown algorithm: maybe a
6702 prior certificate was already trusted. */
6703 break;
6704
6705 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6706 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6707 goto crt_parse_der_failed;
6708
6709 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6710 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6711 goto crt_parse_der_failed;
6712
6713 default:
6714 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6715 crt_parse_der_failed:
6716 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6717 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6718 return( ret );
6719 }
6720
6721 i += n;
6722 }
6723
6724 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6725 return( 0 );
6726}
6727
6728#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006729MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006730static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6731{
6732 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6733 return( -1 );
6734
6735 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6736 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6737 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6738 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6739 {
Ronald Cron19385882022-06-15 16:26:13 +02006740 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006741 return( 0 );
6742 }
6743 return( -1 );
6744}
6745#endif /* MBEDTLS_SSL_SRV_C */
6746
6747/* Check if a certificate message is expected.
6748 * Return either
6749 * - SSL_CERTIFICATE_EXPECTED, or
6750 * - SSL_CERTIFICATE_SKIP
6751 * indicating whether a Certificate message is expected or not.
6752 */
6753#define SSL_CERTIFICATE_EXPECTED 0
6754#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006755MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006756static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6757 int authmode )
6758{
6759 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6760 ssl->handshake->ciphersuite_info;
6761
6762 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6763 return( SSL_CERTIFICATE_SKIP );
6764
6765#if defined(MBEDTLS_SSL_SRV_C)
6766 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6767 {
6768 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6769 return( SSL_CERTIFICATE_SKIP );
6770
6771 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6772 {
6773 ssl->session_negotiate->verify_result =
6774 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6775 return( SSL_CERTIFICATE_SKIP );
6776 }
6777 }
6778#else
6779 ((void) authmode);
6780#endif /* MBEDTLS_SSL_SRV_C */
6781
6782 return( SSL_CERTIFICATE_EXPECTED );
6783}
6784
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006785MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006786static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6787 int authmode,
6788 mbedtls_x509_crt *chain,
6789 void *rs_ctx )
6790{
6791 int ret = 0;
6792 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6793 ssl->handshake->ciphersuite_info;
6794 int have_ca_chain = 0;
6795
6796 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6797 void *p_vrfy;
6798
6799 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6800 return( 0 );
6801
6802 if( ssl->f_vrfy != NULL )
6803 {
6804 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6805 f_vrfy = ssl->f_vrfy;
6806 p_vrfy = ssl->p_vrfy;
6807 }
6808 else
6809 {
6810 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6811 f_vrfy = ssl->conf->f_vrfy;
6812 p_vrfy = ssl->conf->p_vrfy;
6813 }
6814
6815 /*
6816 * Main check: verify certificate
6817 */
6818#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6819 if( ssl->conf->f_ca_cb != NULL )
6820 {
6821 ((void) rs_ctx);
6822 have_ca_chain = 1;
6823
6824 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6825 ret = mbedtls_x509_crt_verify_with_ca_cb(
6826 chain,
6827 ssl->conf->f_ca_cb,
6828 ssl->conf->p_ca_cb,
6829 ssl->conf->cert_profile,
6830 ssl->hostname,
6831 &ssl->session_negotiate->verify_result,
6832 f_vrfy, p_vrfy );
6833 }
6834 else
6835#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6836 {
6837 mbedtls_x509_crt *ca_chain;
6838 mbedtls_x509_crl *ca_crl;
6839
6840#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6841 if( ssl->handshake->sni_ca_chain != NULL )
6842 {
6843 ca_chain = ssl->handshake->sni_ca_chain;
6844 ca_crl = ssl->handshake->sni_ca_crl;
6845 }
6846 else
6847#endif
6848 {
6849 ca_chain = ssl->conf->ca_chain;
6850 ca_crl = ssl->conf->ca_crl;
6851 }
6852
6853 if( ca_chain != NULL )
6854 have_ca_chain = 1;
6855
6856 ret = mbedtls_x509_crt_verify_restartable(
6857 chain,
6858 ca_chain, ca_crl,
6859 ssl->conf->cert_profile,
6860 ssl->hostname,
6861 &ssl->session_negotiate->verify_result,
6862 f_vrfy, p_vrfy, rs_ctx );
6863 }
6864
6865 if( ret != 0 )
6866 {
6867 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6868 }
6869
6870#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6871 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6872 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6873#endif
6874
6875 /*
6876 * Secondary checks: always done, but change 'ret' only if it was 0
6877 */
6878
6879#if defined(MBEDTLS_ECP_C)
6880 {
6881 const mbedtls_pk_context *pk = &chain->pk;
6882
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006883 /* If certificate uses an EC key, make sure the curve is OK.
6884 * This is a public key, so it can't be opaque, so can_do() is a good
6885 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006886 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006887 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006888 /* and in the unlikely case the above assumption no longer holds
6889 * we are making sure that pk_ec() here does not return a NULL
6890 */
6891 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6892 if( ec == NULL )
6893 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006895 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6896 }
Jerry Yud9526692022-02-17 14:23:47 +08006897
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006898 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6899 {
6900 ssl->session_negotiate->verify_result |=
6901 MBEDTLS_X509_BADCERT_BAD_KEY;
6902
6903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6904 if( ret == 0 )
6905 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6906 }
Jerry Yud9526692022-02-17 14:23:47 +08006907 }
6908 }
6909#endif /* MBEDTLS_ECP_C */
6910
6911 if( mbedtls_ssl_check_cert_usage( chain,
6912 ciphersuite_info,
6913 ! ssl->conf->endpoint,
6914 &ssl->session_negotiate->verify_result ) != 0 )
6915 {
6916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6917 if( ret == 0 )
6918 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6919 }
6920
6921 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6922 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6923 * with details encoded in the verification flags. All other kinds
6924 * of error codes, including those from the user provided f_vrfy
6925 * functions, are treated as fatal and lead to a failure of
6926 * ssl_parse_certificate even if verification was optional. */
6927 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6928 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6929 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6930 {
6931 ret = 0;
6932 }
6933
6934 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6935 {
6936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6937 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6938 }
6939
6940 if( ret != 0 )
6941 {
6942 uint8_t alert;
6943
6944 /* The certificate may have been rejected for several reasons.
6945 Pick one and send the corresponding alert. Which alert to send
6946 may be a subject of debate in some cases. */
6947 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6948 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6949 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6950 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6951 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6952 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6953 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6954 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6955 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6956 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6957 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6958 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6959 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6960 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6961 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6962 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6963 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6964 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6965 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6966 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6967 else
6968 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6969 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6970 alert );
6971 }
6972
6973#if defined(MBEDTLS_DEBUG_C)
6974 if( ssl->session_negotiate->verify_result != 0 )
6975 {
6976 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6977 (unsigned int) ssl->session_negotiate->verify_result ) );
6978 }
6979 else
6980 {
6981 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6982 }
6983#endif /* MBEDTLS_DEBUG_C */
6984
6985 return( ret );
6986}
6987
6988#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006989MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006990static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6991 unsigned char *start, size_t len )
6992{
6993 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6994 /* Remember digest of the peer's end-CRT. */
6995 ssl->session_negotiate->peer_cert_digest =
6996 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6997 if( ssl->session_negotiate->peer_cert_digest == NULL )
6998 {
6999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7000 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
7001 mbedtls_ssl_send_alert_message( ssl,
7002 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7003 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7004
7005 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7006 }
7007
7008 ret = mbedtls_md( mbedtls_md_info_from_type(
7009 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7010 start, len,
7011 ssl->session_negotiate->peer_cert_digest );
7012
7013 ssl->session_negotiate->peer_cert_digest_type =
7014 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7015 ssl->session_negotiate->peer_cert_digest_len =
7016 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7017
7018 return( ret );
7019}
7020
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007021MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007022static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7023 unsigned char *start, size_t len )
7024{
7025 unsigned char *end = start + len;
7026 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7027
7028 /* Make a copy of the peer's raw public key. */
7029 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7030 ret = mbedtls_pk_parse_subpubkey( &start, end,
7031 &ssl->handshake->peer_pubkey );
7032 if( ret != 0 )
7033 {
7034 /* We should have parsed the public key before. */
7035 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7036 }
7037
7038 return( 0 );
7039}
7040#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7041
7042int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7043{
7044 int ret = 0;
7045 int crt_expected;
7046#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7047 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7048 ? ssl->handshake->sni_authmode
7049 : ssl->conf->authmode;
7050#else
7051 const int authmode = ssl->conf->authmode;
7052#endif
7053 void *rs_ctx = NULL;
7054 mbedtls_x509_crt *chain = NULL;
7055
7056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7057
7058 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7059 if( crt_expected == SSL_CERTIFICATE_SKIP )
7060 {
7061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
7062 goto exit;
7063 }
7064
7065#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7066 if( ssl->handshake->ecrs_enabled &&
7067 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
7068 {
7069 chain = ssl->handshake->ecrs_peer_cert;
7070 ssl->handshake->ecrs_peer_cert = NULL;
7071 goto crt_verify;
7072 }
7073#endif
7074
7075 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7076 {
7077 /* mbedtls_ssl_read_record may have sent an alert already. We
7078 let it decide whether to alert. */
7079 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7080 goto exit;
7081 }
7082
7083#if defined(MBEDTLS_SSL_SRV_C)
7084 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7085 {
7086 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7087
7088 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
7089 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
7090
7091 goto exit;
7092 }
7093#endif /* MBEDTLS_SSL_SRV_C */
7094
7095 /* Clear existing peer CRT structure in case we tried to
7096 * reuse a session but it failed, and allocate a new one. */
7097 ssl_clear_peer_cert( ssl->session_negotiate );
7098
7099 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7100 if( chain == NULL )
7101 {
7102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7103 sizeof( mbedtls_x509_crt ) ) );
7104 mbedtls_ssl_send_alert_message( ssl,
7105 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7106 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7107
7108 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7109 goto exit;
7110 }
7111 mbedtls_x509_crt_init( chain );
7112
7113 ret = ssl_parse_certificate_chain( ssl, chain );
7114 if( ret != 0 )
7115 goto exit;
7116
7117#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7118 if( ssl->handshake->ecrs_enabled)
7119 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
7120
7121crt_verify:
7122 if( ssl->handshake->ecrs_enabled)
7123 rs_ctx = &ssl->handshake->ecrs_ctx;
7124#endif
7125
7126 ret = ssl_parse_certificate_verify( ssl, authmode,
7127 chain, rs_ctx );
7128 if( ret != 0 )
7129 goto exit;
7130
7131#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7132 {
7133 unsigned char *crt_start, *pk_start;
7134 size_t crt_len, pk_len;
7135
7136 /* We parse the CRT chain without copying, so
7137 * these pointers point into the input buffer,
7138 * and are hence still valid after freeing the
7139 * CRT chain. */
7140
7141 crt_start = chain->raw.p;
7142 crt_len = chain->raw.len;
7143
7144 pk_start = chain->pk_raw.p;
7145 pk_len = chain->pk_raw.len;
7146
7147 /* Free the CRT structures before computing
7148 * digest and copying the peer's public key. */
7149 mbedtls_x509_crt_free( chain );
7150 mbedtls_free( chain );
7151 chain = NULL;
7152
7153 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
7154 if( ret != 0 )
7155 goto exit;
7156
7157 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7158 if( ret != 0 )
7159 goto exit;
7160 }
7161#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7162 /* Pass ownership to session structure. */
7163 ssl->session_negotiate->peer_cert = chain;
7164 chain = NULL;
7165#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7166
7167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
7168
7169exit:
7170
7171 if( ret == 0 )
7172 ssl->state++;
7173
7174#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7175 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7176 {
7177 ssl->handshake->ecrs_peer_cert = chain;
7178 chain = NULL;
7179 }
7180#endif
7181
7182 if( chain != NULL )
7183 {
7184 mbedtls_x509_crt_free( chain );
7185 mbedtls_free( chain );
7186 }
7187
7188 return( ret );
7189}
7190#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7191
Andrzej Kurek25f27152022-08-17 16:09:31 -04007192#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007193static void ssl_calc_finished_tls_sha256(
7194 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7195{
7196 int len = 12;
7197 const char *sender;
7198 unsigned char padbuf[32];
7199#if defined(MBEDTLS_USE_PSA_CRYPTO)
7200 size_t hash_size;
7201 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7202 psa_status_t status;
7203#else
7204 mbedtls_sha256_context sha256;
7205#endif
7206
7207 mbedtls_ssl_session *session = ssl->session_negotiate;
7208 if( !session )
7209 session = ssl->session;
7210
7211 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7212 ? "client finished"
7213 : "server finished";
7214
7215#if defined(MBEDTLS_USE_PSA_CRYPTO)
7216 sha256_psa = psa_hash_operation_init();
7217
7218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7219
7220 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7221 if( status != PSA_SUCCESS )
7222 {
7223 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7224 return;
7225 }
7226
7227 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7228 if( status != PSA_SUCCESS )
7229 {
7230 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7231 return;
7232 }
7233 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7234#else
7235
7236 mbedtls_sha256_init( &sha256 );
7237
7238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7239
7240 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7241
7242 /*
7243 * TLSv1.2:
7244 * hash = PRF( master, finished_label,
7245 * Hash( handshake ) )[0.11]
7246 */
7247
7248#if !defined(MBEDTLS_SHA256_ALT)
7249 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7250 sha256.state, sizeof( sha256.state ) );
7251#endif
7252
7253 mbedtls_sha256_finish( &sha256, padbuf );
7254 mbedtls_sha256_free( &sha256 );
7255#endif /* MBEDTLS_USE_PSA_CRYPTO */
7256
7257 ssl->handshake->tls_prf( session->master, 48, sender,
7258 padbuf, 32, buf, len );
7259
7260 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7261
7262 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7263
7264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7265}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007266#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007267
Jerry Yub7ba49e2022-02-17 14:25:53 +08007268
Andrzej Kurek25f27152022-08-17 16:09:31 -04007269#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007270static void ssl_calc_finished_tls_sha384(
7271 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7272{
7273 int len = 12;
7274 const char *sender;
7275 unsigned char padbuf[48];
7276#if defined(MBEDTLS_USE_PSA_CRYPTO)
7277 size_t hash_size;
7278 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7279 psa_status_t status;
7280#else
7281 mbedtls_sha512_context sha512;
7282#endif
7283
7284 mbedtls_ssl_session *session = ssl->session_negotiate;
7285 if( !session )
7286 session = ssl->session;
7287
7288 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7289 ? "client finished"
7290 : "server finished";
7291
7292#if defined(MBEDTLS_USE_PSA_CRYPTO)
7293 sha384_psa = psa_hash_operation_init();
7294
7295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7296
7297 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7298 if( status != PSA_SUCCESS )
7299 {
7300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7301 return;
7302 }
7303
7304 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7305 if( status != PSA_SUCCESS )
7306 {
7307 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7308 return;
7309 }
7310 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7311#else
7312 mbedtls_sha512_init( &sha512 );
7313
7314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7315
Andrzej Kureka242e832022-08-11 10:03:14 -04007316 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007317
7318 /*
7319 * TLSv1.2:
7320 * hash = PRF( master, finished_label,
7321 * Hash( handshake ) )[0.11]
7322 */
7323
7324#if !defined(MBEDTLS_SHA512_ALT)
7325 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7326 sha512.state, sizeof( sha512.state ) );
7327#endif
7328 mbedtls_sha512_finish( &sha512, padbuf );
7329
7330 mbedtls_sha512_free( &sha512 );
7331#endif
7332
7333 ssl->handshake->tls_prf( session->master, 48, sender,
7334 padbuf, 48, buf, len );
7335
7336 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7337
7338 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7339
7340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7341}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007342#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007343
Jerry Yuaef00152022-02-17 14:27:31 +08007344void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7345{
7346 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7347
7348 /*
7349 * Free our handshake params
7350 */
7351 mbedtls_ssl_handshake_free( ssl );
7352 mbedtls_free( ssl->handshake );
7353 ssl->handshake = NULL;
7354
7355 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007356 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007357 */
7358 if( ssl->transform )
7359 {
7360 mbedtls_ssl_transform_free( ssl->transform );
7361 mbedtls_free( ssl->transform );
7362 }
7363 ssl->transform = ssl->transform_negotiate;
7364 ssl->transform_negotiate = NULL;
7365
7366 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7367}
7368
Jerry Yu2a9fff52022-02-17 14:28:51 +08007369void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7370{
7371 int resume = ssl->handshake->resume;
7372
7373 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7374
7375#if defined(MBEDTLS_SSL_RENEGOTIATION)
7376 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7377 {
7378 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7379 ssl->renego_records_seen = 0;
7380 }
7381#endif
7382
7383 /*
7384 * Free the previous session and switch in the current one
7385 */
7386 if( ssl->session )
7387 {
7388#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7389 /* RFC 7366 3.1: keep the EtM state */
7390 ssl->session_negotiate->encrypt_then_mac =
7391 ssl->session->encrypt_then_mac;
7392#endif
7393
7394 mbedtls_ssl_session_free( ssl->session );
7395 mbedtls_free( ssl->session );
7396 }
7397 ssl->session = ssl->session_negotiate;
7398 ssl->session_negotiate = NULL;
7399
7400 /*
7401 * Add cache entry
7402 */
7403 if( ssl->conf->f_set_cache != NULL &&
7404 ssl->session->id_len != 0 &&
7405 resume == 0 )
7406 {
7407 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7408 ssl->session->id,
7409 ssl->session->id_len,
7410 ssl->session ) != 0 )
7411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7412 }
7413
7414#if defined(MBEDTLS_SSL_PROTO_DTLS)
7415 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7416 ssl->handshake->flight != NULL )
7417 {
7418 /* Cancel handshake timer */
7419 mbedtls_ssl_set_timer( ssl, 0 );
7420
7421 /* Keep last flight around in case we need to resend it:
7422 * we need the handshake and transform structures for that */
7423 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7424 }
7425 else
7426#endif
7427 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7428
7429 ssl->state++;
7430
7431 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7432}
7433
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007434int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7435{
7436 int ret, hash_len;
7437
7438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7439
7440 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7441
7442 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7443
7444 /*
7445 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7446 * may define some other value. Currently (early 2016), no defined
7447 * ciphersuite does this (and this is unlikely to change as activity has
7448 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7449 */
7450 hash_len = 12;
7451
7452#if defined(MBEDTLS_SSL_RENEGOTIATION)
7453 ssl->verify_data_len = hash_len;
7454 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7455#endif
7456
7457 ssl->out_msglen = 4 + hash_len;
7458 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7459 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7460
7461 /*
7462 * In case of session resuming, invert the client and server
7463 * ChangeCipherSpec messages order.
7464 */
7465 if( ssl->handshake->resume != 0 )
7466 {
7467#if defined(MBEDTLS_SSL_CLI_C)
7468 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7469 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7470#endif
7471#if defined(MBEDTLS_SSL_SRV_C)
7472 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7473 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7474#endif
7475 }
7476 else
7477 ssl->state++;
7478
7479 /*
7480 * Switch to our negotiated transform and session parameters for outbound
7481 * data.
7482 */
7483 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7484
7485#if defined(MBEDTLS_SSL_PROTO_DTLS)
7486 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7487 {
7488 unsigned char i;
7489
7490 /* Remember current epoch settings for resending */
7491 ssl->handshake->alt_transform_out = ssl->transform_out;
7492 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7493 sizeof( ssl->handshake->alt_out_ctr ) );
7494
7495 /* Set sequence_number to zero */
7496 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7497
7498
7499 /* Increment epoch */
7500 for( i = 2; i > 0; i-- )
7501 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7502 break;
7503
7504 /* The loop goes to its end iff the counter is wrapping */
7505 if( i == 0 )
7506 {
7507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7508 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7509 }
7510 }
7511 else
7512#endif /* MBEDTLS_SSL_PROTO_DTLS */
7513 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7514
7515 ssl->transform_out = ssl->transform_negotiate;
7516 ssl->session_out = ssl->session_negotiate;
7517
7518#if defined(MBEDTLS_SSL_PROTO_DTLS)
7519 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7520 mbedtls_ssl_send_flight_completed( ssl );
7521#endif
7522
7523 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7524 {
7525 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7526 return( ret );
7527 }
7528
7529#if defined(MBEDTLS_SSL_PROTO_DTLS)
7530 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7531 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7532 {
7533 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7534 return( ret );
7535 }
7536#endif
7537
7538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7539
7540 return( 0 );
7541}
7542
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007543#define SSL_MAX_HASH_LEN 12
7544
7545int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7546{
7547 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7548 unsigned int hash_len = 12;
7549 unsigned char buf[SSL_MAX_HASH_LEN];
7550
7551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7552
7553 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7554
7555 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7556 {
7557 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7558 goto exit;
7559 }
7560
7561 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7562 {
7563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7564 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7565 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7566 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7567 goto exit;
7568 }
7569
7570 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7571 {
7572 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7573 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7574 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7575 goto exit;
7576 }
7577
7578 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7579 {
7580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7581 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7582 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7583 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7584 goto exit;
7585 }
7586
7587 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7588 buf, hash_len ) != 0 )
7589 {
7590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7591 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7592 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7593 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7594 goto exit;
7595 }
7596
7597#if defined(MBEDTLS_SSL_RENEGOTIATION)
7598 ssl->verify_data_len = hash_len;
7599 memcpy( ssl->peer_verify_data, buf, hash_len );
7600#endif
7601
7602 if( ssl->handshake->resume != 0 )
7603 {
7604#if defined(MBEDTLS_SSL_CLI_C)
7605 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7606 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7607#endif
7608#if defined(MBEDTLS_SSL_SRV_C)
7609 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7610 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7611#endif
7612 }
7613 else
7614 ssl->state++;
7615
7616#if defined(MBEDTLS_SSL_PROTO_DTLS)
7617 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7618 mbedtls_ssl_recv_flight_completed( ssl );
7619#endif
7620
7621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7622
7623exit:
7624 mbedtls_platform_zeroize( buf, hash_len );
7625 return( ret );
7626}
7627
Jerry Yu392112c2022-02-17 14:34:10 +08007628#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7629/*
7630 * Helper to get TLS 1.2 PRF from ciphersuite
7631 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7632 */
7633static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7634{
Jerry Yu392112c2022-02-17 14:34:10 +08007635 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Andrzej Kurek68327742022-10-03 06:18:18 -04007636 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7637#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007638 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007639 return( tls_prf_sha384 );
Andrzej Kurek894edde2022-09-29 06:31:14 -04007640 else
Jerry Yu392112c2022-02-17 14:34:10 +08007641#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007642#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7643 {
7644 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
7645 return( tls_prf_sha256 );
7646 }
7647#endif
7648#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7649 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7650 (void) ciphersuite_info;
7651#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007652
Andrzej Kurek894edde2022-09-29 06:31:14 -04007653 return( NULL );
Jerry Yu392112c2022-02-17 14:34:10 +08007654}
7655#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007656
7657static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7658{
7659 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007660#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007661 if( tls_prf == tls_prf_sha384 )
7662 {
7663 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7664 }
7665 else
7666#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007667#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007668 if( tls_prf == tls_prf_sha256 )
7669 {
7670 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7671 }
7672 else
7673#endif
7674 return( MBEDTLS_SSL_TLS_PRF_NONE );
7675}
7676
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007677/*
7678 * Populate a transform structure with session keys and all the other
7679 * necessary information.
7680 *
7681 * Parameters:
7682 * - [in/out]: transform: structure to populate
7683 * [in] must be just initialised with mbedtls_ssl_transform_init()
7684 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7685 * - [in] ciphersuite
7686 * - [in] master
7687 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007688 * - [in] tls_prf: pointer to PRF to use for key derivation
7689 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007690 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007691 * - [in] endpoint: client or server
7692 * - [in] ssl: used for:
7693 * - ssl->conf->{f,p}_export_keys
7694 * [in] optionally used for:
7695 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7696 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007697MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007698static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7699 int ciphersuite,
7700 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007701#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007702 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007703#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007704 ssl_tls_prf_t tls_prf,
7705 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007706 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007707 unsigned endpoint,
7708 const mbedtls_ssl_context *ssl )
7709{
7710 int ret = 0;
7711 unsigned char keyblk[256];
7712 unsigned char *key1;
7713 unsigned char *key2;
7714 unsigned char *mac_enc;
7715 unsigned char *mac_dec;
7716 size_t mac_key_len = 0;
7717 size_t iv_copy_len;
7718 size_t keylen;
7719 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007720 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007721#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007722 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007723 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007724#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007725
7726#if defined(MBEDTLS_USE_PSA_CRYPTO)
7727 psa_key_type_t key_type;
7728 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7729 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007730 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007731 size_t key_bits;
7732 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7733#endif
7734
7735#if !defined(MBEDTLS_DEBUG_C) && \
7736 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7737 if( ssl->f_export_keys == NULL )
7738 {
7739 ssl = NULL; /* make sure we don't use it except for these cases */
7740 (void) ssl;
7741 }
7742#endif
7743
7744 /*
7745 * Some data just needs copying into the structure
7746 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007747#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007748 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007749#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007750 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007751
7752#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7753 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7754#endif
7755
7756#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007757 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007758 {
7759 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7760 * generation separate. This should never happen. */
7761 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7762 }
7763#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7764
7765 /*
7766 * Get various info structures
7767 */
7768 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7769 if( ciphersuite_info == NULL )
7770 {
7771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7772 ciphersuite ) );
7773 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7774 }
7775
Neil Armstrongab555e02022-04-04 11:07:59 +02007776 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007777#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007778 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007779#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007780 ciphersuite_info );
7781
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007782 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7783 transform->taglen =
7784 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7785
7786#if defined(MBEDTLS_USE_PSA_CRYPTO)
7787 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7788 transform->taglen,
7789 &alg,
7790 &key_type,
7791 &key_bits ) ) != PSA_SUCCESS )
7792 {
7793 ret = psa_ssl_status_to_mbedtls( status );
7794 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7795 goto end;
7796 }
7797#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007798 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7799 if( cipher_info == NULL )
7800 {
7801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7802 ciphersuite_info->cipher ) );
7803 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7804 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007805#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007806
Neil Armstronge4512952022-03-08 09:08:22 +01007807#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007808 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007809 if( mac_alg == 0 )
7810 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007812 (unsigned) ciphersuite_info->mac ) );
7813 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7814 }
7815#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007816 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7817 if( md_info == NULL )
7818 {
7819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7820 (unsigned) ciphersuite_info->mac ) );
7821 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7822 }
Neil Armstronge4512952022-03-08 09:08:22 +01007823#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007824
7825#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7826 /* Copy own and peer's CID if the use of the CID
7827 * extension has been negotiated. */
7828 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7829 {
7830 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7831
7832 transform->in_cid_len = ssl->own_cid_len;
7833 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7834 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7835 transform->in_cid_len );
7836
7837 transform->out_cid_len = ssl->handshake->peer_cid_len;
7838 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7839 ssl->handshake->peer_cid_len );
7840 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7841 transform->out_cid_len );
7842 }
7843#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7844
7845 /*
7846 * Compute key block using the PRF
7847 */
7848 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7849 if( ret != 0 )
7850 {
7851 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7852 return( ret );
7853 }
7854
7855 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7856 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7857 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7858 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7859 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7860
7861 /*
7862 * Determine the appropriate key, IV and MAC length.
7863 */
7864
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007865#if defined(MBEDTLS_USE_PSA_CRYPTO)
7866 keylen = PSA_BITS_TO_BYTES(key_bits);
7867#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007868 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007869#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007870
7871#if defined(MBEDTLS_GCM_C) || \
7872 defined(MBEDTLS_CCM_C) || \
7873 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007874 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007875 {
7876 size_t explicit_ivlen;
7877
7878 transform->maclen = 0;
7879 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007880
7881 /* All modes haves 96-bit IVs, but the length of the static parts vary
7882 * with mode and version:
7883 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7884 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7885 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7886 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7887 * sequence number).
7888 */
7889 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01007890
7891 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007892#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann3b2276a2022-10-06 14:49:08 +01007893 is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007894#else
David Horstmann3b2276a2022-10-06 14:49:08 +01007895 is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info )
7896 == MBEDTLS_MODE_CHACHAPOLY );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007897#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01007898
7899 if( is_chachapoly )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007900 transform->fixed_ivlen = 12;
7901 else
7902 transform->fixed_ivlen = 4;
7903
7904 /* Minimum length of encrypted record */
7905 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7906 transform->minlen = explicit_ivlen + transform->taglen;
7907 }
7908 else
7909#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7910#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007911 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7912 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7913 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007914 {
Neil Armstronge4512952022-03-08 09:08:22 +01007915#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007916 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007917#else
7918 size_t block_size = cipher_info->block_size;
7919#endif /* MBEDTLS_USE_PSA_CRYPTO */
7920
7921#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007922 /* Get MAC length */
7923 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7924#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007925 /* Initialize HMAC contexts */
7926 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7927 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7928 {
7929 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7930 goto end;
7931 }
7932
7933 /* Get MAC length */
7934 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007935#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007936 transform->maclen = mac_key_len;
7937
7938 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007939#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007940 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007941#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007942 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007943#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007944
7945 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007946 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007947 transform->minlen = transform->maclen;
7948 else
7949 {
7950 /*
7951 * GenericBlockCipher:
7952 * 1. if EtM is in use: one block plus MAC
7953 * otherwise: * first multiple of blocklen greater than maclen
7954 * 2. IV
7955 */
7956#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007957 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007958 {
7959 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007960 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007961 }
7962 else
7963#endif
7964 {
7965 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007966 + block_size
7967 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007968 }
7969
Glenn Strauss07c64162022-03-14 12:34:51 -04007970 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007971 {
7972 transform->minlen += transform->ivlen;
7973 }
7974 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007975 {
7976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7977 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7978 goto end;
7979 }
7980 }
7981 }
7982 else
7983#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7984 {
7985 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7986 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7987 }
7988
7989 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7990 (unsigned) keylen,
7991 (unsigned) transform->minlen,
7992 (unsigned) transform->ivlen,
7993 (unsigned) transform->maclen ) );
7994
7995 /*
7996 * Finally setup the cipher contexts, IVs and MAC secrets.
7997 */
7998#if defined(MBEDTLS_SSL_CLI_C)
7999 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8000 {
8001 key1 = keyblk + mac_key_len * 2;
8002 key2 = keyblk + mac_key_len * 2 + keylen;
8003
8004 mac_enc = keyblk;
8005 mac_dec = keyblk + mac_key_len;
8006
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008007 iv_copy_len = ( transform->fixed_ivlen ) ?
8008 transform->fixed_ivlen : transform->ivlen;
8009 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
8010 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
8011 iv_copy_len );
8012 }
8013 else
8014#endif /* MBEDTLS_SSL_CLI_C */
8015#if defined(MBEDTLS_SSL_SRV_C)
8016 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8017 {
8018 key1 = keyblk + mac_key_len * 2 + keylen;
8019 key2 = keyblk + mac_key_len * 2;
8020
8021 mac_enc = keyblk + mac_key_len;
8022 mac_dec = keyblk;
8023
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008024 iv_copy_len = ( transform->fixed_ivlen ) ?
8025 transform->fixed_ivlen : transform->ivlen;
8026 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
8027 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
8028 iv_copy_len );
8029 }
8030 else
8031#endif /* MBEDTLS_SSL_SRV_C */
8032 {
8033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8034 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8035 goto end;
8036 }
8037
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008038 if( ssl != NULL && ssl->f_export_keys != NULL )
8039 {
8040 ssl->f_export_keys( ssl->p_export_keys,
8041 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8042 master, 48,
8043 randbytes + 32,
8044 randbytes,
8045 tls_prf_get_type( tls_prf ) );
8046 }
8047
8048#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008049 transform->psa_alg = alg;
8050
8051 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
8052 {
8053 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
8054 psa_set_key_algorithm( &attributes, alg );
8055 psa_set_key_type( &attributes, key_type );
8056
8057 if( ( status = psa_import_key( &attributes,
8058 key1,
8059 PSA_BITS_TO_BYTES( key_bits ),
8060 &transform->psa_key_enc ) ) != PSA_SUCCESS )
8061 {
8062 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
8063 ret = psa_ssl_status_to_mbedtls( status );
8064 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8065 goto end;
8066 }
8067
8068 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
8069
8070 if( ( status = psa_import_key( &attributes,
8071 key2,
8072 PSA_BITS_TO_BYTES( key_bits ),
8073 &transform->psa_key_dec ) ) != PSA_SUCCESS )
8074 {
8075 ret = psa_ssl_status_to_mbedtls( status );
8076 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8077 goto end;
8078 }
8079 }
8080#else
8081 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
8082 cipher_info ) ) != 0 )
8083 {
8084 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8085 goto end;
8086 }
8087
8088 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
8089 cipher_info ) ) != 0 )
8090 {
8091 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8092 goto end;
8093 }
8094
8095 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
8096 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8097 MBEDTLS_ENCRYPT ) ) != 0 )
8098 {
8099 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8100 goto end;
8101 }
8102
8103 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
8104 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8105 MBEDTLS_DECRYPT ) ) != 0 )
8106 {
8107 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8108 goto end;
8109 }
8110
8111#if defined(MBEDTLS_CIPHER_MODE_CBC)
8112 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
8113 {
8114 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
8115 MBEDTLS_PADDING_NONE ) ) != 0 )
8116 {
8117 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8118 goto end;
8119 }
8120
8121 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
8122 MBEDTLS_PADDING_NONE ) ) != 0 )
8123 {
8124 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8125 goto end;
8126 }
8127 }
8128#endif /* MBEDTLS_CIPHER_MODE_CBC */
8129#endif /* MBEDTLS_USE_PSA_CRYPTO */
8130
Neil Armstrong29c0c042022-03-17 17:47:28 +01008131#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8132 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8133 For AEAD-based ciphersuites, there is nothing to do here. */
8134 if( mac_key_len != 0 )
8135 {
8136#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008137 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008138
8139 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01008140 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008141 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
8142
8143 if( ( status = psa_import_key( &attributes,
8144 mac_enc, mac_key_len,
8145 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
8146 {
8147 ret = psa_ssl_status_to_mbedtls( status );
8148 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8149 goto end;
8150 }
8151
Ronald Cronfb39f152022-03-25 14:36:28 +01008152 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008153 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
8154#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
8155 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
8156#endif
8157 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01008158 /* mbedtls_ct_hmac() requires the key to be exportable */
8159 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
8160 PSA_KEY_USAGE_VERIFY_HASH );
8161 else
8162 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
8163
8164 if( ( status = psa_import_key( &attributes,
8165 mac_dec, mac_key_len,
8166 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
8167 {
8168 ret = psa_ssl_status_to_mbedtls( status );
8169 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8170 goto end;
8171 }
8172#else
8173 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
8174 if( ret != 0 )
8175 goto end;
8176 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
8177 if( ret != 0 )
8178 goto end;
8179#endif /* MBEDTLS_USE_PSA_CRYPTO */
8180 }
8181#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8182
8183 ((void) mac_dec);
8184 ((void) mac_enc);
8185
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008186end:
8187 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
8188 return( ret );
8189}
8190
Jerry Yuee40f9d2022-02-17 14:55:16 +08008191#if defined(MBEDTLS_USE_PSA_CRYPTO)
8192int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8193 unsigned char *hash, size_t *hashlen,
8194 unsigned char *data, size_t data_len,
8195 mbedtls_md_type_t md_alg )
8196{
8197 psa_status_t status;
8198 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008199 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008200
8201 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8202
8203 if( ( status = psa_hash_setup( &hash_operation,
8204 hash_alg ) ) != PSA_SUCCESS )
8205 {
8206 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8207 goto exit;
8208 }
8209
8210 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8211 64 ) ) != PSA_SUCCESS )
8212 {
8213 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8214 goto exit;
8215 }
8216
8217 if( ( status = psa_hash_update( &hash_operation,
8218 data, data_len ) ) != PSA_SUCCESS )
8219 {
8220 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8221 goto exit;
8222 }
8223
8224 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8225 hashlen ) ) != PSA_SUCCESS )
8226 {
8227 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8228 goto exit;
8229 }
8230
8231exit:
8232 if( status != PSA_SUCCESS )
8233 {
8234 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8235 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8236 switch( status )
8237 {
8238 case PSA_ERROR_NOT_SUPPORTED:
8239 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8240 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8241 case PSA_ERROR_BUFFER_TOO_SMALL:
8242 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8243 case PSA_ERROR_INSUFFICIENT_MEMORY:
8244 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8245 default:
8246 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8247 }
8248 }
8249 return( 0 );
8250}
8251
8252#else
8253
8254int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8255 unsigned char *hash, size_t *hashlen,
8256 unsigned char *data, size_t data_len,
8257 mbedtls_md_type_t md_alg )
8258{
8259 int ret = 0;
8260 mbedtls_md_context_t ctx;
8261 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8262 *hashlen = mbedtls_md_get_size( md_info );
8263
8264 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8265
8266 mbedtls_md_init( &ctx );
8267
8268 /*
8269 * digitally-signed struct {
8270 * opaque client_random[32];
8271 * opaque server_random[32];
8272 * ServerDHParams params;
8273 * };
8274 */
8275 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8276 {
8277 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8278 goto exit;
8279 }
8280 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8281 {
8282 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8283 goto exit;
8284 }
8285 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8286 {
8287 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8288 goto exit;
8289 }
8290 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8291 {
8292 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8293 goto exit;
8294 }
8295 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8296 {
8297 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8298 goto exit;
8299 }
8300
8301exit:
8302 mbedtls_md_free( &ctx );
8303
8304 if( ret != 0 )
8305 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8306 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8307
8308 return( ret );
8309}
8310#endif /* MBEDTLS_USE_PSA_CRYPTO */
8311
Jerry Yud9d91da2022-02-17 14:57:06 +08008312#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8313
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008314/* Find the preferred hash for a given signature algorithm. */
8315unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8316 mbedtls_ssl_context *ssl,
8317 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008318{
Gabor Mezei078e8032022-04-27 21:17:56 +02008319 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008320 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008321
8322 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008323 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008324
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008325 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008326 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008327 unsigned int hash_alg_received =
8328 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8329 received_sig_algs[i] );
8330 unsigned int sig_alg_received =
8331 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8332 received_sig_algs[i] );
8333
8334 if( sig_alg == sig_alg_received )
8335 {
8336#if defined(MBEDTLS_USE_PSA_CRYPTO)
8337 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8338 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008339 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008340 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008341
Neil Armstrong96eceb82022-06-30 18:05:05 +02008342 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8343 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8344 PSA_ALG_ECDSA( psa_hash_alg ),
8345 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008346 continue;
8347
Neil Armstrong96eceb82022-06-30 18:05:05 +02008348 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008349 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8350 PSA_ALG_RSA_PKCS1V15_SIGN(
8351 psa_hash_alg ),
8352 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008353 continue;
8354 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008355#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008356
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008357 return( hash_alg_received );
8358 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008359 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008360
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008361 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008362}
8363
8364#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8365
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008366/* Serialization of TLS 1.2 sessions:
8367 *
8368 * struct {
8369 * uint64 start_time;
8370 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008371 * uint8 session_id_len; // at most 32
8372 * opaque session_id[32];
8373 * opaque master[48]; // fixed length in the standard
8374 * uint32 verify_result;
8375 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8376 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8377 * uint32 ticket_lifetime;
8378 * uint8 mfl_code; // up to 255 according to standard
8379 * uint8 encrypt_then_mac; // 0 or 1
8380 * } serialized_session_tls12;
8381 *
8382 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008383static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008384 unsigned char *buf,
8385 size_t buf_len )
8386{
8387 unsigned char *p = buf;
8388 size_t used = 0;
8389
8390#if defined(MBEDTLS_HAVE_TIME)
8391 uint64_t start;
8392#endif
8393#if defined(MBEDTLS_X509_CRT_PARSE_C)
8394#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8395 size_t cert_len;
8396#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8397#endif /* MBEDTLS_X509_CRT_PARSE_C */
8398
8399 /*
8400 * Time
8401 */
8402#if defined(MBEDTLS_HAVE_TIME)
8403 used += 8;
8404
8405 if( used <= buf_len )
8406 {
8407 start = (uint64_t) session->start;
8408
8409 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8410 p += 8;
8411 }
8412#endif /* MBEDTLS_HAVE_TIME */
8413
8414 /*
8415 * Basic mandatory fields
8416 */
8417 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008418 + 1 /* id_len */
8419 + sizeof( session->id )
8420 + sizeof( session->master )
8421 + 4; /* verify_result */
8422
8423 if( used <= buf_len )
8424 {
8425 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8426 p += 2;
8427
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008428 *p++ = MBEDTLS_BYTE_0( session->id_len );
8429 memcpy( p, session->id, 32 );
8430 p += 32;
8431
8432 memcpy( p, session->master, 48 );
8433 p += 48;
8434
8435 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8436 p += 4;
8437 }
8438
8439 /*
8440 * Peer's end-entity certificate
8441 */
8442#if defined(MBEDTLS_X509_CRT_PARSE_C)
8443#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8444 if( session->peer_cert == NULL )
8445 cert_len = 0;
8446 else
8447 cert_len = session->peer_cert->raw.len;
8448
8449 used += 3 + cert_len;
8450
8451 if( used <= buf_len )
8452 {
8453 *p++ = MBEDTLS_BYTE_2( cert_len );
8454 *p++ = MBEDTLS_BYTE_1( cert_len );
8455 *p++ = MBEDTLS_BYTE_0( cert_len );
8456
8457 if( session->peer_cert != NULL )
8458 {
8459 memcpy( p, session->peer_cert->raw.p, cert_len );
8460 p += cert_len;
8461 }
8462 }
8463#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8464 if( session->peer_cert_digest != NULL )
8465 {
8466 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8467 if( used <= buf_len )
8468 {
8469 *p++ = (unsigned char) session->peer_cert_digest_type;
8470 *p++ = (unsigned char) session->peer_cert_digest_len;
8471 memcpy( p, session->peer_cert_digest,
8472 session->peer_cert_digest_len );
8473 p += session->peer_cert_digest_len;
8474 }
8475 }
8476 else
8477 {
8478 used += 2;
8479 if( used <= buf_len )
8480 {
8481 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8482 *p++ = 0;
8483 }
8484 }
8485#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8486#endif /* MBEDTLS_X509_CRT_PARSE_C */
8487
8488 /*
8489 * Session ticket if any, plus associated data
8490 */
8491#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8492 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8493
8494 if( used <= buf_len )
8495 {
8496 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8497 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8498 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8499
8500 if( session->ticket != NULL )
8501 {
8502 memcpy( p, session->ticket, session->ticket_len );
8503 p += session->ticket_len;
8504 }
8505
8506 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8507 p += 4;
8508 }
8509#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8510
8511 /*
8512 * Misc extension-related info
8513 */
8514#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8515 used += 1;
8516
8517 if( used <= buf_len )
8518 *p++ = session->mfl_code;
8519#endif
8520
8521#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8522 used += 1;
8523
8524 if( used <= buf_len )
8525 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8526#endif
8527
8528 return( used );
8529}
8530
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008531MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008532static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008533 const unsigned char *buf,
8534 size_t len )
8535{
8536#if defined(MBEDTLS_HAVE_TIME)
8537 uint64_t start;
8538#endif
8539#if defined(MBEDTLS_X509_CRT_PARSE_C)
8540#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8541 size_t cert_len;
8542#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8543#endif /* MBEDTLS_X509_CRT_PARSE_C */
8544
8545 const unsigned char *p = buf;
8546 const unsigned char * const end = buf + len;
8547
8548 /*
8549 * Time
8550 */
8551#if defined(MBEDTLS_HAVE_TIME)
8552 if( 8 > (size_t)( end - p ) )
8553 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8554
8555 start = ( (uint64_t) p[0] << 56 ) |
8556 ( (uint64_t) p[1] << 48 ) |
8557 ( (uint64_t) p[2] << 40 ) |
8558 ( (uint64_t) p[3] << 32 ) |
8559 ( (uint64_t) p[4] << 24 ) |
8560 ( (uint64_t) p[5] << 16 ) |
8561 ( (uint64_t) p[6] << 8 ) |
8562 ( (uint64_t) p[7] );
8563 p += 8;
8564
8565 session->start = (time_t) start;
8566#endif /* MBEDTLS_HAVE_TIME */
8567
8568 /*
8569 * Basic mandatory fields
8570 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008571 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008572 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8573
8574 session->ciphersuite = ( p[0] << 8 ) | p[1];
8575 p += 2;
8576
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008577 session->id_len = *p++;
8578 memcpy( session->id, p, 32 );
8579 p += 32;
8580
8581 memcpy( session->master, p, 48 );
8582 p += 48;
8583
8584 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8585 ( (uint32_t) p[1] << 16 ) |
8586 ( (uint32_t) p[2] << 8 ) |
8587 ( (uint32_t) p[3] );
8588 p += 4;
8589
8590 /* Immediately clear invalid pointer values that have been read, in case
8591 * we exit early before we replaced them with valid ones. */
8592#if defined(MBEDTLS_X509_CRT_PARSE_C)
8593#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8594 session->peer_cert = NULL;
8595#else
8596 session->peer_cert_digest = NULL;
8597#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8598#endif /* MBEDTLS_X509_CRT_PARSE_C */
8599#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8600 session->ticket = NULL;
8601#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8602
8603 /*
8604 * Peer certificate
8605 */
8606#if defined(MBEDTLS_X509_CRT_PARSE_C)
8607#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8608 /* Deserialize CRT from the end of the ticket. */
8609 if( 3 > (size_t)( end - p ) )
8610 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8611
8612 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8613 p += 3;
8614
8615 if( cert_len != 0 )
8616 {
8617 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8618
8619 if( cert_len > (size_t)( end - p ) )
8620 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8621
8622 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8623
8624 if( session->peer_cert == NULL )
8625 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8626
8627 mbedtls_x509_crt_init( session->peer_cert );
8628
8629 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8630 p, cert_len ) ) != 0 )
8631 {
8632 mbedtls_x509_crt_free( session->peer_cert );
8633 mbedtls_free( session->peer_cert );
8634 session->peer_cert = NULL;
8635 return( ret );
8636 }
8637
8638 p += cert_len;
8639 }
8640#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8641 /* Deserialize CRT digest from the end of the ticket. */
8642 if( 2 > (size_t)( end - p ) )
8643 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8644
8645 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8646 session->peer_cert_digest_len = (size_t) *p++;
8647
8648 if( session->peer_cert_digest_len != 0 )
8649 {
8650 const mbedtls_md_info_t *md_info =
8651 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8652 if( md_info == NULL )
8653 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8654 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8656
8657 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8658 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8659
8660 session->peer_cert_digest =
8661 mbedtls_calloc( 1, session->peer_cert_digest_len );
8662 if( session->peer_cert_digest == NULL )
8663 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8664
8665 memcpy( session->peer_cert_digest, p,
8666 session->peer_cert_digest_len );
8667 p += session->peer_cert_digest_len;
8668 }
8669#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8670#endif /* MBEDTLS_X509_CRT_PARSE_C */
8671
8672 /*
8673 * Session ticket and associated data
8674 */
8675#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8676 if( 3 > (size_t)( end - p ) )
8677 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8678
8679 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8680 p += 3;
8681
8682 if( session->ticket_len != 0 )
8683 {
8684 if( session->ticket_len > (size_t)( end - p ) )
8685 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8686
8687 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8688 if( session->ticket == NULL )
8689 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8690
8691 memcpy( session->ticket, p, session->ticket_len );
8692 p += session->ticket_len;
8693 }
8694
8695 if( 4 > (size_t)( end - p ) )
8696 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8697
8698 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8699 ( (uint32_t) p[1] << 16 ) |
8700 ( (uint32_t) p[2] << 8 ) |
8701 ( (uint32_t) p[3] );
8702 p += 4;
8703#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8704
8705 /*
8706 * Misc extension-related info
8707 */
8708#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8709 if( 1 > (size_t)( end - p ) )
8710 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8711
8712 session->mfl_code = *p++;
8713#endif
8714
8715#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8716 if( 1 > (size_t)( end - p ) )
8717 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8718
8719 session->encrypt_then_mac = *p++;
8720#endif
8721
8722 /* Done, should have consumed entire buffer */
8723 if( p != end )
8724 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8725
8726 return( 0 );
8727}
Jerry Yudc7bd172022-02-17 13:44:15 +08008728#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8729
XiaokangQian75d40ef2022-04-20 11:05:24 +00008730int mbedtls_ssl_validate_ciphersuite(
8731 const mbedtls_ssl_context *ssl,
8732 const mbedtls_ssl_ciphersuite_t *suite_info,
8733 mbedtls_ssl_protocol_version min_tls_version,
8734 mbedtls_ssl_protocol_version max_tls_version )
8735{
8736 (void) ssl;
8737
8738 if( suite_info == NULL )
8739 return( -1 );
8740
8741 if( ( suite_info->min_tls_version > max_tls_version ) ||
8742 ( suite_info->max_tls_version < min_tls_version ) )
8743 {
8744 return( -1 );
8745 }
8746
XiaokangQian060d8672022-04-21 09:24:56 +00008747#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008748#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02008749#if defined(MBEDTLS_USE_PSA_CRYPTO)
8750 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8751 ssl->handshake->psa_pake_ctx_is_ok != 1 )
8752#else
XiaokangQian75d40ef2022-04-20 11:05:24 +00008753 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8754 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
Neil Armstrongca7d5062022-05-31 14:43:23 +02008755#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00008756 {
8757 return( -1 );
8758 }
8759#endif
8760
8761 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8762#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8763 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8764 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8765 {
8766 return( -1 );
8767 }
8768#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8769#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8770
8771 return( 0 );
8772}
8773
Ronald Crone68ab4f2022-10-05 12:46:29 +02008774#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00008775/*
8776 * Function for writing a signature algorithm extension.
8777 *
8778 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8779 * value (TLS 1.3 RFC8446):
8780 * enum {
8781 * ....
8782 * ecdsa_secp256r1_sha256( 0x0403 ),
8783 * ecdsa_secp384r1_sha384( 0x0503 ),
8784 * ecdsa_secp521r1_sha512( 0x0603 ),
8785 * ....
8786 * } SignatureScheme;
8787 *
8788 * struct {
8789 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8790 * } SignatureSchemeList;
8791 *
8792 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8793 * value (TLS 1.2 RFC5246):
8794 * enum {
8795 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8796 * sha512(6), (255)
8797 * } HashAlgorithm;
8798 *
8799 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8800 * SignatureAlgorithm;
8801 *
8802 * struct {
8803 * HashAlgorithm hash;
8804 * SignatureAlgorithm signature;
8805 * } SignatureAndHashAlgorithm;
8806 *
8807 * SignatureAndHashAlgorithm
8808 * supported_signature_algorithms<2..2^16-2>;
8809 *
8810 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8811 * generalization of the TLS 1.2 signature algorithm extension.
8812 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8813 * `SignatureScheme` field of TLS 1.3
8814 *
8815 */
8816int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8817 const unsigned char *end, size_t *out_len )
8818{
8819 unsigned char *p = buf;
8820 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8821 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8822
8823 *out_len = 0;
8824
8825 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8826
8827 /* Check if we have space for header and length field:
8828 * - extension_type (2 bytes)
8829 * - extension_data_length (2 bytes)
8830 * - supported_signature_algorithms_length (2 bytes)
8831 */
8832 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8833 p += 6;
8834
8835 /*
8836 * Write supported_signature_algorithms
8837 */
8838 supported_sig_alg = p;
8839 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8840 if( sig_alg == NULL )
8841 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8842
8843 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8844 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008845 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8846 *sig_alg,
8847 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008848 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8849 continue;
8850 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8851 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8852 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008853 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008854 *sig_alg,
8855 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008856 }
8857
8858 /* Length of supported_signature_algorithms */
8859 supported_sig_alg_len = p - supported_sig_alg;
8860 if( supported_sig_alg_len == 0 )
8861 {
8862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8863 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8864 }
8865
XiaokangQianeaf36512022-04-24 09:07:44 +00008866 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008867 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008868 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8869
XiaokangQianeaf36512022-04-24 09:07:44 +00008870 *out_len = p - buf;
8871
8872#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8873 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8874#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8875 return( 0 );
8876}
Ronald Crone68ab4f2022-10-05 12:46:29 +02008877#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00008878
XiaokangQian40a35232022-05-07 09:02:40 +00008879#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008880/*
8881 * mbedtls_ssl_parse_server_name_ext
8882 *
8883 * Structure of server_name extension:
8884 *
8885 * enum {
8886 * host_name(0), (255)
8887 * } NameType;
8888 * opaque HostName<1..2^16-1>;
8889 *
8890 * struct {
8891 * NameType name_type;
8892 * select (name_type) {
8893 * case host_name: HostName;
8894 * } name;
8895 * } ServerName;
8896 * struct {
8897 * ServerName server_name_list<1..2^16-1>
8898 * } ServerNameList;
8899 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008900MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008901int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8902 const unsigned char *buf,
8903 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008904{
8905 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8906 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008907 size_t server_name_list_len, hostname_len;
8908 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008909
XiaokangQianf2a94202022-05-20 06:44:24 +00008910 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008911
8912 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008913 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008914 p += 2;
8915
XiaokangQian9b2b7712022-05-17 02:57:00 +00008916 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8917 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008918 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008919 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008920 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008921 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008922 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8923 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008924
8925 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8926 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008927 /* sni_name is intended to be used only during the parsing of the
8928 * ClientHello message (it is reset to NULL before the end of
8929 * the message parsing). Thus it is ok to just point to the
8930 * reception buffer and not make a copy of it.
8931 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008932 ssl->handshake->sni_name = p + 3;
8933 ssl->handshake->sni_name_len = hostname_len;
8934 if( ssl->conf->f_sni == NULL )
8935 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008936 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008937 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008938 if( ret != 0 )
8939 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008940 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008941 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8942 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008943 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8944 }
8945 return( 0 );
8946 }
8947
8948 p += hostname_len + 3;
8949 }
8950
8951 return( 0 );
8952}
8953#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8954
XiaokangQianacb39922022-06-17 10:18:48 +00008955#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008956MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008957int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8958 const unsigned char *buf,
8959 const unsigned char *end )
8960{
8961 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008962 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008963 const unsigned char *protocol_name_list;
8964 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008965 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008966
8967 /* If ALPN not configured, just ignore the extension */
8968 if( ssl->conf->alpn_list == NULL )
8969 return( 0 );
8970
8971 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008972 * RFC7301, section 3.1
8973 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008974 *
XiaokangQianc7403452022-06-23 03:24:12 +00008975 * struct {
8976 * ProtocolName protocol_name_list<2..2^16-1>
8977 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008978 */
8979
XiaokangQianc7403452022-06-23 03:24:12 +00008980 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008981 * protocol_name_list_len 2 bytes
8982 * protocol_name_len 1 bytes
8983 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008984 */
XiaokangQianacb39922022-06-17 10:18:48 +00008985 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8986
XiaokangQianc7403452022-06-23 03:24:12 +00008987 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008988 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008989 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008990 protocol_name_list = p;
8991 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008992
8993 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008994 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008995 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008996 protocol_name_len = *p++;
8997 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8998 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008999 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00009000 {
9001 MBEDTLS_SSL_PEND_FATAL_ALERT(
9002 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
9003 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00009004 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00009005 }
9006
9007 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009008 }
9009
9010 /* Use our order of preference */
9011 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
9012 {
9013 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00009014 p = protocol_name_list;
9015 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009016 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009017 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00009018 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00009019 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00009020 {
9021 ssl->alpn_chosen = *alpn;
9022 return( 0 );
9023 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009024
9025 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009026 }
9027 }
9028
XiaokangQian95d5f542022-06-24 02:29:26 +00009029 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009030 MBEDTLS_SSL_PEND_FATAL_ALERT(
9031 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9032 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9033 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9034}
9035
9036int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
9037 unsigned char *buf,
9038 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00009039 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00009040{
9041 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009042 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009043 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009044
9045 if( ssl->alpn_chosen == NULL )
9046 {
9047 return( 0 );
9048 }
9049
XiaokangQian95d5f542022-06-24 02:29:26 +00009050 protocol_name_len = strlen( ssl->alpn_chosen );
9051 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009052
9053 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
9054 /*
9055 * 0 . 1 ext identifier
9056 * 2 . 3 ext length
9057 * 4 . 5 protocol list length
9058 * 6 . 6 protocol name length
9059 * 7 . 7+n protocol name
9060 */
9061 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
9062
XiaokangQian95d5f542022-06-24 02:29:26 +00009063 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009064
XiaokangQian95d5f542022-06-24 02:29:26 +00009065 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
9066 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00009067 /* Note: the length of the chosen protocol has been checked to be less
9068 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9069 */
XiaokangQian95d5f542022-06-24 02:29:26 +00009070 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009071
XiaokangQian95d5f542022-06-24 02:29:26 +00009072 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009073 return ( 0 );
9074}
9075#endif /* MBEDTLS_SSL_ALPN */
9076
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009077#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009078 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009079 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009080 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009081int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
9082 const char *hostname )
9083{
9084 /* Initialize to suppress unnecessary compiler warning */
9085 size_t hostname_len = 0;
9086
9087 /* Check if new hostname is valid before
9088 * making any change to current one */
9089 if( hostname != NULL )
9090 {
9091 hostname_len = strlen( hostname );
9092
9093 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9094 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9095 }
9096
9097 /* Now it's clear that we will overwrite the old hostname,
9098 * so we can free it safely */
9099 if( session->hostname != NULL )
9100 {
9101 mbedtls_platform_zeroize( session->hostname,
9102 strlen( session->hostname ) );
9103 mbedtls_free( session->hostname );
9104 }
9105
9106 /* Passing NULL as hostname shall clear the old one */
9107 if( hostname == NULL )
9108 {
9109 session->hostname = NULL;
9110 }
9111 else
9112 {
9113 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
9114 if( session->hostname == NULL )
9115 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9116
9117 memcpy( session->hostname, hostname, hostname_len );
9118 }
9119
9120 return( 0 );
9121}
Xiaokang Qian03409292022-10-12 02:49:52 +00009122#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9123 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009124 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009125 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009127#endif /* MBEDTLS_SSL_TLS_C */