blob: b62e2ac73f9f44c63cb0a3b2b832b52c09238342 [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)
Ronald Crond29e13e2022-10-19 10:33:48 +02001603int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001604{
Ronald Crond29e13e2022-10-19 10:33:48 +02001605 if( conf->psk_identity == NULL ||
1606 conf->psk_identity_len == 0 )
1607 {
1608 return( 0 );
1609 }
1610
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001611#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Crond29e13e2022-10-19 10:33:48 +02001612 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001613 return( 1 );
1614#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02001615
1616 if( conf->psk != NULL && conf->psk_len != 0 )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001617 return( 1 );
1618
1619 return( 0 );
1620}
1621
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001622static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1623{
1624 /* Remove reference to existing PSK, if any. */
1625#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001626 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001627 {
1628 /* The maintenance of the PSK key slot is the
1629 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001630 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001631 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001632#endif /* MBEDTLS_USE_PSA_CRYPTO */
1633 if( conf->psk != NULL )
1634 {
1635 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1636
1637 mbedtls_free( conf->psk );
1638 conf->psk = NULL;
1639 conf->psk_len = 0;
1640 }
1641
1642 /* Remove reference to PSK identity, if any. */
1643 if( conf->psk_identity != NULL )
1644 {
1645 mbedtls_free( conf->psk_identity );
1646 conf->psk_identity = NULL;
1647 conf->psk_identity_len = 0;
1648 }
1649}
1650
Hanno Becker7390c712018-11-15 13:33:04 +00001651/* This function assumes that PSK identity in the SSL config is unset.
1652 * It checks that the provided identity is well-formed and attempts
1653 * to make a copy of it in the SSL config.
1654 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001655MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001656static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1657 unsigned char const *psk_identity,
1658 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001659{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001660 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001661 if( psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02001662 psk_identity_len == 0 ||
Hanno Becker7390c712018-11-15 13:33:04 +00001663 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001664 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001665 {
1666 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1667 }
1668
Hanno Becker7390c712018-11-15 13:33:04 +00001669 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1670 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001671 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001672
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001673 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001674 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001675
1676 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001677}
1678
Hanno Becker7390c712018-11-15 13:33:04 +00001679int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1680 const unsigned char *psk, size_t psk_len,
1681 const unsigned char *psk_identity, size_t psk_identity_len )
1682{
Janos Follath865b3eb2019-12-16 11:46:15 +00001683 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001684
1685 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02001686 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001687 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001688
1689 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001690 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001691 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001692 if( psk_len == 0 )
1693 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1694 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1695 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1696
Hanno Becker7390c712018-11-15 13:33:04 +00001697 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1698 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1699 conf->psk_len = psk_len;
1700 memcpy( conf->psk, psk, conf->psk_len );
1701
1702 /* Check and set PSK Identity */
1703 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1704 if( ret != 0 )
1705 ssl_conf_remove_psk( conf );
1706
1707 return( ret );
1708}
1709
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001710static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1711{
1712#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001713 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001714 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001715 /* The maintenance of the external PSK key slot is the
1716 * user's responsibility. */
1717 if( ssl->handshake->psk_opaque_is_internal )
1718 {
1719 psa_destroy_key( ssl->handshake->psk_opaque );
1720 ssl->handshake->psk_opaque_is_internal = 0;
1721 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001722 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001723 }
Neil Armstronge952a302022-05-03 10:22:14 +02001724#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001725 if( ssl->handshake->psk != NULL )
1726 {
1727 mbedtls_platform_zeroize( ssl->handshake->psk,
1728 ssl->handshake->psk_len );
1729 mbedtls_free( ssl->handshake->psk );
1730 ssl->handshake->psk_len = 0;
1731 }
Neil Armstronge952a302022-05-03 10:22:14 +02001732#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001733}
1734
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001735int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1736 const unsigned char *psk, size_t psk_len )
1737{
Neil Armstrong501c9322022-05-03 09:35:09 +02001738#if defined(MBEDTLS_USE_PSA_CRYPTO)
1739 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001740 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001741 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001742 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001743#endif /* MBEDTLS_USE_PSA_CRYPTO */
1744
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001745 if( psk == NULL || ssl->handshake == NULL )
1746 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1747
1748 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1749 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1750
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001751 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001752
Neil Armstrong501c9322022-05-03 09:35:09 +02001753#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001754#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1755 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1756 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001757 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001758 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1759 else
1760 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1761 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1762 }
1763#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001764
Jerry Yu568ec252022-07-22 21:27:34 +08001765#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001766 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1767 {
1768 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1769 psa_set_key_usage_flags( &key_attributes,
1770 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1771 }
1772#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1773
Neil Armstrong501c9322022-05-03 09:35:09 +02001774 psa_set_key_algorithm( &key_attributes, alg );
1775 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1776
1777 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1778 if( status != PSA_SUCCESS )
1779 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1780
1781 /* Allow calling psa_destroy_key() on psk remove */
1782 ssl->handshake->psk_opaque_is_internal = 1;
1783 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1784#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001785 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001786 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001787
1788 ssl->handshake->psk_len = psk_len;
1789 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1790
1791 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001792#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001793}
1794
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001795#if defined(MBEDTLS_USE_PSA_CRYPTO)
1796int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001797 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001798 const unsigned char *psk_identity,
1799 size_t psk_identity_len )
1800{
Janos Follath865b3eb2019-12-16 11:46:15 +00001801 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001802
1803 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02001804 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001805 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001806
Hanno Becker7390c712018-11-15 13:33:04 +00001807 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001808 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001809 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001810 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001811
1812 /* Check and set PSK Identity */
1813 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1814 psk_identity_len );
1815 if( ret != 0 )
1816 ssl_conf_remove_psk( conf );
1817
1818 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001819}
1820
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001821int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1822 mbedtls_svc_key_id_t psk )
1823{
1824 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1825 ( ssl->handshake == NULL ) )
1826 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1827
1828 ssl_remove_psk( ssl );
1829 ssl->handshake->psk_opaque = psk;
1830 return( 0 );
1831}
1832#endif /* MBEDTLS_USE_PSA_CRYPTO */
1833
Jerry Yu8897c072022-08-12 13:56:53 +08001834#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001835void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1836 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1837 size_t),
1838 void *p_psk )
1839{
1840 conf->f_psk = f_psk;
1841 conf->p_psk = p_psk;
1842}
Jerry Yu8897c072022-08-12 13:56:53 +08001843#endif /* MBEDTLS_SSL_SRV_C */
1844
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001845#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1846
1847#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001848static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1849 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001850{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001851#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001852 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001853 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001854#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001855 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001856 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001857 return( MBEDTLS_SSL_MODE_STREAM );
1858}
1859
1860#else /* MBEDTLS_USE_PSA_CRYPTO */
1861
1862static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1863 mbedtls_cipher_mode_t mode )
1864{
1865#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1866 if( mode == MBEDTLS_MODE_CBC )
1867 return( MBEDTLS_SSL_MODE_CBC );
1868#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1869
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001870#if defined(MBEDTLS_GCM_C) || \
1871 defined(MBEDTLS_CCM_C) || \
1872 defined(MBEDTLS_CHACHAPOLY_C)
1873 if( mode == MBEDTLS_MODE_GCM ||
1874 mode == MBEDTLS_MODE_CCM ||
1875 mode == MBEDTLS_MODE_CHACHAPOLY )
1876 return( MBEDTLS_SSL_MODE_AEAD );
1877#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001878
1879 return( MBEDTLS_SSL_MODE_STREAM );
1880}
Gilles Peskine301711e2022-04-26 16:57:05 +02001881#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001882
Gilles Peskinee108d982022-04-26 16:50:40 +02001883static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1884 mbedtls_ssl_mode_t base_mode,
1885 int encrypt_then_mac )
1886{
1887#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1888 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1889 base_mode == MBEDTLS_SSL_MODE_CBC )
1890 {
1891 return( MBEDTLS_SSL_MODE_CBC_ETM );
1892 }
1893#else
1894 (void) encrypt_then_mac;
1895#endif
1896 return( base_mode );
1897}
1898
Neil Armstrongab555e02022-04-04 11:07:59 +02001899mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001900 const mbedtls_ssl_transform *transform )
1901{
Gilles Peskinee108d982022-04-26 16:50:40 +02001902 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001903#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001904 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001905#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001906 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1907#endif
1908 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001909
Gilles Peskinee108d982022-04-26 16:50:40 +02001910 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001911#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001912 encrypt_then_mac = transform->encrypt_then_mac;
1913#endif
1914 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001915}
1916
Neil Armstrongab555e02022-04-04 11:07:59 +02001917mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001918#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001919 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001920#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001921 const mbedtls_ssl_ciphersuite_t *suite )
1922{
Gilles Peskinee108d982022-04-26 16:50:40 +02001923 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1924
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001925#if defined(MBEDTLS_USE_PSA_CRYPTO)
1926 psa_status_t status;
1927 psa_algorithm_t alg;
1928 psa_key_type_t type;
1929 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001930 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1931 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001932 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001933#else
1934 const mbedtls_cipher_info_t *cipher =
1935 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001936 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001937 {
1938 base_mode =
1939 mbedtls_ssl_get_base_mode(
1940 mbedtls_cipher_info_get_mode( cipher ) );
1941 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001942#endif /* MBEDTLS_USE_PSA_CRYPTO */
1943
Gilles Peskinee108d982022-04-26 16:50:40 +02001944#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1945 int encrypt_then_mac = 0;
1946#endif
1947 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001948}
1949
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001950#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001951
1952#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001953/* Serialization of TLS 1.3 sessions:
1954 *
1955 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00001956 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001957 * uint64 ticket_received;
1958 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08001959 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001960 * } ClientOnlyData;
1961 *
1962 * struct {
1963 * uint8 endpoint;
1964 * uint8 ciphersuite[2];
1965 * uint32 ticket_age_add;
1966 * uint8 ticket_flags;
1967 * opaque resumption_key<0..255>;
1968 * select ( endpoint ) {
1969 * case client: ClientOnlyData;
1970 * case server: uint64 start_time;
1971 * };
1972 * } serialized_session_tls13;
1973 *
1974 */
1975#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08001976MBEDTLS_CHECK_RETURN_CRITICAL
1977static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
1978 unsigned char *buf,
1979 size_t buf_len,
1980 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00001981{
1982 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00001983#if defined(MBEDTLS_SSL_CLI_C) && \
1984 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1985 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00001986 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00001987#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08001988 size_t needed = 1 /* endpoint */
1989 + 2 /* ciphersuite */
1990 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08001991 + 1 /* ticket_flags */
1992 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08001993 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08001994
1995 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
1996 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1997 needed += session->resumption_key_len; /* resumption_key */
1998
Jerry Yu438ddd82022-07-07 06:55:50 +00001999#if defined(MBEDTLS_HAVE_TIME)
2000 needed += 8; /* start_time or ticket_received */
2001#endif
2002
2003#if defined(MBEDTLS_SSL_CLI_C)
2004 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2005 {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002006#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002007 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002008 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002009#endif
2010
Jerry Yu438ddd82022-07-07 06:55:50 +00002011 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002012 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002013
2014 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002015 if( session->ticket_len > SIZE_MAX - needed )
2016 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002017
Jerry Yue28d9742022-08-18 15:44:03 +08002018 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002019 }
2020#endif /* MBEDTLS_SSL_CLI_C */
2021
Jerry Yue36fdd62022-08-17 21:31:36 +08002022 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002023 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002024 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002025
2026 p[0] = session->endpoint;
2027 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2028 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2029 p[7] = session->ticket_flags;
2030
2031 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002032 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002033 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002034 memcpy( p, session->resumption_key, session->resumption_key_len );
2035 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002036
2037#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2038 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2039 {
2040 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2041 p += 8;
2042 }
2043#endif /* MBEDTLS_HAVE_TIME */
2044
2045#if defined(MBEDTLS_SSL_CLI_C)
2046 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2047 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002048#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2049 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2050 p += 2;
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002051 if( hostname_len > 0 )
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002052 {
2053 /* save host name */
2054 memcpy( p, session->hostname, hostname_len );
2055 p += hostname_len;
2056 }
2057#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2058
Jerry Yu438ddd82022-07-07 06:55:50 +00002059#if defined(MBEDTLS_HAVE_TIME)
2060 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2061 p += 8;
2062#endif
2063 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2064 p += 4;
2065
2066 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2067 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002068
2069 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002070 {
2071 memcpy( p, session->ticket, session->ticket_len );
2072 p += session->ticket_len;
2073 }
2074 }
2075#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002076 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002077}
2078
2079MBEDTLS_CHECK_RETURN_CRITICAL
2080static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2081 const unsigned char *buf,
2082 size_t len )
2083{
2084 const unsigned char *p = buf;
2085 const unsigned char *end = buf + len;
2086
2087 if( end - p < 9 )
2088 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2089 session->endpoint = p[0];
2090 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2091 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2092 session->ticket_flags = p[7];
2093
2094 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002095 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002096 p += 9;
2097
Jerry Yubc7c1a42022-07-21 22:57:37 +08002098 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002099 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2100
Jerry Yubc7c1a42022-07-21 22:57:37 +08002101 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002103 memcpy( session->resumption_key, p, session->resumption_key_len );
2104 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002105
2106#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2107 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2108 {
2109 if( end - p < 8 )
2110 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2111 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2112 p += 8;
2113 }
2114#endif /* MBEDTLS_HAVE_TIME */
2115
2116#if defined(MBEDTLS_SSL_CLI_C)
2117 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2118 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002119#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2120 defined(MBEDTLS_SSL_SESSION_TICKETS)
2121 size_t hostname_len;
2122 /* load host name */
2123 if( end - p < 2 )
2124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2125 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2126 p += 2;
2127
2128 if( end - p < ( long int )hostname_len )
2129 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2130 if( hostname_len > 0 )
2131 {
2132 session->hostname = mbedtls_calloc( 1, hostname_len );
2133 if( session->hostname == NULL )
2134 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2135 memcpy( session->hostname, p, hostname_len );
2136 p += hostname_len;
2137 }
2138#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2139 MBEDTLS_SSL_SESSION_TICKETS */
2140
Jerry Yu438ddd82022-07-07 06:55:50 +00002141#if defined(MBEDTLS_HAVE_TIME)
2142 if( end - p < 8 )
2143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2144 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2145 p += 8;
2146#endif
2147 if( end - p < 4 )
2148 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2149 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2150 p += 4;
2151
2152 if( end - p < 2 )
2153 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2154 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2155 p += 2;
2156
2157 if( end - p < ( long int )session->ticket_len )
2158 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2159 if( session->ticket_len > 0 )
2160 {
2161 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2162 if( session->ticket == NULL )
2163 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2164 memcpy( session->ticket, p, session->ticket_len );
2165 p += session->ticket_len;
2166 }
2167 }
2168#endif /* MBEDTLS_SSL_CLI_C */
2169
2170 return( 0 );
2171
2172}
2173#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002174MBEDTLS_CHECK_RETURN_CRITICAL
2175static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2176 unsigned char *buf,
2177 size_t buf_len,
2178 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002179{
2180 ((void) session);
2181 ((void) buf);
2182 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002183 *olen = 0;
2184 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002185}
Jerry Yu438ddd82022-07-07 06:55:50 +00002186
2187static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2188 unsigned char *buf,
2189 size_t buf_len )
2190{
2191 ((void) session);
2192 ((void) buf);
2193 ((void) buf_len);
2194 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2195}
2196#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002197#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2198
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002199psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002200 size_t taglen,
2201 psa_algorithm_t *alg,
2202 psa_key_type_t *key_type,
2203 size_t *key_size )
2204{
2205 switch ( mbedtls_cipher_type )
2206 {
2207 case MBEDTLS_CIPHER_AES_128_CBC:
2208 *alg = PSA_ALG_CBC_NO_PADDING;
2209 *key_type = PSA_KEY_TYPE_AES;
2210 *key_size = 128;
2211 break;
2212 case MBEDTLS_CIPHER_AES_128_CCM:
2213 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2214 *key_type = PSA_KEY_TYPE_AES;
2215 *key_size = 128;
2216 break;
2217 case MBEDTLS_CIPHER_AES_128_GCM:
2218 *alg = PSA_ALG_GCM;
2219 *key_type = PSA_KEY_TYPE_AES;
2220 *key_size = 128;
2221 break;
2222 case MBEDTLS_CIPHER_AES_192_CCM:
2223 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2224 *key_type = PSA_KEY_TYPE_AES;
2225 *key_size = 192;
2226 break;
2227 case MBEDTLS_CIPHER_AES_192_GCM:
2228 *alg = PSA_ALG_GCM;
2229 *key_type = PSA_KEY_TYPE_AES;
2230 *key_size = 192;
2231 break;
2232 case MBEDTLS_CIPHER_AES_256_CBC:
2233 *alg = PSA_ALG_CBC_NO_PADDING;
2234 *key_type = PSA_KEY_TYPE_AES;
2235 *key_size = 256;
2236 break;
2237 case MBEDTLS_CIPHER_AES_256_CCM:
2238 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2239 *key_type = PSA_KEY_TYPE_AES;
2240 *key_size = 256;
2241 break;
2242 case MBEDTLS_CIPHER_AES_256_GCM:
2243 *alg = PSA_ALG_GCM;
2244 *key_type = PSA_KEY_TYPE_AES;
2245 *key_size = 256;
2246 break;
2247 case MBEDTLS_CIPHER_ARIA_128_CBC:
2248 *alg = PSA_ALG_CBC_NO_PADDING;
2249 *key_type = PSA_KEY_TYPE_ARIA;
2250 *key_size = 128;
2251 break;
2252 case MBEDTLS_CIPHER_ARIA_128_CCM:
2253 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2254 *key_type = PSA_KEY_TYPE_ARIA;
2255 *key_size = 128;
2256 break;
2257 case MBEDTLS_CIPHER_ARIA_128_GCM:
2258 *alg = PSA_ALG_GCM;
2259 *key_type = PSA_KEY_TYPE_ARIA;
2260 *key_size = 128;
2261 break;
2262 case MBEDTLS_CIPHER_ARIA_192_CCM:
2263 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2264 *key_type = PSA_KEY_TYPE_ARIA;
2265 *key_size = 192;
2266 break;
2267 case MBEDTLS_CIPHER_ARIA_192_GCM:
2268 *alg = PSA_ALG_GCM;
2269 *key_type = PSA_KEY_TYPE_ARIA;
2270 *key_size = 192;
2271 break;
2272 case MBEDTLS_CIPHER_ARIA_256_CBC:
2273 *alg = PSA_ALG_CBC_NO_PADDING;
2274 *key_type = PSA_KEY_TYPE_ARIA;
2275 *key_size = 256;
2276 break;
2277 case MBEDTLS_CIPHER_ARIA_256_CCM:
2278 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2279 *key_type = PSA_KEY_TYPE_ARIA;
2280 *key_size = 256;
2281 break;
2282 case MBEDTLS_CIPHER_ARIA_256_GCM:
2283 *alg = PSA_ALG_GCM;
2284 *key_type = PSA_KEY_TYPE_ARIA;
2285 *key_size = 256;
2286 break;
2287 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2288 *alg = PSA_ALG_CBC_NO_PADDING;
2289 *key_type = PSA_KEY_TYPE_CAMELLIA;
2290 *key_size = 128;
2291 break;
2292 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2293 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2294 *key_type = PSA_KEY_TYPE_CAMELLIA;
2295 *key_size = 128;
2296 break;
2297 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2298 *alg = PSA_ALG_GCM;
2299 *key_type = PSA_KEY_TYPE_CAMELLIA;
2300 *key_size = 128;
2301 break;
2302 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2303 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2304 *key_type = PSA_KEY_TYPE_CAMELLIA;
2305 *key_size = 192;
2306 break;
2307 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2308 *alg = PSA_ALG_GCM;
2309 *key_type = PSA_KEY_TYPE_CAMELLIA;
2310 *key_size = 192;
2311 break;
2312 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2313 *alg = PSA_ALG_CBC_NO_PADDING;
2314 *key_type = PSA_KEY_TYPE_CAMELLIA;
2315 *key_size = 256;
2316 break;
2317 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2318 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2319 *key_type = PSA_KEY_TYPE_CAMELLIA;
2320 *key_size = 256;
2321 break;
2322 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2323 *alg = PSA_ALG_GCM;
2324 *key_type = PSA_KEY_TYPE_CAMELLIA;
2325 *key_size = 256;
2326 break;
2327 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2328 *alg = PSA_ALG_CHACHA20_POLY1305;
2329 *key_type = PSA_KEY_TYPE_CHACHA20;
2330 *key_size = 256;
2331 break;
2332 case MBEDTLS_CIPHER_NULL:
2333 *alg = MBEDTLS_SSL_NULL_CIPHER;
2334 *key_type = 0;
2335 *key_size = 0;
2336 break;
2337 default:
2338 return PSA_ERROR_NOT_SUPPORTED;
2339 }
2340
2341 return PSA_SUCCESS;
2342}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002343#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002344
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002345#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002346int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2347 const unsigned char *dhm_P, size_t P_len,
2348 const unsigned char *dhm_G, size_t G_len )
2349{
Janos Follath865b3eb2019-12-16 11:46:15 +00002350 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002351
Glenn Strausscee11292021-12-20 01:43:17 -05002352 mbedtls_mpi_free( &conf->dhm_P );
2353 mbedtls_mpi_free( &conf->dhm_G );
2354
Hanno Beckera90658f2017-10-04 15:29:08 +01002355 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2356 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2357 {
2358 mbedtls_mpi_free( &conf->dhm_P );
2359 mbedtls_mpi_free( &conf->dhm_G );
2360 return( ret );
2361 }
2362
2363 return( 0 );
2364}
Paul Bakker5121ce52009-01-03 21:22:43 +00002365
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002366int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002367{
Janos Follath865b3eb2019-12-16 11:46:15 +00002368 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002369
Glenn Strausscee11292021-12-20 01:43:17 -05002370 mbedtls_mpi_free( &conf->dhm_P );
2371 mbedtls_mpi_free( &conf->dhm_G );
2372
Gilles Peskinee5702482021-06-11 21:59:08 +02002373 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2374 &conf->dhm_P ) ) != 0 ||
2375 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2376 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002377 {
2378 mbedtls_mpi_free( &conf->dhm_P );
2379 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002380 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002381 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002382
2383 return( 0 );
2384}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002385#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002386
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002387#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2388/*
2389 * Set the minimum length for Diffie-Hellman parameters
2390 */
2391void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2392 unsigned int bitlen )
2393{
2394 conf->dhm_min_bitlen = bitlen;
2395}
2396#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2397
Gilles Peskineeccd8882020-03-10 12:19:08 +01002398#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002399#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002400/*
2401 * Set allowed/preferred hashes for handshake signatures
2402 */
2403void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2404 const int *hashes )
2405{
2406 conf->sig_hashes = hashes;
2407}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002408#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002409
Jerry Yuf017ee42022-01-12 15:49:48 +08002410/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002411void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2412 const uint16_t* sig_algs )
2413{
Jerry Yuf017ee42022-01-12 15:49:48 +08002414#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2415 conf->sig_hashes = NULL;
2416#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2417 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002418}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002419#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002420
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002421#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002422#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002423/*
2424 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002425 *
2426 * mbedtls_ssl_setup() takes the provided list
2427 * and translates it to a list of IANA TLS group identifiers,
2428 * stored in ssl->handshake->group_list.
2429 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002430 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002431void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002432 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002433{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002434 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002435 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002436}
Brett Warrene0edc842021-08-17 09:53:13 +01002437#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002438#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002439
Brett Warrene0edc842021-08-17 09:53:13 +01002440/*
2441 * Set the allowed groups
2442 */
2443void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2444 const uint16_t *group_list )
2445{
2446#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2447 conf->curve_list = NULL;
2448#endif
2449 conf->group_list = group_list;
2450}
2451
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002452#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002454{
Hanno Becker947194e2017-04-07 13:25:49 +01002455 /* Initialize to suppress unnecessary compiler warning */
2456 size_t hostname_len = 0;
2457
2458 /* Check if new hostname is valid before
2459 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002460 if( hostname != NULL )
2461 {
2462 hostname_len = strlen( hostname );
2463
2464 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2465 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2466 }
2467
2468 /* Now it's clear that we will overwrite the old hostname,
2469 * so we can free it safely */
2470
2471 if( ssl->hostname != NULL )
2472 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002473 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002474 mbedtls_free( ssl->hostname );
2475 }
2476
2477 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002478
Paul Bakker5121ce52009-01-03 21:22:43 +00002479 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002480 {
2481 ssl->hostname = NULL;
2482 }
2483 else
2484 {
2485 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002486 if( ssl->hostname == NULL )
2487 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002488
Hanno Becker947194e2017-04-07 13:25:49 +01002489 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002490
Hanno Becker947194e2017-04-07 13:25:49 +01002491 ssl->hostname[hostname_len] = '\0';
2492 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002493
2494 return( 0 );
2495}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002496#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002497
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002498#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002499void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002501 const unsigned char *, size_t),
2502 void *p_sni )
2503{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002504 conf->f_sni = f_sni;
2505 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002506}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002510int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002511{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002512 size_t cur_len, tot_len;
2513 const char **p;
2514
2515 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002516 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2517 * MUST NOT be truncated."
2518 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002519 */
2520 tot_len = 0;
2521 for( p = protos; *p != NULL; p++ )
2522 {
2523 cur_len = strlen( *p );
2524 tot_len += cur_len;
2525
Ronald Cron8216dd32020-04-23 16:41:44 +02002526 if( ( cur_len == 0 ) ||
2527 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2528 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002530 }
2531
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002532 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002533
2534 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002535}
2536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002538{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002539 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002540}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002542
Johan Pascalb62bb512015-12-03 21:56:45 +01002543#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002544void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2545 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002546{
2547 conf->dtls_srtp_mki_support = support_mki_value;
2548}
2549
Ron Eldoref72faf2018-07-12 11:54:20 +03002550int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2551 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002552 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002553{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002554 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002555 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002556 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002557 }
Ron Eldor591f1622018-01-22 12:30:04 +02002558
2559 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002560 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002561 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002562 }
Ron Eldor591f1622018-01-22 12:30:04 +02002563
2564 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2565 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002566 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002567}
2568
Ron Eldoref72faf2018-07-12 11:54:20 +03002569int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002570 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002571{
Johan Pascal253d0262020-09-22 13:04:45 +02002572 const mbedtls_ssl_srtp_profile *p;
2573 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002574
Johan Pascal253d0262020-09-22 13:04:45 +02002575 /* check the profiles list: all entry must be valid,
2576 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002577 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2578 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2579 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002580 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002581 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002582 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002583 list_size++;
2584 }
2585 else
2586 {
2587 /* unsupported value, stop parsing and set the size to an error value */
2588 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002589 }
2590 }
2591
Johan Pascal5ef72d22020-10-28 17:05:47 +01002592 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002593 {
Johan Pascal253d0262020-09-22 13:04:45 +02002594 conf->dtls_srtp_profile_list = NULL;
2595 conf->dtls_srtp_profile_list_len = 0;
2596 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2597 }
2598
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002599 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002600 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002601
2602 return( 0 );
2603}
2604
Johan Pascal5ef72d22020-10-28 17:05:47 +01002605void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2606 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002607{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002608 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2609 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002610 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002611 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002612 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002613 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002614 else
2615 {
2616 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002617 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2618 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002619 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002620}
Johan Pascalb62bb512015-12-03 21:56:45 +01002621#endif /* MBEDTLS_SSL_DTLS_SRTP */
2622
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302623#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002624void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002625{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002626 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002627}
2628
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002629void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002630{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002631 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002632}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302633#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002634
Janos Follath088ce432017-04-10 12:42:31 +01002635#if defined(MBEDTLS_SSL_SRV_C)
2636void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2637 char cert_req_ca_list )
2638{
2639 conf->cert_req_ca_list = cert_req_ca_list;
2640}
2641#endif
2642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002644void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002645{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002646 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002647}
2648#endif
2649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002651void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002652{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002653 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002654}
2655#endif
2656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002658int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002659{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002661 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002663 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002664 }
2665
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002666 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002667
2668 return( 0 );
2669}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002671
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002672void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002673{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002674 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002675}
2676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002678void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002679{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002680 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002681}
2682
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002683void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002684{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002685 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002686}
2687
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002688void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002689 const unsigned char period[8] )
2690{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002691 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002692}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002696#if defined(MBEDTLS_SSL_CLI_C)
2697void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002698{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002699 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002700}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002701#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002702
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002703#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002704
Jerry Yud0766ec2022-09-22 10:46:57 +08002705#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002706void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2707 uint16_t num_tickets )
2708{
Jerry Yud0766ec2022-09-22 10:46:57 +08002709 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002710}
2711#endif
2712
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002713void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2714 mbedtls_ssl_ticket_write_t *f_ticket_write,
2715 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2716 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002717{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002718 conf->f_ticket_write = f_ticket_write;
2719 conf->f_ticket_parse = f_ticket_parse;
2720 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002721}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002722#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002724
Hanno Becker7e6c1782021-06-08 09:24:55 +01002725void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2726 mbedtls_ssl_export_keys_t *f_export_keys,
2727 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002728{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002729 ssl->f_export_keys = f_export_keys;
2730 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002731}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002732
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002733#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002734void mbedtls_ssl_conf_async_private_cb(
2735 mbedtls_ssl_config *conf,
2736 mbedtls_ssl_async_sign_t *f_async_sign,
2737 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2738 mbedtls_ssl_async_resume_t *f_async_resume,
2739 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002740 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002741{
2742 conf->f_async_sign_start = f_async_sign;
2743 conf->f_async_decrypt_start = f_async_decrypt;
2744 conf->f_async_resume = f_async_resume;
2745 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002746 conf->p_async_config_data = async_config_data;
2747}
2748
Gilles Peskine8f97af72018-04-26 11:46:10 +02002749void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2750{
2751 return( conf->p_async_config_data );
2752}
2753
Gilles Peskine1febfef2018-04-30 11:54:39 +02002754void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002755{
2756 if( ssl->handshake == NULL )
2757 return( NULL );
2758 else
2759 return( ssl->handshake->user_async_ctx );
2760}
2761
Gilles Peskine1febfef2018-04-30 11:54:39 +02002762void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002763 void *ctx )
2764{
2765 if( ssl->handshake != NULL )
2766 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002767}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002768#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002769
Paul Bakker5121ce52009-01-03 21:22:43 +00002770/*
2771 * SSL get accessors
2772 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002773uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002774{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002775 if( ssl->session != NULL )
2776 return( ssl->session->verify_result );
2777
2778 if( ssl->session_negotiate != NULL )
2779 return( ssl->session_negotiate->verify_result );
2780
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002781 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002782}
2783
Glenn Strauss8f526902022-01-13 00:04:49 -05002784int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2785{
2786 if( ssl == NULL || ssl->session == NULL )
2787 return( 0 );
2788
2789 return( ssl->session->ciphersuite );
2790}
2791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002793{
Paul Bakker926c8e42013-03-06 10:23:34 +01002794 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002795 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002798}
2799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002801{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002803 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002804 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002805 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002806 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002807 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002808 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002809 default:
2810 return( "unknown (DTLS)" );
2811 }
2812 }
2813#endif
2814
Glenn Strauss60bfe602022-03-14 19:04:24 -04002815 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002816 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002817 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002818 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002819 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002820 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002821 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002822 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002823 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002824}
2825
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002826#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002827size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2828{
David Horstmann95d516f2021-05-04 18:36:56 +01002829 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002830 size_t read_mfl;
2831
2832 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2833 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2834 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2835 {
2836 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2837 }
2838
2839 /* Check if a smaller max length was negotiated */
2840 if( ssl->session_out != NULL )
2841 {
2842 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2843 if( read_mfl < max_len )
2844 {
2845 max_len = read_mfl;
2846 }
2847 }
2848
2849 // During a handshake, use the value being negotiated
2850 if( ssl->session_negotiate != NULL )
2851 {
2852 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2853 if( read_mfl < max_len )
2854 {
2855 max_len = read_mfl;
2856 }
2857 }
2858
2859 return( max_len );
2860}
2861
2862size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002863{
2864 size_t max_len;
2865
2866 /*
2867 * Assume mfl_code is correct since it was checked when set
2868 */
Angus Grattond8213d02016-05-25 20:56:48 +10002869 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002870
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002871 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002872 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002873 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002874 {
Angus Grattond8213d02016-05-25 20:56:48 +10002875 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002876 }
2877
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002878 /* During a handshake, use the value being negotiated */
2879 if( ssl->session_negotiate != NULL &&
2880 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2881 {
2882 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2883 }
2884
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002885 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002886}
2887#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2888
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002889#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002890size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002891{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002892 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2893 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2894 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2895 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2896 return ( 0 );
2897
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002898 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2899 return( ssl->mtu );
2900
2901 if( ssl->mtu == 0 )
2902 return( ssl->handshake->mtu );
2903
2904 return( ssl->mtu < ssl->handshake->mtu ?
2905 ssl->mtu : ssl->handshake->mtu );
2906}
2907#endif /* MBEDTLS_SSL_PROTO_DTLS */
2908
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002909int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2910{
2911 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2912
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002913#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2914 !defined(MBEDTLS_SSL_PROTO_DTLS)
2915 (void) ssl;
2916#endif
2917
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002918#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002919 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002920
2921 if( max_len > mfl )
2922 max_len = mfl;
2923#endif
2924
2925#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002926 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002927 {
Hanno Becker89490712020-02-05 10:50:12 +00002928 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002929 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2930 const size_t overhead = (size_t) ret;
2931
2932 if( ret < 0 )
2933 return( ret );
2934
2935 if( mtu <= overhead )
2936 {
2937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2938 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2939 }
2940
2941 if( max_len > mtu - overhead )
2942 max_len = mtu - overhead;
2943 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002944#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002945
Hanno Becker0defedb2018-08-10 12:35:02 +01002946#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2947 !defined(MBEDTLS_SSL_PROTO_DTLS)
2948 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002949#endif
2950
2951 return( (int) max_len );
2952}
2953
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002954int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2955{
2956 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2957
2958#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2959 (void) ssl;
2960#endif
2961
2962#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2963 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2964
2965 if( max_len > mfl )
2966 max_len = mfl;
2967#endif
2968
2969 return( (int) max_len );
2970}
2971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972#if defined(MBEDTLS_X509_CRT_PARSE_C)
2973const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002974{
2975 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002976 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002977
Hanno Beckere6824572019-02-07 13:18:46 +00002978#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002979 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002980#else
2981 return( NULL );
2982#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002983}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002984#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002987int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2988 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002989{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002990 int ret;
2991
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002992 if( ssl == NULL ||
2993 dst == NULL ||
2994 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002995 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002998 }
2999
Hanno Beckere810bbc2021-05-14 16:01:05 +01003000 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3001 * idempotent: Each session can only be exported once.
3002 *
3003 * (This is in preparation for TLS 1.3 support where we will
3004 * need the ability to export multiple sessions (aka tickets),
3005 * which will be achieved by calling mbedtls_ssl_get_session()
3006 * multiple times until it fails.)
3007 *
3008 * Check whether we have already exported the current session,
3009 * and fail if so.
3010 */
3011 if( ssl->session->exported == 1 )
3012 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3013
3014 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3015 if( ret != 0 )
3016 return( ret );
3017
3018 /* Remember that we've exported the session. */
3019 ssl->session->exported = 1;
3020 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003021}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003023
Paul Bakker5121ce52009-01-03 21:22:43 +00003024/*
Hanno Beckera835da52019-05-16 12:39:07 +01003025 * Define ticket header determining Mbed TLS version
3026 * and structure of the ticket.
3027 */
3028
Hanno Becker94ef3b32019-05-16 12:50:45 +01003029/*
Hanno Becker50b59662019-05-28 14:30:45 +01003030 * Define bitflag determining compile-time settings influencing
3031 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003032 */
3033
Hanno Becker50b59662019-05-28 14:30:45 +01003034#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003035#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003036#else
Hanno Becker3e088662019-05-29 11:10:18 +01003037#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003038#endif /* MBEDTLS_HAVE_TIME */
3039
3040#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003041#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003042#else
Hanno Becker3e088662019-05-29 11:10:18 +01003043#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003044#endif /* MBEDTLS_X509_CRT_PARSE_C */
3045
3046#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003047#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003048#else
Hanno Becker3e088662019-05-29 11:10:18 +01003049#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003050#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3051
3052#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003053#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003054#else
Hanno Becker3e088662019-05-29 11:10:18 +01003055#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003056#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3057
Hanno Becker94ef3b32019-05-16 12:50:45 +01003058#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003059#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003060#else
Hanno Becker3e088662019-05-29 11:10:18 +01003061#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003062#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3063
Hanno Becker94ef3b32019-05-16 12:50:45 +01003064#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3065#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3066#else
3067#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3068#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3069
Hanno Becker3e088662019-05-29 11:10:18 +01003070#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3071#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3072#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3073#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003074#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3075#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003076
Hanno Becker50b59662019-05-28 14:30:45 +01003077#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003078 ( (uint16_t) ( \
3079 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3080 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3081 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3082 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003083 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003084 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003085
Hanno Beckerf8787072019-05-16 12:41:07 +01003086static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003087 MBEDTLS_VERSION_MAJOR,
3088 MBEDTLS_VERSION_MINOR,
3089 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003090 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3091 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003092};
Hanno Beckera835da52019-05-16 12:39:07 +01003093
3094/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003095 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003096 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003097 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003098 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003099 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003100 * opaque mbedtls_version[3]; // library version: major, minor, patch
3101 * opaque session_format[2]; // library-version specific 16-bit field
3102 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003103 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003104 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003105 * Note: When updating the format, remember to keep
3106 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003107 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003108 * // In this version, `session_format` determines
3109 * // the setting of those compile-time
3110 * // configuration options which influence
3111 * // the structure of mbedtls_ssl_session.
3112 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003113 * uint8_t minor_ver; // Protocol minor version. Possible values:
3114 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003115 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003116 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003117 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003118 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003119 * serialized_session_tls12 data;
3120 *
3121 * };
3122 *
3123 * } serialized_session;
3124 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003125 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003126
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003127MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003128static int ssl_session_save( const mbedtls_ssl_session *session,
3129 unsigned char omit_header,
3130 unsigned char *buf,
3131 size_t buf_len,
3132 size_t *olen )
3133{
3134 unsigned char *p = buf;
3135 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003136 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003137#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3138 size_t out_len;
3139 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3140#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003141 if( session == NULL )
3142 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3143
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003144 if( !omit_header )
3145 {
3146 /*
3147 * Add Mbed TLS version identifier
3148 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003149 used += sizeof( ssl_serialized_session_header );
3150
3151 if( used <= buf_len )
3152 {
3153 memcpy( p, ssl_serialized_session_header,
3154 sizeof( ssl_serialized_session_header ) );
3155 p += sizeof( ssl_serialized_session_header );
3156 }
3157 }
3158
3159 /*
3160 * TLS version identifier
3161 */
3162 used += 1;
3163 if( used <= buf_len )
3164 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003165 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003166 }
3167
3168 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003169 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003170 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003171 {
3172#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003173 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003174 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003175 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003176#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3177
Jerry Yu251a12e2022-07-13 15:15:48 +08003178#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3179 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003180 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3181 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003182 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003183 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003184 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003185#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3186
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003187 default:
3188 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3189 }
3190
3191 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003192 if( used > buf_len )
3193 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003194
3195 return( 0 );
3196}
3197
3198/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003199 * Public wrapper for ssl_session_save()
3200 */
3201int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3202 unsigned char *buf,
3203 size_t buf_len,
3204 size_t *olen )
3205{
3206 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3207}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003208
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003209/*
3210 * Deserialize session, see mbedtls_ssl_session_save() for format.
3211 *
3212 * This internal version is wrapped by a public function that cleans up in
3213 * case of error, and has an extra option omit_header.
3214 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003215MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003216static int ssl_session_load( mbedtls_ssl_session *session,
3217 unsigned char omit_header,
3218 const unsigned char *buf,
3219 size_t len )
3220{
3221 const unsigned char *p = buf;
3222 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003223 size_t remaining_len;
3224
3225
3226 if( session == NULL )
3227 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003228
3229 if( !omit_header )
3230 {
3231 /*
3232 * Check Mbed TLS version identifier
3233 */
3234
3235 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3236 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3237
3238 if( memcmp( p, ssl_serialized_session_header,
3239 sizeof( ssl_serialized_session_header ) ) != 0 )
3240 {
3241 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3242 }
3243 p += sizeof( ssl_serialized_session_header );
3244 }
3245
3246 /*
3247 * TLS version identifier
3248 */
3249 if( 1 > (size_t)( end - p ) )
3250 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003251 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003252
3253 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003254 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003255 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003256 {
3257#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003258 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003259 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003260#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3261
Jerry Yu438ddd82022-07-07 06:55:50 +00003262#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3263 case MBEDTLS_SSL_VERSION_TLS1_3:
3264 return( ssl_tls13_session_load( session, p, remaining_len ) );
3265#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3266
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003267 default:
3268 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3269 }
3270}
3271
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003272/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003273 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003274 */
3275int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3276 const unsigned char *buf,
3277 size_t len )
3278{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003279 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003280
3281 if( ret != 0 )
3282 mbedtls_ssl_session_free( session );
3283
3284 return( ret );
3285}
3286
3287/*
Paul Bakker1961b702013-01-25 14:49:24 +01003288 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003289 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003290MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003291static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3292{
3293 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3294
Ronald Cron66dbf912022-02-02 15:33:46 +01003295 /*
3296 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003297 * that were written into the output buffer by the previous handshake step,
3298 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003299 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3300 * We proceed to the next handshake step only when all data from the
3301 * previous one have been sent to the peer, thus we make sure that this is
3302 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3303 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3304 * we have to wait before to go ahead.
3305 * In the case of TLS 1.3, handshake step handlers do not send data to the
3306 * peer. Data are only sent here and through
3307 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003308 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003309 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003310 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3311 return( ret );
3312
3313#if defined(MBEDTLS_SSL_PROTO_DTLS)
3314 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3315 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3316 {
3317 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3318 return( ret );
3319 }
3320#endif /* MBEDTLS_SSL_PROTO_DTLS */
3321
3322 return( ret );
3323}
3324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003325int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003326{
Hanno Becker41934dd2021-08-07 19:13:43 +01003327 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003328
Hanno Becker41934dd2021-08-07 19:13:43 +01003329 if( ssl == NULL ||
3330 ssl->conf == NULL ||
3331 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003332 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003333 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003334 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003335 }
3336
3337 ret = ssl_prepare_handshake_step( ssl );
3338 if( ret != 0 )
3339 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003340
Jerry Yue7047812021-09-13 19:26:39 +08003341 ret = mbedtls_ssl_handle_pending_alert( ssl );
3342 if( ret != 0 )
3343 goto cleanup;
3344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003346 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003347 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3349 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003350
Ronald Cron9f0fba32022-02-10 16:45:15 +01003351 switch( ssl->state )
3352 {
3353 case MBEDTLS_SSL_HELLO_REQUEST:
3354 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3355 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003356
Ronald Cron9f0fba32022-02-10 16:45:15 +01003357 case MBEDTLS_SSL_CLIENT_HELLO:
3358 ret = mbedtls_ssl_write_client_hello( ssl );
3359 break;
3360
3361 default:
3362#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003363 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003364 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3365 else
3366 ret = mbedtls_ssl_handshake_client_step( ssl );
3367#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3368 ret = mbedtls_ssl_handshake_client_step( ssl );
3369#else
3370 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3371#endif
3372 }
Jerry Yub9930e72021-08-06 17:11:51 +08003373 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003374#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003376 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003377 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003378#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003379 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003380 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003381#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003382
3383#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3384 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3385 ret = mbedtls_ssl_handshake_server_step( ssl );
3386#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3387 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003388#endif
3389
Jerry Yue7047812021-09-13 19:26:39 +08003390 if( ret != 0 )
3391 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003392 /* handshake_step return error. And it is same
3393 * with alert_reason.
3394 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003395 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003396 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003397 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003398 goto cleanup;
3399 }
3400 }
3401
3402cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003403 return( ret );
3404}
3405
3406/*
3407 * Perform the SSL handshake
3408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003410{
3411 int ret = 0;
3412
Hanno Beckera817ea42020-10-20 15:20:23 +01003413 /* Sanity checks */
3414
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003415 if( ssl == NULL || ssl->conf == NULL )
3416 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3417
Hanno Beckera817ea42020-10-20 15:20:23 +01003418#if defined(MBEDTLS_SSL_PROTO_DTLS)
3419 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3420 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3421 {
3422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3423 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3424 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3425 }
3426#endif /* MBEDTLS_SSL_PROTO_DTLS */
3427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003429
Hanno Beckera817ea42020-10-20 15:20:23 +01003430 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003431 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003434
3435 if( ret != 0 )
3436 break;
3437 }
3438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003440
3441 return( ret );
3442}
3443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003444#if defined(MBEDTLS_SSL_RENEGOTIATION)
3445#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003446/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003447 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003448 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003449MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003450static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003451{
Janos Follath865b3eb2019-12-16 11:46:15 +00003452 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003455
3456 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003457 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3458 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003459
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003460 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003461 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003462 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003463 return( ret );
3464 }
3465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003467
3468 return( 0 );
3469}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003471
3472/*
3473 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474 * - any side: calling mbedtls_ssl_renegotiate(),
3475 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3476 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003477 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003478 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003480 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003481int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003482{
Janos Follath865b3eb2019-12-16 11:46:15 +00003483 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003486
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003487 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3488 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003489
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003490 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3491 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003493 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003495 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003496 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003497 ssl->handshake->out_msg_seq = 1;
3498 else
3499 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003500 }
3501#endif
3502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3504 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003508 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003509 return( ret );
3510 }
3511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003513
3514 return( 0 );
3515}
3516
3517/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003518 * Renegotiate current connection on client,
3519 * or request renegotiation on server
3520 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003522{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003524
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003525 if( ssl == NULL || ssl->conf == NULL )
3526 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003528#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003529 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003530 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003531 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003532 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003535 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003536
3537 /* Did we already try/start sending HelloRequest? */
3538 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003539 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003540
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003541 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003542 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003543#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003546 /*
3547 * On client, either start the renegotiation process or,
3548 * if already in progress, continue the handshake
3549 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003550 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003551 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003552 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003553 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003554
Hanno Becker40cdaa12020-02-05 10:48:27 +00003555 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003556 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003557 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003558 return( ret );
3559 }
3560 }
3561 else
3562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003565 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003566 return( ret );
3567 }
3568 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003570
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003571 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003572}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003574
Gilles Peskine9b562d52018-04-25 20:32:43 +02003575void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003576{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003577 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3578
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003579 if( handshake == NULL )
3580 return;
3581
Brett Warrene0edc842021-08-17 09:53:13 +01003582#if defined(MBEDTLS_ECP_C)
3583#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3584 if ( ssl->handshake->group_list_heap_allocated )
3585 mbedtls_free( (void*) handshake->group_list );
3586 handshake->group_list = NULL;
3587#endif /* MBEDTLS_DEPRECATED_REMOVED */
3588#endif /* MBEDTLS_ECP_C */
3589
Jerry Yuf017ee42022-01-12 15:49:48 +08003590#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3591#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3592 if ( ssl->handshake->sig_algs_heap_allocated )
3593 mbedtls_free( (void*) handshake->sig_algs );
3594 handshake->sig_algs = NULL;
3595#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003596#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3597 if( ssl->handshake->certificate_request_context )
3598 {
3599 mbedtls_free( (void*) handshake->certificate_request_context );
3600 }
3601#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003602#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3603
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003604#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3605 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3606 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003607 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003608 handshake->async_in_progress = 0;
3609 }
3610#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3611
Andrzej Kurek25f27152022-08-17 16:09:31 -04003612#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003613#if defined(MBEDTLS_USE_PSA_CRYPTO)
3614 psa_hash_abort( &handshake->fin_sha256_psa );
3615#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003616 mbedtls_sha256_free( &handshake->fin_sha256 );
3617#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003618#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003619#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003620#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003621 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003622#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003623 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003624#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003625#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627#if defined(MBEDTLS_DHM_C)
3628 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003629#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003630#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003631 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003632#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003633#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003634 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003635#if defined(MBEDTLS_SSL_CLI_C)
3636 mbedtls_free( handshake->ecjpake_cache );
3637 handshake->ecjpake_cache = NULL;
3638 handshake->ecjpake_cache_len = 0;
3639#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003640#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003641
Janos Follath4ae5c292016-02-10 11:27:43 +00003642#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3643 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003644 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003645 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003646#endif
3647
Gilles Peskineeccd8882020-03-10 12:19:08 +01003648#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003649#if defined(MBEDTLS_USE_PSA_CRYPTO)
3650 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3651 {
3652 /* The maintenance of the external PSK key slot is the
3653 * user's responsibility. */
3654 if( ssl->handshake->psk_opaque_is_internal )
3655 {
3656 psa_destroy_key( ssl->handshake->psk_opaque );
3657 ssl->handshake->psk_opaque_is_internal = 0;
3658 }
3659 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3660 }
Neil Armstronge952a302022-05-03 10:22:14 +02003661#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003662 if( handshake->psk != NULL )
3663 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003664 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003665 mbedtls_free( handshake->psk );
3666 }
Neil Armstronge952a302022-05-03 10:22:14 +02003667#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003668#endif
3669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003670#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3671 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003672 /*
3673 * Free only the linked list wrapper, not the keys themselves
3674 * since the belong to the SNI callback
3675 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003676 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003678
Gilles Peskineeccd8882020-03-10 12:19:08 +01003679#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003680 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003681 if( handshake->ecrs_peer_cert != NULL )
3682 {
3683 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3684 mbedtls_free( handshake->ecrs_peer_cert );
3685 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003686#endif
3687
Hanno Becker75173122019-02-06 16:18:31 +00003688#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3689 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3690 mbedtls_pk_free( &handshake->peer_pubkey );
3691#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3692
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003693#if defined(MBEDTLS_SSL_CLI_C) && \
3694 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3695 mbedtls_free( handshake->cookie );
3696#endif /* MBEDTLS_SSL_CLI_C &&
3697 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003698
3699#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003700 mbedtls_ssl_flight_free( handshake->flight );
3701 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003702#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003703
Ronald Cronf12b81d2022-03-15 10:42:41 +01003704#if defined(MBEDTLS_ECDH_C) && \
3705 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003706 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003707 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003708#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3709
Ronald Cron6f135e12021-12-08 16:57:54 +01003710#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003711 mbedtls_ssl_transform_free( handshake->transform_handshake );
3712 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003713 mbedtls_free( handshake->transform_earlydata );
3714 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003715#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003716
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003717
3718#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3719 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3720 * processes datagrams and the fact that a datagram is allowed to have
3721 * several records in it, it is possible that the I/O buffers are not
3722 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003723 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3724 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003725#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003726
Jerry Yuba9c7272021-10-30 11:54:10 +08003727 /* mbedtls_platform_zeroize MUST be last one in this function */
3728 mbedtls_platform_zeroize( handshake,
3729 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003730}
3731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003732void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003733{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003734 if( session == NULL )
3735 return;
3736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003738 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003739#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003740
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003741#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00003742#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
3743 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00003744 mbedtls_free( session->hostname );
3745#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003746 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003747#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003748
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003749 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003750}
3751
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003752#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003753
3754#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3755#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3756#else
3757#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3758#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3759
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003760#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003761
3762#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3763#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3764#else
3765#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3766#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3767
3768#if defined(MBEDTLS_SSL_ALPN)
3769#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3770#else
3771#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3772#endif /* MBEDTLS_SSL_ALPN */
3773
3774#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3775#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3776#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3777#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3778
3779#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3780 ( (uint32_t) ( \
3781 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3782 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3783 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3784 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3785 0u ) )
3786
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003787static unsigned char ssl_serialized_context_header[] = {
3788 MBEDTLS_VERSION_MAJOR,
3789 MBEDTLS_VERSION_MINOR,
3790 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003791 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3792 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3793 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3794 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3795 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003796};
3797
Paul Bakker5121ce52009-01-03 21:22:43 +00003798/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003799 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003800 *
3801 * The format of the serialized data is:
3802 * (in the presentation language of TLS, RFC 8446 section 3)
3803 *
3804 * // header
3805 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003806 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003807 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003808 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003809 * Note: When updating the format, remember to keep these
3810 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003811 *
3812 * // session sub-structure
3813 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3814 * // transform sub-structure
3815 * uint8 random[64]; // ServerHello.random+ClientHello.random
3816 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3817 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3818 * // fields from ssl_context
3819 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3820 * uint64 in_window_top; // DTLS: last validated record seq_num
3821 * uint64 in_window; // DTLS: bitmask for replay protection
3822 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3823 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3824 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3825 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3826 *
3827 * Note that many fields of the ssl_context or sub-structures are not
3828 * serialized, as they fall in one of the following categories:
3829 *
3830 * 1. forced value (eg in_left must be 0)
3831 * 2. pointer to dynamically-allocated memory (eg session, transform)
3832 * 3. value can be re-derived from other data (eg session keys from MS)
3833 * 4. value was temporary (eg content of input buffer)
3834 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003835 */
3836int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3837 unsigned char *buf,
3838 size_t buf_len,
3839 size_t *olen )
3840{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003841 unsigned char *p = buf;
3842 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003843 size_t session_len;
3844 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003845
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003846 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003847 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3848 * this function's documentation.
3849 *
3850 * These are due to assumptions/limitations in the implementation. Some of
3851 * them are likely to stay (no handshake in progress) some might go away
3852 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003853 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003854 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003855 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003856 {
3857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003858 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003859 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003860 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003861 {
3862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
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 /* Double-check that sub-structures are indeed ready */
3866 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003867 {
3868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
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 /* There must be no pending incoming or outgoing data */
3872 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003873 {
3874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003875 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003876 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003877 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003878 {
3879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
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 /* Protocol must be DLTS, not TLS */
3883 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003884 {
3885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is 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 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003889 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003890 {
3891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 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 /* We must be using an AEAD ciphersuite */
3895 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003896 {
3897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003898 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003899 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003900 /* Renegotiation must not be enabled */
3901#if defined(MBEDTLS_SSL_RENEGOTIATION)
3902 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003903 {
3904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003905 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003906 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003907#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003908
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003909 /*
3910 * Version and format identifier
3911 */
3912 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003913
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003914 if( used <= buf_len )
3915 {
3916 memcpy( p, ssl_serialized_context_header,
3917 sizeof( ssl_serialized_context_header ) );
3918 p += sizeof( ssl_serialized_context_header );
3919 }
3920
3921 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003922 * Session (length + data)
3923 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003924 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003925 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3926 return( ret );
3927
3928 used += 4 + session_len;
3929 if( used <= buf_len )
3930 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003931 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3932 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003933
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003934 ret = ssl_session_save( ssl->session, 1,
3935 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003936 if( ret != 0 )
3937 return( ret );
3938
3939 p += session_len;
3940 }
3941
3942 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003943 * Transform
3944 */
3945 used += sizeof( ssl->transform->randbytes );
3946 if( used <= buf_len )
3947 {
3948 memcpy( p, ssl->transform->randbytes,
3949 sizeof( ssl->transform->randbytes ) );
3950 p += sizeof( ssl->transform->randbytes );
3951 }
3952
3953#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3954 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3955 if( used <= buf_len )
3956 {
3957 *p++ = ssl->transform->in_cid_len;
3958 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3959 p += ssl->transform->in_cid_len;
3960
3961 *p++ = ssl->transform->out_cid_len;
3962 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3963 p += ssl->transform->out_cid_len;
3964 }
3965#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3966
3967 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003968 * Saved fields from top-level ssl_context structure
3969 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003970 used += 4;
3971 if( used <= buf_len )
3972 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003973 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3974 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003975 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003976
3977#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3978 used += 16;
3979 if( used <= buf_len )
3980 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003981 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3982 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003983
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003984 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3985 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003986 }
3987#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3988
3989#if defined(MBEDTLS_SSL_PROTO_DTLS)
3990 used += 1;
3991 if( used <= buf_len )
3992 {
3993 *p++ = ssl->disable_datagram_packing;
3994 }
3995#endif /* MBEDTLS_SSL_PROTO_DTLS */
3996
Jerry Yuae0b2e22021-10-08 15:21:19 +08003997 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003998 if( used <= buf_len )
3999 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004000 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
4001 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004002 }
4003
4004#if defined(MBEDTLS_SSL_PROTO_DTLS)
4005 used += 2;
4006 if( used <= buf_len )
4007 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004008 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4009 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004010 }
4011#endif /* MBEDTLS_SSL_PROTO_DTLS */
4012
4013#if defined(MBEDTLS_SSL_ALPN)
4014 {
4015 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004016 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004017 : 0;
4018
4019 used += 1 + alpn_len;
4020 if( used <= buf_len )
4021 {
4022 *p++ = alpn_len;
4023
4024 if( ssl->alpn_chosen != NULL )
4025 {
4026 memcpy( p, ssl->alpn_chosen, alpn_len );
4027 p += alpn_len;
4028 }
4029 }
4030 }
4031#endif /* MBEDTLS_SSL_ALPN */
4032
4033 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004034 * Done
4035 */
4036 *olen = used;
4037
4038 if( used > buf_len )
4039 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004040
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004041 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4042
Hanno Becker43aefe22020-02-05 10:44:56 +00004043 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004044}
4045
4046/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004047 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004048 *
4049 * This internal version is wrapped by a public function that cleans up in
4050 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004051 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004052MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004053static int ssl_context_load( mbedtls_ssl_context *ssl,
4054 const unsigned char *buf,
4055 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004056{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004057 const unsigned char *p = buf;
4058 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004059 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004060 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004061#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004062 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004063#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004064
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004065 /*
4066 * The context should have been freshly setup or reset.
4067 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004068 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004069 * renegotiating, or if the user mistakenly loaded a session first.)
4070 */
4071 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4072 ssl->session != NULL )
4073 {
4074 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4075 }
4076
4077 /*
4078 * We can't check that the config matches the initial one, but we can at
4079 * least check it matches the requirements for serializing.
4080 */
4081 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004082 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4083 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004084#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004085 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004086#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004087 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004088 {
4089 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4090 }
4091
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004092 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4093
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004094 /*
4095 * Check version identifier
4096 */
4097 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4098 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4099
4100 if( memcmp( p, ssl_serialized_context_header,
4101 sizeof( ssl_serialized_context_header ) ) != 0 )
4102 {
4103 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4104 }
4105 p += sizeof( ssl_serialized_context_header );
4106
4107 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004108 * Session
4109 */
4110 if( (size_t)( end - p ) < 4 )
4111 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4112
4113 session_len = ( (size_t) p[0] << 24 ) |
4114 ( (size_t) p[1] << 16 ) |
4115 ( (size_t) p[2] << 8 ) |
4116 ( (size_t) p[3] );
4117 p += 4;
4118
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004119 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004120 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004121 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004122 ssl->session_in = ssl->session;
4123 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004124 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004125
4126 if( (size_t)( end - p ) < session_len )
4127 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4128
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004129 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004130 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004131 {
4132 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004133 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004134 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004135
4136 p += session_len;
4137
4138 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004139 * Transform
4140 */
4141
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004142 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004143 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004144 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004145 ssl->transform_in = ssl->transform;
4146 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004147 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004148
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004149#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004150 prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite );
4151 if( prf_func == NULL )
4152 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4153
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004154 /* Read random bytes and populate structure */
4155 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4156 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004157
Hanno Beckerbd257552021-03-22 06:59:27 +00004158 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004159 ssl->session->ciphersuite,
4160 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004161#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004162 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004163#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Andrzej Kurek894edde2022-09-29 06:31:14 -04004164 prf_func,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004165 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004166 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004167 ssl->conf->endpoint,
4168 ssl );
4169 if( ret != 0 )
4170 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004171#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004172 p += sizeof( ssl->transform->randbytes );
4173
4174#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4175 /* Read connection IDs and store them */
4176 if( (size_t)( end - p ) < 1 )
4177 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4178
4179 ssl->transform->in_cid_len = *p++;
4180
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004181 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004182 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4183
4184 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4185 p += ssl->transform->in_cid_len;
4186
4187 ssl->transform->out_cid_len = *p++;
4188
4189 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4190 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4191
4192 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4193 p += ssl->transform->out_cid_len;
4194#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4195
4196 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004197 * Saved fields from top-level ssl_context structure
4198 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004199 if( (size_t)( end - p ) < 4 )
4200 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4201
4202 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4203 ( (uint32_t) p[1] << 16 ) |
4204 ( (uint32_t) p[2] << 8 ) |
4205 ( (uint32_t) p[3] );
4206 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004207
4208#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4209 if( (size_t)( end - p ) < 16 )
4210 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4211
4212 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4213 ( (uint64_t) p[1] << 48 ) |
4214 ( (uint64_t) p[2] << 40 ) |
4215 ( (uint64_t) p[3] << 32 ) |
4216 ( (uint64_t) p[4] << 24 ) |
4217 ( (uint64_t) p[5] << 16 ) |
4218 ( (uint64_t) p[6] << 8 ) |
4219 ( (uint64_t) p[7] );
4220 p += 8;
4221
4222 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4223 ( (uint64_t) p[1] << 48 ) |
4224 ( (uint64_t) p[2] << 40 ) |
4225 ( (uint64_t) p[3] << 32 ) |
4226 ( (uint64_t) p[4] << 24 ) |
4227 ( (uint64_t) p[5] << 16 ) |
4228 ( (uint64_t) p[6] << 8 ) |
4229 ( (uint64_t) p[7] );
4230 p += 8;
4231#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4232
4233#if defined(MBEDTLS_SSL_PROTO_DTLS)
4234 if( (size_t)( end - p ) < 1 )
4235 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4236
4237 ssl->disable_datagram_packing = *p++;
4238#endif /* MBEDTLS_SSL_PROTO_DTLS */
4239
Jerry Yud9a94fe2021-09-28 18:58:59 +08004240 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004241 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004242 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4243 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004244
4245#if defined(MBEDTLS_SSL_PROTO_DTLS)
4246 if( (size_t)( end - p ) < 2 )
4247 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4248
4249 ssl->mtu = ( p[0] << 8 ) | p[1];
4250 p += 2;
4251#endif /* MBEDTLS_SSL_PROTO_DTLS */
4252
4253#if defined(MBEDTLS_SSL_ALPN)
4254 {
4255 uint8_t alpn_len;
4256 const char **cur;
4257
4258 if( (size_t)( end - p ) < 1 )
4259 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4260
4261 alpn_len = *p++;
4262
4263 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4264 {
4265 /* alpn_chosen should point to an item in the configured list */
4266 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4267 {
4268 if( strlen( *cur ) == alpn_len &&
4269 memcmp( p, cur, alpn_len ) == 0 )
4270 {
4271 ssl->alpn_chosen = *cur;
4272 break;
4273 }
4274 }
4275 }
4276
4277 /* can only happen on conf mismatch */
4278 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4279 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4280
4281 p += alpn_len;
4282 }
4283#endif /* MBEDTLS_SSL_ALPN */
4284
4285 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004286 * Forced fields from top-level ssl_context structure
4287 *
4288 * Most of them already set to the correct value by mbedtls_ssl_init() and
4289 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4290 */
4291 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004292 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004293
Hanno Becker361b10d2019-08-30 10:42:49 +01004294 /* Adjust pointers for header fields of outgoing records to
4295 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004296 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004297
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004298#if defined(MBEDTLS_SSL_PROTO_DTLS)
4299 ssl->in_epoch = 1;
4300#endif
4301
4302 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4303 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004304 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4305 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004306 if( ssl->handshake != NULL )
4307 {
4308 mbedtls_ssl_handshake_free( ssl );
4309 mbedtls_free( ssl->handshake );
4310 ssl->handshake = NULL;
4311 }
4312
4313 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004314 * Done - should have consumed entire buffer
4315 */
4316 if( p != end )
4317 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004318
4319 return( 0 );
4320}
4321
4322/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004323 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004324 */
4325int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4326 const unsigned char *buf,
4327 size_t len )
4328{
4329 int ret = ssl_context_load( context, buf, len );
4330
4331 if( ret != 0 )
4332 mbedtls_ssl_free( context );
4333
4334 return( ret );
4335}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004336#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004337
4338/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004339 * Free an SSL context
4340 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004341void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004342{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004343 if( ssl == NULL )
4344 return;
4345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004347
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004348 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004349 {
sander-visserb8aa2072020-05-06 22:05:13 +02004350#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4351 size_t out_buf_len = ssl->out_buf_len;
4352#else
4353 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4354#endif
4355
Darryl Greenb33cc762019-11-28 14:29:44 +00004356 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004357 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004358 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004359 }
4360
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004361 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004362 {
sander-visserb8aa2072020-05-06 22:05:13 +02004363#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4364 size_t in_buf_len = ssl->in_buf_len;
4365#else
4366 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4367#endif
4368
Darryl Greenb33cc762019-11-28 14:29:44 +00004369 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004370 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004371 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004372 }
4373
Paul Bakker48916f92012-09-16 19:57:18 +00004374 if( ssl->transform )
4375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004376 mbedtls_ssl_transform_free( ssl->transform );
4377 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004378 }
4379
4380 if( ssl->handshake )
4381 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004382 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004383 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4384 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004386 mbedtls_free( ssl->handshake );
4387 mbedtls_free( ssl->transform_negotiate );
4388 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004389 }
4390
Ronald Cron6f135e12021-12-08 16:57:54 +01004391#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004392 mbedtls_ssl_transform_free( ssl->transform_application );
4393 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004394#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004395
Paul Bakkerc0463502013-02-14 11:19:38 +01004396 if( ssl->session )
4397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004398 mbedtls_ssl_session_free( ssl->session );
4399 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004400 }
4401
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004402#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004403 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004404 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004405 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004406 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004407 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004408#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004409
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004410#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004411 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004412#endif
4413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004415
Paul Bakker86f04f42013-02-14 11:20:09 +01004416 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004417 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004418}
4419
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004420/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004421 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004422 */
4423void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4424{
4425 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4426}
4427
Gilles Peskineae270bf2021-06-02 00:05:29 +02004428/* The selection should be the same as mbedtls_x509_crt_profile_default in
4429 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004430 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004431 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004432 * about this list.
4433 */
Brett Warrene0edc842021-08-17 09:53:13 +01004434static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004435#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004436 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004437#endif
4438#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004439 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004440#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004441#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004442 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004443#endif
4444#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004445 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004446#endif
4447#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004448 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004449#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004450#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004451 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004452#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004453#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004454 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004455#endif
4456#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004457 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004458#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004459 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004460};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004461
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004462static int ssl_preset_suiteb_ciphersuites[] = {
4463 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4464 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4465 0
4466};
4467
Gilles Peskineeccd8882020-03-10 12:19:08 +01004468#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004469
Jerry Yu909df7b2022-01-22 11:56:27 +08004470/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004471 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004472 * rules SHOULD be upheld.
4473 * - No duplicate entries.
4474 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004475 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004476 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004477 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004478static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004479
Andrzej Kurek25f27152022-08-17 16:09:31 -04004480#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 +08004481 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4482 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004483#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004484 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004485
Andrzej Kurek25f27152022-08-17 16:09:31 -04004486#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 +08004487 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4488 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
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 Yu909df7b2022-01-22 11:56:27 +08004490 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4491
Andrzej Kurek25f27152022-08-17 16:09:31 -04004492#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 +08004493 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4494 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004495#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004496 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004497
Andrzej Kurek25f27152022-08-17 16:09:31 -04004498#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 +08004499 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004500#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 +08004501
Andrzej Kurek25f27152022-08-17 16:09:31 -04004502#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 +08004503 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004504#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 +08004505
Andrzej Kurek25f27152022-08-17 16:09:31 -04004506#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 +08004507 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004508#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 +08004509
Andrzej Kurek25f27152022-08-17 16:09:31 -04004510#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 +08004511 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4512#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4513
Andrzej Kurek25f27152022-08-17 16:09:31 -04004514#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 +08004515 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4516#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4517
Andrzej Kurek25f27152022-08-17 16:09:31 -04004518#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 +08004519 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4520#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4521
Gabor Mezei15b95a62022-05-09 16:37:58 +02004522 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004523};
4524
4525/* NOTICE: see above */
4526#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4527static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004528#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004529#if defined(MBEDTLS_ECDSA_C)
4530 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004531#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004532#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4533 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4534#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004535#if defined(MBEDTLS_RSA_C)
4536 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4537#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004538#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004539#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004540#if defined(MBEDTLS_ECDSA_C)
4541 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004542#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004543#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4544 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4545#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004546#if defined(MBEDTLS_RSA_C)
4547 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4548#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004549#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004550#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004551#if defined(MBEDTLS_ECDSA_C)
4552 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004553#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004554#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4555 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4556#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004557#if defined(MBEDTLS_RSA_C)
4558 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4559#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004560#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004561 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004562};
4563#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4564/* NOTICE: see above */
4565static uint16_t ssl_preset_suiteb_sig_algs[] = {
4566
Andrzej Kurek25f27152022-08-17 16:09:31 -04004567#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 +08004568 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4569 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004570#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004571 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4572
Andrzej Kurek25f27152022-08-17 16:09:31 -04004573#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 +08004574 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4575 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004576#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004577 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004578
Andrzej Kurek25f27152022-08-17 16:09:31 -04004579#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 +08004580 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004581#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 +08004582
Andrzej Kurek25f27152022-08-17 16:09:31 -04004583#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 +08004584 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004585#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004586
Gabor Mezei15b95a62022-05-09 16:37:58 +02004587 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004588};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004589
Jerry Yu909df7b2022-01-22 11:56:27 +08004590/* NOTICE: see above */
4591#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4592static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004593#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004594#if defined(MBEDTLS_ECDSA_C)
4595 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004596#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004597#if defined(MBEDTLS_RSA_C)
4598 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4599#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004600#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004601#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004602#if defined(MBEDTLS_ECDSA_C)
4603 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004604#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004605#if defined(MBEDTLS_RSA_C)
4606 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4607#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004608#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004609 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004610};
Jerry Yu909df7b2022-01-22 11:56:27 +08004611#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4612
Jerry Yu1a8b4812022-01-20 17:56:50 +08004613#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004614
Brett Warrene0edc842021-08-17 09:53:13 +01004615static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004616#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004617 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004618#endif
4619#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004620 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004621#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004622 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004623};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004624
Jerry Yu1a8b4812022-01-20 17:56:50 +08004625#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004626/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004627 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004628MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004629static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004630{
4631 size_t i, j;
4632 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004633
Gabor Mezei15b95a62022-05-09 16:37:58 +02004634 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004635 {
Jerry Yuf377d642022-01-25 10:43:59 +08004636 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004637 {
Jerry Yuf377d642022-01-25 10:43:59 +08004638 if( sig_algs[i] != sig_algs[j] )
4639 continue;
4640 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4641 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4642 sig_algs[i], j, i );
4643 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004644 }
4645 }
4646 return( ret );
4647}
4648
4649#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4650
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004651/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004652 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004653 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004654int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004655 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004656{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004657#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004658 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004659#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004660
Jerry Yu1a8b4812022-01-20 17:56:50 +08004661#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004662 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004663 {
4664 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4665 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4666 }
4667
Jerry Yuf377d642022-01-25 10:43:59 +08004668 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004669 {
4670 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4671 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4672 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004673
4674#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004675 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004676 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004677 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004678 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4679 }
4680
Jerry Yuf377d642022-01-25 10:43:59 +08004681 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004682 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004683 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004684 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4685 }
4686#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004687#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4688
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004689 /* Use the functions here so that they are covered in tests,
4690 * but otherwise access member directly for efficiency */
4691 mbedtls_ssl_conf_endpoint( conf, endpoint );
4692 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004693
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004694 /*
4695 * Things that are common to all presets
4696 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004697#if defined(MBEDTLS_SSL_CLI_C)
4698 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4699 {
4700 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4701#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4702 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4703#endif
4704 }
4705#endif
4706
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004707#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4708 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4709#endif
4710
4711#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4712 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4713#endif
4714
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004715#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004716 conf->f_cookie_write = ssl_cookie_write_dummy;
4717 conf->f_cookie_check = ssl_cookie_check_dummy;
4718#endif
4719
4720#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4721 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4722#endif
4723
Janos Follath088ce432017-04-10 12:42:31 +01004724#if defined(MBEDTLS_SSL_SRV_C)
4725 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004726 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004727#endif
4728
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004729#if defined(MBEDTLS_SSL_PROTO_DTLS)
4730 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4731 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4732#endif
4733
4734#if defined(MBEDTLS_SSL_RENEGOTIATION)
4735 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004736 memset( conf->renego_period, 0x00, 2 );
4737 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004738#endif
4739
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004740#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004741 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4742 {
4743 const unsigned char dhm_p[] =
4744 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4745 const unsigned char dhm_g[] =
4746 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004747
Hanno Beckere2defad2021-07-24 05:59:17 +01004748 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4749 dhm_p, sizeof( dhm_p ),
4750 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4751 {
4752 return( ret );
4753 }
4754 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004755#endif
4756
Ronald Cron6f135e12021-12-08 16:57:54 +01004757#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004758#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004759 mbedtls_ssl_conf_new_session_tickets(
4760 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4761#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004762 /*
4763 * Allow all TLS 1.3 key exchange modes by default.
4764 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004765 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004766#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004767
XiaokangQian4d3a6042022-04-21 13:46:17 +00004768 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4769 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004770#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004771 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004772 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004773#else
XiaokangQian060d8672022-04-21 09:24:56 +00004774 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004775#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004776 }
4777 else
4778 {
4779#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4780 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4781 {
4782 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4783 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4784 }
4785 else
4786 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4787 {
4788 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4789 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4790 }
4791#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4792 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4793 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4794#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4795 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4796 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4797#else
4798 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4799#endif
4800 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004801
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004802 /*
4803 * Preset-specific defaults
4804 */
4805 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004806 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004807 /*
4808 * NSA Suite B
4809 */
4810 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004811
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004812 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004813
4814#if defined(MBEDTLS_X509_CRT_PARSE_C)
4815 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004816#endif
4817
Gilles Peskineeccd8882020-03-10 12:19:08 +01004818#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004819#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4820 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4821 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4822 else
4823#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4824 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004825#endif
4826
Brett Warrene0edc842021-08-17 09:53:13 +01004827#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4828 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004829#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004830 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004831 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004832
4833 /*
4834 * Default
4835 */
4836 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004837
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004838 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004839
4840#if defined(MBEDTLS_X509_CRT_PARSE_C)
4841 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4842#endif
4843
Gilles Peskineeccd8882020-03-10 12:19:08 +01004844#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004845#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4846 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4847 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4848 else
4849#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4850 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004851#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004852
Brett Warrene0edc842021-08-17 09:53:13 +01004853#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4854 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004855#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004856 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004857
4858#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4859 conf->dhm_min_bitlen = 1024;
4860#endif
4861 }
4862
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004863 return( 0 );
4864}
4865
4866/*
4867 * Free mbedtls_ssl_config
4868 */
4869void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4870{
4871#if defined(MBEDTLS_DHM_C)
4872 mbedtls_mpi_free( &conf->dhm_P );
4873 mbedtls_mpi_free( &conf->dhm_G );
4874#endif
4875
Gilles Peskineeccd8882020-03-10 12:19:08 +01004876#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004877#if defined(MBEDTLS_USE_PSA_CRYPTO)
4878 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4879 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004880 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4881 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004882#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004883 if( conf->psk != NULL )
4884 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004885 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004886 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004887 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004888 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004889 }
4890
4891 if( conf->psk_identity != NULL )
4892 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004893 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004894 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004895 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004896 conf->psk_identity_len = 0;
4897 }
4898#endif
4899
4900#if defined(MBEDTLS_X509_CRT_PARSE_C)
4901 ssl_key_cert_free( conf->key_cert );
4902#endif
4903
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004904 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004905}
4906
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004907#if defined(MBEDTLS_PK_C) && \
4908 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004909/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004910 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004911 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004912unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004913{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004914#if defined(MBEDTLS_RSA_C)
4915 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4916 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004917#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004918#if defined(MBEDTLS_ECDSA_C)
4919 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4920 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004921#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004922 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004923}
4924
Hanno Becker7e5437a2017-04-28 17:15:26 +01004925unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4926{
4927 switch( type ) {
4928 case MBEDTLS_PK_RSA:
4929 return( MBEDTLS_SSL_SIG_RSA );
4930 case MBEDTLS_PK_ECDSA:
4931 case MBEDTLS_PK_ECKEY:
4932 return( MBEDTLS_SSL_SIG_ECDSA );
4933 default:
4934 return( MBEDTLS_SSL_SIG_ANON );
4935 }
4936}
4937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004938mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004939{
4940 switch( sig )
4941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004942#if defined(MBEDTLS_RSA_C)
4943 case MBEDTLS_SSL_SIG_RSA:
4944 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004945#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004946#if defined(MBEDTLS_ECDSA_C)
4947 case MBEDTLS_SSL_SIG_ECDSA:
4948 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004949#endif
4950 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004951 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004952 }
4953}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004954#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004955
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004956/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004957 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004958 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004959mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004960{
4961 switch( hash )
4962 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004963#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004964 case MBEDTLS_SSL_HASH_MD5:
4965 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004966#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004967#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004968 case MBEDTLS_SSL_HASH_SHA1:
4969 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004970#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004971#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004972 case MBEDTLS_SSL_HASH_SHA224:
4973 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004974#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004975#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004976 case MBEDTLS_SSL_HASH_SHA256:
4977 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004978#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004979#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004980 case MBEDTLS_SSL_HASH_SHA384:
4981 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004982#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004983#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004984 case MBEDTLS_SSL_HASH_SHA512:
4985 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004986#endif
4987 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004988 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004989 }
4990}
4991
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004992/*
4993 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4994 */
4995unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4996{
4997 switch( md )
4998 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004999#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005000 case MBEDTLS_MD_MD5:
5001 return( MBEDTLS_SSL_HASH_MD5 );
5002#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005003#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005004 case MBEDTLS_MD_SHA1:
5005 return( MBEDTLS_SSL_HASH_SHA1 );
5006#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005007#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005008 case MBEDTLS_MD_SHA224:
5009 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005010#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005011#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005012 case MBEDTLS_MD_SHA256:
5013 return( MBEDTLS_SSL_HASH_SHA256 );
5014#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005015#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005016 case MBEDTLS_MD_SHA384:
5017 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005018#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005019#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005020 case MBEDTLS_MD_SHA512:
5021 return( MBEDTLS_SSL_HASH_SHA512 );
5022#endif
5023 default:
5024 return( MBEDTLS_SSL_HASH_NONE );
5025 }
5026}
5027
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005028/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005029 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005030 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005031 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005032int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005033{
Brett Warrene0edc842021-08-17 09:53:13 +01005034 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005035
Brett Warrene0edc842021-08-17 09:53:13 +01005036 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005037 return( -1 );
5038
Brett Warrene0edc842021-08-17 09:53:13 +01005039 for( ; *group_list != 0; group_list++ )
5040 {
5041 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005042 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005043 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005044
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005045 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005046}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005047
5048#if defined(MBEDTLS_ECP_C)
5049/*
5050 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5051 */
5052int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5053{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005054 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005055 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005056
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005057 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005058 return -1;
5059
5060 uint16_t tls_id = grp_info->tls_id;
5061
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005062 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5063}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005064#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005066#if defined(MBEDTLS_X509_CRT_PARSE_C)
5067int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5068 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005069 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005070 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005071{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005072 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005073 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005074 const char *ext_oid;
5075 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005077 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005078 {
5079 /* Server part of the key exchange */
5080 switch( ciphersuite->key_exchange )
5081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005082 case MBEDTLS_KEY_EXCHANGE_RSA:
5083 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005084 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
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_DHE_RSA:
5088 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5089 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5090 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005091 break;
5092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005093 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5094 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005095 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005096 break;
5097
5098 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005099 case MBEDTLS_KEY_EXCHANGE_NONE:
5100 case MBEDTLS_KEY_EXCHANGE_PSK:
5101 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5102 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005103 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005104 usage = 0;
5105 }
5106 }
5107 else
5108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005109 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5110 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005111 }
5112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005113 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005114 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005115 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005116 ret = -1;
5117 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005119 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005121 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5122 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005123 }
5124 else
5125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005126 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5127 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005128 }
5129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005130 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005131 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005132 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005133 ret = -1;
5134 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005135
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005136 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005137}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005138#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005139
Jerry Yu148165c2021-09-24 23:20:59 +08005140#if defined(MBEDTLS_USE_PSA_CRYPTO)
5141int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5142 const mbedtls_md_type_t md,
5143 unsigned char *dst,
5144 size_t dst_len,
5145 size_t *olen )
5146{
Ronald Cronf6893e12022-01-07 22:09:01 +01005147 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5148 psa_hash_operation_t *hash_operation_to_clone;
5149 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5150
Jerry Yu148165c2021-09-24 23:20:59 +08005151 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005152
5153 switch( md )
5154 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005155#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005156 case MBEDTLS_MD_SHA384:
5157 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5158 break;
5159#endif
5160
Andrzej Kurek25f27152022-08-17 16:09:31 -04005161#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005162 case MBEDTLS_MD_SHA256:
5163 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5164 break;
5165#endif
5166
5167 default:
5168 goto exit;
5169 }
5170
5171 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5172 if( status != PSA_SUCCESS )
5173 goto exit;
5174
5175 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5176 if( status != PSA_SUCCESS )
5177 goto exit;
5178
5179exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005180#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5181 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5182 (void) ssl;
5183#endif
Ronald Cronb788c042022-02-15 09:14:15 +01005184 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005185}
5186#else /* MBEDTLS_USE_PSA_CRYPTO */
5187
Andrzej Kurek25f27152022-08-17 16:09:31 -04005188#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005189MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005190static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5191 unsigned char *dst,
5192 size_t dst_len,
5193 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005194{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005195 int ret;
5196 mbedtls_sha512_context sha512;
5197
5198 if( dst_len < 48 )
5199 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5200
5201 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005202 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005203
5204 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5205 {
5206 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5207 goto exit;
5208 }
5209
5210 *olen = 48;
5211
5212exit:
5213
5214 mbedtls_sha512_free( &sha512 );
5215 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005216}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005217#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005218
Andrzej Kurek25f27152022-08-17 16:09:31 -04005219#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005220MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005221static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5222 unsigned char *dst,
5223 size_t dst_len,
5224 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005225{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005226 int ret;
5227 mbedtls_sha256_context sha256;
5228
5229 if( dst_len < 32 )
5230 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5231
5232 mbedtls_sha256_init( &sha256 );
5233 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005234
Jerry Yu24c0ec32021-09-09 14:21:07 +08005235 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5236 {
5237 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5238 goto exit;
5239 }
5240
5241 *olen = 32;
5242
5243exit:
5244
5245 mbedtls_sha256_free( &sha256 );
5246 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005247}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005248#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005249
Jerry Yu000f9762021-09-14 11:12:51 +08005250int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5251 const mbedtls_md_type_t md,
5252 unsigned char *dst,
5253 size_t dst_len,
5254 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005255{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005256 switch( md )
5257 {
5258
Andrzej Kurek25f27152022-08-17 16:09:31 -04005259#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005260 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005261 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005262#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005263
Andrzej Kurek25f27152022-08-17 16:09:31 -04005264#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005265 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005266 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005267#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005268
5269 default:
5270 break;
5271 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005272 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005273}
XiaokangQian647719a2021-12-07 09:16:29 +00005274
Jerry Yu148165c2021-09-24 23:20:59 +08005275#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005276
Gabor Mezei078e8032022-04-27 21:17:56 +02005277#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5278/* mbedtls_ssl_parse_sig_alg_ext()
5279 *
5280 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5281 * value (TLS 1.3 RFC8446):
5282 * enum {
5283 * ....
5284 * ecdsa_secp256r1_sha256( 0x0403 ),
5285 * ecdsa_secp384r1_sha384( 0x0503 ),
5286 * ecdsa_secp521r1_sha512( 0x0603 ),
5287 * ....
5288 * } SignatureScheme;
5289 *
5290 * struct {
5291 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5292 * } SignatureSchemeList;
5293 *
5294 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5295 * value (TLS 1.2 RFC5246):
5296 * enum {
5297 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5298 * sha512(6), (255)
5299 * } HashAlgorithm;
5300 *
5301 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5302 * SignatureAlgorithm;
5303 *
5304 * struct {
5305 * HashAlgorithm hash;
5306 * SignatureAlgorithm signature;
5307 * } SignatureAndHashAlgorithm;
5308 *
5309 * SignatureAndHashAlgorithm
5310 * supported_signature_algorithms<2..2^16-2>;
5311 *
5312 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5313 * generalization of the TLS 1.2 signature algorithm extension.
5314 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5315 * `SignatureScheme` field of TLS 1.3
5316 *
5317 */
5318int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5319 const unsigned char *buf,
5320 const unsigned char *end )
5321{
5322 const unsigned char *p = buf;
5323 size_t supported_sig_algs_len = 0;
5324 const unsigned char *supported_sig_algs_end;
5325 uint16_t sig_alg;
5326 uint32_t common_idx = 0;
5327
5328 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5329 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5330 p += 2;
5331
5332 memset( ssl->handshake->received_sig_algs, 0,
5333 sizeof(ssl->handshake->received_sig_algs) );
5334
5335 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5336 supported_sig_algs_end = p + supported_sig_algs_len;
5337 while( p < supported_sig_algs_end )
5338 {
5339 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5340 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5341 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005342 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5343 sig_alg,
5344 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005345#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005346 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005347 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5348 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5349 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005350 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005351 }
5352#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005353
Jerry Yuf3b46b52022-06-19 16:52:27 +08005354 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5355 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005356
5357 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5358 {
5359 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5360 common_idx += 1;
5361 }
5362 }
5363 /* Check that we consumed all the message. */
5364 if( p != end )
5365 {
5366 MBEDTLS_SSL_DEBUG_MSG( 1,
5367 ( "Signature algorithms extension length misaligned" ) );
5368 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5369 MBEDTLS_ERR_SSL_DECODE_ERROR );
5370 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5371 }
5372
5373 if( common_idx == 0 )
5374 {
5375 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5376 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5377 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5378 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5379 }
5380
Gabor Mezei15b95a62022-05-09 16:37:58 +02005381 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005382 return( 0 );
5383}
5384
5385#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5386
Jerry Yudc7bd172022-02-17 13:44:15 +08005387#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5388
5389#if defined(MBEDTLS_USE_PSA_CRYPTO)
5390
5391static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5392 mbedtls_svc_key_id_t key,
5393 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005394 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005395 const unsigned char* seed, size_t seed_length,
5396 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005397 const unsigned char* other_secret,
5398 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005399 size_t capacity )
5400{
5401 psa_status_t status;
5402
5403 status = psa_key_derivation_setup( derivation, alg );
5404 if( status != PSA_SUCCESS )
5405 return( status );
5406
5407 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5408 {
5409 status = psa_key_derivation_input_bytes( derivation,
5410 PSA_KEY_DERIVATION_INPUT_SEED,
5411 seed, seed_length );
5412 if( status != PSA_SUCCESS )
5413 return( status );
5414
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005415 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005416 {
5417 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005418 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5419 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005420 if( status != PSA_SUCCESS )
5421 return( status );
5422 }
5423
Jerry Yudc7bd172022-02-17 13:44:15 +08005424 if( mbedtls_svc_key_id_is_null( key ) )
5425 {
5426 status = psa_key_derivation_input_bytes(
5427 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005428 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005429 }
5430 else
5431 {
5432 status = psa_key_derivation_input_key(
5433 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5434 }
5435 if( status != PSA_SUCCESS )
5436 return( status );
5437
5438 status = psa_key_derivation_input_bytes( derivation,
5439 PSA_KEY_DERIVATION_INPUT_LABEL,
5440 label, label_length );
5441 if( status != PSA_SUCCESS )
5442 return( status );
5443 }
5444 else
5445 {
5446 return( PSA_ERROR_NOT_SUPPORTED );
5447 }
5448
5449 status = psa_key_derivation_set_capacity( derivation, capacity );
5450 if( status != PSA_SUCCESS )
5451 return( status );
5452
5453 return( PSA_SUCCESS );
5454}
5455
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005456#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \
5457 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005458MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005459static int tls_prf_generic( mbedtls_md_type_t md_type,
5460 const unsigned char *secret, size_t slen,
5461 const char *label,
5462 const unsigned char *random, size_t rlen,
5463 unsigned char *dstbuf, size_t dlen )
5464{
5465 psa_status_t status;
5466 psa_algorithm_t alg;
5467 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5468 psa_key_derivation_operation_t derivation =
5469 PSA_KEY_DERIVATION_OPERATION_INIT;
5470
5471 if( md_type == MBEDTLS_MD_SHA384 )
5472 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5473 else
5474 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5475
5476 /* Normally a "secret" should be long enough to be impossible to
5477 * find by brute force, and in particular should not be empty. But
5478 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5479 * and for this use case it makes sense to have a 0-length "secret".
5480 * Since the key API doesn't allow importing a key of length 0,
5481 * keep master_key=0, which setup_psa_key_derivation() understands
5482 * to mean a 0-length "secret" input. */
5483 if( slen != 0 )
5484 {
5485 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5486 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5487 psa_set_key_algorithm( &key_attributes, alg );
5488 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5489
5490 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5491 if( status != PSA_SUCCESS )
5492 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5493 }
5494
5495 status = setup_psa_key_derivation( &derivation,
5496 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005497 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005498 random, rlen,
5499 (unsigned char const *) label,
5500 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005501 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005502 dlen );
5503 if( status != PSA_SUCCESS )
5504 {
5505 psa_key_derivation_abort( &derivation );
5506 psa_destroy_key( master_key );
5507 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5508 }
5509
5510 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5511 if( status != PSA_SUCCESS )
5512 {
5513 psa_key_derivation_abort( &derivation );
5514 psa_destroy_key( master_key );
5515 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5516 }
5517
5518 status = psa_key_derivation_abort( &derivation );
5519 if( status != PSA_SUCCESS )
5520 {
5521 psa_destroy_key( master_key );
5522 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5523 }
5524
5525 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5526 status = psa_destroy_key( master_key );
5527 if( status != PSA_SUCCESS )
5528 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5529
5530 return( 0 );
5531}
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005532#endif
Jerry Yudc7bd172022-02-17 13:44:15 +08005533#else /* MBEDTLS_USE_PSA_CRYPTO */
5534
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005535MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005536static int tls_prf_generic( mbedtls_md_type_t md_type,
5537 const unsigned char *secret, size_t slen,
5538 const char *label,
5539 const unsigned char *random, size_t rlen,
5540 unsigned char *dstbuf, size_t dlen )
5541{
5542 size_t nb;
5543 size_t i, j, k, md_len;
5544 unsigned char *tmp;
5545 size_t tmp_len = 0;
5546 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5547 const mbedtls_md_info_t *md_info;
5548 mbedtls_md_context_t md_ctx;
5549 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5550
5551 mbedtls_md_init( &md_ctx );
5552
5553 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5554 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5555
5556 md_len = mbedtls_md_get_size( md_info );
5557
5558 tmp_len = md_len + strlen( label ) + rlen;
5559 tmp = mbedtls_calloc( 1, tmp_len );
5560 if( tmp == NULL )
5561 {
5562 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5563 goto exit;
5564 }
5565
5566 nb = strlen( label );
5567 memcpy( tmp + md_len, label, nb );
5568 memcpy( tmp + md_len + nb, random, rlen );
5569 nb += rlen;
5570
5571 /*
5572 * Compute P_<hash>(secret, label + random)[0..dlen]
5573 */
5574 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5575 goto exit;
5576
5577 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5578 if( ret != 0 )
5579 goto exit;
5580 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5581 if( ret != 0 )
5582 goto exit;
5583 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5584 if( ret != 0 )
5585 goto exit;
5586
5587 for( i = 0; i < dlen; i += md_len )
5588 {
5589 ret = mbedtls_md_hmac_reset ( &md_ctx );
5590 if( ret != 0 )
5591 goto exit;
5592 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5593 if( ret != 0 )
5594 goto exit;
5595 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5596 if( ret != 0 )
5597 goto exit;
5598
5599 ret = mbedtls_md_hmac_reset ( &md_ctx );
5600 if( ret != 0 )
5601 goto exit;
5602 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5603 if( ret != 0 )
5604 goto exit;
5605 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5606 if( ret != 0 )
5607 goto exit;
5608
5609 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5610
5611 for( j = 0; j < k; j++ )
5612 dstbuf[i + j] = h_i[j];
5613 }
5614
5615exit:
5616 mbedtls_md_free( &md_ctx );
5617
5618 mbedtls_platform_zeroize( tmp, tmp_len );
5619 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5620
5621 mbedtls_free( tmp );
5622
5623 return( ret );
5624}
5625#endif /* MBEDTLS_USE_PSA_CRYPTO */
5626
Andrzej Kurek25f27152022-08-17 16:09:31 -04005627#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005628MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005629static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5630 const char *label,
5631 const unsigned char *random, size_t rlen,
5632 unsigned char *dstbuf, size_t dlen )
5633{
5634 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5635 label, random, rlen, dstbuf, dlen ) );
5636}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005637#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005638
Andrzej Kurek25f27152022-08-17 16:09:31 -04005639#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005640MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005641static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5642 const char *label,
5643 const unsigned char *random, size_t rlen,
5644 unsigned char *dstbuf, size_t dlen )
5645{
5646 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5647 label, random, rlen, dstbuf, dlen ) );
5648}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005649#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005650
Jerry Yuf009d862022-02-17 14:01:37 +08005651/*
5652 * Set appropriate PRF function and other SSL / TLS1.2 functions
5653 *
5654 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005655 * - hash associated with the ciphersuite (only used by TLS 1.2)
5656 *
5657 * Outputs:
5658 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5659 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005660MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005661static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005662 mbedtls_md_type_t hash )
5663{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005664#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005665 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005666 {
5667 handshake->tls_prf = tls_prf_sha384;
5668 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5669 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5670 }
5671 else
5672#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005673#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005674 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005675 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005676 handshake->tls_prf = tls_prf_sha256;
5677 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5678 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5679 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005680#else
Jerry Yuf009d862022-02-17 14:01:37 +08005681 {
Jerry Yuf009d862022-02-17 14:01:37 +08005682 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005683 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005684 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5685 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005686#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005687
5688 return( 0 );
5689}
Jerry Yud6ab2352022-02-17 14:03:43 +08005690
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005691/*
5692 * Compute master secret if needed
5693 *
5694 * Parameters:
5695 * [in/out] handshake
5696 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5697 * (PSA-PSK) ciphersuite_info, psk_opaque
5698 * [out] premaster (cleared)
5699 * [out] master
5700 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5701 * debug: conf->f_dbg, conf->p_dbg
5702 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005703 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005704 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005705MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005706static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5707 unsigned char *master,
5708 const mbedtls_ssl_context *ssl )
5709{
5710 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5711
5712 /* cf. RFC 5246, Section 8.1:
5713 * "The master secret is always exactly 48 bytes in length." */
5714 size_t const master_secret_len = 48;
5715
5716#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5717 unsigned char session_hash[48];
5718#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5719
5720 /* The label for the KDF used for key expansion.
5721 * This is either "master secret" or "extended master secret"
5722 * depending on whether the Extended Master Secret extension
5723 * is used. */
5724 char const *lbl = "master secret";
5725
Przemek Stekielae4ed302022-04-05 17:15:55 +02005726 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005727 * - If the Extended Master Secret extension is not used,
5728 * this is ClientHello.Random + ServerHello.Random
5729 * (see Sect. 8.1 in RFC 5246).
5730 * - If the Extended Master Secret extension is used,
5731 * this is the transcript of the handshake so far.
5732 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005733 unsigned char const *seed = handshake->randbytes;
5734 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005735
5736#if !defined(MBEDTLS_DEBUG_C) && \
5737 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5738 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5739 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5740 ssl = NULL; /* make sure we don't use it except for those cases */
5741 (void) ssl;
5742#endif
5743
5744 if( handshake->resume != 0 )
5745 {
5746 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5747 return( 0 );
5748 }
5749
5750#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5751 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5752 {
5753 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005754 seed = session_hash;
5755 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005756
5757 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005758 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005759 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005760#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005761
Przemek Stekiel99114f32022-04-22 11:20:09 +02005762#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005763 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005764 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005765 {
5766 /* Perform PSK-to-MS expansion in a single step. */
5767 psa_status_t status;
5768 psa_algorithm_t alg;
5769 mbedtls_svc_key_id_t psk;
5770 psa_key_derivation_operation_t derivation =
5771 PSA_KEY_DERIVATION_OPERATION_INIT;
5772 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5773
5774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5775
5776 psk = mbedtls_ssl_get_opaque_psk( ssl );
5777
5778 if( hash_alg == MBEDTLS_MD_SHA384 )
5779 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5780 else
5781 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5782
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005783 size_t other_secret_len = 0;
5784 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005785
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005786 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005787 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005788 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005789 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005790 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005791 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005792 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005793 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005794 other_secret_len = 48;
5795 other_secret = handshake->premaster + 2;
5796 break;
5797 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005798 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005799 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5800 other_secret = handshake->premaster + 2;
5801 break;
5802 default:
5803 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005804 }
5805
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005806 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005807 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005808 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005809 (unsigned char const *) lbl,
5810 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005811 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005812 master_secret_len );
5813 if( status != PSA_SUCCESS )
5814 {
5815 psa_key_derivation_abort( &derivation );
5816 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5817 }
5818
5819 status = psa_key_derivation_output_bytes( &derivation,
5820 master,
5821 master_secret_len );
5822 if( status != PSA_SUCCESS )
5823 {
5824 psa_key_derivation_abort( &derivation );
5825 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5826 }
5827
5828 status = psa_key_derivation_abort( &derivation );
5829 if( status != PSA_SUCCESS )
5830 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5831 }
5832 else
5833#endif
5834 {
5835 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005836 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005837 master,
5838 master_secret_len );
5839 if( ret != 0 )
5840 {
5841 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5842 return( ret );
5843 }
5844
5845 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5846 handshake->premaster,
5847 handshake->pmslen );
5848
5849 mbedtls_platform_zeroize( handshake->premaster,
5850 sizeof(handshake->premaster) );
5851 }
5852
5853 return( 0 );
5854}
5855
Jerry Yud62f87e2022-02-17 14:09:02 +08005856int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5857{
5858 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5859 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5860 ssl->handshake->ciphersuite_info;
5861
5862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5863
5864 /* Set PRF, calc_verify and calc_finished function pointers */
5865 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005866 ciphersuite_info->mac );
5867 if( ret != 0 )
5868 {
5869 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5870 return( ret );
5871 }
5872
5873 /* Compute master secret if needed */
5874 ret = ssl_compute_master( ssl->handshake,
5875 ssl->session_negotiate->master,
5876 ssl );
5877 if( ret != 0 )
5878 {
5879 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5880 return( ret );
5881 }
5882
5883 /* Swap the client and server random values:
5884 * - MS derivation wanted client+server (RFC 5246 8.1)
5885 * - key derivation wants server+client (RFC 5246 6.3) */
5886 {
5887 unsigned char tmp[64];
5888 memcpy( tmp, ssl->handshake->randbytes, 64 );
5889 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5890 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5891 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5892 }
5893
5894 /* Populate transform structure */
5895 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5896 ssl->session_negotiate->ciphersuite,
5897 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005898#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005899 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005900#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005901 ssl->handshake->tls_prf,
5902 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005903 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005904 ssl->conf->endpoint,
5905 ssl );
5906 if( ret != 0 )
5907 {
5908 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5909 return( ret );
5910 }
5911
5912 /* We no longer need Server/ClientHello.random values */
5913 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5914 sizeof( ssl->handshake->randbytes ) );
5915
5916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5917
5918 return( 0 );
5919}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005920
Ronald Cron4dcbca92022-03-07 10:21:40 +01005921int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5922{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005923 switch( md )
5924 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005925#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005926 case MBEDTLS_SSL_HASH_SHA384:
5927 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5928 break;
5929#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005930#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005931 case MBEDTLS_SSL_HASH_SHA256:
5932 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5933 break;
5934#endif
5935 default:
5936 return( -1 );
5937 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005938#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5939 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5940 (void) ssl;
5941#endif
Ronald Cronc2f13a02022-03-07 10:25:24 +01005942 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005943}
5944
Andrzej Kurek25f27152022-08-17 16:09:31 -04005945#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005946void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5947 unsigned char *hash,
5948 size_t *hlen )
5949{
5950#if defined(MBEDTLS_USE_PSA_CRYPTO)
5951 size_t hash_size;
5952 psa_status_t status;
5953 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5954
5955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5956 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5957 if( status != PSA_SUCCESS )
5958 {
5959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5960 return;
5961 }
5962
5963 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5964 if( status != PSA_SUCCESS )
5965 {
5966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5967 return;
5968 }
5969
5970 *hlen = 32;
5971 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5973#else
5974 mbedtls_sha256_context sha256;
5975
5976 mbedtls_sha256_init( &sha256 );
5977
5978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5979
5980 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5981 mbedtls_sha256_finish( &sha256, hash );
5982
5983 *hlen = 32;
5984
5985 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5987
5988 mbedtls_sha256_free( &sha256 );
5989#endif /* MBEDTLS_USE_PSA_CRYPTO */
5990 return;
5991}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005992#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08005993
Andrzej Kurek25f27152022-08-17 16:09:31 -04005994#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08005995void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5996 unsigned char *hash,
5997 size_t *hlen )
5998{
5999#if defined(MBEDTLS_USE_PSA_CRYPTO)
6000 size_t hash_size;
6001 psa_status_t status;
6002 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6003
6004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6005 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6006 if( status != PSA_SUCCESS )
6007 {
6008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6009 return;
6010 }
6011
6012 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6013 if( status != PSA_SUCCESS )
6014 {
6015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6016 return;
6017 }
6018
6019 *hlen = 48;
6020 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6022#else
6023 mbedtls_sha512_context sha512;
6024
6025 mbedtls_sha512_init( &sha512 );
6026
6027 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6028
Andrzej Kureka242e832022-08-11 10:03:14 -04006029 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006030 mbedtls_sha512_finish( &sha512, hash );
6031
6032 *hlen = 48;
6033
6034 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6036
6037 mbedtls_sha512_free( &sha512 );
6038#endif /* MBEDTLS_USE_PSA_CRYPTO */
6039 return;
6040}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006041#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006042
Neil Armstrong80f6f322022-05-03 17:56:38 +02006043#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6044 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006045int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6046{
6047 unsigned char *p = ssl->handshake->premaster;
6048 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6049 const unsigned char *psk = NULL;
6050 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006051 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006052
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006053 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006054 {
6055 /*
6056 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006057 * checked before calling this function.
6058 *
6059 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006060 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006061 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006062 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6063 {
6064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6065 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6066 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006067 }
6068
6069 /*
6070 * PMS = struct {
6071 * opaque other_secret<0..2^16-1>;
6072 * opaque psk<0..2^16-1>;
6073 * };
6074 * with "other_secret" depending on the particular key exchange
6075 */
6076#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6077 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6078 {
6079 if( end - p < 2 )
6080 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6081
6082 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6083 p += 2;
6084
6085 if( end < p || (size_t)( end - p ) < psk_len )
6086 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6087
6088 memset( p, 0, psk_len );
6089 p += psk_len;
6090 }
6091 else
6092#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6093#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6094 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6095 {
6096 /*
6097 * other_secret already set by the ClientKeyExchange message,
6098 * and is 48 bytes long
6099 */
6100 if( end - p < 2 )
6101 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6102
6103 *p++ = 0;
6104 *p++ = 48;
6105 p += 48;
6106 }
6107 else
6108#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6109#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6110 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6111 {
6112 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6113 size_t len;
6114
6115 /* Write length only when we know the actual value */
6116 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6117 p + 2, end - ( p + 2 ), &len,
6118 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6119 {
6120 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6121 return( ret );
6122 }
6123 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6124 p += 2 + len;
6125
6126 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6127 }
6128 else
6129#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006130#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006131 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6132 {
6133 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6134 size_t zlen;
6135
6136 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6137 p + 2, end - ( p + 2 ),
6138 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6139 {
6140 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6141 return( ret );
6142 }
6143
6144 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6145 p += 2 + zlen;
6146
6147 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6148 MBEDTLS_DEBUG_ECDH_Z );
6149 }
6150 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006151#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006152 {
6153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6154 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6155 }
6156
6157 /* opaque psk<0..2^16-1>; */
6158 if( end - p < 2 )
6159 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6160
6161 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6162 p += 2;
6163
6164 if( end < p || (size_t)( end - p ) < psk_len )
6165 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6166
6167 memcpy( p, psk, psk_len );
6168 p += psk_len;
6169
6170 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6171
6172 return( 0 );
6173}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006174#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006175
6176#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006177MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006178static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6179
6180#if defined(MBEDTLS_SSL_PROTO_DTLS)
6181int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6182{
6183 /* If renegotiation is not enforced, retransmit until we would reach max
6184 * timeout if we were using the usual handshake doubling scheme */
6185 if( ssl->conf->renego_max_records < 0 )
6186 {
6187 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6188 unsigned char doublings = 1;
6189
6190 while( ratio != 0 )
6191 {
6192 ++doublings;
6193 ratio >>= 1;
6194 }
6195
6196 if( ++ssl->renego_records_seen > doublings )
6197 {
6198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6199 return( 0 );
6200 }
6201 }
6202
6203 return( ssl_write_hello_request( ssl ) );
6204}
6205#endif
6206#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006207
Jerry Yud9526692022-02-17 14:23:47 +08006208/*
6209 * Handshake functions
6210 */
6211#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6212/* No certificate support -> dummy functions */
6213int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6214{
6215 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6216 ssl->handshake->ciphersuite_info;
6217
6218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6219
6220 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6221 {
6222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6223 ssl->state++;
6224 return( 0 );
6225 }
6226
6227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6228 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6229}
6230
6231int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6232{
6233 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6234 ssl->handshake->ciphersuite_info;
6235
6236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6237
6238 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6239 {
6240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6241 ssl->state++;
6242 return( 0 );
6243 }
6244
6245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6246 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6247}
6248
6249#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6250/* Some certificate support -> implement write and parse */
6251
6252int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6253{
6254 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6255 size_t i, n;
6256 const mbedtls_x509_crt *crt;
6257 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6258 ssl->handshake->ciphersuite_info;
6259
6260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6261
6262 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6263 {
6264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6265 ssl->state++;
6266 return( 0 );
6267 }
6268
6269#if defined(MBEDTLS_SSL_CLI_C)
6270 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6271 {
6272 if( ssl->handshake->client_auth == 0 )
6273 {
6274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6275 ssl->state++;
6276 return( 0 );
6277 }
6278 }
6279#endif /* MBEDTLS_SSL_CLI_C */
6280#if defined(MBEDTLS_SSL_SRV_C)
6281 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6282 {
6283 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6284 {
6285 /* Should never happen because we shouldn't have picked the
6286 * ciphersuite if we don't have a certificate. */
6287 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6288 }
6289 }
6290#endif
6291
6292 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6293
6294 /*
6295 * 0 . 0 handshake type
6296 * 1 . 3 handshake length
6297 * 4 . 6 length of all certs
6298 * 7 . 9 length of cert. 1
6299 * 10 . n-1 peer certificate
6300 * n . n+2 length of cert. 2
6301 * n+3 . ... upper level cert, etc.
6302 */
6303 i = 7;
6304 crt = mbedtls_ssl_own_cert( ssl );
6305
6306 while( crt != NULL )
6307 {
6308 n = crt->raw.len;
6309 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6310 {
6311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6312 " > %" MBEDTLS_PRINTF_SIZET,
6313 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6314 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6315 }
6316
6317 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6318 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6319 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6320
6321 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6322 i += n; crt = crt->next;
6323 }
6324
6325 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6326 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6327 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6328
6329 ssl->out_msglen = i;
6330 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6331 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6332
6333 ssl->state++;
6334
6335 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6336 {
6337 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6338 return( ret );
6339 }
6340
6341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6342
6343 return( ret );
6344}
6345
6346#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6347
6348#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006349MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006350static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6351 unsigned char *crt_buf,
6352 size_t crt_buf_len )
6353{
6354 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6355
6356 if( peer_crt == NULL )
6357 return( -1 );
6358
6359 if( peer_crt->raw.len != crt_buf_len )
6360 return( -1 );
6361
6362 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6363}
6364#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006365MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006366static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6367 unsigned char *crt_buf,
6368 size_t crt_buf_len )
6369{
6370 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6371 unsigned char const * const peer_cert_digest =
6372 ssl->session->peer_cert_digest;
6373 mbedtls_md_type_t const peer_cert_digest_type =
6374 ssl->session->peer_cert_digest_type;
6375 mbedtls_md_info_t const * const digest_info =
6376 mbedtls_md_info_from_type( peer_cert_digest_type );
6377 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6378 size_t digest_len;
6379
6380 if( peer_cert_digest == NULL || digest_info == NULL )
6381 return( -1 );
6382
6383 digest_len = mbedtls_md_get_size( digest_info );
6384 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6385 return( -1 );
6386
6387 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6388 if( ret != 0 )
6389 return( -1 );
6390
6391 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6392}
6393#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6394#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6395
6396/*
6397 * Once the certificate message is read, parse it into a cert chain and
6398 * perform basic checks, but leave actual verification to the caller
6399 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006400MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006401static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6402 mbedtls_x509_crt *chain )
6403{
6404 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6405#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6406 int crt_cnt=0;
6407#endif
6408 size_t i, n;
6409 uint8_t alert;
6410
6411 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6412 {
6413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6414 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6415 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6416 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6417 }
6418
6419 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6420 {
6421 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6422 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6423 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6424 }
6425
6426 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6427 {
6428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6429 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6430 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6431 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6432 }
6433
6434 i = mbedtls_ssl_hs_hdr_len( ssl );
6435
6436 /*
6437 * Same message structure as in mbedtls_ssl_write_certificate()
6438 */
6439 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6440
6441 if( ssl->in_msg[i] != 0 ||
6442 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6443 {
6444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6445 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6446 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6447 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6448 }
6449
6450 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6451 i += 3;
6452
6453 /* Iterate through and parse the CRTs in the provided chain. */
6454 while( i < ssl->in_hslen )
6455 {
6456 /* Check that there's room for the next CRT's length fields. */
6457 if ( i + 3 > ssl->in_hslen ) {
6458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6459 mbedtls_ssl_send_alert_message( ssl,
6460 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6461 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6462 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6463 }
6464 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6465 * anything beyond 2**16 ~ 64K. */
6466 if( ssl->in_msg[i] != 0 )
6467 {
6468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6469 mbedtls_ssl_send_alert_message( ssl,
6470 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6471 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6472 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6473 }
6474
6475 /* Read length of the next CRT in the chain. */
6476 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6477 | (unsigned int) ssl->in_msg[i + 2];
6478 i += 3;
6479
6480 if( n < 128 || i + n > ssl->in_hslen )
6481 {
6482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6483 mbedtls_ssl_send_alert_message( ssl,
6484 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6485 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6486 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6487 }
6488
6489 /* Check if we're handling the first CRT in the chain. */
6490#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6491 if( crt_cnt++ == 0 &&
6492 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6493 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6494 {
6495 /* During client-side renegotiation, check that the server's
6496 * end-CRTs hasn't changed compared to the initial handshake,
6497 * mitigating the triple handshake attack. On success, reuse
6498 * the original end-CRT instead of parsing it again. */
6499 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6500 if( ssl_check_peer_crt_unchanged( ssl,
6501 &ssl->in_msg[i],
6502 n ) != 0 )
6503 {
6504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6505 mbedtls_ssl_send_alert_message( ssl,
6506 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6507 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6508 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6509 }
6510
6511 /* Now we can safely free the original chain. */
6512 ssl_clear_peer_cert( ssl->session );
6513 }
6514#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6515
6516 /* Parse the next certificate in the chain. */
6517#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6518 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6519#else
6520 /* If we don't need to store the CRT chain permanently, parse
6521 * it in-place from the input buffer instead of making a copy. */
6522 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6523#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6524 switch( ret )
6525 {
6526 case 0: /*ok*/
6527 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6528 /* Ignore certificate with an unknown algorithm: maybe a
6529 prior certificate was already trusted. */
6530 break;
6531
6532 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6533 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6534 goto crt_parse_der_failed;
6535
6536 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6537 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6538 goto crt_parse_der_failed;
6539
6540 default:
6541 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6542 crt_parse_der_failed:
6543 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6544 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6545 return( ret );
6546 }
6547
6548 i += n;
6549 }
6550
6551 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6552 return( 0 );
6553}
6554
6555#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006556MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006557static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6558{
6559 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6560 return( -1 );
6561
6562 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6563 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6564 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6565 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6566 {
Ronald Cron19385882022-06-15 16:26:13 +02006567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006568 return( 0 );
6569 }
6570 return( -1 );
6571}
6572#endif /* MBEDTLS_SSL_SRV_C */
6573
6574/* Check if a certificate message is expected.
6575 * Return either
6576 * - SSL_CERTIFICATE_EXPECTED, or
6577 * - SSL_CERTIFICATE_SKIP
6578 * indicating whether a Certificate message is expected or not.
6579 */
6580#define SSL_CERTIFICATE_EXPECTED 0
6581#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006582MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006583static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6584 int authmode )
6585{
6586 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6587 ssl->handshake->ciphersuite_info;
6588
6589 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6590 return( SSL_CERTIFICATE_SKIP );
6591
6592#if defined(MBEDTLS_SSL_SRV_C)
6593 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6594 {
6595 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6596 return( SSL_CERTIFICATE_SKIP );
6597
6598 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6599 {
6600 ssl->session_negotiate->verify_result =
6601 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6602 return( SSL_CERTIFICATE_SKIP );
6603 }
6604 }
6605#else
6606 ((void) authmode);
6607#endif /* MBEDTLS_SSL_SRV_C */
6608
6609 return( SSL_CERTIFICATE_EXPECTED );
6610}
6611
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006612MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006613static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6614 int authmode,
6615 mbedtls_x509_crt *chain,
6616 void *rs_ctx )
6617{
6618 int ret = 0;
6619 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6620 ssl->handshake->ciphersuite_info;
6621 int have_ca_chain = 0;
6622
6623 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6624 void *p_vrfy;
6625
6626 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6627 return( 0 );
6628
6629 if( ssl->f_vrfy != NULL )
6630 {
6631 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6632 f_vrfy = ssl->f_vrfy;
6633 p_vrfy = ssl->p_vrfy;
6634 }
6635 else
6636 {
6637 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6638 f_vrfy = ssl->conf->f_vrfy;
6639 p_vrfy = ssl->conf->p_vrfy;
6640 }
6641
6642 /*
6643 * Main check: verify certificate
6644 */
6645#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6646 if( ssl->conf->f_ca_cb != NULL )
6647 {
6648 ((void) rs_ctx);
6649 have_ca_chain = 1;
6650
6651 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6652 ret = mbedtls_x509_crt_verify_with_ca_cb(
6653 chain,
6654 ssl->conf->f_ca_cb,
6655 ssl->conf->p_ca_cb,
6656 ssl->conf->cert_profile,
6657 ssl->hostname,
6658 &ssl->session_negotiate->verify_result,
6659 f_vrfy, p_vrfy );
6660 }
6661 else
6662#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6663 {
6664 mbedtls_x509_crt *ca_chain;
6665 mbedtls_x509_crl *ca_crl;
6666
6667#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6668 if( ssl->handshake->sni_ca_chain != NULL )
6669 {
6670 ca_chain = ssl->handshake->sni_ca_chain;
6671 ca_crl = ssl->handshake->sni_ca_crl;
6672 }
6673 else
6674#endif
6675 {
6676 ca_chain = ssl->conf->ca_chain;
6677 ca_crl = ssl->conf->ca_crl;
6678 }
6679
6680 if( ca_chain != NULL )
6681 have_ca_chain = 1;
6682
6683 ret = mbedtls_x509_crt_verify_restartable(
6684 chain,
6685 ca_chain, ca_crl,
6686 ssl->conf->cert_profile,
6687 ssl->hostname,
6688 &ssl->session_negotiate->verify_result,
6689 f_vrfy, p_vrfy, rs_ctx );
6690 }
6691
6692 if( ret != 0 )
6693 {
6694 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6695 }
6696
6697#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6698 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6699 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6700#endif
6701
6702 /*
6703 * Secondary checks: always done, but change 'ret' only if it was 0
6704 */
6705
6706#if defined(MBEDTLS_ECP_C)
6707 {
6708 const mbedtls_pk_context *pk = &chain->pk;
6709
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006710 /* If certificate uses an EC key, make sure the curve is OK.
6711 * This is a public key, so it can't be opaque, so can_do() is a good
6712 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006713 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006714 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006715 /* and in the unlikely case the above assumption no longer holds
6716 * we are making sure that pk_ec() here does not return a NULL
6717 */
6718 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6719 if( ec == NULL )
6720 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006722 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6723 }
Jerry Yud9526692022-02-17 14:23:47 +08006724
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006725 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6726 {
6727 ssl->session_negotiate->verify_result |=
6728 MBEDTLS_X509_BADCERT_BAD_KEY;
6729
6730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6731 if( ret == 0 )
6732 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6733 }
Jerry Yud9526692022-02-17 14:23:47 +08006734 }
6735 }
6736#endif /* MBEDTLS_ECP_C */
6737
6738 if( mbedtls_ssl_check_cert_usage( chain,
6739 ciphersuite_info,
6740 ! ssl->conf->endpoint,
6741 &ssl->session_negotiate->verify_result ) != 0 )
6742 {
6743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6744 if( ret == 0 )
6745 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6746 }
6747
6748 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6749 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6750 * with details encoded in the verification flags. All other kinds
6751 * of error codes, including those from the user provided f_vrfy
6752 * functions, are treated as fatal and lead to a failure of
6753 * ssl_parse_certificate even if verification was optional. */
6754 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6755 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6756 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6757 {
6758 ret = 0;
6759 }
6760
6761 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6762 {
6763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6764 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6765 }
6766
6767 if( ret != 0 )
6768 {
6769 uint8_t alert;
6770
6771 /* The certificate may have been rejected for several reasons.
6772 Pick one and send the corresponding alert. Which alert to send
6773 may be a subject of debate in some cases. */
6774 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6775 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6776 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6777 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6778 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6779 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6780 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6781 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6782 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6783 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6784 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6785 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6786 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6787 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6788 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6789 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6790 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6791 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6792 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6793 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6794 else
6795 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6796 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6797 alert );
6798 }
6799
6800#if defined(MBEDTLS_DEBUG_C)
6801 if( ssl->session_negotiate->verify_result != 0 )
6802 {
6803 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6804 (unsigned int) ssl->session_negotiate->verify_result ) );
6805 }
6806 else
6807 {
6808 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6809 }
6810#endif /* MBEDTLS_DEBUG_C */
6811
6812 return( ret );
6813}
6814
6815#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006816MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006817static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6818 unsigned char *start, size_t len )
6819{
6820 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6821 /* Remember digest of the peer's end-CRT. */
6822 ssl->session_negotiate->peer_cert_digest =
6823 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6824 if( ssl->session_negotiate->peer_cert_digest == NULL )
6825 {
6826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6827 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6828 mbedtls_ssl_send_alert_message( ssl,
6829 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6830 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6831
6832 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6833 }
6834
6835 ret = mbedtls_md( mbedtls_md_info_from_type(
6836 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6837 start, len,
6838 ssl->session_negotiate->peer_cert_digest );
6839
6840 ssl->session_negotiate->peer_cert_digest_type =
6841 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6842 ssl->session_negotiate->peer_cert_digest_len =
6843 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6844
6845 return( ret );
6846}
6847
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006848MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006849static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6850 unsigned char *start, size_t len )
6851{
6852 unsigned char *end = start + len;
6853 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6854
6855 /* Make a copy of the peer's raw public key. */
6856 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6857 ret = mbedtls_pk_parse_subpubkey( &start, end,
6858 &ssl->handshake->peer_pubkey );
6859 if( ret != 0 )
6860 {
6861 /* We should have parsed the public key before. */
6862 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6863 }
6864
6865 return( 0 );
6866}
6867#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6868
6869int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6870{
6871 int ret = 0;
6872 int crt_expected;
6873#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6874 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6875 ? ssl->handshake->sni_authmode
6876 : ssl->conf->authmode;
6877#else
6878 const int authmode = ssl->conf->authmode;
6879#endif
6880 void *rs_ctx = NULL;
6881 mbedtls_x509_crt *chain = NULL;
6882
6883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6884
6885 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6886 if( crt_expected == SSL_CERTIFICATE_SKIP )
6887 {
6888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6889 goto exit;
6890 }
6891
6892#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6893 if( ssl->handshake->ecrs_enabled &&
6894 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6895 {
6896 chain = ssl->handshake->ecrs_peer_cert;
6897 ssl->handshake->ecrs_peer_cert = NULL;
6898 goto crt_verify;
6899 }
6900#endif
6901
6902 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6903 {
6904 /* mbedtls_ssl_read_record may have sent an alert already. We
6905 let it decide whether to alert. */
6906 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6907 goto exit;
6908 }
6909
6910#if defined(MBEDTLS_SSL_SRV_C)
6911 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6912 {
6913 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6914
6915 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6916 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6917
6918 goto exit;
6919 }
6920#endif /* MBEDTLS_SSL_SRV_C */
6921
6922 /* Clear existing peer CRT structure in case we tried to
6923 * reuse a session but it failed, and allocate a new one. */
6924 ssl_clear_peer_cert( ssl->session_negotiate );
6925
6926 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6927 if( chain == NULL )
6928 {
6929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6930 sizeof( mbedtls_x509_crt ) ) );
6931 mbedtls_ssl_send_alert_message( ssl,
6932 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6933 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6934
6935 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6936 goto exit;
6937 }
6938 mbedtls_x509_crt_init( chain );
6939
6940 ret = ssl_parse_certificate_chain( ssl, chain );
6941 if( ret != 0 )
6942 goto exit;
6943
6944#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6945 if( ssl->handshake->ecrs_enabled)
6946 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6947
6948crt_verify:
6949 if( ssl->handshake->ecrs_enabled)
6950 rs_ctx = &ssl->handshake->ecrs_ctx;
6951#endif
6952
6953 ret = ssl_parse_certificate_verify( ssl, authmode,
6954 chain, rs_ctx );
6955 if( ret != 0 )
6956 goto exit;
6957
6958#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6959 {
6960 unsigned char *crt_start, *pk_start;
6961 size_t crt_len, pk_len;
6962
6963 /* We parse the CRT chain without copying, so
6964 * these pointers point into the input buffer,
6965 * and are hence still valid after freeing the
6966 * CRT chain. */
6967
6968 crt_start = chain->raw.p;
6969 crt_len = chain->raw.len;
6970
6971 pk_start = chain->pk_raw.p;
6972 pk_len = chain->pk_raw.len;
6973
6974 /* Free the CRT structures before computing
6975 * digest and copying the peer's public key. */
6976 mbedtls_x509_crt_free( chain );
6977 mbedtls_free( chain );
6978 chain = NULL;
6979
6980 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6981 if( ret != 0 )
6982 goto exit;
6983
6984 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6985 if( ret != 0 )
6986 goto exit;
6987 }
6988#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6989 /* Pass ownership to session structure. */
6990 ssl->session_negotiate->peer_cert = chain;
6991 chain = NULL;
6992#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6993
6994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6995
6996exit:
6997
6998 if( ret == 0 )
6999 ssl->state++;
7000
7001#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7002 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7003 {
7004 ssl->handshake->ecrs_peer_cert = chain;
7005 chain = NULL;
7006 }
7007#endif
7008
7009 if( chain != NULL )
7010 {
7011 mbedtls_x509_crt_free( chain );
7012 mbedtls_free( chain );
7013 }
7014
7015 return( ret );
7016}
7017#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7018
Andrzej Kurek25f27152022-08-17 16:09:31 -04007019#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007020static void ssl_calc_finished_tls_sha256(
7021 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7022{
7023 int len = 12;
7024 const char *sender;
7025 unsigned char padbuf[32];
7026#if defined(MBEDTLS_USE_PSA_CRYPTO)
7027 size_t hash_size;
7028 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7029 psa_status_t status;
7030#else
7031 mbedtls_sha256_context sha256;
7032#endif
7033
7034 mbedtls_ssl_session *session = ssl->session_negotiate;
7035 if( !session )
7036 session = ssl->session;
7037
7038 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7039 ? "client finished"
7040 : "server finished";
7041
7042#if defined(MBEDTLS_USE_PSA_CRYPTO)
7043 sha256_psa = psa_hash_operation_init();
7044
7045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7046
7047 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7048 if( status != PSA_SUCCESS )
7049 {
7050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7051 return;
7052 }
7053
7054 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7055 if( status != PSA_SUCCESS )
7056 {
7057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7058 return;
7059 }
7060 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7061#else
7062
7063 mbedtls_sha256_init( &sha256 );
7064
7065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7066
7067 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7068
7069 /*
7070 * TLSv1.2:
7071 * hash = PRF( master, finished_label,
7072 * Hash( handshake ) )[0.11]
7073 */
7074
7075#if !defined(MBEDTLS_SHA256_ALT)
7076 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7077 sha256.state, sizeof( sha256.state ) );
7078#endif
7079
7080 mbedtls_sha256_finish( &sha256, padbuf );
7081 mbedtls_sha256_free( &sha256 );
7082#endif /* MBEDTLS_USE_PSA_CRYPTO */
7083
7084 ssl->handshake->tls_prf( session->master, 48, sender,
7085 padbuf, 32, buf, len );
7086
7087 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7088
7089 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7090
7091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7092}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007093#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007094
Jerry Yub7ba49e2022-02-17 14:25:53 +08007095
Andrzej Kurek25f27152022-08-17 16:09:31 -04007096#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007097static void ssl_calc_finished_tls_sha384(
7098 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7099{
7100 int len = 12;
7101 const char *sender;
7102 unsigned char padbuf[48];
7103#if defined(MBEDTLS_USE_PSA_CRYPTO)
7104 size_t hash_size;
7105 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7106 psa_status_t status;
7107#else
7108 mbedtls_sha512_context sha512;
7109#endif
7110
7111 mbedtls_ssl_session *session = ssl->session_negotiate;
7112 if( !session )
7113 session = ssl->session;
7114
7115 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7116 ? "client finished"
7117 : "server finished";
7118
7119#if defined(MBEDTLS_USE_PSA_CRYPTO)
7120 sha384_psa = psa_hash_operation_init();
7121
7122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7123
7124 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7125 if( status != PSA_SUCCESS )
7126 {
7127 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7128 return;
7129 }
7130
7131 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7132 if( status != PSA_SUCCESS )
7133 {
7134 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7135 return;
7136 }
7137 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7138#else
7139 mbedtls_sha512_init( &sha512 );
7140
7141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7142
Andrzej Kureka242e832022-08-11 10:03:14 -04007143 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007144
7145 /*
7146 * TLSv1.2:
7147 * hash = PRF( master, finished_label,
7148 * Hash( handshake ) )[0.11]
7149 */
7150
7151#if !defined(MBEDTLS_SHA512_ALT)
7152 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7153 sha512.state, sizeof( sha512.state ) );
7154#endif
7155 mbedtls_sha512_finish( &sha512, padbuf );
7156
7157 mbedtls_sha512_free( &sha512 );
7158#endif
7159
7160 ssl->handshake->tls_prf( session->master, 48, sender,
7161 padbuf, 48, buf, len );
7162
7163 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7164
7165 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7166
7167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7168}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007169#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007170
Jerry Yuaef00152022-02-17 14:27:31 +08007171void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7172{
7173 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7174
7175 /*
7176 * Free our handshake params
7177 */
7178 mbedtls_ssl_handshake_free( ssl );
7179 mbedtls_free( ssl->handshake );
7180 ssl->handshake = NULL;
7181
7182 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007183 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007184 */
7185 if( ssl->transform )
7186 {
7187 mbedtls_ssl_transform_free( ssl->transform );
7188 mbedtls_free( ssl->transform );
7189 }
7190 ssl->transform = ssl->transform_negotiate;
7191 ssl->transform_negotiate = NULL;
7192
7193 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7194}
7195
Jerry Yu2a9fff52022-02-17 14:28:51 +08007196void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7197{
7198 int resume = ssl->handshake->resume;
7199
7200 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7201
7202#if defined(MBEDTLS_SSL_RENEGOTIATION)
7203 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7204 {
7205 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7206 ssl->renego_records_seen = 0;
7207 }
7208#endif
7209
7210 /*
7211 * Free the previous session and switch in the current one
7212 */
7213 if( ssl->session )
7214 {
7215#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7216 /* RFC 7366 3.1: keep the EtM state */
7217 ssl->session_negotiate->encrypt_then_mac =
7218 ssl->session->encrypt_then_mac;
7219#endif
7220
7221 mbedtls_ssl_session_free( ssl->session );
7222 mbedtls_free( ssl->session );
7223 }
7224 ssl->session = ssl->session_negotiate;
7225 ssl->session_negotiate = NULL;
7226
7227 /*
7228 * Add cache entry
7229 */
7230 if( ssl->conf->f_set_cache != NULL &&
7231 ssl->session->id_len != 0 &&
7232 resume == 0 )
7233 {
7234 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7235 ssl->session->id,
7236 ssl->session->id_len,
7237 ssl->session ) != 0 )
7238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7239 }
7240
7241#if defined(MBEDTLS_SSL_PROTO_DTLS)
7242 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7243 ssl->handshake->flight != NULL )
7244 {
7245 /* Cancel handshake timer */
7246 mbedtls_ssl_set_timer( ssl, 0 );
7247
7248 /* Keep last flight around in case we need to resend it:
7249 * we need the handshake and transform structures for that */
7250 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7251 }
7252 else
7253#endif
7254 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7255
7256 ssl->state++;
7257
7258 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7259}
7260
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007261int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7262{
7263 int ret, hash_len;
7264
7265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7266
7267 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7268
7269 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7270
7271 /*
7272 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7273 * may define some other value. Currently (early 2016), no defined
7274 * ciphersuite does this (and this is unlikely to change as activity has
7275 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7276 */
7277 hash_len = 12;
7278
7279#if defined(MBEDTLS_SSL_RENEGOTIATION)
7280 ssl->verify_data_len = hash_len;
7281 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7282#endif
7283
7284 ssl->out_msglen = 4 + hash_len;
7285 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7286 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7287
7288 /*
7289 * In case of session resuming, invert the client and server
7290 * ChangeCipherSpec messages order.
7291 */
7292 if( ssl->handshake->resume != 0 )
7293 {
7294#if defined(MBEDTLS_SSL_CLI_C)
7295 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7296 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7297#endif
7298#if defined(MBEDTLS_SSL_SRV_C)
7299 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7300 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7301#endif
7302 }
7303 else
7304 ssl->state++;
7305
7306 /*
7307 * Switch to our negotiated transform and session parameters for outbound
7308 * data.
7309 */
7310 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7311
7312#if defined(MBEDTLS_SSL_PROTO_DTLS)
7313 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7314 {
7315 unsigned char i;
7316
7317 /* Remember current epoch settings for resending */
7318 ssl->handshake->alt_transform_out = ssl->transform_out;
7319 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7320 sizeof( ssl->handshake->alt_out_ctr ) );
7321
7322 /* Set sequence_number to zero */
7323 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7324
7325
7326 /* Increment epoch */
7327 for( i = 2; i > 0; i-- )
7328 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7329 break;
7330
7331 /* The loop goes to its end iff the counter is wrapping */
7332 if( i == 0 )
7333 {
7334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7335 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7336 }
7337 }
7338 else
7339#endif /* MBEDTLS_SSL_PROTO_DTLS */
7340 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7341
7342 ssl->transform_out = ssl->transform_negotiate;
7343 ssl->session_out = ssl->session_negotiate;
7344
7345#if defined(MBEDTLS_SSL_PROTO_DTLS)
7346 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7347 mbedtls_ssl_send_flight_completed( ssl );
7348#endif
7349
7350 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7351 {
7352 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7353 return( ret );
7354 }
7355
7356#if defined(MBEDTLS_SSL_PROTO_DTLS)
7357 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7358 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7359 {
7360 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7361 return( ret );
7362 }
7363#endif
7364
7365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7366
7367 return( 0 );
7368}
7369
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007370#define SSL_MAX_HASH_LEN 12
7371
7372int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7373{
7374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7375 unsigned int hash_len = 12;
7376 unsigned char buf[SSL_MAX_HASH_LEN];
7377
7378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7379
7380 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7381
7382 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7383 {
7384 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7385 goto exit;
7386 }
7387
7388 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7389 {
7390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7391 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7392 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7393 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7394 goto exit;
7395 }
7396
7397 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7398 {
7399 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7400 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7401 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7402 goto exit;
7403 }
7404
7405 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7406 {
7407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7408 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7409 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7410 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7411 goto exit;
7412 }
7413
7414 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7415 buf, hash_len ) != 0 )
7416 {
7417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7418 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7419 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7420 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7421 goto exit;
7422 }
7423
7424#if defined(MBEDTLS_SSL_RENEGOTIATION)
7425 ssl->verify_data_len = hash_len;
7426 memcpy( ssl->peer_verify_data, buf, hash_len );
7427#endif
7428
7429 if( ssl->handshake->resume != 0 )
7430 {
7431#if defined(MBEDTLS_SSL_CLI_C)
7432 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7433 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7434#endif
7435#if defined(MBEDTLS_SSL_SRV_C)
7436 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7437 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7438#endif
7439 }
7440 else
7441 ssl->state++;
7442
7443#if defined(MBEDTLS_SSL_PROTO_DTLS)
7444 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7445 mbedtls_ssl_recv_flight_completed( ssl );
7446#endif
7447
7448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7449
7450exit:
7451 mbedtls_platform_zeroize( buf, hash_len );
7452 return( ret );
7453}
7454
Jerry Yu392112c2022-02-17 14:34:10 +08007455#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7456/*
7457 * Helper to get TLS 1.2 PRF from ciphersuite
7458 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7459 */
7460static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7461{
Jerry Yu392112c2022-02-17 14:34:10 +08007462 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Andrzej Kurek68327742022-10-03 06:18:18 -04007463 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7464#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007465 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007466 return( tls_prf_sha384 );
Andrzej Kurek894edde2022-09-29 06:31:14 -04007467 else
Jerry Yu392112c2022-02-17 14:34:10 +08007468#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007469#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7470 {
7471 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
7472 return( tls_prf_sha256 );
7473 }
7474#endif
7475#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7476 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7477 (void) ciphersuite_info;
7478#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007479
Andrzej Kurek894edde2022-09-29 06:31:14 -04007480 return( NULL );
Jerry Yu392112c2022-02-17 14:34:10 +08007481}
7482#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007483
7484static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7485{
7486 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007487#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007488 if( tls_prf == tls_prf_sha384 )
7489 {
7490 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7491 }
7492 else
7493#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007494#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007495 if( tls_prf == tls_prf_sha256 )
7496 {
7497 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7498 }
7499 else
7500#endif
7501 return( MBEDTLS_SSL_TLS_PRF_NONE );
7502}
7503
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007504/*
7505 * Populate a transform structure with session keys and all the other
7506 * necessary information.
7507 *
7508 * Parameters:
7509 * - [in/out]: transform: structure to populate
7510 * [in] must be just initialised with mbedtls_ssl_transform_init()
7511 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7512 * - [in] ciphersuite
7513 * - [in] master
7514 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007515 * - [in] tls_prf: pointer to PRF to use for key derivation
7516 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007517 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007518 * - [in] endpoint: client or server
7519 * - [in] ssl: used for:
7520 * - ssl->conf->{f,p}_export_keys
7521 * [in] optionally used for:
7522 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7523 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007524MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007525static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7526 int ciphersuite,
7527 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007528#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007529 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007530#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007531 ssl_tls_prf_t tls_prf,
7532 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007533 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007534 unsigned endpoint,
7535 const mbedtls_ssl_context *ssl )
7536{
7537 int ret = 0;
7538 unsigned char keyblk[256];
7539 unsigned char *key1;
7540 unsigned char *key2;
7541 unsigned char *mac_enc;
7542 unsigned char *mac_dec;
7543 size_t mac_key_len = 0;
7544 size_t iv_copy_len;
7545 size_t keylen;
7546 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007547 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007548#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007549 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007550 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007551#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007552
7553#if defined(MBEDTLS_USE_PSA_CRYPTO)
7554 psa_key_type_t key_type;
7555 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7556 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007557 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007558 size_t key_bits;
7559 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7560#endif
7561
7562#if !defined(MBEDTLS_DEBUG_C) && \
7563 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7564 if( ssl->f_export_keys == NULL )
7565 {
7566 ssl = NULL; /* make sure we don't use it except for these cases */
7567 (void) ssl;
7568 }
7569#endif
7570
7571 /*
7572 * Some data just needs copying into the structure
7573 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007574#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007575 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007576#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007577 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007578
7579#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7580 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7581#endif
7582
7583#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007584 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007585 {
7586 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7587 * generation separate. This should never happen. */
7588 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7589 }
7590#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7591
7592 /*
7593 * Get various info structures
7594 */
7595 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7596 if( ciphersuite_info == NULL )
7597 {
7598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7599 ciphersuite ) );
7600 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7601 }
7602
Neil Armstrongab555e02022-04-04 11:07:59 +02007603 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007604#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007605 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007606#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007607 ciphersuite_info );
7608
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007609 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7610 transform->taglen =
7611 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7612
7613#if defined(MBEDTLS_USE_PSA_CRYPTO)
7614 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7615 transform->taglen,
7616 &alg,
7617 &key_type,
7618 &key_bits ) ) != PSA_SUCCESS )
7619 {
7620 ret = psa_ssl_status_to_mbedtls( status );
7621 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7622 goto end;
7623 }
7624#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007625 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7626 if( cipher_info == NULL )
7627 {
7628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7629 ciphersuite_info->cipher ) );
7630 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7631 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007632#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007633
Neil Armstronge4512952022-03-08 09:08:22 +01007634#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007635 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007636 if( mac_alg == 0 )
7637 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007639 (unsigned) ciphersuite_info->mac ) );
7640 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7641 }
7642#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007643 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7644 if( md_info == NULL )
7645 {
7646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7647 (unsigned) ciphersuite_info->mac ) );
7648 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7649 }
Neil Armstronge4512952022-03-08 09:08:22 +01007650#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007651
7652#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7653 /* Copy own and peer's CID if the use of the CID
7654 * extension has been negotiated. */
7655 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7656 {
7657 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7658
7659 transform->in_cid_len = ssl->own_cid_len;
7660 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7661 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7662 transform->in_cid_len );
7663
7664 transform->out_cid_len = ssl->handshake->peer_cid_len;
7665 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7666 ssl->handshake->peer_cid_len );
7667 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7668 transform->out_cid_len );
7669 }
7670#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7671
7672 /*
7673 * Compute key block using the PRF
7674 */
7675 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7676 if( ret != 0 )
7677 {
7678 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7679 return( ret );
7680 }
7681
7682 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7683 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7684 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7685 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7686 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7687
7688 /*
7689 * Determine the appropriate key, IV and MAC length.
7690 */
7691
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007692#if defined(MBEDTLS_USE_PSA_CRYPTO)
7693 keylen = PSA_BITS_TO_BYTES(key_bits);
7694#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007695 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007696#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007697
7698#if defined(MBEDTLS_GCM_C) || \
7699 defined(MBEDTLS_CCM_C) || \
7700 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007701 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007702 {
7703 size_t explicit_ivlen;
7704
7705 transform->maclen = 0;
7706 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007707
7708 /* All modes haves 96-bit IVs, but the length of the static parts vary
7709 * with mode and version:
7710 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7711 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7712 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7713 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7714 * sequence number).
7715 */
7716 transform->ivlen = 12;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007717#if defined(MBEDTLS_USE_PSA_CRYPTO)
7718 if( key_type == PSA_KEY_TYPE_CHACHA20 )
7719#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007720 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007721#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007722 transform->fixed_ivlen = 12;
7723 else
7724 transform->fixed_ivlen = 4;
7725
7726 /* Minimum length of encrypted record */
7727 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7728 transform->minlen = explicit_ivlen + transform->taglen;
7729 }
7730 else
7731#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7732#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007733 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7734 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7735 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007736 {
Neil Armstronge4512952022-03-08 09:08:22 +01007737#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007738 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007739#else
7740 size_t block_size = cipher_info->block_size;
7741#endif /* MBEDTLS_USE_PSA_CRYPTO */
7742
7743#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007744 /* Get MAC length */
7745 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7746#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007747 /* Initialize HMAC contexts */
7748 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7749 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7750 {
7751 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7752 goto end;
7753 }
7754
7755 /* Get MAC length */
7756 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007757#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007758 transform->maclen = mac_key_len;
7759
7760 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007761#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007762 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007763#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007764 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007765#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007766
7767 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007768 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007769 transform->minlen = transform->maclen;
7770 else
7771 {
7772 /*
7773 * GenericBlockCipher:
7774 * 1. if EtM is in use: one block plus MAC
7775 * otherwise: * first multiple of blocklen greater than maclen
7776 * 2. IV
7777 */
7778#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007779 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007780 {
7781 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007782 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007783 }
7784 else
7785#endif
7786 {
7787 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007788 + block_size
7789 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007790 }
7791
Glenn Strauss07c64162022-03-14 12:34:51 -04007792 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007793 {
7794 transform->minlen += transform->ivlen;
7795 }
7796 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007797 {
7798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7799 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7800 goto end;
7801 }
7802 }
7803 }
7804 else
7805#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7806 {
7807 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7808 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7809 }
7810
7811 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7812 (unsigned) keylen,
7813 (unsigned) transform->minlen,
7814 (unsigned) transform->ivlen,
7815 (unsigned) transform->maclen ) );
7816
7817 /*
7818 * Finally setup the cipher contexts, IVs and MAC secrets.
7819 */
7820#if defined(MBEDTLS_SSL_CLI_C)
7821 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7822 {
7823 key1 = keyblk + mac_key_len * 2;
7824 key2 = keyblk + mac_key_len * 2 + keylen;
7825
7826 mac_enc = keyblk;
7827 mac_dec = keyblk + mac_key_len;
7828
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007829 iv_copy_len = ( transform->fixed_ivlen ) ?
7830 transform->fixed_ivlen : transform->ivlen;
7831 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7832 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7833 iv_copy_len );
7834 }
7835 else
7836#endif /* MBEDTLS_SSL_CLI_C */
7837#if defined(MBEDTLS_SSL_SRV_C)
7838 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7839 {
7840 key1 = keyblk + mac_key_len * 2 + keylen;
7841 key2 = keyblk + mac_key_len * 2;
7842
7843 mac_enc = keyblk + mac_key_len;
7844 mac_dec = keyblk;
7845
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007846 iv_copy_len = ( transform->fixed_ivlen ) ?
7847 transform->fixed_ivlen : transform->ivlen;
7848 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7849 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7850 iv_copy_len );
7851 }
7852 else
7853#endif /* MBEDTLS_SSL_SRV_C */
7854 {
7855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7856 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7857 goto end;
7858 }
7859
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007860 if( ssl != NULL && ssl->f_export_keys != NULL )
7861 {
7862 ssl->f_export_keys( ssl->p_export_keys,
7863 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7864 master, 48,
7865 randbytes + 32,
7866 randbytes,
7867 tls_prf_get_type( tls_prf ) );
7868 }
7869
7870#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007871 transform->psa_alg = alg;
7872
7873 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7874 {
7875 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7876 psa_set_key_algorithm( &attributes, alg );
7877 psa_set_key_type( &attributes, key_type );
7878
7879 if( ( status = psa_import_key( &attributes,
7880 key1,
7881 PSA_BITS_TO_BYTES( key_bits ),
7882 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7883 {
7884 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7885 ret = psa_ssl_status_to_mbedtls( status );
7886 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7887 goto end;
7888 }
7889
7890 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7891
7892 if( ( status = psa_import_key( &attributes,
7893 key2,
7894 PSA_BITS_TO_BYTES( key_bits ),
7895 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7896 {
7897 ret = psa_ssl_status_to_mbedtls( status );
7898 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7899 goto end;
7900 }
7901 }
7902#else
7903 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7904 cipher_info ) ) != 0 )
7905 {
7906 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7907 goto end;
7908 }
7909
7910 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7911 cipher_info ) ) != 0 )
7912 {
7913 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7914 goto end;
7915 }
7916
7917 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7918 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7919 MBEDTLS_ENCRYPT ) ) != 0 )
7920 {
7921 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7922 goto end;
7923 }
7924
7925 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7926 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7927 MBEDTLS_DECRYPT ) ) != 0 )
7928 {
7929 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7930 goto end;
7931 }
7932
7933#if defined(MBEDTLS_CIPHER_MODE_CBC)
7934 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7935 {
7936 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7937 MBEDTLS_PADDING_NONE ) ) != 0 )
7938 {
7939 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7940 goto end;
7941 }
7942
7943 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7944 MBEDTLS_PADDING_NONE ) ) != 0 )
7945 {
7946 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7947 goto end;
7948 }
7949 }
7950#endif /* MBEDTLS_CIPHER_MODE_CBC */
7951#endif /* MBEDTLS_USE_PSA_CRYPTO */
7952
Neil Armstrong29c0c042022-03-17 17:47:28 +01007953#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7954 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7955 For AEAD-based ciphersuites, there is nothing to do here. */
7956 if( mac_key_len != 0 )
7957 {
7958#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007959 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007960
7961 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007962 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007963 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7964
7965 if( ( status = psa_import_key( &attributes,
7966 mac_enc, mac_key_len,
7967 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7968 {
7969 ret = psa_ssl_status_to_mbedtls( status );
7970 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7971 goto end;
7972 }
7973
Ronald Cronfb39f152022-03-25 14:36:28 +01007974 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04007975 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
7976#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
7977 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
7978#endif
7979 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007980 /* mbedtls_ct_hmac() requires the key to be exportable */
7981 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7982 PSA_KEY_USAGE_VERIFY_HASH );
7983 else
7984 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7985
7986 if( ( status = psa_import_key( &attributes,
7987 mac_dec, mac_key_len,
7988 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7989 {
7990 ret = psa_ssl_status_to_mbedtls( status );
7991 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7992 goto end;
7993 }
7994#else
7995 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7996 if( ret != 0 )
7997 goto end;
7998 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7999 if( ret != 0 )
8000 goto end;
8001#endif /* MBEDTLS_USE_PSA_CRYPTO */
8002 }
8003#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8004
8005 ((void) mac_dec);
8006 ((void) mac_enc);
8007
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008008end:
8009 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
8010 return( ret );
8011}
8012
Jerry Yuee40f9d2022-02-17 14:55:16 +08008013#if defined(MBEDTLS_USE_PSA_CRYPTO)
8014int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8015 unsigned char *hash, size_t *hashlen,
8016 unsigned char *data, size_t data_len,
8017 mbedtls_md_type_t md_alg )
8018{
8019 psa_status_t status;
8020 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008021 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008022
8023 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8024
8025 if( ( status = psa_hash_setup( &hash_operation,
8026 hash_alg ) ) != PSA_SUCCESS )
8027 {
8028 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8029 goto exit;
8030 }
8031
8032 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8033 64 ) ) != PSA_SUCCESS )
8034 {
8035 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8036 goto exit;
8037 }
8038
8039 if( ( status = psa_hash_update( &hash_operation,
8040 data, data_len ) ) != PSA_SUCCESS )
8041 {
8042 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8043 goto exit;
8044 }
8045
8046 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8047 hashlen ) ) != PSA_SUCCESS )
8048 {
8049 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8050 goto exit;
8051 }
8052
8053exit:
8054 if( status != PSA_SUCCESS )
8055 {
8056 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8057 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8058 switch( status )
8059 {
8060 case PSA_ERROR_NOT_SUPPORTED:
8061 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8062 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8063 case PSA_ERROR_BUFFER_TOO_SMALL:
8064 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8065 case PSA_ERROR_INSUFFICIENT_MEMORY:
8066 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8067 default:
8068 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8069 }
8070 }
8071 return( 0 );
8072}
8073
8074#else
8075
8076int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8077 unsigned char *hash, size_t *hashlen,
8078 unsigned char *data, size_t data_len,
8079 mbedtls_md_type_t md_alg )
8080{
8081 int ret = 0;
8082 mbedtls_md_context_t ctx;
8083 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8084 *hashlen = mbedtls_md_get_size( md_info );
8085
8086 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8087
8088 mbedtls_md_init( &ctx );
8089
8090 /*
8091 * digitally-signed struct {
8092 * opaque client_random[32];
8093 * opaque server_random[32];
8094 * ServerDHParams params;
8095 * };
8096 */
8097 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8098 {
8099 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8100 goto exit;
8101 }
8102 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8103 {
8104 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8105 goto exit;
8106 }
8107 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8108 {
8109 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8110 goto exit;
8111 }
8112 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8113 {
8114 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8115 goto exit;
8116 }
8117 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8118 {
8119 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8120 goto exit;
8121 }
8122
8123exit:
8124 mbedtls_md_free( &ctx );
8125
8126 if( ret != 0 )
8127 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8128 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8129
8130 return( ret );
8131}
8132#endif /* MBEDTLS_USE_PSA_CRYPTO */
8133
Jerry Yud9d91da2022-02-17 14:57:06 +08008134#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8135
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008136/* Find the preferred hash for a given signature algorithm. */
8137unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8138 mbedtls_ssl_context *ssl,
8139 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008140{
Gabor Mezei078e8032022-04-27 21:17:56 +02008141 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008142 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008143
8144 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008145 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008146
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008147 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008148 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008149 unsigned int hash_alg_received =
8150 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8151 received_sig_algs[i] );
8152 unsigned int sig_alg_received =
8153 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8154 received_sig_algs[i] );
8155
8156 if( sig_alg == sig_alg_received )
8157 {
8158#if defined(MBEDTLS_USE_PSA_CRYPTO)
8159 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8160 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008161 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008162 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008163
Neil Armstrong96eceb82022-06-30 18:05:05 +02008164 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8165 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8166 PSA_ALG_ECDSA( psa_hash_alg ),
8167 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008168 continue;
8169
Neil Armstrong96eceb82022-06-30 18:05:05 +02008170 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008171 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8172 PSA_ALG_RSA_PKCS1V15_SIGN(
8173 psa_hash_alg ),
8174 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008175 continue;
8176 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008177#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008178
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008179 return( hash_alg_received );
8180 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008181 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008182
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008183 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008184}
8185
8186#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8187
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008188/* Serialization of TLS 1.2 sessions:
8189 *
8190 * struct {
8191 * uint64 start_time;
8192 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008193 * uint8 session_id_len; // at most 32
8194 * opaque session_id[32];
8195 * opaque master[48]; // fixed length in the standard
8196 * uint32 verify_result;
8197 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8198 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8199 * uint32 ticket_lifetime;
8200 * uint8 mfl_code; // up to 255 according to standard
8201 * uint8 encrypt_then_mac; // 0 or 1
8202 * } serialized_session_tls12;
8203 *
8204 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008205static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008206 unsigned char *buf,
8207 size_t buf_len )
8208{
8209 unsigned char *p = buf;
8210 size_t used = 0;
8211
8212#if defined(MBEDTLS_HAVE_TIME)
8213 uint64_t start;
8214#endif
8215#if defined(MBEDTLS_X509_CRT_PARSE_C)
8216#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8217 size_t cert_len;
8218#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8219#endif /* MBEDTLS_X509_CRT_PARSE_C */
8220
8221 /*
8222 * Time
8223 */
8224#if defined(MBEDTLS_HAVE_TIME)
8225 used += 8;
8226
8227 if( used <= buf_len )
8228 {
8229 start = (uint64_t) session->start;
8230
8231 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8232 p += 8;
8233 }
8234#endif /* MBEDTLS_HAVE_TIME */
8235
8236 /*
8237 * Basic mandatory fields
8238 */
8239 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008240 + 1 /* id_len */
8241 + sizeof( session->id )
8242 + sizeof( session->master )
8243 + 4; /* verify_result */
8244
8245 if( used <= buf_len )
8246 {
8247 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8248 p += 2;
8249
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008250 *p++ = MBEDTLS_BYTE_0( session->id_len );
8251 memcpy( p, session->id, 32 );
8252 p += 32;
8253
8254 memcpy( p, session->master, 48 );
8255 p += 48;
8256
8257 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8258 p += 4;
8259 }
8260
8261 /*
8262 * Peer's end-entity certificate
8263 */
8264#if defined(MBEDTLS_X509_CRT_PARSE_C)
8265#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8266 if( session->peer_cert == NULL )
8267 cert_len = 0;
8268 else
8269 cert_len = session->peer_cert->raw.len;
8270
8271 used += 3 + cert_len;
8272
8273 if( used <= buf_len )
8274 {
8275 *p++ = MBEDTLS_BYTE_2( cert_len );
8276 *p++ = MBEDTLS_BYTE_1( cert_len );
8277 *p++ = MBEDTLS_BYTE_0( cert_len );
8278
8279 if( session->peer_cert != NULL )
8280 {
8281 memcpy( p, session->peer_cert->raw.p, cert_len );
8282 p += cert_len;
8283 }
8284 }
8285#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8286 if( session->peer_cert_digest != NULL )
8287 {
8288 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8289 if( used <= buf_len )
8290 {
8291 *p++ = (unsigned char) session->peer_cert_digest_type;
8292 *p++ = (unsigned char) session->peer_cert_digest_len;
8293 memcpy( p, session->peer_cert_digest,
8294 session->peer_cert_digest_len );
8295 p += session->peer_cert_digest_len;
8296 }
8297 }
8298 else
8299 {
8300 used += 2;
8301 if( used <= buf_len )
8302 {
8303 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8304 *p++ = 0;
8305 }
8306 }
8307#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8308#endif /* MBEDTLS_X509_CRT_PARSE_C */
8309
8310 /*
8311 * Session ticket if any, plus associated data
8312 */
8313#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8314 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8315
8316 if( used <= buf_len )
8317 {
8318 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8319 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8320 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8321
8322 if( session->ticket != NULL )
8323 {
8324 memcpy( p, session->ticket, session->ticket_len );
8325 p += session->ticket_len;
8326 }
8327
8328 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8329 p += 4;
8330 }
8331#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8332
8333 /*
8334 * Misc extension-related info
8335 */
8336#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8337 used += 1;
8338
8339 if( used <= buf_len )
8340 *p++ = session->mfl_code;
8341#endif
8342
8343#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8344 used += 1;
8345
8346 if( used <= buf_len )
8347 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8348#endif
8349
8350 return( used );
8351}
8352
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008353MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008354static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008355 const unsigned char *buf,
8356 size_t len )
8357{
8358#if defined(MBEDTLS_HAVE_TIME)
8359 uint64_t start;
8360#endif
8361#if defined(MBEDTLS_X509_CRT_PARSE_C)
8362#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8363 size_t cert_len;
8364#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8365#endif /* MBEDTLS_X509_CRT_PARSE_C */
8366
8367 const unsigned char *p = buf;
8368 const unsigned char * const end = buf + len;
8369
8370 /*
8371 * Time
8372 */
8373#if defined(MBEDTLS_HAVE_TIME)
8374 if( 8 > (size_t)( end - p ) )
8375 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8376
8377 start = ( (uint64_t) p[0] << 56 ) |
8378 ( (uint64_t) p[1] << 48 ) |
8379 ( (uint64_t) p[2] << 40 ) |
8380 ( (uint64_t) p[3] << 32 ) |
8381 ( (uint64_t) p[4] << 24 ) |
8382 ( (uint64_t) p[5] << 16 ) |
8383 ( (uint64_t) p[6] << 8 ) |
8384 ( (uint64_t) p[7] );
8385 p += 8;
8386
8387 session->start = (time_t) start;
8388#endif /* MBEDTLS_HAVE_TIME */
8389
8390 /*
8391 * Basic mandatory fields
8392 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008393 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008394 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8395
8396 session->ciphersuite = ( p[0] << 8 ) | p[1];
8397 p += 2;
8398
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008399 session->id_len = *p++;
8400 memcpy( session->id, p, 32 );
8401 p += 32;
8402
8403 memcpy( session->master, p, 48 );
8404 p += 48;
8405
8406 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8407 ( (uint32_t) p[1] << 16 ) |
8408 ( (uint32_t) p[2] << 8 ) |
8409 ( (uint32_t) p[3] );
8410 p += 4;
8411
8412 /* Immediately clear invalid pointer values that have been read, in case
8413 * we exit early before we replaced them with valid ones. */
8414#if defined(MBEDTLS_X509_CRT_PARSE_C)
8415#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8416 session->peer_cert = NULL;
8417#else
8418 session->peer_cert_digest = NULL;
8419#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8420#endif /* MBEDTLS_X509_CRT_PARSE_C */
8421#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8422 session->ticket = NULL;
8423#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8424
8425 /*
8426 * Peer certificate
8427 */
8428#if defined(MBEDTLS_X509_CRT_PARSE_C)
8429#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8430 /* Deserialize CRT from the end of the ticket. */
8431 if( 3 > (size_t)( end - p ) )
8432 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8433
8434 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8435 p += 3;
8436
8437 if( cert_len != 0 )
8438 {
8439 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8440
8441 if( cert_len > (size_t)( end - p ) )
8442 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8443
8444 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8445
8446 if( session->peer_cert == NULL )
8447 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8448
8449 mbedtls_x509_crt_init( session->peer_cert );
8450
8451 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8452 p, cert_len ) ) != 0 )
8453 {
8454 mbedtls_x509_crt_free( session->peer_cert );
8455 mbedtls_free( session->peer_cert );
8456 session->peer_cert = NULL;
8457 return( ret );
8458 }
8459
8460 p += cert_len;
8461 }
8462#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8463 /* Deserialize CRT digest from the end of the ticket. */
8464 if( 2 > (size_t)( end - p ) )
8465 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8466
8467 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8468 session->peer_cert_digest_len = (size_t) *p++;
8469
8470 if( session->peer_cert_digest_len != 0 )
8471 {
8472 const mbedtls_md_info_t *md_info =
8473 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8474 if( md_info == NULL )
8475 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8476 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8477 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8478
8479 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8480 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8481
8482 session->peer_cert_digest =
8483 mbedtls_calloc( 1, session->peer_cert_digest_len );
8484 if( session->peer_cert_digest == NULL )
8485 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8486
8487 memcpy( session->peer_cert_digest, p,
8488 session->peer_cert_digest_len );
8489 p += session->peer_cert_digest_len;
8490 }
8491#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8492#endif /* MBEDTLS_X509_CRT_PARSE_C */
8493
8494 /*
8495 * Session ticket and associated data
8496 */
8497#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8498 if( 3 > (size_t)( end - p ) )
8499 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8500
8501 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8502 p += 3;
8503
8504 if( session->ticket_len != 0 )
8505 {
8506 if( session->ticket_len > (size_t)( end - p ) )
8507 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8508
8509 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8510 if( session->ticket == NULL )
8511 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8512
8513 memcpy( session->ticket, p, session->ticket_len );
8514 p += session->ticket_len;
8515 }
8516
8517 if( 4 > (size_t)( end - p ) )
8518 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8519
8520 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8521 ( (uint32_t) p[1] << 16 ) |
8522 ( (uint32_t) p[2] << 8 ) |
8523 ( (uint32_t) p[3] );
8524 p += 4;
8525#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8526
8527 /*
8528 * Misc extension-related info
8529 */
8530#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8531 if( 1 > (size_t)( end - p ) )
8532 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8533
8534 session->mfl_code = *p++;
8535#endif
8536
8537#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8538 if( 1 > (size_t)( end - p ) )
8539 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8540
8541 session->encrypt_then_mac = *p++;
8542#endif
8543
8544 /* Done, should have consumed entire buffer */
8545 if( p != end )
8546 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8547
8548 return( 0 );
8549}
Jerry Yudc7bd172022-02-17 13:44:15 +08008550#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8551
XiaokangQian75d40ef2022-04-20 11:05:24 +00008552int mbedtls_ssl_validate_ciphersuite(
8553 const mbedtls_ssl_context *ssl,
8554 const mbedtls_ssl_ciphersuite_t *suite_info,
8555 mbedtls_ssl_protocol_version min_tls_version,
8556 mbedtls_ssl_protocol_version max_tls_version )
8557{
8558 (void) ssl;
8559
8560 if( suite_info == NULL )
8561 return( -1 );
8562
8563 if( ( suite_info->min_tls_version > max_tls_version ) ||
8564 ( suite_info->max_tls_version < min_tls_version ) )
8565 {
8566 return( -1 );
8567 }
8568
XiaokangQian060d8672022-04-21 09:24:56 +00008569#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008570#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8571 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8572 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8573 {
8574 return( -1 );
8575 }
8576#endif
8577
8578 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8579#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8580 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8581 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8582 {
8583 return( -1 );
8584 }
8585#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8586#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8587
8588 return( 0 );
8589}
8590
XiaokangQianeaf36512022-04-24 09:07:44 +00008591#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8592/*
8593 * Function for writing a signature algorithm extension.
8594 *
8595 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8596 * value (TLS 1.3 RFC8446):
8597 * enum {
8598 * ....
8599 * ecdsa_secp256r1_sha256( 0x0403 ),
8600 * ecdsa_secp384r1_sha384( 0x0503 ),
8601 * ecdsa_secp521r1_sha512( 0x0603 ),
8602 * ....
8603 * } SignatureScheme;
8604 *
8605 * struct {
8606 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8607 * } SignatureSchemeList;
8608 *
8609 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8610 * value (TLS 1.2 RFC5246):
8611 * enum {
8612 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8613 * sha512(6), (255)
8614 * } HashAlgorithm;
8615 *
8616 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8617 * SignatureAlgorithm;
8618 *
8619 * struct {
8620 * HashAlgorithm hash;
8621 * SignatureAlgorithm signature;
8622 * } SignatureAndHashAlgorithm;
8623 *
8624 * SignatureAndHashAlgorithm
8625 * supported_signature_algorithms<2..2^16-2>;
8626 *
8627 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8628 * generalization of the TLS 1.2 signature algorithm extension.
8629 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8630 * `SignatureScheme` field of TLS 1.3
8631 *
8632 */
8633int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8634 const unsigned char *end, size_t *out_len )
8635{
8636 unsigned char *p = buf;
8637 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8638 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8639
8640 *out_len = 0;
8641
8642 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8643
8644 /* Check if we have space for header and length field:
8645 * - extension_type (2 bytes)
8646 * - extension_data_length (2 bytes)
8647 * - supported_signature_algorithms_length (2 bytes)
8648 */
8649 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8650 p += 6;
8651
8652 /*
8653 * Write supported_signature_algorithms
8654 */
8655 supported_sig_alg = p;
8656 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8657 if( sig_alg == NULL )
8658 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8659
8660 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8661 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008662 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8663 *sig_alg,
8664 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008665 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8666 continue;
8667 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8668 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8669 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008670 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008671 *sig_alg,
8672 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008673 }
8674
8675 /* Length of supported_signature_algorithms */
8676 supported_sig_alg_len = p - supported_sig_alg;
8677 if( supported_sig_alg_len == 0 )
8678 {
8679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8680 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8681 }
8682
XiaokangQianeaf36512022-04-24 09:07:44 +00008683 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008684 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008685 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8686
XiaokangQianeaf36512022-04-24 09:07:44 +00008687 *out_len = p - buf;
8688
8689#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8690 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8691#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8692 return( 0 );
8693}
8694#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8695
XiaokangQian40a35232022-05-07 09:02:40 +00008696#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008697/*
8698 * mbedtls_ssl_parse_server_name_ext
8699 *
8700 * Structure of server_name extension:
8701 *
8702 * enum {
8703 * host_name(0), (255)
8704 * } NameType;
8705 * opaque HostName<1..2^16-1>;
8706 *
8707 * struct {
8708 * NameType name_type;
8709 * select (name_type) {
8710 * case host_name: HostName;
8711 * } name;
8712 * } ServerName;
8713 * struct {
8714 * ServerName server_name_list<1..2^16-1>
8715 * } ServerNameList;
8716 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008717MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008718int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8719 const unsigned char *buf,
8720 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008721{
8722 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8723 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008724 size_t server_name_list_len, hostname_len;
8725 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008726
XiaokangQianf2a94202022-05-20 06:44:24 +00008727 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008728
8729 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008730 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008731 p += 2;
8732
XiaokangQian9b2b7712022-05-17 02:57:00 +00008733 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8734 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008735 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008736 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008737 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008738 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008739 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8740 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008741
8742 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8743 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008744 /* sni_name is intended to be used only during the parsing of the
8745 * ClientHello message (it is reset to NULL before the end of
8746 * the message parsing). Thus it is ok to just point to the
8747 * reception buffer and not make a copy of it.
8748 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008749 ssl->handshake->sni_name = p + 3;
8750 ssl->handshake->sni_name_len = hostname_len;
8751 if( ssl->conf->f_sni == NULL )
8752 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008753 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008754 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008755 if( ret != 0 )
8756 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008757 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008758 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8759 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008760 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8761 }
8762 return( 0 );
8763 }
8764
8765 p += hostname_len + 3;
8766 }
8767
8768 return( 0 );
8769}
8770#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8771
XiaokangQianacb39922022-06-17 10:18:48 +00008772#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008773MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008774int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8775 const unsigned char *buf,
8776 const unsigned char *end )
8777{
8778 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008779 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008780 const unsigned char *protocol_name_list;
8781 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008782 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008783
8784 /* If ALPN not configured, just ignore the extension */
8785 if( ssl->conf->alpn_list == NULL )
8786 return( 0 );
8787
8788 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008789 * RFC7301, section 3.1
8790 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008791 *
XiaokangQianc7403452022-06-23 03:24:12 +00008792 * struct {
8793 * ProtocolName protocol_name_list<2..2^16-1>
8794 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008795 */
8796
XiaokangQianc7403452022-06-23 03:24:12 +00008797 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008798 * protocol_name_list_len 2 bytes
8799 * protocol_name_len 1 bytes
8800 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008801 */
XiaokangQianacb39922022-06-17 10:18:48 +00008802 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8803
XiaokangQianc7403452022-06-23 03:24:12 +00008804 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008805 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008806 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008807 protocol_name_list = p;
8808 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008809
8810 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008811 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008812 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008813 protocol_name_len = *p++;
8814 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8815 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008816 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008817 {
8818 MBEDTLS_SSL_PEND_FATAL_ALERT(
8819 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8820 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008821 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008822 }
8823
8824 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008825 }
8826
8827 /* Use our order of preference */
8828 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8829 {
8830 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008831 p = protocol_name_list;
8832 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008833 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008834 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008835 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008836 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008837 {
8838 ssl->alpn_chosen = *alpn;
8839 return( 0 );
8840 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008841
8842 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008843 }
8844 }
8845
XiaokangQian95d5f542022-06-24 02:29:26 +00008846 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008847 MBEDTLS_SSL_PEND_FATAL_ALERT(
8848 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8849 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8850 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8851}
8852
8853int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8854 unsigned char *buf,
8855 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008856 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008857{
8858 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008859 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008860 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008861
8862 if( ssl->alpn_chosen == NULL )
8863 {
8864 return( 0 );
8865 }
8866
XiaokangQian95d5f542022-06-24 02:29:26 +00008867 protocol_name_len = strlen( ssl->alpn_chosen );
8868 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008869
8870 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8871 /*
8872 * 0 . 1 ext identifier
8873 * 2 . 3 ext length
8874 * 4 . 5 protocol list length
8875 * 6 . 6 protocol name length
8876 * 7 . 7+n protocol name
8877 */
8878 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8879
XiaokangQian95d5f542022-06-24 02:29:26 +00008880 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008881
XiaokangQian95d5f542022-06-24 02:29:26 +00008882 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8883 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008884 /* Note: the length of the chosen protocol has been checked to be less
8885 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8886 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008887 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008888
XiaokangQian95d5f542022-06-24 02:29:26 +00008889 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008890 return ( 0 );
8891}
8892#endif /* MBEDTLS_SSL_ALPN */
8893
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008894#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00008895 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00008896 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00008897 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008898int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
8899 const char *hostname )
8900{
8901 /* Initialize to suppress unnecessary compiler warning */
8902 size_t hostname_len = 0;
8903
8904 /* Check if new hostname is valid before
8905 * making any change to current one */
8906 if( hostname != NULL )
8907 {
8908 hostname_len = strlen( hostname );
8909
8910 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8911 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8912 }
8913
8914 /* Now it's clear that we will overwrite the old hostname,
8915 * so we can free it safely */
8916 if( session->hostname != NULL )
8917 {
8918 mbedtls_platform_zeroize( session->hostname,
8919 strlen( session->hostname ) );
8920 mbedtls_free( session->hostname );
8921 }
8922
8923 /* Passing NULL as hostname shall clear the old one */
8924 if( hostname == NULL )
8925 {
8926 session->hostname = NULL;
8927 }
8928 else
8929 {
8930 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
8931 if( session->hostname == NULL )
8932 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8933
8934 memcpy( session->hostname, hostname, hostname_len );
8935 }
8936
8937 return( 0 );
8938}
Xiaokang Qian03409292022-10-12 02:49:52 +00008939#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
8940 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00008941 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00008942 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008944#endif /* MBEDTLS_SSL_TLS_C */