blob: c8904f288c4e325e112e0aee5074373f40460253 [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 ||
1657 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001658 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001659 {
1660 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1661 }
1662
Hanno Becker7390c712018-11-15 13:33:04 +00001663 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1664 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001665 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001666
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001667 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001668 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001669
1670 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001671}
1672
Hanno Becker7390c712018-11-15 13:33:04 +00001673int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1674 const unsigned char *psk, size_t psk_len,
1675 const unsigned char *psk_identity, size_t psk_identity_len )
1676{
Janos Follath865b3eb2019-12-16 11:46:15 +00001677 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001678
1679 /* We currently only support one PSK, raw or opaque. */
1680 if( ssl_conf_psk_is_configured( conf ) )
1681 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001682
1683 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001684 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001685 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001686 if( psk_len == 0 )
1687 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1688 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1689 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1690
Hanno Becker7390c712018-11-15 13:33:04 +00001691 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1692 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1693 conf->psk_len = psk_len;
1694 memcpy( conf->psk, psk, conf->psk_len );
1695
1696 /* Check and set PSK Identity */
1697 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1698 if( ret != 0 )
1699 ssl_conf_remove_psk( conf );
1700
1701 return( ret );
1702}
1703
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001704static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1705{
1706#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001707 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001708 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001709 /* The maintenance of the external PSK key slot is the
1710 * user's responsibility. */
1711 if( ssl->handshake->psk_opaque_is_internal )
1712 {
1713 psa_destroy_key( ssl->handshake->psk_opaque );
1714 ssl->handshake->psk_opaque_is_internal = 0;
1715 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001716 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001717 }
Neil Armstronge952a302022-05-03 10:22:14 +02001718#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001719 if( ssl->handshake->psk != NULL )
1720 {
1721 mbedtls_platform_zeroize( ssl->handshake->psk,
1722 ssl->handshake->psk_len );
1723 mbedtls_free( ssl->handshake->psk );
1724 ssl->handshake->psk_len = 0;
1725 }
Neil Armstronge952a302022-05-03 10:22:14 +02001726#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001727}
1728
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001729int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1730 const unsigned char *psk, size_t psk_len )
1731{
Neil Armstrong501c9322022-05-03 09:35:09 +02001732#if defined(MBEDTLS_USE_PSA_CRYPTO)
1733 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001734 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001735 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001736 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001737#endif /* MBEDTLS_USE_PSA_CRYPTO */
1738
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001739 if( psk == NULL || ssl->handshake == NULL )
1740 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1741
1742 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1743 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1744
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001745 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001746
Neil Armstrong501c9322022-05-03 09:35:09 +02001747#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001748#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1749 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1750 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001751 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001752 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1753 else
1754 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1755 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1756 }
1757#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001758
Jerry Yu568ec252022-07-22 21:27:34 +08001759#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001760 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1761 {
1762 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1763 psa_set_key_usage_flags( &key_attributes,
1764 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1765 }
1766#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1767
Neil Armstrong501c9322022-05-03 09:35:09 +02001768 psa_set_key_algorithm( &key_attributes, alg );
1769 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1770
1771 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1772 if( status != PSA_SUCCESS )
1773 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1774
1775 /* Allow calling psa_destroy_key() on psk remove */
1776 ssl->handshake->psk_opaque_is_internal = 1;
1777 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1778#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001779 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001780 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001781
1782 ssl->handshake->psk_len = psk_len;
1783 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1784
1785 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001786#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001787}
1788
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001789#if defined(MBEDTLS_USE_PSA_CRYPTO)
1790int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001791 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001792 const unsigned char *psk_identity,
1793 size_t psk_identity_len )
1794{
Janos Follath865b3eb2019-12-16 11:46:15 +00001795 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001796
1797 /* We currently only support one PSK, raw or opaque. */
1798 if( ssl_conf_psk_is_configured( conf ) )
1799 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001800
Hanno Becker7390c712018-11-15 13:33:04 +00001801 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001802 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001803 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001804 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001805
1806 /* Check and set PSK Identity */
1807 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1808 psk_identity_len );
1809 if( ret != 0 )
1810 ssl_conf_remove_psk( conf );
1811
1812 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001813}
1814
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001815int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1816 mbedtls_svc_key_id_t psk )
1817{
1818 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1819 ( ssl->handshake == NULL ) )
1820 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1821
1822 ssl_remove_psk( ssl );
1823 ssl->handshake->psk_opaque = psk;
1824 return( 0 );
1825}
1826#endif /* MBEDTLS_USE_PSA_CRYPTO */
1827
Jerry Yu8897c072022-08-12 13:56:53 +08001828#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001829void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1830 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1831 size_t),
1832 void *p_psk )
1833{
1834 conf->f_psk = f_psk;
1835 conf->p_psk = p_psk;
1836}
Jerry Yu8897c072022-08-12 13:56:53 +08001837#endif /* MBEDTLS_SSL_SRV_C */
1838
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001839#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1840
1841#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001842static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1843 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001844{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001845#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001846 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001847 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001848#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001849 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001850 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001851 return( MBEDTLS_SSL_MODE_STREAM );
1852}
1853
1854#else /* MBEDTLS_USE_PSA_CRYPTO */
1855
1856static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1857 mbedtls_cipher_mode_t mode )
1858{
1859#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1860 if( mode == MBEDTLS_MODE_CBC )
1861 return( MBEDTLS_SSL_MODE_CBC );
1862#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1863
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001864#if defined(MBEDTLS_GCM_C) || \
1865 defined(MBEDTLS_CCM_C) || \
1866 defined(MBEDTLS_CHACHAPOLY_C)
1867 if( mode == MBEDTLS_MODE_GCM ||
1868 mode == MBEDTLS_MODE_CCM ||
1869 mode == MBEDTLS_MODE_CHACHAPOLY )
1870 return( MBEDTLS_SSL_MODE_AEAD );
1871#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001872
1873 return( MBEDTLS_SSL_MODE_STREAM );
1874}
Gilles Peskine301711e2022-04-26 16:57:05 +02001875#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001876
Gilles Peskinee108d982022-04-26 16:50:40 +02001877static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1878 mbedtls_ssl_mode_t base_mode,
1879 int encrypt_then_mac )
1880{
1881#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1882 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1883 base_mode == MBEDTLS_SSL_MODE_CBC )
1884 {
1885 return( MBEDTLS_SSL_MODE_CBC_ETM );
1886 }
1887#else
1888 (void) encrypt_then_mac;
1889#endif
1890 return( base_mode );
1891}
1892
Neil Armstrongab555e02022-04-04 11:07:59 +02001893mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001894 const mbedtls_ssl_transform *transform )
1895{
Gilles Peskinee108d982022-04-26 16:50:40 +02001896 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001897#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001898 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001899#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001900 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1901#endif
1902 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001903
Gilles Peskinee108d982022-04-26 16:50:40 +02001904 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001905#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001906 encrypt_then_mac = transform->encrypt_then_mac;
1907#endif
1908 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001909}
1910
Neil Armstrongab555e02022-04-04 11:07:59 +02001911mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001912#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001913 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001914#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001915 const mbedtls_ssl_ciphersuite_t *suite )
1916{
Gilles Peskinee108d982022-04-26 16:50:40 +02001917 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1918
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001919#if defined(MBEDTLS_USE_PSA_CRYPTO)
1920 psa_status_t status;
1921 psa_algorithm_t alg;
1922 psa_key_type_t type;
1923 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001924 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1925 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001926 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001927#else
1928 const mbedtls_cipher_info_t *cipher =
1929 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001930 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001931 {
1932 base_mode =
1933 mbedtls_ssl_get_base_mode(
1934 mbedtls_cipher_info_get_mode( cipher ) );
1935 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001936#endif /* MBEDTLS_USE_PSA_CRYPTO */
1937
Gilles Peskinee108d982022-04-26 16:50:40 +02001938#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1939 int encrypt_then_mac = 0;
1940#endif
1941 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001942}
1943
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001944#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001945
1946#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001947/* Serialization of TLS 1.3 sessions:
1948 *
1949 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00001950 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001951 * uint64 ticket_received;
1952 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08001953 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001954 * } ClientOnlyData;
1955 *
1956 * struct {
1957 * uint8 endpoint;
1958 * uint8 ciphersuite[2];
1959 * uint32 ticket_age_add;
1960 * uint8 ticket_flags;
1961 * opaque resumption_key<0..255>;
1962 * select ( endpoint ) {
1963 * case client: ClientOnlyData;
1964 * case server: uint64 start_time;
1965 * };
1966 * } serialized_session_tls13;
1967 *
1968 */
1969#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08001970MBEDTLS_CHECK_RETURN_CRITICAL
1971static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
1972 unsigned char *buf,
1973 size_t buf_len,
1974 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00001975{
1976 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00001977#if defined(MBEDTLS_SSL_CLI_C) && \
1978 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1979 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00001980 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00001981#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08001982 size_t needed = 1 /* endpoint */
1983 + 2 /* ciphersuite */
1984 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08001985 + 1 /* ticket_flags */
1986 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08001987 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08001988
1989 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
1990 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1991 needed += session->resumption_key_len; /* resumption_key */
1992
Jerry Yu438ddd82022-07-07 06:55:50 +00001993#if defined(MBEDTLS_HAVE_TIME)
1994 needed += 8; /* start_time or ticket_received */
1995#endif
1996
1997#if defined(MBEDTLS_SSL_CLI_C)
1998 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1999 {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002000#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002001 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002002 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002003#endif
2004
Jerry Yu438ddd82022-07-07 06:55:50 +00002005 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002006 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002007
2008 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002009 if( session->ticket_len > SIZE_MAX - needed )
2010 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002011
Jerry Yue28d9742022-08-18 15:44:03 +08002012 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002013 }
2014#endif /* MBEDTLS_SSL_CLI_C */
2015
Jerry Yue36fdd62022-08-17 21:31:36 +08002016 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002017 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002018 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002019
2020 p[0] = session->endpoint;
2021 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2022 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2023 p[7] = session->ticket_flags;
2024
2025 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002026 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002027 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002028 memcpy( p, session->resumption_key, session->resumption_key_len );
2029 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002030
2031#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2032 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2033 {
2034 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2035 p += 8;
2036 }
2037#endif /* MBEDTLS_HAVE_TIME */
2038
2039#if defined(MBEDTLS_SSL_CLI_C)
2040 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2041 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002042#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2043 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2044 p += 2;
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002045 if( hostname_len > 0 )
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002046 {
2047 /* save host name */
2048 memcpy( p, session->hostname, hostname_len );
2049 p += hostname_len;
2050 }
2051#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2052
Jerry Yu438ddd82022-07-07 06:55:50 +00002053#if defined(MBEDTLS_HAVE_TIME)
2054 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2055 p += 8;
2056#endif
2057 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2058 p += 4;
2059
2060 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2061 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002062
2063 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002064 {
2065 memcpy( p, session->ticket, session->ticket_len );
2066 p += session->ticket_len;
2067 }
2068 }
2069#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002070 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002071}
2072
2073MBEDTLS_CHECK_RETURN_CRITICAL
2074static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2075 const unsigned char *buf,
2076 size_t len )
2077{
2078 const unsigned char *p = buf;
2079 const unsigned char *end = buf + len;
2080
2081 if( end - p < 9 )
2082 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2083 session->endpoint = p[0];
2084 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2085 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2086 session->ticket_flags = p[7];
2087
2088 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002089 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002090 p += 9;
2091
Jerry Yubc7c1a42022-07-21 22:57:37 +08002092 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002093 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2094
Jerry Yubc7c1a42022-07-21 22:57:37 +08002095 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002096 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002097 memcpy( session->resumption_key, p, session->resumption_key_len );
2098 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002099
2100#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2101 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2102 {
2103 if( end - p < 8 )
2104 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2105 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2106 p += 8;
2107 }
2108#endif /* MBEDTLS_HAVE_TIME */
2109
2110#if defined(MBEDTLS_SSL_CLI_C)
2111 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2112 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002113#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2114 defined(MBEDTLS_SSL_SESSION_TICKETS)
2115 size_t hostname_len;
2116 /* load host name */
2117 if( end - p < 2 )
2118 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2119 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2120 p += 2;
2121
2122 if( end - p < ( long int )hostname_len )
2123 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2124 if( hostname_len > 0 )
2125 {
2126 session->hostname = mbedtls_calloc( 1, hostname_len );
2127 if( session->hostname == NULL )
2128 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2129 memcpy( session->hostname, p, hostname_len );
2130 p += hostname_len;
2131 }
2132#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2133 MBEDTLS_SSL_SESSION_TICKETS */
2134
Jerry Yu438ddd82022-07-07 06:55:50 +00002135#if defined(MBEDTLS_HAVE_TIME)
2136 if( end - p < 8 )
2137 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2138 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2139 p += 8;
2140#endif
2141 if( end - p < 4 )
2142 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2143 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2144 p += 4;
2145
2146 if( end - p < 2 )
2147 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2148 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2149 p += 2;
2150
2151 if( end - p < ( long int )session->ticket_len )
2152 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2153 if( session->ticket_len > 0 )
2154 {
2155 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2156 if( session->ticket == NULL )
2157 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2158 memcpy( session->ticket, p, session->ticket_len );
2159 p += session->ticket_len;
2160 }
2161 }
2162#endif /* MBEDTLS_SSL_CLI_C */
2163
2164 return( 0 );
2165
2166}
2167#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002168MBEDTLS_CHECK_RETURN_CRITICAL
2169static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2170 unsigned char *buf,
2171 size_t buf_len,
2172 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002173{
2174 ((void) session);
2175 ((void) buf);
2176 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002177 *olen = 0;
2178 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002179}
Jerry Yu438ddd82022-07-07 06:55:50 +00002180
2181static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2182 unsigned char *buf,
2183 size_t buf_len )
2184{
2185 ((void) session);
2186 ((void) buf);
2187 ((void) buf_len);
2188 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2189}
2190#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002191#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2192
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002193psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002194 size_t taglen,
2195 psa_algorithm_t *alg,
2196 psa_key_type_t *key_type,
2197 size_t *key_size )
2198{
2199 switch ( mbedtls_cipher_type )
2200 {
2201 case MBEDTLS_CIPHER_AES_128_CBC:
2202 *alg = PSA_ALG_CBC_NO_PADDING;
2203 *key_type = PSA_KEY_TYPE_AES;
2204 *key_size = 128;
2205 break;
2206 case MBEDTLS_CIPHER_AES_128_CCM:
2207 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2208 *key_type = PSA_KEY_TYPE_AES;
2209 *key_size = 128;
2210 break;
2211 case MBEDTLS_CIPHER_AES_128_GCM:
2212 *alg = PSA_ALG_GCM;
2213 *key_type = PSA_KEY_TYPE_AES;
2214 *key_size = 128;
2215 break;
2216 case MBEDTLS_CIPHER_AES_192_CCM:
2217 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2218 *key_type = PSA_KEY_TYPE_AES;
2219 *key_size = 192;
2220 break;
2221 case MBEDTLS_CIPHER_AES_192_GCM:
2222 *alg = PSA_ALG_GCM;
2223 *key_type = PSA_KEY_TYPE_AES;
2224 *key_size = 192;
2225 break;
2226 case MBEDTLS_CIPHER_AES_256_CBC:
2227 *alg = PSA_ALG_CBC_NO_PADDING;
2228 *key_type = PSA_KEY_TYPE_AES;
2229 *key_size = 256;
2230 break;
2231 case MBEDTLS_CIPHER_AES_256_CCM:
2232 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2233 *key_type = PSA_KEY_TYPE_AES;
2234 *key_size = 256;
2235 break;
2236 case MBEDTLS_CIPHER_AES_256_GCM:
2237 *alg = PSA_ALG_GCM;
2238 *key_type = PSA_KEY_TYPE_AES;
2239 *key_size = 256;
2240 break;
2241 case MBEDTLS_CIPHER_ARIA_128_CBC:
2242 *alg = PSA_ALG_CBC_NO_PADDING;
2243 *key_type = PSA_KEY_TYPE_ARIA;
2244 *key_size = 128;
2245 break;
2246 case MBEDTLS_CIPHER_ARIA_128_CCM:
2247 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2248 *key_type = PSA_KEY_TYPE_ARIA;
2249 *key_size = 128;
2250 break;
2251 case MBEDTLS_CIPHER_ARIA_128_GCM:
2252 *alg = PSA_ALG_GCM;
2253 *key_type = PSA_KEY_TYPE_ARIA;
2254 *key_size = 128;
2255 break;
2256 case MBEDTLS_CIPHER_ARIA_192_CCM:
2257 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2258 *key_type = PSA_KEY_TYPE_ARIA;
2259 *key_size = 192;
2260 break;
2261 case MBEDTLS_CIPHER_ARIA_192_GCM:
2262 *alg = PSA_ALG_GCM;
2263 *key_type = PSA_KEY_TYPE_ARIA;
2264 *key_size = 192;
2265 break;
2266 case MBEDTLS_CIPHER_ARIA_256_CBC:
2267 *alg = PSA_ALG_CBC_NO_PADDING;
2268 *key_type = PSA_KEY_TYPE_ARIA;
2269 *key_size = 256;
2270 break;
2271 case MBEDTLS_CIPHER_ARIA_256_CCM:
2272 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2273 *key_type = PSA_KEY_TYPE_ARIA;
2274 *key_size = 256;
2275 break;
2276 case MBEDTLS_CIPHER_ARIA_256_GCM:
2277 *alg = PSA_ALG_GCM;
2278 *key_type = PSA_KEY_TYPE_ARIA;
2279 *key_size = 256;
2280 break;
2281 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2282 *alg = PSA_ALG_CBC_NO_PADDING;
2283 *key_type = PSA_KEY_TYPE_CAMELLIA;
2284 *key_size = 128;
2285 break;
2286 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2287 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2288 *key_type = PSA_KEY_TYPE_CAMELLIA;
2289 *key_size = 128;
2290 break;
2291 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2292 *alg = PSA_ALG_GCM;
2293 *key_type = PSA_KEY_TYPE_CAMELLIA;
2294 *key_size = 128;
2295 break;
2296 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2297 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2298 *key_type = PSA_KEY_TYPE_CAMELLIA;
2299 *key_size = 192;
2300 break;
2301 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2302 *alg = PSA_ALG_GCM;
2303 *key_type = PSA_KEY_TYPE_CAMELLIA;
2304 *key_size = 192;
2305 break;
2306 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2307 *alg = PSA_ALG_CBC_NO_PADDING;
2308 *key_type = PSA_KEY_TYPE_CAMELLIA;
2309 *key_size = 256;
2310 break;
2311 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2312 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2313 *key_type = PSA_KEY_TYPE_CAMELLIA;
2314 *key_size = 256;
2315 break;
2316 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2317 *alg = PSA_ALG_GCM;
2318 *key_type = PSA_KEY_TYPE_CAMELLIA;
2319 *key_size = 256;
2320 break;
2321 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2322 *alg = PSA_ALG_CHACHA20_POLY1305;
2323 *key_type = PSA_KEY_TYPE_CHACHA20;
2324 *key_size = 256;
2325 break;
2326 case MBEDTLS_CIPHER_NULL:
2327 *alg = MBEDTLS_SSL_NULL_CIPHER;
2328 *key_type = 0;
2329 *key_size = 0;
2330 break;
2331 default:
2332 return PSA_ERROR_NOT_SUPPORTED;
2333 }
2334
2335 return PSA_SUCCESS;
2336}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002337#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002338
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002339#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002340int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2341 const unsigned char *dhm_P, size_t P_len,
2342 const unsigned char *dhm_G, size_t G_len )
2343{
Janos Follath865b3eb2019-12-16 11:46:15 +00002344 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002345
Glenn Strausscee11292021-12-20 01:43:17 -05002346 mbedtls_mpi_free( &conf->dhm_P );
2347 mbedtls_mpi_free( &conf->dhm_G );
2348
Hanno Beckera90658f2017-10-04 15:29:08 +01002349 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2350 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2351 {
2352 mbedtls_mpi_free( &conf->dhm_P );
2353 mbedtls_mpi_free( &conf->dhm_G );
2354 return( ret );
2355 }
2356
2357 return( 0 );
2358}
Paul Bakker5121ce52009-01-03 21:22:43 +00002359
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002360int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002361{
Janos Follath865b3eb2019-12-16 11:46:15 +00002362 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002363
Glenn Strausscee11292021-12-20 01:43:17 -05002364 mbedtls_mpi_free( &conf->dhm_P );
2365 mbedtls_mpi_free( &conf->dhm_G );
2366
Gilles Peskinee5702482021-06-11 21:59:08 +02002367 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2368 &conf->dhm_P ) ) != 0 ||
2369 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2370 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002371 {
2372 mbedtls_mpi_free( &conf->dhm_P );
2373 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002374 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002375 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002376
2377 return( 0 );
2378}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002379#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002380
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002381#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2382/*
2383 * Set the minimum length for Diffie-Hellman parameters
2384 */
2385void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2386 unsigned int bitlen )
2387{
2388 conf->dhm_min_bitlen = bitlen;
2389}
2390#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2391
Gilles Peskineeccd8882020-03-10 12:19:08 +01002392#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002393#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002394/*
2395 * Set allowed/preferred hashes for handshake signatures
2396 */
2397void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2398 const int *hashes )
2399{
2400 conf->sig_hashes = hashes;
2401}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002402#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002403
Jerry Yuf017ee42022-01-12 15:49:48 +08002404/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002405void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2406 const uint16_t* sig_algs )
2407{
Jerry Yuf017ee42022-01-12 15:49:48 +08002408#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2409 conf->sig_hashes = NULL;
2410#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2411 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002412}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002413#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002414
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002415#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002416#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002417/*
2418 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002419 *
2420 * mbedtls_ssl_setup() takes the provided list
2421 * and translates it to a list of IANA TLS group identifiers,
2422 * stored in ssl->handshake->group_list.
2423 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002424 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002425void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002426 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002427{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002428 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002429 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002430}
Brett Warrene0edc842021-08-17 09:53:13 +01002431#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002432#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002433
Brett Warrene0edc842021-08-17 09:53:13 +01002434/*
2435 * Set the allowed groups
2436 */
2437void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2438 const uint16_t *group_list )
2439{
2440#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2441 conf->curve_list = NULL;
2442#endif
2443 conf->group_list = group_list;
2444}
2445
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002446#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002448{
Hanno Becker947194e2017-04-07 13:25:49 +01002449 /* Initialize to suppress unnecessary compiler warning */
2450 size_t hostname_len = 0;
2451
2452 /* Check if new hostname is valid before
2453 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002454 if( hostname != NULL )
2455 {
2456 hostname_len = strlen( hostname );
2457
2458 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2459 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2460 }
2461
2462 /* Now it's clear that we will overwrite the old hostname,
2463 * so we can free it safely */
2464
2465 if( ssl->hostname != NULL )
2466 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002467 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002468 mbedtls_free( ssl->hostname );
2469 }
2470
2471 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002472
Paul Bakker5121ce52009-01-03 21:22:43 +00002473 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002474 {
2475 ssl->hostname = NULL;
2476 }
2477 else
2478 {
2479 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002480 if( ssl->hostname == NULL )
2481 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002482
Hanno Becker947194e2017-04-07 13:25:49 +01002483 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002484
Hanno Becker947194e2017-04-07 13:25:49 +01002485 ssl->hostname[hostname_len] = '\0';
2486 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002487
2488 return( 0 );
2489}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002490#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002491
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002492#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002493void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002495 const unsigned char *, size_t),
2496 void *p_sni )
2497{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002498 conf->f_sni = f_sni;
2499 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002500}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002504int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002505{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002506 size_t cur_len, tot_len;
2507 const char **p;
2508
2509 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002510 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2511 * MUST NOT be truncated."
2512 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002513 */
2514 tot_len = 0;
2515 for( p = protos; *p != NULL; p++ )
2516 {
2517 cur_len = strlen( *p );
2518 tot_len += cur_len;
2519
Ronald Cron8216dd32020-04-23 16:41:44 +02002520 if( ( cur_len == 0 ) ||
2521 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2522 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002524 }
2525
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002526 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002527
2528 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002529}
2530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002532{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002533 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002534}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002536
Johan Pascalb62bb512015-12-03 21:56:45 +01002537#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002538void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2539 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002540{
2541 conf->dtls_srtp_mki_support = support_mki_value;
2542}
2543
Ron Eldoref72faf2018-07-12 11:54:20 +03002544int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2545 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002546 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002547{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002548 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002549 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002550 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002551 }
Ron Eldor591f1622018-01-22 12:30:04 +02002552
2553 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002554 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002555 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002556 }
Ron Eldor591f1622018-01-22 12:30:04 +02002557
2558 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2559 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002560 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002561}
2562
Ron Eldoref72faf2018-07-12 11:54:20 +03002563int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002564 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002565{
Johan Pascal253d0262020-09-22 13:04:45 +02002566 const mbedtls_ssl_srtp_profile *p;
2567 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002568
Johan Pascal253d0262020-09-22 13:04:45 +02002569 /* check the profiles list: all entry must be valid,
2570 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002571 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2572 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2573 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002574 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002575 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002576 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002577 list_size++;
2578 }
2579 else
2580 {
2581 /* unsupported value, stop parsing and set the size to an error value */
2582 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002583 }
2584 }
2585
Johan Pascal5ef72d22020-10-28 17:05:47 +01002586 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002587 {
Johan Pascal253d0262020-09-22 13:04:45 +02002588 conf->dtls_srtp_profile_list = NULL;
2589 conf->dtls_srtp_profile_list_len = 0;
2590 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2591 }
2592
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002593 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002594 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002595
2596 return( 0 );
2597}
2598
Johan Pascal5ef72d22020-10-28 17:05:47 +01002599void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2600 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002601{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002602 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2603 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002604 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002605 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002606 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002607 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002608 else
2609 {
2610 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002611 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2612 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002613 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002614}
Johan Pascalb62bb512015-12-03 21:56:45 +01002615#endif /* MBEDTLS_SSL_DTLS_SRTP */
2616
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302617#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002618void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002619{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002620 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002621}
2622
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002623void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002624{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002625 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002626}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302627#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002628
Janos Follath088ce432017-04-10 12:42:31 +01002629#if defined(MBEDTLS_SSL_SRV_C)
2630void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2631 char cert_req_ca_list )
2632{
2633 conf->cert_req_ca_list = cert_req_ca_list;
2634}
2635#endif
2636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002638void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002639{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002640 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002641}
2642#endif
2643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002645void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002646{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002647 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002648}
2649#endif
2650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002652int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002653{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002655 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002658 }
2659
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002660 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002661
2662 return( 0 );
2663}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002665
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002666void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002667{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002668 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002669}
2670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002672void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002673{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002674 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002675}
2676
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002677void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002678{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002679 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002680}
2681
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002682void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002683 const unsigned char period[8] )
2684{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002685 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002686}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002690#if defined(MBEDTLS_SSL_CLI_C)
2691void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002692{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002693 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002694}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002695#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002696
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002697#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002698
Jerry Yud0766ec2022-09-22 10:46:57 +08002699#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002700void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2701 uint16_t num_tickets )
2702{
Jerry Yud0766ec2022-09-22 10:46:57 +08002703 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002704}
2705#endif
2706
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002707void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2708 mbedtls_ssl_ticket_write_t *f_ticket_write,
2709 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2710 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002711{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002712 conf->f_ticket_write = f_ticket_write;
2713 conf->f_ticket_parse = f_ticket_parse;
2714 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002715}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002716#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002718
Hanno Becker7e6c1782021-06-08 09:24:55 +01002719void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2720 mbedtls_ssl_export_keys_t *f_export_keys,
2721 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002722{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002723 ssl->f_export_keys = f_export_keys;
2724 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002725}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002726
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002727#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002728void mbedtls_ssl_conf_async_private_cb(
2729 mbedtls_ssl_config *conf,
2730 mbedtls_ssl_async_sign_t *f_async_sign,
2731 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2732 mbedtls_ssl_async_resume_t *f_async_resume,
2733 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002734 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002735{
2736 conf->f_async_sign_start = f_async_sign;
2737 conf->f_async_decrypt_start = f_async_decrypt;
2738 conf->f_async_resume = f_async_resume;
2739 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002740 conf->p_async_config_data = async_config_data;
2741}
2742
Gilles Peskine8f97af72018-04-26 11:46:10 +02002743void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2744{
2745 return( conf->p_async_config_data );
2746}
2747
Gilles Peskine1febfef2018-04-30 11:54:39 +02002748void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002749{
2750 if( ssl->handshake == NULL )
2751 return( NULL );
2752 else
2753 return( ssl->handshake->user_async_ctx );
2754}
2755
Gilles Peskine1febfef2018-04-30 11:54:39 +02002756void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002757 void *ctx )
2758{
2759 if( ssl->handshake != NULL )
2760 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002761}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002762#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002763
Paul Bakker5121ce52009-01-03 21:22:43 +00002764/*
2765 * SSL get accessors
2766 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002767uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002768{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002769 if( ssl->session != NULL )
2770 return( ssl->session->verify_result );
2771
2772 if( ssl->session_negotiate != NULL )
2773 return( ssl->session_negotiate->verify_result );
2774
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002775 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002776}
2777
Glenn Strauss8f526902022-01-13 00:04:49 -05002778int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2779{
2780 if( ssl == NULL || ssl->session == NULL )
2781 return( 0 );
2782
2783 return( ssl->session->ciphersuite );
2784}
2785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002787{
Paul Bakker926c8e42013-03-06 10:23:34 +01002788 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002789 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002792}
2793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002794const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002795{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002797 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002798 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002799 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002800 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002801 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002802 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002803 default:
2804 return( "unknown (DTLS)" );
2805 }
2806 }
2807#endif
2808
Glenn Strauss60bfe602022-03-14 19:04:24 -04002809 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002810 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002811 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002812 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002813 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002814 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002815 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002816 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002817 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002818}
2819
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002820#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002821size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2822{
David Horstmann95d516f2021-05-04 18:36:56 +01002823 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002824 size_t read_mfl;
2825
2826 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2827 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2828 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2829 {
2830 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2831 }
2832
2833 /* Check if a smaller max length was negotiated */
2834 if( ssl->session_out != NULL )
2835 {
2836 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2837 if( read_mfl < max_len )
2838 {
2839 max_len = read_mfl;
2840 }
2841 }
2842
2843 // During a handshake, use the value being negotiated
2844 if( ssl->session_negotiate != NULL )
2845 {
2846 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2847 if( read_mfl < max_len )
2848 {
2849 max_len = read_mfl;
2850 }
2851 }
2852
2853 return( max_len );
2854}
2855
2856size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002857{
2858 size_t max_len;
2859
2860 /*
2861 * Assume mfl_code is correct since it was checked when set
2862 */
Angus Grattond8213d02016-05-25 20:56:48 +10002863 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002864
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002865 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002866 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002867 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002868 {
Angus Grattond8213d02016-05-25 20:56:48 +10002869 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002870 }
2871
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002872 /* During a handshake, use the value being negotiated */
2873 if( ssl->session_negotiate != NULL &&
2874 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2875 {
2876 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2877 }
2878
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002879 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002880}
2881#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2882
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002883#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002884size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002885{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002886 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2887 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2888 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2889 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2890 return ( 0 );
2891
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002892 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2893 return( ssl->mtu );
2894
2895 if( ssl->mtu == 0 )
2896 return( ssl->handshake->mtu );
2897
2898 return( ssl->mtu < ssl->handshake->mtu ?
2899 ssl->mtu : ssl->handshake->mtu );
2900}
2901#endif /* MBEDTLS_SSL_PROTO_DTLS */
2902
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002903int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2904{
2905 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2906
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002907#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2908 !defined(MBEDTLS_SSL_PROTO_DTLS)
2909 (void) ssl;
2910#endif
2911
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002912#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002913 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002914
2915 if( max_len > mfl )
2916 max_len = mfl;
2917#endif
2918
2919#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002920 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002921 {
Hanno Becker89490712020-02-05 10:50:12 +00002922 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002923 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2924 const size_t overhead = (size_t) ret;
2925
2926 if( ret < 0 )
2927 return( ret );
2928
2929 if( mtu <= overhead )
2930 {
2931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2932 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2933 }
2934
2935 if( max_len > mtu - overhead )
2936 max_len = mtu - overhead;
2937 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002938#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002939
Hanno Becker0defedb2018-08-10 12:35:02 +01002940#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2941 !defined(MBEDTLS_SSL_PROTO_DTLS)
2942 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002943#endif
2944
2945 return( (int) max_len );
2946}
2947
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002948int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2949{
2950 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2951
2952#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2953 (void) ssl;
2954#endif
2955
2956#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2957 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2958
2959 if( max_len > mfl )
2960 max_len = mfl;
2961#endif
2962
2963 return( (int) max_len );
2964}
2965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002966#if defined(MBEDTLS_X509_CRT_PARSE_C)
2967const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002968{
2969 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002970 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002971
Hanno Beckere6824572019-02-07 13:18:46 +00002972#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002973 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002974#else
2975 return( NULL );
2976#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002977}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002981int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2982 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002983{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002984 int ret;
2985
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002986 if( ssl == NULL ||
2987 dst == NULL ||
2988 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002989 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002992 }
2993
Hanno Beckere810bbc2021-05-14 16:01:05 +01002994 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2995 * idempotent: Each session can only be exported once.
2996 *
2997 * (This is in preparation for TLS 1.3 support where we will
2998 * need the ability to export multiple sessions (aka tickets),
2999 * which will be achieved by calling mbedtls_ssl_get_session()
3000 * multiple times until it fails.)
3001 *
3002 * Check whether we have already exported the current session,
3003 * and fail if so.
3004 */
3005 if( ssl->session->exported == 1 )
3006 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3007
3008 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3009 if( ret != 0 )
3010 return( ret );
3011
3012 /* Remember that we've exported the session. */
3013 ssl->session->exported = 1;
3014 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003015}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003017
Paul Bakker5121ce52009-01-03 21:22:43 +00003018/*
Hanno Beckera835da52019-05-16 12:39:07 +01003019 * Define ticket header determining Mbed TLS version
3020 * and structure of the ticket.
3021 */
3022
Hanno Becker94ef3b32019-05-16 12:50:45 +01003023/*
Hanno Becker50b59662019-05-28 14:30:45 +01003024 * Define bitflag determining compile-time settings influencing
3025 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003026 */
3027
Hanno Becker50b59662019-05-28 14:30:45 +01003028#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003029#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003030#else
Hanno Becker3e088662019-05-29 11:10:18 +01003031#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003032#endif /* MBEDTLS_HAVE_TIME */
3033
3034#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003035#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003036#else
Hanno Becker3e088662019-05-29 11:10:18 +01003037#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003038#endif /* MBEDTLS_X509_CRT_PARSE_C */
3039
3040#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003041#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003042#else
Hanno Becker3e088662019-05-29 11:10:18 +01003043#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003044#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3045
3046#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003047#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003048#else
Hanno Becker3e088662019-05-29 11:10:18 +01003049#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003050#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3051
Hanno Becker94ef3b32019-05-16 12:50:45 +01003052#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003053#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003054#else
Hanno Becker3e088662019-05-29 11:10:18 +01003055#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003056#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3057
Hanno Becker94ef3b32019-05-16 12:50:45 +01003058#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3059#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3060#else
3061#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3062#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3063
Hanno Becker3e088662019-05-29 11:10:18 +01003064#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3065#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3066#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3067#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003068#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3069#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003070
Hanno Becker50b59662019-05-28 14:30:45 +01003071#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003072 ( (uint16_t) ( \
3073 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3074 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3075 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3076 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003077 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003078 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003079
Hanno Beckerf8787072019-05-16 12:41:07 +01003080static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003081 MBEDTLS_VERSION_MAJOR,
3082 MBEDTLS_VERSION_MINOR,
3083 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003084 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3085 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003086};
Hanno Beckera835da52019-05-16 12:39:07 +01003087
3088/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003089 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003090 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003091 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003092 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003093 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003094 * opaque mbedtls_version[3]; // library version: major, minor, patch
3095 * opaque session_format[2]; // library-version specific 16-bit field
3096 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003097 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003098 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003099 * Note: When updating the format, remember to keep
3100 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003101 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003102 * // In this version, `session_format` determines
3103 * // the setting of those compile-time
3104 * // configuration options which influence
3105 * // the structure of mbedtls_ssl_session.
3106 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003107 * uint8_t minor_ver; // Protocol minor version. Possible values:
3108 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003109 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003110 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003111 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003112 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003113 * serialized_session_tls12 data;
3114 *
3115 * };
3116 *
3117 * } serialized_session;
3118 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003119 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003120
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003121MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003122static int ssl_session_save( const mbedtls_ssl_session *session,
3123 unsigned char omit_header,
3124 unsigned char *buf,
3125 size_t buf_len,
3126 size_t *olen )
3127{
3128 unsigned char *p = buf;
3129 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003130 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003131#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3132 size_t out_len;
3133 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3134#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003135 if( session == NULL )
3136 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3137
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003138 if( !omit_header )
3139 {
3140 /*
3141 * Add Mbed TLS version identifier
3142 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003143 used += sizeof( ssl_serialized_session_header );
3144
3145 if( used <= buf_len )
3146 {
3147 memcpy( p, ssl_serialized_session_header,
3148 sizeof( ssl_serialized_session_header ) );
3149 p += sizeof( ssl_serialized_session_header );
3150 }
3151 }
3152
3153 /*
3154 * TLS version identifier
3155 */
3156 used += 1;
3157 if( used <= buf_len )
3158 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003159 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003160 }
3161
3162 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003163 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003164 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003165 {
3166#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003167 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003168 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003169 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003170#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3171
Jerry Yu251a12e2022-07-13 15:15:48 +08003172#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3173 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003174 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3175 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003176 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003177 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003178 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003179#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3180
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003181 default:
3182 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3183 }
3184
3185 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003186 if( used > buf_len )
3187 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003188
3189 return( 0 );
3190}
3191
3192/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003193 * Public wrapper for ssl_session_save()
3194 */
3195int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3196 unsigned char *buf,
3197 size_t buf_len,
3198 size_t *olen )
3199{
3200 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3201}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003202
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003203/*
3204 * Deserialize session, see mbedtls_ssl_session_save() for format.
3205 *
3206 * This internal version is wrapped by a public function that cleans up in
3207 * case of error, and has an extra option omit_header.
3208 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003209MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003210static int ssl_session_load( mbedtls_ssl_session *session,
3211 unsigned char omit_header,
3212 const unsigned char *buf,
3213 size_t len )
3214{
3215 const unsigned char *p = buf;
3216 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003217 size_t remaining_len;
3218
3219
3220 if( session == NULL )
3221 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003222
3223 if( !omit_header )
3224 {
3225 /*
3226 * Check Mbed TLS version identifier
3227 */
3228
3229 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3230 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3231
3232 if( memcmp( p, ssl_serialized_session_header,
3233 sizeof( ssl_serialized_session_header ) ) != 0 )
3234 {
3235 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3236 }
3237 p += sizeof( ssl_serialized_session_header );
3238 }
3239
3240 /*
3241 * TLS version identifier
3242 */
3243 if( 1 > (size_t)( end - p ) )
3244 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003245 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003246
3247 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003248 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003249 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003250 {
3251#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003252 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003253 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003254#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3255
Jerry Yu438ddd82022-07-07 06:55:50 +00003256#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3257 case MBEDTLS_SSL_VERSION_TLS1_3:
3258 return( ssl_tls13_session_load( session, p, remaining_len ) );
3259#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3260
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003261 default:
3262 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3263 }
3264}
3265
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003266/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003267 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003268 */
3269int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3270 const unsigned char *buf,
3271 size_t len )
3272{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003273 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003274
3275 if( ret != 0 )
3276 mbedtls_ssl_session_free( session );
3277
3278 return( ret );
3279}
3280
3281/*
Paul Bakker1961b702013-01-25 14:49:24 +01003282 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003283 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003284MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003285static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3286{
3287 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3288
Ronald Cron66dbf912022-02-02 15:33:46 +01003289 /*
3290 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003291 * that were written into the output buffer by the previous handshake step,
3292 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003293 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3294 * We proceed to the next handshake step only when all data from the
3295 * previous one have been sent to the peer, thus we make sure that this is
3296 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3297 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3298 * we have to wait before to go ahead.
3299 * In the case of TLS 1.3, handshake step handlers do not send data to the
3300 * peer. Data are only sent here and through
3301 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003302 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003303 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003304 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3305 return( ret );
3306
3307#if defined(MBEDTLS_SSL_PROTO_DTLS)
3308 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3309 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3310 {
3311 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3312 return( ret );
3313 }
3314#endif /* MBEDTLS_SSL_PROTO_DTLS */
3315
3316 return( ret );
3317}
3318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003320{
Hanno Becker41934dd2021-08-07 19:13:43 +01003321 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003322
Hanno Becker41934dd2021-08-07 19:13:43 +01003323 if( ssl == NULL ||
3324 ssl->conf == NULL ||
3325 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003326 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003327 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003328 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003329 }
3330
3331 ret = ssl_prepare_handshake_step( ssl );
3332 if( ret != 0 )
3333 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003334
Jerry Yue7047812021-09-13 19:26:39 +08003335 ret = mbedtls_ssl_handle_pending_alert( ssl );
3336 if( ret != 0 )
3337 goto cleanup;
3338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003340 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003341 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3343 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003344
Ronald Cron9f0fba32022-02-10 16:45:15 +01003345 switch( ssl->state )
3346 {
3347 case MBEDTLS_SSL_HELLO_REQUEST:
3348 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3349 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003350
Ronald Cron9f0fba32022-02-10 16:45:15 +01003351 case MBEDTLS_SSL_CLIENT_HELLO:
3352 ret = mbedtls_ssl_write_client_hello( ssl );
3353 break;
3354
3355 default:
3356#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003357 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003358 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3359 else
3360 ret = mbedtls_ssl_handshake_client_step( ssl );
3361#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3362 ret = mbedtls_ssl_handshake_client_step( ssl );
3363#else
3364 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3365#endif
3366 }
Jerry Yub9930e72021-08-06 17:11:51 +08003367 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003368#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003370 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003371 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003372#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003373 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003374 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003375#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003376
3377#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3378 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3379 ret = mbedtls_ssl_handshake_server_step( ssl );
3380#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3381 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003382#endif
3383
Jerry Yue7047812021-09-13 19:26:39 +08003384 if( ret != 0 )
3385 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003386 /* handshake_step return error. And it is same
3387 * with alert_reason.
3388 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003389 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003390 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003391 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003392 goto cleanup;
3393 }
3394 }
3395
3396cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003397 return( ret );
3398}
3399
3400/*
3401 * Perform the SSL handshake
3402 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003404{
3405 int ret = 0;
3406
Hanno Beckera817ea42020-10-20 15:20:23 +01003407 /* Sanity checks */
3408
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003409 if( ssl == NULL || ssl->conf == NULL )
3410 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3411
Hanno Beckera817ea42020-10-20 15:20:23 +01003412#if defined(MBEDTLS_SSL_PROTO_DTLS)
3413 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3414 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3415 {
3416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3417 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3418 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3419 }
3420#endif /* MBEDTLS_SSL_PROTO_DTLS */
3421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003423
Hanno Beckera817ea42020-10-20 15:20:23 +01003424 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003425 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003428
3429 if( ret != 0 )
3430 break;
3431 }
3432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003434
3435 return( ret );
3436}
3437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438#if defined(MBEDTLS_SSL_RENEGOTIATION)
3439#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003440/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003441 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003442 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003443MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003444static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003445{
Janos Follath865b3eb2019-12-16 11:46:15 +00003446 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003449
3450 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003451 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3452 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003453
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003454 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003455 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003457 return( ret );
3458 }
3459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003461
3462 return( 0 );
3463}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003464#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003465
3466/*
3467 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468 * - any side: calling mbedtls_ssl_renegotiate(),
3469 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3470 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003471 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003472 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003474 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003475int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003476{
Janos Follath865b3eb2019-12-16 11:46:15 +00003477 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003480
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003481 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3482 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003483
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003484 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3485 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003487 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003489 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003490 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003491 ssl->handshake->out_msg_seq = 1;
3492 else
3493 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003494 }
3495#endif
3496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003497 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3498 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003500 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003503 return( ret );
3504 }
3505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003507
3508 return( 0 );
3509}
3510
3511/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003512 * Renegotiate current connection on client,
3513 * or request renegotiation on server
3514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003515int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003516{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003517 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003518
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003519 if( ssl == NULL || ssl->conf == NULL )
3520 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003522#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003523 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003524 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003525 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003526 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003529 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003530
3531 /* Did we already try/start sending HelloRequest? */
3532 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003534
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003535 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003536 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003537#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003539#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003540 /*
3541 * On client, either start the renegotiation process or,
3542 * if already in progress, continue the handshake
3543 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003544 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003545 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003546 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003547 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003548
Hanno Becker40cdaa12020-02-05 10:48:27 +00003549 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003550 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003551 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003552 return( ret );
3553 }
3554 }
3555 else
3556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003557 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003559 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003560 return( ret );
3561 }
3562 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003564
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003565 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003566}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003568
Gilles Peskine9b562d52018-04-25 20:32:43 +02003569void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003570{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003571 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3572
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003573 if( handshake == NULL )
3574 return;
3575
Brett Warrene0edc842021-08-17 09:53:13 +01003576#if defined(MBEDTLS_ECP_C)
3577#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3578 if ( ssl->handshake->group_list_heap_allocated )
3579 mbedtls_free( (void*) handshake->group_list );
3580 handshake->group_list = NULL;
3581#endif /* MBEDTLS_DEPRECATED_REMOVED */
3582#endif /* MBEDTLS_ECP_C */
3583
Jerry Yuf017ee42022-01-12 15:49:48 +08003584#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3585#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3586 if ( ssl->handshake->sig_algs_heap_allocated )
3587 mbedtls_free( (void*) handshake->sig_algs );
3588 handshake->sig_algs = NULL;
3589#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003590#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3591 if( ssl->handshake->certificate_request_context )
3592 {
3593 mbedtls_free( (void*) handshake->certificate_request_context );
3594 }
3595#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003596#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3597
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003598#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3599 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3600 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003601 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003602 handshake->async_in_progress = 0;
3603 }
3604#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3605
Andrzej Kurek25f27152022-08-17 16:09:31 -04003606#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003607#if defined(MBEDTLS_USE_PSA_CRYPTO)
3608 psa_hash_abort( &handshake->fin_sha256_psa );
3609#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003610 mbedtls_sha256_free( &handshake->fin_sha256 );
3611#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003612#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003613#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003614#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003615 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003616#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003617 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003618#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003619#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621#if defined(MBEDTLS_DHM_C)
3622 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003623#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003624#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003626#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003627#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003628 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003629#if defined(MBEDTLS_SSL_CLI_C)
3630 mbedtls_free( handshake->ecjpake_cache );
3631 handshake->ecjpake_cache = NULL;
3632 handshake->ecjpake_cache_len = 0;
3633#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003634#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003635
Janos Follath4ae5c292016-02-10 11:27:43 +00003636#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3637 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003638 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003639 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003640#endif
3641
Gilles Peskineeccd8882020-03-10 12:19:08 +01003642#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003643#if defined(MBEDTLS_USE_PSA_CRYPTO)
3644 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3645 {
3646 /* The maintenance of the external PSK key slot is the
3647 * user's responsibility. */
3648 if( ssl->handshake->psk_opaque_is_internal )
3649 {
3650 psa_destroy_key( ssl->handshake->psk_opaque );
3651 ssl->handshake->psk_opaque_is_internal = 0;
3652 }
3653 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3654 }
Neil Armstronge952a302022-05-03 10:22:14 +02003655#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003656 if( handshake->psk != NULL )
3657 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003658 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003659 mbedtls_free( handshake->psk );
3660 }
Neil Armstronge952a302022-05-03 10:22:14 +02003661#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003662#endif
3663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3665 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003666 /*
3667 * Free only the linked list wrapper, not the keys themselves
3668 * since the belong to the SNI callback
3669 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003670 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003672
Gilles Peskineeccd8882020-03-10 12:19:08 +01003673#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003674 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003675 if( handshake->ecrs_peer_cert != NULL )
3676 {
3677 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3678 mbedtls_free( handshake->ecrs_peer_cert );
3679 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003680#endif
3681
Hanno Becker75173122019-02-06 16:18:31 +00003682#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3683 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3684 mbedtls_pk_free( &handshake->peer_pubkey );
3685#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3686
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003687#if defined(MBEDTLS_SSL_CLI_C) && \
3688 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3689 mbedtls_free( handshake->cookie );
3690#endif /* MBEDTLS_SSL_CLI_C &&
3691 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003692
3693#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003694 mbedtls_ssl_flight_free( handshake->flight );
3695 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003696#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003697
Ronald Cronf12b81d2022-03-15 10:42:41 +01003698#if defined(MBEDTLS_ECDH_C) && \
3699 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003700 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003701 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003702#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3703
Ronald Cron6f135e12021-12-08 16:57:54 +01003704#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003705 mbedtls_ssl_transform_free( handshake->transform_handshake );
3706 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003707 mbedtls_free( handshake->transform_earlydata );
3708 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003709#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003710
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003711
3712#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3713 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3714 * processes datagrams and the fact that a datagram is allowed to have
3715 * several records in it, it is possible that the I/O buffers are not
3716 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003717 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3718 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003719#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003720
Jerry Yuba9c7272021-10-30 11:54:10 +08003721 /* mbedtls_platform_zeroize MUST be last one in this function */
3722 mbedtls_platform_zeroize( handshake,
3723 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003724}
3725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003726void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003727{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003728 if( session == NULL )
3729 return;
3730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003731#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003732 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003733#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003734
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003735#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00003736#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
3737 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00003738 mbedtls_free( session->hostname );
3739#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003740 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003741#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003742
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003743 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003744}
3745
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003746#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003747
3748#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3749#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3750#else
3751#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3752#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3753
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003754#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003755
3756#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3757#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3758#else
3759#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3760#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3761
3762#if defined(MBEDTLS_SSL_ALPN)
3763#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3764#else
3765#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3766#endif /* MBEDTLS_SSL_ALPN */
3767
3768#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3769#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3770#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3771#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3772
3773#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3774 ( (uint32_t) ( \
3775 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3776 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3777 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3778 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3779 0u ) )
3780
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003781static unsigned char ssl_serialized_context_header[] = {
3782 MBEDTLS_VERSION_MAJOR,
3783 MBEDTLS_VERSION_MINOR,
3784 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003785 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3786 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3787 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3788 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3789 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003790};
3791
Paul Bakker5121ce52009-01-03 21:22:43 +00003792/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003793 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003794 *
3795 * The format of the serialized data is:
3796 * (in the presentation language of TLS, RFC 8446 section 3)
3797 *
3798 * // header
3799 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003800 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003801 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003802 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003803 * Note: When updating the format, remember to keep these
3804 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003805 *
3806 * // session sub-structure
3807 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3808 * // transform sub-structure
3809 * uint8 random[64]; // ServerHello.random+ClientHello.random
3810 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3811 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3812 * // fields from ssl_context
3813 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3814 * uint64 in_window_top; // DTLS: last validated record seq_num
3815 * uint64 in_window; // DTLS: bitmask for replay protection
3816 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3817 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3818 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3819 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3820 *
3821 * Note that many fields of the ssl_context or sub-structures are not
3822 * serialized, as they fall in one of the following categories:
3823 *
3824 * 1. forced value (eg in_left must be 0)
3825 * 2. pointer to dynamically-allocated memory (eg session, transform)
3826 * 3. value can be re-derived from other data (eg session keys from MS)
3827 * 4. value was temporary (eg content of input buffer)
3828 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003829 */
3830int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3831 unsigned char *buf,
3832 size_t buf_len,
3833 size_t *olen )
3834{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003835 unsigned char *p = buf;
3836 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003837 size_t session_len;
3838 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003839
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003840 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003841 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3842 * this function's documentation.
3843 *
3844 * These are due to assumptions/limitations in the implementation. Some of
3845 * them are likely to stay (no handshake in progress) some might go away
3846 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003847 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003848 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003849 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003850 {
3851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003852 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003853 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003854 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003855 {
3856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003857 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003858 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003859 /* Double-check that sub-structures are indeed ready */
3860 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003861 {
3862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003863 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003864 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003865 /* There must be no pending incoming or outgoing data */
3866 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003867 {
3868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003869 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003870 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003871 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003872 {
3873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003874 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003875 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003876 /* Protocol must be DLTS, not TLS */
3877 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003878 {
3879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003880 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003881 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003882 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003883 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003884 {
3885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003886 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003887 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003888 /* We must be using an AEAD ciphersuite */
3889 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003890 {
3891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003892 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003893 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003894 /* Renegotiation must not be enabled */
3895#if defined(MBEDTLS_SSL_RENEGOTIATION)
3896 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003897 {
3898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003899 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003900 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003901#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003902
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003903 /*
3904 * Version and format identifier
3905 */
3906 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003907
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003908 if( used <= buf_len )
3909 {
3910 memcpy( p, ssl_serialized_context_header,
3911 sizeof( ssl_serialized_context_header ) );
3912 p += sizeof( ssl_serialized_context_header );
3913 }
3914
3915 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003916 * Session (length + data)
3917 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003918 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003919 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3920 return( ret );
3921
3922 used += 4 + session_len;
3923 if( used <= buf_len )
3924 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003925 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3926 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003927
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003928 ret = ssl_session_save( ssl->session, 1,
3929 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003930 if( ret != 0 )
3931 return( ret );
3932
3933 p += session_len;
3934 }
3935
3936 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003937 * Transform
3938 */
3939 used += sizeof( ssl->transform->randbytes );
3940 if( used <= buf_len )
3941 {
3942 memcpy( p, ssl->transform->randbytes,
3943 sizeof( ssl->transform->randbytes ) );
3944 p += sizeof( ssl->transform->randbytes );
3945 }
3946
3947#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3948 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3949 if( used <= buf_len )
3950 {
3951 *p++ = ssl->transform->in_cid_len;
3952 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3953 p += ssl->transform->in_cid_len;
3954
3955 *p++ = ssl->transform->out_cid_len;
3956 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3957 p += ssl->transform->out_cid_len;
3958 }
3959#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3960
3961 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003962 * Saved fields from top-level ssl_context structure
3963 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003964 used += 4;
3965 if( used <= buf_len )
3966 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003967 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3968 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003969 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003970
3971#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3972 used += 16;
3973 if( used <= buf_len )
3974 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003975 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3976 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003977
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003978 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3979 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003980 }
3981#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3982
3983#if defined(MBEDTLS_SSL_PROTO_DTLS)
3984 used += 1;
3985 if( used <= buf_len )
3986 {
3987 *p++ = ssl->disable_datagram_packing;
3988 }
3989#endif /* MBEDTLS_SSL_PROTO_DTLS */
3990
Jerry Yuae0b2e22021-10-08 15:21:19 +08003991 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003992 if( used <= buf_len )
3993 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003994 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3995 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003996 }
3997
3998#if defined(MBEDTLS_SSL_PROTO_DTLS)
3999 used += 2;
4000 if( used <= buf_len )
4001 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004002 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4003 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004004 }
4005#endif /* MBEDTLS_SSL_PROTO_DTLS */
4006
4007#if defined(MBEDTLS_SSL_ALPN)
4008 {
4009 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004010 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004011 : 0;
4012
4013 used += 1 + alpn_len;
4014 if( used <= buf_len )
4015 {
4016 *p++ = alpn_len;
4017
4018 if( ssl->alpn_chosen != NULL )
4019 {
4020 memcpy( p, ssl->alpn_chosen, alpn_len );
4021 p += alpn_len;
4022 }
4023 }
4024 }
4025#endif /* MBEDTLS_SSL_ALPN */
4026
4027 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004028 * Done
4029 */
4030 *olen = used;
4031
4032 if( used > buf_len )
4033 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004034
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004035 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4036
Hanno Becker43aefe22020-02-05 10:44:56 +00004037 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004038}
4039
4040/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004041 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004042 *
4043 * This internal version is wrapped by a public function that cleans up in
4044 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004045 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004046MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004047static int ssl_context_load( mbedtls_ssl_context *ssl,
4048 const unsigned char *buf,
4049 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004050{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004051 const unsigned char *p = buf;
4052 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004053 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004054 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004055#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004056 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004057#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004058
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004059 /*
4060 * The context should have been freshly setup or reset.
4061 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004062 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004063 * renegotiating, or if the user mistakenly loaded a session first.)
4064 */
4065 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4066 ssl->session != NULL )
4067 {
4068 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4069 }
4070
4071 /*
4072 * We can't check that the config matches the initial one, but we can at
4073 * least check it matches the requirements for serializing.
4074 */
4075 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004076 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4077 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004078#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004079 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004080#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004081 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004082 {
4083 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4084 }
4085
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004086 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4087
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004088 /*
4089 * Check version identifier
4090 */
4091 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4092 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4093
4094 if( memcmp( p, ssl_serialized_context_header,
4095 sizeof( ssl_serialized_context_header ) ) != 0 )
4096 {
4097 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4098 }
4099 p += sizeof( ssl_serialized_context_header );
4100
4101 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004102 * Session
4103 */
4104 if( (size_t)( end - p ) < 4 )
4105 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4106
4107 session_len = ( (size_t) p[0] << 24 ) |
4108 ( (size_t) p[1] << 16 ) |
4109 ( (size_t) p[2] << 8 ) |
4110 ( (size_t) p[3] );
4111 p += 4;
4112
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004113 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004114 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004115 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004116 ssl->session_in = ssl->session;
4117 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004118 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004119
4120 if( (size_t)( end - p ) < session_len )
4121 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4122
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004123 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004124 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004125 {
4126 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004127 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004128 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004129
4130 p += session_len;
4131
4132 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004133 * Transform
4134 */
4135
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004136 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004137 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004138 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004139 ssl->transform_in = ssl->transform;
4140 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004141 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004142
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004143#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004144 prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite );
4145 if( prf_func == NULL )
4146 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4147
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004148 /* Read random bytes and populate structure */
4149 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4150 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004151
Hanno Beckerbd257552021-03-22 06:59:27 +00004152 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004153 ssl->session->ciphersuite,
4154 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004155#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004156 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004157#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Andrzej Kurek894edde2022-09-29 06:31:14 -04004158 prf_func,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004159 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004160 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004161 ssl->conf->endpoint,
4162 ssl );
4163 if( ret != 0 )
4164 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004165#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004166 p += sizeof( ssl->transform->randbytes );
4167
4168#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4169 /* Read connection IDs and store them */
4170 if( (size_t)( end - p ) < 1 )
4171 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4172
4173 ssl->transform->in_cid_len = *p++;
4174
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004175 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004176 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4177
4178 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4179 p += ssl->transform->in_cid_len;
4180
4181 ssl->transform->out_cid_len = *p++;
4182
4183 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4184 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4185
4186 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4187 p += ssl->transform->out_cid_len;
4188#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4189
4190 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004191 * Saved fields from top-level ssl_context structure
4192 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004193 if( (size_t)( end - p ) < 4 )
4194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4195
4196 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4197 ( (uint32_t) p[1] << 16 ) |
4198 ( (uint32_t) p[2] << 8 ) |
4199 ( (uint32_t) p[3] );
4200 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004201
4202#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4203 if( (size_t)( end - p ) < 16 )
4204 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4205
4206 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4207 ( (uint64_t) p[1] << 48 ) |
4208 ( (uint64_t) p[2] << 40 ) |
4209 ( (uint64_t) p[3] << 32 ) |
4210 ( (uint64_t) p[4] << 24 ) |
4211 ( (uint64_t) p[5] << 16 ) |
4212 ( (uint64_t) p[6] << 8 ) |
4213 ( (uint64_t) p[7] );
4214 p += 8;
4215
4216 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4217 ( (uint64_t) p[1] << 48 ) |
4218 ( (uint64_t) p[2] << 40 ) |
4219 ( (uint64_t) p[3] << 32 ) |
4220 ( (uint64_t) p[4] << 24 ) |
4221 ( (uint64_t) p[5] << 16 ) |
4222 ( (uint64_t) p[6] << 8 ) |
4223 ( (uint64_t) p[7] );
4224 p += 8;
4225#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4226
4227#if defined(MBEDTLS_SSL_PROTO_DTLS)
4228 if( (size_t)( end - p ) < 1 )
4229 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4230
4231 ssl->disable_datagram_packing = *p++;
4232#endif /* MBEDTLS_SSL_PROTO_DTLS */
4233
Jerry Yud9a94fe2021-09-28 18:58:59 +08004234 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004235 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004236 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4237 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004238
4239#if defined(MBEDTLS_SSL_PROTO_DTLS)
4240 if( (size_t)( end - p ) < 2 )
4241 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4242
4243 ssl->mtu = ( p[0] << 8 ) | p[1];
4244 p += 2;
4245#endif /* MBEDTLS_SSL_PROTO_DTLS */
4246
4247#if defined(MBEDTLS_SSL_ALPN)
4248 {
4249 uint8_t alpn_len;
4250 const char **cur;
4251
4252 if( (size_t)( end - p ) < 1 )
4253 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4254
4255 alpn_len = *p++;
4256
4257 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4258 {
4259 /* alpn_chosen should point to an item in the configured list */
4260 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4261 {
4262 if( strlen( *cur ) == alpn_len &&
4263 memcmp( p, cur, alpn_len ) == 0 )
4264 {
4265 ssl->alpn_chosen = *cur;
4266 break;
4267 }
4268 }
4269 }
4270
4271 /* can only happen on conf mismatch */
4272 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4273 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4274
4275 p += alpn_len;
4276 }
4277#endif /* MBEDTLS_SSL_ALPN */
4278
4279 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004280 * Forced fields from top-level ssl_context structure
4281 *
4282 * Most of them already set to the correct value by mbedtls_ssl_init() and
4283 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4284 */
4285 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004286 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004287
Hanno Becker361b10d2019-08-30 10:42:49 +01004288 /* Adjust pointers for header fields of outgoing records to
4289 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004290 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004291
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004292#if defined(MBEDTLS_SSL_PROTO_DTLS)
4293 ssl->in_epoch = 1;
4294#endif
4295
4296 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4297 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004298 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4299 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004300 if( ssl->handshake != NULL )
4301 {
4302 mbedtls_ssl_handshake_free( ssl );
4303 mbedtls_free( ssl->handshake );
4304 ssl->handshake = NULL;
4305 }
4306
4307 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004308 * Done - should have consumed entire buffer
4309 */
4310 if( p != end )
4311 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004312
4313 return( 0 );
4314}
4315
4316/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004317 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004318 */
4319int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4320 const unsigned char *buf,
4321 size_t len )
4322{
4323 int ret = ssl_context_load( context, buf, len );
4324
4325 if( ret != 0 )
4326 mbedtls_ssl_free( context );
4327
4328 return( ret );
4329}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004330#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004331
4332/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004333 * Free an SSL context
4334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004335void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004336{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004337 if( ssl == NULL )
4338 return;
4339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004341
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004342 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004343 {
sander-visserb8aa2072020-05-06 22:05:13 +02004344#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4345 size_t out_buf_len = ssl->out_buf_len;
4346#else
4347 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4348#endif
4349
Darryl Greenb33cc762019-11-28 14:29:44 +00004350 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004351 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004352 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004353 }
4354
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004355 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004356 {
sander-visserb8aa2072020-05-06 22:05:13 +02004357#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4358 size_t in_buf_len = ssl->in_buf_len;
4359#else
4360 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4361#endif
4362
Darryl Greenb33cc762019-11-28 14:29:44 +00004363 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004364 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004365 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004366 }
4367
Paul Bakker48916f92012-09-16 19:57:18 +00004368 if( ssl->transform )
4369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004370 mbedtls_ssl_transform_free( ssl->transform );
4371 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004372 }
4373
4374 if( ssl->handshake )
4375 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004376 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004377 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4378 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004380 mbedtls_free( ssl->handshake );
4381 mbedtls_free( ssl->transform_negotiate );
4382 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004383 }
4384
Ronald Cron6f135e12021-12-08 16:57:54 +01004385#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004386 mbedtls_ssl_transform_free( ssl->transform_application );
4387 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004388#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004389
Paul Bakkerc0463502013-02-14 11:19:38 +01004390 if( ssl->session )
4391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004392 mbedtls_ssl_session_free( ssl->session );
4393 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004394 }
4395
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004396#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004397 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004398 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004399 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004400 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004401 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004402#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004403
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004404#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004405 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004406#endif
4407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004408 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004409
Paul Bakker86f04f42013-02-14 11:20:09 +01004410 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004411 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004412}
4413
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004414/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004415 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004416 */
4417void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4418{
4419 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4420}
4421
Gilles Peskineae270bf2021-06-02 00:05:29 +02004422/* The selection should be the same as mbedtls_x509_crt_profile_default in
4423 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004424 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004425 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004426 * about this list.
4427 */
Brett Warrene0edc842021-08-17 09:53:13 +01004428static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004429#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004430 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004431#endif
4432#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004433 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004434#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004435#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004436 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004437#endif
4438#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004439 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004440#endif
4441#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004442 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004443#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004444#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004445 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004446#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004447#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004448 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004449#endif
4450#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004451 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004452#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004453 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004454};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004455
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004456static int ssl_preset_suiteb_ciphersuites[] = {
4457 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4458 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4459 0
4460};
4461
Gilles Peskineeccd8882020-03-10 12:19:08 +01004462#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004463
Jerry Yu909df7b2022-01-22 11:56:27 +08004464/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004465 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004466 * rules SHOULD be upheld.
4467 * - No duplicate entries.
4468 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004469 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004470 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004471 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004472static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004473
Andrzej Kurek25f27152022-08-17 16:09:31 -04004474#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 +08004475 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4476 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004477#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004478 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004479
Andrzej Kurek25f27152022-08-17 16:09:31 -04004480#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 +08004481 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4482 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004483#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004484 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4485
Andrzej Kurek25f27152022-08-17 16:09:31 -04004486#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 +08004487 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4488 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004489#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004490 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004491
Andrzej Kurek25f27152022-08-17 16:09:31 -04004492#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 +08004493 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004494#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 +08004495
Andrzej Kurek25f27152022-08-17 16:09:31 -04004496#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 +08004497 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004498#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 +08004499
Andrzej Kurek25f27152022-08-17 16:09:31 -04004500#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 +08004501 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004502#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 +08004503
Andrzej Kurek25f27152022-08-17 16:09:31 -04004504#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 +08004505 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4506#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4507
Andrzej Kurek25f27152022-08-17 16:09:31 -04004508#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 +08004509 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4510#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4511
Andrzej Kurek25f27152022-08-17 16:09:31 -04004512#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 +08004513 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4514#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4515
Gabor Mezei15b95a62022-05-09 16:37:58 +02004516 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004517};
4518
4519/* NOTICE: see above */
4520#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4521static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004522#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004523#if defined(MBEDTLS_ECDSA_C)
4524 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004525#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004526#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4527 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4528#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004529#if defined(MBEDTLS_RSA_C)
4530 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4531#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004532#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004533#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004534#if defined(MBEDTLS_ECDSA_C)
4535 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004536#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004537#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4538 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4539#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004540#if defined(MBEDTLS_RSA_C)
4541 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4542#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004543#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004544#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004545#if defined(MBEDTLS_ECDSA_C)
4546 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004547#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004548#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4549 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4550#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004551#if defined(MBEDTLS_RSA_C)
4552 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4553#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004554#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004555 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004556};
4557#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4558/* NOTICE: see above */
4559static uint16_t ssl_preset_suiteb_sig_algs[] = {
4560
Andrzej Kurek25f27152022-08-17 16:09:31 -04004561#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 +08004562 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4563 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004564#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004565 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4566
Andrzej Kurek25f27152022-08-17 16:09:31 -04004567#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 +08004568 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4569 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004570#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004571 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004572
Andrzej Kurek25f27152022-08-17 16:09:31 -04004573#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 +08004574 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004575#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 +08004576
Andrzej Kurek25f27152022-08-17 16:09:31 -04004577#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 +08004578 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004579#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004580
Gabor Mezei15b95a62022-05-09 16:37:58 +02004581 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004582};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004583
Jerry Yu909df7b2022-01-22 11:56:27 +08004584/* NOTICE: see above */
4585#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4586static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004587#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004588#if defined(MBEDTLS_ECDSA_C)
4589 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004590#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004591#if defined(MBEDTLS_RSA_C)
4592 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4593#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004594#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004595#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004596#if defined(MBEDTLS_ECDSA_C)
4597 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004598#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004599#if defined(MBEDTLS_RSA_C)
4600 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4601#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004602#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004603 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004604};
Jerry Yu909df7b2022-01-22 11:56:27 +08004605#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4606
Jerry Yu1a8b4812022-01-20 17:56:50 +08004607#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004608
Brett Warrene0edc842021-08-17 09:53:13 +01004609static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004610#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004611 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004612#endif
4613#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004614 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004615#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004616 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004617};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004618
Jerry Yu1a8b4812022-01-20 17:56:50 +08004619#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004620/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004621 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004622MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004623static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004624{
4625 size_t i, j;
4626 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004627
Gabor Mezei15b95a62022-05-09 16:37:58 +02004628 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004629 {
Jerry Yuf377d642022-01-25 10:43:59 +08004630 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004631 {
Jerry Yuf377d642022-01-25 10:43:59 +08004632 if( sig_algs[i] != sig_algs[j] )
4633 continue;
4634 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4635 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4636 sig_algs[i], j, i );
4637 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004638 }
4639 }
4640 return( ret );
4641}
4642
4643#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4644
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004645/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004646 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004647 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004648int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004649 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004650{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004651#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004652 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004653#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004654
Jerry Yu1a8b4812022-01-20 17:56:50 +08004655#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004656 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004657 {
4658 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4659 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4660 }
4661
Jerry Yuf377d642022-01-25 10:43:59 +08004662 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004663 {
4664 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4665 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4666 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004667
4668#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004669 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004670 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004671 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004672 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4673 }
4674
Jerry Yuf377d642022-01-25 10:43:59 +08004675 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004676 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004677 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004678 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4679 }
4680#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004681#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4682
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004683 /* Use the functions here so that they are covered in tests,
4684 * but otherwise access member directly for efficiency */
4685 mbedtls_ssl_conf_endpoint( conf, endpoint );
4686 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004687
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004688 /*
4689 * Things that are common to all presets
4690 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004691#if defined(MBEDTLS_SSL_CLI_C)
4692 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4693 {
4694 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4695#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4696 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4697#endif
4698 }
4699#endif
4700
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004701#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4702 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4703#endif
4704
4705#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4706 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4707#endif
4708
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004709#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004710 conf->f_cookie_write = ssl_cookie_write_dummy;
4711 conf->f_cookie_check = ssl_cookie_check_dummy;
4712#endif
4713
4714#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4715 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4716#endif
4717
Janos Follath088ce432017-04-10 12:42:31 +01004718#if defined(MBEDTLS_SSL_SRV_C)
4719 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004720 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004721#endif
4722
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004723#if defined(MBEDTLS_SSL_PROTO_DTLS)
4724 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4725 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4726#endif
4727
4728#if defined(MBEDTLS_SSL_RENEGOTIATION)
4729 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004730 memset( conf->renego_period, 0x00, 2 );
4731 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004732#endif
4733
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004734#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004735 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4736 {
4737 const unsigned char dhm_p[] =
4738 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4739 const unsigned char dhm_g[] =
4740 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004741
Hanno Beckere2defad2021-07-24 05:59:17 +01004742 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4743 dhm_p, sizeof( dhm_p ),
4744 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4745 {
4746 return( ret );
4747 }
4748 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004749#endif
4750
Ronald Cron6f135e12021-12-08 16:57:54 +01004751#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004752#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004753 mbedtls_ssl_conf_new_session_tickets(
4754 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4755#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004756 /*
4757 * Allow all TLS 1.3 key exchange modes by default.
4758 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004759 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004760#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004761
XiaokangQian4d3a6042022-04-21 13:46:17 +00004762 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4763 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004764#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004765 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004766 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004767#else
XiaokangQian060d8672022-04-21 09:24:56 +00004768 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004769#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004770 }
4771 else
4772 {
4773#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4774 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4775 {
4776 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4777 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4778 }
4779 else
4780 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4781 {
4782 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4783 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4784 }
4785#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4786 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4787 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4788#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4789 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4790 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4791#else
4792 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4793#endif
4794 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004795
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004796 /*
4797 * Preset-specific defaults
4798 */
4799 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004800 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004801 /*
4802 * NSA Suite B
4803 */
4804 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004805
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004806 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004807
4808#if defined(MBEDTLS_X509_CRT_PARSE_C)
4809 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004810#endif
4811
Gilles Peskineeccd8882020-03-10 12:19:08 +01004812#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004813#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4814 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4815 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4816 else
4817#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4818 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004819#endif
4820
Brett Warrene0edc842021-08-17 09:53:13 +01004821#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4822 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004823#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004824 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004825 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004826
4827 /*
4828 * Default
4829 */
4830 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004831
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004832 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004833
4834#if defined(MBEDTLS_X509_CRT_PARSE_C)
4835 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4836#endif
4837
Gilles Peskineeccd8882020-03-10 12:19:08 +01004838#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004839#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4840 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4841 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4842 else
4843#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4844 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004845#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004846
Brett Warrene0edc842021-08-17 09:53:13 +01004847#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4848 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004849#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004850 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004851
4852#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4853 conf->dhm_min_bitlen = 1024;
4854#endif
4855 }
4856
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004857 return( 0 );
4858}
4859
4860/*
4861 * Free mbedtls_ssl_config
4862 */
4863void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4864{
4865#if defined(MBEDTLS_DHM_C)
4866 mbedtls_mpi_free( &conf->dhm_P );
4867 mbedtls_mpi_free( &conf->dhm_G );
4868#endif
4869
Gilles Peskineeccd8882020-03-10 12:19:08 +01004870#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004871#if defined(MBEDTLS_USE_PSA_CRYPTO)
4872 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4873 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004874 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4875 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004876#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004877 if( conf->psk != NULL )
4878 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004879 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004880 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004881 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004882 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004883 }
4884
4885 if( conf->psk_identity != NULL )
4886 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004887 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004888 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004889 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004890 conf->psk_identity_len = 0;
4891 }
4892#endif
4893
4894#if defined(MBEDTLS_X509_CRT_PARSE_C)
4895 ssl_key_cert_free( conf->key_cert );
4896#endif
4897
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004898 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004899}
4900
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004901#if defined(MBEDTLS_PK_C) && \
4902 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004903/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004904 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004905 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004906unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004907{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004908#if defined(MBEDTLS_RSA_C)
4909 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4910 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004911#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004912#if defined(MBEDTLS_ECDSA_C)
4913 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4914 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004915#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004916 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004917}
4918
Hanno Becker7e5437a2017-04-28 17:15:26 +01004919unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4920{
4921 switch( type ) {
4922 case MBEDTLS_PK_RSA:
4923 return( MBEDTLS_SSL_SIG_RSA );
4924 case MBEDTLS_PK_ECDSA:
4925 case MBEDTLS_PK_ECKEY:
4926 return( MBEDTLS_SSL_SIG_ECDSA );
4927 default:
4928 return( MBEDTLS_SSL_SIG_ANON );
4929 }
4930}
4931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004932mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004933{
4934 switch( sig )
4935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004936#if defined(MBEDTLS_RSA_C)
4937 case MBEDTLS_SSL_SIG_RSA:
4938 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004939#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004940#if defined(MBEDTLS_ECDSA_C)
4941 case MBEDTLS_SSL_SIG_ECDSA:
4942 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004943#endif
4944 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004945 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004946 }
4947}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004948#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004949
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004950/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004951 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004952 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004953mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004954{
4955 switch( hash )
4956 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004957#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004958 case MBEDTLS_SSL_HASH_MD5:
4959 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004960#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004961#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004962 case MBEDTLS_SSL_HASH_SHA1:
4963 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004964#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004965#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004966 case MBEDTLS_SSL_HASH_SHA224:
4967 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004968#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004969#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004970 case MBEDTLS_SSL_HASH_SHA256:
4971 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004972#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004973#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004974 case MBEDTLS_SSL_HASH_SHA384:
4975 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004976#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004977#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004978 case MBEDTLS_SSL_HASH_SHA512:
4979 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004980#endif
4981 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004982 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004983 }
4984}
4985
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004986/*
4987 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4988 */
4989unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4990{
4991 switch( md )
4992 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004993#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004994 case MBEDTLS_MD_MD5:
4995 return( MBEDTLS_SSL_HASH_MD5 );
4996#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004997#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004998 case MBEDTLS_MD_SHA1:
4999 return( MBEDTLS_SSL_HASH_SHA1 );
5000#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005001#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005002 case MBEDTLS_MD_SHA224:
5003 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005004#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005005#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005006 case MBEDTLS_MD_SHA256:
5007 return( MBEDTLS_SSL_HASH_SHA256 );
5008#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005009#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005010 case MBEDTLS_MD_SHA384:
5011 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005012#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005013#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005014 case MBEDTLS_MD_SHA512:
5015 return( MBEDTLS_SSL_HASH_SHA512 );
5016#endif
5017 default:
5018 return( MBEDTLS_SSL_HASH_NONE );
5019 }
5020}
5021
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005022/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005023 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005024 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005025 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005026int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005027{
Brett Warrene0edc842021-08-17 09:53:13 +01005028 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005029
Brett Warrene0edc842021-08-17 09:53:13 +01005030 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005031 return( -1 );
5032
Brett Warrene0edc842021-08-17 09:53:13 +01005033 for( ; *group_list != 0; group_list++ )
5034 {
5035 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005036 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005037 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005038
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005039 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005040}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005041
5042#if defined(MBEDTLS_ECP_C)
5043/*
5044 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5045 */
5046int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5047{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005048 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005049 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005050
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005051 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005052 return -1;
5053
5054 uint16_t tls_id = grp_info->tls_id;
5055
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005056 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5057}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005058#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005060#if defined(MBEDTLS_X509_CRT_PARSE_C)
5061int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5062 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005063 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005064 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005065{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005066 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005067 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005068 const char *ext_oid;
5069 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005071 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005072 {
5073 /* Server part of the key exchange */
5074 switch( ciphersuite->key_exchange )
5075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005076 case MBEDTLS_KEY_EXCHANGE_RSA:
5077 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005078 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005079 break;
5080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005081 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5082 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5083 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5084 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005085 break;
5086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005087 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5088 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005089 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005090 break;
5091
5092 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005093 case MBEDTLS_KEY_EXCHANGE_NONE:
5094 case MBEDTLS_KEY_EXCHANGE_PSK:
5095 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5096 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005097 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005098 usage = 0;
5099 }
5100 }
5101 else
5102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005103 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5104 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005105 }
5106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005107 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005108 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005109 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005110 ret = -1;
5111 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005113 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005115 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5116 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005117 }
5118 else
5119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005120 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5121 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005122 }
5123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005124 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005125 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005126 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005127 ret = -1;
5128 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005129
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005130 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005131}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005132#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005133
Jerry Yu148165c2021-09-24 23:20:59 +08005134#if defined(MBEDTLS_USE_PSA_CRYPTO)
5135int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5136 const mbedtls_md_type_t md,
5137 unsigned char *dst,
5138 size_t dst_len,
5139 size_t *olen )
5140{
Ronald Cronf6893e12022-01-07 22:09:01 +01005141 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5142 psa_hash_operation_t *hash_operation_to_clone;
5143 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5144
Jerry Yu148165c2021-09-24 23:20:59 +08005145 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005146
5147 switch( md )
5148 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005149#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005150 case MBEDTLS_MD_SHA384:
5151 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5152 break;
5153#endif
5154
Andrzej Kurek25f27152022-08-17 16:09:31 -04005155#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005156 case MBEDTLS_MD_SHA256:
5157 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5158 break;
5159#endif
5160
5161 default:
5162 goto exit;
5163 }
5164
5165 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5166 if( status != PSA_SUCCESS )
5167 goto exit;
5168
5169 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5170 if( status != PSA_SUCCESS )
5171 goto exit;
5172
5173exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005174#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5175 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5176 (void) ssl;
5177#endif
Ronald Cronb788c042022-02-15 09:14:15 +01005178 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005179}
5180#else /* MBEDTLS_USE_PSA_CRYPTO */
5181
Andrzej Kurek25f27152022-08-17 16:09:31 -04005182#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005183MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005184static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5185 unsigned char *dst,
5186 size_t dst_len,
5187 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005188{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005189 int ret;
5190 mbedtls_sha512_context sha512;
5191
5192 if( dst_len < 48 )
5193 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5194
5195 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005196 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005197
5198 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5199 {
5200 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5201 goto exit;
5202 }
5203
5204 *olen = 48;
5205
5206exit:
5207
5208 mbedtls_sha512_free( &sha512 );
5209 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005210}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005211#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005212
Andrzej Kurek25f27152022-08-17 16:09:31 -04005213#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005214MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005215static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5216 unsigned char *dst,
5217 size_t dst_len,
5218 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005219{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005220 int ret;
5221 mbedtls_sha256_context sha256;
5222
5223 if( dst_len < 32 )
5224 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5225
5226 mbedtls_sha256_init( &sha256 );
5227 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005228
Jerry Yu24c0ec32021-09-09 14:21:07 +08005229 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5230 {
5231 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5232 goto exit;
5233 }
5234
5235 *olen = 32;
5236
5237exit:
5238
5239 mbedtls_sha256_free( &sha256 );
5240 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005241}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005242#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005243
Jerry Yu000f9762021-09-14 11:12:51 +08005244int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5245 const mbedtls_md_type_t md,
5246 unsigned char *dst,
5247 size_t dst_len,
5248 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005249{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005250 switch( md )
5251 {
5252
Andrzej Kurek25f27152022-08-17 16:09:31 -04005253#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005254 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005255 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005256#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005257
Andrzej Kurek25f27152022-08-17 16:09:31 -04005258#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005259 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005260 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005261#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005262
5263 default:
5264 break;
5265 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005266 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005267}
XiaokangQian647719a2021-12-07 09:16:29 +00005268
Jerry Yu148165c2021-09-24 23:20:59 +08005269#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005270
Gabor Mezei078e8032022-04-27 21:17:56 +02005271#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5272/* mbedtls_ssl_parse_sig_alg_ext()
5273 *
5274 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5275 * value (TLS 1.3 RFC8446):
5276 * enum {
5277 * ....
5278 * ecdsa_secp256r1_sha256( 0x0403 ),
5279 * ecdsa_secp384r1_sha384( 0x0503 ),
5280 * ecdsa_secp521r1_sha512( 0x0603 ),
5281 * ....
5282 * } SignatureScheme;
5283 *
5284 * struct {
5285 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5286 * } SignatureSchemeList;
5287 *
5288 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5289 * value (TLS 1.2 RFC5246):
5290 * enum {
5291 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5292 * sha512(6), (255)
5293 * } HashAlgorithm;
5294 *
5295 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5296 * SignatureAlgorithm;
5297 *
5298 * struct {
5299 * HashAlgorithm hash;
5300 * SignatureAlgorithm signature;
5301 * } SignatureAndHashAlgorithm;
5302 *
5303 * SignatureAndHashAlgorithm
5304 * supported_signature_algorithms<2..2^16-2>;
5305 *
5306 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5307 * generalization of the TLS 1.2 signature algorithm extension.
5308 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5309 * `SignatureScheme` field of TLS 1.3
5310 *
5311 */
5312int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5313 const unsigned char *buf,
5314 const unsigned char *end )
5315{
5316 const unsigned char *p = buf;
5317 size_t supported_sig_algs_len = 0;
5318 const unsigned char *supported_sig_algs_end;
5319 uint16_t sig_alg;
5320 uint32_t common_idx = 0;
5321
5322 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5323 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5324 p += 2;
5325
5326 memset( ssl->handshake->received_sig_algs, 0,
5327 sizeof(ssl->handshake->received_sig_algs) );
5328
5329 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5330 supported_sig_algs_end = p + supported_sig_algs_len;
5331 while( p < supported_sig_algs_end )
5332 {
5333 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5334 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5335 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005336 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5337 sig_alg,
5338 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005339#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005340 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005341 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5342 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5343 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005344 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005345 }
5346#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005347
Jerry Yuf3b46b52022-06-19 16:52:27 +08005348 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5349 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005350
5351 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5352 {
5353 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5354 common_idx += 1;
5355 }
5356 }
5357 /* Check that we consumed all the message. */
5358 if( p != end )
5359 {
5360 MBEDTLS_SSL_DEBUG_MSG( 1,
5361 ( "Signature algorithms extension length misaligned" ) );
5362 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5363 MBEDTLS_ERR_SSL_DECODE_ERROR );
5364 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5365 }
5366
5367 if( common_idx == 0 )
5368 {
5369 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5370 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5371 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5372 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5373 }
5374
Gabor Mezei15b95a62022-05-09 16:37:58 +02005375 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005376 return( 0 );
5377}
5378
5379#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5380
Jerry Yudc7bd172022-02-17 13:44:15 +08005381#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5382
5383#if defined(MBEDTLS_USE_PSA_CRYPTO)
5384
5385static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5386 mbedtls_svc_key_id_t key,
5387 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005388 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005389 const unsigned char* seed, size_t seed_length,
5390 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005391 const unsigned char* other_secret,
5392 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005393 size_t capacity )
5394{
5395 psa_status_t status;
5396
5397 status = psa_key_derivation_setup( derivation, alg );
5398 if( status != PSA_SUCCESS )
5399 return( status );
5400
5401 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5402 {
5403 status = psa_key_derivation_input_bytes( derivation,
5404 PSA_KEY_DERIVATION_INPUT_SEED,
5405 seed, seed_length );
5406 if( status != PSA_SUCCESS )
5407 return( status );
5408
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005409 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005410 {
5411 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005412 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5413 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005414 if( status != PSA_SUCCESS )
5415 return( status );
5416 }
5417
Jerry Yudc7bd172022-02-17 13:44:15 +08005418 if( mbedtls_svc_key_id_is_null( key ) )
5419 {
5420 status = psa_key_derivation_input_bytes(
5421 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005422 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005423 }
5424 else
5425 {
5426 status = psa_key_derivation_input_key(
5427 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5428 }
5429 if( status != PSA_SUCCESS )
5430 return( status );
5431
5432 status = psa_key_derivation_input_bytes( derivation,
5433 PSA_KEY_DERIVATION_INPUT_LABEL,
5434 label, label_length );
5435 if( status != PSA_SUCCESS )
5436 return( status );
5437 }
5438 else
5439 {
5440 return( PSA_ERROR_NOT_SUPPORTED );
5441 }
5442
5443 status = psa_key_derivation_set_capacity( derivation, capacity );
5444 if( status != PSA_SUCCESS )
5445 return( status );
5446
5447 return( PSA_SUCCESS );
5448}
5449
Andrzej Kurek57d10632022-10-24 10:32:01 -04005450#if defined(PSA_WANT_ALG_SHA_384) || \
5451 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005452MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005453static int tls_prf_generic( mbedtls_md_type_t md_type,
5454 const unsigned char *secret, size_t slen,
5455 const char *label,
5456 const unsigned char *random, size_t rlen,
5457 unsigned char *dstbuf, size_t dlen )
5458{
5459 psa_status_t status;
5460 psa_algorithm_t alg;
5461 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5462 psa_key_derivation_operation_t derivation =
5463 PSA_KEY_DERIVATION_OPERATION_INIT;
5464
5465 if( md_type == MBEDTLS_MD_SHA384 )
5466 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5467 else
5468 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5469
5470 /* Normally a "secret" should be long enough to be impossible to
5471 * find by brute force, and in particular should not be empty. But
5472 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5473 * and for this use case it makes sense to have a 0-length "secret".
5474 * Since the key API doesn't allow importing a key of length 0,
5475 * keep master_key=0, which setup_psa_key_derivation() understands
5476 * to mean a 0-length "secret" input. */
5477 if( slen != 0 )
5478 {
5479 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5480 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5481 psa_set_key_algorithm( &key_attributes, alg );
5482 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5483
5484 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5485 if( status != PSA_SUCCESS )
5486 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5487 }
5488
5489 status = setup_psa_key_derivation( &derivation,
5490 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005491 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005492 random, rlen,
5493 (unsigned char const *) label,
5494 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005495 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005496 dlen );
5497 if( status != PSA_SUCCESS )
5498 {
5499 psa_key_derivation_abort( &derivation );
5500 psa_destroy_key( master_key );
5501 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5502 }
5503
5504 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5505 if( status != PSA_SUCCESS )
5506 {
5507 psa_key_derivation_abort( &derivation );
5508 psa_destroy_key( master_key );
5509 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5510 }
5511
5512 status = psa_key_derivation_abort( &derivation );
5513 if( status != PSA_SUCCESS )
5514 {
5515 psa_destroy_key( master_key );
5516 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5517 }
5518
5519 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5520 status = psa_destroy_key( master_key );
5521 if( status != PSA_SUCCESS )
5522 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5523
5524 return( 0 );
5525}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005526#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08005527#else /* MBEDTLS_USE_PSA_CRYPTO */
5528
Andrzej Kurek57d10632022-10-24 10:32:01 -04005529#if defined(MBEDTLS_MD_C) && \
5530 ( defined(MBEDTLS_SHA256_C) || \
5531 defined(MBEDTLS_SHA384_C) )
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005532MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005533static int tls_prf_generic( mbedtls_md_type_t md_type,
5534 const unsigned char *secret, size_t slen,
5535 const char *label,
5536 const unsigned char *random, size_t rlen,
5537 unsigned char *dstbuf, size_t dlen )
5538{
5539 size_t nb;
5540 size_t i, j, k, md_len;
5541 unsigned char *tmp;
5542 size_t tmp_len = 0;
5543 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5544 const mbedtls_md_info_t *md_info;
5545 mbedtls_md_context_t md_ctx;
5546 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5547
5548 mbedtls_md_init( &md_ctx );
5549
5550 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5551 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5552
5553 md_len = mbedtls_md_get_size( md_info );
5554
5555 tmp_len = md_len + strlen( label ) + rlen;
5556 tmp = mbedtls_calloc( 1, tmp_len );
5557 if( tmp == NULL )
5558 {
5559 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5560 goto exit;
5561 }
5562
5563 nb = strlen( label );
5564 memcpy( tmp + md_len, label, nb );
5565 memcpy( tmp + md_len + nb, random, rlen );
5566 nb += rlen;
5567
5568 /*
5569 * Compute P_<hash>(secret, label + random)[0..dlen]
5570 */
5571 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5572 goto exit;
5573
5574 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5575 if( ret != 0 )
5576 goto exit;
5577 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5578 if( ret != 0 )
5579 goto exit;
5580 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5581 if( ret != 0 )
5582 goto exit;
5583
5584 for( i = 0; i < dlen; i += md_len )
5585 {
5586 ret = mbedtls_md_hmac_reset ( &md_ctx );
5587 if( ret != 0 )
5588 goto exit;
5589 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5590 if( ret != 0 )
5591 goto exit;
5592 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5593 if( ret != 0 )
5594 goto exit;
5595
5596 ret = mbedtls_md_hmac_reset ( &md_ctx );
5597 if( ret != 0 )
5598 goto exit;
5599 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5600 if( ret != 0 )
5601 goto exit;
5602 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5603 if( ret != 0 )
5604 goto exit;
5605
5606 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5607
5608 for( j = 0; j < k; j++ )
5609 dstbuf[i + j] = h_i[j];
5610 }
5611
5612exit:
5613 mbedtls_md_free( &md_ctx );
5614
5615 mbedtls_platform_zeroize( tmp, tmp_len );
5616 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5617
5618 mbedtls_free( tmp );
5619
5620 return( ret );
5621}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005622#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08005623#endif /* MBEDTLS_USE_PSA_CRYPTO */
5624
Andrzej Kurek25f27152022-08-17 16:09:31 -04005625#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005626MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005627static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5628 const char *label,
5629 const unsigned char *random, size_t rlen,
5630 unsigned char *dstbuf, size_t dlen )
5631{
5632 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5633 label, random, rlen, dstbuf, dlen ) );
5634}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005635#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005636
Andrzej Kurek25f27152022-08-17 16:09:31 -04005637#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005638MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005639static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5640 const char *label,
5641 const unsigned char *random, size_t rlen,
5642 unsigned char *dstbuf, size_t dlen )
5643{
5644 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5645 label, random, rlen, dstbuf, dlen ) );
5646}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005647#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005648
Jerry Yuf009d862022-02-17 14:01:37 +08005649/*
5650 * Set appropriate PRF function and other SSL / TLS1.2 functions
5651 *
5652 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005653 * - hash associated with the ciphersuite (only used by TLS 1.2)
5654 *
5655 * Outputs:
5656 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5657 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005658MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005659static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005660 mbedtls_md_type_t hash )
5661{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005662#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005663 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005664 {
5665 handshake->tls_prf = tls_prf_sha384;
5666 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5667 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5668 }
5669 else
5670#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005671#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005672 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005673 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005674 handshake->tls_prf = tls_prf_sha256;
5675 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5676 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5677 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005678#else
Jerry Yuf009d862022-02-17 14:01:37 +08005679 {
Jerry Yuf009d862022-02-17 14:01:37 +08005680 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005681 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005682 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5683 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005684#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005685
5686 return( 0 );
5687}
Jerry Yud6ab2352022-02-17 14:03:43 +08005688
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005689/*
5690 * Compute master secret if needed
5691 *
5692 * Parameters:
5693 * [in/out] handshake
5694 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5695 * (PSA-PSK) ciphersuite_info, psk_opaque
5696 * [out] premaster (cleared)
5697 * [out] master
5698 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5699 * debug: conf->f_dbg, conf->p_dbg
5700 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005701 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005702 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005703MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005704static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5705 unsigned char *master,
5706 const mbedtls_ssl_context *ssl )
5707{
5708 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5709
5710 /* cf. RFC 5246, Section 8.1:
5711 * "The master secret is always exactly 48 bytes in length." */
5712 size_t const master_secret_len = 48;
5713
5714#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5715 unsigned char session_hash[48];
5716#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5717
5718 /* The label for the KDF used for key expansion.
5719 * This is either "master secret" or "extended master secret"
5720 * depending on whether the Extended Master Secret extension
5721 * is used. */
5722 char const *lbl = "master secret";
5723
Przemek Stekielae4ed302022-04-05 17:15:55 +02005724 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005725 * - If the Extended Master Secret extension is not used,
5726 * this is ClientHello.Random + ServerHello.Random
5727 * (see Sect. 8.1 in RFC 5246).
5728 * - If the Extended Master Secret extension is used,
5729 * this is the transcript of the handshake so far.
5730 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005731 unsigned char const *seed = handshake->randbytes;
5732 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005733
5734#if !defined(MBEDTLS_DEBUG_C) && \
5735 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5736 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5737 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5738 ssl = NULL; /* make sure we don't use it except for those cases */
5739 (void) ssl;
5740#endif
5741
5742 if( handshake->resume != 0 )
5743 {
5744 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5745 return( 0 );
5746 }
5747
5748#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5749 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5750 {
5751 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005752 seed = session_hash;
5753 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005754
5755 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005756 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005757 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005758#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005759
Przemek Stekiel99114f32022-04-22 11:20:09 +02005760#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005761 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005762 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005763 {
5764 /* Perform PSK-to-MS expansion in a single step. */
5765 psa_status_t status;
5766 psa_algorithm_t alg;
5767 mbedtls_svc_key_id_t psk;
5768 psa_key_derivation_operation_t derivation =
5769 PSA_KEY_DERIVATION_OPERATION_INIT;
5770 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5771
5772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5773
5774 psk = mbedtls_ssl_get_opaque_psk( ssl );
5775
5776 if( hash_alg == MBEDTLS_MD_SHA384 )
5777 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5778 else
5779 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5780
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005781 size_t other_secret_len = 0;
5782 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005783
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005784 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005785 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005786 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005787 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005788 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005789 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005790 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005791 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005792 other_secret_len = 48;
5793 other_secret = handshake->premaster + 2;
5794 break;
5795 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005796 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005797 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5798 other_secret = handshake->premaster + 2;
5799 break;
5800 default:
5801 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005802 }
5803
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005804 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005805 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005806 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005807 (unsigned char const *) lbl,
5808 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005809 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005810 master_secret_len );
5811 if( status != PSA_SUCCESS )
5812 {
5813 psa_key_derivation_abort( &derivation );
5814 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5815 }
5816
5817 status = psa_key_derivation_output_bytes( &derivation,
5818 master,
5819 master_secret_len );
5820 if( status != PSA_SUCCESS )
5821 {
5822 psa_key_derivation_abort( &derivation );
5823 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5824 }
5825
5826 status = psa_key_derivation_abort( &derivation );
5827 if( status != PSA_SUCCESS )
5828 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5829 }
5830 else
5831#endif
5832 {
5833 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005834 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005835 master,
5836 master_secret_len );
5837 if( ret != 0 )
5838 {
5839 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5840 return( ret );
5841 }
5842
5843 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5844 handshake->premaster,
5845 handshake->pmslen );
5846
5847 mbedtls_platform_zeroize( handshake->premaster,
5848 sizeof(handshake->premaster) );
5849 }
5850
5851 return( 0 );
5852}
5853
Jerry Yud62f87e2022-02-17 14:09:02 +08005854int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5855{
5856 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5857 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5858 ssl->handshake->ciphersuite_info;
5859
5860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5861
5862 /* Set PRF, calc_verify and calc_finished function pointers */
5863 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005864 ciphersuite_info->mac );
5865 if( ret != 0 )
5866 {
5867 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5868 return( ret );
5869 }
5870
5871 /* Compute master secret if needed */
5872 ret = ssl_compute_master( ssl->handshake,
5873 ssl->session_negotiate->master,
5874 ssl );
5875 if( ret != 0 )
5876 {
5877 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5878 return( ret );
5879 }
5880
5881 /* Swap the client and server random values:
5882 * - MS derivation wanted client+server (RFC 5246 8.1)
5883 * - key derivation wants server+client (RFC 5246 6.3) */
5884 {
5885 unsigned char tmp[64];
5886 memcpy( tmp, ssl->handshake->randbytes, 64 );
5887 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5888 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5889 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5890 }
5891
5892 /* Populate transform structure */
5893 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5894 ssl->session_negotiate->ciphersuite,
5895 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005896#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005897 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005898#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005899 ssl->handshake->tls_prf,
5900 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005901 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005902 ssl->conf->endpoint,
5903 ssl );
5904 if( ret != 0 )
5905 {
5906 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5907 return( ret );
5908 }
5909
5910 /* We no longer need Server/ClientHello.random values */
5911 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5912 sizeof( ssl->handshake->randbytes ) );
5913
5914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5915
5916 return( 0 );
5917}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005918
Ronald Cron4dcbca92022-03-07 10:21:40 +01005919int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5920{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005921 switch( md )
5922 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005923#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005924 case MBEDTLS_SSL_HASH_SHA384:
5925 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5926 break;
5927#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005928#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005929 case MBEDTLS_SSL_HASH_SHA256:
5930 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5931 break;
5932#endif
5933 default:
5934 return( -1 );
5935 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005936#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5937 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5938 (void) ssl;
5939#endif
Ronald Cronc2f13a02022-03-07 10:25:24 +01005940 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005941}
5942
Andrzej Kurek25f27152022-08-17 16:09:31 -04005943#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005944void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5945 unsigned char *hash,
5946 size_t *hlen )
5947{
5948#if defined(MBEDTLS_USE_PSA_CRYPTO)
5949 size_t hash_size;
5950 psa_status_t status;
5951 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5952
5953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5954 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5955 if( status != PSA_SUCCESS )
5956 {
5957 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5958 return;
5959 }
5960
5961 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5962 if( status != PSA_SUCCESS )
5963 {
5964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5965 return;
5966 }
5967
5968 *hlen = 32;
5969 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5971#else
5972 mbedtls_sha256_context sha256;
5973
5974 mbedtls_sha256_init( &sha256 );
5975
5976 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5977
5978 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5979 mbedtls_sha256_finish( &sha256, hash );
5980
5981 *hlen = 32;
5982
5983 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5985
5986 mbedtls_sha256_free( &sha256 );
5987#endif /* MBEDTLS_USE_PSA_CRYPTO */
5988 return;
5989}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005990#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08005991
Andrzej Kurek25f27152022-08-17 16:09:31 -04005992#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08005993void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5994 unsigned char *hash,
5995 size_t *hlen )
5996{
5997#if defined(MBEDTLS_USE_PSA_CRYPTO)
5998 size_t hash_size;
5999 psa_status_t status;
6000 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6001
6002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6003 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6004 if( status != PSA_SUCCESS )
6005 {
6006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6007 return;
6008 }
6009
6010 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6011 if( status != PSA_SUCCESS )
6012 {
6013 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6014 return;
6015 }
6016
6017 *hlen = 48;
6018 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6020#else
6021 mbedtls_sha512_context sha512;
6022
6023 mbedtls_sha512_init( &sha512 );
6024
6025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6026
Andrzej Kureka242e832022-08-11 10:03:14 -04006027 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006028 mbedtls_sha512_finish( &sha512, hash );
6029
6030 *hlen = 48;
6031
6032 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6034
6035 mbedtls_sha512_free( &sha512 );
6036#endif /* MBEDTLS_USE_PSA_CRYPTO */
6037 return;
6038}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006039#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006040
Neil Armstrong80f6f322022-05-03 17:56:38 +02006041#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6042 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006043int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6044{
6045 unsigned char *p = ssl->handshake->premaster;
6046 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6047 const unsigned char *psk = NULL;
6048 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006049 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006050
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006051 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006052 {
6053 /*
6054 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006055 * checked before calling this function.
6056 *
6057 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006058 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006059 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006060 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6061 {
6062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6063 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6064 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006065 }
6066
6067 /*
6068 * PMS = struct {
6069 * opaque other_secret<0..2^16-1>;
6070 * opaque psk<0..2^16-1>;
6071 * };
6072 * with "other_secret" depending on the particular key exchange
6073 */
6074#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6075 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6076 {
6077 if( end - p < 2 )
6078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6079
6080 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6081 p += 2;
6082
6083 if( end < p || (size_t)( end - p ) < psk_len )
6084 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6085
6086 memset( p, 0, psk_len );
6087 p += psk_len;
6088 }
6089 else
6090#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6091#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6092 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6093 {
6094 /*
6095 * other_secret already set by the ClientKeyExchange message,
6096 * and is 48 bytes long
6097 */
6098 if( end - p < 2 )
6099 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6100
6101 *p++ = 0;
6102 *p++ = 48;
6103 p += 48;
6104 }
6105 else
6106#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6107#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6108 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6109 {
6110 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6111 size_t len;
6112
6113 /* Write length only when we know the actual value */
6114 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6115 p + 2, end - ( p + 2 ), &len,
6116 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6117 {
6118 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6119 return( ret );
6120 }
6121 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6122 p += 2 + len;
6123
6124 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6125 }
6126 else
6127#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006128#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006129 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6130 {
6131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6132 size_t zlen;
6133
6134 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6135 p + 2, end - ( p + 2 ),
6136 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6137 {
6138 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6139 return( ret );
6140 }
6141
6142 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6143 p += 2 + zlen;
6144
6145 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6146 MBEDTLS_DEBUG_ECDH_Z );
6147 }
6148 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006149#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006150 {
6151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6152 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6153 }
6154
6155 /* opaque psk<0..2^16-1>; */
6156 if( end - p < 2 )
6157 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6158
6159 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6160 p += 2;
6161
6162 if( end < p || (size_t)( end - p ) < psk_len )
6163 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6164
6165 memcpy( p, psk, psk_len );
6166 p += psk_len;
6167
6168 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6169
6170 return( 0 );
6171}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006172#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006173
6174#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006175MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006176static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6177
6178#if defined(MBEDTLS_SSL_PROTO_DTLS)
6179int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6180{
6181 /* If renegotiation is not enforced, retransmit until we would reach max
6182 * timeout if we were using the usual handshake doubling scheme */
6183 if( ssl->conf->renego_max_records < 0 )
6184 {
6185 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6186 unsigned char doublings = 1;
6187
6188 while( ratio != 0 )
6189 {
6190 ++doublings;
6191 ratio >>= 1;
6192 }
6193
6194 if( ++ssl->renego_records_seen > doublings )
6195 {
6196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6197 return( 0 );
6198 }
6199 }
6200
6201 return( ssl_write_hello_request( ssl ) );
6202}
6203#endif
6204#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006205
Jerry Yud9526692022-02-17 14:23:47 +08006206/*
6207 * Handshake functions
6208 */
6209#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6210/* No certificate support -> dummy functions */
6211int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6212{
6213 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6214 ssl->handshake->ciphersuite_info;
6215
6216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6217
6218 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6219 {
6220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6221 ssl->state++;
6222 return( 0 );
6223 }
6224
6225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6226 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6227}
6228
6229int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6230{
6231 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6232 ssl->handshake->ciphersuite_info;
6233
6234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6235
6236 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6237 {
6238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6239 ssl->state++;
6240 return( 0 );
6241 }
6242
6243 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6244 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6245}
6246
6247#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6248/* Some certificate support -> implement write and parse */
6249
6250int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6251{
6252 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6253 size_t i, n;
6254 const mbedtls_x509_crt *crt;
6255 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6256 ssl->handshake->ciphersuite_info;
6257
6258 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6259
6260 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6261 {
6262 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6263 ssl->state++;
6264 return( 0 );
6265 }
6266
6267#if defined(MBEDTLS_SSL_CLI_C)
6268 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6269 {
6270 if( ssl->handshake->client_auth == 0 )
6271 {
6272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6273 ssl->state++;
6274 return( 0 );
6275 }
6276 }
6277#endif /* MBEDTLS_SSL_CLI_C */
6278#if defined(MBEDTLS_SSL_SRV_C)
6279 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6280 {
6281 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6282 {
6283 /* Should never happen because we shouldn't have picked the
6284 * ciphersuite if we don't have a certificate. */
6285 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6286 }
6287 }
6288#endif
6289
6290 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6291
6292 /*
6293 * 0 . 0 handshake type
6294 * 1 . 3 handshake length
6295 * 4 . 6 length of all certs
6296 * 7 . 9 length of cert. 1
6297 * 10 . n-1 peer certificate
6298 * n . n+2 length of cert. 2
6299 * n+3 . ... upper level cert, etc.
6300 */
6301 i = 7;
6302 crt = mbedtls_ssl_own_cert( ssl );
6303
6304 while( crt != NULL )
6305 {
6306 n = crt->raw.len;
6307 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6308 {
6309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6310 " > %" MBEDTLS_PRINTF_SIZET,
6311 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6312 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6313 }
6314
6315 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6316 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6317 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6318
6319 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6320 i += n; crt = crt->next;
6321 }
6322
6323 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6324 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6325 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6326
6327 ssl->out_msglen = i;
6328 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6329 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6330
6331 ssl->state++;
6332
6333 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6334 {
6335 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6336 return( ret );
6337 }
6338
6339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6340
6341 return( ret );
6342}
6343
6344#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6345
6346#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006347MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006348static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6349 unsigned char *crt_buf,
6350 size_t crt_buf_len )
6351{
6352 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6353
6354 if( peer_crt == NULL )
6355 return( -1 );
6356
6357 if( peer_crt->raw.len != crt_buf_len )
6358 return( -1 );
6359
6360 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6361}
6362#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006363MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006364static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6365 unsigned char *crt_buf,
6366 size_t crt_buf_len )
6367{
6368 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6369 unsigned char const * const peer_cert_digest =
6370 ssl->session->peer_cert_digest;
6371 mbedtls_md_type_t const peer_cert_digest_type =
6372 ssl->session->peer_cert_digest_type;
6373 mbedtls_md_info_t const * const digest_info =
6374 mbedtls_md_info_from_type( peer_cert_digest_type );
6375 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6376 size_t digest_len;
6377
6378 if( peer_cert_digest == NULL || digest_info == NULL )
6379 return( -1 );
6380
6381 digest_len = mbedtls_md_get_size( digest_info );
6382 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6383 return( -1 );
6384
6385 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6386 if( ret != 0 )
6387 return( -1 );
6388
6389 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6390}
6391#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6392#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6393
6394/*
6395 * Once the certificate message is read, parse it into a cert chain and
6396 * perform basic checks, but leave actual verification to the caller
6397 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006398MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006399static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6400 mbedtls_x509_crt *chain )
6401{
6402 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6403#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6404 int crt_cnt=0;
6405#endif
6406 size_t i, n;
6407 uint8_t alert;
6408
6409 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6410 {
6411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6412 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6413 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6414 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6415 }
6416
6417 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6418 {
6419 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6420 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6421 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6422 }
6423
6424 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6425 {
6426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6427 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6428 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6429 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6430 }
6431
6432 i = mbedtls_ssl_hs_hdr_len( ssl );
6433
6434 /*
6435 * Same message structure as in mbedtls_ssl_write_certificate()
6436 */
6437 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6438
6439 if( ssl->in_msg[i] != 0 ||
6440 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6441 {
6442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6443 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6444 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6445 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6446 }
6447
6448 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6449 i += 3;
6450
6451 /* Iterate through and parse the CRTs in the provided chain. */
6452 while( i < ssl->in_hslen )
6453 {
6454 /* Check that there's room for the next CRT's length fields. */
6455 if ( i + 3 > ssl->in_hslen ) {
6456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6457 mbedtls_ssl_send_alert_message( ssl,
6458 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6459 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6460 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6461 }
6462 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6463 * anything beyond 2**16 ~ 64K. */
6464 if( ssl->in_msg[i] != 0 )
6465 {
6466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6467 mbedtls_ssl_send_alert_message( ssl,
6468 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6469 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6470 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6471 }
6472
6473 /* Read length of the next CRT in the chain. */
6474 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6475 | (unsigned int) ssl->in_msg[i + 2];
6476 i += 3;
6477
6478 if( n < 128 || i + n > ssl->in_hslen )
6479 {
6480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6481 mbedtls_ssl_send_alert_message( ssl,
6482 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6483 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6484 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6485 }
6486
6487 /* Check if we're handling the first CRT in the chain. */
6488#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6489 if( crt_cnt++ == 0 &&
6490 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6491 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6492 {
6493 /* During client-side renegotiation, check that the server's
6494 * end-CRTs hasn't changed compared to the initial handshake,
6495 * mitigating the triple handshake attack. On success, reuse
6496 * the original end-CRT instead of parsing it again. */
6497 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6498 if( ssl_check_peer_crt_unchanged( ssl,
6499 &ssl->in_msg[i],
6500 n ) != 0 )
6501 {
6502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6503 mbedtls_ssl_send_alert_message( ssl,
6504 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6505 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6506 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6507 }
6508
6509 /* Now we can safely free the original chain. */
6510 ssl_clear_peer_cert( ssl->session );
6511 }
6512#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6513
6514 /* Parse the next certificate in the chain. */
6515#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6516 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6517#else
6518 /* If we don't need to store the CRT chain permanently, parse
6519 * it in-place from the input buffer instead of making a copy. */
6520 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6521#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6522 switch( ret )
6523 {
6524 case 0: /*ok*/
6525 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6526 /* Ignore certificate with an unknown algorithm: maybe a
6527 prior certificate was already trusted. */
6528 break;
6529
6530 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6531 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6532 goto crt_parse_der_failed;
6533
6534 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6535 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6536 goto crt_parse_der_failed;
6537
6538 default:
6539 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6540 crt_parse_der_failed:
6541 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6542 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6543 return( ret );
6544 }
6545
6546 i += n;
6547 }
6548
6549 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6550 return( 0 );
6551}
6552
6553#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006554MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006555static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6556{
6557 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6558 return( -1 );
6559
6560 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6561 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6562 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6563 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6564 {
Ronald Cron19385882022-06-15 16:26:13 +02006565 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006566 return( 0 );
6567 }
6568 return( -1 );
6569}
6570#endif /* MBEDTLS_SSL_SRV_C */
6571
6572/* Check if a certificate message is expected.
6573 * Return either
6574 * - SSL_CERTIFICATE_EXPECTED, or
6575 * - SSL_CERTIFICATE_SKIP
6576 * indicating whether a Certificate message is expected or not.
6577 */
6578#define SSL_CERTIFICATE_EXPECTED 0
6579#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006580MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006581static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6582 int authmode )
6583{
6584 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6585 ssl->handshake->ciphersuite_info;
6586
6587 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6588 return( SSL_CERTIFICATE_SKIP );
6589
6590#if defined(MBEDTLS_SSL_SRV_C)
6591 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6592 {
6593 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6594 return( SSL_CERTIFICATE_SKIP );
6595
6596 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6597 {
6598 ssl->session_negotiate->verify_result =
6599 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6600 return( SSL_CERTIFICATE_SKIP );
6601 }
6602 }
6603#else
6604 ((void) authmode);
6605#endif /* MBEDTLS_SSL_SRV_C */
6606
6607 return( SSL_CERTIFICATE_EXPECTED );
6608}
6609
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006610MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006611static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6612 int authmode,
6613 mbedtls_x509_crt *chain,
6614 void *rs_ctx )
6615{
6616 int ret = 0;
6617 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6618 ssl->handshake->ciphersuite_info;
6619 int have_ca_chain = 0;
6620
6621 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6622 void *p_vrfy;
6623
6624 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6625 return( 0 );
6626
6627 if( ssl->f_vrfy != NULL )
6628 {
6629 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6630 f_vrfy = ssl->f_vrfy;
6631 p_vrfy = ssl->p_vrfy;
6632 }
6633 else
6634 {
6635 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6636 f_vrfy = ssl->conf->f_vrfy;
6637 p_vrfy = ssl->conf->p_vrfy;
6638 }
6639
6640 /*
6641 * Main check: verify certificate
6642 */
6643#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6644 if( ssl->conf->f_ca_cb != NULL )
6645 {
6646 ((void) rs_ctx);
6647 have_ca_chain = 1;
6648
6649 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6650 ret = mbedtls_x509_crt_verify_with_ca_cb(
6651 chain,
6652 ssl->conf->f_ca_cb,
6653 ssl->conf->p_ca_cb,
6654 ssl->conf->cert_profile,
6655 ssl->hostname,
6656 &ssl->session_negotiate->verify_result,
6657 f_vrfy, p_vrfy );
6658 }
6659 else
6660#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6661 {
6662 mbedtls_x509_crt *ca_chain;
6663 mbedtls_x509_crl *ca_crl;
6664
6665#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6666 if( ssl->handshake->sni_ca_chain != NULL )
6667 {
6668 ca_chain = ssl->handshake->sni_ca_chain;
6669 ca_crl = ssl->handshake->sni_ca_crl;
6670 }
6671 else
6672#endif
6673 {
6674 ca_chain = ssl->conf->ca_chain;
6675 ca_crl = ssl->conf->ca_crl;
6676 }
6677
6678 if( ca_chain != NULL )
6679 have_ca_chain = 1;
6680
6681 ret = mbedtls_x509_crt_verify_restartable(
6682 chain,
6683 ca_chain, ca_crl,
6684 ssl->conf->cert_profile,
6685 ssl->hostname,
6686 &ssl->session_negotiate->verify_result,
6687 f_vrfy, p_vrfy, rs_ctx );
6688 }
6689
6690 if( ret != 0 )
6691 {
6692 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6693 }
6694
6695#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6696 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6697 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6698#endif
6699
6700 /*
6701 * Secondary checks: always done, but change 'ret' only if it was 0
6702 */
6703
6704#if defined(MBEDTLS_ECP_C)
6705 {
6706 const mbedtls_pk_context *pk = &chain->pk;
6707
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006708 /* If certificate uses an EC key, make sure the curve is OK.
6709 * This is a public key, so it can't be opaque, so can_do() is a good
6710 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006711 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006712 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006713 /* and in the unlikely case the above assumption no longer holds
6714 * we are making sure that pk_ec() here does not return a NULL
6715 */
6716 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6717 if( ec == NULL )
6718 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006720 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6721 }
Jerry Yud9526692022-02-17 14:23:47 +08006722
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006723 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6724 {
6725 ssl->session_negotiate->verify_result |=
6726 MBEDTLS_X509_BADCERT_BAD_KEY;
6727
6728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6729 if( ret == 0 )
6730 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6731 }
Jerry Yud9526692022-02-17 14:23:47 +08006732 }
6733 }
6734#endif /* MBEDTLS_ECP_C */
6735
6736 if( mbedtls_ssl_check_cert_usage( chain,
6737 ciphersuite_info,
6738 ! ssl->conf->endpoint,
6739 &ssl->session_negotiate->verify_result ) != 0 )
6740 {
6741 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6742 if( ret == 0 )
6743 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6744 }
6745
6746 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6747 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6748 * with details encoded in the verification flags. All other kinds
6749 * of error codes, including those from the user provided f_vrfy
6750 * functions, are treated as fatal and lead to a failure of
6751 * ssl_parse_certificate even if verification was optional. */
6752 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6753 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6754 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6755 {
6756 ret = 0;
6757 }
6758
6759 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6760 {
6761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6762 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6763 }
6764
6765 if( ret != 0 )
6766 {
6767 uint8_t alert;
6768
6769 /* The certificate may have been rejected for several reasons.
6770 Pick one and send the corresponding alert. Which alert to send
6771 may be a subject of debate in some cases. */
6772 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6773 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6774 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6775 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6776 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6777 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6778 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6779 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6780 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6781 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6782 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6783 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6784 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6785 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6786 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6787 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6788 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6789 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6790 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6791 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6792 else
6793 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6794 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6795 alert );
6796 }
6797
6798#if defined(MBEDTLS_DEBUG_C)
6799 if( ssl->session_negotiate->verify_result != 0 )
6800 {
6801 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6802 (unsigned int) ssl->session_negotiate->verify_result ) );
6803 }
6804 else
6805 {
6806 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6807 }
6808#endif /* MBEDTLS_DEBUG_C */
6809
6810 return( ret );
6811}
6812
6813#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006814MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006815static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6816 unsigned char *start, size_t len )
6817{
6818 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6819 /* Remember digest of the peer's end-CRT. */
6820 ssl->session_negotiate->peer_cert_digest =
6821 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6822 if( ssl->session_negotiate->peer_cert_digest == NULL )
6823 {
6824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6825 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6826 mbedtls_ssl_send_alert_message( ssl,
6827 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6828 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6829
6830 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6831 }
6832
6833 ret = mbedtls_md( mbedtls_md_info_from_type(
6834 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6835 start, len,
6836 ssl->session_negotiate->peer_cert_digest );
6837
6838 ssl->session_negotiate->peer_cert_digest_type =
6839 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6840 ssl->session_negotiate->peer_cert_digest_len =
6841 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6842
6843 return( ret );
6844}
6845
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006846MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006847static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6848 unsigned char *start, size_t len )
6849{
6850 unsigned char *end = start + len;
6851 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6852
6853 /* Make a copy of the peer's raw public key. */
6854 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6855 ret = mbedtls_pk_parse_subpubkey( &start, end,
6856 &ssl->handshake->peer_pubkey );
6857 if( ret != 0 )
6858 {
6859 /* We should have parsed the public key before. */
6860 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6861 }
6862
6863 return( 0 );
6864}
6865#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6866
6867int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6868{
6869 int ret = 0;
6870 int crt_expected;
6871#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6872 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6873 ? ssl->handshake->sni_authmode
6874 : ssl->conf->authmode;
6875#else
6876 const int authmode = ssl->conf->authmode;
6877#endif
6878 void *rs_ctx = NULL;
6879 mbedtls_x509_crt *chain = NULL;
6880
6881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6882
6883 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6884 if( crt_expected == SSL_CERTIFICATE_SKIP )
6885 {
6886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6887 goto exit;
6888 }
6889
6890#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6891 if( ssl->handshake->ecrs_enabled &&
6892 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6893 {
6894 chain = ssl->handshake->ecrs_peer_cert;
6895 ssl->handshake->ecrs_peer_cert = NULL;
6896 goto crt_verify;
6897 }
6898#endif
6899
6900 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6901 {
6902 /* mbedtls_ssl_read_record may have sent an alert already. We
6903 let it decide whether to alert. */
6904 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6905 goto exit;
6906 }
6907
6908#if defined(MBEDTLS_SSL_SRV_C)
6909 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6910 {
6911 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6912
6913 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6914 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6915
6916 goto exit;
6917 }
6918#endif /* MBEDTLS_SSL_SRV_C */
6919
6920 /* Clear existing peer CRT structure in case we tried to
6921 * reuse a session but it failed, and allocate a new one. */
6922 ssl_clear_peer_cert( ssl->session_negotiate );
6923
6924 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6925 if( chain == NULL )
6926 {
6927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6928 sizeof( mbedtls_x509_crt ) ) );
6929 mbedtls_ssl_send_alert_message( ssl,
6930 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6931 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6932
6933 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6934 goto exit;
6935 }
6936 mbedtls_x509_crt_init( chain );
6937
6938 ret = ssl_parse_certificate_chain( ssl, chain );
6939 if( ret != 0 )
6940 goto exit;
6941
6942#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6943 if( ssl->handshake->ecrs_enabled)
6944 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6945
6946crt_verify:
6947 if( ssl->handshake->ecrs_enabled)
6948 rs_ctx = &ssl->handshake->ecrs_ctx;
6949#endif
6950
6951 ret = ssl_parse_certificate_verify( ssl, authmode,
6952 chain, rs_ctx );
6953 if( ret != 0 )
6954 goto exit;
6955
6956#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6957 {
6958 unsigned char *crt_start, *pk_start;
6959 size_t crt_len, pk_len;
6960
6961 /* We parse the CRT chain without copying, so
6962 * these pointers point into the input buffer,
6963 * and are hence still valid after freeing the
6964 * CRT chain. */
6965
6966 crt_start = chain->raw.p;
6967 crt_len = chain->raw.len;
6968
6969 pk_start = chain->pk_raw.p;
6970 pk_len = chain->pk_raw.len;
6971
6972 /* Free the CRT structures before computing
6973 * digest and copying the peer's public key. */
6974 mbedtls_x509_crt_free( chain );
6975 mbedtls_free( chain );
6976 chain = NULL;
6977
6978 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6979 if( ret != 0 )
6980 goto exit;
6981
6982 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6983 if( ret != 0 )
6984 goto exit;
6985 }
6986#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6987 /* Pass ownership to session structure. */
6988 ssl->session_negotiate->peer_cert = chain;
6989 chain = NULL;
6990#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6991
6992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6993
6994exit:
6995
6996 if( ret == 0 )
6997 ssl->state++;
6998
6999#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7000 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7001 {
7002 ssl->handshake->ecrs_peer_cert = chain;
7003 chain = NULL;
7004 }
7005#endif
7006
7007 if( chain != NULL )
7008 {
7009 mbedtls_x509_crt_free( chain );
7010 mbedtls_free( chain );
7011 }
7012
7013 return( ret );
7014}
7015#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7016
Andrzej Kurek25f27152022-08-17 16:09:31 -04007017#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007018static void ssl_calc_finished_tls_sha256(
7019 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7020{
7021 int len = 12;
7022 const char *sender;
7023 unsigned char padbuf[32];
7024#if defined(MBEDTLS_USE_PSA_CRYPTO)
7025 size_t hash_size;
7026 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7027 psa_status_t status;
7028#else
7029 mbedtls_sha256_context sha256;
7030#endif
7031
7032 mbedtls_ssl_session *session = ssl->session_negotiate;
7033 if( !session )
7034 session = ssl->session;
7035
7036 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7037 ? "client finished"
7038 : "server finished";
7039
7040#if defined(MBEDTLS_USE_PSA_CRYPTO)
7041 sha256_psa = psa_hash_operation_init();
7042
7043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7044
7045 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7046 if( status != PSA_SUCCESS )
7047 {
7048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7049 return;
7050 }
7051
7052 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7053 if( status != PSA_SUCCESS )
7054 {
7055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7056 return;
7057 }
7058 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7059#else
7060
7061 mbedtls_sha256_init( &sha256 );
7062
7063 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7064
7065 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7066
7067 /*
7068 * TLSv1.2:
7069 * hash = PRF( master, finished_label,
7070 * Hash( handshake ) )[0.11]
7071 */
7072
7073#if !defined(MBEDTLS_SHA256_ALT)
7074 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7075 sha256.state, sizeof( sha256.state ) );
7076#endif
7077
7078 mbedtls_sha256_finish( &sha256, padbuf );
7079 mbedtls_sha256_free( &sha256 );
7080#endif /* MBEDTLS_USE_PSA_CRYPTO */
7081
7082 ssl->handshake->tls_prf( session->master, 48, sender,
7083 padbuf, 32, buf, len );
7084
7085 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7086
7087 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7088
7089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7090}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007091#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007092
Jerry Yub7ba49e2022-02-17 14:25:53 +08007093
Andrzej Kurek25f27152022-08-17 16:09:31 -04007094#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007095static void ssl_calc_finished_tls_sha384(
7096 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7097{
7098 int len = 12;
7099 const char *sender;
7100 unsigned char padbuf[48];
7101#if defined(MBEDTLS_USE_PSA_CRYPTO)
7102 size_t hash_size;
7103 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7104 psa_status_t status;
7105#else
7106 mbedtls_sha512_context sha512;
7107#endif
7108
7109 mbedtls_ssl_session *session = ssl->session_negotiate;
7110 if( !session )
7111 session = ssl->session;
7112
7113 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7114 ? "client finished"
7115 : "server finished";
7116
7117#if defined(MBEDTLS_USE_PSA_CRYPTO)
7118 sha384_psa = psa_hash_operation_init();
7119
7120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7121
7122 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7123 if( status != PSA_SUCCESS )
7124 {
7125 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7126 return;
7127 }
7128
7129 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7130 if( status != PSA_SUCCESS )
7131 {
7132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7133 return;
7134 }
7135 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7136#else
7137 mbedtls_sha512_init( &sha512 );
7138
7139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7140
Andrzej Kureka242e832022-08-11 10:03:14 -04007141 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007142
7143 /*
7144 * TLSv1.2:
7145 * hash = PRF( master, finished_label,
7146 * Hash( handshake ) )[0.11]
7147 */
7148
7149#if !defined(MBEDTLS_SHA512_ALT)
7150 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7151 sha512.state, sizeof( sha512.state ) );
7152#endif
7153 mbedtls_sha512_finish( &sha512, padbuf );
7154
7155 mbedtls_sha512_free( &sha512 );
7156#endif
7157
7158 ssl->handshake->tls_prf( session->master, 48, sender,
7159 padbuf, 48, buf, len );
7160
7161 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7162
7163 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7164
7165 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7166}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007167#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007168
Jerry Yuaef00152022-02-17 14:27:31 +08007169void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7170{
7171 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7172
7173 /*
7174 * Free our handshake params
7175 */
7176 mbedtls_ssl_handshake_free( ssl );
7177 mbedtls_free( ssl->handshake );
7178 ssl->handshake = NULL;
7179
7180 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007181 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007182 */
7183 if( ssl->transform )
7184 {
7185 mbedtls_ssl_transform_free( ssl->transform );
7186 mbedtls_free( ssl->transform );
7187 }
7188 ssl->transform = ssl->transform_negotiate;
7189 ssl->transform_negotiate = NULL;
7190
7191 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7192}
7193
Jerry Yu2a9fff52022-02-17 14:28:51 +08007194void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7195{
7196 int resume = ssl->handshake->resume;
7197
7198 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7199
7200#if defined(MBEDTLS_SSL_RENEGOTIATION)
7201 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7202 {
7203 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7204 ssl->renego_records_seen = 0;
7205 }
7206#endif
7207
7208 /*
7209 * Free the previous session and switch in the current one
7210 */
7211 if( ssl->session )
7212 {
7213#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7214 /* RFC 7366 3.1: keep the EtM state */
7215 ssl->session_negotiate->encrypt_then_mac =
7216 ssl->session->encrypt_then_mac;
7217#endif
7218
7219 mbedtls_ssl_session_free( ssl->session );
7220 mbedtls_free( ssl->session );
7221 }
7222 ssl->session = ssl->session_negotiate;
7223 ssl->session_negotiate = NULL;
7224
7225 /*
7226 * Add cache entry
7227 */
7228 if( ssl->conf->f_set_cache != NULL &&
7229 ssl->session->id_len != 0 &&
7230 resume == 0 )
7231 {
7232 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7233 ssl->session->id,
7234 ssl->session->id_len,
7235 ssl->session ) != 0 )
7236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7237 }
7238
7239#if defined(MBEDTLS_SSL_PROTO_DTLS)
7240 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7241 ssl->handshake->flight != NULL )
7242 {
7243 /* Cancel handshake timer */
7244 mbedtls_ssl_set_timer( ssl, 0 );
7245
7246 /* Keep last flight around in case we need to resend it:
7247 * we need the handshake and transform structures for that */
7248 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7249 }
7250 else
7251#endif
7252 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7253
7254 ssl->state++;
7255
7256 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7257}
7258
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007259int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7260{
7261 int ret, hash_len;
7262
7263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7264
7265 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7266
7267 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7268
7269 /*
7270 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7271 * may define some other value. Currently (early 2016), no defined
7272 * ciphersuite does this (and this is unlikely to change as activity has
7273 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7274 */
7275 hash_len = 12;
7276
7277#if defined(MBEDTLS_SSL_RENEGOTIATION)
7278 ssl->verify_data_len = hash_len;
7279 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7280#endif
7281
7282 ssl->out_msglen = 4 + hash_len;
7283 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7284 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7285
7286 /*
7287 * In case of session resuming, invert the client and server
7288 * ChangeCipherSpec messages order.
7289 */
7290 if( ssl->handshake->resume != 0 )
7291 {
7292#if defined(MBEDTLS_SSL_CLI_C)
7293 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7294 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7295#endif
7296#if defined(MBEDTLS_SSL_SRV_C)
7297 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7298 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7299#endif
7300 }
7301 else
7302 ssl->state++;
7303
7304 /*
7305 * Switch to our negotiated transform and session parameters for outbound
7306 * data.
7307 */
7308 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7309
7310#if defined(MBEDTLS_SSL_PROTO_DTLS)
7311 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7312 {
7313 unsigned char i;
7314
7315 /* Remember current epoch settings for resending */
7316 ssl->handshake->alt_transform_out = ssl->transform_out;
7317 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7318 sizeof( ssl->handshake->alt_out_ctr ) );
7319
7320 /* Set sequence_number to zero */
7321 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7322
7323
7324 /* Increment epoch */
7325 for( i = 2; i > 0; i-- )
7326 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7327 break;
7328
7329 /* The loop goes to its end iff the counter is wrapping */
7330 if( i == 0 )
7331 {
7332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7333 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7334 }
7335 }
7336 else
7337#endif /* MBEDTLS_SSL_PROTO_DTLS */
7338 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7339
7340 ssl->transform_out = ssl->transform_negotiate;
7341 ssl->session_out = ssl->session_negotiate;
7342
7343#if defined(MBEDTLS_SSL_PROTO_DTLS)
7344 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7345 mbedtls_ssl_send_flight_completed( ssl );
7346#endif
7347
7348 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7349 {
7350 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7351 return( ret );
7352 }
7353
7354#if defined(MBEDTLS_SSL_PROTO_DTLS)
7355 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7356 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7357 {
7358 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7359 return( ret );
7360 }
7361#endif
7362
7363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7364
7365 return( 0 );
7366}
7367
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007368#define SSL_MAX_HASH_LEN 12
7369
7370int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7371{
7372 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7373 unsigned int hash_len = 12;
7374 unsigned char buf[SSL_MAX_HASH_LEN];
7375
7376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7377
7378 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7379
7380 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7381 {
7382 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7383 goto exit;
7384 }
7385
7386 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7387 {
7388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7389 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7390 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7391 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7392 goto exit;
7393 }
7394
7395 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7396 {
7397 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7398 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7399 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7400 goto exit;
7401 }
7402
7403 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7404 {
7405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7406 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7407 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7408 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7409 goto exit;
7410 }
7411
7412 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7413 buf, hash_len ) != 0 )
7414 {
7415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7416 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7417 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7418 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7419 goto exit;
7420 }
7421
7422#if defined(MBEDTLS_SSL_RENEGOTIATION)
7423 ssl->verify_data_len = hash_len;
7424 memcpy( ssl->peer_verify_data, buf, hash_len );
7425#endif
7426
7427 if( ssl->handshake->resume != 0 )
7428 {
7429#if defined(MBEDTLS_SSL_CLI_C)
7430 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7431 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7432#endif
7433#if defined(MBEDTLS_SSL_SRV_C)
7434 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7435 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7436#endif
7437 }
7438 else
7439 ssl->state++;
7440
7441#if defined(MBEDTLS_SSL_PROTO_DTLS)
7442 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7443 mbedtls_ssl_recv_flight_completed( ssl );
7444#endif
7445
7446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7447
7448exit:
7449 mbedtls_platform_zeroize( buf, hash_len );
7450 return( ret );
7451}
7452
Jerry Yu392112c2022-02-17 14:34:10 +08007453#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7454/*
7455 * Helper to get TLS 1.2 PRF from ciphersuite
7456 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7457 */
7458static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7459{
Jerry Yu392112c2022-02-17 14:34:10 +08007460 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Andrzej Kurek68327742022-10-03 06:18:18 -04007461 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7462#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007463 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007464 return( tls_prf_sha384 );
Andrzej Kurek894edde2022-09-29 06:31:14 -04007465 else
Jerry Yu392112c2022-02-17 14:34:10 +08007466#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007467#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7468 {
7469 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
7470 return( tls_prf_sha256 );
7471 }
7472#endif
7473#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7474 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7475 (void) ciphersuite_info;
7476#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007477
Andrzej Kurek894edde2022-09-29 06:31:14 -04007478 return( NULL );
Jerry Yu392112c2022-02-17 14:34:10 +08007479}
7480#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007481
7482static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7483{
7484 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007485#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007486 if( tls_prf == tls_prf_sha384 )
7487 {
7488 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7489 }
7490 else
7491#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007492#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007493 if( tls_prf == tls_prf_sha256 )
7494 {
7495 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7496 }
7497 else
7498#endif
7499 return( MBEDTLS_SSL_TLS_PRF_NONE );
7500}
7501
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007502/*
7503 * Populate a transform structure with session keys and all the other
7504 * necessary information.
7505 *
7506 * Parameters:
7507 * - [in/out]: transform: structure to populate
7508 * [in] must be just initialised with mbedtls_ssl_transform_init()
7509 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7510 * - [in] ciphersuite
7511 * - [in] master
7512 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007513 * - [in] tls_prf: pointer to PRF to use for key derivation
7514 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007515 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007516 * - [in] endpoint: client or server
7517 * - [in] ssl: used for:
7518 * - ssl->conf->{f,p}_export_keys
7519 * [in] optionally used for:
7520 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7521 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007522MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007523static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7524 int ciphersuite,
7525 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007526#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007527 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007528#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007529 ssl_tls_prf_t tls_prf,
7530 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007531 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007532 unsigned endpoint,
7533 const mbedtls_ssl_context *ssl )
7534{
7535 int ret = 0;
7536 unsigned char keyblk[256];
7537 unsigned char *key1;
7538 unsigned char *key2;
7539 unsigned char *mac_enc;
7540 unsigned char *mac_dec;
7541 size_t mac_key_len = 0;
7542 size_t iv_copy_len;
7543 size_t keylen;
7544 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007545 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007546#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007547 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007548 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007549#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007550
7551#if defined(MBEDTLS_USE_PSA_CRYPTO)
7552 psa_key_type_t key_type;
7553 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7554 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007555 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007556 size_t key_bits;
7557 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7558#endif
7559
7560#if !defined(MBEDTLS_DEBUG_C) && \
7561 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7562 if( ssl->f_export_keys == NULL )
7563 {
7564 ssl = NULL; /* make sure we don't use it except for these cases */
7565 (void) ssl;
7566 }
7567#endif
7568
7569 /*
7570 * Some data just needs copying into the structure
7571 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007572#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007573 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007574#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007575 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007576
7577#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7578 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7579#endif
7580
7581#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007582 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007583 {
7584 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7585 * generation separate. This should never happen. */
7586 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7587 }
7588#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7589
7590 /*
7591 * Get various info structures
7592 */
7593 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7594 if( ciphersuite_info == NULL )
7595 {
7596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7597 ciphersuite ) );
7598 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7599 }
7600
Neil Armstrongab555e02022-04-04 11:07:59 +02007601 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007602#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007603 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007604#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007605 ciphersuite_info );
7606
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007607 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7608 transform->taglen =
7609 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7610
7611#if defined(MBEDTLS_USE_PSA_CRYPTO)
7612 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7613 transform->taglen,
7614 &alg,
7615 &key_type,
7616 &key_bits ) ) != PSA_SUCCESS )
7617 {
7618 ret = psa_ssl_status_to_mbedtls( status );
7619 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7620 goto end;
7621 }
7622#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007623 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7624 if( cipher_info == NULL )
7625 {
7626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7627 ciphersuite_info->cipher ) );
7628 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7629 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007630#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007631
Neil Armstronge4512952022-03-08 09:08:22 +01007632#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007633 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007634 if( mac_alg == 0 )
7635 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007637 (unsigned) ciphersuite_info->mac ) );
7638 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7639 }
7640#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007641 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7642 if( md_info == NULL )
7643 {
7644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7645 (unsigned) ciphersuite_info->mac ) );
7646 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7647 }
Neil Armstronge4512952022-03-08 09:08:22 +01007648#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007649
7650#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7651 /* Copy own and peer's CID if the use of the CID
7652 * extension has been negotiated. */
7653 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7654 {
7655 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7656
7657 transform->in_cid_len = ssl->own_cid_len;
7658 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7659 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7660 transform->in_cid_len );
7661
7662 transform->out_cid_len = ssl->handshake->peer_cid_len;
7663 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7664 ssl->handshake->peer_cid_len );
7665 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7666 transform->out_cid_len );
7667 }
7668#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7669
7670 /*
7671 * Compute key block using the PRF
7672 */
7673 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7674 if( ret != 0 )
7675 {
7676 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7677 return( ret );
7678 }
7679
7680 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7681 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7682 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7683 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7684 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7685
7686 /*
7687 * Determine the appropriate key, IV and MAC length.
7688 */
7689
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007690#if defined(MBEDTLS_USE_PSA_CRYPTO)
7691 keylen = PSA_BITS_TO_BYTES(key_bits);
7692#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007693 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007694#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007695
7696#if defined(MBEDTLS_GCM_C) || \
7697 defined(MBEDTLS_CCM_C) || \
7698 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007699 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007700 {
7701 size_t explicit_ivlen;
7702
7703 transform->maclen = 0;
7704 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007705
7706 /* All modes haves 96-bit IVs, but the length of the static parts vary
7707 * with mode and version:
7708 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7709 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7710 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7711 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7712 * sequence number).
7713 */
7714 transform->ivlen = 12;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007715#if defined(MBEDTLS_USE_PSA_CRYPTO)
7716 if( key_type == PSA_KEY_TYPE_CHACHA20 )
7717#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007718 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007719#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007720 transform->fixed_ivlen = 12;
7721 else
7722 transform->fixed_ivlen = 4;
7723
7724 /* Minimum length of encrypted record */
7725 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7726 transform->minlen = explicit_ivlen + transform->taglen;
7727 }
7728 else
7729#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7730#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007731 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7732 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7733 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007734 {
Neil Armstronge4512952022-03-08 09:08:22 +01007735#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007736 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007737#else
7738 size_t block_size = cipher_info->block_size;
7739#endif /* MBEDTLS_USE_PSA_CRYPTO */
7740
7741#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007742 /* Get MAC length */
7743 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7744#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007745 /* Initialize HMAC contexts */
7746 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7747 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7748 {
7749 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7750 goto end;
7751 }
7752
7753 /* Get MAC length */
7754 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007755#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007756 transform->maclen = mac_key_len;
7757
7758 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007759#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007760 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007761#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007762 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007763#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007764
7765 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007766 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007767 transform->minlen = transform->maclen;
7768 else
7769 {
7770 /*
7771 * GenericBlockCipher:
7772 * 1. if EtM is in use: one block plus MAC
7773 * otherwise: * first multiple of blocklen greater than maclen
7774 * 2. IV
7775 */
7776#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007777 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007778 {
7779 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007780 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007781 }
7782 else
7783#endif
7784 {
7785 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007786 + block_size
7787 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007788 }
7789
Glenn Strauss07c64162022-03-14 12:34:51 -04007790 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007791 {
7792 transform->minlen += transform->ivlen;
7793 }
7794 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007795 {
7796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7797 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7798 goto end;
7799 }
7800 }
7801 }
7802 else
7803#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7804 {
7805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7806 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7807 }
7808
7809 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7810 (unsigned) keylen,
7811 (unsigned) transform->minlen,
7812 (unsigned) transform->ivlen,
7813 (unsigned) transform->maclen ) );
7814
7815 /*
7816 * Finally setup the cipher contexts, IVs and MAC secrets.
7817 */
7818#if defined(MBEDTLS_SSL_CLI_C)
7819 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7820 {
7821 key1 = keyblk + mac_key_len * 2;
7822 key2 = keyblk + mac_key_len * 2 + keylen;
7823
7824 mac_enc = keyblk;
7825 mac_dec = keyblk + mac_key_len;
7826
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007827 iv_copy_len = ( transform->fixed_ivlen ) ?
7828 transform->fixed_ivlen : transform->ivlen;
7829 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7830 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7831 iv_copy_len );
7832 }
7833 else
7834#endif /* MBEDTLS_SSL_CLI_C */
7835#if defined(MBEDTLS_SSL_SRV_C)
7836 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7837 {
7838 key1 = keyblk + mac_key_len * 2 + keylen;
7839 key2 = keyblk + mac_key_len * 2;
7840
7841 mac_enc = keyblk + mac_key_len;
7842 mac_dec = keyblk;
7843
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007844 iv_copy_len = ( transform->fixed_ivlen ) ?
7845 transform->fixed_ivlen : transform->ivlen;
7846 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7847 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7848 iv_copy_len );
7849 }
7850 else
7851#endif /* MBEDTLS_SSL_SRV_C */
7852 {
7853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7854 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7855 goto end;
7856 }
7857
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007858 if( ssl != NULL && ssl->f_export_keys != NULL )
7859 {
7860 ssl->f_export_keys( ssl->p_export_keys,
7861 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7862 master, 48,
7863 randbytes + 32,
7864 randbytes,
7865 tls_prf_get_type( tls_prf ) );
7866 }
7867
7868#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007869 transform->psa_alg = alg;
7870
7871 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7872 {
7873 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7874 psa_set_key_algorithm( &attributes, alg );
7875 psa_set_key_type( &attributes, key_type );
7876
7877 if( ( status = psa_import_key( &attributes,
7878 key1,
7879 PSA_BITS_TO_BYTES( key_bits ),
7880 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7881 {
7882 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7883 ret = psa_ssl_status_to_mbedtls( status );
7884 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7885 goto end;
7886 }
7887
7888 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7889
7890 if( ( status = psa_import_key( &attributes,
7891 key2,
7892 PSA_BITS_TO_BYTES( key_bits ),
7893 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7894 {
7895 ret = psa_ssl_status_to_mbedtls( status );
7896 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7897 goto end;
7898 }
7899 }
7900#else
7901 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7902 cipher_info ) ) != 0 )
7903 {
7904 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7905 goto end;
7906 }
7907
7908 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7909 cipher_info ) ) != 0 )
7910 {
7911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7912 goto end;
7913 }
7914
7915 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7916 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7917 MBEDTLS_ENCRYPT ) ) != 0 )
7918 {
7919 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7920 goto end;
7921 }
7922
7923 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7924 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7925 MBEDTLS_DECRYPT ) ) != 0 )
7926 {
7927 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7928 goto end;
7929 }
7930
7931#if defined(MBEDTLS_CIPHER_MODE_CBC)
7932 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7933 {
7934 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7935 MBEDTLS_PADDING_NONE ) ) != 0 )
7936 {
7937 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7938 goto end;
7939 }
7940
7941 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7942 MBEDTLS_PADDING_NONE ) ) != 0 )
7943 {
7944 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7945 goto end;
7946 }
7947 }
7948#endif /* MBEDTLS_CIPHER_MODE_CBC */
7949#endif /* MBEDTLS_USE_PSA_CRYPTO */
7950
Neil Armstrong29c0c042022-03-17 17:47:28 +01007951#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7952 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7953 For AEAD-based ciphersuites, there is nothing to do here. */
7954 if( mac_key_len != 0 )
7955 {
7956#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007957 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007958
7959 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007960 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007961 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7962
7963 if( ( status = psa_import_key( &attributes,
7964 mac_enc, mac_key_len,
7965 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7966 {
7967 ret = psa_ssl_status_to_mbedtls( status );
7968 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7969 goto end;
7970 }
7971
Ronald Cronfb39f152022-03-25 14:36:28 +01007972 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04007973 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
7974#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
7975 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
7976#endif
7977 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007978 /* mbedtls_ct_hmac() requires the key to be exportable */
7979 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7980 PSA_KEY_USAGE_VERIFY_HASH );
7981 else
7982 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7983
7984 if( ( status = psa_import_key( &attributes,
7985 mac_dec, mac_key_len,
7986 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7987 {
7988 ret = psa_ssl_status_to_mbedtls( status );
7989 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7990 goto end;
7991 }
7992#else
7993 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7994 if( ret != 0 )
7995 goto end;
7996 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7997 if( ret != 0 )
7998 goto end;
7999#endif /* MBEDTLS_USE_PSA_CRYPTO */
8000 }
8001#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8002
8003 ((void) mac_dec);
8004 ((void) mac_enc);
8005
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008006end:
8007 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
8008 return( ret );
8009}
8010
Jerry Yuee40f9d2022-02-17 14:55:16 +08008011#if defined(MBEDTLS_USE_PSA_CRYPTO)
8012int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8013 unsigned char *hash, size_t *hashlen,
8014 unsigned char *data, size_t data_len,
8015 mbedtls_md_type_t md_alg )
8016{
8017 psa_status_t status;
8018 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008019 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008020
8021 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8022
8023 if( ( status = psa_hash_setup( &hash_operation,
8024 hash_alg ) ) != PSA_SUCCESS )
8025 {
8026 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8027 goto exit;
8028 }
8029
8030 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8031 64 ) ) != PSA_SUCCESS )
8032 {
8033 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8034 goto exit;
8035 }
8036
8037 if( ( status = psa_hash_update( &hash_operation,
8038 data, data_len ) ) != PSA_SUCCESS )
8039 {
8040 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8041 goto exit;
8042 }
8043
8044 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8045 hashlen ) ) != PSA_SUCCESS )
8046 {
8047 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8048 goto exit;
8049 }
8050
8051exit:
8052 if( status != PSA_SUCCESS )
8053 {
8054 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8055 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8056 switch( status )
8057 {
8058 case PSA_ERROR_NOT_SUPPORTED:
8059 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8060 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8061 case PSA_ERROR_BUFFER_TOO_SMALL:
8062 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8063 case PSA_ERROR_INSUFFICIENT_MEMORY:
8064 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8065 default:
8066 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8067 }
8068 }
8069 return( 0 );
8070}
8071
8072#else
8073
8074int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8075 unsigned char *hash, size_t *hashlen,
8076 unsigned char *data, size_t data_len,
8077 mbedtls_md_type_t md_alg )
8078{
8079 int ret = 0;
8080 mbedtls_md_context_t ctx;
8081 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8082 *hashlen = mbedtls_md_get_size( md_info );
8083
8084 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8085
8086 mbedtls_md_init( &ctx );
8087
8088 /*
8089 * digitally-signed struct {
8090 * opaque client_random[32];
8091 * opaque server_random[32];
8092 * ServerDHParams params;
8093 * };
8094 */
8095 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8096 {
8097 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8098 goto exit;
8099 }
8100 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8101 {
8102 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8103 goto exit;
8104 }
8105 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8106 {
8107 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8108 goto exit;
8109 }
8110 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8111 {
8112 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8113 goto exit;
8114 }
8115 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8116 {
8117 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8118 goto exit;
8119 }
8120
8121exit:
8122 mbedtls_md_free( &ctx );
8123
8124 if( ret != 0 )
8125 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8126 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8127
8128 return( ret );
8129}
8130#endif /* MBEDTLS_USE_PSA_CRYPTO */
8131
Jerry Yud9d91da2022-02-17 14:57:06 +08008132#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8133
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008134/* Find the preferred hash for a given signature algorithm. */
8135unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8136 mbedtls_ssl_context *ssl,
8137 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008138{
Gabor Mezei078e8032022-04-27 21:17:56 +02008139 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008140 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008141
8142 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008143 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008144
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008145 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008146 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008147 unsigned int hash_alg_received =
8148 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8149 received_sig_algs[i] );
8150 unsigned int sig_alg_received =
8151 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8152 received_sig_algs[i] );
8153
8154 if( sig_alg == sig_alg_received )
8155 {
8156#if defined(MBEDTLS_USE_PSA_CRYPTO)
8157 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8158 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008159 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008160 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008161
Neil Armstrong96eceb82022-06-30 18:05:05 +02008162 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8163 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8164 PSA_ALG_ECDSA( psa_hash_alg ),
8165 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008166 continue;
8167
Neil Armstrong96eceb82022-06-30 18:05:05 +02008168 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008169 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8170 PSA_ALG_RSA_PKCS1V15_SIGN(
8171 psa_hash_alg ),
8172 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008173 continue;
8174 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008175#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008176
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008177 return( hash_alg_received );
8178 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008179 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008180
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008181 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008182}
8183
8184#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8185
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008186/* Serialization of TLS 1.2 sessions:
8187 *
8188 * struct {
8189 * uint64 start_time;
8190 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008191 * uint8 session_id_len; // at most 32
8192 * opaque session_id[32];
8193 * opaque master[48]; // fixed length in the standard
8194 * uint32 verify_result;
8195 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8196 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8197 * uint32 ticket_lifetime;
8198 * uint8 mfl_code; // up to 255 according to standard
8199 * uint8 encrypt_then_mac; // 0 or 1
8200 * } serialized_session_tls12;
8201 *
8202 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008203static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008204 unsigned char *buf,
8205 size_t buf_len )
8206{
8207 unsigned char *p = buf;
8208 size_t used = 0;
8209
8210#if defined(MBEDTLS_HAVE_TIME)
8211 uint64_t start;
8212#endif
8213#if defined(MBEDTLS_X509_CRT_PARSE_C)
8214#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8215 size_t cert_len;
8216#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8217#endif /* MBEDTLS_X509_CRT_PARSE_C */
8218
8219 /*
8220 * Time
8221 */
8222#if defined(MBEDTLS_HAVE_TIME)
8223 used += 8;
8224
8225 if( used <= buf_len )
8226 {
8227 start = (uint64_t) session->start;
8228
8229 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8230 p += 8;
8231 }
8232#endif /* MBEDTLS_HAVE_TIME */
8233
8234 /*
8235 * Basic mandatory fields
8236 */
8237 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008238 + 1 /* id_len */
8239 + sizeof( session->id )
8240 + sizeof( session->master )
8241 + 4; /* verify_result */
8242
8243 if( used <= buf_len )
8244 {
8245 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8246 p += 2;
8247
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008248 *p++ = MBEDTLS_BYTE_0( session->id_len );
8249 memcpy( p, session->id, 32 );
8250 p += 32;
8251
8252 memcpy( p, session->master, 48 );
8253 p += 48;
8254
8255 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8256 p += 4;
8257 }
8258
8259 /*
8260 * Peer's end-entity certificate
8261 */
8262#if defined(MBEDTLS_X509_CRT_PARSE_C)
8263#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8264 if( session->peer_cert == NULL )
8265 cert_len = 0;
8266 else
8267 cert_len = session->peer_cert->raw.len;
8268
8269 used += 3 + cert_len;
8270
8271 if( used <= buf_len )
8272 {
8273 *p++ = MBEDTLS_BYTE_2( cert_len );
8274 *p++ = MBEDTLS_BYTE_1( cert_len );
8275 *p++ = MBEDTLS_BYTE_0( cert_len );
8276
8277 if( session->peer_cert != NULL )
8278 {
8279 memcpy( p, session->peer_cert->raw.p, cert_len );
8280 p += cert_len;
8281 }
8282 }
8283#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8284 if( session->peer_cert_digest != NULL )
8285 {
8286 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8287 if( used <= buf_len )
8288 {
8289 *p++ = (unsigned char) session->peer_cert_digest_type;
8290 *p++ = (unsigned char) session->peer_cert_digest_len;
8291 memcpy( p, session->peer_cert_digest,
8292 session->peer_cert_digest_len );
8293 p += session->peer_cert_digest_len;
8294 }
8295 }
8296 else
8297 {
8298 used += 2;
8299 if( used <= buf_len )
8300 {
8301 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8302 *p++ = 0;
8303 }
8304 }
8305#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8306#endif /* MBEDTLS_X509_CRT_PARSE_C */
8307
8308 /*
8309 * Session ticket if any, plus associated data
8310 */
8311#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8312 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8313
8314 if( used <= buf_len )
8315 {
8316 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8317 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8318 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8319
8320 if( session->ticket != NULL )
8321 {
8322 memcpy( p, session->ticket, session->ticket_len );
8323 p += session->ticket_len;
8324 }
8325
8326 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8327 p += 4;
8328 }
8329#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8330
8331 /*
8332 * Misc extension-related info
8333 */
8334#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8335 used += 1;
8336
8337 if( used <= buf_len )
8338 *p++ = session->mfl_code;
8339#endif
8340
8341#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8342 used += 1;
8343
8344 if( used <= buf_len )
8345 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8346#endif
8347
8348 return( used );
8349}
8350
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008351MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008352static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008353 const unsigned char *buf,
8354 size_t len )
8355{
8356#if defined(MBEDTLS_HAVE_TIME)
8357 uint64_t start;
8358#endif
8359#if defined(MBEDTLS_X509_CRT_PARSE_C)
8360#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8361 size_t cert_len;
8362#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8363#endif /* MBEDTLS_X509_CRT_PARSE_C */
8364
8365 const unsigned char *p = buf;
8366 const unsigned char * const end = buf + len;
8367
8368 /*
8369 * Time
8370 */
8371#if defined(MBEDTLS_HAVE_TIME)
8372 if( 8 > (size_t)( end - p ) )
8373 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8374
8375 start = ( (uint64_t) p[0] << 56 ) |
8376 ( (uint64_t) p[1] << 48 ) |
8377 ( (uint64_t) p[2] << 40 ) |
8378 ( (uint64_t) p[3] << 32 ) |
8379 ( (uint64_t) p[4] << 24 ) |
8380 ( (uint64_t) p[5] << 16 ) |
8381 ( (uint64_t) p[6] << 8 ) |
8382 ( (uint64_t) p[7] );
8383 p += 8;
8384
8385 session->start = (time_t) start;
8386#endif /* MBEDTLS_HAVE_TIME */
8387
8388 /*
8389 * Basic mandatory fields
8390 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008391 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008392 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8393
8394 session->ciphersuite = ( p[0] << 8 ) | p[1];
8395 p += 2;
8396
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008397 session->id_len = *p++;
8398 memcpy( session->id, p, 32 );
8399 p += 32;
8400
8401 memcpy( session->master, p, 48 );
8402 p += 48;
8403
8404 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8405 ( (uint32_t) p[1] << 16 ) |
8406 ( (uint32_t) p[2] << 8 ) |
8407 ( (uint32_t) p[3] );
8408 p += 4;
8409
8410 /* Immediately clear invalid pointer values that have been read, in case
8411 * we exit early before we replaced them with valid ones. */
8412#if defined(MBEDTLS_X509_CRT_PARSE_C)
8413#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8414 session->peer_cert = NULL;
8415#else
8416 session->peer_cert_digest = NULL;
8417#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8418#endif /* MBEDTLS_X509_CRT_PARSE_C */
8419#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8420 session->ticket = NULL;
8421#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8422
8423 /*
8424 * Peer certificate
8425 */
8426#if defined(MBEDTLS_X509_CRT_PARSE_C)
8427#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8428 /* Deserialize CRT from the end of the ticket. */
8429 if( 3 > (size_t)( end - p ) )
8430 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8431
8432 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8433 p += 3;
8434
8435 if( cert_len != 0 )
8436 {
8437 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8438
8439 if( cert_len > (size_t)( end - p ) )
8440 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8441
8442 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8443
8444 if( session->peer_cert == NULL )
8445 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8446
8447 mbedtls_x509_crt_init( session->peer_cert );
8448
8449 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8450 p, cert_len ) ) != 0 )
8451 {
8452 mbedtls_x509_crt_free( session->peer_cert );
8453 mbedtls_free( session->peer_cert );
8454 session->peer_cert = NULL;
8455 return( ret );
8456 }
8457
8458 p += cert_len;
8459 }
8460#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8461 /* Deserialize CRT digest from the end of the ticket. */
8462 if( 2 > (size_t)( end - p ) )
8463 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8464
8465 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8466 session->peer_cert_digest_len = (size_t) *p++;
8467
8468 if( session->peer_cert_digest_len != 0 )
8469 {
8470 const mbedtls_md_info_t *md_info =
8471 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8472 if( md_info == NULL )
8473 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8474 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8475 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8476
8477 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8478 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8479
8480 session->peer_cert_digest =
8481 mbedtls_calloc( 1, session->peer_cert_digest_len );
8482 if( session->peer_cert_digest == NULL )
8483 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8484
8485 memcpy( session->peer_cert_digest, p,
8486 session->peer_cert_digest_len );
8487 p += session->peer_cert_digest_len;
8488 }
8489#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8490#endif /* MBEDTLS_X509_CRT_PARSE_C */
8491
8492 /*
8493 * Session ticket and associated data
8494 */
8495#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8496 if( 3 > (size_t)( end - p ) )
8497 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8498
8499 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8500 p += 3;
8501
8502 if( session->ticket_len != 0 )
8503 {
8504 if( session->ticket_len > (size_t)( end - p ) )
8505 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8506
8507 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8508 if( session->ticket == NULL )
8509 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8510
8511 memcpy( session->ticket, p, session->ticket_len );
8512 p += session->ticket_len;
8513 }
8514
8515 if( 4 > (size_t)( end - p ) )
8516 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8517
8518 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8519 ( (uint32_t) p[1] << 16 ) |
8520 ( (uint32_t) p[2] << 8 ) |
8521 ( (uint32_t) p[3] );
8522 p += 4;
8523#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8524
8525 /*
8526 * Misc extension-related info
8527 */
8528#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8529 if( 1 > (size_t)( end - p ) )
8530 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8531
8532 session->mfl_code = *p++;
8533#endif
8534
8535#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8536 if( 1 > (size_t)( end - p ) )
8537 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8538
8539 session->encrypt_then_mac = *p++;
8540#endif
8541
8542 /* Done, should have consumed entire buffer */
8543 if( p != end )
8544 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8545
8546 return( 0 );
8547}
Jerry Yudc7bd172022-02-17 13:44:15 +08008548#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8549
XiaokangQian75d40ef2022-04-20 11:05:24 +00008550int mbedtls_ssl_validate_ciphersuite(
8551 const mbedtls_ssl_context *ssl,
8552 const mbedtls_ssl_ciphersuite_t *suite_info,
8553 mbedtls_ssl_protocol_version min_tls_version,
8554 mbedtls_ssl_protocol_version max_tls_version )
8555{
8556 (void) ssl;
8557
8558 if( suite_info == NULL )
8559 return( -1 );
8560
8561 if( ( suite_info->min_tls_version > max_tls_version ) ||
8562 ( suite_info->max_tls_version < min_tls_version ) )
8563 {
8564 return( -1 );
8565 }
8566
XiaokangQian060d8672022-04-21 09:24:56 +00008567#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008568#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8569 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8570 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8571 {
8572 return( -1 );
8573 }
8574#endif
8575
8576 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8577#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8578 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8579 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8580 {
8581 return( -1 );
8582 }
8583#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8584#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8585
8586 return( 0 );
8587}
8588
XiaokangQianeaf36512022-04-24 09:07:44 +00008589#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8590/*
8591 * Function for writing a signature algorithm extension.
8592 *
8593 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8594 * value (TLS 1.3 RFC8446):
8595 * enum {
8596 * ....
8597 * ecdsa_secp256r1_sha256( 0x0403 ),
8598 * ecdsa_secp384r1_sha384( 0x0503 ),
8599 * ecdsa_secp521r1_sha512( 0x0603 ),
8600 * ....
8601 * } SignatureScheme;
8602 *
8603 * struct {
8604 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8605 * } SignatureSchemeList;
8606 *
8607 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8608 * value (TLS 1.2 RFC5246):
8609 * enum {
8610 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8611 * sha512(6), (255)
8612 * } HashAlgorithm;
8613 *
8614 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8615 * SignatureAlgorithm;
8616 *
8617 * struct {
8618 * HashAlgorithm hash;
8619 * SignatureAlgorithm signature;
8620 * } SignatureAndHashAlgorithm;
8621 *
8622 * SignatureAndHashAlgorithm
8623 * supported_signature_algorithms<2..2^16-2>;
8624 *
8625 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8626 * generalization of the TLS 1.2 signature algorithm extension.
8627 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8628 * `SignatureScheme` field of TLS 1.3
8629 *
8630 */
8631int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8632 const unsigned char *end, size_t *out_len )
8633{
8634 unsigned char *p = buf;
8635 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8636 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8637
8638 *out_len = 0;
8639
8640 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8641
8642 /* Check if we have space for header and length field:
8643 * - extension_type (2 bytes)
8644 * - extension_data_length (2 bytes)
8645 * - supported_signature_algorithms_length (2 bytes)
8646 */
8647 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8648 p += 6;
8649
8650 /*
8651 * Write supported_signature_algorithms
8652 */
8653 supported_sig_alg = p;
8654 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8655 if( sig_alg == NULL )
8656 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8657
8658 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8659 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008660 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8661 *sig_alg,
8662 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008663 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8664 continue;
8665 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8666 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8667 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008668 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008669 *sig_alg,
8670 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008671 }
8672
8673 /* Length of supported_signature_algorithms */
8674 supported_sig_alg_len = p - supported_sig_alg;
8675 if( supported_sig_alg_len == 0 )
8676 {
8677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8678 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8679 }
8680
XiaokangQianeaf36512022-04-24 09:07:44 +00008681 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008682 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008683 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8684
XiaokangQianeaf36512022-04-24 09:07:44 +00008685 *out_len = p - buf;
8686
8687#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8688 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8689#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8690 return( 0 );
8691}
8692#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8693
XiaokangQian40a35232022-05-07 09:02:40 +00008694#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008695/*
8696 * mbedtls_ssl_parse_server_name_ext
8697 *
8698 * Structure of server_name extension:
8699 *
8700 * enum {
8701 * host_name(0), (255)
8702 * } NameType;
8703 * opaque HostName<1..2^16-1>;
8704 *
8705 * struct {
8706 * NameType name_type;
8707 * select (name_type) {
8708 * case host_name: HostName;
8709 * } name;
8710 * } ServerName;
8711 * struct {
8712 * ServerName server_name_list<1..2^16-1>
8713 * } ServerNameList;
8714 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008715MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008716int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8717 const unsigned char *buf,
8718 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008719{
8720 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8721 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008722 size_t server_name_list_len, hostname_len;
8723 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008724
XiaokangQianf2a94202022-05-20 06:44:24 +00008725 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008726
8727 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008728 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008729 p += 2;
8730
XiaokangQian9b2b7712022-05-17 02:57:00 +00008731 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8732 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008733 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008734 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008735 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008736 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008737 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8738 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008739
8740 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8741 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008742 /* sni_name is intended to be used only during the parsing of the
8743 * ClientHello message (it is reset to NULL before the end of
8744 * the message parsing). Thus it is ok to just point to the
8745 * reception buffer and not make a copy of it.
8746 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008747 ssl->handshake->sni_name = p + 3;
8748 ssl->handshake->sni_name_len = hostname_len;
8749 if( ssl->conf->f_sni == NULL )
8750 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008751 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008752 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008753 if( ret != 0 )
8754 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008755 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008756 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8757 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008758 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8759 }
8760 return( 0 );
8761 }
8762
8763 p += hostname_len + 3;
8764 }
8765
8766 return( 0 );
8767}
8768#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8769
XiaokangQianacb39922022-06-17 10:18:48 +00008770#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008771MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008772int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8773 const unsigned char *buf,
8774 const unsigned char *end )
8775{
8776 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008777 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008778 const unsigned char *protocol_name_list;
8779 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008780 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008781
8782 /* If ALPN not configured, just ignore the extension */
8783 if( ssl->conf->alpn_list == NULL )
8784 return( 0 );
8785
8786 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008787 * RFC7301, section 3.1
8788 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008789 *
XiaokangQianc7403452022-06-23 03:24:12 +00008790 * struct {
8791 * ProtocolName protocol_name_list<2..2^16-1>
8792 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008793 */
8794
XiaokangQianc7403452022-06-23 03:24:12 +00008795 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008796 * protocol_name_list_len 2 bytes
8797 * protocol_name_len 1 bytes
8798 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008799 */
XiaokangQianacb39922022-06-17 10:18:48 +00008800 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8801
XiaokangQianc7403452022-06-23 03:24:12 +00008802 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008803 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008804 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008805 protocol_name_list = p;
8806 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008807
8808 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008809 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008810 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008811 protocol_name_len = *p++;
8812 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8813 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008814 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008815 {
8816 MBEDTLS_SSL_PEND_FATAL_ALERT(
8817 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8818 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008819 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008820 }
8821
8822 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008823 }
8824
8825 /* Use our order of preference */
8826 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8827 {
8828 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008829 p = protocol_name_list;
8830 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008831 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008832 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008833 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008834 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008835 {
8836 ssl->alpn_chosen = *alpn;
8837 return( 0 );
8838 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008839
8840 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008841 }
8842 }
8843
XiaokangQian95d5f542022-06-24 02:29:26 +00008844 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008845 MBEDTLS_SSL_PEND_FATAL_ALERT(
8846 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8847 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8848 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8849}
8850
8851int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8852 unsigned char *buf,
8853 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008854 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008855{
8856 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008857 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008858 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008859
8860 if( ssl->alpn_chosen == NULL )
8861 {
8862 return( 0 );
8863 }
8864
XiaokangQian95d5f542022-06-24 02:29:26 +00008865 protocol_name_len = strlen( ssl->alpn_chosen );
8866 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008867
8868 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8869 /*
8870 * 0 . 1 ext identifier
8871 * 2 . 3 ext length
8872 * 4 . 5 protocol list length
8873 * 6 . 6 protocol name length
8874 * 7 . 7+n protocol name
8875 */
8876 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8877
XiaokangQian95d5f542022-06-24 02:29:26 +00008878 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008879
XiaokangQian95d5f542022-06-24 02:29:26 +00008880 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8881 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008882 /* Note: the length of the chosen protocol has been checked to be less
8883 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8884 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008885 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008886
XiaokangQian95d5f542022-06-24 02:29:26 +00008887 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008888 return ( 0 );
8889}
8890#endif /* MBEDTLS_SSL_ALPN */
8891
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008892#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00008893 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00008894 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00008895 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008896int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
8897 const char *hostname )
8898{
8899 /* Initialize to suppress unnecessary compiler warning */
8900 size_t hostname_len = 0;
8901
8902 /* Check if new hostname is valid before
8903 * making any change to current one */
8904 if( hostname != NULL )
8905 {
8906 hostname_len = strlen( hostname );
8907
8908 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8909 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8910 }
8911
8912 /* Now it's clear that we will overwrite the old hostname,
8913 * so we can free it safely */
8914 if( session->hostname != NULL )
8915 {
8916 mbedtls_platform_zeroize( session->hostname,
8917 strlen( session->hostname ) );
8918 mbedtls_free( session->hostname );
8919 }
8920
8921 /* Passing NULL as hostname shall clear the old one */
8922 if( hostname == NULL )
8923 {
8924 session->hostname = NULL;
8925 }
8926 else
8927 {
8928 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
8929 if( session->hostname == NULL )
8930 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8931
8932 memcpy( session->hostname, hostname, hostname_len );
8933 }
8934
8935 return( 0 );
8936}
Xiaokang Qian03409292022-10-12 02:49:52 +00008937#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
8938 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00008939 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00008940 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008942#endif /* MBEDTLS_SSL_TLS_C */