blob: fe38a0939ac1390012c55892ab722f9c4ccbdceb [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;
Jerry Yuccc68a42022-07-26 16:39:20 +08001696 psa_algorithm_t alg = PSA_ALG_ANY_HASH;
Neil Armstrong501c9322022-05-03 09:35:09 +02001697 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)
Jerry Yuccc68a42022-07-26 16:39:20 +08001709#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1710 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1711 {
1712 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384)
1713 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1714 else
1715 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1716 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1717 }
1718#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001719
Jerry Yu568ec252022-07-22 21:27:34 +08001720#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001721 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1722 {
1723 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1724 psa_set_key_usage_flags( &key_attributes,
1725 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1726 }
1727#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1728
Neil Armstrong501c9322022-05-03 09:35:09 +02001729 psa_set_key_algorithm( &key_attributes, alg );
1730 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1731
1732 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1733 if( status != PSA_SUCCESS )
1734 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1735
1736 /* Allow calling psa_destroy_key() on psk remove */
1737 ssl->handshake->psk_opaque_is_internal = 1;
1738 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1739#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001740 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001741 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001742
1743 ssl->handshake->psk_len = psk_len;
1744 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1745
1746 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001747#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001748}
1749
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001750#if defined(MBEDTLS_USE_PSA_CRYPTO)
1751int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001752 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001753 const unsigned char *psk_identity,
1754 size_t psk_identity_len )
1755{
Janos Follath865b3eb2019-12-16 11:46:15 +00001756 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001757
1758 /* We currently only support one PSK, raw or opaque. */
1759 if( ssl_conf_psk_is_configured( conf ) )
1760 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001761
Hanno Becker7390c712018-11-15 13:33:04 +00001762 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001763 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001764 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001765 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001766
1767 /* Check and set PSK Identity */
1768 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1769 psk_identity_len );
1770 if( ret != 0 )
1771 ssl_conf_remove_psk( conf );
1772
1773 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001774}
1775
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001776int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1777 mbedtls_svc_key_id_t psk )
1778{
1779 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1780 ( ssl->handshake == NULL ) )
1781 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1782
1783 ssl_remove_psk( ssl );
1784 ssl->handshake->psk_opaque = psk;
1785 return( 0 );
1786}
1787#endif /* MBEDTLS_USE_PSA_CRYPTO */
1788
1789void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1790 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1791 size_t),
1792 void *p_psk )
1793{
1794 conf->f_psk = f_psk;
1795 conf->p_psk = p_psk;
1796}
1797#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1798
1799#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001800static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1801 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001802{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001803#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001804 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001805 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001806#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001807 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001808 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001809 return( MBEDTLS_SSL_MODE_STREAM );
1810}
1811
1812#else /* MBEDTLS_USE_PSA_CRYPTO */
1813
1814static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1815 mbedtls_cipher_mode_t mode )
1816{
1817#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1818 if( mode == MBEDTLS_MODE_CBC )
1819 return( MBEDTLS_SSL_MODE_CBC );
1820#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1821
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001822#if defined(MBEDTLS_GCM_C) || \
1823 defined(MBEDTLS_CCM_C) || \
1824 defined(MBEDTLS_CHACHAPOLY_C)
1825 if( mode == MBEDTLS_MODE_GCM ||
1826 mode == MBEDTLS_MODE_CCM ||
1827 mode == MBEDTLS_MODE_CHACHAPOLY )
1828 return( MBEDTLS_SSL_MODE_AEAD );
1829#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001830
1831 return( MBEDTLS_SSL_MODE_STREAM );
1832}
Gilles Peskine301711e2022-04-26 16:57:05 +02001833#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001834
Gilles Peskinee108d982022-04-26 16:50:40 +02001835static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1836 mbedtls_ssl_mode_t base_mode,
1837 int encrypt_then_mac )
1838{
1839#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1840 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1841 base_mode == MBEDTLS_SSL_MODE_CBC )
1842 {
1843 return( MBEDTLS_SSL_MODE_CBC_ETM );
1844 }
1845#else
1846 (void) encrypt_then_mac;
1847#endif
1848 return( base_mode );
1849}
1850
Neil Armstrongab555e02022-04-04 11:07:59 +02001851mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001852 const mbedtls_ssl_transform *transform )
1853{
Gilles Peskinee108d982022-04-26 16:50:40 +02001854 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001855#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001856 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001857#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001858 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1859#endif
1860 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001861
Gilles Peskinee108d982022-04-26 16:50:40 +02001862 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001863#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001864 encrypt_then_mac = transform->encrypt_then_mac;
1865#endif
1866 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001867}
1868
Neil Armstrongab555e02022-04-04 11:07:59 +02001869mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001870#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001871 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001872#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001873 const mbedtls_ssl_ciphersuite_t *suite )
1874{
Gilles Peskinee108d982022-04-26 16:50:40 +02001875 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1876
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001877#if defined(MBEDTLS_USE_PSA_CRYPTO)
1878 psa_status_t status;
1879 psa_algorithm_t alg;
1880 psa_key_type_t type;
1881 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001882 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1883 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001884 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001885#else
1886 const mbedtls_cipher_info_t *cipher =
1887 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001888 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001889 {
1890 base_mode =
1891 mbedtls_ssl_get_base_mode(
1892 mbedtls_cipher_info_get_mode( cipher ) );
1893 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001894#endif /* MBEDTLS_USE_PSA_CRYPTO */
1895
Gilles Peskinee108d982022-04-26 16:50:40 +02001896#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1897 int encrypt_then_mac = 0;
1898#endif
1899 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001900}
1901
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001902#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001903
1904#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001905/* Serialization of TLS 1.3 sessions:
1906 *
1907 * struct {
1908 * uint64 ticket_received;
1909 * uint32 ticket_lifetime;
1910 * opaque ticket<0..2^16>;
1911 * } ClientOnlyData;
1912 *
1913 * struct {
1914 * uint8 endpoint;
1915 * uint8 ciphersuite[2];
1916 * uint32 ticket_age_add;
1917 * uint8 ticket_flags;
1918 * opaque resumption_key<0..255>;
1919 * select ( endpoint ) {
1920 * case client: ClientOnlyData;
1921 * case server: uint64 start_time;
1922 * };
1923 * } serialized_session_tls13;
1924 *
1925 */
1926#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1927static size_t ssl_tls13_session_save( const mbedtls_ssl_session *session,
1928 unsigned char *buf,
1929 size_t buf_len )
1930{
1931 unsigned char *p = buf;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001932 size_t needed = 1 /* endpoint */
1933 + 2 /* ciphersuite */
1934 + 4 /* ticket_age_add */
1935 + 2 /* resumption_key length */
1936 + session->resumption_key_len; /* resumption_key */
Jerry Yu438ddd82022-07-07 06:55:50 +00001937
1938#if defined(MBEDTLS_HAVE_TIME)
1939 needed += 8; /* start_time or ticket_received */
1940#endif
1941
1942#if defined(MBEDTLS_SSL_CLI_C)
1943 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1944 {
1945 needed += 4 /* ticket_lifetime */
1946 + 2 /* ticket_len */
1947 + session->ticket_len; /* ticket */
1948 }
1949#endif /* MBEDTLS_SSL_CLI_C */
1950
1951 if( needed > buf_len )
1952 return( needed );
1953
1954 p[0] = session->endpoint;
1955 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
1956 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
1957 p[7] = session->ticket_flags;
1958
1959 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08001960 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00001961 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001962 memcpy( p, session->resumption_key, session->resumption_key_len );
1963 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00001964
1965#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
1966 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
1967 {
1968 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
1969 p += 8;
1970 }
1971#endif /* MBEDTLS_HAVE_TIME */
1972
1973#if defined(MBEDTLS_SSL_CLI_C)
1974 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1975 {
1976#if defined(MBEDTLS_HAVE_TIME)
1977 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
1978 p += 8;
1979#endif
1980 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
1981 p += 4;
1982
1983 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
1984 p += 2;
1985 if( session->ticket_len > 0 )
1986 {
1987 memcpy( p, session->ticket, session->ticket_len );
1988 p += session->ticket_len;
1989 }
1990 }
1991#endif /* MBEDTLS_SSL_CLI_C */
1992 return( needed );
1993}
1994
1995MBEDTLS_CHECK_RETURN_CRITICAL
1996static int ssl_tls13_session_load( mbedtls_ssl_session *session,
1997 const unsigned char *buf,
1998 size_t len )
1999{
2000 const unsigned char *p = buf;
2001 const unsigned char *end = buf + len;
2002
2003 if( end - p < 9 )
2004 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2005 session->endpoint = p[0];
2006 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2007 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2008 session->ticket_flags = p[7];
2009
2010 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002011 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002012 p += 9;
2013
Jerry Yubc7c1a42022-07-21 22:57:37 +08002014 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002015 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2016
Jerry Yubc7c1a42022-07-21 22:57:37 +08002017 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002018 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002019 memcpy( session->resumption_key, p, session->resumption_key_len );
2020 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002021
2022#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2023 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2024 {
2025 if( end - p < 8 )
2026 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2027 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2028 p += 8;
2029 }
2030#endif /* MBEDTLS_HAVE_TIME */
2031
2032#if defined(MBEDTLS_SSL_CLI_C)
2033 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2034 {
2035#if defined(MBEDTLS_HAVE_TIME)
2036 if( end - p < 8 )
2037 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2038 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2039 p += 8;
2040#endif
2041 if( end - p < 4 )
2042 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2043 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2044 p += 4;
2045
2046 if( end - p < 2 )
2047 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2048 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2049 p += 2;
2050
2051 if( end - p < ( long int )session->ticket_len )
2052 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2053 if( session->ticket_len > 0 )
2054 {
2055 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2056 if( session->ticket == NULL )
2057 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2058 memcpy( session->ticket, p, session->ticket_len );
2059 p += session->ticket_len;
2060 }
2061 }
2062#endif /* MBEDTLS_SSL_CLI_C */
2063
2064 return( 0 );
2065
2066}
2067#else /* MBEDTLS_SSL_SESSION_TICKETS */
2068static size_t ssl_tls13_session_save( const mbedtls_ssl_session *session,
Jerry Yu251a12e2022-07-13 15:15:48 +08002069 unsigned char *buf,
2070 size_t buf_len )
2071{
2072 ((void) session);
2073 ((void) buf);
2074 ((void) buf_len);
2075 return( 0 );
2076}
Jerry Yu438ddd82022-07-07 06:55:50 +00002077
2078static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2079 unsigned char *buf,
2080 size_t buf_len )
2081{
2082 ((void) session);
2083 ((void) buf);
2084 ((void) buf_len);
2085 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2086}
2087#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002088#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2089
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002090psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002091 size_t taglen,
2092 psa_algorithm_t *alg,
2093 psa_key_type_t *key_type,
2094 size_t *key_size )
2095{
2096 switch ( mbedtls_cipher_type )
2097 {
2098 case MBEDTLS_CIPHER_AES_128_CBC:
2099 *alg = PSA_ALG_CBC_NO_PADDING;
2100 *key_type = PSA_KEY_TYPE_AES;
2101 *key_size = 128;
2102 break;
2103 case MBEDTLS_CIPHER_AES_128_CCM:
2104 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2105 *key_type = PSA_KEY_TYPE_AES;
2106 *key_size = 128;
2107 break;
2108 case MBEDTLS_CIPHER_AES_128_GCM:
2109 *alg = PSA_ALG_GCM;
2110 *key_type = PSA_KEY_TYPE_AES;
2111 *key_size = 128;
2112 break;
2113 case MBEDTLS_CIPHER_AES_192_CCM:
2114 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2115 *key_type = PSA_KEY_TYPE_AES;
2116 *key_size = 192;
2117 break;
2118 case MBEDTLS_CIPHER_AES_192_GCM:
2119 *alg = PSA_ALG_GCM;
2120 *key_type = PSA_KEY_TYPE_AES;
2121 *key_size = 192;
2122 break;
2123 case MBEDTLS_CIPHER_AES_256_CBC:
2124 *alg = PSA_ALG_CBC_NO_PADDING;
2125 *key_type = PSA_KEY_TYPE_AES;
2126 *key_size = 256;
2127 break;
2128 case MBEDTLS_CIPHER_AES_256_CCM:
2129 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2130 *key_type = PSA_KEY_TYPE_AES;
2131 *key_size = 256;
2132 break;
2133 case MBEDTLS_CIPHER_AES_256_GCM:
2134 *alg = PSA_ALG_GCM;
2135 *key_type = PSA_KEY_TYPE_AES;
2136 *key_size = 256;
2137 break;
2138 case MBEDTLS_CIPHER_ARIA_128_CBC:
2139 *alg = PSA_ALG_CBC_NO_PADDING;
2140 *key_type = PSA_KEY_TYPE_ARIA;
2141 *key_size = 128;
2142 break;
2143 case MBEDTLS_CIPHER_ARIA_128_CCM:
2144 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2145 *key_type = PSA_KEY_TYPE_ARIA;
2146 *key_size = 128;
2147 break;
2148 case MBEDTLS_CIPHER_ARIA_128_GCM:
2149 *alg = PSA_ALG_GCM;
2150 *key_type = PSA_KEY_TYPE_ARIA;
2151 *key_size = 128;
2152 break;
2153 case MBEDTLS_CIPHER_ARIA_192_CCM:
2154 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2155 *key_type = PSA_KEY_TYPE_ARIA;
2156 *key_size = 192;
2157 break;
2158 case MBEDTLS_CIPHER_ARIA_192_GCM:
2159 *alg = PSA_ALG_GCM;
2160 *key_type = PSA_KEY_TYPE_ARIA;
2161 *key_size = 192;
2162 break;
2163 case MBEDTLS_CIPHER_ARIA_256_CBC:
2164 *alg = PSA_ALG_CBC_NO_PADDING;
2165 *key_type = PSA_KEY_TYPE_ARIA;
2166 *key_size = 256;
2167 break;
2168 case MBEDTLS_CIPHER_ARIA_256_CCM:
2169 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2170 *key_type = PSA_KEY_TYPE_ARIA;
2171 *key_size = 256;
2172 break;
2173 case MBEDTLS_CIPHER_ARIA_256_GCM:
2174 *alg = PSA_ALG_GCM;
2175 *key_type = PSA_KEY_TYPE_ARIA;
2176 *key_size = 256;
2177 break;
2178 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2179 *alg = PSA_ALG_CBC_NO_PADDING;
2180 *key_type = PSA_KEY_TYPE_CAMELLIA;
2181 *key_size = 128;
2182 break;
2183 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2184 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2185 *key_type = PSA_KEY_TYPE_CAMELLIA;
2186 *key_size = 128;
2187 break;
2188 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2189 *alg = PSA_ALG_GCM;
2190 *key_type = PSA_KEY_TYPE_CAMELLIA;
2191 *key_size = 128;
2192 break;
2193 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2194 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2195 *key_type = PSA_KEY_TYPE_CAMELLIA;
2196 *key_size = 192;
2197 break;
2198 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2199 *alg = PSA_ALG_GCM;
2200 *key_type = PSA_KEY_TYPE_CAMELLIA;
2201 *key_size = 192;
2202 break;
2203 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2204 *alg = PSA_ALG_CBC_NO_PADDING;
2205 *key_type = PSA_KEY_TYPE_CAMELLIA;
2206 *key_size = 256;
2207 break;
2208 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2209 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2210 *key_type = PSA_KEY_TYPE_CAMELLIA;
2211 *key_size = 256;
2212 break;
2213 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2214 *alg = PSA_ALG_GCM;
2215 *key_type = PSA_KEY_TYPE_CAMELLIA;
2216 *key_size = 256;
2217 break;
2218 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2219 *alg = PSA_ALG_CHACHA20_POLY1305;
2220 *key_type = PSA_KEY_TYPE_CHACHA20;
2221 *key_size = 256;
2222 break;
2223 case MBEDTLS_CIPHER_NULL:
2224 *alg = MBEDTLS_SSL_NULL_CIPHER;
2225 *key_type = 0;
2226 *key_size = 0;
2227 break;
2228 default:
2229 return PSA_ERROR_NOT_SUPPORTED;
2230 }
2231
2232 return PSA_SUCCESS;
2233}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002234#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002235
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002236#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002237int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2238 const unsigned char *dhm_P, size_t P_len,
2239 const unsigned char *dhm_G, size_t G_len )
2240{
Janos Follath865b3eb2019-12-16 11:46:15 +00002241 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002242
Glenn Strausscee11292021-12-20 01:43:17 -05002243 mbedtls_mpi_free( &conf->dhm_P );
2244 mbedtls_mpi_free( &conf->dhm_G );
2245
Hanno Beckera90658f2017-10-04 15:29:08 +01002246 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2247 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2248 {
2249 mbedtls_mpi_free( &conf->dhm_P );
2250 mbedtls_mpi_free( &conf->dhm_G );
2251 return( ret );
2252 }
2253
2254 return( 0 );
2255}
Paul Bakker5121ce52009-01-03 21:22:43 +00002256
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002257int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002258{
Janos Follath865b3eb2019-12-16 11:46:15 +00002259 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002260
Glenn Strausscee11292021-12-20 01:43:17 -05002261 mbedtls_mpi_free( &conf->dhm_P );
2262 mbedtls_mpi_free( &conf->dhm_G );
2263
Gilles Peskinee5702482021-06-11 21:59:08 +02002264 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2265 &conf->dhm_P ) ) != 0 ||
2266 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2267 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002268 {
2269 mbedtls_mpi_free( &conf->dhm_P );
2270 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002271 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002272 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002273
2274 return( 0 );
2275}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002276#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002277
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002278#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2279/*
2280 * Set the minimum length for Diffie-Hellman parameters
2281 */
2282void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2283 unsigned int bitlen )
2284{
2285 conf->dhm_min_bitlen = bitlen;
2286}
2287#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2288
Gilles Peskineeccd8882020-03-10 12:19:08 +01002289#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002290#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002291/*
2292 * Set allowed/preferred hashes for handshake signatures
2293 */
2294void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2295 const int *hashes )
2296{
2297 conf->sig_hashes = hashes;
2298}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002299#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002300
Jerry Yuf017ee42022-01-12 15:49:48 +08002301/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002302void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2303 const uint16_t* sig_algs )
2304{
Jerry Yuf017ee42022-01-12 15:49:48 +08002305#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2306 conf->sig_hashes = NULL;
2307#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2308 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002309}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002310#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002311
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002312#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002313#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002314/*
2315 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002316 *
2317 * mbedtls_ssl_setup() takes the provided list
2318 * and translates it to a list of IANA TLS group identifiers,
2319 * stored in ssl->handshake->group_list.
2320 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002321 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002322void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002323 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002324{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002325 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002326 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002327}
Brett Warrene0edc842021-08-17 09:53:13 +01002328#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002329#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002330
Brett Warrene0edc842021-08-17 09:53:13 +01002331/*
2332 * Set the allowed groups
2333 */
2334void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2335 const uint16_t *group_list )
2336{
2337#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2338 conf->curve_list = NULL;
2339#endif
2340 conf->group_list = group_list;
2341}
2342
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002343#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002345{
Hanno Becker947194e2017-04-07 13:25:49 +01002346 /* Initialize to suppress unnecessary compiler warning */
2347 size_t hostname_len = 0;
2348
2349 /* Check if new hostname is valid before
2350 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002351 if( hostname != NULL )
2352 {
2353 hostname_len = strlen( hostname );
2354
2355 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2356 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2357 }
2358
2359 /* Now it's clear that we will overwrite the old hostname,
2360 * so we can free it safely */
2361
2362 if( ssl->hostname != NULL )
2363 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002364 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002365 mbedtls_free( ssl->hostname );
2366 }
2367
2368 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002369
Paul Bakker5121ce52009-01-03 21:22:43 +00002370 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002371 {
2372 ssl->hostname = NULL;
2373 }
2374 else
2375 {
2376 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002377 if( ssl->hostname == NULL )
2378 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002379
Hanno Becker947194e2017-04-07 13:25:49 +01002380 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002381
Hanno Becker947194e2017-04-07 13:25:49 +01002382 ssl->hostname[hostname_len] = '\0';
2383 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002384
2385 return( 0 );
2386}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002387#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002388
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002389#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002390void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002392 const unsigned char *, size_t),
2393 void *p_sni )
2394{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002395 conf->f_sni = f_sni;
2396 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002397}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002401int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002402{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002403 size_t cur_len, tot_len;
2404 const char **p;
2405
2406 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002407 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2408 * MUST NOT be truncated."
2409 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002410 */
2411 tot_len = 0;
2412 for( p = protos; *p != NULL; p++ )
2413 {
2414 cur_len = strlen( *p );
2415 tot_len += cur_len;
2416
Ronald Cron8216dd32020-04-23 16:41:44 +02002417 if( ( cur_len == 0 ) ||
2418 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2419 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002421 }
2422
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002423 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002424
2425 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002426}
2427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002429{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002430 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002431}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002433
Johan Pascalb62bb512015-12-03 21:56:45 +01002434#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002435void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2436 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002437{
2438 conf->dtls_srtp_mki_support = support_mki_value;
2439}
2440
Ron Eldoref72faf2018-07-12 11:54:20 +03002441int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2442 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002443 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002444{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002445 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002446 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002447 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002448 }
Ron Eldor591f1622018-01-22 12:30:04 +02002449
2450 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002451 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002452 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002453 }
Ron Eldor591f1622018-01-22 12:30:04 +02002454
2455 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2456 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002457 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002458}
2459
Ron Eldoref72faf2018-07-12 11:54:20 +03002460int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002461 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002462{
Johan Pascal253d0262020-09-22 13:04:45 +02002463 const mbedtls_ssl_srtp_profile *p;
2464 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002465
Johan Pascal253d0262020-09-22 13:04:45 +02002466 /* check the profiles list: all entry must be valid,
2467 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002468 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2469 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2470 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002471 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002472 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002473 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002474 list_size++;
2475 }
2476 else
2477 {
2478 /* unsupported value, stop parsing and set the size to an error value */
2479 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002480 }
2481 }
2482
Johan Pascal5ef72d22020-10-28 17:05:47 +01002483 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002484 {
Johan Pascal253d0262020-09-22 13:04:45 +02002485 conf->dtls_srtp_profile_list = NULL;
2486 conf->dtls_srtp_profile_list_len = 0;
2487 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2488 }
2489
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002490 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002491 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002492
2493 return( 0 );
2494}
2495
Johan Pascal5ef72d22020-10-28 17:05:47 +01002496void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2497 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002498{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002499 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2500 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002501 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002502 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002503 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002504 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002505 else
2506 {
2507 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002508 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2509 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002510 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002511}
Johan Pascalb62bb512015-12-03 21:56:45 +01002512#endif /* MBEDTLS_SSL_DTLS_SRTP */
2513
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302514#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002515void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002516{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002517 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002518}
2519
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002520void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002521{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002522 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002523}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302524#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002525
Janos Follath088ce432017-04-10 12:42:31 +01002526#if defined(MBEDTLS_SSL_SRV_C)
2527void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2528 char cert_req_ca_list )
2529{
2530 conf->cert_req_ca_list = cert_req_ca_list;
2531}
2532#endif
2533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002535void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002536{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002537 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002538}
2539#endif
2540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002542void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002543{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002544 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002545}
2546#endif
2547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002549int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002550{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002552 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002555 }
2556
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002557 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002558
2559 return( 0 );
2560}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002562
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002563void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002564{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002565 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002566}
2567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002569void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002570{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002571 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002572}
2573
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002574void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002575{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002576 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002577}
2578
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002579void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002580 const unsigned char period[8] )
2581{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002582 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002583}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002586#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002587#if defined(MBEDTLS_SSL_CLI_C)
2588void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002589{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002590 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002591}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002592#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002593
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002594#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002595void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2596 mbedtls_ssl_ticket_write_t *f_ticket_write,
2597 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2598 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002599{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002600 conf->f_ticket_write = f_ticket_write;
2601 conf->f_ticket_parse = f_ticket_parse;
2602 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002603}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002604#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002606
Hanno Becker7e6c1782021-06-08 09:24:55 +01002607void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2608 mbedtls_ssl_export_keys_t *f_export_keys,
2609 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002610{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002611 ssl->f_export_keys = f_export_keys;
2612 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002613}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002614
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002615#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002616void mbedtls_ssl_conf_async_private_cb(
2617 mbedtls_ssl_config *conf,
2618 mbedtls_ssl_async_sign_t *f_async_sign,
2619 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2620 mbedtls_ssl_async_resume_t *f_async_resume,
2621 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002622 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002623{
2624 conf->f_async_sign_start = f_async_sign;
2625 conf->f_async_decrypt_start = f_async_decrypt;
2626 conf->f_async_resume = f_async_resume;
2627 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002628 conf->p_async_config_data = async_config_data;
2629}
2630
Gilles Peskine8f97af72018-04-26 11:46:10 +02002631void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2632{
2633 return( conf->p_async_config_data );
2634}
2635
Gilles Peskine1febfef2018-04-30 11:54:39 +02002636void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002637{
2638 if( ssl->handshake == NULL )
2639 return( NULL );
2640 else
2641 return( ssl->handshake->user_async_ctx );
2642}
2643
Gilles Peskine1febfef2018-04-30 11:54:39 +02002644void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002645 void *ctx )
2646{
2647 if( ssl->handshake != NULL )
2648 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002649}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002650#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002651
Paul Bakker5121ce52009-01-03 21:22:43 +00002652/*
2653 * SSL get accessors
2654 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002655uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002656{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002657 if( ssl->session != NULL )
2658 return( ssl->session->verify_result );
2659
2660 if( ssl->session_negotiate != NULL )
2661 return( ssl->session_negotiate->verify_result );
2662
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002663 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002664}
2665
Glenn Strauss8f526902022-01-13 00:04:49 -05002666int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2667{
2668 if( ssl == NULL || ssl->session == NULL )
2669 return( 0 );
2670
2671 return( ssl->session->ciphersuite );
2672}
2673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002675{
Paul Bakker926c8e42013-03-06 10:23:34 +01002676 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002677 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002680}
2681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002683{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002685 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002686 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002687 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002688 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002689 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002690 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002691 default:
2692 return( "unknown (DTLS)" );
2693 }
2694 }
2695#endif
2696
Glenn Strauss60bfe602022-03-14 19:04:24 -04002697 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002698 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002699 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002700 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002701 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002702 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002703 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002704 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002705 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002706}
2707
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002708#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002709size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2710{
David Horstmann95d516f2021-05-04 18:36:56 +01002711 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002712 size_t read_mfl;
2713
2714 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2715 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2716 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2717 {
2718 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2719 }
2720
2721 /* Check if a smaller max length was negotiated */
2722 if( ssl->session_out != NULL )
2723 {
2724 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2725 if( read_mfl < max_len )
2726 {
2727 max_len = read_mfl;
2728 }
2729 }
2730
2731 // During a handshake, use the value being negotiated
2732 if( ssl->session_negotiate != NULL )
2733 {
2734 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2735 if( read_mfl < max_len )
2736 {
2737 max_len = read_mfl;
2738 }
2739 }
2740
2741 return( max_len );
2742}
2743
2744size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002745{
2746 size_t max_len;
2747
2748 /*
2749 * Assume mfl_code is correct since it was checked when set
2750 */
Angus Grattond8213d02016-05-25 20:56:48 +10002751 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002752
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002753 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002754 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002755 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002756 {
Angus Grattond8213d02016-05-25 20:56:48 +10002757 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002758 }
2759
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002760 /* During a handshake, use the value being negotiated */
2761 if( ssl->session_negotiate != NULL &&
2762 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2763 {
2764 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2765 }
2766
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002767 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002768}
2769#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2770
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002771#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002772size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002773{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002774 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2775 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2776 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2777 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2778 return ( 0 );
2779
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002780 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2781 return( ssl->mtu );
2782
2783 if( ssl->mtu == 0 )
2784 return( ssl->handshake->mtu );
2785
2786 return( ssl->mtu < ssl->handshake->mtu ?
2787 ssl->mtu : ssl->handshake->mtu );
2788}
2789#endif /* MBEDTLS_SSL_PROTO_DTLS */
2790
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002791int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2792{
2793 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2794
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002795#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2796 !defined(MBEDTLS_SSL_PROTO_DTLS)
2797 (void) ssl;
2798#endif
2799
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002800#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002801 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002802
2803 if( max_len > mfl )
2804 max_len = mfl;
2805#endif
2806
2807#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002808 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002809 {
Hanno Becker89490712020-02-05 10:50:12 +00002810 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002811 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2812 const size_t overhead = (size_t) ret;
2813
2814 if( ret < 0 )
2815 return( ret );
2816
2817 if( mtu <= overhead )
2818 {
2819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2820 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2821 }
2822
2823 if( max_len > mtu - overhead )
2824 max_len = mtu - overhead;
2825 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002826#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002827
Hanno Becker0defedb2018-08-10 12:35:02 +01002828#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2829 !defined(MBEDTLS_SSL_PROTO_DTLS)
2830 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002831#endif
2832
2833 return( (int) max_len );
2834}
2835
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002836int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2837{
2838 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2839
2840#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2841 (void) ssl;
2842#endif
2843
2844#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2845 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2846
2847 if( max_len > mfl )
2848 max_len = mfl;
2849#endif
2850
2851 return( (int) max_len );
2852}
2853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854#if defined(MBEDTLS_X509_CRT_PARSE_C)
2855const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002856{
2857 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002858 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002859
Hanno Beckere6824572019-02-07 13:18:46 +00002860#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002861 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002862#else
2863 return( NULL );
2864#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002865}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002869int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2870 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002871{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002872 int ret;
2873
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002874 if( ssl == NULL ||
2875 dst == NULL ||
2876 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002877 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002880 }
2881
Hanno Beckere810bbc2021-05-14 16:01:05 +01002882 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2883 * idempotent: Each session can only be exported once.
2884 *
2885 * (This is in preparation for TLS 1.3 support where we will
2886 * need the ability to export multiple sessions (aka tickets),
2887 * which will be achieved by calling mbedtls_ssl_get_session()
2888 * multiple times until it fails.)
2889 *
2890 * Check whether we have already exported the current session,
2891 * and fail if so.
2892 */
2893 if( ssl->session->exported == 1 )
2894 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2895
2896 ret = mbedtls_ssl_session_copy( dst, ssl->session );
2897 if( ret != 0 )
2898 return( ret );
2899
2900 /* Remember that we've exported the session. */
2901 ssl->session->exported = 1;
2902 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002903}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002905
Paul Bakker5121ce52009-01-03 21:22:43 +00002906/*
Hanno Beckera835da52019-05-16 12:39:07 +01002907 * Define ticket header determining Mbed TLS version
2908 * and structure of the ticket.
2909 */
2910
Hanno Becker94ef3b32019-05-16 12:50:45 +01002911/*
Hanno Becker50b59662019-05-28 14:30:45 +01002912 * Define bitflag determining compile-time settings influencing
2913 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01002914 */
2915
Hanno Becker50b59662019-05-28 14:30:45 +01002916#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01002917#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01002918#else
Hanno Becker3e088662019-05-29 11:10:18 +01002919#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002920#endif /* MBEDTLS_HAVE_TIME */
2921
2922#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01002923#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002924#else
Hanno Becker3e088662019-05-29 11:10:18 +01002925#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002926#endif /* MBEDTLS_X509_CRT_PARSE_C */
2927
2928#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01002929#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002930#else
Hanno Becker3e088662019-05-29 11:10:18 +01002931#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002932#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
2933
2934#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01002935#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002936#else
Hanno Becker3e088662019-05-29 11:10:18 +01002937#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002938#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2939
Hanno Becker94ef3b32019-05-16 12:50:45 +01002940#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01002941#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002942#else
Hanno Becker3e088662019-05-29 11:10:18 +01002943#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002944#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2945
Hanno Becker94ef3b32019-05-16 12:50:45 +01002946#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2947#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
2948#else
2949#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
2950#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2951
Hanno Becker3e088662019-05-29 11:10:18 +01002952#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
2953#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
2954#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
2955#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01002956#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
2957#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01002958
Hanno Becker50b59662019-05-28 14:30:45 +01002959#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01002960 ( (uint16_t) ( \
2961 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
2962 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
2963 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
2964 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01002965 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01002966 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01002967
Hanno Beckerf8787072019-05-16 12:41:07 +01002968static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01002969 MBEDTLS_VERSION_MAJOR,
2970 MBEDTLS_VERSION_MINOR,
2971 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01002972 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
2973 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01002974};
Hanno Beckera835da52019-05-16 12:39:07 +01002975
2976/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002977 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002978 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002979 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002980 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002981 *
Hanno Beckerdce50972021-08-01 05:39:23 +01002982 * opaque mbedtls_version[3]; // library version: major, minor, patch
2983 * opaque session_format[2]; // library-version specific 16-bit field
2984 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002985 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002986 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002987 * Note: When updating the format, remember to keep
2988 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002989 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002990 * // In this version, `session_format` determines
2991 * // the setting of those compile-time
2992 * // configuration options which influence
2993 * // the structure of mbedtls_ssl_session.
2994 *
Glenn Straussda7851c2022-03-14 13:29:48 -04002995 * uint8_t minor_ver; // Protocol minor version. Possible values:
2996 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002997 *
Glenn Straussda7851c2022-03-14 13:29:48 -04002998 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002999 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003000 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003001 * serialized_session_tls12 data;
3002 *
3003 * };
3004 *
3005 * } serialized_session;
3006 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003007 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003008
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003009MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003010static int ssl_session_save( const mbedtls_ssl_session *session,
3011 unsigned char omit_header,
3012 unsigned char *buf,
3013 size_t buf_len,
3014 size_t *olen )
3015{
3016 unsigned char *p = buf;
3017 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003018 size_t remaining_len;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003019
Jerry Yu438ddd82022-07-07 06:55:50 +00003020 if( session == NULL )
3021 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3022
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003023 if( !omit_header )
3024 {
3025 /*
3026 * Add Mbed TLS version identifier
3027 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003028 used += sizeof( ssl_serialized_session_header );
3029
3030 if( used <= buf_len )
3031 {
3032 memcpy( p, ssl_serialized_session_header,
3033 sizeof( ssl_serialized_session_header ) );
3034 p += sizeof( ssl_serialized_session_header );
3035 }
3036 }
3037
3038 /*
3039 * TLS version identifier
3040 */
3041 used += 1;
3042 if( used <= buf_len )
3043 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003044 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003045 }
3046
3047 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003048 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003049 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003050 {
3051#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003052 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003053 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003054 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003055#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3056
Jerry Yu251a12e2022-07-13 15:15:48 +08003057#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3058 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yu438ddd82022-07-07 06:55:50 +00003059 used += ssl_tls13_session_save( session, p, remaining_len );
Jerry Yu251a12e2022-07-13 15:15:48 +08003060 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003061#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3062
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003063 default:
3064 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3065 }
3066
3067 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003068 if( used > buf_len )
3069 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003070
3071 return( 0 );
3072}
3073
3074/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003075 * Public wrapper for ssl_session_save()
3076 */
3077int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3078 unsigned char *buf,
3079 size_t buf_len,
3080 size_t *olen )
3081{
3082 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3083}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003084
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003085/*
3086 * Deserialize session, see mbedtls_ssl_session_save() for format.
3087 *
3088 * This internal version is wrapped by a public function that cleans up in
3089 * case of error, and has an extra option omit_header.
3090 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003091MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003092static int ssl_session_load( mbedtls_ssl_session *session,
3093 unsigned char omit_header,
3094 const unsigned char *buf,
3095 size_t len )
3096{
3097 const unsigned char *p = buf;
3098 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003099 size_t remaining_len;
3100
3101
3102 if( session == NULL )
3103 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003104
3105 if( !omit_header )
3106 {
3107 /*
3108 * Check Mbed TLS version identifier
3109 */
3110
3111 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3112 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3113
3114 if( memcmp( p, ssl_serialized_session_header,
3115 sizeof( ssl_serialized_session_header ) ) != 0 )
3116 {
3117 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3118 }
3119 p += sizeof( ssl_serialized_session_header );
3120 }
3121
3122 /*
3123 * TLS version identifier
3124 */
3125 if( 1 > (size_t)( end - p ) )
3126 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003127 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003128
3129 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003130 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003131 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003132 {
3133#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003134 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003135 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003136#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3137
Jerry Yu438ddd82022-07-07 06:55:50 +00003138#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3139 case MBEDTLS_SSL_VERSION_TLS1_3:
3140 return( ssl_tls13_session_load( session, p, remaining_len ) );
3141#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3142
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003143 default:
3144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3145 }
3146}
3147
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003148/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003149 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003150 */
3151int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3152 const unsigned char *buf,
3153 size_t len )
3154{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003155 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003156
3157 if( ret != 0 )
3158 mbedtls_ssl_session_free( session );
3159
3160 return( ret );
3161}
3162
3163/*
Paul Bakker1961b702013-01-25 14:49:24 +01003164 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003165 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003166MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003167static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3168{
3169 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3170
Ronald Cron66dbf912022-02-02 15:33:46 +01003171 /*
3172 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003173 * that were written into the output buffer by the previous handshake step,
3174 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003175 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3176 * We proceed to the next handshake step only when all data from the
3177 * previous one have been sent to the peer, thus we make sure that this is
3178 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3179 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3180 * we have to wait before to go ahead.
3181 * In the case of TLS 1.3, handshake step handlers do not send data to the
3182 * peer. Data are only sent here and through
3183 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003184 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003185 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003186 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3187 return( ret );
3188
3189#if defined(MBEDTLS_SSL_PROTO_DTLS)
3190 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3191 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3192 {
3193 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3194 return( ret );
3195 }
3196#endif /* MBEDTLS_SSL_PROTO_DTLS */
3197
3198 return( ret );
3199}
3200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003202{
Hanno Becker41934dd2021-08-07 19:13:43 +01003203 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003204
Hanno Becker41934dd2021-08-07 19:13:43 +01003205 if( ssl == NULL ||
3206 ssl->conf == NULL ||
3207 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003208 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003209 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003210 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003211 }
3212
3213 ret = ssl_prepare_handshake_step( ssl );
3214 if( ret != 0 )
3215 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003216
Jerry Yue7047812021-09-13 19:26:39 +08003217 ret = mbedtls_ssl_handle_pending_alert( ssl );
3218 if( ret != 0 )
3219 goto cleanup;
3220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003222 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003223 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3225 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003226
Ronald Cron9f0fba32022-02-10 16:45:15 +01003227 switch( ssl->state )
3228 {
3229 case MBEDTLS_SSL_HELLO_REQUEST:
3230 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3231 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003232
Ronald Cron9f0fba32022-02-10 16:45:15 +01003233 case MBEDTLS_SSL_CLIENT_HELLO:
3234 ret = mbedtls_ssl_write_client_hello( ssl );
3235 break;
3236
3237 default:
3238#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003239 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003240 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3241 else
3242 ret = mbedtls_ssl_handshake_client_step( ssl );
3243#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3244 ret = mbedtls_ssl_handshake_client_step( ssl );
3245#else
3246 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3247#endif
3248 }
Jerry Yub9930e72021-08-06 17:11:51 +08003249 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003250#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003252 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003253 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003254#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003255 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003256 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003257#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003258
3259#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3260 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3261 ret = mbedtls_ssl_handshake_server_step( ssl );
3262#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3263 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003264#endif
3265
Jerry Yue7047812021-09-13 19:26:39 +08003266 if( ret != 0 )
3267 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003268 /* handshake_step return error. And it is same
3269 * with alert_reason.
3270 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003271 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003272 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003273 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003274 goto cleanup;
3275 }
3276 }
3277
3278cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003279 return( ret );
3280}
3281
3282/*
3283 * Perform the SSL handshake
3284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003285int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003286{
3287 int ret = 0;
3288
Hanno Beckera817ea42020-10-20 15:20:23 +01003289 /* Sanity checks */
3290
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003291 if( ssl == NULL || ssl->conf == NULL )
3292 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3293
Hanno Beckera817ea42020-10-20 15:20:23 +01003294#if defined(MBEDTLS_SSL_PROTO_DTLS)
3295 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3296 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3297 {
3298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3299 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3300 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3301 }
3302#endif /* MBEDTLS_SSL_PROTO_DTLS */
3303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003305
Hanno Beckera817ea42020-10-20 15:20:23 +01003306 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003307 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003309 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003310
3311 if( ret != 0 )
3312 break;
3313 }
3314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003316
3317 return( ret );
3318}
3319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320#if defined(MBEDTLS_SSL_RENEGOTIATION)
3321#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003322/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003323 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003324 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003325MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003326static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003327{
Janos Follath865b3eb2019-12-16 11:46:15 +00003328 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003330 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003331
3332 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003333 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3334 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003335
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003336 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003337 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003338 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003339 return( ret );
3340 }
3341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003343
3344 return( 0 );
3345}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003347
3348/*
3349 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 * - any side: calling mbedtls_ssl_renegotiate(),
3351 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3352 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003353 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003354 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003355 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003356 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003357int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003358{
Janos Follath865b3eb2019-12-16 11:46:15 +00003359 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003362
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003363 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3364 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003365
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003366 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3367 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003368#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003369 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003370 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003371 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003372 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003373 ssl->handshake->out_msg_seq = 1;
3374 else
3375 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003376 }
3377#endif
3378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003379 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3380 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003384 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003385 return( ret );
3386 }
3387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003389
3390 return( 0 );
3391}
3392
3393/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003394 * Renegotiate current connection on client,
3395 * or request renegotiation on server
3396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003398{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003400
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003401 if( ssl == NULL || ssl->conf == NULL )
3402 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003405 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003406 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003407 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003408 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003412
3413 /* Did we already try/start sending HelloRequest? */
3414 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003416
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003417 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003418 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003422 /*
3423 * On client, either start the renegotiation process or,
3424 * if already in progress, continue the handshake
3425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003427 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003428 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003429 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003430
Hanno Becker40cdaa12020-02-05 10:48:27 +00003431 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003432 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003433 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003434 return( ret );
3435 }
3436 }
3437 else
3438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003439 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003441 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003442 return( ret );
3443 }
3444 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003446
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003447 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003448}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003450
Gilles Peskine9b562d52018-04-25 20:32:43 +02003451void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003452{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003453 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3454
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003455 if( handshake == NULL )
3456 return;
3457
Brett Warrene0edc842021-08-17 09:53:13 +01003458#if defined(MBEDTLS_ECP_C)
3459#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3460 if ( ssl->handshake->group_list_heap_allocated )
3461 mbedtls_free( (void*) handshake->group_list );
3462 handshake->group_list = NULL;
3463#endif /* MBEDTLS_DEPRECATED_REMOVED */
3464#endif /* MBEDTLS_ECP_C */
3465
Jerry Yuf017ee42022-01-12 15:49:48 +08003466#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3467#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3468 if ( ssl->handshake->sig_algs_heap_allocated )
3469 mbedtls_free( (void*) handshake->sig_algs );
3470 handshake->sig_algs = NULL;
3471#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003472#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3473 if( ssl->handshake->certificate_request_context )
3474 {
3475 mbedtls_free( (void*) handshake->certificate_request_context );
3476 }
3477#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003478#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3479
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003480#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3481 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3482 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003483 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003484 handshake->async_in_progress = 0;
3485 }
3486#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3487
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003488#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003489#if defined(MBEDTLS_USE_PSA_CRYPTO)
3490 psa_hash_abort( &handshake->fin_sha256_psa );
3491#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003492 mbedtls_sha256_free( &handshake->fin_sha256 );
3493#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003494#endif
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +02003495#if defined(MBEDTLS_SHA384_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003496#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003497 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003498#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003499 mbedtls_sha512_free( &handshake->fin_sha512 );
3500#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003501#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503#if defined(MBEDTLS_DHM_C)
3504 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003505#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003506#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003508#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003509#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003510 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003511#if defined(MBEDTLS_SSL_CLI_C)
3512 mbedtls_free( handshake->ecjpake_cache );
3513 handshake->ecjpake_cache = NULL;
3514 handshake->ecjpake_cache_len = 0;
3515#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003516#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003517
Janos Follath4ae5c292016-02-10 11:27:43 +00003518#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3519 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003520 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003522#endif
3523
Gilles Peskineeccd8882020-03-10 12:19:08 +01003524#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003525#if defined(MBEDTLS_USE_PSA_CRYPTO)
3526 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3527 {
3528 /* The maintenance of the external PSK key slot is the
3529 * user's responsibility. */
3530 if( ssl->handshake->psk_opaque_is_internal )
3531 {
3532 psa_destroy_key( ssl->handshake->psk_opaque );
3533 ssl->handshake->psk_opaque_is_internal = 0;
3534 }
3535 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3536 }
Neil Armstronge952a302022-05-03 10:22:14 +02003537#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003538 if( handshake->psk != NULL )
3539 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003540 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003541 mbedtls_free( handshake->psk );
3542 }
Neil Armstronge952a302022-05-03 10:22:14 +02003543#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003544#endif
3545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003546#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3547 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003548 /*
3549 * Free only the linked list wrapper, not the keys themselves
3550 * since the belong to the SNI callback
3551 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003552 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003553#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003554
Gilles Peskineeccd8882020-03-10 12:19:08 +01003555#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003556 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003557 if( handshake->ecrs_peer_cert != NULL )
3558 {
3559 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3560 mbedtls_free( handshake->ecrs_peer_cert );
3561 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003562#endif
3563
Hanno Becker75173122019-02-06 16:18:31 +00003564#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3565 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3566 mbedtls_pk_free( &handshake->peer_pubkey );
3567#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3568
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003569#if defined(MBEDTLS_SSL_CLI_C) && \
3570 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3571 mbedtls_free( handshake->cookie );
3572#endif /* MBEDTLS_SSL_CLI_C &&
3573 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003574
3575#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003576 mbedtls_ssl_flight_free( handshake->flight );
3577 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003578#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003579
Ronald Cronf12b81d2022-03-15 10:42:41 +01003580#if defined(MBEDTLS_ECDH_C) && \
3581 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003582 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003583 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003584#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3585
Ronald Cron6f135e12021-12-08 16:57:54 +01003586#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003587 mbedtls_ssl_transform_free( handshake->transform_handshake );
3588 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003589 mbedtls_free( handshake->transform_earlydata );
3590 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003591#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003592
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003593
3594#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3595 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3596 * processes datagrams and the fact that a datagram is allowed to have
3597 * several records in it, it is possible that the I/O buffers are not
3598 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003599 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3600 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003601#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003602
Jerry Yuba9c7272021-10-30 11:54:10 +08003603 /* mbedtls_platform_zeroize MUST be last one in this function */
3604 mbedtls_platform_zeroize( handshake,
3605 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003606}
3607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003609{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003610 if( session == NULL )
3611 return;
3612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003614 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003615#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003616
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003617#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003619#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003620
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003621 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003622}
3623
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003624#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003625
3626#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3627#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3628#else
3629#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3630#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3631
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003632#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003633
3634#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3635#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3636#else
3637#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3638#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3639
3640#if defined(MBEDTLS_SSL_ALPN)
3641#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3642#else
3643#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3644#endif /* MBEDTLS_SSL_ALPN */
3645
3646#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3647#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3648#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3649#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3650
3651#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3652 ( (uint32_t) ( \
3653 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3654 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3655 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3656 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3657 0u ) )
3658
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003659static unsigned char ssl_serialized_context_header[] = {
3660 MBEDTLS_VERSION_MAJOR,
3661 MBEDTLS_VERSION_MINOR,
3662 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003663 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3664 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3665 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3666 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3667 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003668};
3669
Paul Bakker5121ce52009-01-03 21:22:43 +00003670/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003671 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003672 *
3673 * The format of the serialized data is:
3674 * (in the presentation language of TLS, RFC 8446 section 3)
3675 *
3676 * // header
3677 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003678 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003679 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003680 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003681 * Note: When updating the format, remember to keep these
3682 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003683 *
3684 * // session sub-structure
3685 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3686 * // transform sub-structure
3687 * uint8 random[64]; // ServerHello.random+ClientHello.random
3688 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3689 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3690 * // fields from ssl_context
3691 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3692 * uint64 in_window_top; // DTLS: last validated record seq_num
3693 * uint64 in_window; // DTLS: bitmask for replay protection
3694 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3695 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3696 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3697 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3698 *
3699 * Note that many fields of the ssl_context or sub-structures are not
3700 * serialized, as they fall in one of the following categories:
3701 *
3702 * 1. forced value (eg in_left must be 0)
3703 * 2. pointer to dynamically-allocated memory (eg session, transform)
3704 * 3. value can be re-derived from other data (eg session keys from MS)
3705 * 4. value was temporary (eg content of input buffer)
3706 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003707 */
3708int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3709 unsigned char *buf,
3710 size_t buf_len,
3711 size_t *olen )
3712{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003713 unsigned char *p = buf;
3714 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003715 size_t session_len;
3716 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003717
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003718 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003719 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3720 * this function's documentation.
3721 *
3722 * These are due to assumptions/limitations in the implementation. Some of
3723 * them are likely to stay (no handshake in progress) some might go away
3724 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003725 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003726 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003727 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003728 {
3729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003730 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003731 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003732 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003733 {
3734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003735 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003736 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003737 /* Double-check that sub-structures are indeed ready */
3738 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003739 {
3740 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003741 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003742 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003743 /* There must be no pending incoming or outgoing data */
3744 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003745 {
3746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003747 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003748 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003749 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003750 {
3751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003752 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003753 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003754 /* Protocol must be DLTS, not TLS */
3755 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003756 {
3757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003758 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003759 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003760 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003761 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003762 {
3763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003764 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003765 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003766 /* We must be using an AEAD ciphersuite */
3767 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003768 {
3769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003770 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003771 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003772 /* Renegotiation must not be enabled */
3773#if defined(MBEDTLS_SSL_RENEGOTIATION)
3774 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003775 {
3776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003777 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003778 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003779#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003780
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003781 /*
3782 * Version and format identifier
3783 */
3784 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003785
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003786 if( used <= buf_len )
3787 {
3788 memcpy( p, ssl_serialized_context_header,
3789 sizeof( ssl_serialized_context_header ) );
3790 p += sizeof( ssl_serialized_context_header );
3791 }
3792
3793 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003794 * Session (length + data)
3795 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003796 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003797 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3798 return( ret );
3799
3800 used += 4 + session_len;
3801 if( used <= buf_len )
3802 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003803 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3804 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003805
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003806 ret = ssl_session_save( ssl->session, 1,
3807 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003808 if( ret != 0 )
3809 return( ret );
3810
3811 p += session_len;
3812 }
3813
3814 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003815 * Transform
3816 */
3817 used += sizeof( ssl->transform->randbytes );
3818 if( used <= buf_len )
3819 {
3820 memcpy( p, ssl->transform->randbytes,
3821 sizeof( ssl->transform->randbytes ) );
3822 p += sizeof( ssl->transform->randbytes );
3823 }
3824
3825#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3826 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3827 if( used <= buf_len )
3828 {
3829 *p++ = ssl->transform->in_cid_len;
3830 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3831 p += ssl->transform->in_cid_len;
3832
3833 *p++ = ssl->transform->out_cid_len;
3834 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3835 p += ssl->transform->out_cid_len;
3836 }
3837#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3838
3839 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003840 * Saved fields from top-level ssl_context structure
3841 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003842 used += 4;
3843 if( used <= buf_len )
3844 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003845 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3846 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003847 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003848
3849#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3850 used += 16;
3851 if( used <= buf_len )
3852 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003853 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3854 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003855
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003856 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3857 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003858 }
3859#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3860
3861#if defined(MBEDTLS_SSL_PROTO_DTLS)
3862 used += 1;
3863 if( used <= buf_len )
3864 {
3865 *p++ = ssl->disable_datagram_packing;
3866 }
3867#endif /* MBEDTLS_SSL_PROTO_DTLS */
3868
Jerry Yuae0b2e22021-10-08 15:21:19 +08003869 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003870 if( used <= buf_len )
3871 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003872 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3873 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003874 }
3875
3876#if defined(MBEDTLS_SSL_PROTO_DTLS)
3877 used += 2;
3878 if( used <= buf_len )
3879 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003880 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
3881 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003882 }
3883#endif /* MBEDTLS_SSL_PROTO_DTLS */
3884
3885#if defined(MBEDTLS_SSL_ALPN)
3886 {
3887 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02003888 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003889 : 0;
3890
3891 used += 1 + alpn_len;
3892 if( used <= buf_len )
3893 {
3894 *p++ = alpn_len;
3895
3896 if( ssl->alpn_chosen != NULL )
3897 {
3898 memcpy( p, ssl->alpn_chosen, alpn_len );
3899 p += alpn_len;
3900 }
3901 }
3902 }
3903#endif /* MBEDTLS_SSL_ALPN */
3904
3905 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003906 * Done
3907 */
3908 *olen = used;
3909
3910 if( used > buf_len )
3911 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003912
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003913 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
3914
Hanno Becker43aefe22020-02-05 10:44:56 +00003915 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003916}
3917
3918/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003919 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003920 *
3921 * This internal version is wrapped by a public function that cleans up in
3922 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003923 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003924MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003925static int ssl_context_load( mbedtls_ssl_context *ssl,
3926 const unsigned char *buf,
3927 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003928{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003929 const unsigned char *p = buf;
3930 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003931 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00003932 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003933
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003934 /*
3935 * The context should have been freshly setup or reset.
3936 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02003937 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003938 * renegotiating, or if the user mistakenly loaded a session first.)
3939 */
3940 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
3941 ssl->session != NULL )
3942 {
3943 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3944 }
3945
3946 /*
3947 * We can't check that the config matches the initial one, but we can at
3948 * least check it matches the requirements for serializing.
3949 */
3950 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003951 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
3952 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003953#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003954 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003955#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003956 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003957 {
3958 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3959 }
3960
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003961 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
3962
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003963 /*
3964 * Check version identifier
3965 */
3966 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
3967 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3968
3969 if( memcmp( p, ssl_serialized_context_header,
3970 sizeof( ssl_serialized_context_header ) ) != 0 )
3971 {
3972 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3973 }
3974 p += sizeof( ssl_serialized_context_header );
3975
3976 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003977 * Session
3978 */
3979 if( (size_t)( end - p ) < 4 )
3980 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3981
3982 session_len = ( (size_t) p[0] << 24 ) |
3983 ( (size_t) p[1] << 16 ) |
3984 ( (size_t) p[2] << 8 ) |
3985 ( (size_t) p[3] );
3986 p += 4;
3987
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003988 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00003989 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003990 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003991 ssl->session_in = ssl->session;
3992 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003993 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003994
3995 if( (size_t)( end - p ) < session_len )
3996 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3997
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003998 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003999 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004000 {
4001 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004002 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004003 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004004
4005 p += session_len;
4006
4007 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004008 * Transform
4009 */
4010
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004011 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004012 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004013 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004014 ssl->transform_in = ssl->transform;
4015 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004016 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004017
4018 /* Read random bytes and populate structure */
4019 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4020 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08004021#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00004022 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004023 ssl->session->ciphersuite,
4024 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004025#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004026 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004027#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004028 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
4029 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004030 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004031 ssl->conf->endpoint,
4032 ssl );
4033 if( ret != 0 )
4034 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004035#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004036 p += sizeof( ssl->transform->randbytes );
4037
4038#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4039 /* Read connection IDs and store them */
4040 if( (size_t)( end - p ) < 1 )
4041 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4042
4043 ssl->transform->in_cid_len = *p++;
4044
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004045 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004046 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4047
4048 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4049 p += ssl->transform->in_cid_len;
4050
4051 ssl->transform->out_cid_len = *p++;
4052
4053 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4054 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4055
4056 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4057 p += ssl->transform->out_cid_len;
4058#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4059
4060 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004061 * Saved fields from top-level ssl_context structure
4062 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004063 if( (size_t)( end - p ) < 4 )
4064 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4065
4066 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4067 ( (uint32_t) p[1] << 16 ) |
4068 ( (uint32_t) p[2] << 8 ) |
4069 ( (uint32_t) p[3] );
4070 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004071
4072#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4073 if( (size_t)( end - p ) < 16 )
4074 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4075
4076 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4077 ( (uint64_t) p[1] << 48 ) |
4078 ( (uint64_t) p[2] << 40 ) |
4079 ( (uint64_t) p[3] << 32 ) |
4080 ( (uint64_t) p[4] << 24 ) |
4081 ( (uint64_t) p[5] << 16 ) |
4082 ( (uint64_t) p[6] << 8 ) |
4083 ( (uint64_t) p[7] );
4084 p += 8;
4085
4086 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4087 ( (uint64_t) p[1] << 48 ) |
4088 ( (uint64_t) p[2] << 40 ) |
4089 ( (uint64_t) p[3] << 32 ) |
4090 ( (uint64_t) p[4] << 24 ) |
4091 ( (uint64_t) p[5] << 16 ) |
4092 ( (uint64_t) p[6] << 8 ) |
4093 ( (uint64_t) p[7] );
4094 p += 8;
4095#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4096
4097#if defined(MBEDTLS_SSL_PROTO_DTLS)
4098 if( (size_t)( end - p ) < 1 )
4099 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4100
4101 ssl->disable_datagram_packing = *p++;
4102#endif /* MBEDTLS_SSL_PROTO_DTLS */
4103
Jerry Yud9a94fe2021-09-28 18:58:59 +08004104 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004105 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004106 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4107 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004108
4109#if defined(MBEDTLS_SSL_PROTO_DTLS)
4110 if( (size_t)( end - p ) < 2 )
4111 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4112
4113 ssl->mtu = ( p[0] << 8 ) | p[1];
4114 p += 2;
4115#endif /* MBEDTLS_SSL_PROTO_DTLS */
4116
4117#if defined(MBEDTLS_SSL_ALPN)
4118 {
4119 uint8_t alpn_len;
4120 const char **cur;
4121
4122 if( (size_t)( end - p ) < 1 )
4123 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4124
4125 alpn_len = *p++;
4126
4127 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4128 {
4129 /* alpn_chosen should point to an item in the configured list */
4130 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4131 {
4132 if( strlen( *cur ) == alpn_len &&
4133 memcmp( p, cur, alpn_len ) == 0 )
4134 {
4135 ssl->alpn_chosen = *cur;
4136 break;
4137 }
4138 }
4139 }
4140
4141 /* can only happen on conf mismatch */
4142 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4144
4145 p += alpn_len;
4146 }
4147#endif /* MBEDTLS_SSL_ALPN */
4148
4149 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004150 * Forced fields from top-level ssl_context structure
4151 *
4152 * Most of them already set to the correct value by mbedtls_ssl_init() and
4153 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4154 */
4155 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004156 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004157
Hanno Becker361b10d2019-08-30 10:42:49 +01004158 /* Adjust pointers for header fields of outgoing records to
4159 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004160 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004161
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004162#if defined(MBEDTLS_SSL_PROTO_DTLS)
4163 ssl->in_epoch = 1;
4164#endif
4165
4166 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4167 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004168 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4169 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004170 if( ssl->handshake != NULL )
4171 {
4172 mbedtls_ssl_handshake_free( ssl );
4173 mbedtls_free( ssl->handshake );
4174 ssl->handshake = NULL;
4175 }
4176
4177 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004178 * Done - should have consumed entire buffer
4179 */
4180 if( p != end )
4181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004182
4183 return( 0 );
4184}
4185
4186/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004187 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004188 */
4189int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4190 const unsigned char *buf,
4191 size_t len )
4192{
4193 int ret = ssl_context_load( context, buf, len );
4194
4195 if( ret != 0 )
4196 mbedtls_ssl_free( context );
4197
4198 return( ret );
4199}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004200#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004201
4202/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004203 * Free an SSL context
4204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004205void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004206{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004207 if( ssl == NULL )
4208 return;
4209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004211
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004212 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004213 {
sander-visserb8aa2072020-05-06 22:05:13 +02004214#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4215 size_t out_buf_len = ssl->out_buf_len;
4216#else
4217 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4218#endif
4219
Darryl Greenb33cc762019-11-28 14:29:44 +00004220 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004221 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004222 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004223 }
4224
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004225 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004226 {
sander-visserb8aa2072020-05-06 22:05:13 +02004227#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4228 size_t in_buf_len = ssl->in_buf_len;
4229#else
4230 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4231#endif
4232
Darryl Greenb33cc762019-11-28 14:29:44 +00004233 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004234 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004235 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004236 }
4237
Paul Bakker48916f92012-09-16 19:57:18 +00004238 if( ssl->transform )
4239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004240 mbedtls_ssl_transform_free( ssl->transform );
4241 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004242 }
4243
4244 if( ssl->handshake )
4245 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004246 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004247 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4248 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004250 mbedtls_free( ssl->handshake );
4251 mbedtls_free( ssl->transform_negotiate );
4252 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004253 }
4254
Ronald Cron6f135e12021-12-08 16:57:54 +01004255#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004256 mbedtls_ssl_transform_free( ssl->transform_application );
4257 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004258#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004259
Paul Bakkerc0463502013-02-14 11:19:38 +01004260 if( ssl->session )
4261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004262 mbedtls_ssl_session_free( ssl->session );
4263 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004264 }
4265
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004266#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004267 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004268 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004269 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004270 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004271 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004272#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004273
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004274#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004275 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004276#endif
4277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004279
Paul Bakker86f04f42013-02-14 11:20:09 +01004280 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004281 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004282}
4283
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004284/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004285 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004286 */
4287void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4288{
4289 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4290}
4291
Gilles Peskineae270bf2021-06-02 00:05:29 +02004292/* The selection should be the same as mbedtls_x509_crt_profile_default in
4293 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004294 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004295 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004296 * about this list.
4297 */
Brett Warrene0edc842021-08-17 09:53:13 +01004298static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004299#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004300 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004301#endif
4302#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004303 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004304#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004305#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004306 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004307#endif
4308#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004309 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004310#endif
4311#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004312 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004313#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004314#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004315 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004316#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004317#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004318 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004319#endif
4320#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004321 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004322#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004323 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004324};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004325
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004326static int ssl_preset_suiteb_ciphersuites[] = {
4327 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4328 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4329 0
4330};
4331
Gilles Peskineeccd8882020-03-10 12:19:08 +01004332#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004333
Jerry Yu909df7b2022-01-22 11:56:27 +08004334/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004335 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004336 * rules SHOULD be upheld.
4337 * - No duplicate entries.
4338 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004339 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004340 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004341 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004342static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004343
Jerry Yued5e9f42022-01-26 11:21:34 +08004344#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \
4345 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4346 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
4347#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C &&
4348 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004349
Jerry Yu909df7b2022-01-22 11:56:27 +08004350#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \
4351 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4352 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
4353#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
4354 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4355
Jerry Yued5e9f42022-01-26 11:21:34 +08004356#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA512_C) && \
4357 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4358 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
4359#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
4360 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004361
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004362#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA512_C)
4363 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4364#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA512_C */
4365
4366#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA384_C)
4367 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4368#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA384_C */
4369
4370#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C)
4371 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4372#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */
4373
Jerry Yu693a47a2022-06-23 14:02:28 +08004374#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA512_C)
4375 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4376#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4377
4378#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA384_C)
4379 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4380#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4381
4382#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C)
4383 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4384#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4385
Gabor Mezei15b95a62022-05-09 16:37:58 +02004386 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004387};
4388
4389/* NOTICE: see above */
4390#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4391static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Jerry Yu713013f2022-01-17 18:16:35 +08004392#if defined(MBEDTLS_SHA512_C)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004393#if defined(MBEDTLS_ECDSA_C)
4394 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004395#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004396#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4397 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4398#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004399#if defined(MBEDTLS_RSA_C)
4400 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4401#endif
4402#endif /* MBEDTLS_SHA512_C */
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004403#if defined(MBEDTLS_SHA384_C)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004404#if defined(MBEDTLS_ECDSA_C)
4405 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004406#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004407#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4408 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4409#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004410#if defined(MBEDTLS_RSA_C)
4411 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4412#endif
4413#endif /* MBEDTLS_SHA384_C */
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004414#if defined(MBEDTLS_SHA256_C)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004415#if defined(MBEDTLS_ECDSA_C)
4416 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004417#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004418#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4419 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4420#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004421#if defined(MBEDTLS_RSA_C)
4422 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4423#endif
4424#endif /* MBEDTLS_SHA256_C */
Gabor Mezei15b95a62022-05-09 16:37:58 +02004425 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004426};
4427#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4428/* NOTICE: see above */
4429static uint16_t ssl_preset_suiteb_sig_algs[] = {
4430
Jerry Yu909df7b2022-01-22 11:56:27 +08004431#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \
4432 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4433 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
4434#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C &&
4435 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4436
Jerry Yu53037892022-01-25 11:02:06 +08004437#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \
4438 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4439 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
4440#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
4441 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004442
4443#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C)
4444 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4445#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004446
Jerry Yu53037892022-01-25 11:02:06 +08004447#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C)
4448 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4449#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4450
Gabor Mezei15b95a62022-05-09 16:37:58 +02004451 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004452};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004453
Jerry Yu909df7b2022-01-22 11:56:27 +08004454/* NOTICE: see above */
4455#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4456static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Jerry Yu713013f2022-01-17 18:16:35 +08004457#if defined(MBEDTLS_SHA256_C)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004458#if defined(MBEDTLS_ECDSA_C)
4459 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004460#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004461#if defined(MBEDTLS_RSA_C)
4462 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4463#endif
4464#endif /* MBEDTLS_SHA256_C */
Jerry Yu18c833e2022-01-25 10:55:47 +08004465#if defined(MBEDTLS_SHA384_C)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004466#if defined(MBEDTLS_ECDSA_C)
4467 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004468#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004469#if defined(MBEDTLS_RSA_C)
4470 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4471#endif
4472#endif /* MBEDTLS_SHA256_C */
Gabor Mezei15b95a62022-05-09 16:37:58 +02004473 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004474};
Jerry Yu909df7b2022-01-22 11:56:27 +08004475#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4476
Jerry Yu1a8b4812022-01-20 17:56:50 +08004477#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004478
Brett Warrene0edc842021-08-17 09:53:13 +01004479static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004480#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004481 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004482#endif
4483#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004484 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004485#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004486 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004487};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004488
Jerry Yu1a8b4812022-01-20 17:56:50 +08004489#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004490/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004491 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004492MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004493static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004494{
4495 size_t i, j;
4496 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004497
Gabor Mezei15b95a62022-05-09 16:37:58 +02004498 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004499 {
Jerry Yuf377d642022-01-25 10:43:59 +08004500 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004501 {
Jerry Yuf377d642022-01-25 10:43:59 +08004502 if( sig_algs[i] != sig_algs[j] )
4503 continue;
4504 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4505 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4506 sig_algs[i], j, i );
4507 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004508 }
4509 }
4510 return( ret );
4511}
4512
4513#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4514
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004515/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004516 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004517 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004518int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004519 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004520{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004521#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004522 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004523#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004524
Jerry Yu1a8b4812022-01-20 17:56:50 +08004525#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004526 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004527 {
4528 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4529 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4530 }
4531
Jerry Yuf377d642022-01-25 10:43:59 +08004532 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004533 {
4534 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4535 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4536 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004537
4538#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004539 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004540 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004541 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004542 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4543 }
4544
Jerry Yuf377d642022-01-25 10:43:59 +08004545 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004546 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004547 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004548 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4549 }
4550#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004551#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4552
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004553 /* Use the functions here so that they are covered in tests,
4554 * but otherwise access member directly for efficiency */
4555 mbedtls_ssl_conf_endpoint( conf, endpoint );
4556 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004557
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004558 /*
4559 * Things that are common to all presets
4560 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004561#if defined(MBEDTLS_SSL_CLI_C)
4562 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4563 {
4564 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4565#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4566 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4567#endif
4568 }
4569#endif
4570
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004571#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4572 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4573#endif
4574
4575#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4576 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4577#endif
4578
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004579#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004580 conf->f_cookie_write = ssl_cookie_write_dummy;
4581 conf->f_cookie_check = ssl_cookie_check_dummy;
4582#endif
4583
4584#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4585 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4586#endif
4587
Janos Follath088ce432017-04-10 12:42:31 +01004588#if defined(MBEDTLS_SSL_SRV_C)
4589 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004590 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004591#endif
4592
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004593#if defined(MBEDTLS_SSL_PROTO_DTLS)
4594 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4595 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4596#endif
4597
4598#if defined(MBEDTLS_SSL_RENEGOTIATION)
4599 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004600 memset( conf->renego_period, 0x00, 2 );
4601 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004602#endif
4603
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004604#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004605 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4606 {
4607 const unsigned char dhm_p[] =
4608 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4609 const unsigned char dhm_g[] =
4610 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004611
Hanno Beckere2defad2021-07-24 05:59:17 +01004612 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4613 dhm_p, sizeof( dhm_p ),
4614 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4615 {
4616 return( ret );
4617 }
4618 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004619#endif
4620
Ronald Cron6f135e12021-12-08 16:57:54 +01004621#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker71f1ed62021-07-24 06:01:47 +01004622 /*
4623 * Allow all TLS 1.3 key exchange modes by default.
4624 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004625 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004626#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004627
XiaokangQian4d3a6042022-04-21 13:46:17 +00004628 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4629 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004630#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004631 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004632 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004633#else
XiaokangQian060d8672022-04-21 09:24:56 +00004634 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004635#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004636 }
4637 else
4638 {
4639#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4640 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4641 {
4642 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4643 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4644 }
4645 else
4646 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4647 {
4648 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4649 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4650 }
4651#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4652 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4653 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4654#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4655 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4656 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4657#else
4658 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4659#endif
4660 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004661
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004662 /*
4663 * Preset-specific defaults
4664 */
4665 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004666 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004667 /*
4668 * NSA Suite B
4669 */
4670 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004671
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004672 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004673
4674#if defined(MBEDTLS_X509_CRT_PARSE_C)
4675 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004676#endif
4677
Gilles Peskineeccd8882020-03-10 12:19:08 +01004678#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004679#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4680 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4681 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4682 else
4683#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4684 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004685#endif
4686
Brett Warrene0edc842021-08-17 09:53:13 +01004687#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4688 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004689#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004690 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004691 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004692
4693 /*
4694 * Default
4695 */
4696 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004697
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004698 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004699
4700#if defined(MBEDTLS_X509_CRT_PARSE_C)
4701 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4702#endif
4703
Gilles Peskineeccd8882020-03-10 12:19:08 +01004704#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004705#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4706 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4707 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4708 else
4709#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4710 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004711#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004712
Brett Warrene0edc842021-08-17 09:53:13 +01004713#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4714 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004715#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004716 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004717
4718#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4719 conf->dhm_min_bitlen = 1024;
4720#endif
4721 }
4722
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004723 return( 0 );
4724}
4725
4726/*
4727 * Free mbedtls_ssl_config
4728 */
4729void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4730{
4731#if defined(MBEDTLS_DHM_C)
4732 mbedtls_mpi_free( &conf->dhm_P );
4733 mbedtls_mpi_free( &conf->dhm_G );
4734#endif
4735
Gilles Peskineeccd8882020-03-10 12:19:08 +01004736#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004737#if defined(MBEDTLS_USE_PSA_CRYPTO)
4738 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4739 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004740 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4741 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004742#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004743 if( conf->psk != NULL )
4744 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004745 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004746 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004747 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004748 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004749 }
4750
4751 if( conf->psk_identity != NULL )
4752 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004753 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004754 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004755 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004756 conf->psk_identity_len = 0;
4757 }
4758#endif
4759
4760#if defined(MBEDTLS_X509_CRT_PARSE_C)
4761 ssl_key_cert_free( conf->key_cert );
4762#endif
4763
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004764 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004765}
4766
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004767#if defined(MBEDTLS_PK_C) && \
4768 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004769/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004770 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004771 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004772unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004773{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004774#if defined(MBEDTLS_RSA_C)
4775 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4776 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004777#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004778#if defined(MBEDTLS_ECDSA_C)
4779 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4780 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004781#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004782 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004783}
4784
Hanno Becker7e5437a2017-04-28 17:15:26 +01004785unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4786{
4787 switch( type ) {
4788 case MBEDTLS_PK_RSA:
4789 return( MBEDTLS_SSL_SIG_RSA );
4790 case MBEDTLS_PK_ECDSA:
4791 case MBEDTLS_PK_ECKEY:
4792 return( MBEDTLS_SSL_SIG_ECDSA );
4793 default:
4794 return( MBEDTLS_SSL_SIG_ANON );
4795 }
4796}
4797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004798mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004799{
4800 switch( sig )
4801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004802#if defined(MBEDTLS_RSA_C)
4803 case MBEDTLS_SSL_SIG_RSA:
4804 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004805#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004806#if defined(MBEDTLS_ECDSA_C)
4807 case MBEDTLS_SSL_SIG_ECDSA:
4808 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004809#endif
4810 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004811 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004812 }
4813}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004814#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004815
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004816/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004817 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004819mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004820{
4821 switch( hash )
4822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004823#if defined(MBEDTLS_MD5_C)
4824 case MBEDTLS_SSL_HASH_MD5:
4825 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004826#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004827#if defined(MBEDTLS_SHA1_C)
4828 case MBEDTLS_SSL_HASH_SHA1:
4829 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004830#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004831#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004832 case MBEDTLS_SSL_HASH_SHA224:
4833 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004834#endif
4835#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004836 case MBEDTLS_SSL_HASH_SHA256:
4837 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004838#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004839#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004840 case MBEDTLS_SSL_HASH_SHA384:
4841 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004842#endif
4843#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004844 case MBEDTLS_SSL_HASH_SHA512:
4845 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004846#endif
4847 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004848 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004849 }
4850}
4851
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004852/*
4853 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4854 */
4855unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4856{
4857 switch( md )
4858 {
4859#if defined(MBEDTLS_MD5_C)
4860 case MBEDTLS_MD_MD5:
4861 return( MBEDTLS_SSL_HASH_MD5 );
4862#endif
4863#if defined(MBEDTLS_SHA1_C)
4864 case MBEDTLS_MD_SHA1:
4865 return( MBEDTLS_SSL_HASH_SHA1 );
4866#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004867#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004868 case MBEDTLS_MD_SHA224:
4869 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004870#endif
4871#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004872 case MBEDTLS_MD_SHA256:
4873 return( MBEDTLS_SSL_HASH_SHA256 );
4874#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004875#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004876 case MBEDTLS_MD_SHA384:
4877 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004878#endif
4879#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004880 case MBEDTLS_MD_SHA512:
4881 return( MBEDTLS_SSL_HASH_SHA512 );
4882#endif
4883 default:
4884 return( MBEDTLS_SSL_HASH_NONE );
4885 }
4886}
4887
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004888/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004889 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004890 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004891 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004892int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004893{
Brett Warrene0edc842021-08-17 09:53:13 +01004894 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004895
Brett Warrene0edc842021-08-17 09:53:13 +01004896 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004897 return( -1 );
4898
Brett Warrene0edc842021-08-17 09:53:13 +01004899 for( ; *group_list != 0; group_list++ )
4900 {
4901 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004902 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01004903 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004904
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004905 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004906}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004907
4908#if defined(MBEDTLS_ECP_C)
4909/*
4910 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
4911 */
4912int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
4913{
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01004914 uint16_t tls_id = mbedtls_ecp_curve_info_from_grp_id( grp_id )->tls_id;
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004915 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
4916}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004917#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004919#if defined(MBEDTLS_X509_CRT_PARSE_C)
4920int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
4921 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004922 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02004923 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004924{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004925 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004926 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004927 const char *ext_oid;
4928 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004930 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004931 {
4932 /* Server part of the key exchange */
4933 switch( ciphersuite->key_exchange )
4934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004935 case MBEDTLS_KEY_EXCHANGE_RSA:
4936 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004937 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004938 break;
4939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004940 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
4941 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
4942 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
4943 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004944 break;
4945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004946 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
4947 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004948 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004949 break;
4950
4951 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004952 case MBEDTLS_KEY_EXCHANGE_NONE:
4953 case MBEDTLS_KEY_EXCHANGE_PSK:
4954 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
4955 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02004956 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004957 usage = 0;
4958 }
4959 }
4960 else
4961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004962 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
4963 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004964 }
4965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004966 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004967 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004968 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004969 ret = -1;
4970 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004972 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004974 ext_oid = MBEDTLS_OID_SERVER_AUTH;
4975 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004976 }
4977 else
4978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004979 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
4980 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004981 }
4982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004983 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004984 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004985 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004986 ret = -1;
4987 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004988
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004989 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004990}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004991#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004992
Jerry Yu148165c2021-09-24 23:20:59 +08004993#if defined(MBEDTLS_USE_PSA_CRYPTO)
4994int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
4995 const mbedtls_md_type_t md,
4996 unsigned char *dst,
4997 size_t dst_len,
4998 size_t *olen )
4999{
Ronald Cronf6893e12022-01-07 22:09:01 +01005000 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5001 psa_hash_operation_t *hash_operation_to_clone;
5002 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5003
Jerry Yu148165c2021-09-24 23:20:59 +08005004 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005005
5006 switch( md )
5007 {
5008#if defined(MBEDTLS_SHA384_C)
5009 case MBEDTLS_MD_SHA384:
5010 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5011 break;
5012#endif
5013
5014#if defined(MBEDTLS_SHA256_C)
5015 case MBEDTLS_MD_SHA256:
5016 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5017 break;
5018#endif
5019
5020 default:
5021 goto exit;
5022 }
5023
5024 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5025 if( status != PSA_SUCCESS )
5026 goto exit;
5027
5028 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5029 if( status != PSA_SUCCESS )
5030 goto exit;
5031
5032exit:
Ronald Cronb788c042022-02-15 09:14:15 +01005033 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005034}
5035#else /* MBEDTLS_USE_PSA_CRYPTO */
5036
Jerry Yu24c0ec32021-09-09 14:21:07 +08005037#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005038MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005039static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5040 unsigned char *dst,
5041 size_t dst_len,
5042 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005043{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005044 int ret;
5045 mbedtls_sha512_context sha512;
5046
5047 if( dst_len < 48 )
5048 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5049
5050 mbedtls_sha512_init( &sha512 );
5051 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
5052
5053 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5054 {
5055 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5056 goto exit;
5057 }
5058
5059 *olen = 48;
5060
5061exit:
5062
5063 mbedtls_sha512_free( &sha512 );
5064 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005065}
5066#endif /* MBEDTLS_SHA384_C */
5067
5068#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005069MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005070static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5071 unsigned char *dst,
5072 size_t dst_len,
5073 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005074{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005075 int ret;
5076 mbedtls_sha256_context sha256;
5077
5078 if( dst_len < 32 )
5079 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5080
5081 mbedtls_sha256_init( &sha256 );
5082 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005083
Jerry Yu24c0ec32021-09-09 14:21:07 +08005084 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5085 {
5086 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5087 goto exit;
5088 }
5089
5090 *olen = 32;
5091
5092exit:
5093
5094 mbedtls_sha256_free( &sha256 );
5095 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005096}
5097#endif /* MBEDTLS_SHA256_C */
5098
Jerry Yu000f9762021-09-14 11:12:51 +08005099int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5100 const mbedtls_md_type_t md,
5101 unsigned char *dst,
5102 size_t dst_len,
5103 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005104{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005105 switch( md )
5106 {
5107
Jerry Yu24c0ec32021-09-09 14:21:07 +08005108#if defined(MBEDTLS_SHA384_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005109 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005110 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08005111#endif /* MBEDTLS_SHA384_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005112
Jerry Yu24c0ec32021-09-09 14:21:07 +08005113#if defined(MBEDTLS_SHA256_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005114 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005115 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08005116#endif /* MBEDTLS_SHA256_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005117
5118 default:
5119 break;
5120 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005121 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005122}
XiaokangQian647719a2021-12-07 09:16:29 +00005123
Jerry Yu148165c2021-09-24 23:20:59 +08005124#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005125
Gabor Mezei078e8032022-04-27 21:17:56 +02005126#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5127/* mbedtls_ssl_parse_sig_alg_ext()
5128 *
5129 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5130 * value (TLS 1.3 RFC8446):
5131 * enum {
5132 * ....
5133 * ecdsa_secp256r1_sha256( 0x0403 ),
5134 * ecdsa_secp384r1_sha384( 0x0503 ),
5135 * ecdsa_secp521r1_sha512( 0x0603 ),
5136 * ....
5137 * } SignatureScheme;
5138 *
5139 * struct {
5140 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5141 * } SignatureSchemeList;
5142 *
5143 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5144 * value (TLS 1.2 RFC5246):
5145 * enum {
5146 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5147 * sha512(6), (255)
5148 * } HashAlgorithm;
5149 *
5150 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5151 * SignatureAlgorithm;
5152 *
5153 * struct {
5154 * HashAlgorithm hash;
5155 * SignatureAlgorithm signature;
5156 * } SignatureAndHashAlgorithm;
5157 *
5158 * SignatureAndHashAlgorithm
5159 * supported_signature_algorithms<2..2^16-2>;
5160 *
5161 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5162 * generalization of the TLS 1.2 signature algorithm extension.
5163 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5164 * `SignatureScheme` field of TLS 1.3
5165 *
5166 */
5167int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5168 const unsigned char *buf,
5169 const unsigned char *end )
5170{
5171 const unsigned char *p = buf;
5172 size_t supported_sig_algs_len = 0;
5173 const unsigned char *supported_sig_algs_end;
5174 uint16_t sig_alg;
5175 uint32_t common_idx = 0;
5176
5177 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5178 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5179 p += 2;
5180
5181 memset( ssl->handshake->received_sig_algs, 0,
5182 sizeof(ssl->handshake->received_sig_algs) );
5183
5184 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5185 supported_sig_algs_end = p + supported_sig_algs_len;
5186 while( p < supported_sig_algs_end )
5187 {
5188 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5189 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5190 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005191 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5192 sig_alg,
5193 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005194#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005195 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005196 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5197 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5198 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005199 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005200 }
5201#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005202
Jerry Yuf3b46b52022-06-19 16:52:27 +08005203 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5204 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005205
5206 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5207 {
5208 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5209 common_idx += 1;
5210 }
5211 }
5212 /* Check that we consumed all the message. */
5213 if( p != end )
5214 {
5215 MBEDTLS_SSL_DEBUG_MSG( 1,
5216 ( "Signature algorithms extension length misaligned" ) );
5217 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5218 MBEDTLS_ERR_SSL_DECODE_ERROR );
5219 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5220 }
5221
5222 if( common_idx == 0 )
5223 {
5224 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5225 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5226 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5227 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5228 }
5229
Gabor Mezei15b95a62022-05-09 16:37:58 +02005230 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005231 return( 0 );
5232}
5233
5234#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5235
Jerry Yudc7bd172022-02-17 13:44:15 +08005236#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5237
5238#if defined(MBEDTLS_USE_PSA_CRYPTO)
5239
5240static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5241 mbedtls_svc_key_id_t key,
5242 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005243 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005244 const unsigned char* seed, size_t seed_length,
5245 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005246 const unsigned char* other_secret,
5247 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005248 size_t capacity )
5249{
5250 psa_status_t status;
5251
5252 status = psa_key_derivation_setup( derivation, alg );
5253 if( status != PSA_SUCCESS )
5254 return( status );
5255
5256 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5257 {
5258 status = psa_key_derivation_input_bytes( derivation,
5259 PSA_KEY_DERIVATION_INPUT_SEED,
5260 seed, seed_length );
5261 if( status != PSA_SUCCESS )
5262 return( status );
5263
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005264 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005265 {
5266 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005267 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5268 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005269 if( status != PSA_SUCCESS )
5270 return( status );
5271 }
5272
Jerry Yudc7bd172022-02-17 13:44:15 +08005273 if( mbedtls_svc_key_id_is_null( key ) )
5274 {
5275 status = psa_key_derivation_input_bytes(
5276 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005277 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005278 }
5279 else
5280 {
5281 status = psa_key_derivation_input_key(
5282 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5283 }
5284 if( status != PSA_SUCCESS )
5285 return( status );
5286
5287 status = psa_key_derivation_input_bytes( derivation,
5288 PSA_KEY_DERIVATION_INPUT_LABEL,
5289 label, label_length );
5290 if( status != PSA_SUCCESS )
5291 return( status );
5292 }
5293 else
5294 {
5295 return( PSA_ERROR_NOT_SUPPORTED );
5296 }
5297
5298 status = psa_key_derivation_set_capacity( derivation, capacity );
5299 if( status != PSA_SUCCESS )
5300 return( status );
5301
5302 return( PSA_SUCCESS );
5303}
5304
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005305MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005306static int tls_prf_generic( mbedtls_md_type_t md_type,
5307 const unsigned char *secret, size_t slen,
5308 const char *label,
5309 const unsigned char *random, size_t rlen,
5310 unsigned char *dstbuf, size_t dlen )
5311{
5312 psa_status_t status;
5313 psa_algorithm_t alg;
5314 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5315 psa_key_derivation_operation_t derivation =
5316 PSA_KEY_DERIVATION_OPERATION_INIT;
5317
5318 if( md_type == MBEDTLS_MD_SHA384 )
5319 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5320 else
5321 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5322
5323 /* Normally a "secret" should be long enough to be impossible to
5324 * find by brute force, and in particular should not be empty. But
5325 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5326 * and for this use case it makes sense to have a 0-length "secret".
5327 * Since the key API doesn't allow importing a key of length 0,
5328 * keep master_key=0, which setup_psa_key_derivation() understands
5329 * to mean a 0-length "secret" input. */
5330 if( slen != 0 )
5331 {
5332 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5333 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5334 psa_set_key_algorithm( &key_attributes, alg );
5335 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5336
5337 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5338 if( status != PSA_SUCCESS )
5339 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5340 }
5341
5342 status = setup_psa_key_derivation( &derivation,
5343 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005344 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005345 random, rlen,
5346 (unsigned char const *) label,
5347 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005348 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005349 dlen );
5350 if( status != PSA_SUCCESS )
5351 {
5352 psa_key_derivation_abort( &derivation );
5353 psa_destroy_key( master_key );
5354 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5355 }
5356
5357 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5358 if( status != PSA_SUCCESS )
5359 {
5360 psa_key_derivation_abort( &derivation );
5361 psa_destroy_key( master_key );
5362 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5363 }
5364
5365 status = psa_key_derivation_abort( &derivation );
5366 if( status != PSA_SUCCESS )
5367 {
5368 psa_destroy_key( master_key );
5369 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5370 }
5371
5372 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5373 status = psa_destroy_key( master_key );
5374 if( status != PSA_SUCCESS )
5375 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5376
5377 return( 0 );
5378}
5379
5380#else /* MBEDTLS_USE_PSA_CRYPTO */
5381
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005382MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005383static int tls_prf_generic( mbedtls_md_type_t md_type,
5384 const unsigned char *secret, size_t slen,
5385 const char *label,
5386 const unsigned char *random, size_t rlen,
5387 unsigned char *dstbuf, size_t dlen )
5388{
5389 size_t nb;
5390 size_t i, j, k, md_len;
5391 unsigned char *tmp;
5392 size_t tmp_len = 0;
5393 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5394 const mbedtls_md_info_t *md_info;
5395 mbedtls_md_context_t md_ctx;
5396 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5397
5398 mbedtls_md_init( &md_ctx );
5399
5400 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5401 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5402
5403 md_len = mbedtls_md_get_size( md_info );
5404
5405 tmp_len = md_len + strlen( label ) + rlen;
5406 tmp = mbedtls_calloc( 1, tmp_len );
5407 if( tmp == NULL )
5408 {
5409 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5410 goto exit;
5411 }
5412
5413 nb = strlen( label );
5414 memcpy( tmp + md_len, label, nb );
5415 memcpy( tmp + md_len + nb, random, rlen );
5416 nb += rlen;
5417
5418 /*
5419 * Compute P_<hash>(secret, label + random)[0..dlen]
5420 */
5421 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5422 goto exit;
5423
5424 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5425 if( ret != 0 )
5426 goto exit;
5427 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5428 if( ret != 0 )
5429 goto exit;
5430 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5431 if( ret != 0 )
5432 goto exit;
5433
5434 for( i = 0; i < dlen; i += md_len )
5435 {
5436 ret = mbedtls_md_hmac_reset ( &md_ctx );
5437 if( ret != 0 )
5438 goto exit;
5439 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5440 if( ret != 0 )
5441 goto exit;
5442 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5443 if( ret != 0 )
5444 goto exit;
5445
5446 ret = mbedtls_md_hmac_reset ( &md_ctx );
5447 if( ret != 0 )
5448 goto exit;
5449 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5450 if( ret != 0 )
5451 goto exit;
5452 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5453 if( ret != 0 )
5454 goto exit;
5455
5456 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5457
5458 for( j = 0; j < k; j++ )
5459 dstbuf[i + j] = h_i[j];
5460 }
5461
5462exit:
5463 mbedtls_md_free( &md_ctx );
5464
5465 mbedtls_platform_zeroize( tmp, tmp_len );
5466 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5467
5468 mbedtls_free( tmp );
5469
5470 return( ret );
5471}
5472#endif /* MBEDTLS_USE_PSA_CRYPTO */
5473
5474#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005475MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005476static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5477 const char *label,
5478 const unsigned char *random, size_t rlen,
5479 unsigned char *dstbuf, size_t dlen )
5480{
5481 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5482 label, random, rlen, dstbuf, dlen ) );
5483}
5484#endif /* MBEDTLS_SHA256_C */
5485
5486#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005487MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005488static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5489 const char *label,
5490 const unsigned char *random, size_t rlen,
5491 unsigned char *dstbuf, size_t dlen )
5492{
5493 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5494 label, random, rlen, dstbuf, dlen ) );
5495}
5496#endif /* MBEDTLS_SHA384_C */
5497
Jerry Yuf009d862022-02-17 14:01:37 +08005498/*
5499 * Set appropriate PRF function and other SSL / TLS1.2 functions
5500 *
5501 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005502 * - hash associated with the ciphersuite (only used by TLS 1.2)
5503 *
5504 * Outputs:
5505 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5506 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005507MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005508static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005509 mbedtls_md_type_t hash )
5510{
Jerry Yuf009d862022-02-17 14:01:37 +08005511#if defined(MBEDTLS_SHA384_C)
Ronald Cron81591aa2022-03-07 09:05:51 +01005512 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005513 {
5514 handshake->tls_prf = tls_prf_sha384;
5515 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5516 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5517 }
5518 else
5519#endif
5520#if defined(MBEDTLS_SHA256_C)
Jerry Yuf009d862022-02-17 14:01:37 +08005521 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005522 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005523 handshake->tls_prf = tls_prf_sha256;
5524 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5525 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5526 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005527#else
Jerry Yuf009d862022-02-17 14:01:37 +08005528 {
Jerry Yuf009d862022-02-17 14:01:37 +08005529 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005530 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005531 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5532 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005533#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005534
5535 return( 0 );
5536}
Jerry Yud6ab2352022-02-17 14:03:43 +08005537
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005538/*
5539 * Compute master secret if needed
5540 *
5541 * Parameters:
5542 * [in/out] handshake
5543 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5544 * (PSA-PSK) ciphersuite_info, psk_opaque
5545 * [out] premaster (cleared)
5546 * [out] master
5547 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5548 * debug: conf->f_dbg, conf->p_dbg
5549 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005550 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005551 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005552MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005553static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5554 unsigned char *master,
5555 const mbedtls_ssl_context *ssl )
5556{
5557 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5558
5559 /* cf. RFC 5246, Section 8.1:
5560 * "The master secret is always exactly 48 bytes in length." */
5561 size_t const master_secret_len = 48;
5562
5563#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5564 unsigned char session_hash[48];
5565#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5566
5567 /* The label for the KDF used for key expansion.
5568 * This is either "master secret" or "extended master secret"
5569 * depending on whether the Extended Master Secret extension
5570 * is used. */
5571 char const *lbl = "master secret";
5572
Przemek Stekielae4ed302022-04-05 17:15:55 +02005573 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005574 * - If the Extended Master Secret extension is not used,
5575 * this is ClientHello.Random + ServerHello.Random
5576 * (see Sect. 8.1 in RFC 5246).
5577 * - If the Extended Master Secret extension is used,
5578 * this is the transcript of the handshake so far.
5579 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005580 unsigned char const *seed = handshake->randbytes;
5581 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005582
5583#if !defined(MBEDTLS_DEBUG_C) && \
5584 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5585 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5586 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5587 ssl = NULL; /* make sure we don't use it except for those cases */
5588 (void) ssl;
5589#endif
5590
5591 if( handshake->resume != 0 )
5592 {
5593 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5594 return( 0 );
5595 }
5596
5597#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5598 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5599 {
5600 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005601 seed = session_hash;
5602 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005603
5604 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005605 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005606 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005607#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005608
Przemek Stekiel99114f32022-04-22 11:20:09 +02005609#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005610 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005611 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005612 {
5613 /* Perform PSK-to-MS expansion in a single step. */
5614 psa_status_t status;
5615 psa_algorithm_t alg;
5616 mbedtls_svc_key_id_t psk;
5617 psa_key_derivation_operation_t derivation =
5618 PSA_KEY_DERIVATION_OPERATION_INIT;
5619 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5620
5621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5622
5623 psk = mbedtls_ssl_get_opaque_psk( ssl );
5624
5625 if( hash_alg == MBEDTLS_MD_SHA384 )
5626 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5627 else
5628 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5629
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005630 size_t other_secret_len = 0;
5631 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005632
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005633 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005634 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005635 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005636 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005637 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005638 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005639 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005640 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005641 other_secret_len = 48;
5642 other_secret = handshake->premaster + 2;
5643 break;
5644 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005645 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005646 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5647 other_secret = handshake->premaster + 2;
5648 break;
5649 default:
5650 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005651 }
5652
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005653 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005654 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005655 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005656 (unsigned char const *) lbl,
5657 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005658 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005659 master_secret_len );
5660 if( status != PSA_SUCCESS )
5661 {
5662 psa_key_derivation_abort( &derivation );
5663 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5664 }
5665
5666 status = psa_key_derivation_output_bytes( &derivation,
5667 master,
5668 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_abort( &derivation );
5676 if( status != PSA_SUCCESS )
5677 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5678 }
5679 else
5680#endif
5681 {
5682 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005683 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005684 master,
5685 master_secret_len );
5686 if( ret != 0 )
5687 {
5688 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5689 return( ret );
5690 }
5691
5692 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5693 handshake->premaster,
5694 handshake->pmslen );
5695
5696 mbedtls_platform_zeroize( handshake->premaster,
5697 sizeof(handshake->premaster) );
5698 }
5699
5700 return( 0 );
5701}
5702
Jerry Yud62f87e2022-02-17 14:09:02 +08005703int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5704{
5705 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5706 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5707 ssl->handshake->ciphersuite_info;
5708
5709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5710
5711 /* Set PRF, calc_verify and calc_finished function pointers */
5712 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005713 ciphersuite_info->mac );
5714 if( ret != 0 )
5715 {
5716 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5717 return( ret );
5718 }
5719
5720 /* Compute master secret if needed */
5721 ret = ssl_compute_master( ssl->handshake,
5722 ssl->session_negotiate->master,
5723 ssl );
5724 if( ret != 0 )
5725 {
5726 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5727 return( ret );
5728 }
5729
5730 /* Swap the client and server random values:
5731 * - MS derivation wanted client+server (RFC 5246 8.1)
5732 * - key derivation wants server+client (RFC 5246 6.3) */
5733 {
5734 unsigned char tmp[64];
5735 memcpy( tmp, ssl->handshake->randbytes, 64 );
5736 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5737 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5738 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5739 }
5740
5741 /* Populate transform structure */
5742 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5743 ssl->session_negotiate->ciphersuite,
5744 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005745#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005746 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005747#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005748 ssl->handshake->tls_prf,
5749 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005750 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005751 ssl->conf->endpoint,
5752 ssl );
5753 if( ret != 0 )
5754 {
5755 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5756 return( ret );
5757 }
5758
5759 /* We no longer need Server/ClientHello.random values */
5760 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5761 sizeof( ssl->handshake->randbytes ) );
5762
5763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5764
5765 return( 0 );
5766}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005767
Ronald Cron4dcbca92022-03-07 10:21:40 +01005768int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5769{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005770 switch( md )
5771 {
5772#if defined(MBEDTLS_SHA384_C)
5773 case MBEDTLS_SSL_HASH_SHA384:
5774 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5775 break;
5776#endif
5777#if defined(MBEDTLS_SHA256_C)
5778 case MBEDTLS_SSL_HASH_SHA256:
5779 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5780 break;
5781#endif
5782 default:
5783 return( -1 );
5784 }
5785
Ronald Cronc2f13a02022-03-07 10:25:24 +01005786 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005787}
5788
Jerry Yu8392e0d2022-02-17 14:10:24 +08005789#if defined(MBEDTLS_SHA256_C)
5790void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5791 unsigned char *hash,
5792 size_t *hlen )
5793{
5794#if defined(MBEDTLS_USE_PSA_CRYPTO)
5795 size_t hash_size;
5796 psa_status_t status;
5797 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5798
5799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5800 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5801 if( status != PSA_SUCCESS )
5802 {
5803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5804 return;
5805 }
5806
5807 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5808 if( status != PSA_SUCCESS )
5809 {
5810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5811 return;
5812 }
5813
5814 *hlen = 32;
5815 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5817#else
5818 mbedtls_sha256_context sha256;
5819
5820 mbedtls_sha256_init( &sha256 );
5821
5822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5823
5824 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5825 mbedtls_sha256_finish( &sha256, hash );
5826
5827 *hlen = 32;
5828
5829 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5831
5832 mbedtls_sha256_free( &sha256 );
5833#endif /* MBEDTLS_USE_PSA_CRYPTO */
5834 return;
5835}
5836#endif /* MBEDTLS_SHA256_C */
5837
Jerry Yuc1cb3842022-02-17 14:13:48 +08005838#if defined(MBEDTLS_SHA384_C)
5839void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5840 unsigned char *hash,
5841 size_t *hlen )
5842{
5843#if defined(MBEDTLS_USE_PSA_CRYPTO)
5844 size_t hash_size;
5845 psa_status_t status;
5846 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5847
5848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5849 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5850 if( status != PSA_SUCCESS )
5851 {
5852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5853 return;
5854 }
5855
5856 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5857 if( status != PSA_SUCCESS )
5858 {
5859 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5860 return;
5861 }
5862
5863 *hlen = 48;
5864 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5866#else
5867 mbedtls_sha512_context sha512;
5868
5869 mbedtls_sha512_init( &sha512 );
5870
5871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
5872
5873 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
5874 mbedtls_sha512_finish( &sha512, hash );
5875
5876 *hlen = 48;
5877
5878 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5880
5881 mbedtls_sha512_free( &sha512 );
5882#endif /* MBEDTLS_USE_PSA_CRYPTO */
5883 return;
5884}
5885#endif /* MBEDTLS_SHA384_C */
5886
Neil Armstrong80f6f322022-05-03 17:56:38 +02005887#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
5888 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08005889int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
5890{
5891 unsigned char *p = ssl->handshake->premaster;
5892 unsigned char *end = p + sizeof( ssl->handshake->premaster );
5893 const unsigned char *psk = NULL;
5894 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005895 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08005896
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005897 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08005898 {
5899 /*
5900 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005901 * checked before calling this function.
5902 *
5903 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005904 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08005905 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005906 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5907 {
5908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5909 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5910 }
Jerry Yuce3dca42022-02-17 14:16:37 +08005911 }
5912
5913 /*
5914 * PMS = struct {
5915 * opaque other_secret<0..2^16-1>;
5916 * opaque psk<0..2^16-1>;
5917 * };
5918 * with "other_secret" depending on the particular key exchange
5919 */
5920#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5921 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
5922 {
5923 if( end - p < 2 )
5924 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5925
5926 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5927 p += 2;
5928
5929 if( end < p || (size_t)( end - p ) < psk_len )
5930 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5931
5932 memset( p, 0, psk_len );
5933 p += psk_len;
5934 }
5935 else
5936#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5937#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
5938 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5939 {
5940 /*
5941 * other_secret already set by the ClientKeyExchange message,
5942 * and is 48 bytes long
5943 */
5944 if( end - p < 2 )
5945 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5946
5947 *p++ = 0;
5948 *p++ = 48;
5949 p += 48;
5950 }
5951 else
5952#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
5953#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
5954 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5955 {
5956 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5957 size_t len;
5958
5959 /* Write length only when we know the actual value */
5960 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
5961 p + 2, end - ( p + 2 ), &len,
5962 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5963 {
5964 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
5965 return( ret );
5966 }
5967 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
5968 p += 2 + len;
5969
5970 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
5971 }
5972 else
5973#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02005974#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08005975 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
5976 {
5977 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5978 size_t zlen;
5979
5980 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
5981 p + 2, end - ( p + 2 ),
5982 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5983 {
5984 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
5985 return( ret );
5986 }
5987
5988 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
5989 p += 2 + zlen;
5990
5991 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
5992 MBEDTLS_DEBUG_ECDH_Z );
5993 }
5994 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02005995#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08005996 {
5997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5998 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5999 }
6000
6001 /* opaque psk<0..2^16-1>; */
6002 if( end - p < 2 )
6003 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6004
6005 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6006 p += 2;
6007
6008 if( end < p || (size_t)( end - p ) < psk_len )
6009 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6010
6011 memcpy( p, psk, psk_len );
6012 p += psk_len;
6013
6014 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6015
6016 return( 0 );
6017}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006018#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006019
6020#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006021MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006022static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6023
6024#if defined(MBEDTLS_SSL_PROTO_DTLS)
6025int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6026{
6027 /* If renegotiation is not enforced, retransmit until we would reach max
6028 * timeout if we were using the usual handshake doubling scheme */
6029 if( ssl->conf->renego_max_records < 0 )
6030 {
6031 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6032 unsigned char doublings = 1;
6033
6034 while( ratio != 0 )
6035 {
6036 ++doublings;
6037 ratio >>= 1;
6038 }
6039
6040 if( ++ssl->renego_records_seen > doublings )
6041 {
6042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6043 return( 0 );
6044 }
6045 }
6046
6047 return( ssl_write_hello_request( ssl ) );
6048}
6049#endif
6050#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006051
Jerry Yud9526692022-02-17 14:23:47 +08006052/*
6053 * Handshake functions
6054 */
6055#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6056/* No certificate support -> dummy functions */
6057int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6058{
6059 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6060 ssl->handshake->ciphersuite_info;
6061
6062 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6063
6064 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6065 {
6066 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6067 ssl->state++;
6068 return( 0 );
6069 }
6070
6071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6072 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6073}
6074
6075int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6076{
6077 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6078 ssl->handshake->ciphersuite_info;
6079
6080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6081
6082 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6083 {
6084 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6085 ssl->state++;
6086 return( 0 );
6087 }
6088
6089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6090 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6091}
6092
6093#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6094/* Some certificate support -> implement write and parse */
6095
6096int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6097{
6098 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6099 size_t i, n;
6100 const mbedtls_x509_crt *crt;
6101 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6102 ssl->handshake->ciphersuite_info;
6103
6104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6105
6106 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6107 {
6108 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6109 ssl->state++;
6110 return( 0 );
6111 }
6112
6113#if defined(MBEDTLS_SSL_CLI_C)
6114 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6115 {
6116 if( ssl->handshake->client_auth == 0 )
6117 {
6118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6119 ssl->state++;
6120 return( 0 );
6121 }
6122 }
6123#endif /* MBEDTLS_SSL_CLI_C */
6124#if defined(MBEDTLS_SSL_SRV_C)
6125 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6126 {
6127 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6128 {
6129 /* Should never happen because we shouldn't have picked the
6130 * ciphersuite if we don't have a certificate. */
6131 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6132 }
6133 }
6134#endif
6135
6136 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6137
6138 /*
6139 * 0 . 0 handshake type
6140 * 1 . 3 handshake length
6141 * 4 . 6 length of all certs
6142 * 7 . 9 length of cert. 1
6143 * 10 . n-1 peer certificate
6144 * n . n+2 length of cert. 2
6145 * n+3 . ... upper level cert, etc.
6146 */
6147 i = 7;
6148 crt = mbedtls_ssl_own_cert( ssl );
6149
6150 while( crt != NULL )
6151 {
6152 n = crt->raw.len;
6153 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6154 {
6155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6156 " > %" MBEDTLS_PRINTF_SIZET,
6157 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6158 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6159 }
6160
6161 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6162 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6163 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6164
6165 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6166 i += n; crt = crt->next;
6167 }
6168
6169 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6170 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6171 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6172
6173 ssl->out_msglen = i;
6174 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6175 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6176
6177 ssl->state++;
6178
6179 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6180 {
6181 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6182 return( ret );
6183 }
6184
6185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6186
6187 return( ret );
6188}
6189
6190#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6191
6192#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006193MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006194static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6195 unsigned char *crt_buf,
6196 size_t crt_buf_len )
6197{
6198 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6199
6200 if( peer_crt == NULL )
6201 return( -1 );
6202
6203 if( peer_crt->raw.len != crt_buf_len )
6204 return( -1 );
6205
6206 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6207}
6208#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006209MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006210static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6211 unsigned char *crt_buf,
6212 size_t crt_buf_len )
6213{
6214 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6215 unsigned char const * const peer_cert_digest =
6216 ssl->session->peer_cert_digest;
6217 mbedtls_md_type_t const peer_cert_digest_type =
6218 ssl->session->peer_cert_digest_type;
6219 mbedtls_md_info_t const * const digest_info =
6220 mbedtls_md_info_from_type( peer_cert_digest_type );
6221 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6222 size_t digest_len;
6223
6224 if( peer_cert_digest == NULL || digest_info == NULL )
6225 return( -1 );
6226
6227 digest_len = mbedtls_md_get_size( digest_info );
6228 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6229 return( -1 );
6230
6231 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6232 if( ret != 0 )
6233 return( -1 );
6234
6235 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6236}
6237#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6238#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6239
6240/*
6241 * Once the certificate message is read, parse it into a cert chain and
6242 * perform basic checks, but leave actual verification to the caller
6243 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006244MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006245static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6246 mbedtls_x509_crt *chain )
6247{
6248 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6249#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6250 int crt_cnt=0;
6251#endif
6252 size_t i, n;
6253 uint8_t alert;
6254
6255 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6256 {
6257 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6258 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6259 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6260 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6261 }
6262
6263 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6264 {
6265 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6266 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6267 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6268 }
6269
6270 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6271 {
6272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6273 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6274 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6275 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6276 }
6277
6278 i = mbedtls_ssl_hs_hdr_len( ssl );
6279
6280 /*
6281 * Same message structure as in mbedtls_ssl_write_certificate()
6282 */
6283 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6284
6285 if( ssl->in_msg[i] != 0 ||
6286 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6287 {
6288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6289 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6290 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6291 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6292 }
6293
6294 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6295 i += 3;
6296
6297 /* Iterate through and parse the CRTs in the provided chain. */
6298 while( i < ssl->in_hslen )
6299 {
6300 /* Check that there's room for the next CRT's length fields. */
6301 if ( i + 3 > ssl->in_hslen ) {
6302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6303 mbedtls_ssl_send_alert_message( ssl,
6304 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6305 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6306 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6307 }
6308 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6309 * anything beyond 2**16 ~ 64K. */
6310 if( ssl->in_msg[i] != 0 )
6311 {
6312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6313 mbedtls_ssl_send_alert_message( ssl,
6314 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6315 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6316 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6317 }
6318
6319 /* Read length of the next CRT in the chain. */
6320 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6321 | (unsigned int) ssl->in_msg[i + 2];
6322 i += 3;
6323
6324 if( n < 128 || i + n > ssl->in_hslen )
6325 {
6326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6327 mbedtls_ssl_send_alert_message( ssl,
6328 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6329 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6330 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6331 }
6332
6333 /* Check if we're handling the first CRT in the chain. */
6334#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6335 if( crt_cnt++ == 0 &&
6336 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6337 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6338 {
6339 /* During client-side renegotiation, check that the server's
6340 * end-CRTs hasn't changed compared to the initial handshake,
6341 * mitigating the triple handshake attack. On success, reuse
6342 * the original end-CRT instead of parsing it again. */
6343 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6344 if( ssl_check_peer_crt_unchanged( ssl,
6345 &ssl->in_msg[i],
6346 n ) != 0 )
6347 {
6348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6349 mbedtls_ssl_send_alert_message( ssl,
6350 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6351 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6352 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6353 }
6354
6355 /* Now we can safely free the original chain. */
6356 ssl_clear_peer_cert( ssl->session );
6357 }
6358#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6359
6360 /* Parse the next certificate in the chain. */
6361#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6362 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6363#else
6364 /* If we don't need to store the CRT chain permanently, parse
6365 * it in-place from the input buffer instead of making a copy. */
6366 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6367#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6368 switch( ret )
6369 {
6370 case 0: /*ok*/
6371 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6372 /* Ignore certificate with an unknown algorithm: maybe a
6373 prior certificate was already trusted. */
6374 break;
6375
6376 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6377 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6378 goto crt_parse_der_failed;
6379
6380 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6381 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6382 goto crt_parse_der_failed;
6383
6384 default:
6385 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6386 crt_parse_der_failed:
6387 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6388 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6389 return( ret );
6390 }
6391
6392 i += n;
6393 }
6394
6395 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6396 return( 0 );
6397}
6398
6399#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006400MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006401static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6402{
6403 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6404 return( -1 );
6405
6406 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6407 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6408 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6409 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6410 {
Ronald Cron19385882022-06-15 16:26:13 +02006411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006412 return( 0 );
6413 }
6414 return( -1 );
6415}
6416#endif /* MBEDTLS_SSL_SRV_C */
6417
6418/* Check if a certificate message is expected.
6419 * Return either
6420 * - SSL_CERTIFICATE_EXPECTED, or
6421 * - SSL_CERTIFICATE_SKIP
6422 * indicating whether a Certificate message is expected or not.
6423 */
6424#define SSL_CERTIFICATE_EXPECTED 0
6425#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006426MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006427static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6428 int authmode )
6429{
6430 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6431 ssl->handshake->ciphersuite_info;
6432
6433 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6434 return( SSL_CERTIFICATE_SKIP );
6435
6436#if defined(MBEDTLS_SSL_SRV_C)
6437 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6438 {
6439 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6440 return( SSL_CERTIFICATE_SKIP );
6441
6442 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6443 {
6444 ssl->session_negotiate->verify_result =
6445 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6446 return( SSL_CERTIFICATE_SKIP );
6447 }
6448 }
6449#else
6450 ((void) authmode);
6451#endif /* MBEDTLS_SSL_SRV_C */
6452
6453 return( SSL_CERTIFICATE_EXPECTED );
6454}
6455
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006456MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006457static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6458 int authmode,
6459 mbedtls_x509_crt *chain,
6460 void *rs_ctx )
6461{
6462 int ret = 0;
6463 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6464 ssl->handshake->ciphersuite_info;
6465 int have_ca_chain = 0;
6466
6467 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6468 void *p_vrfy;
6469
6470 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6471 return( 0 );
6472
6473 if( ssl->f_vrfy != NULL )
6474 {
6475 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6476 f_vrfy = ssl->f_vrfy;
6477 p_vrfy = ssl->p_vrfy;
6478 }
6479 else
6480 {
6481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6482 f_vrfy = ssl->conf->f_vrfy;
6483 p_vrfy = ssl->conf->p_vrfy;
6484 }
6485
6486 /*
6487 * Main check: verify certificate
6488 */
6489#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6490 if( ssl->conf->f_ca_cb != NULL )
6491 {
6492 ((void) rs_ctx);
6493 have_ca_chain = 1;
6494
6495 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6496 ret = mbedtls_x509_crt_verify_with_ca_cb(
6497 chain,
6498 ssl->conf->f_ca_cb,
6499 ssl->conf->p_ca_cb,
6500 ssl->conf->cert_profile,
6501 ssl->hostname,
6502 &ssl->session_negotiate->verify_result,
6503 f_vrfy, p_vrfy );
6504 }
6505 else
6506#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6507 {
6508 mbedtls_x509_crt *ca_chain;
6509 mbedtls_x509_crl *ca_crl;
6510
6511#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6512 if( ssl->handshake->sni_ca_chain != NULL )
6513 {
6514 ca_chain = ssl->handshake->sni_ca_chain;
6515 ca_crl = ssl->handshake->sni_ca_crl;
6516 }
6517 else
6518#endif
6519 {
6520 ca_chain = ssl->conf->ca_chain;
6521 ca_crl = ssl->conf->ca_crl;
6522 }
6523
6524 if( ca_chain != NULL )
6525 have_ca_chain = 1;
6526
6527 ret = mbedtls_x509_crt_verify_restartable(
6528 chain,
6529 ca_chain, ca_crl,
6530 ssl->conf->cert_profile,
6531 ssl->hostname,
6532 &ssl->session_negotiate->verify_result,
6533 f_vrfy, p_vrfy, rs_ctx );
6534 }
6535
6536 if( ret != 0 )
6537 {
6538 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6539 }
6540
6541#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6542 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6543 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6544#endif
6545
6546 /*
6547 * Secondary checks: always done, but change 'ret' only if it was 0
6548 */
6549
6550#if defined(MBEDTLS_ECP_C)
6551 {
6552 const mbedtls_pk_context *pk = &chain->pk;
6553
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006554 /* If certificate uses an EC key, make sure the curve is OK.
6555 * This is a public key, so it can't be opaque, so can_do() is a good
6556 * enough check to ensure pk_ec() is safe to use here. */
Jerry Yud9526692022-02-17 14:23:47 +08006557 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6558 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6559 {
6560 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6561
6562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6563 if( ret == 0 )
6564 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6565 }
6566 }
6567#endif /* MBEDTLS_ECP_C */
6568
6569 if( mbedtls_ssl_check_cert_usage( chain,
6570 ciphersuite_info,
6571 ! ssl->conf->endpoint,
6572 &ssl->session_negotiate->verify_result ) != 0 )
6573 {
6574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6575 if( ret == 0 )
6576 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6577 }
6578
6579 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6580 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6581 * with details encoded in the verification flags. All other kinds
6582 * of error codes, including those from the user provided f_vrfy
6583 * functions, are treated as fatal and lead to a failure of
6584 * ssl_parse_certificate even if verification was optional. */
6585 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6586 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6587 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6588 {
6589 ret = 0;
6590 }
6591
6592 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6593 {
6594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6595 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6596 }
6597
6598 if( ret != 0 )
6599 {
6600 uint8_t alert;
6601
6602 /* The certificate may have been rejected for several reasons.
6603 Pick one and send the corresponding alert. Which alert to send
6604 may be a subject of debate in some cases. */
6605 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6606 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6607 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6608 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6609 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6610 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6611 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6612 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6613 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6614 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6615 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6616 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6617 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6618 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6619 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6620 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6621 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6622 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6623 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6624 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6625 else
6626 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6627 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6628 alert );
6629 }
6630
6631#if defined(MBEDTLS_DEBUG_C)
6632 if( ssl->session_negotiate->verify_result != 0 )
6633 {
6634 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6635 (unsigned int) ssl->session_negotiate->verify_result ) );
6636 }
6637 else
6638 {
6639 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6640 }
6641#endif /* MBEDTLS_DEBUG_C */
6642
6643 return( ret );
6644}
6645
6646#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006647MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006648static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6649 unsigned char *start, size_t len )
6650{
6651 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6652 /* Remember digest of the peer's end-CRT. */
6653 ssl->session_negotiate->peer_cert_digest =
6654 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6655 if( ssl->session_negotiate->peer_cert_digest == NULL )
6656 {
6657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6658 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6659 mbedtls_ssl_send_alert_message( ssl,
6660 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6661 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6662
6663 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6664 }
6665
6666 ret = mbedtls_md( mbedtls_md_info_from_type(
6667 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6668 start, len,
6669 ssl->session_negotiate->peer_cert_digest );
6670
6671 ssl->session_negotiate->peer_cert_digest_type =
6672 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6673 ssl->session_negotiate->peer_cert_digest_len =
6674 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6675
6676 return( ret );
6677}
6678
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006679MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006680static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6681 unsigned char *start, size_t len )
6682{
6683 unsigned char *end = start + len;
6684 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6685
6686 /* Make a copy of the peer's raw public key. */
6687 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6688 ret = mbedtls_pk_parse_subpubkey( &start, end,
6689 &ssl->handshake->peer_pubkey );
6690 if( ret != 0 )
6691 {
6692 /* We should have parsed the public key before. */
6693 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6694 }
6695
6696 return( 0 );
6697}
6698#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6699
6700int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6701{
6702 int ret = 0;
6703 int crt_expected;
6704#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6705 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6706 ? ssl->handshake->sni_authmode
6707 : ssl->conf->authmode;
6708#else
6709 const int authmode = ssl->conf->authmode;
6710#endif
6711 void *rs_ctx = NULL;
6712 mbedtls_x509_crt *chain = NULL;
6713
6714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6715
6716 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6717 if( crt_expected == SSL_CERTIFICATE_SKIP )
6718 {
6719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6720 goto exit;
6721 }
6722
6723#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6724 if( ssl->handshake->ecrs_enabled &&
6725 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6726 {
6727 chain = ssl->handshake->ecrs_peer_cert;
6728 ssl->handshake->ecrs_peer_cert = NULL;
6729 goto crt_verify;
6730 }
6731#endif
6732
6733 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6734 {
6735 /* mbedtls_ssl_read_record may have sent an alert already. We
6736 let it decide whether to alert. */
6737 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6738 goto exit;
6739 }
6740
6741#if defined(MBEDTLS_SSL_SRV_C)
6742 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6743 {
6744 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6745
6746 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6747 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6748
6749 goto exit;
6750 }
6751#endif /* MBEDTLS_SSL_SRV_C */
6752
6753 /* Clear existing peer CRT structure in case we tried to
6754 * reuse a session but it failed, and allocate a new one. */
6755 ssl_clear_peer_cert( ssl->session_negotiate );
6756
6757 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6758 if( chain == NULL )
6759 {
6760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6761 sizeof( mbedtls_x509_crt ) ) );
6762 mbedtls_ssl_send_alert_message( ssl,
6763 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6764 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6765
6766 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6767 goto exit;
6768 }
6769 mbedtls_x509_crt_init( chain );
6770
6771 ret = ssl_parse_certificate_chain( ssl, chain );
6772 if( ret != 0 )
6773 goto exit;
6774
6775#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6776 if( ssl->handshake->ecrs_enabled)
6777 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6778
6779crt_verify:
6780 if( ssl->handshake->ecrs_enabled)
6781 rs_ctx = &ssl->handshake->ecrs_ctx;
6782#endif
6783
6784 ret = ssl_parse_certificate_verify( ssl, authmode,
6785 chain, rs_ctx );
6786 if( ret != 0 )
6787 goto exit;
6788
6789#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6790 {
6791 unsigned char *crt_start, *pk_start;
6792 size_t crt_len, pk_len;
6793
6794 /* We parse the CRT chain without copying, so
6795 * these pointers point into the input buffer,
6796 * and are hence still valid after freeing the
6797 * CRT chain. */
6798
6799 crt_start = chain->raw.p;
6800 crt_len = chain->raw.len;
6801
6802 pk_start = chain->pk_raw.p;
6803 pk_len = chain->pk_raw.len;
6804
6805 /* Free the CRT structures before computing
6806 * digest and copying the peer's public key. */
6807 mbedtls_x509_crt_free( chain );
6808 mbedtls_free( chain );
6809 chain = NULL;
6810
6811 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6812 if( ret != 0 )
6813 goto exit;
6814
6815 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6816 if( ret != 0 )
6817 goto exit;
6818 }
6819#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6820 /* Pass ownership to session structure. */
6821 ssl->session_negotiate->peer_cert = chain;
6822 chain = NULL;
6823#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6824
6825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6826
6827exit:
6828
6829 if( ret == 0 )
6830 ssl->state++;
6831
6832#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6833 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6834 {
6835 ssl->handshake->ecrs_peer_cert = chain;
6836 chain = NULL;
6837 }
6838#endif
6839
6840 if( chain != NULL )
6841 {
6842 mbedtls_x509_crt_free( chain );
6843 mbedtls_free( chain );
6844 }
6845
6846 return( ret );
6847}
6848#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6849
Jerry Yu615bd6f2022-02-17 14:25:15 +08006850#if defined(MBEDTLS_SHA256_C)
6851static void ssl_calc_finished_tls_sha256(
6852 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6853{
6854 int len = 12;
6855 const char *sender;
6856 unsigned char padbuf[32];
6857#if defined(MBEDTLS_USE_PSA_CRYPTO)
6858 size_t hash_size;
6859 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
6860 psa_status_t status;
6861#else
6862 mbedtls_sha256_context sha256;
6863#endif
6864
6865 mbedtls_ssl_session *session = ssl->session_negotiate;
6866 if( !session )
6867 session = ssl->session;
6868
6869 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6870 ? "client finished"
6871 : "server finished";
6872
6873#if defined(MBEDTLS_USE_PSA_CRYPTO)
6874 sha256_psa = psa_hash_operation_init();
6875
6876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6877
6878 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6879 if( status != PSA_SUCCESS )
6880 {
6881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6882 return;
6883 }
6884
6885 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6886 if( status != PSA_SUCCESS )
6887 {
6888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6889 return;
6890 }
6891 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6892#else
6893
6894 mbedtls_sha256_init( &sha256 );
6895
6896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
6897
6898 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6899
6900 /*
6901 * TLSv1.2:
6902 * hash = PRF( master, finished_label,
6903 * Hash( handshake ) )[0.11]
6904 */
6905
6906#if !defined(MBEDTLS_SHA256_ALT)
6907 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
6908 sha256.state, sizeof( sha256.state ) );
6909#endif
6910
6911 mbedtls_sha256_finish( &sha256, padbuf );
6912 mbedtls_sha256_free( &sha256 );
6913#endif /* MBEDTLS_USE_PSA_CRYPTO */
6914
6915 ssl->handshake->tls_prf( session->master, 48, sender,
6916 padbuf, 32, buf, len );
6917
6918 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6919
6920 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6921
6922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6923}
6924#endif /* MBEDTLS_SHA256_C */
6925
Jerry Yub7ba49e2022-02-17 14:25:53 +08006926
6927#if defined(MBEDTLS_SHA384_C)
6928
6929static void ssl_calc_finished_tls_sha384(
6930 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6931{
6932 int len = 12;
6933 const char *sender;
6934 unsigned char padbuf[48];
6935#if defined(MBEDTLS_USE_PSA_CRYPTO)
6936 size_t hash_size;
6937 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
6938 psa_status_t status;
6939#else
6940 mbedtls_sha512_context sha512;
6941#endif
6942
6943 mbedtls_ssl_session *session = ssl->session_negotiate;
6944 if( !session )
6945 session = ssl->session;
6946
6947 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6948 ? "client finished"
6949 : "server finished";
6950
6951#if defined(MBEDTLS_USE_PSA_CRYPTO)
6952 sha384_psa = psa_hash_operation_init();
6953
6954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6955
6956 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6957 if( status != PSA_SUCCESS )
6958 {
6959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6960 return;
6961 }
6962
6963 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
6964 if( status != PSA_SUCCESS )
6965 {
6966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6967 return;
6968 }
6969 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
6970#else
6971 mbedtls_sha512_init( &sha512 );
6972
6973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
6974
6975 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
6976
6977 /*
6978 * TLSv1.2:
6979 * hash = PRF( master, finished_label,
6980 * Hash( handshake ) )[0.11]
6981 */
6982
6983#if !defined(MBEDTLS_SHA512_ALT)
6984 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6985 sha512.state, sizeof( sha512.state ) );
6986#endif
6987 mbedtls_sha512_finish( &sha512, padbuf );
6988
6989 mbedtls_sha512_free( &sha512 );
6990#endif
6991
6992 ssl->handshake->tls_prf( session->master, 48, sender,
6993 padbuf, 48, buf, len );
6994
6995 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6996
6997 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6998
6999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7000}
7001#endif /* MBEDTLS_SHA384_C */
7002
Jerry Yuaef00152022-02-17 14:27:31 +08007003void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7004{
7005 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7006
7007 /*
7008 * Free our handshake params
7009 */
7010 mbedtls_ssl_handshake_free( ssl );
7011 mbedtls_free( ssl->handshake );
7012 ssl->handshake = NULL;
7013
7014 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007015 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007016 */
7017 if( ssl->transform )
7018 {
7019 mbedtls_ssl_transform_free( ssl->transform );
7020 mbedtls_free( ssl->transform );
7021 }
7022 ssl->transform = ssl->transform_negotiate;
7023 ssl->transform_negotiate = NULL;
7024
7025 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7026}
7027
Jerry Yu2a9fff52022-02-17 14:28:51 +08007028void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7029{
7030 int resume = ssl->handshake->resume;
7031
7032 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7033
7034#if defined(MBEDTLS_SSL_RENEGOTIATION)
7035 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7036 {
7037 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7038 ssl->renego_records_seen = 0;
7039 }
7040#endif
7041
7042 /*
7043 * Free the previous session and switch in the current one
7044 */
7045 if( ssl->session )
7046 {
7047#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7048 /* RFC 7366 3.1: keep the EtM state */
7049 ssl->session_negotiate->encrypt_then_mac =
7050 ssl->session->encrypt_then_mac;
7051#endif
7052
7053 mbedtls_ssl_session_free( ssl->session );
7054 mbedtls_free( ssl->session );
7055 }
7056 ssl->session = ssl->session_negotiate;
7057 ssl->session_negotiate = NULL;
7058
7059 /*
7060 * Add cache entry
7061 */
7062 if( ssl->conf->f_set_cache != NULL &&
7063 ssl->session->id_len != 0 &&
7064 resume == 0 )
7065 {
7066 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7067 ssl->session->id,
7068 ssl->session->id_len,
7069 ssl->session ) != 0 )
7070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7071 }
7072
7073#if defined(MBEDTLS_SSL_PROTO_DTLS)
7074 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7075 ssl->handshake->flight != NULL )
7076 {
7077 /* Cancel handshake timer */
7078 mbedtls_ssl_set_timer( ssl, 0 );
7079
7080 /* Keep last flight around in case we need to resend it:
7081 * we need the handshake and transform structures for that */
7082 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7083 }
7084 else
7085#endif
7086 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7087
7088 ssl->state++;
7089
7090 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7091}
7092
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007093int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7094{
7095 int ret, hash_len;
7096
7097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7098
7099 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7100
7101 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7102
7103 /*
7104 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7105 * may define some other value. Currently (early 2016), no defined
7106 * ciphersuite does this (and this is unlikely to change as activity has
7107 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7108 */
7109 hash_len = 12;
7110
7111#if defined(MBEDTLS_SSL_RENEGOTIATION)
7112 ssl->verify_data_len = hash_len;
7113 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7114#endif
7115
7116 ssl->out_msglen = 4 + hash_len;
7117 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7118 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7119
7120 /*
7121 * In case of session resuming, invert the client and server
7122 * ChangeCipherSpec messages order.
7123 */
7124 if( ssl->handshake->resume != 0 )
7125 {
7126#if defined(MBEDTLS_SSL_CLI_C)
7127 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7128 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7129#endif
7130#if defined(MBEDTLS_SSL_SRV_C)
7131 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7132 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7133#endif
7134 }
7135 else
7136 ssl->state++;
7137
7138 /*
7139 * Switch to our negotiated transform and session parameters for outbound
7140 * data.
7141 */
7142 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7143
7144#if defined(MBEDTLS_SSL_PROTO_DTLS)
7145 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7146 {
7147 unsigned char i;
7148
7149 /* Remember current epoch settings for resending */
7150 ssl->handshake->alt_transform_out = ssl->transform_out;
7151 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7152 sizeof( ssl->handshake->alt_out_ctr ) );
7153
7154 /* Set sequence_number to zero */
7155 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7156
7157
7158 /* Increment epoch */
7159 for( i = 2; i > 0; i-- )
7160 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7161 break;
7162
7163 /* The loop goes to its end iff the counter is wrapping */
7164 if( i == 0 )
7165 {
7166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7167 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7168 }
7169 }
7170 else
7171#endif /* MBEDTLS_SSL_PROTO_DTLS */
7172 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7173
7174 ssl->transform_out = ssl->transform_negotiate;
7175 ssl->session_out = ssl->session_negotiate;
7176
7177#if defined(MBEDTLS_SSL_PROTO_DTLS)
7178 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7179 mbedtls_ssl_send_flight_completed( ssl );
7180#endif
7181
7182 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7183 {
7184 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7185 return( ret );
7186 }
7187
7188#if defined(MBEDTLS_SSL_PROTO_DTLS)
7189 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7190 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7191 {
7192 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7193 return( ret );
7194 }
7195#endif
7196
7197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7198
7199 return( 0 );
7200}
7201
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007202#define SSL_MAX_HASH_LEN 12
7203
7204int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7205{
7206 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7207 unsigned int hash_len = 12;
7208 unsigned char buf[SSL_MAX_HASH_LEN];
7209
7210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7211
7212 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7213
7214 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7215 {
7216 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7217 goto exit;
7218 }
7219
7220 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7221 {
7222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7223 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7224 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7225 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7226 goto exit;
7227 }
7228
7229 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7230 {
7231 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7232 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7233 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7234 goto exit;
7235 }
7236
7237 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7238 {
7239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7240 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7241 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7242 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7243 goto exit;
7244 }
7245
7246 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7247 buf, hash_len ) != 0 )
7248 {
7249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7250 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7251 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7252 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7253 goto exit;
7254 }
7255
7256#if defined(MBEDTLS_SSL_RENEGOTIATION)
7257 ssl->verify_data_len = hash_len;
7258 memcpy( ssl->peer_verify_data, buf, hash_len );
7259#endif
7260
7261 if( ssl->handshake->resume != 0 )
7262 {
7263#if defined(MBEDTLS_SSL_CLI_C)
7264 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7265 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7266#endif
7267#if defined(MBEDTLS_SSL_SRV_C)
7268 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7269 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7270#endif
7271 }
7272 else
7273 ssl->state++;
7274
7275#if defined(MBEDTLS_SSL_PROTO_DTLS)
7276 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7277 mbedtls_ssl_recv_flight_completed( ssl );
7278#endif
7279
7280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7281
7282exit:
7283 mbedtls_platform_zeroize( buf, hash_len );
7284 return( ret );
7285}
7286
Jerry Yu392112c2022-02-17 14:34:10 +08007287#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7288/*
7289 * Helper to get TLS 1.2 PRF from ciphersuite
7290 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7291 */
7292static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7293{
7294#if defined(MBEDTLS_SHA384_C)
7295 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7296 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7297
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007298 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007299 return( tls_prf_sha384 );
7300#else
7301 (void) ciphersuite_id;
7302#endif
7303 return( tls_prf_sha256 );
7304}
7305#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007306
7307static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7308{
7309 ((void) tls_prf);
7310#if defined(MBEDTLS_SHA384_C)
7311 if( tls_prf == tls_prf_sha384 )
7312 {
7313 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7314 }
7315 else
7316#endif
7317#if defined(MBEDTLS_SHA256_C)
7318 if( tls_prf == tls_prf_sha256 )
7319 {
7320 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7321 }
7322 else
7323#endif
7324 return( MBEDTLS_SSL_TLS_PRF_NONE );
7325}
7326
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007327/*
7328 * Populate a transform structure with session keys and all the other
7329 * necessary information.
7330 *
7331 * Parameters:
7332 * - [in/out]: transform: structure to populate
7333 * [in] must be just initialised with mbedtls_ssl_transform_init()
7334 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7335 * - [in] ciphersuite
7336 * - [in] master
7337 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007338 * - [in] tls_prf: pointer to PRF to use for key derivation
7339 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007340 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007341 * - [in] endpoint: client or server
7342 * - [in] ssl: used for:
7343 * - ssl->conf->{f,p}_export_keys
7344 * [in] optionally used for:
7345 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7346 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007347MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007348static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7349 int ciphersuite,
7350 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007351#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007352 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007353#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007354 ssl_tls_prf_t tls_prf,
7355 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007356 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007357 unsigned endpoint,
7358 const mbedtls_ssl_context *ssl )
7359{
7360 int ret = 0;
7361 unsigned char keyblk[256];
7362 unsigned char *key1;
7363 unsigned char *key2;
7364 unsigned char *mac_enc;
7365 unsigned char *mac_dec;
7366 size_t mac_key_len = 0;
7367 size_t iv_copy_len;
7368 size_t keylen;
7369 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007370 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007371#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007372 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007373 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007374#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007375
7376#if defined(MBEDTLS_USE_PSA_CRYPTO)
7377 psa_key_type_t key_type;
7378 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7379 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007380 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007381 size_t key_bits;
7382 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7383#endif
7384
7385#if !defined(MBEDTLS_DEBUG_C) && \
7386 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7387 if( ssl->f_export_keys == NULL )
7388 {
7389 ssl = NULL; /* make sure we don't use it except for these cases */
7390 (void) ssl;
7391 }
7392#endif
7393
7394 /*
7395 * Some data just needs copying into the structure
7396 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007397#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007398 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007399#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007400 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007401
7402#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7403 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7404#endif
7405
7406#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007407 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007408 {
7409 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7410 * generation separate. This should never happen. */
7411 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7412 }
7413#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7414
7415 /*
7416 * Get various info structures
7417 */
7418 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7419 if( ciphersuite_info == NULL )
7420 {
7421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7422 ciphersuite ) );
7423 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7424 }
7425
Neil Armstrongab555e02022-04-04 11:07:59 +02007426 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007427#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007428 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007429#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007430 ciphersuite_info );
7431
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007432 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7433 transform->taglen =
7434 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7435
7436#if defined(MBEDTLS_USE_PSA_CRYPTO)
7437 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7438 transform->taglen,
7439 &alg,
7440 &key_type,
7441 &key_bits ) ) != PSA_SUCCESS )
7442 {
7443 ret = psa_ssl_status_to_mbedtls( status );
7444 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7445 goto end;
7446 }
7447#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007448 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7449 if( cipher_info == NULL )
7450 {
7451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7452 ciphersuite_info->cipher ) );
7453 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7454 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007455#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007456
Neil Armstronge4512952022-03-08 09:08:22 +01007457#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007458 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007459 if( mac_alg == 0 )
7460 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007462 (unsigned) ciphersuite_info->mac ) );
7463 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7464 }
7465#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007466 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7467 if( md_info == NULL )
7468 {
7469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7470 (unsigned) ciphersuite_info->mac ) );
7471 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7472 }
Neil Armstronge4512952022-03-08 09:08:22 +01007473#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007474
7475#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7476 /* Copy own and peer's CID if the use of the CID
7477 * extension has been negotiated. */
7478 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7479 {
7480 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7481
7482 transform->in_cid_len = ssl->own_cid_len;
7483 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7484 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7485 transform->in_cid_len );
7486
7487 transform->out_cid_len = ssl->handshake->peer_cid_len;
7488 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7489 ssl->handshake->peer_cid_len );
7490 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7491 transform->out_cid_len );
7492 }
7493#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7494
7495 /*
7496 * Compute key block using the PRF
7497 */
7498 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7499 if( ret != 0 )
7500 {
7501 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7502 return( ret );
7503 }
7504
7505 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7506 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7507 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7508 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7509 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7510
7511 /*
7512 * Determine the appropriate key, IV and MAC length.
7513 */
7514
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007515#if defined(MBEDTLS_USE_PSA_CRYPTO)
7516 keylen = PSA_BITS_TO_BYTES(key_bits);
7517#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007518 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007519#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007520
7521#if defined(MBEDTLS_GCM_C) || \
7522 defined(MBEDTLS_CCM_C) || \
7523 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007524 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007525 {
7526 size_t explicit_ivlen;
7527
7528 transform->maclen = 0;
7529 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007530
7531 /* All modes haves 96-bit IVs, but the length of the static parts vary
7532 * with mode and version:
7533 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7534 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7535 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7536 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7537 * sequence number).
7538 */
7539 transform->ivlen = 12;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007540#if defined(MBEDTLS_USE_PSA_CRYPTO)
7541 if( key_type == PSA_KEY_TYPE_CHACHA20 )
7542#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007543 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007544#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007545 transform->fixed_ivlen = 12;
7546 else
7547 transform->fixed_ivlen = 4;
7548
7549 /* Minimum length of encrypted record */
7550 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7551 transform->minlen = explicit_ivlen + transform->taglen;
7552 }
7553 else
7554#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7555#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007556 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7557 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7558 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007559 {
Neil Armstronge4512952022-03-08 09:08:22 +01007560#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007561 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007562#else
7563 size_t block_size = cipher_info->block_size;
7564#endif /* MBEDTLS_USE_PSA_CRYPTO */
7565
7566#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007567 /* Get MAC length */
7568 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7569#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007570 /* Initialize HMAC contexts */
7571 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7572 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7573 {
7574 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7575 goto end;
7576 }
7577
7578 /* Get MAC length */
7579 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007580#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007581 transform->maclen = mac_key_len;
7582
7583 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007584#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007585 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007586#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007587 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007588#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007589
7590 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007591 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007592 transform->minlen = transform->maclen;
7593 else
7594 {
7595 /*
7596 * GenericBlockCipher:
7597 * 1. if EtM is in use: one block plus MAC
7598 * otherwise: * first multiple of blocklen greater than maclen
7599 * 2. IV
7600 */
7601#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007602 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007603 {
7604 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007605 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007606 }
7607 else
7608#endif
7609 {
7610 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007611 + block_size
7612 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007613 }
7614
Glenn Strauss07c64162022-03-14 12:34:51 -04007615 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007616 {
7617 transform->minlen += transform->ivlen;
7618 }
7619 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007620 {
7621 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7622 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7623 goto end;
7624 }
7625 }
7626 }
7627 else
7628#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7629 {
7630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7631 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7632 }
7633
7634 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7635 (unsigned) keylen,
7636 (unsigned) transform->minlen,
7637 (unsigned) transform->ivlen,
7638 (unsigned) transform->maclen ) );
7639
7640 /*
7641 * Finally setup the cipher contexts, IVs and MAC secrets.
7642 */
7643#if defined(MBEDTLS_SSL_CLI_C)
7644 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7645 {
7646 key1 = keyblk + mac_key_len * 2;
7647 key2 = keyblk + mac_key_len * 2 + keylen;
7648
7649 mac_enc = keyblk;
7650 mac_dec = keyblk + mac_key_len;
7651
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007652 iv_copy_len = ( transform->fixed_ivlen ) ?
7653 transform->fixed_ivlen : transform->ivlen;
7654 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7655 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7656 iv_copy_len );
7657 }
7658 else
7659#endif /* MBEDTLS_SSL_CLI_C */
7660#if defined(MBEDTLS_SSL_SRV_C)
7661 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7662 {
7663 key1 = keyblk + mac_key_len * 2 + keylen;
7664 key2 = keyblk + mac_key_len * 2;
7665
7666 mac_enc = keyblk + mac_key_len;
7667 mac_dec = keyblk;
7668
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007669 iv_copy_len = ( transform->fixed_ivlen ) ?
7670 transform->fixed_ivlen : transform->ivlen;
7671 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7672 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7673 iv_copy_len );
7674 }
7675 else
7676#endif /* MBEDTLS_SSL_SRV_C */
7677 {
7678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7679 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7680 goto end;
7681 }
7682
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007683 if( ssl != NULL && ssl->f_export_keys != NULL )
7684 {
7685 ssl->f_export_keys( ssl->p_export_keys,
7686 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7687 master, 48,
7688 randbytes + 32,
7689 randbytes,
7690 tls_prf_get_type( tls_prf ) );
7691 }
7692
7693#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007694 transform->psa_alg = alg;
7695
7696 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7697 {
7698 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7699 psa_set_key_algorithm( &attributes, alg );
7700 psa_set_key_type( &attributes, key_type );
7701
7702 if( ( status = psa_import_key( &attributes,
7703 key1,
7704 PSA_BITS_TO_BYTES( key_bits ),
7705 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7706 {
7707 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7708 ret = psa_ssl_status_to_mbedtls( status );
7709 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7710 goto end;
7711 }
7712
7713 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7714
7715 if( ( status = psa_import_key( &attributes,
7716 key2,
7717 PSA_BITS_TO_BYTES( key_bits ),
7718 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7719 {
7720 ret = psa_ssl_status_to_mbedtls( status );
7721 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7722 goto end;
7723 }
7724 }
7725#else
7726 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7727 cipher_info ) ) != 0 )
7728 {
7729 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7730 goto end;
7731 }
7732
7733 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7734 cipher_info ) ) != 0 )
7735 {
7736 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7737 goto end;
7738 }
7739
7740 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7741 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7742 MBEDTLS_ENCRYPT ) ) != 0 )
7743 {
7744 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7745 goto end;
7746 }
7747
7748 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7749 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7750 MBEDTLS_DECRYPT ) ) != 0 )
7751 {
7752 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7753 goto end;
7754 }
7755
7756#if defined(MBEDTLS_CIPHER_MODE_CBC)
7757 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7758 {
7759 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7760 MBEDTLS_PADDING_NONE ) ) != 0 )
7761 {
7762 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7763 goto end;
7764 }
7765
7766 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7767 MBEDTLS_PADDING_NONE ) ) != 0 )
7768 {
7769 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7770 goto end;
7771 }
7772 }
7773#endif /* MBEDTLS_CIPHER_MODE_CBC */
7774#endif /* MBEDTLS_USE_PSA_CRYPTO */
7775
Neil Armstrong29c0c042022-03-17 17:47:28 +01007776#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7777 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7778 For AEAD-based ciphersuites, there is nothing to do here. */
7779 if( mac_key_len != 0 )
7780 {
7781#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007782 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007783
7784 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007785 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007786 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7787
7788 if( ( status = psa_import_key( &attributes,
7789 mac_enc, mac_key_len,
7790 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7791 {
7792 ret = psa_ssl_status_to_mbedtls( status );
7793 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7794 goto end;
7795 }
7796
Ronald Cronfb39f152022-03-25 14:36:28 +01007797 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
7798 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) &&
7799 ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007800 /* mbedtls_ct_hmac() requires the key to be exportable */
7801 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7802 PSA_KEY_USAGE_VERIFY_HASH );
7803 else
7804 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7805
7806 if( ( status = psa_import_key( &attributes,
7807 mac_dec, mac_key_len,
7808 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7809 {
7810 ret = psa_ssl_status_to_mbedtls( status );
7811 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7812 goto end;
7813 }
7814#else
7815 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7816 if( ret != 0 )
7817 goto end;
7818 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7819 if( ret != 0 )
7820 goto end;
7821#endif /* MBEDTLS_USE_PSA_CRYPTO */
7822 }
7823#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7824
7825 ((void) mac_dec);
7826 ((void) mac_enc);
7827
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007828end:
7829 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7830 return( ret );
7831}
7832
Jerry Yuee40f9d2022-02-17 14:55:16 +08007833#if defined(MBEDTLS_USE_PSA_CRYPTO)
7834int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7835 unsigned char *hash, size_t *hashlen,
7836 unsigned char *data, size_t data_len,
7837 mbedtls_md_type_t md_alg )
7838{
7839 psa_status_t status;
7840 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007841 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08007842
7843 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7844
7845 if( ( status = psa_hash_setup( &hash_operation,
7846 hash_alg ) ) != PSA_SUCCESS )
7847 {
7848 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7849 goto exit;
7850 }
7851
7852 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
7853 64 ) ) != PSA_SUCCESS )
7854 {
7855 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7856 goto exit;
7857 }
7858
7859 if( ( status = psa_hash_update( &hash_operation,
7860 data, data_len ) ) != PSA_SUCCESS )
7861 {
7862 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7863 goto exit;
7864 }
7865
7866 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
7867 hashlen ) ) != PSA_SUCCESS )
7868 {
7869 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
7870 goto exit;
7871 }
7872
7873exit:
7874 if( status != PSA_SUCCESS )
7875 {
7876 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7877 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7878 switch( status )
7879 {
7880 case PSA_ERROR_NOT_SUPPORTED:
7881 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
7882 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
7883 case PSA_ERROR_BUFFER_TOO_SMALL:
7884 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
7885 case PSA_ERROR_INSUFFICIENT_MEMORY:
7886 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
7887 default:
7888 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
7889 }
7890 }
7891 return( 0 );
7892}
7893
7894#else
7895
7896int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7897 unsigned char *hash, size_t *hashlen,
7898 unsigned char *data, size_t data_len,
7899 mbedtls_md_type_t md_alg )
7900{
7901 int ret = 0;
7902 mbedtls_md_context_t ctx;
7903 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
7904 *hashlen = mbedtls_md_get_size( md_info );
7905
7906 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
7907
7908 mbedtls_md_init( &ctx );
7909
7910 /*
7911 * digitally-signed struct {
7912 * opaque client_random[32];
7913 * opaque server_random[32];
7914 * ServerDHParams params;
7915 * };
7916 */
7917 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
7918 {
7919 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7920 goto exit;
7921 }
7922 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
7923 {
7924 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
7925 goto exit;
7926 }
7927 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
7928 {
7929 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7930 goto exit;
7931 }
7932 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
7933 {
7934 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7935 goto exit;
7936 }
7937 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
7938 {
7939 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
7940 goto exit;
7941 }
7942
7943exit:
7944 mbedtls_md_free( &ctx );
7945
7946 if( ret != 0 )
7947 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7948 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7949
7950 return( ret );
7951}
7952#endif /* MBEDTLS_USE_PSA_CRYPTO */
7953
Jerry Yud9d91da2022-02-17 14:57:06 +08007954#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
7955
Gabor Mezeia3d016c2022-05-10 12:44:09 +02007956/* Find the preferred hash for a given signature algorithm. */
7957unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
7958 mbedtls_ssl_context *ssl,
7959 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08007960{
Gabor Mezei078e8032022-04-27 21:17:56 +02007961 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02007962 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02007963
7964 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02007965 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02007966
Gabor Mezeia3d016c2022-05-10 12:44:09 +02007967 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08007968 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02007969 unsigned int hash_alg_received =
7970 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
7971 received_sig_algs[i] );
7972 unsigned int sig_alg_received =
7973 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
7974 received_sig_algs[i] );
7975
7976 if( sig_alg == sig_alg_received )
7977 {
7978#if defined(MBEDTLS_USE_PSA_CRYPTO)
7979 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
7980 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02007981 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007982 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02007983
Neil Armstrong96eceb82022-06-30 18:05:05 +02007984 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
7985 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
7986 PSA_ALG_ECDSA( psa_hash_alg ),
7987 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02007988 continue;
7989
Neil Armstrong96eceb82022-06-30 18:05:05 +02007990 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02007991 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
7992 PSA_ALG_RSA_PKCS1V15_SIGN(
7993 psa_hash_alg ),
7994 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02007995 continue;
7996 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02007997#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02007998
Neil Armstrong9f1176a2022-06-24 18:19:19 +02007999 return( hash_alg_received );
8000 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008001 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008002
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008003 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008004}
8005
8006#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8007
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008008/* Serialization of TLS 1.2 sessions:
8009 *
8010 * struct {
8011 * uint64 start_time;
8012 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008013 * uint8 session_id_len; // at most 32
8014 * opaque session_id[32];
8015 * opaque master[48]; // fixed length in the standard
8016 * uint32 verify_result;
8017 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8018 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8019 * uint32 ticket_lifetime;
8020 * uint8 mfl_code; // up to 255 according to standard
8021 * uint8 encrypt_then_mac; // 0 or 1
8022 * } serialized_session_tls12;
8023 *
8024 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008025static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008026 unsigned char *buf,
8027 size_t buf_len )
8028{
8029 unsigned char *p = buf;
8030 size_t used = 0;
8031
8032#if defined(MBEDTLS_HAVE_TIME)
8033 uint64_t start;
8034#endif
8035#if defined(MBEDTLS_X509_CRT_PARSE_C)
8036#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8037 size_t cert_len;
8038#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8039#endif /* MBEDTLS_X509_CRT_PARSE_C */
8040
8041 /*
8042 * Time
8043 */
8044#if defined(MBEDTLS_HAVE_TIME)
8045 used += 8;
8046
8047 if( used <= buf_len )
8048 {
8049 start = (uint64_t) session->start;
8050
8051 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8052 p += 8;
8053 }
8054#endif /* MBEDTLS_HAVE_TIME */
8055
8056 /*
8057 * Basic mandatory fields
8058 */
8059 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008060 + 1 /* id_len */
8061 + sizeof( session->id )
8062 + sizeof( session->master )
8063 + 4; /* verify_result */
8064
8065 if( used <= buf_len )
8066 {
8067 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8068 p += 2;
8069
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008070 *p++ = MBEDTLS_BYTE_0( session->id_len );
8071 memcpy( p, session->id, 32 );
8072 p += 32;
8073
8074 memcpy( p, session->master, 48 );
8075 p += 48;
8076
8077 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8078 p += 4;
8079 }
8080
8081 /*
8082 * Peer's end-entity certificate
8083 */
8084#if defined(MBEDTLS_X509_CRT_PARSE_C)
8085#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8086 if( session->peer_cert == NULL )
8087 cert_len = 0;
8088 else
8089 cert_len = session->peer_cert->raw.len;
8090
8091 used += 3 + cert_len;
8092
8093 if( used <= buf_len )
8094 {
8095 *p++ = MBEDTLS_BYTE_2( cert_len );
8096 *p++ = MBEDTLS_BYTE_1( cert_len );
8097 *p++ = MBEDTLS_BYTE_0( cert_len );
8098
8099 if( session->peer_cert != NULL )
8100 {
8101 memcpy( p, session->peer_cert->raw.p, cert_len );
8102 p += cert_len;
8103 }
8104 }
8105#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8106 if( session->peer_cert_digest != NULL )
8107 {
8108 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8109 if( used <= buf_len )
8110 {
8111 *p++ = (unsigned char) session->peer_cert_digest_type;
8112 *p++ = (unsigned char) session->peer_cert_digest_len;
8113 memcpy( p, session->peer_cert_digest,
8114 session->peer_cert_digest_len );
8115 p += session->peer_cert_digest_len;
8116 }
8117 }
8118 else
8119 {
8120 used += 2;
8121 if( used <= buf_len )
8122 {
8123 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8124 *p++ = 0;
8125 }
8126 }
8127#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8128#endif /* MBEDTLS_X509_CRT_PARSE_C */
8129
8130 /*
8131 * Session ticket if any, plus associated data
8132 */
8133#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8134 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8135
8136 if( used <= buf_len )
8137 {
8138 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8139 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8140 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8141
8142 if( session->ticket != NULL )
8143 {
8144 memcpy( p, session->ticket, session->ticket_len );
8145 p += session->ticket_len;
8146 }
8147
8148 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8149 p += 4;
8150 }
8151#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8152
8153 /*
8154 * Misc extension-related info
8155 */
8156#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8157 used += 1;
8158
8159 if( used <= buf_len )
8160 *p++ = session->mfl_code;
8161#endif
8162
8163#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8164 used += 1;
8165
8166 if( used <= buf_len )
8167 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8168#endif
8169
8170 return( used );
8171}
8172
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008173MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008174static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008175 const unsigned char *buf,
8176 size_t len )
8177{
8178#if defined(MBEDTLS_HAVE_TIME)
8179 uint64_t start;
8180#endif
8181#if defined(MBEDTLS_X509_CRT_PARSE_C)
8182#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8183 size_t cert_len;
8184#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8185#endif /* MBEDTLS_X509_CRT_PARSE_C */
8186
8187 const unsigned char *p = buf;
8188 const unsigned char * const end = buf + len;
8189
8190 /*
8191 * Time
8192 */
8193#if defined(MBEDTLS_HAVE_TIME)
8194 if( 8 > (size_t)( end - p ) )
8195 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8196
8197 start = ( (uint64_t) p[0] << 56 ) |
8198 ( (uint64_t) p[1] << 48 ) |
8199 ( (uint64_t) p[2] << 40 ) |
8200 ( (uint64_t) p[3] << 32 ) |
8201 ( (uint64_t) p[4] << 24 ) |
8202 ( (uint64_t) p[5] << 16 ) |
8203 ( (uint64_t) p[6] << 8 ) |
8204 ( (uint64_t) p[7] );
8205 p += 8;
8206
8207 session->start = (time_t) start;
8208#endif /* MBEDTLS_HAVE_TIME */
8209
8210 /*
8211 * Basic mandatory fields
8212 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008213 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008214 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8215
8216 session->ciphersuite = ( p[0] << 8 ) | p[1];
8217 p += 2;
8218
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008219 session->id_len = *p++;
8220 memcpy( session->id, p, 32 );
8221 p += 32;
8222
8223 memcpy( session->master, p, 48 );
8224 p += 48;
8225
8226 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8227 ( (uint32_t) p[1] << 16 ) |
8228 ( (uint32_t) p[2] << 8 ) |
8229 ( (uint32_t) p[3] );
8230 p += 4;
8231
8232 /* Immediately clear invalid pointer values that have been read, in case
8233 * we exit early before we replaced them with valid ones. */
8234#if defined(MBEDTLS_X509_CRT_PARSE_C)
8235#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8236 session->peer_cert = NULL;
8237#else
8238 session->peer_cert_digest = NULL;
8239#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8240#endif /* MBEDTLS_X509_CRT_PARSE_C */
8241#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8242 session->ticket = NULL;
8243#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8244
8245 /*
8246 * Peer certificate
8247 */
8248#if defined(MBEDTLS_X509_CRT_PARSE_C)
8249#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8250 /* Deserialize CRT from the end of the ticket. */
8251 if( 3 > (size_t)( end - p ) )
8252 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8253
8254 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8255 p += 3;
8256
8257 if( cert_len != 0 )
8258 {
8259 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8260
8261 if( cert_len > (size_t)( end - p ) )
8262 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8263
8264 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8265
8266 if( session->peer_cert == NULL )
8267 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8268
8269 mbedtls_x509_crt_init( session->peer_cert );
8270
8271 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8272 p, cert_len ) ) != 0 )
8273 {
8274 mbedtls_x509_crt_free( session->peer_cert );
8275 mbedtls_free( session->peer_cert );
8276 session->peer_cert = NULL;
8277 return( ret );
8278 }
8279
8280 p += cert_len;
8281 }
8282#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8283 /* Deserialize CRT digest from the end of the ticket. */
8284 if( 2 > (size_t)( end - p ) )
8285 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8286
8287 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8288 session->peer_cert_digest_len = (size_t) *p++;
8289
8290 if( session->peer_cert_digest_len != 0 )
8291 {
8292 const mbedtls_md_info_t *md_info =
8293 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8294 if( md_info == NULL )
8295 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8296 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8297 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8298
8299 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8300 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8301
8302 session->peer_cert_digest =
8303 mbedtls_calloc( 1, session->peer_cert_digest_len );
8304 if( session->peer_cert_digest == NULL )
8305 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8306
8307 memcpy( session->peer_cert_digest, p,
8308 session->peer_cert_digest_len );
8309 p += session->peer_cert_digest_len;
8310 }
8311#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8312#endif /* MBEDTLS_X509_CRT_PARSE_C */
8313
8314 /*
8315 * Session ticket and associated data
8316 */
8317#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8318 if( 3 > (size_t)( end - p ) )
8319 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8320
8321 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8322 p += 3;
8323
8324 if( session->ticket_len != 0 )
8325 {
8326 if( session->ticket_len > (size_t)( end - p ) )
8327 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8328
8329 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8330 if( session->ticket == NULL )
8331 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8332
8333 memcpy( session->ticket, p, session->ticket_len );
8334 p += session->ticket_len;
8335 }
8336
8337 if( 4 > (size_t)( end - p ) )
8338 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8339
8340 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8341 ( (uint32_t) p[1] << 16 ) |
8342 ( (uint32_t) p[2] << 8 ) |
8343 ( (uint32_t) p[3] );
8344 p += 4;
8345#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8346
8347 /*
8348 * Misc extension-related info
8349 */
8350#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8351 if( 1 > (size_t)( end - p ) )
8352 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8353
8354 session->mfl_code = *p++;
8355#endif
8356
8357#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8358 if( 1 > (size_t)( end - p ) )
8359 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8360
8361 session->encrypt_then_mac = *p++;
8362#endif
8363
8364 /* Done, should have consumed entire buffer */
8365 if( p != end )
8366 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8367
8368 return( 0 );
8369}
Jerry Yudc7bd172022-02-17 13:44:15 +08008370#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8371
XiaokangQian75d40ef2022-04-20 11:05:24 +00008372int mbedtls_ssl_validate_ciphersuite(
8373 const mbedtls_ssl_context *ssl,
8374 const mbedtls_ssl_ciphersuite_t *suite_info,
8375 mbedtls_ssl_protocol_version min_tls_version,
8376 mbedtls_ssl_protocol_version max_tls_version )
8377{
8378 (void) ssl;
8379
8380 if( suite_info == NULL )
8381 return( -1 );
8382
8383 if( ( suite_info->min_tls_version > max_tls_version ) ||
8384 ( suite_info->max_tls_version < min_tls_version ) )
8385 {
8386 return( -1 );
8387 }
8388
XiaokangQian060d8672022-04-21 09:24:56 +00008389#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008390#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8391 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8392 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8393 {
8394 return( -1 );
8395 }
8396#endif
8397
8398 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8399#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8400 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8401 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8402 {
8403 return( -1 );
8404 }
8405#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8406#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8407
8408 return( 0 );
8409}
8410
XiaokangQianeaf36512022-04-24 09:07:44 +00008411#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8412/*
8413 * Function for writing a signature algorithm extension.
8414 *
8415 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8416 * value (TLS 1.3 RFC8446):
8417 * enum {
8418 * ....
8419 * ecdsa_secp256r1_sha256( 0x0403 ),
8420 * ecdsa_secp384r1_sha384( 0x0503 ),
8421 * ecdsa_secp521r1_sha512( 0x0603 ),
8422 * ....
8423 * } SignatureScheme;
8424 *
8425 * struct {
8426 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8427 * } SignatureSchemeList;
8428 *
8429 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8430 * value (TLS 1.2 RFC5246):
8431 * enum {
8432 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8433 * sha512(6), (255)
8434 * } HashAlgorithm;
8435 *
8436 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8437 * SignatureAlgorithm;
8438 *
8439 * struct {
8440 * HashAlgorithm hash;
8441 * SignatureAlgorithm signature;
8442 * } SignatureAndHashAlgorithm;
8443 *
8444 * SignatureAndHashAlgorithm
8445 * supported_signature_algorithms<2..2^16-2>;
8446 *
8447 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8448 * generalization of the TLS 1.2 signature algorithm extension.
8449 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8450 * `SignatureScheme` field of TLS 1.3
8451 *
8452 */
8453int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8454 const unsigned char *end, size_t *out_len )
8455{
8456 unsigned char *p = buf;
8457 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8458 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8459
8460 *out_len = 0;
8461
8462 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8463
8464 /* Check if we have space for header and length field:
8465 * - extension_type (2 bytes)
8466 * - extension_data_length (2 bytes)
8467 * - supported_signature_algorithms_length (2 bytes)
8468 */
8469 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8470 p += 6;
8471
8472 /*
8473 * Write supported_signature_algorithms
8474 */
8475 supported_sig_alg = p;
8476 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8477 if( sig_alg == NULL )
8478 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8479
8480 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8481 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008482 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8483 *sig_alg,
8484 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008485 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8486 continue;
8487 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8488 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8489 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008490 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008491 *sig_alg,
8492 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008493 }
8494
8495 /* Length of supported_signature_algorithms */
8496 supported_sig_alg_len = p - supported_sig_alg;
8497 if( supported_sig_alg_len == 0 )
8498 {
8499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8500 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8501 }
8502
XiaokangQianeaf36512022-04-24 09:07:44 +00008503 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008504 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008505 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8506
XiaokangQianeaf36512022-04-24 09:07:44 +00008507 *out_len = p - buf;
8508
8509#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8510 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8511#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8512 return( 0 );
8513}
8514#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8515
XiaokangQian40a35232022-05-07 09:02:40 +00008516#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008517/*
8518 * mbedtls_ssl_parse_server_name_ext
8519 *
8520 * Structure of server_name extension:
8521 *
8522 * enum {
8523 * host_name(0), (255)
8524 * } NameType;
8525 * opaque HostName<1..2^16-1>;
8526 *
8527 * struct {
8528 * NameType name_type;
8529 * select (name_type) {
8530 * case host_name: HostName;
8531 * } name;
8532 * } ServerName;
8533 * struct {
8534 * ServerName server_name_list<1..2^16-1>
8535 * } ServerNameList;
8536 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008537MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008538int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8539 const unsigned char *buf,
8540 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008541{
8542 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8543 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008544 size_t server_name_list_len, hostname_len;
8545 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008546
XiaokangQianf2a94202022-05-20 06:44:24 +00008547 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008548
8549 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008550 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008551 p += 2;
8552
XiaokangQian9b2b7712022-05-17 02:57:00 +00008553 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8554 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008555 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008556 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008557 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008558 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008559 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8560 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008561
8562 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8563 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008564 /* sni_name is intended to be used only during the parsing of the
8565 * ClientHello message (it is reset to NULL before the end of
8566 * the message parsing). Thus it is ok to just point to the
8567 * reception buffer and not make a copy of it.
8568 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008569 ssl->handshake->sni_name = p + 3;
8570 ssl->handshake->sni_name_len = hostname_len;
8571 if( ssl->conf->f_sni == NULL )
8572 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008573 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008574 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008575 if( ret != 0 )
8576 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008577 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008578 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8579 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008580 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8581 }
8582 return( 0 );
8583 }
8584
8585 p += hostname_len + 3;
8586 }
8587
8588 return( 0 );
8589}
8590#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8591
XiaokangQianacb39922022-06-17 10:18:48 +00008592#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008593MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008594int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8595 const unsigned char *buf,
8596 const unsigned char *end )
8597{
8598 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008599 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008600 const unsigned char *protocol_name_list;
8601 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008602 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008603
8604 /* If ALPN not configured, just ignore the extension */
8605 if( ssl->conf->alpn_list == NULL )
8606 return( 0 );
8607
8608 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008609 * RFC7301, section 3.1
8610 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008611 *
XiaokangQianc7403452022-06-23 03:24:12 +00008612 * struct {
8613 * ProtocolName protocol_name_list<2..2^16-1>
8614 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008615 */
8616
XiaokangQianc7403452022-06-23 03:24:12 +00008617 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008618 * protocol_name_list_len 2 bytes
8619 * protocol_name_len 1 bytes
8620 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008621 */
XiaokangQianacb39922022-06-17 10:18:48 +00008622 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8623
XiaokangQianc7403452022-06-23 03:24:12 +00008624 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008625 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008626 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008627 protocol_name_list = p;
8628 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008629
8630 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008631 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008632 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008633 protocol_name_len = *p++;
8634 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8635 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008636 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008637 {
8638 MBEDTLS_SSL_PEND_FATAL_ALERT(
8639 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8640 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008641 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008642 }
8643
8644 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008645 }
8646
8647 /* Use our order of preference */
8648 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8649 {
8650 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008651 p = protocol_name_list;
8652 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008653 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008654 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008655 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008656 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008657 {
8658 ssl->alpn_chosen = *alpn;
8659 return( 0 );
8660 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008661
8662 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008663 }
8664 }
8665
XiaokangQian95d5f542022-06-24 02:29:26 +00008666 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008667 MBEDTLS_SSL_PEND_FATAL_ALERT(
8668 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8669 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8670 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8671}
8672
8673int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8674 unsigned char *buf,
8675 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008676 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008677{
8678 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008679 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008680 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008681
8682 if( ssl->alpn_chosen == NULL )
8683 {
8684 return( 0 );
8685 }
8686
XiaokangQian95d5f542022-06-24 02:29:26 +00008687 protocol_name_len = strlen( ssl->alpn_chosen );
8688 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008689
8690 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8691 /*
8692 * 0 . 1 ext identifier
8693 * 2 . 3 ext length
8694 * 4 . 5 protocol list length
8695 * 6 . 6 protocol name length
8696 * 7 . 7+n protocol name
8697 */
8698 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8699
XiaokangQian95d5f542022-06-24 02:29:26 +00008700 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008701
XiaokangQian95d5f542022-06-24 02:29:26 +00008702 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8703 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008704 /* Note: the length of the chosen protocol has been checked to be less
8705 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8706 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008707 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008708
XiaokangQian95d5f542022-06-24 02:29:26 +00008709 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008710 return ( 0 );
8711}
8712#endif /* MBEDTLS_SSL_ALPN */
8713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008714#endif /* MBEDTLS_SSL_TLS_C */