blob: 77d4e2d4fb8408e0b4ae16e9b842927c74d47037 [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
117int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
118 int *enabled,
119 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
120 size_t *peer_cid_len )
121{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100122 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100123
Hanno Becker76a79ab2019-05-03 14:38:32 +0100124 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
125 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
126 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100127 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100128 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100129
Hanno Beckerc5f24222019-05-03 12:54:52 +0100130 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
131 * were used, but client and server requested the empty CID.
132 * This is indistinguishable from not using the CID extension
133 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100134 if( ssl->transform_in->in_cid_len == 0 &&
135 ssl->transform_in->out_cid_len == 0 )
136 {
137 return( 0 );
138 }
139
Hanno Becker615ef172019-05-22 16:50:35 +0100140 if( peer_cid_len != NULL )
141 {
142 *peer_cid_len = ssl->transform_in->out_cid_len;
143 if( peer_cid != NULL )
144 {
145 memcpy( peer_cid, ssl->transform_in->out_cid,
146 ssl->transform_in->out_cid_len );
147 }
148 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100149
150 *enabled = MBEDTLS_SSL_CID_ENABLED;
151
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100152 return( 0 );
153}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100154#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200159/*
160 * Convert max_fragment_length codes to length.
161 * RFC 6066 says:
162 * enum{
163 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
164 * } MaxFragmentLength;
165 * and we add 0 -> extension unused
166 */
Angus Grattond8213d02016-05-25 20:56:48 +1000167static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200168{
Angus Grattond8213d02016-05-25 20:56:48 +1000169 switch( mfl )
170 {
171 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
172 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
173 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
174 return 512;
175 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
176 return 1024;
177 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
178 return 2048;
179 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
180 return 4096;
181 default:
182 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
183 }
184}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200186
Hanno Becker52055ae2019-02-06 14:30:46 +0000187int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
188 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200189{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_ssl_session_free( dst );
191 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200192
吴敬辉0b716112021-11-29 10:46:35 +0800193#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
194 dst->ticket = NULL;
195#endif
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000198
199#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200200 if( src->peer_cert != NULL )
201 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000202 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200203
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200204 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200205 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200206 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200211 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200214 dst->peer_cert = NULL;
215 return( ret );
216 }
217 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000218#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000219 if( src->peer_cert_digest != NULL )
220 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000221 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000222 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000223 if( dst->peer_cert_digest == NULL )
224 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
225
226 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
227 src->peer_cert_digest_len );
228 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000229 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000230 }
231#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200234
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200235#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200236 if( src->ticket != NULL )
237 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200238 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200239 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200240 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200241
242 memcpy( dst->ticket, src->ticket, src->ticket_len );
243 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200244#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200245
246 return( 0 );
247}
248
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500249#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
250static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
251{
252 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
253 if( resized_buffer == NULL )
254 return -1;
255
256 /* We want to copy len_new bytes when downsizing the buffer, and
257 * len_old bytes when upsizing, so we choose the smaller of two sizes,
258 * to fit one buffer into another. Size checks, ensuring that no data is
259 * lost, are done outside of this function. */
260 memcpy( resized_buffer, *buffer,
261 ( len_new < *len_old ) ? len_new : *len_old );
262 mbedtls_platform_zeroize( *buffer, *len_old );
263 mbedtls_free( *buffer );
264
265 *buffer = resized_buffer;
266 *len_old = len_new;
267
268 return 0;
269}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200270
271static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500272 size_t in_buf_new_len,
273 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200274{
275 int modified = 0;
276 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
277 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
278 if( ssl->in_buf != NULL )
279 {
280 written_in = ssl->in_msg - ssl->in_buf;
281 iv_offset_in = ssl->in_iv - ssl->in_buf;
282 len_offset_in = ssl->in_len - ssl->in_buf;
283 if( downsizing ?
284 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
285 ssl->in_buf_len < in_buf_new_len )
286 {
287 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
288 {
289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
290 }
291 else
292 {
Paul Elliottb7449902021-03-10 18:14:58 +0000293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
294 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200295 modified = 1;
296 }
297 }
298 }
299
300 if( ssl->out_buf != NULL )
301 {
302 written_out = ssl->out_msg - ssl->out_buf;
303 iv_offset_out = ssl->out_iv - ssl->out_buf;
304 len_offset_out = ssl->out_len - ssl->out_buf;
305 if( downsizing ?
306 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
307 ssl->out_buf_len < out_buf_new_len )
308 {
309 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
310 {
311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
312 }
313 else
314 {
Paul Elliottb7449902021-03-10 18:14:58 +0000315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
316 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200317 modified = 1;
318 }
319 }
320 }
321 if( modified )
322 {
323 /* Update pointers here to avoid doing it twice. */
324 mbedtls_ssl_reset_in_out_pointers( ssl );
325 /* Fields below might not be properly updated with record
326 * splitting or with CID, so they are manually updated here. */
327 ssl->out_msg = ssl->out_buf + written_out;
328 ssl->out_len = ssl->out_buf + len_offset_out;
329 ssl->out_iv = ssl->out_buf + iv_offset_out;
330
331 ssl->in_msg = ssl->in_buf + written_in;
332 ssl->in_len = ssl->in_buf + len_offset_in;
333 ssl->in_iv = ssl->in_buf + iv_offset_in;
334 }
335}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500336#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
337
Jerry Yudb8c48a2022-01-27 14:54:54 +0800338#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800339
340#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
341typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
342 const char *label,
343 const unsigned char *random, size_t rlen,
344 unsigned char *dstbuf, size_t dlen );
345
346static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
347
348#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
349
350/* Type for the TLS PRF */
351typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
352 const unsigned char *, size_t,
353 unsigned char *, size_t);
354
355static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
356 int ciphersuite,
357 const unsigned char master[48],
358#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
359 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
360 int encrypt_then_mac,
361#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
362 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
363 ssl_tls_prf_t tls_prf,
364 const unsigned char randbytes[64],
365 int minor_ver,
366 unsigned endpoint,
367 const mbedtls_ssl_context *ssl );
368
369#if defined(MBEDTLS_SHA256_C)
370static int tls_prf_sha256( const unsigned char *secret, size_t slen,
371 const char *label,
372 const unsigned char *random, size_t rlen,
373 unsigned char *dstbuf, size_t dlen );
374static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
375static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
376
377#endif /* MBEDTLS_SHA256_C */
378
379#if defined(MBEDTLS_SHA384_C)
380static int tls_prf_sha384( const unsigned char *secret, size_t slen,
381 const char *label,
382 const unsigned char *random, size_t rlen,
383 unsigned char *dstbuf, size_t dlen );
384
385static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
386static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
387#endif /* MBEDTLS_SHA384_C */
388
389static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session,
390 unsigned char *buf,
391 size_t buf_len );
392static int ssl_session_load_tls12( mbedtls_ssl_session *session,
393 const unsigned char *buf,
394 size_t len );
395#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
396
Jerry Yu53d23e22022-02-09 16:25:09 +0800397static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
398
Jerry Yudb8c48a2022-01-27 14:54:54 +0800399#if defined(MBEDTLS_SHA256_C)
400static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Jerry Yu53d23e22022-02-09 16:25:09 +0800401#endif /* MBEDTLS_SHA256_C */
Jerry Yudb8c48a2022-01-27 14:54:54 +0800402
403#if defined(MBEDTLS_SHA384_C)
404static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Jerry Yu53d23e22022-02-09 16:25:09 +0800405#endif /* MBEDTLS_SHA384_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000406
Ron Eldor51d3ab52019-05-12 14:54:30 +0300407int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
408 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{
413 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
414
415 switch( prf )
416 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300417#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +0200418#if defined(MBEDTLS_SHA384_C)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300419 case MBEDTLS_SSL_TLS_PRF_SHA384:
420 tls_prf = tls_prf_sha384;
421 break;
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +0200422#endif /* MBEDTLS_SHA384_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300423#if defined(MBEDTLS_SHA256_C)
424 case MBEDTLS_SSL_TLS_PRF_SHA256:
425 tls_prf = tls_prf_sha256;
426 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300427#endif /* MBEDTLS_SHA256_C */
428#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300429 default:
430 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
431 }
432
433 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
434}
435
Jerry Yuc73c6182022-02-08 20:29:25 +0800436#if defined(MBEDTLS_X509_CRT_PARSE_C)
437static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
438{
439#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
440 if( session->peer_cert != NULL )
441 {
442 mbedtls_x509_crt_free( session->peer_cert );
443 mbedtls_free( session->peer_cert );
444 session->peer_cert = NULL;
445 }
446#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
447 if( session->peer_cert_digest != NULL )
448 {
449 /* Zeroization is not necessary. */
450 mbedtls_free( session->peer_cert_digest );
451 session->peer_cert_digest = NULL;
452 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
453 session->peer_cert_digest_len = 0;
454 }
455#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
456}
457#endif /* MBEDTLS_X509_CRT_PARSE_C */
458
459void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
460 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
461{
462 ((void) ciphersuite_info);
463
464#if defined(MBEDTLS_SHA384_C)
465 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
466 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
467 else
468#endif
469#if defined(MBEDTLS_SHA256_C)
470 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
471 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
472 else
473#endif
474 {
475 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
476 return;
477 }
478}
479
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100480static void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
481 unsigned hs_type,
482 size_t total_hs_len )
483{
484 unsigned char hs_hdr[4];
485
486 /* Build HS header for checksum update. */
487 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
488 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
489 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
490 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
491
492 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
493}
494
495void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
496 unsigned hs_type,
497 unsigned char const *msg,
498 size_t msg_len )
499{
500 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
501 ssl->handshake->update_checksum( ssl, msg, msg_len );
502}
503
Jerry Yuc73c6182022-02-08 20:29:25 +0800504void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
505{
506 ((void) ssl);
507#if defined(MBEDTLS_SHA256_C)
508#if defined(MBEDTLS_USE_PSA_CRYPTO)
509 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
510 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
511#else
512 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
513#endif
514#endif
515#if defined(MBEDTLS_SHA384_C)
516#if defined(MBEDTLS_USE_PSA_CRYPTO)
517 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
518 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
519#else
520 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
521#endif
522#endif
523}
524
525static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
526 const unsigned char *buf, size_t len )
527{
528#if defined(MBEDTLS_SHA256_C)
529#if defined(MBEDTLS_USE_PSA_CRYPTO)
530 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
531#else
532 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
533#endif
534#endif
535#if defined(MBEDTLS_SHA384_C)
536#if defined(MBEDTLS_USE_PSA_CRYPTO)
537 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
538#else
539 mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len );
540#endif
541#endif
542}
543
544#if defined(MBEDTLS_SHA256_C)
545static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
546 const unsigned char *buf, size_t len )
547{
548#if defined(MBEDTLS_USE_PSA_CRYPTO)
549 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
550#else
551 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
552#endif
553}
554#endif
555
556#if defined(MBEDTLS_SHA384_C)
557static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
558 const unsigned char *buf, size_t len )
559{
560#if defined(MBEDTLS_USE_PSA_CRYPTO)
561 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
562#else
563 mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len );
564#endif
565}
566#endif
567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200569{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500573#if defined(MBEDTLS_USE_PSA_CRYPTO)
574 handshake->fin_sha256_psa = psa_hash_operation_init();
575 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
576#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200578 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200579#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500580#endif
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +0200581#if defined(MBEDTLS_SHA384_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500582#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500583 handshake->fin_sha384_psa = psa_hash_operation_init();
584 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500585#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_sha512_init( &handshake->fin_sha512 );
TRodziewicz26371e42021-06-08 16:45:41 +0200587 mbedtls_sha512_starts( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200588#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500589#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200590
591 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100592
593#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +0100594 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +0100595 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
596#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598#if defined(MBEDTLS_DHM_C)
599 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200600#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601#if defined(MBEDTLS_ECDH_C)
602 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200603#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200604#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200605 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200606#if defined(MBEDTLS_SSL_CLI_C)
607 handshake->ecjpake_cache = NULL;
608 handshake->ecjpake_cache_len = 0;
609#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200610#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200611
Gilles Peskineeccd8882020-03-10 12:19:08 +0100612#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200613 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200614#endif
615
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200616#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
617 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
618#endif
Hanno Becker75173122019-02-06 16:18:31 +0000619
620#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
621 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
622 mbedtls_pk_init( &handshake->peer_pubkey );
623#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200624}
625
Hanno Beckera18d1322018-01-03 14:27:32 +0000626void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200627{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200629
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100630#if defined(MBEDTLS_USE_PSA_CRYPTO)
631 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
632 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100633#else
634 mbedtls_cipher_init( &transform->cipher_ctx_enc );
635 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100636#endif
637
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000638#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100639#if defined(MBEDTLS_USE_PSA_CRYPTO)
640 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
641 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100642#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 mbedtls_md_init( &transform->md_ctx_enc );
644 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000645#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100646#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200647}
648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200650{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200652}
653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000655{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200656 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000657 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200659 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200661 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200662 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200663
664 /*
665 * Either the pointers are now NULL or cleared properly and can be freed.
666 * Now allocate missing structures.
667 */
668 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200669 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200670 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200671 }
Paul Bakker48916f92012-09-16 19:57:18 +0000672
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200673 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200674 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200675 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200676 }
Paul Bakker48916f92012-09-16 19:57:18 +0000677
Paul Bakker82788fb2014-10-20 13:59:19 +0200678 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200679 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200680 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200681 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500682#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
683 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400684
Andrzej Kurek4a063792020-10-21 15:08:44 +0200685 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
686 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500687#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000688
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200689 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000690 if( ssl->handshake == NULL ||
691 ssl->transform_negotiate == NULL ||
692 ssl->session_negotiate == NULL )
693 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_free( ssl->handshake );
697 mbedtls_free( ssl->transform_negotiate );
698 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200699
700 ssl->handshake = NULL;
701 ssl->transform_negotiate = NULL;
702 ssl->session_negotiate = NULL;
703
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200704 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000705 }
706
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200707 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000709 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200710 ssl_handshake_params_init( ssl->handshake );
711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200713 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
714 {
715 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200716
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200717 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
718 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
719 else
720 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200721
Hanno Becker0f57a652020-02-05 10:37:26 +0000722 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200723 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200724#endif
725
Brett Warrene0edc842021-08-17 09:53:13 +0100726/*
727 * curve_list is translated to IANA TLS group identifiers here because
728 * mbedtls_ssl_conf_curves returns void and so can't return
729 * any error codes.
730 */
731#if defined(MBEDTLS_ECP_C)
732#if !defined(MBEDTLS_DEPRECATED_REMOVED)
733 /* Heap allocate and translate curve_list from internal to IANA group ids */
734 if ( ssl->conf->curve_list != NULL )
735 {
736 size_t length;
737 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
738
739 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
740 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
741
742 /* Leave room for zero termination */
743 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
744 if ( group_list == NULL )
745 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
746
747 for( size_t i = 0; i < length; i++ )
748 {
749 const mbedtls_ecp_curve_info *info =
750 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
751 if ( info == NULL )
752 {
753 mbedtls_free( group_list );
754 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
755 }
756 group_list[i] = info->tls_id;
757 }
758
759 group_list[length] = 0;
760
761 ssl->handshake->group_list = group_list;
762 ssl->handshake->group_list_heap_allocated = 1;
763 }
764 else
765 {
766 ssl->handshake->group_list = ssl->conf->group_list;
767 ssl->handshake->group_list_heap_allocated = 0;
768 }
769#endif /* MBEDTLS_DEPRECATED_REMOVED */
770#endif /* MBEDTLS_ECP_C */
771
Jerry Yuf017ee42022-01-12 15:49:48 +0800772#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
773#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800774#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800775 /* Heap allocate and translate sig_hashes from internal hash identifiers to
776 signature algorithms IANA identifiers. */
777 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800778 ssl->conf->sig_hashes != NULL )
779 {
780 const int *md;
781 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800782 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800783 uint16_t *p;
784
Jerry Yub476a442022-01-21 18:14:45 +0800785#if defined(static_assert)
786 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
787 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
788 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800789#endif
790
Jerry Yuf017ee42022-01-12 15:49:48 +0800791 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
792 {
793 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
794 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800795#if defined(MBEDTLS_ECDSA_C)
796 sig_algs_len += sizeof( uint16_t );
797#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800798
Jerry Yub476a442022-01-21 18:14:45 +0800799#if defined(MBEDTLS_RSA_C)
800 sig_algs_len += sizeof( uint16_t );
801#endif
802 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
803 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800804 }
805
Jerry Yub476a442022-01-21 18:14:45 +0800806 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800807 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
808
Jerry Yub476a442022-01-21 18:14:45 +0800809 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
810 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800811 if( ssl->handshake->sig_algs == NULL )
812 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
813
814 p = (uint16_t *)ssl->handshake->sig_algs;
815 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
816 {
817 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
818 if( hash == MBEDTLS_SSL_HASH_NONE )
819 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800820#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800821 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
822 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800823#endif
824#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800825 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
826 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800827#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800828 }
829 *p = MBEDTLS_TLS1_3_SIG_NONE;
830 ssl->handshake->sig_algs_heap_allocated = 1;
831 }
832 else
Jerry Yua69269a2022-01-17 21:06:01 +0800833#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800834 {
835 ssl->handshake->sig_algs = ssl->conf->sig_algs;
836 ssl->handshake->sig_algs_heap_allocated = 0;
837 }
838#endif /* MBEDTLS_DEPRECATED_REMOVED */
839#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000840 return( 0 );
841}
842
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200843#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200844/* Dummy cookie callbacks for defaults */
845static int ssl_cookie_write_dummy( void *ctx,
846 unsigned char **p, unsigned char *end,
847 const unsigned char *cli_id, size_t cli_id_len )
848{
849 ((void) ctx);
850 ((void) p);
851 ((void) end);
852 ((void) cli_id);
853 ((void) cli_id_len);
854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200856}
857
858static int ssl_cookie_check_dummy( void *ctx,
859 const unsigned char *cookie, size_t cookie_len,
860 const unsigned char *cli_id, size_t cli_id_len )
861{
862 ((void) ctx);
863 ((void) cookie);
864 ((void) cookie_len);
865 ((void) cli_id);
866 ((void) cli_id_len);
867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200869}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200870#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200871
Paul Bakker5121ce52009-01-03 21:22:43 +0000872/*
873 * Initialize an SSL context
874 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200875void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
876{
877 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
878}
879
Jerry Yu60835a82021-08-04 10:13:52 +0800880static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
881{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100882 const mbedtls_ssl_config *conf = ssl->conf;
883
Ronald Cron6f135e12021-12-08 16:57:54 +0100884#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100885 if( mbedtls_ssl_conf_is_tls13_enabled( conf ) &&
886 ( conf->endpoint == MBEDTLS_SSL_IS_SERVER ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800887 {
Ronald Cron086ee0b2022-03-15 15:18:51 +0100888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
889 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
890 }
891
892 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
893 {
894 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jerry Yu60835a82021-08-04 10:13:52 +0800895 {
Ronald Cron086ee0b2022-03-15 15:18:51 +0100896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
Jerry Yu60835a82021-08-04 10:13:52 +0800897 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
898 }
899 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
900 return( 0 );
901 }
902#endif
903
904#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100905 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800906 {
907 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
908 return( 0 );
909 }
910#endif
911
Ronald Cron6f135e12021-12-08 16:57:54 +0100912#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100913 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800914 {
Ronald Crone1d3f062022-02-10 14:50:54 +0100915 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
916 {
917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
918 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
919 }
920 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
921 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +0800922 }
923#endif
924
925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
926 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
927}
928
929static int ssl_conf_check(const mbedtls_ssl_context *ssl)
930{
931 int ret;
932 ret = ssl_conf_version_check( ssl );
933 if( ret != 0 )
934 return( ret );
935
936 /* Space for further checks */
937
938 return( 0 );
939}
940
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200941/*
942 * Setup an SSL context
943 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100944
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200945int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +0200946 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +0000947{
Janos Follath865b3eb2019-12-16 11:46:15 +0000948 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +0000949 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
950 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +0000951
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200952 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +0000953
Jerry Yu60835a82021-08-04 10:13:52 +0800954 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
955 return( ret );
956
Paul Bakker62f2dee2012-09-28 07:31:51 +0000957 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +0100958 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +0000959 */
k-stachowiakc9a5f022018-07-24 13:53:31 +0200960
961 /* Set to NULL in case of an error condition */
962 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +0200963
Darryl Greenb33cc762019-11-28 14:29:44 +0000964#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
965 ssl->in_buf_len = in_buf_len;
966#endif
967 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +1000968 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000969 {
Paul Elliottd48d5c62021-01-07 14:47:05 +0000970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +0200971 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +0200972 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +1000973 }
974
Darryl Greenb33cc762019-11-28 14:29:44 +0000975#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
976 ssl->out_buf_len = out_buf_len;
977#endif
978 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +1000979 if( ssl->out_buf == NULL )
980 {
Paul Elliottd48d5c62021-01-07 14:47:05 +0000981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +0200982 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +0200983 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +0000984 }
985
Hanno Becker3e6f8ab2020-02-05 10:40:57 +0000986 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200987
Johan Pascalb62bb512015-12-03 21:56:45 +0100988#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +0200989 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +0100990#endif
991
Paul Bakker48916f92012-09-16 19:57:18 +0000992 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +0200993 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +0000994
995 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +0200996
997error:
998 mbedtls_free( ssl->in_buf );
999 mbedtls_free( ssl->out_buf );
1000
1001 ssl->conf = NULL;
1002
Darryl Greenb33cc762019-11-28 14:29:44 +00001003#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1004 ssl->in_buf_len = 0;
1005 ssl->out_buf_len = 0;
1006#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001007 ssl->in_buf = NULL;
1008 ssl->out_buf = NULL;
1009
1010 ssl->in_hdr = NULL;
1011 ssl->in_ctr = NULL;
1012 ssl->in_len = NULL;
1013 ssl->in_iv = NULL;
1014 ssl->in_msg = NULL;
1015
1016 ssl->out_hdr = NULL;
1017 ssl->out_ctr = NULL;
1018 ssl->out_len = NULL;
1019 ssl->out_iv = NULL;
1020 ssl->out_msg = NULL;
1021
k-stachowiak9f7798e2018-07-31 16:52:32 +02001022 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001023}
1024
1025/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001026 * Reset an initialized and used SSL context for re-use while retaining
1027 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001028 *
1029 * If partial is non-zero, keep data in the input buffer and client ID.
1030 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001031 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001032void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1033 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001034{
Darryl Greenb33cc762019-11-28 14:29:44 +00001035#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1036 size_t in_buf_len = ssl->in_buf_len;
1037 size_t out_buf_len = ssl->out_buf_len;
1038#else
1039 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1040 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1041#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001042
Hanno Beckerb0302c42021-08-03 09:39:42 +01001043#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1044 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001045#endif
1046
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001047 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001048 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001049
Hanno Beckerb0302c42021-08-03 09:39:42 +01001050 mbedtls_ssl_reset_in_out_pointers( ssl );
1051
1052 /* Reset incoming message parsing */
1053 ssl->in_offt = NULL;
1054 ssl->nb_zero = 0;
1055 ssl->in_msgtype = 0;
1056 ssl->in_msglen = 0;
1057 ssl->in_hslen = 0;
1058 ssl->keep_current_message = 0;
1059 ssl->transform_in = NULL;
1060
1061#if defined(MBEDTLS_SSL_PROTO_DTLS)
1062 ssl->next_record_offset = 0;
1063 ssl->in_epoch = 0;
1064#endif
1065
1066 /* Keep current datagram if partial == 1 */
1067 if( partial == 0 )
1068 {
1069 ssl->in_left = 0;
1070 memset( ssl->in_buf, 0, in_buf_len );
1071 }
1072
1073 /* Reset outgoing message writing */
1074 ssl->out_msgtype = 0;
1075 ssl->out_msglen = 0;
1076 ssl->out_left = 0;
1077 memset( ssl->out_buf, 0, out_buf_len );
1078 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1079 ssl->transform_out = NULL;
1080
1081#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1082 mbedtls_ssl_dtls_replay_reset( ssl );
1083#endif
1084
XiaokangQian2b01dc32022-01-21 02:53:13 +00001085#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001086 if( ssl->transform )
1087 {
1088 mbedtls_ssl_transform_free( ssl->transform );
1089 mbedtls_free( ssl->transform );
1090 ssl->transform = NULL;
1091 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001092#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1093
XiaokangQian2b01dc32022-01-21 02:53:13 +00001094#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1095 mbedtls_ssl_transform_free( ssl->transform_application );
1096 mbedtls_free( ssl->transform_application );
1097 ssl->transform_application = NULL;
1098
1099 if( ssl->handshake != NULL )
1100 {
1101 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1102 mbedtls_free( ssl->handshake->transform_earlydata );
1103 ssl->handshake->transform_earlydata = NULL;
1104
1105 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1106 mbedtls_free( ssl->handshake->transform_handshake );
1107 ssl->handshake->transform_handshake = NULL;
1108 }
1109
XiaokangQian2b01dc32022-01-21 02:53:13 +00001110#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001111}
1112
1113int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1114{
1115 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1116
1117 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1118
XiaokangQian78b1fa72022-01-19 06:56:30 +00001119 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001120
1121 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122#if defined(MBEDTLS_SSL_RENEGOTIATION)
1123 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001124 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001125
1126 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1128 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001129#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001131
Hanno Beckerb0302c42021-08-03 09:39:42 +01001132 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001133 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001134 if( ssl->session )
1135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 mbedtls_ssl_session_free( ssl->session );
1137 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001138 ssl->session = NULL;
1139 }
1140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001142 ssl->alpn_chosen = NULL;
1143#endif
1144
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001145#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01001146#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001147 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01001148#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001149 {
1150 mbedtls_free( ssl->cli_id );
1151 ssl->cli_id = NULL;
1152 ssl->cli_id_len = 0;
1153 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001154#endif
1155
Paul Bakker48916f92012-09-16 19:57:18 +00001156 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1157 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001158
1159 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001160}
1161
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001162/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001163 * Reset an initialized and used SSL context for re-use while retaining
1164 * all application-set variables, function pointers and data.
1165 */
1166int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1167{
Hanno Becker43aefe22020-02-05 10:44:56 +00001168 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001169}
1170
1171/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001172 * SSL set accessors
1173 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001174void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001175{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001176 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001177}
1178
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001179void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001180{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001181 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001182}
1183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001185void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001186{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001187 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001188}
1189#endif
1190
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001191void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001192{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001193 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001194}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001197
Hanno Becker1841b0a2018-08-24 11:13:57 +01001198void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1199 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001200{
1201 ssl->disable_datagram_packing = !allow_packing;
1202}
1203
1204void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1205 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001206{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001207 conf->hs_timeout_min = min;
1208 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001209}
1210#endif
1211
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001212void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001213{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001214 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001215}
1216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001218void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001219 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001220 void *p_vrfy )
1221{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001222 conf->f_vrfy = f_vrfy;
1223 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001224}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001226
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001227void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001228 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001229 void *p_rng )
1230{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001231 conf->f_rng = f_rng;
1232 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001233}
1234
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001235void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001236 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 void *p_dbg )
1238{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001239 conf->f_dbg = f_dbg;
1240 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001241}
1242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001244 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001245 mbedtls_ssl_send_t *f_send,
1246 mbedtls_ssl_recv_t *f_recv,
1247 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001248{
1249 ssl->p_bio = p_bio;
1250 ssl->f_send = f_send;
1251 ssl->f_recv = f_recv;
1252 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001253}
1254
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001255#if defined(MBEDTLS_SSL_PROTO_DTLS)
1256void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1257{
1258 ssl->mtu = mtu;
1259}
1260#endif
1261
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001262void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001263{
1264 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001265}
1266
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001267void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1268 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001269 mbedtls_ssl_set_timer_t *f_set_timer,
1270 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001271{
1272 ssl->p_timer = p_timer;
1273 ssl->f_set_timer = f_set_timer;
1274 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001275
1276 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001277 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001278}
1279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280#if defined(MBEDTLS_SSL_SRV_C)
Glenn Strauss2ed95272022-01-21 18:02:17 -05001281void mbedtls_ssl_conf_cert_cb( mbedtls_ssl_config *conf,
1282 int (*f_cert_cb)(mbedtls_ssl_context *) )
1283{
1284 conf->f_cert_cb = f_cert_cb;
1285}
1286#endif /* MBEDTLS_SSL_SRV_C */
1287
1288#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001289void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001290 void *p_cache,
1291 mbedtls_ssl_cache_get_t *f_get_cache,
1292 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001293{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001294 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001295 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001296 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001297}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300#if defined(MBEDTLS_SSL_CLI_C)
1301int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001302{
Janos Follath865b3eb2019-12-16 11:46:15 +00001303 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001304
1305 if( ssl == NULL ||
1306 session == NULL ||
1307 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001308 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001311 }
1312
Hanno Beckere810bbc2021-05-14 16:01:05 +01001313 if( ssl->handshake->resume == 1 )
1314 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1315
Hanno Becker52055ae2019-02-06 14:30:46 +00001316 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1317 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001318 return( ret );
1319
Paul Bakker0a597072012-09-25 21:55:46 +00001320 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001321
1322 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001323}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001325
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001326void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1327 const int *ciphersuites )
1328{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001329 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001330}
1331
Ronald Cron6f135e12021-12-08 16:57:54 +01001332#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001333void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001334 const int kex_modes )
1335{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001336 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001337}
Ronald Cron6f135e12021-12-08 16:57:54 +01001338#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001341void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001342 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001343{
1344 conf->cert_profile = profile;
1345}
1346
Glenn Strauss36872db2022-01-22 05:06:31 -05001347static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1348{
1349 mbedtls_ssl_key_cert *cur = key_cert, *next;
1350
1351 while( cur != NULL )
1352 {
1353 next = cur->next;
1354 mbedtls_free( cur );
1355 cur = next;
1356 }
1357}
1358
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001359/* Append a new keycert entry to a (possibly empty) list */
1360static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1361 mbedtls_x509_crt *cert,
1362 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001363{
niisato8ee24222018-06-25 19:05:48 +09001364 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001365
Glenn Strauss36872db2022-01-22 05:06:31 -05001366 if( cert == NULL )
1367 {
1368 /* Free list if cert is null */
1369 ssl_key_cert_free( *head );
1370 *head = NULL;
1371 return( 0 );
1372 }
1373
niisato8ee24222018-06-25 19:05:48 +09001374 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1375 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001376 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001377
niisato8ee24222018-06-25 19:05:48 +09001378 new_cert->cert = cert;
1379 new_cert->key = key;
1380 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001381
Glenn Strauss36872db2022-01-22 05:06:31 -05001382 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001383 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001384 {
niisato8ee24222018-06-25 19:05:48 +09001385 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001386 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001387 else
1388 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001389 mbedtls_ssl_key_cert *cur = *head;
1390 while( cur->next != NULL )
1391 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001392 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001393 }
1394
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001395 return( 0 );
1396}
1397
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001398int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001399 mbedtls_x509_crt *own_cert,
1400 mbedtls_pk_context *pk_key )
1401{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001402 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001403}
1404
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001405void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001406 mbedtls_x509_crt *ca_chain,
1407 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001408{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001409 conf->ca_chain = ca_chain;
1410 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001411
1412#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1413 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1414 * cannot be used together. */
1415 conf->f_ca_cb = NULL;
1416 conf->p_ca_cb = NULL;
1417#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001418}
Hanno Becker5adaad92019-03-27 16:54:37 +00001419
1420#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1421void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001422 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001423 void *p_ca_cb )
1424{
1425 conf->f_ca_cb = f_ca_cb;
1426 conf->p_ca_cb = p_ca_cb;
1427
1428 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1429 * cannot be used together. */
1430 conf->ca_chain = NULL;
1431 conf->ca_crl = NULL;
1432}
1433#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001435
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001436#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001437const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1438 size_t *name_len )
1439{
1440 *name_len = ssl->handshake->sni_name_len;
1441 return( ssl->handshake->sni_name );
1442}
1443
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001444int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1445 mbedtls_x509_crt *own_cert,
1446 mbedtls_pk_context *pk_key )
1447{
1448 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1449 own_cert, pk_key ) );
1450}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001451
1452void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1453 mbedtls_x509_crt *ca_chain,
1454 mbedtls_x509_crl *ca_crl )
1455{
1456 ssl->handshake->sni_ca_chain = ca_chain;
1457 ssl->handshake->sni_ca_crl = ca_crl;
1458}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001459
1460void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1461 int authmode )
1462{
1463 ssl->handshake->sni_authmode = authmode;
1464}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001465#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1466
Hanno Becker8927c832019-04-03 12:52:50 +01001467#if defined(MBEDTLS_X509_CRT_PARSE_C)
1468void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1469 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1470 void *p_vrfy )
1471{
1472 ssl->f_vrfy = f_vrfy;
1473 ssl->p_vrfy = p_vrfy;
1474}
1475#endif
1476
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001477#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001478/*
1479 * Set EC J-PAKE password for current handshake
1480 */
1481int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1482 const unsigned char *pw,
1483 size_t pw_len )
1484{
1485 mbedtls_ecjpake_role role;
1486
Janos Follath8eb64132016-06-03 15:40:57 +01001487 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001488 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1489
1490 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1491 role = MBEDTLS_ECJPAKE_SERVER;
1492 else
1493 role = MBEDTLS_ECJPAKE_CLIENT;
1494
1495 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1496 role,
1497 MBEDTLS_MD_SHA256,
1498 MBEDTLS_ECP_DP_SECP256R1,
1499 pw, pw_len ) );
1500}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001501#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001502
Gilles Peskineeccd8882020-03-10 12:19:08 +01001503#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001504
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001505static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1506{
1507#if defined(MBEDTLS_USE_PSA_CRYPTO)
1508 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1509 return( 1 );
1510#endif /* MBEDTLS_USE_PSA_CRYPTO */
1511
1512 if( conf->psk != NULL )
1513 return( 1 );
1514
1515 return( 0 );
1516}
1517
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001518static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1519{
1520 /* Remove reference to existing PSK, if any. */
1521#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001522 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001523 {
1524 /* The maintenance of the PSK key slot is the
1525 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001526 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001527 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00001528 /* This and the following branch should never
1529 * be taken simultaenously as we maintain the
1530 * invariant that raw and opaque PSKs are never
1531 * configured simultaneously. As a safeguard,
1532 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001533#endif /* MBEDTLS_USE_PSA_CRYPTO */
1534 if( conf->psk != NULL )
1535 {
1536 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1537
1538 mbedtls_free( conf->psk );
1539 conf->psk = NULL;
1540 conf->psk_len = 0;
1541 }
1542
1543 /* Remove reference to PSK identity, if any. */
1544 if( conf->psk_identity != NULL )
1545 {
1546 mbedtls_free( conf->psk_identity );
1547 conf->psk_identity = NULL;
1548 conf->psk_identity_len = 0;
1549 }
1550}
1551
Hanno Becker7390c712018-11-15 13:33:04 +00001552/* This function assumes that PSK identity in the SSL config is unset.
1553 * It checks that the provided identity is well-formed and attempts
1554 * to make a copy of it in the SSL config.
1555 * On failure, the PSK identity in the config remains unset. */
1556static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1557 unsigned char const *psk_identity,
1558 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001559{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001560 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001561 if( psk_identity == NULL ||
1562 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001563 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001564 {
1565 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1566 }
1567
Hanno Becker7390c712018-11-15 13:33:04 +00001568 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1569 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001570 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001571
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001572 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001573 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001574
1575 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001576}
1577
Hanno Becker7390c712018-11-15 13:33:04 +00001578int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1579 const unsigned char *psk, size_t psk_len,
1580 const unsigned char *psk_identity, size_t psk_identity_len )
1581{
Janos Follath865b3eb2019-12-16 11:46:15 +00001582 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001583
1584 /* We currently only support one PSK, raw or opaque. */
1585 if( ssl_conf_psk_is_configured( conf ) )
1586 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001587
1588 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001589 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001590 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001591 if( psk_len == 0 )
1592 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1593 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1594 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1595
Hanno Becker7390c712018-11-15 13:33:04 +00001596 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1597 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1598 conf->psk_len = psk_len;
1599 memcpy( conf->psk, psk, conf->psk_len );
1600
1601 /* Check and set PSK Identity */
1602 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1603 if( ret != 0 )
1604 ssl_conf_remove_psk( conf );
1605
1606 return( ret );
1607}
1608
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001609static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1610{
1611#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001612 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001613 {
Ronald Croncf56a0a2020-08-04 09:51:30 +02001614 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001615 }
1616 else
1617#endif /* MBEDTLS_USE_PSA_CRYPTO */
1618 if( ssl->handshake->psk != NULL )
1619 {
1620 mbedtls_platform_zeroize( ssl->handshake->psk,
1621 ssl->handshake->psk_len );
1622 mbedtls_free( ssl->handshake->psk );
1623 ssl->handshake->psk_len = 0;
1624 }
1625}
1626
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001627int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1628 const unsigned char *psk, size_t psk_len )
1629{
1630 if( psk == NULL || ssl->handshake == NULL )
1631 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1632
1633 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1634 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1635
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001636 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001637
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001638 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001639 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001640
1641 ssl->handshake->psk_len = psk_len;
1642 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1643
1644 return( 0 );
1645}
1646
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001647#if defined(MBEDTLS_USE_PSA_CRYPTO)
1648int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001649 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001650 const unsigned char *psk_identity,
1651 size_t psk_identity_len )
1652{
Janos Follath865b3eb2019-12-16 11:46:15 +00001653 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001654
1655 /* We currently only support one PSK, raw or opaque. */
1656 if( ssl_conf_psk_is_configured( conf ) )
1657 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001658
Hanno Becker7390c712018-11-15 13:33:04 +00001659 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001660 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001661 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001662 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001663
1664 /* Check and set PSK Identity */
1665 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1666 psk_identity_len );
1667 if( ret != 0 )
1668 ssl_conf_remove_psk( conf );
1669
1670 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001671}
1672
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001673int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1674 mbedtls_svc_key_id_t psk )
1675{
1676 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1677 ( ssl->handshake == NULL ) )
1678 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1679
1680 ssl_remove_psk( ssl );
1681 ssl->handshake->psk_opaque = psk;
1682 return( 0 );
1683}
1684#endif /* MBEDTLS_USE_PSA_CRYPTO */
1685
1686void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1687 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1688 size_t),
1689 void *p_psk )
1690{
1691 conf->f_psk = f_psk;
1692 conf->p_psk = p_psk;
1693}
1694#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1695
1696#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01001697psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01001698 size_t taglen,
1699 psa_algorithm_t *alg,
1700 psa_key_type_t *key_type,
1701 size_t *key_size )
1702{
1703 switch ( mbedtls_cipher_type )
1704 {
1705 case MBEDTLS_CIPHER_AES_128_CBC:
1706 *alg = PSA_ALG_CBC_NO_PADDING;
1707 *key_type = PSA_KEY_TYPE_AES;
1708 *key_size = 128;
1709 break;
1710 case MBEDTLS_CIPHER_AES_128_CCM:
1711 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1712 *key_type = PSA_KEY_TYPE_AES;
1713 *key_size = 128;
1714 break;
1715 case MBEDTLS_CIPHER_AES_128_GCM:
1716 *alg = PSA_ALG_GCM;
1717 *key_type = PSA_KEY_TYPE_AES;
1718 *key_size = 128;
1719 break;
1720 case MBEDTLS_CIPHER_AES_192_CCM:
1721 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1722 *key_type = PSA_KEY_TYPE_AES;
1723 *key_size = 192;
1724 break;
1725 case MBEDTLS_CIPHER_AES_192_GCM:
1726 *alg = PSA_ALG_GCM;
1727 *key_type = PSA_KEY_TYPE_AES;
1728 *key_size = 192;
1729 break;
1730 case MBEDTLS_CIPHER_AES_256_CBC:
1731 *alg = PSA_ALG_CBC_NO_PADDING;
1732 *key_type = PSA_KEY_TYPE_AES;
1733 *key_size = 256;
1734 break;
1735 case MBEDTLS_CIPHER_AES_256_CCM:
1736 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1737 *key_type = PSA_KEY_TYPE_AES;
1738 *key_size = 256;
1739 break;
1740 case MBEDTLS_CIPHER_AES_256_GCM:
1741 *alg = PSA_ALG_GCM;
1742 *key_type = PSA_KEY_TYPE_AES;
1743 *key_size = 256;
1744 break;
1745 case MBEDTLS_CIPHER_ARIA_128_CBC:
1746 *alg = PSA_ALG_CBC_NO_PADDING;
1747 *key_type = PSA_KEY_TYPE_ARIA;
1748 *key_size = 128;
1749 break;
1750 case MBEDTLS_CIPHER_ARIA_128_CCM:
1751 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1752 *key_type = PSA_KEY_TYPE_ARIA;
1753 *key_size = 128;
1754 break;
1755 case MBEDTLS_CIPHER_ARIA_128_GCM:
1756 *alg = PSA_ALG_GCM;
1757 *key_type = PSA_KEY_TYPE_ARIA;
1758 *key_size = 128;
1759 break;
1760 case MBEDTLS_CIPHER_ARIA_192_CCM:
1761 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1762 *key_type = PSA_KEY_TYPE_ARIA;
1763 *key_size = 192;
1764 break;
1765 case MBEDTLS_CIPHER_ARIA_192_GCM:
1766 *alg = PSA_ALG_GCM;
1767 *key_type = PSA_KEY_TYPE_ARIA;
1768 *key_size = 192;
1769 break;
1770 case MBEDTLS_CIPHER_ARIA_256_CBC:
1771 *alg = PSA_ALG_CBC_NO_PADDING;
1772 *key_type = PSA_KEY_TYPE_ARIA;
1773 *key_size = 256;
1774 break;
1775 case MBEDTLS_CIPHER_ARIA_256_CCM:
1776 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1777 *key_type = PSA_KEY_TYPE_ARIA;
1778 *key_size = 256;
1779 break;
1780 case MBEDTLS_CIPHER_ARIA_256_GCM:
1781 *alg = PSA_ALG_GCM;
1782 *key_type = PSA_KEY_TYPE_ARIA;
1783 *key_size = 256;
1784 break;
1785 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
1786 *alg = PSA_ALG_CBC_NO_PADDING;
1787 *key_type = PSA_KEY_TYPE_CAMELLIA;
1788 *key_size = 128;
1789 break;
1790 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
1791 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1792 *key_type = PSA_KEY_TYPE_CAMELLIA;
1793 *key_size = 128;
1794 break;
1795 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
1796 *alg = PSA_ALG_GCM;
1797 *key_type = PSA_KEY_TYPE_CAMELLIA;
1798 *key_size = 128;
1799 break;
1800 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
1801 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1802 *key_type = PSA_KEY_TYPE_CAMELLIA;
1803 *key_size = 192;
1804 break;
1805 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
1806 *alg = PSA_ALG_GCM;
1807 *key_type = PSA_KEY_TYPE_CAMELLIA;
1808 *key_size = 192;
1809 break;
1810 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
1811 *alg = PSA_ALG_CBC_NO_PADDING;
1812 *key_type = PSA_KEY_TYPE_CAMELLIA;
1813 *key_size = 256;
1814 break;
1815 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
1816 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1817 *key_type = PSA_KEY_TYPE_CAMELLIA;
1818 *key_size = 256;
1819 break;
1820 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
1821 *alg = PSA_ALG_GCM;
1822 *key_type = PSA_KEY_TYPE_CAMELLIA;
1823 *key_size = 256;
1824 break;
1825 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
1826 *alg = PSA_ALG_CHACHA20_POLY1305;
1827 *key_type = PSA_KEY_TYPE_CHACHA20;
1828 *key_size = 256;
1829 break;
1830 case MBEDTLS_CIPHER_NULL:
1831 *alg = MBEDTLS_SSL_NULL_CIPHER;
1832 *key_type = 0;
1833 *key_size = 0;
1834 break;
1835 default:
1836 return PSA_ERROR_NOT_SUPPORTED;
1837 }
1838
1839 return PSA_SUCCESS;
1840}
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001841#endif /* MBEDTLS_USE_PSA_CRYPTO */
1842
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02001843#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01001844int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
1845 const unsigned char *dhm_P, size_t P_len,
1846 const unsigned char *dhm_G, size_t G_len )
1847{
Janos Follath865b3eb2019-12-16 11:46:15 +00001848 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01001849
Glenn Strausscee11292021-12-20 01:43:17 -05001850 mbedtls_mpi_free( &conf->dhm_P );
1851 mbedtls_mpi_free( &conf->dhm_G );
1852
Hanno Beckera90658f2017-10-04 15:29:08 +01001853 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
1854 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
1855 {
1856 mbedtls_mpi_free( &conf->dhm_P );
1857 mbedtls_mpi_free( &conf->dhm_G );
1858 return( ret );
1859 }
1860
1861 return( 0 );
1862}
Paul Bakker5121ce52009-01-03 21:22:43 +00001863
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001864int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00001865{
Janos Follath865b3eb2019-12-16 11:46:15 +00001866 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00001867
Glenn Strausscee11292021-12-20 01:43:17 -05001868 mbedtls_mpi_free( &conf->dhm_P );
1869 mbedtls_mpi_free( &conf->dhm_G );
1870
Gilles Peskinee5702482021-06-11 21:59:08 +02001871 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
1872 &conf->dhm_P ) ) != 0 ||
1873 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
1874 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01001875 {
1876 mbedtls_mpi_free( &conf->dhm_P );
1877 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00001878 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01001879 }
Paul Bakker1b57b062011-01-06 15:48:19 +00001880
1881 return( 0 );
1882}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02001883#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00001884
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02001885#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
1886/*
1887 * Set the minimum length for Diffie-Hellman parameters
1888 */
1889void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
1890 unsigned int bitlen )
1891{
1892 conf->dhm_min_bitlen = bitlen;
1893}
1894#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
1895
Gilles Peskineeccd8882020-03-10 12:19:08 +01001896#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08001897#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02001898/*
1899 * Set allowed/preferred hashes for handshake signatures
1900 */
1901void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
1902 const int *hashes )
1903{
1904 conf->sig_hashes = hashes;
1905}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08001906#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01001907
Jerry Yuf017ee42022-01-12 15:49:48 +08001908/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01001909void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
1910 const uint16_t* sig_algs )
1911{
Jerry Yuf017ee42022-01-12 15:49:48 +08001912#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1913 conf->sig_hashes = NULL;
1914#endif /* !MBEDTLS_DEPRECATED_REMOVED */
1915 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01001916}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001917#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02001918
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02001919#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01001920#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001921/*
1922 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01001923 *
1924 * mbedtls_ssl_setup() takes the provided list
1925 * and translates it to a list of IANA TLS group identifiers,
1926 * stored in ssl->handshake->group_list.
1927 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001928 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001929void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001930 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001931{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001932 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01001933 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001934}
Brett Warrene0edc842021-08-17 09:53:13 +01001935#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01001936#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001937
Brett Warrene0edc842021-08-17 09:53:13 +01001938/*
1939 * Set the allowed groups
1940 */
1941void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
1942 const uint16_t *group_list )
1943{
1944#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
1945 conf->curve_list = NULL;
1946#endif
1947 conf->group_list = group_list;
1948}
1949
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001950#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00001952{
Hanno Becker947194e2017-04-07 13:25:49 +01001953 /* Initialize to suppress unnecessary compiler warning */
1954 size_t hostname_len = 0;
1955
1956 /* Check if new hostname is valid before
1957 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01001958 if( hostname != NULL )
1959 {
1960 hostname_len = strlen( hostname );
1961
1962 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
1963 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1964 }
1965
1966 /* Now it's clear that we will overwrite the old hostname,
1967 * so we can free it safely */
1968
1969 if( ssl->hostname != NULL )
1970 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001971 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01001972 mbedtls_free( ssl->hostname );
1973 }
1974
1975 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01001976
Paul Bakker5121ce52009-01-03 21:22:43 +00001977 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01001978 {
1979 ssl->hostname = NULL;
1980 }
1981 else
1982 {
1983 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01001984 if( ssl->hostname == NULL )
1985 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02001986
Hanno Becker947194e2017-04-07 13:25:49 +01001987 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02001988
Hanno Becker947194e2017-04-07 13:25:49 +01001989 ssl->hostname[hostname_len] = '\0';
1990 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001991
1992 return( 0 );
1993}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01001994#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001995
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001996#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001997void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00001999 const unsigned char *, size_t),
2000 void *p_sni )
2001{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002002 conf->f_sni = f_sni;
2003 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002004}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002008int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002009{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002010 size_t cur_len, tot_len;
2011 const char **p;
2012
2013 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002014 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2015 * MUST NOT be truncated."
2016 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002017 */
2018 tot_len = 0;
2019 for( p = protos; *p != NULL; p++ )
2020 {
2021 cur_len = strlen( *p );
2022 tot_len += cur_len;
2023
Ronald Cron8216dd32020-04-23 16:41:44 +02002024 if( ( cur_len == 0 ) ||
2025 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2026 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002028 }
2029
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002030 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002031
2032 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002033}
2034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002036{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002037 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002038}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002040
Johan Pascalb62bb512015-12-03 21:56:45 +01002041#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002042void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2043 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002044{
2045 conf->dtls_srtp_mki_support = support_mki_value;
2046}
2047
Ron Eldoref72faf2018-07-12 11:54:20 +03002048int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2049 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002050 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002051{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002052 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002053 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002054 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002055 }
Ron Eldor591f1622018-01-22 12:30:04 +02002056
2057 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002058 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002059 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002060 }
Ron Eldor591f1622018-01-22 12:30:04 +02002061
2062 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2063 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002064 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002065}
2066
Ron Eldoref72faf2018-07-12 11:54:20 +03002067int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002068 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002069{
Johan Pascal253d0262020-09-22 13:04:45 +02002070 const mbedtls_ssl_srtp_profile *p;
2071 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002072
Johan Pascal253d0262020-09-22 13:04:45 +02002073 /* check the profiles list: all entry must be valid,
2074 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002075 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2076 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2077 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002078 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002079 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002080 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002081 list_size++;
2082 }
2083 else
2084 {
2085 /* unsupported value, stop parsing and set the size to an error value */
2086 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002087 }
2088 }
2089
Johan Pascal5ef72d22020-10-28 17:05:47 +01002090 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002091 {
Johan Pascal253d0262020-09-22 13:04:45 +02002092 conf->dtls_srtp_profile_list = NULL;
2093 conf->dtls_srtp_profile_list_len = 0;
2094 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2095 }
2096
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002097 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002098 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002099
2100 return( 0 );
2101}
2102
Johan Pascal5ef72d22020-10-28 17:05:47 +01002103void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2104 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002105{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002106 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2107 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002108 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002109 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002110 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002111 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002112 else
2113 {
2114 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002115 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2116 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002117 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002118}
Johan Pascalb62bb512015-12-03 21:56:45 +01002119#endif /* MBEDTLS_SSL_DTLS_SRTP */
2120
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002121void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002122{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002123 conf->max_major_ver = major;
2124 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002125}
2126
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002127void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002128{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002129 conf->min_major_ver = major;
2130 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002131}
2132
Janos Follath088ce432017-04-10 12:42:31 +01002133#if defined(MBEDTLS_SSL_SRV_C)
2134void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2135 char cert_req_ca_list )
2136{
2137 conf->cert_req_ca_list = cert_req_ca_list;
2138}
2139#endif
2140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002142void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002143{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002144 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002145}
2146#endif
2147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002149void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002150{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002151 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002152}
2153#endif
2154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002156int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002157{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002159 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002162 }
2163
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002164 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002165
2166 return( 0 );
2167}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002169
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002170void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002171{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002172 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002173}
2174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002176void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002177{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002178 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002179}
2180
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002181void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002182{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002183 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002184}
2185
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002186void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002187 const unsigned char period[8] )
2188{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002189 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002190}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002194#if defined(MBEDTLS_SSL_CLI_C)
2195void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002196{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002197 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002198}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002199#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002200
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002201#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002202void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2203 mbedtls_ssl_ticket_write_t *f_ticket_write,
2204 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2205 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002206{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002207 conf->f_ticket_write = f_ticket_write;
2208 conf->f_ticket_parse = f_ticket_parse;
2209 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002210}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002211#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002213
Hanno Becker7e6c1782021-06-08 09:24:55 +01002214void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2215 mbedtls_ssl_export_keys_t *f_export_keys,
2216 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002217{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002218 ssl->f_export_keys = f_export_keys;
2219 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002220}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002221
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002222#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002223void mbedtls_ssl_conf_async_private_cb(
2224 mbedtls_ssl_config *conf,
2225 mbedtls_ssl_async_sign_t *f_async_sign,
2226 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2227 mbedtls_ssl_async_resume_t *f_async_resume,
2228 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002229 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002230{
2231 conf->f_async_sign_start = f_async_sign;
2232 conf->f_async_decrypt_start = f_async_decrypt;
2233 conf->f_async_resume = f_async_resume;
2234 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002235 conf->p_async_config_data = async_config_data;
2236}
2237
Gilles Peskine8f97af72018-04-26 11:46:10 +02002238void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2239{
2240 return( conf->p_async_config_data );
2241}
2242
Gilles Peskine1febfef2018-04-30 11:54:39 +02002243void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002244{
2245 if( ssl->handshake == NULL )
2246 return( NULL );
2247 else
2248 return( ssl->handshake->user_async_ctx );
2249}
2250
Gilles Peskine1febfef2018-04-30 11:54:39 +02002251void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002252 void *ctx )
2253{
2254 if( ssl->handshake != NULL )
2255 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002256}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002257#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002258
Paul Bakker5121ce52009-01-03 21:22:43 +00002259/*
2260 * SSL get accessors
2261 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002262uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002263{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002264 if( ssl->session != NULL )
2265 return( ssl->session->verify_result );
2266
2267 if( ssl->session_negotiate != NULL )
2268 return( ssl->session_negotiate->verify_result );
2269
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002270 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002271}
2272
Glenn Strauss8f526902022-01-13 00:04:49 -05002273int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2274{
2275 if( ssl == NULL || ssl->session == NULL )
2276 return( 0 );
2277
2278 return( ssl->session->ciphersuite );
2279}
2280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002282{
Paul Bakker926c8e42013-03-06 10:23:34 +01002283 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002284 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002287}
2288
Gilles Peskinee1a0c252022-01-13 01:08:03 +01002289mbedtls_ssl_protocol_version mbedtls_ssl_get_version_number(
2290 const mbedtls_ssl_context *ssl )
2291{
2292 /* For major_ver, only 3 is supported, so skip checking it. */
2293 switch( ssl->minor_ver )
2294 {
2295 case MBEDTLS_SSL_MINOR_VERSION_3:
2296 return( MBEDTLS_SSL_VERSION_1_2 );
2297 case MBEDTLS_SSL_MINOR_VERSION_4:
2298 return( MBEDTLS_SSL_VERSION_1_3 );
2299 default:
2300 return( MBEDTLS_SSL_VERSION_UNKNOWN );
2301 }
2302}
2303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002305{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002307 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002308 {
2309 switch( ssl->minor_ver )
2310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002312 return( "DTLSv1.2" );
2313
2314 default:
2315 return( "unknown (DTLS)" );
2316 }
2317 }
2318#endif
2319
Paul Bakker43ca69c2011-01-15 17:35:19 +00002320 switch( ssl->minor_ver )
2321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002323 return( "TLSv1.2" );
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002324 case MBEDTLS_SSL_MINOR_VERSION_4:
2325 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002326 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002327 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002328 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002329}
2330
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002331#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002332size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2333{
David Horstmann95d516f2021-05-04 18:36:56 +01002334 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002335 size_t read_mfl;
2336
2337 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2338 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2339 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2340 {
2341 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2342 }
2343
2344 /* Check if a smaller max length was negotiated */
2345 if( ssl->session_out != NULL )
2346 {
2347 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2348 if( read_mfl < max_len )
2349 {
2350 max_len = read_mfl;
2351 }
2352 }
2353
2354 // During a handshake, use the value being negotiated
2355 if( ssl->session_negotiate != NULL )
2356 {
2357 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2358 if( read_mfl < max_len )
2359 {
2360 max_len = read_mfl;
2361 }
2362 }
2363
2364 return( max_len );
2365}
2366
2367size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002368{
2369 size_t max_len;
2370
2371 /*
2372 * Assume mfl_code is correct since it was checked when set
2373 */
Angus Grattond8213d02016-05-25 20:56:48 +10002374 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002375
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002376 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002377 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002378 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002379 {
Angus Grattond8213d02016-05-25 20:56:48 +10002380 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002381 }
2382
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002383 /* During a handshake, use the value being negotiated */
2384 if( ssl->session_negotiate != NULL &&
2385 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2386 {
2387 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2388 }
2389
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002390 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002391}
2392#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2393
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002394#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002395size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002396{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002397 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2398 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2399 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2400 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2401 return ( 0 );
2402
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002403 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2404 return( ssl->mtu );
2405
2406 if( ssl->mtu == 0 )
2407 return( ssl->handshake->mtu );
2408
2409 return( ssl->mtu < ssl->handshake->mtu ?
2410 ssl->mtu : ssl->handshake->mtu );
2411}
2412#endif /* MBEDTLS_SSL_PROTO_DTLS */
2413
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002414int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2415{
2416 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2417
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002418#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2419 !defined(MBEDTLS_SSL_PROTO_DTLS)
2420 (void) ssl;
2421#endif
2422
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002423#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002424 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002425
2426 if( max_len > mfl )
2427 max_len = mfl;
2428#endif
2429
2430#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002431 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002432 {
Hanno Becker89490712020-02-05 10:50:12 +00002433 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002434 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2435 const size_t overhead = (size_t) ret;
2436
2437 if( ret < 0 )
2438 return( ret );
2439
2440 if( mtu <= overhead )
2441 {
2442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2443 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2444 }
2445
2446 if( max_len > mtu - overhead )
2447 max_len = mtu - overhead;
2448 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002449#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002450
Hanno Becker0defedb2018-08-10 12:35:02 +01002451#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2452 !defined(MBEDTLS_SSL_PROTO_DTLS)
2453 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002454#endif
2455
2456 return( (int) max_len );
2457}
2458
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002459int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2460{
2461 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2462
2463#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2464 (void) ssl;
2465#endif
2466
2467#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2468 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2469
2470 if( max_len > mfl )
2471 max_len = mfl;
2472#endif
2473
2474 return( (int) max_len );
2475}
2476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477#if defined(MBEDTLS_X509_CRT_PARSE_C)
2478const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002479{
2480 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002481 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002482
Hanno Beckere6824572019-02-07 13:18:46 +00002483#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002484 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002485#else
2486 return( NULL );
2487#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002488}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002492int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2493 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002494{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002495 int ret;
2496
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002497 if( ssl == NULL ||
2498 dst == NULL ||
2499 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002500 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002503 }
2504
Hanno Beckere810bbc2021-05-14 16:01:05 +01002505 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2506 * idempotent: Each session can only be exported once.
2507 *
2508 * (This is in preparation for TLS 1.3 support where we will
2509 * need the ability to export multiple sessions (aka tickets),
2510 * which will be achieved by calling mbedtls_ssl_get_session()
2511 * multiple times until it fails.)
2512 *
2513 * Check whether we have already exported the current session,
2514 * and fail if so.
2515 */
2516 if( ssl->session->exported == 1 )
2517 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2518
2519 ret = mbedtls_ssl_session_copy( dst, ssl->session );
2520 if( ret != 0 )
2521 return( ret );
2522
2523 /* Remember that we've exported the session. */
2524 ssl->session->exported = 1;
2525 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002526}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002528
Paul Bakker5121ce52009-01-03 21:22:43 +00002529/*
Hanno Beckera835da52019-05-16 12:39:07 +01002530 * Define ticket header determining Mbed TLS version
2531 * and structure of the ticket.
2532 */
2533
Hanno Becker94ef3b32019-05-16 12:50:45 +01002534/*
Hanno Becker50b59662019-05-28 14:30:45 +01002535 * Define bitflag determining compile-time settings influencing
2536 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01002537 */
2538
Hanno Becker50b59662019-05-28 14:30:45 +01002539#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01002540#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01002541#else
Hanno Becker3e088662019-05-29 11:10:18 +01002542#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002543#endif /* MBEDTLS_HAVE_TIME */
2544
2545#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01002546#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002547#else
Hanno Becker3e088662019-05-29 11:10:18 +01002548#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002549#endif /* MBEDTLS_X509_CRT_PARSE_C */
2550
2551#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01002552#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002553#else
Hanno Becker3e088662019-05-29 11:10:18 +01002554#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002555#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
2556
2557#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01002558#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002559#else
Hanno Becker3e088662019-05-29 11:10:18 +01002560#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002561#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2562
Hanno Becker94ef3b32019-05-16 12:50:45 +01002563#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01002564#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002565#else
Hanno Becker3e088662019-05-29 11:10:18 +01002566#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002567#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2568
Hanno Becker94ef3b32019-05-16 12:50:45 +01002569#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2570#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
2571#else
2572#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
2573#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2574
Hanno Becker3e088662019-05-29 11:10:18 +01002575#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
2576#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
2577#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
2578#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01002579#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
2580#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01002581
Hanno Becker50b59662019-05-28 14:30:45 +01002582#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01002583 ( (uint16_t) ( \
2584 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
2585 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
2586 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
2587 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01002588 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01002589 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01002590
Hanno Beckerf8787072019-05-16 12:41:07 +01002591static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01002592 MBEDTLS_VERSION_MAJOR,
2593 MBEDTLS_VERSION_MINOR,
2594 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01002595 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
2596 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01002597};
Hanno Beckera835da52019-05-16 12:39:07 +01002598
2599/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002600 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002601 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002602 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002603 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002604 *
Hanno Beckerdce50972021-08-01 05:39:23 +01002605 * opaque mbedtls_version[3]; // library version: major, minor, patch
2606 * opaque session_format[2]; // library-version specific 16-bit field
2607 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002608 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002609 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002610 * Note: When updating the format, remember to keep
2611 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002612 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002613 * // In this version, `session_format` determines
2614 * // the setting of those compile-time
2615 * // configuration options which influence
2616 * // the structure of mbedtls_ssl_session.
2617 *
Hanno Beckerfa0d61e2021-08-02 08:56:14 +01002618 * uint8_t minor_ver; // Protocol-version. Possible values:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002619 * // - TLS 1.2 (MBEDTLS_SSL_MINOR_VERSION_3)
2620 *
2621 * select (serialized_session.minor_ver) {
2622 *
2623 * case MBEDTLS_SSL_MINOR_VERSION_3: // TLS 1.2
2624 * serialized_session_tls12 data;
2625 *
2626 * };
2627 *
2628 * } serialized_session;
2629 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002630 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002631
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002632static int ssl_session_save( const mbedtls_ssl_session *session,
2633 unsigned char omit_header,
2634 unsigned char *buf,
2635 size_t buf_len,
2636 size_t *olen )
2637{
2638 unsigned char *p = buf;
2639 size_t used = 0;
2640
2641 if( !omit_header )
2642 {
2643 /*
2644 * Add Mbed TLS version identifier
2645 */
2646
2647 used += sizeof( ssl_serialized_session_header );
2648
2649 if( used <= buf_len )
2650 {
2651 memcpy( p, ssl_serialized_session_header,
2652 sizeof( ssl_serialized_session_header ) );
2653 p += sizeof( ssl_serialized_session_header );
2654 }
2655 }
2656
2657 /*
2658 * TLS version identifier
2659 */
2660 used += 1;
2661 if( used <= buf_len )
2662 {
2663 *p++ = session->minor_ver;
2664 }
2665
2666 /* Forward to version-specific serialization routine. */
2667 switch( session->minor_ver )
2668 {
2669#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2670 case MBEDTLS_SSL_MINOR_VERSION_3:
2671 {
2672 size_t remaining_len = used <= buf_len ? buf_len - used : 0;
2673 used += ssl_session_save_tls12( session, p, remaining_len );
2674 break;
2675 }
2676#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2677
2678 default:
2679 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2680 }
2681
2682 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02002683 if( used > buf_len )
2684 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002685
2686 return( 0 );
2687}
2688
2689/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02002690 * Public wrapper for ssl_session_save()
2691 */
2692int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
2693 unsigned char *buf,
2694 size_t buf_len,
2695 size_t *olen )
2696{
2697 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
2698}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002699
Jerry Yuf1b23ca2022-02-18 11:48:47 +08002700/*
2701 * Deserialize session, see mbedtls_ssl_session_save() for format.
2702 *
2703 * This internal version is wrapped by a public function that cleans up in
2704 * case of error, and has an extra option omit_header.
2705 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002706static int ssl_session_load( mbedtls_ssl_session *session,
2707 unsigned char omit_header,
2708 const unsigned char *buf,
2709 size_t len )
2710{
2711 const unsigned char *p = buf;
2712 const unsigned char * const end = buf + len;
2713
2714 if( !omit_header )
2715 {
2716 /*
2717 * Check Mbed TLS version identifier
2718 */
2719
2720 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
2721 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2722
2723 if( memcmp( p, ssl_serialized_session_header,
2724 sizeof( ssl_serialized_session_header ) ) != 0 )
2725 {
2726 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
2727 }
2728 p += sizeof( ssl_serialized_session_header );
2729 }
2730
2731 /*
2732 * TLS version identifier
2733 */
2734 if( 1 > (size_t)( end - p ) )
2735 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2736 session->minor_ver = *p++;
2737
2738 /* Dispatch according to TLS version. */
2739 switch( session->minor_ver )
2740 {
2741#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2742 case MBEDTLS_SSL_MINOR_VERSION_3: /* TLS 1.2 */
2743 {
2744 size_t remaining_len = ( end - p );
2745 return( ssl_session_load_tls12( session, p, remaining_len ) );
2746 }
2747#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2748
2749 default:
2750 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2751 }
2752}
2753
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002754/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02002755 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02002756 */
2757int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
2758 const unsigned char *buf,
2759 size_t len )
2760{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02002761 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02002762
2763 if( ret != 0 )
2764 mbedtls_ssl_session_free( session );
2765
2766 return( ret );
2767}
2768
2769/*
Paul Bakker1961b702013-01-25 14:49:24 +01002770 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00002771 */
Hanno Becker41934dd2021-08-07 19:13:43 +01002772static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
2773{
2774 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2775
Ronald Cron66dbf912022-02-02 15:33:46 +01002776 /*
2777 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01002778 * that were written into the output buffer by the previous handshake step,
2779 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01002780 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
2781 * We proceed to the next handshake step only when all data from the
2782 * previous one have been sent to the peer, thus we make sure that this is
2783 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
2784 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
2785 * we have to wait before to go ahead.
2786 * In the case of TLS 1.3, handshake step handlers do not send data to the
2787 * peer. Data are only sent here and through
2788 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
2789 * alert occured.
2790 */
Hanno Becker41934dd2021-08-07 19:13:43 +01002791 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2792 return( ret );
2793
2794#if defined(MBEDTLS_SSL_PROTO_DTLS)
2795 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2796 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
2797 {
2798 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
2799 return( ret );
2800 }
2801#endif /* MBEDTLS_SSL_PROTO_DTLS */
2802
2803 return( ret );
2804}
2805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002807{
Hanno Becker41934dd2021-08-07 19:13:43 +01002808 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002809
Hanno Becker41934dd2021-08-07 19:13:43 +01002810 if( ssl == NULL ||
2811 ssl->conf == NULL ||
2812 ssl->handshake == NULL ||
2813 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2814 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002815 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01002816 }
2817
2818 ret = ssl_prepare_handshake_step( ssl );
2819 if( ret != 0 )
2820 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002821
Jerry Yue7047812021-09-13 19:26:39 +08002822 ret = mbedtls_ssl_handle_pending_alert( ssl );
2823 if( ret != 0 )
2824 goto cleanup;
2825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002827 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08002828 {
Ronald Cron27c85e72022-03-08 11:37:55 +01002829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
2830 mbedtls_ssl_states_str( ssl->state ) ) );
2831
Ronald Cron9f0fba32022-02-10 16:45:15 +01002832 switch( ssl->state )
2833 {
2834 case MBEDTLS_SSL_HELLO_REQUEST:
2835 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
2836 break;
Jerry Yub9930e72021-08-06 17:11:51 +08002837
Ronald Cron9f0fba32022-02-10 16:45:15 +01002838 case MBEDTLS_SSL_CLIENT_HELLO:
2839 ret = mbedtls_ssl_write_client_hello( ssl );
2840 break;
2841
2842 default:
2843#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
2844 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
2845 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
2846 else
2847 ret = mbedtls_ssl_handshake_client_step( ssl );
2848#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
2849 ret = mbedtls_ssl_handshake_client_step( ssl );
2850#else
2851 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
2852#endif
2853 }
Jerry Yub9930e72021-08-06 17:11:51 +08002854 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002855#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002857 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08002858 {
Ronald Cron6f135e12021-12-08 16:57:54 +01002859#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08002860 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08002861 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01002862#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08002863
2864#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2865 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
2866 ret = mbedtls_ssl_handshake_server_step( ssl );
2867#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2868 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002869#endif
2870
Jerry Yue7047812021-09-13 19:26:39 +08002871 if( ret != 0 )
2872 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08002873 /* handshake_step return error. And it is same
2874 * with alert_reason.
2875 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08002876 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08002877 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08002878 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08002879 goto cleanup;
2880 }
2881 }
2882
2883cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01002884 return( ret );
2885}
2886
2887/*
2888 * Perform the SSL handshake
2889 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01002891{
2892 int ret = 0;
2893
Hanno Beckera817ea42020-10-20 15:20:23 +01002894 /* Sanity checks */
2895
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002896 if( ssl == NULL || ssl->conf == NULL )
2897 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2898
Hanno Beckera817ea42020-10-20 15:20:23 +01002899#if defined(MBEDTLS_SSL_PROTO_DTLS)
2900 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2901 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
2902 {
2903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2904 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2905 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2906 }
2907#endif /* MBEDTLS_SSL_PROTO_DTLS */
2908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01002910
Hanno Beckera817ea42020-10-20 15:20:23 +01002911 /* Main handshake loop */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01002913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002915
2916 if( ret != 0 )
2917 break;
2918 }
2919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002921
2922 return( ret );
2923}
2924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925#if defined(MBEDTLS_SSL_RENEGOTIATION)
2926#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002927/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002928 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00002929 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002931{
Janos Follath865b3eb2019-12-16 11:46:15 +00002932 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002935
2936 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2938 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002939
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002940 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002941 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002942 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002943 return( ret );
2944 }
2945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002947
2948 return( 0 );
2949}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002951
2952/*
2953 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954 * - any side: calling mbedtls_ssl_renegotiate(),
2955 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
2956 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02002957 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002958 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002960 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00002961int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00002962{
Janos Follath865b3eb2019-12-16 11:46:15 +00002963 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00002964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002966
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002967 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
2968 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00002969
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02002970 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
2971 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002973 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02002975 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002976 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002977 ssl->handshake->out_msg_seq = 1;
2978 else
2979 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02002980 }
2981#endif
2982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
2984 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00002985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00002987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00002989 return( ret );
2990 }
2991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002993
2994 return( 0 );
2995}
2996
2997/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002998 * Renegotiate current connection on client,
2999 * or request renegotiation on server
3000 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003001int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003002{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003004
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003005 if( ssl == NULL || ssl->conf == NULL )
3006 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003009 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003010 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
3013 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003016
3017 /* Did we already try/start sending HelloRequest? */
3018 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003020
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003021 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003022 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003023#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003026 /*
3027 * On client, either start the renegotiation process or,
3028 * if already in progress, continue the handshake
3029 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
3033 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003034
Hanno Becker40cdaa12020-02-05 10:48:27 +00003035 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003036 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003037 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003038 return( ret );
3039 }
3040 }
3041 else
3042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003045 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003046 return( ret );
3047 }
3048 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003050
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003051 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003052}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003054
Gilles Peskine9b562d52018-04-25 20:32:43 +02003055void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003056{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003057 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3058
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003059 if( handshake == NULL )
3060 return;
3061
Brett Warrene0edc842021-08-17 09:53:13 +01003062#if defined(MBEDTLS_ECP_C)
3063#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3064 if ( ssl->handshake->group_list_heap_allocated )
3065 mbedtls_free( (void*) handshake->group_list );
3066 handshake->group_list = NULL;
3067#endif /* MBEDTLS_DEPRECATED_REMOVED */
3068#endif /* MBEDTLS_ECP_C */
3069
Jerry Yuf017ee42022-01-12 15:49:48 +08003070#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3071#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3072 if ( ssl->handshake->sig_algs_heap_allocated )
3073 mbedtls_free( (void*) handshake->sig_algs );
3074 handshake->sig_algs = NULL;
3075#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003076#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3077 if( ssl->handshake->certificate_request_context )
3078 {
3079 mbedtls_free( (void*) handshake->certificate_request_context );
3080 }
3081#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003082#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3083
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003084#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3085 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3086 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003087 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003088 handshake->async_in_progress = 0;
3089 }
3090#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3091
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003092#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003093#if defined(MBEDTLS_USE_PSA_CRYPTO)
3094 psa_hash_abort( &handshake->fin_sha256_psa );
3095#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003096 mbedtls_sha256_free( &handshake->fin_sha256 );
3097#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003098#endif
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +02003099#if defined(MBEDTLS_SHA384_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003100#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003101 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003102#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003103 mbedtls_sha512_free( &handshake->fin_sha512 );
3104#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003105#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107#if defined(MBEDTLS_DHM_C)
3108 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003109#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110#if defined(MBEDTLS_ECDH_C)
3111 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003112#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003113#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003114 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003115#if defined(MBEDTLS_SSL_CLI_C)
3116 mbedtls_free( handshake->ecjpake_cache );
3117 handshake->ecjpake_cache = NULL;
3118 handshake->ecjpake_cache_len = 0;
3119#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003120#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003121
Janos Follath4ae5c292016-02-10 11:27:43 +00003122#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3123 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003124 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003126#endif
3127
Gilles Peskineeccd8882020-03-10 12:19:08 +01003128#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003129 if( handshake->psk != NULL )
3130 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003131 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003132 mbedtls_free( handshake->psk );
3133 }
3134#endif
3135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3137 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003138 /*
3139 * Free only the linked list wrapper, not the keys themselves
3140 * since the belong to the SNI callback
3141 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003142 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003144
Gilles Peskineeccd8882020-03-10 12:19:08 +01003145#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003146 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003147 if( handshake->ecrs_peer_cert != NULL )
3148 {
3149 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3150 mbedtls_free( handshake->ecrs_peer_cert );
3151 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003152#endif
3153
Hanno Becker75173122019-02-06 16:18:31 +00003154#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3155 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3156 mbedtls_pk_free( &handshake->peer_pubkey );
3157#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3158
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003159#if defined(MBEDTLS_SSL_CLI_C) && \
3160 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3161 mbedtls_free( handshake->cookie );
3162#endif /* MBEDTLS_SSL_CLI_C &&
3163 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003164
3165#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003166 mbedtls_ssl_flight_free( handshake->flight );
3167 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003168#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003169
Ronald Cronf12b81d2022-03-15 10:42:41 +01003170#if defined(MBEDTLS_ECDH_C) && \
3171 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Hanno Becker4a63ed42019-01-08 11:39:35 +00003172 psa_destroy_key( handshake->ecdh_psa_privkey );
3173#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3174
Ronald Cron6f135e12021-12-08 16:57:54 +01003175#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003176 mbedtls_ssl_transform_free( handshake->transform_handshake );
3177 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003178 mbedtls_free( handshake->transform_earlydata );
3179 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003180#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003181
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003182
3183#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3184 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3185 * processes datagrams and the fact that a datagram is allowed to have
3186 * several records in it, it is possible that the I/O buffers are not
3187 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003188 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3189 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003190#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003191
Jerry Yuba9c7272021-10-30 11:54:10 +08003192 /* mbedtls_platform_zeroize MUST be last one in this function */
3193 mbedtls_platform_zeroize( handshake,
3194 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003195}
3196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003198{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003199 if( session == NULL )
3200 return;
3201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003203 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003204#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003205
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003206#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003208#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003209
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003210 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003211}
3212
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003213#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003214
3215#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3216#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3217#else
3218#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3219#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3220
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003221#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003222
3223#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3224#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3225#else
3226#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3227#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3228
3229#if defined(MBEDTLS_SSL_ALPN)
3230#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3231#else
3232#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3233#endif /* MBEDTLS_SSL_ALPN */
3234
3235#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3236#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3237#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3238#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3239
3240#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3241 ( (uint32_t) ( \
3242 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3243 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3244 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3245 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3246 0u ) )
3247
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003248static unsigned char ssl_serialized_context_header[] = {
3249 MBEDTLS_VERSION_MAJOR,
3250 MBEDTLS_VERSION_MINOR,
3251 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003252 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3253 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3254 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3255 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3256 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003257};
3258
Paul Bakker5121ce52009-01-03 21:22:43 +00003259/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003260 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003261 *
3262 * The format of the serialized data is:
3263 * (in the presentation language of TLS, RFC 8446 section 3)
3264 *
3265 * // header
3266 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003267 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003268 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003269 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003270 * Note: When updating the format, remember to keep these
3271 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003272 *
3273 * // session sub-structure
3274 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3275 * // transform sub-structure
3276 * uint8 random[64]; // ServerHello.random+ClientHello.random
3277 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3278 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3279 * // fields from ssl_context
3280 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3281 * uint64 in_window_top; // DTLS: last validated record seq_num
3282 * uint64 in_window; // DTLS: bitmask for replay protection
3283 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3284 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3285 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3286 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3287 *
3288 * Note that many fields of the ssl_context or sub-structures are not
3289 * serialized, as they fall in one of the following categories:
3290 *
3291 * 1. forced value (eg in_left must be 0)
3292 * 2. pointer to dynamically-allocated memory (eg session, transform)
3293 * 3. value can be re-derived from other data (eg session keys from MS)
3294 * 4. value was temporary (eg content of input buffer)
3295 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003296 */
3297int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3298 unsigned char *buf,
3299 size_t buf_len,
3300 size_t *olen )
3301{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003302 unsigned char *p = buf;
3303 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003304 size_t session_len;
3305 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003306
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003307 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003308 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3309 * this function's documentation.
3310 *
3311 * These are due to assumptions/limitations in the implementation. Some of
3312 * them are likely to stay (no handshake in progress) some might go away
3313 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003314 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003315 /* The initial handshake must be over */
3316 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003317 {
3318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003319 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003320 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003321 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003322 {
3323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003324 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003325 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003326 /* Double-check that sub-structures are indeed ready */
3327 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003328 {
3329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003330 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003331 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003332 /* There must be no pending incoming or outgoing data */
3333 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003334 {
3335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003336 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003337 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003338 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003339 {
3340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003341 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003342 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003343 /* Protocol must be DLTS, not TLS */
3344 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003345 {
3346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +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 /* Version must be 1.2 */
3350 if( ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003351 {
3352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003353 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003354 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003355 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003356 {
3357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
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 /* We must be using an AEAD ciphersuite */
3361 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003362 {
3363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
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 /* Renegotiation must not be enabled */
3367#if defined(MBEDTLS_SSL_RENEGOTIATION)
3368 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003369 {
3370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003371 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003372 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003373#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003374
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003375 /*
3376 * Version and format identifier
3377 */
3378 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003379
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003380 if( used <= buf_len )
3381 {
3382 memcpy( p, ssl_serialized_context_header,
3383 sizeof( ssl_serialized_context_header ) );
3384 p += sizeof( ssl_serialized_context_header );
3385 }
3386
3387 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003388 * Session (length + data)
3389 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003390 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003391 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3392 return( ret );
3393
3394 used += 4 + session_len;
3395 if( used <= buf_len )
3396 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003397 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3398 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003399
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003400 ret = ssl_session_save( ssl->session, 1,
3401 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003402 if( ret != 0 )
3403 return( ret );
3404
3405 p += session_len;
3406 }
3407
3408 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003409 * Transform
3410 */
3411 used += sizeof( ssl->transform->randbytes );
3412 if( used <= buf_len )
3413 {
3414 memcpy( p, ssl->transform->randbytes,
3415 sizeof( ssl->transform->randbytes ) );
3416 p += sizeof( ssl->transform->randbytes );
3417 }
3418
3419#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3420 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3421 if( used <= buf_len )
3422 {
3423 *p++ = ssl->transform->in_cid_len;
3424 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3425 p += ssl->transform->in_cid_len;
3426
3427 *p++ = ssl->transform->out_cid_len;
3428 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3429 p += ssl->transform->out_cid_len;
3430 }
3431#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3432
3433 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003434 * Saved fields from top-level ssl_context structure
3435 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003436 used += 4;
3437 if( used <= buf_len )
3438 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003439 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3440 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003441 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003442
3443#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3444 used += 16;
3445 if( used <= buf_len )
3446 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003447 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3448 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003449
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003450 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3451 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003452 }
3453#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3454
3455#if defined(MBEDTLS_SSL_PROTO_DTLS)
3456 used += 1;
3457 if( used <= buf_len )
3458 {
3459 *p++ = ssl->disable_datagram_packing;
3460 }
3461#endif /* MBEDTLS_SSL_PROTO_DTLS */
3462
Jerry Yuae0b2e22021-10-08 15:21:19 +08003463 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003464 if( used <= buf_len )
3465 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003466 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3467 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003468 }
3469
3470#if defined(MBEDTLS_SSL_PROTO_DTLS)
3471 used += 2;
3472 if( used <= buf_len )
3473 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003474 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
3475 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003476 }
3477#endif /* MBEDTLS_SSL_PROTO_DTLS */
3478
3479#if defined(MBEDTLS_SSL_ALPN)
3480 {
3481 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02003482 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003483 : 0;
3484
3485 used += 1 + alpn_len;
3486 if( used <= buf_len )
3487 {
3488 *p++ = alpn_len;
3489
3490 if( ssl->alpn_chosen != NULL )
3491 {
3492 memcpy( p, ssl->alpn_chosen, alpn_len );
3493 p += alpn_len;
3494 }
3495 }
3496 }
3497#endif /* MBEDTLS_SSL_ALPN */
3498
3499 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003500 * Done
3501 */
3502 *olen = used;
3503
3504 if( used > buf_len )
3505 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003506
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003507 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
3508
Hanno Becker43aefe22020-02-05 10:44:56 +00003509 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003510}
3511
3512/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003513 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003514 *
3515 * This internal version is wrapped by a public function that cleans up in
3516 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003517 */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003518static int ssl_context_load( mbedtls_ssl_context *ssl,
3519 const unsigned char *buf,
3520 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003521{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003522 const unsigned char *p = buf;
3523 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003524 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00003525 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003526
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003527 /*
3528 * The context should have been freshly setup or reset.
3529 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02003530 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003531 * renegotiating, or if the user mistakenly loaded a session first.)
3532 */
3533 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
3534 ssl->session != NULL )
3535 {
3536 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3537 }
3538
3539 /*
3540 * We can't check that the config matches the initial one, but we can at
3541 * least check it matches the requirements for serializing.
3542 */
3543 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
3544 ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
3545 ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 ||
3546 ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ||
3547 ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 ||
3548#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003549 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003550#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003551 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003552 {
3553 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3554 }
3555
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003556 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
3557
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003558 /*
3559 * Check version identifier
3560 */
3561 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
3562 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3563
3564 if( memcmp( p, ssl_serialized_context_header,
3565 sizeof( ssl_serialized_context_header ) ) != 0 )
3566 {
3567 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3568 }
3569 p += sizeof( ssl_serialized_context_header );
3570
3571 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003572 * Session
3573 */
3574 if( (size_t)( end - p ) < 4 )
3575 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3576
3577 session_len = ( (size_t) p[0] << 24 ) |
3578 ( (size_t) p[1] << 16 ) |
3579 ( (size_t) p[2] << 8 ) |
3580 ( (size_t) p[3] );
3581 p += 4;
3582
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003583 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00003584 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003585 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003586 ssl->session_in = ssl->session;
3587 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003588 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003589
3590 if( (size_t)( end - p ) < session_len )
3591 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3592
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003593 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003594 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003595 {
3596 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003597 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003598 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003599
3600 p += session_len;
3601
3602 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003603 * Transform
3604 */
3605
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003606 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00003607 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003608 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003609 ssl->transform_in = ssl->transform;
3610 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003611 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003612
3613 /* Read random bytes and populate structure */
3614 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
3615 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08003616#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00003617 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003618 ssl->session->ciphersuite,
3619 ssl->session->master,
Hanno Beckerbd257552021-03-22 06:59:27 +00003620#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
3621 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003622 ssl->session->encrypt_then_mac,
Hanno Beckerbd257552021-03-22 06:59:27 +00003623#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
3624 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003625 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
3626 p, /* currently pointing to randbytes */
3627 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
3628 ssl->conf->endpoint,
3629 ssl );
3630 if( ret != 0 )
3631 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08003632#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003633 p += sizeof( ssl->transform->randbytes );
3634
3635#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3636 /* Read connection IDs and store them */
3637 if( (size_t)( end - p ) < 1 )
3638 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3639
3640 ssl->transform->in_cid_len = *p++;
3641
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02003642 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003643 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3644
3645 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
3646 p += ssl->transform->in_cid_len;
3647
3648 ssl->transform->out_cid_len = *p++;
3649
3650 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
3651 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3652
3653 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
3654 p += ssl->transform->out_cid_len;
3655#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3656
3657 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003658 * Saved fields from top-level ssl_context structure
3659 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003660 if( (size_t)( end - p ) < 4 )
3661 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3662
3663 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
3664 ( (uint32_t) p[1] << 16 ) |
3665 ( (uint32_t) p[2] << 8 ) |
3666 ( (uint32_t) p[3] );
3667 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003668
3669#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3670 if( (size_t)( end - p ) < 16 )
3671 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3672
3673 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
3674 ( (uint64_t) p[1] << 48 ) |
3675 ( (uint64_t) p[2] << 40 ) |
3676 ( (uint64_t) p[3] << 32 ) |
3677 ( (uint64_t) p[4] << 24 ) |
3678 ( (uint64_t) p[5] << 16 ) |
3679 ( (uint64_t) p[6] << 8 ) |
3680 ( (uint64_t) p[7] );
3681 p += 8;
3682
3683 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
3684 ( (uint64_t) p[1] << 48 ) |
3685 ( (uint64_t) p[2] << 40 ) |
3686 ( (uint64_t) p[3] << 32 ) |
3687 ( (uint64_t) p[4] << 24 ) |
3688 ( (uint64_t) p[5] << 16 ) |
3689 ( (uint64_t) p[6] << 8 ) |
3690 ( (uint64_t) p[7] );
3691 p += 8;
3692#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3693
3694#if defined(MBEDTLS_SSL_PROTO_DTLS)
3695 if( (size_t)( end - p ) < 1 )
3696 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3697
3698 ssl->disable_datagram_packing = *p++;
3699#endif /* MBEDTLS_SSL_PROTO_DTLS */
3700
Jerry Yud9a94fe2021-09-28 18:58:59 +08003701 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003702 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08003703 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
3704 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003705
3706#if defined(MBEDTLS_SSL_PROTO_DTLS)
3707 if( (size_t)( end - p ) < 2 )
3708 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3709
3710 ssl->mtu = ( p[0] << 8 ) | p[1];
3711 p += 2;
3712#endif /* MBEDTLS_SSL_PROTO_DTLS */
3713
3714#if defined(MBEDTLS_SSL_ALPN)
3715 {
3716 uint8_t alpn_len;
3717 const char **cur;
3718
3719 if( (size_t)( end - p ) < 1 )
3720 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3721
3722 alpn_len = *p++;
3723
3724 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
3725 {
3726 /* alpn_chosen should point to an item in the configured list */
3727 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
3728 {
3729 if( strlen( *cur ) == alpn_len &&
3730 memcmp( p, cur, alpn_len ) == 0 )
3731 {
3732 ssl->alpn_chosen = *cur;
3733 break;
3734 }
3735 }
3736 }
3737
3738 /* can only happen on conf mismatch */
3739 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
3740 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3741
3742 p += alpn_len;
3743 }
3744#endif /* MBEDTLS_SSL_ALPN */
3745
3746 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003747 * Forced fields from top-level ssl_context structure
3748 *
3749 * Most of them already set to the correct value by mbedtls_ssl_init() and
3750 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
3751 */
3752 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
3753
3754 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
3755 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
3756
Hanno Becker361b10d2019-08-30 10:42:49 +01003757 /* Adjust pointers for header fields of outgoing records to
3758 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00003759 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01003760
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003761#if defined(MBEDTLS_SSL_PROTO_DTLS)
3762 ssl->in_epoch = 1;
3763#endif
3764
3765 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
3766 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00003767 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
3768 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003769 if( ssl->handshake != NULL )
3770 {
3771 mbedtls_ssl_handshake_free( ssl );
3772 mbedtls_free( ssl->handshake );
3773 ssl->handshake = NULL;
3774 }
3775
3776 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003777 * Done - should have consumed entire buffer
3778 */
3779 if( p != end )
3780 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003781
3782 return( 0 );
3783}
3784
3785/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003786 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003787 */
3788int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
3789 const unsigned char *buf,
3790 size_t len )
3791{
3792 int ret = ssl_context_load( context, buf, len );
3793
3794 if( ret != 0 )
3795 mbedtls_ssl_free( context );
3796
3797 return( ret );
3798}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003799#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003800
3801/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003802 * Free an SSL context
3803 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003804void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003805{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003806 if( ssl == NULL )
3807 return;
3808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003810
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003811 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003812 {
sander-visserb8aa2072020-05-06 22:05:13 +02003813#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3814 size_t out_buf_len = ssl->out_buf_len;
3815#else
3816 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
3817#endif
3818
Darryl Greenb33cc762019-11-28 14:29:44 +00003819 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003820 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003821 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003822 }
3823
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003824 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003825 {
sander-visserb8aa2072020-05-06 22:05:13 +02003826#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3827 size_t in_buf_len = ssl->in_buf_len;
3828#else
3829 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
3830#endif
3831
Darryl Greenb33cc762019-11-28 14:29:44 +00003832 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003833 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003834 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003835 }
3836
Paul Bakker48916f92012-09-16 19:57:18 +00003837 if( ssl->transform )
3838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003839 mbedtls_ssl_transform_free( ssl->transform );
3840 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003841 }
3842
3843 if( ssl->handshake )
3844 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02003845 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 mbedtls_ssl_transform_free( ssl->transform_negotiate );
3847 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00003848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003849 mbedtls_free( ssl->handshake );
3850 mbedtls_free( ssl->transform_negotiate );
3851 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00003852 }
3853
Ronald Cron6f135e12021-12-08 16:57:54 +01003854#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01003855 mbedtls_ssl_transform_free( ssl->transform_application );
3856 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01003857#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01003858
Paul Bakkerc0463502013-02-14 11:19:38 +01003859 if( ssl->session )
3860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861 mbedtls_ssl_session_free( ssl->session );
3862 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003863 }
3864
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02003865#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02003866 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003867 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003868 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003869 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00003870 }
Paul Bakker0be444a2013-08-27 21:55:01 +02003871#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003872
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003873#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02003875#endif
3876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00003878
Paul Bakker86f04f42013-02-14 11:20:09 +01003879 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003880 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003881}
3882
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02003883/*
3884 * Initialze mbedtls_ssl_config
3885 */
3886void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
3887{
3888 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
3889}
3890
Gilles Peskineeccd8882020-03-10 12:19:08 +01003891#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003892#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskineae270bf2021-06-02 00:05:29 +02003893/* The selection should be the same as mbedtls_x509_crt_profile_default in
Gilles Peskinea28f0f52021-06-02 15:29:38 +02003894 * x509_crt.c. Here, the order matters. Currently we favor stronger hashes,
3895 * for no fundamental reason.
Gilles Peskineae270bf2021-06-02 00:05:29 +02003896 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
3897 * about this list. */
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003898static int ssl_preset_default_hashes[] = {
3899#if defined(MBEDTLS_SHA512_C)
3900 MBEDTLS_MD_SHA512,
Mateusz Starzyk3352a532021-04-06 14:28:22 +02003901#endif
3902#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003903 MBEDTLS_MD_SHA384,
3904#endif
3905#if defined(MBEDTLS_SHA256_C)
3906 MBEDTLS_MD_SHA256,
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02003907#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003908 MBEDTLS_MD_NONE
3909};
Jerry Yuf017ee42022-01-12 15:49:48 +08003910#endif /* !MBEDTLS_DEPRECATED_REMOVED */
3911#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003912
Gilles Peskineae270bf2021-06-02 00:05:29 +02003913/* The selection should be the same as mbedtls_x509_crt_profile_default in
3914 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02003915 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02003916 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02003917 * about this list.
3918 */
Brett Warrene0edc842021-08-17 09:53:13 +01003919static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02003920#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003921 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003922#endif
3923#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003924 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003925#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02003926#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003927 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003928#endif
3929#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003930 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003931#endif
3932#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003933 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003934#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02003935#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003936 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003937#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02003938#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003939 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003940#endif
3941#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003942 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003943#endif
Brett Warrene0edc842021-08-17 09:53:13 +01003944 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02003945};
Gilles Peskineae270bf2021-06-02 00:05:29 +02003946
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003947static int ssl_preset_suiteb_ciphersuites[] = {
3948 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
3949 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
3950 0
3951};
3952
Gilles Peskineeccd8882020-03-10 12:19:08 +01003953#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003954#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003955static int ssl_preset_suiteb_hashes[] = {
Jerry Yuf017ee42022-01-12 15:49:48 +08003956#if defined(MBEDTLS_SHA256_C)
3957 MBEDTLS_MD_SHA256,
3958#endif
Jerry Yu713013f2022-01-17 18:16:35 +08003959#if defined(MBEDTLS_SHA384_C)
3960 MBEDTLS_MD_SHA384,
3961#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003962 MBEDTLS_MD_NONE
3963};
Jerry Yuf017ee42022-01-12 15:49:48 +08003964#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01003965
Jerry Yu909df7b2022-01-22 11:56:27 +08003966/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08003967 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08003968 * rules SHOULD be upheld.
3969 * - No duplicate entries.
3970 * - But if there is a good reason, do not change the order of the algorithms.
3971 * - ssl_tls12_present* is for TLS 1.2 use only.
3972 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08003973 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01003974static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08003975
Jerry Yued5e9f42022-01-26 11:21:34 +08003976#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \
3977 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
3978 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
3979#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C &&
3980 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08003981
Jerry Yu909df7b2022-01-22 11:56:27 +08003982#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \
3983 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
3984 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
3985#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
3986 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
3987
Jerry Yued5e9f42022-01-26 11:21:34 +08003988#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA512_C) && \
3989 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
3990 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
3991#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
3992 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08003993
Jerry Yu909df7b2022-01-22 11:56:27 +08003994#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C)
3995 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
3996#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */
3997
Ronald Cron8540cf62022-03-16 08:01:09 +01003998#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA512_C)
3999 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4000#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4001
4002#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA384_C)
4003 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4004#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4005
Jerry Yu53037892022-01-25 11:02:06 +08004006#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C)
4007 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4008#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4009
Jerry Yu909df7b2022-01-22 11:56:27 +08004010 MBEDTLS_TLS1_3_SIG_NONE
4011};
4012
4013/* NOTICE: see above */
4014#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4015static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Jerry Yu713013f2022-01-17 18:16:35 +08004016#if defined(MBEDTLS_SHA512_C)
4017 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA512 )
4018#endif
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004019#if defined(MBEDTLS_SHA384_C)
4020 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 )
4021#endif
4022#if defined(MBEDTLS_SHA256_C)
4023 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 )
4024#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08004025 MBEDTLS_TLS1_3_SIG_NONE
4026};
4027#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4028/* NOTICE: see above */
4029static uint16_t ssl_preset_suiteb_sig_algs[] = {
4030
Jerry Yu909df7b2022-01-22 11:56:27 +08004031#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \
4032 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4033 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
4034#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C &&
4035 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4036
Jerry Yu53037892022-01-25 11:02:06 +08004037#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \
4038 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4039 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
4040#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
4041 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004042
4043#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C)
4044 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4045#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004046
Jerry Yu53037892022-01-25 11:02:06 +08004047#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C)
4048 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4049#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4050
Jerry Yu713013f2022-01-17 18:16:35 +08004051 MBEDTLS_TLS1_3_SIG_NONE
4052};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004053
Jerry Yu909df7b2022-01-22 11:56:27 +08004054/* NOTICE: see above */
4055#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4056static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Jerry Yu713013f2022-01-17 18:16:35 +08004057#if defined(MBEDTLS_SHA256_C)
4058 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 )
4059#endif
Jerry Yu18c833e2022-01-25 10:55:47 +08004060#if defined(MBEDTLS_SHA384_C)
4061 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 )
4062#endif
Xiaofei Bai95395012021-11-25 08:51:30 +00004063 MBEDTLS_TLS1_3_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004064};
Jerry Yu909df7b2022-01-22 11:56:27 +08004065#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4066
Jerry Yu1a8b4812022-01-20 17:56:50 +08004067#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004068
Brett Warrene0edc842021-08-17 09:53:13 +01004069static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004070#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004071 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004072#endif
4073#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004074 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004075#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004076 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004077};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004078
Jerry Yu1a8b4812022-01-20 17:56:50 +08004079#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004080/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004081 * to make sure there are no duplicated signature algorithm entries. */
Jerry Yuf377d642022-01-25 10:43:59 +08004082static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004083{
4084 size_t i, j;
4085 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004086
Jerry Yuf377d642022-01-25 10:43:59 +08004087 for( i = 0; sig_algs[i] != MBEDTLS_TLS1_3_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004088 {
Jerry Yuf377d642022-01-25 10:43:59 +08004089 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004090 {
Jerry Yuf377d642022-01-25 10:43:59 +08004091 if( sig_algs[i] != sig_algs[j] )
4092 continue;
4093 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4094 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4095 sig_algs[i], j, i );
4096 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004097 }
4098 }
4099 return( ret );
4100}
4101
4102#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4103
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004104/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004105 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004106 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004107int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004108 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004109{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004110#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004111 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004112#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004113
Jerry Yu1a8b4812022-01-20 17:56:50 +08004114#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004115 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004116 {
4117 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4118 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4119 }
4120
Jerry Yuf377d642022-01-25 10:43:59 +08004121 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004122 {
4123 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4124 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4125 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004126
4127#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004128 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004129 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004130 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004131 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4132 }
4133
Jerry Yuf377d642022-01-25 10:43:59 +08004134 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004135 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004136 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004137 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4138 }
4139#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004140#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4141
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004142 /* Use the functions here so that they are covered in tests,
4143 * but otherwise access member directly for efficiency */
4144 mbedtls_ssl_conf_endpoint( conf, endpoint );
4145 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004146
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004147 /*
4148 * Things that are common to all presets
4149 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004150#if defined(MBEDTLS_SSL_CLI_C)
4151 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4152 {
4153 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4154#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4155 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4156#endif
4157 }
4158#endif
4159
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004160#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4161 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4162#endif
4163
4164#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4165 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4166#endif
4167
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004168#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004169 conf->f_cookie_write = ssl_cookie_write_dummy;
4170 conf->f_cookie_check = ssl_cookie_check_dummy;
4171#endif
4172
4173#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4174 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4175#endif
4176
Janos Follath088ce432017-04-10 12:42:31 +01004177#if defined(MBEDTLS_SSL_SRV_C)
4178 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004179 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004180#endif
4181
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004182#if defined(MBEDTLS_SSL_PROTO_DTLS)
4183 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4184 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4185#endif
4186
4187#if defined(MBEDTLS_SSL_RENEGOTIATION)
4188 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004189 memset( conf->renego_period, 0x00, 2 );
4190 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004191#endif
4192
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004193#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004194 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4195 {
4196 const unsigned char dhm_p[] =
4197 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4198 const unsigned char dhm_g[] =
4199 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004200
Hanno Beckere2defad2021-07-24 05:59:17 +01004201 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4202 dhm_p, sizeof( dhm_p ),
4203 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4204 {
4205 return( ret );
4206 }
4207 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004208#endif
4209
Ronald Cron6f135e12021-12-08 16:57:54 +01004210#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker71f1ed62021-07-24 06:01:47 +01004211 /*
4212 * Allow all TLS 1.3 key exchange modes by default.
4213 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004214 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004215#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004216
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004217 /*
4218 * Preset-specific defaults
4219 */
4220 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004221 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004222 /*
4223 * NSA Suite B
4224 */
4225 case MBEDTLS_SSL_PRESET_SUITEB:
Ronald Crone71639d2022-03-11 11:31:31 +01004226 conf->min_major_ver = MBEDTLS_SSL_MIN_MAJOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004227 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Ronald Crone71639d2022-03-11 11:31:31 +01004228
4229 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Jerry Yu7d239632022-01-27 14:16:44 +08004230#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4231 /* Hybrid TLS 1.2/1.3 is not supported yet */
4232 conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
4233#else
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004234 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004235#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3 */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004236
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004237 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004238
4239#if defined(MBEDTLS_X509_CRT_PARSE_C)
4240 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004241#endif
4242
Gilles Peskineeccd8882020-03-10 12:19:08 +01004243#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004244#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004245 conf->sig_hashes = ssl_preset_suiteb_hashes;
Jerry Yuf017ee42022-01-12 15:49:48 +08004246#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004247#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4248 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4249 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4250 else
4251#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4252 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004253#endif
4254
Brett Warrene0edc842021-08-17 09:53:13 +01004255#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4256 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004257#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004258 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004259 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004260
4261 /*
4262 * Default
4263 */
4264 default:
Ronald Crone71639d2022-03-11 11:31:31 +01004265 conf->min_major_ver = MBEDTLS_SSL_MIN_MAJOR_VERSION;
4266 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
4267
Ron Eldor5e9f14d2017-05-28 10:46:38 +03004268 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
4269 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
4270 MBEDTLS_SSL_MIN_MINOR_VERSION :
4271 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004272#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4273 /* Hybrid TLS 1.2/1.3 is not supported yet */
4274 conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
4275#else
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004276 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004277#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3 */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004278
4279#if defined(MBEDTLS_SSL_PROTO_DTLS)
4280 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
TRodziewiczef73f012021-05-13 14:53:36 +02004281 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004282#endif
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004283 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004284
4285#if defined(MBEDTLS_X509_CRT_PARSE_C)
4286 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4287#endif
4288
Gilles Peskineeccd8882020-03-10 12:19:08 +01004289#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004290#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01004291 conf->sig_hashes = ssl_preset_default_hashes;
Jerry Yuf017ee42022-01-12 15:49:48 +08004292#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004293#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4294 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4295 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4296 else
4297#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4298 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004299#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004300
Brett Warrene0edc842021-08-17 09:53:13 +01004301#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4302 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004303#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004304 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004305
4306#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4307 conf->dhm_min_bitlen = 1024;
4308#endif
4309 }
4310
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004311 return( 0 );
4312}
4313
4314/*
4315 * Free mbedtls_ssl_config
4316 */
4317void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4318{
4319#if defined(MBEDTLS_DHM_C)
4320 mbedtls_mpi_free( &conf->dhm_P );
4321 mbedtls_mpi_free( &conf->dhm_G );
4322#endif
4323
Gilles Peskineeccd8882020-03-10 12:19:08 +01004324#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004325 if( conf->psk != NULL )
4326 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004327 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004328 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004329 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004330 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004331 }
4332
4333 if( conf->psk_identity != NULL )
4334 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004335 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004336 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004337 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004338 conf->psk_identity_len = 0;
4339 }
4340#endif
4341
4342#if defined(MBEDTLS_X509_CRT_PARSE_C)
4343 ssl_key_cert_free( conf->key_cert );
4344#endif
4345
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004346 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004347}
4348
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004349#if defined(MBEDTLS_PK_C) && \
4350 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004351/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004352 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004354unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004355{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004356#if defined(MBEDTLS_RSA_C)
4357 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4358 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004359#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004360#if defined(MBEDTLS_ECDSA_C)
4361 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4362 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004363#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004364 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004365}
4366
Hanno Becker7e5437a2017-04-28 17:15:26 +01004367unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4368{
4369 switch( type ) {
4370 case MBEDTLS_PK_RSA:
4371 return( MBEDTLS_SSL_SIG_RSA );
4372 case MBEDTLS_PK_ECDSA:
4373 case MBEDTLS_PK_ECKEY:
4374 return( MBEDTLS_SSL_SIG_ECDSA );
4375 default:
4376 return( MBEDTLS_SSL_SIG_ANON );
4377 }
4378}
4379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004380mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004381{
4382 switch( sig )
4383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004384#if defined(MBEDTLS_RSA_C)
4385 case MBEDTLS_SSL_SIG_RSA:
4386 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004387#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004388#if defined(MBEDTLS_ECDSA_C)
4389 case MBEDTLS_SSL_SIG_ECDSA:
4390 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004391#endif
4392 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004393 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004394 }
4395}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004396#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004397
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004398/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004399 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004400 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004401mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004402{
4403 switch( hash )
4404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004405#if defined(MBEDTLS_MD5_C)
4406 case MBEDTLS_SSL_HASH_MD5:
4407 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004408#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004409#if defined(MBEDTLS_SHA1_C)
4410 case MBEDTLS_SSL_HASH_SHA1:
4411 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004412#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004413#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004414 case MBEDTLS_SSL_HASH_SHA224:
4415 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004416#endif
4417#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004418 case MBEDTLS_SSL_HASH_SHA256:
4419 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004420#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004421#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004422 case MBEDTLS_SSL_HASH_SHA384:
4423 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004424#endif
4425#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004426 case MBEDTLS_SSL_HASH_SHA512:
4427 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004428#endif
4429 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004430 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004431 }
4432}
4433
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004434/*
4435 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4436 */
4437unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4438{
4439 switch( md )
4440 {
4441#if defined(MBEDTLS_MD5_C)
4442 case MBEDTLS_MD_MD5:
4443 return( MBEDTLS_SSL_HASH_MD5 );
4444#endif
4445#if defined(MBEDTLS_SHA1_C)
4446 case MBEDTLS_MD_SHA1:
4447 return( MBEDTLS_SSL_HASH_SHA1 );
4448#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004449#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004450 case MBEDTLS_MD_SHA224:
4451 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004452#endif
4453#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004454 case MBEDTLS_MD_SHA256:
4455 return( MBEDTLS_SSL_HASH_SHA256 );
4456#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004457#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004458 case MBEDTLS_MD_SHA384:
4459 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004460#endif
4461#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004462 case MBEDTLS_MD_SHA512:
4463 return( MBEDTLS_SSL_HASH_SHA512 );
4464#endif
4465 default:
4466 return( MBEDTLS_SSL_HASH_NONE );
4467 }
4468}
4469
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004470/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004471 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004472 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004473 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004474int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004475{
Brett Warrene0edc842021-08-17 09:53:13 +01004476 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004477
Brett Warrene0edc842021-08-17 09:53:13 +01004478 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004479 return( -1 );
4480
Brett Warrene0edc842021-08-17 09:53:13 +01004481 for( ; *group_list != 0; group_list++ )
4482 {
4483 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004484 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01004485 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004486
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004487 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004488}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004489
4490#if defined(MBEDTLS_ECP_C)
4491/*
4492 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
4493 */
4494int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
4495{
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01004496 uint16_t tls_id = mbedtls_ecp_curve_info_from_grp_id( grp_id )->tls_id;
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004497 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
4498}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004499#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004501#if defined(MBEDTLS_X509_CRT_PARSE_C)
4502int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
4503 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004504 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02004505 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004506{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004507 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004508 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004509 const char *ext_oid;
4510 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004512 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004513 {
4514 /* Server part of the key exchange */
4515 switch( ciphersuite->key_exchange )
4516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004517 case MBEDTLS_KEY_EXCHANGE_RSA:
4518 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004519 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004520 break;
4521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004522 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
4523 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
4524 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
4525 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004526 break;
4527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004528 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
4529 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004530 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004531 break;
4532
4533 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004534 case MBEDTLS_KEY_EXCHANGE_NONE:
4535 case MBEDTLS_KEY_EXCHANGE_PSK:
4536 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
4537 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02004538 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004539 usage = 0;
4540 }
4541 }
4542 else
4543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004544 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
4545 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004546 }
4547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004548 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004549 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004550 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004551 ret = -1;
4552 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004554 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004556 ext_oid = MBEDTLS_OID_SERVER_AUTH;
4557 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004558 }
4559 else
4560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004561 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
4562 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004563 }
4564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004565 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004566 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004567 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004568 ret = -1;
4569 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004570
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004571 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004572}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004573#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004574
Jerry Yu148165c2021-09-24 23:20:59 +08004575#if defined(MBEDTLS_USE_PSA_CRYPTO)
4576int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
4577 const mbedtls_md_type_t md,
4578 unsigned char *dst,
4579 size_t dst_len,
4580 size_t *olen )
4581{
Ronald Cronf6893e12022-01-07 22:09:01 +01004582 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4583 psa_hash_operation_t *hash_operation_to_clone;
4584 psa_hash_operation_t hash_operation = psa_hash_operation_init();
4585
Jerry Yu148165c2021-09-24 23:20:59 +08004586 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01004587
4588 switch( md )
4589 {
4590#if defined(MBEDTLS_SHA384_C)
4591 case MBEDTLS_MD_SHA384:
4592 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
4593 break;
4594#endif
4595
4596#if defined(MBEDTLS_SHA256_C)
4597 case MBEDTLS_MD_SHA256:
4598 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
4599 break;
4600#endif
4601
4602 default:
4603 goto exit;
4604 }
4605
4606 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
4607 if( status != PSA_SUCCESS )
4608 goto exit;
4609
4610 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
4611 if( status != PSA_SUCCESS )
4612 goto exit;
4613
4614exit:
Ronald Cronb788c042022-02-15 09:14:15 +01004615 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08004616}
4617#else /* MBEDTLS_USE_PSA_CRYPTO */
4618
Jerry Yu24c0ec32021-09-09 14:21:07 +08004619#if defined(MBEDTLS_SHA384_C)
Jerry Yu000f9762021-09-14 11:12:51 +08004620static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
4621 unsigned char *dst,
4622 size_t dst_len,
4623 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004624{
Jerry Yu24c0ec32021-09-09 14:21:07 +08004625 int ret;
4626 mbedtls_sha512_context sha512;
4627
4628 if( dst_len < 48 )
4629 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4630
4631 mbedtls_sha512_init( &sha512 );
4632 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
4633
4634 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
4635 {
4636 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
4637 goto exit;
4638 }
4639
4640 *olen = 48;
4641
4642exit:
4643
4644 mbedtls_sha512_free( &sha512 );
4645 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004646}
4647#endif /* MBEDTLS_SHA384_C */
4648
4649#if defined(MBEDTLS_SHA256_C)
Jerry Yu000f9762021-09-14 11:12:51 +08004650static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
4651 unsigned char *dst,
4652 size_t dst_len,
4653 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004654{
Jerry Yu24c0ec32021-09-09 14:21:07 +08004655 int ret;
4656 mbedtls_sha256_context sha256;
4657
4658 if( dst_len < 32 )
4659 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4660
4661 mbedtls_sha256_init( &sha256 );
4662 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08004663
Jerry Yu24c0ec32021-09-09 14:21:07 +08004664 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
4665 {
4666 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
4667 goto exit;
4668 }
4669
4670 *olen = 32;
4671
4672exit:
4673
4674 mbedtls_sha256_free( &sha256 );
4675 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004676}
4677#endif /* MBEDTLS_SHA256_C */
4678
Jerry Yu000f9762021-09-14 11:12:51 +08004679int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
4680 const mbedtls_md_type_t md,
4681 unsigned char *dst,
4682 size_t dst_len,
4683 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004684{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004685 switch( md )
4686 {
4687
Jerry Yu24c0ec32021-09-09 14:21:07 +08004688#if defined(MBEDTLS_SHA384_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004689 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08004690 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08004691#endif /* MBEDTLS_SHA384_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004692
Jerry Yu24c0ec32021-09-09 14:21:07 +08004693#if defined(MBEDTLS_SHA256_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004694 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08004695 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08004696#endif /* MBEDTLS_SHA256_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004697
4698 default:
4699 break;
4700 }
Jerry Yuc5aef882021-12-23 20:15:02 +08004701 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004702}
XiaokangQian647719a2021-12-07 09:16:29 +00004703
Jerry Yu148165c2021-09-24 23:20:59 +08004704#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08004705
Jerry Yudc7bd172022-02-17 13:44:15 +08004706#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4707
4708#if defined(MBEDTLS_USE_PSA_CRYPTO)
4709
4710static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
4711 mbedtls_svc_key_id_t key,
4712 psa_algorithm_t alg,
4713 const unsigned char* seed, size_t seed_length,
4714 const unsigned char* label, size_t label_length,
4715 size_t capacity )
4716{
4717 psa_status_t status;
4718
4719 status = psa_key_derivation_setup( derivation, alg );
4720 if( status != PSA_SUCCESS )
4721 return( status );
4722
4723 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4724 {
4725 status = psa_key_derivation_input_bytes( derivation,
4726 PSA_KEY_DERIVATION_INPUT_SEED,
4727 seed, seed_length );
4728 if( status != PSA_SUCCESS )
4729 return( status );
4730
4731 if( mbedtls_svc_key_id_is_null( key ) )
4732 {
4733 status = psa_key_derivation_input_bytes(
4734 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
4735 NULL, 0 );
4736 }
4737 else
4738 {
4739 status = psa_key_derivation_input_key(
4740 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
4741 }
4742 if( status != PSA_SUCCESS )
4743 return( status );
4744
4745 status = psa_key_derivation_input_bytes( derivation,
4746 PSA_KEY_DERIVATION_INPUT_LABEL,
4747 label, label_length );
4748 if( status != PSA_SUCCESS )
4749 return( status );
4750 }
4751 else
4752 {
4753 return( PSA_ERROR_NOT_SUPPORTED );
4754 }
4755
4756 status = psa_key_derivation_set_capacity( derivation, capacity );
4757 if( status != PSA_SUCCESS )
4758 return( status );
4759
4760 return( PSA_SUCCESS );
4761}
4762
4763static int tls_prf_generic( mbedtls_md_type_t md_type,
4764 const unsigned char *secret, size_t slen,
4765 const char *label,
4766 const unsigned char *random, size_t rlen,
4767 unsigned char *dstbuf, size_t dlen )
4768{
4769 psa_status_t status;
4770 psa_algorithm_t alg;
4771 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
4772 psa_key_derivation_operation_t derivation =
4773 PSA_KEY_DERIVATION_OPERATION_INIT;
4774
4775 if( md_type == MBEDTLS_MD_SHA384 )
4776 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
4777 else
4778 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
4779
4780 /* Normally a "secret" should be long enough to be impossible to
4781 * find by brute force, and in particular should not be empty. But
4782 * this PRF is also used to derive an IV, in particular in EAP-TLS,
4783 * and for this use case it makes sense to have a 0-length "secret".
4784 * Since the key API doesn't allow importing a key of length 0,
4785 * keep master_key=0, which setup_psa_key_derivation() understands
4786 * to mean a 0-length "secret" input. */
4787 if( slen != 0 )
4788 {
4789 psa_key_attributes_t key_attributes = psa_key_attributes_init();
4790 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
4791 psa_set_key_algorithm( &key_attributes, alg );
4792 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
4793
4794 status = psa_import_key( &key_attributes, secret, slen, &master_key );
4795 if( status != PSA_SUCCESS )
4796 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4797 }
4798
4799 status = setup_psa_key_derivation( &derivation,
4800 master_key, alg,
4801 random, rlen,
4802 (unsigned char const *) label,
4803 (size_t) strlen( label ),
4804 dlen );
4805 if( status != PSA_SUCCESS )
4806 {
4807 psa_key_derivation_abort( &derivation );
4808 psa_destroy_key( master_key );
4809 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4810 }
4811
4812 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
4813 if( status != PSA_SUCCESS )
4814 {
4815 psa_key_derivation_abort( &derivation );
4816 psa_destroy_key( master_key );
4817 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4818 }
4819
4820 status = psa_key_derivation_abort( &derivation );
4821 if( status != PSA_SUCCESS )
4822 {
4823 psa_destroy_key( master_key );
4824 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4825 }
4826
4827 if( ! mbedtls_svc_key_id_is_null( master_key ) )
4828 status = psa_destroy_key( master_key );
4829 if( status != PSA_SUCCESS )
4830 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4831
4832 return( 0 );
4833}
4834
4835#else /* MBEDTLS_USE_PSA_CRYPTO */
4836
4837static int tls_prf_generic( mbedtls_md_type_t md_type,
4838 const unsigned char *secret, size_t slen,
4839 const char *label,
4840 const unsigned char *random, size_t rlen,
4841 unsigned char *dstbuf, size_t dlen )
4842{
4843 size_t nb;
4844 size_t i, j, k, md_len;
4845 unsigned char *tmp;
4846 size_t tmp_len = 0;
4847 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
4848 const mbedtls_md_info_t *md_info;
4849 mbedtls_md_context_t md_ctx;
4850 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4851
4852 mbedtls_md_init( &md_ctx );
4853
4854 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
4855 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4856
4857 md_len = mbedtls_md_get_size( md_info );
4858
4859 tmp_len = md_len + strlen( label ) + rlen;
4860 tmp = mbedtls_calloc( 1, tmp_len );
4861 if( tmp == NULL )
4862 {
4863 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
4864 goto exit;
4865 }
4866
4867 nb = strlen( label );
4868 memcpy( tmp + md_len, label, nb );
4869 memcpy( tmp + md_len + nb, random, rlen );
4870 nb += rlen;
4871
4872 /*
4873 * Compute P_<hash>(secret, label + random)[0..dlen]
4874 */
4875 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
4876 goto exit;
4877
4878 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
4879 if( ret != 0 )
4880 goto exit;
4881 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
4882 if( ret != 0 )
4883 goto exit;
4884 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
4885 if( ret != 0 )
4886 goto exit;
4887
4888 for( i = 0; i < dlen; i += md_len )
4889 {
4890 ret = mbedtls_md_hmac_reset ( &md_ctx );
4891 if( ret != 0 )
4892 goto exit;
4893 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
4894 if( ret != 0 )
4895 goto exit;
4896 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
4897 if( ret != 0 )
4898 goto exit;
4899
4900 ret = mbedtls_md_hmac_reset ( &md_ctx );
4901 if( ret != 0 )
4902 goto exit;
4903 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
4904 if( ret != 0 )
4905 goto exit;
4906 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
4907 if( ret != 0 )
4908 goto exit;
4909
4910 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
4911
4912 for( j = 0; j < k; j++ )
4913 dstbuf[i + j] = h_i[j];
4914 }
4915
4916exit:
4917 mbedtls_md_free( &md_ctx );
4918
4919 mbedtls_platform_zeroize( tmp, tmp_len );
4920 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
4921
4922 mbedtls_free( tmp );
4923
4924 return( ret );
4925}
4926#endif /* MBEDTLS_USE_PSA_CRYPTO */
4927
4928#if defined(MBEDTLS_SHA256_C)
4929static int tls_prf_sha256( const unsigned char *secret, size_t slen,
4930 const char *label,
4931 const unsigned char *random, size_t rlen,
4932 unsigned char *dstbuf, size_t dlen )
4933{
4934 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
4935 label, random, rlen, dstbuf, dlen ) );
4936}
4937#endif /* MBEDTLS_SHA256_C */
4938
4939#if defined(MBEDTLS_SHA384_C)
4940static int tls_prf_sha384( const unsigned char *secret, size_t slen,
4941 const char *label,
4942 const unsigned char *random, size_t rlen,
4943 unsigned char *dstbuf, size_t dlen )
4944{
4945 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
4946 label, random, rlen, dstbuf, dlen ) );
4947}
4948#endif /* MBEDTLS_SHA384_C */
4949
Jerry Yuf009d862022-02-17 14:01:37 +08004950/*
4951 * Set appropriate PRF function and other SSL / TLS1.2 functions
4952 *
4953 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08004954 * - hash associated with the ciphersuite (only used by TLS 1.2)
4955 *
4956 * Outputs:
4957 * - the tls_prf, calc_verify and calc_finished members of handshake structure
4958 */
4959static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08004960 mbedtls_md_type_t hash )
4961{
Jerry Yuf009d862022-02-17 14:01:37 +08004962#if defined(MBEDTLS_SHA384_C)
Ronald Cron81591aa2022-03-07 09:05:51 +01004963 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08004964 {
4965 handshake->tls_prf = tls_prf_sha384;
4966 handshake->calc_verify = ssl_calc_verify_tls_sha384;
4967 handshake->calc_finished = ssl_calc_finished_tls_sha384;
4968 }
4969 else
4970#endif
4971#if defined(MBEDTLS_SHA256_C)
Jerry Yuf009d862022-02-17 14:01:37 +08004972 {
Ronald Cron81591aa2022-03-07 09:05:51 +01004973 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08004974 handshake->tls_prf = tls_prf_sha256;
4975 handshake->calc_verify = ssl_calc_verify_tls_sha256;
4976 handshake->calc_finished = ssl_calc_finished_tls_sha256;
4977 }
Ronald Cron81591aa2022-03-07 09:05:51 +01004978#else
Jerry Yuf009d862022-02-17 14:01:37 +08004979 {
Jerry Yuf009d862022-02-17 14:01:37 +08004980 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01004981 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08004982 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4983 }
Ronald Cron81591aa2022-03-07 09:05:51 +01004984#endif
Jerry Yuf009d862022-02-17 14:01:37 +08004985
4986 return( 0 );
4987}
Jerry Yud6ab2352022-02-17 14:03:43 +08004988
4989#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
4990 defined(MBEDTLS_USE_PSA_CRYPTO)
4991static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
4992{
4993 if( ssl->conf->f_psk != NULL )
4994 {
4995 /* If we've used a callback to select the PSK,
4996 * the static configuration is irrelevant. */
4997 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
4998 return( 1 );
4999
5000 return( 0 );
5001 }
5002
5003 if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) )
5004 return( 1 );
5005
5006 return( 0 );
5007}
5008#endif /* MBEDTLS_USE_PSA_CRYPTO &&
5009 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5010
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005011/*
5012 * Compute master secret if needed
5013 *
5014 * Parameters:
5015 * [in/out] handshake
5016 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5017 * (PSA-PSK) ciphersuite_info, psk_opaque
5018 * [out] premaster (cleared)
5019 * [out] master
5020 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5021 * debug: conf->f_dbg, conf->p_dbg
5022 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005023 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005024 */
5025static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5026 unsigned char *master,
5027 const mbedtls_ssl_context *ssl )
5028{
5029 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5030
5031 /* cf. RFC 5246, Section 8.1:
5032 * "The master secret is always exactly 48 bytes in length." */
5033 size_t const master_secret_len = 48;
5034
5035#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5036 unsigned char session_hash[48];
5037#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5038
5039 /* The label for the KDF used for key expansion.
5040 * This is either "master secret" or "extended master secret"
5041 * depending on whether the Extended Master Secret extension
5042 * is used. */
5043 char const *lbl = "master secret";
5044
5045 /* The salt for the KDF used for key expansion.
5046 * - If the Extended Master Secret extension is not used,
5047 * this is ClientHello.Random + ServerHello.Random
5048 * (see Sect. 8.1 in RFC 5246).
5049 * - If the Extended Master Secret extension is used,
5050 * this is the transcript of the handshake so far.
5051 * (see Sect. 4 in RFC 7627). */
5052 unsigned char const *salt = handshake->randbytes;
5053 size_t salt_len = 64;
5054
5055#if !defined(MBEDTLS_DEBUG_C) && \
5056 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5057 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5058 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5059 ssl = NULL; /* make sure we don't use it except for those cases */
5060 (void) ssl;
5061#endif
5062
5063 if( handshake->resume != 0 )
5064 {
5065 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5066 return( 0 );
5067 }
5068
5069#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5070 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5071 {
5072 lbl = "extended master secret";
5073 salt = session_hash;
5074 handshake->calc_verify( ssl, session_hash, &salt_len );
5075
5076 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
5077 session_hash, salt_len );
5078 }
5079#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
5080
5081#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
5082 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5083 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005084 ssl_use_opaque_psk( ssl ) == 1 )
5085 {
5086 /* Perform PSK-to-MS expansion in a single step. */
5087 psa_status_t status;
5088 psa_algorithm_t alg;
5089 mbedtls_svc_key_id_t psk;
5090 psa_key_derivation_operation_t derivation =
5091 PSA_KEY_DERIVATION_OPERATION_INIT;
5092 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5093
5094 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5095
5096 psk = mbedtls_ssl_get_opaque_psk( ssl );
5097
5098 if( hash_alg == MBEDTLS_MD_SHA384 )
5099 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5100 else
5101 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5102
5103 status = setup_psa_key_derivation( &derivation, psk, alg,
5104 salt, salt_len,
5105 (unsigned char const *) lbl,
5106 (size_t) strlen( lbl ),
5107 master_secret_len );
5108 if( status != PSA_SUCCESS )
5109 {
5110 psa_key_derivation_abort( &derivation );
5111 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5112 }
5113
5114 status = psa_key_derivation_output_bytes( &derivation,
5115 master,
5116 master_secret_len );
5117 if( status != PSA_SUCCESS )
5118 {
5119 psa_key_derivation_abort( &derivation );
5120 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5121 }
5122
5123 status = psa_key_derivation_abort( &derivation );
5124 if( status != PSA_SUCCESS )
5125 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5126 }
5127 else
5128#endif
5129 {
5130 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
5131 lbl, salt, salt_len,
5132 master,
5133 master_secret_len );
5134 if( ret != 0 )
5135 {
5136 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5137 return( ret );
5138 }
5139
5140 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5141 handshake->premaster,
5142 handshake->pmslen );
5143
5144 mbedtls_platform_zeroize( handshake->premaster,
5145 sizeof(handshake->premaster) );
5146 }
5147
5148 return( 0 );
5149}
5150
Jerry Yud62f87e2022-02-17 14:09:02 +08005151int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5152{
5153 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5154 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5155 ssl->handshake->ciphersuite_info;
5156
5157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5158
5159 /* Set PRF, calc_verify and calc_finished function pointers */
5160 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005161 ciphersuite_info->mac );
5162 if( ret != 0 )
5163 {
5164 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5165 return( ret );
5166 }
5167
5168 /* Compute master secret if needed */
5169 ret = ssl_compute_master( ssl->handshake,
5170 ssl->session_negotiate->master,
5171 ssl );
5172 if( ret != 0 )
5173 {
5174 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5175 return( ret );
5176 }
5177
5178 /* Swap the client and server random values:
5179 * - MS derivation wanted client+server (RFC 5246 8.1)
5180 * - key derivation wants server+client (RFC 5246 6.3) */
5181 {
5182 unsigned char tmp[64];
5183 memcpy( tmp, ssl->handshake->randbytes, 64 );
5184 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5185 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5186 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5187 }
5188
5189 /* Populate transform structure */
5190 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5191 ssl->session_negotiate->ciphersuite,
5192 ssl->session_negotiate->master,
5193#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
5194 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5195 ssl->session_negotiate->encrypt_then_mac,
5196#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
5197 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
5198 ssl->handshake->tls_prf,
5199 ssl->handshake->randbytes,
5200 ssl->minor_ver,
5201 ssl->conf->endpoint,
5202 ssl );
5203 if( ret != 0 )
5204 {
5205 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5206 return( ret );
5207 }
5208
5209 /* We no longer need Server/ClientHello.random values */
5210 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5211 sizeof( ssl->handshake->randbytes ) );
5212
5213 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5214
5215 return( 0 );
5216}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005217
Ronald Cron4dcbca92022-03-07 10:21:40 +01005218int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5219{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005220 switch( md )
5221 {
5222#if defined(MBEDTLS_SHA384_C)
5223 case MBEDTLS_SSL_HASH_SHA384:
5224 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5225 break;
5226#endif
5227#if defined(MBEDTLS_SHA256_C)
5228 case MBEDTLS_SSL_HASH_SHA256:
5229 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5230 break;
5231#endif
5232 default:
5233 return( -1 );
5234 }
5235
Ronald Cronc2f13a02022-03-07 10:25:24 +01005236 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005237}
5238
Jerry Yu8392e0d2022-02-17 14:10:24 +08005239#if defined(MBEDTLS_SHA256_C)
5240void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5241 unsigned char *hash,
5242 size_t *hlen )
5243{
5244#if defined(MBEDTLS_USE_PSA_CRYPTO)
5245 size_t hash_size;
5246 psa_status_t status;
5247 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5248
5249 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5250 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5251 if( status != PSA_SUCCESS )
5252 {
5253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5254 return;
5255 }
5256
5257 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5258 if( status != PSA_SUCCESS )
5259 {
5260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5261 return;
5262 }
5263
5264 *hlen = 32;
5265 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5267#else
5268 mbedtls_sha256_context sha256;
5269
5270 mbedtls_sha256_init( &sha256 );
5271
5272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5273
5274 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5275 mbedtls_sha256_finish( &sha256, hash );
5276
5277 *hlen = 32;
5278
5279 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5281
5282 mbedtls_sha256_free( &sha256 );
5283#endif /* MBEDTLS_USE_PSA_CRYPTO */
5284 return;
5285}
5286#endif /* MBEDTLS_SHA256_C */
5287
Jerry Yuc1cb3842022-02-17 14:13:48 +08005288#if defined(MBEDTLS_SHA384_C)
5289void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5290 unsigned char *hash,
5291 size_t *hlen )
5292{
5293#if defined(MBEDTLS_USE_PSA_CRYPTO)
5294 size_t hash_size;
5295 psa_status_t status;
5296 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5297
5298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5299 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5300 if( status != PSA_SUCCESS )
5301 {
5302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5303 return;
5304 }
5305
5306 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5307 if( status != PSA_SUCCESS )
5308 {
5309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5310 return;
5311 }
5312
5313 *hlen = 48;
5314 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5316#else
5317 mbedtls_sha512_context sha512;
5318
5319 mbedtls_sha512_init( &sha512 );
5320
5321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
5322
5323 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
5324 mbedtls_sha512_finish( &sha512, hash );
5325
5326 *hlen = 48;
5327
5328 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5330
5331 mbedtls_sha512_free( &sha512 );
5332#endif /* MBEDTLS_USE_PSA_CRYPTO */
5333 return;
5334}
5335#endif /* MBEDTLS_SHA384_C */
5336
Jerry Yuce3dca42022-02-17 14:16:37 +08005337#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
5338int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
5339{
5340 unsigned char *p = ssl->handshake->premaster;
5341 unsigned char *end = p + sizeof( ssl->handshake->premaster );
5342 const unsigned char *psk = NULL;
5343 size_t psk_len = 0;
5344
5345 if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len )
5346 == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
5347 {
5348 /*
5349 * This should never happen because the existence of a PSK is always
5350 * checked before calling this function
5351 */
5352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5353 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5354 }
5355
5356 /*
5357 * PMS = struct {
5358 * opaque other_secret<0..2^16-1>;
5359 * opaque psk<0..2^16-1>;
5360 * };
5361 * with "other_secret" depending on the particular key exchange
5362 */
5363#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5364 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
5365 {
5366 if( end - p < 2 )
5367 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5368
5369 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5370 p += 2;
5371
5372 if( end < p || (size_t)( end - p ) < psk_len )
5373 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5374
5375 memset( p, 0, psk_len );
5376 p += psk_len;
5377 }
5378 else
5379#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5380#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
5381 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5382 {
5383 /*
5384 * other_secret already set by the ClientKeyExchange message,
5385 * and is 48 bytes long
5386 */
5387 if( end - p < 2 )
5388 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5389
5390 *p++ = 0;
5391 *p++ = 48;
5392 p += 48;
5393 }
5394 else
5395#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
5396#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
5397 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5398 {
5399 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5400 size_t len;
5401
5402 /* Write length only when we know the actual value */
5403 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
5404 p + 2, end - ( p + 2 ), &len,
5405 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5406 {
5407 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
5408 return( ret );
5409 }
5410 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
5411 p += 2 + len;
5412
5413 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
5414 }
5415 else
5416#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
5417#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
5418 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
5419 {
5420 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5421 size_t zlen;
5422
5423 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
5424 p + 2, end - ( p + 2 ),
5425 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5426 {
5427 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
5428 return( ret );
5429 }
5430
5431 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
5432 p += 2 + zlen;
5433
5434 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
5435 MBEDTLS_DEBUG_ECDH_Z );
5436 }
5437 else
5438#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
5439 {
5440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5441 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5442 }
5443
5444 /* opaque psk<0..2^16-1>; */
5445 if( end - p < 2 )
5446 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5447
5448 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5449 p += 2;
5450
5451 if( end < p || (size_t)( end - p ) < psk_len )
5452 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5453
5454 memcpy( p, psk, psk_len );
5455 p += psk_len;
5456
5457 ssl->handshake->pmslen = p - ssl->handshake->premaster;
5458
5459 return( 0 );
5460}
5461#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08005462
5463#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
5464static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
5465
5466#if defined(MBEDTLS_SSL_PROTO_DTLS)
5467int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
5468{
5469 /* If renegotiation is not enforced, retransmit until we would reach max
5470 * timeout if we were using the usual handshake doubling scheme */
5471 if( ssl->conf->renego_max_records < 0 )
5472 {
5473 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
5474 unsigned char doublings = 1;
5475
5476 while( ratio != 0 )
5477 {
5478 ++doublings;
5479 ratio >>= 1;
5480 }
5481
5482 if( ++ssl->renego_records_seen > doublings )
5483 {
5484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
5485 return( 0 );
5486 }
5487 }
5488
5489 return( ssl_write_hello_request( ssl ) );
5490}
5491#endif
5492#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08005493
Jerry Yud9526692022-02-17 14:23:47 +08005494/*
5495 * Handshake functions
5496 */
5497#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5498/* No certificate support -> dummy functions */
5499int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
5500{
5501 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5502 ssl->handshake->ciphersuite_info;
5503
5504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
5505
5506 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5507 {
5508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5509 ssl->state++;
5510 return( 0 );
5511 }
5512
5513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5514 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5515}
5516
5517int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5518{
5519 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5520 ssl->handshake->ciphersuite_info;
5521
5522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5523
5524 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5525 {
5526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5527 ssl->state++;
5528 return( 0 );
5529 }
5530
5531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5532 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5533}
5534
5535#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5536/* Some certificate support -> implement write and parse */
5537
5538int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
5539{
5540 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
5541 size_t i, n;
5542 const mbedtls_x509_crt *crt;
5543 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5544 ssl->handshake->ciphersuite_info;
5545
5546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
5547
5548 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5549 {
5550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5551 ssl->state++;
5552 return( 0 );
5553 }
5554
5555#if defined(MBEDTLS_SSL_CLI_C)
5556 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5557 {
5558 if( ssl->handshake->client_auth == 0 )
5559 {
5560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5561 ssl->state++;
5562 return( 0 );
5563 }
5564 }
5565#endif /* MBEDTLS_SSL_CLI_C */
5566#if defined(MBEDTLS_SSL_SRV_C)
5567 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
5568 {
5569 if( mbedtls_ssl_own_cert( ssl ) == NULL )
5570 {
5571 /* Should never happen because we shouldn't have picked the
5572 * ciphersuite if we don't have a certificate. */
5573 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5574 }
5575 }
5576#endif
5577
5578 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
5579
5580 /*
5581 * 0 . 0 handshake type
5582 * 1 . 3 handshake length
5583 * 4 . 6 length of all certs
5584 * 7 . 9 length of cert. 1
5585 * 10 . n-1 peer certificate
5586 * n . n+2 length of cert. 2
5587 * n+3 . ... upper level cert, etc.
5588 */
5589 i = 7;
5590 crt = mbedtls_ssl_own_cert( ssl );
5591
5592 while( crt != NULL )
5593 {
5594 n = crt->raw.len;
5595 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
5596 {
5597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
5598 " > %" MBEDTLS_PRINTF_SIZET,
5599 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
5600 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5601 }
5602
5603 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
5604 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
5605 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
5606
5607 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5608 i += n; crt = crt->next;
5609 }
5610
5611 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
5612 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
5613 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
5614
5615 ssl->out_msglen = i;
5616 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5617 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
5618
5619 ssl->state++;
5620
5621 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
5622 {
5623 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
5624 return( ret );
5625 }
5626
5627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
5628
5629 return( ret );
5630}
5631
5632#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5633
5634#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5635static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5636 unsigned char *crt_buf,
5637 size_t crt_buf_len )
5638{
5639 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5640
5641 if( peer_crt == NULL )
5642 return( -1 );
5643
5644 if( peer_crt->raw.len != crt_buf_len )
5645 return( -1 );
5646
5647 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
5648}
5649#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5650static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5651 unsigned char *crt_buf,
5652 size_t crt_buf_len )
5653{
5654 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5655 unsigned char const * const peer_cert_digest =
5656 ssl->session->peer_cert_digest;
5657 mbedtls_md_type_t const peer_cert_digest_type =
5658 ssl->session->peer_cert_digest_type;
5659 mbedtls_md_info_t const * const digest_info =
5660 mbedtls_md_info_from_type( peer_cert_digest_type );
5661 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
5662 size_t digest_len;
5663
5664 if( peer_cert_digest == NULL || digest_info == NULL )
5665 return( -1 );
5666
5667 digest_len = mbedtls_md_get_size( digest_info );
5668 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
5669 return( -1 );
5670
5671 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
5672 if( ret != 0 )
5673 return( -1 );
5674
5675 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
5676}
5677#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5678#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5679
5680/*
5681 * Once the certificate message is read, parse it into a cert chain and
5682 * perform basic checks, but leave actual verification to the caller
5683 */
5684static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5685 mbedtls_x509_crt *chain )
5686{
5687 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5688#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5689 int crt_cnt=0;
5690#endif
5691 size_t i, n;
5692 uint8_t alert;
5693
5694 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5695 {
5696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5697 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5698 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5699 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5700 }
5701
5702 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
5703 {
5704 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5705 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5706 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5707 }
5708
5709 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
5710 {
5711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5712 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5713 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5714 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5715 }
5716
5717 i = mbedtls_ssl_hs_hdr_len( ssl );
5718
5719 /*
5720 * Same message structure as in mbedtls_ssl_write_certificate()
5721 */
5722 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
5723
5724 if( ssl->in_msg[i] != 0 ||
5725 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
5726 {
5727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5728 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5729 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5730 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5731 }
5732
5733 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5734 i += 3;
5735
5736 /* Iterate through and parse the CRTs in the provided chain. */
5737 while( i < ssl->in_hslen )
5738 {
5739 /* Check that there's room for the next CRT's length fields. */
5740 if ( i + 3 > ssl->in_hslen ) {
5741 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5742 mbedtls_ssl_send_alert_message( ssl,
5743 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5744 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5745 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5746 }
5747 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
5748 * anything beyond 2**16 ~ 64K. */
5749 if( ssl->in_msg[i] != 0 )
5750 {
5751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5752 mbedtls_ssl_send_alert_message( ssl,
5753 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5754 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
5755 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
5756 }
5757
5758 /* Read length of the next CRT in the chain. */
5759 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5760 | (unsigned int) ssl->in_msg[i + 2];
5761 i += 3;
5762
5763 if( n < 128 || i + n > ssl->in_hslen )
5764 {
5765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5766 mbedtls_ssl_send_alert_message( ssl,
5767 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5768 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5769 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5770 }
5771
5772 /* Check if we're handling the first CRT in the chain. */
5773#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5774 if( crt_cnt++ == 0 &&
5775 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5776 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
5777 {
5778 /* During client-side renegotiation, check that the server's
5779 * end-CRTs hasn't changed compared to the initial handshake,
5780 * mitigating the triple handshake attack. On success, reuse
5781 * the original end-CRT instead of parsing it again. */
5782 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
5783 if( ssl_check_peer_crt_unchanged( ssl,
5784 &ssl->in_msg[i],
5785 n ) != 0 )
5786 {
5787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
5788 mbedtls_ssl_send_alert_message( ssl,
5789 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5790 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
5791 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
5792 }
5793
5794 /* Now we can safely free the original chain. */
5795 ssl_clear_peer_cert( ssl->session );
5796 }
5797#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5798
5799 /* Parse the next certificate in the chain. */
5800#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5801 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
5802#else
5803 /* If we don't need to store the CRT chain permanently, parse
5804 * it in-place from the input buffer instead of making a copy. */
5805 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
5806#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5807 switch( ret )
5808 {
5809 case 0: /*ok*/
5810 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5811 /* Ignore certificate with an unknown algorithm: maybe a
5812 prior certificate was already trusted. */
5813 break;
5814
5815 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5816 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5817 goto crt_parse_der_failed;
5818
5819 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5820 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5821 goto crt_parse_der_failed;
5822
5823 default:
5824 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5825 crt_parse_der_failed:
5826 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
5827 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
5828 return( ret );
5829 }
5830
5831 i += n;
5832 }
5833
5834 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
5835 return( 0 );
5836}
5837
5838#if defined(MBEDTLS_SSL_SRV_C)
5839static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
5840{
5841 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5842 return( -1 );
5843
5844 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5845 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5846 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5847 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
5848 {
5849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
5850 return( 0 );
5851 }
5852 return( -1 );
5853}
5854#endif /* MBEDTLS_SSL_SRV_C */
5855
5856/* Check if a certificate message is expected.
5857 * Return either
5858 * - SSL_CERTIFICATE_EXPECTED, or
5859 * - SSL_CERTIFICATE_SKIP
5860 * indicating whether a Certificate message is expected or not.
5861 */
5862#define SSL_CERTIFICATE_EXPECTED 0
5863#define SSL_CERTIFICATE_SKIP 1
5864static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
5865 int authmode )
5866{
5867 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5868 ssl->handshake->ciphersuite_info;
5869
5870 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5871 return( SSL_CERTIFICATE_SKIP );
5872
5873#if defined(MBEDTLS_SSL_SRV_C)
5874 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
5875 {
5876 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5877 return( SSL_CERTIFICATE_SKIP );
5878
5879 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
5880 {
5881 ssl->session_negotiate->verify_result =
5882 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5883 return( SSL_CERTIFICATE_SKIP );
5884 }
5885 }
5886#else
5887 ((void) authmode);
5888#endif /* MBEDTLS_SSL_SRV_C */
5889
5890 return( SSL_CERTIFICATE_EXPECTED );
5891}
5892
5893static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
5894 int authmode,
5895 mbedtls_x509_crt *chain,
5896 void *rs_ctx )
5897{
5898 int ret = 0;
5899 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5900 ssl->handshake->ciphersuite_info;
5901 int have_ca_chain = 0;
5902
5903 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
5904 void *p_vrfy;
5905
5906 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
5907 return( 0 );
5908
5909 if( ssl->f_vrfy != NULL )
5910 {
5911 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
5912 f_vrfy = ssl->f_vrfy;
5913 p_vrfy = ssl->p_vrfy;
5914 }
5915 else
5916 {
5917 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
5918 f_vrfy = ssl->conf->f_vrfy;
5919 p_vrfy = ssl->conf->p_vrfy;
5920 }
5921
5922 /*
5923 * Main check: verify certificate
5924 */
5925#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
5926 if( ssl->conf->f_ca_cb != NULL )
5927 {
5928 ((void) rs_ctx);
5929 have_ca_chain = 1;
5930
5931 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
5932 ret = mbedtls_x509_crt_verify_with_ca_cb(
5933 chain,
5934 ssl->conf->f_ca_cb,
5935 ssl->conf->p_ca_cb,
5936 ssl->conf->cert_profile,
5937 ssl->hostname,
5938 &ssl->session_negotiate->verify_result,
5939 f_vrfy, p_vrfy );
5940 }
5941 else
5942#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
5943 {
5944 mbedtls_x509_crt *ca_chain;
5945 mbedtls_x509_crl *ca_crl;
5946
5947#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5948 if( ssl->handshake->sni_ca_chain != NULL )
5949 {
5950 ca_chain = ssl->handshake->sni_ca_chain;
5951 ca_crl = ssl->handshake->sni_ca_crl;
5952 }
5953 else
5954#endif
5955 {
5956 ca_chain = ssl->conf->ca_chain;
5957 ca_crl = ssl->conf->ca_crl;
5958 }
5959
5960 if( ca_chain != NULL )
5961 have_ca_chain = 1;
5962
5963 ret = mbedtls_x509_crt_verify_restartable(
5964 chain,
5965 ca_chain, ca_crl,
5966 ssl->conf->cert_profile,
5967 ssl->hostname,
5968 &ssl->session_negotiate->verify_result,
5969 f_vrfy, p_vrfy, rs_ctx );
5970 }
5971
5972 if( ret != 0 )
5973 {
5974 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
5975 }
5976
5977#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
5978 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
5979 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
5980#endif
5981
5982 /*
5983 * Secondary checks: always done, but change 'ret' only if it was 0
5984 */
5985
5986#if defined(MBEDTLS_ECP_C)
5987 {
5988 const mbedtls_pk_context *pk = &chain->pk;
5989
5990 /* If certificate uses an EC key, make sure the curve is OK */
5991 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
5992 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
5993 {
5994 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5995
5996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
5997 if( ret == 0 )
5998 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
5999 }
6000 }
6001#endif /* MBEDTLS_ECP_C */
6002
6003 if( mbedtls_ssl_check_cert_usage( chain,
6004 ciphersuite_info,
6005 ! ssl->conf->endpoint,
6006 &ssl->session_negotiate->verify_result ) != 0 )
6007 {
6008 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6009 if( ret == 0 )
6010 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6011 }
6012
6013 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6014 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6015 * with details encoded in the verification flags. All other kinds
6016 * of error codes, including those from the user provided f_vrfy
6017 * functions, are treated as fatal and lead to a failure of
6018 * ssl_parse_certificate even if verification was optional. */
6019 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6020 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6021 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6022 {
6023 ret = 0;
6024 }
6025
6026 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6027 {
6028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6029 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6030 }
6031
6032 if( ret != 0 )
6033 {
6034 uint8_t alert;
6035
6036 /* The certificate may have been rejected for several reasons.
6037 Pick one and send the corresponding alert. Which alert to send
6038 may be a subject of debate in some cases. */
6039 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6040 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6041 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6042 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6043 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6044 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6045 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6046 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6047 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6048 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6049 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6050 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6051 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6052 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6053 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6054 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6055 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6056 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6057 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6058 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6059 else
6060 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6061 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6062 alert );
6063 }
6064
6065#if defined(MBEDTLS_DEBUG_C)
6066 if( ssl->session_negotiate->verify_result != 0 )
6067 {
6068 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6069 (unsigned int) ssl->session_negotiate->verify_result ) );
6070 }
6071 else
6072 {
6073 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6074 }
6075#endif /* MBEDTLS_DEBUG_C */
6076
6077 return( ret );
6078}
6079
6080#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6081static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6082 unsigned char *start, size_t len )
6083{
6084 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6085 /* Remember digest of the peer's end-CRT. */
6086 ssl->session_negotiate->peer_cert_digest =
6087 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6088 if( ssl->session_negotiate->peer_cert_digest == NULL )
6089 {
6090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6091 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6092 mbedtls_ssl_send_alert_message( ssl,
6093 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6094 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6095
6096 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6097 }
6098
6099 ret = mbedtls_md( mbedtls_md_info_from_type(
6100 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6101 start, len,
6102 ssl->session_negotiate->peer_cert_digest );
6103
6104 ssl->session_negotiate->peer_cert_digest_type =
6105 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6106 ssl->session_negotiate->peer_cert_digest_len =
6107 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6108
6109 return( ret );
6110}
6111
6112static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6113 unsigned char *start, size_t len )
6114{
6115 unsigned char *end = start + len;
6116 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6117
6118 /* Make a copy of the peer's raw public key. */
6119 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6120 ret = mbedtls_pk_parse_subpubkey( &start, end,
6121 &ssl->handshake->peer_pubkey );
6122 if( ret != 0 )
6123 {
6124 /* We should have parsed the public key before. */
6125 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6126 }
6127
6128 return( 0 );
6129}
6130#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6131
6132int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6133{
6134 int ret = 0;
6135 int crt_expected;
6136#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6137 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6138 ? ssl->handshake->sni_authmode
6139 : ssl->conf->authmode;
6140#else
6141 const int authmode = ssl->conf->authmode;
6142#endif
6143 void *rs_ctx = NULL;
6144 mbedtls_x509_crt *chain = NULL;
6145
6146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6147
6148 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6149 if( crt_expected == SSL_CERTIFICATE_SKIP )
6150 {
6151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6152 goto exit;
6153 }
6154
6155#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6156 if( ssl->handshake->ecrs_enabled &&
6157 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6158 {
6159 chain = ssl->handshake->ecrs_peer_cert;
6160 ssl->handshake->ecrs_peer_cert = NULL;
6161 goto crt_verify;
6162 }
6163#endif
6164
6165 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6166 {
6167 /* mbedtls_ssl_read_record may have sent an alert already. We
6168 let it decide whether to alert. */
6169 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6170 goto exit;
6171 }
6172
6173#if defined(MBEDTLS_SSL_SRV_C)
6174 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6175 {
6176 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6177
6178 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6179 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6180
6181 goto exit;
6182 }
6183#endif /* MBEDTLS_SSL_SRV_C */
6184
6185 /* Clear existing peer CRT structure in case we tried to
6186 * reuse a session but it failed, and allocate a new one. */
6187 ssl_clear_peer_cert( ssl->session_negotiate );
6188
6189 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6190 if( chain == NULL )
6191 {
6192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6193 sizeof( mbedtls_x509_crt ) ) );
6194 mbedtls_ssl_send_alert_message( ssl,
6195 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6196 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6197
6198 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6199 goto exit;
6200 }
6201 mbedtls_x509_crt_init( chain );
6202
6203 ret = ssl_parse_certificate_chain( ssl, chain );
6204 if( ret != 0 )
6205 goto exit;
6206
6207#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6208 if( ssl->handshake->ecrs_enabled)
6209 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6210
6211crt_verify:
6212 if( ssl->handshake->ecrs_enabled)
6213 rs_ctx = &ssl->handshake->ecrs_ctx;
6214#endif
6215
6216 ret = ssl_parse_certificate_verify( ssl, authmode,
6217 chain, rs_ctx );
6218 if( ret != 0 )
6219 goto exit;
6220
6221#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6222 {
6223 unsigned char *crt_start, *pk_start;
6224 size_t crt_len, pk_len;
6225
6226 /* We parse the CRT chain without copying, so
6227 * these pointers point into the input buffer,
6228 * and are hence still valid after freeing the
6229 * CRT chain. */
6230
6231 crt_start = chain->raw.p;
6232 crt_len = chain->raw.len;
6233
6234 pk_start = chain->pk_raw.p;
6235 pk_len = chain->pk_raw.len;
6236
6237 /* Free the CRT structures before computing
6238 * digest and copying the peer's public key. */
6239 mbedtls_x509_crt_free( chain );
6240 mbedtls_free( chain );
6241 chain = NULL;
6242
6243 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6244 if( ret != 0 )
6245 goto exit;
6246
6247 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6248 if( ret != 0 )
6249 goto exit;
6250 }
6251#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6252 /* Pass ownership to session structure. */
6253 ssl->session_negotiate->peer_cert = chain;
6254 chain = NULL;
6255#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6256
6257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6258
6259exit:
6260
6261 if( ret == 0 )
6262 ssl->state++;
6263
6264#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6265 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6266 {
6267 ssl->handshake->ecrs_peer_cert = chain;
6268 chain = NULL;
6269 }
6270#endif
6271
6272 if( chain != NULL )
6273 {
6274 mbedtls_x509_crt_free( chain );
6275 mbedtls_free( chain );
6276 }
6277
6278 return( ret );
6279}
6280#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6281
Jerry Yu615bd6f2022-02-17 14:25:15 +08006282#if defined(MBEDTLS_SHA256_C)
6283static void ssl_calc_finished_tls_sha256(
6284 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6285{
6286 int len = 12;
6287 const char *sender;
6288 unsigned char padbuf[32];
6289#if defined(MBEDTLS_USE_PSA_CRYPTO)
6290 size_t hash_size;
6291 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
6292 psa_status_t status;
6293#else
6294 mbedtls_sha256_context sha256;
6295#endif
6296
6297 mbedtls_ssl_session *session = ssl->session_negotiate;
6298 if( !session )
6299 session = ssl->session;
6300
6301 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6302 ? "client finished"
6303 : "server finished";
6304
6305#if defined(MBEDTLS_USE_PSA_CRYPTO)
6306 sha256_psa = psa_hash_operation_init();
6307
6308 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6309
6310 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6311 if( status != PSA_SUCCESS )
6312 {
6313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6314 return;
6315 }
6316
6317 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6318 if( status != PSA_SUCCESS )
6319 {
6320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6321 return;
6322 }
6323 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6324#else
6325
6326 mbedtls_sha256_init( &sha256 );
6327
6328 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
6329
6330 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6331
6332 /*
6333 * TLSv1.2:
6334 * hash = PRF( master, finished_label,
6335 * Hash( handshake ) )[0.11]
6336 */
6337
6338#if !defined(MBEDTLS_SHA256_ALT)
6339 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
6340 sha256.state, sizeof( sha256.state ) );
6341#endif
6342
6343 mbedtls_sha256_finish( &sha256, padbuf );
6344 mbedtls_sha256_free( &sha256 );
6345#endif /* MBEDTLS_USE_PSA_CRYPTO */
6346
6347 ssl->handshake->tls_prf( session->master, 48, sender,
6348 padbuf, 32, buf, len );
6349
6350 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6351
6352 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6353
6354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6355}
6356#endif /* MBEDTLS_SHA256_C */
6357
Jerry Yub7ba49e2022-02-17 14:25:53 +08006358
6359#if defined(MBEDTLS_SHA384_C)
6360
6361static void ssl_calc_finished_tls_sha384(
6362 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6363{
6364 int len = 12;
6365 const char *sender;
6366 unsigned char padbuf[48];
6367#if defined(MBEDTLS_USE_PSA_CRYPTO)
6368 size_t hash_size;
6369 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
6370 psa_status_t status;
6371#else
6372 mbedtls_sha512_context sha512;
6373#endif
6374
6375 mbedtls_ssl_session *session = ssl->session_negotiate;
6376 if( !session )
6377 session = ssl->session;
6378
6379 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6380 ? "client finished"
6381 : "server finished";
6382
6383#if defined(MBEDTLS_USE_PSA_CRYPTO)
6384 sha384_psa = psa_hash_operation_init();
6385
6386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6387
6388 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6389 if( status != PSA_SUCCESS )
6390 {
6391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6392 return;
6393 }
6394
6395 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
6396 if( status != PSA_SUCCESS )
6397 {
6398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6399 return;
6400 }
6401 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
6402#else
6403 mbedtls_sha512_init( &sha512 );
6404
6405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
6406
6407 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
6408
6409 /*
6410 * TLSv1.2:
6411 * hash = PRF( master, finished_label,
6412 * Hash( handshake ) )[0.11]
6413 */
6414
6415#if !defined(MBEDTLS_SHA512_ALT)
6416 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6417 sha512.state, sizeof( sha512.state ) );
6418#endif
6419 mbedtls_sha512_finish( &sha512, padbuf );
6420
6421 mbedtls_sha512_free( &sha512 );
6422#endif
6423
6424 ssl->handshake->tls_prf( session->master, 48, sender,
6425 padbuf, 48, buf, len );
6426
6427 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6428
6429 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6430
6431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6432}
6433#endif /* MBEDTLS_SHA384_C */
6434
Jerry Yuaef00152022-02-17 14:27:31 +08006435void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
6436{
6437 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
6438
6439 /*
6440 * Free our handshake params
6441 */
6442 mbedtls_ssl_handshake_free( ssl );
6443 mbedtls_free( ssl->handshake );
6444 ssl->handshake = NULL;
6445
6446 /*
6447 * Free the previous transform and swith in the current one
6448 */
6449 if( ssl->transform )
6450 {
6451 mbedtls_ssl_transform_free( ssl->transform );
6452 mbedtls_free( ssl->transform );
6453 }
6454 ssl->transform = ssl->transform_negotiate;
6455 ssl->transform_negotiate = NULL;
6456
6457 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
6458}
6459
Jerry Yu2a9fff52022-02-17 14:28:51 +08006460void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
6461{
6462 int resume = ssl->handshake->resume;
6463
6464 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
6465
6466#if defined(MBEDTLS_SSL_RENEGOTIATION)
6467 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6468 {
6469 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
6470 ssl->renego_records_seen = 0;
6471 }
6472#endif
6473
6474 /*
6475 * Free the previous session and switch in the current one
6476 */
6477 if( ssl->session )
6478 {
6479#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6480 /* RFC 7366 3.1: keep the EtM state */
6481 ssl->session_negotiate->encrypt_then_mac =
6482 ssl->session->encrypt_then_mac;
6483#endif
6484
6485 mbedtls_ssl_session_free( ssl->session );
6486 mbedtls_free( ssl->session );
6487 }
6488 ssl->session = ssl->session_negotiate;
6489 ssl->session_negotiate = NULL;
6490
6491 /*
6492 * Add cache entry
6493 */
6494 if( ssl->conf->f_set_cache != NULL &&
6495 ssl->session->id_len != 0 &&
6496 resume == 0 )
6497 {
6498 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
6499 ssl->session->id,
6500 ssl->session->id_len,
6501 ssl->session ) != 0 )
6502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
6503 }
6504
6505#if defined(MBEDTLS_SSL_PROTO_DTLS)
6506 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6507 ssl->handshake->flight != NULL )
6508 {
6509 /* Cancel handshake timer */
6510 mbedtls_ssl_set_timer( ssl, 0 );
6511
6512 /* Keep last flight around in case we need to resend it:
6513 * we need the handshake and transform structures for that */
6514 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
6515 }
6516 else
6517#endif
6518 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
6519
6520 ssl->state++;
6521
6522 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
6523}
6524
Jerry Yu3c8e47b2022-02-17 14:30:01 +08006525int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
6526{
6527 int ret, hash_len;
6528
6529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
6530
6531 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
6532
6533 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
6534
6535 /*
6536 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6537 * may define some other value. Currently (early 2016), no defined
6538 * ciphersuite does this (and this is unlikely to change as activity has
6539 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6540 */
6541 hash_len = 12;
6542
6543#if defined(MBEDTLS_SSL_RENEGOTIATION)
6544 ssl->verify_data_len = hash_len;
6545 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
6546#endif
6547
6548 ssl->out_msglen = 4 + hash_len;
6549 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6550 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
6551
6552 /*
6553 * In case of session resuming, invert the client and server
6554 * ChangeCipherSpec messages order.
6555 */
6556 if( ssl->handshake->resume != 0 )
6557 {
6558#if defined(MBEDTLS_SSL_CLI_C)
6559 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6560 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
6561#endif
6562#if defined(MBEDTLS_SSL_SRV_C)
6563 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6564 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
6565#endif
6566 }
6567 else
6568 ssl->state++;
6569
6570 /*
6571 * Switch to our negotiated transform and session parameters for outbound
6572 * data.
6573 */
6574 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
6575
6576#if defined(MBEDTLS_SSL_PROTO_DTLS)
6577 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6578 {
6579 unsigned char i;
6580
6581 /* Remember current epoch settings for resending */
6582 ssl->handshake->alt_transform_out = ssl->transform_out;
6583 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
6584 sizeof( ssl->handshake->alt_out_ctr ) );
6585
6586 /* Set sequence_number to zero */
6587 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
6588
6589
6590 /* Increment epoch */
6591 for( i = 2; i > 0; i-- )
6592 if( ++ssl->cur_out_ctr[i - 1] != 0 )
6593 break;
6594
6595 /* The loop goes to its end iff the counter is wrapping */
6596 if( i == 0 )
6597 {
6598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6599 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
6600 }
6601 }
6602 else
6603#endif /* MBEDTLS_SSL_PROTO_DTLS */
6604 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6605
6606 ssl->transform_out = ssl->transform_negotiate;
6607 ssl->session_out = ssl->session_negotiate;
6608
6609#if defined(MBEDTLS_SSL_PROTO_DTLS)
6610 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6611 mbedtls_ssl_send_flight_completed( ssl );
6612#endif
6613
6614 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6615 {
6616 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6617 return( ret );
6618 }
6619
6620#if defined(MBEDTLS_SSL_PROTO_DTLS)
6621 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6622 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6623 {
6624 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6625 return( ret );
6626 }
6627#endif
6628
6629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
6630
6631 return( 0 );
6632}
6633
Jerry Yu0b3d7c12022-02-17 14:30:51 +08006634#define SSL_MAX_HASH_LEN 12
6635
6636int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
6637{
6638 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6639 unsigned int hash_len = 12;
6640 unsigned char buf[SSL_MAX_HASH_LEN];
6641
6642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
6643
6644 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
6645
6646 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6647 {
6648 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6649 goto exit;
6650 }
6651
6652 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6653 {
6654 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6655 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6656 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6657 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
6658 goto exit;
6659 }
6660
6661 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
6662 {
6663 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6664 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6665 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
6666 goto exit;
6667 }
6668
6669 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
6670 {
6671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6672 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6673 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6674 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
6675 goto exit;
6676 }
6677
6678 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
6679 buf, hash_len ) != 0 )
6680 {
6681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6682 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6683 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
6684 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
6685 goto exit;
6686 }
6687
6688#if defined(MBEDTLS_SSL_RENEGOTIATION)
6689 ssl->verify_data_len = hash_len;
6690 memcpy( ssl->peer_verify_data, buf, hash_len );
6691#endif
6692
6693 if( ssl->handshake->resume != 0 )
6694 {
6695#if defined(MBEDTLS_SSL_CLI_C)
6696 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6697 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
6698#endif
6699#if defined(MBEDTLS_SSL_SRV_C)
6700 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6701 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
6702#endif
6703 }
6704 else
6705 ssl->state++;
6706
6707#if defined(MBEDTLS_SSL_PROTO_DTLS)
6708 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6709 mbedtls_ssl_recv_flight_completed( ssl );
6710#endif
6711
6712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
6713
6714exit:
6715 mbedtls_platform_zeroize( buf, hash_len );
6716 return( ret );
6717}
6718
Jerry Yu392112c2022-02-17 14:34:10 +08006719#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
6720/*
6721 * Helper to get TLS 1.2 PRF from ciphersuite
6722 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
6723 */
6724static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
6725{
6726#if defined(MBEDTLS_SHA384_C)
6727 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6728 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
6729
6730 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
6731 return( tls_prf_sha384 );
6732#else
6733 (void) ciphersuite_id;
6734#endif
6735 return( tls_prf_sha256 );
6736}
6737#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08006738
6739static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
6740{
6741 ((void) tls_prf);
6742#if defined(MBEDTLS_SHA384_C)
6743 if( tls_prf == tls_prf_sha384 )
6744 {
6745 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
6746 }
6747 else
6748#endif
6749#if defined(MBEDTLS_SHA256_C)
6750 if( tls_prf == tls_prf_sha256 )
6751 {
6752 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
6753 }
6754 else
6755#endif
6756 return( MBEDTLS_SSL_TLS_PRF_NONE );
6757}
6758
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006759/*
6760 * Populate a transform structure with session keys and all the other
6761 * necessary information.
6762 *
6763 * Parameters:
6764 * - [in/out]: transform: structure to populate
6765 * [in] must be just initialised with mbedtls_ssl_transform_init()
6766 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
6767 * - [in] ciphersuite
6768 * - [in] master
6769 * - [in] encrypt_then_mac
6770 * - [in] compression
6771 * - [in] tls_prf: pointer to PRF to use for key derivation
6772 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
6773 * - [in] minor_ver: SSL/TLS minor version
6774 * - [in] endpoint: client or server
6775 * - [in] ssl: used for:
6776 * - ssl->conf->{f,p}_export_keys
6777 * [in] optionally used for:
6778 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
6779 */
6780static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
6781 int ciphersuite,
6782 const unsigned char master[48],
6783#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
6784 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6785 int encrypt_then_mac,
6786#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
6787 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
6788 ssl_tls_prf_t tls_prf,
6789 const unsigned char randbytes[64],
6790 int minor_ver,
6791 unsigned endpoint,
6792 const mbedtls_ssl_context *ssl )
6793{
6794 int ret = 0;
6795 unsigned char keyblk[256];
6796 unsigned char *key1;
6797 unsigned char *key2;
6798 unsigned char *mac_enc;
6799 unsigned char *mac_dec;
6800 size_t mac_key_len = 0;
6801 size_t iv_copy_len;
6802 size_t keylen;
6803 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
6804 const mbedtls_cipher_info_t *cipher_info;
6805 const mbedtls_md_info_t *md_info;
6806
6807#if defined(MBEDTLS_USE_PSA_CRYPTO)
6808 psa_key_type_t key_type;
6809 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6810 psa_algorithm_t alg;
6811 size_t key_bits;
6812 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
6813#endif
6814
6815#if !defined(MBEDTLS_DEBUG_C) && \
6816 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6817 if( ssl->f_export_keys == NULL )
6818 {
6819 ssl = NULL; /* make sure we don't use it except for these cases */
6820 (void) ssl;
6821 }
6822#endif
6823
6824 /*
6825 * Some data just needs copying into the structure
6826 */
6827#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
6828 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
6829 transform->encrypt_then_mac = encrypt_then_mac;
6830#endif
6831 transform->minor_ver = minor_ver;
6832
6833#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
6834 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
6835#endif
6836
6837#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
6838 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
6839 {
6840 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
6841 * generation separate. This should never happen. */
6842 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6843 }
6844#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
6845
6846 /*
6847 * Get various info structures
6848 */
6849 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
6850 if( ciphersuite_info == NULL )
6851 {
6852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
6853 ciphersuite ) );
6854 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6855 }
6856
6857 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
6858 if( cipher_info == NULL )
6859 {
6860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
6861 ciphersuite_info->cipher ) );
6862 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6863 }
6864
6865 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
6866 if( md_info == NULL )
6867 {
6868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
6869 (unsigned) ciphersuite_info->mac ) );
6870 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6871 }
6872
6873#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6874 /* Copy own and peer's CID if the use of the CID
6875 * extension has been negotiated. */
6876 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
6877 {
6878 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
6879
6880 transform->in_cid_len = ssl->own_cid_len;
6881 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
6882 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
6883 transform->in_cid_len );
6884
6885 transform->out_cid_len = ssl->handshake->peer_cid_len;
6886 memcpy( transform->out_cid, ssl->handshake->peer_cid,
6887 ssl->handshake->peer_cid_len );
6888 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
6889 transform->out_cid_len );
6890 }
6891#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6892
6893 /*
6894 * Compute key block using the PRF
6895 */
6896 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
6897 if( ret != 0 )
6898 {
6899 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
6900 return( ret );
6901 }
6902
6903 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
6904 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
6905 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
6906 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
6907 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
6908
6909 /*
6910 * Determine the appropriate key, IV and MAC length.
6911 */
6912
6913 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
6914
6915#if defined(MBEDTLS_GCM_C) || \
6916 defined(MBEDTLS_CCM_C) || \
6917 defined(MBEDTLS_CHACHAPOLY_C)
6918 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_GCM ||
6919 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CCM ||
6920 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
6921 {
6922 size_t explicit_ivlen;
6923
6924 transform->maclen = 0;
6925 mac_key_len = 0;
6926 transform->taglen =
6927 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
6928
6929 /* All modes haves 96-bit IVs, but the length of the static parts vary
6930 * with mode and version:
6931 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
6932 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
6933 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
6934 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
6935 * sequence number).
6936 */
6937 transform->ivlen = 12;
6938 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
6939 transform->fixed_ivlen = 12;
6940 else
6941 transform->fixed_ivlen = 4;
6942
6943 /* Minimum length of encrypted record */
6944 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
6945 transform->minlen = explicit_ivlen + transform->taglen;
6946 }
6947 else
6948#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
6949#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
6950 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM ||
6951 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
6952 {
Neil Armstrongcf8841a2022-02-24 11:17:45 +01006953#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006954 /* Initialize HMAC contexts */
6955 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
6956 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
6957 {
6958 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
6959 goto end;
6960 }
Neil Armstrongcf8841a2022-02-24 11:17:45 +01006961#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006962
6963 /* Get MAC length */
6964 mac_key_len = mbedtls_md_get_size( md_info );
6965 transform->maclen = mac_key_len;
6966
6967 /* IV length */
6968 transform->ivlen = cipher_info->iv_size;
6969
6970 /* Minimum length */
6971 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM )
6972 transform->minlen = transform->maclen;
6973 else
6974 {
6975 /*
6976 * GenericBlockCipher:
6977 * 1. if EtM is in use: one block plus MAC
6978 * otherwise: * first multiple of blocklen greater than maclen
6979 * 2. IV
6980 */
6981#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6982 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
6983 {
6984 transform->minlen = transform->maclen
6985 + cipher_info->block_size;
6986 }
6987 else
6988#endif
6989 {
6990 transform->minlen = transform->maclen
6991 + cipher_info->block_size
6992 - transform->maclen % cipher_info->block_size;
6993 }
6994
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006995 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
6996 {
6997 transform->minlen += transform->ivlen;
6998 }
6999 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007000 {
7001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7002 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7003 goto end;
7004 }
7005 }
7006 }
7007 else
7008#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7009 {
7010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7011 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7012 }
7013
7014 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7015 (unsigned) keylen,
7016 (unsigned) transform->minlen,
7017 (unsigned) transform->ivlen,
7018 (unsigned) transform->maclen ) );
7019
7020 /*
7021 * Finally setup the cipher contexts, IVs and MAC secrets.
7022 */
7023#if defined(MBEDTLS_SSL_CLI_C)
7024 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7025 {
7026 key1 = keyblk + mac_key_len * 2;
7027 key2 = keyblk + mac_key_len * 2 + keylen;
7028
7029 mac_enc = keyblk;
7030 mac_dec = keyblk + mac_key_len;
7031
7032 /*
7033 * This is not used in TLS v1.1.
7034 */
7035 iv_copy_len = ( transform->fixed_ivlen ) ?
7036 transform->fixed_ivlen : transform->ivlen;
7037 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7038 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7039 iv_copy_len );
7040 }
7041 else
7042#endif /* MBEDTLS_SSL_CLI_C */
7043#if defined(MBEDTLS_SSL_SRV_C)
7044 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7045 {
7046 key1 = keyblk + mac_key_len * 2 + keylen;
7047 key2 = keyblk + mac_key_len * 2;
7048
7049 mac_enc = keyblk + mac_key_len;
7050 mac_dec = keyblk;
7051
7052 /*
7053 * This is not used in TLS v1.1.
7054 */
7055 iv_copy_len = ( transform->fixed_ivlen ) ?
7056 transform->fixed_ivlen : transform->ivlen;
7057 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7058 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7059 iv_copy_len );
7060 }
7061 else
7062#endif /* MBEDTLS_SSL_SRV_C */
7063 {
7064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7065 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7066 goto end;
7067 }
7068
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007069 if( ssl != NULL && ssl->f_export_keys != NULL )
7070 {
7071 ssl->f_export_keys( ssl->p_export_keys,
7072 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7073 master, 48,
7074 randbytes + 32,
7075 randbytes,
7076 tls_prf_get_type( tls_prf ) );
7077 }
7078
7079#if defined(MBEDTLS_USE_PSA_CRYPTO)
7080 if( ( status = mbedtls_ssl_cipher_to_psa( cipher_info->type,
7081 transform->taglen,
7082 &alg,
7083 &key_type,
7084 &key_bits ) ) != PSA_SUCCESS )
7085 {
7086 ret = psa_ssl_status_to_mbedtls( status );
7087 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7088 goto end;
7089 }
7090
7091 transform->psa_alg = alg;
7092
7093 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7094 {
7095 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7096 psa_set_key_algorithm( &attributes, alg );
7097 psa_set_key_type( &attributes, key_type );
7098
7099 if( ( status = psa_import_key( &attributes,
7100 key1,
7101 PSA_BITS_TO_BYTES( key_bits ),
7102 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7103 {
7104 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7105 ret = psa_ssl_status_to_mbedtls( status );
7106 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7107 goto end;
7108 }
7109
7110 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7111
7112 if( ( status = psa_import_key( &attributes,
7113 key2,
7114 PSA_BITS_TO_BYTES( key_bits ),
7115 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7116 {
7117 ret = psa_ssl_status_to_mbedtls( status );
7118 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7119 goto end;
7120 }
7121 }
7122#else
7123 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7124 cipher_info ) ) != 0 )
7125 {
7126 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7127 goto end;
7128 }
7129
7130 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7131 cipher_info ) ) != 0 )
7132 {
7133 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7134 goto end;
7135 }
7136
7137 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7138 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7139 MBEDTLS_ENCRYPT ) ) != 0 )
7140 {
7141 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7142 goto end;
7143 }
7144
7145 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7146 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7147 MBEDTLS_DECRYPT ) ) != 0 )
7148 {
7149 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7150 goto end;
7151 }
7152
7153#if defined(MBEDTLS_CIPHER_MODE_CBC)
7154 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7155 {
7156 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7157 MBEDTLS_PADDING_NONE ) ) != 0 )
7158 {
7159 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7160 goto end;
7161 }
7162
7163 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7164 MBEDTLS_PADDING_NONE ) ) != 0 )
7165 {
7166 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7167 goto end;
7168 }
7169 }
7170#endif /* MBEDTLS_CIPHER_MODE_CBC */
7171#endif /* MBEDTLS_USE_PSA_CRYPTO */
7172
Neil Armstrong29c0c042022-03-17 17:47:28 +01007173#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7174 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7175 For AEAD-based ciphersuites, there is nothing to do here. */
7176 if( mac_key_len != 0 )
7177 {
7178#if defined(MBEDTLS_USE_PSA_CRYPTO)
7179 alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
7180 if( alg == 0 )
7181 {
7182 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7183 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_md_type_to_psa", ret );
7184 goto end;
7185 }
7186
7187 transform->psa_mac_alg = PSA_ALG_HMAC( alg );
7188
7189 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
7190 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( alg ) );
7191 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7192
7193 if( ( status = psa_import_key( &attributes,
7194 mac_enc, mac_key_len,
7195 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7196 {
7197 ret = psa_ssl_status_to_mbedtls( status );
7198 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7199 goto end;
7200 }
7201
Ronald Cronfb39f152022-03-25 14:36:28 +01007202 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
7203 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) &&
7204 ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007205 /* mbedtls_ct_hmac() requires the key to be exportable */
7206 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7207 PSA_KEY_USAGE_VERIFY_HASH );
7208 else
7209 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7210
7211 if( ( status = psa_import_key( &attributes,
7212 mac_dec, mac_key_len,
7213 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7214 {
7215 ret = psa_ssl_status_to_mbedtls( status );
7216 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7217 goto end;
7218 }
7219#else
7220 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7221 if( ret != 0 )
7222 goto end;
7223 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7224 if( ret != 0 )
7225 goto end;
7226#endif /* MBEDTLS_USE_PSA_CRYPTO */
7227 }
7228#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7229
7230 ((void) mac_dec);
7231 ((void) mac_enc);
7232
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007233end:
7234 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7235 return( ret );
7236}
7237
Jerry Yuee40f9d2022-02-17 14:55:16 +08007238#if defined(MBEDTLS_USE_PSA_CRYPTO)
7239int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7240 unsigned char *hash, size_t *hashlen,
7241 unsigned char *data, size_t data_len,
7242 mbedtls_md_type_t md_alg )
7243{
7244 psa_status_t status;
7245 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
7246 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
7247
7248 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7249
7250 if( ( status = psa_hash_setup( &hash_operation,
7251 hash_alg ) ) != PSA_SUCCESS )
7252 {
7253 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7254 goto exit;
7255 }
7256
7257 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
7258 64 ) ) != PSA_SUCCESS )
7259 {
7260 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7261 goto exit;
7262 }
7263
7264 if( ( status = psa_hash_update( &hash_operation,
7265 data, data_len ) ) != PSA_SUCCESS )
7266 {
7267 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7268 goto exit;
7269 }
7270
7271 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
7272 hashlen ) ) != PSA_SUCCESS )
7273 {
7274 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
7275 goto exit;
7276 }
7277
7278exit:
7279 if( status != PSA_SUCCESS )
7280 {
7281 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7282 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7283 switch( status )
7284 {
7285 case PSA_ERROR_NOT_SUPPORTED:
7286 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
7287 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
7288 case PSA_ERROR_BUFFER_TOO_SMALL:
7289 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
7290 case PSA_ERROR_INSUFFICIENT_MEMORY:
7291 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
7292 default:
7293 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
7294 }
7295 }
7296 return( 0 );
7297}
7298
7299#else
7300
7301int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7302 unsigned char *hash, size_t *hashlen,
7303 unsigned char *data, size_t data_len,
7304 mbedtls_md_type_t md_alg )
7305{
7306 int ret = 0;
7307 mbedtls_md_context_t ctx;
7308 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
7309 *hashlen = mbedtls_md_get_size( md_info );
7310
7311 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
7312
7313 mbedtls_md_init( &ctx );
7314
7315 /*
7316 * digitally-signed struct {
7317 * opaque client_random[32];
7318 * opaque server_random[32];
7319 * ServerDHParams params;
7320 * };
7321 */
7322 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
7323 {
7324 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7325 goto exit;
7326 }
7327 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
7328 {
7329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
7330 goto exit;
7331 }
7332 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
7333 {
7334 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7335 goto exit;
7336 }
7337 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
7338 {
7339 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7340 goto exit;
7341 }
7342 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
7343 {
7344 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
7345 goto exit;
7346 }
7347
7348exit:
7349 mbedtls_md_free( &ctx );
7350
7351 if( ret != 0 )
7352 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7353 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7354
7355 return( ret );
7356}
7357#endif /* MBEDTLS_USE_PSA_CRYPTO */
7358
Jerry Yud9d91da2022-02-17 14:57:06 +08007359#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
7360
7361/* Find an entry in a signature-hash set matching a given hash algorithm. */
7362mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
7363 mbedtls_pk_type_t sig_alg )
7364{
7365 switch( sig_alg )
7366 {
7367 case MBEDTLS_PK_RSA:
7368 return( set->rsa );
7369 case MBEDTLS_PK_ECDSA:
7370 return( set->ecdsa );
7371 default:
7372 return( MBEDTLS_MD_NONE );
7373 }
7374}
7375
7376/* Add a signature-hash-pair to a signature-hash set */
7377void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
7378 mbedtls_pk_type_t sig_alg,
7379 mbedtls_md_type_t md_alg )
7380{
7381 switch( sig_alg )
7382 {
7383 case MBEDTLS_PK_RSA:
7384 if( set->rsa == MBEDTLS_MD_NONE )
7385 set->rsa = md_alg;
7386 break;
7387
7388 case MBEDTLS_PK_ECDSA:
7389 if( set->ecdsa == MBEDTLS_MD_NONE )
7390 set->ecdsa = md_alg;
7391 break;
7392
7393 default:
7394 break;
7395 }
7396}
7397
7398/* Allow exactly one hash algorithm for each signature. */
7399void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
7400 mbedtls_md_type_t md_alg )
7401{
7402 set->rsa = md_alg;
7403 set->ecdsa = md_alg;
7404}
7405
7406#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7407
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08007408/* Serialization of TLS 1.2 sessions:
7409 *
7410 * struct {
7411 * uint64 start_time;
7412 * uint8 ciphersuite[2]; // defined by the standard
7413 * uint8 compression; // 0 or 1
7414 * uint8 session_id_len; // at most 32
7415 * opaque session_id[32];
7416 * opaque master[48]; // fixed length in the standard
7417 * uint32 verify_result;
7418 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
7419 * opaque ticket<0..2^24-1>; // length 0 means no ticket
7420 * uint32 ticket_lifetime;
7421 * uint8 mfl_code; // up to 255 according to standard
7422 * uint8 encrypt_then_mac; // 0 or 1
7423 * } serialized_session_tls12;
7424 *
7425 */
7426static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session,
7427 unsigned char *buf,
7428 size_t buf_len )
7429{
7430 unsigned char *p = buf;
7431 size_t used = 0;
7432
7433#if defined(MBEDTLS_HAVE_TIME)
7434 uint64_t start;
7435#endif
7436#if defined(MBEDTLS_X509_CRT_PARSE_C)
7437#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7438 size_t cert_len;
7439#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7440#endif /* MBEDTLS_X509_CRT_PARSE_C */
7441
7442 /*
7443 * Time
7444 */
7445#if defined(MBEDTLS_HAVE_TIME)
7446 used += 8;
7447
7448 if( used <= buf_len )
7449 {
7450 start = (uint64_t) session->start;
7451
7452 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
7453 p += 8;
7454 }
7455#endif /* MBEDTLS_HAVE_TIME */
7456
7457 /*
7458 * Basic mandatory fields
7459 */
7460 used += 2 /* ciphersuite */
7461 + 1 /* compression */
7462 + 1 /* id_len */
7463 + sizeof( session->id )
7464 + sizeof( session->master )
7465 + 4; /* verify_result */
7466
7467 if( used <= buf_len )
7468 {
7469 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
7470 p += 2;
7471
7472 *p++ = MBEDTLS_BYTE_0( session->compression );
7473
7474 *p++ = MBEDTLS_BYTE_0( session->id_len );
7475 memcpy( p, session->id, 32 );
7476 p += 32;
7477
7478 memcpy( p, session->master, 48 );
7479 p += 48;
7480
7481 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
7482 p += 4;
7483 }
7484
7485 /*
7486 * Peer's end-entity certificate
7487 */
7488#if defined(MBEDTLS_X509_CRT_PARSE_C)
7489#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7490 if( session->peer_cert == NULL )
7491 cert_len = 0;
7492 else
7493 cert_len = session->peer_cert->raw.len;
7494
7495 used += 3 + cert_len;
7496
7497 if( used <= buf_len )
7498 {
7499 *p++ = MBEDTLS_BYTE_2( cert_len );
7500 *p++ = MBEDTLS_BYTE_1( cert_len );
7501 *p++ = MBEDTLS_BYTE_0( cert_len );
7502
7503 if( session->peer_cert != NULL )
7504 {
7505 memcpy( p, session->peer_cert->raw.p, cert_len );
7506 p += cert_len;
7507 }
7508 }
7509#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7510 if( session->peer_cert_digest != NULL )
7511 {
7512 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
7513 if( used <= buf_len )
7514 {
7515 *p++ = (unsigned char) session->peer_cert_digest_type;
7516 *p++ = (unsigned char) session->peer_cert_digest_len;
7517 memcpy( p, session->peer_cert_digest,
7518 session->peer_cert_digest_len );
7519 p += session->peer_cert_digest_len;
7520 }
7521 }
7522 else
7523 {
7524 used += 2;
7525 if( used <= buf_len )
7526 {
7527 *p++ = (unsigned char) MBEDTLS_MD_NONE;
7528 *p++ = 0;
7529 }
7530 }
7531#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7532#endif /* MBEDTLS_X509_CRT_PARSE_C */
7533
7534 /*
7535 * Session ticket if any, plus associated data
7536 */
7537#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7538 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
7539
7540 if( used <= buf_len )
7541 {
7542 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
7543 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
7544 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
7545
7546 if( session->ticket != NULL )
7547 {
7548 memcpy( p, session->ticket, session->ticket_len );
7549 p += session->ticket_len;
7550 }
7551
7552 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
7553 p += 4;
7554 }
7555#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7556
7557 /*
7558 * Misc extension-related info
7559 */
7560#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7561 used += 1;
7562
7563 if( used <= buf_len )
7564 *p++ = session->mfl_code;
7565#endif
7566
7567#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7568 used += 1;
7569
7570 if( used <= buf_len )
7571 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
7572#endif
7573
7574 return( used );
7575}
7576
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08007577static int ssl_session_load_tls12( mbedtls_ssl_session *session,
7578 const unsigned char *buf,
7579 size_t len )
7580{
7581#if defined(MBEDTLS_HAVE_TIME)
7582 uint64_t start;
7583#endif
7584#if defined(MBEDTLS_X509_CRT_PARSE_C)
7585#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7586 size_t cert_len;
7587#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7588#endif /* MBEDTLS_X509_CRT_PARSE_C */
7589
7590 const unsigned char *p = buf;
7591 const unsigned char * const end = buf + len;
7592
7593 /*
7594 * Time
7595 */
7596#if defined(MBEDTLS_HAVE_TIME)
7597 if( 8 > (size_t)( end - p ) )
7598 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7599
7600 start = ( (uint64_t) p[0] << 56 ) |
7601 ( (uint64_t) p[1] << 48 ) |
7602 ( (uint64_t) p[2] << 40 ) |
7603 ( (uint64_t) p[3] << 32 ) |
7604 ( (uint64_t) p[4] << 24 ) |
7605 ( (uint64_t) p[5] << 16 ) |
7606 ( (uint64_t) p[6] << 8 ) |
7607 ( (uint64_t) p[7] );
7608 p += 8;
7609
7610 session->start = (time_t) start;
7611#endif /* MBEDTLS_HAVE_TIME */
7612
7613 /*
7614 * Basic mandatory fields
7615 */
7616 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
7617 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7618
7619 session->ciphersuite = ( p[0] << 8 ) | p[1];
7620 p += 2;
7621
7622 session->compression = *p++;
7623
7624 session->id_len = *p++;
7625 memcpy( session->id, p, 32 );
7626 p += 32;
7627
7628 memcpy( session->master, p, 48 );
7629 p += 48;
7630
7631 session->verify_result = ( (uint32_t) p[0] << 24 ) |
7632 ( (uint32_t) p[1] << 16 ) |
7633 ( (uint32_t) p[2] << 8 ) |
7634 ( (uint32_t) p[3] );
7635 p += 4;
7636
7637 /* Immediately clear invalid pointer values that have been read, in case
7638 * we exit early before we replaced them with valid ones. */
7639#if defined(MBEDTLS_X509_CRT_PARSE_C)
7640#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7641 session->peer_cert = NULL;
7642#else
7643 session->peer_cert_digest = NULL;
7644#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7645#endif /* MBEDTLS_X509_CRT_PARSE_C */
7646#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7647 session->ticket = NULL;
7648#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7649
7650 /*
7651 * Peer certificate
7652 */
7653#if defined(MBEDTLS_X509_CRT_PARSE_C)
7654#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7655 /* Deserialize CRT from the end of the ticket. */
7656 if( 3 > (size_t)( end - p ) )
7657 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7658
7659 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
7660 p += 3;
7661
7662 if( cert_len != 0 )
7663 {
7664 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7665
7666 if( cert_len > (size_t)( end - p ) )
7667 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7668
7669 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7670
7671 if( session->peer_cert == NULL )
7672 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7673
7674 mbedtls_x509_crt_init( session->peer_cert );
7675
7676 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
7677 p, cert_len ) ) != 0 )
7678 {
7679 mbedtls_x509_crt_free( session->peer_cert );
7680 mbedtls_free( session->peer_cert );
7681 session->peer_cert = NULL;
7682 return( ret );
7683 }
7684
7685 p += cert_len;
7686 }
7687#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7688 /* Deserialize CRT digest from the end of the ticket. */
7689 if( 2 > (size_t)( end - p ) )
7690 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7691
7692 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
7693 session->peer_cert_digest_len = (size_t) *p++;
7694
7695 if( session->peer_cert_digest_len != 0 )
7696 {
7697 const mbedtls_md_info_t *md_info =
7698 mbedtls_md_info_from_type( session->peer_cert_digest_type );
7699 if( md_info == NULL )
7700 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7701 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
7702 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7703
7704 if( session->peer_cert_digest_len > (size_t)( end - p ) )
7705 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7706
7707 session->peer_cert_digest =
7708 mbedtls_calloc( 1, session->peer_cert_digest_len );
7709 if( session->peer_cert_digest == NULL )
7710 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7711
7712 memcpy( session->peer_cert_digest, p,
7713 session->peer_cert_digest_len );
7714 p += session->peer_cert_digest_len;
7715 }
7716#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7717#endif /* MBEDTLS_X509_CRT_PARSE_C */
7718
7719 /*
7720 * Session ticket and associated data
7721 */
7722#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7723 if( 3 > (size_t)( end - p ) )
7724 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7725
7726 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
7727 p += 3;
7728
7729 if( session->ticket_len != 0 )
7730 {
7731 if( session->ticket_len > (size_t)( end - p ) )
7732 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7733
7734 session->ticket = mbedtls_calloc( 1, session->ticket_len );
7735 if( session->ticket == NULL )
7736 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7737
7738 memcpy( session->ticket, p, session->ticket_len );
7739 p += session->ticket_len;
7740 }
7741
7742 if( 4 > (size_t)( end - p ) )
7743 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7744
7745 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
7746 ( (uint32_t) p[1] << 16 ) |
7747 ( (uint32_t) p[2] << 8 ) |
7748 ( (uint32_t) p[3] );
7749 p += 4;
7750#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7751
7752 /*
7753 * Misc extension-related info
7754 */
7755#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7756 if( 1 > (size_t)( end - p ) )
7757 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7758
7759 session->mfl_code = *p++;
7760#endif
7761
7762#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7763 if( 1 > (size_t)( end - p ) )
7764 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7765
7766 session->encrypt_then_mac = *p++;
7767#endif
7768
7769 /* Done, should have consumed entire buffer */
7770 if( p != end )
7771 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7772
7773 return( 0 );
7774}
Jerry Yudc7bd172022-02-17 13:44:15 +08007775#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007777#endif /* MBEDTLS_SSL_TLS_C */