blob: 3fc07019df631502d6a3700ddb3c8a887057c93b [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
Paul Bakker5121ce52009-01-03 21:22:43 +000020 * http://www.ietf.org/rfc/rfc2246.txt
21 * http://www.ietf.org/rfc/rfc4346.txt
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Jerry Yub476a442022-01-21 18:14:45 +080028#include <assert.h>
29
SimonBd5800b72016-04-26 07:43:27 +010030#if defined(MBEDTLS_PLATFORM_C)
31#include "mbedtls/platform.h"
32#else
33#include <stdlib.h>
Jerry Yu6ade7432022-01-25 10:39:33 +080034#include <stdio.h>
SimonBd5800b72016-04-26 07:43:27 +010035#define mbedtls_calloc calloc
36#define mbedtls_free free
Jerry Yu6ade7432022-01-25 10:39:33 +080037#define mbedtls_printf printf
38#endif /* !MBEDTLS_PLATFORM_C */
SimonBd5800b72016-04-26 07:43:27 +010039
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010041#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010042#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000043#include "ssl_misc.h"
Janos Follath73c616b2019-12-18 15:07:04 +000044#include "mbedtls/debug.h"
45#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050046#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010047#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020048#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020049
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <string.h>
51
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050052#if defined(MBEDTLS_USE_PSA_CRYPTO)
53#include "mbedtls/psa_util.h"
54#include "psa/crypto.h"
55#endif
56
Janos Follath23bdca02016-10-07 14:47:14 +010057#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020059#endif
60
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020061#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010062
Hanno Beckera0e20d02019-05-15 14:03:01 +010063#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010064/* Top-level Connection ID API */
65
Hanno Becker8367ccc2019-05-14 11:30:10 +010066int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
67 size_t len,
68 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +010069{
70 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
71 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
72
Hanno Becker611ac772019-05-14 11:45:26 +010073 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
74 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
75 {
76 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
77 }
78
79 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010080 conf->cid_len = len;
81 return( 0 );
82}
83
Hanno Beckerf8542cf2019-04-09 15:22:03 +010084int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
85 int enable,
86 unsigned char const *own_cid,
87 size_t own_cid_len )
88{
Hanno Becker76a79ab2019-05-03 14:38:32 +010089 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
90 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
91
Hanno Beckerca092242019-04-25 16:01:49 +010092 ssl->negotiate_cid = enable;
93 if( enable == MBEDTLS_SSL_CID_DISABLED )
94 {
95 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
96 return( 0 );
97 }
98 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +010099 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100100
Hanno Beckerad4a1372019-05-03 13:06:44 +0100101 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100102 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100103 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
104 (unsigned) own_cid_len,
105 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100106 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
107 }
108
109 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100110 /* Truncation is not an issue here because
111 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
112 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100113
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100114 return( 0 );
115}
116
Paul Elliott0113cf12022-03-11 20:26:47 +0000117int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl,
118 int *enabled,
119 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
120 size_t *own_cid_len )
121{
122 *enabled = MBEDTLS_SSL_CID_DISABLED;
123
124 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
126
127 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
128 * zero as this is indistinguishable from not requesting to use
129 * the CID extension. */
130 if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
131 return( 0 );
132
133 if( own_cid_len != NULL )
134 {
135 *own_cid_len = ssl->own_cid_len;
136 if( own_cid != NULL )
137 memcpy( own_cid, ssl->own_cid, ssl->own_cid_len );
138 }
139
140 *enabled = MBEDTLS_SSL_CID_ENABLED;
141
142 return( 0 );
143}
144
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100145int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
146 int *enabled,
147 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
148 size_t *peer_cid_len )
149{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100150 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100151
Hanno Becker76a79ab2019-05-03 14:38:32 +0100152 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Paul Elliott27b0d942022-03-18 21:55:32 +0000153 mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Hanno Becker76a79ab2019-05-03 14:38:32 +0100154 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100156 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100157
Hanno Beckerc5f24222019-05-03 12:54:52 +0100158 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
159 * were used, but client and server requested the empty CID.
160 * This is indistinguishable from not using the CID extension
161 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100162 if( ssl->transform_in->in_cid_len == 0 &&
163 ssl->transform_in->out_cid_len == 0 )
164 {
165 return( 0 );
166 }
167
Hanno Becker615ef172019-05-22 16:50:35 +0100168 if( peer_cid_len != NULL )
169 {
170 *peer_cid_len = ssl->transform_in->out_cid_len;
171 if( peer_cid != NULL )
172 {
173 memcpy( peer_cid, ssl->transform_in->out_cid,
174 ssl->transform_in->out_cid_len );
175 }
176 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100177
178 *enabled = MBEDTLS_SSL_CID_ENABLED;
179
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100180 return( 0 );
181}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100182#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200187/*
188 * Convert max_fragment_length codes to length.
189 * RFC 6066 says:
190 * enum{
191 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
192 * } MaxFragmentLength;
193 * and we add 0 -> extension unused
194 */
Angus Grattond8213d02016-05-25 20:56:48 +1000195static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200196{
Angus Grattond8213d02016-05-25 20:56:48 +1000197 switch( mfl )
198 {
199 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
200 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
201 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
202 return 512;
203 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
204 return 1024;
205 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
206 return 2048;
207 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
208 return 4096;
209 default:
210 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
211 }
212}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200214
Hanno Becker52055ae2019-02-06 14:30:46 +0000215int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
216 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200217{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 mbedtls_ssl_session_free( dst );
219 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200220
吴敬辉0b716112021-11-29 10:46:35 +0800221#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
222 dst->ticket = NULL;
223#endif
224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000226
227#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200228 if( src->peer_cert != NULL )
229 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000230 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200231
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200232 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200233 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200234 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200239 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200242 dst->peer_cert = NULL;
243 return( ret );
244 }
245 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000246#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000247 if( src->peer_cert_digest != NULL )
248 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000249 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000250 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000251 if( dst->peer_cert_digest == NULL )
252 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
253
254 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
255 src->peer_cert_digest_len );
256 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000257 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000258 }
259#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200262
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200263#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200264 if( src->ticket != NULL )
265 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200266 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200267 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200268 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200269
270 memcpy( dst->ticket, src->ticket, src->ticket_len );
271 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200272#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200273
274 return( 0 );
275}
276
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500277#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
278static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
279{
280 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
281 if( resized_buffer == NULL )
282 return -1;
283
284 /* We want to copy len_new bytes when downsizing the buffer, and
285 * len_old bytes when upsizing, so we choose the smaller of two sizes,
286 * to fit one buffer into another. Size checks, ensuring that no data is
287 * lost, are done outside of this function. */
288 memcpy( resized_buffer, *buffer,
289 ( len_new < *len_old ) ? len_new : *len_old );
290 mbedtls_platform_zeroize( *buffer, *len_old );
291 mbedtls_free( *buffer );
292
293 *buffer = resized_buffer;
294 *len_old = len_new;
295
296 return 0;
297}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200298
299static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500300 size_t in_buf_new_len,
301 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200302{
303 int modified = 0;
304 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
305 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
306 if( ssl->in_buf != NULL )
307 {
308 written_in = ssl->in_msg - ssl->in_buf;
309 iv_offset_in = ssl->in_iv - ssl->in_buf;
310 len_offset_in = ssl->in_len - ssl->in_buf;
311 if( downsizing ?
312 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
313 ssl->in_buf_len < in_buf_new_len )
314 {
315 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
316 {
317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
318 }
319 else
320 {
Paul Elliottb7449902021-03-10 18:14:58 +0000321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
322 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200323 modified = 1;
324 }
325 }
326 }
327
328 if( ssl->out_buf != NULL )
329 {
330 written_out = ssl->out_msg - ssl->out_buf;
331 iv_offset_out = ssl->out_iv - ssl->out_buf;
332 len_offset_out = ssl->out_len - ssl->out_buf;
333 if( downsizing ?
334 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
335 ssl->out_buf_len < out_buf_new_len )
336 {
337 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
338 {
339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
340 }
341 else
342 {
Paul Elliottb7449902021-03-10 18:14:58 +0000343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
344 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200345 modified = 1;
346 }
347 }
348 }
349 if( modified )
350 {
351 /* Update pointers here to avoid doing it twice. */
352 mbedtls_ssl_reset_in_out_pointers( ssl );
353 /* Fields below might not be properly updated with record
354 * splitting or with CID, so they are manually updated here. */
355 ssl->out_msg = ssl->out_buf + written_out;
356 ssl->out_len = ssl->out_buf + len_offset_out;
357 ssl->out_iv = ssl->out_buf + iv_offset_out;
358
359 ssl->in_msg = ssl->in_buf + written_in;
360 ssl->in_len = ssl->in_buf + len_offset_in;
361 ssl->in_iv = ssl->in_buf + iv_offset_in;
362 }
363}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500364#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
365
Jerry Yudb8c48a2022-01-27 14:54:54 +0800366#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800367
368#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
369typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
370 const char *label,
371 const unsigned char *random, size_t rlen,
372 unsigned char *dstbuf, size_t dlen );
373
374static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
375
376#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
377
378/* Type for the TLS PRF */
379typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
380 const unsigned char *, size_t,
381 unsigned char *, size_t);
382
383static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
384 int ciphersuite,
385 const unsigned char master[48],
386#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
387 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
388 int encrypt_then_mac,
389#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
390 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
391 ssl_tls_prf_t tls_prf,
392 const unsigned char randbytes[64],
393 int minor_ver,
394 unsigned endpoint,
395 const mbedtls_ssl_context *ssl );
396
397#if defined(MBEDTLS_SHA256_C)
398static int tls_prf_sha256( const unsigned char *secret, size_t slen,
399 const char *label,
400 const unsigned char *random, size_t rlen,
401 unsigned char *dstbuf, size_t dlen );
402static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
403static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
404
405#endif /* MBEDTLS_SHA256_C */
406
407#if defined(MBEDTLS_SHA384_C)
408static int tls_prf_sha384( const unsigned char *secret, size_t slen,
409 const char *label,
410 const unsigned char *random, size_t rlen,
411 unsigned char *dstbuf, size_t dlen );
412
413static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
414static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
415#endif /* MBEDTLS_SHA384_C */
416
417static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session,
418 unsigned char *buf,
419 size_t buf_len );
420static int ssl_session_load_tls12( mbedtls_ssl_session *session,
421 const unsigned char *buf,
422 size_t len );
423#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
424
Jerry Yu53d23e22022-02-09 16:25:09 +0800425static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
426
Jerry Yudb8c48a2022-01-27 14:54:54 +0800427#if defined(MBEDTLS_SHA256_C)
428static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Jerry Yu53d23e22022-02-09 16:25:09 +0800429#endif /* MBEDTLS_SHA256_C */
Jerry Yudb8c48a2022-01-27 14:54:54 +0800430
431#if defined(MBEDTLS_SHA384_C)
432static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Jerry Yu53d23e22022-02-09 16:25:09 +0800433#endif /* MBEDTLS_SHA384_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000434
Ron Eldor51d3ab52019-05-12 14:54:30 +0300435int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
436 const unsigned char *secret, size_t slen,
437 const char *label,
438 const unsigned char *random, size_t rlen,
439 unsigned char *dstbuf, size_t dlen )
440{
441 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
442
443 switch( prf )
444 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300445#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +0200446#if defined(MBEDTLS_SHA384_C)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300447 case MBEDTLS_SSL_TLS_PRF_SHA384:
448 tls_prf = tls_prf_sha384;
449 break;
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +0200450#endif /* MBEDTLS_SHA384_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300451#if defined(MBEDTLS_SHA256_C)
452 case MBEDTLS_SSL_TLS_PRF_SHA256:
453 tls_prf = tls_prf_sha256;
454 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300455#endif /* MBEDTLS_SHA256_C */
456#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300457 default:
458 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
459 }
460
461 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
462}
463
Jerry Yuc73c6182022-02-08 20:29:25 +0800464#if defined(MBEDTLS_X509_CRT_PARSE_C)
465static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
466{
467#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
468 if( session->peer_cert != NULL )
469 {
470 mbedtls_x509_crt_free( session->peer_cert );
471 mbedtls_free( session->peer_cert );
472 session->peer_cert = NULL;
473 }
474#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
475 if( session->peer_cert_digest != NULL )
476 {
477 /* Zeroization is not necessary. */
478 mbedtls_free( session->peer_cert_digest );
479 session->peer_cert_digest = NULL;
480 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
481 session->peer_cert_digest_len = 0;
482 }
483#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
484}
485#endif /* MBEDTLS_X509_CRT_PARSE_C */
486
487void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
488 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
489{
490 ((void) ciphersuite_info);
491
492#if defined(MBEDTLS_SHA384_C)
493 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
494 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
495 else
496#endif
497#if defined(MBEDTLS_SHA256_C)
498 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
499 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
500 else
501#endif
502 {
503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
504 return;
505 }
506}
507
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100508static void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
509 unsigned hs_type,
510 size_t total_hs_len )
511{
512 unsigned char hs_hdr[4];
513
514 /* Build HS header for checksum update. */
515 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
516 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
517 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
518 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
519
520 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
521}
522
523void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
524 unsigned hs_type,
525 unsigned char const *msg,
526 size_t msg_len )
527{
528 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
529 ssl->handshake->update_checksum( ssl, msg, msg_len );
530}
531
Jerry Yuc73c6182022-02-08 20:29:25 +0800532void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
533{
534 ((void) ssl);
535#if defined(MBEDTLS_SHA256_C)
536#if defined(MBEDTLS_USE_PSA_CRYPTO)
537 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
538 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
539#else
540 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
541#endif
542#endif
543#if defined(MBEDTLS_SHA384_C)
544#if defined(MBEDTLS_USE_PSA_CRYPTO)
545 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
546 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
547#else
548 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
549#endif
550#endif
551}
552
553static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
554 const unsigned char *buf, size_t len )
555{
556#if defined(MBEDTLS_SHA256_C)
557#if defined(MBEDTLS_USE_PSA_CRYPTO)
558 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
559#else
560 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
561#endif
562#endif
563#if defined(MBEDTLS_SHA384_C)
564#if defined(MBEDTLS_USE_PSA_CRYPTO)
565 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
566#else
567 mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len );
568#endif
569#endif
570}
571
572#if defined(MBEDTLS_SHA256_C)
573static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
574 const unsigned char *buf, size_t len )
575{
576#if defined(MBEDTLS_USE_PSA_CRYPTO)
577 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
578#else
579 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
580#endif
581}
582#endif
583
584#if defined(MBEDTLS_SHA384_C)
585static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
586 const unsigned char *buf, size_t len )
587{
588#if defined(MBEDTLS_USE_PSA_CRYPTO)
589 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
590#else
591 mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len );
592#endif
593}
594#endif
595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200597{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500601#if defined(MBEDTLS_USE_PSA_CRYPTO)
602 handshake->fin_sha256_psa = psa_hash_operation_init();
603 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
604#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200606 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200607#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500608#endif
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +0200609#if defined(MBEDTLS_SHA384_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500610#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500611 handshake->fin_sha384_psa = psa_hash_operation_init();
612 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500613#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 mbedtls_sha512_init( &handshake->fin_sha512 );
TRodziewicz26371e42021-06-08 16:45:41 +0200615 mbedtls_sha512_starts( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200616#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500617#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200618
619 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100620
621#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +0100622 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +0100623 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
624#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626#if defined(MBEDTLS_DHM_C)
627 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200628#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629#if defined(MBEDTLS_ECDH_C)
630 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200631#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200632#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200633 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200634#if defined(MBEDTLS_SSL_CLI_C)
635 handshake->ecjpake_cache = NULL;
636 handshake->ecjpake_cache_len = 0;
637#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200638#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200639
Gilles Peskineeccd8882020-03-10 12:19:08 +0100640#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200641 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200642#endif
643
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200644#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
645 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
646#endif
Hanno Becker75173122019-02-06 16:18:31 +0000647
648#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
649 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
650 mbedtls_pk_init( &handshake->peer_pubkey );
651#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200652}
653
Hanno Beckera18d1322018-01-03 14:27:32 +0000654void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200655{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200657
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100658#if defined(MBEDTLS_USE_PSA_CRYPTO)
659 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
660 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100661#else
662 mbedtls_cipher_init( &transform->cipher_ctx_enc );
663 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100664#endif
665
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000666#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100667#if defined(MBEDTLS_USE_PSA_CRYPTO)
668 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
669 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100670#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_md_init( &transform->md_ctx_enc );
672 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000673#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100674#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200675}
676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200678{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200680}
681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000683{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200684 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000685 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200687 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200689 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200690 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200691
692 /*
693 * Either the pointers are now NULL or cleared properly and can be freed.
694 * Now allocate missing structures.
695 */
696 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200697 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200698 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200699 }
Paul Bakker48916f92012-09-16 19:57:18 +0000700
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200701 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200702 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200703 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200704 }
Paul Bakker48916f92012-09-16 19:57:18 +0000705
Paul Bakker82788fb2014-10-20 13:59:19 +0200706 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200707 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200708 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200709 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500710#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
711 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400712
Andrzej Kurek4a063792020-10-21 15:08:44 +0200713 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
714 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500715#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000716
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200717 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000718 if( ssl->handshake == NULL ||
719 ssl->transform_negotiate == NULL ||
720 ssl->session_negotiate == NULL )
721 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 mbedtls_free( ssl->handshake );
725 mbedtls_free( ssl->transform_negotiate );
726 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200727
728 ssl->handshake = NULL;
729 ssl->transform_negotiate = NULL;
730 ssl->session_negotiate = NULL;
731
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200732 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000733 }
734
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200735 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000737 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200738 ssl_handshake_params_init( ssl->handshake );
739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200741 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
742 {
743 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200744
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200745 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
746 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
747 else
748 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200749
Hanno Becker0f57a652020-02-05 10:37:26 +0000750 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200751 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200752#endif
753
Brett Warrene0edc842021-08-17 09:53:13 +0100754/*
755 * curve_list is translated to IANA TLS group identifiers here because
756 * mbedtls_ssl_conf_curves returns void and so can't return
757 * any error codes.
758 */
759#if defined(MBEDTLS_ECP_C)
760#if !defined(MBEDTLS_DEPRECATED_REMOVED)
761 /* Heap allocate and translate curve_list from internal to IANA group ids */
762 if ( ssl->conf->curve_list != NULL )
763 {
764 size_t length;
765 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
766
767 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
768 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
769
770 /* Leave room for zero termination */
771 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
772 if ( group_list == NULL )
773 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
774
775 for( size_t i = 0; i < length; i++ )
776 {
777 const mbedtls_ecp_curve_info *info =
778 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
779 if ( info == NULL )
780 {
781 mbedtls_free( group_list );
782 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
783 }
784 group_list[i] = info->tls_id;
785 }
786
787 group_list[length] = 0;
788
789 ssl->handshake->group_list = group_list;
790 ssl->handshake->group_list_heap_allocated = 1;
791 }
792 else
793 {
794 ssl->handshake->group_list = ssl->conf->group_list;
795 ssl->handshake->group_list_heap_allocated = 0;
796 }
797#endif /* MBEDTLS_DEPRECATED_REMOVED */
798#endif /* MBEDTLS_ECP_C */
799
Jerry Yuf017ee42022-01-12 15:49:48 +0800800#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
801#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800802#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800803 /* Heap allocate and translate sig_hashes from internal hash identifiers to
804 signature algorithms IANA identifiers. */
805 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800806 ssl->conf->sig_hashes != NULL )
807 {
808 const int *md;
809 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800810 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800811 uint16_t *p;
812
Jerry Yub476a442022-01-21 18:14:45 +0800813#if defined(static_assert)
814 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
815 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
816 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800817#endif
818
Jerry Yuf017ee42022-01-12 15:49:48 +0800819 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
820 {
821 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
822 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800823#if defined(MBEDTLS_ECDSA_C)
824 sig_algs_len += sizeof( uint16_t );
825#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800826
Jerry Yub476a442022-01-21 18:14:45 +0800827#if defined(MBEDTLS_RSA_C)
828 sig_algs_len += sizeof( uint16_t );
829#endif
830 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
831 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800832 }
833
Jerry Yub476a442022-01-21 18:14:45 +0800834 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800835 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
836
Jerry Yub476a442022-01-21 18:14:45 +0800837 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
838 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800839 if( ssl->handshake->sig_algs == NULL )
840 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
841
842 p = (uint16_t *)ssl->handshake->sig_algs;
843 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
844 {
845 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
846 if( hash == MBEDTLS_SSL_HASH_NONE )
847 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800848#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800849 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
850 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800851#endif
852#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800853 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
854 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800855#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800856 }
857 *p = MBEDTLS_TLS1_3_SIG_NONE;
858 ssl->handshake->sig_algs_heap_allocated = 1;
859 }
860 else
Jerry Yua69269a2022-01-17 21:06:01 +0800861#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800862 {
863 ssl->handshake->sig_algs = ssl->conf->sig_algs;
864 ssl->handshake->sig_algs_heap_allocated = 0;
865 }
866#endif /* MBEDTLS_DEPRECATED_REMOVED */
867#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000868 return( 0 );
869}
870
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200871#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200872/* Dummy cookie callbacks for defaults */
873static int ssl_cookie_write_dummy( void *ctx,
874 unsigned char **p, unsigned char *end,
875 const unsigned char *cli_id, size_t cli_id_len )
876{
877 ((void) ctx);
878 ((void) p);
879 ((void) end);
880 ((void) cli_id);
881 ((void) cli_id_len);
882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200884}
885
886static int ssl_cookie_check_dummy( void *ctx,
887 const unsigned char *cookie, size_t cookie_len,
888 const unsigned char *cli_id, size_t cli_id_len )
889{
890 ((void) ctx);
891 ((void) cookie);
892 ((void) cookie_len);
893 ((void) cli_id);
894 ((void) cli_id_len);
895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200897}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200898#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200899
Paul Bakker5121ce52009-01-03 21:22:43 +0000900/*
901 * Initialize an SSL context
902 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200903void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
904{
905 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
906}
907
Jerry Yu60835a82021-08-04 10:13:52 +0800908static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
909{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100910 const mbedtls_ssl_config *conf = ssl->conf;
911
Ronald Cron6f135e12021-12-08 16:57:54 +0100912#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100913 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
914 {
915 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jerry Yu60835a82021-08-04 10:13:52 +0800916 {
Ronald Cron086ee0b2022-03-15 15:18:51 +0100917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
Jerry Yu60835a82021-08-04 10:13:52 +0800918 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
919 }
Ronald Cron37bdaab2022-03-30 16:45:51 +0200920
921 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
922 {
923 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
924 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
925 }
926
Jerry Yu60835a82021-08-04 10:13:52 +0800927 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
928 return( 0 );
929 }
930#endif
931
932#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100933 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800934 {
935 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
936 return( 0 );
937 }
938#endif
939
Ronald Cron6f135e12021-12-08 16:57:54 +0100940#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100941 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800942 {
Ronald Crone1d3f062022-02-10 14:50:54 +0100943 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
944 {
945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
946 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
947 }
Ronald Cron37bdaab2022-03-30 16:45:51 +0200948
949 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
950 {
951 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
952 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
953 }
954
Ronald Crone1d3f062022-02-10 14:50:54 +0100955 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
956 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +0800957 }
958#endif
959
960 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
961 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
962}
963
964static int ssl_conf_check(const mbedtls_ssl_context *ssl)
965{
966 int ret;
967 ret = ssl_conf_version_check( ssl );
968 if( ret != 0 )
969 return( ret );
970
971 /* Space for further checks */
972
973 return( 0 );
974}
975
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200976/*
977 * Setup an SSL context
978 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100979
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200980int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +0200981 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +0000982{
Janos Follath865b3eb2019-12-16 11:46:15 +0000983 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +0000984 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
985 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +0000986
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200987 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +0000988
Jerry Yu60835a82021-08-04 10:13:52 +0800989 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
990 return( ret );
991
Paul Bakker62f2dee2012-09-28 07:31:51 +0000992 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +0100993 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +0000994 */
k-stachowiakc9a5f022018-07-24 13:53:31 +0200995
996 /* Set to NULL in case of an error condition */
997 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +0200998
Darryl Greenb33cc762019-11-28 14:29:44 +0000999#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1000 ssl->in_buf_len = in_buf_len;
1001#endif
1002 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001003 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001004 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001006 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001007 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001008 }
1009
Darryl Greenb33cc762019-11-28 14:29:44 +00001010#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1011 ssl->out_buf_len = out_buf_len;
1012#endif
1013 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001014 if( ssl->out_buf == NULL )
1015 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001017 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001018 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001019 }
1020
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001021 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001022
Johan Pascalb62bb512015-12-03 21:56:45 +01001023#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001024 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001025#endif
1026
Paul Bakker48916f92012-09-16 19:57:18 +00001027 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001028 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001029
1030 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001031
1032error:
1033 mbedtls_free( ssl->in_buf );
1034 mbedtls_free( ssl->out_buf );
1035
1036 ssl->conf = NULL;
1037
Darryl Greenb33cc762019-11-28 14:29:44 +00001038#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1039 ssl->in_buf_len = 0;
1040 ssl->out_buf_len = 0;
1041#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001042 ssl->in_buf = NULL;
1043 ssl->out_buf = NULL;
1044
1045 ssl->in_hdr = NULL;
1046 ssl->in_ctr = NULL;
1047 ssl->in_len = NULL;
1048 ssl->in_iv = NULL;
1049 ssl->in_msg = NULL;
1050
1051 ssl->out_hdr = NULL;
1052 ssl->out_ctr = NULL;
1053 ssl->out_len = NULL;
1054 ssl->out_iv = NULL;
1055 ssl->out_msg = NULL;
1056
k-stachowiak9f7798e2018-07-31 16:52:32 +02001057 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001058}
1059
1060/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001061 * Reset an initialized and used SSL context for re-use while retaining
1062 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001063 *
1064 * If partial is non-zero, keep data in the input buffer and client ID.
1065 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001066 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001067void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1068 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001069{
Darryl Greenb33cc762019-11-28 14:29:44 +00001070#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1071 size_t in_buf_len = ssl->in_buf_len;
1072 size_t out_buf_len = ssl->out_buf_len;
1073#else
1074 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1075 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1076#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001077
Hanno Beckerb0302c42021-08-03 09:39:42 +01001078#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1079 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001080#endif
1081
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001082 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001083 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001084
Hanno Beckerb0302c42021-08-03 09:39:42 +01001085 mbedtls_ssl_reset_in_out_pointers( ssl );
1086
1087 /* Reset incoming message parsing */
1088 ssl->in_offt = NULL;
1089 ssl->nb_zero = 0;
1090 ssl->in_msgtype = 0;
1091 ssl->in_msglen = 0;
1092 ssl->in_hslen = 0;
1093 ssl->keep_current_message = 0;
1094 ssl->transform_in = NULL;
1095
1096#if defined(MBEDTLS_SSL_PROTO_DTLS)
1097 ssl->next_record_offset = 0;
1098 ssl->in_epoch = 0;
1099#endif
1100
1101 /* Keep current datagram if partial == 1 */
1102 if( partial == 0 )
1103 {
1104 ssl->in_left = 0;
1105 memset( ssl->in_buf, 0, in_buf_len );
1106 }
1107
1108 /* Reset outgoing message writing */
1109 ssl->out_msgtype = 0;
1110 ssl->out_msglen = 0;
1111 ssl->out_left = 0;
1112 memset( ssl->out_buf, 0, out_buf_len );
1113 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1114 ssl->transform_out = NULL;
1115
1116#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1117 mbedtls_ssl_dtls_replay_reset( ssl );
1118#endif
1119
XiaokangQian2b01dc32022-01-21 02:53:13 +00001120#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001121 if( ssl->transform )
1122 {
1123 mbedtls_ssl_transform_free( ssl->transform );
1124 mbedtls_free( ssl->transform );
1125 ssl->transform = NULL;
1126 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001127#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1128
XiaokangQian2b01dc32022-01-21 02:53:13 +00001129#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1130 mbedtls_ssl_transform_free( ssl->transform_application );
1131 mbedtls_free( ssl->transform_application );
1132 ssl->transform_application = NULL;
1133
1134 if( ssl->handshake != NULL )
1135 {
1136 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1137 mbedtls_free( ssl->handshake->transform_earlydata );
1138 ssl->handshake->transform_earlydata = NULL;
1139
1140 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1141 mbedtls_free( ssl->handshake->transform_handshake );
1142 ssl->handshake->transform_handshake = NULL;
1143 }
1144
XiaokangQian2b01dc32022-01-21 02:53:13 +00001145#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001146}
1147
1148int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1149{
1150 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1151
1152 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1153
XiaokangQian78b1fa72022-01-19 06:56:30 +00001154 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001155
1156 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#if defined(MBEDTLS_SSL_RENEGOTIATION)
1158 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001159 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001160
1161 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1163 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001164#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001166
Hanno Beckerb0302c42021-08-03 09:39:42 +01001167 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001168 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001169 if( ssl->session )
1170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 mbedtls_ssl_session_free( ssl->session );
1172 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001173 ssl->session = NULL;
1174 }
1175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001177 ssl->alpn_chosen = NULL;
1178#endif
1179
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001180#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01001181#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001182 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01001183#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001184 {
1185 mbedtls_free( ssl->cli_id );
1186 ssl->cli_id = NULL;
1187 ssl->cli_id_len = 0;
1188 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001189#endif
1190
Paul Bakker48916f92012-09-16 19:57:18 +00001191 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1192 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001193
1194 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001195}
1196
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001197/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001198 * Reset an initialized and used SSL context for re-use while retaining
1199 * all application-set variables, function pointers and data.
1200 */
1201int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1202{
Hanno Becker43aefe22020-02-05 10:44:56 +00001203 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001204}
1205
1206/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001207 * SSL set accessors
1208 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001209void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001210{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001211 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001212}
1213
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001214void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001215{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001216 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001217}
1218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001220void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001221{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001222 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001223}
1224#endif
1225
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001226void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001227{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001228 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001229}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001232
Hanno Becker1841b0a2018-08-24 11:13:57 +01001233void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1234 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001235{
1236 ssl->disable_datagram_packing = !allow_packing;
1237}
1238
1239void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1240 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001241{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001242 conf->hs_timeout_min = min;
1243 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001244}
1245#endif
1246
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001247void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001248{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001249 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001250}
1251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001253void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001254 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001255 void *p_vrfy )
1256{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001257 conf->f_vrfy = f_vrfy;
1258 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001259}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001261
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001262void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001263 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001264 void *p_rng )
1265{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001266 conf->f_rng = f_rng;
1267 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001268}
1269
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001270void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001271 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001272 void *p_dbg )
1273{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001274 conf->f_dbg = f_dbg;
1275 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001276}
1277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001279 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001280 mbedtls_ssl_send_t *f_send,
1281 mbedtls_ssl_recv_t *f_recv,
1282 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001283{
1284 ssl->p_bio = p_bio;
1285 ssl->f_send = f_send;
1286 ssl->f_recv = f_recv;
1287 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001288}
1289
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001290#if defined(MBEDTLS_SSL_PROTO_DTLS)
1291void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1292{
1293 ssl->mtu = mtu;
1294}
1295#endif
1296
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001297void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001298{
1299 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001300}
1301
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001302void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1303 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001304 mbedtls_ssl_set_timer_t *f_set_timer,
1305 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001306{
1307 ssl->p_timer = p_timer;
1308 ssl->f_set_timer = f_set_timer;
1309 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001310
1311 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001312 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001313}
1314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001316void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001317 void *p_cache,
1318 mbedtls_ssl_cache_get_t *f_get_cache,
1319 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001320{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001321 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001322 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001323 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001324}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327#if defined(MBEDTLS_SSL_CLI_C)
1328int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001329{
Janos Follath865b3eb2019-12-16 11:46:15 +00001330 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001331
1332 if( ssl == NULL ||
1333 session == NULL ||
1334 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001335 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001338 }
1339
Hanno Beckere810bbc2021-05-14 16:01:05 +01001340 if( ssl->handshake->resume == 1 )
1341 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1342
Hanno Becker52055ae2019-02-06 14:30:46 +00001343 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1344 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001345 return( ret );
1346
Paul Bakker0a597072012-09-25 21:55:46 +00001347 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001348
1349 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001350}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001352
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001353void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1354 const int *ciphersuites )
1355{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001356 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001357}
1358
Ronald Cron6f135e12021-12-08 16:57:54 +01001359#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001360void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001361 const int kex_modes )
1362{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001363 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001364}
Ronald Cron6f135e12021-12-08 16:57:54 +01001365#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001368void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001369 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001370{
1371 conf->cert_profile = profile;
1372}
1373
Glenn Strauss36872db2022-01-22 05:06:31 -05001374static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1375{
1376 mbedtls_ssl_key_cert *cur = key_cert, *next;
1377
1378 while( cur != NULL )
1379 {
1380 next = cur->next;
1381 mbedtls_free( cur );
1382 cur = next;
1383 }
1384}
1385
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001386/* Append a new keycert entry to a (possibly empty) list */
1387static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1388 mbedtls_x509_crt *cert,
1389 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001390{
niisato8ee24222018-06-25 19:05:48 +09001391 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001392
Glenn Strauss36872db2022-01-22 05:06:31 -05001393 if( cert == NULL )
1394 {
1395 /* Free list if cert is null */
1396 ssl_key_cert_free( *head );
1397 *head = NULL;
1398 return( 0 );
1399 }
1400
niisato8ee24222018-06-25 19:05:48 +09001401 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1402 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001403 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001404
niisato8ee24222018-06-25 19:05:48 +09001405 new_cert->cert = cert;
1406 new_cert->key = key;
1407 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001408
Glenn Strauss36872db2022-01-22 05:06:31 -05001409 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001410 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001411 {
niisato8ee24222018-06-25 19:05:48 +09001412 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001413 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001414 else
1415 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001416 mbedtls_ssl_key_cert *cur = *head;
1417 while( cur->next != NULL )
1418 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001419 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001420 }
1421
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001422 return( 0 );
1423}
1424
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001425int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001426 mbedtls_x509_crt *own_cert,
1427 mbedtls_pk_context *pk_key )
1428{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001429 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001430}
1431
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001432void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001433 mbedtls_x509_crt *ca_chain,
1434 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001435{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001436 conf->ca_chain = ca_chain;
1437 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001438
1439#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1440 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1441 * cannot be used together. */
1442 conf->f_ca_cb = NULL;
1443 conf->p_ca_cb = NULL;
1444#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001445}
Hanno Becker5adaad92019-03-27 16:54:37 +00001446
1447#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1448void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001449 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001450 void *p_ca_cb )
1451{
1452 conf->f_ca_cb = f_ca_cb;
1453 conf->p_ca_cb = p_ca_cb;
1454
1455 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1456 * cannot be used together. */
1457 conf->ca_chain = NULL;
1458 conf->ca_crl = NULL;
1459}
1460#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001462
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001463#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001464const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1465 size_t *name_len )
1466{
1467 *name_len = ssl->handshake->sni_name_len;
1468 return( ssl->handshake->sni_name );
1469}
1470
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001471int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1472 mbedtls_x509_crt *own_cert,
1473 mbedtls_pk_context *pk_key )
1474{
1475 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1476 own_cert, pk_key ) );
1477}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001478
1479void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1480 mbedtls_x509_crt *ca_chain,
1481 mbedtls_x509_crl *ca_crl )
1482{
1483 ssl->handshake->sni_ca_chain = ca_chain;
1484 ssl->handshake->sni_ca_crl = ca_crl;
1485}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001486
1487void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1488 int authmode )
1489{
1490 ssl->handshake->sni_authmode = authmode;
1491}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001492#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1493
Hanno Becker8927c832019-04-03 12:52:50 +01001494#if defined(MBEDTLS_X509_CRT_PARSE_C)
1495void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1496 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1497 void *p_vrfy )
1498{
1499 ssl->f_vrfy = f_vrfy;
1500 ssl->p_vrfy = p_vrfy;
1501}
1502#endif
1503
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001504#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001505/*
1506 * Set EC J-PAKE password for current handshake
1507 */
1508int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1509 const unsigned char *pw,
1510 size_t pw_len )
1511{
1512 mbedtls_ecjpake_role role;
1513
Janos Follath8eb64132016-06-03 15:40:57 +01001514 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001515 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1516
1517 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1518 role = MBEDTLS_ECJPAKE_SERVER;
1519 else
1520 role = MBEDTLS_ECJPAKE_CLIENT;
1521
1522 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1523 role,
1524 MBEDTLS_MD_SHA256,
1525 MBEDTLS_ECP_DP_SECP256R1,
1526 pw, pw_len ) );
1527}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001528#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001529
Gilles Peskineeccd8882020-03-10 12:19:08 +01001530#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001531
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001532static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1533{
1534#if defined(MBEDTLS_USE_PSA_CRYPTO)
1535 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1536 return( 1 );
1537#endif /* MBEDTLS_USE_PSA_CRYPTO */
1538
1539 if( conf->psk != NULL )
1540 return( 1 );
1541
1542 return( 0 );
1543}
1544
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001545static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1546{
1547 /* Remove reference to existing PSK, if any. */
1548#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001549 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001550 {
1551 /* The maintenance of the PSK key slot is the
1552 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001553 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001554 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00001555 /* This and the following branch should never
1556 * be taken simultaenously as we maintain the
1557 * invariant that raw and opaque PSKs are never
1558 * configured simultaneously. As a safeguard,
1559 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001560#endif /* MBEDTLS_USE_PSA_CRYPTO */
1561 if( conf->psk != NULL )
1562 {
1563 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1564
1565 mbedtls_free( conf->psk );
1566 conf->psk = NULL;
1567 conf->psk_len = 0;
1568 }
1569
1570 /* Remove reference to PSK identity, if any. */
1571 if( conf->psk_identity != NULL )
1572 {
1573 mbedtls_free( conf->psk_identity );
1574 conf->psk_identity = NULL;
1575 conf->psk_identity_len = 0;
1576 }
1577}
1578
Hanno Becker7390c712018-11-15 13:33:04 +00001579/* This function assumes that PSK identity in the SSL config is unset.
1580 * It checks that the provided identity is well-formed and attempts
1581 * to make a copy of it in the SSL config.
1582 * On failure, the PSK identity in the config remains unset. */
1583static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1584 unsigned char const *psk_identity,
1585 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001586{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001587 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001588 if( psk_identity == NULL ||
1589 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001590 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001591 {
1592 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1593 }
1594
Hanno Becker7390c712018-11-15 13:33:04 +00001595 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1596 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001597 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001598
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001599 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001600 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001601
1602 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001603}
1604
Hanno Becker7390c712018-11-15 13:33:04 +00001605int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1606 const unsigned char *psk, size_t psk_len,
1607 const unsigned char *psk_identity, size_t psk_identity_len )
1608{
Janos Follath865b3eb2019-12-16 11:46:15 +00001609 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001610
1611 /* We currently only support one PSK, raw or opaque. */
1612 if( ssl_conf_psk_is_configured( conf ) )
1613 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001614
1615 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001616 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001617 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001618 if( psk_len == 0 )
1619 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1620 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1621 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1622
Hanno Becker7390c712018-11-15 13:33:04 +00001623 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1624 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1625 conf->psk_len = psk_len;
1626 memcpy( conf->psk, psk, conf->psk_len );
1627
1628 /* Check and set PSK Identity */
1629 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1630 if( ret != 0 )
1631 ssl_conf_remove_psk( conf );
1632
1633 return( ret );
1634}
1635
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001636static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1637{
1638#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001639 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001640 {
Ronald Croncf56a0a2020-08-04 09:51:30 +02001641 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001642 }
1643 else
1644#endif /* MBEDTLS_USE_PSA_CRYPTO */
1645 if( ssl->handshake->psk != NULL )
1646 {
1647 mbedtls_platform_zeroize( ssl->handshake->psk,
1648 ssl->handshake->psk_len );
1649 mbedtls_free( ssl->handshake->psk );
1650 ssl->handshake->psk_len = 0;
1651 }
1652}
1653
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001654int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1655 const unsigned char *psk, size_t psk_len )
1656{
1657 if( psk == NULL || ssl->handshake == NULL )
1658 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1659
1660 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1661 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1662
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001663 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001664
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001665 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001666 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001667
1668 ssl->handshake->psk_len = psk_len;
1669 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1670
1671 return( 0 );
1672}
1673
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001674#if defined(MBEDTLS_USE_PSA_CRYPTO)
1675int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001676 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001677 const unsigned char *psk_identity,
1678 size_t psk_identity_len )
1679{
Janos Follath865b3eb2019-12-16 11:46:15 +00001680 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001681
1682 /* We currently only support one PSK, raw or opaque. */
1683 if( ssl_conf_psk_is_configured( conf ) )
1684 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001685
Hanno Becker7390c712018-11-15 13:33:04 +00001686 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001687 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001688 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001689 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001690
1691 /* Check and set PSK Identity */
1692 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1693 psk_identity_len );
1694 if( ret != 0 )
1695 ssl_conf_remove_psk( conf );
1696
1697 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001698}
1699
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001700int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1701 mbedtls_svc_key_id_t psk )
1702{
1703 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1704 ( ssl->handshake == NULL ) )
1705 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1706
1707 ssl_remove_psk( ssl );
1708 ssl->handshake->psk_opaque = psk;
1709 return( 0 );
1710}
1711#endif /* MBEDTLS_USE_PSA_CRYPTO */
1712
1713void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1714 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1715 size_t),
1716 void *p_psk )
1717{
1718 conf->f_psk = f_psk;
1719 conf->p_psk = p_psk;
1720}
1721#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1722
1723#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01001724psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01001725 size_t taglen,
1726 psa_algorithm_t *alg,
1727 psa_key_type_t *key_type,
1728 size_t *key_size )
1729{
1730 switch ( mbedtls_cipher_type )
1731 {
1732 case MBEDTLS_CIPHER_AES_128_CBC:
1733 *alg = PSA_ALG_CBC_NO_PADDING;
1734 *key_type = PSA_KEY_TYPE_AES;
1735 *key_size = 128;
1736 break;
1737 case MBEDTLS_CIPHER_AES_128_CCM:
1738 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1739 *key_type = PSA_KEY_TYPE_AES;
1740 *key_size = 128;
1741 break;
1742 case MBEDTLS_CIPHER_AES_128_GCM:
1743 *alg = PSA_ALG_GCM;
1744 *key_type = PSA_KEY_TYPE_AES;
1745 *key_size = 128;
1746 break;
1747 case MBEDTLS_CIPHER_AES_192_CCM:
1748 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1749 *key_type = PSA_KEY_TYPE_AES;
1750 *key_size = 192;
1751 break;
1752 case MBEDTLS_CIPHER_AES_192_GCM:
1753 *alg = PSA_ALG_GCM;
1754 *key_type = PSA_KEY_TYPE_AES;
1755 *key_size = 192;
1756 break;
1757 case MBEDTLS_CIPHER_AES_256_CBC:
1758 *alg = PSA_ALG_CBC_NO_PADDING;
1759 *key_type = PSA_KEY_TYPE_AES;
1760 *key_size = 256;
1761 break;
1762 case MBEDTLS_CIPHER_AES_256_CCM:
1763 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1764 *key_type = PSA_KEY_TYPE_AES;
1765 *key_size = 256;
1766 break;
1767 case MBEDTLS_CIPHER_AES_256_GCM:
1768 *alg = PSA_ALG_GCM;
1769 *key_type = PSA_KEY_TYPE_AES;
1770 *key_size = 256;
1771 break;
1772 case MBEDTLS_CIPHER_ARIA_128_CBC:
1773 *alg = PSA_ALG_CBC_NO_PADDING;
1774 *key_type = PSA_KEY_TYPE_ARIA;
1775 *key_size = 128;
1776 break;
1777 case MBEDTLS_CIPHER_ARIA_128_CCM:
1778 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1779 *key_type = PSA_KEY_TYPE_ARIA;
1780 *key_size = 128;
1781 break;
1782 case MBEDTLS_CIPHER_ARIA_128_GCM:
1783 *alg = PSA_ALG_GCM;
1784 *key_type = PSA_KEY_TYPE_ARIA;
1785 *key_size = 128;
1786 break;
1787 case MBEDTLS_CIPHER_ARIA_192_CCM:
1788 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1789 *key_type = PSA_KEY_TYPE_ARIA;
1790 *key_size = 192;
1791 break;
1792 case MBEDTLS_CIPHER_ARIA_192_GCM:
1793 *alg = PSA_ALG_GCM;
1794 *key_type = PSA_KEY_TYPE_ARIA;
1795 *key_size = 192;
1796 break;
1797 case MBEDTLS_CIPHER_ARIA_256_CBC:
1798 *alg = PSA_ALG_CBC_NO_PADDING;
1799 *key_type = PSA_KEY_TYPE_ARIA;
1800 *key_size = 256;
1801 break;
1802 case MBEDTLS_CIPHER_ARIA_256_CCM:
1803 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1804 *key_type = PSA_KEY_TYPE_ARIA;
1805 *key_size = 256;
1806 break;
1807 case MBEDTLS_CIPHER_ARIA_256_GCM:
1808 *alg = PSA_ALG_GCM;
1809 *key_type = PSA_KEY_TYPE_ARIA;
1810 *key_size = 256;
1811 break;
1812 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
1813 *alg = PSA_ALG_CBC_NO_PADDING;
1814 *key_type = PSA_KEY_TYPE_CAMELLIA;
1815 *key_size = 128;
1816 break;
1817 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
1818 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1819 *key_type = PSA_KEY_TYPE_CAMELLIA;
1820 *key_size = 128;
1821 break;
1822 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
1823 *alg = PSA_ALG_GCM;
1824 *key_type = PSA_KEY_TYPE_CAMELLIA;
1825 *key_size = 128;
1826 break;
1827 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
1828 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1829 *key_type = PSA_KEY_TYPE_CAMELLIA;
1830 *key_size = 192;
1831 break;
1832 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
1833 *alg = PSA_ALG_GCM;
1834 *key_type = PSA_KEY_TYPE_CAMELLIA;
1835 *key_size = 192;
1836 break;
1837 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
1838 *alg = PSA_ALG_CBC_NO_PADDING;
1839 *key_type = PSA_KEY_TYPE_CAMELLIA;
1840 *key_size = 256;
1841 break;
1842 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
1843 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1844 *key_type = PSA_KEY_TYPE_CAMELLIA;
1845 *key_size = 256;
1846 break;
1847 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
1848 *alg = PSA_ALG_GCM;
1849 *key_type = PSA_KEY_TYPE_CAMELLIA;
1850 *key_size = 256;
1851 break;
1852 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
1853 *alg = PSA_ALG_CHACHA20_POLY1305;
1854 *key_type = PSA_KEY_TYPE_CHACHA20;
1855 *key_size = 256;
1856 break;
1857 case MBEDTLS_CIPHER_NULL:
1858 *alg = MBEDTLS_SSL_NULL_CIPHER;
1859 *key_type = 0;
1860 *key_size = 0;
1861 break;
1862 default:
1863 return PSA_ERROR_NOT_SUPPORTED;
1864 }
1865
1866 return PSA_SUCCESS;
1867}
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001868#endif /* MBEDTLS_USE_PSA_CRYPTO */
1869
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02001870#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01001871int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
1872 const unsigned char *dhm_P, size_t P_len,
1873 const unsigned char *dhm_G, size_t G_len )
1874{
Janos Follath865b3eb2019-12-16 11:46:15 +00001875 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01001876
Glenn Strausscee11292021-12-20 01:43:17 -05001877 mbedtls_mpi_free( &conf->dhm_P );
1878 mbedtls_mpi_free( &conf->dhm_G );
1879
Hanno Beckera90658f2017-10-04 15:29:08 +01001880 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
1881 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
1882 {
1883 mbedtls_mpi_free( &conf->dhm_P );
1884 mbedtls_mpi_free( &conf->dhm_G );
1885 return( ret );
1886 }
1887
1888 return( 0 );
1889}
Paul Bakker5121ce52009-01-03 21:22:43 +00001890
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001891int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00001892{
Janos Follath865b3eb2019-12-16 11:46:15 +00001893 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00001894
Glenn Strausscee11292021-12-20 01:43:17 -05001895 mbedtls_mpi_free( &conf->dhm_P );
1896 mbedtls_mpi_free( &conf->dhm_G );
1897
Gilles Peskinee5702482021-06-11 21:59:08 +02001898 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
1899 &conf->dhm_P ) ) != 0 ||
1900 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
1901 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01001902 {
1903 mbedtls_mpi_free( &conf->dhm_P );
1904 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00001905 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01001906 }
Paul Bakker1b57b062011-01-06 15:48:19 +00001907
1908 return( 0 );
1909}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02001910#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00001911
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02001912#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
1913/*
1914 * Set the minimum length for Diffie-Hellman parameters
1915 */
1916void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
1917 unsigned int bitlen )
1918{
1919 conf->dhm_min_bitlen = bitlen;
1920}
1921#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
1922
Gilles Peskineeccd8882020-03-10 12:19:08 +01001923#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08001924#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02001925/*
1926 * Set allowed/preferred hashes for handshake signatures
1927 */
1928void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
1929 const int *hashes )
1930{
1931 conf->sig_hashes = hashes;
1932}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08001933#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01001934
Jerry Yuf017ee42022-01-12 15:49:48 +08001935/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01001936void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
1937 const uint16_t* sig_algs )
1938{
Jerry Yuf017ee42022-01-12 15:49:48 +08001939#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1940 conf->sig_hashes = NULL;
1941#endif /* !MBEDTLS_DEPRECATED_REMOVED */
1942 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01001943}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001944#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02001945
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02001946#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01001947#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001948/*
1949 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01001950 *
1951 * mbedtls_ssl_setup() takes the provided list
1952 * and translates it to a list of IANA TLS group identifiers,
1953 * stored in ssl->handshake->group_list.
1954 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001955 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001956void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001957 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001958{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001959 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01001960 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001961}
Brett Warrene0edc842021-08-17 09:53:13 +01001962#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01001963#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001964
Brett Warrene0edc842021-08-17 09:53:13 +01001965/*
1966 * Set the allowed groups
1967 */
1968void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
1969 const uint16_t *group_list )
1970{
1971#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
1972 conf->curve_list = NULL;
1973#endif
1974 conf->group_list = group_list;
1975}
1976
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001977#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00001979{
Hanno Becker947194e2017-04-07 13:25:49 +01001980 /* Initialize to suppress unnecessary compiler warning */
1981 size_t hostname_len = 0;
1982
1983 /* Check if new hostname is valid before
1984 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01001985 if( hostname != NULL )
1986 {
1987 hostname_len = strlen( hostname );
1988
1989 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
1990 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1991 }
1992
1993 /* Now it's clear that we will overwrite the old hostname,
1994 * so we can free it safely */
1995
1996 if( ssl->hostname != NULL )
1997 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001998 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01001999 mbedtls_free( ssl->hostname );
2000 }
2001
2002 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002003
Paul Bakker5121ce52009-01-03 21:22:43 +00002004 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002005 {
2006 ssl->hostname = NULL;
2007 }
2008 else
2009 {
2010 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002011 if( ssl->hostname == NULL )
2012 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002013
Hanno Becker947194e2017-04-07 13:25:49 +01002014 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002015
Hanno Becker947194e2017-04-07 13:25:49 +01002016 ssl->hostname[hostname_len] = '\0';
2017 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002018
2019 return( 0 );
2020}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002021#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002022
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002023#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002024void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002026 const unsigned char *, size_t),
2027 void *p_sni )
2028{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002029 conf->f_sni = f_sni;
2030 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002031}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002035int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002036{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002037 size_t cur_len, tot_len;
2038 const char **p;
2039
2040 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002041 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2042 * MUST NOT be truncated."
2043 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002044 */
2045 tot_len = 0;
2046 for( p = protos; *p != NULL; p++ )
2047 {
2048 cur_len = strlen( *p );
2049 tot_len += cur_len;
2050
Ronald Cron8216dd32020-04-23 16:41:44 +02002051 if( ( cur_len == 0 ) ||
2052 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2053 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002055 }
2056
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002057 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002058
2059 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002060}
2061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002063{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002064 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002065}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002067
Johan Pascalb62bb512015-12-03 21:56:45 +01002068#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002069void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2070 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002071{
2072 conf->dtls_srtp_mki_support = support_mki_value;
2073}
2074
Ron Eldoref72faf2018-07-12 11:54:20 +03002075int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2076 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002077 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002078{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002079 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002080 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002081 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002082 }
Ron Eldor591f1622018-01-22 12:30:04 +02002083
2084 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002085 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002086 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002087 }
Ron Eldor591f1622018-01-22 12:30:04 +02002088
2089 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2090 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002091 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002092}
2093
Ron Eldoref72faf2018-07-12 11:54:20 +03002094int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002095 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002096{
Johan Pascal253d0262020-09-22 13:04:45 +02002097 const mbedtls_ssl_srtp_profile *p;
2098 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002099
Johan Pascal253d0262020-09-22 13:04:45 +02002100 /* check the profiles list: all entry must be valid,
2101 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002102 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2103 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2104 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002105 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002106 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002107 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002108 list_size++;
2109 }
2110 else
2111 {
2112 /* unsupported value, stop parsing and set the size to an error value */
2113 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002114 }
2115 }
2116
Johan Pascal5ef72d22020-10-28 17:05:47 +01002117 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002118 {
Johan Pascal253d0262020-09-22 13:04:45 +02002119 conf->dtls_srtp_profile_list = NULL;
2120 conf->dtls_srtp_profile_list_len = 0;
2121 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2122 }
2123
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002124 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002125 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002126
2127 return( 0 );
2128}
2129
Johan Pascal5ef72d22020-10-28 17:05:47 +01002130void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2131 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002132{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002133 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2134 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002135 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002136 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002137 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002138 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002139 else
2140 {
2141 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002142 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2143 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002144 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002145}
Johan Pascalb62bb512015-12-03 21:56:45 +01002146#endif /* MBEDTLS_SSL_DTLS_SRTP */
2147
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002148void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002149{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002150 conf->max_major_ver = major;
2151 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002152}
2153
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002154void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002155{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002156 conf->min_major_ver = major;
2157 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002158}
2159
Janos Follath088ce432017-04-10 12:42:31 +01002160#if defined(MBEDTLS_SSL_SRV_C)
2161void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2162 char cert_req_ca_list )
2163{
2164 conf->cert_req_ca_list = cert_req_ca_list;
2165}
2166#endif
2167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002169void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002170{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002171 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002172}
2173#endif
2174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002176void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002177{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002178 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002179}
2180#endif
2181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002183int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002184{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002186 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002189 }
2190
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002191 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002192
2193 return( 0 );
2194}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002196
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002197void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002198{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002199 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002200}
2201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002203void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002204{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002205 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002206}
2207
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002208void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002209{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002210 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002211}
2212
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002213void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002214 const unsigned char period[8] )
2215{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002216 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002217}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002221#if defined(MBEDTLS_SSL_CLI_C)
2222void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002223{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002224 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002225}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002226#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002227
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002228#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002229void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2230 mbedtls_ssl_ticket_write_t *f_ticket_write,
2231 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2232 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002233{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002234 conf->f_ticket_write = f_ticket_write;
2235 conf->f_ticket_parse = f_ticket_parse;
2236 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002237}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002238#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002240
Hanno Becker7e6c1782021-06-08 09:24:55 +01002241void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2242 mbedtls_ssl_export_keys_t *f_export_keys,
2243 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002244{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002245 ssl->f_export_keys = f_export_keys;
2246 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002247}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002248
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002249#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002250void mbedtls_ssl_conf_async_private_cb(
2251 mbedtls_ssl_config *conf,
2252 mbedtls_ssl_async_sign_t *f_async_sign,
2253 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2254 mbedtls_ssl_async_resume_t *f_async_resume,
2255 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002256 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002257{
2258 conf->f_async_sign_start = f_async_sign;
2259 conf->f_async_decrypt_start = f_async_decrypt;
2260 conf->f_async_resume = f_async_resume;
2261 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002262 conf->p_async_config_data = async_config_data;
2263}
2264
Gilles Peskine8f97af72018-04-26 11:46:10 +02002265void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2266{
2267 return( conf->p_async_config_data );
2268}
2269
Gilles Peskine1febfef2018-04-30 11:54:39 +02002270void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002271{
2272 if( ssl->handshake == NULL )
2273 return( NULL );
2274 else
2275 return( ssl->handshake->user_async_ctx );
2276}
2277
Gilles Peskine1febfef2018-04-30 11:54:39 +02002278void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002279 void *ctx )
2280{
2281 if( ssl->handshake != NULL )
2282 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002283}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002284#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002285
Paul Bakker5121ce52009-01-03 21:22:43 +00002286/*
2287 * SSL get accessors
2288 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002289uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002290{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002291 if( ssl->session != NULL )
2292 return( ssl->session->verify_result );
2293
2294 if( ssl->session_negotiate != NULL )
2295 return( ssl->session_negotiate->verify_result );
2296
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002297 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002298}
2299
Glenn Strauss8f526902022-01-13 00:04:49 -05002300int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2301{
2302 if( ssl == NULL || ssl->session == NULL )
2303 return( 0 );
2304
2305 return( ssl->session->ciphersuite );
2306}
2307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002309{
Paul Bakker926c8e42013-03-06 10:23:34 +01002310 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002311 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002314}
2315
Gilles Peskinee1a0c252022-01-13 01:08:03 +01002316mbedtls_ssl_protocol_version mbedtls_ssl_get_version_number(
2317 const mbedtls_ssl_context *ssl )
2318{
2319 /* For major_ver, only 3 is supported, so skip checking it. */
2320 switch( ssl->minor_ver )
2321 {
2322 case MBEDTLS_SSL_MINOR_VERSION_3:
2323 return( MBEDTLS_SSL_VERSION_1_2 );
2324 case MBEDTLS_SSL_MINOR_VERSION_4:
2325 return( MBEDTLS_SSL_VERSION_1_3 );
2326 default:
2327 return( MBEDTLS_SSL_VERSION_UNKNOWN );
2328 }
2329}
2330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002332{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002334 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002335 {
2336 switch( ssl->minor_ver )
2337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002339 return( "DTLSv1.2" );
2340
2341 default:
2342 return( "unknown (DTLS)" );
2343 }
2344 }
2345#endif
2346
Paul Bakker43ca69c2011-01-15 17:35:19 +00002347 switch( ssl->minor_ver )
2348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002350 return( "TLSv1.2" );
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002351 case MBEDTLS_SSL_MINOR_VERSION_4:
2352 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002353 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002354 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002355 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002356}
2357
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002358#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002359size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2360{
David Horstmann95d516f2021-05-04 18:36:56 +01002361 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002362 size_t read_mfl;
2363
2364 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2365 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2366 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2367 {
2368 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2369 }
2370
2371 /* Check if a smaller max length was negotiated */
2372 if( ssl->session_out != NULL )
2373 {
2374 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2375 if( read_mfl < max_len )
2376 {
2377 max_len = read_mfl;
2378 }
2379 }
2380
2381 // During a handshake, use the value being negotiated
2382 if( ssl->session_negotiate != NULL )
2383 {
2384 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2385 if( read_mfl < max_len )
2386 {
2387 max_len = read_mfl;
2388 }
2389 }
2390
2391 return( max_len );
2392}
2393
2394size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002395{
2396 size_t max_len;
2397
2398 /*
2399 * Assume mfl_code is correct since it was checked when set
2400 */
Angus Grattond8213d02016-05-25 20:56:48 +10002401 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002402
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002403 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002404 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002405 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002406 {
Angus Grattond8213d02016-05-25 20:56:48 +10002407 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002408 }
2409
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002410 /* During a handshake, use the value being negotiated */
2411 if( ssl->session_negotiate != NULL &&
2412 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2413 {
2414 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2415 }
2416
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002417 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002418}
2419#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2420
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002421#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002422size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002423{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002424 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2425 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2426 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2427 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2428 return ( 0 );
2429
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002430 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2431 return( ssl->mtu );
2432
2433 if( ssl->mtu == 0 )
2434 return( ssl->handshake->mtu );
2435
2436 return( ssl->mtu < ssl->handshake->mtu ?
2437 ssl->mtu : ssl->handshake->mtu );
2438}
2439#endif /* MBEDTLS_SSL_PROTO_DTLS */
2440
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002441int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2442{
2443 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2444
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002445#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2446 !defined(MBEDTLS_SSL_PROTO_DTLS)
2447 (void) ssl;
2448#endif
2449
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002450#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002451 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002452
2453 if( max_len > mfl )
2454 max_len = mfl;
2455#endif
2456
2457#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002458 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002459 {
Hanno Becker89490712020-02-05 10:50:12 +00002460 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002461 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2462 const size_t overhead = (size_t) ret;
2463
2464 if( ret < 0 )
2465 return( ret );
2466
2467 if( mtu <= overhead )
2468 {
2469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2470 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2471 }
2472
2473 if( max_len > mtu - overhead )
2474 max_len = mtu - overhead;
2475 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002476#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002477
Hanno Becker0defedb2018-08-10 12:35:02 +01002478#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2479 !defined(MBEDTLS_SSL_PROTO_DTLS)
2480 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002481#endif
2482
2483 return( (int) max_len );
2484}
2485
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002486int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2487{
2488 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2489
2490#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2491 (void) ssl;
2492#endif
2493
2494#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2495 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2496
2497 if( max_len > mfl )
2498 max_len = mfl;
2499#endif
2500
2501 return( (int) max_len );
2502}
2503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504#if defined(MBEDTLS_X509_CRT_PARSE_C)
2505const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002506{
2507 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002508 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002509
Hanno Beckere6824572019-02-07 13:18:46 +00002510#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002511 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002512#else
2513 return( NULL );
2514#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002515}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002519int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2520 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002521{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002522 int ret;
2523
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002524 if( ssl == NULL ||
2525 dst == NULL ||
2526 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002527 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002530 }
2531
Hanno Beckere810bbc2021-05-14 16:01:05 +01002532 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2533 * idempotent: Each session can only be exported once.
2534 *
2535 * (This is in preparation for TLS 1.3 support where we will
2536 * need the ability to export multiple sessions (aka tickets),
2537 * which will be achieved by calling mbedtls_ssl_get_session()
2538 * multiple times until it fails.)
2539 *
2540 * Check whether we have already exported the current session,
2541 * and fail if so.
2542 */
2543 if( ssl->session->exported == 1 )
2544 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2545
2546 ret = mbedtls_ssl_session_copy( dst, ssl->session );
2547 if( ret != 0 )
2548 return( ret );
2549
2550 /* Remember that we've exported the session. */
2551 ssl->session->exported = 1;
2552 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002553}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002555
Paul Bakker5121ce52009-01-03 21:22:43 +00002556/*
Hanno Beckera835da52019-05-16 12:39:07 +01002557 * Define ticket header determining Mbed TLS version
2558 * and structure of the ticket.
2559 */
2560
Hanno Becker94ef3b32019-05-16 12:50:45 +01002561/*
Hanno Becker50b59662019-05-28 14:30:45 +01002562 * Define bitflag determining compile-time settings influencing
2563 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01002564 */
2565
Hanno Becker50b59662019-05-28 14:30:45 +01002566#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01002567#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01002568#else
Hanno Becker3e088662019-05-29 11:10:18 +01002569#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002570#endif /* MBEDTLS_HAVE_TIME */
2571
2572#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01002573#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002574#else
Hanno Becker3e088662019-05-29 11:10:18 +01002575#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002576#endif /* MBEDTLS_X509_CRT_PARSE_C */
2577
2578#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01002579#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002580#else
Hanno Becker3e088662019-05-29 11:10:18 +01002581#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002582#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
2583
2584#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01002585#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002586#else
Hanno Becker3e088662019-05-29 11:10:18 +01002587#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002588#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2589
Hanno Becker94ef3b32019-05-16 12:50:45 +01002590#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01002591#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002592#else
Hanno Becker3e088662019-05-29 11:10:18 +01002593#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002594#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2595
Hanno Becker94ef3b32019-05-16 12:50:45 +01002596#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2597#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
2598#else
2599#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
2600#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2601
Hanno Becker3e088662019-05-29 11:10:18 +01002602#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
2603#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
2604#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
2605#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01002606#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
2607#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01002608
Hanno Becker50b59662019-05-28 14:30:45 +01002609#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01002610 ( (uint16_t) ( \
2611 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
2612 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
2613 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
2614 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01002615 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01002616 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01002617
Hanno Beckerf8787072019-05-16 12:41:07 +01002618static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01002619 MBEDTLS_VERSION_MAJOR,
2620 MBEDTLS_VERSION_MINOR,
2621 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01002622 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
2623 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01002624};
Hanno Beckera835da52019-05-16 12:39:07 +01002625
2626/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002627 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002628 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002629 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002630 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002631 *
Hanno Beckerdce50972021-08-01 05:39:23 +01002632 * opaque mbedtls_version[3]; // library version: major, minor, patch
2633 * opaque session_format[2]; // library-version specific 16-bit field
2634 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002635 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002636 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002637 * Note: When updating the format, remember to keep
2638 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002639 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002640 * // In this version, `session_format` determines
2641 * // the setting of those compile-time
2642 * // configuration options which influence
2643 * // the structure of mbedtls_ssl_session.
2644 *
Hanno Beckerfa0d61e2021-08-02 08:56:14 +01002645 * uint8_t minor_ver; // Protocol-version. Possible values:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002646 * // - TLS 1.2 (MBEDTLS_SSL_MINOR_VERSION_3)
2647 *
2648 * select (serialized_session.minor_ver) {
2649 *
2650 * case MBEDTLS_SSL_MINOR_VERSION_3: // TLS 1.2
2651 * serialized_session_tls12 data;
2652 *
2653 * };
2654 *
2655 * } serialized_session;
2656 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002657 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002658
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002659static int ssl_session_save( const mbedtls_ssl_session *session,
2660 unsigned char omit_header,
2661 unsigned char *buf,
2662 size_t buf_len,
2663 size_t *olen )
2664{
2665 unsigned char *p = buf;
2666 size_t used = 0;
2667
2668 if( !omit_header )
2669 {
2670 /*
2671 * Add Mbed TLS version identifier
2672 */
2673
2674 used += sizeof( ssl_serialized_session_header );
2675
2676 if( used <= buf_len )
2677 {
2678 memcpy( p, ssl_serialized_session_header,
2679 sizeof( ssl_serialized_session_header ) );
2680 p += sizeof( ssl_serialized_session_header );
2681 }
2682 }
2683
2684 /*
2685 * TLS version identifier
2686 */
2687 used += 1;
2688 if( used <= buf_len )
2689 {
2690 *p++ = session->minor_ver;
2691 }
2692
2693 /* Forward to version-specific serialization routine. */
2694 switch( session->minor_ver )
2695 {
2696#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2697 case MBEDTLS_SSL_MINOR_VERSION_3:
2698 {
2699 size_t remaining_len = used <= buf_len ? buf_len - used : 0;
2700 used += ssl_session_save_tls12( session, p, remaining_len );
2701 break;
2702 }
2703#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2704
2705 default:
2706 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2707 }
2708
2709 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02002710 if( used > buf_len )
2711 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002712
2713 return( 0 );
2714}
2715
2716/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02002717 * Public wrapper for ssl_session_save()
2718 */
2719int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
2720 unsigned char *buf,
2721 size_t buf_len,
2722 size_t *olen )
2723{
2724 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
2725}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002726
Jerry Yuf1b23ca2022-02-18 11:48:47 +08002727/*
2728 * Deserialize session, see mbedtls_ssl_session_save() for format.
2729 *
2730 * This internal version is wrapped by a public function that cleans up in
2731 * case of error, and has an extra option omit_header.
2732 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002733static int ssl_session_load( mbedtls_ssl_session *session,
2734 unsigned char omit_header,
2735 const unsigned char *buf,
2736 size_t len )
2737{
2738 const unsigned char *p = buf;
2739 const unsigned char * const end = buf + len;
2740
2741 if( !omit_header )
2742 {
2743 /*
2744 * Check Mbed TLS version identifier
2745 */
2746
2747 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
2748 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2749
2750 if( memcmp( p, ssl_serialized_session_header,
2751 sizeof( ssl_serialized_session_header ) ) != 0 )
2752 {
2753 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
2754 }
2755 p += sizeof( ssl_serialized_session_header );
2756 }
2757
2758 /*
2759 * TLS version identifier
2760 */
2761 if( 1 > (size_t)( end - p ) )
2762 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2763 session->minor_ver = *p++;
2764
2765 /* Dispatch according to TLS version. */
2766 switch( session->minor_ver )
2767 {
2768#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2769 case MBEDTLS_SSL_MINOR_VERSION_3: /* TLS 1.2 */
2770 {
2771 size_t remaining_len = ( end - p );
2772 return( ssl_session_load_tls12( session, p, remaining_len ) );
2773 }
2774#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2775
2776 default:
2777 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2778 }
2779}
2780
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002781/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02002782 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02002783 */
2784int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
2785 const unsigned char *buf,
2786 size_t len )
2787{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02002788 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02002789
2790 if( ret != 0 )
2791 mbedtls_ssl_session_free( session );
2792
2793 return( ret );
2794}
2795
2796/*
Paul Bakker1961b702013-01-25 14:49:24 +01002797 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00002798 */
Hanno Becker41934dd2021-08-07 19:13:43 +01002799static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
2800{
2801 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2802
Ronald Cron66dbf912022-02-02 15:33:46 +01002803 /*
2804 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01002805 * that were written into the output buffer by the previous handshake step,
2806 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01002807 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
2808 * We proceed to the next handshake step only when all data from the
2809 * previous one have been sent to the peer, thus we make sure that this is
2810 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
2811 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
2812 * we have to wait before to go ahead.
2813 * In the case of TLS 1.3, handshake step handlers do not send data to the
2814 * peer. Data are only sent here and through
2815 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
2816 * alert occured.
2817 */
Hanno Becker41934dd2021-08-07 19:13:43 +01002818 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2819 return( ret );
2820
2821#if defined(MBEDTLS_SSL_PROTO_DTLS)
2822 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2823 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
2824 {
2825 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
2826 return( ret );
2827 }
2828#endif /* MBEDTLS_SSL_PROTO_DTLS */
2829
2830 return( ret );
2831}
2832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002834{
Hanno Becker41934dd2021-08-07 19:13:43 +01002835 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002836
Hanno Becker41934dd2021-08-07 19:13:43 +01002837 if( ssl == NULL ||
2838 ssl->conf == NULL ||
2839 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00002840 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01002841 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002842 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01002843 }
2844
2845 ret = ssl_prepare_handshake_step( ssl );
2846 if( ret != 0 )
2847 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002848
Jerry Yue7047812021-09-13 19:26:39 +08002849 ret = mbedtls_ssl_handle_pending_alert( ssl );
2850 if( ret != 0 )
2851 goto cleanup;
2852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002853#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002854 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08002855 {
Ronald Cron27c85e72022-03-08 11:37:55 +01002856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
2857 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08002858
Ronald Cron9f0fba32022-02-10 16:45:15 +01002859 switch( ssl->state )
2860 {
2861 case MBEDTLS_SSL_HELLO_REQUEST:
2862 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
2863 break;
Jerry Yub9930e72021-08-06 17:11:51 +08002864
Ronald Cron9f0fba32022-02-10 16:45:15 +01002865 case MBEDTLS_SSL_CLIENT_HELLO:
2866 ret = mbedtls_ssl_write_client_hello( ssl );
2867 break;
2868
2869 default:
2870#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
2871 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
2872 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
2873 else
2874 ret = mbedtls_ssl_handshake_client_step( ssl );
2875#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
2876 ret = mbedtls_ssl_handshake_client_step( ssl );
2877#else
2878 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
2879#endif
2880 }
Jerry Yub9930e72021-08-06 17:11:51 +08002881 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002882#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002884 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08002885 {
Ronald Cron6f135e12021-12-08 16:57:54 +01002886#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08002887 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08002888 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01002889#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08002890
2891#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2892 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
2893 ret = mbedtls_ssl_handshake_server_step( ssl );
2894#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2895 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002896#endif
2897
Jerry Yue7047812021-09-13 19:26:39 +08002898 if( ret != 0 )
2899 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08002900 /* handshake_step return error. And it is same
2901 * with alert_reason.
2902 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08002903 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08002904 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08002905 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08002906 goto cleanup;
2907 }
2908 }
2909
2910cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01002911 return( ret );
2912}
2913
2914/*
2915 * Perform the SSL handshake
2916 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01002918{
2919 int ret = 0;
2920
Hanno Beckera817ea42020-10-20 15:20:23 +01002921 /* Sanity checks */
2922
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002923 if( ssl == NULL || ssl->conf == NULL )
2924 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2925
Hanno Beckera817ea42020-10-20 15:20:23 +01002926#if defined(MBEDTLS_SSL_PROTO_DTLS)
2927 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2928 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
2929 {
2930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2931 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2932 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2933 }
2934#endif /* MBEDTLS_SSL_PROTO_DTLS */
2935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01002937
Hanno Beckera817ea42020-10-20 15:20:23 +01002938 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00002939 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01002940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002941 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002942
2943 if( ret != 0 )
2944 break;
2945 }
2946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002948
2949 return( ret );
2950}
2951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952#if defined(MBEDTLS_SSL_RENEGOTIATION)
2953#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002954/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002955 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00002956 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002958{
Janos Follath865b3eb2019-12-16 11:46:15 +00002959 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002962
2963 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2965 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002966
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002967 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002968 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002969 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002970 return( ret );
2971 }
2972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002974
2975 return( 0 );
2976}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002978
2979/*
2980 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981 * - any side: calling mbedtls_ssl_renegotiate(),
2982 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
2983 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02002984 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002985 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002987 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00002988int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00002989{
Janos Follath865b3eb2019-12-16 11:46:15 +00002990 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00002991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002993
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002994 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
2995 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00002996
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02002997 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
2998 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002999#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003000 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003001 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003002 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003003 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003004 ssl->handshake->out_msg_seq = 1;
3005 else
3006 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003007 }
3008#endif
3009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3011 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003016 return( ret );
3017 }
3018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003020
3021 return( 0 );
3022}
3023
3024/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003025 * Renegotiate current connection on client,
3026 * or request renegotiation on server
3027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003028int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003029{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003031
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003032 if( ssl == NULL || ssl->conf == NULL )
3033 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003036 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003037 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003038 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003039 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003040 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003042 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003043
3044 /* Did we already try/start sending HelloRequest? */
3045 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003047
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003048 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003049 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003050#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003053 /*
3054 * On client, either start the renegotiation process or,
3055 * if already in progress, continue the handshake
3056 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003058 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003059 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003061
Hanno Becker40cdaa12020-02-05 10:48:27 +00003062 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003063 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003064 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003065 return( ret );
3066 }
3067 }
3068 else
3069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003070 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003073 return( ret );
3074 }
3075 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003077
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003078 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003079}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003081
Gilles Peskine9b562d52018-04-25 20:32:43 +02003082void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003083{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003084 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3085
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003086 if( handshake == NULL )
3087 return;
3088
Brett Warrene0edc842021-08-17 09:53:13 +01003089#if defined(MBEDTLS_ECP_C)
3090#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3091 if ( ssl->handshake->group_list_heap_allocated )
3092 mbedtls_free( (void*) handshake->group_list );
3093 handshake->group_list = NULL;
3094#endif /* MBEDTLS_DEPRECATED_REMOVED */
3095#endif /* MBEDTLS_ECP_C */
3096
Jerry Yuf017ee42022-01-12 15:49:48 +08003097#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3098#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3099 if ( ssl->handshake->sig_algs_heap_allocated )
3100 mbedtls_free( (void*) handshake->sig_algs );
3101 handshake->sig_algs = NULL;
3102#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003103#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3104 if( ssl->handshake->certificate_request_context )
3105 {
3106 mbedtls_free( (void*) handshake->certificate_request_context );
3107 }
3108#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003109#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3110
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003111#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3112 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3113 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003114 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003115 handshake->async_in_progress = 0;
3116 }
3117#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3118
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003119#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003120#if defined(MBEDTLS_USE_PSA_CRYPTO)
3121 psa_hash_abort( &handshake->fin_sha256_psa );
3122#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003123 mbedtls_sha256_free( &handshake->fin_sha256 );
3124#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003125#endif
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +02003126#if defined(MBEDTLS_SHA384_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003127#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003128 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003129#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003130 mbedtls_sha512_free( &handshake->fin_sha512 );
3131#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003132#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134#if defined(MBEDTLS_DHM_C)
3135 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003136#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137#if defined(MBEDTLS_ECDH_C)
3138 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003139#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003140#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003141 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003142#if defined(MBEDTLS_SSL_CLI_C)
3143 mbedtls_free( handshake->ecjpake_cache );
3144 handshake->ecjpake_cache = NULL;
3145 handshake->ecjpake_cache_len = 0;
3146#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003147#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003148
Janos Follath4ae5c292016-02-10 11:27:43 +00003149#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3150 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003151 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003153#endif
3154
Gilles Peskineeccd8882020-03-10 12:19:08 +01003155#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003156 if( handshake->psk != NULL )
3157 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003158 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003159 mbedtls_free( handshake->psk );
3160 }
3161#endif
3162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003163#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3164 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003165 /*
3166 * Free only the linked list wrapper, not the keys themselves
3167 * since the belong to the SNI callback
3168 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003169 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003171
Gilles Peskineeccd8882020-03-10 12:19:08 +01003172#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003173 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003174 if( handshake->ecrs_peer_cert != NULL )
3175 {
3176 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3177 mbedtls_free( handshake->ecrs_peer_cert );
3178 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003179#endif
3180
Hanno Becker75173122019-02-06 16:18:31 +00003181#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3182 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3183 mbedtls_pk_free( &handshake->peer_pubkey );
3184#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3185
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003186#if defined(MBEDTLS_SSL_CLI_C) && \
3187 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3188 mbedtls_free( handshake->cookie );
3189#endif /* MBEDTLS_SSL_CLI_C &&
3190 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003191
3192#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003193 mbedtls_ssl_flight_free( handshake->flight );
3194 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003195#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003196
Ronald Cronf12b81d2022-03-15 10:42:41 +01003197#if defined(MBEDTLS_ECDH_C) && \
3198 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003199 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003200 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003201#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3202
Ronald Cron6f135e12021-12-08 16:57:54 +01003203#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003204 mbedtls_ssl_transform_free( handshake->transform_handshake );
3205 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003206 mbedtls_free( handshake->transform_earlydata );
3207 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003208#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003209
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003210
3211#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3212 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3213 * processes datagrams and the fact that a datagram is allowed to have
3214 * several records in it, it is possible that the I/O buffers are not
3215 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003216 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3217 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003218#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003219
Jerry Yuba9c7272021-10-30 11:54:10 +08003220 /* mbedtls_platform_zeroize MUST be last one in this function */
3221 mbedtls_platform_zeroize( handshake,
3222 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003223}
3224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003226{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003227 if( session == NULL )
3228 return;
3229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003231 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003232#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003233
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003234#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003236#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003237
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003238 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003239}
3240
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003241#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003242
3243#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3244#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3245#else
3246#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3247#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3248
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003249#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003250
3251#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3252#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3253#else
3254#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3255#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3256
3257#if defined(MBEDTLS_SSL_ALPN)
3258#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3259#else
3260#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3261#endif /* MBEDTLS_SSL_ALPN */
3262
3263#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3264#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3265#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3266#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3267
3268#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3269 ( (uint32_t) ( \
3270 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3271 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3272 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3273 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3274 0u ) )
3275
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003276static unsigned char ssl_serialized_context_header[] = {
3277 MBEDTLS_VERSION_MAJOR,
3278 MBEDTLS_VERSION_MINOR,
3279 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003280 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3281 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3282 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3283 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3284 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003285};
3286
Paul Bakker5121ce52009-01-03 21:22:43 +00003287/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003288 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003289 *
3290 * The format of the serialized data is:
3291 * (in the presentation language of TLS, RFC 8446 section 3)
3292 *
3293 * // header
3294 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003295 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003296 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003297 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003298 * Note: When updating the format, remember to keep these
3299 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003300 *
3301 * // session sub-structure
3302 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3303 * // transform sub-structure
3304 * uint8 random[64]; // ServerHello.random+ClientHello.random
3305 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3306 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3307 * // fields from ssl_context
3308 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3309 * uint64 in_window_top; // DTLS: last validated record seq_num
3310 * uint64 in_window; // DTLS: bitmask for replay protection
3311 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3312 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3313 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3314 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3315 *
3316 * Note that many fields of the ssl_context or sub-structures are not
3317 * serialized, as they fall in one of the following categories:
3318 *
3319 * 1. forced value (eg in_left must be 0)
3320 * 2. pointer to dynamically-allocated memory (eg session, transform)
3321 * 3. value can be re-derived from other data (eg session keys from MS)
3322 * 4. value was temporary (eg content of input buffer)
3323 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003324 */
3325int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3326 unsigned char *buf,
3327 size_t buf_len,
3328 size_t *olen )
3329{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003330 unsigned char *p = buf;
3331 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003332 size_t session_len;
3333 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003334
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003335 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003336 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3337 * this function's documentation.
3338 *
3339 * These are due to assumptions/limitations in the implementation. Some of
3340 * them are likely to stay (no handshake in progress) some might go away
3341 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003342 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003343 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003344 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003345 {
3346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003347 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003348 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003349 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003350 {
3351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003352 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003353 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003354 /* Double-check that sub-structures are indeed ready */
3355 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003356 {
3357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003358 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003359 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003360 /* There must be no pending incoming or outgoing data */
3361 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003362 {
3363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003364 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003365 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003366 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003367 {
3368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003369 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003370 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003371 /* Protocol must be DLTS, not TLS */
3372 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003373 {
3374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003375 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003376 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003377 /* Version must be 1.2 */
3378 if( ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003379 {
3380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003381 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003382 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003383 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003384 {
3385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003386 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003387 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003388 /* We must be using an AEAD ciphersuite */
3389 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003390 {
3391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003392 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003393 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003394 /* Renegotiation must not be enabled */
3395#if defined(MBEDTLS_SSL_RENEGOTIATION)
3396 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003397 {
3398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003399 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003400 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003401#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003402
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003403 /*
3404 * Version and format identifier
3405 */
3406 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003407
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003408 if( used <= buf_len )
3409 {
3410 memcpy( p, ssl_serialized_context_header,
3411 sizeof( ssl_serialized_context_header ) );
3412 p += sizeof( ssl_serialized_context_header );
3413 }
3414
3415 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003416 * Session (length + data)
3417 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003418 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003419 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3420 return( ret );
3421
3422 used += 4 + session_len;
3423 if( used <= buf_len )
3424 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003425 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3426 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003427
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003428 ret = ssl_session_save( ssl->session, 1,
3429 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003430 if( ret != 0 )
3431 return( ret );
3432
3433 p += session_len;
3434 }
3435
3436 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003437 * Transform
3438 */
3439 used += sizeof( ssl->transform->randbytes );
3440 if( used <= buf_len )
3441 {
3442 memcpy( p, ssl->transform->randbytes,
3443 sizeof( ssl->transform->randbytes ) );
3444 p += sizeof( ssl->transform->randbytes );
3445 }
3446
3447#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3448 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3449 if( used <= buf_len )
3450 {
3451 *p++ = ssl->transform->in_cid_len;
3452 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3453 p += ssl->transform->in_cid_len;
3454
3455 *p++ = ssl->transform->out_cid_len;
3456 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3457 p += ssl->transform->out_cid_len;
3458 }
3459#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3460
3461 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003462 * Saved fields from top-level ssl_context structure
3463 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003464 used += 4;
3465 if( used <= buf_len )
3466 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003467 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3468 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003469 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003470
3471#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3472 used += 16;
3473 if( used <= buf_len )
3474 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003475 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3476 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003477
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003478 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3479 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003480 }
3481#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3482
3483#if defined(MBEDTLS_SSL_PROTO_DTLS)
3484 used += 1;
3485 if( used <= buf_len )
3486 {
3487 *p++ = ssl->disable_datagram_packing;
3488 }
3489#endif /* MBEDTLS_SSL_PROTO_DTLS */
3490
Jerry Yuae0b2e22021-10-08 15:21:19 +08003491 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003492 if( used <= buf_len )
3493 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003494 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3495 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003496 }
3497
3498#if defined(MBEDTLS_SSL_PROTO_DTLS)
3499 used += 2;
3500 if( used <= buf_len )
3501 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003502 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
3503 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003504 }
3505#endif /* MBEDTLS_SSL_PROTO_DTLS */
3506
3507#if defined(MBEDTLS_SSL_ALPN)
3508 {
3509 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02003510 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003511 : 0;
3512
3513 used += 1 + alpn_len;
3514 if( used <= buf_len )
3515 {
3516 *p++ = alpn_len;
3517
3518 if( ssl->alpn_chosen != NULL )
3519 {
3520 memcpy( p, ssl->alpn_chosen, alpn_len );
3521 p += alpn_len;
3522 }
3523 }
3524 }
3525#endif /* MBEDTLS_SSL_ALPN */
3526
3527 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003528 * Done
3529 */
3530 *olen = used;
3531
3532 if( used > buf_len )
3533 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003534
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003535 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
3536
Hanno Becker43aefe22020-02-05 10:44:56 +00003537 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003538}
3539
3540/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003541 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003542 *
3543 * This internal version is wrapped by a public function that cleans up in
3544 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003545 */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003546static int ssl_context_load( mbedtls_ssl_context *ssl,
3547 const unsigned char *buf,
3548 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003549{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003550 const unsigned char *p = buf;
3551 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003552 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00003553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003554
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003555 /*
3556 * The context should have been freshly setup or reset.
3557 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02003558 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003559 * renegotiating, or if the user mistakenly loaded a session first.)
3560 */
3561 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
3562 ssl->session != NULL )
3563 {
3564 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3565 }
3566
3567 /*
3568 * We can't check that the config matches the initial one, but we can at
3569 * least check it matches the requirements for serializing.
3570 */
3571 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
3572 ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
3573 ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 ||
3574 ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ||
3575 ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 ||
3576#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003577 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003578#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003579 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003580 {
3581 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3582 }
3583
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003584 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
3585
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003586 /*
3587 * Check version identifier
3588 */
3589 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
3590 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3591
3592 if( memcmp( p, ssl_serialized_context_header,
3593 sizeof( ssl_serialized_context_header ) ) != 0 )
3594 {
3595 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3596 }
3597 p += sizeof( ssl_serialized_context_header );
3598
3599 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003600 * Session
3601 */
3602 if( (size_t)( end - p ) < 4 )
3603 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3604
3605 session_len = ( (size_t) p[0] << 24 ) |
3606 ( (size_t) p[1] << 16 ) |
3607 ( (size_t) p[2] << 8 ) |
3608 ( (size_t) p[3] );
3609 p += 4;
3610
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003611 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00003612 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003613 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003614 ssl->session_in = ssl->session;
3615 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003616 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003617
3618 if( (size_t)( end - p ) < session_len )
3619 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3620
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003621 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003622 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003623 {
3624 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003625 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003626 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003627
3628 p += session_len;
3629
3630 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003631 * Transform
3632 */
3633
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003634 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00003635 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003636 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003637 ssl->transform_in = ssl->transform;
3638 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003639 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003640
3641 /* Read random bytes and populate structure */
3642 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
3643 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08003644#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00003645 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003646 ssl->session->ciphersuite,
3647 ssl->session->master,
Hanno Beckerbd257552021-03-22 06:59:27 +00003648#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
3649 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003650 ssl->session->encrypt_then_mac,
Hanno Beckerbd257552021-03-22 06:59:27 +00003651#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
3652 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003653 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
3654 p, /* currently pointing to randbytes */
3655 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
3656 ssl->conf->endpoint,
3657 ssl );
3658 if( ret != 0 )
3659 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08003660#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003661 p += sizeof( ssl->transform->randbytes );
3662
3663#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3664 /* Read connection IDs and store them */
3665 if( (size_t)( end - p ) < 1 )
3666 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3667
3668 ssl->transform->in_cid_len = *p++;
3669
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02003670 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003671 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3672
3673 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
3674 p += ssl->transform->in_cid_len;
3675
3676 ssl->transform->out_cid_len = *p++;
3677
3678 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
3679 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3680
3681 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
3682 p += ssl->transform->out_cid_len;
3683#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3684
3685 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003686 * Saved fields from top-level ssl_context structure
3687 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003688 if( (size_t)( end - p ) < 4 )
3689 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3690
3691 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
3692 ( (uint32_t) p[1] << 16 ) |
3693 ( (uint32_t) p[2] << 8 ) |
3694 ( (uint32_t) p[3] );
3695 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003696
3697#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3698 if( (size_t)( end - p ) < 16 )
3699 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3700
3701 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
3702 ( (uint64_t) p[1] << 48 ) |
3703 ( (uint64_t) p[2] << 40 ) |
3704 ( (uint64_t) p[3] << 32 ) |
3705 ( (uint64_t) p[4] << 24 ) |
3706 ( (uint64_t) p[5] << 16 ) |
3707 ( (uint64_t) p[6] << 8 ) |
3708 ( (uint64_t) p[7] );
3709 p += 8;
3710
3711 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
3712 ( (uint64_t) p[1] << 48 ) |
3713 ( (uint64_t) p[2] << 40 ) |
3714 ( (uint64_t) p[3] << 32 ) |
3715 ( (uint64_t) p[4] << 24 ) |
3716 ( (uint64_t) p[5] << 16 ) |
3717 ( (uint64_t) p[6] << 8 ) |
3718 ( (uint64_t) p[7] );
3719 p += 8;
3720#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3721
3722#if defined(MBEDTLS_SSL_PROTO_DTLS)
3723 if( (size_t)( end - p ) < 1 )
3724 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3725
3726 ssl->disable_datagram_packing = *p++;
3727#endif /* MBEDTLS_SSL_PROTO_DTLS */
3728
Jerry Yud9a94fe2021-09-28 18:58:59 +08003729 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003730 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08003731 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
3732 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003733
3734#if defined(MBEDTLS_SSL_PROTO_DTLS)
3735 if( (size_t)( end - p ) < 2 )
3736 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3737
3738 ssl->mtu = ( p[0] << 8 ) | p[1];
3739 p += 2;
3740#endif /* MBEDTLS_SSL_PROTO_DTLS */
3741
3742#if defined(MBEDTLS_SSL_ALPN)
3743 {
3744 uint8_t alpn_len;
3745 const char **cur;
3746
3747 if( (size_t)( end - p ) < 1 )
3748 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3749
3750 alpn_len = *p++;
3751
3752 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
3753 {
3754 /* alpn_chosen should point to an item in the configured list */
3755 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
3756 {
3757 if( strlen( *cur ) == alpn_len &&
3758 memcmp( p, cur, alpn_len ) == 0 )
3759 {
3760 ssl->alpn_chosen = *cur;
3761 break;
3762 }
3763 }
3764 }
3765
3766 /* can only happen on conf mismatch */
3767 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
3768 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3769
3770 p += alpn_len;
3771 }
3772#endif /* MBEDTLS_SSL_ALPN */
3773
3774 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003775 * Forced fields from top-level ssl_context structure
3776 *
3777 * Most of them already set to the correct value by mbedtls_ssl_init() and
3778 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
3779 */
3780 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
3781
3782 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
3783 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
3784
Hanno Becker361b10d2019-08-30 10:42:49 +01003785 /* Adjust pointers for header fields of outgoing records to
3786 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00003787 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01003788
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003789#if defined(MBEDTLS_SSL_PROTO_DTLS)
3790 ssl->in_epoch = 1;
3791#endif
3792
3793 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
3794 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00003795 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
3796 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003797 if( ssl->handshake != NULL )
3798 {
3799 mbedtls_ssl_handshake_free( ssl );
3800 mbedtls_free( ssl->handshake );
3801 ssl->handshake = NULL;
3802 }
3803
3804 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003805 * Done - should have consumed entire buffer
3806 */
3807 if( p != end )
3808 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003809
3810 return( 0 );
3811}
3812
3813/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003814 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003815 */
3816int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
3817 const unsigned char *buf,
3818 size_t len )
3819{
3820 int ret = ssl_context_load( context, buf, len );
3821
3822 if( ret != 0 )
3823 mbedtls_ssl_free( context );
3824
3825 return( ret );
3826}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003827#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003828
3829/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003830 * Free an SSL context
3831 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003832void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003833{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003834 if( ssl == NULL )
3835 return;
3836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003838
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003839 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003840 {
sander-visserb8aa2072020-05-06 22:05:13 +02003841#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3842 size_t out_buf_len = ssl->out_buf_len;
3843#else
3844 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
3845#endif
3846
Darryl Greenb33cc762019-11-28 14:29:44 +00003847 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003848 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003849 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003850 }
3851
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003852 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003853 {
sander-visserb8aa2072020-05-06 22:05:13 +02003854#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3855 size_t in_buf_len = ssl->in_buf_len;
3856#else
3857 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
3858#endif
3859
Darryl Greenb33cc762019-11-28 14:29:44 +00003860 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003862 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003863 }
3864
Paul Bakker48916f92012-09-16 19:57:18 +00003865 if( ssl->transform )
3866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003867 mbedtls_ssl_transform_free( ssl->transform );
3868 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003869 }
3870
3871 if( ssl->handshake )
3872 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02003873 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874 mbedtls_ssl_transform_free( ssl->transform_negotiate );
3875 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00003876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003877 mbedtls_free( ssl->handshake );
3878 mbedtls_free( ssl->transform_negotiate );
3879 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00003880 }
3881
Ronald Cron6f135e12021-12-08 16:57:54 +01003882#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01003883 mbedtls_ssl_transform_free( ssl->transform_application );
3884 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01003885#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01003886
Paul Bakkerc0463502013-02-14 11:19:38 +01003887 if( ssl->session )
3888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003889 mbedtls_ssl_session_free( ssl->session );
3890 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003891 }
3892
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02003893#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02003894 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003895 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003896 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003897 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00003898 }
Paul Bakker0be444a2013-08-27 21:55:01 +02003899#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003900
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003901#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003902 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02003903#endif
3904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00003906
Paul Bakker86f04f42013-02-14 11:20:09 +01003907 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003908 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003909}
3910
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02003911/*
3912 * Initialze mbedtls_ssl_config
3913 */
3914void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
3915{
3916 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
3917}
3918
Gilles Peskineeccd8882020-03-10 12:19:08 +01003919#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003920#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskineae270bf2021-06-02 00:05:29 +02003921/* The selection should be the same as mbedtls_x509_crt_profile_default in
Gilles Peskinea28f0f52021-06-02 15:29:38 +02003922 * x509_crt.c. Here, the order matters. Currently we favor stronger hashes,
3923 * for no fundamental reason.
Gilles Peskineae270bf2021-06-02 00:05:29 +02003924 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
3925 * about this list. */
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003926static int ssl_preset_default_hashes[] = {
3927#if defined(MBEDTLS_SHA512_C)
3928 MBEDTLS_MD_SHA512,
Mateusz Starzyk3352a532021-04-06 14:28:22 +02003929#endif
3930#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003931 MBEDTLS_MD_SHA384,
3932#endif
3933#if defined(MBEDTLS_SHA256_C)
3934 MBEDTLS_MD_SHA256,
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02003935#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003936 MBEDTLS_MD_NONE
3937};
Jerry Yuf017ee42022-01-12 15:49:48 +08003938#endif /* !MBEDTLS_DEPRECATED_REMOVED */
3939#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003940
Gilles Peskineae270bf2021-06-02 00:05:29 +02003941/* The selection should be the same as mbedtls_x509_crt_profile_default in
3942 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02003943 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02003944 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02003945 * about this list.
3946 */
Brett Warrene0edc842021-08-17 09:53:13 +01003947static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02003948#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003949 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003950#endif
3951#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003952 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003953#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02003954#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003955 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003956#endif
3957#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003958 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003959#endif
3960#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003961 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003962#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02003963#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003964 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003965#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02003966#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003967 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003968#endif
3969#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003970 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003971#endif
Brett Warrene0edc842021-08-17 09:53:13 +01003972 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02003973};
Gilles Peskineae270bf2021-06-02 00:05:29 +02003974
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003975static int ssl_preset_suiteb_ciphersuites[] = {
3976 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
3977 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
3978 0
3979};
3980
Gilles Peskineeccd8882020-03-10 12:19:08 +01003981#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003982#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003983static int ssl_preset_suiteb_hashes[] = {
Jerry Yuf017ee42022-01-12 15:49:48 +08003984#if defined(MBEDTLS_SHA256_C)
3985 MBEDTLS_MD_SHA256,
3986#endif
Jerry Yu713013f2022-01-17 18:16:35 +08003987#if defined(MBEDTLS_SHA384_C)
3988 MBEDTLS_MD_SHA384,
3989#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003990 MBEDTLS_MD_NONE
3991};
Jerry Yuf017ee42022-01-12 15:49:48 +08003992#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01003993
Jerry Yu909df7b2022-01-22 11:56:27 +08003994/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08003995 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08003996 * rules SHOULD be upheld.
3997 * - No duplicate entries.
3998 * - But if there is a good reason, do not change the order of the algorithms.
3999 * - ssl_tls12_present* is for TLS 1.2 use only.
4000 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004001 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004002static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004003
Jerry Yued5e9f42022-01-26 11:21:34 +08004004#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \
4005 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4006 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
4007#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C &&
4008 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004009
Jerry Yu909df7b2022-01-22 11:56:27 +08004010#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \
4011 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4012 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
4013#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
4014 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4015
Jerry Yued5e9f42022-01-26 11:21:34 +08004016#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA512_C) && \
4017 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4018 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
4019#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
4020 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004021
Jerry Yu909df7b2022-01-22 11:56:27 +08004022#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C)
4023 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4024#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */
4025
Ronald Cron8540cf62022-03-16 08:01:09 +01004026#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA512_C)
4027 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4028#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4029
4030#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA384_C)
4031 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4032#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4033
Jerry Yu53037892022-01-25 11:02:06 +08004034#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C)
4035 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4036#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4037
Jerry Yu909df7b2022-01-22 11:56:27 +08004038 MBEDTLS_TLS1_3_SIG_NONE
4039};
4040
4041/* NOTICE: see above */
4042#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4043static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Jerry Yu713013f2022-01-17 18:16:35 +08004044#if defined(MBEDTLS_SHA512_C)
4045 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA512 )
4046#endif
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004047#if defined(MBEDTLS_SHA384_C)
4048 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 )
4049#endif
4050#if defined(MBEDTLS_SHA256_C)
4051 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 )
4052#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08004053 MBEDTLS_TLS1_3_SIG_NONE
4054};
4055#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4056/* NOTICE: see above */
4057static uint16_t ssl_preset_suiteb_sig_algs[] = {
4058
Jerry Yu909df7b2022-01-22 11:56:27 +08004059#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \
4060 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4061 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
4062#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C &&
4063 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4064
Jerry Yu53037892022-01-25 11:02:06 +08004065#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \
4066 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4067 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
4068#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
4069 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004070
4071#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C)
4072 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4073#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004074
Jerry Yu53037892022-01-25 11:02:06 +08004075#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C)
4076 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4077#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4078
Jerry Yu713013f2022-01-17 18:16:35 +08004079 MBEDTLS_TLS1_3_SIG_NONE
4080};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004081
Jerry Yu909df7b2022-01-22 11:56:27 +08004082/* NOTICE: see above */
4083#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4084static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Jerry Yu713013f2022-01-17 18:16:35 +08004085#if defined(MBEDTLS_SHA256_C)
4086 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 )
4087#endif
Jerry Yu18c833e2022-01-25 10:55:47 +08004088#if defined(MBEDTLS_SHA384_C)
4089 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 )
4090#endif
Xiaofei Bai95395012021-11-25 08:51:30 +00004091 MBEDTLS_TLS1_3_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004092};
Jerry Yu909df7b2022-01-22 11:56:27 +08004093#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4094
Jerry Yu1a8b4812022-01-20 17:56:50 +08004095#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004096
Brett Warrene0edc842021-08-17 09:53:13 +01004097static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004098#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004099 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004100#endif
4101#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004102 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004103#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004104 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004105};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004106
Jerry Yu1a8b4812022-01-20 17:56:50 +08004107#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004108/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004109 * to make sure there are no duplicated signature algorithm entries. */
Jerry Yuf377d642022-01-25 10:43:59 +08004110static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004111{
4112 size_t i, j;
4113 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004114
Jerry Yuf377d642022-01-25 10:43:59 +08004115 for( i = 0; sig_algs[i] != MBEDTLS_TLS1_3_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004116 {
Jerry Yuf377d642022-01-25 10:43:59 +08004117 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004118 {
Jerry Yuf377d642022-01-25 10:43:59 +08004119 if( sig_algs[i] != sig_algs[j] )
4120 continue;
4121 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4122 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4123 sig_algs[i], j, i );
4124 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004125 }
4126 }
4127 return( ret );
4128}
4129
4130#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4131
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004132/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004133 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004134 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004135int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004136 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004137{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004138#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004139 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004140#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004141
Jerry Yu1a8b4812022-01-20 17:56:50 +08004142#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004143 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004144 {
4145 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4146 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4147 }
4148
Jerry Yuf377d642022-01-25 10:43:59 +08004149 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004150 {
4151 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4152 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4153 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004154
4155#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004156 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004157 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004158 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004159 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4160 }
4161
Jerry Yuf377d642022-01-25 10:43:59 +08004162 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004163 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004164 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004165 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4166 }
4167#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004168#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4169
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004170 /* Use the functions here so that they are covered in tests,
4171 * but otherwise access member directly for efficiency */
4172 mbedtls_ssl_conf_endpoint( conf, endpoint );
4173 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004174
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004175 /*
4176 * Things that are common to all presets
4177 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004178#if defined(MBEDTLS_SSL_CLI_C)
4179 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4180 {
4181 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4182#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4183 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4184#endif
4185 }
4186#endif
4187
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004188#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4189 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4190#endif
4191
4192#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4193 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4194#endif
4195
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004196#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004197 conf->f_cookie_write = ssl_cookie_write_dummy;
4198 conf->f_cookie_check = ssl_cookie_check_dummy;
4199#endif
4200
4201#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4202 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4203#endif
4204
Janos Follath088ce432017-04-10 12:42:31 +01004205#if defined(MBEDTLS_SSL_SRV_C)
4206 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004207 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004208#endif
4209
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004210#if defined(MBEDTLS_SSL_PROTO_DTLS)
4211 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4212 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4213#endif
4214
4215#if defined(MBEDTLS_SSL_RENEGOTIATION)
4216 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004217 memset( conf->renego_period, 0x00, 2 );
4218 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004219#endif
4220
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004221#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004222 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4223 {
4224 const unsigned char dhm_p[] =
4225 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4226 const unsigned char dhm_g[] =
4227 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004228
Hanno Beckere2defad2021-07-24 05:59:17 +01004229 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4230 dhm_p, sizeof( dhm_p ),
4231 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4232 {
4233 return( ret );
4234 }
4235 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004236#endif
4237
Ronald Cron6f135e12021-12-08 16:57:54 +01004238#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker71f1ed62021-07-24 06:01:47 +01004239 /*
4240 * Allow all TLS 1.3 key exchange modes by default.
4241 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004242 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004243#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004244
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004245 /*
4246 * Preset-specific defaults
4247 */
4248 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004249 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004250 /*
4251 * NSA Suite B
4252 */
4253 case MBEDTLS_SSL_PRESET_SUITEB:
Ronald Crone71639d2022-03-11 11:31:31 +01004254 conf->min_major_ver = MBEDTLS_SSL_MIN_MAJOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004255 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004256
Ronald Cronf6606552022-03-15 11:23:25 +01004257 if( ( endpoint == MBEDTLS_SSL_IS_SERVER ) ||
4258 ( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) )
4259#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4260 {
4261 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
4262 conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
4263 }
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004264#else
Ronald Cronf6606552022-03-15 11:23:25 +01004265 {
4266 conf->min_major_ver = 0;
4267 conf->max_major_ver = 0;
4268 conf->min_minor_ver = 0;
4269 conf->max_minor_ver = 0;
Ronald Cron1fa4f682022-03-30 17:35:18 +02004270 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ronald Cronf6606552022-03-15 11:23:25 +01004271 }
4272#endif
4273 else
4274 {
4275 conf->min_minor_ver = MBEDTLS_SSL_MIN_MINOR_VERSION;
4276 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
4277 }
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004278 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004279
4280#if defined(MBEDTLS_X509_CRT_PARSE_C)
4281 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004282#endif
4283
Gilles Peskineeccd8882020-03-10 12:19:08 +01004284#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004285#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004286 conf->sig_hashes = ssl_preset_suiteb_hashes;
Jerry Yuf017ee42022-01-12 15:49:48 +08004287#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004288#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4289 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4290 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4291 else
4292#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4293 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004294#endif
4295
Brett Warrene0edc842021-08-17 09:53:13 +01004296#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4297 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004298#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004299 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004300 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004301
4302 /*
4303 * Default
4304 */
4305 default:
Ronald Crone71639d2022-03-11 11:31:31 +01004306 conf->min_major_ver = MBEDTLS_SSL_MIN_MAJOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004307 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004308
Ronald Cronf6606552022-03-15 11:23:25 +01004309 if( ( endpoint == MBEDTLS_SSL_IS_SERVER ) ||
4310 ( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) )
4311#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4312 {
TRodziewiczef73f012021-05-13 14:53:36 +02004313 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Ronald Cronf6606552022-03-15 11:23:25 +01004314 conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
4315 }
4316#else
4317 {
4318 conf->min_major_ver = 0;
4319 conf->max_major_ver = 0;
4320 conf->min_minor_ver = 0;
4321 conf->max_minor_ver = 0;
Ronald Cron1fa4f682022-03-30 17:35:18 +02004322 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ronald Cronf6606552022-03-15 11:23:25 +01004323 }
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004324#endif
Ronald Cronf6606552022-03-15 11:23:25 +01004325 else
4326 {
4327 conf->min_minor_ver = MBEDTLS_SSL_MIN_MINOR_VERSION;
4328 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
4329 }
4330
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004331 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004332
4333#if defined(MBEDTLS_X509_CRT_PARSE_C)
4334 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4335#endif
4336
Gilles Peskineeccd8882020-03-10 12:19:08 +01004337#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004338#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01004339 conf->sig_hashes = ssl_preset_default_hashes;
Jerry Yuf017ee42022-01-12 15:49:48 +08004340#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004341#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4342 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4343 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4344 else
4345#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4346 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004347#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004348
Brett Warrene0edc842021-08-17 09:53:13 +01004349#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4350 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004351#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004352 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004353
4354#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4355 conf->dhm_min_bitlen = 1024;
4356#endif
4357 }
4358
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004359 return( 0 );
4360}
4361
4362/*
4363 * Free mbedtls_ssl_config
4364 */
4365void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4366{
4367#if defined(MBEDTLS_DHM_C)
4368 mbedtls_mpi_free( &conf->dhm_P );
4369 mbedtls_mpi_free( &conf->dhm_G );
4370#endif
4371
Gilles Peskineeccd8882020-03-10 12:19:08 +01004372#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004373 if( conf->psk != NULL )
4374 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004375 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004376 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004377 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004378 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004379 }
4380
4381 if( conf->psk_identity != NULL )
4382 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004383 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004384 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004385 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004386 conf->psk_identity_len = 0;
4387 }
4388#endif
4389
4390#if defined(MBEDTLS_X509_CRT_PARSE_C)
4391 ssl_key_cert_free( conf->key_cert );
4392#endif
4393
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004394 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004395}
4396
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004397#if defined(MBEDTLS_PK_C) && \
4398 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004399/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004400 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004401 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004402unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004403{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004404#if defined(MBEDTLS_RSA_C)
4405 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4406 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004407#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004408#if defined(MBEDTLS_ECDSA_C)
4409 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4410 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004411#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004412 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004413}
4414
Hanno Becker7e5437a2017-04-28 17:15:26 +01004415unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4416{
4417 switch( type ) {
4418 case MBEDTLS_PK_RSA:
4419 return( MBEDTLS_SSL_SIG_RSA );
4420 case MBEDTLS_PK_ECDSA:
4421 case MBEDTLS_PK_ECKEY:
4422 return( MBEDTLS_SSL_SIG_ECDSA );
4423 default:
4424 return( MBEDTLS_SSL_SIG_ANON );
4425 }
4426}
4427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004428mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004429{
4430 switch( sig )
4431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004432#if defined(MBEDTLS_RSA_C)
4433 case MBEDTLS_SSL_SIG_RSA:
4434 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004435#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004436#if defined(MBEDTLS_ECDSA_C)
4437 case MBEDTLS_SSL_SIG_ECDSA:
4438 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004439#endif
4440 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004441 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004442 }
4443}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004444#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004445
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004446/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004447 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004449mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004450{
4451 switch( hash )
4452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004453#if defined(MBEDTLS_MD5_C)
4454 case MBEDTLS_SSL_HASH_MD5:
4455 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004456#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004457#if defined(MBEDTLS_SHA1_C)
4458 case MBEDTLS_SSL_HASH_SHA1:
4459 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004460#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004461#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004462 case MBEDTLS_SSL_HASH_SHA224:
4463 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004464#endif
4465#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004466 case MBEDTLS_SSL_HASH_SHA256:
4467 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004468#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004469#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004470 case MBEDTLS_SSL_HASH_SHA384:
4471 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004472#endif
4473#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004474 case MBEDTLS_SSL_HASH_SHA512:
4475 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004476#endif
4477 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004478 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004479 }
4480}
4481
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004482/*
4483 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4484 */
4485unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4486{
4487 switch( md )
4488 {
4489#if defined(MBEDTLS_MD5_C)
4490 case MBEDTLS_MD_MD5:
4491 return( MBEDTLS_SSL_HASH_MD5 );
4492#endif
4493#if defined(MBEDTLS_SHA1_C)
4494 case MBEDTLS_MD_SHA1:
4495 return( MBEDTLS_SSL_HASH_SHA1 );
4496#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004497#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004498 case MBEDTLS_MD_SHA224:
4499 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004500#endif
4501#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004502 case MBEDTLS_MD_SHA256:
4503 return( MBEDTLS_SSL_HASH_SHA256 );
4504#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004505#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004506 case MBEDTLS_MD_SHA384:
4507 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004508#endif
4509#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004510 case MBEDTLS_MD_SHA512:
4511 return( MBEDTLS_SSL_HASH_SHA512 );
4512#endif
4513 default:
4514 return( MBEDTLS_SSL_HASH_NONE );
4515 }
4516}
4517
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004518/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004519 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004520 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004521 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004522int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004523{
Brett Warrene0edc842021-08-17 09:53:13 +01004524 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004525
Brett Warrene0edc842021-08-17 09:53:13 +01004526 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004527 return( -1 );
4528
Brett Warrene0edc842021-08-17 09:53:13 +01004529 for( ; *group_list != 0; group_list++ )
4530 {
4531 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004532 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01004533 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004534
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004535 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004536}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004537
4538#if defined(MBEDTLS_ECP_C)
4539/*
4540 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
4541 */
4542int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
4543{
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01004544 uint16_t tls_id = mbedtls_ecp_curve_info_from_grp_id( grp_id )->tls_id;
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004545 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
4546}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004547#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004549#if defined(MBEDTLS_X509_CRT_PARSE_C)
4550int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
4551 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004552 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02004553 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004554{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004555 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004556 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004557 const char *ext_oid;
4558 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004560 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004561 {
4562 /* Server part of the key exchange */
4563 switch( ciphersuite->key_exchange )
4564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004565 case MBEDTLS_KEY_EXCHANGE_RSA:
4566 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004567 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004568 break;
4569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004570 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
4571 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
4572 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
4573 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004574 break;
4575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004576 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
4577 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004578 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004579 break;
4580
4581 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004582 case MBEDTLS_KEY_EXCHANGE_NONE:
4583 case MBEDTLS_KEY_EXCHANGE_PSK:
4584 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
4585 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02004586 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004587 usage = 0;
4588 }
4589 }
4590 else
4591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004592 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
4593 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004594 }
4595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004596 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004597 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004598 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004599 ret = -1;
4600 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004602 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004604 ext_oid = MBEDTLS_OID_SERVER_AUTH;
4605 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004606 }
4607 else
4608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004609 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
4610 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004611 }
4612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004613 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004614 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004615 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004616 ret = -1;
4617 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004618
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004619 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004620}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004621#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004622
Jerry Yu148165c2021-09-24 23:20:59 +08004623#if defined(MBEDTLS_USE_PSA_CRYPTO)
4624int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
4625 const mbedtls_md_type_t md,
4626 unsigned char *dst,
4627 size_t dst_len,
4628 size_t *olen )
4629{
Ronald Cronf6893e12022-01-07 22:09:01 +01004630 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4631 psa_hash_operation_t *hash_operation_to_clone;
4632 psa_hash_operation_t hash_operation = psa_hash_operation_init();
4633
Jerry Yu148165c2021-09-24 23:20:59 +08004634 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01004635
4636 switch( md )
4637 {
4638#if defined(MBEDTLS_SHA384_C)
4639 case MBEDTLS_MD_SHA384:
4640 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
4641 break;
4642#endif
4643
4644#if defined(MBEDTLS_SHA256_C)
4645 case MBEDTLS_MD_SHA256:
4646 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
4647 break;
4648#endif
4649
4650 default:
4651 goto exit;
4652 }
4653
4654 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
4655 if( status != PSA_SUCCESS )
4656 goto exit;
4657
4658 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
4659 if( status != PSA_SUCCESS )
4660 goto exit;
4661
4662exit:
Ronald Cronb788c042022-02-15 09:14:15 +01004663 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08004664}
4665#else /* MBEDTLS_USE_PSA_CRYPTO */
4666
Jerry Yu24c0ec32021-09-09 14:21:07 +08004667#if defined(MBEDTLS_SHA384_C)
Jerry Yu000f9762021-09-14 11:12:51 +08004668static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
4669 unsigned char *dst,
4670 size_t dst_len,
4671 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004672{
Jerry Yu24c0ec32021-09-09 14:21:07 +08004673 int ret;
4674 mbedtls_sha512_context sha512;
4675
4676 if( dst_len < 48 )
4677 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4678
4679 mbedtls_sha512_init( &sha512 );
4680 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
4681
4682 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
4683 {
4684 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
4685 goto exit;
4686 }
4687
4688 *olen = 48;
4689
4690exit:
4691
4692 mbedtls_sha512_free( &sha512 );
4693 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004694}
4695#endif /* MBEDTLS_SHA384_C */
4696
4697#if defined(MBEDTLS_SHA256_C)
Jerry Yu000f9762021-09-14 11:12:51 +08004698static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
4699 unsigned char *dst,
4700 size_t dst_len,
4701 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004702{
Jerry Yu24c0ec32021-09-09 14:21:07 +08004703 int ret;
4704 mbedtls_sha256_context sha256;
4705
4706 if( dst_len < 32 )
4707 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4708
4709 mbedtls_sha256_init( &sha256 );
4710 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08004711
Jerry Yu24c0ec32021-09-09 14:21:07 +08004712 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
4713 {
4714 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
4715 goto exit;
4716 }
4717
4718 *olen = 32;
4719
4720exit:
4721
4722 mbedtls_sha256_free( &sha256 );
4723 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004724}
4725#endif /* MBEDTLS_SHA256_C */
4726
Jerry Yu000f9762021-09-14 11:12:51 +08004727int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
4728 const mbedtls_md_type_t md,
4729 unsigned char *dst,
4730 size_t dst_len,
4731 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004732{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004733 switch( md )
4734 {
4735
Jerry Yu24c0ec32021-09-09 14:21:07 +08004736#if defined(MBEDTLS_SHA384_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004737 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08004738 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08004739#endif /* MBEDTLS_SHA384_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004740
Jerry Yu24c0ec32021-09-09 14:21:07 +08004741#if defined(MBEDTLS_SHA256_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004742 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08004743 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08004744#endif /* MBEDTLS_SHA256_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004745
4746 default:
4747 break;
4748 }
Jerry Yuc5aef882021-12-23 20:15:02 +08004749 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004750}
XiaokangQian647719a2021-12-07 09:16:29 +00004751
Jerry Yu148165c2021-09-24 23:20:59 +08004752#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08004753
Jerry Yudc7bd172022-02-17 13:44:15 +08004754#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4755
4756#if defined(MBEDTLS_USE_PSA_CRYPTO)
4757
4758static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
4759 mbedtls_svc_key_id_t key,
4760 psa_algorithm_t alg,
4761 const unsigned char* seed, size_t seed_length,
4762 const unsigned char* label, size_t label_length,
4763 size_t capacity )
4764{
4765 psa_status_t status;
4766
4767 status = psa_key_derivation_setup( derivation, alg );
4768 if( status != PSA_SUCCESS )
4769 return( status );
4770
4771 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4772 {
4773 status = psa_key_derivation_input_bytes( derivation,
4774 PSA_KEY_DERIVATION_INPUT_SEED,
4775 seed, seed_length );
4776 if( status != PSA_SUCCESS )
4777 return( status );
4778
4779 if( mbedtls_svc_key_id_is_null( key ) )
4780 {
4781 status = psa_key_derivation_input_bytes(
4782 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
4783 NULL, 0 );
4784 }
4785 else
4786 {
4787 status = psa_key_derivation_input_key(
4788 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
4789 }
4790 if( status != PSA_SUCCESS )
4791 return( status );
4792
4793 status = psa_key_derivation_input_bytes( derivation,
4794 PSA_KEY_DERIVATION_INPUT_LABEL,
4795 label, label_length );
4796 if( status != PSA_SUCCESS )
4797 return( status );
4798 }
4799 else
4800 {
4801 return( PSA_ERROR_NOT_SUPPORTED );
4802 }
4803
4804 status = psa_key_derivation_set_capacity( derivation, capacity );
4805 if( status != PSA_SUCCESS )
4806 return( status );
4807
4808 return( PSA_SUCCESS );
4809}
4810
4811static int tls_prf_generic( mbedtls_md_type_t md_type,
4812 const unsigned char *secret, size_t slen,
4813 const char *label,
4814 const unsigned char *random, size_t rlen,
4815 unsigned char *dstbuf, size_t dlen )
4816{
4817 psa_status_t status;
4818 psa_algorithm_t alg;
4819 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
4820 psa_key_derivation_operation_t derivation =
4821 PSA_KEY_DERIVATION_OPERATION_INIT;
4822
4823 if( md_type == MBEDTLS_MD_SHA384 )
4824 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
4825 else
4826 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
4827
4828 /* Normally a "secret" should be long enough to be impossible to
4829 * find by brute force, and in particular should not be empty. But
4830 * this PRF is also used to derive an IV, in particular in EAP-TLS,
4831 * and for this use case it makes sense to have a 0-length "secret".
4832 * Since the key API doesn't allow importing a key of length 0,
4833 * keep master_key=0, which setup_psa_key_derivation() understands
4834 * to mean a 0-length "secret" input. */
4835 if( slen != 0 )
4836 {
4837 psa_key_attributes_t key_attributes = psa_key_attributes_init();
4838 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
4839 psa_set_key_algorithm( &key_attributes, alg );
4840 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
4841
4842 status = psa_import_key( &key_attributes, secret, slen, &master_key );
4843 if( status != PSA_SUCCESS )
4844 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4845 }
4846
4847 status = setup_psa_key_derivation( &derivation,
4848 master_key, alg,
4849 random, rlen,
4850 (unsigned char const *) label,
4851 (size_t) strlen( label ),
4852 dlen );
4853 if( status != PSA_SUCCESS )
4854 {
4855 psa_key_derivation_abort( &derivation );
4856 psa_destroy_key( master_key );
4857 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4858 }
4859
4860 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
4861 if( status != PSA_SUCCESS )
4862 {
4863 psa_key_derivation_abort( &derivation );
4864 psa_destroy_key( master_key );
4865 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4866 }
4867
4868 status = psa_key_derivation_abort( &derivation );
4869 if( status != PSA_SUCCESS )
4870 {
4871 psa_destroy_key( master_key );
4872 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4873 }
4874
4875 if( ! mbedtls_svc_key_id_is_null( master_key ) )
4876 status = psa_destroy_key( master_key );
4877 if( status != PSA_SUCCESS )
4878 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4879
4880 return( 0 );
4881}
4882
4883#else /* MBEDTLS_USE_PSA_CRYPTO */
4884
4885static int tls_prf_generic( mbedtls_md_type_t md_type,
4886 const unsigned char *secret, size_t slen,
4887 const char *label,
4888 const unsigned char *random, size_t rlen,
4889 unsigned char *dstbuf, size_t dlen )
4890{
4891 size_t nb;
4892 size_t i, j, k, md_len;
4893 unsigned char *tmp;
4894 size_t tmp_len = 0;
4895 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
4896 const mbedtls_md_info_t *md_info;
4897 mbedtls_md_context_t md_ctx;
4898 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4899
4900 mbedtls_md_init( &md_ctx );
4901
4902 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
4903 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4904
4905 md_len = mbedtls_md_get_size( md_info );
4906
4907 tmp_len = md_len + strlen( label ) + rlen;
4908 tmp = mbedtls_calloc( 1, tmp_len );
4909 if( tmp == NULL )
4910 {
4911 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
4912 goto exit;
4913 }
4914
4915 nb = strlen( label );
4916 memcpy( tmp + md_len, label, nb );
4917 memcpy( tmp + md_len + nb, random, rlen );
4918 nb += rlen;
4919
4920 /*
4921 * Compute P_<hash>(secret, label + random)[0..dlen]
4922 */
4923 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
4924 goto exit;
4925
4926 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
4927 if( ret != 0 )
4928 goto exit;
4929 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
4930 if( ret != 0 )
4931 goto exit;
4932 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
4933 if( ret != 0 )
4934 goto exit;
4935
4936 for( i = 0; i < dlen; i += md_len )
4937 {
4938 ret = mbedtls_md_hmac_reset ( &md_ctx );
4939 if( ret != 0 )
4940 goto exit;
4941 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
4942 if( ret != 0 )
4943 goto exit;
4944 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
4945 if( ret != 0 )
4946 goto exit;
4947
4948 ret = mbedtls_md_hmac_reset ( &md_ctx );
4949 if( ret != 0 )
4950 goto exit;
4951 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
4952 if( ret != 0 )
4953 goto exit;
4954 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
4955 if( ret != 0 )
4956 goto exit;
4957
4958 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
4959
4960 for( j = 0; j < k; j++ )
4961 dstbuf[i + j] = h_i[j];
4962 }
4963
4964exit:
4965 mbedtls_md_free( &md_ctx );
4966
4967 mbedtls_platform_zeroize( tmp, tmp_len );
4968 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
4969
4970 mbedtls_free( tmp );
4971
4972 return( ret );
4973}
4974#endif /* MBEDTLS_USE_PSA_CRYPTO */
4975
4976#if defined(MBEDTLS_SHA256_C)
4977static int tls_prf_sha256( const unsigned char *secret, size_t slen,
4978 const char *label,
4979 const unsigned char *random, size_t rlen,
4980 unsigned char *dstbuf, size_t dlen )
4981{
4982 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
4983 label, random, rlen, dstbuf, dlen ) );
4984}
4985#endif /* MBEDTLS_SHA256_C */
4986
4987#if defined(MBEDTLS_SHA384_C)
4988static int tls_prf_sha384( const unsigned char *secret, size_t slen,
4989 const char *label,
4990 const unsigned char *random, size_t rlen,
4991 unsigned char *dstbuf, size_t dlen )
4992{
4993 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
4994 label, random, rlen, dstbuf, dlen ) );
4995}
4996#endif /* MBEDTLS_SHA384_C */
4997
Jerry Yuf009d862022-02-17 14:01:37 +08004998/*
4999 * Set appropriate PRF function and other SSL / TLS1.2 functions
5000 *
5001 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005002 * - hash associated with the ciphersuite (only used by TLS 1.2)
5003 *
5004 * Outputs:
5005 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5006 */
5007static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005008 mbedtls_md_type_t hash )
5009{
Jerry Yuf009d862022-02-17 14:01:37 +08005010#if defined(MBEDTLS_SHA384_C)
Ronald Cron81591aa2022-03-07 09:05:51 +01005011 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005012 {
5013 handshake->tls_prf = tls_prf_sha384;
5014 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5015 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5016 }
5017 else
5018#endif
5019#if defined(MBEDTLS_SHA256_C)
Jerry Yuf009d862022-02-17 14:01:37 +08005020 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005021 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005022 handshake->tls_prf = tls_prf_sha256;
5023 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5024 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5025 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005026#else
Jerry Yuf009d862022-02-17 14:01:37 +08005027 {
Jerry Yuf009d862022-02-17 14:01:37 +08005028 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005029 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005030 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5031 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005032#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005033
5034 return( 0 );
5035}
Jerry Yud6ab2352022-02-17 14:03:43 +08005036
5037#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
5038 defined(MBEDTLS_USE_PSA_CRYPTO)
5039static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
5040{
5041 if( ssl->conf->f_psk != NULL )
5042 {
5043 /* If we've used a callback to select the PSK,
5044 * the static configuration is irrelevant. */
5045 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
5046 return( 1 );
5047
5048 return( 0 );
5049 }
5050
5051 if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) )
5052 return( 1 );
5053
5054 return( 0 );
5055}
5056#endif /* MBEDTLS_USE_PSA_CRYPTO &&
5057 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5058
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005059/*
5060 * Compute master secret if needed
5061 *
5062 * Parameters:
5063 * [in/out] handshake
5064 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5065 * (PSA-PSK) ciphersuite_info, psk_opaque
5066 * [out] premaster (cleared)
5067 * [out] master
5068 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5069 * debug: conf->f_dbg, conf->p_dbg
5070 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005071 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005072 */
5073static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5074 unsigned char *master,
5075 const mbedtls_ssl_context *ssl )
5076{
5077 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5078
5079 /* cf. RFC 5246, Section 8.1:
5080 * "The master secret is always exactly 48 bytes in length." */
5081 size_t const master_secret_len = 48;
5082
5083#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5084 unsigned char session_hash[48];
5085#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5086
5087 /* The label for the KDF used for key expansion.
5088 * This is either "master secret" or "extended master secret"
5089 * depending on whether the Extended Master Secret extension
5090 * is used. */
5091 char const *lbl = "master secret";
5092
5093 /* The salt for the KDF used for key expansion.
5094 * - If the Extended Master Secret extension is not used,
5095 * this is ClientHello.Random + ServerHello.Random
5096 * (see Sect. 8.1 in RFC 5246).
5097 * - If the Extended Master Secret extension is used,
5098 * this is the transcript of the handshake so far.
5099 * (see Sect. 4 in RFC 7627). */
5100 unsigned char const *salt = handshake->randbytes;
5101 size_t salt_len = 64;
5102
5103#if !defined(MBEDTLS_DEBUG_C) && \
5104 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5105 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5106 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5107 ssl = NULL; /* make sure we don't use it except for those cases */
5108 (void) ssl;
5109#endif
5110
5111 if( handshake->resume != 0 )
5112 {
5113 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5114 return( 0 );
5115 }
5116
5117#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5118 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5119 {
5120 lbl = "extended master secret";
5121 salt = session_hash;
5122 handshake->calc_verify( ssl, session_hash, &salt_len );
5123
5124 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
5125 session_hash, salt_len );
5126 }
5127#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
5128
5129#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
5130 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5131 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005132 ssl_use_opaque_psk( ssl ) == 1 )
5133 {
5134 /* Perform PSK-to-MS expansion in a single step. */
5135 psa_status_t status;
5136 psa_algorithm_t alg;
5137 mbedtls_svc_key_id_t psk;
5138 psa_key_derivation_operation_t derivation =
5139 PSA_KEY_DERIVATION_OPERATION_INIT;
5140 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5141
5142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5143
5144 psk = mbedtls_ssl_get_opaque_psk( ssl );
5145
5146 if( hash_alg == MBEDTLS_MD_SHA384 )
5147 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5148 else
5149 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5150
5151 status = setup_psa_key_derivation( &derivation, psk, alg,
5152 salt, salt_len,
5153 (unsigned char const *) lbl,
5154 (size_t) strlen( lbl ),
5155 master_secret_len );
5156 if( status != PSA_SUCCESS )
5157 {
5158 psa_key_derivation_abort( &derivation );
5159 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5160 }
5161
5162 status = psa_key_derivation_output_bytes( &derivation,
5163 master,
5164 master_secret_len );
5165 if( status != PSA_SUCCESS )
5166 {
5167 psa_key_derivation_abort( &derivation );
5168 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5169 }
5170
5171 status = psa_key_derivation_abort( &derivation );
5172 if( status != PSA_SUCCESS )
5173 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5174 }
5175 else
5176#endif
5177 {
5178 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
5179 lbl, salt, salt_len,
5180 master,
5181 master_secret_len );
5182 if( ret != 0 )
5183 {
5184 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5185 return( ret );
5186 }
5187
5188 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5189 handshake->premaster,
5190 handshake->pmslen );
5191
5192 mbedtls_platform_zeroize( handshake->premaster,
5193 sizeof(handshake->premaster) );
5194 }
5195
5196 return( 0 );
5197}
5198
Jerry Yud62f87e2022-02-17 14:09:02 +08005199int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5200{
5201 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5202 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5203 ssl->handshake->ciphersuite_info;
5204
5205 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5206
5207 /* Set PRF, calc_verify and calc_finished function pointers */
5208 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005209 ciphersuite_info->mac );
5210 if( ret != 0 )
5211 {
5212 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5213 return( ret );
5214 }
5215
5216 /* Compute master secret if needed */
5217 ret = ssl_compute_master( ssl->handshake,
5218 ssl->session_negotiate->master,
5219 ssl );
5220 if( ret != 0 )
5221 {
5222 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5223 return( ret );
5224 }
5225
5226 /* Swap the client and server random values:
5227 * - MS derivation wanted client+server (RFC 5246 8.1)
5228 * - key derivation wants server+client (RFC 5246 6.3) */
5229 {
5230 unsigned char tmp[64];
5231 memcpy( tmp, ssl->handshake->randbytes, 64 );
5232 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5233 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5234 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5235 }
5236
5237 /* Populate transform structure */
5238 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5239 ssl->session_negotiate->ciphersuite,
5240 ssl->session_negotiate->master,
5241#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
5242 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5243 ssl->session_negotiate->encrypt_then_mac,
5244#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
5245 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
5246 ssl->handshake->tls_prf,
5247 ssl->handshake->randbytes,
5248 ssl->minor_ver,
5249 ssl->conf->endpoint,
5250 ssl );
5251 if( ret != 0 )
5252 {
5253 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5254 return( ret );
5255 }
5256
5257 /* We no longer need Server/ClientHello.random values */
5258 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5259 sizeof( ssl->handshake->randbytes ) );
5260
5261 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5262
5263 return( 0 );
5264}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005265
Ronald Cron4dcbca92022-03-07 10:21:40 +01005266int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5267{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005268 switch( md )
5269 {
5270#if defined(MBEDTLS_SHA384_C)
5271 case MBEDTLS_SSL_HASH_SHA384:
5272 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5273 break;
5274#endif
5275#if defined(MBEDTLS_SHA256_C)
5276 case MBEDTLS_SSL_HASH_SHA256:
5277 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5278 break;
5279#endif
5280 default:
5281 return( -1 );
5282 }
5283
Ronald Cronc2f13a02022-03-07 10:25:24 +01005284 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005285}
5286
Jerry Yu8392e0d2022-02-17 14:10:24 +08005287#if defined(MBEDTLS_SHA256_C)
5288void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5289 unsigned char *hash,
5290 size_t *hlen )
5291{
5292#if defined(MBEDTLS_USE_PSA_CRYPTO)
5293 size_t hash_size;
5294 psa_status_t status;
5295 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5296
5297 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5298 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5299 if( status != PSA_SUCCESS )
5300 {
5301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5302 return;
5303 }
5304
5305 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5306 if( status != PSA_SUCCESS )
5307 {
5308 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5309 return;
5310 }
5311
5312 *hlen = 32;
5313 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5315#else
5316 mbedtls_sha256_context sha256;
5317
5318 mbedtls_sha256_init( &sha256 );
5319
5320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5321
5322 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5323 mbedtls_sha256_finish( &sha256, hash );
5324
5325 *hlen = 32;
5326
5327 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5328 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5329
5330 mbedtls_sha256_free( &sha256 );
5331#endif /* MBEDTLS_USE_PSA_CRYPTO */
5332 return;
5333}
5334#endif /* MBEDTLS_SHA256_C */
5335
Jerry Yuc1cb3842022-02-17 14:13:48 +08005336#if defined(MBEDTLS_SHA384_C)
5337void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5338 unsigned char *hash,
5339 size_t *hlen )
5340{
5341#if defined(MBEDTLS_USE_PSA_CRYPTO)
5342 size_t hash_size;
5343 psa_status_t status;
5344 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5345
5346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5347 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5348 if( status != PSA_SUCCESS )
5349 {
5350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5351 return;
5352 }
5353
5354 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5355 if( status != PSA_SUCCESS )
5356 {
5357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5358 return;
5359 }
5360
5361 *hlen = 48;
5362 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5364#else
5365 mbedtls_sha512_context sha512;
5366
5367 mbedtls_sha512_init( &sha512 );
5368
5369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
5370
5371 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
5372 mbedtls_sha512_finish( &sha512, hash );
5373
5374 *hlen = 48;
5375
5376 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5378
5379 mbedtls_sha512_free( &sha512 );
5380#endif /* MBEDTLS_USE_PSA_CRYPTO */
5381 return;
5382}
5383#endif /* MBEDTLS_SHA384_C */
5384
Jerry Yuce3dca42022-02-17 14:16:37 +08005385#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
5386int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
5387{
5388 unsigned char *p = ssl->handshake->premaster;
5389 unsigned char *end = p + sizeof( ssl->handshake->premaster );
5390 const unsigned char *psk = NULL;
5391 size_t psk_len = 0;
5392
5393 if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len )
5394 == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
5395 {
5396 /*
5397 * This should never happen because the existence of a PSK is always
5398 * checked before calling this function
5399 */
5400 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5401 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5402 }
5403
5404 /*
5405 * PMS = struct {
5406 * opaque other_secret<0..2^16-1>;
5407 * opaque psk<0..2^16-1>;
5408 * };
5409 * with "other_secret" depending on the particular key exchange
5410 */
5411#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5412 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
5413 {
5414 if( end - p < 2 )
5415 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5416
5417 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5418 p += 2;
5419
5420 if( end < p || (size_t)( end - p ) < psk_len )
5421 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5422
5423 memset( p, 0, psk_len );
5424 p += psk_len;
5425 }
5426 else
5427#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5428#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
5429 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5430 {
5431 /*
5432 * other_secret already set by the ClientKeyExchange message,
5433 * and is 48 bytes long
5434 */
5435 if( end - p < 2 )
5436 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5437
5438 *p++ = 0;
5439 *p++ = 48;
5440 p += 48;
5441 }
5442 else
5443#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
5444#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
5445 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5446 {
5447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5448 size_t len;
5449
5450 /* Write length only when we know the actual value */
5451 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
5452 p + 2, end - ( p + 2 ), &len,
5453 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5454 {
5455 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
5456 return( ret );
5457 }
5458 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
5459 p += 2 + len;
5460
5461 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
5462 }
5463 else
5464#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
5465#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
5466 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
5467 {
5468 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5469 size_t zlen;
5470
5471 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
5472 p + 2, end - ( p + 2 ),
5473 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5474 {
5475 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
5476 return( ret );
5477 }
5478
5479 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
5480 p += 2 + zlen;
5481
5482 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
5483 MBEDTLS_DEBUG_ECDH_Z );
5484 }
5485 else
5486#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
5487 {
5488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5489 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5490 }
5491
5492 /* opaque psk<0..2^16-1>; */
5493 if( end - p < 2 )
5494 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5495
5496 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5497 p += 2;
5498
5499 if( end < p || (size_t)( end - p ) < psk_len )
5500 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5501
5502 memcpy( p, psk, psk_len );
5503 p += psk_len;
5504
5505 ssl->handshake->pmslen = p - ssl->handshake->premaster;
5506
5507 return( 0 );
5508}
5509#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08005510
5511#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
5512static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
5513
5514#if defined(MBEDTLS_SSL_PROTO_DTLS)
5515int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
5516{
5517 /* If renegotiation is not enforced, retransmit until we would reach max
5518 * timeout if we were using the usual handshake doubling scheme */
5519 if( ssl->conf->renego_max_records < 0 )
5520 {
5521 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
5522 unsigned char doublings = 1;
5523
5524 while( ratio != 0 )
5525 {
5526 ++doublings;
5527 ratio >>= 1;
5528 }
5529
5530 if( ++ssl->renego_records_seen > doublings )
5531 {
5532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
5533 return( 0 );
5534 }
5535 }
5536
5537 return( ssl_write_hello_request( ssl ) );
5538}
5539#endif
5540#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08005541
Jerry Yud9526692022-02-17 14:23:47 +08005542/*
5543 * Handshake functions
5544 */
5545#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5546/* No certificate support -> dummy functions */
5547int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
5548{
5549 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5550 ssl->handshake->ciphersuite_info;
5551
5552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
5553
5554 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5555 {
5556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5557 ssl->state++;
5558 return( 0 );
5559 }
5560
5561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5562 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5563}
5564
5565int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5566{
5567 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5568 ssl->handshake->ciphersuite_info;
5569
5570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5571
5572 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5573 {
5574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5575 ssl->state++;
5576 return( 0 );
5577 }
5578
5579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5580 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5581}
5582
5583#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5584/* Some certificate support -> implement write and parse */
5585
5586int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
5587{
5588 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
5589 size_t i, n;
5590 const mbedtls_x509_crt *crt;
5591 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5592 ssl->handshake->ciphersuite_info;
5593
5594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
5595
5596 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5597 {
5598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5599 ssl->state++;
5600 return( 0 );
5601 }
5602
5603#if defined(MBEDTLS_SSL_CLI_C)
5604 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5605 {
5606 if( ssl->handshake->client_auth == 0 )
5607 {
5608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5609 ssl->state++;
5610 return( 0 );
5611 }
5612 }
5613#endif /* MBEDTLS_SSL_CLI_C */
5614#if defined(MBEDTLS_SSL_SRV_C)
5615 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
5616 {
5617 if( mbedtls_ssl_own_cert( ssl ) == NULL )
5618 {
5619 /* Should never happen because we shouldn't have picked the
5620 * ciphersuite if we don't have a certificate. */
5621 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5622 }
5623 }
5624#endif
5625
5626 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
5627
5628 /*
5629 * 0 . 0 handshake type
5630 * 1 . 3 handshake length
5631 * 4 . 6 length of all certs
5632 * 7 . 9 length of cert. 1
5633 * 10 . n-1 peer certificate
5634 * n . n+2 length of cert. 2
5635 * n+3 . ... upper level cert, etc.
5636 */
5637 i = 7;
5638 crt = mbedtls_ssl_own_cert( ssl );
5639
5640 while( crt != NULL )
5641 {
5642 n = crt->raw.len;
5643 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
5644 {
5645 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
5646 " > %" MBEDTLS_PRINTF_SIZET,
5647 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
5648 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5649 }
5650
5651 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
5652 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
5653 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
5654
5655 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5656 i += n; crt = crt->next;
5657 }
5658
5659 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
5660 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
5661 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
5662
5663 ssl->out_msglen = i;
5664 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5665 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
5666
5667 ssl->state++;
5668
5669 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
5670 {
5671 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
5672 return( ret );
5673 }
5674
5675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
5676
5677 return( ret );
5678}
5679
5680#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5681
5682#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5683static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5684 unsigned char *crt_buf,
5685 size_t crt_buf_len )
5686{
5687 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5688
5689 if( peer_crt == NULL )
5690 return( -1 );
5691
5692 if( peer_crt->raw.len != crt_buf_len )
5693 return( -1 );
5694
5695 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
5696}
5697#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5698static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5699 unsigned char *crt_buf,
5700 size_t crt_buf_len )
5701{
5702 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5703 unsigned char const * const peer_cert_digest =
5704 ssl->session->peer_cert_digest;
5705 mbedtls_md_type_t const peer_cert_digest_type =
5706 ssl->session->peer_cert_digest_type;
5707 mbedtls_md_info_t const * const digest_info =
5708 mbedtls_md_info_from_type( peer_cert_digest_type );
5709 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
5710 size_t digest_len;
5711
5712 if( peer_cert_digest == NULL || digest_info == NULL )
5713 return( -1 );
5714
5715 digest_len = mbedtls_md_get_size( digest_info );
5716 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
5717 return( -1 );
5718
5719 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
5720 if( ret != 0 )
5721 return( -1 );
5722
5723 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
5724}
5725#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5726#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5727
5728/*
5729 * Once the certificate message is read, parse it into a cert chain and
5730 * perform basic checks, but leave actual verification to the caller
5731 */
5732static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5733 mbedtls_x509_crt *chain )
5734{
5735 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5736#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5737 int crt_cnt=0;
5738#endif
5739 size_t i, n;
5740 uint8_t alert;
5741
5742 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5743 {
5744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5745 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5746 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5747 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5748 }
5749
5750 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
5751 {
5752 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5753 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5754 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5755 }
5756
5757 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
5758 {
5759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5760 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5761 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5762 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5763 }
5764
5765 i = mbedtls_ssl_hs_hdr_len( ssl );
5766
5767 /*
5768 * Same message structure as in mbedtls_ssl_write_certificate()
5769 */
5770 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
5771
5772 if( ssl->in_msg[i] != 0 ||
5773 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
5774 {
5775 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5776 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5777 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5778 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5779 }
5780
5781 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5782 i += 3;
5783
5784 /* Iterate through and parse the CRTs in the provided chain. */
5785 while( i < ssl->in_hslen )
5786 {
5787 /* Check that there's room for the next CRT's length fields. */
5788 if ( i + 3 > ssl->in_hslen ) {
5789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5790 mbedtls_ssl_send_alert_message( ssl,
5791 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5792 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5793 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5794 }
5795 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
5796 * anything beyond 2**16 ~ 64K. */
5797 if( ssl->in_msg[i] != 0 )
5798 {
5799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5800 mbedtls_ssl_send_alert_message( ssl,
5801 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5802 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
5803 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
5804 }
5805
5806 /* Read length of the next CRT in the chain. */
5807 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5808 | (unsigned int) ssl->in_msg[i + 2];
5809 i += 3;
5810
5811 if( n < 128 || i + n > ssl->in_hslen )
5812 {
5813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5814 mbedtls_ssl_send_alert_message( ssl,
5815 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5816 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5817 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5818 }
5819
5820 /* Check if we're handling the first CRT in the chain. */
5821#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5822 if( crt_cnt++ == 0 &&
5823 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5824 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
5825 {
5826 /* During client-side renegotiation, check that the server's
5827 * end-CRTs hasn't changed compared to the initial handshake,
5828 * mitigating the triple handshake attack. On success, reuse
5829 * the original end-CRT instead of parsing it again. */
5830 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
5831 if( ssl_check_peer_crt_unchanged( ssl,
5832 &ssl->in_msg[i],
5833 n ) != 0 )
5834 {
5835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
5836 mbedtls_ssl_send_alert_message( ssl,
5837 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5838 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
5839 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
5840 }
5841
5842 /* Now we can safely free the original chain. */
5843 ssl_clear_peer_cert( ssl->session );
5844 }
5845#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5846
5847 /* Parse the next certificate in the chain. */
5848#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5849 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
5850#else
5851 /* If we don't need to store the CRT chain permanently, parse
5852 * it in-place from the input buffer instead of making a copy. */
5853 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
5854#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5855 switch( ret )
5856 {
5857 case 0: /*ok*/
5858 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5859 /* Ignore certificate with an unknown algorithm: maybe a
5860 prior certificate was already trusted. */
5861 break;
5862
5863 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5864 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5865 goto crt_parse_der_failed;
5866
5867 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5868 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5869 goto crt_parse_der_failed;
5870
5871 default:
5872 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5873 crt_parse_der_failed:
5874 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
5875 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
5876 return( ret );
5877 }
5878
5879 i += n;
5880 }
5881
5882 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
5883 return( 0 );
5884}
5885
5886#if defined(MBEDTLS_SSL_SRV_C)
5887static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
5888{
5889 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5890 return( -1 );
5891
5892 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5893 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5894 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5895 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
5896 {
5897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
5898 return( 0 );
5899 }
5900 return( -1 );
5901}
5902#endif /* MBEDTLS_SSL_SRV_C */
5903
5904/* Check if a certificate message is expected.
5905 * Return either
5906 * - SSL_CERTIFICATE_EXPECTED, or
5907 * - SSL_CERTIFICATE_SKIP
5908 * indicating whether a Certificate message is expected or not.
5909 */
5910#define SSL_CERTIFICATE_EXPECTED 0
5911#define SSL_CERTIFICATE_SKIP 1
5912static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
5913 int authmode )
5914{
5915 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5916 ssl->handshake->ciphersuite_info;
5917
5918 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5919 return( SSL_CERTIFICATE_SKIP );
5920
5921#if defined(MBEDTLS_SSL_SRV_C)
5922 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
5923 {
5924 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5925 return( SSL_CERTIFICATE_SKIP );
5926
5927 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
5928 {
5929 ssl->session_negotiate->verify_result =
5930 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5931 return( SSL_CERTIFICATE_SKIP );
5932 }
5933 }
5934#else
5935 ((void) authmode);
5936#endif /* MBEDTLS_SSL_SRV_C */
5937
5938 return( SSL_CERTIFICATE_EXPECTED );
5939}
5940
5941static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
5942 int authmode,
5943 mbedtls_x509_crt *chain,
5944 void *rs_ctx )
5945{
5946 int ret = 0;
5947 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5948 ssl->handshake->ciphersuite_info;
5949 int have_ca_chain = 0;
5950
5951 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
5952 void *p_vrfy;
5953
5954 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
5955 return( 0 );
5956
5957 if( ssl->f_vrfy != NULL )
5958 {
5959 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
5960 f_vrfy = ssl->f_vrfy;
5961 p_vrfy = ssl->p_vrfy;
5962 }
5963 else
5964 {
5965 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
5966 f_vrfy = ssl->conf->f_vrfy;
5967 p_vrfy = ssl->conf->p_vrfy;
5968 }
5969
5970 /*
5971 * Main check: verify certificate
5972 */
5973#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
5974 if( ssl->conf->f_ca_cb != NULL )
5975 {
5976 ((void) rs_ctx);
5977 have_ca_chain = 1;
5978
5979 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
5980 ret = mbedtls_x509_crt_verify_with_ca_cb(
5981 chain,
5982 ssl->conf->f_ca_cb,
5983 ssl->conf->p_ca_cb,
5984 ssl->conf->cert_profile,
5985 ssl->hostname,
5986 &ssl->session_negotiate->verify_result,
5987 f_vrfy, p_vrfy );
5988 }
5989 else
5990#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
5991 {
5992 mbedtls_x509_crt *ca_chain;
5993 mbedtls_x509_crl *ca_crl;
5994
5995#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5996 if( ssl->handshake->sni_ca_chain != NULL )
5997 {
5998 ca_chain = ssl->handshake->sni_ca_chain;
5999 ca_crl = ssl->handshake->sni_ca_crl;
6000 }
6001 else
6002#endif
6003 {
6004 ca_chain = ssl->conf->ca_chain;
6005 ca_crl = ssl->conf->ca_crl;
6006 }
6007
6008 if( ca_chain != NULL )
6009 have_ca_chain = 1;
6010
6011 ret = mbedtls_x509_crt_verify_restartable(
6012 chain,
6013 ca_chain, ca_crl,
6014 ssl->conf->cert_profile,
6015 ssl->hostname,
6016 &ssl->session_negotiate->verify_result,
6017 f_vrfy, p_vrfy, rs_ctx );
6018 }
6019
6020 if( ret != 0 )
6021 {
6022 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6023 }
6024
6025#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6026 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6027 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6028#endif
6029
6030 /*
6031 * Secondary checks: always done, but change 'ret' only if it was 0
6032 */
6033
6034#if defined(MBEDTLS_ECP_C)
6035 {
6036 const mbedtls_pk_context *pk = &chain->pk;
6037
6038 /* If certificate uses an EC key, make sure the curve is OK */
6039 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6040 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6041 {
6042 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6043
6044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6045 if( ret == 0 )
6046 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6047 }
6048 }
6049#endif /* MBEDTLS_ECP_C */
6050
6051 if( mbedtls_ssl_check_cert_usage( chain,
6052 ciphersuite_info,
6053 ! ssl->conf->endpoint,
6054 &ssl->session_negotiate->verify_result ) != 0 )
6055 {
6056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6057 if( ret == 0 )
6058 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6059 }
6060
6061 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6062 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6063 * with details encoded in the verification flags. All other kinds
6064 * of error codes, including those from the user provided f_vrfy
6065 * functions, are treated as fatal and lead to a failure of
6066 * ssl_parse_certificate even if verification was optional. */
6067 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6068 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6069 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6070 {
6071 ret = 0;
6072 }
6073
6074 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6075 {
6076 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6077 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6078 }
6079
6080 if( ret != 0 )
6081 {
6082 uint8_t alert;
6083
6084 /* The certificate may have been rejected for several reasons.
6085 Pick one and send the corresponding alert. Which alert to send
6086 may be a subject of debate in some cases. */
6087 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6088 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6089 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6090 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6091 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6092 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6093 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6094 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6095 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6096 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6097 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6098 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6099 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6100 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6101 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6102 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6103 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6104 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6105 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6106 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6107 else
6108 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6109 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6110 alert );
6111 }
6112
6113#if defined(MBEDTLS_DEBUG_C)
6114 if( ssl->session_negotiate->verify_result != 0 )
6115 {
6116 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6117 (unsigned int) ssl->session_negotiate->verify_result ) );
6118 }
6119 else
6120 {
6121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6122 }
6123#endif /* MBEDTLS_DEBUG_C */
6124
6125 return( ret );
6126}
6127
6128#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6129static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6130 unsigned char *start, size_t len )
6131{
6132 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6133 /* Remember digest of the peer's end-CRT. */
6134 ssl->session_negotiate->peer_cert_digest =
6135 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6136 if( ssl->session_negotiate->peer_cert_digest == NULL )
6137 {
6138 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6139 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6140 mbedtls_ssl_send_alert_message( ssl,
6141 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6142 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6143
6144 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6145 }
6146
6147 ret = mbedtls_md( mbedtls_md_info_from_type(
6148 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6149 start, len,
6150 ssl->session_negotiate->peer_cert_digest );
6151
6152 ssl->session_negotiate->peer_cert_digest_type =
6153 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6154 ssl->session_negotiate->peer_cert_digest_len =
6155 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6156
6157 return( ret );
6158}
6159
6160static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6161 unsigned char *start, size_t len )
6162{
6163 unsigned char *end = start + len;
6164 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6165
6166 /* Make a copy of the peer's raw public key. */
6167 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6168 ret = mbedtls_pk_parse_subpubkey( &start, end,
6169 &ssl->handshake->peer_pubkey );
6170 if( ret != 0 )
6171 {
6172 /* We should have parsed the public key before. */
6173 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6174 }
6175
6176 return( 0 );
6177}
6178#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6179
6180int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6181{
6182 int ret = 0;
6183 int crt_expected;
6184#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6185 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6186 ? ssl->handshake->sni_authmode
6187 : ssl->conf->authmode;
6188#else
6189 const int authmode = ssl->conf->authmode;
6190#endif
6191 void *rs_ctx = NULL;
6192 mbedtls_x509_crt *chain = NULL;
6193
6194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6195
6196 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6197 if( crt_expected == SSL_CERTIFICATE_SKIP )
6198 {
6199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6200 goto exit;
6201 }
6202
6203#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6204 if( ssl->handshake->ecrs_enabled &&
6205 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6206 {
6207 chain = ssl->handshake->ecrs_peer_cert;
6208 ssl->handshake->ecrs_peer_cert = NULL;
6209 goto crt_verify;
6210 }
6211#endif
6212
6213 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6214 {
6215 /* mbedtls_ssl_read_record may have sent an alert already. We
6216 let it decide whether to alert. */
6217 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6218 goto exit;
6219 }
6220
6221#if defined(MBEDTLS_SSL_SRV_C)
6222 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6223 {
6224 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6225
6226 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6227 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6228
6229 goto exit;
6230 }
6231#endif /* MBEDTLS_SSL_SRV_C */
6232
6233 /* Clear existing peer CRT structure in case we tried to
6234 * reuse a session but it failed, and allocate a new one. */
6235 ssl_clear_peer_cert( ssl->session_negotiate );
6236
6237 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6238 if( chain == NULL )
6239 {
6240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6241 sizeof( mbedtls_x509_crt ) ) );
6242 mbedtls_ssl_send_alert_message( ssl,
6243 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6244 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6245
6246 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6247 goto exit;
6248 }
6249 mbedtls_x509_crt_init( chain );
6250
6251 ret = ssl_parse_certificate_chain( ssl, chain );
6252 if( ret != 0 )
6253 goto exit;
6254
6255#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6256 if( ssl->handshake->ecrs_enabled)
6257 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6258
6259crt_verify:
6260 if( ssl->handshake->ecrs_enabled)
6261 rs_ctx = &ssl->handshake->ecrs_ctx;
6262#endif
6263
6264 ret = ssl_parse_certificate_verify( ssl, authmode,
6265 chain, rs_ctx );
6266 if( ret != 0 )
6267 goto exit;
6268
6269#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6270 {
6271 unsigned char *crt_start, *pk_start;
6272 size_t crt_len, pk_len;
6273
6274 /* We parse the CRT chain without copying, so
6275 * these pointers point into the input buffer,
6276 * and are hence still valid after freeing the
6277 * CRT chain. */
6278
6279 crt_start = chain->raw.p;
6280 crt_len = chain->raw.len;
6281
6282 pk_start = chain->pk_raw.p;
6283 pk_len = chain->pk_raw.len;
6284
6285 /* Free the CRT structures before computing
6286 * digest and copying the peer's public key. */
6287 mbedtls_x509_crt_free( chain );
6288 mbedtls_free( chain );
6289 chain = NULL;
6290
6291 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6292 if( ret != 0 )
6293 goto exit;
6294
6295 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6296 if( ret != 0 )
6297 goto exit;
6298 }
6299#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6300 /* Pass ownership to session structure. */
6301 ssl->session_negotiate->peer_cert = chain;
6302 chain = NULL;
6303#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6304
6305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6306
6307exit:
6308
6309 if( ret == 0 )
6310 ssl->state++;
6311
6312#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6313 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6314 {
6315 ssl->handshake->ecrs_peer_cert = chain;
6316 chain = NULL;
6317 }
6318#endif
6319
6320 if( chain != NULL )
6321 {
6322 mbedtls_x509_crt_free( chain );
6323 mbedtls_free( chain );
6324 }
6325
6326 return( ret );
6327}
6328#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6329
Jerry Yu615bd6f2022-02-17 14:25:15 +08006330#if defined(MBEDTLS_SHA256_C)
6331static void ssl_calc_finished_tls_sha256(
6332 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6333{
6334 int len = 12;
6335 const char *sender;
6336 unsigned char padbuf[32];
6337#if defined(MBEDTLS_USE_PSA_CRYPTO)
6338 size_t hash_size;
6339 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
6340 psa_status_t status;
6341#else
6342 mbedtls_sha256_context sha256;
6343#endif
6344
6345 mbedtls_ssl_session *session = ssl->session_negotiate;
6346 if( !session )
6347 session = ssl->session;
6348
6349 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6350 ? "client finished"
6351 : "server finished";
6352
6353#if defined(MBEDTLS_USE_PSA_CRYPTO)
6354 sha256_psa = psa_hash_operation_init();
6355
6356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6357
6358 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6359 if( status != PSA_SUCCESS )
6360 {
6361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6362 return;
6363 }
6364
6365 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6366 if( status != PSA_SUCCESS )
6367 {
6368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6369 return;
6370 }
6371 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6372#else
6373
6374 mbedtls_sha256_init( &sha256 );
6375
6376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
6377
6378 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6379
6380 /*
6381 * TLSv1.2:
6382 * hash = PRF( master, finished_label,
6383 * Hash( handshake ) )[0.11]
6384 */
6385
6386#if !defined(MBEDTLS_SHA256_ALT)
6387 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
6388 sha256.state, sizeof( sha256.state ) );
6389#endif
6390
6391 mbedtls_sha256_finish( &sha256, padbuf );
6392 mbedtls_sha256_free( &sha256 );
6393#endif /* MBEDTLS_USE_PSA_CRYPTO */
6394
6395 ssl->handshake->tls_prf( session->master, 48, sender,
6396 padbuf, 32, buf, len );
6397
6398 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6399
6400 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6401
6402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6403}
6404#endif /* MBEDTLS_SHA256_C */
6405
Jerry Yub7ba49e2022-02-17 14:25:53 +08006406
6407#if defined(MBEDTLS_SHA384_C)
6408
6409static void ssl_calc_finished_tls_sha384(
6410 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6411{
6412 int len = 12;
6413 const char *sender;
6414 unsigned char padbuf[48];
6415#if defined(MBEDTLS_USE_PSA_CRYPTO)
6416 size_t hash_size;
6417 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
6418 psa_status_t status;
6419#else
6420 mbedtls_sha512_context sha512;
6421#endif
6422
6423 mbedtls_ssl_session *session = ssl->session_negotiate;
6424 if( !session )
6425 session = ssl->session;
6426
6427 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6428 ? "client finished"
6429 : "server finished";
6430
6431#if defined(MBEDTLS_USE_PSA_CRYPTO)
6432 sha384_psa = psa_hash_operation_init();
6433
6434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6435
6436 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6437 if( status != PSA_SUCCESS )
6438 {
6439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6440 return;
6441 }
6442
6443 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
6444 if( status != PSA_SUCCESS )
6445 {
6446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6447 return;
6448 }
6449 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
6450#else
6451 mbedtls_sha512_init( &sha512 );
6452
6453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
6454
6455 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
6456
6457 /*
6458 * TLSv1.2:
6459 * hash = PRF( master, finished_label,
6460 * Hash( handshake ) )[0.11]
6461 */
6462
6463#if !defined(MBEDTLS_SHA512_ALT)
6464 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6465 sha512.state, sizeof( sha512.state ) );
6466#endif
6467 mbedtls_sha512_finish( &sha512, padbuf );
6468
6469 mbedtls_sha512_free( &sha512 );
6470#endif
6471
6472 ssl->handshake->tls_prf( session->master, 48, sender,
6473 padbuf, 48, buf, len );
6474
6475 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6476
6477 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6478
6479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6480}
6481#endif /* MBEDTLS_SHA384_C */
6482
Jerry Yuaef00152022-02-17 14:27:31 +08006483void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
6484{
6485 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
6486
6487 /*
6488 * Free our handshake params
6489 */
6490 mbedtls_ssl_handshake_free( ssl );
6491 mbedtls_free( ssl->handshake );
6492 ssl->handshake = NULL;
6493
6494 /*
6495 * Free the previous transform and swith in the current one
6496 */
6497 if( ssl->transform )
6498 {
6499 mbedtls_ssl_transform_free( ssl->transform );
6500 mbedtls_free( ssl->transform );
6501 }
6502 ssl->transform = ssl->transform_negotiate;
6503 ssl->transform_negotiate = NULL;
6504
6505 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
6506}
6507
Jerry Yu2a9fff52022-02-17 14:28:51 +08006508void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
6509{
6510 int resume = ssl->handshake->resume;
6511
6512 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
6513
6514#if defined(MBEDTLS_SSL_RENEGOTIATION)
6515 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6516 {
6517 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
6518 ssl->renego_records_seen = 0;
6519 }
6520#endif
6521
6522 /*
6523 * Free the previous session and switch in the current one
6524 */
6525 if( ssl->session )
6526 {
6527#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6528 /* RFC 7366 3.1: keep the EtM state */
6529 ssl->session_negotiate->encrypt_then_mac =
6530 ssl->session->encrypt_then_mac;
6531#endif
6532
6533 mbedtls_ssl_session_free( ssl->session );
6534 mbedtls_free( ssl->session );
6535 }
6536 ssl->session = ssl->session_negotiate;
6537 ssl->session_negotiate = NULL;
6538
6539 /*
6540 * Add cache entry
6541 */
6542 if( ssl->conf->f_set_cache != NULL &&
6543 ssl->session->id_len != 0 &&
6544 resume == 0 )
6545 {
6546 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
6547 ssl->session->id,
6548 ssl->session->id_len,
6549 ssl->session ) != 0 )
6550 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
6551 }
6552
6553#if defined(MBEDTLS_SSL_PROTO_DTLS)
6554 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6555 ssl->handshake->flight != NULL )
6556 {
6557 /* Cancel handshake timer */
6558 mbedtls_ssl_set_timer( ssl, 0 );
6559
6560 /* Keep last flight around in case we need to resend it:
6561 * we need the handshake and transform structures for that */
6562 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
6563 }
6564 else
6565#endif
6566 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
6567
6568 ssl->state++;
6569
6570 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
6571}
6572
Jerry Yu3c8e47b2022-02-17 14:30:01 +08006573int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
6574{
6575 int ret, hash_len;
6576
6577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
6578
6579 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
6580
6581 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
6582
6583 /*
6584 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6585 * may define some other value. Currently (early 2016), no defined
6586 * ciphersuite does this (and this is unlikely to change as activity has
6587 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6588 */
6589 hash_len = 12;
6590
6591#if defined(MBEDTLS_SSL_RENEGOTIATION)
6592 ssl->verify_data_len = hash_len;
6593 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
6594#endif
6595
6596 ssl->out_msglen = 4 + hash_len;
6597 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6598 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
6599
6600 /*
6601 * In case of session resuming, invert the client and server
6602 * ChangeCipherSpec messages order.
6603 */
6604 if( ssl->handshake->resume != 0 )
6605 {
6606#if defined(MBEDTLS_SSL_CLI_C)
6607 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6608 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
6609#endif
6610#if defined(MBEDTLS_SSL_SRV_C)
6611 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6612 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
6613#endif
6614 }
6615 else
6616 ssl->state++;
6617
6618 /*
6619 * Switch to our negotiated transform and session parameters for outbound
6620 * data.
6621 */
6622 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
6623
6624#if defined(MBEDTLS_SSL_PROTO_DTLS)
6625 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6626 {
6627 unsigned char i;
6628
6629 /* Remember current epoch settings for resending */
6630 ssl->handshake->alt_transform_out = ssl->transform_out;
6631 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
6632 sizeof( ssl->handshake->alt_out_ctr ) );
6633
6634 /* Set sequence_number to zero */
6635 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
6636
6637
6638 /* Increment epoch */
6639 for( i = 2; i > 0; i-- )
6640 if( ++ssl->cur_out_ctr[i - 1] != 0 )
6641 break;
6642
6643 /* The loop goes to its end iff the counter is wrapping */
6644 if( i == 0 )
6645 {
6646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6647 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
6648 }
6649 }
6650 else
6651#endif /* MBEDTLS_SSL_PROTO_DTLS */
6652 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6653
6654 ssl->transform_out = ssl->transform_negotiate;
6655 ssl->session_out = ssl->session_negotiate;
6656
6657#if defined(MBEDTLS_SSL_PROTO_DTLS)
6658 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6659 mbedtls_ssl_send_flight_completed( ssl );
6660#endif
6661
6662 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6663 {
6664 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6665 return( ret );
6666 }
6667
6668#if defined(MBEDTLS_SSL_PROTO_DTLS)
6669 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6670 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6671 {
6672 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6673 return( ret );
6674 }
6675#endif
6676
6677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
6678
6679 return( 0 );
6680}
6681
Jerry Yu0b3d7c12022-02-17 14:30:51 +08006682#define SSL_MAX_HASH_LEN 12
6683
6684int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
6685{
6686 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6687 unsigned int hash_len = 12;
6688 unsigned char buf[SSL_MAX_HASH_LEN];
6689
6690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
6691
6692 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
6693
6694 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6695 {
6696 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6697 goto exit;
6698 }
6699
6700 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6701 {
6702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6703 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6704 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6705 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
6706 goto exit;
6707 }
6708
6709 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
6710 {
6711 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6712 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6713 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
6714 goto exit;
6715 }
6716
6717 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
6718 {
6719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6720 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6721 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6722 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
6723 goto exit;
6724 }
6725
6726 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
6727 buf, hash_len ) != 0 )
6728 {
6729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6730 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6731 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
6732 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
6733 goto exit;
6734 }
6735
6736#if defined(MBEDTLS_SSL_RENEGOTIATION)
6737 ssl->verify_data_len = hash_len;
6738 memcpy( ssl->peer_verify_data, buf, hash_len );
6739#endif
6740
6741 if( ssl->handshake->resume != 0 )
6742 {
6743#if defined(MBEDTLS_SSL_CLI_C)
6744 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6745 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
6746#endif
6747#if defined(MBEDTLS_SSL_SRV_C)
6748 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6749 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
6750#endif
6751 }
6752 else
6753 ssl->state++;
6754
6755#if defined(MBEDTLS_SSL_PROTO_DTLS)
6756 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6757 mbedtls_ssl_recv_flight_completed( ssl );
6758#endif
6759
6760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
6761
6762exit:
6763 mbedtls_platform_zeroize( buf, hash_len );
6764 return( ret );
6765}
6766
Jerry Yu392112c2022-02-17 14:34:10 +08006767#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
6768/*
6769 * Helper to get TLS 1.2 PRF from ciphersuite
6770 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
6771 */
6772static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
6773{
6774#if defined(MBEDTLS_SHA384_C)
6775 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6776 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
6777
6778 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
6779 return( tls_prf_sha384 );
6780#else
6781 (void) ciphersuite_id;
6782#endif
6783 return( tls_prf_sha256 );
6784}
6785#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08006786
6787static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
6788{
6789 ((void) tls_prf);
6790#if defined(MBEDTLS_SHA384_C)
6791 if( tls_prf == tls_prf_sha384 )
6792 {
6793 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
6794 }
6795 else
6796#endif
6797#if defined(MBEDTLS_SHA256_C)
6798 if( tls_prf == tls_prf_sha256 )
6799 {
6800 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
6801 }
6802 else
6803#endif
6804 return( MBEDTLS_SSL_TLS_PRF_NONE );
6805}
6806
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006807/*
6808 * Populate a transform structure with session keys and all the other
6809 * necessary information.
6810 *
6811 * Parameters:
6812 * - [in/out]: transform: structure to populate
6813 * [in] must be just initialised with mbedtls_ssl_transform_init()
6814 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
6815 * - [in] ciphersuite
6816 * - [in] master
6817 * - [in] encrypt_then_mac
6818 * - [in] compression
6819 * - [in] tls_prf: pointer to PRF to use for key derivation
6820 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
6821 * - [in] minor_ver: SSL/TLS minor version
6822 * - [in] endpoint: client or server
6823 * - [in] ssl: used for:
6824 * - ssl->conf->{f,p}_export_keys
6825 * [in] optionally used for:
6826 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
6827 */
6828static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
6829 int ciphersuite,
6830 const unsigned char master[48],
6831#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
6832 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6833 int encrypt_then_mac,
6834#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
6835 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
6836 ssl_tls_prf_t tls_prf,
6837 const unsigned char randbytes[64],
6838 int minor_ver,
6839 unsigned endpoint,
6840 const mbedtls_ssl_context *ssl )
6841{
6842 int ret = 0;
6843 unsigned char keyblk[256];
6844 unsigned char *key1;
6845 unsigned char *key2;
6846 unsigned char *mac_enc;
6847 unsigned char *mac_dec;
6848 size_t mac_key_len = 0;
6849 size_t iv_copy_len;
6850 size_t keylen;
6851 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
6852 const mbedtls_cipher_info_t *cipher_info;
Neil Armstronge4512952022-03-08 09:08:22 +01006853#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006854 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01006855#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006856
6857#if defined(MBEDTLS_USE_PSA_CRYPTO)
6858 psa_key_type_t key_type;
6859 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6860 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01006861 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006862 size_t key_bits;
6863 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
6864#endif
6865
6866#if !defined(MBEDTLS_DEBUG_C) && \
6867 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6868 if( ssl->f_export_keys == NULL )
6869 {
6870 ssl = NULL; /* make sure we don't use it except for these cases */
6871 (void) ssl;
6872 }
6873#endif
6874
6875 /*
6876 * Some data just needs copying into the structure
6877 */
6878#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
6879 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
6880 transform->encrypt_then_mac = encrypt_then_mac;
6881#endif
6882 transform->minor_ver = minor_ver;
6883
6884#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
6885 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
6886#endif
6887
6888#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
6889 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
6890 {
6891 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
6892 * generation separate. This should never happen. */
6893 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6894 }
6895#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
6896
6897 /*
6898 * Get various info structures
6899 */
6900 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
6901 if( ciphersuite_info == NULL )
6902 {
6903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
6904 ciphersuite ) );
6905 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6906 }
6907
6908 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
6909 if( cipher_info == NULL )
6910 {
6911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
6912 ciphersuite_info->cipher ) );
6913 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6914 }
6915
Neil Armstronge4512952022-03-08 09:08:22 +01006916#if defined(MBEDTLS_USE_PSA_CRYPTO)
6917 mac_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
6918 if( mac_alg == 0 )
6919 {
6920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_psa_translate_md for %u not found",
6921 (unsigned) ciphersuite_info->mac ) );
6922 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6923 }
6924#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006925 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
6926 if( md_info == NULL )
6927 {
6928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
6929 (unsigned) ciphersuite_info->mac ) );
6930 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6931 }
Neil Armstronge4512952022-03-08 09:08:22 +01006932#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006933
6934#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6935 /* Copy own and peer's CID if the use of the CID
6936 * extension has been negotiated. */
6937 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
6938 {
6939 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
6940
6941 transform->in_cid_len = ssl->own_cid_len;
6942 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
6943 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
6944 transform->in_cid_len );
6945
6946 transform->out_cid_len = ssl->handshake->peer_cid_len;
6947 memcpy( transform->out_cid, ssl->handshake->peer_cid,
6948 ssl->handshake->peer_cid_len );
6949 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
6950 transform->out_cid_len );
6951 }
6952#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6953
6954 /*
6955 * Compute key block using the PRF
6956 */
6957 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
6958 if( ret != 0 )
6959 {
6960 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
6961 return( ret );
6962 }
6963
6964 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
6965 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
6966 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
6967 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
6968 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
6969
6970 /*
6971 * Determine the appropriate key, IV and MAC length.
6972 */
6973
6974 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
6975
6976#if defined(MBEDTLS_GCM_C) || \
6977 defined(MBEDTLS_CCM_C) || \
6978 defined(MBEDTLS_CHACHAPOLY_C)
6979 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_GCM ||
6980 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CCM ||
6981 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
6982 {
6983 size_t explicit_ivlen;
6984
6985 transform->maclen = 0;
6986 mac_key_len = 0;
6987 transform->taglen =
6988 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
6989
6990 /* All modes haves 96-bit IVs, but the length of the static parts vary
6991 * with mode and version:
6992 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
6993 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
6994 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
6995 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
6996 * sequence number).
6997 */
6998 transform->ivlen = 12;
6999 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
7000 transform->fixed_ivlen = 12;
7001 else
7002 transform->fixed_ivlen = 4;
7003
7004 /* Minimum length of encrypted record */
7005 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7006 transform->minlen = explicit_ivlen + transform->taglen;
7007 }
7008 else
7009#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7010#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7011 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM ||
7012 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7013 {
Neil Armstronge4512952022-03-08 09:08:22 +01007014#if defined(MBEDTLS_USE_PSA_CRYPTO)
7015 /* Get MAC length */
7016 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7017#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007018 /* Initialize HMAC contexts */
7019 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7020 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7021 {
7022 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7023 goto end;
7024 }
7025
7026 /* Get MAC length */
7027 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007028#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007029 transform->maclen = mac_key_len;
7030
7031 /* IV length */
7032 transform->ivlen = cipher_info->iv_size;
7033
7034 /* Minimum length */
7035 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM )
7036 transform->minlen = transform->maclen;
7037 else
7038 {
7039 /*
7040 * GenericBlockCipher:
7041 * 1. if EtM is in use: one block plus MAC
7042 * otherwise: * first multiple of blocklen greater than maclen
7043 * 2. IV
7044 */
7045#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7046 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
7047 {
7048 transform->minlen = transform->maclen
7049 + cipher_info->block_size;
7050 }
7051 else
7052#endif
7053 {
7054 transform->minlen = transform->maclen
7055 + cipher_info->block_size
7056 - transform->maclen % cipher_info->block_size;
7057 }
7058
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007059 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
7060 {
7061 transform->minlen += transform->ivlen;
7062 }
7063 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007064 {
7065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7066 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7067 goto end;
7068 }
7069 }
7070 }
7071 else
7072#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7073 {
7074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7075 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7076 }
7077
7078 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7079 (unsigned) keylen,
7080 (unsigned) transform->minlen,
7081 (unsigned) transform->ivlen,
7082 (unsigned) transform->maclen ) );
7083
7084 /*
7085 * Finally setup the cipher contexts, IVs and MAC secrets.
7086 */
7087#if defined(MBEDTLS_SSL_CLI_C)
7088 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7089 {
7090 key1 = keyblk + mac_key_len * 2;
7091 key2 = keyblk + mac_key_len * 2 + keylen;
7092
7093 mac_enc = keyblk;
7094 mac_dec = keyblk + mac_key_len;
7095
7096 /*
7097 * This is not used in TLS v1.1.
7098 */
7099 iv_copy_len = ( transform->fixed_ivlen ) ?
7100 transform->fixed_ivlen : transform->ivlen;
7101 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7102 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7103 iv_copy_len );
7104 }
7105 else
7106#endif /* MBEDTLS_SSL_CLI_C */
7107#if defined(MBEDTLS_SSL_SRV_C)
7108 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7109 {
7110 key1 = keyblk + mac_key_len * 2 + keylen;
7111 key2 = keyblk + mac_key_len * 2;
7112
7113 mac_enc = keyblk + mac_key_len;
7114 mac_dec = keyblk;
7115
7116 /*
7117 * This is not used in TLS v1.1.
7118 */
7119 iv_copy_len = ( transform->fixed_ivlen ) ?
7120 transform->fixed_ivlen : transform->ivlen;
7121 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7122 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7123 iv_copy_len );
7124 }
7125 else
7126#endif /* MBEDTLS_SSL_SRV_C */
7127 {
7128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7129 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7130 goto end;
7131 }
7132
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007133 if( ssl != NULL && ssl->f_export_keys != NULL )
7134 {
7135 ssl->f_export_keys( ssl->p_export_keys,
7136 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7137 master, 48,
7138 randbytes + 32,
7139 randbytes,
7140 tls_prf_get_type( tls_prf ) );
7141 }
7142
7143#if defined(MBEDTLS_USE_PSA_CRYPTO)
7144 if( ( status = mbedtls_ssl_cipher_to_psa( cipher_info->type,
7145 transform->taglen,
7146 &alg,
7147 &key_type,
7148 &key_bits ) ) != PSA_SUCCESS )
7149 {
7150 ret = psa_ssl_status_to_mbedtls( status );
7151 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7152 goto end;
7153 }
7154
7155 transform->psa_alg = alg;
7156
7157 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7158 {
7159 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7160 psa_set_key_algorithm( &attributes, alg );
7161 psa_set_key_type( &attributes, key_type );
7162
7163 if( ( status = psa_import_key( &attributes,
7164 key1,
7165 PSA_BITS_TO_BYTES( key_bits ),
7166 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7167 {
7168 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7169 ret = psa_ssl_status_to_mbedtls( status );
7170 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7171 goto end;
7172 }
7173
7174 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7175
7176 if( ( status = psa_import_key( &attributes,
7177 key2,
7178 PSA_BITS_TO_BYTES( key_bits ),
7179 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7180 {
7181 ret = psa_ssl_status_to_mbedtls( status );
7182 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7183 goto end;
7184 }
7185 }
7186#else
7187 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7188 cipher_info ) ) != 0 )
7189 {
7190 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7191 goto end;
7192 }
7193
7194 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7195 cipher_info ) ) != 0 )
7196 {
7197 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7198 goto end;
7199 }
7200
7201 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7202 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7203 MBEDTLS_ENCRYPT ) ) != 0 )
7204 {
7205 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7206 goto end;
7207 }
7208
7209 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7210 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7211 MBEDTLS_DECRYPT ) ) != 0 )
7212 {
7213 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7214 goto end;
7215 }
7216
7217#if defined(MBEDTLS_CIPHER_MODE_CBC)
7218 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7219 {
7220 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7221 MBEDTLS_PADDING_NONE ) ) != 0 )
7222 {
7223 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7224 goto end;
7225 }
7226
7227 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7228 MBEDTLS_PADDING_NONE ) ) != 0 )
7229 {
7230 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7231 goto end;
7232 }
7233 }
7234#endif /* MBEDTLS_CIPHER_MODE_CBC */
7235#endif /* MBEDTLS_USE_PSA_CRYPTO */
7236
Neil Armstrong29c0c042022-03-17 17:47:28 +01007237#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7238 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7239 For AEAD-based ciphersuites, there is nothing to do here. */
7240 if( mac_key_len != 0 )
7241 {
7242#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007243 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007244
7245 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007246 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007247 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7248
7249 if( ( status = psa_import_key( &attributes,
7250 mac_enc, mac_key_len,
7251 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7252 {
7253 ret = psa_ssl_status_to_mbedtls( status );
7254 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7255 goto end;
7256 }
7257
Ronald Cronfb39f152022-03-25 14:36:28 +01007258 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
7259 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) &&
7260 ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007261 /* mbedtls_ct_hmac() requires the key to be exportable */
7262 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7263 PSA_KEY_USAGE_VERIFY_HASH );
7264 else
7265 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7266
7267 if( ( status = psa_import_key( &attributes,
7268 mac_dec, mac_key_len,
7269 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7270 {
7271 ret = psa_ssl_status_to_mbedtls( status );
7272 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7273 goto end;
7274 }
7275#else
7276 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7277 if( ret != 0 )
7278 goto end;
7279 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7280 if( ret != 0 )
7281 goto end;
7282#endif /* MBEDTLS_USE_PSA_CRYPTO */
7283 }
7284#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7285
7286 ((void) mac_dec);
7287 ((void) mac_enc);
7288
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007289end:
7290 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7291 return( ret );
7292}
7293
Jerry Yuee40f9d2022-02-17 14:55:16 +08007294#if defined(MBEDTLS_USE_PSA_CRYPTO)
7295int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7296 unsigned char *hash, size_t *hashlen,
7297 unsigned char *data, size_t data_len,
7298 mbedtls_md_type_t md_alg )
7299{
7300 psa_status_t status;
7301 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
7302 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
7303
7304 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7305
7306 if( ( status = psa_hash_setup( &hash_operation,
7307 hash_alg ) ) != PSA_SUCCESS )
7308 {
7309 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7310 goto exit;
7311 }
7312
7313 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
7314 64 ) ) != PSA_SUCCESS )
7315 {
7316 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7317 goto exit;
7318 }
7319
7320 if( ( status = psa_hash_update( &hash_operation,
7321 data, data_len ) ) != PSA_SUCCESS )
7322 {
7323 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7324 goto exit;
7325 }
7326
7327 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
7328 hashlen ) ) != PSA_SUCCESS )
7329 {
7330 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
7331 goto exit;
7332 }
7333
7334exit:
7335 if( status != PSA_SUCCESS )
7336 {
7337 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7338 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7339 switch( status )
7340 {
7341 case PSA_ERROR_NOT_SUPPORTED:
7342 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
7343 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
7344 case PSA_ERROR_BUFFER_TOO_SMALL:
7345 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
7346 case PSA_ERROR_INSUFFICIENT_MEMORY:
7347 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
7348 default:
7349 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
7350 }
7351 }
7352 return( 0 );
7353}
7354
7355#else
7356
7357int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7358 unsigned char *hash, size_t *hashlen,
7359 unsigned char *data, size_t data_len,
7360 mbedtls_md_type_t md_alg )
7361{
7362 int ret = 0;
7363 mbedtls_md_context_t ctx;
7364 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
7365 *hashlen = mbedtls_md_get_size( md_info );
7366
7367 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
7368
7369 mbedtls_md_init( &ctx );
7370
7371 /*
7372 * digitally-signed struct {
7373 * opaque client_random[32];
7374 * opaque server_random[32];
7375 * ServerDHParams params;
7376 * };
7377 */
7378 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
7379 {
7380 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7381 goto exit;
7382 }
7383 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
7384 {
7385 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
7386 goto exit;
7387 }
7388 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
7389 {
7390 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7391 goto exit;
7392 }
7393 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
7394 {
7395 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7396 goto exit;
7397 }
7398 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
7399 {
7400 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
7401 goto exit;
7402 }
7403
7404exit:
7405 mbedtls_md_free( &ctx );
7406
7407 if( ret != 0 )
7408 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7409 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7410
7411 return( ret );
7412}
7413#endif /* MBEDTLS_USE_PSA_CRYPTO */
7414
Jerry Yud9d91da2022-02-17 14:57:06 +08007415#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
7416
7417/* Find an entry in a signature-hash set matching a given hash algorithm. */
7418mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
7419 mbedtls_pk_type_t sig_alg )
7420{
7421 switch( sig_alg )
7422 {
7423 case MBEDTLS_PK_RSA:
7424 return( set->rsa );
7425 case MBEDTLS_PK_ECDSA:
7426 return( set->ecdsa );
7427 default:
7428 return( MBEDTLS_MD_NONE );
7429 }
7430}
7431
7432/* Add a signature-hash-pair to a signature-hash set */
7433void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
7434 mbedtls_pk_type_t sig_alg,
7435 mbedtls_md_type_t md_alg )
7436{
7437 switch( sig_alg )
7438 {
7439 case MBEDTLS_PK_RSA:
7440 if( set->rsa == MBEDTLS_MD_NONE )
7441 set->rsa = md_alg;
7442 break;
7443
7444 case MBEDTLS_PK_ECDSA:
7445 if( set->ecdsa == MBEDTLS_MD_NONE )
7446 set->ecdsa = md_alg;
7447 break;
7448
7449 default:
7450 break;
7451 }
7452}
7453
7454/* Allow exactly one hash algorithm for each signature. */
7455void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
7456 mbedtls_md_type_t md_alg )
7457{
7458 set->rsa = md_alg;
7459 set->ecdsa = md_alg;
7460}
7461
7462#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7463
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08007464/* Serialization of TLS 1.2 sessions:
7465 *
7466 * struct {
7467 * uint64 start_time;
7468 * uint8 ciphersuite[2]; // defined by the standard
7469 * uint8 compression; // 0 or 1
7470 * uint8 session_id_len; // at most 32
7471 * opaque session_id[32];
7472 * opaque master[48]; // fixed length in the standard
7473 * uint32 verify_result;
7474 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
7475 * opaque ticket<0..2^24-1>; // length 0 means no ticket
7476 * uint32 ticket_lifetime;
7477 * uint8 mfl_code; // up to 255 according to standard
7478 * uint8 encrypt_then_mac; // 0 or 1
7479 * } serialized_session_tls12;
7480 *
7481 */
7482static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session,
7483 unsigned char *buf,
7484 size_t buf_len )
7485{
7486 unsigned char *p = buf;
7487 size_t used = 0;
7488
7489#if defined(MBEDTLS_HAVE_TIME)
7490 uint64_t start;
7491#endif
7492#if defined(MBEDTLS_X509_CRT_PARSE_C)
7493#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7494 size_t cert_len;
7495#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7496#endif /* MBEDTLS_X509_CRT_PARSE_C */
7497
7498 /*
7499 * Time
7500 */
7501#if defined(MBEDTLS_HAVE_TIME)
7502 used += 8;
7503
7504 if( used <= buf_len )
7505 {
7506 start = (uint64_t) session->start;
7507
7508 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
7509 p += 8;
7510 }
7511#endif /* MBEDTLS_HAVE_TIME */
7512
7513 /*
7514 * Basic mandatory fields
7515 */
7516 used += 2 /* ciphersuite */
7517 + 1 /* compression */
7518 + 1 /* id_len */
7519 + sizeof( session->id )
7520 + sizeof( session->master )
7521 + 4; /* verify_result */
7522
7523 if( used <= buf_len )
7524 {
7525 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
7526 p += 2;
7527
7528 *p++ = MBEDTLS_BYTE_0( session->compression );
7529
7530 *p++ = MBEDTLS_BYTE_0( session->id_len );
7531 memcpy( p, session->id, 32 );
7532 p += 32;
7533
7534 memcpy( p, session->master, 48 );
7535 p += 48;
7536
7537 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
7538 p += 4;
7539 }
7540
7541 /*
7542 * Peer's end-entity certificate
7543 */
7544#if defined(MBEDTLS_X509_CRT_PARSE_C)
7545#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7546 if( session->peer_cert == NULL )
7547 cert_len = 0;
7548 else
7549 cert_len = session->peer_cert->raw.len;
7550
7551 used += 3 + cert_len;
7552
7553 if( used <= buf_len )
7554 {
7555 *p++ = MBEDTLS_BYTE_2( cert_len );
7556 *p++ = MBEDTLS_BYTE_1( cert_len );
7557 *p++ = MBEDTLS_BYTE_0( cert_len );
7558
7559 if( session->peer_cert != NULL )
7560 {
7561 memcpy( p, session->peer_cert->raw.p, cert_len );
7562 p += cert_len;
7563 }
7564 }
7565#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7566 if( session->peer_cert_digest != NULL )
7567 {
7568 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
7569 if( used <= buf_len )
7570 {
7571 *p++ = (unsigned char) session->peer_cert_digest_type;
7572 *p++ = (unsigned char) session->peer_cert_digest_len;
7573 memcpy( p, session->peer_cert_digest,
7574 session->peer_cert_digest_len );
7575 p += session->peer_cert_digest_len;
7576 }
7577 }
7578 else
7579 {
7580 used += 2;
7581 if( used <= buf_len )
7582 {
7583 *p++ = (unsigned char) MBEDTLS_MD_NONE;
7584 *p++ = 0;
7585 }
7586 }
7587#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7588#endif /* MBEDTLS_X509_CRT_PARSE_C */
7589
7590 /*
7591 * Session ticket if any, plus associated data
7592 */
7593#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7594 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
7595
7596 if( used <= buf_len )
7597 {
7598 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
7599 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
7600 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
7601
7602 if( session->ticket != NULL )
7603 {
7604 memcpy( p, session->ticket, session->ticket_len );
7605 p += session->ticket_len;
7606 }
7607
7608 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
7609 p += 4;
7610 }
7611#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7612
7613 /*
7614 * Misc extension-related info
7615 */
7616#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7617 used += 1;
7618
7619 if( used <= buf_len )
7620 *p++ = session->mfl_code;
7621#endif
7622
7623#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7624 used += 1;
7625
7626 if( used <= buf_len )
7627 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
7628#endif
7629
7630 return( used );
7631}
7632
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08007633static int ssl_session_load_tls12( mbedtls_ssl_session *session,
7634 const unsigned char *buf,
7635 size_t len )
7636{
7637#if defined(MBEDTLS_HAVE_TIME)
7638 uint64_t start;
7639#endif
7640#if defined(MBEDTLS_X509_CRT_PARSE_C)
7641#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7642 size_t cert_len;
7643#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7644#endif /* MBEDTLS_X509_CRT_PARSE_C */
7645
7646 const unsigned char *p = buf;
7647 const unsigned char * const end = buf + len;
7648
7649 /*
7650 * Time
7651 */
7652#if defined(MBEDTLS_HAVE_TIME)
7653 if( 8 > (size_t)( end - p ) )
7654 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7655
7656 start = ( (uint64_t) p[0] << 56 ) |
7657 ( (uint64_t) p[1] << 48 ) |
7658 ( (uint64_t) p[2] << 40 ) |
7659 ( (uint64_t) p[3] << 32 ) |
7660 ( (uint64_t) p[4] << 24 ) |
7661 ( (uint64_t) p[5] << 16 ) |
7662 ( (uint64_t) p[6] << 8 ) |
7663 ( (uint64_t) p[7] );
7664 p += 8;
7665
7666 session->start = (time_t) start;
7667#endif /* MBEDTLS_HAVE_TIME */
7668
7669 /*
7670 * Basic mandatory fields
7671 */
7672 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
7673 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7674
7675 session->ciphersuite = ( p[0] << 8 ) | p[1];
7676 p += 2;
7677
7678 session->compression = *p++;
7679
7680 session->id_len = *p++;
7681 memcpy( session->id, p, 32 );
7682 p += 32;
7683
7684 memcpy( session->master, p, 48 );
7685 p += 48;
7686
7687 session->verify_result = ( (uint32_t) p[0] << 24 ) |
7688 ( (uint32_t) p[1] << 16 ) |
7689 ( (uint32_t) p[2] << 8 ) |
7690 ( (uint32_t) p[3] );
7691 p += 4;
7692
7693 /* Immediately clear invalid pointer values that have been read, in case
7694 * we exit early before we replaced them with valid ones. */
7695#if defined(MBEDTLS_X509_CRT_PARSE_C)
7696#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7697 session->peer_cert = NULL;
7698#else
7699 session->peer_cert_digest = NULL;
7700#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7701#endif /* MBEDTLS_X509_CRT_PARSE_C */
7702#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7703 session->ticket = NULL;
7704#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7705
7706 /*
7707 * Peer certificate
7708 */
7709#if defined(MBEDTLS_X509_CRT_PARSE_C)
7710#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7711 /* Deserialize CRT from the end of the ticket. */
7712 if( 3 > (size_t)( end - p ) )
7713 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7714
7715 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
7716 p += 3;
7717
7718 if( cert_len != 0 )
7719 {
7720 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7721
7722 if( cert_len > (size_t)( end - p ) )
7723 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7724
7725 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7726
7727 if( session->peer_cert == NULL )
7728 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7729
7730 mbedtls_x509_crt_init( session->peer_cert );
7731
7732 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
7733 p, cert_len ) ) != 0 )
7734 {
7735 mbedtls_x509_crt_free( session->peer_cert );
7736 mbedtls_free( session->peer_cert );
7737 session->peer_cert = NULL;
7738 return( ret );
7739 }
7740
7741 p += cert_len;
7742 }
7743#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7744 /* Deserialize CRT digest from the end of the ticket. */
7745 if( 2 > (size_t)( end - p ) )
7746 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7747
7748 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
7749 session->peer_cert_digest_len = (size_t) *p++;
7750
7751 if( session->peer_cert_digest_len != 0 )
7752 {
7753 const mbedtls_md_info_t *md_info =
7754 mbedtls_md_info_from_type( session->peer_cert_digest_type );
7755 if( md_info == NULL )
7756 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7757 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
7758 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7759
7760 if( session->peer_cert_digest_len > (size_t)( end - p ) )
7761 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7762
7763 session->peer_cert_digest =
7764 mbedtls_calloc( 1, session->peer_cert_digest_len );
7765 if( session->peer_cert_digest == NULL )
7766 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7767
7768 memcpy( session->peer_cert_digest, p,
7769 session->peer_cert_digest_len );
7770 p += session->peer_cert_digest_len;
7771 }
7772#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7773#endif /* MBEDTLS_X509_CRT_PARSE_C */
7774
7775 /*
7776 * Session ticket and associated data
7777 */
7778#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7779 if( 3 > (size_t)( end - p ) )
7780 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7781
7782 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
7783 p += 3;
7784
7785 if( session->ticket_len != 0 )
7786 {
7787 if( session->ticket_len > (size_t)( end - p ) )
7788 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7789
7790 session->ticket = mbedtls_calloc( 1, session->ticket_len );
7791 if( session->ticket == NULL )
7792 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7793
7794 memcpy( session->ticket, p, session->ticket_len );
7795 p += session->ticket_len;
7796 }
7797
7798 if( 4 > (size_t)( end - p ) )
7799 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7800
7801 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
7802 ( (uint32_t) p[1] << 16 ) |
7803 ( (uint32_t) p[2] << 8 ) |
7804 ( (uint32_t) p[3] );
7805 p += 4;
7806#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7807
7808 /*
7809 * Misc extension-related info
7810 */
7811#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7812 if( 1 > (size_t)( end - p ) )
7813 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7814
7815 session->mfl_code = *p++;
7816#endif
7817
7818#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7819 if( 1 > (size_t)( end - p ) )
7820 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7821
7822 session->encrypt_then_mac = *p++;
7823#endif
7824
7825 /* Done, should have consumed entire buffer */
7826 if( p != end )
7827 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7828
7829 return( 0 );
7830}
Jerry Yudc7bd172022-02-17 13:44:15 +08007831#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007833#endif /* MBEDTLS_SSL_TLS_C */