blob: 66bbf122a27b3a7d2233eff7a1e4e772e99a0ab1 [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#if defined(MBEDTLS_PLATFORM_C)
31#include "mbedtls/platform.h"
32#else
33#include <stdlib.h>
Jerry Yu6ade7432022-01-25 10:39:33 +080034#include <stdio.h>
SimonBd5800b72016-04-26 07:43:27 +010035#define mbedtls_calloc calloc
36#define mbedtls_free free
Jerry Yu6ade7432022-01-25 10:39:33 +080037#define mbedtls_printf printf
38#endif /* !MBEDTLS_PLATFORM_C */
SimonBd5800b72016-04-26 07:43:27 +010039
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010041#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010042#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000043#include "ssl_misc.h"
Janos Follath73c616b2019-12-18 15:07:04 +000044#include "mbedtls/debug.h"
45#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050046#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010047#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020048#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020049
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <string.h>
51
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050052#if defined(MBEDTLS_USE_PSA_CRYPTO)
53#include "mbedtls/psa_util.h"
54#include "psa/crypto.h"
55#endif
56
Janos Follath23bdca02016-10-07 14:47:14 +010057#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020059#endif
60
Ronald Cronad8c17b2022-06-10 17:18:09 +020061#if defined(MBEDTLS_TEST_HOOKS)
62static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
63
64void mbedtls_ssl_set_chk_buf_ptr_fail_args(
65 const uint8_t *cur, const uint8_t *end, size_t need )
66{
67 chk_buf_ptr_fail_args.cur = cur;
68 chk_buf_ptr_fail_args.end = end;
69 chk_buf_ptr_fail_args.need = need;
70}
71
72void mbedtls_ssl_reset_chk_buf_ptr_fail_args( void )
73{
74 memset( &chk_buf_ptr_fail_args, 0, sizeof( chk_buf_ptr_fail_args ) );
75}
76
77int mbedtls_ssl_cmp_chk_buf_ptr_fail_args( mbedtls_ssl_chk_buf_ptr_args *args )
78{
79 return( ( chk_buf_ptr_fail_args.cur != args->cur ) ||
80 ( chk_buf_ptr_fail_args.end != args->end ) ||
81 ( chk_buf_ptr_fail_args.need != args->need ) );
82}
83#endif /* MBEDTLS_TEST_HOOKS */
84
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020085#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010086
Hanno Beckera0e20d02019-05-15 14:03:01 +010087#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010088/* Top-level Connection ID API */
89
Hanno Becker8367ccc2019-05-14 11:30:10 +010090int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
91 size_t len,
92 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +010093{
94 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
95 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
96
Hanno Becker611ac772019-05-14 11:45:26 +010097 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
98 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
99 {
100 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
101 }
102
103 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100104 conf->cid_len = len;
105 return( 0 );
106}
107
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100108int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
109 int enable,
110 unsigned char const *own_cid,
111 size_t own_cid_len )
112{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100113 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
114 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
115
Hanno Beckerca092242019-04-25 16:01:49 +0100116 ssl->negotiate_cid = enable;
117 if( enable == MBEDTLS_SSL_CID_DISABLED )
118 {
119 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
120 return( 0 );
121 }
122 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100123 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100124
Hanno Beckerad4a1372019-05-03 13:06:44 +0100125 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100126 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100127 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
128 (unsigned) own_cid_len,
129 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100130 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
131 }
132
133 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100134 /* Truncation is not an issue here because
135 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
136 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100137
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100138 return( 0 );
139}
140
Paul Elliott0113cf12022-03-11 20:26:47 +0000141int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl,
142 int *enabled,
143 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
144 size_t *own_cid_len )
145{
146 *enabled = MBEDTLS_SSL_CID_DISABLED;
147
148 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
149 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
150
151 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
152 * zero as this is indistinguishable from not requesting to use
153 * the CID extension. */
154 if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
155 return( 0 );
156
157 if( own_cid_len != NULL )
158 {
159 *own_cid_len = ssl->own_cid_len;
160 if( own_cid != NULL )
161 memcpy( own_cid, ssl->own_cid, ssl->own_cid_len );
162 }
163
164 *enabled = MBEDTLS_SSL_CID_ENABLED;
165
166 return( 0 );
167}
168
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100169int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
170 int *enabled,
171 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
172 size_t *peer_cid_len )
173{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100174 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100175
Hanno Becker76a79ab2019-05-03 14:38:32 +0100176 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Paul Elliott27b0d942022-03-18 21:55:32 +0000177 mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Hanno Becker76a79ab2019-05-03 14:38:32 +0100178 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100179 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100180 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100181
Hanno Beckerc5f24222019-05-03 12:54:52 +0100182 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
183 * were used, but client and server requested the empty CID.
184 * This is indistinguishable from not using the CID extension
185 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100186 if( ssl->transform_in->in_cid_len == 0 &&
187 ssl->transform_in->out_cid_len == 0 )
188 {
189 return( 0 );
190 }
191
Hanno Becker615ef172019-05-22 16:50:35 +0100192 if( peer_cid_len != NULL )
193 {
194 *peer_cid_len = ssl->transform_in->out_cid_len;
195 if( peer_cid != NULL )
196 {
197 memcpy( peer_cid, ssl->transform_in->out_cid,
198 ssl->transform_in->out_cid_len );
199 }
200 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100201
202 *enabled = MBEDTLS_SSL_CID_ENABLED;
203
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100204 return( 0 );
205}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100206#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200211/*
212 * Convert max_fragment_length codes to length.
213 * RFC 6066 says:
214 * enum{
215 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
216 * } MaxFragmentLength;
217 * and we add 0 -> extension unused
218 */
Angus Grattond8213d02016-05-25 20:56:48 +1000219static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200220{
Angus Grattond8213d02016-05-25 20:56:48 +1000221 switch( mfl )
222 {
223 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
224 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
225 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
226 return 512;
227 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
228 return 1024;
229 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
230 return 2048;
231 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
232 return 4096;
233 default:
234 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
235 }
236}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200238
Hanno Becker52055ae2019-02-06 14:30:46 +0000239int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
240 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200241{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_ssl_session_free( dst );
243 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200244
吴敬辉0b716112021-11-29 10:46:35 +0800245#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
246 dst->ticket = NULL;
247#endif
248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000250
251#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200252 if( src->peer_cert != NULL )
253 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000254 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200255
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200256 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200257 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200258 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200263 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200266 dst->peer_cert = NULL;
267 return( ret );
268 }
269 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000270#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000271 if( src->peer_cert_digest != NULL )
272 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000273 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000274 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000275 if( dst->peer_cert_digest == NULL )
276 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
277
278 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
279 src->peer_cert_digest_len );
280 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000281 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000282 }
283#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200286
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200287#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200288 if( src->ticket != NULL )
289 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200290 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200291 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200292 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200293
294 memcpy( dst->ticket, src->ticket, src->ticket_len );
295 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200296#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200297
298 return( 0 );
299}
300
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500301#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200302MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500303static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
304{
305 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
306 if( resized_buffer == NULL )
307 return -1;
308
309 /* We want to copy len_new bytes when downsizing the buffer, and
310 * len_old bytes when upsizing, so we choose the smaller of two sizes,
311 * to fit one buffer into another. Size checks, ensuring that no data is
312 * lost, are done outside of this function. */
313 memcpy( resized_buffer, *buffer,
314 ( len_new < *len_old ) ? len_new : *len_old );
315 mbedtls_platform_zeroize( *buffer, *len_old );
316 mbedtls_free( *buffer );
317
318 *buffer = resized_buffer;
319 *len_old = len_new;
320
321 return 0;
322}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200323
324static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500325 size_t in_buf_new_len,
326 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200327{
328 int modified = 0;
329 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
330 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
331 if( ssl->in_buf != NULL )
332 {
333 written_in = ssl->in_msg - ssl->in_buf;
334 iv_offset_in = ssl->in_iv - ssl->in_buf;
335 len_offset_in = ssl->in_len - ssl->in_buf;
336 if( downsizing ?
337 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
338 ssl->in_buf_len < in_buf_new_len )
339 {
340 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
341 {
342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
343 }
344 else
345 {
Paul Elliottb7449902021-03-10 18:14:58 +0000346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
347 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200348 modified = 1;
349 }
350 }
351 }
352
353 if( ssl->out_buf != NULL )
354 {
355 written_out = ssl->out_msg - ssl->out_buf;
356 iv_offset_out = ssl->out_iv - ssl->out_buf;
357 len_offset_out = ssl->out_len - ssl->out_buf;
358 if( downsizing ?
359 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
360 ssl->out_buf_len < out_buf_new_len )
361 {
362 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
363 {
364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
365 }
366 else
367 {
Paul Elliottb7449902021-03-10 18:14:58 +0000368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
369 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200370 modified = 1;
371 }
372 }
373 }
374 if( modified )
375 {
376 /* Update pointers here to avoid doing it twice. */
377 mbedtls_ssl_reset_in_out_pointers( ssl );
378 /* Fields below might not be properly updated with record
379 * splitting or with CID, so they are manually updated here. */
380 ssl->out_msg = ssl->out_buf + written_out;
381 ssl->out_len = ssl->out_buf + len_offset_out;
382 ssl->out_iv = ssl->out_buf + iv_offset_out;
383
384 ssl->in_msg = ssl->in_buf + written_in;
385 ssl->in_len = ssl->in_buf + len_offset_in;
386 ssl->in_iv = ssl->in_buf + iv_offset_in;
387 }
388}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500389#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
390
Jerry Yudb8c48a2022-01-27 14:54:54 +0800391#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800392
393#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
394typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
395 const char *label,
396 const unsigned char *random, size_t rlen,
397 unsigned char *dstbuf, size_t dlen );
398
399static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
400
401#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
402
403/* Type for the TLS PRF */
404typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
405 const unsigned char *, size_t,
406 unsigned char *, size_t);
407
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200408MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800409static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
410 int ciphersuite,
411 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200412#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yued14c932022-02-17 13:40:45 +0800413 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200414#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yued14c932022-02-17 13:40:45 +0800415 ssl_tls_prf_t tls_prf,
416 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -0400417 mbedtls_ssl_protocol_version tls_version,
Jerry Yued14c932022-02-17 13:40:45 +0800418 unsigned endpoint,
419 const mbedtls_ssl_context *ssl );
420
421#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200422MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800423static int tls_prf_sha256( const unsigned char *secret, size_t slen,
424 const char *label,
425 const unsigned char *random, size_t rlen,
426 unsigned char *dstbuf, size_t dlen );
427static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
428static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
429
430#endif /* MBEDTLS_SHA256_C */
431
432#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200433MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800434static int tls_prf_sha384( const unsigned char *secret, size_t slen,
435 const char *label,
436 const unsigned char *random, size_t rlen,
437 unsigned char *dstbuf, size_t dlen );
438
439static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
440static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
441#endif /* MBEDTLS_SHA384_C */
442
Jerry Yu438ddd82022-07-07 06:55:50 +0000443static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800444 unsigned char *buf,
445 size_t buf_len );
Jerry Yu251a12e2022-07-13 15:15:48 +0800446
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200447MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +0000448static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800449 const unsigned char *buf,
450 size_t len );
451#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
452
Jerry Yu53d23e22022-02-09 16:25:09 +0800453static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
454
Jerry Yudb8c48a2022-01-27 14:54:54 +0800455#if defined(MBEDTLS_SHA256_C)
456static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Jerry Yu53d23e22022-02-09 16:25:09 +0800457#endif /* MBEDTLS_SHA256_C */
Jerry Yudb8c48a2022-01-27 14:54:54 +0800458
459#if defined(MBEDTLS_SHA384_C)
460static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Jerry Yu53d23e22022-02-09 16:25:09 +0800461#endif /* MBEDTLS_SHA384_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000462
Ron Eldor51d3ab52019-05-12 14:54:30 +0300463int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
464 const unsigned char *secret, size_t slen,
465 const char *label,
466 const unsigned char *random, size_t rlen,
467 unsigned char *dstbuf, size_t dlen )
468{
469 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
470
471 switch( prf )
472 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300473#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +0200474#if defined(MBEDTLS_SHA384_C)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300475 case MBEDTLS_SSL_TLS_PRF_SHA384:
476 tls_prf = tls_prf_sha384;
477 break;
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +0200478#endif /* MBEDTLS_SHA384_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300479#if defined(MBEDTLS_SHA256_C)
480 case MBEDTLS_SSL_TLS_PRF_SHA256:
481 tls_prf = tls_prf_sha256;
482 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300483#endif /* MBEDTLS_SHA256_C */
484#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300485 default:
486 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
487 }
488
489 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
490}
491
Jerry Yuc73c6182022-02-08 20:29:25 +0800492#if defined(MBEDTLS_X509_CRT_PARSE_C)
493static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
494{
495#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
496 if( session->peer_cert != NULL )
497 {
498 mbedtls_x509_crt_free( session->peer_cert );
499 mbedtls_free( session->peer_cert );
500 session->peer_cert = NULL;
501 }
502#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
503 if( session->peer_cert_digest != NULL )
504 {
505 /* Zeroization is not necessary. */
506 mbedtls_free( session->peer_cert_digest );
507 session->peer_cert_digest = NULL;
508 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
509 session->peer_cert_digest_len = 0;
510 }
511#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
512}
513#endif /* MBEDTLS_X509_CRT_PARSE_C */
514
515void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
516 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
517{
518 ((void) ciphersuite_info);
519
520#if defined(MBEDTLS_SHA384_C)
521 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
522 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
523 else
524#endif
525#if defined(MBEDTLS_SHA256_C)
526 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
527 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
528 else
529#endif
530 {
531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
532 return;
533 }
534}
535
XiaokangQianadab9a62022-07-18 07:41:26 +0000536void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
537 unsigned hs_type,
538 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100539{
540 unsigned char hs_hdr[4];
541
542 /* Build HS header for checksum update. */
543 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
544 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
545 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
546 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
547
548 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
549}
550
551void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
552 unsigned hs_type,
553 unsigned char const *msg,
554 size_t msg_len )
555{
556 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
557 ssl->handshake->update_checksum( ssl, msg, msg_len );
558}
559
Jerry Yuc73c6182022-02-08 20:29:25 +0800560void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
561{
562 ((void) ssl);
563#if defined(MBEDTLS_SHA256_C)
564#if defined(MBEDTLS_USE_PSA_CRYPTO)
565 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
566 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
567#else
568 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
569#endif
570#endif
571#if defined(MBEDTLS_SHA384_C)
572#if defined(MBEDTLS_USE_PSA_CRYPTO)
573 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
574 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
575#else
576 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
577#endif
578#endif
579}
580
581static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
582 const unsigned char *buf, size_t len )
583{
584#if defined(MBEDTLS_SHA256_C)
585#if defined(MBEDTLS_USE_PSA_CRYPTO)
586 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
587#else
588 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
589#endif
590#endif
591#if defined(MBEDTLS_SHA384_C)
592#if defined(MBEDTLS_USE_PSA_CRYPTO)
593 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
594#else
595 mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len );
596#endif
597#endif
598}
599
600#if defined(MBEDTLS_SHA256_C)
601static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
602 const unsigned char *buf, size_t len )
603{
604#if defined(MBEDTLS_USE_PSA_CRYPTO)
605 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
606#else
607 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
608#endif
609}
610#endif
611
612#if defined(MBEDTLS_SHA384_C)
613static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
614 const unsigned char *buf, size_t len )
615{
616#if defined(MBEDTLS_USE_PSA_CRYPTO)
617 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
618#else
619 mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len );
620#endif
621}
622#endif
623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200625{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500629#if defined(MBEDTLS_USE_PSA_CRYPTO)
630 handshake->fin_sha256_psa = psa_hash_operation_init();
631 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
632#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200634 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200635#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500636#endif
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +0200637#if defined(MBEDTLS_SHA384_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500638#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500639 handshake->fin_sha384_psa = psa_hash_operation_init();
640 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500641#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_sha512_init( &handshake->fin_sha512 );
TRodziewicz26371e42021-06-08 16:45:41 +0200643 mbedtls_sha512_starts( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200644#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500645#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200646
647 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#if defined(MBEDTLS_DHM_C)
650 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200651#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200652#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200654#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200655#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200656 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200657#if defined(MBEDTLS_SSL_CLI_C)
658 handshake->ecjpake_cache = NULL;
659 handshake->ecjpake_cache_len = 0;
660#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200661#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200662
Gilles Peskineeccd8882020-03-10 12:19:08 +0100663#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200664 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200665#endif
666
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200667#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
668 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
669#endif
Hanno Becker75173122019-02-06 16:18:31 +0000670
671#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
672 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
673 mbedtls_pk_init( &handshake->peer_pubkey );
674#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200675}
676
Hanno Beckera18d1322018-01-03 14:27:32 +0000677void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200678{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200680
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100681#if defined(MBEDTLS_USE_PSA_CRYPTO)
682 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
683 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100684#else
685 mbedtls_cipher_init( &transform->cipher_ctx_enc );
686 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100687#endif
688
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000689#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100690#if defined(MBEDTLS_USE_PSA_CRYPTO)
691 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
692 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100693#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 mbedtls_md_init( &transform->md_ctx_enc );
695 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000696#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100697#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200698}
699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200701{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200703}
704
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200705MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000707{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200708 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000709 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200711 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200713 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200714 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200715
716 /*
717 * Either the pointers are now NULL or cleared properly and can be freed.
718 * Now allocate missing structures.
719 */
720 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200721 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200722 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200723 }
Paul Bakker48916f92012-09-16 19:57:18 +0000724
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200725 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200726 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200727 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200728 }
Paul Bakker48916f92012-09-16 19:57:18 +0000729
Paul Bakker82788fb2014-10-20 13:59:19 +0200730 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200731 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200732 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200733 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500734#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
735 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400736
Andrzej Kurek4a063792020-10-21 15:08:44 +0200737 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
738 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500739#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000740
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200741 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000742 if( ssl->handshake == NULL ||
743 ssl->transform_negotiate == NULL ||
744 ssl->session_negotiate == NULL )
745 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 mbedtls_free( ssl->handshake );
749 mbedtls_free( ssl->transform_negotiate );
750 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200751
752 ssl->handshake = NULL;
753 ssl->transform_negotiate = NULL;
754 ssl->session_negotiate = NULL;
755
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200756 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000757 }
758
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200759 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000761 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200762 ssl_handshake_params_init( ssl->handshake );
763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200765 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
766 {
767 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200768
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200769 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
770 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
771 else
772 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200773
Hanno Becker0f57a652020-02-05 10:37:26 +0000774 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200775 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200776#endif
777
Brett Warrene0edc842021-08-17 09:53:13 +0100778/*
779 * curve_list is translated to IANA TLS group identifiers here because
780 * mbedtls_ssl_conf_curves returns void and so can't return
781 * any error codes.
782 */
783#if defined(MBEDTLS_ECP_C)
784#if !defined(MBEDTLS_DEPRECATED_REMOVED)
785 /* Heap allocate and translate curve_list from internal to IANA group ids */
786 if ( ssl->conf->curve_list != NULL )
787 {
788 size_t length;
789 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
790
791 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
792 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
793
794 /* Leave room for zero termination */
795 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
796 if ( group_list == NULL )
797 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
798
799 for( size_t i = 0; i < length; i++ )
800 {
801 const mbedtls_ecp_curve_info *info =
802 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
803 if ( info == NULL )
804 {
805 mbedtls_free( group_list );
806 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
807 }
808 group_list[i] = info->tls_id;
809 }
810
811 group_list[length] = 0;
812
813 ssl->handshake->group_list = group_list;
814 ssl->handshake->group_list_heap_allocated = 1;
815 }
816 else
817 {
818 ssl->handshake->group_list = ssl->conf->group_list;
819 ssl->handshake->group_list_heap_allocated = 0;
820 }
821#endif /* MBEDTLS_DEPRECATED_REMOVED */
822#endif /* MBEDTLS_ECP_C */
823
Jerry Yuf017ee42022-01-12 15:49:48 +0800824#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
825#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800826#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800827 /* Heap allocate and translate sig_hashes from internal hash identifiers to
828 signature algorithms IANA identifiers. */
829 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800830 ssl->conf->sig_hashes != NULL )
831 {
832 const int *md;
833 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800834 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800835 uint16_t *p;
836
Jerry Yub476a442022-01-21 18:14:45 +0800837#if defined(static_assert)
838 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
839 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
840 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800841#endif
842
Jerry Yuf017ee42022-01-12 15:49:48 +0800843 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
844 {
845 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
846 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800847#if defined(MBEDTLS_ECDSA_C)
848 sig_algs_len += sizeof( uint16_t );
849#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800850
Jerry Yub476a442022-01-21 18:14:45 +0800851#if defined(MBEDTLS_RSA_C)
852 sig_algs_len += sizeof( uint16_t );
853#endif
854 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
855 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800856 }
857
Jerry Yub476a442022-01-21 18:14:45 +0800858 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800859 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
860
Jerry Yub476a442022-01-21 18:14:45 +0800861 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
862 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800863 if( ssl->handshake->sig_algs == NULL )
864 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
865
866 p = (uint16_t *)ssl->handshake->sig_algs;
867 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
868 {
869 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
870 if( hash == MBEDTLS_SSL_HASH_NONE )
871 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800872#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800873 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
874 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800875#endif
876#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800877 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
878 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800879#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800880 }
Gabor Mezei15b95a62022-05-09 16:37:58 +0200881 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +0800882 ssl->handshake->sig_algs_heap_allocated = 1;
883 }
884 else
Jerry Yua69269a2022-01-17 21:06:01 +0800885#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800886 {
Jerry Yuf017ee42022-01-12 15:49:48 +0800887 ssl->handshake->sig_algs_heap_allocated = 0;
888 }
Jerry Yucc539102022-06-27 16:27:35 +0800889#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yuf017ee42022-01-12 15:49:48 +0800890#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000891 return( 0 );
892}
893
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200894#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200895/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200896MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200897static int ssl_cookie_write_dummy( void *ctx,
898 unsigned char **p, unsigned char *end,
899 const unsigned char *cli_id, size_t cli_id_len )
900{
901 ((void) ctx);
902 ((void) p);
903 ((void) end);
904 ((void) cli_id);
905 ((void) cli_id_len);
906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200908}
909
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200910MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200911static int ssl_cookie_check_dummy( void *ctx,
912 const unsigned char *cookie, size_t cookie_len,
913 const unsigned char *cli_id, size_t cli_id_len )
914{
915 ((void) ctx);
916 ((void) cookie);
917 ((void) cookie_len);
918 ((void) cli_id);
919 ((void) cli_id_len);
920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200922}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200923#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200924
Paul Bakker5121ce52009-01-03 21:22:43 +0000925/*
926 * Initialize an SSL context
927 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200928void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
929{
930 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
931}
932
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200933MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800934static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
935{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100936 const mbedtls_ssl_config *conf = ssl->conf;
937
Ronald Cron6f135e12021-12-08 16:57:54 +0100938#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100939 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
940 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000941 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
942 {
943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
944 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
945 }
946
Jerry Yu60835a82021-08-04 10:13:52 +0800947 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
948 return( 0 );
949 }
950#endif
951
952#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100953 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800954 {
955 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
956 return( 0 );
957 }
958#endif
959
Ronald Cron6f135e12021-12-08 16:57:54 +0100960#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100961 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800962 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000963 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
964 {
965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
966 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
967 }
968
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000969 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
970 {
971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
972 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
973 }
974
975
Ronald Crone1d3f062022-02-10 14:50:54 +0100976 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
977 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +0800978 }
979#endif
980
981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
982 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
983}
984
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200985MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800986static int ssl_conf_check(const mbedtls_ssl_context *ssl)
987{
988 int ret;
989 ret = ssl_conf_version_check( ssl );
990 if( ret != 0 )
991 return( ret );
992
993 /* Space for further checks */
994
995 return( 0 );
996}
997
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200998/*
999 * Setup an SSL context
1000 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001001
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001002int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001003 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001004{
Janos Follath865b3eb2019-12-16 11:46:15 +00001005 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001006 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1007 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001008
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001009 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001010
Jerry Yu60835a82021-08-04 10:13:52 +08001011 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1012 return( ret );
1013
Paul Bakker62f2dee2012-09-28 07:31:51 +00001014 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001015 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001016 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001017
1018 /* Set to NULL in case of an error condition */
1019 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001020
Darryl Greenb33cc762019-11-28 14:29:44 +00001021#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1022 ssl->in_buf_len = in_buf_len;
1023#endif
1024 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001025 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001026 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001028 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001029 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001030 }
1031
Darryl Greenb33cc762019-11-28 14:29:44 +00001032#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1033 ssl->out_buf_len = out_buf_len;
1034#endif
1035 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001036 if( ssl->out_buf == NULL )
1037 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001039 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001040 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001041 }
1042
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001043 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001044
Johan Pascalb62bb512015-12-03 21:56:45 +01001045#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001046 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001047#endif
1048
Paul Bakker48916f92012-09-16 19:57:18 +00001049 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001050 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001051
1052 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001053
1054error:
1055 mbedtls_free( ssl->in_buf );
1056 mbedtls_free( ssl->out_buf );
1057
1058 ssl->conf = NULL;
1059
Darryl Greenb33cc762019-11-28 14:29:44 +00001060#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1061 ssl->in_buf_len = 0;
1062 ssl->out_buf_len = 0;
1063#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001064 ssl->in_buf = NULL;
1065 ssl->out_buf = NULL;
1066
1067 ssl->in_hdr = NULL;
1068 ssl->in_ctr = NULL;
1069 ssl->in_len = NULL;
1070 ssl->in_iv = NULL;
1071 ssl->in_msg = NULL;
1072
1073 ssl->out_hdr = NULL;
1074 ssl->out_ctr = NULL;
1075 ssl->out_len = NULL;
1076 ssl->out_iv = NULL;
1077 ssl->out_msg = NULL;
1078
k-stachowiak9f7798e2018-07-31 16:52:32 +02001079 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001080}
1081
1082/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001083 * Reset an initialized and used SSL context for re-use while retaining
1084 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001085 *
1086 * If partial is non-zero, keep data in the input buffer and client ID.
1087 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001088 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001089void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1090 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001091{
Darryl Greenb33cc762019-11-28 14:29:44 +00001092#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1093 size_t in_buf_len = ssl->in_buf_len;
1094 size_t out_buf_len = ssl->out_buf_len;
1095#else
1096 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1097 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1098#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001099
Hanno Beckerb0302c42021-08-03 09:39:42 +01001100#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1101 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001102#endif
1103
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001104 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001105 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001106
Hanno Beckerb0302c42021-08-03 09:39:42 +01001107 mbedtls_ssl_reset_in_out_pointers( ssl );
1108
1109 /* Reset incoming message parsing */
1110 ssl->in_offt = NULL;
1111 ssl->nb_zero = 0;
1112 ssl->in_msgtype = 0;
1113 ssl->in_msglen = 0;
1114 ssl->in_hslen = 0;
1115 ssl->keep_current_message = 0;
1116 ssl->transform_in = NULL;
1117
1118#if defined(MBEDTLS_SSL_PROTO_DTLS)
1119 ssl->next_record_offset = 0;
1120 ssl->in_epoch = 0;
1121#endif
1122
1123 /* Keep current datagram if partial == 1 */
1124 if( partial == 0 )
1125 {
1126 ssl->in_left = 0;
1127 memset( ssl->in_buf, 0, in_buf_len );
1128 }
1129
Ronald Cronad8c17b2022-06-10 17:18:09 +02001130 ssl->send_alert = 0;
1131
Hanno Beckerb0302c42021-08-03 09:39:42 +01001132 /* Reset outgoing message writing */
1133 ssl->out_msgtype = 0;
1134 ssl->out_msglen = 0;
1135 ssl->out_left = 0;
1136 memset( ssl->out_buf, 0, out_buf_len );
1137 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1138 ssl->transform_out = NULL;
1139
1140#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1141 mbedtls_ssl_dtls_replay_reset( ssl );
1142#endif
1143
XiaokangQian2b01dc32022-01-21 02:53:13 +00001144#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001145 if( ssl->transform )
1146 {
1147 mbedtls_ssl_transform_free( ssl->transform );
1148 mbedtls_free( ssl->transform );
1149 ssl->transform = NULL;
1150 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001151#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1152
XiaokangQian2b01dc32022-01-21 02:53:13 +00001153#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1154 mbedtls_ssl_transform_free( ssl->transform_application );
1155 mbedtls_free( ssl->transform_application );
1156 ssl->transform_application = NULL;
1157
1158 if( ssl->handshake != NULL )
1159 {
1160 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1161 mbedtls_free( ssl->handshake->transform_earlydata );
1162 ssl->handshake->transform_earlydata = NULL;
1163
1164 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1165 mbedtls_free( ssl->handshake->transform_handshake );
1166 ssl->handshake->transform_handshake = NULL;
1167 }
1168
XiaokangQian2b01dc32022-01-21 02:53:13 +00001169#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001170}
1171
1172int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1173{
1174 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1175
1176 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1177
XiaokangQian78b1fa72022-01-19 06:56:30 +00001178 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001179
1180 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181#if defined(MBEDTLS_SSL_RENEGOTIATION)
1182 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001183 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001184
1185 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1187 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001188#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001190
Hanno Beckerb0302c42021-08-03 09:39:42 +01001191 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001192 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001193 if( ssl->session )
1194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 mbedtls_ssl_session_free( ssl->session );
1196 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001197 ssl->session = NULL;
1198 }
1199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001201 ssl->alpn_chosen = NULL;
1202#endif
1203
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001204#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01001205#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001206 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01001207#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001208 {
1209 mbedtls_free( ssl->cli_id );
1210 ssl->cli_id = NULL;
1211 ssl->cli_id_len = 0;
1212 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001213#endif
1214
Paul Bakker48916f92012-09-16 19:57:18 +00001215 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1216 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001217
1218 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001219}
1220
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001221/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001222 * Reset an initialized and used SSL context for re-use while retaining
1223 * all application-set variables, function pointers and data.
1224 */
1225int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1226{
Hanno Becker43aefe22020-02-05 10:44:56 +00001227 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001228}
1229
1230/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001231 * SSL set accessors
1232 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001233void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001234{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001235 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001236}
1237
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001238void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001239{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001240 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001241}
1242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001244void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001245{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001246 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001247}
1248#endif
1249
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001250void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001251{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001252 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001253}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001256
Hanno Becker1841b0a2018-08-24 11:13:57 +01001257void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1258 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001259{
1260 ssl->disable_datagram_packing = !allow_packing;
1261}
1262
1263void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1264 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001265{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001266 conf->hs_timeout_min = min;
1267 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001268}
1269#endif
1270
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001271void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001272{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001273 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001274}
1275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001277void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001278 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001279 void *p_vrfy )
1280{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001281 conf->f_vrfy = f_vrfy;
1282 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001283}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001285
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001286void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001287 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001288 void *p_rng )
1289{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001290 conf->f_rng = f_rng;
1291 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001292}
1293
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001294void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001295 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001296 void *p_dbg )
1297{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001298 conf->f_dbg = f_dbg;
1299 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001300}
1301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001303 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001304 mbedtls_ssl_send_t *f_send,
1305 mbedtls_ssl_recv_t *f_recv,
1306 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001307{
1308 ssl->p_bio = p_bio;
1309 ssl->f_send = f_send;
1310 ssl->f_recv = f_recv;
1311 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001312}
1313
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001314#if defined(MBEDTLS_SSL_PROTO_DTLS)
1315void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1316{
1317 ssl->mtu = mtu;
1318}
1319#endif
1320
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001321void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001322{
1323 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001324}
1325
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001326void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1327 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001328 mbedtls_ssl_set_timer_t *f_set_timer,
1329 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001330{
1331 ssl->p_timer = p_timer;
1332 ssl->f_set_timer = f_set_timer;
1333 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001334
1335 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001336 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001337}
1338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001340void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001341 void *p_cache,
1342 mbedtls_ssl_cache_get_t *f_get_cache,
1343 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001344{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001345 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001346 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001347 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001348}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351#if defined(MBEDTLS_SSL_CLI_C)
1352int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001353{
Janos Follath865b3eb2019-12-16 11:46:15 +00001354 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001355
1356 if( ssl == NULL ||
1357 session == NULL ||
1358 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001359 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001362 }
1363
Hanno Beckere810bbc2021-05-14 16:01:05 +01001364 if( ssl->handshake->resume == 1 )
1365 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1366
Hanno Becker52055ae2019-02-06 14:30:46 +00001367 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1368 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001369 return( ret );
1370
Paul Bakker0a597072012-09-25 21:55:46 +00001371 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001372
1373 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001374}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001376
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001377void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1378 const int *ciphersuites )
1379{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001380 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001381}
1382
Ronald Cron6f135e12021-12-08 16:57:54 +01001383#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001384void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001385 const int kex_modes )
1386{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001387 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001388}
Ronald Cron6f135e12021-12-08 16:57:54 +01001389#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001392void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001393 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001394{
1395 conf->cert_profile = profile;
1396}
1397
Glenn Strauss36872db2022-01-22 05:06:31 -05001398static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1399{
1400 mbedtls_ssl_key_cert *cur = key_cert, *next;
1401
1402 while( cur != NULL )
1403 {
1404 next = cur->next;
1405 mbedtls_free( cur );
1406 cur = next;
1407 }
1408}
1409
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001410/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001411MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001412static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1413 mbedtls_x509_crt *cert,
1414 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001415{
niisato8ee24222018-06-25 19:05:48 +09001416 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001417
Glenn Strauss36872db2022-01-22 05:06:31 -05001418 if( cert == NULL )
1419 {
1420 /* Free list if cert is null */
1421 ssl_key_cert_free( *head );
1422 *head = NULL;
1423 return( 0 );
1424 }
1425
niisato8ee24222018-06-25 19:05:48 +09001426 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1427 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001428 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001429
niisato8ee24222018-06-25 19:05:48 +09001430 new_cert->cert = cert;
1431 new_cert->key = key;
1432 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001433
Glenn Strauss36872db2022-01-22 05:06:31 -05001434 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001435 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001436 {
niisato8ee24222018-06-25 19:05:48 +09001437 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001438 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001439 else
1440 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001441 mbedtls_ssl_key_cert *cur = *head;
1442 while( cur->next != NULL )
1443 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001444 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001445 }
1446
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001447 return( 0 );
1448}
1449
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001450int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001451 mbedtls_x509_crt *own_cert,
1452 mbedtls_pk_context *pk_key )
1453{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001454 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001455}
1456
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001457void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001458 mbedtls_x509_crt *ca_chain,
1459 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001460{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001461 conf->ca_chain = ca_chain;
1462 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001463
1464#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1465 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1466 * cannot be used together. */
1467 conf->f_ca_cb = NULL;
1468 conf->p_ca_cb = NULL;
1469#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001470}
Hanno Becker5adaad92019-03-27 16:54:37 +00001471
1472#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1473void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001474 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001475 void *p_ca_cb )
1476{
1477 conf->f_ca_cb = f_ca_cb;
1478 conf->p_ca_cb = p_ca_cb;
1479
1480 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1481 * cannot be used together. */
1482 conf->ca_chain = NULL;
1483 conf->ca_crl = NULL;
1484}
1485#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001487
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001488#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001489const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1490 size_t *name_len )
1491{
1492 *name_len = ssl->handshake->sni_name_len;
1493 return( ssl->handshake->sni_name );
1494}
1495
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001496int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1497 mbedtls_x509_crt *own_cert,
1498 mbedtls_pk_context *pk_key )
1499{
1500 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1501 own_cert, pk_key ) );
1502}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001503
1504void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1505 mbedtls_x509_crt *ca_chain,
1506 mbedtls_x509_crl *ca_crl )
1507{
1508 ssl->handshake->sni_ca_chain = ca_chain;
1509 ssl->handshake->sni_ca_crl = ca_crl;
1510}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001511
Glenn Strauss999ef702022-03-11 01:37:23 -05001512#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1513void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1514 const mbedtls_x509_crt *crt)
1515{
1516 ssl->handshake->dn_hints = crt;
1517}
1518#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1519
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001520void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1521 int authmode )
1522{
1523 ssl->handshake->sni_authmode = authmode;
1524}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001525#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1526
Hanno Becker8927c832019-04-03 12:52:50 +01001527#if defined(MBEDTLS_X509_CRT_PARSE_C)
1528void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1529 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1530 void *p_vrfy )
1531{
1532 ssl->f_vrfy = f_vrfy;
1533 ssl->p_vrfy = p_vrfy;
1534}
1535#endif
1536
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001537#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001538/*
1539 * Set EC J-PAKE password for current handshake
1540 */
1541int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1542 const unsigned char *pw,
1543 size_t pw_len )
1544{
1545 mbedtls_ecjpake_role role;
1546
Janos Follath8eb64132016-06-03 15:40:57 +01001547 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001548 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1549
1550 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1551 role = MBEDTLS_ECJPAKE_SERVER;
1552 else
1553 role = MBEDTLS_ECJPAKE_CLIENT;
1554
1555 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1556 role,
1557 MBEDTLS_MD_SHA256,
1558 MBEDTLS_ECP_DP_SECP256R1,
1559 pw, pw_len ) );
1560}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001561#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001562
Gilles Peskineeccd8882020-03-10 12:19:08 +01001563#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001564
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001565MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001566static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1567{
1568#if defined(MBEDTLS_USE_PSA_CRYPTO)
1569 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1570 return( 1 );
1571#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001572 if( conf->psk != NULL )
1573 return( 1 );
1574
1575 return( 0 );
1576}
1577
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001578static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1579{
1580 /* Remove reference to existing PSK, if any. */
1581#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001582 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001583 {
1584 /* The maintenance of the PSK key slot is the
1585 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001586 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001587 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001588#endif /* MBEDTLS_USE_PSA_CRYPTO */
1589 if( conf->psk != NULL )
1590 {
1591 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1592
1593 mbedtls_free( conf->psk );
1594 conf->psk = NULL;
1595 conf->psk_len = 0;
1596 }
1597
1598 /* Remove reference to PSK identity, if any. */
1599 if( conf->psk_identity != NULL )
1600 {
1601 mbedtls_free( conf->psk_identity );
1602 conf->psk_identity = NULL;
1603 conf->psk_identity_len = 0;
1604 }
1605}
1606
Hanno Becker7390c712018-11-15 13:33:04 +00001607/* This function assumes that PSK identity in the SSL config is unset.
1608 * It checks that the provided identity is well-formed and attempts
1609 * to make a copy of it in the SSL config.
1610 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001611MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001612static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1613 unsigned char const *psk_identity,
1614 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001615{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001616 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001617 if( psk_identity == NULL ||
1618 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001619 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001620 {
1621 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1622 }
1623
Hanno Becker7390c712018-11-15 13:33:04 +00001624 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1625 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001626 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001627
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001628 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001629 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001630
1631 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001632}
1633
Hanno Becker7390c712018-11-15 13:33:04 +00001634int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1635 const unsigned char *psk, size_t psk_len,
1636 const unsigned char *psk_identity, size_t psk_identity_len )
1637{
Janos Follath865b3eb2019-12-16 11:46:15 +00001638 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001639
1640 /* We currently only support one PSK, raw or opaque. */
1641 if( ssl_conf_psk_is_configured( conf ) )
1642 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001643
1644 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001645 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001646 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001647 if( psk_len == 0 )
1648 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1649 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1650 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1651
Hanno Becker7390c712018-11-15 13:33:04 +00001652 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1653 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1654 conf->psk_len = psk_len;
1655 memcpy( conf->psk, psk, conf->psk_len );
1656
1657 /* Check and set PSK Identity */
1658 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1659 if( ret != 0 )
1660 ssl_conf_remove_psk( conf );
1661
1662 return( ret );
1663}
1664
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001665static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1666{
1667#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001668 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001669 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001670 /* The maintenance of the external PSK key slot is the
1671 * user's responsibility. */
1672 if( ssl->handshake->psk_opaque_is_internal )
1673 {
1674 psa_destroy_key( ssl->handshake->psk_opaque );
1675 ssl->handshake->psk_opaque_is_internal = 0;
1676 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001677 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001678 }
Neil Armstronge952a302022-05-03 10:22:14 +02001679#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001680 if( ssl->handshake->psk != NULL )
1681 {
1682 mbedtls_platform_zeroize( ssl->handshake->psk,
1683 ssl->handshake->psk_len );
1684 mbedtls_free( ssl->handshake->psk );
1685 ssl->handshake->psk_len = 0;
1686 }
Neil Armstronge952a302022-05-03 10:22:14 +02001687#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001688}
1689
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001690int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1691 const unsigned char *psk, size_t psk_len )
1692{
Neil Armstrong501c9322022-05-03 09:35:09 +02001693#if defined(MBEDTLS_USE_PSA_CRYPTO)
1694 psa_key_attributes_t key_attributes = psa_key_attributes_init();
1695 psa_status_t status;
1696 psa_algorithm_t alg;
1697 mbedtls_svc_key_id_t key;
1698#endif /* MBEDTLS_USE_PSA_CRYPTO */
1699
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001700 if( psk == NULL || ssl->handshake == NULL )
1701 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1702
1703 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1704 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1705
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001706 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001707
Neil Armstrong501c9322022-05-03 09:35:09 +02001708#if defined(MBEDTLS_USE_PSA_CRYPTO)
1709 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384)
1710 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1711 else
1712 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1713
Jerry Yu568ec252022-07-22 21:27:34 +08001714#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu1c105562022-07-10 06:32:38 +00001715 psa_set_key_usage_flags( &key_attributes,
1716 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
Jerry Yu568ec252022-07-22 21:27:34 +08001717#else
Neil Armstrong501c9322022-05-03 09:35:09 +02001718 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
Jerry Yu568ec252022-07-22 21:27:34 +08001719#endif
Neil Armstrong501c9322022-05-03 09:35:09 +02001720 psa_set_key_algorithm( &key_attributes, alg );
1721 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1722
1723 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1724 if( status != PSA_SUCCESS )
1725 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1726
1727 /* Allow calling psa_destroy_key() on psk remove */
1728 ssl->handshake->psk_opaque_is_internal = 1;
1729 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1730#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001731 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001732 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001733
1734 ssl->handshake->psk_len = psk_len;
1735 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1736
1737 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001738#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001739}
1740
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001741#if defined(MBEDTLS_USE_PSA_CRYPTO)
1742int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001743 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001744 const unsigned char *psk_identity,
1745 size_t psk_identity_len )
1746{
Janos Follath865b3eb2019-12-16 11:46:15 +00001747 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001748
1749 /* We currently only support one PSK, raw or opaque. */
1750 if( ssl_conf_psk_is_configured( conf ) )
1751 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001752
Hanno Becker7390c712018-11-15 13:33:04 +00001753 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001754 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001755 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001756 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001757
1758 /* Check and set PSK Identity */
1759 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1760 psk_identity_len );
1761 if( ret != 0 )
1762 ssl_conf_remove_psk( conf );
1763
1764 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001765}
1766
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001767int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1768 mbedtls_svc_key_id_t psk )
1769{
1770 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1771 ( ssl->handshake == NULL ) )
1772 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1773
1774 ssl_remove_psk( ssl );
1775 ssl->handshake->psk_opaque = psk;
1776 return( 0 );
1777}
1778#endif /* MBEDTLS_USE_PSA_CRYPTO */
1779
1780void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1781 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1782 size_t),
1783 void *p_psk )
1784{
1785 conf->f_psk = f_psk;
1786 conf->p_psk = p_psk;
1787}
1788#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1789
1790#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001791static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1792 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001793{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001794#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001795 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001796 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001797#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001798 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001799 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001800 return( MBEDTLS_SSL_MODE_STREAM );
1801}
1802
1803#else /* MBEDTLS_USE_PSA_CRYPTO */
1804
1805static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1806 mbedtls_cipher_mode_t mode )
1807{
1808#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1809 if( mode == MBEDTLS_MODE_CBC )
1810 return( MBEDTLS_SSL_MODE_CBC );
1811#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1812
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001813#if defined(MBEDTLS_GCM_C) || \
1814 defined(MBEDTLS_CCM_C) || \
1815 defined(MBEDTLS_CHACHAPOLY_C)
1816 if( mode == MBEDTLS_MODE_GCM ||
1817 mode == MBEDTLS_MODE_CCM ||
1818 mode == MBEDTLS_MODE_CHACHAPOLY )
1819 return( MBEDTLS_SSL_MODE_AEAD );
1820#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001821
1822 return( MBEDTLS_SSL_MODE_STREAM );
1823}
Gilles Peskine301711e2022-04-26 16:57:05 +02001824#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001825
Gilles Peskinee108d982022-04-26 16:50:40 +02001826static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1827 mbedtls_ssl_mode_t base_mode,
1828 int encrypt_then_mac )
1829{
1830#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1831 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1832 base_mode == MBEDTLS_SSL_MODE_CBC )
1833 {
1834 return( MBEDTLS_SSL_MODE_CBC_ETM );
1835 }
1836#else
1837 (void) encrypt_then_mac;
1838#endif
1839 return( base_mode );
1840}
1841
Neil Armstrongab555e02022-04-04 11:07:59 +02001842mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001843 const mbedtls_ssl_transform *transform )
1844{
Gilles Peskinee108d982022-04-26 16:50:40 +02001845 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001846#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001847 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001848#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001849 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1850#endif
1851 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001852
Gilles Peskinee108d982022-04-26 16:50:40 +02001853 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001854#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001855 encrypt_then_mac = transform->encrypt_then_mac;
1856#endif
1857 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001858}
1859
Neil Armstrongab555e02022-04-04 11:07:59 +02001860mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001861#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001862 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001863#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001864 const mbedtls_ssl_ciphersuite_t *suite )
1865{
Gilles Peskinee108d982022-04-26 16:50:40 +02001866 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1867
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001868#if defined(MBEDTLS_USE_PSA_CRYPTO)
1869 psa_status_t status;
1870 psa_algorithm_t alg;
1871 psa_key_type_t type;
1872 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001873 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1874 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001875 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001876#else
1877 const mbedtls_cipher_info_t *cipher =
1878 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001879 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001880 {
1881 base_mode =
1882 mbedtls_ssl_get_base_mode(
1883 mbedtls_cipher_info_get_mode( cipher ) );
1884 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001885#endif /* MBEDTLS_USE_PSA_CRYPTO */
1886
Gilles Peskinee108d982022-04-26 16:50:40 +02001887#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1888 int encrypt_then_mac = 0;
1889#endif
1890 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001891}
1892
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001893#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001894
1895#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001896/* Serialization of TLS 1.3 sessions:
1897 *
1898 * struct {
1899 * uint64 ticket_received;
1900 * uint32 ticket_lifetime;
Jerry Yue36fdd62022-08-17 21:31:36 +08001901 * opaque ticket<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001902 * } ClientOnlyData;
1903 *
1904 * struct {
1905 * uint8 endpoint;
1906 * uint8 ciphersuite[2];
1907 * uint32 ticket_age_add;
1908 * uint8 ticket_flags;
1909 * opaque resumption_key<0..255>;
1910 * select ( endpoint ) {
1911 * case client: ClientOnlyData;
1912 * case server: uint64 start_time;
1913 * };
1914 * } serialized_session_tls13;
1915 *
1916 */
1917#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08001918MBEDTLS_CHECK_RETURN_CRITICAL
1919static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
1920 unsigned char *buf,
1921 size_t buf_len,
1922 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00001923{
1924 unsigned char *p = buf;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001925 size_t needed = 1 /* endpoint */
1926 + 2 /* ciphersuite */
1927 + 4 /* ticket_age_add */
1928 + 2 /* resumption_key length */
1929 + session->resumption_key_len; /* resumption_key */
Jerry Yue36fdd62022-08-17 21:31:36 +08001930 *olen = 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00001931#if defined(MBEDTLS_HAVE_TIME)
1932 needed += 8; /* start_time or ticket_received */
1933#endif
1934
1935#if defined(MBEDTLS_SSL_CLI_C)
1936 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1937 {
1938 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08001939 + 2; /* ticket_len */
1940 if( session->ticket_len > SIZE_MAX - needed )
1941 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1942 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00001943 }
1944#endif /* MBEDTLS_SSL_CLI_C */
1945
Jerry Yue36fdd62022-08-17 21:31:36 +08001946 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00001947 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08001948 {
1949 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1950 }
Jerry Yu438ddd82022-07-07 06:55:50 +00001951
1952 p[0] = session->endpoint;
1953 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
1954 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
1955 p[7] = session->ticket_flags;
1956
1957 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08001958 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00001959 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001960 memcpy( p, session->resumption_key, session->resumption_key_len );
1961 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00001962
1963#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
1964 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
1965 {
1966 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
1967 p += 8;
1968 }
1969#endif /* MBEDTLS_HAVE_TIME */
1970
1971#if defined(MBEDTLS_SSL_CLI_C)
1972 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1973 {
1974#if defined(MBEDTLS_HAVE_TIME)
1975 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
1976 p += 8;
1977#endif
1978 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
1979 p += 4;
1980
1981 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
1982 p += 2;
1983 if( session->ticket_len > 0 )
1984 {
1985 memcpy( p, session->ticket, session->ticket_len );
1986 p += session->ticket_len;
1987 }
1988 }
1989#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08001990 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00001991}
1992
1993MBEDTLS_CHECK_RETURN_CRITICAL
1994static int ssl_tls13_session_load( mbedtls_ssl_session *session,
1995 const unsigned char *buf,
1996 size_t len )
1997{
1998 const unsigned char *p = buf;
1999 const unsigned char *end = buf + len;
2000
2001 if( end - p < 9 )
2002 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2003 session->endpoint = p[0];
2004 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2005 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2006 session->ticket_flags = p[7];
2007
2008 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002009 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002010 p += 9;
2011
Jerry Yubc7c1a42022-07-21 22:57:37 +08002012 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002013 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2014
Jerry Yubc7c1a42022-07-21 22:57:37 +08002015 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002016 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002017 memcpy( session->resumption_key, p, session->resumption_key_len );
2018 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002019
2020#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2021 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2022 {
2023 if( end - p < 8 )
2024 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2025 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2026 p += 8;
2027 }
2028#endif /* MBEDTLS_HAVE_TIME */
2029
2030#if defined(MBEDTLS_SSL_CLI_C)
2031 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2032 {
2033#if defined(MBEDTLS_HAVE_TIME)
2034 if( end - p < 8 )
2035 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2036 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2037 p += 8;
2038#endif
2039 if( end - p < 4 )
2040 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2041 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2042 p += 4;
2043
2044 if( end - p < 2 )
2045 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2046 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2047 p += 2;
2048
2049 if( end - p < ( long int )session->ticket_len )
2050 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2051 if( session->ticket_len > 0 )
2052 {
2053 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2054 if( session->ticket == NULL )
2055 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2056 memcpy( session->ticket, p, session->ticket_len );
2057 p += session->ticket_len;
2058 }
2059 }
2060#endif /* MBEDTLS_SSL_CLI_C */
2061
2062 return( 0 );
2063
2064}
2065#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002066MBEDTLS_CHECK_RETURN_CRITICAL
2067static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2068 unsigned char *buf,
2069 size_t buf_len,
2070 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002071{
2072 ((void) session);
2073 ((void) buf);
2074 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002075 *olen = 0;
2076 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002077}
Jerry Yu438ddd82022-07-07 06:55:50 +00002078
2079static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2080 unsigned char *buf,
2081 size_t buf_len )
2082{
2083 ((void) session);
2084 ((void) buf);
2085 ((void) buf_len);
2086 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2087}
2088#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002089#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2090
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002091psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002092 size_t taglen,
2093 psa_algorithm_t *alg,
2094 psa_key_type_t *key_type,
2095 size_t *key_size )
2096{
2097 switch ( mbedtls_cipher_type )
2098 {
2099 case MBEDTLS_CIPHER_AES_128_CBC:
2100 *alg = PSA_ALG_CBC_NO_PADDING;
2101 *key_type = PSA_KEY_TYPE_AES;
2102 *key_size = 128;
2103 break;
2104 case MBEDTLS_CIPHER_AES_128_CCM:
2105 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2106 *key_type = PSA_KEY_TYPE_AES;
2107 *key_size = 128;
2108 break;
2109 case MBEDTLS_CIPHER_AES_128_GCM:
2110 *alg = PSA_ALG_GCM;
2111 *key_type = PSA_KEY_TYPE_AES;
2112 *key_size = 128;
2113 break;
2114 case MBEDTLS_CIPHER_AES_192_CCM:
2115 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2116 *key_type = PSA_KEY_TYPE_AES;
2117 *key_size = 192;
2118 break;
2119 case MBEDTLS_CIPHER_AES_192_GCM:
2120 *alg = PSA_ALG_GCM;
2121 *key_type = PSA_KEY_TYPE_AES;
2122 *key_size = 192;
2123 break;
2124 case MBEDTLS_CIPHER_AES_256_CBC:
2125 *alg = PSA_ALG_CBC_NO_PADDING;
2126 *key_type = PSA_KEY_TYPE_AES;
2127 *key_size = 256;
2128 break;
2129 case MBEDTLS_CIPHER_AES_256_CCM:
2130 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2131 *key_type = PSA_KEY_TYPE_AES;
2132 *key_size = 256;
2133 break;
2134 case MBEDTLS_CIPHER_AES_256_GCM:
2135 *alg = PSA_ALG_GCM;
2136 *key_type = PSA_KEY_TYPE_AES;
2137 *key_size = 256;
2138 break;
2139 case MBEDTLS_CIPHER_ARIA_128_CBC:
2140 *alg = PSA_ALG_CBC_NO_PADDING;
2141 *key_type = PSA_KEY_TYPE_ARIA;
2142 *key_size = 128;
2143 break;
2144 case MBEDTLS_CIPHER_ARIA_128_CCM:
2145 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2146 *key_type = PSA_KEY_TYPE_ARIA;
2147 *key_size = 128;
2148 break;
2149 case MBEDTLS_CIPHER_ARIA_128_GCM:
2150 *alg = PSA_ALG_GCM;
2151 *key_type = PSA_KEY_TYPE_ARIA;
2152 *key_size = 128;
2153 break;
2154 case MBEDTLS_CIPHER_ARIA_192_CCM:
2155 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2156 *key_type = PSA_KEY_TYPE_ARIA;
2157 *key_size = 192;
2158 break;
2159 case MBEDTLS_CIPHER_ARIA_192_GCM:
2160 *alg = PSA_ALG_GCM;
2161 *key_type = PSA_KEY_TYPE_ARIA;
2162 *key_size = 192;
2163 break;
2164 case MBEDTLS_CIPHER_ARIA_256_CBC:
2165 *alg = PSA_ALG_CBC_NO_PADDING;
2166 *key_type = PSA_KEY_TYPE_ARIA;
2167 *key_size = 256;
2168 break;
2169 case MBEDTLS_CIPHER_ARIA_256_CCM:
2170 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2171 *key_type = PSA_KEY_TYPE_ARIA;
2172 *key_size = 256;
2173 break;
2174 case MBEDTLS_CIPHER_ARIA_256_GCM:
2175 *alg = PSA_ALG_GCM;
2176 *key_type = PSA_KEY_TYPE_ARIA;
2177 *key_size = 256;
2178 break;
2179 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2180 *alg = PSA_ALG_CBC_NO_PADDING;
2181 *key_type = PSA_KEY_TYPE_CAMELLIA;
2182 *key_size = 128;
2183 break;
2184 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2185 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2186 *key_type = PSA_KEY_TYPE_CAMELLIA;
2187 *key_size = 128;
2188 break;
2189 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2190 *alg = PSA_ALG_GCM;
2191 *key_type = PSA_KEY_TYPE_CAMELLIA;
2192 *key_size = 128;
2193 break;
2194 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2195 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2196 *key_type = PSA_KEY_TYPE_CAMELLIA;
2197 *key_size = 192;
2198 break;
2199 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2200 *alg = PSA_ALG_GCM;
2201 *key_type = PSA_KEY_TYPE_CAMELLIA;
2202 *key_size = 192;
2203 break;
2204 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2205 *alg = PSA_ALG_CBC_NO_PADDING;
2206 *key_type = PSA_KEY_TYPE_CAMELLIA;
2207 *key_size = 256;
2208 break;
2209 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2210 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2211 *key_type = PSA_KEY_TYPE_CAMELLIA;
2212 *key_size = 256;
2213 break;
2214 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2215 *alg = PSA_ALG_GCM;
2216 *key_type = PSA_KEY_TYPE_CAMELLIA;
2217 *key_size = 256;
2218 break;
2219 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2220 *alg = PSA_ALG_CHACHA20_POLY1305;
2221 *key_type = PSA_KEY_TYPE_CHACHA20;
2222 *key_size = 256;
2223 break;
2224 case MBEDTLS_CIPHER_NULL:
2225 *alg = MBEDTLS_SSL_NULL_CIPHER;
2226 *key_type = 0;
2227 *key_size = 0;
2228 break;
2229 default:
2230 return PSA_ERROR_NOT_SUPPORTED;
2231 }
2232
2233 return PSA_SUCCESS;
2234}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002235#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002236
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002237#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002238int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2239 const unsigned char *dhm_P, size_t P_len,
2240 const unsigned char *dhm_G, size_t G_len )
2241{
Janos Follath865b3eb2019-12-16 11:46:15 +00002242 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002243
Glenn Strausscee11292021-12-20 01:43:17 -05002244 mbedtls_mpi_free( &conf->dhm_P );
2245 mbedtls_mpi_free( &conf->dhm_G );
2246
Hanno Beckera90658f2017-10-04 15:29:08 +01002247 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2248 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2249 {
2250 mbedtls_mpi_free( &conf->dhm_P );
2251 mbedtls_mpi_free( &conf->dhm_G );
2252 return( ret );
2253 }
2254
2255 return( 0 );
2256}
Paul Bakker5121ce52009-01-03 21:22:43 +00002257
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002258int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002259{
Janos Follath865b3eb2019-12-16 11:46:15 +00002260 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002261
Glenn Strausscee11292021-12-20 01:43:17 -05002262 mbedtls_mpi_free( &conf->dhm_P );
2263 mbedtls_mpi_free( &conf->dhm_G );
2264
Gilles Peskinee5702482021-06-11 21:59:08 +02002265 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2266 &conf->dhm_P ) ) != 0 ||
2267 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2268 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002269 {
2270 mbedtls_mpi_free( &conf->dhm_P );
2271 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002272 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002273 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002274
2275 return( 0 );
2276}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002277#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002278
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002279#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2280/*
2281 * Set the minimum length for Diffie-Hellman parameters
2282 */
2283void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2284 unsigned int bitlen )
2285{
2286 conf->dhm_min_bitlen = bitlen;
2287}
2288#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2289
Gilles Peskineeccd8882020-03-10 12:19:08 +01002290#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002291#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002292/*
2293 * Set allowed/preferred hashes for handshake signatures
2294 */
2295void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2296 const int *hashes )
2297{
2298 conf->sig_hashes = hashes;
2299}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002300#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002301
Jerry Yuf017ee42022-01-12 15:49:48 +08002302/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002303void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2304 const uint16_t* sig_algs )
2305{
Jerry Yuf017ee42022-01-12 15:49:48 +08002306#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2307 conf->sig_hashes = NULL;
2308#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2309 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002310}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002311#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002312
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002313#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002314#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002315/*
2316 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002317 *
2318 * mbedtls_ssl_setup() takes the provided list
2319 * and translates it to a list of IANA TLS group identifiers,
2320 * stored in ssl->handshake->group_list.
2321 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002322 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002323void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002324 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002325{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002326 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002327 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002328}
Brett Warrene0edc842021-08-17 09:53:13 +01002329#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002330#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002331
Brett Warrene0edc842021-08-17 09:53:13 +01002332/*
2333 * Set the allowed groups
2334 */
2335void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2336 const uint16_t *group_list )
2337{
2338#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2339 conf->curve_list = NULL;
2340#endif
2341 conf->group_list = group_list;
2342}
2343
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002344#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002346{
Hanno Becker947194e2017-04-07 13:25:49 +01002347 /* Initialize to suppress unnecessary compiler warning */
2348 size_t hostname_len = 0;
2349
2350 /* Check if new hostname is valid before
2351 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002352 if( hostname != NULL )
2353 {
2354 hostname_len = strlen( hostname );
2355
2356 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2357 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2358 }
2359
2360 /* Now it's clear that we will overwrite the old hostname,
2361 * so we can free it safely */
2362
2363 if( ssl->hostname != NULL )
2364 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002365 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002366 mbedtls_free( ssl->hostname );
2367 }
2368
2369 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002370
Paul Bakker5121ce52009-01-03 21:22:43 +00002371 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002372 {
2373 ssl->hostname = NULL;
2374 }
2375 else
2376 {
2377 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002378 if( ssl->hostname == NULL )
2379 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002380
Hanno Becker947194e2017-04-07 13:25:49 +01002381 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002382
Hanno Becker947194e2017-04-07 13:25:49 +01002383 ssl->hostname[hostname_len] = '\0';
2384 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002385
2386 return( 0 );
2387}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002388#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002389
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002390#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002391void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002393 const unsigned char *, size_t),
2394 void *p_sni )
2395{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002396 conf->f_sni = f_sni;
2397 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002398}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002402int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002403{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002404 size_t cur_len, tot_len;
2405 const char **p;
2406
2407 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002408 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2409 * MUST NOT be truncated."
2410 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002411 */
2412 tot_len = 0;
2413 for( p = protos; *p != NULL; p++ )
2414 {
2415 cur_len = strlen( *p );
2416 tot_len += cur_len;
2417
Ronald Cron8216dd32020-04-23 16:41:44 +02002418 if( ( cur_len == 0 ) ||
2419 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2420 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002422 }
2423
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002424 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002425
2426 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002427}
2428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002430{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002431 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002432}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002434
Johan Pascalb62bb512015-12-03 21:56:45 +01002435#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002436void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2437 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002438{
2439 conf->dtls_srtp_mki_support = support_mki_value;
2440}
2441
Ron Eldoref72faf2018-07-12 11:54:20 +03002442int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2443 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002444 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002445{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002446 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002447 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002448 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002449 }
Ron Eldor591f1622018-01-22 12:30:04 +02002450
2451 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002452 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002453 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002454 }
Ron Eldor591f1622018-01-22 12:30:04 +02002455
2456 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2457 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002458 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002459}
2460
Ron Eldoref72faf2018-07-12 11:54:20 +03002461int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002462 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002463{
Johan Pascal253d0262020-09-22 13:04:45 +02002464 const mbedtls_ssl_srtp_profile *p;
2465 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002466
Johan Pascal253d0262020-09-22 13:04:45 +02002467 /* check the profiles list: all entry must be valid,
2468 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002469 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2470 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2471 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002472 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002473 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002474 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002475 list_size++;
2476 }
2477 else
2478 {
2479 /* unsupported value, stop parsing and set the size to an error value */
2480 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002481 }
2482 }
2483
Johan Pascal5ef72d22020-10-28 17:05:47 +01002484 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002485 {
Johan Pascal253d0262020-09-22 13:04:45 +02002486 conf->dtls_srtp_profile_list = NULL;
2487 conf->dtls_srtp_profile_list_len = 0;
2488 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2489 }
2490
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002491 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002492 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002493
2494 return( 0 );
2495}
2496
Johan Pascal5ef72d22020-10-28 17:05:47 +01002497void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2498 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002499{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002500 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2501 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002502 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002503 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002504 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002505 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002506 else
2507 {
2508 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002509 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2510 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002511 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002512}
Johan Pascalb62bb512015-12-03 21:56:45 +01002513#endif /* MBEDTLS_SSL_DTLS_SRTP */
2514
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302515#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002516void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002517{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002518 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002519}
2520
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002521void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002522{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002523 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002524}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302525#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002526
Janos Follath088ce432017-04-10 12:42:31 +01002527#if defined(MBEDTLS_SSL_SRV_C)
2528void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2529 char cert_req_ca_list )
2530{
2531 conf->cert_req_ca_list = cert_req_ca_list;
2532}
2533#endif
2534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002536void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002537{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002538 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002539}
2540#endif
2541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002543void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002544{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002545 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002546}
2547#endif
2548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002550int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002551{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002552 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002553 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002556 }
2557
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002558 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002559
2560 return( 0 );
2561}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002563
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002564void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002565{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002566 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002567}
2568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002570void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002571{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002572 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002573}
2574
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002575void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002576{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002577 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002578}
2579
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002580void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002581 const unsigned char period[8] )
2582{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002583 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002584}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002588#if defined(MBEDTLS_SSL_CLI_C)
2589void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002590{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002591 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002592}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002593#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002594
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002595#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002596void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2597 mbedtls_ssl_ticket_write_t *f_ticket_write,
2598 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2599 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002600{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002601 conf->f_ticket_write = f_ticket_write;
2602 conf->f_ticket_parse = f_ticket_parse;
2603 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002604}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002605#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002607
Hanno Becker7e6c1782021-06-08 09:24:55 +01002608void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2609 mbedtls_ssl_export_keys_t *f_export_keys,
2610 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002611{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002612 ssl->f_export_keys = f_export_keys;
2613 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002614}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002615
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002616#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002617void mbedtls_ssl_conf_async_private_cb(
2618 mbedtls_ssl_config *conf,
2619 mbedtls_ssl_async_sign_t *f_async_sign,
2620 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2621 mbedtls_ssl_async_resume_t *f_async_resume,
2622 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002623 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002624{
2625 conf->f_async_sign_start = f_async_sign;
2626 conf->f_async_decrypt_start = f_async_decrypt;
2627 conf->f_async_resume = f_async_resume;
2628 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002629 conf->p_async_config_data = async_config_data;
2630}
2631
Gilles Peskine8f97af72018-04-26 11:46:10 +02002632void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2633{
2634 return( conf->p_async_config_data );
2635}
2636
Gilles Peskine1febfef2018-04-30 11:54:39 +02002637void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002638{
2639 if( ssl->handshake == NULL )
2640 return( NULL );
2641 else
2642 return( ssl->handshake->user_async_ctx );
2643}
2644
Gilles Peskine1febfef2018-04-30 11:54:39 +02002645void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002646 void *ctx )
2647{
2648 if( ssl->handshake != NULL )
2649 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002650}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002651#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002652
Paul Bakker5121ce52009-01-03 21:22:43 +00002653/*
2654 * SSL get accessors
2655 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002656uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002657{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002658 if( ssl->session != NULL )
2659 return( ssl->session->verify_result );
2660
2661 if( ssl->session_negotiate != NULL )
2662 return( ssl->session_negotiate->verify_result );
2663
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002664 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002665}
2666
Glenn Strauss8f526902022-01-13 00:04:49 -05002667int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2668{
2669 if( ssl == NULL || ssl->session == NULL )
2670 return( 0 );
2671
2672 return( ssl->session->ciphersuite );
2673}
2674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002676{
Paul Bakker926c8e42013-03-06 10:23:34 +01002677 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002678 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002681}
2682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002684{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002686 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002687 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002688 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002689 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002690 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002691 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002692 default:
2693 return( "unknown (DTLS)" );
2694 }
2695 }
2696#endif
2697
Glenn Strauss60bfe602022-03-14 19:04:24 -04002698 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002699 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002700 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002701 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002702 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002703 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002704 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002705 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002706 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002707}
2708
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002709#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002710size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2711{
David Horstmann95d516f2021-05-04 18:36:56 +01002712 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002713 size_t read_mfl;
2714
2715 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2716 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2717 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2718 {
2719 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2720 }
2721
2722 /* Check if a smaller max length was negotiated */
2723 if( ssl->session_out != NULL )
2724 {
2725 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2726 if( read_mfl < max_len )
2727 {
2728 max_len = read_mfl;
2729 }
2730 }
2731
2732 // During a handshake, use the value being negotiated
2733 if( ssl->session_negotiate != NULL )
2734 {
2735 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2736 if( read_mfl < max_len )
2737 {
2738 max_len = read_mfl;
2739 }
2740 }
2741
2742 return( max_len );
2743}
2744
2745size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002746{
2747 size_t max_len;
2748
2749 /*
2750 * Assume mfl_code is correct since it was checked when set
2751 */
Angus Grattond8213d02016-05-25 20:56:48 +10002752 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002753
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002754 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002755 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002756 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002757 {
Angus Grattond8213d02016-05-25 20:56:48 +10002758 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002759 }
2760
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002761 /* During a handshake, use the value being negotiated */
2762 if( ssl->session_negotiate != NULL &&
2763 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2764 {
2765 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2766 }
2767
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002768 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002769}
2770#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2771
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002772#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002773size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002774{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002775 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2776 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2777 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2778 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2779 return ( 0 );
2780
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002781 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2782 return( ssl->mtu );
2783
2784 if( ssl->mtu == 0 )
2785 return( ssl->handshake->mtu );
2786
2787 return( ssl->mtu < ssl->handshake->mtu ?
2788 ssl->mtu : ssl->handshake->mtu );
2789}
2790#endif /* MBEDTLS_SSL_PROTO_DTLS */
2791
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002792int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2793{
2794 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2795
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002796#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2797 !defined(MBEDTLS_SSL_PROTO_DTLS)
2798 (void) ssl;
2799#endif
2800
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002801#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002802 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002803
2804 if( max_len > mfl )
2805 max_len = mfl;
2806#endif
2807
2808#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002809 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002810 {
Hanno Becker89490712020-02-05 10:50:12 +00002811 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002812 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2813 const size_t overhead = (size_t) ret;
2814
2815 if( ret < 0 )
2816 return( ret );
2817
2818 if( mtu <= overhead )
2819 {
2820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2821 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2822 }
2823
2824 if( max_len > mtu - overhead )
2825 max_len = mtu - overhead;
2826 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002827#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002828
Hanno Becker0defedb2018-08-10 12:35:02 +01002829#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2830 !defined(MBEDTLS_SSL_PROTO_DTLS)
2831 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002832#endif
2833
2834 return( (int) max_len );
2835}
2836
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002837int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2838{
2839 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2840
2841#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2842 (void) ssl;
2843#endif
2844
2845#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2846 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2847
2848 if( max_len > mfl )
2849 max_len = mfl;
2850#endif
2851
2852 return( (int) max_len );
2853}
2854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855#if defined(MBEDTLS_X509_CRT_PARSE_C)
2856const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002857{
2858 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002859 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002860
Hanno Beckere6824572019-02-07 13:18:46 +00002861#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002862 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002863#else
2864 return( NULL );
2865#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002866}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002869#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002870int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2871 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002872{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002873 int ret;
2874
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002875 if( ssl == NULL ||
2876 dst == NULL ||
2877 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002878 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002881 }
2882
Hanno Beckere810bbc2021-05-14 16:01:05 +01002883 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2884 * idempotent: Each session can only be exported once.
2885 *
2886 * (This is in preparation for TLS 1.3 support where we will
2887 * need the ability to export multiple sessions (aka tickets),
2888 * which will be achieved by calling mbedtls_ssl_get_session()
2889 * multiple times until it fails.)
2890 *
2891 * Check whether we have already exported the current session,
2892 * and fail if so.
2893 */
2894 if( ssl->session->exported == 1 )
2895 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2896
2897 ret = mbedtls_ssl_session_copy( dst, ssl->session );
2898 if( ret != 0 )
2899 return( ret );
2900
2901 /* Remember that we've exported the session. */
2902 ssl->session->exported = 1;
2903 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002904}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002906
Paul Bakker5121ce52009-01-03 21:22:43 +00002907/*
Hanno Beckera835da52019-05-16 12:39:07 +01002908 * Define ticket header determining Mbed TLS version
2909 * and structure of the ticket.
2910 */
2911
Hanno Becker94ef3b32019-05-16 12:50:45 +01002912/*
Hanno Becker50b59662019-05-28 14:30:45 +01002913 * Define bitflag determining compile-time settings influencing
2914 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01002915 */
2916
Hanno Becker50b59662019-05-28 14:30:45 +01002917#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01002918#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01002919#else
Hanno Becker3e088662019-05-29 11:10:18 +01002920#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002921#endif /* MBEDTLS_HAVE_TIME */
2922
2923#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01002924#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002925#else
Hanno Becker3e088662019-05-29 11:10:18 +01002926#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002927#endif /* MBEDTLS_X509_CRT_PARSE_C */
2928
2929#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01002930#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002931#else
Hanno Becker3e088662019-05-29 11:10:18 +01002932#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002933#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
2934
2935#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01002936#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002937#else
Hanno Becker3e088662019-05-29 11:10:18 +01002938#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002939#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2940
Hanno Becker94ef3b32019-05-16 12:50:45 +01002941#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01002942#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002943#else
Hanno Becker3e088662019-05-29 11:10:18 +01002944#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002945#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2946
Hanno Becker94ef3b32019-05-16 12:50:45 +01002947#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2948#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
2949#else
2950#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
2951#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2952
Hanno Becker3e088662019-05-29 11:10:18 +01002953#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
2954#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
2955#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
2956#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01002957#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
2958#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01002959
Hanno Becker50b59662019-05-28 14:30:45 +01002960#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01002961 ( (uint16_t) ( \
2962 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
2963 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
2964 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
2965 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01002966 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01002967 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01002968
Hanno Beckerf8787072019-05-16 12:41:07 +01002969static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01002970 MBEDTLS_VERSION_MAJOR,
2971 MBEDTLS_VERSION_MINOR,
2972 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01002973 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
2974 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01002975};
Hanno Beckera835da52019-05-16 12:39:07 +01002976
2977/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002978 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002979 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002980 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002981 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002982 *
Hanno Beckerdce50972021-08-01 05:39:23 +01002983 * opaque mbedtls_version[3]; // library version: major, minor, patch
2984 * opaque session_format[2]; // library-version specific 16-bit field
2985 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002986 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002987 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002988 * Note: When updating the format, remember to keep
2989 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002990 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002991 * // In this version, `session_format` determines
2992 * // the setting of those compile-time
2993 * // configuration options which influence
2994 * // the structure of mbedtls_ssl_session.
2995 *
Glenn Straussda7851c2022-03-14 13:29:48 -04002996 * uint8_t minor_ver; // Protocol minor version. Possible values:
2997 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002998 *
Glenn Straussda7851c2022-03-14 13:29:48 -04002999 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003000 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003001 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003002 * serialized_session_tls12 data;
3003 *
3004 * };
3005 *
3006 * } serialized_session;
3007 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003008 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003009
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003010MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003011static int ssl_session_save( const mbedtls_ssl_session *session,
3012 unsigned char omit_header,
3013 unsigned char *buf,
3014 size_t buf_len,
3015 size_t *olen )
3016{
3017 unsigned char *p = buf;
3018 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003019 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003020#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3021 size_t out_len;
3022 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3023#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003024 if( session == NULL )
3025 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3026
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003027 if( !omit_header )
3028 {
3029 /*
3030 * Add Mbed TLS version identifier
3031 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003032 used += sizeof( ssl_serialized_session_header );
3033
3034 if( used <= buf_len )
3035 {
3036 memcpy( p, ssl_serialized_session_header,
3037 sizeof( ssl_serialized_session_header ) );
3038 p += sizeof( ssl_serialized_session_header );
3039 }
3040 }
3041
3042 /*
3043 * TLS version identifier
3044 */
3045 used += 1;
3046 if( used <= buf_len )
3047 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003048 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003049 }
3050
3051 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003052 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003053 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003054 {
3055#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003056 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003057 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003058 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003059#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3060
Jerry Yu251a12e2022-07-13 15:15:48 +08003061#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3062 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003063 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3064 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3065 {
3066 return( ret );
3067 }
3068 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003069 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003070#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3071
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003072 default:
3073 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3074 }
3075
3076 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003077 if( used > buf_len )
3078 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003079
3080 return( 0 );
3081}
3082
3083/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003084 * Public wrapper for ssl_session_save()
3085 */
3086int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3087 unsigned char *buf,
3088 size_t buf_len,
3089 size_t *olen )
3090{
3091 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3092}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003093
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003094/*
3095 * Deserialize session, see mbedtls_ssl_session_save() for format.
3096 *
3097 * This internal version is wrapped by a public function that cleans up in
3098 * case of error, and has an extra option omit_header.
3099 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003100MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003101static int ssl_session_load( mbedtls_ssl_session *session,
3102 unsigned char omit_header,
3103 const unsigned char *buf,
3104 size_t len )
3105{
3106 const unsigned char *p = buf;
3107 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003108 size_t remaining_len;
3109
3110
3111 if( session == NULL )
3112 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003113
3114 if( !omit_header )
3115 {
3116 /*
3117 * Check Mbed TLS version identifier
3118 */
3119
3120 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3121 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3122
3123 if( memcmp( p, ssl_serialized_session_header,
3124 sizeof( ssl_serialized_session_header ) ) != 0 )
3125 {
3126 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3127 }
3128 p += sizeof( ssl_serialized_session_header );
3129 }
3130
3131 /*
3132 * TLS version identifier
3133 */
3134 if( 1 > (size_t)( end - p ) )
3135 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003136 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003137
3138 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003139 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003140 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003141 {
3142#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003143 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003144 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003145#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3146
Jerry Yu438ddd82022-07-07 06:55:50 +00003147#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3148 case MBEDTLS_SSL_VERSION_TLS1_3:
3149 return( ssl_tls13_session_load( session, p, remaining_len ) );
3150#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3151
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003152 default:
3153 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3154 }
3155}
3156
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003157/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003158 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003159 */
3160int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3161 const unsigned char *buf,
3162 size_t len )
3163{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003164 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003165
3166 if( ret != 0 )
3167 mbedtls_ssl_session_free( session );
3168
3169 return( ret );
3170}
3171
3172/*
Paul Bakker1961b702013-01-25 14:49:24 +01003173 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003174 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003175MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003176static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3177{
3178 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3179
Ronald Cron66dbf912022-02-02 15:33:46 +01003180 /*
3181 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003182 * that were written into the output buffer by the previous handshake step,
3183 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003184 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3185 * We proceed to the next handshake step only when all data from the
3186 * previous one have been sent to the peer, thus we make sure that this is
3187 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3188 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3189 * we have to wait before to go ahead.
3190 * In the case of TLS 1.3, handshake step handlers do not send data to the
3191 * peer. Data are only sent here and through
3192 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003193 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003194 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003195 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3196 return( ret );
3197
3198#if defined(MBEDTLS_SSL_PROTO_DTLS)
3199 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3200 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3201 {
3202 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3203 return( ret );
3204 }
3205#endif /* MBEDTLS_SSL_PROTO_DTLS */
3206
3207 return( ret );
3208}
3209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003211{
Hanno Becker41934dd2021-08-07 19:13:43 +01003212 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003213
Hanno Becker41934dd2021-08-07 19:13:43 +01003214 if( ssl == NULL ||
3215 ssl->conf == NULL ||
3216 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003217 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003218 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003219 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003220 }
3221
3222 ret = ssl_prepare_handshake_step( ssl );
3223 if( ret != 0 )
3224 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003225
Jerry Yue7047812021-09-13 19:26:39 +08003226 ret = mbedtls_ssl_handle_pending_alert( ssl );
3227 if( ret != 0 )
3228 goto cleanup;
3229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003231 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003232 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003233 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3234 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003235
Ronald Cron9f0fba32022-02-10 16:45:15 +01003236 switch( ssl->state )
3237 {
3238 case MBEDTLS_SSL_HELLO_REQUEST:
3239 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3240 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003241
Ronald Cron9f0fba32022-02-10 16:45:15 +01003242 case MBEDTLS_SSL_CLIENT_HELLO:
3243 ret = mbedtls_ssl_write_client_hello( ssl );
3244 break;
3245
3246 default:
3247#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003248 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003249 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3250 else
3251 ret = mbedtls_ssl_handshake_client_step( ssl );
3252#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3253 ret = mbedtls_ssl_handshake_client_step( ssl );
3254#else
3255 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3256#endif
3257 }
Jerry Yub9930e72021-08-06 17:11:51 +08003258 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003259#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003261 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003262 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003263#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003264 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003265 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003266#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003267
3268#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3269 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3270 ret = mbedtls_ssl_handshake_server_step( ssl );
3271#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3272 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003273#endif
3274
Jerry Yue7047812021-09-13 19:26:39 +08003275 if( ret != 0 )
3276 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003277 /* handshake_step return error. And it is same
3278 * with alert_reason.
3279 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003280 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003281 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003282 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003283 goto cleanup;
3284 }
3285 }
3286
3287cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003288 return( ret );
3289}
3290
3291/*
3292 * Perform the SSL handshake
3293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003295{
3296 int ret = 0;
3297
Hanno Beckera817ea42020-10-20 15:20:23 +01003298 /* Sanity checks */
3299
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003300 if( ssl == NULL || ssl->conf == NULL )
3301 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3302
Hanno Beckera817ea42020-10-20 15:20:23 +01003303#if defined(MBEDTLS_SSL_PROTO_DTLS)
3304 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3305 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3306 {
3307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3308 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3309 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3310 }
3311#endif /* MBEDTLS_SSL_PROTO_DTLS */
3312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003314
Hanno Beckera817ea42020-10-20 15:20:23 +01003315 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003316 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003318 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003319
3320 if( ret != 0 )
3321 break;
3322 }
3323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003324 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003325
3326 return( ret );
3327}
3328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329#if defined(MBEDTLS_SSL_RENEGOTIATION)
3330#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003331/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003332 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003333 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003334MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003336{
Janos Follath865b3eb2019-12-16 11:46:15 +00003337 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003340
3341 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3343 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003344
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003345 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003346 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003347 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003348 return( ret );
3349 }
3350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003352
3353 return( 0 );
3354}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003355#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003356
3357/*
3358 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359 * - any side: calling mbedtls_ssl_renegotiate(),
3360 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3361 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003362 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003363 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003365 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003366int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003367{
Janos Follath865b3eb2019-12-16 11:46:15 +00003368 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003371
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003372 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3373 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003374
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003375 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3376 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003378 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003379 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003380 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003381 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003382 ssl->handshake->out_msg_seq = 1;
3383 else
3384 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003385 }
3386#endif
3387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3389 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003394 return( ret );
3395 }
3396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003398
3399 return( 0 );
3400}
3401
3402/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003403 * Renegotiate current connection on client,
3404 * or request renegotiation on server
3405 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003407{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003408 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003409
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003410 if( ssl == NULL || ssl->conf == NULL )
3411 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003414 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003415 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003416 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003417 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003418 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003420 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003421
3422 /* Did we already try/start sending HelloRequest? */
3423 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003425
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003426 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003427 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003431 /*
3432 * On client, either start the renegotiation process or,
3433 * if already in progress, continue the handshake
3434 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003436 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003437 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003439
Hanno Becker40cdaa12020-02-05 10:48:27 +00003440 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003441 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003443 return( ret );
3444 }
3445 }
3446 else
3447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003448 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003450 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003451 return( ret );
3452 }
3453 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003454#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003455
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003456 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003457}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003458#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003459
Gilles Peskine9b562d52018-04-25 20:32:43 +02003460void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003461{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003462 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3463
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003464 if( handshake == NULL )
3465 return;
3466
Brett Warrene0edc842021-08-17 09:53:13 +01003467#if defined(MBEDTLS_ECP_C)
3468#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3469 if ( ssl->handshake->group_list_heap_allocated )
3470 mbedtls_free( (void*) handshake->group_list );
3471 handshake->group_list = NULL;
3472#endif /* MBEDTLS_DEPRECATED_REMOVED */
3473#endif /* MBEDTLS_ECP_C */
3474
Jerry Yuf017ee42022-01-12 15:49:48 +08003475#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3476#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3477 if ( ssl->handshake->sig_algs_heap_allocated )
3478 mbedtls_free( (void*) handshake->sig_algs );
3479 handshake->sig_algs = NULL;
3480#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003481#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3482 if( ssl->handshake->certificate_request_context )
3483 {
3484 mbedtls_free( (void*) handshake->certificate_request_context );
3485 }
3486#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003487#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3488
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003489#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3490 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3491 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003492 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003493 handshake->async_in_progress = 0;
3494 }
3495#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3496
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003497#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003498#if defined(MBEDTLS_USE_PSA_CRYPTO)
3499 psa_hash_abort( &handshake->fin_sha256_psa );
3500#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003501 mbedtls_sha256_free( &handshake->fin_sha256 );
3502#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003503#endif
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +02003504#if defined(MBEDTLS_SHA384_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003505#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003506 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003507#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003508 mbedtls_sha512_free( &handshake->fin_sha512 );
3509#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003510#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003512#if defined(MBEDTLS_DHM_C)
3513 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003514#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003515#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003517#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003518#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003519 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003520#if defined(MBEDTLS_SSL_CLI_C)
3521 mbedtls_free( handshake->ecjpake_cache );
3522 handshake->ecjpake_cache = NULL;
3523 handshake->ecjpake_cache_len = 0;
3524#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003525#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003526
Janos Follath4ae5c292016-02-10 11:27:43 +00003527#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3528 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003529 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003530 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003531#endif
3532
Gilles Peskineeccd8882020-03-10 12:19:08 +01003533#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003534#if defined(MBEDTLS_USE_PSA_CRYPTO)
3535 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3536 {
3537 /* The maintenance of the external PSK key slot is the
3538 * user's responsibility. */
3539 if( ssl->handshake->psk_opaque_is_internal )
3540 {
3541 psa_destroy_key( ssl->handshake->psk_opaque );
3542 ssl->handshake->psk_opaque_is_internal = 0;
3543 }
3544 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3545 }
Neil Armstronge952a302022-05-03 10:22:14 +02003546#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003547 if( handshake->psk != NULL )
3548 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003549 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003550 mbedtls_free( handshake->psk );
3551 }
Neil Armstronge952a302022-05-03 10:22:14 +02003552#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003553#endif
3554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3556 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003557 /*
3558 * Free only the linked list wrapper, not the keys themselves
3559 * since the belong to the SNI callback
3560 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003561 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003562#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003563
Gilles Peskineeccd8882020-03-10 12:19:08 +01003564#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003565 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003566 if( handshake->ecrs_peer_cert != NULL )
3567 {
3568 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3569 mbedtls_free( handshake->ecrs_peer_cert );
3570 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003571#endif
3572
Hanno Becker75173122019-02-06 16:18:31 +00003573#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3574 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3575 mbedtls_pk_free( &handshake->peer_pubkey );
3576#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3577
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003578#if defined(MBEDTLS_SSL_CLI_C) && \
3579 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3580 mbedtls_free( handshake->cookie );
3581#endif /* MBEDTLS_SSL_CLI_C &&
3582 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003583
3584#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003585 mbedtls_ssl_flight_free( handshake->flight );
3586 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003587#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003588
Ronald Cronf12b81d2022-03-15 10:42:41 +01003589#if defined(MBEDTLS_ECDH_C) && \
3590 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003591 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003592 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003593#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3594
Ronald Cron6f135e12021-12-08 16:57:54 +01003595#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003596 mbedtls_ssl_transform_free( handshake->transform_handshake );
3597 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003598 mbedtls_free( handshake->transform_earlydata );
3599 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003600#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003601
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003602
3603#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3604 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3605 * processes datagrams and the fact that a datagram is allowed to have
3606 * several records in it, it is possible that the I/O buffers are not
3607 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003608 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3609 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003610#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003611
Jerry Yuba9c7272021-10-30 11:54:10 +08003612 /* mbedtls_platform_zeroize MUST be last one in this function */
3613 mbedtls_platform_zeroize( handshake,
3614 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003615}
3616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003617void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003618{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003619 if( session == NULL )
3620 return;
3621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003623 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003624#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003625
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003626#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003628#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003629
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003630 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003631}
3632
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003633#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003634
3635#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3636#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3637#else
3638#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3639#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3640
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003641#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003642
3643#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3644#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3645#else
3646#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3647#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3648
3649#if defined(MBEDTLS_SSL_ALPN)
3650#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3651#else
3652#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3653#endif /* MBEDTLS_SSL_ALPN */
3654
3655#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3656#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3657#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3658#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3659
3660#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3661 ( (uint32_t) ( \
3662 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3663 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3664 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3665 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3666 0u ) )
3667
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003668static unsigned char ssl_serialized_context_header[] = {
3669 MBEDTLS_VERSION_MAJOR,
3670 MBEDTLS_VERSION_MINOR,
3671 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003672 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3673 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3674 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3675 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3676 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003677};
3678
Paul Bakker5121ce52009-01-03 21:22:43 +00003679/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003680 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003681 *
3682 * The format of the serialized data is:
3683 * (in the presentation language of TLS, RFC 8446 section 3)
3684 *
3685 * // header
3686 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003687 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003688 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003689 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003690 * Note: When updating the format, remember to keep these
3691 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003692 *
3693 * // session sub-structure
3694 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3695 * // transform sub-structure
3696 * uint8 random[64]; // ServerHello.random+ClientHello.random
3697 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3698 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3699 * // fields from ssl_context
3700 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3701 * uint64 in_window_top; // DTLS: last validated record seq_num
3702 * uint64 in_window; // DTLS: bitmask for replay protection
3703 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3704 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3705 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3706 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3707 *
3708 * Note that many fields of the ssl_context or sub-structures are not
3709 * serialized, as they fall in one of the following categories:
3710 *
3711 * 1. forced value (eg in_left must be 0)
3712 * 2. pointer to dynamically-allocated memory (eg session, transform)
3713 * 3. value can be re-derived from other data (eg session keys from MS)
3714 * 4. value was temporary (eg content of input buffer)
3715 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003716 */
3717int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3718 unsigned char *buf,
3719 size_t buf_len,
3720 size_t *olen )
3721{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003722 unsigned char *p = buf;
3723 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003724 size_t session_len;
3725 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003726
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003727 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003728 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3729 * this function's documentation.
3730 *
3731 * These are due to assumptions/limitations in the implementation. Some of
3732 * them are likely to stay (no handshake in progress) some might go away
3733 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003734 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003735 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003736 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003737 {
3738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003739 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003740 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003741 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003742 {
3743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003744 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003745 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003746 /* Double-check that sub-structures are indeed ready */
3747 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003748 {
3749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003750 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003751 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003752 /* There must be no pending incoming or outgoing data */
3753 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003754 {
3755 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003756 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003757 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003758 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003759 {
3760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003761 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003762 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003763 /* Protocol must be DLTS, not TLS */
3764 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003765 {
3766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003767 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003768 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003769 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003770 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003771 {
3772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003773 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003774 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003775 /* We must be using an AEAD ciphersuite */
3776 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003777 {
3778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003779 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003780 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003781 /* Renegotiation must not be enabled */
3782#if defined(MBEDTLS_SSL_RENEGOTIATION)
3783 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003784 {
3785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003786 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003787 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003788#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003789
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003790 /*
3791 * Version and format identifier
3792 */
3793 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003794
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003795 if( used <= buf_len )
3796 {
3797 memcpy( p, ssl_serialized_context_header,
3798 sizeof( ssl_serialized_context_header ) );
3799 p += sizeof( ssl_serialized_context_header );
3800 }
3801
3802 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003803 * Session (length + data)
3804 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003805 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003806 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3807 return( ret );
3808
3809 used += 4 + session_len;
3810 if( used <= buf_len )
3811 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003812 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3813 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003814
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003815 ret = ssl_session_save( ssl->session, 1,
3816 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003817 if( ret != 0 )
3818 return( ret );
3819
3820 p += session_len;
3821 }
3822
3823 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003824 * Transform
3825 */
3826 used += sizeof( ssl->transform->randbytes );
3827 if( used <= buf_len )
3828 {
3829 memcpy( p, ssl->transform->randbytes,
3830 sizeof( ssl->transform->randbytes ) );
3831 p += sizeof( ssl->transform->randbytes );
3832 }
3833
3834#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3835 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3836 if( used <= buf_len )
3837 {
3838 *p++ = ssl->transform->in_cid_len;
3839 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3840 p += ssl->transform->in_cid_len;
3841
3842 *p++ = ssl->transform->out_cid_len;
3843 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3844 p += ssl->transform->out_cid_len;
3845 }
3846#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3847
3848 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003849 * Saved fields from top-level ssl_context structure
3850 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003851 used += 4;
3852 if( used <= buf_len )
3853 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003854 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3855 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003856 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003857
3858#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3859 used += 16;
3860 if( used <= buf_len )
3861 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003862 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3863 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003864
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003865 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3866 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003867 }
3868#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3869
3870#if defined(MBEDTLS_SSL_PROTO_DTLS)
3871 used += 1;
3872 if( used <= buf_len )
3873 {
3874 *p++ = ssl->disable_datagram_packing;
3875 }
3876#endif /* MBEDTLS_SSL_PROTO_DTLS */
3877
Jerry Yuae0b2e22021-10-08 15:21:19 +08003878 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003879 if( used <= buf_len )
3880 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003881 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3882 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003883 }
3884
3885#if defined(MBEDTLS_SSL_PROTO_DTLS)
3886 used += 2;
3887 if( used <= buf_len )
3888 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003889 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
3890 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003891 }
3892#endif /* MBEDTLS_SSL_PROTO_DTLS */
3893
3894#if defined(MBEDTLS_SSL_ALPN)
3895 {
3896 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02003897 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003898 : 0;
3899
3900 used += 1 + alpn_len;
3901 if( used <= buf_len )
3902 {
3903 *p++ = alpn_len;
3904
3905 if( ssl->alpn_chosen != NULL )
3906 {
3907 memcpy( p, ssl->alpn_chosen, alpn_len );
3908 p += alpn_len;
3909 }
3910 }
3911 }
3912#endif /* MBEDTLS_SSL_ALPN */
3913
3914 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003915 * Done
3916 */
3917 *olen = used;
3918
3919 if( used > buf_len )
3920 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003921
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003922 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
3923
Hanno Becker43aefe22020-02-05 10:44:56 +00003924 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003925}
3926
3927/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003928 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003929 *
3930 * This internal version is wrapped by a public function that cleans up in
3931 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003932 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003933MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003934static int ssl_context_load( mbedtls_ssl_context *ssl,
3935 const unsigned char *buf,
3936 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003937{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003938 const unsigned char *p = buf;
3939 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003940 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00003941 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003942
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003943 /*
3944 * The context should have been freshly setup or reset.
3945 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02003946 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003947 * renegotiating, or if the user mistakenly loaded a session first.)
3948 */
3949 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
3950 ssl->session != NULL )
3951 {
3952 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3953 }
3954
3955 /*
3956 * We can't check that the config matches the initial one, but we can at
3957 * least check it matches the requirements for serializing.
3958 */
3959 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003960 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
3961 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003962#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003963 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003964#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003965 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003966 {
3967 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3968 }
3969
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003970 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
3971
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003972 /*
3973 * Check version identifier
3974 */
3975 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
3976 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3977
3978 if( memcmp( p, ssl_serialized_context_header,
3979 sizeof( ssl_serialized_context_header ) ) != 0 )
3980 {
3981 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3982 }
3983 p += sizeof( ssl_serialized_context_header );
3984
3985 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003986 * Session
3987 */
3988 if( (size_t)( end - p ) < 4 )
3989 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3990
3991 session_len = ( (size_t) p[0] << 24 ) |
3992 ( (size_t) p[1] << 16 ) |
3993 ( (size_t) p[2] << 8 ) |
3994 ( (size_t) p[3] );
3995 p += 4;
3996
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003997 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00003998 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003999 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004000 ssl->session_in = ssl->session;
4001 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004002 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004003
4004 if( (size_t)( end - p ) < session_len )
4005 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4006
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004007 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004008 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004009 {
4010 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004011 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004012 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004013
4014 p += session_len;
4015
4016 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004017 * Transform
4018 */
4019
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004020 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004021 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004022 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004023 ssl->transform_in = ssl->transform;
4024 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004025 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004026
4027 /* Read random bytes and populate structure */
4028 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4029 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08004030#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00004031 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004032 ssl->session->ciphersuite,
4033 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004034#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004035 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004036#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004037 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
4038 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004039 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004040 ssl->conf->endpoint,
4041 ssl );
4042 if( ret != 0 )
4043 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004044#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004045 p += sizeof( ssl->transform->randbytes );
4046
4047#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4048 /* Read connection IDs and store them */
4049 if( (size_t)( end - p ) < 1 )
4050 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4051
4052 ssl->transform->in_cid_len = *p++;
4053
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004054 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004055 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4056
4057 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4058 p += ssl->transform->in_cid_len;
4059
4060 ssl->transform->out_cid_len = *p++;
4061
4062 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4063 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4064
4065 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4066 p += ssl->transform->out_cid_len;
4067#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4068
4069 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004070 * Saved fields from top-level ssl_context structure
4071 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004072 if( (size_t)( end - p ) < 4 )
4073 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4074
4075 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4076 ( (uint32_t) p[1] << 16 ) |
4077 ( (uint32_t) p[2] << 8 ) |
4078 ( (uint32_t) p[3] );
4079 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004080
4081#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4082 if( (size_t)( end - p ) < 16 )
4083 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4084
4085 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4086 ( (uint64_t) p[1] << 48 ) |
4087 ( (uint64_t) p[2] << 40 ) |
4088 ( (uint64_t) p[3] << 32 ) |
4089 ( (uint64_t) p[4] << 24 ) |
4090 ( (uint64_t) p[5] << 16 ) |
4091 ( (uint64_t) p[6] << 8 ) |
4092 ( (uint64_t) p[7] );
4093 p += 8;
4094
4095 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4096 ( (uint64_t) p[1] << 48 ) |
4097 ( (uint64_t) p[2] << 40 ) |
4098 ( (uint64_t) p[3] << 32 ) |
4099 ( (uint64_t) p[4] << 24 ) |
4100 ( (uint64_t) p[5] << 16 ) |
4101 ( (uint64_t) p[6] << 8 ) |
4102 ( (uint64_t) p[7] );
4103 p += 8;
4104#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4105
4106#if defined(MBEDTLS_SSL_PROTO_DTLS)
4107 if( (size_t)( end - p ) < 1 )
4108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4109
4110 ssl->disable_datagram_packing = *p++;
4111#endif /* MBEDTLS_SSL_PROTO_DTLS */
4112
Jerry Yud9a94fe2021-09-28 18:58:59 +08004113 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004114 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004115 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4116 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004117
4118#if defined(MBEDTLS_SSL_PROTO_DTLS)
4119 if( (size_t)( end - p ) < 2 )
4120 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4121
4122 ssl->mtu = ( p[0] << 8 ) | p[1];
4123 p += 2;
4124#endif /* MBEDTLS_SSL_PROTO_DTLS */
4125
4126#if defined(MBEDTLS_SSL_ALPN)
4127 {
4128 uint8_t alpn_len;
4129 const char **cur;
4130
4131 if( (size_t)( end - p ) < 1 )
4132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4133
4134 alpn_len = *p++;
4135
4136 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4137 {
4138 /* alpn_chosen should point to an item in the configured list */
4139 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4140 {
4141 if( strlen( *cur ) == alpn_len &&
4142 memcmp( p, cur, alpn_len ) == 0 )
4143 {
4144 ssl->alpn_chosen = *cur;
4145 break;
4146 }
4147 }
4148 }
4149
4150 /* can only happen on conf mismatch */
4151 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4152 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4153
4154 p += alpn_len;
4155 }
4156#endif /* MBEDTLS_SSL_ALPN */
4157
4158 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004159 * Forced fields from top-level ssl_context structure
4160 *
4161 * Most of them already set to the correct value by mbedtls_ssl_init() and
4162 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4163 */
4164 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004165 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004166
Hanno Becker361b10d2019-08-30 10:42:49 +01004167 /* Adjust pointers for header fields of outgoing records to
4168 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004169 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004170
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004171#if defined(MBEDTLS_SSL_PROTO_DTLS)
4172 ssl->in_epoch = 1;
4173#endif
4174
4175 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4176 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004177 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4178 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004179 if( ssl->handshake != NULL )
4180 {
4181 mbedtls_ssl_handshake_free( ssl );
4182 mbedtls_free( ssl->handshake );
4183 ssl->handshake = NULL;
4184 }
4185
4186 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004187 * Done - should have consumed entire buffer
4188 */
4189 if( p != end )
4190 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004191
4192 return( 0 );
4193}
4194
4195/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004196 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004197 */
4198int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4199 const unsigned char *buf,
4200 size_t len )
4201{
4202 int ret = ssl_context_load( context, buf, len );
4203
4204 if( ret != 0 )
4205 mbedtls_ssl_free( context );
4206
4207 return( ret );
4208}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004209#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004210
4211/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004212 * Free an SSL context
4213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004214void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004215{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004216 if( ssl == NULL )
4217 return;
4218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004220
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004221 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004222 {
sander-visserb8aa2072020-05-06 22:05:13 +02004223#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4224 size_t out_buf_len = ssl->out_buf_len;
4225#else
4226 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4227#endif
4228
Darryl Greenb33cc762019-11-28 14:29:44 +00004229 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004230 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004231 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004232 }
4233
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004234 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004235 {
sander-visserb8aa2072020-05-06 22:05:13 +02004236#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4237 size_t in_buf_len = ssl->in_buf_len;
4238#else
4239 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4240#endif
4241
Darryl Greenb33cc762019-11-28 14:29:44 +00004242 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004243 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004244 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004245 }
4246
Paul Bakker48916f92012-09-16 19:57:18 +00004247 if( ssl->transform )
4248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004249 mbedtls_ssl_transform_free( ssl->transform );
4250 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004251 }
4252
4253 if( ssl->handshake )
4254 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004255 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004256 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4257 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004259 mbedtls_free( ssl->handshake );
4260 mbedtls_free( ssl->transform_negotiate );
4261 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004262 }
4263
Ronald Cron6f135e12021-12-08 16:57:54 +01004264#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004265 mbedtls_ssl_transform_free( ssl->transform_application );
4266 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004267#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004268
Paul Bakkerc0463502013-02-14 11:19:38 +01004269 if( ssl->session )
4270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004271 mbedtls_ssl_session_free( ssl->session );
4272 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004273 }
4274
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004275#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004276 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004277 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004278 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004279 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004280 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004281#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004282
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004283#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004285#endif
4286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004287 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004288
Paul Bakker86f04f42013-02-14 11:20:09 +01004289 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004290 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004291}
4292
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004293/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004294 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004295 */
4296void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4297{
4298 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4299}
4300
Gilles Peskineae270bf2021-06-02 00:05:29 +02004301/* The selection should be the same as mbedtls_x509_crt_profile_default in
4302 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004303 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004304 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004305 * about this list.
4306 */
Brett Warrene0edc842021-08-17 09:53:13 +01004307static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004308#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004309 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004310#endif
4311#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004312 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004313#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004314#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004315 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004316#endif
4317#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004318 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004319#endif
4320#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004321 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004322#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004323#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004324 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004325#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004326#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004327 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004328#endif
4329#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004330 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004331#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004332 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004333};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004334
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004335static int ssl_preset_suiteb_ciphersuites[] = {
4336 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4337 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4338 0
4339};
4340
Gilles Peskineeccd8882020-03-10 12:19:08 +01004341#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004342
Jerry Yu909df7b2022-01-22 11:56:27 +08004343/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004344 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004345 * rules SHOULD be upheld.
4346 * - No duplicate entries.
4347 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004348 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004349 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004350 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004351static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004352
Jerry Yued5e9f42022-01-26 11:21:34 +08004353#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \
4354 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4355 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
4356#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C &&
4357 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004358
Jerry Yu909df7b2022-01-22 11:56:27 +08004359#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \
4360 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4361 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
4362#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
4363 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4364
Jerry Yued5e9f42022-01-26 11:21:34 +08004365#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA512_C) && \
4366 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4367 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
4368#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
4369 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004370
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004371#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA512_C)
4372 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4373#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA512_C */
4374
4375#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA384_C)
4376 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4377#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA384_C */
4378
4379#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C)
4380 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4381#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */
4382
Jerry Yu693a47a2022-06-23 14:02:28 +08004383#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA512_C)
4384 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4385#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4386
4387#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA384_C)
4388 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4389#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4390
4391#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C)
4392 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4393#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4394
Gabor Mezei15b95a62022-05-09 16:37:58 +02004395 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004396};
4397
4398/* NOTICE: see above */
4399#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4400static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Jerry Yu713013f2022-01-17 18:16:35 +08004401#if defined(MBEDTLS_SHA512_C)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004402#if defined(MBEDTLS_ECDSA_C)
4403 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004404#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004405#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4406 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4407#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004408#if defined(MBEDTLS_RSA_C)
4409 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4410#endif
4411#endif /* MBEDTLS_SHA512_C */
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004412#if defined(MBEDTLS_SHA384_C)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004413#if defined(MBEDTLS_ECDSA_C)
4414 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004415#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004416#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4417 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4418#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004419#if defined(MBEDTLS_RSA_C)
4420 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4421#endif
4422#endif /* MBEDTLS_SHA384_C */
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004423#if defined(MBEDTLS_SHA256_C)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004424#if defined(MBEDTLS_ECDSA_C)
4425 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004426#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004427#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4428 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4429#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004430#if defined(MBEDTLS_RSA_C)
4431 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4432#endif
4433#endif /* MBEDTLS_SHA256_C */
Gabor Mezei15b95a62022-05-09 16:37:58 +02004434 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004435};
4436#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4437/* NOTICE: see above */
4438static uint16_t ssl_preset_suiteb_sig_algs[] = {
4439
Jerry Yu909df7b2022-01-22 11:56:27 +08004440#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \
4441 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4442 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
4443#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C &&
4444 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4445
Jerry Yu53037892022-01-25 11:02:06 +08004446#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \
4447 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4448 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
4449#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
4450 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004451
4452#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C)
4453 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4454#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004455
Jerry Yu53037892022-01-25 11:02:06 +08004456#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C)
4457 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4458#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4459
Gabor Mezei15b95a62022-05-09 16:37:58 +02004460 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004461};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004462
Jerry Yu909df7b2022-01-22 11:56:27 +08004463/* NOTICE: see above */
4464#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4465static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Jerry Yu713013f2022-01-17 18:16:35 +08004466#if defined(MBEDTLS_SHA256_C)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004467#if defined(MBEDTLS_ECDSA_C)
4468 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004469#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004470#if defined(MBEDTLS_RSA_C)
4471 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4472#endif
4473#endif /* MBEDTLS_SHA256_C */
Jerry Yu18c833e2022-01-25 10:55:47 +08004474#if defined(MBEDTLS_SHA384_C)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004475#if defined(MBEDTLS_ECDSA_C)
4476 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004477#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004478#if defined(MBEDTLS_RSA_C)
4479 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4480#endif
4481#endif /* MBEDTLS_SHA256_C */
Gabor Mezei15b95a62022-05-09 16:37:58 +02004482 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004483};
Jerry Yu909df7b2022-01-22 11:56:27 +08004484#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4485
Jerry Yu1a8b4812022-01-20 17:56:50 +08004486#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004487
Brett Warrene0edc842021-08-17 09:53:13 +01004488static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004489#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004490 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004491#endif
4492#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004493 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004494#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004495 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004496};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004497
Jerry Yu1a8b4812022-01-20 17:56:50 +08004498#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004499/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004500 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004501MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004502static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004503{
4504 size_t i, j;
4505 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004506
Gabor Mezei15b95a62022-05-09 16:37:58 +02004507 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004508 {
Jerry Yuf377d642022-01-25 10:43:59 +08004509 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004510 {
Jerry Yuf377d642022-01-25 10:43:59 +08004511 if( sig_algs[i] != sig_algs[j] )
4512 continue;
4513 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4514 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4515 sig_algs[i], j, i );
4516 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004517 }
4518 }
4519 return( ret );
4520}
4521
4522#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4523
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004524/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004525 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004526 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004527int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004528 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004529{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004530#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004532#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004533
Jerry Yu1a8b4812022-01-20 17:56:50 +08004534#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004535 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004536 {
4537 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4538 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4539 }
4540
Jerry Yuf377d642022-01-25 10:43:59 +08004541 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004542 {
4543 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4544 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4545 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004546
4547#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004548 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004549 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004550 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004551 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4552 }
4553
Jerry Yuf377d642022-01-25 10:43:59 +08004554 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004555 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004556 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004557 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4558 }
4559#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004560#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4561
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004562 /* Use the functions here so that they are covered in tests,
4563 * but otherwise access member directly for efficiency */
4564 mbedtls_ssl_conf_endpoint( conf, endpoint );
4565 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004566
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004567 /*
4568 * Things that are common to all presets
4569 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004570#if defined(MBEDTLS_SSL_CLI_C)
4571 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4572 {
4573 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4574#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4575 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4576#endif
4577 }
4578#endif
4579
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004580#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4581 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4582#endif
4583
4584#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4585 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4586#endif
4587
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004588#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004589 conf->f_cookie_write = ssl_cookie_write_dummy;
4590 conf->f_cookie_check = ssl_cookie_check_dummy;
4591#endif
4592
4593#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4594 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4595#endif
4596
Janos Follath088ce432017-04-10 12:42:31 +01004597#if defined(MBEDTLS_SSL_SRV_C)
4598 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004599 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004600#endif
4601
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004602#if defined(MBEDTLS_SSL_PROTO_DTLS)
4603 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4604 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4605#endif
4606
4607#if defined(MBEDTLS_SSL_RENEGOTIATION)
4608 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004609 memset( conf->renego_period, 0x00, 2 );
4610 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004611#endif
4612
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004613#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004614 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4615 {
4616 const unsigned char dhm_p[] =
4617 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4618 const unsigned char dhm_g[] =
4619 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004620
Hanno Beckere2defad2021-07-24 05:59:17 +01004621 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4622 dhm_p, sizeof( dhm_p ),
4623 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4624 {
4625 return( ret );
4626 }
4627 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004628#endif
4629
Ronald Cron6f135e12021-12-08 16:57:54 +01004630#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker71f1ed62021-07-24 06:01:47 +01004631 /*
4632 * Allow all TLS 1.3 key exchange modes by default.
4633 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004634 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004635#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004636
XiaokangQian4d3a6042022-04-21 13:46:17 +00004637 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4638 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004639#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004640 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004641 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004642#else
XiaokangQian060d8672022-04-21 09:24:56 +00004643 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004644#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004645 }
4646 else
4647 {
4648#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4649 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4650 {
4651 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4652 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4653 }
4654 else
4655 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4656 {
4657 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4658 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4659 }
4660#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4661 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4662 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4663#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4664 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4665 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4666#else
4667 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4668#endif
4669 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004670
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004671 /*
4672 * Preset-specific defaults
4673 */
4674 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004675 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004676 /*
4677 * NSA Suite B
4678 */
4679 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004680
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004681 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004682
4683#if defined(MBEDTLS_X509_CRT_PARSE_C)
4684 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004685#endif
4686
Gilles Peskineeccd8882020-03-10 12:19:08 +01004687#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004688#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4689 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4690 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4691 else
4692#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4693 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004694#endif
4695
Brett Warrene0edc842021-08-17 09:53:13 +01004696#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4697 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004698#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004699 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004700 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004701
4702 /*
4703 * Default
4704 */
4705 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004706
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004707 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004708
4709#if defined(MBEDTLS_X509_CRT_PARSE_C)
4710 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4711#endif
4712
Gilles Peskineeccd8882020-03-10 12:19:08 +01004713#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004714#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4715 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4716 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4717 else
4718#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4719 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004720#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004721
Brett Warrene0edc842021-08-17 09:53:13 +01004722#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4723 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004724#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004725 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004726
4727#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4728 conf->dhm_min_bitlen = 1024;
4729#endif
4730 }
4731
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004732 return( 0 );
4733}
4734
4735/*
4736 * Free mbedtls_ssl_config
4737 */
4738void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4739{
4740#if defined(MBEDTLS_DHM_C)
4741 mbedtls_mpi_free( &conf->dhm_P );
4742 mbedtls_mpi_free( &conf->dhm_G );
4743#endif
4744
Gilles Peskineeccd8882020-03-10 12:19:08 +01004745#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004746#if defined(MBEDTLS_USE_PSA_CRYPTO)
4747 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4748 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004749 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4750 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004751#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004752 if( conf->psk != NULL )
4753 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004754 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004755 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004756 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004757 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004758 }
4759
4760 if( conf->psk_identity != NULL )
4761 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004762 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004763 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004764 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004765 conf->psk_identity_len = 0;
4766 }
4767#endif
4768
4769#if defined(MBEDTLS_X509_CRT_PARSE_C)
4770 ssl_key_cert_free( conf->key_cert );
4771#endif
4772
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004773 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004774}
4775
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004776#if defined(MBEDTLS_PK_C) && \
4777 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004778/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004779 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004780 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004781unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004782{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004783#if defined(MBEDTLS_RSA_C)
4784 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4785 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004786#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004787#if defined(MBEDTLS_ECDSA_C)
4788 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4789 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004790#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004791 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004792}
4793
Hanno Becker7e5437a2017-04-28 17:15:26 +01004794unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4795{
4796 switch( type ) {
4797 case MBEDTLS_PK_RSA:
4798 return( MBEDTLS_SSL_SIG_RSA );
4799 case MBEDTLS_PK_ECDSA:
4800 case MBEDTLS_PK_ECKEY:
4801 return( MBEDTLS_SSL_SIG_ECDSA );
4802 default:
4803 return( MBEDTLS_SSL_SIG_ANON );
4804 }
4805}
4806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004807mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004808{
4809 switch( sig )
4810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004811#if defined(MBEDTLS_RSA_C)
4812 case MBEDTLS_SSL_SIG_RSA:
4813 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004814#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004815#if defined(MBEDTLS_ECDSA_C)
4816 case MBEDTLS_SSL_SIG_ECDSA:
4817 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004818#endif
4819 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004820 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004821 }
4822}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004823#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004824
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004825/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004826 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004827 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004828mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004829{
4830 switch( hash )
4831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004832#if defined(MBEDTLS_MD5_C)
4833 case MBEDTLS_SSL_HASH_MD5:
4834 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004835#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004836#if defined(MBEDTLS_SHA1_C)
4837 case MBEDTLS_SSL_HASH_SHA1:
4838 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004839#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004840#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004841 case MBEDTLS_SSL_HASH_SHA224:
4842 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004843#endif
4844#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004845 case MBEDTLS_SSL_HASH_SHA256:
4846 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004847#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004848#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004849 case MBEDTLS_SSL_HASH_SHA384:
4850 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004851#endif
4852#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004853 case MBEDTLS_SSL_HASH_SHA512:
4854 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004855#endif
4856 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004857 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004858 }
4859}
4860
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004861/*
4862 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4863 */
4864unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4865{
4866 switch( md )
4867 {
4868#if defined(MBEDTLS_MD5_C)
4869 case MBEDTLS_MD_MD5:
4870 return( MBEDTLS_SSL_HASH_MD5 );
4871#endif
4872#if defined(MBEDTLS_SHA1_C)
4873 case MBEDTLS_MD_SHA1:
4874 return( MBEDTLS_SSL_HASH_SHA1 );
4875#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004876#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004877 case MBEDTLS_MD_SHA224:
4878 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004879#endif
4880#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004881 case MBEDTLS_MD_SHA256:
4882 return( MBEDTLS_SSL_HASH_SHA256 );
4883#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004884#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004885 case MBEDTLS_MD_SHA384:
4886 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004887#endif
4888#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004889 case MBEDTLS_MD_SHA512:
4890 return( MBEDTLS_SSL_HASH_SHA512 );
4891#endif
4892 default:
4893 return( MBEDTLS_SSL_HASH_NONE );
4894 }
4895}
4896
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004897/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004898 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004899 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004900 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004901int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004902{
Brett Warrene0edc842021-08-17 09:53:13 +01004903 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004904
Brett Warrene0edc842021-08-17 09:53:13 +01004905 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004906 return( -1 );
4907
Brett Warrene0edc842021-08-17 09:53:13 +01004908 for( ; *group_list != 0; group_list++ )
4909 {
4910 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004911 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01004912 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004913
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004914 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004915}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004916
4917#if defined(MBEDTLS_ECP_C)
4918/*
4919 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
4920 */
4921int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
4922{
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01004923 uint16_t tls_id = mbedtls_ecp_curve_info_from_grp_id( grp_id )->tls_id;
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004924 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
4925}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004926#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004928#if defined(MBEDTLS_X509_CRT_PARSE_C)
4929int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
4930 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004931 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02004932 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004933{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004934 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004935 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004936 const char *ext_oid;
4937 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004939 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004940 {
4941 /* Server part of the key exchange */
4942 switch( ciphersuite->key_exchange )
4943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004944 case MBEDTLS_KEY_EXCHANGE_RSA:
4945 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004946 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004947 break;
4948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004949 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
4950 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
4951 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
4952 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004953 break;
4954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004955 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
4956 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004957 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004958 break;
4959
4960 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004961 case MBEDTLS_KEY_EXCHANGE_NONE:
4962 case MBEDTLS_KEY_EXCHANGE_PSK:
4963 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
4964 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02004965 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004966 usage = 0;
4967 }
4968 }
4969 else
4970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004971 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
4972 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004973 }
4974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004975 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004976 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004977 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004978 ret = -1;
4979 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004981 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004983 ext_oid = MBEDTLS_OID_SERVER_AUTH;
4984 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004985 }
4986 else
4987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004988 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
4989 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004990 }
4991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004992 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004993 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004994 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004995 ret = -1;
4996 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004997
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004998 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004999}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005000#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005001
Jerry Yu148165c2021-09-24 23:20:59 +08005002#if defined(MBEDTLS_USE_PSA_CRYPTO)
5003int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5004 const mbedtls_md_type_t md,
5005 unsigned char *dst,
5006 size_t dst_len,
5007 size_t *olen )
5008{
Ronald Cronf6893e12022-01-07 22:09:01 +01005009 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5010 psa_hash_operation_t *hash_operation_to_clone;
5011 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5012
Jerry Yu148165c2021-09-24 23:20:59 +08005013 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005014
5015 switch( md )
5016 {
5017#if defined(MBEDTLS_SHA384_C)
5018 case MBEDTLS_MD_SHA384:
5019 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5020 break;
5021#endif
5022
5023#if defined(MBEDTLS_SHA256_C)
5024 case MBEDTLS_MD_SHA256:
5025 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5026 break;
5027#endif
5028
5029 default:
5030 goto exit;
5031 }
5032
5033 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5034 if( status != PSA_SUCCESS )
5035 goto exit;
5036
5037 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5038 if( status != PSA_SUCCESS )
5039 goto exit;
5040
5041exit:
Ronald Cronb788c042022-02-15 09:14:15 +01005042 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005043}
5044#else /* MBEDTLS_USE_PSA_CRYPTO */
5045
Jerry Yu24c0ec32021-09-09 14:21:07 +08005046#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005047MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005048static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5049 unsigned char *dst,
5050 size_t dst_len,
5051 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005052{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005053 int ret;
5054 mbedtls_sha512_context sha512;
5055
5056 if( dst_len < 48 )
5057 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5058
5059 mbedtls_sha512_init( &sha512 );
5060 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
5061
5062 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5063 {
5064 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5065 goto exit;
5066 }
5067
5068 *olen = 48;
5069
5070exit:
5071
5072 mbedtls_sha512_free( &sha512 );
5073 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005074}
5075#endif /* MBEDTLS_SHA384_C */
5076
5077#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005078MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005079static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5080 unsigned char *dst,
5081 size_t dst_len,
5082 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005083{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005084 int ret;
5085 mbedtls_sha256_context sha256;
5086
5087 if( dst_len < 32 )
5088 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5089
5090 mbedtls_sha256_init( &sha256 );
5091 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005092
Jerry Yu24c0ec32021-09-09 14:21:07 +08005093 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5094 {
5095 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5096 goto exit;
5097 }
5098
5099 *olen = 32;
5100
5101exit:
5102
5103 mbedtls_sha256_free( &sha256 );
5104 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005105}
5106#endif /* MBEDTLS_SHA256_C */
5107
Jerry Yu000f9762021-09-14 11:12:51 +08005108int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5109 const mbedtls_md_type_t md,
5110 unsigned char *dst,
5111 size_t dst_len,
5112 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005113{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005114 switch( md )
5115 {
5116
Jerry Yu24c0ec32021-09-09 14:21:07 +08005117#if defined(MBEDTLS_SHA384_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005118 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005119 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08005120#endif /* MBEDTLS_SHA384_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005121
Jerry Yu24c0ec32021-09-09 14:21:07 +08005122#if defined(MBEDTLS_SHA256_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005123 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005124 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08005125#endif /* MBEDTLS_SHA256_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005126
5127 default:
5128 break;
5129 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005130 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005131}
XiaokangQian647719a2021-12-07 09:16:29 +00005132
Jerry Yu148165c2021-09-24 23:20:59 +08005133#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005134
Gabor Mezei078e8032022-04-27 21:17:56 +02005135#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5136/* mbedtls_ssl_parse_sig_alg_ext()
5137 *
5138 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5139 * value (TLS 1.3 RFC8446):
5140 * enum {
5141 * ....
5142 * ecdsa_secp256r1_sha256( 0x0403 ),
5143 * ecdsa_secp384r1_sha384( 0x0503 ),
5144 * ecdsa_secp521r1_sha512( 0x0603 ),
5145 * ....
5146 * } SignatureScheme;
5147 *
5148 * struct {
5149 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5150 * } SignatureSchemeList;
5151 *
5152 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5153 * value (TLS 1.2 RFC5246):
5154 * enum {
5155 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5156 * sha512(6), (255)
5157 * } HashAlgorithm;
5158 *
5159 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5160 * SignatureAlgorithm;
5161 *
5162 * struct {
5163 * HashAlgorithm hash;
5164 * SignatureAlgorithm signature;
5165 * } SignatureAndHashAlgorithm;
5166 *
5167 * SignatureAndHashAlgorithm
5168 * supported_signature_algorithms<2..2^16-2>;
5169 *
5170 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5171 * generalization of the TLS 1.2 signature algorithm extension.
5172 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5173 * `SignatureScheme` field of TLS 1.3
5174 *
5175 */
5176int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5177 const unsigned char *buf,
5178 const unsigned char *end )
5179{
5180 const unsigned char *p = buf;
5181 size_t supported_sig_algs_len = 0;
5182 const unsigned char *supported_sig_algs_end;
5183 uint16_t sig_alg;
5184 uint32_t common_idx = 0;
5185
5186 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5187 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5188 p += 2;
5189
5190 memset( ssl->handshake->received_sig_algs, 0,
5191 sizeof(ssl->handshake->received_sig_algs) );
5192
5193 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5194 supported_sig_algs_end = p + supported_sig_algs_len;
5195 while( p < supported_sig_algs_end )
5196 {
5197 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5198 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5199 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005200 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5201 sig_alg,
5202 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005203#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005204 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005205 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5206 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5207 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005208 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005209 }
5210#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005211
Jerry Yuf3b46b52022-06-19 16:52:27 +08005212 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5213 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005214
5215 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5216 {
5217 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5218 common_idx += 1;
5219 }
5220 }
5221 /* Check that we consumed all the message. */
5222 if( p != end )
5223 {
5224 MBEDTLS_SSL_DEBUG_MSG( 1,
5225 ( "Signature algorithms extension length misaligned" ) );
5226 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5227 MBEDTLS_ERR_SSL_DECODE_ERROR );
5228 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5229 }
5230
5231 if( common_idx == 0 )
5232 {
5233 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5234 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5235 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5236 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5237 }
5238
Gabor Mezei15b95a62022-05-09 16:37:58 +02005239 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005240 return( 0 );
5241}
5242
5243#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5244
Jerry Yudc7bd172022-02-17 13:44:15 +08005245#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5246
5247#if defined(MBEDTLS_USE_PSA_CRYPTO)
5248
5249static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5250 mbedtls_svc_key_id_t key,
5251 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005252 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005253 const unsigned char* seed, size_t seed_length,
5254 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005255 const unsigned char* other_secret,
5256 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005257 size_t capacity )
5258{
5259 psa_status_t status;
5260
5261 status = psa_key_derivation_setup( derivation, alg );
5262 if( status != PSA_SUCCESS )
5263 return( status );
5264
5265 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5266 {
5267 status = psa_key_derivation_input_bytes( derivation,
5268 PSA_KEY_DERIVATION_INPUT_SEED,
5269 seed, seed_length );
5270 if( status != PSA_SUCCESS )
5271 return( status );
5272
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005273 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005274 {
5275 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005276 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5277 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005278 if( status != PSA_SUCCESS )
5279 return( status );
5280 }
5281
Jerry Yudc7bd172022-02-17 13:44:15 +08005282 if( mbedtls_svc_key_id_is_null( key ) )
5283 {
5284 status = psa_key_derivation_input_bytes(
5285 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005286 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005287 }
5288 else
5289 {
5290 status = psa_key_derivation_input_key(
5291 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5292 }
5293 if( status != PSA_SUCCESS )
5294 return( status );
5295
5296 status = psa_key_derivation_input_bytes( derivation,
5297 PSA_KEY_DERIVATION_INPUT_LABEL,
5298 label, label_length );
5299 if( status != PSA_SUCCESS )
5300 return( status );
5301 }
5302 else
5303 {
5304 return( PSA_ERROR_NOT_SUPPORTED );
5305 }
5306
5307 status = psa_key_derivation_set_capacity( derivation, capacity );
5308 if( status != PSA_SUCCESS )
5309 return( status );
5310
5311 return( PSA_SUCCESS );
5312}
5313
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005314MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005315static int tls_prf_generic( mbedtls_md_type_t md_type,
5316 const unsigned char *secret, size_t slen,
5317 const char *label,
5318 const unsigned char *random, size_t rlen,
5319 unsigned char *dstbuf, size_t dlen )
5320{
5321 psa_status_t status;
5322 psa_algorithm_t alg;
5323 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5324 psa_key_derivation_operation_t derivation =
5325 PSA_KEY_DERIVATION_OPERATION_INIT;
5326
5327 if( md_type == MBEDTLS_MD_SHA384 )
5328 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5329 else
5330 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5331
5332 /* Normally a "secret" should be long enough to be impossible to
5333 * find by brute force, and in particular should not be empty. But
5334 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5335 * and for this use case it makes sense to have a 0-length "secret".
5336 * Since the key API doesn't allow importing a key of length 0,
5337 * keep master_key=0, which setup_psa_key_derivation() understands
5338 * to mean a 0-length "secret" input. */
5339 if( slen != 0 )
5340 {
5341 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5342 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5343 psa_set_key_algorithm( &key_attributes, alg );
5344 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5345
5346 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5347 if( status != PSA_SUCCESS )
5348 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5349 }
5350
5351 status = setup_psa_key_derivation( &derivation,
5352 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005353 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005354 random, rlen,
5355 (unsigned char const *) label,
5356 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005357 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005358 dlen );
5359 if( status != PSA_SUCCESS )
5360 {
5361 psa_key_derivation_abort( &derivation );
5362 psa_destroy_key( master_key );
5363 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5364 }
5365
5366 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5367 if( status != PSA_SUCCESS )
5368 {
5369 psa_key_derivation_abort( &derivation );
5370 psa_destroy_key( master_key );
5371 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5372 }
5373
5374 status = psa_key_derivation_abort( &derivation );
5375 if( status != PSA_SUCCESS )
5376 {
5377 psa_destroy_key( master_key );
5378 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5379 }
5380
5381 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5382 status = psa_destroy_key( master_key );
5383 if( status != PSA_SUCCESS )
5384 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5385
5386 return( 0 );
5387}
5388
5389#else /* MBEDTLS_USE_PSA_CRYPTO */
5390
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005391MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005392static int tls_prf_generic( mbedtls_md_type_t md_type,
5393 const unsigned char *secret, size_t slen,
5394 const char *label,
5395 const unsigned char *random, size_t rlen,
5396 unsigned char *dstbuf, size_t dlen )
5397{
5398 size_t nb;
5399 size_t i, j, k, md_len;
5400 unsigned char *tmp;
5401 size_t tmp_len = 0;
5402 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5403 const mbedtls_md_info_t *md_info;
5404 mbedtls_md_context_t md_ctx;
5405 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5406
5407 mbedtls_md_init( &md_ctx );
5408
5409 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5410 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5411
5412 md_len = mbedtls_md_get_size( md_info );
5413
5414 tmp_len = md_len + strlen( label ) + rlen;
5415 tmp = mbedtls_calloc( 1, tmp_len );
5416 if( tmp == NULL )
5417 {
5418 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5419 goto exit;
5420 }
5421
5422 nb = strlen( label );
5423 memcpy( tmp + md_len, label, nb );
5424 memcpy( tmp + md_len + nb, random, rlen );
5425 nb += rlen;
5426
5427 /*
5428 * Compute P_<hash>(secret, label + random)[0..dlen]
5429 */
5430 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5431 goto exit;
5432
5433 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5434 if( ret != 0 )
5435 goto exit;
5436 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5437 if( ret != 0 )
5438 goto exit;
5439 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5440 if( ret != 0 )
5441 goto exit;
5442
5443 for( i = 0; i < dlen; i += md_len )
5444 {
5445 ret = mbedtls_md_hmac_reset ( &md_ctx );
5446 if( ret != 0 )
5447 goto exit;
5448 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5449 if( ret != 0 )
5450 goto exit;
5451 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5452 if( ret != 0 )
5453 goto exit;
5454
5455 ret = mbedtls_md_hmac_reset ( &md_ctx );
5456 if( ret != 0 )
5457 goto exit;
5458 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5459 if( ret != 0 )
5460 goto exit;
5461 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5462 if( ret != 0 )
5463 goto exit;
5464
5465 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5466
5467 for( j = 0; j < k; j++ )
5468 dstbuf[i + j] = h_i[j];
5469 }
5470
5471exit:
5472 mbedtls_md_free( &md_ctx );
5473
5474 mbedtls_platform_zeroize( tmp, tmp_len );
5475 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5476
5477 mbedtls_free( tmp );
5478
5479 return( ret );
5480}
5481#endif /* MBEDTLS_USE_PSA_CRYPTO */
5482
5483#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005484MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005485static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5486 const char *label,
5487 const unsigned char *random, size_t rlen,
5488 unsigned char *dstbuf, size_t dlen )
5489{
5490 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5491 label, random, rlen, dstbuf, dlen ) );
5492}
5493#endif /* MBEDTLS_SHA256_C */
5494
5495#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005496MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005497static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5498 const char *label,
5499 const unsigned char *random, size_t rlen,
5500 unsigned char *dstbuf, size_t dlen )
5501{
5502 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5503 label, random, rlen, dstbuf, dlen ) );
5504}
5505#endif /* MBEDTLS_SHA384_C */
5506
Jerry Yuf009d862022-02-17 14:01:37 +08005507/*
5508 * Set appropriate PRF function and other SSL / TLS1.2 functions
5509 *
5510 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005511 * - hash associated with the ciphersuite (only used by TLS 1.2)
5512 *
5513 * Outputs:
5514 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5515 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005516MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005517static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005518 mbedtls_md_type_t hash )
5519{
Jerry Yuf009d862022-02-17 14:01:37 +08005520#if defined(MBEDTLS_SHA384_C)
Ronald Cron81591aa2022-03-07 09:05:51 +01005521 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005522 {
5523 handshake->tls_prf = tls_prf_sha384;
5524 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5525 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5526 }
5527 else
5528#endif
5529#if defined(MBEDTLS_SHA256_C)
Jerry Yuf009d862022-02-17 14:01:37 +08005530 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005531 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005532 handshake->tls_prf = tls_prf_sha256;
5533 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5534 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5535 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005536#else
Jerry Yuf009d862022-02-17 14:01:37 +08005537 {
Jerry Yuf009d862022-02-17 14:01:37 +08005538 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005539 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005540 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5541 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005542#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005543
5544 return( 0 );
5545}
Jerry Yud6ab2352022-02-17 14:03:43 +08005546
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005547/*
5548 * Compute master secret if needed
5549 *
5550 * Parameters:
5551 * [in/out] handshake
5552 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5553 * (PSA-PSK) ciphersuite_info, psk_opaque
5554 * [out] premaster (cleared)
5555 * [out] master
5556 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5557 * debug: conf->f_dbg, conf->p_dbg
5558 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005559 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005560 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005561MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005562static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5563 unsigned char *master,
5564 const mbedtls_ssl_context *ssl )
5565{
5566 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5567
5568 /* cf. RFC 5246, Section 8.1:
5569 * "The master secret is always exactly 48 bytes in length." */
5570 size_t const master_secret_len = 48;
5571
5572#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5573 unsigned char session_hash[48];
5574#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5575
5576 /* The label for the KDF used for key expansion.
5577 * This is either "master secret" or "extended master secret"
5578 * depending on whether the Extended Master Secret extension
5579 * is used. */
5580 char const *lbl = "master secret";
5581
Przemek Stekielae4ed302022-04-05 17:15:55 +02005582 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005583 * - If the Extended Master Secret extension is not used,
5584 * this is ClientHello.Random + ServerHello.Random
5585 * (see Sect. 8.1 in RFC 5246).
5586 * - If the Extended Master Secret extension is used,
5587 * this is the transcript of the handshake so far.
5588 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005589 unsigned char const *seed = handshake->randbytes;
5590 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005591
5592#if !defined(MBEDTLS_DEBUG_C) && \
5593 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5594 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5595 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5596 ssl = NULL; /* make sure we don't use it except for those cases */
5597 (void) ssl;
5598#endif
5599
5600 if( handshake->resume != 0 )
5601 {
5602 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5603 return( 0 );
5604 }
5605
5606#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5607 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5608 {
5609 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005610 seed = session_hash;
5611 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005612
5613 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005614 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005615 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005616#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005617
Przemek Stekiel99114f32022-04-22 11:20:09 +02005618#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005619 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005620 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005621 {
5622 /* Perform PSK-to-MS expansion in a single step. */
5623 psa_status_t status;
5624 psa_algorithm_t alg;
5625 mbedtls_svc_key_id_t psk;
5626 psa_key_derivation_operation_t derivation =
5627 PSA_KEY_DERIVATION_OPERATION_INIT;
5628 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5629
5630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5631
5632 psk = mbedtls_ssl_get_opaque_psk( ssl );
5633
5634 if( hash_alg == MBEDTLS_MD_SHA384 )
5635 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5636 else
5637 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5638
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005639 size_t other_secret_len = 0;
5640 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005641
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005642 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005643 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005644 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005645 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005646 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005647 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005648 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005649 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005650 other_secret_len = 48;
5651 other_secret = handshake->premaster + 2;
5652 break;
5653 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005654 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005655 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5656 other_secret = handshake->premaster + 2;
5657 break;
5658 default:
5659 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005660 }
5661
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005662 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005663 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005664 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005665 (unsigned char const *) lbl,
5666 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005667 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005668 master_secret_len );
5669 if( status != PSA_SUCCESS )
5670 {
5671 psa_key_derivation_abort( &derivation );
5672 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5673 }
5674
5675 status = psa_key_derivation_output_bytes( &derivation,
5676 master,
5677 master_secret_len );
5678 if( status != PSA_SUCCESS )
5679 {
5680 psa_key_derivation_abort( &derivation );
5681 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5682 }
5683
5684 status = psa_key_derivation_abort( &derivation );
5685 if( status != PSA_SUCCESS )
5686 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5687 }
5688 else
5689#endif
5690 {
5691 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005692 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005693 master,
5694 master_secret_len );
5695 if( ret != 0 )
5696 {
5697 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5698 return( ret );
5699 }
5700
5701 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5702 handshake->premaster,
5703 handshake->pmslen );
5704
5705 mbedtls_platform_zeroize( handshake->premaster,
5706 sizeof(handshake->premaster) );
5707 }
5708
5709 return( 0 );
5710}
5711
Jerry Yud62f87e2022-02-17 14:09:02 +08005712int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5713{
5714 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5715 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5716 ssl->handshake->ciphersuite_info;
5717
5718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5719
5720 /* Set PRF, calc_verify and calc_finished function pointers */
5721 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005722 ciphersuite_info->mac );
5723 if( ret != 0 )
5724 {
5725 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5726 return( ret );
5727 }
5728
5729 /* Compute master secret if needed */
5730 ret = ssl_compute_master( ssl->handshake,
5731 ssl->session_negotiate->master,
5732 ssl );
5733 if( ret != 0 )
5734 {
5735 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5736 return( ret );
5737 }
5738
5739 /* Swap the client and server random values:
5740 * - MS derivation wanted client+server (RFC 5246 8.1)
5741 * - key derivation wants server+client (RFC 5246 6.3) */
5742 {
5743 unsigned char tmp[64];
5744 memcpy( tmp, ssl->handshake->randbytes, 64 );
5745 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5746 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5747 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5748 }
5749
5750 /* Populate transform structure */
5751 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5752 ssl->session_negotiate->ciphersuite,
5753 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005754#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005755 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005756#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005757 ssl->handshake->tls_prf,
5758 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005759 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005760 ssl->conf->endpoint,
5761 ssl );
5762 if( ret != 0 )
5763 {
5764 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5765 return( ret );
5766 }
5767
5768 /* We no longer need Server/ClientHello.random values */
5769 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5770 sizeof( ssl->handshake->randbytes ) );
5771
5772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5773
5774 return( 0 );
5775}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005776
Ronald Cron4dcbca92022-03-07 10:21:40 +01005777int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5778{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005779 switch( md )
5780 {
5781#if defined(MBEDTLS_SHA384_C)
5782 case MBEDTLS_SSL_HASH_SHA384:
5783 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5784 break;
5785#endif
5786#if defined(MBEDTLS_SHA256_C)
5787 case MBEDTLS_SSL_HASH_SHA256:
5788 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5789 break;
5790#endif
5791 default:
5792 return( -1 );
5793 }
5794
Ronald Cronc2f13a02022-03-07 10:25:24 +01005795 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005796}
5797
Jerry Yu8392e0d2022-02-17 14:10:24 +08005798#if defined(MBEDTLS_SHA256_C)
5799void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5800 unsigned char *hash,
5801 size_t *hlen )
5802{
5803#if defined(MBEDTLS_USE_PSA_CRYPTO)
5804 size_t hash_size;
5805 psa_status_t status;
5806 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5807
5808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5809 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5810 if( status != PSA_SUCCESS )
5811 {
5812 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5813 return;
5814 }
5815
5816 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5817 if( status != PSA_SUCCESS )
5818 {
5819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5820 return;
5821 }
5822
5823 *hlen = 32;
5824 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5826#else
5827 mbedtls_sha256_context sha256;
5828
5829 mbedtls_sha256_init( &sha256 );
5830
5831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5832
5833 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5834 mbedtls_sha256_finish( &sha256, hash );
5835
5836 *hlen = 32;
5837
5838 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5840
5841 mbedtls_sha256_free( &sha256 );
5842#endif /* MBEDTLS_USE_PSA_CRYPTO */
5843 return;
5844}
5845#endif /* MBEDTLS_SHA256_C */
5846
Jerry Yuc1cb3842022-02-17 14:13:48 +08005847#if defined(MBEDTLS_SHA384_C)
5848void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5849 unsigned char *hash,
5850 size_t *hlen )
5851{
5852#if defined(MBEDTLS_USE_PSA_CRYPTO)
5853 size_t hash_size;
5854 psa_status_t status;
5855 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5856
5857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5858 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5859 if( status != PSA_SUCCESS )
5860 {
5861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5862 return;
5863 }
5864
5865 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5866 if( status != PSA_SUCCESS )
5867 {
5868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5869 return;
5870 }
5871
5872 *hlen = 48;
5873 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5875#else
5876 mbedtls_sha512_context sha512;
5877
5878 mbedtls_sha512_init( &sha512 );
5879
5880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
5881
5882 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
5883 mbedtls_sha512_finish( &sha512, hash );
5884
5885 *hlen = 48;
5886
5887 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5889
5890 mbedtls_sha512_free( &sha512 );
5891#endif /* MBEDTLS_USE_PSA_CRYPTO */
5892 return;
5893}
5894#endif /* MBEDTLS_SHA384_C */
5895
Neil Armstrong80f6f322022-05-03 17:56:38 +02005896#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
5897 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08005898int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
5899{
5900 unsigned char *p = ssl->handshake->premaster;
5901 unsigned char *end = p + sizeof( ssl->handshake->premaster );
5902 const unsigned char *psk = NULL;
5903 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005904 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08005905
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005906 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08005907 {
5908 /*
5909 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005910 * checked before calling this function.
5911 *
5912 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005913 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08005914 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005915 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5916 {
5917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5918 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5919 }
Jerry Yuce3dca42022-02-17 14:16:37 +08005920 }
5921
5922 /*
5923 * PMS = struct {
5924 * opaque other_secret<0..2^16-1>;
5925 * opaque psk<0..2^16-1>;
5926 * };
5927 * with "other_secret" depending on the particular key exchange
5928 */
5929#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5930 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
5931 {
5932 if( end - p < 2 )
5933 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5934
5935 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5936 p += 2;
5937
5938 if( end < p || (size_t)( end - p ) < psk_len )
5939 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5940
5941 memset( p, 0, psk_len );
5942 p += psk_len;
5943 }
5944 else
5945#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5946#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
5947 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5948 {
5949 /*
5950 * other_secret already set by the ClientKeyExchange message,
5951 * and is 48 bytes long
5952 */
5953 if( end - p < 2 )
5954 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5955
5956 *p++ = 0;
5957 *p++ = 48;
5958 p += 48;
5959 }
5960 else
5961#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
5962#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
5963 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5964 {
5965 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5966 size_t len;
5967
5968 /* Write length only when we know the actual value */
5969 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
5970 p + 2, end - ( p + 2 ), &len,
5971 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5972 {
5973 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
5974 return( ret );
5975 }
5976 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
5977 p += 2 + len;
5978
5979 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
5980 }
5981 else
5982#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02005983#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08005984 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
5985 {
5986 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5987 size_t zlen;
5988
5989 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
5990 p + 2, end - ( p + 2 ),
5991 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5992 {
5993 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
5994 return( ret );
5995 }
5996
5997 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
5998 p += 2 + zlen;
5999
6000 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6001 MBEDTLS_DEBUG_ECDH_Z );
6002 }
6003 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006004#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006005 {
6006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6007 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6008 }
6009
6010 /* opaque psk<0..2^16-1>; */
6011 if( end - p < 2 )
6012 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6013
6014 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6015 p += 2;
6016
6017 if( end < p || (size_t)( end - p ) < psk_len )
6018 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6019
6020 memcpy( p, psk, psk_len );
6021 p += psk_len;
6022
6023 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6024
6025 return( 0 );
6026}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006027#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006028
6029#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006030MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006031static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6032
6033#if defined(MBEDTLS_SSL_PROTO_DTLS)
6034int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6035{
6036 /* If renegotiation is not enforced, retransmit until we would reach max
6037 * timeout if we were using the usual handshake doubling scheme */
6038 if( ssl->conf->renego_max_records < 0 )
6039 {
6040 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6041 unsigned char doublings = 1;
6042
6043 while( ratio != 0 )
6044 {
6045 ++doublings;
6046 ratio >>= 1;
6047 }
6048
6049 if( ++ssl->renego_records_seen > doublings )
6050 {
6051 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6052 return( 0 );
6053 }
6054 }
6055
6056 return( ssl_write_hello_request( ssl ) );
6057}
6058#endif
6059#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006060
Jerry Yud9526692022-02-17 14:23:47 +08006061/*
6062 * Handshake functions
6063 */
6064#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6065/* No certificate support -> dummy functions */
6066int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6067{
6068 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6069 ssl->handshake->ciphersuite_info;
6070
6071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6072
6073 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6074 {
6075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6076 ssl->state++;
6077 return( 0 );
6078 }
6079
6080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6081 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6082}
6083
6084int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6085{
6086 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6087 ssl->handshake->ciphersuite_info;
6088
6089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6090
6091 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6092 {
6093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6094 ssl->state++;
6095 return( 0 );
6096 }
6097
6098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6099 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6100}
6101
6102#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6103/* Some certificate support -> implement write and parse */
6104
6105int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6106{
6107 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6108 size_t i, n;
6109 const mbedtls_x509_crt *crt;
6110 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6111 ssl->handshake->ciphersuite_info;
6112
6113 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6114
6115 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6116 {
6117 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6118 ssl->state++;
6119 return( 0 );
6120 }
6121
6122#if defined(MBEDTLS_SSL_CLI_C)
6123 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6124 {
6125 if( ssl->handshake->client_auth == 0 )
6126 {
6127 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6128 ssl->state++;
6129 return( 0 );
6130 }
6131 }
6132#endif /* MBEDTLS_SSL_CLI_C */
6133#if defined(MBEDTLS_SSL_SRV_C)
6134 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6135 {
6136 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6137 {
6138 /* Should never happen because we shouldn't have picked the
6139 * ciphersuite if we don't have a certificate. */
6140 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6141 }
6142 }
6143#endif
6144
6145 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6146
6147 /*
6148 * 0 . 0 handshake type
6149 * 1 . 3 handshake length
6150 * 4 . 6 length of all certs
6151 * 7 . 9 length of cert. 1
6152 * 10 . n-1 peer certificate
6153 * n . n+2 length of cert. 2
6154 * n+3 . ... upper level cert, etc.
6155 */
6156 i = 7;
6157 crt = mbedtls_ssl_own_cert( ssl );
6158
6159 while( crt != NULL )
6160 {
6161 n = crt->raw.len;
6162 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6163 {
6164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6165 " > %" MBEDTLS_PRINTF_SIZET,
6166 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6167 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6168 }
6169
6170 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6171 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6172 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6173
6174 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6175 i += n; crt = crt->next;
6176 }
6177
6178 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6179 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6180 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6181
6182 ssl->out_msglen = i;
6183 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6184 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6185
6186 ssl->state++;
6187
6188 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6189 {
6190 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6191 return( ret );
6192 }
6193
6194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6195
6196 return( ret );
6197}
6198
6199#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6200
6201#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006202MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006203static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6204 unsigned char *crt_buf,
6205 size_t crt_buf_len )
6206{
6207 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6208
6209 if( peer_crt == NULL )
6210 return( -1 );
6211
6212 if( peer_crt->raw.len != crt_buf_len )
6213 return( -1 );
6214
6215 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6216}
6217#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006218MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006219static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6220 unsigned char *crt_buf,
6221 size_t crt_buf_len )
6222{
6223 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6224 unsigned char const * const peer_cert_digest =
6225 ssl->session->peer_cert_digest;
6226 mbedtls_md_type_t const peer_cert_digest_type =
6227 ssl->session->peer_cert_digest_type;
6228 mbedtls_md_info_t const * const digest_info =
6229 mbedtls_md_info_from_type( peer_cert_digest_type );
6230 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6231 size_t digest_len;
6232
6233 if( peer_cert_digest == NULL || digest_info == NULL )
6234 return( -1 );
6235
6236 digest_len = mbedtls_md_get_size( digest_info );
6237 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6238 return( -1 );
6239
6240 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6241 if( ret != 0 )
6242 return( -1 );
6243
6244 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6245}
6246#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6247#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6248
6249/*
6250 * Once the certificate message is read, parse it into a cert chain and
6251 * perform basic checks, but leave actual verification to the caller
6252 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006253MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006254static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6255 mbedtls_x509_crt *chain )
6256{
6257 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6258#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6259 int crt_cnt=0;
6260#endif
6261 size_t i, n;
6262 uint8_t alert;
6263
6264 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6265 {
6266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6267 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6268 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6269 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6270 }
6271
6272 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6273 {
6274 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6275 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6276 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6277 }
6278
6279 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6280 {
6281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6282 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6283 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6284 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6285 }
6286
6287 i = mbedtls_ssl_hs_hdr_len( ssl );
6288
6289 /*
6290 * Same message structure as in mbedtls_ssl_write_certificate()
6291 */
6292 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6293
6294 if( ssl->in_msg[i] != 0 ||
6295 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6296 {
6297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6298 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6299 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6300 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6301 }
6302
6303 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6304 i += 3;
6305
6306 /* Iterate through and parse the CRTs in the provided chain. */
6307 while( i < ssl->in_hslen )
6308 {
6309 /* Check that there's room for the next CRT's length fields. */
6310 if ( i + 3 > ssl->in_hslen ) {
6311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6312 mbedtls_ssl_send_alert_message( ssl,
6313 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6314 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6315 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6316 }
6317 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6318 * anything beyond 2**16 ~ 64K. */
6319 if( ssl->in_msg[i] != 0 )
6320 {
6321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6322 mbedtls_ssl_send_alert_message( ssl,
6323 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6324 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6325 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6326 }
6327
6328 /* Read length of the next CRT in the chain. */
6329 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6330 | (unsigned int) ssl->in_msg[i + 2];
6331 i += 3;
6332
6333 if( n < 128 || i + n > ssl->in_hslen )
6334 {
6335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6336 mbedtls_ssl_send_alert_message( ssl,
6337 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6338 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6339 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6340 }
6341
6342 /* Check if we're handling the first CRT in the chain. */
6343#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6344 if( crt_cnt++ == 0 &&
6345 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6346 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6347 {
6348 /* During client-side renegotiation, check that the server's
6349 * end-CRTs hasn't changed compared to the initial handshake,
6350 * mitigating the triple handshake attack. On success, reuse
6351 * the original end-CRT instead of parsing it again. */
6352 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6353 if( ssl_check_peer_crt_unchanged( ssl,
6354 &ssl->in_msg[i],
6355 n ) != 0 )
6356 {
6357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6358 mbedtls_ssl_send_alert_message( ssl,
6359 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6360 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6361 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6362 }
6363
6364 /* Now we can safely free the original chain. */
6365 ssl_clear_peer_cert( ssl->session );
6366 }
6367#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6368
6369 /* Parse the next certificate in the chain. */
6370#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6371 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6372#else
6373 /* If we don't need to store the CRT chain permanently, parse
6374 * it in-place from the input buffer instead of making a copy. */
6375 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6376#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6377 switch( ret )
6378 {
6379 case 0: /*ok*/
6380 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6381 /* Ignore certificate with an unknown algorithm: maybe a
6382 prior certificate was already trusted. */
6383 break;
6384
6385 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6386 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6387 goto crt_parse_der_failed;
6388
6389 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6390 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6391 goto crt_parse_der_failed;
6392
6393 default:
6394 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6395 crt_parse_der_failed:
6396 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6397 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6398 return( ret );
6399 }
6400
6401 i += n;
6402 }
6403
6404 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6405 return( 0 );
6406}
6407
6408#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006409MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006410static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6411{
6412 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6413 return( -1 );
6414
6415 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6416 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6417 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6418 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6419 {
Ronald Cron19385882022-06-15 16:26:13 +02006420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006421 return( 0 );
6422 }
6423 return( -1 );
6424}
6425#endif /* MBEDTLS_SSL_SRV_C */
6426
6427/* Check if a certificate message is expected.
6428 * Return either
6429 * - SSL_CERTIFICATE_EXPECTED, or
6430 * - SSL_CERTIFICATE_SKIP
6431 * indicating whether a Certificate message is expected or not.
6432 */
6433#define SSL_CERTIFICATE_EXPECTED 0
6434#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006435MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006436static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6437 int authmode )
6438{
6439 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6440 ssl->handshake->ciphersuite_info;
6441
6442 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6443 return( SSL_CERTIFICATE_SKIP );
6444
6445#if defined(MBEDTLS_SSL_SRV_C)
6446 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6447 {
6448 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6449 return( SSL_CERTIFICATE_SKIP );
6450
6451 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6452 {
6453 ssl->session_negotiate->verify_result =
6454 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6455 return( SSL_CERTIFICATE_SKIP );
6456 }
6457 }
6458#else
6459 ((void) authmode);
6460#endif /* MBEDTLS_SSL_SRV_C */
6461
6462 return( SSL_CERTIFICATE_EXPECTED );
6463}
6464
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006465MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006466static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6467 int authmode,
6468 mbedtls_x509_crt *chain,
6469 void *rs_ctx )
6470{
6471 int ret = 0;
6472 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6473 ssl->handshake->ciphersuite_info;
6474 int have_ca_chain = 0;
6475
6476 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6477 void *p_vrfy;
6478
6479 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6480 return( 0 );
6481
6482 if( ssl->f_vrfy != NULL )
6483 {
6484 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6485 f_vrfy = ssl->f_vrfy;
6486 p_vrfy = ssl->p_vrfy;
6487 }
6488 else
6489 {
6490 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6491 f_vrfy = ssl->conf->f_vrfy;
6492 p_vrfy = ssl->conf->p_vrfy;
6493 }
6494
6495 /*
6496 * Main check: verify certificate
6497 */
6498#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6499 if( ssl->conf->f_ca_cb != NULL )
6500 {
6501 ((void) rs_ctx);
6502 have_ca_chain = 1;
6503
6504 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6505 ret = mbedtls_x509_crt_verify_with_ca_cb(
6506 chain,
6507 ssl->conf->f_ca_cb,
6508 ssl->conf->p_ca_cb,
6509 ssl->conf->cert_profile,
6510 ssl->hostname,
6511 &ssl->session_negotiate->verify_result,
6512 f_vrfy, p_vrfy );
6513 }
6514 else
6515#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6516 {
6517 mbedtls_x509_crt *ca_chain;
6518 mbedtls_x509_crl *ca_crl;
6519
6520#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6521 if( ssl->handshake->sni_ca_chain != NULL )
6522 {
6523 ca_chain = ssl->handshake->sni_ca_chain;
6524 ca_crl = ssl->handshake->sni_ca_crl;
6525 }
6526 else
6527#endif
6528 {
6529 ca_chain = ssl->conf->ca_chain;
6530 ca_crl = ssl->conf->ca_crl;
6531 }
6532
6533 if( ca_chain != NULL )
6534 have_ca_chain = 1;
6535
6536 ret = mbedtls_x509_crt_verify_restartable(
6537 chain,
6538 ca_chain, ca_crl,
6539 ssl->conf->cert_profile,
6540 ssl->hostname,
6541 &ssl->session_negotiate->verify_result,
6542 f_vrfy, p_vrfy, rs_ctx );
6543 }
6544
6545 if( ret != 0 )
6546 {
6547 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6548 }
6549
6550#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6551 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6552 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6553#endif
6554
6555 /*
6556 * Secondary checks: always done, but change 'ret' only if it was 0
6557 */
6558
6559#if defined(MBEDTLS_ECP_C)
6560 {
6561 const mbedtls_pk_context *pk = &chain->pk;
6562
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006563 /* If certificate uses an EC key, make sure the curve is OK.
6564 * This is a public key, so it can't be opaque, so can_do() is a good
6565 * enough check to ensure pk_ec() is safe to use here. */
Jerry Yud9526692022-02-17 14:23:47 +08006566 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6567 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6568 {
6569 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6570
6571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6572 if( ret == 0 )
6573 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6574 }
6575 }
6576#endif /* MBEDTLS_ECP_C */
6577
6578 if( mbedtls_ssl_check_cert_usage( chain,
6579 ciphersuite_info,
6580 ! ssl->conf->endpoint,
6581 &ssl->session_negotiate->verify_result ) != 0 )
6582 {
6583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6584 if( ret == 0 )
6585 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6586 }
6587
6588 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6589 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6590 * with details encoded in the verification flags. All other kinds
6591 * of error codes, including those from the user provided f_vrfy
6592 * functions, are treated as fatal and lead to a failure of
6593 * ssl_parse_certificate even if verification was optional. */
6594 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6595 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6596 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6597 {
6598 ret = 0;
6599 }
6600
6601 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6602 {
6603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6604 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6605 }
6606
6607 if( ret != 0 )
6608 {
6609 uint8_t alert;
6610
6611 /* The certificate may have been rejected for several reasons.
6612 Pick one and send the corresponding alert. Which alert to send
6613 may be a subject of debate in some cases. */
6614 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6615 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6616 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6617 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6618 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6619 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6620 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6621 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6622 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6623 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6624 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6625 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6626 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6627 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6628 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6629 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6630 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6631 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6632 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6633 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6634 else
6635 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6636 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6637 alert );
6638 }
6639
6640#if defined(MBEDTLS_DEBUG_C)
6641 if( ssl->session_negotiate->verify_result != 0 )
6642 {
6643 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6644 (unsigned int) ssl->session_negotiate->verify_result ) );
6645 }
6646 else
6647 {
6648 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6649 }
6650#endif /* MBEDTLS_DEBUG_C */
6651
6652 return( ret );
6653}
6654
6655#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006656MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006657static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6658 unsigned char *start, size_t len )
6659{
6660 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6661 /* Remember digest of the peer's end-CRT. */
6662 ssl->session_negotiate->peer_cert_digest =
6663 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6664 if( ssl->session_negotiate->peer_cert_digest == NULL )
6665 {
6666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6667 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6668 mbedtls_ssl_send_alert_message( ssl,
6669 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6670 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6671
6672 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6673 }
6674
6675 ret = mbedtls_md( mbedtls_md_info_from_type(
6676 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6677 start, len,
6678 ssl->session_negotiate->peer_cert_digest );
6679
6680 ssl->session_negotiate->peer_cert_digest_type =
6681 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6682 ssl->session_negotiate->peer_cert_digest_len =
6683 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6684
6685 return( ret );
6686}
6687
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006688MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006689static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6690 unsigned char *start, size_t len )
6691{
6692 unsigned char *end = start + len;
6693 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6694
6695 /* Make a copy of the peer's raw public key. */
6696 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6697 ret = mbedtls_pk_parse_subpubkey( &start, end,
6698 &ssl->handshake->peer_pubkey );
6699 if( ret != 0 )
6700 {
6701 /* We should have parsed the public key before. */
6702 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6703 }
6704
6705 return( 0 );
6706}
6707#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6708
6709int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6710{
6711 int ret = 0;
6712 int crt_expected;
6713#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6714 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6715 ? ssl->handshake->sni_authmode
6716 : ssl->conf->authmode;
6717#else
6718 const int authmode = ssl->conf->authmode;
6719#endif
6720 void *rs_ctx = NULL;
6721 mbedtls_x509_crt *chain = NULL;
6722
6723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6724
6725 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6726 if( crt_expected == SSL_CERTIFICATE_SKIP )
6727 {
6728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6729 goto exit;
6730 }
6731
6732#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6733 if( ssl->handshake->ecrs_enabled &&
6734 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6735 {
6736 chain = ssl->handshake->ecrs_peer_cert;
6737 ssl->handshake->ecrs_peer_cert = NULL;
6738 goto crt_verify;
6739 }
6740#endif
6741
6742 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6743 {
6744 /* mbedtls_ssl_read_record may have sent an alert already. We
6745 let it decide whether to alert. */
6746 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6747 goto exit;
6748 }
6749
6750#if defined(MBEDTLS_SSL_SRV_C)
6751 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6752 {
6753 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6754
6755 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6756 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6757
6758 goto exit;
6759 }
6760#endif /* MBEDTLS_SSL_SRV_C */
6761
6762 /* Clear existing peer CRT structure in case we tried to
6763 * reuse a session but it failed, and allocate a new one. */
6764 ssl_clear_peer_cert( ssl->session_negotiate );
6765
6766 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6767 if( chain == NULL )
6768 {
6769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6770 sizeof( mbedtls_x509_crt ) ) );
6771 mbedtls_ssl_send_alert_message( ssl,
6772 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6773 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6774
6775 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6776 goto exit;
6777 }
6778 mbedtls_x509_crt_init( chain );
6779
6780 ret = ssl_parse_certificate_chain( ssl, chain );
6781 if( ret != 0 )
6782 goto exit;
6783
6784#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6785 if( ssl->handshake->ecrs_enabled)
6786 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6787
6788crt_verify:
6789 if( ssl->handshake->ecrs_enabled)
6790 rs_ctx = &ssl->handshake->ecrs_ctx;
6791#endif
6792
6793 ret = ssl_parse_certificate_verify( ssl, authmode,
6794 chain, rs_ctx );
6795 if( ret != 0 )
6796 goto exit;
6797
6798#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6799 {
6800 unsigned char *crt_start, *pk_start;
6801 size_t crt_len, pk_len;
6802
6803 /* We parse the CRT chain without copying, so
6804 * these pointers point into the input buffer,
6805 * and are hence still valid after freeing the
6806 * CRT chain. */
6807
6808 crt_start = chain->raw.p;
6809 crt_len = chain->raw.len;
6810
6811 pk_start = chain->pk_raw.p;
6812 pk_len = chain->pk_raw.len;
6813
6814 /* Free the CRT structures before computing
6815 * digest and copying the peer's public key. */
6816 mbedtls_x509_crt_free( chain );
6817 mbedtls_free( chain );
6818 chain = NULL;
6819
6820 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6821 if( ret != 0 )
6822 goto exit;
6823
6824 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6825 if( ret != 0 )
6826 goto exit;
6827 }
6828#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6829 /* Pass ownership to session structure. */
6830 ssl->session_negotiate->peer_cert = chain;
6831 chain = NULL;
6832#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6833
6834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6835
6836exit:
6837
6838 if( ret == 0 )
6839 ssl->state++;
6840
6841#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6842 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6843 {
6844 ssl->handshake->ecrs_peer_cert = chain;
6845 chain = NULL;
6846 }
6847#endif
6848
6849 if( chain != NULL )
6850 {
6851 mbedtls_x509_crt_free( chain );
6852 mbedtls_free( chain );
6853 }
6854
6855 return( ret );
6856}
6857#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6858
Jerry Yu615bd6f2022-02-17 14:25:15 +08006859#if defined(MBEDTLS_SHA256_C)
6860static void ssl_calc_finished_tls_sha256(
6861 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6862{
6863 int len = 12;
6864 const char *sender;
6865 unsigned char padbuf[32];
6866#if defined(MBEDTLS_USE_PSA_CRYPTO)
6867 size_t hash_size;
6868 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
6869 psa_status_t status;
6870#else
6871 mbedtls_sha256_context sha256;
6872#endif
6873
6874 mbedtls_ssl_session *session = ssl->session_negotiate;
6875 if( !session )
6876 session = ssl->session;
6877
6878 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6879 ? "client finished"
6880 : "server finished";
6881
6882#if defined(MBEDTLS_USE_PSA_CRYPTO)
6883 sha256_psa = psa_hash_operation_init();
6884
6885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6886
6887 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6888 if( status != PSA_SUCCESS )
6889 {
6890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6891 return;
6892 }
6893
6894 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6895 if( status != PSA_SUCCESS )
6896 {
6897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6898 return;
6899 }
6900 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6901#else
6902
6903 mbedtls_sha256_init( &sha256 );
6904
6905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
6906
6907 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6908
6909 /*
6910 * TLSv1.2:
6911 * hash = PRF( master, finished_label,
6912 * Hash( handshake ) )[0.11]
6913 */
6914
6915#if !defined(MBEDTLS_SHA256_ALT)
6916 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
6917 sha256.state, sizeof( sha256.state ) );
6918#endif
6919
6920 mbedtls_sha256_finish( &sha256, padbuf );
6921 mbedtls_sha256_free( &sha256 );
6922#endif /* MBEDTLS_USE_PSA_CRYPTO */
6923
6924 ssl->handshake->tls_prf( session->master, 48, sender,
6925 padbuf, 32, buf, len );
6926
6927 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6928
6929 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6930
6931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6932}
6933#endif /* MBEDTLS_SHA256_C */
6934
Jerry Yub7ba49e2022-02-17 14:25:53 +08006935
6936#if defined(MBEDTLS_SHA384_C)
6937
6938static void ssl_calc_finished_tls_sha384(
6939 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6940{
6941 int len = 12;
6942 const char *sender;
6943 unsigned char padbuf[48];
6944#if defined(MBEDTLS_USE_PSA_CRYPTO)
6945 size_t hash_size;
6946 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
6947 psa_status_t status;
6948#else
6949 mbedtls_sha512_context sha512;
6950#endif
6951
6952 mbedtls_ssl_session *session = ssl->session_negotiate;
6953 if( !session )
6954 session = ssl->session;
6955
6956 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6957 ? "client finished"
6958 : "server finished";
6959
6960#if defined(MBEDTLS_USE_PSA_CRYPTO)
6961 sha384_psa = psa_hash_operation_init();
6962
6963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6964
6965 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6966 if( status != PSA_SUCCESS )
6967 {
6968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6969 return;
6970 }
6971
6972 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
6973 if( status != PSA_SUCCESS )
6974 {
6975 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6976 return;
6977 }
6978 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
6979#else
6980 mbedtls_sha512_init( &sha512 );
6981
6982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
6983
6984 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
6985
6986 /*
6987 * TLSv1.2:
6988 * hash = PRF( master, finished_label,
6989 * Hash( handshake ) )[0.11]
6990 */
6991
6992#if !defined(MBEDTLS_SHA512_ALT)
6993 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6994 sha512.state, sizeof( sha512.state ) );
6995#endif
6996 mbedtls_sha512_finish( &sha512, padbuf );
6997
6998 mbedtls_sha512_free( &sha512 );
6999#endif
7000
7001 ssl->handshake->tls_prf( session->master, 48, sender,
7002 padbuf, 48, buf, len );
7003
7004 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7005
7006 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7007
7008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7009}
7010#endif /* MBEDTLS_SHA384_C */
7011
Jerry Yuaef00152022-02-17 14:27:31 +08007012void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7013{
7014 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7015
7016 /*
7017 * Free our handshake params
7018 */
7019 mbedtls_ssl_handshake_free( ssl );
7020 mbedtls_free( ssl->handshake );
7021 ssl->handshake = NULL;
7022
7023 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007024 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007025 */
7026 if( ssl->transform )
7027 {
7028 mbedtls_ssl_transform_free( ssl->transform );
7029 mbedtls_free( ssl->transform );
7030 }
7031 ssl->transform = ssl->transform_negotiate;
7032 ssl->transform_negotiate = NULL;
7033
7034 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7035}
7036
Jerry Yu2a9fff52022-02-17 14:28:51 +08007037void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7038{
7039 int resume = ssl->handshake->resume;
7040
7041 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7042
7043#if defined(MBEDTLS_SSL_RENEGOTIATION)
7044 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7045 {
7046 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7047 ssl->renego_records_seen = 0;
7048 }
7049#endif
7050
7051 /*
7052 * Free the previous session and switch in the current one
7053 */
7054 if( ssl->session )
7055 {
7056#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7057 /* RFC 7366 3.1: keep the EtM state */
7058 ssl->session_negotiate->encrypt_then_mac =
7059 ssl->session->encrypt_then_mac;
7060#endif
7061
7062 mbedtls_ssl_session_free( ssl->session );
7063 mbedtls_free( ssl->session );
7064 }
7065 ssl->session = ssl->session_negotiate;
7066 ssl->session_negotiate = NULL;
7067
7068 /*
7069 * Add cache entry
7070 */
7071 if( ssl->conf->f_set_cache != NULL &&
7072 ssl->session->id_len != 0 &&
7073 resume == 0 )
7074 {
7075 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7076 ssl->session->id,
7077 ssl->session->id_len,
7078 ssl->session ) != 0 )
7079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7080 }
7081
7082#if defined(MBEDTLS_SSL_PROTO_DTLS)
7083 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7084 ssl->handshake->flight != NULL )
7085 {
7086 /* Cancel handshake timer */
7087 mbedtls_ssl_set_timer( ssl, 0 );
7088
7089 /* Keep last flight around in case we need to resend it:
7090 * we need the handshake and transform structures for that */
7091 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7092 }
7093 else
7094#endif
7095 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7096
7097 ssl->state++;
7098
7099 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7100}
7101
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007102int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7103{
7104 int ret, hash_len;
7105
7106 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7107
7108 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7109
7110 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7111
7112 /*
7113 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7114 * may define some other value. Currently (early 2016), no defined
7115 * ciphersuite does this (and this is unlikely to change as activity has
7116 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7117 */
7118 hash_len = 12;
7119
7120#if defined(MBEDTLS_SSL_RENEGOTIATION)
7121 ssl->verify_data_len = hash_len;
7122 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7123#endif
7124
7125 ssl->out_msglen = 4 + hash_len;
7126 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7127 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7128
7129 /*
7130 * In case of session resuming, invert the client and server
7131 * ChangeCipherSpec messages order.
7132 */
7133 if( ssl->handshake->resume != 0 )
7134 {
7135#if defined(MBEDTLS_SSL_CLI_C)
7136 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7137 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7138#endif
7139#if defined(MBEDTLS_SSL_SRV_C)
7140 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7141 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7142#endif
7143 }
7144 else
7145 ssl->state++;
7146
7147 /*
7148 * Switch to our negotiated transform and session parameters for outbound
7149 * data.
7150 */
7151 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7152
7153#if defined(MBEDTLS_SSL_PROTO_DTLS)
7154 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7155 {
7156 unsigned char i;
7157
7158 /* Remember current epoch settings for resending */
7159 ssl->handshake->alt_transform_out = ssl->transform_out;
7160 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7161 sizeof( ssl->handshake->alt_out_ctr ) );
7162
7163 /* Set sequence_number to zero */
7164 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7165
7166
7167 /* Increment epoch */
7168 for( i = 2; i > 0; i-- )
7169 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7170 break;
7171
7172 /* The loop goes to its end iff the counter is wrapping */
7173 if( i == 0 )
7174 {
7175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7176 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7177 }
7178 }
7179 else
7180#endif /* MBEDTLS_SSL_PROTO_DTLS */
7181 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7182
7183 ssl->transform_out = ssl->transform_negotiate;
7184 ssl->session_out = ssl->session_negotiate;
7185
7186#if defined(MBEDTLS_SSL_PROTO_DTLS)
7187 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7188 mbedtls_ssl_send_flight_completed( ssl );
7189#endif
7190
7191 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7192 {
7193 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7194 return( ret );
7195 }
7196
7197#if defined(MBEDTLS_SSL_PROTO_DTLS)
7198 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7199 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7200 {
7201 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7202 return( ret );
7203 }
7204#endif
7205
7206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7207
7208 return( 0 );
7209}
7210
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007211#define SSL_MAX_HASH_LEN 12
7212
7213int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7214{
7215 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7216 unsigned int hash_len = 12;
7217 unsigned char buf[SSL_MAX_HASH_LEN];
7218
7219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7220
7221 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7222
7223 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7224 {
7225 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7226 goto exit;
7227 }
7228
7229 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7230 {
7231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7232 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7233 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7234 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7235 goto exit;
7236 }
7237
7238 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7239 {
7240 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7241 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7242 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7243 goto exit;
7244 }
7245
7246 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7247 {
7248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7249 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7250 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7251 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7252 goto exit;
7253 }
7254
7255 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7256 buf, hash_len ) != 0 )
7257 {
7258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7259 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7260 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7261 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7262 goto exit;
7263 }
7264
7265#if defined(MBEDTLS_SSL_RENEGOTIATION)
7266 ssl->verify_data_len = hash_len;
7267 memcpy( ssl->peer_verify_data, buf, hash_len );
7268#endif
7269
7270 if( ssl->handshake->resume != 0 )
7271 {
7272#if defined(MBEDTLS_SSL_CLI_C)
7273 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7274 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7275#endif
7276#if defined(MBEDTLS_SSL_SRV_C)
7277 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7278 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7279#endif
7280 }
7281 else
7282 ssl->state++;
7283
7284#if defined(MBEDTLS_SSL_PROTO_DTLS)
7285 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7286 mbedtls_ssl_recv_flight_completed( ssl );
7287#endif
7288
7289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7290
7291exit:
7292 mbedtls_platform_zeroize( buf, hash_len );
7293 return( ret );
7294}
7295
Jerry Yu392112c2022-02-17 14:34:10 +08007296#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7297/*
7298 * Helper to get TLS 1.2 PRF from ciphersuite
7299 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7300 */
7301static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7302{
7303#if defined(MBEDTLS_SHA384_C)
7304 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7305 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7306
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007307 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007308 return( tls_prf_sha384 );
7309#else
7310 (void) ciphersuite_id;
7311#endif
7312 return( tls_prf_sha256 );
7313}
7314#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007315
7316static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7317{
7318 ((void) tls_prf);
7319#if defined(MBEDTLS_SHA384_C)
7320 if( tls_prf == tls_prf_sha384 )
7321 {
7322 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7323 }
7324 else
7325#endif
7326#if defined(MBEDTLS_SHA256_C)
7327 if( tls_prf == tls_prf_sha256 )
7328 {
7329 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7330 }
7331 else
7332#endif
7333 return( MBEDTLS_SSL_TLS_PRF_NONE );
7334}
7335
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007336/*
7337 * Populate a transform structure with session keys and all the other
7338 * necessary information.
7339 *
7340 * Parameters:
7341 * - [in/out]: transform: structure to populate
7342 * [in] must be just initialised with mbedtls_ssl_transform_init()
7343 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7344 * - [in] ciphersuite
7345 * - [in] master
7346 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007347 * - [in] tls_prf: pointer to PRF to use for key derivation
7348 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007349 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007350 * - [in] endpoint: client or server
7351 * - [in] ssl: used for:
7352 * - ssl->conf->{f,p}_export_keys
7353 * [in] optionally used for:
7354 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7355 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007356MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007357static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7358 int ciphersuite,
7359 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007360#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007361 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007362#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007363 ssl_tls_prf_t tls_prf,
7364 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007365 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007366 unsigned endpoint,
7367 const mbedtls_ssl_context *ssl )
7368{
7369 int ret = 0;
7370 unsigned char keyblk[256];
7371 unsigned char *key1;
7372 unsigned char *key2;
7373 unsigned char *mac_enc;
7374 unsigned char *mac_dec;
7375 size_t mac_key_len = 0;
7376 size_t iv_copy_len;
7377 size_t keylen;
7378 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007379 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007380#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007381 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007382 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007383#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007384
7385#if defined(MBEDTLS_USE_PSA_CRYPTO)
7386 psa_key_type_t key_type;
7387 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7388 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007389 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007390 size_t key_bits;
7391 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7392#endif
7393
7394#if !defined(MBEDTLS_DEBUG_C) && \
7395 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7396 if( ssl->f_export_keys == NULL )
7397 {
7398 ssl = NULL; /* make sure we don't use it except for these cases */
7399 (void) ssl;
7400 }
7401#endif
7402
7403 /*
7404 * Some data just needs copying into the structure
7405 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007406#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007407 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007408#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007409 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007410
7411#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7412 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7413#endif
7414
7415#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007416 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007417 {
7418 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7419 * generation separate. This should never happen. */
7420 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7421 }
7422#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7423
7424 /*
7425 * Get various info structures
7426 */
7427 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7428 if( ciphersuite_info == NULL )
7429 {
7430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7431 ciphersuite ) );
7432 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7433 }
7434
Neil Armstrongab555e02022-04-04 11:07:59 +02007435 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007436#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007437 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007438#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007439 ciphersuite_info );
7440
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007441 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7442 transform->taglen =
7443 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7444
7445#if defined(MBEDTLS_USE_PSA_CRYPTO)
7446 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7447 transform->taglen,
7448 &alg,
7449 &key_type,
7450 &key_bits ) ) != PSA_SUCCESS )
7451 {
7452 ret = psa_ssl_status_to_mbedtls( status );
7453 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7454 goto end;
7455 }
7456#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007457 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7458 if( cipher_info == NULL )
7459 {
7460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7461 ciphersuite_info->cipher ) );
7462 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7463 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007464#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007465
Neil Armstronge4512952022-03-08 09:08:22 +01007466#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007467 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007468 if( mac_alg == 0 )
7469 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007471 (unsigned) ciphersuite_info->mac ) );
7472 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7473 }
7474#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007475 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7476 if( md_info == NULL )
7477 {
7478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7479 (unsigned) ciphersuite_info->mac ) );
7480 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7481 }
Neil Armstronge4512952022-03-08 09:08:22 +01007482#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007483
7484#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7485 /* Copy own and peer's CID if the use of the CID
7486 * extension has been negotiated. */
7487 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7488 {
7489 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7490
7491 transform->in_cid_len = ssl->own_cid_len;
7492 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7493 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7494 transform->in_cid_len );
7495
7496 transform->out_cid_len = ssl->handshake->peer_cid_len;
7497 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7498 ssl->handshake->peer_cid_len );
7499 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7500 transform->out_cid_len );
7501 }
7502#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7503
7504 /*
7505 * Compute key block using the PRF
7506 */
7507 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7508 if( ret != 0 )
7509 {
7510 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7511 return( ret );
7512 }
7513
7514 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7515 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7516 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7517 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7518 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7519
7520 /*
7521 * Determine the appropriate key, IV and MAC length.
7522 */
7523
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007524#if defined(MBEDTLS_USE_PSA_CRYPTO)
7525 keylen = PSA_BITS_TO_BYTES(key_bits);
7526#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007527 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007528#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007529
7530#if defined(MBEDTLS_GCM_C) || \
7531 defined(MBEDTLS_CCM_C) || \
7532 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007533 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007534 {
7535 size_t explicit_ivlen;
7536
7537 transform->maclen = 0;
7538 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007539
7540 /* All modes haves 96-bit IVs, but the length of the static parts vary
7541 * with mode and version:
7542 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7543 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7544 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7545 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7546 * sequence number).
7547 */
7548 transform->ivlen = 12;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007549#if defined(MBEDTLS_USE_PSA_CRYPTO)
7550 if( key_type == PSA_KEY_TYPE_CHACHA20 )
7551#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007552 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007553#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007554 transform->fixed_ivlen = 12;
7555 else
7556 transform->fixed_ivlen = 4;
7557
7558 /* Minimum length of encrypted record */
7559 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7560 transform->minlen = explicit_ivlen + transform->taglen;
7561 }
7562 else
7563#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7564#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007565 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7566 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7567 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007568 {
Neil Armstronge4512952022-03-08 09:08:22 +01007569#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007570 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007571#else
7572 size_t block_size = cipher_info->block_size;
7573#endif /* MBEDTLS_USE_PSA_CRYPTO */
7574
7575#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007576 /* Get MAC length */
7577 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7578#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007579 /* Initialize HMAC contexts */
7580 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7581 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7582 {
7583 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7584 goto end;
7585 }
7586
7587 /* Get MAC length */
7588 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007589#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007590 transform->maclen = mac_key_len;
7591
7592 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007593#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007594 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007595#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007596 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007597#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007598
7599 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007600 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007601 transform->minlen = transform->maclen;
7602 else
7603 {
7604 /*
7605 * GenericBlockCipher:
7606 * 1. if EtM is in use: one block plus MAC
7607 * otherwise: * first multiple of blocklen greater than maclen
7608 * 2. IV
7609 */
7610#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007611 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007612 {
7613 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007614 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007615 }
7616 else
7617#endif
7618 {
7619 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007620 + block_size
7621 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007622 }
7623
Glenn Strauss07c64162022-03-14 12:34:51 -04007624 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007625 {
7626 transform->minlen += transform->ivlen;
7627 }
7628 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007629 {
7630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7631 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7632 goto end;
7633 }
7634 }
7635 }
7636 else
7637#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7638 {
7639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7640 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7641 }
7642
7643 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7644 (unsigned) keylen,
7645 (unsigned) transform->minlen,
7646 (unsigned) transform->ivlen,
7647 (unsigned) transform->maclen ) );
7648
7649 /*
7650 * Finally setup the cipher contexts, IVs and MAC secrets.
7651 */
7652#if defined(MBEDTLS_SSL_CLI_C)
7653 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7654 {
7655 key1 = keyblk + mac_key_len * 2;
7656 key2 = keyblk + mac_key_len * 2 + keylen;
7657
7658 mac_enc = keyblk;
7659 mac_dec = keyblk + mac_key_len;
7660
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007661 iv_copy_len = ( transform->fixed_ivlen ) ?
7662 transform->fixed_ivlen : transform->ivlen;
7663 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7664 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7665 iv_copy_len );
7666 }
7667 else
7668#endif /* MBEDTLS_SSL_CLI_C */
7669#if defined(MBEDTLS_SSL_SRV_C)
7670 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7671 {
7672 key1 = keyblk + mac_key_len * 2 + keylen;
7673 key2 = keyblk + mac_key_len * 2;
7674
7675 mac_enc = keyblk + mac_key_len;
7676 mac_dec = keyblk;
7677
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007678 iv_copy_len = ( transform->fixed_ivlen ) ?
7679 transform->fixed_ivlen : transform->ivlen;
7680 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7681 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7682 iv_copy_len );
7683 }
7684 else
7685#endif /* MBEDTLS_SSL_SRV_C */
7686 {
7687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7688 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7689 goto end;
7690 }
7691
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007692 if( ssl != NULL && ssl->f_export_keys != NULL )
7693 {
7694 ssl->f_export_keys( ssl->p_export_keys,
7695 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7696 master, 48,
7697 randbytes + 32,
7698 randbytes,
7699 tls_prf_get_type( tls_prf ) );
7700 }
7701
7702#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007703 transform->psa_alg = alg;
7704
7705 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7706 {
7707 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7708 psa_set_key_algorithm( &attributes, alg );
7709 psa_set_key_type( &attributes, key_type );
7710
7711 if( ( status = psa_import_key( &attributes,
7712 key1,
7713 PSA_BITS_TO_BYTES( key_bits ),
7714 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7715 {
7716 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7717 ret = psa_ssl_status_to_mbedtls( status );
7718 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7719 goto end;
7720 }
7721
7722 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7723
7724 if( ( status = psa_import_key( &attributes,
7725 key2,
7726 PSA_BITS_TO_BYTES( key_bits ),
7727 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7728 {
7729 ret = psa_ssl_status_to_mbedtls( status );
7730 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7731 goto end;
7732 }
7733 }
7734#else
7735 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7736 cipher_info ) ) != 0 )
7737 {
7738 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7739 goto end;
7740 }
7741
7742 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7743 cipher_info ) ) != 0 )
7744 {
7745 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7746 goto end;
7747 }
7748
7749 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7750 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7751 MBEDTLS_ENCRYPT ) ) != 0 )
7752 {
7753 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7754 goto end;
7755 }
7756
7757 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7758 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7759 MBEDTLS_DECRYPT ) ) != 0 )
7760 {
7761 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7762 goto end;
7763 }
7764
7765#if defined(MBEDTLS_CIPHER_MODE_CBC)
7766 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7767 {
7768 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7769 MBEDTLS_PADDING_NONE ) ) != 0 )
7770 {
7771 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7772 goto end;
7773 }
7774
7775 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7776 MBEDTLS_PADDING_NONE ) ) != 0 )
7777 {
7778 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7779 goto end;
7780 }
7781 }
7782#endif /* MBEDTLS_CIPHER_MODE_CBC */
7783#endif /* MBEDTLS_USE_PSA_CRYPTO */
7784
Neil Armstrong29c0c042022-03-17 17:47:28 +01007785#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7786 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7787 For AEAD-based ciphersuites, there is nothing to do here. */
7788 if( mac_key_len != 0 )
7789 {
7790#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007791 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007792
7793 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007794 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007795 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7796
7797 if( ( status = psa_import_key( &attributes,
7798 mac_enc, mac_key_len,
7799 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7800 {
7801 ret = psa_ssl_status_to_mbedtls( status );
7802 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7803 goto end;
7804 }
7805
Ronald Cronfb39f152022-03-25 14:36:28 +01007806 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
7807 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) &&
7808 ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007809 /* mbedtls_ct_hmac() requires the key to be exportable */
7810 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7811 PSA_KEY_USAGE_VERIFY_HASH );
7812 else
7813 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7814
7815 if( ( status = psa_import_key( &attributes,
7816 mac_dec, mac_key_len,
7817 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7818 {
7819 ret = psa_ssl_status_to_mbedtls( status );
7820 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7821 goto end;
7822 }
7823#else
7824 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7825 if( ret != 0 )
7826 goto end;
7827 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7828 if( ret != 0 )
7829 goto end;
7830#endif /* MBEDTLS_USE_PSA_CRYPTO */
7831 }
7832#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7833
7834 ((void) mac_dec);
7835 ((void) mac_enc);
7836
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007837end:
7838 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7839 return( ret );
7840}
7841
Jerry Yuee40f9d2022-02-17 14:55:16 +08007842#if defined(MBEDTLS_USE_PSA_CRYPTO)
7843int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7844 unsigned char *hash, size_t *hashlen,
7845 unsigned char *data, size_t data_len,
7846 mbedtls_md_type_t md_alg )
7847{
7848 psa_status_t status;
7849 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007850 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08007851
7852 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7853
7854 if( ( status = psa_hash_setup( &hash_operation,
7855 hash_alg ) ) != PSA_SUCCESS )
7856 {
7857 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7858 goto exit;
7859 }
7860
7861 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
7862 64 ) ) != PSA_SUCCESS )
7863 {
7864 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7865 goto exit;
7866 }
7867
7868 if( ( status = psa_hash_update( &hash_operation,
7869 data, data_len ) ) != PSA_SUCCESS )
7870 {
7871 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7872 goto exit;
7873 }
7874
7875 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
7876 hashlen ) ) != PSA_SUCCESS )
7877 {
7878 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
7879 goto exit;
7880 }
7881
7882exit:
7883 if( status != PSA_SUCCESS )
7884 {
7885 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7886 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7887 switch( status )
7888 {
7889 case PSA_ERROR_NOT_SUPPORTED:
7890 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
7891 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
7892 case PSA_ERROR_BUFFER_TOO_SMALL:
7893 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
7894 case PSA_ERROR_INSUFFICIENT_MEMORY:
7895 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
7896 default:
7897 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
7898 }
7899 }
7900 return( 0 );
7901}
7902
7903#else
7904
7905int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7906 unsigned char *hash, size_t *hashlen,
7907 unsigned char *data, size_t data_len,
7908 mbedtls_md_type_t md_alg )
7909{
7910 int ret = 0;
7911 mbedtls_md_context_t ctx;
7912 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
7913 *hashlen = mbedtls_md_get_size( md_info );
7914
7915 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
7916
7917 mbedtls_md_init( &ctx );
7918
7919 /*
7920 * digitally-signed struct {
7921 * opaque client_random[32];
7922 * opaque server_random[32];
7923 * ServerDHParams params;
7924 * };
7925 */
7926 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
7927 {
7928 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7929 goto exit;
7930 }
7931 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
7932 {
7933 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
7934 goto exit;
7935 }
7936 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
7937 {
7938 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7939 goto exit;
7940 }
7941 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
7942 {
7943 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7944 goto exit;
7945 }
7946 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
7947 {
7948 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
7949 goto exit;
7950 }
7951
7952exit:
7953 mbedtls_md_free( &ctx );
7954
7955 if( ret != 0 )
7956 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7957 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7958
7959 return( ret );
7960}
7961#endif /* MBEDTLS_USE_PSA_CRYPTO */
7962
Jerry Yud9d91da2022-02-17 14:57:06 +08007963#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
7964
Gabor Mezeia3d016c2022-05-10 12:44:09 +02007965/* Find the preferred hash for a given signature algorithm. */
7966unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
7967 mbedtls_ssl_context *ssl,
7968 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08007969{
Gabor Mezei078e8032022-04-27 21:17:56 +02007970 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02007971 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02007972
7973 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02007974 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02007975
Gabor Mezeia3d016c2022-05-10 12:44:09 +02007976 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08007977 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02007978 unsigned int hash_alg_received =
7979 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
7980 received_sig_algs[i] );
7981 unsigned int sig_alg_received =
7982 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
7983 received_sig_algs[i] );
7984
7985 if( sig_alg == sig_alg_received )
7986 {
7987#if defined(MBEDTLS_USE_PSA_CRYPTO)
7988 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
7989 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02007990 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007991 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02007992
Neil Armstrong96eceb82022-06-30 18:05:05 +02007993 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
7994 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
7995 PSA_ALG_ECDSA( psa_hash_alg ),
7996 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02007997 continue;
7998
Neil Armstrong96eceb82022-06-30 18:05:05 +02007999 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008000 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8001 PSA_ALG_RSA_PKCS1V15_SIGN(
8002 psa_hash_alg ),
8003 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008004 continue;
8005 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008006#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008007
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008008 return( hash_alg_received );
8009 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008010 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008011
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008012 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008013}
8014
8015#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8016
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008017/* Serialization of TLS 1.2 sessions:
8018 *
8019 * struct {
8020 * uint64 start_time;
8021 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008022 * uint8 session_id_len; // at most 32
8023 * opaque session_id[32];
8024 * opaque master[48]; // fixed length in the standard
8025 * uint32 verify_result;
8026 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8027 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8028 * uint32 ticket_lifetime;
8029 * uint8 mfl_code; // up to 255 according to standard
8030 * uint8 encrypt_then_mac; // 0 or 1
8031 * } serialized_session_tls12;
8032 *
8033 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008034static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008035 unsigned char *buf,
8036 size_t buf_len )
8037{
8038 unsigned char *p = buf;
8039 size_t used = 0;
8040
8041#if defined(MBEDTLS_HAVE_TIME)
8042 uint64_t start;
8043#endif
8044#if defined(MBEDTLS_X509_CRT_PARSE_C)
8045#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8046 size_t cert_len;
8047#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8048#endif /* MBEDTLS_X509_CRT_PARSE_C */
8049
8050 /*
8051 * Time
8052 */
8053#if defined(MBEDTLS_HAVE_TIME)
8054 used += 8;
8055
8056 if( used <= buf_len )
8057 {
8058 start = (uint64_t) session->start;
8059
8060 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8061 p += 8;
8062 }
8063#endif /* MBEDTLS_HAVE_TIME */
8064
8065 /*
8066 * Basic mandatory fields
8067 */
8068 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008069 + 1 /* id_len */
8070 + sizeof( session->id )
8071 + sizeof( session->master )
8072 + 4; /* verify_result */
8073
8074 if( used <= buf_len )
8075 {
8076 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8077 p += 2;
8078
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008079 *p++ = MBEDTLS_BYTE_0( session->id_len );
8080 memcpy( p, session->id, 32 );
8081 p += 32;
8082
8083 memcpy( p, session->master, 48 );
8084 p += 48;
8085
8086 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8087 p += 4;
8088 }
8089
8090 /*
8091 * Peer's end-entity certificate
8092 */
8093#if defined(MBEDTLS_X509_CRT_PARSE_C)
8094#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8095 if( session->peer_cert == NULL )
8096 cert_len = 0;
8097 else
8098 cert_len = session->peer_cert->raw.len;
8099
8100 used += 3 + cert_len;
8101
8102 if( used <= buf_len )
8103 {
8104 *p++ = MBEDTLS_BYTE_2( cert_len );
8105 *p++ = MBEDTLS_BYTE_1( cert_len );
8106 *p++ = MBEDTLS_BYTE_0( cert_len );
8107
8108 if( session->peer_cert != NULL )
8109 {
8110 memcpy( p, session->peer_cert->raw.p, cert_len );
8111 p += cert_len;
8112 }
8113 }
8114#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8115 if( session->peer_cert_digest != NULL )
8116 {
8117 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8118 if( used <= buf_len )
8119 {
8120 *p++ = (unsigned char) session->peer_cert_digest_type;
8121 *p++ = (unsigned char) session->peer_cert_digest_len;
8122 memcpy( p, session->peer_cert_digest,
8123 session->peer_cert_digest_len );
8124 p += session->peer_cert_digest_len;
8125 }
8126 }
8127 else
8128 {
8129 used += 2;
8130 if( used <= buf_len )
8131 {
8132 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8133 *p++ = 0;
8134 }
8135 }
8136#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8137#endif /* MBEDTLS_X509_CRT_PARSE_C */
8138
8139 /*
8140 * Session ticket if any, plus associated data
8141 */
8142#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8143 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8144
8145 if( used <= buf_len )
8146 {
8147 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8148 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8149 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8150
8151 if( session->ticket != NULL )
8152 {
8153 memcpy( p, session->ticket, session->ticket_len );
8154 p += session->ticket_len;
8155 }
8156
8157 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8158 p += 4;
8159 }
8160#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8161
8162 /*
8163 * Misc extension-related info
8164 */
8165#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8166 used += 1;
8167
8168 if( used <= buf_len )
8169 *p++ = session->mfl_code;
8170#endif
8171
8172#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8173 used += 1;
8174
8175 if( used <= buf_len )
8176 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8177#endif
8178
8179 return( used );
8180}
8181
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008182MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008183static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008184 const unsigned char *buf,
8185 size_t len )
8186{
8187#if defined(MBEDTLS_HAVE_TIME)
8188 uint64_t start;
8189#endif
8190#if defined(MBEDTLS_X509_CRT_PARSE_C)
8191#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8192 size_t cert_len;
8193#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8194#endif /* MBEDTLS_X509_CRT_PARSE_C */
8195
8196 const unsigned char *p = buf;
8197 const unsigned char * const end = buf + len;
8198
8199 /*
8200 * Time
8201 */
8202#if defined(MBEDTLS_HAVE_TIME)
8203 if( 8 > (size_t)( end - p ) )
8204 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8205
8206 start = ( (uint64_t) p[0] << 56 ) |
8207 ( (uint64_t) p[1] << 48 ) |
8208 ( (uint64_t) p[2] << 40 ) |
8209 ( (uint64_t) p[3] << 32 ) |
8210 ( (uint64_t) p[4] << 24 ) |
8211 ( (uint64_t) p[5] << 16 ) |
8212 ( (uint64_t) p[6] << 8 ) |
8213 ( (uint64_t) p[7] );
8214 p += 8;
8215
8216 session->start = (time_t) start;
8217#endif /* MBEDTLS_HAVE_TIME */
8218
8219 /*
8220 * Basic mandatory fields
8221 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008222 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008223 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8224
8225 session->ciphersuite = ( p[0] << 8 ) | p[1];
8226 p += 2;
8227
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008228 session->id_len = *p++;
8229 memcpy( session->id, p, 32 );
8230 p += 32;
8231
8232 memcpy( session->master, p, 48 );
8233 p += 48;
8234
8235 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8236 ( (uint32_t) p[1] << 16 ) |
8237 ( (uint32_t) p[2] << 8 ) |
8238 ( (uint32_t) p[3] );
8239 p += 4;
8240
8241 /* Immediately clear invalid pointer values that have been read, in case
8242 * we exit early before we replaced them with valid ones. */
8243#if defined(MBEDTLS_X509_CRT_PARSE_C)
8244#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8245 session->peer_cert = NULL;
8246#else
8247 session->peer_cert_digest = NULL;
8248#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8249#endif /* MBEDTLS_X509_CRT_PARSE_C */
8250#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8251 session->ticket = NULL;
8252#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8253
8254 /*
8255 * Peer certificate
8256 */
8257#if defined(MBEDTLS_X509_CRT_PARSE_C)
8258#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8259 /* Deserialize CRT from the end of the ticket. */
8260 if( 3 > (size_t)( end - p ) )
8261 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8262
8263 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8264 p += 3;
8265
8266 if( cert_len != 0 )
8267 {
8268 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8269
8270 if( cert_len > (size_t)( end - p ) )
8271 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8272
8273 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8274
8275 if( session->peer_cert == NULL )
8276 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8277
8278 mbedtls_x509_crt_init( session->peer_cert );
8279
8280 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8281 p, cert_len ) ) != 0 )
8282 {
8283 mbedtls_x509_crt_free( session->peer_cert );
8284 mbedtls_free( session->peer_cert );
8285 session->peer_cert = NULL;
8286 return( ret );
8287 }
8288
8289 p += cert_len;
8290 }
8291#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8292 /* Deserialize CRT digest from the end of the ticket. */
8293 if( 2 > (size_t)( end - p ) )
8294 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8295
8296 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8297 session->peer_cert_digest_len = (size_t) *p++;
8298
8299 if( session->peer_cert_digest_len != 0 )
8300 {
8301 const mbedtls_md_info_t *md_info =
8302 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8303 if( md_info == NULL )
8304 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8305 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8306 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8307
8308 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8309 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8310
8311 session->peer_cert_digest =
8312 mbedtls_calloc( 1, session->peer_cert_digest_len );
8313 if( session->peer_cert_digest == NULL )
8314 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8315
8316 memcpy( session->peer_cert_digest, p,
8317 session->peer_cert_digest_len );
8318 p += session->peer_cert_digest_len;
8319 }
8320#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8321#endif /* MBEDTLS_X509_CRT_PARSE_C */
8322
8323 /*
8324 * Session ticket and associated data
8325 */
8326#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8327 if( 3 > (size_t)( end - p ) )
8328 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8329
8330 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8331 p += 3;
8332
8333 if( session->ticket_len != 0 )
8334 {
8335 if( session->ticket_len > (size_t)( end - p ) )
8336 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8337
8338 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8339 if( session->ticket == NULL )
8340 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8341
8342 memcpy( session->ticket, p, session->ticket_len );
8343 p += session->ticket_len;
8344 }
8345
8346 if( 4 > (size_t)( end - p ) )
8347 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8348
8349 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8350 ( (uint32_t) p[1] << 16 ) |
8351 ( (uint32_t) p[2] << 8 ) |
8352 ( (uint32_t) p[3] );
8353 p += 4;
8354#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8355
8356 /*
8357 * Misc extension-related info
8358 */
8359#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8360 if( 1 > (size_t)( end - p ) )
8361 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8362
8363 session->mfl_code = *p++;
8364#endif
8365
8366#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8367 if( 1 > (size_t)( end - p ) )
8368 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8369
8370 session->encrypt_then_mac = *p++;
8371#endif
8372
8373 /* Done, should have consumed entire buffer */
8374 if( p != end )
8375 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8376
8377 return( 0 );
8378}
Jerry Yudc7bd172022-02-17 13:44:15 +08008379#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8380
XiaokangQian75d40ef2022-04-20 11:05:24 +00008381int mbedtls_ssl_validate_ciphersuite(
8382 const mbedtls_ssl_context *ssl,
8383 const mbedtls_ssl_ciphersuite_t *suite_info,
8384 mbedtls_ssl_protocol_version min_tls_version,
8385 mbedtls_ssl_protocol_version max_tls_version )
8386{
8387 (void) ssl;
8388
8389 if( suite_info == NULL )
8390 return( -1 );
8391
8392 if( ( suite_info->min_tls_version > max_tls_version ) ||
8393 ( suite_info->max_tls_version < min_tls_version ) )
8394 {
8395 return( -1 );
8396 }
8397
XiaokangQian060d8672022-04-21 09:24:56 +00008398#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008399#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8400 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8401 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8402 {
8403 return( -1 );
8404 }
8405#endif
8406
8407 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8408#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8409 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8410 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8411 {
8412 return( -1 );
8413 }
8414#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8415#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8416
8417 return( 0 );
8418}
8419
XiaokangQianeaf36512022-04-24 09:07:44 +00008420#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8421/*
8422 * Function for writing a signature algorithm extension.
8423 *
8424 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8425 * value (TLS 1.3 RFC8446):
8426 * enum {
8427 * ....
8428 * ecdsa_secp256r1_sha256( 0x0403 ),
8429 * ecdsa_secp384r1_sha384( 0x0503 ),
8430 * ecdsa_secp521r1_sha512( 0x0603 ),
8431 * ....
8432 * } SignatureScheme;
8433 *
8434 * struct {
8435 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8436 * } SignatureSchemeList;
8437 *
8438 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8439 * value (TLS 1.2 RFC5246):
8440 * enum {
8441 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8442 * sha512(6), (255)
8443 * } HashAlgorithm;
8444 *
8445 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8446 * SignatureAlgorithm;
8447 *
8448 * struct {
8449 * HashAlgorithm hash;
8450 * SignatureAlgorithm signature;
8451 * } SignatureAndHashAlgorithm;
8452 *
8453 * SignatureAndHashAlgorithm
8454 * supported_signature_algorithms<2..2^16-2>;
8455 *
8456 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8457 * generalization of the TLS 1.2 signature algorithm extension.
8458 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8459 * `SignatureScheme` field of TLS 1.3
8460 *
8461 */
8462int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8463 const unsigned char *end, size_t *out_len )
8464{
8465 unsigned char *p = buf;
8466 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8467 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8468
8469 *out_len = 0;
8470
8471 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8472
8473 /* Check if we have space for header and length field:
8474 * - extension_type (2 bytes)
8475 * - extension_data_length (2 bytes)
8476 * - supported_signature_algorithms_length (2 bytes)
8477 */
8478 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8479 p += 6;
8480
8481 /*
8482 * Write supported_signature_algorithms
8483 */
8484 supported_sig_alg = p;
8485 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8486 if( sig_alg == NULL )
8487 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8488
8489 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8490 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008491 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8492 *sig_alg,
8493 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008494 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8495 continue;
8496 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8497 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8498 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008499 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008500 *sig_alg,
8501 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008502 }
8503
8504 /* Length of supported_signature_algorithms */
8505 supported_sig_alg_len = p - supported_sig_alg;
8506 if( supported_sig_alg_len == 0 )
8507 {
8508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8509 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8510 }
8511
XiaokangQianeaf36512022-04-24 09:07:44 +00008512 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008513 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008514 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8515
XiaokangQianeaf36512022-04-24 09:07:44 +00008516 *out_len = p - buf;
8517
8518#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8519 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8520#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8521 return( 0 );
8522}
8523#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8524
XiaokangQian40a35232022-05-07 09:02:40 +00008525#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008526/*
8527 * mbedtls_ssl_parse_server_name_ext
8528 *
8529 * Structure of server_name extension:
8530 *
8531 * enum {
8532 * host_name(0), (255)
8533 * } NameType;
8534 * opaque HostName<1..2^16-1>;
8535 *
8536 * struct {
8537 * NameType name_type;
8538 * select (name_type) {
8539 * case host_name: HostName;
8540 * } name;
8541 * } ServerName;
8542 * struct {
8543 * ServerName server_name_list<1..2^16-1>
8544 * } ServerNameList;
8545 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008546MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008547int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8548 const unsigned char *buf,
8549 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008550{
8551 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8552 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008553 size_t server_name_list_len, hostname_len;
8554 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008555
XiaokangQianf2a94202022-05-20 06:44:24 +00008556 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008557
8558 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008559 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008560 p += 2;
8561
XiaokangQian9b2b7712022-05-17 02:57:00 +00008562 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8563 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008564 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008565 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008566 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008567 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008568 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8569 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008570
8571 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8572 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008573 /* sni_name is intended to be used only during the parsing of the
8574 * ClientHello message (it is reset to NULL before the end of
8575 * the message parsing). Thus it is ok to just point to the
8576 * reception buffer and not make a copy of it.
8577 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008578 ssl->handshake->sni_name = p + 3;
8579 ssl->handshake->sni_name_len = hostname_len;
8580 if( ssl->conf->f_sni == NULL )
8581 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008582 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008583 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008584 if( ret != 0 )
8585 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008586 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008587 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8588 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008589 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8590 }
8591 return( 0 );
8592 }
8593
8594 p += hostname_len + 3;
8595 }
8596
8597 return( 0 );
8598}
8599#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8600
XiaokangQianacb39922022-06-17 10:18:48 +00008601#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008602MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008603int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8604 const unsigned char *buf,
8605 const unsigned char *end )
8606{
8607 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008608 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008609 const unsigned char *protocol_name_list;
8610 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008611 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008612
8613 /* If ALPN not configured, just ignore the extension */
8614 if( ssl->conf->alpn_list == NULL )
8615 return( 0 );
8616
8617 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008618 * RFC7301, section 3.1
8619 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008620 *
XiaokangQianc7403452022-06-23 03:24:12 +00008621 * struct {
8622 * ProtocolName protocol_name_list<2..2^16-1>
8623 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008624 */
8625
XiaokangQianc7403452022-06-23 03:24:12 +00008626 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008627 * protocol_name_list_len 2 bytes
8628 * protocol_name_len 1 bytes
8629 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008630 */
XiaokangQianacb39922022-06-17 10:18:48 +00008631 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8632
XiaokangQianc7403452022-06-23 03:24:12 +00008633 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008634 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008635 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008636 protocol_name_list = p;
8637 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008638
8639 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008640 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008641 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008642 protocol_name_len = *p++;
8643 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8644 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008645 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008646 {
8647 MBEDTLS_SSL_PEND_FATAL_ALERT(
8648 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8649 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008650 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008651 }
8652
8653 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008654 }
8655
8656 /* Use our order of preference */
8657 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8658 {
8659 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008660 p = protocol_name_list;
8661 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008662 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008663 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008664 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008665 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008666 {
8667 ssl->alpn_chosen = *alpn;
8668 return( 0 );
8669 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008670
8671 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008672 }
8673 }
8674
XiaokangQian95d5f542022-06-24 02:29:26 +00008675 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008676 MBEDTLS_SSL_PEND_FATAL_ALERT(
8677 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8678 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8679 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8680}
8681
8682int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8683 unsigned char *buf,
8684 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008685 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008686{
8687 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008688 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008689 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008690
8691 if( ssl->alpn_chosen == NULL )
8692 {
8693 return( 0 );
8694 }
8695
XiaokangQian95d5f542022-06-24 02:29:26 +00008696 protocol_name_len = strlen( ssl->alpn_chosen );
8697 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008698
8699 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8700 /*
8701 * 0 . 1 ext identifier
8702 * 2 . 3 ext length
8703 * 4 . 5 protocol list length
8704 * 6 . 6 protocol name length
8705 * 7 . 7+n protocol name
8706 */
8707 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8708
XiaokangQian95d5f542022-06-24 02:29:26 +00008709 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008710
XiaokangQian95d5f542022-06-24 02:29:26 +00008711 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8712 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008713 /* Note: the length of the chosen protocol has been checked to be less
8714 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8715 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008716 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008717
XiaokangQian95d5f542022-06-24 02:29:26 +00008718 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008719 return ( 0 );
8720}
8721#endif /* MBEDTLS_SSL_ALPN */
8722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008723#endif /* MBEDTLS_SSL_TLS_C */