blob: a3586e74702ca5c4b60712b3d520195ba8c104a8 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
Paul Bakker5121ce52009-01-03 21:22:43 +000020 * http://www.ietf.org/rfc/rfc2246.txt
21 * http://www.ietf.org/rfc/rfc4346.txt
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Jerry Yub476a442022-01-21 18:14:45 +080028#include <assert.h>
29
SimonBd5800b72016-04-26 07:43:27 +010030#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010033#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010034#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000035#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040036
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/debug.h"
38#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010040#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020041#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020042
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <string.h>
44
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050045#if defined(MBEDTLS_USE_PSA_CRYPTO)
46#include "mbedtls/psa_util.h"
47#include "psa/crypto.h"
48#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020049#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050050
Janos Follath23bdca02016-10-07 14:47:14 +010051#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020053#endif
54
Ronald Cronad8c17b2022-06-10 17:18:09 +020055#if defined(MBEDTLS_TEST_HOOKS)
56static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
57
58void mbedtls_ssl_set_chk_buf_ptr_fail_args(
59 const uint8_t *cur, const uint8_t *end, size_t need )
60{
61 chk_buf_ptr_fail_args.cur = cur;
62 chk_buf_ptr_fail_args.end = end;
63 chk_buf_ptr_fail_args.need = need;
64}
65
66void mbedtls_ssl_reset_chk_buf_ptr_fail_args( void )
67{
68 memset( &chk_buf_ptr_fail_args, 0, sizeof( chk_buf_ptr_fail_args ) );
69}
70
71int mbedtls_ssl_cmp_chk_buf_ptr_fail_args( mbedtls_ssl_chk_buf_ptr_args *args )
72{
73 return( ( chk_buf_ptr_fail_args.cur != args->cur ) ||
74 ( chk_buf_ptr_fail_args.end != args->end ) ||
75 ( chk_buf_ptr_fail_args.need != args->need ) );
76}
77#endif /* MBEDTLS_TEST_HOOKS */
78
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010080
Hanno Beckera0e20d02019-05-15 14:03:01 +010081#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010082/* Top-level Connection ID API */
83
Hanno Becker8367ccc2019-05-14 11:30:10 +010084int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
85 size_t len,
86 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +010087{
88 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
89 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
90
Hanno Becker611ac772019-05-14 11:45:26 +010091 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
92 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
93 {
94 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
95 }
96
97 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010098 conf->cid_len = len;
99 return( 0 );
100}
101
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100102int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
103 int enable,
104 unsigned char const *own_cid,
105 size_t own_cid_len )
106{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100107 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
109
Hanno Beckerca092242019-04-25 16:01:49 +0100110 ssl->negotiate_cid = enable;
111 if( enable == MBEDTLS_SSL_CID_DISABLED )
112 {
113 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
114 return( 0 );
115 }
116 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100117 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100118
Hanno Beckerad4a1372019-05-03 13:06:44 +0100119 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100120 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
122 (unsigned) own_cid_len,
123 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
125 }
126
127 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100128 /* Truncation is not an issue here because
129 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
130 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100131
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100132 return( 0 );
133}
134
Paul Elliott0113cf12022-03-11 20:26:47 +0000135int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl,
136 int *enabled,
137 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
138 size_t *own_cid_len )
139{
140 *enabled = MBEDTLS_SSL_CID_DISABLED;
141
142 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
144
145 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
146 * zero as this is indistinguishable from not requesting to use
147 * the CID extension. */
148 if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
149 return( 0 );
150
151 if( own_cid_len != NULL )
152 {
153 *own_cid_len = ssl->own_cid_len;
154 if( own_cid != NULL )
155 memcpy( own_cid, ssl->own_cid, ssl->own_cid_len );
156 }
157
158 *enabled = MBEDTLS_SSL_CID_ENABLED;
159
160 return( 0 );
161}
162
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100163int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
164 int *enabled,
165 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
166 size_t *peer_cid_len )
167{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100168 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100169
Hanno Becker76a79ab2019-05-03 14:38:32 +0100170 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Paul Elliott27b0d942022-03-18 21:55:32 +0000171 mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Hanno Becker76a79ab2019-05-03 14:38:32 +0100172 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100173 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100174 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100175
Hanno Beckerc5f24222019-05-03 12:54:52 +0100176 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
177 * were used, but client and server requested the empty CID.
178 * This is indistinguishable from not using the CID extension
179 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100180 if( ssl->transform_in->in_cid_len == 0 &&
181 ssl->transform_in->out_cid_len == 0 )
182 {
183 return( 0 );
184 }
185
Hanno Becker615ef172019-05-22 16:50:35 +0100186 if( peer_cid_len != NULL )
187 {
188 *peer_cid_len = ssl->transform_in->out_cid_len;
189 if( peer_cid != NULL )
190 {
191 memcpy( peer_cid, ssl->transform_in->out_cid,
192 ssl->transform_in->out_cid_len );
193 }
194 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100195
196 *enabled = MBEDTLS_SSL_CID_ENABLED;
197
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100198 return( 0 );
199}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100200#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200205/*
206 * Convert max_fragment_length codes to length.
207 * RFC 6066 says:
208 * enum{
209 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
210 * } MaxFragmentLength;
211 * and we add 0 -> extension unused
212 */
Angus Grattond8213d02016-05-25 20:56:48 +1000213static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200214{
Angus Grattond8213d02016-05-25 20:56:48 +1000215 switch( mfl )
216 {
217 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
218 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
219 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
220 return 512;
221 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
222 return 1024;
223 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
224 return 2048;
225 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
226 return 4096;
227 default:
228 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
229 }
230}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200232
Hanno Becker52055ae2019-02-06 14:30:46 +0000233int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
234 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_ssl_session_free( dst );
237 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
吴敬辉0b716112021-11-29 10:46:35 +0800238#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
239 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000240#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
241 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000242 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800243#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000244#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000247
248#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200249 if( src->peer_cert != NULL )
250 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000251 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200252
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200253 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200254 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200255 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200260 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263 dst->peer_cert = NULL;
264 return( ret );
265 }
266 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000267#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000268 if( src->peer_cert_digest != NULL )
269 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000270 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000271 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000272 if( dst->peer_cert_digest == NULL )
273 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
274
275 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
276 src->peer_cert_digest_len );
277 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000278 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000279 }
280#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200283
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200284#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200285 if( src->ticket != NULL )
286 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200287 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200288 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200289 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200290
291 memcpy( dst->ticket, src->ticket, src->ticket_len );
292 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000293
294#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
295 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
296 if( src->endpoint == MBEDTLS_SSL_IS_CLIENT )
297 {
298 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
299 ret = mbedtls_ssl_session_set_hostname( dst, src->hostname );
300 if( ret != 0 )
301 return ( ret );
302 }
303#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
304 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200305#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200306
307 return( 0 );
308}
309
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500310#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200311MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500312static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
313{
314 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
315 if( resized_buffer == NULL )
316 return -1;
317
318 /* We want to copy len_new bytes when downsizing the buffer, and
319 * len_old bytes when upsizing, so we choose the smaller of two sizes,
320 * to fit one buffer into another. Size checks, ensuring that no data is
321 * lost, are done outside of this function. */
322 memcpy( resized_buffer, *buffer,
323 ( len_new < *len_old ) ? len_new : *len_old );
324 mbedtls_platform_zeroize( *buffer, *len_old );
325 mbedtls_free( *buffer );
326
327 *buffer = resized_buffer;
328 *len_old = len_new;
329
330 return 0;
331}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200332
333static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500334 size_t in_buf_new_len,
335 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200336{
337 int modified = 0;
338 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
339 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
340 if( ssl->in_buf != NULL )
341 {
342 written_in = ssl->in_msg - ssl->in_buf;
343 iv_offset_in = ssl->in_iv - ssl->in_buf;
344 len_offset_in = ssl->in_len - ssl->in_buf;
345 if( downsizing ?
346 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
347 ssl->in_buf_len < in_buf_new_len )
348 {
349 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
350 {
351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
352 }
353 else
354 {
Paul Elliottb7449902021-03-10 18:14:58 +0000355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
356 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200357 modified = 1;
358 }
359 }
360 }
361
362 if( ssl->out_buf != NULL )
363 {
364 written_out = ssl->out_msg - ssl->out_buf;
365 iv_offset_out = ssl->out_iv - ssl->out_buf;
366 len_offset_out = ssl->out_len - ssl->out_buf;
367 if( downsizing ?
368 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
369 ssl->out_buf_len < out_buf_new_len )
370 {
371 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
372 {
373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
374 }
375 else
376 {
Paul Elliottb7449902021-03-10 18:14:58 +0000377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
378 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200379 modified = 1;
380 }
381 }
382 }
383 if( modified )
384 {
385 /* Update pointers here to avoid doing it twice. */
386 mbedtls_ssl_reset_in_out_pointers( ssl );
387 /* Fields below might not be properly updated with record
388 * splitting or with CID, so they are manually updated here. */
389 ssl->out_msg = ssl->out_buf + written_out;
390 ssl->out_len = ssl->out_buf + len_offset_out;
391 ssl->out_iv = ssl->out_buf + iv_offset_out;
392
393 ssl->in_msg = ssl->in_buf + written_in;
394 ssl->in_len = ssl->in_buf + len_offset_in;
395 ssl->in_iv = ssl->in_buf + iv_offset_in;
396 }
397}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500398#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
399
Jerry Yudb8c48a2022-01-27 14:54:54 +0800400#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800401
402#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
403typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
404 const char *label,
405 const unsigned char *random, size_t rlen,
406 unsigned char *dstbuf, size_t dlen );
407
408static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
409
410#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
411
412/* Type for the TLS PRF */
413typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
414 const unsigned char *, size_t,
415 unsigned char *, size_t);
416
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200417MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800418static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
419 int ciphersuite,
420 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200421#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yued14c932022-02-17 13:40:45 +0800422 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200423#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yued14c932022-02-17 13:40:45 +0800424 ssl_tls_prf_t tls_prf,
425 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -0400426 mbedtls_ssl_protocol_version tls_version,
Jerry Yued14c932022-02-17 13:40:45 +0800427 unsigned endpoint,
428 const mbedtls_ssl_context *ssl );
429
Andrzej Kurek25f27152022-08-17 16:09:31 -0400430#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200431MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800432static int tls_prf_sha256( const unsigned char *secret, size_t slen,
433 const char *label,
434 const unsigned char *random, size_t rlen,
435 unsigned char *dstbuf, size_t dlen );
436static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
437static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
438
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400439#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800440
Andrzej Kurek25f27152022-08-17 16:09:31 -0400441#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200442MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800443static int tls_prf_sha384( const unsigned char *secret, size_t slen,
444 const char *label,
445 const unsigned char *random, size_t rlen,
446 unsigned char *dstbuf, size_t dlen );
447
448static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
449static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400450#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800451
Jerry Yu438ddd82022-07-07 06:55:50 +0000452static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800453 unsigned char *buf,
454 size_t buf_len );
Jerry Yu251a12e2022-07-13 15:15:48 +0800455
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200456MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +0000457static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800458 const unsigned char *buf,
459 size_t len );
460#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
461
Jerry Yu53d23e22022-02-09 16:25:09 +0800462static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
463
Andrzej Kurek25f27152022-08-17 16:09:31 -0400464#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800465static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400466#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudb8c48a2022-01-27 14:54:54 +0800467
Andrzej Kurek25f27152022-08-17 16:09:31 -0400468#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800469static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400470#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Paul Bakker1ef83d62012-04-11 12:09:53 +0000471
Ron Eldor51d3ab52019-05-12 14:54:30 +0300472int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
473 const unsigned char *secret, size_t slen,
474 const char *label,
475 const unsigned char *random, size_t rlen,
476 unsigned char *dstbuf, size_t dlen )
477{
478 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
479
480 switch( prf )
481 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300482#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400483#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300484 case MBEDTLS_SSL_TLS_PRF_SHA384:
485 tls_prf = tls_prf_sha384;
486 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400487#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400488#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300489 case MBEDTLS_SSL_TLS_PRF_SHA256:
490 tls_prf = tls_prf_sha256;
491 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400492#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300493#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300494 default:
495 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
496 }
497
498 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
499}
500
Jerry Yuc73c6182022-02-08 20:29:25 +0800501#if defined(MBEDTLS_X509_CRT_PARSE_C)
502static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
503{
504#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
505 if( session->peer_cert != NULL )
506 {
507 mbedtls_x509_crt_free( session->peer_cert );
508 mbedtls_free( session->peer_cert );
509 session->peer_cert = NULL;
510 }
511#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
512 if( session->peer_cert_digest != NULL )
513 {
514 /* Zeroization is not necessary. */
515 mbedtls_free( session->peer_cert_digest );
516 session->peer_cert_digest = NULL;
517 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
518 session->peer_cert_digest_len = 0;
519 }
520#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
521}
522#endif /* MBEDTLS_X509_CRT_PARSE_C */
523
524void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
525 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
526{
527 ((void) ciphersuite_info);
528
Andrzej Kurek25f27152022-08-17 16:09:31 -0400529#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800530 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
531 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
532 else
533#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400534#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800535 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
536 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
537 else
538#endif
539 {
540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
541 return;
542 }
543}
544
XiaokangQianadab9a62022-07-18 07:41:26 +0000545void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
546 unsigned hs_type,
547 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100548{
549 unsigned char hs_hdr[4];
550
551 /* Build HS header for checksum update. */
552 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
553 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
554 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
555 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
556
557 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
558}
559
560void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
561 unsigned hs_type,
562 unsigned char const *msg,
563 size_t msg_len )
564{
565 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
566 ssl->handshake->update_checksum( ssl, msg, msg_len );
567}
568
Jerry Yuc73c6182022-02-08 20:29:25 +0800569void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
570{
571 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400572#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800573#if defined(MBEDTLS_USE_PSA_CRYPTO)
574 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
575 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
576#else
577 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
578#endif
579#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400580#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800581#if defined(MBEDTLS_USE_PSA_CRYPTO)
582 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
583 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
584#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400585 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800586#endif
587#endif
588}
589
590static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
591 const unsigned char *buf, size_t len )
592{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400593#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800594#if defined(MBEDTLS_USE_PSA_CRYPTO)
595 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
596#else
597 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
598#endif
599#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400600#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800601#if defined(MBEDTLS_USE_PSA_CRYPTO)
602 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
603#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400604 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800605#endif
606#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -0400607#if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
608 !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
609 (void) ssl;
610 (void) buf;
611 (void) len;
612#endif
Jerry Yuc73c6182022-02-08 20:29:25 +0800613}
614
Andrzej Kurek25f27152022-08-17 16:09:31 -0400615#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800616static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
617 const unsigned char *buf, size_t len )
618{
619#if defined(MBEDTLS_USE_PSA_CRYPTO)
620 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
621#else
622 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
623#endif
624}
625#endif
626
Andrzej Kurek25f27152022-08-17 16:09:31 -0400627#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800628static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
629 const unsigned char *buf, size_t len )
630{
631#if defined(MBEDTLS_USE_PSA_CRYPTO)
632 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
633#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400634 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800635#endif
636}
637#endif
638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200640{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200642
Andrzej Kurek25f27152022-08-17 16:09:31 -0400643#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500644#if defined(MBEDTLS_USE_PSA_CRYPTO)
645 handshake->fin_sha256_psa = psa_hash_operation_init();
646 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
647#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200649 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200650#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500651#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400652#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500653#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500654 handshake->fin_sha384_psa = psa_hash_operation_init();
655 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500656#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400657 mbedtls_sha512_init( &handshake->fin_sha384 );
658 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200659#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500660#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200661
662 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664#if defined(MBEDTLS_DHM_C)
665 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200666#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200667#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200669#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200670#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200671 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200672#if defined(MBEDTLS_SSL_CLI_C)
673 handshake->ecjpake_cache = NULL;
674 handshake->ecjpake_cache_len = 0;
675#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200676#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200677
Gilles Peskineeccd8882020-03-10 12:19:08 +0100678#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200679 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200680#endif
681
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200682#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
683 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
684#endif
Hanno Becker75173122019-02-06 16:18:31 +0000685
686#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
687 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
688 mbedtls_pk_init( &handshake->peer_pubkey );
689#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200690}
691
Hanno Beckera18d1322018-01-03 14:27:32 +0000692void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200693{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200695
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100696#if defined(MBEDTLS_USE_PSA_CRYPTO)
697 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
698 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100699#else
700 mbedtls_cipher_init( &transform->cipher_ctx_enc );
701 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100702#endif
703
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000704#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100705#if defined(MBEDTLS_USE_PSA_CRYPTO)
706 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
707 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100708#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_md_init( &transform->md_ctx_enc );
710 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000711#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100712#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200713}
714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200716{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200718}
719
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200720MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000722{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200723 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000724 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200726 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200728 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200729 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200730
731 /*
732 * Either the pointers are now NULL or cleared properly and can be freed.
733 * Now allocate missing structures.
734 */
735 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200736 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200737 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200738 }
Paul Bakker48916f92012-09-16 19:57:18 +0000739
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200740 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200741 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200742 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200743 }
Paul Bakker48916f92012-09-16 19:57:18 +0000744
Paul Bakker82788fb2014-10-20 13:59:19 +0200745 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200746 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200747 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200748 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500749#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
750 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400751
Andrzej Kurek4a063792020-10-21 15:08:44 +0200752 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
753 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500754#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000755
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200756 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000757 if( ssl->handshake == NULL ||
758 ssl->transform_negotiate == NULL ||
759 ssl->session_negotiate == NULL )
760 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 mbedtls_free( ssl->handshake );
764 mbedtls_free( ssl->transform_negotiate );
765 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200766
767 ssl->handshake = NULL;
768 ssl->transform_negotiate = NULL;
769 ssl->session_negotiate = NULL;
770
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200771 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000772 }
773
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200774 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000776 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200777 ssl_handshake_params_init( ssl->handshake );
778
Jerry Yud0766ec2022-09-22 10:46:57 +0800779#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
780 defined(MBEDTLS_SSL_SRV_C) && \
781 defined(MBEDTLS_SSL_SESSION_TICKETS)
782 ssl->handshake->new_session_tickets_count =
783 ssl->conf->new_session_tickets_count ;
784#endif
785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200787 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
788 {
789 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200790
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200791 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
792 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
793 else
794 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200795
Hanno Becker0f57a652020-02-05 10:37:26 +0000796 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200797 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200798#endif
799
Brett Warrene0edc842021-08-17 09:53:13 +0100800/*
801 * curve_list is translated to IANA TLS group identifiers here because
802 * mbedtls_ssl_conf_curves returns void and so can't return
803 * any error codes.
804 */
805#if defined(MBEDTLS_ECP_C)
806#if !defined(MBEDTLS_DEPRECATED_REMOVED)
807 /* Heap allocate and translate curve_list from internal to IANA group ids */
808 if ( ssl->conf->curve_list != NULL )
809 {
810 size_t length;
811 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
812
813 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
814 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
815
816 /* Leave room for zero termination */
817 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
818 if ( group_list == NULL )
819 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
820
821 for( size_t i = 0; i < length; i++ )
822 {
823 const mbedtls_ecp_curve_info *info =
824 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
825 if ( info == NULL )
826 {
827 mbedtls_free( group_list );
828 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
829 }
830 group_list[i] = info->tls_id;
831 }
832
833 group_list[length] = 0;
834
835 ssl->handshake->group_list = group_list;
836 ssl->handshake->group_list_heap_allocated = 1;
837 }
838 else
839 {
840 ssl->handshake->group_list = ssl->conf->group_list;
841 ssl->handshake->group_list_heap_allocated = 0;
842 }
843#endif /* MBEDTLS_DEPRECATED_REMOVED */
844#endif /* MBEDTLS_ECP_C */
845
Jerry Yuf017ee42022-01-12 15:49:48 +0800846#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
847#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800848#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800849 /* Heap allocate and translate sig_hashes from internal hash identifiers to
850 signature algorithms IANA identifiers. */
851 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800852 ssl->conf->sig_hashes != NULL )
853 {
854 const int *md;
855 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800856 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800857 uint16_t *p;
858
Jerry Yub476a442022-01-21 18:14:45 +0800859#if defined(static_assert)
860 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
861 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
862 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800863#endif
864
Jerry Yuf017ee42022-01-12 15:49:48 +0800865 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
866 {
867 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
868 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800869#if defined(MBEDTLS_ECDSA_C)
870 sig_algs_len += sizeof( uint16_t );
871#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800872
Jerry Yub476a442022-01-21 18:14:45 +0800873#if defined(MBEDTLS_RSA_C)
874 sig_algs_len += sizeof( uint16_t );
875#endif
876 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
877 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800878 }
879
Jerry Yub476a442022-01-21 18:14:45 +0800880 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800881 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
882
Jerry Yub476a442022-01-21 18:14:45 +0800883 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
884 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800885 if( ssl->handshake->sig_algs == NULL )
886 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
887
888 p = (uint16_t *)ssl->handshake->sig_algs;
889 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
890 {
891 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
892 if( hash == MBEDTLS_SSL_HASH_NONE )
893 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800894#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800895 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
896 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800897#endif
898#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800899 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
900 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800901#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800902 }
Gabor Mezei15b95a62022-05-09 16:37:58 +0200903 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +0800904 ssl->handshake->sig_algs_heap_allocated = 1;
905 }
906 else
Jerry Yua69269a2022-01-17 21:06:01 +0800907#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800908 {
Jerry Yuf017ee42022-01-12 15:49:48 +0800909 ssl->handshake->sig_algs_heap_allocated = 0;
910 }
Jerry Yucc539102022-06-27 16:27:35 +0800911#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yuf017ee42022-01-12 15:49:48 +0800912#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000913 return( 0 );
914}
915
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200916#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200917/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200918MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200919static int ssl_cookie_write_dummy( void *ctx,
920 unsigned char **p, unsigned char *end,
921 const unsigned char *cli_id, size_t cli_id_len )
922{
923 ((void) ctx);
924 ((void) p);
925 ((void) end);
926 ((void) cli_id);
927 ((void) cli_id_len);
928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200930}
931
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200932MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200933static int ssl_cookie_check_dummy( void *ctx,
934 const unsigned char *cookie, size_t cookie_len,
935 const unsigned char *cli_id, size_t cli_id_len )
936{
937 ((void) ctx);
938 ((void) cookie);
939 ((void) cookie_len);
940 ((void) cli_id);
941 ((void) cli_id_len);
942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200944}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200945#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200946
Paul Bakker5121ce52009-01-03 21:22:43 +0000947/*
948 * Initialize an SSL context
949 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200950void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
951{
952 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
953}
954
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200955MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800956static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
957{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100958 const mbedtls_ssl_config *conf = ssl->conf;
959
Ronald Cron6f135e12021-12-08 16:57:54 +0100960#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100961 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
962 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000963 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
964 {
965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
966 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
967 }
968
Jerry Yu60835a82021-08-04 10:13:52 +0800969 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
970 return( 0 );
971 }
972#endif
973
974#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100975 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800976 {
977 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
978 return( 0 );
979 }
980#endif
981
Ronald Cron6f135e12021-12-08 16:57:54 +0100982#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100983 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800984 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000985 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
986 {
987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
988 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
989 }
990
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000991 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
992 {
993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
994 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
995 }
996
997
Ronald Crone1d3f062022-02-10 14:50:54 +0100998 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
999 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +08001000 }
1001#endif
1002
1003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
1004 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1005}
1006
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001007MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001008static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1009{
1010 int ret;
1011 ret = ssl_conf_version_check( ssl );
1012 if( ret != 0 )
1013 return( ret );
1014
1015 /* Space for further checks */
1016
1017 return( 0 );
1018}
1019
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001020/*
1021 * Setup an SSL context
1022 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001023
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001024int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001025 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001026{
Janos Follath865b3eb2019-12-16 11:46:15 +00001027 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001028 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1029 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001030
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001031 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001032
Jerry Yu60835a82021-08-04 10:13:52 +08001033 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1034 return( ret );
1035
Paul Bakker62f2dee2012-09-28 07:31:51 +00001036 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001037 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001038 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001039
1040 /* Set to NULL in case of an error condition */
1041 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001042
Darryl Greenb33cc762019-11-28 14:29:44 +00001043#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1044 ssl->in_buf_len = in_buf_len;
1045#endif
1046 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001047 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001048 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001050 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001051 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001052 }
1053
Darryl Greenb33cc762019-11-28 14:29:44 +00001054#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1055 ssl->out_buf_len = out_buf_len;
1056#endif
1057 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001058 if( ssl->out_buf == NULL )
1059 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001061 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001062 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001063 }
1064
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001065 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001066
Johan Pascalb62bb512015-12-03 21:56:45 +01001067#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001068 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001069#endif
1070
Paul Bakker48916f92012-09-16 19:57:18 +00001071 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001072 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001073
1074 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001075
1076error:
1077 mbedtls_free( ssl->in_buf );
1078 mbedtls_free( ssl->out_buf );
1079
1080 ssl->conf = NULL;
1081
Darryl Greenb33cc762019-11-28 14:29:44 +00001082#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1083 ssl->in_buf_len = 0;
1084 ssl->out_buf_len = 0;
1085#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001086 ssl->in_buf = NULL;
1087 ssl->out_buf = NULL;
1088
1089 ssl->in_hdr = NULL;
1090 ssl->in_ctr = NULL;
1091 ssl->in_len = NULL;
1092 ssl->in_iv = NULL;
1093 ssl->in_msg = NULL;
1094
1095 ssl->out_hdr = NULL;
1096 ssl->out_ctr = NULL;
1097 ssl->out_len = NULL;
1098 ssl->out_iv = NULL;
1099 ssl->out_msg = NULL;
1100
k-stachowiak9f7798e2018-07-31 16:52:32 +02001101 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001102}
1103
1104/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001105 * Reset an initialized and used SSL context for re-use while retaining
1106 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001107 *
1108 * If partial is non-zero, keep data in the input buffer and client ID.
1109 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001110 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001111void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1112 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001113{
Darryl Greenb33cc762019-11-28 14:29:44 +00001114#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1115 size_t in_buf_len = ssl->in_buf_len;
1116 size_t out_buf_len = ssl->out_buf_len;
1117#else
1118 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1119 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1120#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001121
Hanno Beckerb0302c42021-08-03 09:39:42 +01001122#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1123 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001124#endif
1125
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001126 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001127 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001128
Hanno Beckerb0302c42021-08-03 09:39:42 +01001129 mbedtls_ssl_reset_in_out_pointers( ssl );
1130
1131 /* Reset incoming message parsing */
1132 ssl->in_offt = NULL;
1133 ssl->nb_zero = 0;
1134 ssl->in_msgtype = 0;
1135 ssl->in_msglen = 0;
1136 ssl->in_hslen = 0;
1137 ssl->keep_current_message = 0;
1138 ssl->transform_in = NULL;
1139
1140#if defined(MBEDTLS_SSL_PROTO_DTLS)
1141 ssl->next_record_offset = 0;
1142 ssl->in_epoch = 0;
1143#endif
1144
1145 /* Keep current datagram if partial == 1 */
1146 if( partial == 0 )
1147 {
1148 ssl->in_left = 0;
1149 memset( ssl->in_buf, 0, in_buf_len );
1150 }
1151
Ronald Cronad8c17b2022-06-10 17:18:09 +02001152 ssl->send_alert = 0;
1153
Hanno Beckerb0302c42021-08-03 09:39:42 +01001154 /* Reset outgoing message writing */
1155 ssl->out_msgtype = 0;
1156 ssl->out_msglen = 0;
1157 ssl->out_left = 0;
1158 memset( ssl->out_buf, 0, out_buf_len );
1159 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1160 ssl->transform_out = NULL;
1161
1162#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1163 mbedtls_ssl_dtls_replay_reset( ssl );
1164#endif
1165
XiaokangQian2b01dc32022-01-21 02:53:13 +00001166#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001167 if( ssl->transform )
1168 {
1169 mbedtls_ssl_transform_free( ssl->transform );
1170 mbedtls_free( ssl->transform );
1171 ssl->transform = NULL;
1172 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001173#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1174
XiaokangQian2b01dc32022-01-21 02:53:13 +00001175#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1176 mbedtls_ssl_transform_free( ssl->transform_application );
1177 mbedtls_free( ssl->transform_application );
1178 ssl->transform_application = NULL;
1179
1180 if( ssl->handshake != NULL )
1181 {
1182 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1183 mbedtls_free( ssl->handshake->transform_earlydata );
1184 ssl->handshake->transform_earlydata = NULL;
1185
1186 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1187 mbedtls_free( ssl->handshake->transform_handshake );
1188 ssl->handshake->transform_handshake = NULL;
1189 }
1190
XiaokangQian2b01dc32022-01-21 02:53:13 +00001191#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001192}
1193
1194int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1195{
1196 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1197
1198 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1199
XiaokangQian78b1fa72022-01-19 06:56:30 +00001200 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001201
1202 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203#if defined(MBEDTLS_SSL_RENEGOTIATION)
1204 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001205 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001206
1207 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1209 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001210#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001212
Hanno Beckerb0302c42021-08-03 09:39:42 +01001213 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001214 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001215 if( ssl->session )
1216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 mbedtls_ssl_session_free( ssl->session );
1218 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001219 ssl->session = NULL;
1220 }
1221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001223 ssl->alpn_chosen = NULL;
1224#endif
1225
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001226#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01001227#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001228 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01001229#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001230 {
1231 mbedtls_free( ssl->cli_id );
1232 ssl->cli_id = NULL;
1233 ssl->cli_id_len = 0;
1234 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001235#endif
1236
Paul Bakker48916f92012-09-16 19:57:18 +00001237 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1238 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001239
1240 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001241}
1242
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001243/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001244 * Reset an initialized and used SSL context for re-use while retaining
1245 * all application-set variables, function pointers and data.
1246 */
1247int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1248{
Hanno Becker43aefe22020-02-05 10:44:56 +00001249 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001250}
1251
1252/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001253 * SSL set accessors
1254 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001255void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001256{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001257 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001258}
1259
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001260void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001261{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001262 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001263}
1264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001266void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001267{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001268 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001269}
1270#endif
1271
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001272void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001273{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001274 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001275}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001278
Hanno Becker1841b0a2018-08-24 11:13:57 +01001279void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1280 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001281{
1282 ssl->disable_datagram_packing = !allow_packing;
1283}
1284
1285void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1286 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001287{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001288 conf->hs_timeout_min = min;
1289 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001290}
1291#endif
1292
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001293void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001294{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001295 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001296}
1297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001299void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001300 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001301 void *p_vrfy )
1302{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001303 conf->f_vrfy = f_vrfy;
1304 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001305}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001307
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001308void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001309 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001310 void *p_rng )
1311{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001312 conf->f_rng = f_rng;
1313 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001314}
1315
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001316void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001317 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001318 void *p_dbg )
1319{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001320 conf->f_dbg = f_dbg;
1321 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001322}
1323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001325 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001326 mbedtls_ssl_send_t *f_send,
1327 mbedtls_ssl_recv_t *f_recv,
1328 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001329{
1330 ssl->p_bio = p_bio;
1331 ssl->f_send = f_send;
1332 ssl->f_recv = f_recv;
1333 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001334}
1335
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001336#if defined(MBEDTLS_SSL_PROTO_DTLS)
1337void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1338{
1339 ssl->mtu = mtu;
1340}
1341#endif
1342
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001343void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001344{
1345 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001346}
1347
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001348void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1349 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001350 mbedtls_ssl_set_timer_t *f_set_timer,
1351 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001352{
1353 ssl->p_timer = p_timer;
1354 ssl->f_set_timer = f_set_timer;
1355 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001356
1357 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001358 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001359}
1360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001362void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001363 void *p_cache,
1364 mbedtls_ssl_cache_get_t *f_get_cache,
1365 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001366{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001367 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001368 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001369 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001370}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373#if defined(MBEDTLS_SSL_CLI_C)
1374int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001375{
Janos Follath865b3eb2019-12-16 11:46:15 +00001376 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001377
1378 if( ssl == NULL ||
1379 session == NULL ||
1380 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001381 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001384 }
1385
Hanno Beckere810bbc2021-05-14 16:01:05 +01001386 if( ssl->handshake->resume == 1 )
1387 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1388
Jerry Yu21092062022-10-10 21:21:31 +08001389#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1390 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001391 {
Jerry Yu21092062022-10-10 21:21:31 +08001392 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1393 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1394
1395 if( mbedtls_ssl_validate_ciphersuite(
1396 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1397 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1398 {
1399 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1400 session->ciphersuite ) );
1401 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1402 }
Jerry Yu40afab62022-10-08 10:42:13 +08001403 }
Jerry Yu21092062022-10-10 21:21:31 +08001404#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001405
Hanno Becker52055ae2019-02-06 14:30:46 +00001406 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1407 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001408 return( ret );
1409
Paul Bakker0a597072012-09-25 21:55:46 +00001410 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001411
1412 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001413}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001415
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001416void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1417 const int *ciphersuites )
1418{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001419 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001420}
1421
Ronald Cron6f135e12021-12-08 16:57:54 +01001422#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001423void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001424 const int kex_modes )
1425{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001426 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001427}
Ronald Cron6f135e12021-12-08 16:57:54 +01001428#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001431void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001432 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001433{
1434 conf->cert_profile = profile;
1435}
1436
Glenn Strauss36872db2022-01-22 05:06:31 -05001437static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1438{
1439 mbedtls_ssl_key_cert *cur = key_cert, *next;
1440
1441 while( cur != NULL )
1442 {
1443 next = cur->next;
1444 mbedtls_free( cur );
1445 cur = next;
1446 }
1447}
1448
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001449/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001450MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001451static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1452 mbedtls_x509_crt *cert,
1453 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001454{
niisato8ee24222018-06-25 19:05:48 +09001455 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001456
Glenn Strauss36872db2022-01-22 05:06:31 -05001457 if( cert == NULL )
1458 {
1459 /* Free list if cert is null */
1460 ssl_key_cert_free( *head );
1461 *head = NULL;
1462 return( 0 );
1463 }
1464
niisato8ee24222018-06-25 19:05:48 +09001465 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1466 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001467 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001468
niisato8ee24222018-06-25 19:05:48 +09001469 new_cert->cert = cert;
1470 new_cert->key = key;
1471 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001472
Glenn Strauss36872db2022-01-22 05:06:31 -05001473 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001474 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001475 {
niisato8ee24222018-06-25 19:05:48 +09001476 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001477 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001478 else
1479 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001480 mbedtls_ssl_key_cert *cur = *head;
1481 while( cur->next != NULL )
1482 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001483 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001484 }
1485
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001486 return( 0 );
1487}
1488
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001489int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001490 mbedtls_x509_crt *own_cert,
1491 mbedtls_pk_context *pk_key )
1492{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001493 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001494}
1495
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001496void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001497 mbedtls_x509_crt *ca_chain,
1498 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001499{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001500 conf->ca_chain = ca_chain;
1501 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001502
1503#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1504 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1505 * cannot be used together. */
1506 conf->f_ca_cb = NULL;
1507 conf->p_ca_cb = NULL;
1508#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001509}
Hanno Becker5adaad92019-03-27 16:54:37 +00001510
1511#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1512void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001513 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001514 void *p_ca_cb )
1515{
1516 conf->f_ca_cb = f_ca_cb;
1517 conf->p_ca_cb = p_ca_cb;
1518
1519 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1520 * cannot be used together. */
1521 conf->ca_chain = NULL;
1522 conf->ca_crl = NULL;
1523}
1524#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001526
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001527#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001528const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1529 size_t *name_len )
1530{
1531 *name_len = ssl->handshake->sni_name_len;
1532 return( ssl->handshake->sni_name );
1533}
1534
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001535int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1536 mbedtls_x509_crt *own_cert,
1537 mbedtls_pk_context *pk_key )
1538{
1539 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1540 own_cert, pk_key ) );
1541}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001542
1543void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1544 mbedtls_x509_crt *ca_chain,
1545 mbedtls_x509_crl *ca_crl )
1546{
1547 ssl->handshake->sni_ca_chain = ca_chain;
1548 ssl->handshake->sni_ca_crl = ca_crl;
1549}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001550
Glenn Strauss999ef702022-03-11 01:37:23 -05001551#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1552void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1553 const mbedtls_x509_crt *crt)
1554{
1555 ssl->handshake->dn_hints = crt;
1556}
1557#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1558
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001559void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1560 int authmode )
1561{
1562 ssl->handshake->sni_authmode = authmode;
1563}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001564#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1565
Hanno Becker8927c832019-04-03 12:52:50 +01001566#if defined(MBEDTLS_X509_CRT_PARSE_C)
1567void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1568 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1569 void *p_vrfy )
1570{
1571 ssl->f_vrfy = f_vrfy;
1572 ssl->p_vrfy = p_vrfy;
1573}
1574#endif
1575
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001576#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001577/*
1578 * Set EC J-PAKE password for current handshake
1579 */
1580int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1581 const unsigned char *pw,
1582 size_t pw_len )
1583{
1584 mbedtls_ecjpake_role role;
1585
Janos Follath8eb64132016-06-03 15:40:57 +01001586 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001587 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1588
1589 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1590 role = MBEDTLS_ECJPAKE_SERVER;
1591 else
1592 role = MBEDTLS_ECJPAKE_CLIENT;
1593
1594 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1595 role,
1596 MBEDTLS_MD_SHA256,
1597 MBEDTLS_ECP_DP_SECP256R1,
1598 pw, pw_len ) );
1599}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001600#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001601
Gilles Peskineeccd8882020-03-10 12:19:08 +01001602#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001603
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001604MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001605static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1606{
1607#if defined(MBEDTLS_USE_PSA_CRYPTO)
1608 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1609 return( 1 );
1610#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001611 if( conf->psk != NULL )
1612 return( 1 );
1613
1614 return( 0 );
1615}
1616
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001617static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1618{
1619 /* Remove reference to existing PSK, if any. */
1620#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001621 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001622 {
1623 /* The maintenance of the PSK key slot is the
1624 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001625 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001626 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001627#endif /* MBEDTLS_USE_PSA_CRYPTO */
1628 if( conf->psk != NULL )
1629 {
1630 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1631
1632 mbedtls_free( conf->psk );
1633 conf->psk = NULL;
1634 conf->psk_len = 0;
1635 }
1636
1637 /* Remove reference to PSK identity, if any. */
1638 if( conf->psk_identity != NULL )
1639 {
1640 mbedtls_free( conf->psk_identity );
1641 conf->psk_identity = NULL;
1642 conf->psk_identity_len = 0;
1643 }
1644}
1645
Hanno Becker7390c712018-11-15 13:33:04 +00001646/* This function assumes that PSK identity in the SSL config is unset.
1647 * It checks that the provided identity is well-formed and attempts
1648 * to make a copy of it in the SSL config.
1649 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001650MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001651static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1652 unsigned char const *psk_identity,
1653 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001654{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001655 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001656 if( psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02001657 psk_identity_len == 0 ||
Hanno Becker7390c712018-11-15 13:33:04 +00001658 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001659 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001660 {
1661 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1662 }
1663
Hanno Becker7390c712018-11-15 13:33:04 +00001664 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1665 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001666 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001667
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001668 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001669 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001670
1671 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001672}
1673
Hanno Becker7390c712018-11-15 13:33:04 +00001674int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1675 const unsigned char *psk, size_t psk_len,
1676 const unsigned char *psk_identity, size_t psk_identity_len )
1677{
Janos Follath865b3eb2019-12-16 11:46:15 +00001678 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001679
1680 /* We currently only support one PSK, raw or opaque. */
1681 if( ssl_conf_psk_is_configured( conf ) )
1682 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001683
1684 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001685 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001686 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001687 if( psk_len == 0 )
1688 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1689 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1690 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1691
Hanno Becker7390c712018-11-15 13:33:04 +00001692 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1693 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1694 conf->psk_len = psk_len;
1695 memcpy( conf->psk, psk, conf->psk_len );
1696
1697 /* Check and set PSK Identity */
1698 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1699 if( ret != 0 )
1700 ssl_conf_remove_psk( conf );
1701
1702 return( ret );
1703}
1704
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001705static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1706{
1707#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001708 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001709 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001710 /* The maintenance of the external PSK key slot is the
1711 * user's responsibility. */
1712 if( ssl->handshake->psk_opaque_is_internal )
1713 {
1714 psa_destroy_key( ssl->handshake->psk_opaque );
1715 ssl->handshake->psk_opaque_is_internal = 0;
1716 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001717 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001718 }
Neil Armstronge952a302022-05-03 10:22:14 +02001719#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001720 if( ssl->handshake->psk != NULL )
1721 {
1722 mbedtls_platform_zeroize( ssl->handshake->psk,
1723 ssl->handshake->psk_len );
1724 mbedtls_free( ssl->handshake->psk );
1725 ssl->handshake->psk_len = 0;
1726 }
Neil Armstronge952a302022-05-03 10:22:14 +02001727#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001728}
1729
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001730int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1731 const unsigned char *psk, size_t psk_len )
1732{
Neil Armstrong501c9322022-05-03 09:35:09 +02001733#if defined(MBEDTLS_USE_PSA_CRYPTO)
1734 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001735 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001736 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001737 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001738#endif /* MBEDTLS_USE_PSA_CRYPTO */
1739
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001740 if( psk == NULL || ssl->handshake == NULL )
1741 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1742
1743 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1744 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1745
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001746 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001747
Neil Armstrong501c9322022-05-03 09:35:09 +02001748#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001749#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1750 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1751 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001752 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001753 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1754 else
1755 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1756 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1757 }
1758#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001759
Jerry Yu568ec252022-07-22 21:27:34 +08001760#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001761 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1762 {
1763 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1764 psa_set_key_usage_flags( &key_attributes,
1765 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1766 }
1767#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1768
Neil Armstrong501c9322022-05-03 09:35:09 +02001769 psa_set_key_algorithm( &key_attributes, alg );
1770 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1771
1772 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1773 if( status != PSA_SUCCESS )
1774 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1775
1776 /* Allow calling psa_destroy_key() on psk remove */
1777 ssl->handshake->psk_opaque_is_internal = 1;
1778 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1779#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001780 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001781 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001782
1783 ssl->handshake->psk_len = psk_len;
1784 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1785
1786 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001787#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001788}
1789
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001790#if defined(MBEDTLS_USE_PSA_CRYPTO)
1791int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001792 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001793 const unsigned char *psk_identity,
1794 size_t psk_identity_len )
1795{
Janos Follath865b3eb2019-12-16 11:46:15 +00001796 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001797
1798 /* We currently only support one PSK, raw or opaque. */
1799 if( ssl_conf_psk_is_configured( conf ) )
1800 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001801
Hanno Becker7390c712018-11-15 13:33:04 +00001802 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001803 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001804 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001805 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001806
1807 /* Check and set PSK Identity */
1808 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1809 psk_identity_len );
1810 if( ret != 0 )
1811 ssl_conf_remove_psk( conf );
1812
1813 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001814}
1815
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001816int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1817 mbedtls_svc_key_id_t psk )
1818{
1819 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1820 ( ssl->handshake == NULL ) )
1821 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1822
1823 ssl_remove_psk( ssl );
1824 ssl->handshake->psk_opaque = psk;
1825 return( 0 );
1826}
1827#endif /* MBEDTLS_USE_PSA_CRYPTO */
1828
Jerry Yu8897c072022-08-12 13:56:53 +08001829#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001830void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1831 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1832 size_t),
1833 void *p_psk )
1834{
1835 conf->f_psk = f_psk;
1836 conf->p_psk = p_psk;
1837}
Jerry Yu8897c072022-08-12 13:56:53 +08001838#endif /* MBEDTLS_SSL_SRV_C */
1839
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001840#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1841
1842#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001843static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1844 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001845{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001846#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001847 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001848 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001849#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001850 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001851 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001852 return( MBEDTLS_SSL_MODE_STREAM );
1853}
1854
1855#else /* MBEDTLS_USE_PSA_CRYPTO */
1856
1857static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1858 mbedtls_cipher_mode_t mode )
1859{
1860#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1861 if( mode == MBEDTLS_MODE_CBC )
1862 return( MBEDTLS_SSL_MODE_CBC );
1863#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1864
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001865#if defined(MBEDTLS_GCM_C) || \
1866 defined(MBEDTLS_CCM_C) || \
1867 defined(MBEDTLS_CHACHAPOLY_C)
1868 if( mode == MBEDTLS_MODE_GCM ||
1869 mode == MBEDTLS_MODE_CCM ||
1870 mode == MBEDTLS_MODE_CHACHAPOLY )
1871 return( MBEDTLS_SSL_MODE_AEAD );
1872#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001873
1874 return( MBEDTLS_SSL_MODE_STREAM );
1875}
Gilles Peskine301711e2022-04-26 16:57:05 +02001876#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001877
Gilles Peskinee108d982022-04-26 16:50:40 +02001878static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1879 mbedtls_ssl_mode_t base_mode,
1880 int encrypt_then_mac )
1881{
1882#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1883 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1884 base_mode == MBEDTLS_SSL_MODE_CBC )
1885 {
1886 return( MBEDTLS_SSL_MODE_CBC_ETM );
1887 }
1888#else
1889 (void) encrypt_then_mac;
1890#endif
1891 return( base_mode );
1892}
1893
Neil Armstrongab555e02022-04-04 11:07:59 +02001894mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001895 const mbedtls_ssl_transform *transform )
1896{
Gilles Peskinee108d982022-04-26 16:50:40 +02001897 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001898#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001899 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001900#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001901 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1902#endif
1903 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001904
Gilles Peskinee108d982022-04-26 16:50:40 +02001905 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001906#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001907 encrypt_then_mac = transform->encrypt_then_mac;
1908#endif
1909 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001910}
1911
Neil Armstrongab555e02022-04-04 11:07:59 +02001912mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001913#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001914 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001915#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001916 const mbedtls_ssl_ciphersuite_t *suite )
1917{
Gilles Peskinee108d982022-04-26 16:50:40 +02001918 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1919
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001920#if defined(MBEDTLS_USE_PSA_CRYPTO)
1921 psa_status_t status;
1922 psa_algorithm_t alg;
1923 psa_key_type_t type;
1924 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001925 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1926 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001927 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001928#else
1929 const mbedtls_cipher_info_t *cipher =
1930 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001931 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001932 {
1933 base_mode =
1934 mbedtls_ssl_get_base_mode(
1935 mbedtls_cipher_info_get_mode( cipher ) );
1936 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001937#endif /* MBEDTLS_USE_PSA_CRYPTO */
1938
Gilles Peskinee108d982022-04-26 16:50:40 +02001939#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1940 int encrypt_then_mac = 0;
1941#endif
1942 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001943}
1944
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001945#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001946
1947#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001948/* Serialization of TLS 1.3 sessions:
1949 *
1950 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00001951 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001952 * uint64 ticket_received;
1953 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08001954 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001955 * } ClientOnlyData;
1956 *
1957 * struct {
1958 * uint8 endpoint;
1959 * uint8 ciphersuite[2];
1960 * uint32 ticket_age_add;
1961 * uint8 ticket_flags;
1962 * opaque resumption_key<0..255>;
1963 * select ( endpoint ) {
1964 * case client: ClientOnlyData;
1965 * case server: uint64 start_time;
1966 * };
1967 * } serialized_session_tls13;
1968 *
1969 */
1970#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08001971MBEDTLS_CHECK_RETURN_CRITICAL
1972static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
1973 unsigned char *buf,
1974 size_t buf_len,
1975 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00001976{
1977 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00001978#if defined(MBEDTLS_SSL_CLI_C) && \
1979 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1980 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00001981 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00001982#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08001983 size_t needed = 1 /* endpoint */
1984 + 2 /* ciphersuite */
1985 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08001986 + 1 /* ticket_flags */
1987 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08001988 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08001989
1990 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
1991 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1992 needed += session->resumption_key_len; /* resumption_key */
1993
Jerry Yu438ddd82022-07-07 06:55:50 +00001994#if defined(MBEDTLS_HAVE_TIME)
1995 needed += 8; /* start_time or ticket_received */
1996#endif
1997
1998#if defined(MBEDTLS_SSL_CLI_C)
1999 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2000 {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002001#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002002 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002003 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002004#endif
2005
Jerry Yu438ddd82022-07-07 06:55:50 +00002006 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002007 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002008
2009 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002010 if( session->ticket_len > SIZE_MAX - needed )
2011 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002012
Jerry Yue28d9742022-08-18 15:44:03 +08002013 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002014 }
2015#endif /* MBEDTLS_SSL_CLI_C */
2016
Jerry Yue36fdd62022-08-17 21:31:36 +08002017 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002018 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002019 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002020
2021 p[0] = session->endpoint;
2022 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2023 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2024 p[7] = session->ticket_flags;
2025
2026 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002027 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002028 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002029 memcpy( p, session->resumption_key, session->resumption_key_len );
2030 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002031
2032#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2033 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2034 {
2035 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2036 p += 8;
2037 }
2038#endif /* MBEDTLS_HAVE_TIME */
2039
2040#if defined(MBEDTLS_SSL_CLI_C)
2041 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2042 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002043#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2044 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2045 p += 2;
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002046 if( hostname_len > 0 )
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002047 {
2048 /* save host name */
2049 memcpy( p, session->hostname, hostname_len );
2050 p += hostname_len;
2051 }
2052#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2053
Jerry Yu438ddd82022-07-07 06:55:50 +00002054#if defined(MBEDTLS_HAVE_TIME)
2055 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2056 p += 8;
2057#endif
2058 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2059 p += 4;
2060
2061 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2062 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002063
2064 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002065 {
2066 memcpy( p, session->ticket, session->ticket_len );
2067 p += session->ticket_len;
2068 }
2069 }
2070#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002071 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002072}
2073
2074MBEDTLS_CHECK_RETURN_CRITICAL
2075static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2076 const unsigned char *buf,
2077 size_t len )
2078{
2079 const unsigned char *p = buf;
2080 const unsigned char *end = buf + len;
2081
2082 if( end - p < 9 )
2083 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2084 session->endpoint = p[0];
2085 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2086 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2087 session->ticket_flags = p[7];
2088
2089 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002090 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002091 p += 9;
2092
Jerry Yubc7c1a42022-07-21 22:57:37 +08002093 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002094 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2095
Jerry Yubc7c1a42022-07-21 22:57:37 +08002096 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002097 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002098 memcpy( session->resumption_key, p, session->resumption_key_len );
2099 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002100
2101#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2102 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2103 {
2104 if( end - p < 8 )
2105 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2106 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2107 p += 8;
2108 }
2109#endif /* MBEDTLS_HAVE_TIME */
2110
2111#if defined(MBEDTLS_SSL_CLI_C)
2112 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2113 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002114#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2115 defined(MBEDTLS_SSL_SESSION_TICKETS)
2116 size_t hostname_len;
2117 /* load host name */
2118 if( end - p < 2 )
2119 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2120 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2121 p += 2;
2122
2123 if( end - p < ( long int )hostname_len )
2124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2125 if( hostname_len > 0 )
2126 {
2127 session->hostname = mbedtls_calloc( 1, hostname_len );
2128 if( session->hostname == NULL )
2129 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2130 memcpy( session->hostname, p, hostname_len );
2131 p += hostname_len;
2132 }
2133#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2134 MBEDTLS_SSL_SESSION_TICKETS */
2135
Jerry Yu438ddd82022-07-07 06:55:50 +00002136#if defined(MBEDTLS_HAVE_TIME)
2137 if( end - p < 8 )
2138 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2139 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2140 p += 8;
2141#endif
2142 if( end - p < 4 )
2143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2144 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2145 p += 4;
2146
2147 if( end - p < 2 )
2148 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2149 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2150 p += 2;
2151
2152 if( end - p < ( long int )session->ticket_len )
2153 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2154 if( session->ticket_len > 0 )
2155 {
2156 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2157 if( session->ticket == NULL )
2158 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2159 memcpy( session->ticket, p, session->ticket_len );
2160 p += session->ticket_len;
2161 }
2162 }
2163#endif /* MBEDTLS_SSL_CLI_C */
2164
2165 return( 0 );
2166
2167}
2168#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002169MBEDTLS_CHECK_RETURN_CRITICAL
2170static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2171 unsigned char *buf,
2172 size_t buf_len,
2173 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002174{
2175 ((void) session);
2176 ((void) buf);
2177 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002178 *olen = 0;
2179 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002180}
Jerry Yu438ddd82022-07-07 06:55:50 +00002181
2182static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2183 unsigned char *buf,
2184 size_t buf_len )
2185{
2186 ((void) session);
2187 ((void) buf);
2188 ((void) buf_len);
2189 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2190}
2191#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002192#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2193
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002194psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002195 size_t taglen,
2196 psa_algorithm_t *alg,
2197 psa_key_type_t *key_type,
2198 size_t *key_size )
2199{
2200 switch ( mbedtls_cipher_type )
2201 {
2202 case MBEDTLS_CIPHER_AES_128_CBC:
2203 *alg = PSA_ALG_CBC_NO_PADDING;
2204 *key_type = PSA_KEY_TYPE_AES;
2205 *key_size = 128;
2206 break;
2207 case MBEDTLS_CIPHER_AES_128_CCM:
2208 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2209 *key_type = PSA_KEY_TYPE_AES;
2210 *key_size = 128;
2211 break;
2212 case MBEDTLS_CIPHER_AES_128_GCM:
2213 *alg = PSA_ALG_GCM;
2214 *key_type = PSA_KEY_TYPE_AES;
2215 *key_size = 128;
2216 break;
2217 case MBEDTLS_CIPHER_AES_192_CCM:
2218 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2219 *key_type = PSA_KEY_TYPE_AES;
2220 *key_size = 192;
2221 break;
2222 case MBEDTLS_CIPHER_AES_192_GCM:
2223 *alg = PSA_ALG_GCM;
2224 *key_type = PSA_KEY_TYPE_AES;
2225 *key_size = 192;
2226 break;
2227 case MBEDTLS_CIPHER_AES_256_CBC:
2228 *alg = PSA_ALG_CBC_NO_PADDING;
2229 *key_type = PSA_KEY_TYPE_AES;
2230 *key_size = 256;
2231 break;
2232 case MBEDTLS_CIPHER_AES_256_CCM:
2233 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2234 *key_type = PSA_KEY_TYPE_AES;
2235 *key_size = 256;
2236 break;
2237 case MBEDTLS_CIPHER_AES_256_GCM:
2238 *alg = PSA_ALG_GCM;
2239 *key_type = PSA_KEY_TYPE_AES;
2240 *key_size = 256;
2241 break;
2242 case MBEDTLS_CIPHER_ARIA_128_CBC:
2243 *alg = PSA_ALG_CBC_NO_PADDING;
2244 *key_type = PSA_KEY_TYPE_ARIA;
2245 *key_size = 128;
2246 break;
2247 case MBEDTLS_CIPHER_ARIA_128_CCM:
2248 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2249 *key_type = PSA_KEY_TYPE_ARIA;
2250 *key_size = 128;
2251 break;
2252 case MBEDTLS_CIPHER_ARIA_128_GCM:
2253 *alg = PSA_ALG_GCM;
2254 *key_type = PSA_KEY_TYPE_ARIA;
2255 *key_size = 128;
2256 break;
2257 case MBEDTLS_CIPHER_ARIA_192_CCM:
2258 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2259 *key_type = PSA_KEY_TYPE_ARIA;
2260 *key_size = 192;
2261 break;
2262 case MBEDTLS_CIPHER_ARIA_192_GCM:
2263 *alg = PSA_ALG_GCM;
2264 *key_type = PSA_KEY_TYPE_ARIA;
2265 *key_size = 192;
2266 break;
2267 case MBEDTLS_CIPHER_ARIA_256_CBC:
2268 *alg = PSA_ALG_CBC_NO_PADDING;
2269 *key_type = PSA_KEY_TYPE_ARIA;
2270 *key_size = 256;
2271 break;
2272 case MBEDTLS_CIPHER_ARIA_256_CCM:
2273 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2274 *key_type = PSA_KEY_TYPE_ARIA;
2275 *key_size = 256;
2276 break;
2277 case MBEDTLS_CIPHER_ARIA_256_GCM:
2278 *alg = PSA_ALG_GCM;
2279 *key_type = PSA_KEY_TYPE_ARIA;
2280 *key_size = 256;
2281 break;
2282 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2283 *alg = PSA_ALG_CBC_NO_PADDING;
2284 *key_type = PSA_KEY_TYPE_CAMELLIA;
2285 *key_size = 128;
2286 break;
2287 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2288 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2289 *key_type = PSA_KEY_TYPE_CAMELLIA;
2290 *key_size = 128;
2291 break;
2292 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2293 *alg = PSA_ALG_GCM;
2294 *key_type = PSA_KEY_TYPE_CAMELLIA;
2295 *key_size = 128;
2296 break;
2297 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2298 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2299 *key_type = PSA_KEY_TYPE_CAMELLIA;
2300 *key_size = 192;
2301 break;
2302 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2303 *alg = PSA_ALG_GCM;
2304 *key_type = PSA_KEY_TYPE_CAMELLIA;
2305 *key_size = 192;
2306 break;
2307 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2308 *alg = PSA_ALG_CBC_NO_PADDING;
2309 *key_type = PSA_KEY_TYPE_CAMELLIA;
2310 *key_size = 256;
2311 break;
2312 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2313 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2314 *key_type = PSA_KEY_TYPE_CAMELLIA;
2315 *key_size = 256;
2316 break;
2317 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2318 *alg = PSA_ALG_GCM;
2319 *key_type = PSA_KEY_TYPE_CAMELLIA;
2320 *key_size = 256;
2321 break;
2322 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2323 *alg = PSA_ALG_CHACHA20_POLY1305;
2324 *key_type = PSA_KEY_TYPE_CHACHA20;
2325 *key_size = 256;
2326 break;
2327 case MBEDTLS_CIPHER_NULL:
2328 *alg = MBEDTLS_SSL_NULL_CIPHER;
2329 *key_type = 0;
2330 *key_size = 0;
2331 break;
2332 default:
2333 return PSA_ERROR_NOT_SUPPORTED;
2334 }
2335
2336 return PSA_SUCCESS;
2337}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002338#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002339
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002340#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002341int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2342 const unsigned char *dhm_P, size_t P_len,
2343 const unsigned char *dhm_G, size_t G_len )
2344{
Janos Follath865b3eb2019-12-16 11:46:15 +00002345 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002346
Glenn Strausscee11292021-12-20 01:43:17 -05002347 mbedtls_mpi_free( &conf->dhm_P );
2348 mbedtls_mpi_free( &conf->dhm_G );
2349
Hanno Beckera90658f2017-10-04 15:29:08 +01002350 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2351 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2352 {
2353 mbedtls_mpi_free( &conf->dhm_P );
2354 mbedtls_mpi_free( &conf->dhm_G );
2355 return( ret );
2356 }
2357
2358 return( 0 );
2359}
Paul Bakker5121ce52009-01-03 21:22:43 +00002360
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002361int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002362{
Janos Follath865b3eb2019-12-16 11:46:15 +00002363 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002364
Glenn Strausscee11292021-12-20 01:43:17 -05002365 mbedtls_mpi_free( &conf->dhm_P );
2366 mbedtls_mpi_free( &conf->dhm_G );
2367
Gilles Peskinee5702482021-06-11 21:59:08 +02002368 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2369 &conf->dhm_P ) ) != 0 ||
2370 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2371 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002372 {
2373 mbedtls_mpi_free( &conf->dhm_P );
2374 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002375 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002376 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002377
2378 return( 0 );
2379}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002380#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002381
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002382#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2383/*
2384 * Set the minimum length for Diffie-Hellman parameters
2385 */
2386void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2387 unsigned int bitlen )
2388{
2389 conf->dhm_min_bitlen = bitlen;
2390}
2391#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2392
Gilles Peskineeccd8882020-03-10 12:19:08 +01002393#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002394#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002395/*
2396 * Set allowed/preferred hashes for handshake signatures
2397 */
2398void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2399 const int *hashes )
2400{
2401 conf->sig_hashes = hashes;
2402}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002403#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002404
Jerry Yuf017ee42022-01-12 15:49:48 +08002405/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002406void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2407 const uint16_t* sig_algs )
2408{
Jerry Yuf017ee42022-01-12 15:49:48 +08002409#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2410 conf->sig_hashes = NULL;
2411#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2412 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002413}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002414#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002415
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002416#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002417#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002418/*
2419 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002420 *
2421 * mbedtls_ssl_setup() takes the provided list
2422 * and translates it to a list of IANA TLS group identifiers,
2423 * stored in ssl->handshake->group_list.
2424 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002425 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002426void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002427 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002428{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002429 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002430 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002431}
Brett Warrene0edc842021-08-17 09:53:13 +01002432#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002433#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002434
Brett Warrene0edc842021-08-17 09:53:13 +01002435/*
2436 * Set the allowed groups
2437 */
2438void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2439 const uint16_t *group_list )
2440{
2441#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2442 conf->curve_list = NULL;
2443#endif
2444 conf->group_list = group_list;
2445}
2446
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002447#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002449{
Hanno Becker947194e2017-04-07 13:25:49 +01002450 /* Initialize to suppress unnecessary compiler warning */
2451 size_t hostname_len = 0;
2452
2453 /* Check if new hostname is valid before
2454 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002455 if( hostname != NULL )
2456 {
2457 hostname_len = strlen( hostname );
2458
2459 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2460 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2461 }
2462
2463 /* Now it's clear that we will overwrite the old hostname,
2464 * so we can free it safely */
2465
2466 if( ssl->hostname != NULL )
2467 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002468 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002469 mbedtls_free( ssl->hostname );
2470 }
2471
2472 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002473
Paul Bakker5121ce52009-01-03 21:22:43 +00002474 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002475 {
2476 ssl->hostname = NULL;
2477 }
2478 else
2479 {
2480 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002481 if( ssl->hostname == NULL )
2482 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002483
Hanno Becker947194e2017-04-07 13:25:49 +01002484 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002485
Hanno Becker947194e2017-04-07 13:25:49 +01002486 ssl->hostname[hostname_len] = '\0';
2487 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002488
2489 return( 0 );
2490}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002491#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002492
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002493#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002494void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002496 const unsigned char *, size_t),
2497 void *p_sni )
2498{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002499 conf->f_sni = f_sni;
2500 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002501}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002505int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002506{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002507 size_t cur_len, tot_len;
2508 const char **p;
2509
2510 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002511 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2512 * MUST NOT be truncated."
2513 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002514 */
2515 tot_len = 0;
2516 for( p = protos; *p != NULL; p++ )
2517 {
2518 cur_len = strlen( *p );
2519 tot_len += cur_len;
2520
Ronald Cron8216dd32020-04-23 16:41:44 +02002521 if( ( cur_len == 0 ) ||
2522 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2523 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002525 }
2526
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002527 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002528
2529 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002530}
2531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002533{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002534 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002535}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002537
Johan Pascalb62bb512015-12-03 21:56:45 +01002538#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002539void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2540 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002541{
2542 conf->dtls_srtp_mki_support = support_mki_value;
2543}
2544
Ron Eldoref72faf2018-07-12 11:54:20 +03002545int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2546 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002547 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002548{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002549 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002550 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002551 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002552 }
Ron Eldor591f1622018-01-22 12:30:04 +02002553
2554 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002555 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002556 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002557 }
Ron Eldor591f1622018-01-22 12:30:04 +02002558
2559 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2560 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002561 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002562}
2563
Ron Eldoref72faf2018-07-12 11:54:20 +03002564int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002565 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002566{
Johan Pascal253d0262020-09-22 13:04:45 +02002567 const mbedtls_ssl_srtp_profile *p;
2568 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002569
Johan Pascal253d0262020-09-22 13:04:45 +02002570 /* check the profiles list: all entry must be valid,
2571 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002572 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2573 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2574 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002575 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002576 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002577 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002578 list_size++;
2579 }
2580 else
2581 {
2582 /* unsupported value, stop parsing and set the size to an error value */
2583 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002584 }
2585 }
2586
Johan Pascal5ef72d22020-10-28 17:05:47 +01002587 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002588 {
Johan Pascal253d0262020-09-22 13:04:45 +02002589 conf->dtls_srtp_profile_list = NULL;
2590 conf->dtls_srtp_profile_list_len = 0;
2591 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2592 }
2593
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002594 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002595 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002596
2597 return( 0 );
2598}
2599
Johan Pascal5ef72d22020-10-28 17:05:47 +01002600void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2601 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002602{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002603 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2604 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002605 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002606 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002607 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002608 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002609 else
2610 {
2611 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002612 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2613 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002614 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002615}
Johan Pascalb62bb512015-12-03 21:56:45 +01002616#endif /* MBEDTLS_SSL_DTLS_SRTP */
2617
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302618#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002619void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002620{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002621 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002622}
2623
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002624void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002625{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002626 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002627}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302628#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002629
Janos Follath088ce432017-04-10 12:42:31 +01002630#if defined(MBEDTLS_SSL_SRV_C)
2631void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2632 char cert_req_ca_list )
2633{
2634 conf->cert_req_ca_list = cert_req_ca_list;
2635}
2636#endif
2637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002639void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002640{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002641 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002642}
2643#endif
2644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002646void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002647{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002648 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002649}
2650#endif
2651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002653int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002654{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002656 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002659 }
2660
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002661 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002662
2663 return( 0 );
2664}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002666
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002667void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002668{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002669 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002670}
2671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002673void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002674{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002675 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002676}
2677
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002678void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002679{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002680 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002681}
2682
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002683void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002684 const unsigned char period[8] )
2685{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002686 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002687}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002691#if defined(MBEDTLS_SSL_CLI_C)
2692void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002693{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002694 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002695}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002696#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002697
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002698#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002699
Jerry Yud0766ec2022-09-22 10:46:57 +08002700#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002701void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2702 uint16_t num_tickets )
2703{
Jerry Yud0766ec2022-09-22 10:46:57 +08002704 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002705}
2706#endif
2707
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002708void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2709 mbedtls_ssl_ticket_write_t *f_ticket_write,
2710 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2711 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002712{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002713 conf->f_ticket_write = f_ticket_write;
2714 conf->f_ticket_parse = f_ticket_parse;
2715 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002716}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002717#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002719
Hanno Becker7e6c1782021-06-08 09:24:55 +01002720void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2721 mbedtls_ssl_export_keys_t *f_export_keys,
2722 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002723{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002724 ssl->f_export_keys = f_export_keys;
2725 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002726}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002727
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002728#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002729void mbedtls_ssl_conf_async_private_cb(
2730 mbedtls_ssl_config *conf,
2731 mbedtls_ssl_async_sign_t *f_async_sign,
2732 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2733 mbedtls_ssl_async_resume_t *f_async_resume,
2734 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002735 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002736{
2737 conf->f_async_sign_start = f_async_sign;
2738 conf->f_async_decrypt_start = f_async_decrypt;
2739 conf->f_async_resume = f_async_resume;
2740 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002741 conf->p_async_config_data = async_config_data;
2742}
2743
Gilles Peskine8f97af72018-04-26 11:46:10 +02002744void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2745{
2746 return( conf->p_async_config_data );
2747}
2748
Gilles Peskine1febfef2018-04-30 11:54:39 +02002749void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002750{
2751 if( ssl->handshake == NULL )
2752 return( NULL );
2753 else
2754 return( ssl->handshake->user_async_ctx );
2755}
2756
Gilles Peskine1febfef2018-04-30 11:54:39 +02002757void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002758 void *ctx )
2759{
2760 if( ssl->handshake != NULL )
2761 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002762}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002763#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002764
Paul Bakker5121ce52009-01-03 21:22:43 +00002765/*
2766 * SSL get accessors
2767 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002768uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002769{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002770 if( ssl->session != NULL )
2771 return( ssl->session->verify_result );
2772
2773 if( ssl->session_negotiate != NULL )
2774 return( ssl->session_negotiate->verify_result );
2775
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002776 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002777}
2778
Glenn Strauss8f526902022-01-13 00:04:49 -05002779int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2780{
2781 if( ssl == NULL || ssl->session == NULL )
2782 return( 0 );
2783
2784 return( ssl->session->ciphersuite );
2785}
2786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002788{
Paul Bakker926c8e42013-03-06 10:23:34 +01002789 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002790 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002793}
2794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002796{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002798 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002799 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002800 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002801 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002802 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002803 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002804 default:
2805 return( "unknown (DTLS)" );
2806 }
2807 }
2808#endif
2809
Glenn Strauss60bfe602022-03-14 19:04:24 -04002810 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002811 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002812 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002813 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002814 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002815 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002816 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002817 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002818 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002819}
2820
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002821#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002822size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2823{
David Horstmann95d516f2021-05-04 18:36:56 +01002824 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002825 size_t read_mfl;
2826
2827 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2828 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2829 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2830 {
2831 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2832 }
2833
2834 /* Check if a smaller max length was negotiated */
2835 if( ssl->session_out != NULL )
2836 {
2837 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2838 if( read_mfl < max_len )
2839 {
2840 max_len = read_mfl;
2841 }
2842 }
2843
2844 // During a handshake, use the value being negotiated
2845 if( ssl->session_negotiate != NULL )
2846 {
2847 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2848 if( read_mfl < max_len )
2849 {
2850 max_len = read_mfl;
2851 }
2852 }
2853
2854 return( max_len );
2855}
2856
2857size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002858{
2859 size_t max_len;
2860
2861 /*
2862 * Assume mfl_code is correct since it was checked when set
2863 */
Angus Grattond8213d02016-05-25 20:56:48 +10002864 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002865
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002866 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002867 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002868 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002869 {
Angus Grattond8213d02016-05-25 20:56:48 +10002870 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002871 }
2872
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002873 /* During a handshake, use the value being negotiated */
2874 if( ssl->session_negotiate != NULL &&
2875 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2876 {
2877 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2878 }
2879
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002880 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002881}
2882#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2883
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002884#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002885size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002886{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002887 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2888 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2889 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2890 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2891 return ( 0 );
2892
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002893 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2894 return( ssl->mtu );
2895
2896 if( ssl->mtu == 0 )
2897 return( ssl->handshake->mtu );
2898
2899 return( ssl->mtu < ssl->handshake->mtu ?
2900 ssl->mtu : ssl->handshake->mtu );
2901}
2902#endif /* MBEDTLS_SSL_PROTO_DTLS */
2903
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002904int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2905{
2906 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2907
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002908#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2909 !defined(MBEDTLS_SSL_PROTO_DTLS)
2910 (void) ssl;
2911#endif
2912
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002913#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002914 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002915
2916 if( max_len > mfl )
2917 max_len = mfl;
2918#endif
2919
2920#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002921 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002922 {
Hanno Becker89490712020-02-05 10:50:12 +00002923 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002924 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2925 const size_t overhead = (size_t) ret;
2926
2927 if( ret < 0 )
2928 return( ret );
2929
2930 if( mtu <= overhead )
2931 {
2932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2933 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2934 }
2935
2936 if( max_len > mtu - overhead )
2937 max_len = mtu - overhead;
2938 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002939#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002940
Hanno Becker0defedb2018-08-10 12:35:02 +01002941#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2942 !defined(MBEDTLS_SSL_PROTO_DTLS)
2943 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002944#endif
2945
2946 return( (int) max_len );
2947}
2948
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002949int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2950{
2951 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2952
2953#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2954 (void) ssl;
2955#endif
2956
2957#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2958 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2959
2960 if( max_len > mfl )
2961 max_len = mfl;
2962#endif
2963
2964 return( (int) max_len );
2965}
2966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967#if defined(MBEDTLS_X509_CRT_PARSE_C)
2968const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002969{
2970 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002971 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002972
Hanno Beckere6824572019-02-07 13:18:46 +00002973#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002974 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002975#else
2976 return( NULL );
2977#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002978}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002982int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2983 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002984{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002985 int ret;
2986
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002987 if( ssl == NULL ||
2988 dst == NULL ||
2989 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002990 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002993 }
2994
Hanno Beckere810bbc2021-05-14 16:01:05 +01002995 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2996 * idempotent: Each session can only be exported once.
2997 *
2998 * (This is in preparation for TLS 1.3 support where we will
2999 * need the ability to export multiple sessions (aka tickets),
3000 * which will be achieved by calling mbedtls_ssl_get_session()
3001 * multiple times until it fails.)
3002 *
3003 * Check whether we have already exported the current session,
3004 * and fail if so.
3005 */
3006 if( ssl->session->exported == 1 )
3007 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3008
3009 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3010 if( ret != 0 )
3011 return( ret );
3012
3013 /* Remember that we've exported the session. */
3014 ssl->session->exported = 1;
3015 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003016}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003018
Paul Bakker5121ce52009-01-03 21:22:43 +00003019/*
Hanno Beckera835da52019-05-16 12:39:07 +01003020 * Define ticket header determining Mbed TLS version
3021 * and structure of the ticket.
3022 */
3023
Hanno Becker94ef3b32019-05-16 12:50:45 +01003024/*
Hanno Becker50b59662019-05-28 14:30:45 +01003025 * Define bitflag determining compile-time settings influencing
3026 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003027 */
3028
Hanno Becker50b59662019-05-28 14:30:45 +01003029#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003030#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003031#else
Hanno Becker3e088662019-05-29 11:10:18 +01003032#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003033#endif /* MBEDTLS_HAVE_TIME */
3034
3035#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003036#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003037#else
Hanno Becker3e088662019-05-29 11:10:18 +01003038#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003039#endif /* MBEDTLS_X509_CRT_PARSE_C */
3040
3041#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003042#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003043#else
Hanno Becker3e088662019-05-29 11:10:18 +01003044#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003045#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3046
3047#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003048#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003049#else
Hanno Becker3e088662019-05-29 11:10:18 +01003050#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003051#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3052
Hanno Becker94ef3b32019-05-16 12:50:45 +01003053#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003054#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003055#else
Hanno Becker3e088662019-05-29 11:10:18 +01003056#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003057#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3058
Hanno Becker94ef3b32019-05-16 12:50:45 +01003059#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3060#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3061#else
3062#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3063#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3064
Hanno Becker3e088662019-05-29 11:10:18 +01003065#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3066#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3067#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3068#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003069#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3070#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003071
Hanno Becker50b59662019-05-28 14:30:45 +01003072#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003073 ( (uint16_t) ( \
3074 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3075 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3076 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3077 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003078 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003079 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003080
Hanno Beckerf8787072019-05-16 12:41:07 +01003081static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003082 MBEDTLS_VERSION_MAJOR,
3083 MBEDTLS_VERSION_MINOR,
3084 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003085 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3086 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003087};
Hanno Beckera835da52019-05-16 12:39:07 +01003088
3089/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003090 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003091 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003092 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003093 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003094 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003095 * opaque mbedtls_version[3]; // library version: major, minor, patch
3096 * opaque session_format[2]; // library-version specific 16-bit field
3097 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003098 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003099 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003100 * Note: When updating the format, remember to keep
3101 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003102 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003103 * // In this version, `session_format` determines
3104 * // the setting of those compile-time
3105 * // configuration options which influence
3106 * // the structure of mbedtls_ssl_session.
3107 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003108 * uint8_t minor_ver; // Protocol minor version. Possible values:
3109 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003110 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003111 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003112 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003113 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003114 * serialized_session_tls12 data;
3115 *
3116 * };
3117 *
3118 * } serialized_session;
3119 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003120 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003121
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003122MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003123static int ssl_session_save( const mbedtls_ssl_session *session,
3124 unsigned char omit_header,
3125 unsigned char *buf,
3126 size_t buf_len,
3127 size_t *olen )
3128{
3129 unsigned char *p = buf;
3130 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003131 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003132#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3133 size_t out_len;
3134 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3135#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003136 if( session == NULL )
3137 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3138
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003139 if( !omit_header )
3140 {
3141 /*
3142 * Add Mbed TLS version identifier
3143 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003144 used += sizeof( ssl_serialized_session_header );
3145
3146 if( used <= buf_len )
3147 {
3148 memcpy( p, ssl_serialized_session_header,
3149 sizeof( ssl_serialized_session_header ) );
3150 p += sizeof( ssl_serialized_session_header );
3151 }
3152 }
3153
3154 /*
3155 * TLS version identifier
3156 */
3157 used += 1;
3158 if( used <= buf_len )
3159 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003160 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003161 }
3162
3163 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003164 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003165 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003166 {
3167#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003168 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003169 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003170 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003171#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3172
Jerry Yu251a12e2022-07-13 15:15:48 +08003173#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3174 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003175 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3176 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003177 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003178 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003179 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003180#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3181
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003182 default:
3183 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3184 }
3185
3186 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003187 if( used > buf_len )
3188 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003189
3190 return( 0 );
3191}
3192
3193/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003194 * Public wrapper for ssl_session_save()
3195 */
3196int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3197 unsigned char *buf,
3198 size_t buf_len,
3199 size_t *olen )
3200{
3201 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3202}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003203
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003204/*
3205 * Deserialize session, see mbedtls_ssl_session_save() for format.
3206 *
3207 * This internal version is wrapped by a public function that cleans up in
3208 * case of error, and has an extra option omit_header.
3209 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003210MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003211static int ssl_session_load( mbedtls_ssl_session *session,
3212 unsigned char omit_header,
3213 const unsigned char *buf,
3214 size_t len )
3215{
3216 const unsigned char *p = buf;
3217 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003218 size_t remaining_len;
3219
3220
3221 if( session == NULL )
3222 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003223
3224 if( !omit_header )
3225 {
3226 /*
3227 * Check Mbed TLS version identifier
3228 */
3229
3230 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3231 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3232
3233 if( memcmp( p, ssl_serialized_session_header,
3234 sizeof( ssl_serialized_session_header ) ) != 0 )
3235 {
3236 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3237 }
3238 p += sizeof( ssl_serialized_session_header );
3239 }
3240
3241 /*
3242 * TLS version identifier
3243 */
3244 if( 1 > (size_t)( end - p ) )
3245 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003246 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003247
3248 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003249 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003250 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003251 {
3252#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003253 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003254 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003255#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3256
Jerry Yu438ddd82022-07-07 06:55:50 +00003257#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3258 case MBEDTLS_SSL_VERSION_TLS1_3:
3259 return( ssl_tls13_session_load( session, p, remaining_len ) );
3260#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3261
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003262 default:
3263 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3264 }
3265}
3266
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003267/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003268 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003269 */
3270int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3271 const unsigned char *buf,
3272 size_t len )
3273{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003274 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003275
3276 if( ret != 0 )
3277 mbedtls_ssl_session_free( session );
3278
3279 return( ret );
3280}
3281
3282/*
Paul Bakker1961b702013-01-25 14:49:24 +01003283 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003284 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003285MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003286static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3287{
3288 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3289
Ronald Cron66dbf912022-02-02 15:33:46 +01003290 /*
3291 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003292 * that were written into the output buffer by the previous handshake step,
3293 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003294 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3295 * We proceed to the next handshake step only when all data from the
3296 * previous one have been sent to the peer, thus we make sure that this is
3297 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3298 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3299 * we have to wait before to go ahead.
3300 * In the case of TLS 1.3, handshake step handlers do not send data to the
3301 * peer. Data are only sent here and through
3302 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003303 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003304 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003305 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3306 return( ret );
3307
3308#if defined(MBEDTLS_SSL_PROTO_DTLS)
3309 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3310 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3311 {
3312 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3313 return( ret );
3314 }
3315#endif /* MBEDTLS_SSL_PROTO_DTLS */
3316
3317 return( ret );
3318}
3319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003321{
Hanno Becker41934dd2021-08-07 19:13:43 +01003322 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003323
Hanno Becker41934dd2021-08-07 19:13:43 +01003324 if( ssl == NULL ||
3325 ssl->conf == NULL ||
3326 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003327 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003328 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003329 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003330 }
3331
3332 ret = ssl_prepare_handshake_step( ssl );
3333 if( ret != 0 )
3334 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003335
Jerry Yue7047812021-09-13 19:26:39 +08003336 ret = mbedtls_ssl_handle_pending_alert( ssl );
3337 if( ret != 0 )
3338 goto cleanup;
3339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003341 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003342 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3344 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003345
Ronald Cron9f0fba32022-02-10 16:45:15 +01003346 switch( ssl->state )
3347 {
3348 case MBEDTLS_SSL_HELLO_REQUEST:
3349 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3350 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003351
Ronald Cron9f0fba32022-02-10 16:45:15 +01003352 case MBEDTLS_SSL_CLIENT_HELLO:
3353 ret = mbedtls_ssl_write_client_hello( ssl );
3354 break;
3355
3356 default:
3357#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003358 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003359 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3360 else
3361 ret = mbedtls_ssl_handshake_client_step( ssl );
3362#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3363 ret = mbedtls_ssl_handshake_client_step( ssl );
3364#else
3365 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3366#endif
3367 }
Jerry Yub9930e72021-08-06 17:11:51 +08003368 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003369#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003370#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003371 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003372 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003373#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003374 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003375 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003376#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003377
3378#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3379 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3380 ret = mbedtls_ssl_handshake_server_step( ssl );
3381#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3382 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003383#endif
3384
Jerry Yue7047812021-09-13 19:26:39 +08003385 if( ret != 0 )
3386 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003387 /* handshake_step return error. And it is same
3388 * with alert_reason.
3389 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003390 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003391 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003392 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003393 goto cleanup;
3394 }
3395 }
3396
3397cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003398 return( ret );
3399}
3400
3401/*
3402 * Perform the SSL handshake
3403 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003405{
3406 int ret = 0;
3407
Hanno Beckera817ea42020-10-20 15:20:23 +01003408 /* Sanity checks */
3409
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003410 if( ssl == NULL || ssl->conf == NULL )
3411 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3412
Hanno Beckera817ea42020-10-20 15:20:23 +01003413#if defined(MBEDTLS_SSL_PROTO_DTLS)
3414 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3415 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3416 {
3417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3418 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3419 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3420 }
3421#endif /* MBEDTLS_SSL_PROTO_DTLS */
3422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003424
Hanno Beckera817ea42020-10-20 15:20:23 +01003425 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003426 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003429
3430 if( ret != 0 )
3431 break;
3432 }
3433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003435
3436 return( ret );
3437}
3438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003439#if defined(MBEDTLS_SSL_RENEGOTIATION)
3440#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003441/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003442 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003443 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003444MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003446{
Janos Follath865b3eb2019-12-16 11:46:15 +00003447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003450
3451 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3453 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003454
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003455 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003456 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003457 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003458 return( ret );
3459 }
3460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003462
3463 return( 0 );
3464}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003465#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003466
3467/*
3468 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003469 * - any side: calling mbedtls_ssl_renegotiate(),
3470 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3471 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003472 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003473 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003475 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003476int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003477{
Janos Follath865b3eb2019-12-16 11:46:15 +00003478 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003481
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003482 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3483 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003484
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003485 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3486 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003487#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003488 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003490 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003491 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003492 ssl->handshake->out_msg_seq = 1;
3493 else
3494 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003495 }
3496#endif
3497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3499 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003501 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003504 return( ret );
3505 }
3506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003508
3509 return( 0 );
3510}
3511
3512/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003513 * Renegotiate current connection on client,
3514 * or request renegotiation on server
3515 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003517{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003519
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003520 if( ssl == NULL || ssl->conf == NULL )
3521 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003524 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003525 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003526 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003527 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003530 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003531
3532 /* Did we already try/start sending HelloRequest? */
3533 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003534 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003535
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003536 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003537 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003538#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003541 /*
3542 * On client, either start the renegotiation process or,
3543 * if already in progress, continue the handshake
3544 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003546 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003547 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003549
Hanno Becker40cdaa12020-02-05 10:48:27 +00003550 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003551 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003552 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003553 return( ret );
3554 }
3555 }
3556 else
3557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003558 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003561 return( ret );
3562 }
3563 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003565
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003566 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003567}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003568#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003569
Gilles Peskine9b562d52018-04-25 20:32:43 +02003570void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003571{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003572 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3573
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003574 if( handshake == NULL )
3575 return;
3576
Brett Warrene0edc842021-08-17 09:53:13 +01003577#if defined(MBEDTLS_ECP_C)
3578#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3579 if ( ssl->handshake->group_list_heap_allocated )
3580 mbedtls_free( (void*) handshake->group_list );
3581 handshake->group_list = NULL;
3582#endif /* MBEDTLS_DEPRECATED_REMOVED */
3583#endif /* MBEDTLS_ECP_C */
3584
Jerry Yuf017ee42022-01-12 15:49:48 +08003585#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3586#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3587 if ( ssl->handshake->sig_algs_heap_allocated )
3588 mbedtls_free( (void*) handshake->sig_algs );
3589 handshake->sig_algs = NULL;
3590#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003591#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3592 if( ssl->handshake->certificate_request_context )
3593 {
3594 mbedtls_free( (void*) handshake->certificate_request_context );
3595 }
3596#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003597#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3598
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003599#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3600 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3601 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003602 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003603 handshake->async_in_progress = 0;
3604 }
3605#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3606
Andrzej Kurek25f27152022-08-17 16:09:31 -04003607#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003608#if defined(MBEDTLS_USE_PSA_CRYPTO)
3609 psa_hash_abort( &handshake->fin_sha256_psa );
3610#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003611 mbedtls_sha256_free( &handshake->fin_sha256 );
3612#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003613#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003614#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003615#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003616 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003617#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003618 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003619#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003620#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622#if defined(MBEDTLS_DHM_C)
3623 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003624#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003625#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003627#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003628#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003629 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003630#if defined(MBEDTLS_SSL_CLI_C)
3631 mbedtls_free( handshake->ecjpake_cache );
3632 handshake->ecjpake_cache = NULL;
3633 handshake->ecjpake_cache_len = 0;
3634#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003635#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003636
Janos Follath4ae5c292016-02-10 11:27:43 +00003637#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3638 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003639 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003641#endif
3642
Gilles Peskineeccd8882020-03-10 12:19:08 +01003643#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003644#if defined(MBEDTLS_USE_PSA_CRYPTO)
3645 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3646 {
3647 /* The maintenance of the external PSK key slot is the
3648 * user's responsibility. */
3649 if( ssl->handshake->psk_opaque_is_internal )
3650 {
3651 psa_destroy_key( ssl->handshake->psk_opaque );
3652 ssl->handshake->psk_opaque_is_internal = 0;
3653 }
3654 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3655 }
Neil Armstronge952a302022-05-03 10:22:14 +02003656#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003657 if( handshake->psk != NULL )
3658 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003659 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003660 mbedtls_free( handshake->psk );
3661 }
Neil Armstronge952a302022-05-03 10:22:14 +02003662#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003663#endif
3664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3666 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003667 /*
3668 * Free only the linked list wrapper, not the keys themselves
3669 * since the belong to the SNI callback
3670 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003671 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003673
Gilles Peskineeccd8882020-03-10 12:19:08 +01003674#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003675 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003676 if( handshake->ecrs_peer_cert != NULL )
3677 {
3678 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3679 mbedtls_free( handshake->ecrs_peer_cert );
3680 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003681#endif
3682
Hanno Becker75173122019-02-06 16:18:31 +00003683#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3684 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3685 mbedtls_pk_free( &handshake->peer_pubkey );
3686#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3687
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003688#if defined(MBEDTLS_SSL_CLI_C) && \
3689 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3690 mbedtls_free( handshake->cookie );
3691#endif /* MBEDTLS_SSL_CLI_C &&
3692 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003693
3694#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003695 mbedtls_ssl_flight_free( handshake->flight );
3696 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003697#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003698
Ronald Cronf12b81d2022-03-15 10:42:41 +01003699#if defined(MBEDTLS_ECDH_C) && \
3700 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003701 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003702 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003703#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3704
Ronald Cron6f135e12021-12-08 16:57:54 +01003705#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003706 mbedtls_ssl_transform_free( handshake->transform_handshake );
3707 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003708 mbedtls_free( handshake->transform_earlydata );
3709 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003710#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003711
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003712
3713#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3714 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3715 * processes datagrams and the fact that a datagram is allowed to have
3716 * several records in it, it is possible that the I/O buffers are not
3717 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003718 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3719 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003720#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003721
Jerry Yuba9c7272021-10-30 11:54:10 +08003722 /* mbedtls_platform_zeroize MUST be last one in this function */
3723 mbedtls_platform_zeroize( handshake,
3724 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003725}
3726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003728{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003729 if( session == NULL )
3730 return;
3731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003732#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003733 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003734#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003735
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003736#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00003737#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
3738 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00003739 mbedtls_free( session->hostname );
3740#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003741 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003742#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003743
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003744 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003745}
3746
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003747#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003748
3749#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3750#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3751#else
3752#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3753#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3754
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003755#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003756
3757#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3758#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3759#else
3760#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3761#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3762
3763#if defined(MBEDTLS_SSL_ALPN)
3764#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3765#else
3766#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3767#endif /* MBEDTLS_SSL_ALPN */
3768
3769#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3770#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3771#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3772#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3773
3774#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3775 ( (uint32_t) ( \
3776 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3777 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3778 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3779 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3780 0u ) )
3781
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003782static unsigned char ssl_serialized_context_header[] = {
3783 MBEDTLS_VERSION_MAJOR,
3784 MBEDTLS_VERSION_MINOR,
3785 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003786 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3787 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3788 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3789 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3790 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003791};
3792
Paul Bakker5121ce52009-01-03 21:22:43 +00003793/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003794 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003795 *
3796 * The format of the serialized data is:
3797 * (in the presentation language of TLS, RFC 8446 section 3)
3798 *
3799 * // header
3800 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003801 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003802 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003803 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003804 * Note: When updating the format, remember to keep these
3805 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003806 *
3807 * // session sub-structure
3808 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3809 * // transform sub-structure
3810 * uint8 random[64]; // ServerHello.random+ClientHello.random
3811 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3812 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3813 * // fields from ssl_context
3814 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3815 * uint64 in_window_top; // DTLS: last validated record seq_num
3816 * uint64 in_window; // DTLS: bitmask for replay protection
3817 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3818 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3819 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3820 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3821 *
3822 * Note that many fields of the ssl_context or sub-structures are not
3823 * serialized, as they fall in one of the following categories:
3824 *
3825 * 1. forced value (eg in_left must be 0)
3826 * 2. pointer to dynamically-allocated memory (eg session, transform)
3827 * 3. value can be re-derived from other data (eg session keys from MS)
3828 * 4. value was temporary (eg content of input buffer)
3829 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003830 */
3831int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3832 unsigned char *buf,
3833 size_t buf_len,
3834 size_t *olen )
3835{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003836 unsigned char *p = buf;
3837 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003838 size_t session_len;
3839 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003840
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003841 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003842 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3843 * this function's documentation.
3844 *
3845 * These are due to assumptions/limitations in the implementation. Some of
3846 * them are likely to stay (no handshake in progress) some might go away
3847 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003848 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003849 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003850 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003851 {
3852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003853 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003854 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003855 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003856 {
3857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003858 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003859 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003860 /* Double-check that sub-structures are indeed ready */
3861 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003862 {
3863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003864 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003865 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003866 /* There must be no pending incoming or outgoing data */
3867 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003868 {
3869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003870 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003871 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003872 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003873 {
3874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003875 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003876 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003877 /* Protocol must be DLTS, not TLS */
3878 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003879 {
3880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003881 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003882 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003883 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003884 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003885 {
3886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003887 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003888 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003889 /* We must be using an AEAD ciphersuite */
3890 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003891 {
3892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003893 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003894 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003895 /* Renegotiation must not be enabled */
3896#if defined(MBEDTLS_SSL_RENEGOTIATION)
3897 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003898 {
3899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003900 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003901 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003902#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003903
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003904 /*
3905 * Version and format identifier
3906 */
3907 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003908
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003909 if( used <= buf_len )
3910 {
3911 memcpy( p, ssl_serialized_context_header,
3912 sizeof( ssl_serialized_context_header ) );
3913 p += sizeof( ssl_serialized_context_header );
3914 }
3915
3916 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003917 * Session (length + data)
3918 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003919 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003920 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3921 return( ret );
3922
3923 used += 4 + session_len;
3924 if( used <= buf_len )
3925 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003926 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3927 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003928
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003929 ret = ssl_session_save( ssl->session, 1,
3930 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003931 if( ret != 0 )
3932 return( ret );
3933
3934 p += session_len;
3935 }
3936
3937 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003938 * Transform
3939 */
3940 used += sizeof( ssl->transform->randbytes );
3941 if( used <= buf_len )
3942 {
3943 memcpy( p, ssl->transform->randbytes,
3944 sizeof( ssl->transform->randbytes ) );
3945 p += sizeof( ssl->transform->randbytes );
3946 }
3947
3948#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3949 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3950 if( used <= buf_len )
3951 {
3952 *p++ = ssl->transform->in_cid_len;
3953 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3954 p += ssl->transform->in_cid_len;
3955
3956 *p++ = ssl->transform->out_cid_len;
3957 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3958 p += ssl->transform->out_cid_len;
3959 }
3960#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3961
3962 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003963 * Saved fields from top-level ssl_context structure
3964 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003965 used += 4;
3966 if( used <= buf_len )
3967 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003968 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3969 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003970 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003971
3972#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3973 used += 16;
3974 if( used <= buf_len )
3975 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003976 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3977 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003978
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003979 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3980 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003981 }
3982#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3983
3984#if defined(MBEDTLS_SSL_PROTO_DTLS)
3985 used += 1;
3986 if( used <= buf_len )
3987 {
3988 *p++ = ssl->disable_datagram_packing;
3989 }
3990#endif /* MBEDTLS_SSL_PROTO_DTLS */
3991
Jerry Yuae0b2e22021-10-08 15:21:19 +08003992 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003993 if( used <= buf_len )
3994 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003995 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3996 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003997 }
3998
3999#if defined(MBEDTLS_SSL_PROTO_DTLS)
4000 used += 2;
4001 if( used <= buf_len )
4002 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004003 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4004 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004005 }
4006#endif /* MBEDTLS_SSL_PROTO_DTLS */
4007
4008#if defined(MBEDTLS_SSL_ALPN)
4009 {
4010 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004011 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004012 : 0;
4013
4014 used += 1 + alpn_len;
4015 if( used <= buf_len )
4016 {
4017 *p++ = alpn_len;
4018
4019 if( ssl->alpn_chosen != NULL )
4020 {
4021 memcpy( p, ssl->alpn_chosen, alpn_len );
4022 p += alpn_len;
4023 }
4024 }
4025 }
4026#endif /* MBEDTLS_SSL_ALPN */
4027
4028 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004029 * Done
4030 */
4031 *olen = used;
4032
4033 if( used > buf_len )
4034 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004035
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004036 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4037
Hanno Becker43aefe22020-02-05 10:44:56 +00004038 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004039}
4040
4041/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004042 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004043 *
4044 * This internal version is wrapped by a public function that cleans up in
4045 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004046 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004047MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004048static int ssl_context_load( mbedtls_ssl_context *ssl,
4049 const unsigned char *buf,
4050 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004051{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004052 const unsigned char *p = buf;
4053 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004054 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004055 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004056#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004057 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004058#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004059
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004060 /*
4061 * The context should have been freshly setup or reset.
4062 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004063 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004064 * renegotiating, or if the user mistakenly loaded a session first.)
4065 */
4066 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4067 ssl->session != NULL )
4068 {
4069 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4070 }
4071
4072 /*
4073 * We can't check that the config matches the initial one, but we can at
4074 * least check it matches the requirements for serializing.
4075 */
4076 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004077 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4078 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004079#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004080 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004081#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004082 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004083 {
4084 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4085 }
4086
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004087 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4088
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004089 /*
4090 * Check version identifier
4091 */
4092 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4093 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4094
4095 if( memcmp( p, ssl_serialized_context_header,
4096 sizeof( ssl_serialized_context_header ) ) != 0 )
4097 {
4098 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4099 }
4100 p += sizeof( ssl_serialized_context_header );
4101
4102 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004103 * Session
4104 */
4105 if( (size_t)( end - p ) < 4 )
4106 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4107
4108 session_len = ( (size_t) p[0] << 24 ) |
4109 ( (size_t) p[1] << 16 ) |
4110 ( (size_t) p[2] << 8 ) |
4111 ( (size_t) p[3] );
4112 p += 4;
4113
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004114 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004115 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004116 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004117 ssl->session_in = ssl->session;
4118 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004119 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004120
4121 if( (size_t)( end - p ) < session_len )
4122 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4123
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004124 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004125 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004126 {
4127 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004128 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004129 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004130
4131 p += session_len;
4132
4133 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004134 * Transform
4135 */
4136
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004137 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004138 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004139 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004140 ssl->transform_in = ssl->transform;
4141 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004142 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004143
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004144#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004145 prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite );
4146 if( prf_func == NULL )
4147 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4148
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004149 /* Read random bytes and populate structure */
4150 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4151 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004152
Hanno Beckerbd257552021-03-22 06:59:27 +00004153 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004154 ssl->session->ciphersuite,
4155 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004156#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004157 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004158#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Andrzej Kurek894edde2022-09-29 06:31:14 -04004159 prf_func,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004160 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004161 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004162 ssl->conf->endpoint,
4163 ssl );
4164 if( ret != 0 )
4165 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004166#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004167 p += sizeof( ssl->transform->randbytes );
4168
4169#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4170 /* Read connection IDs and store them */
4171 if( (size_t)( end - p ) < 1 )
4172 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4173
4174 ssl->transform->in_cid_len = *p++;
4175
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004176 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004177 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4178
4179 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4180 p += ssl->transform->in_cid_len;
4181
4182 ssl->transform->out_cid_len = *p++;
4183
4184 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4185 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4186
4187 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4188 p += ssl->transform->out_cid_len;
4189#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4190
4191 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004192 * Saved fields from top-level ssl_context structure
4193 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004194 if( (size_t)( end - p ) < 4 )
4195 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4196
4197 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4198 ( (uint32_t) p[1] << 16 ) |
4199 ( (uint32_t) p[2] << 8 ) |
4200 ( (uint32_t) p[3] );
4201 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004202
4203#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4204 if( (size_t)( end - p ) < 16 )
4205 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4206
4207 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4208 ( (uint64_t) p[1] << 48 ) |
4209 ( (uint64_t) p[2] << 40 ) |
4210 ( (uint64_t) p[3] << 32 ) |
4211 ( (uint64_t) p[4] << 24 ) |
4212 ( (uint64_t) p[5] << 16 ) |
4213 ( (uint64_t) p[6] << 8 ) |
4214 ( (uint64_t) p[7] );
4215 p += 8;
4216
4217 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4218 ( (uint64_t) p[1] << 48 ) |
4219 ( (uint64_t) p[2] << 40 ) |
4220 ( (uint64_t) p[3] << 32 ) |
4221 ( (uint64_t) p[4] << 24 ) |
4222 ( (uint64_t) p[5] << 16 ) |
4223 ( (uint64_t) p[6] << 8 ) |
4224 ( (uint64_t) p[7] );
4225 p += 8;
4226#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4227
4228#if defined(MBEDTLS_SSL_PROTO_DTLS)
4229 if( (size_t)( end - p ) < 1 )
4230 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4231
4232 ssl->disable_datagram_packing = *p++;
4233#endif /* MBEDTLS_SSL_PROTO_DTLS */
4234
Jerry Yud9a94fe2021-09-28 18:58:59 +08004235 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004236 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004237 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4238 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004239
4240#if defined(MBEDTLS_SSL_PROTO_DTLS)
4241 if( (size_t)( end - p ) < 2 )
4242 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4243
4244 ssl->mtu = ( p[0] << 8 ) | p[1];
4245 p += 2;
4246#endif /* MBEDTLS_SSL_PROTO_DTLS */
4247
4248#if defined(MBEDTLS_SSL_ALPN)
4249 {
4250 uint8_t alpn_len;
4251 const char **cur;
4252
4253 if( (size_t)( end - p ) < 1 )
4254 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4255
4256 alpn_len = *p++;
4257
4258 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4259 {
4260 /* alpn_chosen should point to an item in the configured list */
4261 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4262 {
4263 if( strlen( *cur ) == alpn_len &&
4264 memcmp( p, cur, alpn_len ) == 0 )
4265 {
4266 ssl->alpn_chosen = *cur;
4267 break;
4268 }
4269 }
4270 }
4271
4272 /* can only happen on conf mismatch */
4273 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4274 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4275
4276 p += alpn_len;
4277 }
4278#endif /* MBEDTLS_SSL_ALPN */
4279
4280 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004281 * Forced fields from top-level ssl_context structure
4282 *
4283 * Most of them already set to the correct value by mbedtls_ssl_init() and
4284 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4285 */
4286 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004287 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004288
Hanno Becker361b10d2019-08-30 10:42:49 +01004289 /* Adjust pointers for header fields of outgoing records to
4290 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004291 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004292
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004293#if defined(MBEDTLS_SSL_PROTO_DTLS)
4294 ssl->in_epoch = 1;
4295#endif
4296
4297 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4298 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004299 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4300 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004301 if( ssl->handshake != NULL )
4302 {
4303 mbedtls_ssl_handshake_free( ssl );
4304 mbedtls_free( ssl->handshake );
4305 ssl->handshake = NULL;
4306 }
4307
4308 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004309 * Done - should have consumed entire buffer
4310 */
4311 if( p != end )
4312 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004313
4314 return( 0 );
4315}
4316
4317/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004318 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004319 */
4320int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4321 const unsigned char *buf,
4322 size_t len )
4323{
4324 int ret = ssl_context_load( context, buf, len );
4325
4326 if( ret != 0 )
4327 mbedtls_ssl_free( context );
4328
4329 return( ret );
4330}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004331#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004332
4333/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004334 * Free an SSL context
4335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004336void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004337{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004338 if( ssl == NULL )
4339 return;
4340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004342
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004343 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004344 {
sander-visserb8aa2072020-05-06 22:05:13 +02004345#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4346 size_t out_buf_len = ssl->out_buf_len;
4347#else
4348 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4349#endif
4350
Darryl Greenb33cc762019-11-28 14:29:44 +00004351 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004352 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004353 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004354 }
4355
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004356 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004357 {
sander-visserb8aa2072020-05-06 22:05:13 +02004358#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4359 size_t in_buf_len = ssl->in_buf_len;
4360#else
4361 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4362#endif
4363
Darryl Greenb33cc762019-11-28 14:29:44 +00004364 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004365 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004366 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004367 }
4368
Paul Bakker48916f92012-09-16 19:57:18 +00004369 if( ssl->transform )
4370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004371 mbedtls_ssl_transform_free( ssl->transform );
4372 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004373 }
4374
4375 if( ssl->handshake )
4376 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004377 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004378 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4379 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004381 mbedtls_free( ssl->handshake );
4382 mbedtls_free( ssl->transform_negotiate );
4383 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004384 }
4385
Ronald Cron6f135e12021-12-08 16:57:54 +01004386#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004387 mbedtls_ssl_transform_free( ssl->transform_application );
4388 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004389#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004390
Paul Bakkerc0463502013-02-14 11:19:38 +01004391 if( ssl->session )
4392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004393 mbedtls_ssl_session_free( ssl->session );
4394 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004395 }
4396
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004397#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004398 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004399 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004400 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004401 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004402 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004403#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004404
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004405#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004406 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004407#endif
4408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004410
Paul Bakker86f04f42013-02-14 11:20:09 +01004411 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004412 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004413}
4414
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004415/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004416 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004417 */
4418void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4419{
4420 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4421}
4422
Gilles Peskineae270bf2021-06-02 00:05:29 +02004423/* The selection should be the same as mbedtls_x509_crt_profile_default in
4424 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004425 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004426 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004427 * about this list.
4428 */
Brett Warrene0edc842021-08-17 09:53:13 +01004429static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004430#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004431 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004432#endif
4433#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004434 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004435#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004436#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004437 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004438#endif
4439#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004440 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004441#endif
4442#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004443 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004444#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004445#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004446 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004447#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004448#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004449 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004450#endif
4451#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004452 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004453#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004454 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004455};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004456
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004457static int ssl_preset_suiteb_ciphersuites[] = {
4458 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4459 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4460 0
4461};
4462
Gilles Peskineeccd8882020-03-10 12:19:08 +01004463#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004464
Jerry Yu909df7b2022-01-22 11:56:27 +08004465/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004466 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004467 * rules SHOULD be upheld.
4468 * - No duplicate entries.
4469 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004470 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004471 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004472 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004473static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004474
Andrzej Kurek25f27152022-08-17 16:09:31 -04004475#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 +08004476 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4477 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004478#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004479 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004480
Andrzej Kurek25f27152022-08-17 16:09:31 -04004481#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 +08004482 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4483 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004484#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004485 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4486
Andrzej Kurek25f27152022-08-17 16:09:31 -04004487#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 +08004488 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4489 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004490#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004491 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004492
Andrzej Kurek25f27152022-08-17 16:09:31 -04004493#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 +08004494 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004495#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 +08004496
Andrzej Kurek25f27152022-08-17 16:09:31 -04004497#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 +08004498 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004499#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 +08004500
Andrzej Kurek25f27152022-08-17 16:09:31 -04004501#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 +08004502 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004503#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 +08004504
Andrzej Kurek25f27152022-08-17 16:09:31 -04004505#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 +08004506 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4507#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4508
Andrzej Kurek25f27152022-08-17 16:09:31 -04004509#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 +08004510 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4511#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4512
Andrzej Kurek25f27152022-08-17 16:09:31 -04004513#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 +08004514 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4515#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4516
Gabor Mezei15b95a62022-05-09 16:37:58 +02004517 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004518};
4519
4520/* NOTICE: see above */
4521#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4522static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004523#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004524#if defined(MBEDTLS_ECDSA_C)
4525 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004526#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004527#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4528 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4529#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004530#if defined(MBEDTLS_RSA_C)
4531 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4532#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004533#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004534#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004535#if defined(MBEDTLS_ECDSA_C)
4536 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004537#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004538#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4539 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4540#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004541#if defined(MBEDTLS_RSA_C)
4542 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4543#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004544#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004545#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004546#if defined(MBEDTLS_ECDSA_C)
4547 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004548#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004549#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4550 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4551#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004552#if defined(MBEDTLS_RSA_C)
4553 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4554#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004555#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004556 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004557};
4558#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4559/* NOTICE: see above */
4560static uint16_t ssl_preset_suiteb_sig_algs[] = {
4561
Andrzej Kurek25f27152022-08-17 16:09:31 -04004562#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 +08004563 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4564 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004565#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004566 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4567
Andrzej Kurek25f27152022-08-17 16:09:31 -04004568#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 +08004569 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4570 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004571#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004572 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004573
Andrzej Kurek25f27152022-08-17 16:09:31 -04004574#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 +08004575 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004576#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 +08004577
Andrzej Kurek25f27152022-08-17 16:09:31 -04004578#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 +08004579 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004580#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004581
Gabor Mezei15b95a62022-05-09 16:37:58 +02004582 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004583};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004584
Jerry Yu909df7b2022-01-22 11:56:27 +08004585/* NOTICE: see above */
4586#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4587static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004588#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004589#if defined(MBEDTLS_ECDSA_C)
4590 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004591#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004592#if defined(MBEDTLS_RSA_C)
4593 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4594#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004595#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004596#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004597#if defined(MBEDTLS_ECDSA_C)
4598 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004599#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004600#if defined(MBEDTLS_RSA_C)
4601 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4602#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004603#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004604 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004605};
Jerry Yu909df7b2022-01-22 11:56:27 +08004606#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4607
Jerry Yu1a8b4812022-01-20 17:56:50 +08004608#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004609
Brett Warrene0edc842021-08-17 09:53:13 +01004610static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004611#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004612 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004613#endif
4614#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004615 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004616#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004617 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004618};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004619
Jerry Yu1a8b4812022-01-20 17:56:50 +08004620#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004621/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004622 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004623MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004624static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004625{
4626 size_t i, j;
4627 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004628
Gabor Mezei15b95a62022-05-09 16:37:58 +02004629 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004630 {
Jerry Yuf377d642022-01-25 10:43:59 +08004631 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004632 {
Jerry Yuf377d642022-01-25 10:43:59 +08004633 if( sig_algs[i] != sig_algs[j] )
4634 continue;
4635 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4636 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4637 sig_algs[i], j, i );
4638 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004639 }
4640 }
4641 return( ret );
4642}
4643
4644#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4645
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004646/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004647 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004648 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004649int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004650 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004651{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004652#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004653 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004654#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004655
Jerry Yu1a8b4812022-01-20 17:56:50 +08004656#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004657 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004658 {
4659 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4660 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4661 }
4662
Jerry Yuf377d642022-01-25 10:43:59 +08004663 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004664 {
4665 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4666 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4667 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004668
4669#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004670 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004671 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004672 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004673 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4674 }
4675
Jerry Yuf377d642022-01-25 10:43:59 +08004676 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004677 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004678 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004679 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4680 }
4681#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004682#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4683
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004684 /* Use the functions here so that they are covered in tests,
4685 * but otherwise access member directly for efficiency */
4686 mbedtls_ssl_conf_endpoint( conf, endpoint );
4687 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004688
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004689 /*
4690 * Things that are common to all presets
4691 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004692#if defined(MBEDTLS_SSL_CLI_C)
4693 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4694 {
4695 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4696#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4697 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4698#endif
4699 }
4700#endif
4701
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004702#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4703 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4704#endif
4705
4706#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4707 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4708#endif
4709
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004710#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004711 conf->f_cookie_write = ssl_cookie_write_dummy;
4712 conf->f_cookie_check = ssl_cookie_check_dummy;
4713#endif
4714
4715#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4716 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4717#endif
4718
Janos Follath088ce432017-04-10 12:42:31 +01004719#if defined(MBEDTLS_SSL_SRV_C)
4720 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004721 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004722#endif
4723
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004724#if defined(MBEDTLS_SSL_PROTO_DTLS)
4725 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4726 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4727#endif
4728
4729#if defined(MBEDTLS_SSL_RENEGOTIATION)
4730 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004731 memset( conf->renego_period, 0x00, 2 );
4732 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004733#endif
4734
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004735#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004736 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4737 {
4738 const unsigned char dhm_p[] =
4739 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4740 const unsigned char dhm_g[] =
4741 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004742
Hanno Beckere2defad2021-07-24 05:59:17 +01004743 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4744 dhm_p, sizeof( dhm_p ),
4745 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4746 {
4747 return( ret );
4748 }
4749 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004750#endif
4751
Ronald Cron6f135e12021-12-08 16:57:54 +01004752#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004753#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004754 mbedtls_ssl_conf_new_session_tickets(
4755 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4756#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004757 /*
4758 * Allow all TLS 1.3 key exchange modes by default.
4759 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004760 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004761#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004762
XiaokangQian4d3a6042022-04-21 13:46:17 +00004763 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4764 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004765#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004766 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004767 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004768#else
XiaokangQian060d8672022-04-21 09:24:56 +00004769 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004770#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004771 }
4772 else
4773 {
4774#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4775 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4776 {
4777 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4778 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4779 }
4780 else
4781 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4782 {
4783 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4784 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4785 }
4786#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4787 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4788 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4789#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4790 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4791 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4792#else
4793 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4794#endif
4795 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004796
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004797 /*
4798 * Preset-specific defaults
4799 */
4800 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004801 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004802 /*
4803 * NSA Suite B
4804 */
4805 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004806
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004807 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004808
4809#if defined(MBEDTLS_X509_CRT_PARSE_C)
4810 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004811#endif
4812
Gilles Peskineeccd8882020-03-10 12:19:08 +01004813#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004814#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4815 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4816 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4817 else
4818#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4819 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004820#endif
4821
Brett Warrene0edc842021-08-17 09:53:13 +01004822#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4823 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004824#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004825 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004826 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004827
4828 /*
4829 * Default
4830 */
4831 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004832
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004833 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004834
4835#if defined(MBEDTLS_X509_CRT_PARSE_C)
4836 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4837#endif
4838
Gilles Peskineeccd8882020-03-10 12:19:08 +01004839#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004840#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4841 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4842 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4843 else
4844#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4845 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004846#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004847
Brett Warrene0edc842021-08-17 09:53:13 +01004848#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4849 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004850#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004851 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004852
4853#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4854 conf->dhm_min_bitlen = 1024;
4855#endif
4856 }
4857
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004858 return( 0 );
4859}
4860
4861/*
4862 * Free mbedtls_ssl_config
4863 */
4864void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4865{
4866#if defined(MBEDTLS_DHM_C)
4867 mbedtls_mpi_free( &conf->dhm_P );
4868 mbedtls_mpi_free( &conf->dhm_G );
4869#endif
4870
Gilles Peskineeccd8882020-03-10 12:19:08 +01004871#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004872#if defined(MBEDTLS_USE_PSA_CRYPTO)
4873 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4874 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004875 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4876 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004877#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004878 if( conf->psk != NULL )
4879 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004880 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004881 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004882 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004883 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004884 }
4885
4886 if( conf->psk_identity != NULL )
4887 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004888 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004889 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004890 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004891 conf->psk_identity_len = 0;
4892 }
4893#endif
4894
4895#if defined(MBEDTLS_X509_CRT_PARSE_C)
4896 ssl_key_cert_free( conf->key_cert );
4897#endif
4898
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004899 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004900}
4901
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004902#if defined(MBEDTLS_PK_C) && \
4903 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004904/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004905 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004906 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004907unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004908{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004909#if defined(MBEDTLS_RSA_C)
4910 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4911 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004912#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004913#if defined(MBEDTLS_ECDSA_C)
4914 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4915 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004916#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004917 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004918}
4919
Hanno Becker7e5437a2017-04-28 17:15:26 +01004920unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4921{
4922 switch( type ) {
4923 case MBEDTLS_PK_RSA:
4924 return( MBEDTLS_SSL_SIG_RSA );
4925 case MBEDTLS_PK_ECDSA:
4926 case MBEDTLS_PK_ECKEY:
4927 return( MBEDTLS_SSL_SIG_ECDSA );
4928 default:
4929 return( MBEDTLS_SSL_SIG_ANON );
4930 }
4931}
4932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004933mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004934{
4935 switch( sig )
4936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004937#if defined(MBEDTLS_RSA_C)
4938 case MBEDTLS_SSL_SIG_RSA:
4939 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004940#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004941#if defined(MBEDTLS_ECDSA_C)
4942 case MBEDTLS_SSL_SIG_ECDSA:
4943 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004944#endif
4945 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004946 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004947 }
4948}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004949#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004950
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004951/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004952 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004953 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004955{
4956 switch( hash )
4957 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004958#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004959 case MBEDTLS_SSL_HASH_MD5:
4960 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004961#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004962#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004963 case MBEDTLS_SSL_HASH_SHA1:
4964 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004965#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004966#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004967 case MBEDTLS_SSL_HASH_SHA224:
4968 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004969#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004970#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004971 case MBEDTLS_SSL_HASH_SHA256:
4972 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004973#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004974#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004975 case MBEDTLS_SSL_HASH_SHA384:
4976 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004977#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004978#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004979 case MBEDTLS_SSL_HASH_SHA512:
4980 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004981#endif
4982 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004983 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004984 }
4985}
4986
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004987/*
4988 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4989 */
4990unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4991{
4992 switch( md )
4993 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004994#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004995 case MBEDTLS_MD_MD5:
4996 return( MBEDTLS_SSL_HASH_MD5 );
4997#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004998#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004999 case MBEDTLS_MD_SHA1:
5000 return( MBEDTLS_SSL_HASH_SHA1 );
5001#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005002#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005003 case MBEDTLS_MD_SHA224:
5004 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005005#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005006#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005007 case MBEDTLS_MD_SHA256:
5008 return( MBEDTLS_SSL_HASH_SHA256 );
5009#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005010#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005011 case MBEDTLS_MD_SHA384:
5012 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005013#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005014#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005015 case MBEDTLS_MD_SHA512:
5016 return( MBEDTLS_SSL_HASH_SHA512 );
5017#endif
5018 default:
5019 return( MBEDTLS_SSL_HASH_NONE );
5020 }
5021}
5022
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005023/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005024 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005025 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005026 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005027int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005028{
Brett Warrene0edc842021-08-17 09:53:13 +01005029 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005030
Brett Warrene0edc842021-08-17 09:53:13 +01005031 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005032 return( -1 );
5033
Brett Warrene0edc842021-08-17 09:53:13 +01005034 for( ; *group_list != 0; group_list++ )
5035 {
5036 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005037 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005038 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005039
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005040 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005041}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005042
5043#if defined(MBEDTLS_ECP_C)
5044/*
5045 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5046 */
5047int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5048{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005049 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005050 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005051
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005052 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005053 return -1;
5054
5055 uint16_t tls_id = grp_info->tls_id;
5056
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005057 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5058}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005059#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005061#if defined(MBEDTLS_X509_CRT_PARSE_C)
5062int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5063 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005064 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005065 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005066{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005067 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005068 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005069 const char *ext_oid;
5070 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005072 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005073 {
5074 /* Server part of the key exchange */
5075 switch( ciphersuite->key_exchange )
5076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005077 case MBEDTLS_KEY_EXCHANGE_RSA:
5078 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005079 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005080 break;
5081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005082 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5083 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5084 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5085 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005086 break;
5087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005088 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5089 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005090 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005091 break;
5092
5093 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005094 case MBEDTLS_KEY_EXCHANGE_NONE:
5095 case MBEDTLS_KEY_EXCHANGE_PSK:
5096 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5097 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005098 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005099 usage = 0;
5100 }
5101 }
5102 else
5103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005104 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5105 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005106 }
5107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005108 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005109 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005110 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005111 ret = -1;
5112 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005114 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005116 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5117 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005118 }
5119 else
5120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005121 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5122 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005123 }
5124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005125 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005126 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005127 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005128 ret = -1;
5129 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005130
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005131 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005132}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005133#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005134
Jerry Yu148165c2021-09-24 23:20:59 +08005135#if defined(MBEDTLS_USE_PSA_CRYPTO)
5136int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5137 const mbedtls_md_type_t md,
5138 unsigned char *dst,
5139 size_t dst_len,
5140 size_t *olen )
5141{
Ronald Cronf6893e12022-01-07 22:09:01 +01005142 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5143 psa_hash_operation_t *hash_operation_to_clone;
5144 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5145
Jerry Yu148165c2021-09-24 23:20:59 +08005146 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005147
5148 switch( md )
5149 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005150#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005151 case MBEDTLS_MD_SHA384:
5152 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5153 break;
5154#endif
5155
Andrzej Kurek25f27152022-08-17 16:09:31 -04005156#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005157 case MBEDTLS_MD_SHA256:
5158 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5159 break;
5160#endif
5161
5162 default:
5163 goto exit;
5164 }
5165
5166 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5167 if( status != PSA_SUCCESS )
5168 goto exit;
5169
5170 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5171 if( status != PSA_SUCCESS )
5172 goto exit;
5173
5174exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005175#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5176 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5177 (void) ssl;
5178#endif
Ronald Cronb788c042022-02-15 09:14:15 +01005179 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005180}
5181#else /* MBEDTLS_USE_PSA_CRYPTO */
5182
Andrzej Kurek25f27152022-08-17 16:09:31 -04005183#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005184MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005185static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5186 unsigned char *dst,
5187 size_t dst_len,
5188 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005189{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005190 int ret;
5191 mbedtls_sha512_context sha512;
5192
5193 if( dst_len < 48 )
5194 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5195
5196 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005197 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005198
5199 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5200 {
5201 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5202 goto exit;
5203 }
5204
5205 *olen = 48;
5206
5207exit:
5208
5209 mbedtls_sha512_free( &sha512 );
5210 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005211}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005212#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005213
Andrzej Kurek25f27152022-08-17 16:09:31 -04005214#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005215MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005216static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5217 unsigned char *dst,
5218 size_t dst_len,
5219 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005220{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005221 int ret;
5222 mbedtls_sha256_context sha256;
5223
5224 if( dst_len < 32 )
5225 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5226
5227 mbedtls_sha256_init( &sha256 );
5228 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005229
Jerry Yu24c0ec32021-09-09 14:21:07 +08005230 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5231 {
5232 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5233 goto exit;
5234 }
5235
5236 *olen = 32;
5237
5238exit:
5239
5240 mbedtls_sha256_free( &sha256 );
5241 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005242}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005243#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005244
Jerry Yu000f9762021-09-14 11:12:51 +08005245int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5246 const mbedtls_md_type_t md,
5247 unsigned char *dst,
5248 size_t dst_len,
5249 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005250{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005251 switch( md )
5252 {
5253
Andrzej Kurek25f27152022-08-17 16:09:31 -04005254#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005255 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005256 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005257#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005258
Andrzej Kurek25f27152022-08-17 16:09:31 -04005259#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005260 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005261 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005262#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005263
5264 default:
5265 break;
5266 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005267 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005268}
XiaokangQian647719a2021-12-07 09:16:29 +00005269
Jerry Yu148165c2021-09-24 23:20:59 +08005270#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005271
Gabor Mezei078e8032022-04-27 21:17:56 +02005272#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5273/* mbedtls_ssl_parse_sig_alg_ext()
5274 *
5275 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5276 * value (TLS 1.3 RFC8446):
5277 * enum {
5278 * ....
5279 * ecdsa_secp256r1_sha256( 0x0403 ),
5280 * ecdsa_secp384r1_sha384( 0x0503 ),
5281 * ecdsa_secp521r1_sha512( 0x0603 ),
5282 * ....
5283 * } SignatureScheme;
5284 *
5285 * struct {
5286 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5287 * } SignatureSchemeList;
5288 *
5289 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5290 * value (TLS 1.2 RFC5246):
5291 * enum {
5292 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5293 * sha512(6), (255)
5294 * } HashAlgorithm;
5295 *
5296 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5297 * SignatureAlgorithm;
5298 *
5299 * struct {
5300 * HashAlgorithm hash;
5301 * SignatureAlgorithm signature;
5302 * } SignatureAndHashAlgorithm;
5303 *
5304 * SignatureAndHashAlgorithm
5305 * supported_signature_algorithms<2..2^16-2>;
5306 *
5307 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5308 * generalization of the TLS 1.2 signature algorithm extension.
5309 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5310 * `SignatureScheme` field of TLS 1.3
5311 *
5312 */
5313int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5314 const unsigned char *buf,
5315 const unsigned char *end )
5316{
5317 const unsigned char *p = buf;
5318 size_t supported_sig_algs_len = 0;
5319 const unsigned char *supported_sig_algs_end;
5320 uint16_t sig_alg;
5321 uint32_t common_idx = 0;
5322
5323 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5324 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5325 p += 2;
5326
5327 memset( ssl->handshake->received_sig_algs, 0,
5328 sizeof(ssl->handshake->received_sig_algs) );
5329
5330 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5331 supported_sig_algs_end = p + supported_sig_algs_len;
5332 while( p < supported_sig_algs_end )
5333 {
5334 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5335 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5336 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005337 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5338 sig_alg,
5339 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005340#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005341 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005342 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5343 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5344 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005345 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005346 }
5347#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005348
Jerry Yuf3b46b52022-06-19 16:52:27 +08005349 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5350 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005351
5352 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5353 {
5354 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5355 common_idx += 1;
5356 }
5357 }
5358 /* Check that we consumed all the message. */
5359 if( p != end )
5360 {
5361 MBEDTLS_SSL_DEBUG_MSG( 1,
5362 ( "Signature algorithms extension length misaligned" ) );
5363 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5364 MBEDTLS_ERR_SSL_DECODE_ERROR );
5365 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5366 }
5367
5368 if( common_idx == 0 )
5369 {
5370 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5371 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5372 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5373 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5374 }
5375
Gabor Mezei15b95a62022-05-09 16:37:58 +02005376 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005377 return( 0 );
5378}
5379
5380#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5381
Jerry Yudc7bd172022-02-17 13:44:15 +08005382#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5383
5384#if defined(MBEDTLS_USE_PSA_CRYPTO)
5385
5386static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5387 mbedtls_svc_key_id_t key,
5388 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005389 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005390 const unsigned char* seed, size_t seed_length,
5391 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005392 const unsigned char* other_secret,
5393 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005394 size_t capacity )
5395{
5396 psa_status_t status;
5397
5398 status = psa_key_derivation_setup( derivation, alg );
5399 if( status != PSA_SUCCESS )
5400 return( status );
5401
5402 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5403 {
5404 status = psa_key_derivation_input_bytes( derivation,
5405 PSA_KEY_DERIVATION_INPUT_SEED,
5406 seed, seed_length );
5407 if( status != PSA_SUCCESS )
5408 return( status );
5409
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005410 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005411 {
5412 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005413 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5414 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005415 if( status != PSA_SUCCESS )
5416 return( status );
5417 }
5418
Jerry Yudc7bd172022-02-17 13:44:15 +08005419 if( mbedtls_svc_key_id_is_null( key ) )
5420 {
5421 status = psa_key_derivation_input_bytes(
5422 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005423 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005424 }
5425 else
5426 {
5427 status = psa_key_derivation_input_key(
5428 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5429 }
5430 if( status != PSA_SUCCESS )
5431 return( status );
5432
5433 status = psa_key_derivation_input_bytes( derivation,
5434 PSA_KEY_DERIVATION_INPUT_LABEL,
5435 label, label_length );
5436 if( status != PSA_SUCCESS )
5437 return( status );
5438 }
5439 else
5440 {
5441 return( PSA_ERROR_NOT_SUPPORTED );
5442 }
5443
5444 status = psa_key_derivation_set_capacity( derivation, capacity );
5445 if( status != PSA_SUCCESS )
5446 return( status );
5447
5448 return( PSA_SUCCESS );
5449}
5450
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005451#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \
5452 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005453MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005454static int tls_prf_generic( mbedtls_md_type_t md_type,
5455 const unsigned char *secret, size_t slen,
5456 const char *label,
5457 const unsigned char *random, size_t rlen,
5458 unsigned char *dstbuf, size_t dlen )
5459{
5460 psa_status_t status;
5461 psa_algorithm_t alg;
5462 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5463 psa_key_derivation_operation_t derivation =
5464 PSA_KEY_DERIVATION_OPERATION_INIT;
5465
5466 if( md_type == MBEDTLS_MD_SHA384 )
5467 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5468 else
5469 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5470
5471 /* Normally a "secret" should be long enough to be impossible to
5472 * find by brute force, and in particular should not be empty. But
5473 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5474 * and for this use case it makes sense to have a 0-length "secret".
5475 * Since the key API doesn't allow importing a key of length 0,
5476 * keep master_key=0, which setup_psa_key_derivation() understands
5477 * to mean a 0-length "secret" input. */
5478 if( slen != 0 )
5479 {
5480 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5481 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5482 psa_set_key_algorithm( &key_attributes, alg );
5483 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5484
5485 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5486 if( status != PSA_SUCCESS )
5487 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5488 }
5489
5490 status = setup_psa_key_derivation( &derivation,
5491 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005492 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005493 random, rlen,
5494 (unsigned char const *) label,
5495 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005496 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005497 dlen );
5498 if( status != PSA_SUCCESS )
5499 {
5500 psa_key_derivation_abort( &derivation );
5501 psa_destroy_key( master_key );
5502 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5503 }
5504
5505 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5506 if( status != PSA_SUCCESS )
5507 {
5508 psa_key_derivation_abort( &derivation );
5509 psa_destroy_key( master_key );
5510 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5511 }
5512
5513 status = psa_key_derivation_abort( &derivation );
5514 if( status != PSA_SUCCESS )
5515 {
5516 psa_destroy_key( master_key );
5517 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5518 }
5519
5520 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5521 status = psa_destroy_key( master_key );
5522 if( status != PSA_SUCCESS )
5523 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5524
5525 return( 0 );
5526}
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005527#endif
Jerry Yudc7bd172022-02-17 13:44:15 +08005528#else /* MBEDTLS_USE_PSA_CRYPTO */
5529
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005530MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005531static int tls_prf_generic( mbedtls_md_type_t md_type,
5532 const unsigned char *secret, size_t slen,
5533 const char *label,
5534 const unsigned char *random, size_t rlen,
5535 unsigned char *dstbuf, size_t dlen )
5536{
5537 size_t nb;
5538 size_t i, j, k, md_len;
5539 unsigned char *tmp;
5540 size_t tmp_len = 0;
5541 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5542 const mbedtls_md_info_t *md_info;
5543 mbedtls_md_context_t md_ctx;
5544 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5545
5546 mbedtls_md_init( &md_ctx );
5547
5548 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5549 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5550
5551 md_len = mbedtls_md_get_size( md_info );
5552
5553 tmp_len = md_len + strlen( label ) + rlen;
5554 tmp = mbedtls_calloc( 1, tmp_len );
5555 if( tmp == NULL )
5556 {
5557 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5558 goto exit;
5559 }
5560
5561 nb = strlen( label );
5562 memcpy( tmp + md_len, label, nb );
5563 memcpy( tmp + md_len + nb, random, rlen );
5564 nb += rlen;
5565
5566 /*
5567 * Compute P_<hash>(secret, label + random)[0..dlen]
5568 */
5569 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5570 goto exit;
5571
5572 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5573 if( ret != 0 )
5574 goto exit;
5575 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5576 if( ret != 0 )
5577 goto exit;
5578 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5579 if( ret != 0 )
5580 goto exit;
5581
5582 for( i = 0; i < dlen; i += md_len )
5583 {
5584 ret = mbedtls_md_hmac_reset ( &md_ctx );
5585 if( ret != 0 )
5586 goto exit;
5587 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5588 if( ret != 0 )
5589 goto exit;
5590 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5591 if( ret != 0 )
5592 goto exit;
5593
5594 ret = mbedtls_md_hmac_reset ( &md_ctx );
5595 if( ret != 0 )
5596 goto exit;
5597 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5598 if( ret != 0 )
5599 goto exit;
5600 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5601 if( ret != 0 )
5602 goto exit;
5603
5604 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5605
5606 for( j = 0; j < k; j++ )
5607 dstbuf[i + j] = h_i[j];
5608 }
5609
5610exit:
5611 mbedtls_md_free( &md_ctx );
5612
5613 mbedtls_platform_zeroize( tmp, tmp_len );
5614 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5615
5616 mbedtls_free( tmp );
5617
5618 return( ret );
5619}
5620#endif /* MBEDTLS_USE_PSA_CRYPTO */
5621
Andrzej Kurek25f27152022-08-17 16:09:31 -04005622#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005623MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005624static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5625 const char *label,
5626 const unsigned char *random, size_t rlen,
5627 unsigned char *dstbuf, size_t dlen )
5628{
5629 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5630 label, random, rlen, dstbuf, dlen ) );
5631}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005632#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005633
Andrzej Kurek25f27152022-08-17 16:09:31 -04005634#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005635MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005636static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5637 const char *label,
5638 const unsigned char *random, size_t rlen,
5639 unsigned char *dstbuf, size_t dlen )
5640{
5641 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5642 label, random, rlen, dstbuf, dlen ) );
5643}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005644#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005645
Jerry Yuf009d862022-02-17 14:01:37 +08005646/*
5647 * Set appropriate PRF function and other SSL / TLS1.2 functions
5648 *
5649 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005650 * - hash associated with the ciphersuite (only used by TLS 1.2)
5651 *
5652 * Outputs:
5653 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5654 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005655MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005656static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005657 mbedtls_md_type_t hash )
5658{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005659#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005660 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005661 {
5662 handshake->tls_prf = tls_prf_sha384;
5663 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5664 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5665 }
5666 else
5667#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005668#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005669 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005670 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005671 handshake->tls_prf = tls_prf_sha256;
5672 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5673 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5674 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005675#else
Jerry Yuf009d862022-02-17 14:01:37 +08005676 {
Jerry Yuf009d862022-02-17 14:01:37 +08005677 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005678 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005679 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5680 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005681#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005682
5683 return( 0 );
5684}
Jerry Yud6ab2352022-02-17 14:03:43 +08005685
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005686/*
5687 * Compute master secret if needed
5688 *
5689 * Parameters:
5690 * [in/out] handshake
5691 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5692 * (PSA-PSK) ciphersuite_info, psk_opaque
5693 * [out] premaster (cleared)
5694 * [out] master
5695 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5696 * debug: conf->f_dbg, conf->p_dbg
5697 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005698 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005699 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005700MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005701static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5702 unsigned char *master,
5703 const mbedtls_ssl_context *ssl )
5704{
5705 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5706
5707 /* cf. RFC 5246, Section 8.1:
5708 * "The master secret is always exactly 48 bytes in length." */
5709 size_t const master_secret_len = 48;
5710
5711#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5712 unsigned char session_hash[48];
5713#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5714
5715 /* The label for the KDF used for key expansion.
5716 * This is either "master secret" or "extended master secret"
5717 * depending on whether the Extended Master Secret extension
5718 * is used. */
5719 char const *lbl = "master secret";
5720
Przemek Stekielae4ed302022-04-05 17:15:55 +02005721 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005722 * - If the Extended Master Secret extension is not used,
5723 * this is ClientHello.Random + ServerHello.Random
5724 * (see Sect. 8.1 in RFC 5246).
5725 * - If the Extended Master Secret extension is used,
5726 * this is the transcript of the handshake so far.
5727 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005728 unsigned char const *seed = handshake->randbytes;
5729 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005730
5731#if !defined(MBEDTLS_DEBUG_C) && \
5732 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5733 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5734 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5735 ssl = NULL; /* make sure we don't use it except for those cases */
5736 (void) ssl;
5737#endif
5738
5739 if( handshake->resume != 0 )
5740 {
5741 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5742 return( 0 );
5743 }
5744
5745#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5746 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5747 {
5748 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005749 seed = session_hash;
5750 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005751
5752 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005753 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005754 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005755#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005756
Przemek Stekiel99114f32022-04-22 11:20:09 +02005757#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005758 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005759 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005760 {
5761 /* Perform PSK-to-MS expansion in a single step. */
5762 psa_status_t status;
5763 psa_algorithm_t alg;
5764 mbedtls_svc_key_id_t psk;
5765 psa_key_derivation_operation_t derivation =
5766 PSA_KEY_DERIVATION_OPERATION_INIT;
5767 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5768
5769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5770
5771 psk = mbedtls_ssl_get_opaque_psk( ssl );
5772
5773 if( hash_alg == MBEDTLS_MD_SHA384 )
5774 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5775 else
5776 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5777
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005778 size_t other_secret_len = 0;
5779 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005780
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005781 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005782 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005783 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005784 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005785 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005786 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005787 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005788 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005789 other_secret_len = 48;
5790 other_secret = handshake->premaster + 2;
5791 break;
5792 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005793 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005794 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5795 other_secret = handshake->premaster + 2;
5796 break;
5797 default:
5798 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005799 }
5800
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005801 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005802 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005803 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005804 (unsigned char const *) lbl,
5805 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005806 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005807 master_secret_len );
5808 if( status != PSA_SUCCESS )
5809 {
5810 psa_key_derivation_abort( &derivation );
5811 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5812 }
5813
5814 status = psa_key_derivation_output_bytes( &derivation,
5815 master,
5816 master_secret_len );
5817 if( status != PSA_SUCCESS )
5818 {
5819 psa_key_derivation_abort( &derivation );
5820 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5821 }
5822
5823 status = psa_key_derivation_abort( &derivation );
5824 if( status != PSA_SUCCESS )
5825 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5826 }
5827 else
5828#endif
5829 {
5830 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005831 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005832 master,
5833 master_secret_len );
5834 if( ret != 0 )
5835 {
5836 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5837 return( ret );
5838 }
5839
5840 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5841 handshake->premaster,
5842 handshake->pmslen );
5843
5844 mbedtls_platform_zeroize( handshake->premaster,
5845 sizeof(handshake->premaster) );
5846 }
5847
5848 return( 0 );
5849}
5850
Jerry Yud62f87e2022-02-17 14:09:02 +08005851int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5852{
5853 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5854 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5855 ssl->handshake->ciphersuite_info;
5856
5857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5858
5859 /* Set PRF, calc_verify and calc_finished function pointers */
5860 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005861 ciphersuite_info->mac );
5862 if( ret != 0 )
5863 {
5864 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5865 return( ret );
5866 }
5867
5868 /* Compute master secret if needed */
5869 ret = ssl_compute_master( ssl->handshake,
5870 ssl->session_negotiate->master,
5871 ssl );
5872 if( ret != 0 )
5873 {
5874 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5875 return( ret );
5876 }
5877
5878 /* Swap the client and server random values:
5879 * - MS derivation wanted client+server (RFC 5246 8.1)
5880 * - key derivation wants server+client (RFC 5246 6.3) */
5881 {
5882 unsigned char tmp[64];
5883 memcpy( tmp, ssl->handshake->randbytes, 64 );
5884 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5885 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5886 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5887 }
5888
5889 /* Populate transform structure */
5890 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5891 ssl->session_negotiate->ciphersuite,
5892 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005893#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005894 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005895#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005896 ssl->handshake->tls_prf,
5897 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005898 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005899 ssl->conf->endpoint,
5900 ssl );
5901 if( ret != 0 )
5902 {
5903 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5904 return( ret );
5905 }
5906
5907 /* We no longer need Server/ClientHello.random values */
5908 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5909 sizeof( ssl->handshake->randbytes ) );
5910
5911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5912
5913 return( 0 );
5914}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005915
Ronald Cron4dcbca92022-03-07 10:21:40 +01005916int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5917{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005918 switch( md )
5919 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005920#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005921 case MBEDTLS_SSL_HASH_SHA384:
5922 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5923 break;
5924#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005925#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005926 case MBEDTLS_SSL_HASH_SHA256:
5927 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5928 break;
5929#endif
5930 default:
5931 return( -1 );
5932 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005933#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5934 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5935 (void) ssl;
5936#endif
Ronald Cronc2f13a02022-03-07 10:25:24 +01005937 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005938}
5939
Andrzej Kurek25f27152022-08-17 16:09:31 -04005940#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005941void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5942 unsigned char *hash,
5943 size_t *hlen )
5944{
5945#if defined(MBEDTLS_USE_PSA_CRYPTO)
5946 size_t hash_size;
5947 psa_status_t status;
5948 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5949
5950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5951 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5952 if( status != PSA_SUCCESS )
5953 {
5954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5955 return;
5956 }
5957
5958 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5959 if( status != PSA_SUCCESS )
5960 {
5961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5962 return;
5963 }
5964
5965 *hlen = 32;
5966 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5968#else
5969 mbedtls_sha256_context sha256;
5970
5971 mbedtls_sha256_init( &sha256 );
5972
5973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5974
5975 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5976 mbedtls_sha256_finish( &sha256, hash );
5977
5978 *hlen = 32;
5979
5980 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5982
5983 mbedtls_sha256_free( &sha256 );
5984#endif /* MBEDTLS_USE_PSA_CRYPTO */
5985 return;
5986}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005987#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08005988
Andrzej Kurek25f27152022-08-17 16:09:31 -04005989#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08005990void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5991 unsigned char *hash,
5992 size_t *hlen )
5993{
5994#if defined(MBEDTLS_USE_PSA_CRYPTO)
5995 size_t hash_size;
5996 psa_status_t status;
5997 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5998
5999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6000 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6001 if( status != PSA_SUCCESS )
6002 {
6003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6004 return;
6005 }
6006
6007 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6008 if( status != PSA_SUCCESS )
6009 {
6010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6011 return;
6012 }
6013
6014 *hlen = 48;
6015 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6017#else
6018 mbedtls_sha512_context sha512;
6019
6020 mbedtls_sha512_init( &sha512 );
6021
6022 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6023
Andrzej Kureka242e832022-08-11 10:03:14 -04006024 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006025 mbedtls_sha512_finish( &sha512, hash );
6026
6027 *hlen = 48;
6028
6029 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6031
6032 mbedtls_sha512_free( &sha512 );
6033#endif /* MBEDTLS_USE_PSA_CRYPTO */
6034 return;
6035}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006036#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006037
Neil Armstrong80f6f322022-05-03 17:56:38 +02006038#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6039 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006040int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6041{
6042 unsigned char *p = ssl->handshake->premaster;
6043 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6044 const unsigned char *psk = NULL;
6045 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006046 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006047
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006048 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006049 {
6050 /*
6051 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006052 * checked before calling this function.
6053 *
6054 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006055 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006056 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006057 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6058 {
6059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6060 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6061 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006062 }
6063
6064 /*
6065 * PMS = struct {
6066 * opaque other_secret<0..2^16-1>;
6067 * opaque psk<0..2^16-1>;
6068 * };
6069 * with "other_secret" depending on the particular key exchange
6070 */
6071#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6072 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6073 {
6074 if( end - p < 2 )
6075 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6076
6077 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6078 p += 2;
6079
6080 if( end < p || (size_t)( end - p ) < psk_len )
6081 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6082
6083 memset( p, 0, psk_len );
6084 p += psk_len;
6085 }
6086 else
6087#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6088#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6089 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6090 {
6091 /*
6092 * other_secret already set by the ClientKeyExchange message,
6093 * and is 48 bytes long
6094 */
6095 if( end - p < 2 )
6096 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6097
6098 *p++ = 0;
6099 *p++ = 48;
6100 p += 48;
6101 }
6102 else
6103#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6104#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6105 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6106 {
6107 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6108 size_t len;
6109
6110 /* Write length only when we know the actual value */
6111 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6112 p + 2, end - ( p + 2 ), &len,
6113 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6114 {
6115 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6116 return( ret );
6117 }
6118 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6119 p += 2 + len;
6120
6121 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6122 }
6123 else
6124#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006125#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006126 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6127 {
6128 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6129 size_t zlen;
6130
6131 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6132 p + 2, end - ( p + 2 ),
6133 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6134 {
6135 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6136 return( ret );
6137 }
6138
6139 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6140 p += 2 + zlen;
6141
6142 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6143 MBEDTLS_DEBUG_ECDH_Z );
6144 }
6145 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006146#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006147 {
6148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6149 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6150 }
6151
6152 /* opaque psk<0..2^16-1>; */
6153 if( end - p < 2 )
6154 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6155
6156 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6157 p += 2;
6158
6159 if( end < p || (size_t)( end - p ) < psk_len )
6160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6161
6162 memcpy( p, psk, psk_len );
6163 p += psk_len;
6164
6165 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6166
6167 return( 0 );
6168}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006169#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006170
6171#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006172MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006173static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6174
6175#if defined(MBEDTLS_SSL_PROTO_DTLS)
6176int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6177{
6178 /* If renegotiation is not enforced, retransmit until we would reach max
6179 * timeout if we were using the usual handshake doubling scheme */
6180 if( ssl->conf->renego_max_records < 0 )
6181 {
6182 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6183 unsigned char doublings = 1;
6184
6185 while( ratio != 0 )
6186 {
6187 ++doublings;
6188 ratio >>= 1;
6189 }
6190
6191 if( ++ssl->renego_records_seen > doublings )
6192 {
6193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6194 return( 0 );
6195 }
6196 }
6197
6198 return( ssl_write_hello_request( ssl ) );
6199}
6200#endif
6201#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006202
Jerry Yud9526692022-02-17 14:23:47 +08006203/*
6204 * Handshake functions
6205 */
6206#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6207/* No certificate support -> dummy functions */
6208int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6209{
6210 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6211 ssl->handshake->ciphersuite_info;
6212
6213 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6214
6215 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6216 {
6217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6218 ssl->state++;
6219 return( 0 );
6220 }
6221
6222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6223 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6224}
6225
6226int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6227{
6228 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6229 ssl->handshake->ciphersuite_info;
6230
6231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6232
6233 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6234 {
6235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6236 ssl->state++;
6237 return( 0 );
6238 }
6239
6240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6241 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6242}
6243
6244#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6245/* Some certificate support -> implement write and parse */
6246
6247int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6248{
6249 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6250 size_t i, n;
6251 const mbedtls_x509_crt *crt;
6252 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6253 ssl->handshake->ciphersuite_info;
6254
6255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6256
6257 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6258 {
6259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6260 ssl->state++;
6261 return( 0 );
6262 }
6263
6264#if defined(MBEDTLS_SSL_CLI_C)
6265 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6266 {
6267 if( ssl->handshake->client_auth == 0 )
6268 {
6269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6270 ssl->state++;
6271 return( 0 );
6272 }
6273 }
6274#endif /* MBEDTLS_SSL_CLI_C */
6275#if defined(MBEDTLS_SSL_SRV_C)
6276 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6277 {
6278 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6279 {
6280 /* Should never happen because we shouldn't have picked the
6281 * ciphersuite if we don't have a certificate. */
6282 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6283 }
6284 }
6285#endif
6286
6287 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6288
6289 /*
6290 * 0 . 0 handshake type
6291 * 1 . 3 handshake length
6292 * 4 . 6 length of all certs
6293 * 7 . 9 length of cert. 1
6294 * 10 . n-1 peer certificate
6295 * n . n+2 length of cert. 2
6296 * n+3 . ... upper level cert, etc.
6297 */
6298 i = 7;
6299 crt = mbedtls_ssl_own_cert( ssl );
6300
6301 while( crt != NULL )
6302 {
6303 n = crt->raw.len;
6304 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6305 {
6306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6307 " > %" MBEDTLS_PRINTF_SIZET,
6308 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6309 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6310 }
6311
6312 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6313 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6314 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6315
6316 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6317 i += n; crt = crt->next;
6318 }
6319
6320 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6321 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6322 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6323
6324 ssl->out_msglen = i;
6325 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6326 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6327
6328 ssl->state++;
6329
6330 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6331 {
6332 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6333 return( ret );
6334 }
6335
6336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6337
6338 return( ret );
6339}
6340
6341#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6342
6343#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006344MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006345static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6346 unsigned char *crt_buf,
6347 size_t crt_buf_len )
6348{
6349 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6350
6351 if( peer_crt == NULL )
6352 return( -1 );
6353
6354 if( peer_crt->raw.len != crt_buf_len )
6355 return( -1 );
6356
6357 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6358}
6359#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006360MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006361static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6362 unsigned char *crt_buf,
6363 size_t crt_buf_len )
6364{
6365 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6366 unsigned char const * const peer_cert_digest =
6367 ssl->session->peer_cert_digest;
6368 mbedtls_md_type_t const peer_cert_digest_type =
6369 ssl->session->peer_cert_digest_type;
6370 mbedtls_md_info_t const * const digest_info =
6371 mbedtls_md_info_from_type( peer_cert_digest_type );
6372 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6373 size_t digest_len;
6374
6375 if( peer_cert_digest == NULL || digest_info == NULL )
6376 return( -1 );
6377
6378 digest_len = mbedtls_md_get_size( digest_info );
6379 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6380 return( -1 );
6381
6382 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6383 if( ret != 0 )
6384 return( -1 );
6385
6386 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6387}
6388#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6389#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6390
6391/*
6392 * Once the certificate message is read, parse it into a cert chain and
6393 * perform basic checks, but leave actual verification to the caller
6394 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006395MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006396static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6397 mbedtls_x509_crt *chain )
6398{
6399 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6400#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6401 int crt_cnt=0;
6402#endif
6403 size_t i, n;
6404 uint8_t alert;
6405
6406 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6407 {
6408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6409 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6410 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6411 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6412 }
6413
6414 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6415 {
6416 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6417 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6418 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6419 }
6420
6421 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6422 {
6423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6424 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6425 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6426 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6427 }
6428
6429 i = mbedtls_ssl_hs_hdr_len( ssl );
6430
6431 /*
6432 * Same message structure as in mbedtls_ssl_write_certificate()
6433 */
6434 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6435
6436 if( ssl->in_msg[i] != 0 ||
6437 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6438 {
6439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6440 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6441 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6442 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6443 }
6444
6445 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6446 i += 3;
6447
6448 /* Iterate through and parse the CRTs in the provided chain. */
6449 while( i < ssl->in_hslen )
6450 {
6451 /* Check that there's room for the next CRT's length fields. */
6452 if ( i + 3 > ssl->in_hslen ) {
6453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6454 mbedtls_ssl_send_alert_message( ssl,
6455 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6456 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6457 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6458 }
6459 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6460 * anything beyond 2**16 ~ 64K. */
6461 if( ssl->in_msg[i] != 0 )
6462 {
6463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6464 mbedtls_ssl_send_alert_message( ssl,
6465 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6466 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6467 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6468 }
6469
6470 /* Read length of the next CRT in the chain. */
6471 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6472 | (unsigned int) ssl->in_msg[i + 2];
6473 i += 3;
6474
6475 if( n < 128 || i + n > ssl->in_hslen )
6476 {
6477 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6478 mbedtls_ssl_send_alert_message( ssl,
6479 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6480 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6481 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6482 }
6483
6484 /* Check if we're handling the first CRT in the chain. */
6485#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6486 if( crt_cnt++ == 0 &&
6487 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6488 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6489 {
6490 /* During client-side renegotiation, check that the server's
6491 * end-CRTs hasn't changed compared to the initial handshake,
6492 * mitigating the triple handshake attack. On success, reuse
6493 * the original end-CRT instead of parsing it again. */
6494 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6495 if( ssl_check_peer_crt_unchanged( ssl,
6496 &ssl->in_msg[i],
6497 n ) != 0 )
6498 {
6499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6500 mbedtls_ssl_send_alert_message( ssl,
6501 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6502 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6503 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6504 }
6505
6506 /* Now we can safely free the original chain. */
6507 ssl_clear_peer_cert( ssl->session );
6508 }
6509#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6510
6511 /* Parse the next certificate in the chain. */
6512#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6513 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6514#else
6515 /* If we don't need to store the CRT chain permanently, parse
6516 * it in-place from the input buffer instead of making a copy. */
6517 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6518#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6519 switch( ret )
6520 {
6521 case 0: /*ok*/
6522 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6523 /* Ignore certificate with an unknown algorithm: maybe a
6524 prior certificate was already trusted. */
6525 break;
6526
6527 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6528 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6529 goto crt_parse_der_failed;
6530
6531 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6532 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6533 goto crt_parse_der_failed;
6534
6535 default:
6536 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6537 crt_parse_der_failed:
6538 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6539 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6540 return( ret );
6541 }
6542
6543 i += n;
6544 }
6545
6546 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6547 return( 0 );
6548}
6549
6550#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006551MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006552static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6553{
6554 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6555 return( -1 );
6556
6557 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6558 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6559 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6560 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6561 {
Ronald Cron19385882022-06-15 16:26:13 +02006562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006563 return( 0 );
6564 }
6565 return( -1 );
6566}
6567#endif /* MBEDTLS_SSL_SRV_C */
6568
6569/* Check if a certificate message is expected.
6570 * Return either
6571 * - SSL_CERTIFICATE_EXPECTED, or
6572 * - SSL_CERTIFICATE_SKIP
6573 * indicating whether a Certificate message is expected or not.
6574 */
6575#define SSL_CERTIFICATE_EXPECTED 0
6576#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006577MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006578static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6579 int authmode )
6580{
6581 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6582 ssl->handshake->ciphersuite_info;
6583
6584 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6585 return( SSL_CERTIFICATE_SKIP );
6586
6587#if defined(MBEDTLS_SSL_SRV_C)
6588 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6589 {
6590 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6591 return( SSL_CERTIFICATE_SKIP );
6592
6593 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6594 {
6595 ssl->session_negotiate->verify_result =
6596 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6597 return( SSL_CERTIFICATE_SKIP );
6598 }
6599 }
6600#else
6601 ((void) authmode);
6602#endif /* MBEDTLS_SSL_SRV_C */
6603
6604 return( SSL_CERTIFICATE_EXPECTED );
6605}
6606
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006607MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006608static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6609 int authmode,
6610 mbedtls_x509_crt *chain,
6611 void *rs_ctx )
6612{
6613 int ret = 0;
6614 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6615 ssl->handshake->ciphersuite_info;
6616 int have_ca_chain = 0;
6617
6618 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6619 void *p_vrfy;
6620
6621 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6622 return( 0 );
6623
6624 if( ssl->f_vrfy != NULL )
6625 {
6626 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6627 f_vrfy = ssl->f_vrfy;
6628 p_vrfy = ssl->p_vrfy;
6629 }
6630 else
6631 {
6632 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6633 f_vrfy = ssl->conf->f_vrfy;
6634 p_vrfy = ssl->conf->p_vrfy;
6635 }
6636
6637 /*
6638 * Main check: verify certificate
6639 */
6640#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6641 if( ssl->conf->f_ca_cb != NULL )
6642 {
6643 ((void) rs_ctx);
6644 have_ca_chain = 1;
6645
6646 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6647 ret = mbedtls_x509_crt_verify_with_ca_cb(
6648 chain,
6649 ssl->conf->f_ca_cb,
6650 ssl->conf->p_ca_cb,
6651 ssl->conf->cert_profile,
6652 ssl->hostname,
6653 &ssl->session_negotiate->verify_result,
6654 f_vrfy, p_vrfy );
6655 }
6656 else
6657#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6658 {
6659 mbedtls_x509_crt *ca_chain;
6660 mbedtls_x509_crl *ca_crl;
6661
6662#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6663 if( ssl->handshake->sni_ca_chain != NULL )
6664 {
6665 ca_chain = ssl->handshake->sni_ca_chain;
6666 ca_crl = ssl->handshake->sni_ca_crl;
6667 }
6668 else
6669#endif
6670 {
6671 ca_chain = ssl->conf->ca_chain;
6672 ca_crl = ssl->conf->ca_crl;
6673 }
6674
6675 if( ca_chain != NULL )
6676 have_ca_chain = 1;
6677
6678 ret = mbedtls_x509_crt_verify_restartable(
6679 chain,
6680 ca_chain, ca_crl,
6681 ssl->conf->cert_profile,
6682 ssl->hostname,
6683 &ssl->session_negotiate->verify_result,
6684 f_vrfy, p_vrfy, rs_ctx );
6685 }
6686
6687 if( ret != 0 )
6688 {
6689 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6690 }
6691
6692#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6693 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6694 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6695#endif
6696
6697 /*
6698 * Secondary checks: always done, but change 'ret' only if it was 0
6699 */
6700
6701#if defined(MBEDTLS_ECP_C)
6702 {
6703 const mbedtls_pk_context *pk = &chain->pk;
6704
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006705 /* If certificate uses an EC key, make sure the curve is OK.
6706 * This is a public key, so it can't be opaque, so can_do() is a good
6707 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006708 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006709 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006710 /* and in the unlikely case the above assumption no longer holds
6711 * we are making sure that pk_ec() here does not return a NULL
6712 */
6713 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6714 if( ec == NULL )
6715 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006717 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6718 }
Jerry Yud9526692022-02-17 14:23:47 +08006719
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006720 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6721 {
6722 ssl->session_negotiate->verify_result |=
6723 MBEDTLS_X509_BADCERT_BAD_KEY;
6724
6725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6726 if( ret == 0 )
6727 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6728 }
Jerry Yud9526692022-02-17 14:23:47 +08006729 }
6730 }
6731#endif /* MBEDTLS_ECP_C */
6732
6733 if( mbedtls_ssl_check_cert_usage( chain,
6734 ciphersuite_info,
6735 ! ssl->conf->endpoint,
6736 &ssl->session_negotiate->verify_result ) != 0 )
6737 {
6738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6739 if( ret == 0 )
6740 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6741 }
6742
6743 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6744 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6745 * with details encoded in the verification flags. All other kinds
6746 * of error codes, including those from the user provided f_vrfy
6747 * functions, are treated as fatal and lead to a failure of
6748 * ssl_parse_certificate even if verification was optional. */
6749 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6750 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6751 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6752 {
6753 ret = 0;
6754 }
6755
6756 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6757 {
6758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6759 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6760 }
6761
6762 if( ret != 0 )
6763 {
6764 uint8_t alert;
6765
6766 /* The certificate may have been rejected for several reasons.
6767 Pick one and send the corresponding alert. Which alert to send
6768 may be a subject of debate in some cases. */
6769 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6770 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6771 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6772 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6773 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6774 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6775 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6776 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6777 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6778 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6779 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6780 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6781 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6782 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6783 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6784 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6785 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6786 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6787 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6788 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6789 else
6790 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6791 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6792 alert );
6793 }
6794
6795#if defined(MBEDTLS_DEBUG_C)
6796 if( ssl->session_negotiate->verify_result != 0 )
6797 {
6798 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6799 (unsigned int) ssl->session_negotiate->verify_result ) );
6800 }
6801 else
6802 {
6803 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6804 }
6805#endif /* MBEDTLS_DEBUG_C */
6806
6807 return( ret );
6808}
6809
6810#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006811MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006812static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6813 unsigned char *start, size_t len )
6814{
6815 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6816 /* Remember digest of the peer's end-CRT. */
6817 ssl->session_negotiate->peer_cert_digest =
6818 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6819 if( ssl->session_negotiate->peer_cert_digest == NULL )
6820 {
6821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6822 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6823 mbedtls_ssl_send_alert_message( ssl,
6824 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6825 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6826
6827 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6828 }
6829
6830 ret = mbedtls_md( mbedtls_md_info_from_type(
6831 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6832 start, len,
6833 ssl->session_negotiate->peer_cert_digest );
6834
6835 ssl->session_negotiate->peer_cert_digest_type =
6836 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6837 ssl->session_negotiate->peer_cert_digest_len =
6838 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6839
6840 return( ret );
6841}
6842
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006843MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006844static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6845 unsigned char *start, size_t len )
6846{
6847 unsigned char *end = start + len;
6848 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6849
6850 /* Make a copy of the peer's raw public key. */
6851 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6852 ret = mbedtls_pk_parse_subpubkey( &start, end,
6853 &ssl->handshake->peer_pubkey );
6854 if( ret != 0 )
6855 {
6856 /* We should have parsed the public key before. */
6857 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6858 }
6859
6860 return( 0 );
6861}
6862#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6863
6864int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6865{
6866 int ret = 0;
6867 int crt_expected;
6868#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6869 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6870 ? ssl->handshake->sni_authmode
6871 : ssl->conf->authmode;
6872#else
6873 const int authmode = ssl->conf->authmode;
6874#endif
6875 void *rs_ctx = NULL;
6876 mbedtls_x509_crt *chain = NULL;
6877
6878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6879
6880 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6881 if( crt_expected == SSL_CERTIFICATE_SKIP )
6882 {
6883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6884 goto exit;
6885 }
6886
6887#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6888 if( ssl->handshake->ecrs_enabled &&
6889 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6890 {
6891 chain = ssl->handshake->ecrs_peer_cert;
6892 ssl->handshake->ecrs_peer_cert = NULL;
6893 goto crt_verify;
6894 }
6895#endif
6896
6897 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6898 {
6899 /* mbedtls_ssl_read_record may have sent an alert already. We
6900 let it decide whether to alert. */
6901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6902 goto exit;
6903 }
6904
6905#if defined(MBEDTLS_SSL_SRV_C)
6906 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6907 {
6908 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6909
6910 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6911 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6912
6913 goto exit;
6914 }
6915#endif /* MBEDTLS_SSL_SRV_C */
6916
6917 /* Clear existing peer CRT structure in case we tried to
6918 * reuse a session but it failed, and allocate a new one. */
6919 ssl_clear_peer_cert( ssl->session_negotiate );
6920
6921 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6922 if( chain == NULL )
6923 {
6924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6925 sizeof( mbedtls_x509_crt ) ) );
6926 mbedtls_ssl_send_alert_message( ssl,
6927 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6928 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6929
6930 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6931 goto exit;
6932 }
6933 mbedtls_x509_crt_init( chain );
6934
6935 ret = ssl_parse_certificate_chain( ssl, chain );
6936 if( ret != 0 )
6937 goto exit;
6938
6939#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6940 if( ssl->handshake->ecrs_enabled)
6941 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6942
6943crt_verify:
6944 if( ssl->handshake->ecrs_enabled)
6945 rs_ctx = &ssl->handshake->ecrs_ctx;
6946#endif
6947
6948 ret = ssl_parse_certificate_verify( ssl, authmode,
6949 chain, rs_ctx );
6950 if( ret != 0 )
6951 goto exit;
6952
6953#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6954 {
6955 unsigned char *crt_start, *pk_start;
6956 size_t crt_len, pk_len;
6957
6958 /* We parse the CRT chain without copying, so
6959 * these pointers point into the input buffer,
6960 * and are hence still valid after freeing the
6961 * CRT chain. */
6962
6963 crt_start = chain->raw.p;
6964 crt_len = chain->raw.len;
6965
6966 pk_start = chain->pk_raw.p;
6967 pk_len = chain->pk_raw.len;
6968
6969 /* Free the CRT structures before computing
6970 * digest and copying the peer's public key. */
6971 mbedtls_x509_crt_free( chain );
6972 mbedtls_free( chain );
6973 chain = NULL;
6974
6975 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6976 if( ret != 0 )
6977 goto exit;
6978
6979 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6980 if( ret != 0 )
6981 goto exit;
6982 }
6983#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6984 /* Pass ownership to session structure. */
6985 ssl->session_negotiate->peer_cert = chain;
6986 chain = NULL;
6987#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6988
6989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6990
6991exit:
6992
6993 if( ret == 0 )
6994 ssl->state++;
6995
6996#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6997 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6998 {
6999 ssl->handshake->ecrs_peer_cert = chain;
7000 chain = NULL;
7001 }
7002#endif
7003
7004 if( chain != NULL )
7005 {
7006 mbedtls_x509_crt_free( chain );
7007 mbedtls_free( chain );
7008 }
7009
7010 return( ret );
7011}
7012#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7013
Andrzej Kurek25f27152022-08-17 16:09:31 -04007014#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007015static void ssl_calc_finished_tls_sha256(
7016 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7017{
7018 int len = 12;
7019 const char *sender;
7020 unsigned char padbuf[32];
7021#if defined(MBEDTLS_USE_PSA_CRYPTO)
7022 size_t hash_size;
7023 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7024 psa_status_t status;
7025#else
7026 mbedtls_sha256_context sha256;
7027#endif
7028
7029 mbedtls_ssl_session *session = ssl->session_negotiate;
7030 if( !session )
7031 session = ssl->session;
7032
7033 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7034 ? "client finished"
7035 : "server finished";
7036
7037#if defined(MBEDTLS_USE_PSA_CRYPTO)
7038 sha256_psa = psa_hash_operation_init();
7039
7040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7041
7042 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7043 if( status != PSA_SUCCESS )
7044 {
7045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7046 return;
7047 }
7048
7049 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7050 if( status != PSA_SUCCESS )
7051 {
7052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7053 return;
7054 }
7055 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7056#else
7057
7058 mbedtls_sha256_init( &sha256 );
7059
7060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7061
7062 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7063
7064 /*
7065 * TLSv1.2:
7066 * hash = PRF( master, finished_label,
7067 * Hash( handshake ) )[0.11]
7068 */
7069
7070#if !defined(MBEDTLS_SHA256_ALT)
7071 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7072 sha256.state, sizeof( sha256.state ) );
7073#endif
7074
7075 mbedtls_sha256_finish( &sha256, padbuf );
7076 mbedtls_sha256_free( &sha256 );
7077#endif /* MBEDTLS_USE_PSA_CRYPTO */
7078
7079 ssl->handshake->tls_prf( session->master, 48, sender,
7080 padbuf, 32, buf, len );
7081
7082 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7083
7084 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7085
7086 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7087}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007088#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007089
Jerry Yub7ba49e2022-02-17 14:25:53 +08007090
Andrzej Kurek25f27152022-08-17 16:09:31 -04007091#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007092static void ssl_calc_finished_tls_sha384(
7093 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7094{
7095 int len = 12;
7096 const char *sender;
7097 unsigned char padbuf[48];
7098#if defined(MBEDTLS_USE_PSA_CRYPTO)
7099 size_t hash_size;
7100 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7101 psa_status_t status;
7102#else
7103 mbedtls_sha512_context sha512;
7104#endif
7105
7106 mbedtls_ssl_session *session = ssl->session_negotiate;
7107 if( !session )
7108 session = ssl->session;
7109
7110 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7111 ? "client finished"
7112 : "server finished";
7113
7114#if defined(MBEDTLS_USE_PSA_CRYPTO)
7115 sha384_psa = psa_hash_operation_init();
7116
7117 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7118
7119 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7120 if( status != PSA_SUCCESS )
7121 {
7122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7123 return;
7124 }
7125
7126 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7127 if( status != PSA_SUCCESS )
7128 {
7129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7130 return;
7131 }
7132 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7133#else
7134 mbedtls_sha512_init( &sha512 );
7135
7136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7137
Andrzej Kureka242e832022-08-11 10:03:14 -04007138 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007139
7140 /*
7141 * TLSv1.2:
7142 * hash = PRF( master, finished_label,
7143 * Hash( handshake ) )[0.11]
7144 */
7145
7146#if !defined(MBEDTLS_SHA512_ALT)
7147 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7148 sha512.state, sizeof( sha512.state ) );
7149#endif
7150 mbedtls_sha512_finish( &sha512, padbuf );
7151
7152 mbedtls_sha512_free( &sha512 );
7153#endif
7154
7155 ssl->handshake->tls_prf( session->master, 48, sender,
7156 padbuf, 48, buf, len );
7157
7158 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7159
7160 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7161
7162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7163}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007164#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007165
Jerry Yuaef00152022-02-17 14:27:31 +08007166void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7167{
7168 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7169
7170 /*
7171 * Free our handshake params
7172 */
7173 mbedtls_ssl_handshake_free( ssl );
7174 mbedtls_free( ssl->handshake );
7175 ssl->handshake = NULL;
7176
7177 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007178 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007179 */
7180 if( ssl->transform )
7181 {
7182 mbedtls_ssl_transform_free( ssl->transform );
7183 mbedtls_free( ssl->transform );
7184 }
7185 ssl->transform = ssl->transform_negotiate;
7186 ssl->transform_negotiate = NULL;
7187
7188 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7189}
7190
Jerry Yu2a9fff52022-02-17 14:28:51 +08007191void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7192{
7193 int resume = ssl->handshake->resume;
7194
7195 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7196
7197#if defined(MBEDTLS_SSL_RENEGOTIATION)
7198 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7199 {
7200 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7201 ssl->renego_records_seen = 0;
7202 }
7203#endif
7204
7205 /*
7206 * Free the previous session and switch in the current one
7207 */
7208 if( ssl->session )
7209 {
7210#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7211 /* RFC 7366 3.1: keep the EtM state */
7212 ssl->session_negotiate->encrypt_then_mac =
7213 ssl->session->encrypt_then_mac;
7214#endif
7215
7216 mbedtls_ssl_session_free( ssl->session );
7217 mbedtls_free( ssl->session );
7218 }
7219 ssl->session = ssl->session_negotiate;
7220 ssl->session_negotiate = NULL;
7221
7222 /*
7223 * Add cache entry
7224 */
7225 if( ssl->conf->f_set_cache != NULL &&
7226 ssl->session->id_len != 0 &&
7227 resume == 0 )
7228 {
7229 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7230 ssl->session->id,
7231 ssl->session->id_len,
7232 ssl->session ) != 0 )
7233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7234 }
7235
7236#if defined(MBEDTLS_SSL_PROTO_DTLS)
7237 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7238 ssl->handshake->flight != NULL )
7239 {
7240 /* Cancel handshake timer */
7241 mbedtls_ssl_set_timer( ssl, 0 );
7242
7243 /* Keep last flight around in case we need to resend it:
7244 * we need the handshake and transform structures for that */
7245 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7246 }
7247 else
7248#endif
7249 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7250
7251 ssl->state++;
7252
7253 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7254}
7255
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007256int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7257{
7258 int ret, hash_len;
7259
7260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7261
7262 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7263
7264 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7265
7266 /*
7267 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7268 * may define some other value. Currently (early 2016), no defined
7269 * ciphersuite does this (and this is unlikely to change as activity has
7270 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7271 */
7272 hash_len = 12;
7273
7274#if defined(MBEDTLS_SSL_RENEGOTIATION)
7275 ssl->verify_data_len = hash_len;
7276 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7277#endif
7278
7279 ssl->out_msglen = 4 + hash_len;
7280 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7281 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7282
7283 /*
7284 * In case of session resuming, invert the client and server
7285 * ChangeCipherSpec messages order.
7286 */
7287 if( ssl->handshake->resume != 0 )
7288 {
7289#if defined(MBEDTLS_SSL_CLI_C)
7290 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7291 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7292#endif
7293#if defined(MBEDTLS_SSL_SRV_C)
7294 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7295 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7296#endif
7297 }
7298 else
7299 ssl->state++;
7300
7301 /*
7302 * Switch to our negotiated transform and session parameters for outbound
7303 * data.
7304 */
7305 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7306
7307#if defined(MBEDTLS_SSL_PROTO_DTLS)
7308 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7309 {
7310 unsigned char i;
7311
7312 /* Remember current epoch settings for resending */
7313 ssl->handshake->alt_transform_out = ssl->transform_out;
7314 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7315 sizeof( ssl->handshake->alt_out_ctr ) );
7316
7317 /* Set sequence_number to zero */
7318 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7319
7320
7321 /* Increment epoch */
7322 for( i = 2; i > 0; i-- )
7323 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7324 break;
7325
7326 /* The loop goes to its end iff the counter is wrapping */
7327 if( i == 0 )
7328 {
7329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7330 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7331 }
7332 }
7333 else
7334#endif /* MBEDTLS_SSL_PROTO_DTLS */
7335 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7336
7337 ssl->transform_out = ssl->transform_negotiate;
7338 ssl->session_out = ssl->session_negotiate;
7339
7340#if defined(MBEDTLS_SSL_PROTO_DTLS)
7341 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7342 mbedtls_ssl_send_flight_completed( ssl );
7343#endif
7344
7345 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7346 {
7347 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7348 return( ret );
7349 }
7350
7351#if defined(MBEDTLS_SSL_PROTO_DTLS)
7352 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7353 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7354 {
7355 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7356 return( ret );
7357 }
7358#endif
7359
7360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7361
7362 return( 0 );
7363}
7364
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007365#define SSL_MAX_HASH_LEN 12
7366
7367int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7368{
7369 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7370 unsigned int hash_len = 12;
7371 unsigned char buf[SSL_MAX_HASH_LEN];
7372
7373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7374
7375 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7376
7377 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7378 {
7379 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7380 goto exit;
7381 }
7382
7383 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7384 {
7385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7386 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7387 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7388 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7389 goto exit;
7390 }
7391
7392 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7393 {
7394 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7395 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7396 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7397 goto exit;
7398 }
7399
7400 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7401 {
7402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7403 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7404 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7405 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7406 goto exit;
7407 }
7408
7409 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7410 buf, hash_len ) != 0 )
7411 {
7412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7413 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7414 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7415 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7416 goto exit;
7417 }
7418
7419#if defined(MBEDTLS_SSL_RENEGOTIATION)
7420 ssl->verify_data_len = hash_len;
7421 memcpy( ssl->peer_verify_data, buf, hash_len );
7422#endif
7423
7424 if( ssl->handshake->resume != 0 )
7425 {
7426#if defined(MBEDTLS_SSL_CLI_C)
7427 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7428 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7429#endif
7430#if defined(MBEDTLS_SSL_SRV_C)
7431 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7432 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7433#endif
7434 }
7435 else
7436 ssl->state++;
7437
7438#if defined(MBEDTLS_SSL_PROTO_DTLS)
7439 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7440 mbedtls_ssl_recv_flight_completed( ssl );
7441#endif
7442
7443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7444
7445exit:
7446 mbedtls_platform_zeroize( buf, hash_len );
7447 return( ret );
7448}
7449
Jerry Yu392112c2022-02-17 14:34:10 +08007450#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7451/*
7452 * Helper to get TLS 1.2 PRF from ciphersuite
7453 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7454 */
7455static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7456{
Jerry Yu392112c2022-02-17 14:34:10 +08007457 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Andrzej Kurek68327742022-10-03 06:18:18 -04007458 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7459#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007460 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007461 return( tls_prf_sha384 );
Andrzej Kurek894edde2022-09-29 06:31:14 -04007462 else
Jerry Yu392112c2022-02-17 14:34:10 +08007463#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007464#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7465 {
7466 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
7467 return( tls_prf_sha256 );
7468 }
7469#endif
7470#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7471 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7472 (void) ciphersuite_info;
7473#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007474
Andrzej Kurek894edde2022-09-29 06:31:14 -04007475 return( NULL );
Jerry Yu392112c2022-02-17 14:34:10 +08007476}
7477#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007478
7479static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7480{
7481 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007482#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007483 if( tls_prf == tls_prf_sha384 )
7484 {
7485 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7486 }
7487 else
7488#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007489#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007490 if( tls_prf == tls_prf_sha256 )
7491 {
7492 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7493 }
7494 else
7495#endif
7496 return( MBEDTLS_SSL_TLS_PRF_NONE );
7497}
7498
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007499/*
7500 * Populate a transform structure with session keys and all the other
7501 * necessary information.
7502 *
7503 * Parameters:
7504 * - [in/out]: transform: structure to populate
7505 * [in] must be just initialised with mbedtls_ssl_transform_init()
7506 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7507 * - [in] ciphersuite
7508 * - [in] master
7509 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007510 * - [in] tls_prf: pointer to PRF to use for key derivation
7511 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007512 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007513 * - [in] endpoint: client or server
7514 * - [in] ssl: used for:
7515 * - ssl->conf->{f,p}_export_keys
7516 * [in] optionally used for:
7517 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7518 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007519MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007520static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7521 int ciphersuite,
7522 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007523#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007524 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007525#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007526 ssl_tls_prf_t tls_prf,
7527 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007528 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007529 unsigned endpoint,
7530 const mbedtls_ssl_context *ssl )
7531{
7532 int ret = 0;
7533 unsigned char keyblk[256];
7534 unsigned char *key1;
7535 unsigned char *key2;
7536 unsigned char *mac_enc;
7537 unsigned char *mac_dec;
7538 size_t mac_key_len = 0;
7539 size_t iv_copy_len;
7540 size_t keylen;
7541 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007542 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007543#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007544 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007545 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007546#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007547
7548#if defined(MBEDTLS_USE_PSA_CRYPTO)
7549 psa_key_type_t key_type;
7550 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7551 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007552 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007553 size_t key_bits;
7554 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7555#endif
7556
7557#if !defined(MBEDTLS_DEBUG_C) && \
7558 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7559 if( ssl->f_export_keys == NULL )
7560 {
7561 ssl = NULL; /* make sure we don't use it except for these cases */
7562 (void) ssl;
7563 }
7564#endif
7565
7566 /*
7567 * Some data just needs copying into the structure
7568 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007569#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007570 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007571#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007572 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007573
7574#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7575 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7576#endif
7577
7578#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007579 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007580 {
7581 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7582 * generation separate. This should never happen. */
7583 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7584 }
7585#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7586
7587 /*
7588 * Get various info structures
7589 */
7590 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7591 if( ciphersuite_info == NULL )
7592 {
7593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7594 ciphersuite ) );
7595 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7596 }
7597
Neil Armstrongab555e02022-04-04 11:07:59 +02007598 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007599#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007600 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007601#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007602 ciphersuite_info );
7603
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007604 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7605 transform->taglen =
7606 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7607
7608#if defined(MBEDTLS_USE_PSA_CRYPTO)
7609 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7610 transform->taglen,
7611 &alg,
7612 &key_type,
7613 &key_bits ) ) != PSA_SUCCESS )
7614 {
7615 ret = psa_ssl_status_to_mbedtls( status );
7616 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7617 goto end;
7618 }
7619#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007620 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7621 if( cipher_info == NULL )
7622 {
7623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7624 ciphersuite_info->cipher ) );
7625 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7626 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007627#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007628
Neil Armstronge4512952022-03-08 09:08:22 +01007629#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007630 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007631 if( mac_alg == 0 )
7632 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007634 (unsigned) ciphersuite_info->mac ) );
7635 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7636 }
7637#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007638 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7639 if( md_info == NULL )
7640 {
7641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7642 (unsigned) ciphersuite_info->mac ) );
7643 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7644 }
Neil Armstronge4512952022-03-08 09:08:22 +01007645#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007646
7647#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7648 /* Copy own and peer's CID if the use of the CID
7649 * extension has been negotiated. */
7650 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7651 {
7652 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7653
7654 transform->in_cid_len = ssl->own_cid_len;
7655 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7656 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7657 transform->in_cid_len );
7658
7659 transform->out_cid_len = ssl->handshake->peer_cid_len;
7660 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7661 ssl->handshake->peer_cid_len );
7662 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7663 transform->out_cid_len );
7664 }
7665#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7666
7667 /*
7668 * Compute key block using the PRF
7669 */
7670 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7671 if( ret != 0 )
7672 {
7673 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7674 return( ret );
7675 }
7676
7677 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7678 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7679 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7680 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7681 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7682
7683 /*
7684 * Determine the appropriate key, IV and MAC length.
7685 */
7686
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007687#if defined(MBEDTLS_USE_PSA_CRYPTO)
7688 keylen = PSA_BITS_TO_BYTES(key_bits);
7689#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007690 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007691#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007692
7693#if defined(MBEDTLS_GCM_C) || \
7694 defined(MBEDTLS_CCM_C) || \
7695 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007696 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007697 {
7698 size_t explicit_ivlen;
7699
7700 transform->maclen = 0;
7701 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007702
7703 /* All modes haves 96-bit IVs, but the length of the static parts vary
7704 * with mode and version:
7705 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7706 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7707 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7708 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7709 * sequence number).
7710 */
7711 transform->ivlen = 12;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007712#if defined(MBEDTLS_USE_PSA_CRYPTO)
7713 if( key_type == PSA_KEY_TYPE_CHACHA20 )
7714#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007715 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007716#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007717 transform->fixed_ivlen = 12;
7718 else
7719 transform->fixed_ivlen = 4;
7720
7721 /* Minimum length of encrypted record */
7722 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7723 transform->minlen = explicit_ivlen + transform->taglen;
7724 }
7725 else
7726#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7727#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007728 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7729 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7730 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007731 {
Neil Armstronge4512952022-03-08 09:08:22 +01007732#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007733 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007734#else
7735 size_t block_size = cipher_info->block_size;
7736#endif /* MBEDTLS_USE_PSA_CRYPTO */
7737
7738#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007739 /* Get MAC length */
7740 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7741#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007742 /* Initialize HMAC contexts */
7743 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7744 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7745 {
7746 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7747 goto end;
7748 }
7749
7750 /* Get MAC length */
7751 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007752#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007753 transform->maclen = mac_key_len;
7754
7755 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007756#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007757 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007758#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007759 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007760#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007761
7762 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007763 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007764 transform->minlen = transform->maclen;
7765 else
7766 {
7767 /*
7768 * GenericBlockCipher:
7769 * 1. if EtM is in use: one block plus MAC
7770 * otherwise: * first multiple of blocklen greater than maclen
7771 * 2. IV
7772 */
7773#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007774 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007775 {
7776 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007777 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007778 }
7779 else
7780#endif
7781 {
7782 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007783 + block_size
7784 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007785 }
7786
Glenn Strauss07c64162022-03-14 12:34:51 -04007787 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007788 {
7789 transform->minlen += transform->ivlen;
7790 }
7791 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007792 {
7793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7794 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7795 goto end;
7796 }
7797 }
7798 }
7799 else
7800#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7801 {
7802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7803 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7804 }
7805
7806 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7807 (unsigned) keylen,
7808 (unsigned) transform->minlen,
7809 (unsigned) transform->ivlen,
7810 (unsigned) transform->maclen ) );
7811
7812 /*
7813 * Finally setup the cipher contexts, IVs and MAC secrets.
7814 */
7815#if defined(MBEDTLS_SSL_CLI_C)
7816 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7817 {
7818 key1 = keyblk + mac_key_len * 2;
7819 key2 = keyblk + mac_key_len * 2 + keylen;
7820
7821 mac_enc = keyblk;
7822 mac_dec = keyblk + mac_key_len;
7823
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007824 iv_copy_len = ( transform->fixed_ivlen ) ?
7825 transform->fixed_ivlen : transform->ivlen;
7826 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7827 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7828 iv_copy_len );
7829 }
7830 else
7831#endif /* MBEDTLS_SSL_CLI_C */
7832#if defined(MBEDTLS_SSL_SRV_C)
7833 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7834 {
7835 key1 = keyblk + mac_key_len * 2 + keylen;
7836 key2 = keyblk + mac_key_len * 2;
7837
7838 mac_enc = keyblk + mac_key_len;
7839 mac_dec = keyblk;
7840
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007841 iv_copy_len = ( transform->fixed_ivlen ) ?
7842 transform->fixed_ivlen : transform->ivlen;
7843 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7844 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7845 iv_copy_len );
7846 }
7847 else
7848#endif /* MBEDTLS_SSL_SRV_C */
7849 {
7850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7851 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7852 goto end;
7853 }
7854
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007855 if( ssl != NULL && ssl->f_export_keys != NULL )
7856 {
7857 ssl->f_export_keys( ssl->p_export_keys,
7858 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7859 master, 48,
7860 randbytes + 32,
7861 randbytes,
7862 tls_prf_get_type( tls_prf ) );
7863 }
7864
7865#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007866 transform->psa_alg = alg;
7867
7868 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7869 {
7870 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7871 psa_set_key_algorithm( &attributes, alg );
7872 psa_set_key_type( &attributes, key_type );
7873
7874 if( ( status = psa_import_key( &attributes,
7875 key1,
7876 PSA_BITS_TO_BYTES( key_bits ),
7877 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7878 {
7879 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7880 ret = psa_ssl_status_to_mbedtls( status );
7881 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7882 goto end;
7883 }
7884
7885 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7886
7887 if( ( status = psa_import_key( &attributes,
7888 key2,
7889 PSA_BITS_TO_BYTES( key_bits ),
7890 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7891 {
7892 ret = psa_ssl_status_to_mbedtls( status );
7893 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7894 goto end;
7895 }
7896 }
7897#else
7898 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7899 cipher_info ) ) != 0 )
7900 {
7901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7902 goto end;
7903 }
7904
7905 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7906 cipher_info ) ) != 0 )
7907 {
7908 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7909 goto end;
7910 }
7911
7912 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7913 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7914 MBEDTLS_ENCRYPT ) ) != 0 )
7915 {
7916 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7917 goto end;
7918 }
7919
7920 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7921 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7922 MBEDTLS_DECRYPT ) ) != 0 )
7923 {
7924 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7925 goto end;
7926 }
7927
7928#if defined(MBEDTLS_CIPHER_MODE_CBC)
7929 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7930 {
7931 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7932 MBEDTLS_PADDING_NONE ) ) != 0 )
7933 {
7934 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7935 goto end;
7936 }
7937
7938 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7939 MBEDTLS_PADDING_NONE ) ) != 0 )
7940 {
7941 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7942 goto end;
7943 }
7944 }
7945#endif /* MBEDTLS_CIPHER_MODE_CBC */
7946#endif /* MBEDTLS_USE_PSA_CRYPTO */
7947
Neil Armstrong29c0c042022-03-17 17:47:28 +01007948#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7949 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7950 For AEAD-based ciphersuites, there is nothing to do here. */
7951 if( mac_key_len != 0 )
7952 {
7953#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007954 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007955
7956 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007957 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007958 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7959
7960 if( ( status = psa_import_key( &attributes,
7961 mac_enc, mac_key_len,
7962 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7963 {
7964 ret = psa_ssl_status_to_mbedtls( status );
7965 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7966 goto end;
7967 }
7968
Ronald Cronfb39f152022-03-25 14:36:28 +01007969 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04007970 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
7971#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
7972 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
7973#endif
7974 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007975 /* mbedtls_ct_hmac() requires the key to be exportable */
7976 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7977 PSA_KEY_USAGE_VERIFY_HASH );
7978 else
7979 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7980
7981 if( ( status = psa_import_key( &attributes,
7982 mac_dec, mac_key_len,
7983 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7984 {
7985 ret = psa_ssl_status_to_mbedtls( status );
7986 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7987 goto end;
7988 }
7989#else
7990 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7991 if( ret != 0 )
7992 goto end;
7993 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7994 if( ret != 0 )
7995 goto end;
7996#endif /* MBEDTLS_USE_PSA_CRYPTO */
7997 }
7998#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7999
8000 ((void) mac_dec);
8001 ((void) mac_enc);
8002
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008003end:
8004 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
8005 return( ret );
8006}
8007
Jerry Yuee40f9d2022-02-17 14:55:16 +08008008#if defined(MBEDTLS_USE_PSA_CRYPTO)
8009int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8010 unsigned char *hash, size_t *hashlen,
8011 unsigned char *data, size_t data_len,
8012 mbedtls_md_type_t md_alg )
8013{
8014 psa_status_t status;
8015 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008016 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008017
8018 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8019
8020 if( ( status = psa_hash_setup( &hash_operation,
8021 hash_alg ) ) != PSA_SUCCESS )
8022 {
8023 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8024 goto exit;
8025 }
8026
8027 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8028 64 ) ) != PSA_SUCCESS )
8029 {
8030 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8031 goto exit;
8032 }
8033
8034 if( ( status = psa_hash_update( &hash_operation,
8035 data, data_len ) ) != PSA_SUCCESS )
8036 {
8037 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8038 goto exit;
8039 }
8040
8041 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8042 hashlen ) ) != PSA_SUCCESS )
8043 {
8044 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8045 goto exit;
8046 }
8047
8048exit:
8049 if( status != PSA_SUCCESS )
8050 {
8051 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8052 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8053 switch( status )
8054 {
8055 case PSA_ERROR_NOT_SUPPORTED:
8056 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8057 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8058 case PSA_ERROR_BUFFER_TOO_SMALL:
8059 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8060 case PSA_ERROR_INSUFFICIENT_MEMORY:
8061 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8062 default:
8063 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8064 }
8065 }
8066 return( 0 );
8067}
8068
8069#else
8070
8071int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8072 unsigned char *hash, size_t *hashlen,
8073 unsigned char *data, size_t data_len,
8074 mbedtls_md_type_t md_alg )
8075{
8076 int ret = 0;
8077 mbedtls_md_context_t ctx;
8078 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8079 *hashlen = mbedtls_md_get_size( md_info );
8080
8081 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8082
8083 mbedtls_md_init( &ctx );
8084
8085 /*
8086 * digitally-signed struct {
8087 * opaque client_random[32];
8088 * opaque server_random[32];
8089 * ServerDHParams params;
8090 * };
8091 */
8092 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8093 {
8094 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8095 goto exit;
8096 }
8097 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8098 {
8099 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8100 goto exit;
8101 }
8102 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8103 {
8104 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8105 goto exit;
8106 }
8107 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8108 {
8109 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8110 goto exit;
8111 }
8112 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8113 {
8114 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8115 goto exit;
8116 }
8117
8118exit:
8119 mbedtls_md_free( &ctx );
8120
8121 if( ret != 0 )
8122 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8123 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8124
8125 return( ret );
8126}
8127#endif /* MBEDTLS_USE_PSA_CRYPTO */
8128
Jerry Yud9d91da2022-02-17 14:57:06 +08008129#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8130
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008131/* Find the preferred hash for a given signature algorithm. */
8132unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8133 mbedtls_ssl_context *ssl,
8134 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008135{
Gabor Mezei078e8032022-04-27 21:17:56 +02008136 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008137 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008138
8139 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008140 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008141
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008142 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008143 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008144 unsigned int hash_alg_received =
8145 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8146 received_sig_algs[i] );
8147 unsigned int sig_alg_received =
8148 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8149 received_sig_algs[i] );
8150
8151 if( sig_alg == sig_alg_received )
8152 {
8153#if defined(MBEDTLS_USE_PSA_CRYPTO)
8154 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8155 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008156 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008157 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008158
Neil Armstrong96eceb82022-06-30 18:05:05 +02008159 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8160 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8161 PSA_ALG_ECDSA( psa_hash_alg ),
8162 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008163 continue;
8164
Neil Armstrong96eceb82022-06-30 18:05:05 +02008165 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008166 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8167 PSA_ALG_RSA_PKCS1V15_SIGN(
8168 psa_hash_alg ),
8169 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008170 continue;
8171 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008172#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008173
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008174 return( hash_alg_received );
8175 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008176 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008177
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008178 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008179}
8180
8181#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8182
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008183/* Serialization of TLS 1.2 sessions:
8184 *
8185 * struct {
8186 * uint64 start_time;
8187 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008188 * uint8 session_id_len; // at most 32
8189 * opaque session_id[32];
8190 * opaque master[48]; // fixed length in the standard
8191 * uint32 verify_result;
8192 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8193 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8194 * uint32 ticket_lifetime;
8195 * uint8 mfl_code; // up to 255 according to standard
8196 * uint8 encrypt_then_mac; // 0 or 1
8197 * } serialized_session_tls12;
8198 *
8199 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008200static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008201 unsigned char *buf,
8202 size_t buf_len )
8203{
8204 unsigned char *p = buf;
8205 size_t used = 0;
8206
8207#if defined(MBEDTLS_HAVE_TIME)
8208 uint64_t start;
8209#endif
8210#if defined(MBEDTLS_X509_CRT_PARSE_C)
8211#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8212 size_t cert_len;
8213#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8214#endif /* MBEDTLS_X509_CRT_PARSE_C */
8215
8216 /*
8217 * Time
8218 */
8219#if defined(MBEDTLS_HAVE_TIME)
8220 used += 8;
8221
8222 if( used <= buf_len )
8223 {
8224 start = (uint64_t) session->start;
8225
8226 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8227 p += 8;
8228 }
8229#endif /* MBEDTLS_HAVE_TIME */
8230
8231 /*
8232 * Basic mandatory fields
8233 */
8234 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008235 + 1 /* id_len */
8236 + sizeof( session->id )
8237 + sizeof( session->master )
8238 + 4; /* verify_result */
8239
8240 if( used <= buf_len )
8241 {
8242 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8243 p += 2;
8244
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008245 *p++ = MBEDTLS_BYTE_0( session->id_len );
8246 memcpy( p, session->id, 32 );
8247 p += 32;
8248
8249 memcpy( p, session->master, 48 );
8250 p += 48;
8251
8252 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8253 p += 4;
8254 }
8255
8256 /*
8257 * Peer's end-entity certificate
8258 */
8259#if defined(MBEDTLS_X509_CRT_PARSE_C)
8260#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8261 if( session->peer_cert == NULL )
8262 cert_len = 0;
8263 else
8264 cert_len = session->peer_cert->raw.len;
8265
8266 used += 3 + cert_len;
8267
8268 if( used <= buf_len )
8269 {
8270 *p++ = MBEDTLS_BYTE_2( cert_len );
8271 *p++ = MBEDTLS_BYTE_1( cert_len );
8272 *p++ = MBEDTLS_BYTE_0( cert_len );
8273
8274 if( session->peer_cert != NULL )
8275 {
8276 memcpy( p, session->peer_cert->raw.p, cert_len );
8277 p += cert_len;
8278 }
8279 }
8280#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8281 if( session->peer_cert_digest != NULL )
8282 {
8283 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8284 if( used <= buf_len )
8285 {
8286 *p++ = (unsigned char) session->peer_cert_digest_type;
8287 *p++ = (unsigned char) session->peer_cert_digest_len;
8288 memcpy( p, session->peer_cert_digest,
8289 session->peer_cert_digest_len );
8290 p += session->peer_cert_digest_len;
8291 }
8292 }
8293 else
8294 {
8295 used += 2;
8296 if( used <= buf_len )
8297 {
8298 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8299 *p++ = 0;
8300 }
8301 }
8302#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8303#endif /* MBEDTLS_X509_CRT_PARSE_C */
8304
8305 /*
8306 * Session ticket if any, plus associated data
8307 */
8308#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8309 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8310
8311 if( used <= buf_len )
8312 {
8313 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8314 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8315 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8316
8317 if( session->ticket != NULL )
8318 {
8319 memcpy( p, session->ticket, session->ticket_len );
8320 p += session->ticket_len;
8321 }
8322
8323 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8324 p += 4;
8325 }
8326#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8327
8328 /*
8329 * Misc extension-related info
8330 */
8331#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8332 used += 1;
8333
8334 if( used <= buf_len )
8335 *p++ = session->mfl_code;
8336#endif
8337
8338#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8339 used += 1;
8340
8341 if( used <= buf_len )
8342 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8343#endif
8344
8345 return( used );
8346}
8347
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008348MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008349static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008350 const unsigned char *buf,
8351 size_t len )
8352{
8353#if defined(MBEDTLS_HAVE_TIME)
8354 uint64_t start;
8355#endif
8356#if defined(MBEDTLS_X509_CRT_PARSE_C)
8357#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8358 size_t cert_len;
8359#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8360#endif /* MBEDTLS_X509_CRT_PARSE_C */
8361
8362 const unsigned char *p = buf;
8363 const unsigned char * const end = buf + len;
8364
8365 /*
8366 * Time
8367 */
8368#if defined(MBEDTLS_HAVE_TIME)
8369 if( 8 > (size_t)( end - p ) )
8370 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8371
8372 start = ( (uint64_t) p[0] << 56 ) |
8373 ( (uint64_t) p[1] << 48 ) |
8374 ( (uint64_t) p[2] << 40 ) |
8375 ( (uint64_t) p[3] << 32 ) |
8376 ( (uint64_t) p[4] << 24 ) |
8377 ( (uint64_t) p[5] << 16 ) |
8378 ( (uint64_t) p[6] << 8 ) |
8379 ( (uint64_t) p[7] );
8380 p += 8;
8381
8382 session->start = (time_t) start;
8383#endif /* MBEDTLS_HAVE_TIME */
8384
8385 /*
8386 * Basic mandatory fields
8387 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008388 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008389 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8390
8391 session->ciphersuite = ( p[0] << 8 ) | p[1];
8392 p += 2;
8393
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008394 session->id_len = *p++;
8395 memcpy( session->id, p, 32 );
8396 p += 32;
8397
8398 memcpy( session->master, p, 48 );
8399 p += 48;
8400
8401 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8402 ( (uint32_t) p[1] << 16 ) |
8403 ( (uint32_t) p[2] << 8 ) |
8404 ( (uint32_t) p[3] );
8405 p += 4;
8406
8407 /* Immediately clear invalid pointer values that have been read, in case
8408 * we exit early before we replaced them with valid ones. */
8409#if defined(MBEDTLS_X509_CRT_PARSE_C)
8410#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8411 session->peer_cert = NULL;
8412#else
8413 session->peer_cert_digest = NULL;
8414#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8415#endif /* MBEDTLS_X509_CRT_PARSE_C */
8416#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8417 session->ticket = NULL;
8418#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8419
8420 /*
8421 * Peer certificate
8422 */
8423#if defined(MBEDTLS_X509_CRT_PARSE_C)
8424#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8425 /* Deserialize CRT from the end of the ticket. */
8426 if( 3 > (size_t)( end - p ) )
8427 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8428
8429 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8430 p += 3;
8431
8432 if( cert_len != 0 )
8433 {
8434 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8435
8436 if( cert_len > (size_t)( end - p ) )
8437 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8438
8439 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8440
8441 if( session->peer_cert == NULL )
8442 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8443
8444 mbedtls_x509_crt_init( session->peer_cert );
8445
8446 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8447 p, cert_len ) ) != 0 )
8448 {
8449 mbedtls_x509_crt_free( session->peer_cert );
8450 mbedtls_free( session->peer_cert );
8451 session->peer_cert = NULL;
8452 return( ret );
8453 }
8454
8455 p += cert_len;
8456 }
8457#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8458 /* Deserialize CRT digest from the end of the ticket. */
8459 if( 2 > (size_t)( end - p ) )
8460 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8461
8462 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8463 session->peer_cert_digest_len = (size_t) *p++;
8464
8465 if( session->peer_cert_digest_len != 0 )
8466 {
8467 const mbedtls_md_info_t *md_info =
8468 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8469 if( md_info == NULL )
8470 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8471 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8472 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8473
8474 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8475 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8476
8477 session->peer_cert_digest =
8478 mbedtls_calloc( 1, session->peer_cert_digest_len );
8479 if( session->peer_cert_digest == NULL )
8480 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8481
8482 memcpy( session->peer_cert_digest, p,
8483 session->peer_cert_digest_len );
8484 p += session->peer_cert_digest_len;
8485 }
8486#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8487#endif /* MBEDTLS_X509_CRT_PARSE_C */
8488
8489 /*
8490 * Session ticket and associated data
8491 */
8492#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8493 if( 3 > (size_t)( end - p ) )
8494 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8495
8496 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8497 p += 3;
8498
8499 if( session->ticket_len != 0 )
8500 {
8501 if( session->ticket_len > (size_t)( end - p ) )
8502 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8503
8504 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8505 if( session->ticket == NULL )
8506 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8507
8508 memcpy( session->ticket, p, session->ticket_len );
8509 p += session->ticket_len;
8510 }
8511
8512 if( 4 > (size_t)( end - p ) )
8513 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8514
8515 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8516 ( (uint32_t) p[1] << 16 ) |
8517 ( (uint32_t) p[2] << 8 ) |
8518 ( (uint32_t) p[3] );
8519 p += 4;
8520#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8521
8522 /*
8523 * Misc extension-related info
8524 */
8525#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8526 if( 1 > (size_t)( end - p ) )
8527 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8528
8529 session->mfl_code = *p++;
8530#endif
8531
8532#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8533 if( 1 > (size_t)( end - p ) )
8534 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8535
8536 session->encrypt_then_mac = *p++;
8537#endif
8538
8539 /* Done, should have consumed entire buffer */
8540 if( p != end )
8541 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8542
8543 return( 0 );
8544}
Jerry Yudc7bd172022-02-17 13:44:15 +08008545#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8546
XiaokangQian75d40ef2022-04-20 11:05:24 +00008547int mbedtls_ssl_validate_ciphersuite(
8548 const mbedtls_ssl_context *ssl,
8549 const mbedtls_ssl_ciphersuite_t *suite_info,
8550 mbedtls_ssl_protocol_version min_tls_version,
8551 mbedtls_ssl_protocol_version max_tls_version )
8552{
8553 (void) ssl;
8554
8555 if( suite_info == NULL )
8556 return( -1 );
8557
8558 if( ( suite_info->min_tls_version > max_tls_version ) ||
8559 ( suite_info->max_tls_version < min_tls_version ) )
8560 {
8561 return( -1 );
8562 }
8563
XiaokangQian060d8672022-04-21 09:24:56 +00008564#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008565#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8566 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8567 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8568 {
8569 return( -1 );
8570 }
8571#endif
8572
8573 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8574#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8575 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8576 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8577 {
8578 return( -1 );
8579 }
8580#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8581#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8582
8583 return( 0 );
8584}
8585
XiaokangQianeaf36512022-04-24 09:07:44 +00008586#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8587/*
8588 * Function for writing a signature algorithm extension.
8589 *
8590 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8591 * value (TLS 1.3 RFC8446):
8592 * enum {
8593 * ....
8594 * ecdsa_secp256r1_sha256( 0x0403 ),
8595 * ecdsa_secp384r1_sha384( 0x0503 ),
8596 * ecdsa_secp521r1_sha512( 0x0603 ),
8597 * ....
8598 * } SignatureScheme;
8599 *
8600 * struct {
8601 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8602 * } SignatureSchemeList;
8603 *
8604 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8605 * value (TLS 1.2 RFC5246):
8606 * enum {
8607 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8608 * sha512(6), (255)
8609 * } HashAlgorithm;
8610 *
8611 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8612 * SignatureAlgorithm;
8613 *
8614 * struct {
8615 * HashAlgorithm hash;
8616 * SignatureAlgorithm signature;
8617 * } SignatureAndHashAlgorithm;
8618 *
8619 * SignatureAndHashAlgorithm
8620 * supported_signature_algorithms<2..2^16-2>;
8621 *
8622 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8623 * generalization of the TLS 1.2 signature algorithm extension.
8624 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8625 * `SignatureScheme` field of TLS 1.3
8626 *
8627 */
8628int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8629 const unsigned char *end, size_t *out_len )
8630{
8631 unsigned char *p = buf;
8632 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8633 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8634
8635 *out_len = 0;
8636
8637 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8638
8639 /* Check if we have space for header and length field:
8640 * - extension_type (2 bytes)
8641 * - extension_data_length (2 bytes)
8642 * - supported_signature_algorithms_length (2 bytes)
8643 */
8644 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8645 p += 6;
8646
8647 /*
8648 * Write supported_signature_algorithms
8649 */
8650 supported_sig_alg = p;
8651 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8652 if( sig_alg == NULL )
8653 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8654
8655 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8656 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008657 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8658 *sig_alg,
8659 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008660 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8661 continue;
8662 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8663 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8664 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008665 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008666 *sig_alg,
8667 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008668 }
8669
8670 /* Length of supported_signature_algorithms */
8671 supported_sig_alg_len = p - supported_sig_alg;
8672 if( supported_sig_alg_len == 0 )
8673 {
8674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8675 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8676 }
8677
XiaokangQianeaf36512022-04-24 09:07:44 +00008678 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008679 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008680 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8681
XiaokangQianeaf36512022-04-24 09:07:44 +00008682 *out_len = p - buf;
8683
8684#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8685 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8686#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8687 return( 0 );
8688}
8689#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8690
XiaokangQian40a35232022-05-07 09:02:40 +00008691#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008692/*
8693 * mbedtls_ssl_parse_server_name_ext
8694 *
8695 * Structure of server_name extension:
8696 *
8697 * enum {
8698 * host_name(0), (255)
8699 * } NameType;
8700 * opaque HostName<1..2^16-1>;
8701 *
8702 * struct {
8703 * NameType name_type;
8704 * select (name_type) {
8705 * case host_name: HostName;
8706 * } name;
8707 * } ServerName;
8708 * struct {
8709 * ServerName server_name_list<1..2^16-1>
8710 * } ServerNameList;
8711 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008712MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008713int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8714 const unsigned char *buf,
8715 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008716{
8717 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8718 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008719 size_t server_name_list_len, hostname_len;
8720 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008721
XiaokangQianf2a94202022-05-20 06:44:24 +00008722 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008723
8724 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008725 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008726 p += 2;
8727
XiaokangQian9b2b7712022-05-17 02:57:00 +00008728 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8729 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008730 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008731 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008732 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008733 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008734 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8735 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008736
8737 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8738 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008739 /* sni_name is intended to be used only during the parsing of the
8740 * ClientHello message (it is reset to NULL before the end of
8741 * the message parsing). Thus it is ok to just point to the
8742 * reception buffer and not make a copy of it.
8743 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008744 ssl->handshake->sni_name = p + 3;
8745 ssl->handshake->sni_name_len = hostname_len;
8746 if( ssl->conf->f_sni == NULL )
8747 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008748 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008749 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008750 if( ret != 0 )
8751 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008752 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008753 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8754 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008755 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8756 }
8757 return( 0 );
8758 }
8759
8760 p += hostname_len + 3;
8761 }
8762
8763 return( 0 );
8764}
8765#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8766
XiaokangQianacb39922022-06-17 10:18:48 +00008767#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008768MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008769int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8770 const unsigned char *buf,
8771 const unsigned char *end )
8772{
8773 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008774 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008775 const unsigned char *protocol_name_list;
8776 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008777 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008778
8779 /* If ALPN not configured, just ignore the extension */
8780 if( ssl->conf->alpn_list == NULL )
8781 return( 0 );
8782
8783 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008784 * RFC7301, section 3.1
8785 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008786 *
XiaokangQianc7403452022-06-23 03:24:12 +00008787 * struct {
8788 * ProtocolName protocol_name_list<2..2^16-1>
8789 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008790 */
8791
XiaokangQianc7403452022-06-23 03:24:12 +00008792 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008793 * protocol_name_list_len 2 bytes
8794 * protocol_name_len 1 bytes
8795 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008796 */
XiaokangQianacb39922022-06-17 10:18:48 +00008797 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8798
XiaokangQianc7403452022-06-23 03:24:12 +00008799 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008800 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008801 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008802 protocol_name_list = p;
8803 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008804
8805 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008806 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008807 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008808 protocol_name_len = *p++;
8809 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8810 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008811 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008812 {
8813 MBEDTLS_SSL_PEND_FATAL_ALERT(
8814 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8815 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008816 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008817 }
8818
8819 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008820 }
8821
8822 /* Use our order of preference */
8823 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8824 {
8825 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008826 p = protocol_name_list;
8827 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008828 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008829 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008830 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008831 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008832 {
8833 ssl->alpn_chosen = *alpn;
8834 return( 0 );
8835 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008836
8837 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008838 }
8839 }
8840
XiaokangQian95d5f542022-06-24 02:29:26 +00008841 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008842 MBEDTLS_SSL_PEND_FATAL_ALERT(
8843 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8844 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8845 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8846}
8847
8848int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8849 unsigned char *buf,
8850 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008851 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008852{
8853 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008854 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008855 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008856
8857 if( ssl->alpn_chosen == NULL )
8858 {
8859 return( 0 );
8860 }
8861
XiaokangQian95d5f542022-06-24 02:29:26 +00008862 protocol_name_len = strlen( ssl->alpn_chosen );
8863 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008864
8865 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8866 /*
8867 * 0 . 1 ext identifier
8868 * 2 . 3 ext length
8869 * 4 . 5 protocol list length
8870 * 6 . 6 protocol name length
8871 * 7 . 7+n protocol name
8872 */
8873 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8874
XiaokangQian95d5f542022-06-24 02:29:26 +00008875 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008876
XiaokangQian95d5f542022-06-24 02:29:26 +00008877 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8878 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008879 /* Note: the length of the chosen protocol has been checked to be less
8880 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8881 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008882 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008883
XiaokangQian95d5f542022-06-24 02:29:26 +00008884 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008885 return ( 0 );
8886}
8887#endif /* MBEDTLS_SSL_ALPN */
8888
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008889#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00008890 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00008891 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00008892 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008893int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
8894 const char *hostname )
8895{
8896 /* Initialize to suppress unnecessary compiler warning */
8897 size_t hostname_len = 0;
8898
8899 /* Check if new hostname is valid before
8900 * making any change to current one */
8901 if( hostname != NULL )
8902 {
8903 hostname_len = strlen( hostname );
8904
8905 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8906 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8907 }
8908
8909 /* Now it's clear that we will overwrite the old hostname,
8910 * so we can free it safely */
8911 if( session->hostname != NULL )
8912 {
8913 mbedtls_platform_zeroize( session->hostname,
8914 strlen( session->hostname ) );
8915 mbedtls_free( session->hostname );
8916 }
8917
8918 /* Passing NULL as hostname shall clear the old one */
8919 if( hostname == NULL )
8920 {
8921 session->hostname = NULL;
8922 }
8923 else
8924 {
8925 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
8926 if( session->hostname == NULL )
8927 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8928
8929 memcpy( session->hostname, hostname, hostname_len );
8930 }
8931
8932 return( 0 );
8933}
Xiaokang Qian03409292022-10-12 02:49:52 +00008934#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
8935 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00008936 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00008937 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008939#endif /* MBEDTLS_SSL_TLS_C */