blob: d37102657a4ddfd38e5c14c9cecaf6032b6cc9e3 [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"
Andrzej Kurek25f27152022-08-17 16:09:31 -040044
Janos Follath73c616b2019-12-18 15:07:04 +000045#include "mbedtls/debug.h"
46#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050047#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010048#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020049#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020057#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050058
Janos Follath23bdca02016-10-07 14:47:14 +010059#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020061#endif
62
Ronald Cronad8c17b2022-06-10 17:18:09 +020063#if defined(MBEDTLS_TEST_HOOKS)
64static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
65
66void mbedtls_ssl_set_chk_buf_ptr_fail_args(
67 const uint8_t *cur, const uint8_t *end, size_t need )
68{
69 chk_buf_ptr_fail_args.cur = cur;
70 chk_buf_ptr_fail_args.end = end;
71 chk_buf_ptr_fail_args.need = need;
72}
73
74void mbedtls_ssl_reset_chk_buf_ptr_fail_args( void )
75{
76 memset( &chk_buf_ptr_fail_args, 0, sizeof( chk_buf_ptr_fail_args ) );
77}
78
79int mbedtls_ssl_cmp_chk_buf_ptr_fail_args( mbedtls_ssl_chk_buf_ptr_args *args )
80{
81 return( ( chk_buf_ptr_fail_args.cur != args->cur ) ||
82 ( chk_buf_ptr_fail_args.end != args->end ) ||
83 ( chk_buf_ptr_fail_args.need != args->need ) );
84}
85#endif /* MBEDTLS_TEST_HOOKS */
86
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020087#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010088
Hanno Beckera0e20d02019-05-15 14:03:01 +010089#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010090/* Top-level Connection ID API */
91
Hanno Becker8367ccc2019-05-14 11:30:10 +010092int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
93 size_t len,
94 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +010095{
96 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
97 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
98
Hanno Becker611ac772019-05-14 11:45:26 +010099 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
100 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
101 {
102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
103 }
104
105 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100106 conf->cid_len = len;
107 return( 0 );
108}
109
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100110int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
111 int enable,
112 unsigned char const *own_cid,
113 size_t own_cid_len )
114{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100115 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
116 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
117
Hanno Beckerca092242019-04-25 16:01:49 +0100118 ssl->negotiate_cid = enable;
119 if( enable == MBEDTLS_SSL_CID_DISABLED )
120 {
121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
122 return( 0 );
123 }
124 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100125 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100126
Hanno Beckerad4a1372019-05-03 13:06:44 +0100127 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100128 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100129 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
130 (unsigned) own_cid_len,
131 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
133 }
134
135 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100136 /* Truncation is not an issue here because
137 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
138 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100139
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100140 return( 0 );
141}
142
Paul Elliott0113cf12022-03-11 20:26:47 +0000143int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl,
144 int *enabled,
145 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
146 size_t *own_cid_len )
147{
148 *enabled = MBEDTLS_SSL_CID_DISABLED;
149
150 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
151 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
152
153 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
154 * zero as this is indistinguishable from not requesting to use
155 * the CID extension. */
156 if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
157 return( 0 );
158
159 if( own_cid_len != NULL )
160 {
161 *own_cid_len = ssl->own_cid_len;
162 if( own_cid != NULL )
163 memcpy( own_cid, ssl->own_cid, ssl->own_cid_len );
164 }
165
166 *enabled = MBEDTLS_SSL_CID_ENABLED;
167
168 return( 0 );
169}
170
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100171int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
172 int *enabled,
173 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
174 size_t *peer_cid_len )
175{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100176 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100177
Hanno Becker76a79ab2019-05-03 14:38:32 +0100178 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Paul Elliott27b0d942022-03-18 21:55:32 +0000179 mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Hanno Becker76a79ab2019-05-03 14:38:32 +0100180 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100182 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100183
Hanno Beckerc5f24222019-05-03 12:54:52 +0100184 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
185 * were used, but client and server requested the empty CID.
186 * This is indistinguishable from not using the CID extension
187 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100188 if( ssl->transform_in->in_cid_len == 0 &&
189 ssl->transform_in->out_cid_len == 0 )
190 {
191 return( 0 );
192 }
193
Hanno Becker615ef172019-05-22 16:50:35 +0100194 if( peer_cid_len != NULL )
195 {
196 *peer_cid_len = ssl->transform_in->out_cid_len;
197 if( peer_cid != NULL )
198 {
199 memcpy( peer_cid, ssl->transform_in->out_cid,
200 ssl->transform_in->out_cid_len );
201 }
202 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100203
204 *enabled = MBEDTLS_SSL_CID_ENABLED;
205
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100206 return( 0 );
207}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100208#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200213/*
214 * Convert max_fragment_length codes to length.
215 * RFC 6066 says:
216 * enum{
217 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
218 * } MaxFragmentLength;
219 * and we add 0 -> extension unused
220 */
Angus Grattond8213d02016-05-25 20:56:48 +1000221static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200222{
Angus Grattond8213d02016-05-25 20:56:48 +1000223 switch( mfl )
224 {
225 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
226 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
227 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
228 return 512;
229 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
230 return 1024;
231 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
232 return 2048;
233 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
234 return 4096;
235 default:
236 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
237 }
238}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200240
Hanno Becker52055ae2019-02-06 14:30:46 +0000241int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
242 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200243{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_ssl_session_free( dst );
245 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200246
吴敬辉0b716112021-11-29 10:46:35 +0800247#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qian87306442022-10-12 09:47:38 +0000248 if( src->ticket != NULL )
249 {
250 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
251 if( dst->ticket == NULL )
252 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
253
254 memcpy( dst->ticket, src->ticket, src->ticket_len );
255 }
256
257#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
258 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
259 if( src->endpoint == MBEDTLS_SSL_IS_CLIENT )
260 {
261 dst->hostname = NULL;
262 return mbedtls_ssl_session_set_hostname( dst,
263 src->hostname );
264 }
吴敬辉0b716112021-11-29 10:46:35 +0800265#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000266#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000269
270#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200271 if( src->peer_cert != NULL )
272 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000273 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200274
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200275 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200276 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200277 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200282 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200285 dst->peer_cert = NULL;
286 return( ret );
287 }
288 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000289#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000290 if( src->peer_cert_digest != NULL )
291 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000292 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000293 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000294 if( dst->peer_cert_digest == NULL )
295 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
296
297 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
298 src->peer_cert_digest_len );
299 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000300 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000301 }
302#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200305
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200306 return( 0 );
307}
308
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500309#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200310MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500311static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
312{
313 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
314 if( resized_buffer == NULL )
315 return -1;
316
317 /* We want to copy len_new bytes when downsizing the buffer, and
318 * len_old bytes when upsizing, so we choose the smaller of two sizes,
319 * to fit one buffer into another. Size checks, ensuring that no data is
320 * lost, are done outside of this function. */
321 memcpy( resized_buffer, *buffer,
322 ( len_new < *len_old ) ? len_new : *len_old );
323 mbedtls_platform_zeroize( *buffer, *len_old );
324 mbedtls_free( *buffer );
325
326 *buffer = resized_buffer;
327 *len_old = len_new;
328
329 return 0;
330}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200331
332static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500333 size_t in_buf_new_len,
334 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200335{
336 int modified = 0;
337 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
338 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
339 if( ssl->in_buf != NULL )
340 {
341 written_in = ssl->in_msg - ssl->in_buf;
342 iv_offset_in = ssl->in_iv - ssl->in_buf;
343 len_offset_in = ssl->in_len - ssl->in_buf;
344 if( downsizing ?
345 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
346 ssl->in_buf_len < in_buf_new_len )
347 {
348 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
349 {
350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
351 }
352 else
353 {
Paul Elliottb7449902021-03-10 18:14:58 +0000354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
355 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200356 modified = 1;
357 }
358 }
359 }
360
361 if( ssl->out_buf != NULL )
362 {
363 written_out = ssl->out_msg - ssl->out_buf;
364 iv_offset_out = ssl->out_iv - ssl->out_buf;
365 len_offset_out = ssl->out_len - ssl->out_buf;
366 if( downsizing ?
367 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
368 ssl->out_buf_len < out_buf_new_len )
369 {
370 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
371 {
372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
373 }
374 else
375 {
Paul Elliottb7449902021-03-10 18:14:58 +0000376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
377 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200378 modified = 1;
379 }
380 }
381 }
382 if( modified )
383 {
384 /* Update pointers here to avoid doing it twice. */
385 mbedtls_ssl_reset_in_out_pointers( ssl );
386 /* Fields below might not be properly updated with record
387 * splitting or with CID, so they are manually updated here. */
388 ssl->out_msg = ssl->out_buf + written_out;
389 ssl->out_len = ssl->out_buf + len_offset_out;
390 ssl->out_iv = ssl->out_buf + iv_offset_out;
391
392 ssl->in_msg = ssl->in_buf + written_in;
393 ssl->in_len = ssl->in_buf + len_offset_in;
394 ssl->in_iv = ssl->in_buf + iv_offset_in;
395 }
396}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500397#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
398
Jerry Yudb8c48a2022-01-27 14:54:54 +0800399#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800400
401#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
402typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
403 const char *label,
404 const unsigned char *random, size_t rlen,
405 unsigned char *dstbuf, size_t dlen );
406
407static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
408
409#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
410
411/* Type for the TLS PRF */
412typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
413 const unsigned char *, size_t,
414 unsigned char *, size_t);
415
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200416MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800417static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
418 int ciphersuite,
419 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200420#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yued14c932022-02-17 13:40:45 +0800421 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200422#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yued14c932022-02-17 13:40:45 +0800423 ssl_tls_prf_t tls_prf,
424 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -0400425 mbedtls_ssl_protocol_version tls_version,
Jerry Yued14c932022-02-17 13:40:45 +0800426 unsigned endpoint,
427 const mbedtls_ssl_context *ssl );
428
Andrzej Kurek25f27152022-08-17 16:09:31 -0400429#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200430MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800431static int tls_prf_sha256( const unsigned char *secret, size_t slen,
432 const char *label,
433 const unsigned char *random, size_t rlen,
434 unsigned char *dstbuf, size_t dlen );
435static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
436static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
437
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400438#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800439
Andrzej Kurek25f27152022-08-17 16:09:31 -0400440#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200441MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800442static int tls_prf_sha384( const unsigned char *secret, size_t slen,
443 const char *label,
444 const unsigned char *random, size_t rlen,
445 unsigned char *dstbuf, size_t dlen );
446
447static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
448static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400449#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800450
Jerry Yu438ddd82022-07-07 06:55:50 +0000451static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800452 unsigned char *buf,
453 size_t buf_len );
Jerry Yu251a12e2022-07-13 15:15:48 +0800454
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200455MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +0000456static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800457 const unsigned char *buf,
458 size_t len );
459#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
460
Jerry Yu53d23e22022-02-09 16:25:09 +0800461static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
462
Andrzej Kurek25f27152022-08-17 16:09:31 -0400463#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800464static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400465#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudb8c48a2022-01-27 14:54:54 +0800466
Andrzej Kurek25f27152022-08-17 16:09:31 -0400467#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800468static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400469#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Paul Bakker1ef83d62012-04-11 12:09:53 +0000470
Ron Eldor51d3ab52019-05-12 14:54:30 +0300471int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
472 const unsigned char *secret, size_t slen,
473 const char *label,
474 const unsigned char *random, size_t rlen,
475 unsigned char *dstbuf, size_t dlen )
476{
477 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
478
479 switch( prf )
480 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300481#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400482#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300483 case MBEDTLS_SSL_TLS_PRF_SHA384:
484 tls_prf = tls_prf_sha384;
485 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400486#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400487#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300488 case MBEDTLS_SSL_TLS_PRF_SHA256:
489 tls_prf = tls_prf_sha256;
490 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400491#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300492#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300493 default:
494 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
495 }
496
497 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
498}
499
Jerry Yuc73c6182022-02-08 20:29:25 +0800500#if defined(MBEDTLS_X509_CRT_PARSE_C)
501static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
502{
503#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
504 if( session->peer_cert != NULL )
505 {
506 mbedtls_x509_crt_free( session->peer_cert );
507 mbedtls_free( session->peer_cert );
508 session->peer_cert = NULL;
509 }
510#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
511 if( session->peer_cert_digest != NULL )
512 {
513 /* Zeroization is not necessary. */
514 mbedtls_free( session->peer_cert_digest );
515 session->peer_cert_digest = NULL;
516 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
517 session->peer_cert_digest_len = 0;
518 }
519#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
520}
521#endif /* MBEDTLS_X509_CRT_PARSE_C */
522
523void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
524 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
525{
526 ((void) ciphersuite_info);
527
Andrzej Kurek25f27152022-08-17 16:09:31 -0400528#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800529 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
530 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
531 else
532#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400533#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800534 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
535 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
536 else
537#endif
538 {
539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
540 return;
541 }
542}
543
XiaokangQianadab9a62022-07-18 07:41:26 +0000544void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
545 unsigned hs_type,
546 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100547{
548 unsigned char hs_hdr[4];
549
550 /* Build HS header for checksum update. */
551 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
552 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
553 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
554 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
555
556 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
557}
558
559void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
560 unsigned hs_type,
561 unsigned char const *msg,
562 size_t msg_len )
563{
564 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
565 ssl->handshake->update_checksum( ssl, msg, msg_len );
566}
567
Jerry Yuc73c6182022-02-08 20:29:25 +0800568void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
569{
570 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400571#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800572#if defined(MBEDTLS_USE_PSA_CRYPTO)
573 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
574 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
575#else
576 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
577#endif
578#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400579#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800580#if defined(MBEDTLS_USE_PSA_CRYPTO)
581 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
582 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
583#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400584 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800585#endif
586#endif
587}
588
589static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
590 const unsigned char *buf, size_t len )
591{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400592#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800593#if defined(MBEDTLS_USE_PSA_CRYPTO)
594 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
595#else
596 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
597#endif
598#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400599#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800600#if defined(MBEDTLS_USE_PSA_CRYPTO)
601 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
602#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400603 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800604#endif
605#endif
606}
607
Andrzej Kurek25f27152022-08-17 16:09:31 -0400608#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800609static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
610 const unsigned char *buf, size_t len )
611{
612#if defined(MBEDTLS_USE_PSA_CRYPTO)
613 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
614#else
615 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
616#endif
617}
618#endif
619
Andrzej Kurek25f27152022-08-17 16:09:31 -0400620#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800621static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
622 const unsigned char *buf, size_t len )
623{
624#if defined(MBEDTLS_USE_PSA_CRYPTO)
625 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
626#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400627 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800628#endif
629}
630#endif
631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200633{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200635
Andrzej Kurek25f27152022-08-17 16:09:31 -0400636#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500637#if defined(MBEDTLS_USE_PSA_CRYPTO)
638 handshake->fin_sha256_psa = psa_hash_operation_init();
639 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
640#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200642 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200643#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500644#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400645#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500646#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500647 handshake->fin_sha384_psa = psa_hash_operation_init();
648 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500649#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400650 mbedtls_sha512_init( &handshake->fin_sha384 );
651 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200652#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500653#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200654
655 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657#if defined(MBEDTLS_DHM_C)
658 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200659#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200660#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200662#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200663#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200664 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200665#if defined(MBEDTLS_SSL_CLI_C)
666 handshake->ecjpake_cache = NULL;
667 handshake->ecjpake_cache_len = 0;
668#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200669#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200670
Gilles Peskineeccd8882020-03-10 12:19:08 +0100671#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200672 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200673#endif
674
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200675#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
676 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
677#endif
Hanno Becker75173122019-02-06 16:18:31 +0000678
679#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
680 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
681 mbedtls_pk_init( &handshake->peer_pubkey );
682#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200683}
684
Hanno Beckera18d1322018-01-03 14:27:32 +0000685void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200686{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200688
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100689#if defined(MBEDTLS_USE_PSA_CRYPTO)
690 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
691 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100692#else
693 mbedtls_cipher_init( &transform->cipher_ctx_enc );
694 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100695#endif
696
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000697#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100698#if defined(MBEDTLS_USE_PSA_CRYPTO)
699 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
700 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100701#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 mbedtls_md_init( &transform->md_ctx_enc );
703 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000704#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100705#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200706}
707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200709{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200711}
712
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200713MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000715{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200716 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000717 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200719 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200721 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200722 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200723
724 /*
725 * Either the pointers are now NULL or cleared properly and can be freed.
726 * Now allocate missing structures.
727 */
728 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200729 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200730 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200731 }
Paul Bakker48916f92012-09-16 19:57:18 +0000732
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200733 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200734 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200735 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200736 }
Paul Bakker48916f92012-09-16 19:57:18 +0000737
Paul Bakker82788fb2014-10-20 13:59:19 +0200738 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200739 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200740 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200741 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500742#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
743 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400744
Andrzej Kurek4a063792020-10-21 15:08:44 +0200745 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
746 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500747#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000748
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200749 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000750 if( ssl->handshake == NULL ||
751 ssl->transform_negotiate == NULL ||
752 ssl->session_negotiate == NULL )
753 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 mbedtls_free( ssl->handshake );
757 mbedtls_free( ssl->transform_negotiate );
758 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200759
760 ssl->handshake = NULL;
761 ssl->transform_negotiate = NULL;
762 ssl->session_negotiate = NULL;
763
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200764 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000765 }
766
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200767 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000769 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200770 ssl_handshake_params_init( ssl->handshake );
771
Jerry Yud0766ec2022-09-22 10:46:57 +0800772#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
773 defined(MBEDTLS_SSL_SRV_C) && \
774 defined(MBEDTLS_SSL_SESSION_TICKETS)
775 ssl->handshake->new_session_tickets_count =
776 ssl->conf->new_session_tickets_count ;
777#endif
778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200780 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
781 {
782 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200783
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200784 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
785 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
786 else
787 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200788
Hanno Becker0f57a652020-02-05 10:37:26 +0000789 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200790 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200791#endif
792
Brett Warrene0edc842021-08-17 09:53:13 +0100793/*
794 * curve_list is translated to IANA TLS group identifiers here because
795 * mbedtls_ssl_conf_curves returns void and so can't return
796 * any error codes.
797 */
798#if defined(MBEDTLS_ECP_C)
799#if !defined(MBEDTLS_DEPRECATED_REMOVED)
800 /* Heap allocate and translate curve_list from internal to IANA group ids */
801 if ( ssl->conf->curve_list != NULL )
802 {
803 size_t length;
804 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
805
806 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
807 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
808
809 /* Leave room for zero termination */
810 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
811 if ( group_list == NULL )
812 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
813
814 for( size_t i = 0; i < length; i++ )
815 {
816 const mbedtls_ecp_curve_info *info =
817 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
818 if ( info == NULL )
819 {
820 mbedtls_free( group_list );
821 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
822 }
823 group_list[i] = info->tls_id;
824 }
825
826 group_list[length] = 0;
827
828 ssl->handshake->group_list = group_list;
829 ssl->handshake->group_list_heap_allocated = 1;
830 }
831 else
832 {
833 ssl->handshake->group_list = ssl->conf->group_list;
834 ssl->handshake->group_list_heap_allocated = 0;
835 }
836#endif /* MBEDTLS_DEPRECATED_REMOVED */
837#endif /* MBEDTLS_ECP_C */
838
Jerry Yuf017ee42022-01-12 15:49:48 +0800839#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
840#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800841#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800842 /* Heap allocate and translate sig_hashes from internal hash identifiers to
843 signature algorithms IANA identifiers. */
844 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800845 ssl->conf->sig_hashes != NULL )
846 {
847 const int *md;
848 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800849 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800850 uint16_t *p;
851
Jerry Yub476a442022-01-21 18:14:45 +0800852#if defined(static_assert)
853 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
854 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
855 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800856#endif
857
Jerry Yuf017ee42022-01-12 15:49:48 +0800858 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
859 {
860 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
861 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800862#if defined(MBEDTLS_ECDSA_C)
863 sig_algs_len += sizeof( uint16_t );
864#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800865
Jerry Yub476a442022-01-21 18:14:45 +0800866#if defined(MBEDTLS_RSA_C)
867 sig_algs_len += sizeof( uint16_t );
868#endif
869 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
870 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800871 }
872
Jerry Yub476a442022-01-21 18:14:45 +0800873 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800874 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
875
Jerry Yub476a442022-01-21 18:14:45 +0800876 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
877 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800878 if( ssl->handshake->sig_algs == NULL )
879 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
880
881 p = (uint16_t *)ssl->handshake->sig_algs;
882 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
883 {
884 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
885 if( hash == MBEDTLS_SSL_HASH_NONE )
886 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800887#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800888 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
889 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800890#endif
891#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800892 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
893 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800894#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800895 }
Gabor Mezei15b95a62022-05-09 16:37:58 +0200896 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +0800897 ssl->handshake->sig_algs_heap_allocated = 1;
898 }
899 else
Jerry Yua69269a2022-01-17 21:06:01 +0800900#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800901 {
Jerry Yuf017ee42022-01-12 15:49:48 +0800902 ssl->handshake->sig_algs_heap_allocated = 0;
903 }
Jerry Yucc539102022-06-27 16:27:35 +0800904#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yuf017ee42022-01-12 15:49:48 +0800905#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000906 return( 0 );
907}
908
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200909#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200910/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200911MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200912static int ssl_cookie_write_dummy( void *ctx,
913 unsigned char **p, unsigned char *end,
914 const unsigned char *cli_id, size_t cli_id_len )
915{
916 ((void) ctx);
917 ((void) p);
918 ((void) end);
919 ((void) cli_id);
920 ((void) cli_id_len);
921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200923}
924
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200925MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200926static int ssl_cookie_check_dummy( void *ctx,
927 const unsigned char *cookie, size_t cookie_len,
928 const unsigned char *cli_id, size_t cli_id_len )
929{
930 ((void) ctx);
931 ((void) cookie);
932 ((void) cookie_len);
933 ((void) cli_id);
934 ((void) cli_id_len);
935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200937}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200938#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200939
Paul Bakker5121ce52009-01-03 21:22:43 +0000940/*
941 * Initialize an SSL context
942 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200943void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
944{
945 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
946}
947
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200948MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800949static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
950{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100951 const mbedtls_ssl_config *conf = ssl->conf;
952
Ronald Cron6f135e12021-12-08 16:57:54 +0100953#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100954 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
955 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000956 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
957 {
958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
959 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
960 }
961
Jerry Yu60835a82021-08-04 10:13:52 +0800962 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
963 return( 0 );
964 }
965#endif
966
967#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100968 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800969 {
970 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
971 return( 0 );
972 }
973#endif
974
Ronald Cron6f135e12021-12-08 16:57:54 +0100975#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100976 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800977 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000978 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
979 {
980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
981 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
982 }
983
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000984 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
985 {
986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
987 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
988 }
989
990
Ronald Crone1d3f062022-02-10 14:50:54 +0100991 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
992 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +0800993 }
994#endif
995
996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
997 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
998}
999
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001000MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001001static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1002{
1003 int ret;
1004 ret = ssl_conf_version_check( ssl );
1005 if( ret != 0 )
1006 return( ret );
1007
1008 /* Space for further checks */
1009
1010 return( 0 );
1011}
1012
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001013/*
1014 * Setup an SSL context
1015 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001016
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001017int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001018 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001019{
Janos Follath865b3eb2019-12-16 11:46:15 +00001020 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001021 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1022 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001023
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001024 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001025
Jerry Yu60835a82021-08-04 10:13:52 +08001026 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1027 return( ret );
1028
Paul Bakker62f2dee2012-09-28 07:31:51 +00001029 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001030 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001031 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001032
1033 /* Set to NULL in case of an error condition */
1034 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001035
Darryl Greenb33cc762019-11-28 14:29:44 +00001036#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1037 ssl->in_buf_len = in_buf_len;
1038#endif
1039 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001040 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001041 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001043 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001044 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001045 }
1046
Darryl Greenb33cc762019-11-28 14:29:44 +00001047#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1048 ssl->out_buf_len = out_buf_len;
1049#endif
1050 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001051 if( ssl->out_buf == NULL )
1052 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001054 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001055 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 }
1057
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001058 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001059
Johan Pascalb62bb512015-12-03 21:56:45 +01001060#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001061 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001062#endif
1063
Paul Bakker48916f92012-09-16 19:57:18 +00001064 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001065 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001066
1067 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001068
1069error:
1070 mbedtls_free( ssl->in_buf );
1071 mbedtls_free( ssl->out_buf );
1072
1073 ssl->conf = NULL;
1074
Darryl Greenb33cc762019-11-28 14:29:44 +00001075#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1076 ssl->in_buf_len = 0;
1077 ssl->out_buf_len = 0;
1078#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001079 ssl->in_buf = NULL;
1080 ssl->out_buf = NULL;
1081
1082 ssl->in_hdr = NULL;
1083 ssl->in_ctr = NULL;
1084 ssl->in_len = NULL;
1085 ssl->in_iv = NULL;
1086 ssl->in_msg = NULL;
1087
1088 ssl->out_hdr = NULL;
1089 ssl->out_ctr = NULL;
1090 ssl->out_len = NULL;
1091 ssl->out_iv = NULL;
1092 ssl->out_msg = NULL;
1093
k-stachowiak9f7798e2018-07-31 16:52:32 +02001094 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001095}
1096
1097/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001098 * Reset an initialized and used SSL context for re-use while retaining
1099 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001100 *
1101 * If partial is non-zero, keep data in the input buffer and client ID.
1102 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001103 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001104void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1105 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001106{
Darryl Greenb33cc762019-11-28 14:29:44 +00001107#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1108 size_t in_buf_len = ssl->in_buf_len;
1109 size_t out_buf_len = ssl->out_buf_len;
1110#else
1111 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1112 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1113#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001114
Hanno Beckerb0302c42021-08-03 09:39:42 +01001115#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1116 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001117#endif
1118
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001119 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001120 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001121
Hanno Beckerb0302c42021-08-03 09:39:42 +01001122 mbedtls_ssl_reset_in_out_pointers( ssl );
1123
1124 /* Reset incoming message parsing */
1125 ssl->in_offt = NULL;
1126 ssl->nb_zero = 0;
1127 ssl->in_msgtype = 0;
1128 ssl->in_msglen = 0;
1129 ssl->in_hslen = 0;
1130 ssl->keep_current_message = 0;
1131 ssl->transform_in = NULL;
1132
1133#if defined(MBEDTLS_SSL_PROTO_DTLS)
1134 ssl->next_record_offset = 0;
1135 ssl->in_epoch = 0;
1136#endif
1137
1138 /* Keep current datagram if partial == 1 */
1139 if( partial == 0 )
1140 {
1141 ssl->in_left = 0;
1142 memset( ssl->in_buf, 0, in_buf_len );
1143 }
1144
Ronald Cronad8c17b2022-06-10 17:18:09 +02001145 ssl->send_alert = 0;
1146
Hanno Beckerb0302c42021-08-03 09:39:42 +01001147 /* Reset outgoing message writing */
1148 ssl->out_msgtype = 0;
1149 ssl->out_msglen = 0;
1150 ssl->out_left = 0;
1151 memset( ssl->out_buf, 0, out_buf_len );
1152 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1153 ssl->transform_out = NULL;
1154
1155#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1156 mbedtls_ssl_dtls_replay_reset( ssl );
1157#endif
1158
XiaokangQian2b01dc32022-01-21 02:53:13 +00001159#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001160 if( ssl->transform )
1161 {
1162 mbedtls_ssl_transform_free( ssl->transform );
1163 mbedtls_free( ssl->transform );
1164 ssl->transform = NULL;
1165 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001166#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1167
XiaokangQian2b01dc32022-01-21 02:53:13 +00001168#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1169 mbedtls_ssl_transform_free( ssl->transform_application );
1170 mbedtls_free( ssl->transform_application );
1171 ssl->transform_application = NULL;
1172
1173 if( ssl->handshake != NULL )
1174 {
1175 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1176 mbedtls_free( ssl->handshake->transform_earlydata );
1177 ssl->handshake->transform_earlydata = NULL;
1178
1179 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1180 mbedtls_free( ssl->handshake->transform_handshake );
1181 ssl->handshake->transform_handshake = NULL;
1182 }
1183
XiaokangQian2b01dc32022-01-21 02:53:13 +00001184#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001185}
1186
1187int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1188{
1189 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1190
1191 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1192
XiaokangQian78b1fa72022-01-19 06:56:30 +00001193 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001194
1195 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196#if defined(MBEDTLS_SSL_RENEGOTIATION)
1197 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001198 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001199
1200 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1202 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001203#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001205
Hanno Beckerb0302c42021-08-03 09:39:42 +01001206 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001207 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001208 if( ssl->session )
1209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 mbedtls_ssl_session_free( ssl->session );
1211 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001212 ssl->session = NULL;
1213 }
1214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001216 ssl->alpn_chosen = NULL;
1217#endif
1218
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001219#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01001220#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001221 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01001222#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001223 {
1224 mbedtls_free( ssl->cli_id );
1225 ssl->cli_id = NULL;
1226 ssl->cli_id_len = 0;
1227 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001228#endif
1229
Paul Bakker48916f92012-09-16 19:57:18 +00001230 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1231 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001232
1233 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001234}
1235
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001236/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001237 * Reset an initialized and used SSL context for re-use while retaining
1238 * all application-set variables, function pointers and data.
1239 */
1240int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1241{
Hanno Becker43aefe22020-02-05 10:44:56 +00001242 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001243}
1244
1245/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001246 * SSL set accessors
1247 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001248void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001249{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001250 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001251}
1252
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001253void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001254{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001255 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001256}
1257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001259void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001260{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001261 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001262}
1263#endif
1264
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001265void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001266{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001267 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001268}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001271
Hanno Becker1841b0a2018-08-24 11:13:57 +01001272void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1273 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001274{
1275 ssl->disable_datagram_packing = !allow_packing;
1276}
1277
1278void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1279 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001280{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001281 conf->hs_timeout_min = min;
1282 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001283}
1284#endif
1285
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001286void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001287{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001288 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001289}
1290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001292void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001293 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001294 void *p_vrfy )
1295{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001296 conf->f_vrfy = f_vrfy;
1297 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001298}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001300
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001301void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001302 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001303 void *p_rng )
1304{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001305 conf->f_rng = f_rng;
1306 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001307}
1308
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001309void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001310 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001311 void *p_dbg )
1312{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001313 conf->f_dbg = f_dbg;
1314 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001315}
1316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001318 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001319 mbedtls_ssl_send_t *f_send,
1320 mbedtls_ssl_recv_t *f_recv,
1321 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001322{
1323 ssl->p_bio = p_bio;
1324 ssl->f_send = f_send;
1325 ssl->f_recv = f_recv;
1326 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001327}
1328
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001329#if defined(MBEDTLS_SSL_PROTO_DTLS)
1330void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1331{
1332 ssl->mtu = mtu;
1333}
1334#endif
1335
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001336void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001337{
1338 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001339}
1340
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001341void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1342 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001343 mbedtls_ssl_set_timer_t *f_set_timer,
1344 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001345{
1346 ssl->p_timer = p_timer;
1347 ssl->f_set_timer = f_set_timer;
1348 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001349
1350 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001351 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001352}
1353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001355void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001356 void *p_cache,
1357 mbedtls_ssl_cache_get_t *f_get_cache,
1358 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001359{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001360 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001361 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001362 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001363}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#if defined(MBEDTLS_SSL_CLI_C)
1367int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001368{
Janos Follath865b3eb2019-12-16 11:46:15 +00001369 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001370
1371 if( ssl == NULL ||
1372 session == NULL ||
1373 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001374 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001377 }
1378
Hanno Beckere810bbc2021-05-14 16:01:05 +01001379 if( ssl->handshake->resume == 1 )
1380 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1381
Jerry Yu21092062022-10-10 21:21:31 +08001382#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1383 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001384 {
Jerry Yu21092062022-10-10 21:21:31 +08001385 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1386 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1387
1388 if( mbedtls_ssl_validate_ciphersuite(
1389 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1390 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1391 {
1392 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1393 session->ciphersuite ) );
1394 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1395 }
Jerry Yu40afab62022-10-08 10:42:13 +08001396 }
Jerry Yu21092062022-10-10 21:21:31 +08001397#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001398
Hanno Becker52055ae2019-02-06 14:30:46 +00001399 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1400 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001401 return( ret );
1402
Paul Bakker0a597072012-09-25 21:55:46 +00001403 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001404
1405 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001406}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001408
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001409void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1410 const int *ciphersuites )
1411{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001412 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001413}
1414
Ronald Cron6f135e12021-12-08 16:57:54 +01001415#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001416void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001417 const int kex_modes )
1418{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001419 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001420}
Ronald Cron6f135e12021-12-08 16:57:54 +01001421#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001424void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001425 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001426{
1427 conf->cert_profile = profile;
1428}
1429
Glenn Strauss36872db2022-01-22 05:06:31 -05001430static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1431{
1432 mbedtls_ssl_key_cert *cur = key_cert, *next;
1433
1434 while( cur != NULL )
1435 {
1436 next = cur->next;
1437 mbedtls_free( cur );
1438 cur = next;
1439 }
1440}
1441
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001442/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001443MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001444static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1445 mbedtls_x509_crt *cert,
1446 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001447{
niisato8ee24222018-06-25 19:05:48 +09001448 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001449
Glenn Strauss36872db2022-01-22 05:06:31 -05001450 if( cert == NULL )
1451 {
1452 /* Free list if cert is null */
1453 ssl_key_cert_free( *head );
1454 *head = NULL;
1455 return( 0 );
1456 }
1457
niisato8ee24222018-06-25 19:05:48 +09001458 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1459 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001460 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001461
niisato8ee24222018-06-25 19:05:48 +09001462 new_cert->cert = cert;
1463 new_cert->key = key;
1464 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001465
Glenn Strauss36872db2022-01-22 05:06:31 -05001466 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001467 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001468 {
niisato8ee24222018-06-25 19:05:48 +09001469 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001470 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001471 else
1472 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001473 mbedtls_ssl_key_cert *cur = *head;
1474 while( cur->next != NULL )
1475 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001476 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001477 }
1478
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001479 return( 0 );
1480}
1481
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001482int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001483 mbedtls_x509_crt *own_cert,
1484 mbedtls_pk_context *pk_key )
1485{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001486 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001487}
1488
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001489void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001490 mbedtls_x509_crt *ca_chain,
1491 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001492{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001493 conf->ca_chain = ca_chain;
1494 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001495
1496#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1497 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1498 * cannot be used together. */
1499 conf->f_ca_cb = NULL;
1500 conf->p_ca_cb = NULL;
1501#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001502}
Hanno Becker5adaad92019-03-27 16:54:37 +00001503
1504#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1505void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001506 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001507 void *p_ca_cb )
1508{
1509 conf->f_ca_cb = f_ca_cb;
1510 conf->p_ca_cb = p_ca_cb;
1511
1512 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1513 * cannot be used together. */
1514 conf->ca_chain = NULL;
1515 conf->ca_crl = NULL;
1516}
1517#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001519
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001520#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001521const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1522 size_t *name_len )
1523{
1524 *name_len = ssl->handshake->sni_name_len;
1525 return( ssl->handshake->sni_name );
1526}
1527
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001528int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1529 mbedtls_x509_crt *own_cert,
1530 mbedtls_pk_context *pk_key )
1531{
1532 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1533 own_cert, pk_key ) );
1534}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001535
1536void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1537 mbedtls_x509_crt *ca_chain,
1538 mbedtls_x509_crl *ca_crl )
1539{
1540 ssl->handshake->sni_ca_chain = ca_chain;
1541 ssl->handshake->sni_ca_crl = ca_crl;
1542}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001543
Glenn Strauss999ef702022-03-11 01:37:23 -05001544#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1545void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1546 const mbedtls_x509_crt *crt)
1547{
1548 ssl->handshake->dn_hints = crt;
1549}
1550#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1551
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001552void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1553 int authmode )
1554{
1555 ssl->handshake->sni_authmode = authmode;
1556}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001557#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1558
Hanno Becker8927c832019-04-03 12:52:50 +01001559#if defined(MBEDTLS_X509_CRT_PARSE_C)
1560void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1561 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1562 void *p_vrfy )
1563{
1564 ssl->f_vrfy = f_vrfy;
1565 ssl->p_vrfy = p_vrfy;
1566}
1567#endif
1568
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001569#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001570/*
1571 * Set EC J-PAKE password for current handshake
1572 */
1573int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1574 const unsigned char *pw,
1575 size_t pw_len )
1576{
1577 mbedtls_ecjpake_role role;
1578
Janos Follath8eb64132016-06-03 15:40:57 +01001579 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001580 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1581
1582 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1583 role = MBEDTLS_ECJPAKE_SERVER;
1584 else
1585 role = MBEDTLS_ECJPAKE_CLIENT;
1586
1587 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1588 role,
1589 MBEDTLS_MD_SHA256,
1590 MBEDTLS_ECP_DP_SECP256R1,
1591 pw, pw_len ) );
1592}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001593#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001594
Gilles Peskineeccd8882020-03-10 12:19:08 +01001595#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001596
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001597MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001598static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1599{
1600#if defined(MBEDTLS_USE_PSA_CRYPTO)
1601 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1602 return( 1 );
1603#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001604 if( conf->psk != NULL )
1605 return( 1 );
1606
1607 return( 0 );
1608}
1609
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001610static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1611{
1612 /* Remove reference to existing PSK, if any. */
1613#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001614 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001615 {
1616 /* The maintenance of the PSK key slot is the
1617 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001618 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001619 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001620#endif /* MBEDTLS_USE_PSA_CRYPTO */
1621 if( conf->psk != NULL )
1622 {
1623 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1624
1625 mbedtls_free( conf->psk );
1626 conf->psk = NULL;
1627 conf->psk_len = 0;
1628 }
1629
1630 /* Remove reference to PSK identity, if any. */
1631 if( conf->psk_identity != NULL )
1632 {
1633 mbedtls_free( conf->psk_identity );
1634 conf->psk_identity = NULL;
1635 conf->psk_identity_len = 0;
1636 }
1637}
1638
Hanno Becker7390c712018-11-15 13:33:04 +00001639/* This function assumes that PSK identity in the SSL config is unset.
1640 * It checks that the provided identity is well-formed and attempts
1641 * to make a copy of it in the SSL config.
1642 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001643MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001644static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1645 unsigned char const *psk_identity,
1646 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001647{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001648 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001649 if( psk_identity == NULL ||
1650 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001651 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001652 {
1653 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1654 }
1655
Hanno Becker7390c712018-11-15 13:33:04 +00001656 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1657 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001658 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001659
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001660 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001661 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001662
1663 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001664}
1665
Hanno Becker7390c712018-11-15 13:33:04 +00001666int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1667 const unsigned char *psk, size_t psk_len,
1668 const unsigned char *psk_identity, size_t psk_identity_len )
1669{
Janos Follath865b3eb2019-12-16 11:46:15 +00001670 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001671
1672 /* We currently only support one PSK, raw or opaque. */
1673 if( ssl_conf_psk_is_configured( conf ) )
1674 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001675
1676 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001677 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001678 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001679 if( psk_len == 0 )
1680 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1681 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1682 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1683
Hanno Becker7390c712018-11-15 13:33:04 +00001684 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1685 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1686 conf->psk_len = psk_len;
1687 memcpy( conf->psk, psk, conf->psk_len );
1688
1689 /* Check and set PSK Identity */
1690 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1691 if( ret != 0 )
1692 ssl_conf_remove_psk( conf );
1693
1694 return( ret );
1695}
1696
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001697static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1698{
1699#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001700 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001701 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001702 /* The maintenance of the external PSK key slot is the
1703 * user's responsibility. */
1704 if( ssl->handshake->psk_opaque_is_internal )
1705 {
1706 psa_destroy_key( ssl->handshake->psk_opaque );
1707 ssl->handshake->psk_opaque_is_internal = 0;
1708 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001709 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001710 }
Neil Armstronge952a302022-05-03 10:22:14 +02001711#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001712 if( ssl->handshake->psk != NULL )
1713 {
1714 mbedtls_platform_zeroize( ssl->handshake->psk,
1715 ssl->handshake->psk_len );
1716 mbedtls_free( ssl->handshake->psk );
1717 ssl->handshake->psk_len = 0;
1718 }
Neil Armstronge952a302022-05-03 10:22:14 +02001719#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001720}
1721
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001722int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1723 const unsigned char *psk, size_t psk_len )
1724{
Neil Armstrong501c9322022-05-03 09:35:09 +02001725#if defined(MBEDTLS_USE_PSA_CRYPTO)
1726 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001727 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001728 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001729 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001730#endif /* MBEDTLS_USE_PSA_CRYPTO */
1731
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001732 if( psk == NULL || ssl->handshake == NULL )
1733 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1734
1735 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1736 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1737
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001738 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001739
Neil Armstrong501c9322022-05-03 09:35:09 +02001740#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001741#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1742 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1743 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001744 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001745 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1746 else
1747 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1748 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1749 }
1750#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001751
Jerry Yu568ec252022-07-22 21:27:34 +08001752#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001753 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1754 {
1755 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1756 psa_set_key_usage_flags( &key_attributes,
1757 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1758 }
1759#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1760
Neil Armstrong501c9322022-05-03 09:35:09 +02001761 psa_set_key_algorithm( &key_attributes, alg );
1762 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1763
1764 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1765 if( status != PSA_SUCCESS )
1766 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1767
1768 /* Allow calling psa_destroy_key() on psk remove */
1769 ssl->handshake->psk_opaque_is_internal = 1;
1770 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1771#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001772 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001773 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001774
1775 ssl->handshake->psk_len = psk_len;
1776 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1777
1778 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001779#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001780}
1781
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001782#if defined(MBEDTLS_USE_PSA_CRYPTO)
1783int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001784 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001785 const unsigned char *psk_identity,
1786 size_t psk_identity_len )
1787{
Janos Follath865b3eb2019-12-16 11:46:15 +00001788 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001789
1790 /* We currently only support one PSK, raw or opaque. */
1791 if( ssl_conf_psk_is_configured( conf ) )
1792 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001793
Hanno Becker7390c712018-11-15 13:33:04 +00001794 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001795 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001796 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001797 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001798
1799 /* Check and set PSK Identity */
1800 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1801 psk_identity_len );
1802 if( ret != 0 )
1803 ssl_conf_remove_psk( conf );
1804
1805 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001806}
1807
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001808int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1809 mbedtls_svc_key_id_t psk )
1810{
1811 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1812 ( ssl->handshake == NULL ) )
1813 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1814
1815 ssl_remove_psk( ssl );
1816 ssl->handshake->psk_opaque = psk;
1817 return( 0 );
1818}
1819#endif /* MBEDTLS_USE_PSA_CRYPTO */
1820
Jerry Yu8897c072022-08-12 13:56:53 +08001821#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001822void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1823 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1824 size_t),
1825 void *p_psk )
1826{
1827 conf->f_psk = f_psk;
1828 conf->p_psk = p_psk;
1829}
Jerry Yu8897c072022-08-12 13:56:53 +08001830#endif /* MBEDTLS_SSL_SRV_C */
1831
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001832#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1833
1834#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001835static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1836 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001837{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001838#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001839 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001840 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001841#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001842 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001843 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001844 return( MBEDTLS_SSL_MODE_STREAM );
1845}
1846
1847#else /* MBEDTLS_USE_PSA_CRYPTO */
1848
1849static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1850 mbedtls_cipher_mode_t mode )
1851{
1852#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1853 if( mode == MBEDTLS_MODE_CBC )
1854 return( MBEDTLS_SSL_MODE_CBC );
1855#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1856
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001857#if defined(MBEDTLS_GCM_C) || \
1858 defined(MBEDTLS_CCM_C) || \
1859 defined(MBEDTLS_CHACHAPOLY_C)
1860 if( mode == MBEDTLS_MODE_GCM ||
1861 mode == MBEDTLS_MODE_CCM ||
1862 mode == MBEDTLS_MODE_CHACHAPOLY )
1863 return( MBEDTLS_SSL_MODE_AEAD );
1864#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001865
1866 return( MBEDTLS_SSL_MODE_STREAM );
1867}
Gilles Peskine301711e2022-04-26 16:57:05 +02001868#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001869
Gilles Peskinee108d982022-04-26 16:50:40 +02001870static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1871 mbedtls_ssl_mode_t base_mode,
1872 int encrypt_then_mac )
1873{
1874#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1875 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1876 base_mode == MBEDTLS_SSL_MODE_CBC )
1877 {
1878 return( MBEDTLS_SSL_MODE_CBC_ETM );
1879 }
1880#else
1881 (void) encrypt_then_mac;
1882#endif
1883 return( base_mode );
1884}
1885
Neil Armstrongab555e02022-04-04 11:07:59 +02001886mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001887 const mbedtls_ssl_transform *transform )
1888{
Gilles Peskinee108d982022-04-26 16:50:40 +02001889 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001890#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001891 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001892#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001893 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1894#endif
1895 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001896
Gilles Peskinee108d982022-04-26 16:50:40 +02001897 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001898#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001899 encrypt_then_mac = transform->encrypt_then_mac;
1900#endif
1901 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001902}
1903
Neil Armstrongab555e02022-04-04 11:07:59 +02001904mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001905#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001906 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001907#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001908 const mbedtls_ssl_ciphersuite_t *suite )
1909{
Gilles Peskinee108d982022-04-26 16:50:40 +02001910 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1911
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001912#if defined(MBEDTLS_USE_PSA_CRYPTO)
1913 psa_status_t status;
1914 psa_algorithm_t alg;
1915 psa_key_type_t type;
1916 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001917 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1918 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001919 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001920#else
1921 const mbedtls_cipher_info_t *cipher =
1922 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001923 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001924 {
1925 base_mode =
1926 mbedtls_ssl_get_base_mode(
1927 mbedtls_cipher_info_get_mode( cipher ) );
1928 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001929#endif /* MBEDTLS_USE_PSA_CRYPTO */
1930
Gilles Peskinee108d982022-04-26 16:50:40 +02001931#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1932 int encrypt_then_mac = 0;
1933#endif
1934 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001935}
1936
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001937#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001938
1939#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001940/* Serialization of TLS 1.3 sessions:
1941 *
1942 * struct {
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +00001943 * opaque hostname<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001944 * uint64 ticket_received;
1945 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08001946 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001947 * } ClientOnlyData;
1948 *
1949 * struct {
1950 * uint8 endpoint;
1951 * uint8 ciphersuite[2];
1952 * uint32 ticket_age_add;
1953 * uint8 ticket_flags;
1954 * opaque resumption_key<0..255>;
1955 * select ( endpoint ) {
1956 * case client: ClientOnlyData;
1957 * case server: uint64 start_time;
1958 * };
1959 * } serialized_session_tls13;
1960 *
1961 */
1962#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08001963MBEDTLS_CHECK_RETURN_CRITICAL
1964static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
1965 unsigned char *buf,
1966 size_t buf_len,
1967 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00001968{
1969 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00001970#if defined(MBEDTLS_SSL_CLI_C) && \
1971 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1972 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00001973 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00001974#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08001975 size_t needed = 1 /* endpoint */
1976 + 2 /* ciphersuite */
1977 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08001978 + 1 /* ticket_flags */
1979 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08001980 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08001981
1982 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
1983 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1984 needed += session->resumption_key_len; /* resumption_key */
1985
Jerry Yu438ddd82022-07-07 06:55:50 +00001986#if defined(MBEDTLS_HAVE_TIME)
1987 needed += 8; /* start_time or ticket_received */
1988#endif
1989
1990#if defined(MBEDTLS_SSL_CLI_C)
1991 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1992 {
Xiaokang Qian03409292022-10-12 02:49:52 +00001993#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
1994 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00001995 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00001996 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00001997#endif
1998
Jerry Yu438ddd82022-07-07 06:55:50 +00001999 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002000 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002001
2002 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002003 if( session->ticket_len > SIZE_MAX - needed )
2004 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002005
Jerry Yue28d9742022-08-18 15:44:03 +08002006 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002007 }
2008#endif /* MBEDTLS_SSL_CLI_C */
2009
Jerry Yue36fdd62022-08-17 21:31:36 +08002010 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002011 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002012 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002013
2014 p[0] = session->endpoint;
2015 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2016 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2017 p[7] = session->ticket_flags;
2018
2019 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002020 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002021 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002022 memcpy( p, session->resumption_key, session->resumption_key_len );
2023 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002024
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002025
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002026
Jerry Yu438ddd82022-07-07 06:55:50 +00002027#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2028 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2029 {
2030 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2031 p += 8;
2032 }
2033#endif /* MBEDTLS_HAVE_TIME */
2034
2035#if defined(MBEDTLS_SSL_CLI_C)
2036 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2037 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002038#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2039 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2040 p += 2;
2041 if ( hostname_len > 0 )
2042 {
2043 /* save host name */
2044 memcpy( p, session->hostname, hostname_len );
2045 p += hostname_len;
2046 }
2047#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2048
Jerry Yu438ddd82022-07-07 06:55:50 +00002049#if defined(MBEDTLS_HAVE_TIME)
2050 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2051 p += 8;
2052#endif
2053 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2054 p += 4;
2055
2056 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2057 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002058
2059 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002060 {
2061 memcpy( p, session->ticket, session->ticket_len );
2062 p += session->ticket_len;
2063 }
2064 }
2065#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002066 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002067}
2068
2069MBEDTLS_CHECK_RETURN_CRITICAL
2070static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2071 const unsigned char *buf,
2072 size_t len )
2073{
2074 const unsigned char *p = buf;
2075 const unsigned char *end = buf + len;
2076
2077 if( end - p < 9 )
2078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2079 session->endpoint = p[0];
2080 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2081 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2082 session->ticket_flags = p[7];
2083
2084 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002085 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002086 p += 9;
2087
Jerry Yubc7c1a42022-07-21 22:57:37 +08002088 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002089 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2090
Jerry Yubc7c1a42022-07-21 22:57:37 +08002091 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002092 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002093 memcpy( session->resumption_key, p, session->resumption_key_len );
2094 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002095
2096#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2097 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2098 {
2099 if( end - p < 8 )
2100 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2101 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2102 p += 8;
2103 }
2104#endif /* MBEDTLS_HAVE_TIME */
2105
2106#if defined(MBEDTLS_SSL_CLI_C)
2107 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2108 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002109#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2110 defined(MBEDTLS_SSL_SESSION_TICKETS)
2111 size_t hostname_len;
2112 /* load host name */
2113 if( end - p < 2 )
2114 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2115 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2116 p += 2;
2117
2118 if( end - p < ( long int )hostname_len )
2119 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2120 if( hostname_len > 0 )
2121 {
2122 session->hostname = mbedtls_calloc( 1, hostname_len );
2123 if( session->hostname == NULL )
2124 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2125 memcpy( session->hostname, p, hostname_len );
2126 p += hostname_len;
2127 }
2128#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2129 MBEDTLS_SSL_SESSION_TICKETS */
2130
Jerry Yu438ddd82022-07-07 06:55:50 +00002131#if defined(MBEDTLS_HAVE_TIME)
2132 if( end - p < 8 )
2133 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2134 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2135 p += 8;
2136#endif
2137 if( end - p < 4 )
2138 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2139 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2140 p += 4;
2141
2142 if( end - p < 2 )
2143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2144 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2145 p += 2;
2146
2147 if( end - p < ( long int )session->ticket_len )
2148 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2149 if( session->ticket_len > 0 )
2150 {
2151 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2152 if( session->ticket == NULL )
2153 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2154 memcpy( session->ticket, p, session->ticket_len );
2155 p += session->ticket_len;
2156 }
2157 }
2158#endif /* MBEDTLS_SSL_CLI_C */
2159
2160 return( 0 );
2161
2162}
2163#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002164MBEDTLS_CHECK_RETURN_CRITICAL
2165static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2166 unsigned char *buf,
2167 size_t buf_len,
2168 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002169{
2170 ((void) session);
2171 ((void) buf);
2172 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002173 *olen = 0;
2174 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002175}
Jerry Yu438ddd82022-07-07 06:55:50 +00002176
2177static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2178 unsigned char *buf,
2179 size_t buf_len )
2180{
2181 ((void) session);
2182 ((void) buf);
2183 ((void) buf_len);
2184 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2185}
2186#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002187#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2188
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002189psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002190 size_t taglen,
2191 psa_algorithm_t *alg,
2192 psa_key_type_t *key_type,
2193 size_t *key_size )
2194{
2195 switch ( mbedtls_cipher_type )
2196 {
2197 case MBEDTLS_CIPHER_AES_128_CBC:
2198 *alg = PSA_ALG_CBC_NO_PADDING;
2199 *key_type = PSA_KEY_TYPE_AES;
2200 *key_size = 128;
2201 break;
2202 case MBEDTLS_CIPHER_AES_128_CCM:
2203 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2204 *key_type = PSA_KEY_TYPE_AES;
2205 *key_size = 128;
2206 break;
2207 case MBEDTLS_CIPHER_AES_128_GCM:
2208 *alg = PSA_ALG_GCM;
2209 *key_type = PSA_KEY_TYPE_AES;
2210 *key_size = 128;
2211 break;
2212 case MBEDTLS_CIPHER_AES_192_CCM:
2213 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2214 *key_type = PSA_KEY_TYPE_AES;
2215 *key_size = 192;
2216 break;
2217 case MBEDTLS_CIPHER_AES_192_GCM:
2218 *alg = PSA_ALG_GCM;
2219 *key_type = PSA_KEY_TYPE_AES;
2220 *key_size = 192;
2221 break;
2222 case MBEDTLS_CIPHER_AES_256_CBC:
2223 *alg = PSA_ALG_CBC_NO_PADDING;
2224 *key_type = PSA_KEY_TYPE_AES;
2225 *key_size = 256;
2226 break;
2227 case MBEDTLS_CIPHER_AES_256_CCM:
2228 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2229 *key_type = PSA_KEY_TYPE_AES;
2230 *key_size = 256;
2231 break;
2232 case MBEDTLS_CIPHER_AES_256_GCM:
2233 *alg = PSA_ALG_GCM;
2234 *key_type = PSA_KEY_TYPE_AES;
2235 *key_size = 256;
2236 break;
2237 case MBEDTLS_CIPHER_ARIA_128_CBC:
2238 *alg = PSA_ALG_CBC_NO_PADDING;
2239 *key_type = PSA_KEY_TYPE_ARIA;
2240 *key_size = 128;
2241 break;
2242 case MBEDTLS_CIPHER_ARIA_128_CCM:
2243 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2244 *key_type = PSA_KEY_TYPE_ARIA;
2245 *key_size = 128;
2246 break;
2247 case MBEDTLS_CIPHER_ARIA_128_GCM:
2248 *alg = PSA_ALG_GCM;
2249 *key_type = PSA_KEY_TYPE_ARIA;
2250 *key_size = 128;
2251 break;
2252 case MBEDTLS_CIPHER_ARIA_192_CCM:
2253 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2254 *key_type = PSA_KEY_TYPE_ARIA;
2255 *key_size = 192;
2256 break;
2257 case MBEDTLS_CIPHER_ARIA_192_GCM:
2258 *alg = PSA_ALG_GCM;
2259 *key_type = PSA_KEY_TYPE_ARIA;
2260 *key_size = 192;
2261 break;
2262 case MBEDTLS_CIPHER_ARIA_256_CBC:
2263 *alg = PSA_ALG_CBC_NO_PADDING;
2264 *key_type = PSA_KEY_TYPE_ARIA;
2265 *key_size = 256;
2266 break;
2267 case MBEDTLS_CIPHER_ARIA_256_CCM:
2268 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2269 *key_type = PSA_KEY_TYPE_ARIA;
2270 *key_size = 256;
2271 break;
2272 case MBEDTLS_CIPHER_ARIA_256_GCM:
2273 *alg = PSA_ALG_GCM;
2274 *key_type = PSA_KEY_TYPE_ARIA;
2275 *key_size = 256;
2276 break;
2277 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2278 *alg = PSA_ALG_CBC_NO_PADDING;
2279 *key_type = PSA_KEY_TYPE_CAMELLIA;
2280 *key_size = 128;
2281 break;
2282 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2283 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2284 *key_type = PSA_KEY_TYPE_CAMELLIA;
2285 *key_size = 128;
2286 break;
2287 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2288 *alg = PSA_ALG_GCM;
2289 *key_type = PSA_KEY_TYPE_CAMELLIA;
2290 *key_size = 128;
2291 break;
2292 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2293 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2294 *key_type = PSA_KEY_TYPE_CAMELLIA;
2295 *key_size = 192;
2296 break;
2297 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2298 *alg = PSA_ALG_GCM;
2299 *key_type = PSA_KEY_TYPE_CAMELLIA;
2300 *key_size = 192;
2301 break;
2302 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2303 *alg = PSA_ALG_CBC_NO_PADDING;
2304 *key_type = PSA_KEY_TYPE_CAMELLIA;
2305 *key_size = 256;
2306 break;
2307 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2308 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2309 *key_type = PSA_KEY_TYPE_CAMELLIA;
2310 *key_size = 256;
2311 break;
2312 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2313 *alg = PSA_ALG_GCM;
2314 *key_type = PSA_KEY_TYPE_CAMELLIA;
2315 *key_size = 256;
2316 break;
2317 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2318 *alg = PSA_ALG_CHACHA20_POLY1305;
2319 *key_type = PSA_KEY_TYPE_CHACHA20;
2320 *key_size = 256;
2321 break;
2322 case MBEDTLS_CIPHER_NULL:
2323 *alg = MBEDTLS_SSL_NULL_CIPHER;
2324 *key_type = 0;
2325 *key_size = 0;
2326 break;
2327 default:
2328 return PSA_ERROR_NOT_SUPPORTED;
2329 }
2330
2331 return PSA_SUCCESS;
2332}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002333#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002334
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002335#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002336int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2337 const unsigned char *dhm_P, size_t P_len,
2338 const unsigned char *dhm_G, size_t G_len )
2339{
Janos Follath865b3eb2019-12-16 11:46:15 +00002340 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002341
Glenn Strausscee11292021-12-20 01:43:17 -05002342 mbedtls_mpi_free( &conf->dhm_P );
2343 mbedtls_mpi_free( &conf->dhm_G );
2344
Hanno Beckera90658f2017-10-04 15:29:08 +01002345 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2346 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2347 {
2348 mbedtls_mpi_free( &conf->dhm_P );
2349 mbedtls_mpi_free( &conf->dhm_G );
2350 return( ret );
2351 }
2352
2353 return( 0 );
2354}
Paul Bakker5121ce52009-01-03 21:22:43 +00002355
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002356int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002357{
Janos Follath865b3eb2019-12-16 11:46:15 +00002358 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002359
Glenn Strausscee11292021-12-20 01:43:17 -05002360 mbedtls_mpi_free( &conf->dhm_P );
2361 mbedtls_mpi_free( &conf->dhm_G );
2362
Gilles Peskinee5702482021-06-11 21:59:08 +02002363 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2364 &conf->dhm_P ) ) != 0 ||
2365 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2366 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002367 {
2368 mbedtls_mpi_free( &conf->dhm_P );
2369 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002370 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002371 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002372
2373 return( 0 );
2374}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002375#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002376
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002377#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2378/*
2379 * Set the minimum length for Diffie-Hellman parameters
2380 */
2381void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2382 unsigned int bitlen )
2383{
2384 conf->dhm_min_bitlen = bitlen;
2385}
2386#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2387
Gilles Peskineeccd8882020-03-10 12:19:08 +01002388#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002389#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002390/*
2391 * Set allowed/preferred hashes for handshake signatures
2392 */
2393void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2394 const int *hashes )
2395{
2396 conf->sig_hashes = hashes;
2397}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002398#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002399
Jerry Yuf017ee42022-01-12 15:49:48 +08002400/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002401void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2402 const uint16_t* sig_algs )
2403{
Jerry Yuf017ee42022-01-12 15:49:48 +08002404#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2405 conf->sig_hashes = NULL;
2406#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2407 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002408}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002409#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002410
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002411#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002412#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002413/*
2414 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002415 *
2416 * mbedtls_ssl_setup() takes the provided list
2417 * and translates it to a list of IANA TLS group identifiers,
2418 * stored in ssl->handshake->group_list.
2419 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002420 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002421void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002422 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002423{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002424 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002425 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002426}
Brett Warrene0edc842021-08-17 09:53:13 +01002427#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002428#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002429
Brett Warrene0edc842021-08-17 09:53:13 +01002430/*
2431 * Set the allowed groups
2432 */
2433void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2434 const uint16_t *group_list )
2435{
2436#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2437 conf->curve_list = NULL;
2438#endif
2439 conf->group_list = group_list;
2440}
2441
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002442#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002444{
Hanno Becker947194e2017-04-07 13:25:49 +01002445 /* Initialize to suppress unnecessary compiler warning */
2446 size_t hostname_len = 0;
2447
2448 /* Check if new hostname is valid before
2449 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002450 if( hostname != NULL )
2451 {
2452 hostname_len = strlen( hostname );
2453
2454 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2455 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2456 }
2457
2458 /* Now it's clear that we will overwrite the old hostname,
2459 * so we can free it safely */
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002460
Hanno Becker947194e2017-04-07 13:25:49 +01002461 if( ssl->hostname != NULL )
2462 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002463 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002464 mbedtls_free( ssl->hostname );
2465 }
2466
2467 /* Passing NULL as hostname shall clear the old one */
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002468
Paul Bakker5121ce52009-01-03 21:22:43 +00002469 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002470 {
2471 ssl->hostname = NULL;
2472 }
2473 else
2474 {
2475 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002476 if( ssl->hostname == NULL )
2477 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002478
Hanno Becker947194e2017-04-07 13:25:49 +01002479 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002480
Hanno Becker947194e2017-04-07 13:25:49 +01002481 ssl->hostname[hostname_len] = '\0';
2482 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002483
2484 return( 0 );
2485}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002486#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002487
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002488#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002489void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002491 const unsigned char *, size_t),
2492 void *p_sni )
2493{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002494 conf->f_sni = f_sni;
2495 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002496}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002500int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002501{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002502 size_t cur_len, tot_len;
2503 const char **p;
2504
2505 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002506 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2507 * MUST NOT be truncated."
2508 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002509 */
2510 tot_len = 0;
2511 for( p = protos; *p != NULL; p++ )
2512 {
2513 cur_len = strlen( *p );
2514 tot_len += cur_len;
2515
Ronald Cron8216dd32020-04-23 16:41:44 +02002516 if( ( cur_len == 0 ) ||
2517 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2518 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002520 }
2521
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002522 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002523
2524 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002525}
2526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002528{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002529 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002530}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002532
Johan Pascalb62bb512015-12-03 21:56:45 +01002533#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002534void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2535 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002536{
2537 conf->dtls_srtp_mki_support = support_mki_value;
2538}
2539
Ron Eldoref72faf2018-07-12 11:54:20 +03002540int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2541 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002542 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002543{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002544 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002545 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002546 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002547 }
Ron Eldor591f1622018-01-22 12:30:04 +02002548
2549 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002550 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002551 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002552 }
Ron Eldor591f1622018-01-22 12:30:04 +02002553
2554 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2555 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002556 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002557}
2558
Ron Eldoref72faf2018-07-12 11:54:20 +03002559int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002560 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002561{
Johan Pascal253d0262020-09-22 13:04:45 +02002562 const mbedtls_ssl_srtp_profile *p;
2563 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002564
Johan Pascal253d0262020-09-22 13:04:45 +02002565 /* check the profiles list: all entry must be valid,
2566 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002567 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2568 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2569 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002570 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002571 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002572 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002573 list_size++;
2574 }
2575 else
2576 {
2577 /* unsupported value, stop parsing and set the size to an error value */
2578 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002579 }
2580 }
2581
Johan Pascal5ef72d22020-10-28 17:05:47 +01002582 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002583 {
Johan Pascal253d0262020-09-22 13:04:45 +02002584 conf->dtls_srtp_profile_list = NULL;
2585 conf->dtls_srtp_profile_list_len = 0;
2586 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2587 }
2588
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002589 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002590 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002591
2592 return( 0 );
2593}
2594
Johan Pascal5ef72d22020-10-28 17:05:47 +01002595void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2596 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002597{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002598 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2599 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002600 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002601 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002602 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002603 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002604 else
2605 {
2606 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002607 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2608 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002609 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002610}
Johan Pascalb62bb512015-12-03 21:56:45 +01002611#endif /* MBEDTLS_SSL_DTLS_SRTP */
2612
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302613#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002614void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002615{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002616 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002617}
2618
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002619void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002620{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002621 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002622}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302623#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002624
Janos Follath088ce432017-04-10 12:42:31 +01002625#if defined(MBEDTLS_SSL_SRV_C)
2626void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2627 char cert_req_ca_list )
2628{
2629 conf->cert_req_ca_list = cert_req_ca_list;
2630}
2631#endif
2632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002634void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002635{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002636 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002637}
2638#endif
2639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002641void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002642{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002643 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002644}
2645#endif
2646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002648int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002649{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002651 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002654 }
2655
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002656 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002657
2658 return( 0 );
2659}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002661
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002662void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002663{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002664 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002665}
2666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002668void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002669{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002670 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002671}
2672
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002673void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002674{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002675 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002676}
2677
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002678void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002679 const unsigned char period[8] )
2680{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002681 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002682}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002686#if defined(MBEDTLS_SSL_CLI_C)
2687void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002688{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002689 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002690}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002691#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002692
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002693#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002694
Jerry Yud0766ec2022-09-22 10:46:57 +08002695#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002696void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2697 uint16_t num_tickets )
2698{
Jerry Yud0766ec2022-09-22 10:46:57 +08002699 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002700}
2701#endif
2702
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002703void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2704 mbedtls_ssl_ticket_write_t *f_ticket_write,
2705 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2706 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002707{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002708 conf->f_ticket_write = f_ticket_write;
2709 conf->f_ticket_parse = f_ticket_parse;
2710 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002711}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002712#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002714
Hanno Becker7e6c1782021-06-08 09:24:55 +01002715void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2716 mbedtls_ssl_export_keys_t *f_export_keys,
2717 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002718{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002719 ssl->f_export_keys = f_export_keys;
2720 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002721}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002722
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002723#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002724void mbedtls_ssl_conf_async_private_cb(
2725 mbedtls_ssl_config *conf,
2726 mbedtls_ssl_async_sign_t *f_async_sign,
2727 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2728 mbedtls_ssl_async_resume_t *f_async_resume,
2729 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002730 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002731{
2732 conf->f_async_sign_start = f_async_sign;
2733 conf->f_async_decrypt_start = f_async_decrypt;
2734 conf->f_async_resume = f_async_resume;
2735 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002736 conf->p_async_config_data = async_config_data;
2737}
2738
Gilles Peskine8f97af72018-04-26 11:46:10 +02002739void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2740{
2741 return( conf->p_async_config_data );
2742}
2743
Gilles Peskine1febfef2018-04-30 11:54:39 +02002744void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002745{
2746 if( ssl->handshake == NULL )
2747 return( NULL );
2748 else
2749 return( ssl->handshake->user_async_ctx );
2750}
2751
Gilles Peskine1febfef2018-04-30 11:54:39 +02002752void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002753 void *ctx )
2754{
2755 if( ssl->handshake != NULL )
2756 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002757}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002758#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002759
Paul Bakker5121ce52009-01-03 21:22:43 +00002760/*
2761 * SSL get accessors
2762 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002763uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002764{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002765 if( ssl->session != NULL )
2766 return( ssl->session->verify_result );
2767
2768 if( ssl->session_negotiate != NULL )
2769 return( ssl->session_negotiate->verify_result );
2770
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002771 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002772}
2773
Glenn Strauss8f526902022-01-13 00:04:49 -05002774int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2775{
2776 if( ssl == NULL || ssl->session == NULL )
2777 return( 0 );
2778
2779 return( ssl->session->ciphersuite );
2780}
2781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002783{
Paul Bakker926c8e42013-03-06 10:23:34 +01002784 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002785 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002788}
2789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002791{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002793 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002794 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002795 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002796 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002797 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002798 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002799 default:
2800 return( "unknown (DTLS)" );
2801 }
2802 }
2803#endif
2804
Glenn Strauss60bfe602022-03-14 19:04:24 -04002805 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002806 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002807 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002808 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002809 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002810 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002811 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002812 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002813 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002814}
2815
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002816#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002817size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2818{
David Horstmann95d516f2021-05-04 18:36:56 +01002819 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002820 size_t read_mfl;
2821
2822 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2823 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2824 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2825 {
2826 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2827 }
2828
2829 /* Check if a smaller max length was negotiated */
2830 if( ssl->session_out != NULL )
2831 {
2832 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2833 if( read_mfl < max_len )
2834 {
2835 max_len = read_mfl;
2836 }
2837 }
2838
2839 // During a handshake, use the value being negotiated
2840 if( ssl->session_negotiate != NULL )
2841 {
2842 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2843 if( read_mfl < max_len )
2844 {
2845 max_len = read_mfl;
2846 }
2847 }
2848
2849 return( max_len );
2850}
2851
2852size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002853{
2854 size_t max_len;
2855
2856 /*
2857 * Assume mfl_code is correct since it was checked when set
2858 */
Angus Grattond8213d02016-05-25 20:56:48 +10002859 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002860
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002861 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002862 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002863 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002864 {
Angus Grattond8213d02016-05-25 20:56:48 +10002865 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002866 }
2867
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002868 /* During a handshake, use the value being negotiated */
2869 if( ssl->session_negotiate != NULL &&
2870 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2871 {
2872 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2873 }
2874
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002875 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002876}
2877#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2878
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002879#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002880size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002881{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002882 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2883 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2884 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2885 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2886 return ( 0 );
2887
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002888 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2889 return( ssl->mtu );
2890
2891 if( ssl->mtu == 0 )
2892 return( ssl->handshake->mtu );
2893
2894 return( ssl->mtu < ssl->handshake->mtu ?
2895 ssl->mtu : ssl->handshake->mtu );
2896}
2897#endif /* MBEDTLS_SSL_PROTO_DTLS */
2898
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002899int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2900{
2901 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2902
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002903#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2904 !defined(MBEDTLS_SSL_PROTO_DTLS)
2905 (void) ssl;
2906#endif
2907
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002908#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002909 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002910
2911 if( max_len > mfl )
2912 max_len = mfl;
2913#endif
2914
2915#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002916 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002917 {
Hanno Becker89490712020-02-05 10:50:12 +00002918 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002919 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2920 const size_t overhead = (size_t) ret;
2921
2922 if( ret < 0 )
2923 return( ret );
2924
2925 if( mtu <= overhead )
2926 {
2927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2928 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2929 }
2930
2931 if( max_len > mtu - overhead )
2932 max_len = mtu - overhead;
2933 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002934#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002935
Hanno Becker0defedb2018-08-10 12:35:02 +01002936#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2937 !defined(MBEDTLS_SSL_PROTO_DTLS)
2938 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002939#endif
2940
2941 return( (int) max_len );
2942}
2943
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002944int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2945{
2946 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2947
2948#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2949 (void) ssl;
2950#endif
2951
2952#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2953 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2954
2955 if( max_len > mfl )
2956 max_len = mfl;
2957#endif
2958
2959 return( (int) max_len );
2960}
2961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962#if defined(MBEDTLS_X509_CRT_PARSE_C)
2963const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002964{
2965 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002966 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002967
Hanno Beckere6824572019-02-07 13:18:46 +00002968#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002969 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002970#else
2971 return( NULL );
2972#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002973}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002977int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2978 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002979{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002980 int ret;
2981
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002982 if( ssl == NULL ||
2983 dst == NULL ||
2984 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002985 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002988 }
2989
Hanno Beckere810bbc2021-05-14 16:01:05 +01002990 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2991 * idempotent: Each session can only be exported once.
2992 *
2993 * (This is in preparation for TLS 1.3 support where we will
2994 * need the ability to export multiple sessions (aka tickets),
2995 * which will be achieved by calling mbedtls_ssl_get_session()
2996 * multiple times until it fails.)
2997 *
2998 * Check whether we have already exported the current session,
2999 * and fail if so.
3000 */
3001 if( ssl->session->exported == 1 )
3002 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3003
3004 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3005 if( ret != 0 )
3006 return( ret );
3007
3008 /* Remember that we've exported the session. */
3009 ssl->session->exported = 1;
3010 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003011}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003013
Paul Bakker5121ce52009-01-03 21:22:43 +00003014/*
Hanno Beckera835da52019-05-16 12:39:07 +01003015 * Define ticket header determining Mbed TLS version
3016 * and structure of the ticket.
3017 */
3018
Hanno Becker94ef3b32019-05-16 12:50:45 +01003019/*
Hanno Becker50b59662019-05-28 14:30:45 +01003020 * Define bitflag determining compile-time settings influencing
3021 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003022 */
3023
Hanno Becker50b59662019-05-28 14:30:45 +01003024#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003025#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003026#else
Hanno Becker3e088662019-05-29 11:10:18 +01003027#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003028#endif /* MBEDTLS_HAVE_TIME */
3029
3030#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003031#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003032#else
Hanno Becker3e088662019-05-29 11:10:18 +01003033#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003034#endif /* MBEDTLS_X509_CRT_PARSE_C */
3035
3036#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003037#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003038#else
Hanno Becker3e088662019-05-29 11:10:18 +01003039#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003040#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3041
3042#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003043#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003044#else
Hanno Becker3e088662019-05-29 11:10:18 +01003045#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003046#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3047
Hanno Becker94ef3b32019-05-16 12:50:45 +01003048#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003049#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003050#else
Hanno Becker3e088662019-05-29 11:10:18 +01003051#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003052#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3053
Hanno Becker94ef3b32019-05-16 12:50:45 +01003054#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3055#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3056#else
3057#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3058#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3059
Hanno Becker3e088662019-05-29 11:10:18 +01003060#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3061#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3062#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3063#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003064#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3065#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003066
Hanno Becker50b59662019-05-28 14:30:45 +01003067#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003068 ( (uint16_t) ( \
3069 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3070 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3071 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3072 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003073 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003074 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003075
Hanno Beckerf8787072019-05-16 12:41:07 +01003076static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003077 MBEDTLS_VERSION_MAJOR,
3078 MBEDTLS_VERSION_MINOR,
3079 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003080 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3081 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003082};
Hanno Beckera835da52019-05-16 12:39:07 +01003083
3084/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003085 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003086 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003087 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003088 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003089 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003090 * opaque mbedtls_version[3]; // library version: major, minor, patch
3091 * opaque session_format[2]; // library-version specific 16-bit field
3092 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003093 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003094 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003095 * Note: When updating the format, remember to keep
3096 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003097 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003098 * // In this version, `session_format` determines
3099 * // the setting of those compile-time
3100 * // configuration options which influence
3101 * // the structure of mbedtls_ssl_session.
3102 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003103 * uint8_t minor_ver; // Protocol minor version. Possible values:
3104 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003105 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003106 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003107 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003108 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003109 * serialized_session_tls12 data;
3110 *
3111 * };
3112 *
3113 * } serialized_session;
3114 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003115 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003116
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003117MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003118static int ssl_session_save( const mbedtls_ssl_session *session,
3119 unsigned char omit_header,
3120 unsigned char *buf,
3121 size_t buf_len,
3122 size_t *olen )
3123{
3124 unsigned char *p = buf;
3125 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003126 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003127#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3128 size_t out_len;
3129 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3130#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003131 if( session == NULL )
3132 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3133
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003134 if( !omit_header )
3135 {
3136 /*
3137 * Add Mbed TLS version identifier
3138 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003139 used += sizeof( ssl_serialized_session_header );
3140
3141 if( used <= buf_len )
3142 {
3143 memcpy( p, ssl_serialized_session_header,
3144 sizeof( ssl_serialized_session_header ) );
3145 p += sizeof( ssl_serialized_session_header );
3146 }
3147 }
3148
3149 /*
3150 * TLS version identifier
3151 */
3152 used += 1;
3153 if( used <= buf_len )
3154 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003155 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003156 }
3157
3158 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003159 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003160 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003161 {
3162#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003163 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003164 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003165 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003166#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3167
Jerry Yu251a12e2022-07-13 15:15:48 +08003168#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3169 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003170 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3171 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003172 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003173 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003174 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003175#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3176
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003177 default:
3178 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3179 }
3180
3181 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003182 if( used > buf_len )
3183 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003184
3185 return( 0 );
3186}
3187
3188/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003189 * Public wrapper for ssl_session_save()
3190 */
3191int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3192 unsigned char *buf,
3193 size_t buf_len,
3194 size_t *olen )
3195{
3196 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3197}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003198
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003199/*
3200 * Deserialize session, see mbedtls_ssl_session_save() for format.
3201 *
3202 * This internal version is wrapped by a public function that cleans up in
3203 * case of error, and has an extra option omit_header.
3204 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003205MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003206static int ssl_session_load( mbedtls_ssl_session *session,
3207 unsigned char omit_header,
3208 const unsigned char *buf,
3209 size_t len )
3210{
3211 const unsigned char *p = buf;
3212 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003213 size_t remaining_len;
3214
3215
3216 if( session == NULL )
3217 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003218
3219 if( !omit_header )
3220 {
3221 /*
3222 * Check Mbed TLS version identifier
3223 */
3224
3225 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3226 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3227
3228 if( memcmp( p, ssl_serialized_session_header,
3229 sizeof( ssl_serialized_session_header ) ) != 0 )
3230 {
3231 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3232 }
3233 p += sizeof( ssl_serialized_session_header );
3234 }
3235
3236 /*
3237 * TLS version identifier
3238 */
3239 if( 1 > (size_t)( end - p ) )
3240 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003241 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003242
3243 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003244 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003245 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003246 {
3247#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003248 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003249 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003250#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3251
Jerry Yu438ddd82022-07-07 06:55:50 +00003252#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3253 case MBEDTLS_SSL_VERSION_TLS1_3:
3254 return( ssl_tls13_session_load( session, p, remaining_len ) );
3255#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3256
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003257 default:
3258 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3259 }
3260}
3261
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003262/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003263 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003264 */
3265int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3266 const unsigned char *buf,
3267 size_t len )
3268{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003269 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003270
3271 if( ret != 0 )
3272 mbedtls_ssl_session_free( session );
3273
3274 return( ret );
3275}
3276
3277/*
Paul Bakker1961b702013-01-25 14:49:24 +01003278 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003279 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003280MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003281static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3282{
3283 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3284
Ronald Cron66dbf912022-02-02 15:33:46 +01003285 /*
3286 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003287 * that were written into the output buffer by the previous handshake step,
3288 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003289 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3290 * We proceed to the next handshake step only when all data from the
3291 * previous one have been sent to the peer, thus we make sure that this is
3292 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3293 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3294 * we have to wait before to go ahead.
3295 * In the case of TLS 1.3, handshake step handlers do not send data to the
3296 * peer. Data are only sent here and through
3297 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003298 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003299 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003300 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3301 return( ret );
3302
3303#if defined(MBEDTLS_SSL_PROTO_DTLS)
3304 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3305 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3306 {
3307 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3308 return( ret );
3309 }
3310#endif /* MBEDTLS_SSL_PROTO_DTLS */
3311
3312 return( ret );
3313}
3314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003316{
Hanno Becker41934dd2021-08-07 19:13:43 +01003317 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003318
Hanno Becker41934dd2021-08-07 19:13:43 +01003319 if( ssl == NULL ||
3320 ssl->conf == NULL ||
3321 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003322 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003323 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003324 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003325 }
3326
3327 ret = ssl_prepare_handshake_step( ssl );
3328 if( ret != 0 )
3329 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003330
Jerry Yue7047812021-09-13 19:26:39 +08003331 ret = mbedtls_ssl_handle_pending_alert( ssl );
3332 if( ret != 0 )
3333 goto cleanup;
3334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003336 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003337 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3339 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003340
Ronald Cron9f0fba32022-02-10 16:45:15 +01003341 switch( ssl->state )
3342 {
3343 case MBEDTLS_SSL_HELLO_REQUEST:
3344 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3345 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003346
Ronald Cron9f0fba32022-02-10 16:45:15 +01003347 case MBEDTLS_SSL_CLIENT_HELLO:
3348 ret = mbedtls_ssl_write_client_hello( ssl );
3349 break;
3350
3351 default:
3352#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003353 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003354 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3355 else
3356 ret = mbedtls_ssl_handshake_client_step( ssl );
3357#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3358 ret = mbedtls_ssl_handshake_client_step( ssl );
3359#else
3360 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3361#endif
3362 }
Jerry Yub9930e72021-08-06 17:11:51 +08003363 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003364#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003365#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003366 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003367 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003368#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003369 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003370 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003371#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003372
3373#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3374 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3375 ret = mbedtls_ssl_handshake_server_step( ssl );
3376#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3377 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003378#endif
3379
Jerry Yue7047812021-09-13 19:26:39 +08003380 if( ret != 0 )
3381 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003382 /* handshake_step return error. And it is same
3383 * with alert_reason.
3384 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003385 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003386 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003387 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003388 goto cleanup;
3389 }
3390 }
3391
3392cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003393 return( ret );
3394}
3395
3396/*
3397 * Perform the SSL handshake
3398 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003400{
3401 int ret = 0;
3402
Hanno Beckera817ea42020-10-20 15:20:23 +01003403 /* Sanity checks */
3404
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003405 if( ssl == NULL || ssl->conf == NULL )
3406 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3407
Hanno Beckera817ea42020-10-20 15:20:23 +01003408#if defined(MBEDTLS_SSL_PROTO_DTLS)
3409 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3410 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3411 {
3412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3413 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3414 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3415 }
3416#endif /* MBEDTLS_SSL_PROTO_DTLS */
3417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003419
Hanno Beckera817ea42020-10-20 15:20:23 +01003420 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003421 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003424
3425 if( ret != 0 )
3426 break;
3427 }
3428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003430
3431 return( ret );
3432}
3433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434#if defined(MBEDTLS_SSL_RENEGOTIATION)
3435#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003436/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003437 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003438 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003439MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003441{
Janos Follath865b3eb2019-12-16 11:46:15 +00003442 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003445
3446 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003447 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3448 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003449
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003450 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003451 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003452 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003453 return( ret );
3454 }
3455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003457
3458 return( 0 );
3459}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003461
3462/*
3463 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003464 * - any side: calling mbedtls_ssl_renegotiate(),
3465 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3466 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003467 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003468 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003469 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003470 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003471int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003472{
Janos Follath865b3eb2019-12-16 11:46:15 +00003473 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003475 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003476
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003477 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3478 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003479
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003480 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3481 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003483 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003485 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003486 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003487 ssl->handshake->out_msg_seq = 1;
3488 else
3489 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003490 }
3491#endif
3492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003493 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3494 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003496 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003499 return( ret );
3500 }
3501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003503
3504 return( 0 );
3505}
3506
3507/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003508 * Renegotiate current connection on client,
3509 * or request renegotiation on server
3510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003512{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003514
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003515 if( ssl == NULL || ssl->conf == NULL )
3516 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003519 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003520 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003521 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003522 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003525 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003526
3527 /* Did we already try/start sending HelloRequest? */
3528 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003529 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003530
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003531 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003532 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003535#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003536 /*
3537 * On client, either start the renegotiation process or,
3538 * if already in progress, continue the handshake
3539 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003541 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003542 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003543 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003544
Hanno Becker40cdaa12020-02-05 10:48:27 +00003545 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003546 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003547 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003548 return( ret );
3549 }
3550 }
3551 else
3552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003553 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003556 return( ret );
3557 }
3558 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003559#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003560
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003561 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003562}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003564
Gilles Peskine9b562d52018-04-25 20:32:43 +02003565void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003566{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003567 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3568
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003569 if( handshake == NULL )
3570 return;
3571
Brett Warrene0edc842021-08-17 09:53:13 +01003572#if defined(MBEDTLS_ECP_C)
3573#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3574 if ( ssl->handshake->group_list_heap_allocated )
3575 mbedtls_free( (void*) handshake->group_list );
3576 handshake->group_list = NULL;
3577#endif /* MBEDTLS_DEPRECATED_REMOVED */
3578#endif /* MBEDTLS_ECP_C */
3579
Jerry Yuf017ee42022-01-12 15:49:48 +08003580#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3581#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3582 if ( ssl->handshake->sig_algs_heap_allocated )
3583 mbedtls_free( (void*) handshake->sig_algs );
3584 handshake->sig_algs = NULL;
3585#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003586#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3587 if( ssl->handshake->certificate_request_context )
3588 {
3589 mbedtls_free( (void*) handshake->certificate_request_context );
3590 }
3591#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003592#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3593
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003594#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3595 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3596 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003597 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003598 handshake->async_in_progress = 0;
3599 }
3600#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3601
Andrzej Kurek25f27152022-08-17 16:09:31 -04003602#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003603#if defined(MBEDTLS_USE_PSA_CRYPTO)
3604 psa_hash_abort( &handshake->fin_sha256_psa );
3605#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003606 mbedtls_sha256_free( &handshake->fin_sha256 );
3607#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003608#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003609#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003610#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003611 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003612#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003613 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003614#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003615#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003617#if defined(MBEDTLS_DHM_C)
3618 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003619#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003620#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003622#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003623#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003624 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003625#if defined(MBEDTLS_SSL_CLI_C)
3626 mbedtls_free( handshake->ecjpake_cache );
3627 handshake->ecjpake_cache = NULL;
3628 handshake->ecjpake_cache_len = 0;
3629#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003630#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003631
Janos Follath4ae5c292016-02-10 11:27:43 +00003632#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3633 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003634 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003636#endif
3637
Gilles Peskineeccd8882020-03-10 12:19:08 +01003638#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003639#if defined(MBEDTLS_USE_PSA_CRYPTO)
3640 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3641 {
3642 /* The maintenance of the external PSK key slot is the
3643 * user's responsibility. */
3644 if( ssl->handshake->psk_opaque_is_internal )
3645 {
3646 psa_destroy_key( ssl->handshake->psk_opaque );
3647 ssl->handshake->psk_opaque_is_internal = 0;
3648 }
3649 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3650 }
Neil Armstronge952a302022-05-03 10:22:14 +02003651#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003652 if( handshake->psk != NULL )
3653 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003654 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003655 mbedtls_free( handshake->psk );
3656 }
Neil Armstronge952a302022-05-03 10:22:14 +02003657#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003658#endif
3659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3661 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003662 /*
3663 * Free only the linked list wrapper, not the keys themselves
3664 * since the belong to the SNI callback
3665 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003666 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003668
Gilles Peskineeccd8882020-03-10 12:19:08 +01003669#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003670 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003671 if( handshake->ecrs_peer_cert != NULL )
3672 {
3673 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3674 mbedtls_free( handshake->ecrs_peer_cert );
3675 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003676#endif
3677
Hanno Becker75173122019-02-06 16:18:31 +00003678#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3679 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3680 mbedtls_pk_free( &handshake->peer_pubkey );
3681#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3682
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003683#if defined(MBEDTLS_SSL_CLI_C) && \
3684 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3685 mbedtls_free( handshake->cookie );
3686#endif /* MBEDTLS_SSL_CLI_C &&
3687 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003688
3689#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003690 mbedtls_ssl_flight_free( handshake->flight );
3691 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003692#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003693
Ronald Cronf12b81d2022-03-15 10:42:41 +01003694#if defined(MBEDTLS_ECDH_C) && \
3695 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003696 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003697 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003698#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3699
Ronald Cron6f135e12021-12-08 16:57:54 +01003700#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003701 mbedtls_ssl_transform_free( handshake->transform_handshake );
3702 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003703 mbedtls_free( handshake->transform_earlydata );
3704 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003705#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003706
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003707
3708#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3709 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3710 * processes datagrams and the fact that a datagram is allowed to have
3711 * several records in it, it is possible that the I/O buffers are not
3712 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003713 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3714 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003715#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003716
Jerry Yuba9c7272021-10-30 11:54:10 +08003717 /* mbedtls_platform_zeroize MUST be last one in this function */
3718 mbedtls_platform_zeroize( handshake,
3719 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003720}
3721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003722void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003723{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003724 if( session == NULL )
3725 return;
3726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003728 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003729#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003730
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003731#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00003732#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
3733 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00003734 mbedtls_free( session->hostname );
3735#endif
Xiaokang Qianed0620c2022-10-12 06:58:13 +00003736 mbedtls_free( session->ticket );
3737#endif
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00003738
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003739 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003740}
3741
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003742#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003743
3744#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3745#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3746#else
3747#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3748#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3749
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003750#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003751
3752#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3753#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3754#else
3755#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3756#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3757
3758#if defined(MBEDTLS_SSL_ALPN)
3759#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3760#else
3761#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3762#endif /* MBEDTLS_SSL_ALPN */
3763
3764#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3765#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3766#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3767#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3768
3769#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3770 ( (uint32_t) ( \
3771 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3772 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3773 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3774 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3775 0u ) )
3776
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003777static unsigned char ssl_serialized_context_header[] = {
3778 MBEDTLS_VERSION_MAJOR,
3779 MBEDTLS_VERSION_MINOR,
3780 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003781 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3782 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3783 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3784 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3785 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003786};
3787
Paul Bakker5121ce52009-01-03 21:22:43 +00003788/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003789 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003790 *
3791 * The format of the serialized data is:
3792 * (in the presentation language of TLS, RFC 8446 section 3)
3793 *
3794 * // header
3795 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003796 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003797 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003798 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003799 * Note: When updating the format, remember to keep these
3800 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003801 *
3802 * // session sub-structure
3803 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3804 * // transform sub-structure
3805 * uint8 random[64]; // ServerHello.random+ClientHello.random
3806 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3807 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3808 * // fields from ssl_context
3809 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3810 * uint64 in_window_top; // DTLS: last validated record seq_num
3811 * uint64 in_window; // DTLS: bitmask for replay protection
3812 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3813 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3814 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3815 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3816 *
3817 * Note that many fields of the ssl_context or sub-structures are not
3818 * serialized, as they fall in one of the following categories:
3819 *
3820 * 1. forced value (eg in_left must be 0)
3821 * 2. pointer to dynamically-allocated memory (eg session, transform)
3822 * 3. value can be re-derived from other data (eg session keys from MS)
3823 * 4. value was temporary (eg content of input buffer)
3824 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003825 */
3826int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3827 unsigned char *buf,
3828 size_t buf_len,
3829 size_t *olen )
3830{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003831 unsigned char *p = buf;
3832 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003833 size_t session_len;
3834 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003835
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003836 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003837 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3838 * this function's documentation.
3839 *
3840 * These are due to assumptions/limitations in the implementation. Some of
3841 * them are likely to stay (no handshake in progress) some might go away
3842 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003843 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003844 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003845 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003846 {
3847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003848 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003849 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003850 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003851 {
3852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003853 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003854 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003855 /* Double-check that sub-structures are indeed ready */
3856 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003857 {
3858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003859 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003860 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003861 /* There must be no pending incoming or outgoing data */
3862 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003863 {
3864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003865 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003866 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003867 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003868 {
3869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003870 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003871 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003872 /* Protocol must be DLTS, not TLS */
3873 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003874 {
3875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003876 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003877 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003878 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003879 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003880 {
3881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003882 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003883 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003884 /* We must be using an AEAD ciphersuite */
3885 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003886 {
3887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003888 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003889 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003890 /* Renegotiation must not be enabled */
3891#if defined(MBEDTLS_SSL_RENEGOTIATION)
3892 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003893 {
3894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003895 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003896 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003897#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003898
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003899 /*
3900 * Version and format identifier
3901 */
3902 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003903
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003904 if( used <= buf_len )
3905 {
3906 memcpy( p, ssl_serialized_context_header,
3907 sizeof( ssl_serialized_context_header ) );
3908 p += sizeof( ssl_serialized_context_header );
3909 }
3910
3911 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003912 * Session (length + data)
3913 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003914 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003915 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3916 return( ret );
3917
3918 used += 4 + session_len;
3919 if( used <= buf_len )
3920 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003921 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3922 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003923
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003924 ret = ssl_session_save( ssl->session, 1,
3925 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003926 if( ret != 0 )
3927 return( ret );
3928
3929 p += session_len;
3930 }
3931
3932 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003933 * Transform
3934 */
3935 used += sizeof( ssl->transform->randbytes );
3936 if( used <= buf_len )
3937 {
3938 memcpy( p, ssl->transform->randbytes,
3939 sizeof( ssl->transform->randbytes ) );
3940 p += sizeof( ssl->transform->randbytes );
3941 }
3942
3943#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3944 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3945 if( used <= buf_len )
3946 {
3947 *p++ = ssl->transform->in_cid_len;
3948 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3949 p += ssl->transform->in_cid_len;
3950
3951 *p++ = ssl->transform->out_cid_len;
3952 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3953 p += ssl->transform->out_cid_len;
3954 }
3955#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3956
3957 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003958 * Saved fields from top-level ssl_context structure
3959 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003960 used += 4;
3961 if( used <= buf_len )
3962 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003963 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3964 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003965 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003966
3967#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3968 used += 16;
3969 if( used <= buf_len )
3970 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003971 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3972 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003973
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003974 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3975 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003976 }
3977#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3978
3979#if defined(MBEDTLS_SSL_PROTO_DTLS)
3980 used += 1;
3981 if( used <= buf_len )
3982 {
3983 *p++ = ssl->disable_datagram_packing;
3984 }
3985#endif /* MBEDTLS_SSL_PROTO_DTLS */
3986
Jerry Yuae0b2e22021-10-08 15:21:19 +08003987 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003988 if( used <= buf_len )
3989 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003990 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3991 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003992 }
3993
3994#if defined(MBEDTLS_SSL_PROTO_DTLS)
3995 used += 2;
3996 if( used <= buf_len )
3997 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003998 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
3999 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004000 }
4001#endif /* MBEDTLS_SSL_PROTO_DTLS */
4002
4003#if defined(MBEDTLS_SSL_ALPN)
4004 {
4005 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004006 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004007 : 0;
4008
4009 used += 1 + alpn_len;
4010 if( used <= buf_len )
4011 {
4012 *p++ = alpn_len;
4013
4014 if( ssl->alpn_chosen != NULL )
4015 {
4016 memcpy( p, ssl->alpn_chosen, alpn_len );
4017 p += alpn_len;
4018 }
4019 }
4020 }
4021#endif /* MBEDTLS_SSL_ALPN */
4022
4023 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004024 * Done
4025 */
4026 *olen = used;
4027
4028 if( used > buf_len )
4029 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004030
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004031 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4032
Hanno Becker43aefe22020-02-05 10:44:56 +00004033 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004034}
4035
4036/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004037 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004038 *
4039 * This internal version is wrapped by a public function that cleans up in
4040 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004041 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004042MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004043static int ssl_context_load( mbedtls_ssl_context *ssl,
4044 const unsigned char *buf,
4045 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004046{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004047 const unsigned char *p = buf;
4048 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004049 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004050 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004051
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004052 /*
4053 * The context should have been freshly setup or reset.
4054 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004055 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004056 * renegotiating, or if the user mistakenly loaded a session first.)
4057 */
4058 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4059 ssl->session != NULL )
4060 {
4061 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4062 }
4063
4064 /*
4065 * We can't check that the config matches the initial one, but we can at
4066 * least check it matches the requirements for serializing.
4067 */
4068 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004069 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4070 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004071#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004072 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004073#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004074 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004075 {
4076 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4077 }
4078
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004079 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4080
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004081 /*
4082 * Check version identifier
4083 */
4084 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4085 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4086
4087 if( memcmp( p, ssl_serialized_context_header,
4088 sizeof( ssl_serialized_context_header ) ) != 0 )
4089 {
4090 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4091 }
4092 p += sizeof( ssl_serialized_context_header );
4093
4094 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004095 * Session
4096 */
4097 if( (size_t)( end - p ) < 4 )
4098 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4099
4100 session_len = ( (size_t) p[0] << 24 ) |
4101 ( (size_t) p[1] << 16 ) |
4102 ( (size_t) p[2] << 8 ) |
4103 ( (size_t) p[3] );
4104 p += 4;
4105
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004106 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004107 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004108 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004109 ssl->session_in = ssl->session;
4110 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004111 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004112
4113 if( (size_t)( end - p ) < session_len )
4114 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4115
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004116 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004117 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004118 {
4119 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004120 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004121 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004122
4123 p += session_len;
4124
4125 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004126 * Transform
4127 */
4128
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004129 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004130 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004131 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004132 ssl->transform_in = ssl->transform;
4133 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004134 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004135
4136 /* Read random bytes and populate structure */
4137 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4138 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08004139#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00004140 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004141 ssl->session->ciphersuite,
4142 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004143#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004144 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004145#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004146 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
4147 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004148 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004149 ssl->conf->endpoint,
4150 ssl );
4151 if( ret != 0 )
4152 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004153#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004154 p += sizeof( ssl->transform->randbytes );
4155
4156#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4157 /* Read connection IDs and store them */
4158 if( (size_t)( end - p ) < 1 )
4159 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4160
4161 ssl->transform->in_cid_len = *p++;
4162
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004163 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004164 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4165
4166 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4167 p += ssl->transform->in_cid_len;
4168
4169 ssl->transform->out_cid_len = *p++;
4170
4171 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4172 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4173
4174 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4175 p += ssl->transform->out_cid_len;
4176#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4177
4178 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004179 * Saved fields from top-level ssl_context structure
4180 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004181 if( (size_t)( end - p ) < 4 )
4182 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4183
4184 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4185 ( (uint32_t) p[1] << 16 ) |
4186 ( (uint32_t) p[2] << 8 ) |
4187 ( (uint32_t) p[3] );
4188 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004189
4190#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4191 if( (size_t)( end - p ) < 16 )
4192 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4193
4194 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4195 ( (uint64_t) p[1] << 48 ) |
4196 ( (uint64_t) p[2] << 40 ) |
4197 ( (uint64_t) p[3] << 32 ) |
4198 ( (uint64_t) p[4] << 24 ) |
4199 ( (uint64_t) p[5] << 16 ) |
4200 ( (uint64_t) p[6] << 8 ) |
4201 ( (uint64_t) p[7] );
4202 p += 8;
4203
4204 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4205 ( (uint64_t) p[1] << 48 ) |
4206 ( (uint64_t) p[2] << 40 ) |
4207 ( (uint64_t) p[3] << 32 ) |
4208 ( (uint64_t) p[4] << 24 ) |
4209 ( (uint64_t) p[5] << 16 ) |
4210 ( (uint64_t) p[6] << 8 ) |
4211 ( (uint64_t) p[7] );
4212 p += 8;
4213#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4214
4215#if defined(MBEDTLS_SSL_PROTO_DTLS)
4216 if( (size_t)( end - p ) < 1 )
4217 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4218
4219 ssl->disable_datagram_packing = *p++;
4220#endif /* MBEDTLS_SSL_PROTO_DTLS */
4221
Jerry Yud9a94fe2021-09-28 18:58:59 +08004222 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004223 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004224 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4225 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004226
4227#if defined(MBEDTLS_SSL_PROTO_DTLS)
4228 if( (size_t)( end - p ) < 2 )
4229 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4230
4231 ssl->mtu = ( p[0] << 8 ) | p[1];
4232 p += 2;
4233#endif /* MBEDTLS_SSL_PROTO_DTLS */
4234
4235#if defined(MBEDTLS_SSL_ALPN)
4236 {
4237 uint8_t alpn_len;
4238 const char **cur;
4239
4240 if( (size_t)( end - p ) < 1 )
4241 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4242
4243 alpn_len = *p++;
4244
4245 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4246 {
4247 /* alpn_chosen should point to an item in the configured list */
4248 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4249 {
4250 if( strlen( *cur ) == alpn_len &&
4251 memcmp( p, cur, alpn_len ) == 0 )
4252 {
4253 ssl->alpn_chosen = *cur;
4254 break;
4255 }
4256 }
4257 }
4258
4259 /* can only happen on conf mismatch */
4260 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4261 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4262
4263 p += alpn_len;
4264 }
4265#endif /* MBEDTLS_SSL_ALPN */
4266
4267 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004268 * Forced fields from top-level ssl_context structure
4269 *
4270 * Most of them already set to the correct value by mbedtls_ssl_init() and
4271 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4272 */
4273 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004274 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004275
Hanno Becker361b10d2019-08-30 10:42:49 +01004276 /* Adjust pointers for header fields of outgoing records to
4277 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004278 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004279
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004280#if defined(MBEDTLS_SSL_PROTO_DTLS)
4281 ssl->in_epoch = 1;
4282#endif
4283
4284 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4285 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004286 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4287 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004288 if( ssl->handshake != NULL )
4289 {
4290 mbedtls_ssl_handshake_free( ssl );
4291 mbedtls_free( ssl->handshake );
4292 ssl->handshake = NULL;
4293 }
4294
4295 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004296 * Done - should have consumed entire buffer
4297 */
4298 if( p != end )
4299 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004300
4301 return( 0 );
4302}
4303
4304/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004305 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004306 */
4307int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4308 const unsigned char *buf,
4309 size_t len )
4310{
4311 int ret = ssl_context_load( context, buf, len );
4312
4313 if( ret != 0 )
4314 mbedtls_ssl_free( context );
4315
4316 return( ret );
4317}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004318#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004319
4320/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004321 * Free an SSL context
4322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004324{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004325 if( ssl == NULL )
4326 return;
4327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004328 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004329
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004330 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004331 {
sander-visserb8aa2072020-05-06 22:05:13 +02004332#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4333 size_t out_buf_len = ssl->out_buf_len;
4334#else
4335 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4336#endif
4337
Darryl Greenb33cc762019-11-28 14:29:44 +00004338 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004339 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004340 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004341 }
4342
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004343 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004344 {
sander-visserb8aa2072020-05-06 22:05:13 +02004345#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4346 size_t in_buf_len = ssl->in_buf_len;
4347#else
4348 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4349#endif
4350
Darryl Greenb33cc762019-11-28 14:29:44 +00004351 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004352 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004353 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004354 }
4355
Paul Bakker48916f92012-09-16 19:57:18 +00004356 if( ssl->transform )
4357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004358 mbedtls_ssl_transform_free( ssl->transform );
4359 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004360 }
4361
4362 if( ssl->handshake )
4363 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004364 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004365 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4366 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004368 mbedtls_free( ssl->handshake );
4369 mbedtls_free( ssl->transform_negotiate );
4370 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004371 }
4372
Ronald Cron6f135e12021-12-08 16:57:54 +01004373#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004374 mbedtls_ssl_transform_free( ssl->transform_application );
4375 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004376#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004377
Paul Bakkerc0463502013-02-14 11:19:38 +01004378 if( ssl->session )
4379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004380 mbedtls_ssl_session_free( ssl->session );
4381 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004382 }
4383
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004384#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004385 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004386 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004387 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004388 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004389 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004390#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004391
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004392#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004393 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004394#endif
4395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004397
Paul Bakker86f04f42013-02-14 11:20:09 +01004398 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004399 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004400}
4401
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004402/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004403 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004404 */
4405void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4406{
4407 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4408}
4409
Gilles Peskineae270bf2021-06-02 00:05:29 +02004410/* The selection should be the same as mbedtls_x509_crt_profile_default in
4411 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004412 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004413 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004414 * about this list.
4415 */
Brett Warrene0edc842021-08-17 09:53:13 +01004416static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004417#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004418 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004419#endif
4420#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004421 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004422#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004423#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004424 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004425#endif
4426#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004427 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004428#endif
4429#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004430 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004431#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004432#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004433 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004434#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004435#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004436 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004437#endif
4438#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004439 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004440#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004441 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004442};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004443
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004444static int ssl_preset_suiteb_ciphersuites[] = {
4445 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4446 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4447 0
4448};
4449
Gilles Peskineeccd8882020-03-10 12:19:08 +01004450#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004451
Jerry Yu909df7b2022-01-22 11:56:27 +08004452/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004453 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004454 * rules SHOULD be upheld.
4455 * - No duplicate entries.
4456 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004457 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004458 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004459 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004460static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004461
Andrzej Kurek25f27152022-08-17 16:09:31 -04004462#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yued5e9f42022-01-26 11:21:34 +08004463 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4464 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004465#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004466 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004467
Andrzej Kurek25f27152022-08-17 16:09:31 -04004468#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yu909df7b2022-01-22 11:56:27 +08004469 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4470 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004471#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004472 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4473
Andrzej Kurek25f27152022-08-17 16:09:31 -04004474#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yued5e9f42022-01-26 11:21:34 +08004475 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4476 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004477#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004478 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004479
Andrzej Kurek25f27152022-08-17 16:09:31 -04004480#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004481 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004482#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004483
Andrzej Kurek25f27152022-08-17 16:09:31 -04004484#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004485 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004486#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004487
Andrzej Kurek25f27152022-08-17 16:09:31 -04004488#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004489 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004490#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004491
Andrzej Kurek25f27152022-08-17 16:09:31 -04004492#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu693a47a2022-06-23 14:02:28 +08004493 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4494#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4495
Andrzej Kurek25f27152022-08-17 16:09:31 -04004496#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu693a47a2022-06-23 14:02:28 +08004497 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4498#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4499
Andrzej Kurek25f27152022-08-17 16:09:31 -04004500#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu693a47a2022-06-23 14:02:28 +08004501 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4502#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4503
Gabor Mezei15b95a62022-05-09 16:37:58 +02004504 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004505};
4506
4507/* NOTICE: see above */
4508#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4509static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004510#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004511#if defined(MBEDTLS_ECDSA_C)
4512 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004513#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004514#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4515 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4516#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004517#if defined(MBEDTLS_RSA_C)
4518 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4519#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004520#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004521#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004522#if defined(MBEDTLS_ECDSA_C)
4523 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004524#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004525#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4526 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4527#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004528#if defined(MBEDTLS_RSA_C)
4529 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4530#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004531#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004532#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004533#if defined(MBEDTLS_ECDSA_C)
4534 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004535#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004536#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4537 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4538#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004539#if defined(MBEDTLS_RSA_C)
4540 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4541#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004542#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004543 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004544};
4545#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4546/* NOTICE: see above */
4547static uint16_t ssl_preset_suiteb_sig_algs[] = {
4548
Andrzej Kurek25f27152022-08-17 16:09:31 -04004549#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yu909df7b2022-01-22 11:56:27 +08004550 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4551 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004552#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004553 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4554
Andrzej Kurek25f27152022-08-17 16:09:31 -04004555#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yu53037892022-01-25 11:02:06 +08004556 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4557 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004558#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004559 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004560
Andrzej Kurek25f27152022-08-17 16:09:31 -04004561#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu909df7b2022-01-22 11:56:27 +08004562 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004563#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu1a8b4812022-01-20 17:56:50 +08004564
Andrzej Kurek25f27152022-08-17 16:09:31 -04004565#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu53037892022-01-25 11:02:06 +08004566 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004567#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004568
Gabor Mezei15b95a62022-05-09 16:37:58 +02004569 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004570};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004571
Jerry Yu909df7b2022-01-22 11:56:27 +08004572/* NOTICE: see above */
4573#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4574static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004575#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004576#if defined(MBEDTLS_ECDSA_C)
4577 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004578#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004579#if defined(MBEDTLS_RSA_C)
4580 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4581#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004582#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004583#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004584#if defined(MBEDTLS_ECDSA_C)
4585 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004586#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004587#if defined(MBEDTLS_RSA_C)
4588 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4589#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004590#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004591 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004592};
Jerry Yu909df7b2022-01-22 11:56:27 +08004593#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4594
Jerry Yu1a8b4812022-01-20 17:56:50 +08004595#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004596
Brett Warrene0edc842021-08-17 09:53:13 +01004597static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004598#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004599 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004600#endif
4601#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004602 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004603#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004604 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004605};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004606
Jerry Yu1a8b4812022-01-20 17:56:50 +08004607#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004608/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004609 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004610MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004611static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004612{
4613 size_t i, j;
4614 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004615
Gabor Mezei15b95a62022-05-09 16:37:58 +02004616 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004617 {
Jerry Yuf377d642022-01-25 10:43:59 +08004618 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004619 {
Jerry Yuf377d642022-01-25 10:43:59 +08004620 if( sig_algs[i] != sig_algs[j] )
4621 continue;
4622 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4623 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4624 sig_algs[i], j, i );
4625 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004626 }
4627 }
4628 return( ret );
4629}
4630
4631#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4632
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004633/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004634 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004635 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004636int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004637 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004638{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004639#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004640 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004641#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004642
Jerry Yu1a8b4812022-01-20 17:56:50 +08004643#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004644 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004645 {
4646 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4647 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4648 }
4649
Jerry Yuf377d642022-01-25 10:43:59 +08004650 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004651 {
4652 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4653 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4654 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004655
4656#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004657 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004658 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004659 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004660 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4661 }
4662
Jerry Yuf377d642022-01-25 10:43:59 +08004663 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004664 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004665 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004666 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4667 }
4668#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004669#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4670
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004671 /* Use the functions here so that they are covered in tests,
4672 * but otherwise access member directly for efficiency */
4673 mbedtls_ssl_conf_endpoint( conf, endpoint );
4674 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004675
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004676 /*
4677 * Things that are common to all presets
4678 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004679#if defined(MBEDTLS_SSL_CLI_C)
4680 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4681 {
4682 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4683#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4684 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4685#endif
4686 }
4687#endif
4688
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004689#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4690 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4691#endif
4692
4693#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4694 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4695#endif
4696
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004697#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004698 conf->f_cookie_write = ssl_cookie_write_dummy;
4699 conf->f_cookie_check = ssl_cookie_check_dummy;
4700#endif
4701
4702#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4703 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4704#endif
4705
Janos Follath088ce432017-04-10 12:42:31 +01004706#if defined(MBEDTLS_SSL_SRV_C)
4707 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004708 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004709#endif
4710
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004711#if defined(MBEDTLS_SSL_PROTO_DTLS)
4712 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4713 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4714#endif
4715
4716#if defined(MBEDTLS_SSL_RENEGOTIATION)
4717 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004718 memset( conf->renego_period, 0x00, 2 );
4719 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004720#endif
4721
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004722#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004723 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4724 {
4725 const unsigned char dhm_p[] =
4726 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4727 const unsigned char dhm_g[] =
4728 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004729
Hanno Beckere2defad2021-07-24 05:59:17 +01004730 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4731 dhm_p, sizeof( dhm_p ),
4732 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4733 {
4734 return( ret );
4735 }
4736 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004737#endif
4738
Ronald Cron6f135e12021-12-08 16:57:54 +01004739#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004740#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004741 mbedtls_ssl_conf_new_session_tickets(
4742 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4743#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004744 /*
4745 * Allow all TLS 1.3 key exchange modes by default.
4746 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004747 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004748#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004749
XiaokangQian4d3a6042022-04-21 13:46:17 +00004750 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4751 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004752#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004753 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004754 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004755#else
XiaokangQian060d8672022-04-21 09:24:56 +00004756 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004757#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004758 }
4759 else
4760 {
4761#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4762 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4763 {
4764 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4765 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4766 }
4767 else
4768 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4769 {
4770 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4771 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4772 }
4773#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4774 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4775 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4776#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4777 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4778 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4779#else
4780 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4781#endif
4782 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004783
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004784 /*
4785 * Preset-specific defaults
4786 */
4787 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004788 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004789 /*
4790 * NSA Suite B
4791 */
4792 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004793
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004794 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004795
4796#if defined(MBEDTLS_X509_CRT_PARSE_C)
4797 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004798#endif
4799
Gilles Peskineeccd8882020-03-10 12:19:08 +01004800#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004801#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4802 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4803 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4804 else
4805#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4806 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004807#endif
4808
Brett Warrene0edc842021-08-17 09:53:13 +01004809#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4810 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004811#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004812 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004813 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004814
4815 /*
4816 * Default
4817 */
4818 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004819
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004820 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004821
4822#if defined(MBEDTLS_X509_CRT_PARSE_C)
4823 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4824#endif
4825
Gilles Peskineeccd8882020-03-10 12:19:08 +01004826#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004827#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4828 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4829 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4830 else
4831#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4832 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004833#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004834
Brett Warrene0edc842021-08-17 09:53:13 +01004835#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4836 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004837#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004838 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004839
4840#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4841 conf->dhm_min_bitlen = 1024;
4842#endif
4843 }
4844
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004845 return( 0 );
4846}
4847
4848/*
4849 * Free mbedtls_ssl_config
4850 */
4851void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4852{
4853#if defined(MBEDTLS_DHM_C)
4854 mbedtls_mpi_free( &conf->dhm_P );
4855 mbedtls_mpi_free( &conf->dhm_G );
4856#endif
4857
Gilles Peskineeccd8882020-03-10 12:19:08 +01004858#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004859#if defined(MBEDTLS_USE_PSA_CRYPTO)
4860 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4861 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004862 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4863 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004864#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004865 if( conf->psk != NULL )
4866 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004867 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004868 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004869 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004870 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004871 }
4872
4873 if( conf->psk_identity != NULL )
4874 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004875 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004876 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004877 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004878 conf->psk_identity_len = 0;
4879 }
4880#endif
4881
4882#if defined(MBEDTLS_X509_CRT_PARSE_C)
4883 ssl_key_cert_free( conf->key_cert );
4884#endif
4885
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004886 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004887}
4888
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004889#if defined(MBEDTLS_PK_C) && \
4890 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004891/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004892 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004893 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004894unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004895{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004896#if defined(MBEDTLS_RSA_C)
4897 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4898 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004899#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004900#if defined(MBEDTLS_ECDSA_C)
4901 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4902 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004903#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004904 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004905}
4906
Hanno Becker7e5437a2017-04-28 17:15:26 +01004907unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4908{
4909 switch( type ) {
4910 case MBEDTLS_PK_RSA:
4911 return( MBEDTLS_SSL_SIG_RSA );
4912 case MBEDTLS_PK_ECDSA:
4913 case MBEDTLS_PK_ECKEY:
4914 return( MBEDTLS_SSL_SIG_ECDSA );
4915 default:
4916 return( MBEDTLS_SSL_SIG_ANON );
4917 }
4918}
4919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004920mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004921{
4922 switch( sig )
4923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004924#if defined(MBEDTLS_RSA_C)
4925 case MBEDTLS_SSL_SIG_RSA:
4926 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004927#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004928#if defined(MBEDTLS_ECDSA_C)
4929 case MBEDTLS_SSL_SIG_ECDSA:
4930 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004931#endif
4932 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004933 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004934 }
4935}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004936#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004937
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004938/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004939 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004940 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004941mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004942{
4943 switch( hash )
4944 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004945#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004946 case MBEDTLS_SSL_HASH_MD5:
4947 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004948#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004949#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004950 case MBEDTLS_SSL_HASH_SHA1:
4951 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004952#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004953#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954 case MBEDTLS_SSL_HASH_SHA224:
4955 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004956#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004957#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004958 case MBEDTLS_SSL_HASH_SHA256:
4959 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004960#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004961#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004962 case MBEDTLS_SSL_HASH_SHA384:
4963 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004964#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004965#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004966 case MBEDTLS_SSL_HASH_SHA512:
4967 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004968#endif
4969 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004970 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004971 }
4972}
4973
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004974/*
4975 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4976 */
4977unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4978{
4979 switch( md )
4980 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004981#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004982 case MBEDTLS_MD_MD5:
4983 return( MBEDTLS_SSL_HASH_MD5 );
4984#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004985#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004986 case MBEDTLS_MD_SHA1:
4987 return( MBEDTLS_SSL_HASH_SHA1 );
4988#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004989#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004990 case MBEDTLS_MD_SHA224:
4991 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004992#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004993#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004994 case MBEDTLS_MD_SHA256:
4995 return( MBEDTLS_SSL_HASH_SHA256 );
4996#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004997#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004998 case MBEDTLS_MD_SHA384:
4999 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005000#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005001#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005002 case MBEDTLS_MD_SHA512:
5003 return( MBEDTLS_SSL_HASH_SHA512 );
5004#endif
5005 default:
5006 return( MBEDTLS_SSL_HASH_NONE );
5007 }
5008}
5009
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005010/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005011 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005012 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005013 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005014int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005015{
Brett Warrene0edc842021-08-17 09:53:13 +01005016 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005017
Brett Warrene0edc842021-08-17 09:53:13 +01005018 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005019 return( -1 );
5020
Brett Warrene0edc842021-08-17 09:53:13 +01005021 for( ; *group_list != 0; group_list++ )
5022 {
5023 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005024 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005025 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005026
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005027 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005028}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005029
5030#if defined(MBEDTLS_ECP_C)
5031/*
5032 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5033 */
5034int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5035{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005036 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005037 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005038
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005039 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005040 return -1;
5041
5042 uint16_t tls_id = grp_info->tls_id;
5043
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005044 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5045}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005046#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005048#if defined(MBEDTLS_X509_CRT_PARSE_C)
5049int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5050 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005051 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005052 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005053{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005054 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005055 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005056 const char *ext_oid;
5057 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005059 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005060 {
5061 /* Server part of the key exchange */
5062 switch( ciphersuite->key_exchange )
5063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005064 case MBEDTLS_KEY_EXCHANGE_RSA:
5065 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005066 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005067 break;
5068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005069 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5070 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5071 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5072 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005073 break;
5074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005075 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5076 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005077 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005078 break;
5079
5080 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005081 case MBEDTLS_KEY_EXCHANGE_NONE:
5082 case MBEDTLS_KEY_EXCHANGE_PSK:
5083 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5084 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005085 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005086 usage = 0;
5087 }
5088 }
5089 else
5090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005091 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5092 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005093 }
5094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005095 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005096 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005097 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005098 ret = -1;
5099 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005101 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005103 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5104 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005105 }
5106 else
5107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005108 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5109 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005110 }
5111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005112 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005113 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005114 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005115 ret = -1;
5116 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005117
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005118 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005119}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005120#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005121
Jerry Yu148165c2021-09-24 23:20:59 +08005122#if defined(MBEDTLS_USE_PSA_CRYPTO)
5123int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5124 const mbedtls_md_type_t md,
5125 unsigned char *dst,
5126 size_t dst_len,
5127 size_t *olen )
5128{
Ronald Cronf6893e12022-01-07 22:09:01 +01005129 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5130 psa_hash_operation_t *hash_operation_to_clone;
5131 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5132
Jerry Yu148165c2021-09-24 23:20:59 +08005133 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005134
5135 switch( md )
5136 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005137#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005138 case MBEDTLS_MD_SHA384:
5139 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5140 break;
5141#endif
5142
Andrzej Kurek25f27152022-08-17 16:09:31 -04005143#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005144 case MBEDTLS_MD_SHA256:
5145 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5146 break;
5147#endif
5148
5149 default:
5150 goto exit;
5151 }
5152
5153 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5154 if( status != PSA_SUCCESS )
5155 goto exit;
5156
5157 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5158 if( status != PSA_SUCCESS )
5159 goto exit;
5160
5161exit:
Ronald Cronb788c042022-02-15 09:14:15 +01005162 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005163}
5164#else /* MBEDTLS_USE_PSA_CRYPTO */
5165
Andrzej Kurek25f27152022-08-17 16:09:31 -04005166#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005167MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005168static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5169 unsigned char *dst,
5170 size_t dst_len,
5171 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005172{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005173 int ret;
5174 mbedtls_sha512_context sha512;
5175
5176 if( dst_len < 48 )
5177 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5178
5179 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005180 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005181
5182 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5183 {
5184 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5185 goto exit;
5186 }
5187
5188 *olen = 48;
5189
5190exit:
5191
5192 mbedtls_sha512_free( &sha512 );
5193 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005194}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005195#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005196
Andrzej Kurek25f27152022-08-17 16:09:31 -04005197#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005198MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005199static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5200 unsigned char *dst,
5201 size_t dst_len,
5202 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005203{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005204 int ret;
5205 mbedtls_sha256_context sha256;
5206
5207 if( dst_len < 32 )
5208 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5209
5210 mbedtls_sha256_init( &sha256 );
5211 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005212
Jerry Yu24c0ec32021-09-09 14:21:07 +08005213 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5214 {
5215 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5216 goto exit;
5217 }
5218
5219 *olen = 32;
5220
5221exit:
5222
5223 mbedtls_sha256_free( &sha256 );
5224 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005225}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005226#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005227
Jerry Yu000f9762021-09-14 11:12:51 +08005228int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5229 const mbedtls_md_type_t md,
5230 unsigned char *dst,
5231 size_t dst_len,
5232 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005233{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005234 switch( md )
5235 {
5236
Andrzej Kurek25f27152022-08-17 16:09:31 -04005237#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005238 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005239 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005240#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005241
Andrzej Kurek25f27152022-08-17 16:09:31 -04005242#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005243 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005244 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005245#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005246
5247 default:
5248 break;
5249 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005250 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005251}
XiaokangQian647719a2021-12-07 09:16:29 +00005252
Jerry Yu148165c2021-09-24 23:20:59 +08005253#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005254
Gabor Mezei078e8032022-04-27 21:17:56 +02005255#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5256/* mbedtls_ssl_parse_sig_alg_ext()
5257 *
5258 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5259 * value (TLS 1.3 RFC8446):
5260 * enum {
5261 * ....
5262 * ecdsa_secp256r1_sha256( 0x0403 ),
5263 * ecdsa_secp384r1_sha384( 0x0503 ),
5264 * ecdsa_secp521r1_sha512( 0x0603 ),
5265 * ....
5266 * } SignatureScheme;
5267 *
5268 * struct {
5269 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5270 * } SignatureSchemeList;
5271 *
5272 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5273 * value (TLS 1.2 RFC5246):
5274 * enum {
5275 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5276 * sha512(6), (255)
5277 * } HashAlgorithm;
5278 *
5279 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5280 * SignatureAlgorithm;
5281 *
5282 * struct {
5283 * HashAlgorithm hash;
5284 * SignatureAlgorithm signature;
5285 * } SignatureAndHashAlgorithm;
5286 *
5287 * SignatureAndHashAlgorithm
5288 * supported_signature_algorithms<2..2^16-2>;
5289 *
5290 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5291 * generalization of the TLS 1.2 signature algorithm extension.
5292 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5293 * `SignatureScheme` field of TLS 1.3
5294 *
5295 */
5296int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5297 const unsigned char *buf,
5298 const unsigned char *end )
5299{
5300 const unsigned char *p = buf;
5301 size_t supported_sig_algs_len = 0;
5302 const unsigned char *supported_sig_algs_end;
5303 uint16_t sig_alg;
5304 uint32_t common_idx = 0;
5305
5306 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5307 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5308 p += 2;
5309
5310 memset( ssl->handshake->received_sig_algs, 0,
5311 sizeof(ssl->handshake->received_sig_algs) );
5312
5313 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5314 supported_sig_algs_end = p + supported_sig_algs_len;
5315 while( p < supported_sig_algs_end )
5316 {
5317 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5318 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5319 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005320 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5321 sig_alg,
5322 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005323#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005324 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005325 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5326 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5327 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005328 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005329 }
5330#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005331
Jerry Yuf3b46b52022-06-19 16:52:27 +08005332 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5333 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005334
5335 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5336 {
5337 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5338 common_idx += 1;
5339 }
5340 }
5341 /* Check that we consumed all the message. */
5342 if( p != end )
5343 {
5344 MBEDTLS_SSL_DEBUG_MSG( 1,
5345 ( "Signature algorithms extension length misaligned" ) );
5346 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5347 MBEDTLS_ERR_SSL_DECODE_ERROR );
5348 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5349 }
5350
5351 if( common_idx == 0 )
5352 {
5353 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5354 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5355 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5356 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5357 }
5358
Gabor Mezei15b95a62022-05-09 16:37:58 +02005359 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005360 return( 0 );
5361}
5362
5363#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5364
Jerry Yudc7bd172022-02-17 13:44:15 +08005365#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5366
5367#if defined(MBEDTLS_USE_PSA_CRYPTO)
5368
5369static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5370 mbedtls_svc_key_id_t key,
5371 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005372 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005373 const unsigned char* seed, size_t seed_length,
5374 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005375 const unsigned char* other_secret,
5376 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005377 size_t capacity )
5378{
5379 psa_status_t status;
5380
5381 status = psa_key_derivation_setup( derivation, alg );
5382 if( status != PSA_SUCCESS )
5383 return( status );
5384
5385 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5386 {
5387 status = psa_key_derivation_input_bytes( derivation,
5388 PSA_KEY_DERIVATION_INPUT_SEED,
5389 seed, seed_length );
5390 if( status != PSA_SUCCESS )
5391 return( status );
5392
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005393 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005394 {
5395 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005396 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5397 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005398 if( status != PSA_SUCCESS )
5399 return( status );
5400 }
5401
Jerry Yudc7bd172022-02-17 13:44:15 +08005402 if( mbedtls_svc_key_id_is_null( key ) )
5403 {
5404 status = psa_key_derivation_input_bytes(
5405 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005406 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005407 }
5408 else
5409 {
5410 status = psa_key_derivation_input_key(
5411 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5412 }
5413 if( status != PSA_SUCCESS )
5414 return( status );
5415
5416 status = psa_key_derivation_input_bytes( derivation,
5417 PSA_KEY_DERIVATION_INPUT_LABEL,
5418 label, label_length );
5419 if( status != PSA_SUCCESS )
5420 return( status );
5421 }
5422 else
5423 {
5424 return( PSA_ERROR_NOT_SUPPORTED );
5425 }
5426
5427 status = psa_key_derivation_set_capacity( derivation, capacity );
5428 if( status != PSA_SUCCESS )
5429 return( status );
5430
5431 return( PSA_SUCCESS );
5432}
5433
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005434MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005435static int tls_prf_generic( mbedtls_md_type_t md_type,
5436 const unsigned char *secret, size_t slen,
5437 const char *label,
5438 const unsigned char *random, size_t rlen,
5439 unsigned char *dstbuf, size_t dlen )
5440{
5441 psa_status_t status;
5442 psa_algorithm_t alg;
5443 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5444 psa_key_derivation_operation_t derivation =
5445 PSA_KEY_DERIVATION_OPERATION_INIT;
5446
5447 if( md_type == MBEDTLS_MD_SHA384 )
5448 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5449 else
5450 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5451
5452 /* Normally a "secret" should be long enough to be impossible to
5453 * find by brute force, and in particular should not be empty. But
5454 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5455 * and for this use case it makes sense to have a 0-length "secret".
5456 * Since the key API doesn't allow importing a key of length 0,
5457 * keep master_key=0, which setup_psa_key_derivation() understands
5458 * to mean a 0-length "secret" input. */
5459 if( slen != 0 )
5460 {
5461 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5462 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5463 psa_set_key_algorithm( &key_attributes, alg );
5464 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5465
5466 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5467 if( status != PSA_SUCCESS )
5468 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5469 }
5470
5471 status = setup_psa_key_derivation( &derivation,
5472 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005473 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005474 random, rlen,
5475 (unsigned char const *) label,
5476 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005477 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005478 dlen );
5479 if( status != PSA_SUCCESS )
5480 {
5481 psa_key_derivation_abort( &derivation );
5482 psa_destroy_key( master_key );
5483 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5484 }
5485
5486 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5487 if( status != PSA_SUCCESS )
5488 {
5489 psa_key_derivation_abort( &derivation );
5490 psa_destroy_key( master_key );
5491 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5492 }
5493
5494 status = psa_key_derivation_abort( &derivation );
5495 if( status != PSA_SUCCESS )
5496 {
5497 psa_destroy_key( master_key );
5498 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5499 }
5500
5501 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5502 status = psa_destroy_key( master_key );
5503 if( status != PSA_SUCCESS )
5504 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5505
5506 return( 0 );
5507}
5508
5509#else /* MBEDTLS_USE_PSA_CRYPTO */
5510
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005511MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005512static int tls_prf_generic( mbedtls_md_type_t md_type,
5513 const unsigned char *secret, size_t slen,
5514 const char *label,
5515 const unsigned char *random, size_t rlen,
5516 unsigned char *dstbuf, size_t dlen )
5517{
5518 size_t nb;
5519 size_t i, j, k, md_len;
5520 unsigned char *tmp;
5521 size_t tmp_len = 0;
5522 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5523 const mbedtls_md_info_t *md_info;
5524 mbedtls_md_context_t md_ctx;
5525 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5526
5527 mbedtls_md_init( &md_ctx );
5528
5529 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5530 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5531
5532 md_len = mbedtls_md_get_size( md_info );
5533
5534 tmp_len = md_len + strlen( label ) + rlen;
5535 tmp = mbedtls_calloc( 1, tmp_len );
5536 if( tmp == NULL )
5537 {
5538 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5539 goto exit;
5540 }
5541
5542 nb = strlen( label );
5543 memcpy( tmp + md_len, label, nb );
5544 memcpy( tmp + md_len + nb, random, rlen );
5545 nb += rlen;
5546
5547 /*
5548 * Compute P_<hash>(secret, label + random)[0..dlen]
5549 */
5550 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5551 goto exit;
5552
5553 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5554 if( ret != 0 )
5555 goto exit;
5556 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5557 if( ret != 0 )
5558 goto exit;
5559 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5560 if( ret != 0 )
5561 goto exit;
5562
5563 for( i = 0; i < dlen; i += md_len )
5564 {
5565 ret = mbedtls_md_hmac_reset ( &md_ctx );
5566 if( ret != 0 )
5567 goto exit;
5568 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5569 if( ret != 0 )
5570 goto exit;
5571 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5572 if( ret != 0 )
5573 goto exit;
5574
5575 ret = mbedtls_md_hmac_reset ( &md_ctx );
5576 if( ret != 0 )
5577 goto exit;
5578 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5579 if( ret != 0 )
5580 goto exit;
5581 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5582 if( ret != 0 )
5583 goto exit;
5584
5585 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5586
5587 for( j = 0; j < k; j++ )
5588 dstbuf[i + j] = h_i[j];
5589 }
5590
5591exit:
5592 mbedtls_md_free( &md_ctx );
5593
5594 mbedtls_platform_zeroize( tmp, tmp_len );
5595 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5596
5597 mbedtls_free( tmp );
5598
5599 return( ret );
5600}
5601#endif /* MBEDTLS_USE_PSA_CRYPTO */
5602
Andrzej Kurek25f27152022-08-17 16:09:31 -04005603#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005604MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005605static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5606 const char *label,
5607 const unsigned char *random, size_t rlen,
5608 unsigned char *dstbuf, size_t dlen )
5609{
5610 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5611 label, random, rlen, dstbuf, dlen ) );
5612}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005613#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005614
Andrzej Kurek25f27152022-08-17 16:09:31 -04005615#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005616MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005617static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5618 const char *label,
5619 const unsigned char *random, size_t rlen,
5620 unsigned char *dstbuf, size_t dlen )
5621{
5622 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5623 label, random, rlen, dstbuf, dlen ) );
5624}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005625#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005626
Jerry Yuf009d862022-02-17 14:01:37 +08005627/*
5628 * Set appropriate PRF function and other SSL / TLS1.2 functions
5629 *
5630 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005631 * - hash associated with the ciphersuite (only used by TLS 1.2)
5632 *
5633 * Outputs:
5634 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5635 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005636MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005637static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005638 mbedtls_md_type_t hash )
5639{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005640#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005641 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005642 {
5643 handshake->tls_prf = tls_prf_sha384;
5644 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5645 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5646 }
5647 else
5648#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005649#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005650 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005651 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005652 handshake->tls_prf = tls_prf_sha256;
5653 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5654 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5655 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005656#else
Jerry Yuf009d862022-02-17 14:01:37 +08005657 {
Jerry Yuf009d862022-02-17 14:01:37 +08005658 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005659 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005660 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5661 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005662#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005663
5664 return( 0 );
5665}
Jerry Yud6ab2352022-02-17 14:03:43 +08005666
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005667/*
5668 * Compute master secret if needed
5669 *
5670 * Parameters:
5671 * [in/out] handshake
5672 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5673 * (PSA-PSK) ciphersuite_info, psk_opaque
5674 * [out] premaster (cleared)
5675 * [out] master
5676 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5677 * debug: conf->f_dbg, conf->p_dbg
5678 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005679 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005680 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005681MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005682static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5683 unsigned char *master,
5684 const mbedtls_ssl_context *ssl )
5685{
5686 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5687
5688 /* cf. RFC 5246, Section 8.1:
5689 * "The master secret is always exactly 48 bytes in length." */
5690 size_t const master_secret_len = 48;
5691
5692#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5693 unsigned char session_hash[48];
5694#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5695
5696 /* The label for the KDF used for key expansion.
5697 * This is either "master secret" or "extended master secret"
5698 * depending on whether the Extended Master Secret extension
5699 * is used. */
5700 char const *lbl = "master secret";
5701
Przemek Stekielae4ed302022-04-05 17:15:55 +02005702 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005703 * - If the Extended Master Secret extension is not used,
5704 * this is ClientHello.Random + ServerHello.Random
5705 * (see Sect. 8.1 in RFC 5246).
5706 * - If the Extended Master Secret extension is used,
5707 * this is the transcript of the handshake so far.
5708 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005709 unsigned char const *seed = handshake->randbytes;
5710 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005711
5712#if !defined(MBEDTLS_DEBUG_C) && \
5713 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5714 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5715 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5716 ssl = NULL; /* make sure we don't use it except for those cases */
5717 (void) ssl;
5718#endif
5719
5720 if( handshake->resume != 0 )
5721 {
5722 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5723 return( 0 );
5724 }
5725
5726#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5727 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5728 {
5729 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005730 seed = session_hash;
5731 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005732
5733 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005734 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005735 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005736#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005737
Przemek Stekiel99114f32022-04-22 11:20:09 +02005738#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005739 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005740 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005741 {
5742 /* Perform PSK-to-MS expansion in a single step. */
5743 psa_status_t status;
5744 psa_algorithm_t alg;
5745 mbedtls_svc_key_id_t psk;
5746 psa_key_derivation_operation_t derivation =
5747 PSA_KEY_DERIVATION_OPERATION_INIT;
5748 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5749
5750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5751
5752 psk = mbedtls_ssl_get_opaque_psk( ssl );
5753
5754 if( hash_alg == MBEDTLS_MD_SHA384 )
5755 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5756 else
5757 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5758
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005759 size_t other_secret_len = 0;
5760 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005761
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005762 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005763 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005764 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005765 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005766 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005767 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005768 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005769 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005770 other_secret_len = 48;
5771 other_secret = handshake->premaster + 2;
5772 break;
5773 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005774 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005775 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5776 other_secret = handshake->premaster + 2;
5777 break;
5778 default:
5779 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005780 }
5781
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005782 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005783 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005784 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005785 (unsigned char const *) lbl,
5786 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005787 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005788 master_secret_len );
5789 if( status != PSA_SUCCESS )
5790 {
5791 psa_key_derivation_abort( &derivation );
5792 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5793 }
5794
5795 status = psa_key_derivation_output_bytes( &derivation,
5796 master,
5797 master_secret_len );
5798 if( status != PSA_SUCCESS )
5799 {
5800 psa_key_derivation_abort( &derivation );
5801 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5802 }
5803
5804 status = psa_key_derivation_abort( &derivation );
5805 if( status != PSA_SUCCESS )
5806 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5807 }
5808 else
5809#endif
5810 {
5811 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005812 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005813 master,
5814 master_secret_len );
5815 if( ret != 0 )
5816 {
5817 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5818 return( ret );
5819 }
5820
5821 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5822 handshake->premaster,
5823 handshake->pmslen );
5824
5825 mbedtls_platform_zeroize( handshake->premaster,
5826 sizeof(handshake->premaster) );
5827 }
5828
5829 return( 0 );
5830}
5831
Jerry Yud62f87e2022-02-17 14:09:02 +08005832int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5833{
5834 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5835 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5836 ssl->handshake->ciphersuite_info;
5837
5838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5839
5840 /* Set PRF, calc_verify and calc_finished function pointers */
5841 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005842 ciphersuite_info->mac );
5843 if( ret != 0 )
5844 {
5845 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5846 return( ret );
5847 }
5848
5849 /* Compute master secret if needed */
5850 ret = ssl_compute_master( ssl->handshake,
5851 ssl->session_negotiate->master,
5852 ssl );
5853 if( ret != 0 )
5854 {
5855 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5856 return( ret );
5857 }
5858
5859 /* Swap the client and server random values:
5860 * - MS derivation wanted client+server (RFC 5246 8.1)
5861 * - key derivation wants server+client (RFC 5246 6.3) */
5862 {
5863 unsigned char tmp[64];
5864 memcpy( tmp, ssl->handshake->randbytes, 64 );
5865 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5866 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5867 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5868 }
5869
5870 /* Populate transform structure */
5871 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5872 ssl->session_negotiate->ciphersuite,
5873 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005874#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005875 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005876#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005877 ssl->handshake->tls_prf,
5878 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005879 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005880 ssl->conf->endpoint,
5881 ssl );
5882 if( ret != 0 )
5883 {
5884 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5885 return( ret );
5886 }
5887
5888 /* We no longer need Server/ClientHello.random values */
5889 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5890 sizeof( ssl->handshake->randbytes ) );
5891
5892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5893
5894 return( 0 );
5895}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005896
Ronald Cron4dcbca92022-03-07 10:21:40 +01005897int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5898{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005899 switch( md )
5900 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005901#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005902 case MBEDTLS_SSL_HASH_SHA384:
5903 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5904 break;
5905#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005906#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005907 case MBEDTLS_SSL_HASH_SHA256:
5908 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5909 break;
5910#endif
5911 default:
5912 return( -1 );
5913 }
5914
Ronald Cronc2f13a02022-03-07 10:25:24 +01005915 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005916}
5917
Andrzej Kurek25f27152022-08-17 16:09:31 -04005918#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005919void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5920 unsigned char *hash,
5921 size_t *hlen )
5922{
5923#if defined(MBEDTLS_USE_PSA_CRYPTO)
5924 size_t hash_size;
5925 psa_status_t status;
5926 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5927
5928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5929 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5930 if( status != PSA_SUCCESS )
5931 {
5932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5933 return;
5934 }
5935
5936 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5937 if( status != PSA_SUCCESS )
5938 {
5939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5940 return;
5941 }
5942
5943 *hlen = 32;
5944 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5946#else
5947 mbedtls_sha256_context sha256;
5948
5949 mbedtls_sha256_init( &sha256 );
5950
5951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5952
5953 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5954 mbedtls_sha256_finish( &sha256, hash );
5955
5956 *hlen = 32;
5957
5958 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5960
5961 mbedtls_sha256_free( &sha256 );
5962#endif /* MBEDTLS_USE_PSA_CRYPTO */
5963 return;
5964}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005965#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08005966
Andrzej Kurek25f27152022-08-17 16:09:31 -04005967#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08005968void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5969 unsigned char *hash,
5970 size_t *hlen )
5971{
5972#if defined(MBEDTLS_USE_PSA_CRYPTO)
5973 size_t hash_size;
5974 psa_status_t status;
5975 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5976
5977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5978 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5979 if( status != PSA_SUCCESS )
5980 {
5981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5982 return;
5983 }
5984
5985 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5986 if( status != PSA_SUCCESS )
5987 {
5988 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5989 return;
5990 }
5991
5992 *hlen = 48;
5993 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5995#else
5996 mbedtls_sha512_context sha512;
5997
5998 mbedtls_sha512_init( &sha512 );
5999
6000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6001
Andrzej Kureka242e832022-08-11 10:03:14 -04006002 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006003 mbedtls_sha512_finish( &sha512, hash );
6004
6005 *hlen = 48;
6006
6007 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6009
6010 mbedtls_sha512_free( &sha512 );
6011#endif /* MBEDTLS_USE_PSA_CRYPTO */
6012 return;
6013}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006014#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006015
Neil Armstrong80f6f322022-05-03 17:56:38 +02006016#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6017 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006018int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6019{
6020 unsigned char *p = ssl->handshake->premaster;
6021 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6022 const unsigned char *psk = NULL;
6023 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006024 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006025
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006026 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006027 {
6028 /*
6029 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006030 * checked before calling this function.
6031 *
6032 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006033 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006034 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006035 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6036 {
6037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6038 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6039 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006040 }
6041
6042 /*
6043 * PMS = struct {
6044 * opaque other_secret<0..2^16-1>;
6045 * opaque psk<0..2^16-1>;
6046 * };
6047 * with "other_secret" depending on the particular key exchange
6048 */
6049#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6050 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6051 {
6052 if( end - p < 2 )
6053 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6054
6055 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6056 p += 2;
6057
6058 if( end < p || (size_t)( end - p ) < psk_len )
6059 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6060
6061 memset( p, 0, psk_len );
6062 p += psk_len;
6063 }
6064 else
6065#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6066#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6067 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6068 {
6069 /*
6070 * other_secret already set by the ClientKeyExchange message,
6071 * and is 48 bytes long
6072 */
6073 if( end - p < 2 )
6074 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6075
6076 *p++ = 0;
6077 *p++ = 48;
6078 p += 48;
6079 }
6080 else
6081#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6082#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6083 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6084 {
6085 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6086 size_t len;
6087
6088 /* Write length only when we know the actual value */
6089 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6090 p + 2, end - ( p + 2 ), &len,
6091 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6092 {
6093 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6094 return( ret );
6095 }
6096 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6097 p += 2 + len;
6098
6099 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6100 }
6101 else
6102#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006103#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006104 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6105 {
6106 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6107 size_t zlen;
6108
6109 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6110 p + 2, end - ( p + 2 ),
6111 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6112 {
6113 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6114 return( ret );
6115 }
6116
6117 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6118 p += 2 + zlen;
6119
6120 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6121 MBEDTLS_DEBUG_ECDH_Z );
6122 }
6123 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006124#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006125 {
6126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6127 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6128 }
6129
6130 /* opaque psk<0..2^16-1>; */
6131 if( end - p < 2 )
6132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6133
6134 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6135 p += 2;
6136
6137 if( end < p || (size_t)( end - p ) < psk_len )
6138 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6139
6140 memcpy( p, psk, psk_len );
6141 p += psk_len;
6142
6143 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6144
6145 return( 0 );
6146}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006147#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006148
6149#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006150MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006151static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6152
6153#if defined(MBEDTLS_SSL_PROTO_DTLS)
6154int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6155{
6156 /* If renegotiation is not enforced, retransmit until we would reach max
6157 * timeout if we were using the usual handshake doubling scheme */
6158 if( ssl->conf->renego_max_records < 0 )
6159 {
6160 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6161 unsigned char doublings = 1;
6162
6163 while( ratio != 0 )
6164 {
6165 ++doublings;
6166 ratio >>= 1;
6167 }
6168
6169 if( ++ssl->renego_records_seen > doublings )
6170 {
6171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6172 return( 0 );
6173 }
6174 }
6175
6176 return( ssl_write_hello_request( ssl ) );
6177}
6178#endif
6179#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006180
Jerry Yud9526692022-02-17 14:23:47 +08006181/*
6182 * Handshake functions
6183 */
6184#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6185/* No certificate support -> dummy functions */
6186int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6187{
6188 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6189 ssl->handshake->ciphersuite_info;
6190
6191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6192
6193 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6194 {
6195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6196 ssl->state++;
6197 return( 0 );
6198 }
6199
6200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6201 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6202}
6203
6204int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6205{
6206 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6207 ssl->handshake->ciphersuite_info;
6208
6209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6210
6211 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6212 {
6213 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6214 ssl->state++;
6215 return( 0 );
6216 }
6217
6218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6219 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6220}
6221
6222#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6223/* Some certificate support -> implement write and parse */
6224
6225int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6226{
6227 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6228 size_t i, n;
6229 const mbedtls_x509_crt *crt;
6230 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6231 ssl->handshake->ciphersuite_info;
6232
6233 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6234
6235 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6236 {
6237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6238 ssl->state++;
6239 return( 0 );
6240 }
6241
6242#if defined(MBEDTLS_SSL_CLI_C)
6243 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6244 {
6245 if( ssl->handshake->client_auth == 0 )
6246 {
6247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6248 ssl->state++;
6249 return( 0 );
6250 }
6251 }
6252#endif /* MBEDTLS_SSL_CLI_C */
6253#if defined(MBEDTLS_SSL_SRV_C)
6254 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6255 {
6256 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6257 {
6258 /* Should never happen because we shouldn't have picked the
6259 * ciphersuite if we don't have a certificate. */
6260 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6261 }
6262 }
6263#endif
6264
6265 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6266
6267 /*
6268 * 0 . 0 handshake type
6269 * 1 . 3 handshake length
6270 * 4 . 6 length of all certs
6271 * 7 . 9 length of cert. 1
6272 * 10 . n-1 peer certificate
6273 * n . n+2 length of cert. 2
6274 * n+3 . ... upper level cert, etc.
6275 */
6276 i = 7;
6277 crt = mbedtls_ssl_own_cert( ssl );
6278
6279 while( crt != NULL )
6280 {
6281 n = crt->raw.len;
6282 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6283 {
6284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6285 " > %" MBEDTLS_PRINTF_SIZET,
6286 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6287 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6288 }
6289
6290 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6291 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6292 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6293
6294 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6295 i += n; crt = crt->next;
6296 }
6297
6298 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6299 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6300 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6301
6302 ssl->out_msglen = i;
6303 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6304 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6305
6306 ssl->state++;
6307
6308 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6309 {
6310 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6311 return( ret );
6312 }
6313
6314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6315
6316 return( ret );
6317}
6318
6319#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6320
6321#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006322MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006323static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6324 unsigned char *crt_buf,
6325 size_t crt_buf_len )
6326{
6327 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6328
6329 if( peer_crt == NULL )
6330 return( -1 );
6331
6332 if( peer_crt->raw.len != crt_buf_len )
6333 return( -1 );
6334
6335 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6336}
6337#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006338MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006339static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6340 unsigned char *crt_buf,
6341 size_t crt_buf_len )
6342{
6343 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6344 unsigned char const * const peer_cert_digest =
6345 ssl->session->peer_cert_digest;
6346 mbedtls_md_type_t const peer_cert_digest_type =
6347 ssl->session->peer_cert_digest_type;
6348 mbedtls_md_info_t const * const digest_info =
6349 mbedtls_md_info_from_type( peer_cert_digest_type );
6350 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6351 size_t digest_len;
6352
6353 if( peer_cert_digest == NULL || digest_info == NULL )
6354 return( -1 );
6355
6356 digest_len = mbedtls_md_get_size( digest_info );
6357 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6358 return( -1 );
6359
6360 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6361 if( ret != 0 )
6362 return( -1 );
6363
6364 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6365}
6366#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6367#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6368
6369/*
6370 * Once the certificate message is read, parse it into a cert chain and
6371 * perform basic checks, but leave actual verification to the caller
6372 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006373MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006374static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6375 mbedtls_x509_crt *chain )
6376{
6377 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6378#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6379 int crt_cnt=0;
6380#endif
6381 size_t i, n;
6382 uint8_t alert;
6383
6384 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6385 {
6386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6387 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6388 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6389 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6390 }
6391
6392 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6393 {
6394 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6395 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6396 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6397 }
6398
6399 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6400 {
6401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6402 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6403 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6404 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6405 }
6406
6407 i = mbedtls_ssl_hs_hdr_len( ssl );
6408
6409 /*
6410 * Same message structure as in mbedtls_ssl_write_certificate()
6411 */
6412 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6413
6414 if( ssl->in_msg[i] != 0 ||
6415 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6416 {
6417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6418 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6419 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6420 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6421 }
6422
6423 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6424 i += 3;
6425
6426 /* Iterate through and parse the CRTs in the provided chain. */
6427 while( i < ssl->in_hslen )
6428 {
6429 /* Check that there's room for the next CRT's length fields. */
6430 if ( i + 3 > ssl->in_hslen ) {
6431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6432 mbedtls_ssl_send_alert_message( ssl,
6433 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6434 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6435 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6436 }
6437 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6438 * anything beyond 2**16 ~ 64K. */
6439 if( ssl->in_msg[i] != 0 )
6440 {
6441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6442 mbedtls_ssl_send_alert_message( ssl,
6443 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6444 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6445 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6446 }
6447
6448 /* Read length of the next CRT in the chain. */
6449 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6450 | (unsigned int) ssl->in_msg[i + 2];
6451 i += 3;
6452
6453 if( n < 128 || i + n > ssl->in_hslen )
6454 {
6455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6456 mbedtls_ssl_send_alert_message( ssl,
6457 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6458 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6459 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6460 }
6461
6462 /* Check if we're handling the first CRT in the chain. */
6463#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6464 if( crt_cnt++ == 0 &&
6465 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6466 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6467 {
6468 /* During client-side renegotiation, check that the server's
6469 * end-CRTs hasn't changed compared to the initial handshake,
6470 * mitigating the triple handshake attack. On success, reuse
6471 * the original end-CRT instead of parsing it again. */
6472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6473 if( ssl_check_peer_crt_unchanged( ssl,
6474 &ssl->in_msg[i],
6475 n ) != 0 )
6476 {
6477 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6478 mbedtls_ssl_send_alert_message( ssl,
6479 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6480 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6481 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6482 }
6483
6484 /* Now we can safely free the original chain. */
6485 ssl_clear_peer_cert( ssl->session );
6486 }
6487#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6488
6489 /* Parse the next certificate in the chain. */
6490#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6491 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6492#else
6493 /* If we don't need to store the CRT chain permanently, parse
6494 * it in-place from the input buffer instead of making a copy. */
6495 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6496#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6497 switch( ret )
6498 {
6499 case 0: /*ok*/
6500 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6501 /* Ignore certificate with an unknown algorithm: maybe a
6502 prior certificate was already trusted. */
6503 break;
6504
6505 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6506 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6507 goto crt_parse_der_failed;
6508
6509 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6510 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6511 goto crt_parse_der_failed;
6512
6513 default:
6514 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6515 crt_parse_der_failed:
6516 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6517 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6518 return( ret );
6519 }
6520
6521 i += n;
6522 }
6523
6524 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6525 return( 0 );
6526}
6527
6528#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006529MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006530static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6531{
6532 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6533 return( -1 );
6534
6535 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6536 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6537 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6538 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6539 {
Ronald Cron19385882022-06-15 16:26:13 +02006540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006541 return( 0 );
6542 }
6543 return( -1 );
6544}
6545#endif /* MBEDTLS_SSL_SRV_C */
6546
6547/* Check if a certificate message is expected.
6548 * Return either
6549 * - SSL_CERTIFICATE_EXPECTED, or
6550 * - SSL_CERTIFICATE_SKIP
6551 * indicating whether a Certificate message is expected or not.
6552 */
6553#define SSL_CERTIFICATE_EXPECTED 0
6554#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006555MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006556static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6557 int authmode )
6558{
6559 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6560 ssl->handshake->ciphersuite_info;
6561
6562 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6563 return( SSL_CERTIFICATE_SKIP );
6564
6565#if defined(MBEDTLS_SSL_SRV_C)
6566 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6567 {
6568 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6569 return( SSL_CERTIFICATE_SKIP );
6570
6571 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6572 {
6573 ssl->session_negotiate->verify_result =
6574 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6575 return( SSL_CERTIFICATE_SKIP );
6576 }
6577 }
6578#else
6579 ((void) authmode);
6580#endif /* MBEDTLS_SSL_SRV_C */
6581
6582 return( SSL_CERTIFICATE_EXPECTED );
6583}
6584
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006585MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006586static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6587 int authmode,
6588 mbedtls_x509_crt *chain,
6589 void *rs_ctx )
6590{
6591 int ret = 0;
6592 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6593 ssl->handshake->ciphersuite_info;
6594 int have_ca_chain = 0;
6595
6596 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6597 void *p_vrfy;
6598
6599 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6600 return( 0 );
6601
6602 if( ssl->f_vrfy != NULL )
6603 {
6604 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6605 f_vrfy = ssl->f_vrfy;
6606 p_vrfy = ssl->p_vrfy;
6607 }
6608 else
6609 {
6610 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6611 f_vrfy = ssl->conf->f_vrfy;
6612 p_vrfy = ssl->conf->p_vrfy;
6613 }
6614
6615 /*
6616 * Main check: verify certificate
6617 */
6618#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6619 if( ssl->conf->f_ca_cb != NULL )
6620 {
6621 ((void) rs_ctx);
6622 have_ca_chain = 1;
6623
6624 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6625 ret = mbedtls_x509_crt_verify_with_ca_cb(
6626 chain,
6627 ssl->conf->f_ca_cb,
6628 ssl->conf->p_ca_cb,
6629 ssl->conf->cert_profile,
6630 ssl->hostname,
6631 &ssl->session_negotiate->verify_result,
6632 f_vrfy, p_vrfy );
6633 }
6634 else
6635#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6636 {
6637 mbedtls_x509_crt *ca_chain;
6638 mbedtls_x509_crl *ca_crl;
6639
6640#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6641 if( ssl->handshake->sni_ca_chain != NULL )
6642 {
6643 ca_chain = ssl->handshake->sni_ca_chain;
6644 ca_crl = ssl->handshake->sni_ca_crl;
6645 }
6646 else
6647#endif
6648 {
6649 ca_chain = ssl->conf->ca_chain;
6650 ca_crl = ssl->conf->ca_crl;
6651 }
6652
6653 if( ca_chain != NULL )
6654 have_ca_chain = 1;
6655
6656 ret = mbedtls_x509_crt_verify_restartable(
6657 chain,
6658 ca_chain, ca_crl,
6659 ssl->conf->cert_profile,
6660 ssl->hostname,
6661 &ssl->session_negotiate->verify_result,
6662 f_vrfy, p_vrfy, rs_ctx );
6663 }
6664
6665 if( ret != 0 )
6666 {
6667 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6668 }
6669
6670#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6671 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6672 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6673#endif
6674
6675 /*
6676 * Secondary checks: always done, but change 'ret' only if it was 0
6677 */
6678
6679#if defined(MBEDTLS_ECP_C)
6680 {
6681 const mbedtls_pk_context *pk = &chain->pk;
6682
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006683 /* If certificate uses an EC key, make sure the curve is OK.
6684 * This is a public key, so it can't be opaque, so can_do() is a good
6685 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006686 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006687 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006688 /* and in the unlikely case the above assumption no longer holds
6689 * we are making sure that pk_ec() here does not return a NULL
6690 */
6691 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6692 if( ec == NULL )
6693 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006695 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6696 }
Jerry Yud9526692022-02-17 14:23:47 +08006697
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006698 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6699 {
6700 ssl->session_negotiate->verify_result |=
6701 MBEDTLS_X509_BADCERT_BAD_KEY;
6702
6703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6704 if( ret == 0 )
6705 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6706 }
Jerry Yud9526692022-02-17 14:23:47 +08006707 }
6708 }
6709#endif /* MBEDTLS_ECP_C */
6710
6711 if( mbedtls_ssl_check_cert_usage( chain,
6712 ciphersuite_info,
6713 ! ssl->conf->endpoint,
6714 &ssl->session_negotiate->verify_result ) != 0 )
6715 {
6716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6717 if( ret == 0 )
6718 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6719 }
6720
6721 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6722 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6723 * with details encoded in the verification flags. All other kinds
6724 * of error codes, including those from the user provided f_vrfy
6725 * functions, are treated as fatal and lead to a failure of
6726 * ssl_parse_certificate even if verification was optional. */
6727 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6728 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6729 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6730 {
6731 ret = 0;
6732 }
6733
6734 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6735 {
6736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6737 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6738 }
6739
6740 if( ret != 0 )
6741 {
6742 uint8_t alert;
6743
6744 /* The certificate may have been rejected for several reasons.
6745 Pick one and send the corresponding alert. Which alert to send
6746 may be a subject of debate in some cases. */
6747 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6748 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6749 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6750 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6751 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6752 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6753 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6754 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6755 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6756 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6757 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6758 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6759 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6760 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6761 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6762 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6763 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6764 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6765 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6766 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6767 else
6768 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6769 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6770 alert );
6771 }
6772
6773#if defined(MBEDTLS_DEBUG_C)
6774 if( ssl->session_negotiate->verify_result != 0 )
6775 {
6776 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6777 (unsigned int) ssl->session_negotiate->verify_result ) );
6778 }
6779 else
6780 {
6781 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6782 }
6783#endif /* MBEDTLS_DEBUG_C */
6784
6785 return( ret );
6786}
6787
6788#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006789MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006790static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6791 unsigned char *start, size_t len )
6792{
6793 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6794 /* Remember digest of the peer's end-CRT. */
6795 ssl->session_negotiate->peer_cert_digest =
6796 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6797 if( ssl->session_negotiate->peer_cert_digest == NULL )
6798 {
6799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6800 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6801 mbedtls_ssl_send_alert_message( ssl,
6802 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6803 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6804
6805 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6806 }
6807
6808 ret = mbedtls_md( mbedtls_md_info_from_type(
6809 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6810 start, len,
6811 ssl->session_negotiate->peer_cert_digest );
6812
6813 ssl->session_negotiate->peer_cert_digest_type =
6814 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6815 ssl->session_negotiate->peer_cert_digest_len =
6816 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6817
6818 return( ret );
6819}
6820
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006821MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006822static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6823 unsigned char *start, size_t len )
6824{
6825 unsigned char *end = start + len;
6826 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6827
6828 /* Make a copy of the peer's raw public key. */
6829 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6830 ret = mbedtls_pk_parse_subpubkey( &start, end,
6831 &ssl->handshake->peer_pubkey );
6832 if( ret != 0 )
6833 {
6834 /* We should have parsed the public key before. */
6835 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6836 }
6837
6838 return( 0 );
6839}
6840#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6841
6842int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6843{
6844 int ret = 0;
6845 int crt_expected;
6846#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6847 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6848 ? ssl->handshake->sni_authmode
6849 : ssl->conf->authmode;
6850#else
6851 const int authmode = ssl->conf->authmode;
6852#endif
6853 void *rs_ctx = NULL;
6854 mbedtls_x509_crt *chain = NULL;
6855
6856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6857
6858 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6859 if( crt_expected == SSL_CERTIFICATE_SKIP )
6860 {
6861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6862 goto exit;
6863 }
6864
6865#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6866 if( ssl->handshake->ecrs_enabled &&
6867 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6868 {
6869 chain = ssl->handshake->ecrs_peer_cert;
6870 ssl->handshake->ecrs_peer_cert = NULL;
6871 goto crt_verify;
6872 }
6873#endif
6874
6875 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6876 {
6877 /* mbedtls_ssl_read_record may have sent an alert already. We
6878 let it decide whether to alert. */
6879 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6880 goto exit;
6881 }
6882
6883#if defined(MBEDTLS_SSL_SRV_C)
6884 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6885 {
6886 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6887
6888 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6889 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6890
6891 goto exit;
6892 }
6893#endif /* MBEDTLS_SSL_SRV_C */
6894
6895 /* Clear existing peer CRT structure in case we tried to
6896 * reuse a session but it failed, and allocate a new one. */
6897 ssl_clear_peer_cert( ssl->session_negotiate );
6898
6899 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6900 if( chain == NULL )
6901 {
6902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6903 sizeof( mbedtls_x509_crt ) ) );
6904 mbedtls_ssl_send_alert_message( ssl,
6905 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6906 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6907
6908 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6909 goto exit;
6910 }
6911 mbedtls_x509_crt_init( chain );
6912
6913 ret = ssl_parse_certificate_chain( ssl, chain );
6914 if( ret != 0 )
6915 goto exit;
6916
6917#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6918 if( ssl->handshake->ecrs_enabled)
6919 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6920
6921crt_verify:
6922 if( ssl->handshake->ecrs_enabled)
6923 rs_ctx = &ssl->handshake->ecrs_ctx;
6924#endif
6925
6926 ret = ssl_parse_certificate_verify( ssl, authmode,
6927 chain, rs_ctx );
6928 if( ret != 0 )
6929 goto exit;
6930
6931#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6932 {
6933 unsigned char *crt_start, *pk_start;
6934 size_t crt_len, pk_len;
6935
6936 /* We parse the CRT chain without copying, so
6937 * these pointers point into the input buffer,
6938 * and are hence still valid after freeing the
6939 * CRT chain. */
6940
6941 crt_start = chain->raw.p;
6942 crt_len = chain->raw.len;
6943
6944 pk_start = chain->pk_raw.p;
6945 pk_len = chain->pk_raw.len;
6946
6947 /* Free the CRT structures before computing
6948 * digest and copying the peer's public key. */
6949 mbedtls_x509_crt_free( chain );
6950 mbedtls_free( chain );
6951 chain = NULL;
6952
6953 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6954 if( ret != 0 )
6955 goto exit;
6956
6957 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6958 if( ret != 0 )
6959 goto exit;
6960 }
6961#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6962 /* Pass ownership to session structure. */
6963 ssl->session_negotiate->peer_cert = chain;
6964 chain = NULL;
6965#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6966
6967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6968
6969exit:
6970
6971 if( ret == 0 )
6972 ssl->state++;
6973
6974#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6975 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6976 {
6977 ssl->handshake->ecrs_peer_cert = chain;
6978 chain = NULL;
6979 }
6980#endif
6981
6982 if( chain != NULL )
6983 {
6984 mbedtls_x509_crt_free( chain );
6985 mbedtls_free( chain );
6986 }
6987
6988 return( ret );
6989}
6990#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6991
Andrzej Kurek25f27152022-08-17 16:09:31 -04006992#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08006993static void ssl_calc_finished_tls_sha256(
6994 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6995{
6996 int len = 12;
6997 const char *sender;
6998 unsigned char padbuf[32];
6999#if defined(MBEDTLS_USE_PSA_CRYPTO)
7000 size_t hash_size;
7001 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7002 psa_status_t status;
7003#else
7004 mbedtls_sha256_context sha256;
7005#endif
7006
7007 mbedtls_ssl_session *session = ssl->session_negotiate;
7008 if( !session )
7009 session = ssl->session;
7010
7011 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7012 ? "client finished"
7013 : "server finished";
7014
7015#if defined(MBEDTLS_USE_PSA_CRYPTO)
7016 sha256_psa = psa_hash_operation_init();
7017
7018 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7019
7020 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7021 if( status != PSA_SUCCESS )
7022 {
7023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7024 return;
7025 }
7026
7027 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7028 if( status != PSA_SUCCESS )
7029 {
7030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7031 return;
7032 }
7033 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7034#else
7035
7036 mbedtls_sha256_init( &sha256 );
7037
7038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7039
7040 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7041
7042 /*
7043 * TLSv1.2:
7044 * hash = PRF( master, finished_label,
7045 * Hash( handshake ) )[0.11]
7046 */
7047
7048#if !defined(MBEDTLS_SHA256_ALT)
7049 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7050 sha256.state, sizeof( sha256.state ) );
7051#endif
7052
7053 mbedtls_sha256_finish( &sha256, padbuf );
7054 mbedtls_sha256_free( &sha256 );
7055#endif /* MBEDTLS_USE_PSA_CRYPTO */
7056
7057 ssl->handshake->tls_prf( session->master, 48, sender,
7058 padbuf, 32, buf, len );
7059
7060 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7061
7062 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7063
7064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7065}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007066#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007067
Jerry Yub7ba49e2022-02-17 14:25:53 +08007068
Andrzej Kurek25f27152022-08-17 16:09:31 -04007069#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007070static void ssl_calc_finished_tls_sha384(
7071 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7072{
7073 int len = 12;
7074 const char *sender;
7075 unsigned char padbuf[48];
7076#if defined(MBEDTLS_USE_PSA_CRYPTO)
7077 size_t hash_size;
7078 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7079 psa_status_t status;
7080#else
7081 mbedtls_sha512_context sha512;
7082#endif
7083
7084 mbedtls_ssl_session *session = ssl->session_negotiate;
7085 if( !session )
7086 session = ssl->session;
7087
7088 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7089 ? "client finished"
7090 : "server finished";
7091
7092#if defined(MBEDTLS_USE_PSA_CRYPTO)
7093 sha384_psa = psa_hash_operation_init();
7094
7095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7096
7097 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7098 if( status != PSA_SUCCESS )
7099 {
7100 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7101 return;
7102 }
7103
7104 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7105 if( status != PSA_SUCCESS )
7106 {
7107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7108 return;
7109 }
7110 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7111#else
7112 mbedtls_sha512_init( &sha512 );
7113
7114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7115
Andrzej Kureka242e832022-08-11 10:03:14 -04007116 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007117
7118 /*
7119 * TLSv1.2:
7120 * hash = PRF( master, finished_label,
7121 * Hash( handshake ) )[0.11]
7122 */
7123
7124#if !defined(MBEDTLS_SHA512_ALT)
7125 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7126 sha512.state, sizeof( sha512.state ) );
7127#endif
7128 mbedtls_sha512_finish( &sha512, padbuf );
7129
7130 mbedtls_sha512_free( &sha512 );
7131#endif
7132
7133 ssl->handshake->tls_prf( session->master, 48, sender,
7134 padbuf, 48, buf, len );
7135
7136 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7137
7138 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7139
7140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7141}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007142#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007143
Jerry Yuaef00152022-02-17 14:27:31 +08007144void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7145{
7146 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7147
7148 /*
7149 * Free our handshake params
7150 */
7151 mbedtls_ssl_handshake_free( ssl );
7152 mbedtls_free( ssl->handshake );
7153 ssl->handshake = NULL;
7154
7155 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007156 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007157 */
7158 if( ssl->transform )
7159 {
7160 mbedtls_ssl_transform_free( ssl->transform );
7161 mbedtls_free( ssl->transform );
7162 }
7163 ssl->transform = ssl->transform_negotiate;
7164 ssl->transform_negotiate = NULL;
7165
7166 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7167}
7168
Jerry Yu2a9fff52022-02-17 14:28:51 +08007169void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7170{
7171 int resume = ssl->handshake->resume;
7172
7173 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7174
7175#if defined(MBEDTLS_SSL_RENEGOTIATION)
7176 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7177 {
7178 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7179 ssl->renego_records_seen = 0;
7180 }
7181#endif
7182
7183 /*
7184 * Free the previous session and switch in the current one
7185 */
7186 if( ssl->session )
7187 {
7188#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7189 /* RFC 7366 3.1: keep the EtM state */
7190 ssl->session_negotiate->encrypt_then_mac =
7191 ssl->session->encrypt_then_mac;
7192#endif
7193
7194 mbedtls_ssl_session_free( ssl->session );
7195 mbedtls_free( ssl->session );
7196 }
7197 ssl->session = ssl->session_negotiate;
7198 ssl->session_negotiate = NULL;
7199
7200 /*
7201 * Add cache entry
7202 */
7203 if( ssl->conf->f_set_cache != NULL &&
7204 ssl->session->id_len != 0 &&
7205 resume == 0 )
7206 {
7207 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7208 ssl->session->id,
7209 ssl->session->id_len,
7210 ssl->session ) != 0 )
7211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7212 }
7213
7214#if defined(MBEDTLS_SSL_PROTO_DTLS)
7215 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7216 ssl->handshake->flight != NULL )
7217 {
7218 /* Cancel handshake timer */
7219 mbedtls_ssl_set_timer( ssl, 0 );
7220
7221 /* Keep last flight around in case we need to resend it:
7222 * we need the handshake and transform structures for that */
7223 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7224 }
7225 else
7226#endif
7227 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7228
7229 ssl->state++;
7230
7231 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7232}
7233
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007234int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7235{
7236 int ret, hash_len;
7237
7238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7239
7240 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7241
7242 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7243
7244 /*
7245 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7246 * may define some other value. Currently (early 2016), no defined
7247 * ciphersuite does this (and this is unlikely to change as activity has
7248 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7249 */
7250 hash_len = 12;
7251
7252#if defined(MBEDTLS_SSL_RENEGOTIATION)
7253 ssl->verify_data_len = hash_len;
7254 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7255#endif
7256
7257 ssl->out_msglen = 4 + hash_len;
7258 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7259 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7260
7261 /*
7262 * In case of session resuming, invert the client and server
7263 * ChangeCipherSpec messages order.
7264 */
7265 if( ssl->handshake->resume != 0 )
7266 {
7267#if defined(MBEDTLS_SSL_CLI_C)
7268 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7269 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7270#endif
7271#if defined(MBEDTLS_SSL_SRV_C)
7272 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7273 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7274#endif
7275 }
7276 else
7277 ssl->state++;
7278
7279 /*
7280 * Switch to our negotiated transform and session parameters for outbound
7281 * data.
7282 */
7283 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7284
7285#if defined(MBEDTLS_SSL_PROTO_DTLS)
7286 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7287 {
7288 unsigned char i;
7289
7290 /* Remember current epoch settings for resending */
7291 ssl->handshake->alt_transform_out = ssl->transform_out;
7292 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7293 sizeof( ssl->handshake->alt_out_ctr ) );
7294
7295 /* Set sequence_number to zero */
7296 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7297
7298
7299 /* Increment epoch */
7300 for( i = 2; i > 0; i-- )
7301 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7302 break;
7303
7304 /* The loop goes to its end iff the counter is wrapping */
7305 if( i == 0 )
7306 {
7307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7308 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7309 }
7310 }
7311 else
7312#endif /* MBEDTLS_SSL_PROTO_DTLS */
7313 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7314
7315 ssl->transform_out = ssl->transform_negotiate;
7316 ssl->session_out = ssl->session_negotiate;
7317
7318#if defined(MBEDTLS_SSL_PROTO_DTLS)
7319 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7320 mbedtls_ssl_send_flight_completed( ssl );
7321#endif
7322
7323 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7324 {
7325 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7326 return( ret );
7327 }
7328
7329#if defined(MBEDTLS_SSL_PROTO_DTLS)
7330 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7331 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7332 {
7333 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7334 return( ret );
7335 }
7336#endif
7337
7338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7339
7340 return( 0 );
7341}
7342
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007343#define SSL_MAX_HASH_LEN 12
7344
7345int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7346{
7347 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7348 unsigned int hash_len = 12;
7349 unsigned char buf[SSL_MAX_HASH_LEN];
7350
7351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7352
7353 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7354
7355 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7356 {
7357 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7358 goto exit;
7359 }
7360
7361 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7362 {
7363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7364 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7365 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7366 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7367 goto exit;
7368 }
7369
7370 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7371 {
7372 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7373 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7374 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7375 goto exit;
7376 }
7377
7378 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7379 {
7380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7381 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7382 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7383 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7384 goto exit;
7385 }
7386
7387 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7388 buf, hash_len ) != 0 )
7389 {
7390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7391 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7392 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7393 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7394 goto exit;
7395 }
7396
7397#if defined(MBEDTLS_SSL_RENEGOTIATION)
7398 ssl->verify_data_len = hash_len;
7399 memcpy( ssl->peer_verify_data, buf, hash_len );
7400#endif
7401
7402 if( ssl->handshake->resume != 0 )
7403 {
7404#if defined(MBEDTLS_SSL_CLI_C)
7405 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7406 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7407#endif
7408#if defined(MBEDTLS_SSL_SRV_C)
7409 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7410 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7411#endif
7412 }
7413 else
7414 ssl->state++;
7415
7416#if defined(MBEDTLS_SSL_PROTO_DTLS)
7417 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7418 mbedtls_ssl_recv_flight_completed( ssl );
7419#endif
7420
7421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7422
7423exit:
7424 mbedtls_platform_zeroize( buf, hash_len );
7425 return( ret );
7426}
7427
Jerry Yu392112c2022-02-17 14:34:10 +08007428#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7429/*
7430 * Helper to get TLS 1.2 PRF from ciphersuite
7431 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7432 */
7433static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7434{
Andrzej Kurek25f27152022-08-17 16:09:31 -04007435#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu392112c2022-02-17 14:34:10 +08007436 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7437 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7438
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007439 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007440 return( tls_prf_sha384 );
7441#else
7442 (void) ciphersuite_id;
7443#endif
7444 return( tls_prf_sha256 );
7445}
7446#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007447
7448static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7449{
7450 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007451#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007452 if( tls_prf == tls_prf_sha384 )
7453 {
7454 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7455 }
7456 else
7457#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007458#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007459 if( tls_prf == tls_prf_sha256 )
7460 {
7461 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7462 }
7463 else
7464#endif
7465 return( MBEDTLS_SSL_TLS_PRF_NONE );
7466}
7467
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007468/*
7469 * Populate a transform structure with session keys and all the other
7470 * necessary information.
7471 *
7472 * Parameters:
7473 * - [in/out]: transform: structure to populate
7474 * [in] must be just initialised with mbedtls_ssl_transform_init()
7475 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7476 * - [in] ciphersuite
7477 * - [in] master
7478 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007479 * - [in] tls_prf: pointer to PRF to use for key derivation
7480 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007481 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007482 * - [in] endpoint: client or server
7483 * - [in] ssl: used for:
7484 * - ssl->conf->{f,p}_export_keys
7485 * [in] optionally used for:
7486 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7487 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007488MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007489static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7490 int ciphersuite,
7491 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007492#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007493 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007494#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007495 ssl_tls_prf_t tls_prf,
7496 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007497 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007498 unsigned endpoint,
7499 const mbedtls_ssl_context *ssl )
7500{
7501 int ret = 0;
7502 unsigned char keyblk[256];
7503 unsigned char *key1;
7504 unsigned char *key2;
7505 unsigned char *mac_enc;
7506 unsigned char *mac_dec;
7507 size_t mac_key_len = 0;
7508 size_t iv_copy_len;
7509 size_t keylen;
7510 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007511 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007512#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007513 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007514 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007515#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007516
7517#if defined(MBEDTLS_USE_PSA_CRYPTO)
7518 psa_key_type_t key_type;
7519 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7520 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007521 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007522 size_t key_bits;
7523 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7524#endif
7525
7526#if !defined(MBEDTLS_DEBUG_C) && \
7527 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7528 if( ssl->f_export_keys == NULL )
7529 {
7530 ssl = NULL; /* make sure we don't use it except for these cases */
7531 (void) ssl;
7532 }
7533#endif
7534
7535 /*
7536 * Some data just needs copying into the structure
7537 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007538#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007539 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007540#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007541 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007542
7543#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7544 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7545#endif
7546
7547#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007548 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007549 {
7550 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7551 * generation separate. This should never happen. */
7552 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7553 }
7554#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7555
7556 /*
7557 * Get various info structures
7558 */
7559 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7560 if( ciphersuite_info == NULL )
7561 {
7562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7563 ciphersuite ) );
7564 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7565 }
7566
Neil Armstrongab555e02022-04-04 11:07:59 +02007567 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007568#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007569 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007570#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007571 ciphersuite_info );
7572
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007573 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7574 transform->taglen =
7575 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7576
7577#if defined(MBEDTLS_USE_PSA_CRYPTO)
7578 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7579 transform->taglen,
7580 &alg,
7581 &key_type,
7582 &key_bits ) ) != PSA_SUCCESS )
7583 {
7584 ret = psa_ssl_status_to_mbedtls( status );
7585 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7586 goto end;
7587 }
7588#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007589 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7590 if( cipher_info == NULL )
7591 {
7592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7593 ciphersuite_info->cipher ) );
7594 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7595 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007596#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007597
Neil Armstronge4512952022-03-08 09:08:22 +01007598#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007599 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007600 if( mac_alg == 0 )
7601 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007603 (unsigned) ciphersuite_info->mac ) );
7604 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7605 }
7606#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007607 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7608 if( md_info == NULL )
7609 {
7610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7611 (unsigned) ciphersuite_info->mac ) );
7612 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7613 }
Neil Armstronge4512952022-03-08 09:08:22 +01007614#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007615
7616#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7617 /* Copy own and peer's CID if the use of the CID
7618 * extension has been negotiated. */
7619 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7620 {
7621 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7622
7623 transform->in_cid_len = ssl->own_cid_len;
7624 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7625 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7626 transform->in_cid_len );
7627
7628 transform->out_cid_len = ssl->handshake->peer_cid_len;
7629 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7630 ssl->handshake->peer_cid_len );
7631 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7632 transform->out_cid_len );
7633 }
7634#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7635
7636 /*
7637 * Compute key block using the PRF
7638 */
7639 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7640 if( ret != 0 )
7641 {
7642 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7643 return( ret );
7644 }
7645
7646 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7647 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7648 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7649 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7650 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7651
7652 /*
7653 * Determine the appropriate key, IV and MAC length.
7654 */
7655
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007656#if defined(MBEDTLS_USE_PSA_CRYPTO)
7657 keylen = PSA_BITS_TO_BYTES(key_bits);
7658#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007659 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007660#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007661
7662#if defined(MBEDTLS_GCM_C) || \
7663 defined(MBEDTLS_CCM_C) || \
7664 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007665 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007666 {
7667 size_t explicit_ivlen;
7668
7669 transform->maclen = 0;
7670 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007671
7672 /* All modes haves 96-bit IVs, but the length of the static parts vary
7673 * with mode and version:
7674 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7675 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7676 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7677 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7678 * sequence number).
7679 */
7680 transform->ivlen = 12;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007681#if defined(MBEDTLS_USE_PSA_CRYPTO)
7682 if( key_type == PSA_KEY_TYPE_CHACHA20 )
7683#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007684 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007685#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007686 transform->fixed_ivlen = 12;
7687 else
7688 transform->fixed_ivlen = 4;
7689
7690 /* Minimum length of encrypted record */
7691 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7692 transform->minlen = explicit_ivlen + transform->taglen;
7693 }
7694 else
7695#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7696#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007697 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7698 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7699 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007700 {
Neil Armstronge4512952022-03-08 09:08:22 +01007701#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007702 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007703#else
7704 size_t block_size = cipher_info->block_size;
7705#endif /* MBEDTLS_USE_PSA_CRYPTO */
7706
7707#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007708 /* Get MAC length */
7709 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7710#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007711 /* Initialize HMAC contexts */
7712 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7713 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7714 {
7715 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7716 goto end;
7717 }
7718
7719 /* Get MAC length */
7720 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007721#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007722 transform->maclen = mac_key_len;
7723
7724 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007725#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007726 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007727#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007728 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007729#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007730
7731 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007732 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007733 transform->minlen = transform->maclen;
7734 else
7735 {
7736 /*
7737 * GenericBlockCipher:
7738 * 1. if EtM is in use: one block plus MAC
7739 * otherwise: * first multiple of blocklen greater than maclen
7740 * 2. IV
7741 */
7742#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007743 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007744 {
7745 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007746 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007747 }
7748 else
7749#endif
7750 {
7751 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007752 + block_size
7753 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007754 }
7755
Glenn Strauss07c64162022-03-14 12:34:51 -04007756 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007757 {
7758 transform->minlen += transform->ivlen;
7759 }
7760 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007761 {
7762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7763 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7764 goto end;
7765 }
7766 }
7767 }
7768 else
7769#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7770 {
7771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7772 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7773 }
7774
7775 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7776 (unsigned) keylen,
7777 (unsigned) transform->minlen,
7778 (unsigned) transform->ivlen,
7779 (unsigned) transform->maclen ) );
7780
7781 /*
7782 * Finally setup the cipher contexts, IVs and MAC secrets.
7783 */
7784#if defined(MBEDTLS_SSL_CLI_C)
7785 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7786 {
7787 key1 = keyblk + mac_key_len * 2;
7788 key2 = keyblk + mac_key_len * 2 + keylen;
7789
7790 mac_enc = keyblk;
7791 mac_dec = keyblk + mac_key_len;
7792
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007793 iv_copy_len = ( transform->fixed_ivlen ) ?
7794 transform->fixed_ivlen : transform->ivlen;
7795 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7796 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7797 iv_copy_len );
7798 }
7799 else
7800#endif /* MBEDTLS_SSL_CLI_C */
7801#if defined(MBEDTLS_SSL_SRV_C)
7802 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7803 {
7804 key1 = keyblk + mac_key_len * 2 + keylen;
7805 key2 = keyblk + mac_key_len * 2;
7806
7807 mac_enc = keyblk + mac_key_len;
7808 mac_dec = keyblk;
7809
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007810 iv_copy_len = ( transform->fixed_ivlen ) ?
7811 transform->fixed_ivlen : transform->ivlen;
7812 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7813 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7814 iv_copy_len );
7815 }
7816 else
7817#endif /* MBEDTLS_SSL_SRV_C */
7818 {
7819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7820 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7821 goto end;
7822 }
7823
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007824 if( ssl != NULL && ssl->f_export_keys != NULL )
7825 {
7826 ssl->f_export_keys( ssl->p_export_keys,
7827 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7828 master, 48,
7829 randbytes + 32,
7830 randbytes,
7831 tls_prf_get_type( tls_prf ) );
7832 }
7833
7834#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007835 transform->psa_alg = alg;
7836
7837 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7838 {
7839 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7840 psa_set_key_algorithm( &attributes, alg );
7841 psa_set_key_type( &attributes, key_type );
7842
7843 if( ( status = psa_import_key( &attributes,
7844 key1,
7845 PSA_BITS_TO_BYTES( key_bits ),
7846 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7847 {
7848 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7849 ret = psa_ssl_status_to_mbedtls( status );
7850 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7851 goto end;
7852 }
7853
7854 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7855
7856 if( ( status = psa_import_key( &attributes,
7857 key2,
7858 PSA_BITS_TO_BYTES( key_bits ),
7859 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7860 {
7861 ret = psa_ssl_status_to_mbedtls( status );
7862 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7863 goto end;
7864 }
7865 }
7866#else
7867 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7868 cipher_info ) ) != 0 )
7869 {
7870 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7871 goto end;
7872 }
7873
7874 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7875 cipher_info ) ) != 0 )
7876 {
7877 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7878 goto end;
7879 }
7880
7881 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7882 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7883 MBEDTLS_ENCRYPT ) ) != 0 )
7884 {
7885 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7886 goto end;
7887 }
7888
7889 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7890 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7891 MBEDTLS_DECRYPT ) ) != 0 )
7892 {
7893 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7894 goto end;
7895 }
7896
7897#if defined(MBEDTLS_CIPHER_MODE_CBC)
7898 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7899 {
7900 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7901 MBEDTLS_PADDING_NONE ) ) != 0 )
7902 {
7903 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7904 goto end;
7905 }
7906
7907 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7908 MBEDTLS_PADDING_NONE ) ) != 0 )
7909 {
7910 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7911 goto end;
7912 }
7913 }
7914#endif /* MBEDTLS_CIPHER_MODE_CBC */
7915#endif /* MBEDTLS_USE_PSA_CRYPTO */
7916
Neil Armstrong29c0c042022-03-17 17:47:28 +01007917#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7918 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7919 For AEAD-based ciphersuites, there is nothing to do here. */
7920 if( mac_key_len != 0 )
7921 {
7922#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007923 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007924
7925 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007926 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007927 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7928
7929 if( ( status = psa_import_key( &attributes,
7930 mac_enc, mac_key_len,
7931 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7932 {
7933 ret = psa_ssl_status_to_mbedtls( status );
7934 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7935 goto end;
7936 }
7937
Ronald Cronfb39f152022-03-25 14:36:28 +01007938 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04007939 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
7940#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
7941 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
7942#endif
7943 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007944 /* mbedtls_ct_hmac() requires the key to be exportable */
7945 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7946 PSA_KEY_USAGE_VERIFY_HASH );
7947 else
7948 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7949
7950 if( ( status = psa_import_key( &attributes,
7951 mac_dec, mac_key_len,
7952 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7953 {
7954 ret = psa_ssl_status_to_mbedtls( status );
7955 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7956 goto end;
7957 }
7958#else
7959 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7960 if( ret != 0 )
7961 goto end;
7962 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7963 if( ret != 0 )
7964 goto end;
7965#endif /* MBEDTLS_USE_PSA_CRYPTO */
7966 }
7967#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7968
7969 ((void) mac_dec);
7970 ((void) mac_enc);
7971
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007972end:
7973 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7974 return( ret );
7975}
7976
Jerry Yuee40f9d2022-02-17 14:55:16 +08007977#if defined(MBEDTLS_USE_PSA_CRYPTO)
7978int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7979 unsigned char *hash, size_t *hashlen,
7980 unsigned char *data, size_t data_len,
7981 mbedtls_md_type_t md_alg )
7982{
7983 psa_status_t status;
7984 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007985 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08007986
7987 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7988
7989 if( ( status = psa_hash_setup( &hash_operation,
7990 hash_alg ) ) != PSA_SUCCESS )
7991 {
7992 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7993 goto exit;
7994 }
7995
7996 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
7997 64 ) ) != PSA_SUCCESS )
7998 {
7999 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8000 goto exit;
8001 }
8002
8003 if( ( status = psa_hash_update( &hash_operation,
8004 data, data_len ) ) != PSA_SUCCESS )
8005 {
8006 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8007 goto exit;
8008 }
8009
8010 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8011 hashlen ) ) != PSA_SUCCESS )
8012 {
8013 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8014 goto exit;
8015 }
8016
8017exit:
8018 if( status != PSA_SUCCESS )
8019 {
8020 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8021 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8022 switch( status )
8023 {
8024 case PSA_ERROR_NOT_SUPPORTED:
8025 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8026 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8027 case PSA_ERROR_BUFFER_TOO_SMALL:
8028 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8029 case PSA_ERROR_INSUFFICIENT_MEMORY:
8030 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8031 default:
8032 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8033 }
8034 }
8035 return( 0 );
8036}
8037
8038#else
8039
8040int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8041 unsigned char *hash, size_t *hashlen,
8042 unsigned char *data, size_t data_len,
8043 mbedtls_md_type_t md_alg )
8044{
8045 int ret = 0;
8046 mbedtls_md_context_t ctx;
8047 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8048 *hashlen = mbedtls_md_get_size( md_info );
8049
8050 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8051
8052 mbedtls_md_init( &ctx );
8053
8054 /*
8055 * digitally-signed struct {
8056 * opaque client_random[32];
8057 * opaque server_random[32];
8058 * ServerDHParams params;
8059 * };
8060 */
8061 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8062 {
8063 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8064 goto exit;
8065 }
8066 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8067 {
8068 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8069 goto exit;
8070 }
8071 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8072 {
8073 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8074 goto exit;
8075 }
8076 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8077 {
8078 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8079 goto exit;
8080 }
8081 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8082 {
8083 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8084 goto exit;
8085 }
8086
8087exit:
8088 mbedtls_md_free( &ctx );
8089
8090 if( ret != 0 )
8091 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8092 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8093
8094 return( ret );
8095}
8096#endif /* MBEDTLS_USE_PSA_CRYPTO */
8097
Jerry Yud9d91da2022-02-17 14:57:06 +08008098#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8099
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008100/* Find the preferred hash for a given signature algorithm. */
8101unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8102 mbedtls_ssl_context *ssl,
8103 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008104{
Gabor Mezei078e8032022-04-27 21:17:56 +02008105 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008106 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008107
8108 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008109 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008110
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008111 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008112 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008113 unsigned int hash_alg_received =
8114 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8115 received_sig_algs[i] );
8116 unsigned int sig_alg_received =
8117 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8118 received_sig_algs[i] );
8119
8120 if( sig_alg == sig_alg_received )
8121 {
8122#if defined(MBEDTLS_USE_PSA_CRYPTO)
8123 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8124 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008125 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008126 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008127
Neil Armstrong96eceb82022-06-30 18:05:05 +02008128 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8129 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8130 PSA_ALG_ECDSA( psa_hash_alg ),
8131 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008132 continue;
8133
Neil Armstrong96eceb82022-06-30 18:05:05 +02008134 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008135 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8136 PSA_ALG_RSA_PKCS1V15_SIGN(
8137 psa_hash_alg ),
8138 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008139 continue;
8140 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008141#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008142
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008143 return( hash_alg_received );
8144 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008145 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008146
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008147 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008148}
8149
8150#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8151
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008152/* Serialization of TLS 1.2 sessions:
8153 *
8154 * struct {
8155 * uint64 start_time;
8156 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008157 * uint8 session_id_len; // at most 32
8158 * opaque session_id[32];
8159 * opaque master[48]; // fixed length in the standard
8160 * uint32 verify_result;
8161 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8162 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8163 * uint32 ticket_lifetime;
8164 * uint8 mfl_code; // up to 255 according to standard
8165 * uint8 encrypt_then_mac; // 0 or 1
8166 * } serialized_session_tls12;
8167 *
8168 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008169static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008170 unsigned char *buf,
8171 size_t buf_len )
8172{
8173 unsigned char *p = buf;
8174 size_t used = 0;
8175
8176#if defined(MBEDTLS_HAVE_TIME)
8177 uint64_t start;
8178#endif
8179#if defined(MBEDTLS_X509_CRT_PARSE_C)
8180#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8181 size_t cert_len;
8182#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8183#endif /* MBEDTLS_X509_CRT_PARSE_C */
8184
8185 /*
8186 * Time
8187 */
8188#if defined(MBEDTLS_HAVE_TIME)
8189 used += 8;
8190
8191 if( used <= buf_len )
8192 {
8193 start = (uint64_t) session->start;
8194
8195 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8196 p += 8;
8197 }
8198#endif /* MBEDTLS_HAVE_TIME */
8199
8200 /*
8201 * Basic mandatory fields
8202 */
8203 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008204 + 1 /* id_len */
8205 + sizeof( session->id )
8206 + sizeof( session->master )
8207 + 4; /* verify_result */
8208
8209 if( used <= buf_len )
8210 {
8211 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8212 p += 2;
8213
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008214 *p++ = MBEDTLS_BYTE_0( session->id_len );
8215 memcpy( p, session->id, 32 );
8216 p += 32;
8217
8218 memcpy( p, session->master, 48 );
8219 p += 48;
8220
8221 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8222 p += 4;
8223 }
8224
8225 /*
8226 * Peer's end-entity certificate
8227 */
8228#if defined(MBEDTLS_X509_CRT_PARSE_C)
8229#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8230 if( session->peer_cert == NULL )
8231 cert_len = 0;
8232 else
8233 cert_len = session->peer_cert->raw.len;
8234
8235 used += 3 + cert_len;
8236
8237 if( used <= buf_len )
8238 {
8239 *p++ = MBEDTLS_BYTE_2( cert_len );
8240 *p++ = MBEDTLS_BYTE_1( cert_len );
8241 *p++ = MBEDTLS_BYTE_0( cert_len );
8242
8243 if( session->peer_cert != NULL )
8244 {
8245 memcpy( p, session->peer_cert->raw.p, cert_len );
8246 p += cert_len;
8247 }
8248 }
8249#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8250 if( session->peer_cert_digest != NULL )
8251 {
8252 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8253 if( used <= buf_len )
8254 {
8255 *p++ = (unsigned char) session->peer_cert_digest_type;
8256 *p++ = (unsigned char) session->peer_cert_digest_len;
8257 memcpy( p, session->peer_cert_digest,
8258 session->peer_cert_digest_len );
8259 p += session->peer_cert_digest_len;
8260 }
8261 }
8262 else
8263 {
8264 used += 2;
8265 if( used <= buf_len )
8266 {
8267 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8268 *p++ = 0;
8269 }
8270 }
8271#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8272#endif /* MBEDTLS_X509_CRT_PARSE_C */
8273
8274 /*
8275 * Session ticket if any, plus associated data
8276 */
8277#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8278 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8279
8280 if( used <= buf_len )
8281 {
8282 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8283 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8284 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8285
8286 if( session->ticket != NULL )
8287 {
8288 memcpy( p, session->ticket, session->ticket_len );
8289 p += session->ticket_len;
8290 }
8291
8292 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8293 p += 4;
8294 }
8295#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8296
8297 /*
8298 * Misc extension-related info
8299 */
8300#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8301 used += 1;
8302
8303 if( used <= buf_len )
8304 *p++ = session->mfl_code;
8305#endif
8306
8307#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8308 used += 1;
8309
8310 if( used <= buf_len )
8311 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8312#endif
8313
8314 return( used );
8315}
8316
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008317MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008318static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008319 const unsigned char *buf,
8320 size_t len )
8321{
8322#if defined(MBEDTLS_HAVE_TIME)
8323 uint64_t start;
8324#endif
8325#if defined(MBEDTLS_X509_CRT_PARSE_C)
8326#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8327 size_t cert_len;
8328#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8329#endif /* MBEDTLS_X509_CRT_PARSE_C */
8330
8331 const unsigned char *p = buf;
8332 const unsigned char * const end = buf + len;
8333
8334 /*
8335 * Time
8336 */
8337#if defined(MBEDTLS_HAVE_TIME)
8338 if( 8 > (size_t)( end - p ) )
8339 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8340
8341 start = ( (uint64_t) p[0] << 56 ) |
8342 ( (uint64_t) p[1] << 48 ) |
8343 ( (uint64_t) p[2] << 40 ) |
8344 ( (uint64_t) p[3] << 32 ) |
8345 ( (uint64_t) p[4] << 24 ) |
8346 ( (uint64_t) p[5] << 16 ) |
8347 ( (uint64_t) p[6] << 8 ) |
8348 ( (uint64_t) p[7] );
8349 p += 8;
8350
8351 session->start = (time_t) start;
8352#endif /* MBEDTLS_HAVE_TIME */
8353
8354 /*
8355 * Basic mandatory fields
8356 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008357 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008358 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8359
8360 session->ciphersuite = ( p[0] << 8 ) | p[1];
8361 p += 2;
8362
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008363 session->id_len = *p++;
8364 memcpy( session->id, p, 32 );
8365 p += 32;
8366
8367 memcpy( session->master, p, 48 );
8368 p += 48;
8369
8370 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8371 ( (uint32_t) p[1] << 16 ) |
8372 ( (uint32_t) p[2] << 8 ) |
8373 ( (uint32_t) p[3] );
8374 p += 4;
8375
8376 /* Immediately clear invalid pointer values that have been read, in case
8377 * we exit early before we replaced them with valid ones. */
8378#if defined(MBEDTLS_X509_CRT_PARSE_C)
8379#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8380 session->peer_cert = NULL;
8381#else
8382 session->peer_cert_digest = NULL;
8383#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8384#endif /* MBEDTLS_X509_CRT_PARSE_C */
8385#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8386 session->ticket = NULL;
8387#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8388
8389 /*
8390 * Peer certificate
8391 */
8392#if defined(MBEDTLS_X509_CRT_PARSE_C)
8393#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8394 /* Deserialize CRT from the end of the ticket. */
8395 if( 3 > (size_t)( end - p ) )
8396 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8397
8398 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8399 p += 3;
8400
8401 if( cert_len != 0 )
8402 {
8403 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8404
8405 if( cert_len > (size_t)( end - p ) )
8406 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8407
8408 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8409
8410 if( session->peer_cert == NULL )
8411 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8412
8413 mbedtls_x509_crt_init( session->peer_cert );
8414
8415 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8416 p, cert_len ) ) != 0 )
8417 {
8418 mbedtls_x509_crt_free( session->peer_cert );
8419 mbedtls_free( session->peer_cert );
8420 session->peer_cert = NULL;
8421 return( ret );
8422 }
8423
8424 p += cert_len;
8425 }
8426#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8427 /* Deserialize CRT digest from the end of the ticket. */
8428 if( 2 > (size_t)( end - p ) )
8429 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8430
8431 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8432 session->peer_cert_digest_len = (size_t) *p++;
8433
8434 if( session->peer_cert_digest_len != 0 )
8435 {
8436 const mbedtls_md_info_t *md_info =
8437 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8438 if( md_info == NULL )
8439 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8440 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8441 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8442
8443 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8444 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8445
8446 session->peer_cert_digest =
8447 mbedtls_calloc( 1, session->peer_cert_digest_len );
8448 if( session->peer_cert_digest == NULL )
8449 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8450
8451 memcpy( session->peer_cert_digest, p,
8452 session->peer_cert_digest_len );
8453 p += session->peer_cert_digest_len;
8454 }
8455#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8456#endif /* MBEDTLS_X509_CRT_PARSE_C */
8457
8458 /*
8459 * Session ticket and associated data
8460 */
8461#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8462 if( 3 > (size_t)( end - p ) )
8463 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8464
8465 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8466 p += 3;
8467
8468 if( session->ticket_len != 0 )
8469 {
8470 if( session->ticket_len > (size_t)( end - p ) )
8471 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8472
8473 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8474 if( session->ticket == NULL )
8475 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8476
8477 memcpy( session->ticket, p, session->ticket_len );
8478 p += session->ticket_len;
8479 }
8480
8481 if( 4 > (size_t)( end - p ) )
8482 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8483
8484 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8485 ( (uint32_t) p[1] << 16 ) |
8486 ( (uint32_t) p[2] << 8 ) |
8487 ( (uint32_t) p[3] );
8488 p += 4;
8489#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8490
8491 /*
8492 * Misc extension-related info
8493 */
8494#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8495 if( 1 > (size_t)( end - p ) )
8496 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8497
8498 session->mfl_code = *p++;
8499#endif
8500
8501#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8502 if( 1 > (size_t)( end - p ) )
8503 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8504
8505 session->encrypt_then_mac = *p++;
8506#endif
8507
8508 /* Done, should have consumed entire buffer */
8509 if( p != end )
8510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8511
8512 return( 0 );
8513}
Jerry Yudc7bd172022-02-17 13:44:15 +08008514#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8515
XiaokangQian75d40ef2022-04-20 11:05:24 +00008516int mbedtls_ssl_validate_ciphersuite(
8517 const mbedtls_ssl_context *ssl,
8518 const mbedtls_ssl_ciphersuite_t *suite_info,
8519 mbedtls_ssl_protocol_version min_tls_version,
8520 mbedtls_ssl_protocol_version max_tls_version )
8521{
8522 (void) ssl;
8523
8524 if( suite_info == NULL )
8525 return( -1 );
8526
8527 if( ( suite_info->min_tls_version > max_tls_version ) ||
8528 ( suite_info->max_tls_version < min_tls_version ) )
8529 {
8530 return( -1 );
8531 }
8532
XiaokangQian060d8672022-04-21 09:24:56 +00008533#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008534#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8535 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8536 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8537 {
8538 return( -1 );
8539 }
8540#endif
8541
8542 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8543#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8544 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8545 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8546 {
8547 return( -1 );
8548 }
8549#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8550#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8551
8552 return( 0 );
8553}
8554
XiaokangQianeaf36512022-04-24 09:07:44 +00008555#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8556/*
8557 * Function for writing a signature algorithm extension.
8558 *
8559 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8560 * value (TLS 1.3 RFC8446):
8561 * enum {
8562 * ....
8563 * ecdsa_secp256r1_sha256( 0x0403 ),
8564 * ecdsa_secp384r1_sha384( 0x0503 ),
8565 * ecdsa_secp521r1_sha512( 0x0603 ),
8566 * ....
8567 * } SignatureScheme;
8568 *
8569 * struct {
8570 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8571 * } SignatureSchemeList;
8572 *
8573 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8574 * value (TLS 1.2 RFC5246):
8575 * enum {
8576 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8577 * sha512(6), (255)
8578 * } HashAlgorithm;
8579 *
8580 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8581 * SignatureAlgorithm;
8582 *
8583 * struct {
8584 * HashAlgorithm hash;
8585 * SignatureAlgorithm signature;
8586 * } SignatureAndHashAlgorithm;
8587 *
8588 * SignatureAndHashAlgorithm
8589 * supported_signature_algorithms<2..2^16-2>;
8590 *
8591 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8592 * generalization of the TLS 1.2 signature algorithm extension.
8593 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8594 * `SignatureScheme` field of TLS 1.3
8595 *
8596 */
8597int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8598 const unsigned char *end, size_t *out_len )
8599{
8600 unsigned char *p = buf;
8601 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8602 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8603
8604 *out_len = 0;
8605
8606 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8607
8608 /* Check if we have space for header and length field:
8609 * - extension_type (2 bytes)
8610 * - extension_data_length (2 bytes)
8611 * - supported_signature_algorithms_length (2 bytes)
8612 */
8613 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8614 p += 6;
8615
8616 /*
8617 * Write supported_signature_algorithms
8618 */
8619 supported_sig_alg = p;
8620 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8621 if( sig_alg == NULL )
8622 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8623
8624 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8625 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008626 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8627 *sig_alg,
8628 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008629 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8630 continue;
8631 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8632 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8633 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008634 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008635 *sig_alg,
8636 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008637 }
8638
8639 /* Length of supported_signature_algorithms */
8640 supported_sig_alg_len = p - supported_sig_alg;
8641 if( supported_sig_alg_len == 0 )
8642 {
8643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8644 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8645 }
8646
XiaokangQianeaf36512022-04-24 09:07:44 +00008647 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008648 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008649 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8650
XiaokangQianeaf36512022-04-24 09:07:44 +00008651 *out_len = p - buf;
8652
8653#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8654 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8655#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8656 return( 0 );
8657}
8658#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8659
XiaokangQian40a35232022-05-07 09:02:40 +00008660#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008661/*
8662 * mbedtls_ssl_parse_server_name_ext
8663 *
8664 * Structure of server_name extension:
8665 *
8666 * enum {
8667 * host_name(0), (255)
8668 * } NameType;
8669 * opaque HostName<1..2^16-1>;
8670 *
8671 * struct {
8672 * NameType name_type;
8673 * select (name_type) {
8674 * case host_name: HostName;
8675 * } name;
8676 * } ServerName;
8677 * struct {
8678 * ServerName server_name_list<1..2^16-1>
8679 * } ServerNameList;
8680 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008681MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008682int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8683 const unsigned char *buf,
8684 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008685{
8686 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8687 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008688 size_t server_name_list_len, hostname_len;
8689 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008690
XiaokangQianf2a94202022-05-20 06:44:24 +00008691 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008692
8693 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008694 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008695 p += 2;
8696
XiaokangQian9b2b7712022-05-17 02:57:00 +00008697 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8698 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008699 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008700 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008701 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008702 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008703 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8704 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008705
8706 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8707 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008708 /* sni_name is intended to be used only during the parsing of the
8709 * ClientHello message (it is reset to NULL before the end of
8710 * the message parsing). Thus it is ok to just point to the
8711 * reception buffer and not make a copy of it.
8712 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008713 ssl->handshake->sni_name = p + 3;
8714 ssl->handshake->sni_name_len = hostname_len;
8715 if( ssl->conf->f_sni == NULL )
8716 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008717 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008718 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008719 if( ret != 0 )
8720 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008721 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008722 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8723 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008724 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8725 }
8726 return( 0 );
8727 }
8728
8729 p += hostname_len + 3;
8730 }
8731
8732 return( 0 );
8733}
8734#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8735
XiaokangQianacb39922022-06-17 10:18:48 +00008736#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008737MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008738int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8739 const unsigned char *buf,
8740 const unsigned char *end )
8741{
8742 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008743 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008744 const unsigned char *protocol_name_list;
8745 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008746 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008747
8748 /* If ALPN not configured, just ignore the extension */
8749 if( ssl->conf->alpn_list == NULL )
8750 return( 0 );
8751
8752 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008753 * RFC7301, section 3.1
8754 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008755 *
XiaokangQianc7403452022-06-23 03:24:12 +00008756 * struct {
8757 * ProtocolName protocol_name_list<2..2^16-1>
8758 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008759 */
8760
XiaokangQianc7403452022-06-23 03:24:12 +00008761 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008762 * protocol_name_list_len 2 bytes
8763 * protocol_name_len 1 bytes
8764 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008765 */
XiaokangQianacb39922022-06-17 10:18:48 +00008766 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8767
XiaokangQianc7403452022-06-23 03:24:12 +00008768 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008769 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008770 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008771 protocol_name_list = p;
8772 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008773
8774 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008775 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008776 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008777 protocol_name_len = *p++;
8778 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8779 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008780 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008781 {
8782 MBEDTLS_SSL_PEND_FATAL_ALERT(
8783 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8784 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008785 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008786 }
8787
8788 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008789 }
8790
8791 /* Use our order of preference */
8792 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8793 {
8794 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008795 p = protocol_name_list;
8796 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008797 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008798 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008799 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008800 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008801 {
8802 ssl->alpn_chosen = *alpn;
8803 return( 0 );
8804 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008805
8806 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008807 }
8808 }
8809
XiaokangQian95d5f542022-06-24 02:29:26 +00008810 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008811 MBEDTLS_SSL_PEND_FATAL_ALERT(
8812 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8813 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8814 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8815}
8816
8817int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8818 unsigned char *buf,
8819 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008820 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008821{
8822 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008823 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008824 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008825
8826 if( ssl->alpn_chosen == NULL )
8827 {
8828 return( 0 );
8829 }
8830
XiaokangQian95d5f542022-06-24 02:29:26 +00008831 protocol_name_len = strlen( ssl->alpn_chosen );
8832 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008833
8834 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8835 /*
8836 * 0 . 1 ext identifier
8837 * 2 . 3 ext length
8838 * 4 . 5 protocol list length
8839 * 6 . 6 protocol name length
8840 * 7 . 7+n protocol name
8841 */
8842 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8843
XiaokangQian95d5f542022-06-24 02:29:26 +00008844 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008845
XiaokangQian95d5f542022-06-24 02:29:26 +00008846 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8847 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008848 /* Note: the length of the chosen protocol has been checked to be less
8849 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8850 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008851 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008852
XiaokangQian95d5f542022-06-24 02:29:26 +00008853 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008854 return ( 0 );
8855}
8856#endif /* MBEDTLS_SSL_ALPN */
8857
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008858#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00008859 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00008860 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00008861 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008862int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
8863 const char *hostname )
8864{
8865 /* Initialize to suppress unnecessary compiler warning */
8866 size_t hostname_len = 0;
8867
8868 /* Check if new hostname is valid before
8869 * making any change to current one */
8870 if( hostname != NULL )
8871 {
8872 hostname_len = strlen( hostname );
8873
8874 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8875 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8876 }
8877
8878 /* Now it's clear that we will overwrite the old hostname,
8879 * so we can free it safely */
8880 if( session->hostname != NULL )
8881 {
8882 mbedtls_platform_zeroize( session->hostname,
8883 strlen( session->hostname ) );
8884 mbedtls_free( session->hostname );
8885 }
8886
8887 /* Passing NULL as hostname shall clear the old one */
8888 if( hostname == NULL )
8889 {
8890 session->hostname = NULL;
8891 }
8892 else
8893 {
8894 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
8895 if( session->hostname == NULL )
8896 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8897
8898 memcpy( session->hostname, hostname, hostname_len );
8899 }
8900
8901 return( 0 );
8902}
Xiaokang Qian03409292022-10-12 02:49:52 +00008903#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
8904 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00008905 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00008906 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008908#endif /* MBEDTLS_SSL_TLS_C */