blob: 7c4e6fce8de27c3ba0da439bee2bb9eaad140365 [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:
4226 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
4227 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
4228 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004229#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4230 /* Hybrid TLS 1.2/1.3 is not supported yet */
4231 conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
4232#else
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004233 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004234#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3 */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004235
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004236 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004237
4238#if defined(MBEDTLS_X509_CRT_PARSE_C)
4239 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004240#endif
4241
Gilles Peskineeccd8882020-03-10 12:19:08 +01004242#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004243#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004244 conf->sig_hashes = ssl_preset_suiteb_hashes;
Jerry Yuf017ee42022-01-12 15:49:48 +08004245#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004246#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4247 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4248 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4249 else
4250#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4251 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004252#endif
4253
Brett Warrene0edc842021-08-17 09:53:13 +01004254#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4255 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004256#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004257 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004258 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004259
4260 /*
4261 * Default
4262 */
4263 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03004264 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
4265 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
4266 MBEDTLS_SSL_MIN_MAJOR_VERSION :
4267 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
4268 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;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004272 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004273#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4274 /* Hybrid TLS 1.2/1.3 is not supported yet */
4275 conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
4276#else
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004277 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004278#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3 */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004279
4280#if defined(MBEDTLS_SSL_PROTO_DTLS)
4281 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
TRodziewiczef73f012021-05-13 14:53:36 +02004282 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004283#endif
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004284 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004285
4286#if defined(MBEDTLS_X509_CRT_PARSE_C)
4287 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4288#endif
4289
Gilles Peskineeccd8882020-03-10 12:19:08 +01004290#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004291#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01004292 conf->sig_hashes = ssl_preset_default_hashes;
Jerry Yuf017ee42022-01-12 15:49:48 +08004293#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004294#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4295 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4296 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4297 else
4298#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4299 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004300#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004301
Brett Warrene0edc842021-08-17 09:53:13 +01004302#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4303 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004304#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004305 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004306
4307#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4308 conf->dhm_min_bitlen = 1024;
4309#endif
4310 }
4311
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004312 return( 0 );
4313}
4314
4315/*
4316 * Free mbedtls_ssl_config
4317 */
4318void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4319{
4320#if defined(MBEDTLS_DHM_C)
4321 mbedtls_mpi_free( &conf->dhm_P );
4322 mbedtls_mpi_free( &conf->dhm_G );
4323#endif
4324
Gilles Peskineeccd8882020-03-10 12:19:08 +01004325#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004326 if( conf->psk != NULL )
4327 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004328 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004329 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004330 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004331 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004332 }
4333
4334 if( conf->psk_identity != NULL )
4335 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004336 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004337 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004338 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004339 conf->psk_identity_len = 0;
4340 }
4341#endif
4342
4343#if defined(MBEDTLS_X509_CRT_PARSE_C)
4344 ssl_key_cert_free( conf->key_cert );
4345#endif
4346
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004347 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004348}
4349
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004350#if defined(MBEDTLS_PK_C) && \
4351 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004352/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004353 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004355unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004356{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004357#if defined(MBEDTLS_RSA_C)
4358 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4359 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004360#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004361#if defined(MBEDTLS_ECDSA_C)
4362 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4363 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004364#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004365 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004366}
4367
Hanno Becker7e5437a2017-04-28 17:15:26 +01004368unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4369{
4370 switch( type ) {
4371 case MBEDTLS_PK_RSA:
4372 return( MBEDTLS_SSL_SIG_RSA );
4373 case MBEDTLS_PK_ECDSA:
4374 case MBEDTLS_PK_ECKEY:
4375 return( MBEDTLS_SSL_SIG_ECDSA );
4376 default:
4377 return( MBEDTLS_SSL_SIG_ANON );
4378 }
4379}
4380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004381mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004382{
4383 switch( sig )
4384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004385#if defined(MBEDTLS_RSA_C)
4386 case MBEDTLS_SSL_SIG_RSA:
4387 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004388#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004389#if defined(MBEDTLS_ECDSA_C)
4390 case MBEDTLS_SSL_SIG_ECDSA:
4391 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004392#endif
4393 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004394 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004395 }
4396}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004397#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004398
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004399/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004400 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004401 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004402mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004403{
4404 switch( hash )
4405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004406#if defined(MBEDTLS_MD5_C)
4407 case MBEDTLS_SSL_HASH_MD5:
4408 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004409#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004410#if defined(MBEDTLS_SHA1_C)
4411 case MBEDTLS_SSL_HASH_SHA1:
4412 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004413#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004414#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004415 case MBEDTLS_SSL_HASH_SHA224:
4416 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004417#endif
4418#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004419 case MBEDTLS_SSL_HASH_SHA256:
4420 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004421#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004422#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004423 case MBEDTLS_SSL_HASH_SHA384:
4424 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004425#endif
4426#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004427 case MBEDTLS_SSL_HASH_SHA512:
4428 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004429#endif
4430 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004431 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004432 }
4433}
4434
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004435/*
4436 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4437 */
4438unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4439{
4440 switch( md )
4441 {
4442#if defined(MBEDTLS_MD5_C)
4443 case MBEDTLS_MD_MD5:
4444 return( MBEDTLS_SSL_HASH_MD5 );
4445#endif
4446#if defined(MBEDTLS_SHA1_C)
4447 case MBEDTLS_MD_SHA1:
4448 return( MBEDTLS_SSL_HASH_SHA1 );
4449#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004450#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004451 case MBEDTLS_MD_SHA224:
4452 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004453#endif
4454#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004455 case MBEDTLS_MD_SHA256:
4456 return( MBEDTLS_SSL_HASH_SHA256 );
4457#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004458#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004459 case MBEDTLS_MD_SHA384:
4460 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004461#endif
4462#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004463 case MBEDTLS_MD_SHA512:
4464 return( MBEDTLS_SSL_HASH_SHA512 );
4465#endif
4466 default:
4467 return( MBEDTLS_SSL_HASH_NONE );
4468 }
4469}
4470
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004471/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004472 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004473 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004474 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004475int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004476{
Brett Warrene0edc842021-08-17 09:53:13 +01004477 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004478
Brett Warrene0edc842021-08-17 09:53:13 +01004479 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004480 return( -1 );
4481
Brett Warrene0edc842021-08-17 09:53:13 +01004482 for( ; *group_list != 0; group_list++ )
4483 {
4484 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004485 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01004486 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004487
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004488 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004489}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004490
4491#if defined(MBEDTLS_ECP_C)
4492/*
4493 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
4494 */
4495int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
4496{
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01004497 uint16_t tls_id = mbedtls_ecp_curve_info_from_grp_id( grp_id )->tls_id;
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004498 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
4499}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004500#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004502#if defined(MBEDTLS_X509_CRT_PARSE_C)
4503int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
4504 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004505 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02004506 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004507{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004508 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004509 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004510 const char *ext_oid;
4511 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004513 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004514 {
4515 /* Server part of the key exchange */
4516 switch( ciphersuite->key_exchange )
4517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004518 case MBEDTLS_KEY_EXCHANGE_RSA:
4519 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004520 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004521 break;
4522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004523 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
4524 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
4525 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
4526 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004527 break;
4528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004529 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
4530 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004531 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004532 break;
4533
4534 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004535 case MBEDTLS_KEY_EXCHANGE_NONE:
4536 case MBEDTLS_KEY_EXCHANGE_PSK:
4537 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
4538 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02004539 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004540 usage = 0;
4541 }
4542 }
4543 else
4544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004545 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
4546 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004547 }
4548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004549 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004550 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004551 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004552 ret = -1;
4553 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004555 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004557 ext_oid = MBEDTLS_OID_SERVER_AUTH;
4558 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004559 }
4560 else
4561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004562 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
4563 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004564 }
4565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004566 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004567 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004568 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004569 ret = -1;
4570 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004571
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004572 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004573}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004574#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004575
Jerry Yu148165c2021-09-24 23:20:59 +08004576#if defined(MBEDTLS_USE_PSA_CRYPTO)
4577int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
4578 const mbedtls_md_type_t md,
4579 unsigned char *dst,
4580 size_t dst_len,
4581 size_t *olen )
4582{
Ronald Cronf6893e12022-01-07 22:09:01 +01004583 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4584 psa_hash_operation_t *hash_operation_to_clone;
4585 psa_hash_operation_t hash_operation = psa_hash_operation_init();
4586
Jerry Yu148165c2021-09-24 23:20:59 +08004587 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01004588
4589 switch( md )
4590 {
4591#if defined(MBEDTLS_SHA384_C)
4592 case MBEDTLS_MD_SHA384:
4593 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
4594 break;
4595#endif
4596
4597#if defined(MBEDTLS_SHA256_C)
4598 case MBEDTLS_MD_SHA256:
4599 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
4600 break;
4601#endif
4602
4603 default:
4604 goto exit;
4605 }
4606
4607 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
4608 if( status != PSA_SUCCESS )
4609 goto exit;
4610
4611 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
4612 if( status != PSA_SUCCESS )
4613 goto exit;
4614
4615exit:
Ronald Cronb788c042022-02-15 09:14:15 +01004616 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08004617}
4618#else /* MBEDTLS_USE_PSA_CRYPTO */
4619
Jerry Yu24c0ec32021-09-09 14:21:07 +08004620#if defined(MBEDTLS_SHA384_C)
Jerry Yu000f9762021-09-14 11:12:51 +08004621static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
4622 unsigned char *dst,
4623 size_t dst_len,
4624 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004625{
Jerry Yu24c0ec32021-09-09 14:21:07 +08004626 int ret;
4627 mbedtls_sha512_context sha512;
4628
4629 if( dst_len < 48 )
4630 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4631
4632 mbedtls_sha512_init( &sha512 );
4633 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
4634
4635 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
4636 {
4637 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
4638 goto exit;
4639 }
4640
4641 *olen = 48;
4642
4643exit:
4644
4645 mbedtls_sha512_free( &sha512 );
4646 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004647}
4648#endif /* MBEDTLS_SHA384_C */
4649
4650#if defined(MBEDTLS_SHA256_C)
Jerry Yu000f9762021-09-14 11:12:51 +08004651static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
4652 unsigned char *dst,
4653 size_t dst_len,
4654 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004655{
Jerry Yu24c0ec32021-09-09 14:21:07 +08004656 int ret;
4657 mbedtls_sha256_context sha256;
4658
4659 if( dst_len < 32 )
4660 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4661
4662 mbedtls_sha256_init( &sha256 );
4663 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08004664
Jerry Yu24c0ec32021-09-09 14:21:07 +08004665 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
4666 {
4667 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
4668 goto exit;
4669 }
4670
4671 *olen = 32;
4672
4673exit:
4674
4675 mbedtls_sha256_free( &sha256 );
4676 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004677}
4678#endif /* MBEDTLS_SHA256_C */
4679
Jerry Yu000f9762021-09-14 11:12:51 +08004680int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
4681 const mbedtls_md_type_t md,
4682 unsigned char *dst,
4683 size_t dst_len,
4684 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004685{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004686 switch( md )
4687 {
4688
Jerry Yu24c0ec32021-09-09 14:21:07 +08004689#if defined(MBEDTLS_SHA384_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004690 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08004691 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08004692#endif /* MBEDTLS_SHA384_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004693
Jerry Yu24c0ec32021-09-09 14:21:07 +08004694#if defined(MBEDTLS_SHA256_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004695 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08004696 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08004697#endif /* MBEDTLS_SHA256_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004698
4699 default:
4700 break;
4701 }
Jerry Yuc5aef882021-12-23 20:15:02 +08004702 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004703}
XiaokangQian647719a2021-12-07 09:16:29 +00004704
Jerry Yu148165c2021-09-24 23:20:59 +08004705#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08004706
Jerry Yudc7bd172022-02-17 13:44:15 +08004707#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4708
4709#if defined(MBEDTLS_USE_PSA_CRYPTO)
4710
4711static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
4712 mbedtls_svc_key_id_t key,
4713 psa_algorithm_t alg,
4714 const unsigned char* seed, size_t seed_length,
4715 const unsigned char* label, size_t label_length,
4716 size_t capacity )
4717{
4718 psa_status_t status;
4719
4720 status = psa_key_derivation_setup( derivation, alg );
4721 if( status != PSA_SUCCESS )
4722 return( status );
4723
4724 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4725 {
4726 status = psa_key_derivation_input_bytes( derivation,
4727 PSA_KEY_DERIVATION_INPUT_SEED,
4728 seed, seed_length );
4729 if( status != PSA_SUCCESS )
4730 return( status );
4731
4732 if( mbedtls_svc_key_id_is_null( key ) )
4733 {
4734 status = psa_key_derivation_input_bytes(
4735 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
4736 NULL, 0 );
4737 }
4738 else
4739 {
4740 status = psa_key_derivation_input_key(
4741 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
4742 }
4743 if( status != PSA_SUCCESS )
4744 return( status );
4745
4746 status = psa_key_derivation_input_bytes( derivation,
4747 PSA_KEY_DERIVATION_INPUT_LABEL,
4748 label, label_length );
4749 if( status != PSA_SUCCESS )
4750 return( status );
4751 }
4752 else
4753 {
4754 return( PSA_ERROR_NOT_SUPPORTED );
4755 }
4756
4757 status = psa_key_derivation_set_capacity( derivation, capacity );
4758 if( status != PSA_SUCCESS )
4759 return( status );
4760
4761 return( PSA_SUCCESS );
4762}
4763
4764static int tls_prf_generic( mbedtls_md_type_t md_type,
4765 const unsigned char *secret, size_t slen,
4766 const char *label,
4767 const unsigned char *random, size_t rlen,
4768 unsigned char *dstbuf, size_t dlen )
4769{
4770 psa_status_t status;
4771 psa_algorithm_t alg;
4772 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
4773 psa_key_derivation_operation_t derivation =
4774 PSA_KEY_DERIVATION_OPERATION_INIT;
4775
4776 if( md_type == MBEDTLS_MD_SHA384 )
4777 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
4778 else
4779 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
4780
4781 /* Normally a "secret" should be long enough to be impossible to
4782 * find by brute force, and in particular should not be empty. But
4783 * this PRF is also used to derive an IV, in particular in EAP-TLS,
4784 * and for this use case it makes sense to have a 0-length "secret".
4785 * Since the key API doesn't allow importing a key of length 0,
4786 * keep master_key=0, which setup_psa_key_derivation() understands
4787 * to mean a 0-length "secret" input. */
4788 if( slen != 0 )
4789 {
4790 psa_key_attributes_t key_attributes = psa_key_attributes_init();
4791 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
4792 psa_set_key_algorithm( &key_attributes, alg );
4793 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
4794
4795 status = psa_import_key( &key_attributes, secret, slen, &master_key );
4796 if( status != PSA_SUCCESS )
4797 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4798 }
4799
4800 status = setup_psa_key_derivation( &derivation,
4801 master_key, alg,
4802 random, rlen,
4803 (unsigned char const *) label,
4804 (size_t) strlen( label ),
4805 dlen );
4806 if( status != PSA_SUCCESS )
4807 {
4808 psa_key_derivation_abort( &derivation );
4809 psa_destroy_key( master_key );
4810 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4811 }
4812
4813 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
4814 if( status != PSA_SUCCESS )
4815 {
4816 psa_key_derivation_abort( &derivation );
4817 psa_destroy_key( master_key );
4818 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4819 }
4820
4821 status = psa_key_derivation_abort( &derivation );
4822 if( status != PSA_SUCCESS )
4823 {
4824 psa_destroy_key( master_key );
4825 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4826 }
4827
4828 if( ! mbedtls_svc_key_id_is_null( master_key ) )
4829 status = psa_destroy_key( master_key );
4830 if( status != PSA_SUCCESS )
4831 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4832
4833 return( 0 );
4834}
4835
4836#else /* MBEDTLS_USE_PSA_CRYPTO */
4837
4838static int tls_prf_generic( mbedtls_md_type_t md_type,
4839 const unsigned char *secret, size_t slen,
4840 const char *label,
4841 const unsigned char *random, size_t rlen,
4842 unsigned char *dstbuf, size_t dlen )
4843{
4844 size_t nb;
4845 size_t i, j, k, md_len;
4846 unsigned char *tmp;
4847 size_t tmp_len = 0;
4848 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
4849 const mbedtls_md_info_t *md_info;
4850 mbedtls_md_context_t md_ctx;
4851 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4852
4853 mbedtls_md_init( &md_ctx );
4854
4855 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
4856 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4857
4858 md_len = mbedtls_md_get_size( md_info );
4859
4860 tmp_len = md_len + strlen( label ) + rlen;
4861 tmp = mbedtls_calloc( 1, tmp_len );
4862 if( tmp == NULL )
4863 {
4864 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
4865 goto exit;
4866 }
4867
4868 nb = strlen( label );
4869 memcpy( tmp + md_len, label, nb );
4870 memcpy( tmp + md_len + nb, random, rlen );
4871 nb += rlen;
4872
4873 /*
4874 * Compute P_<hash>(secret, label + random)[0..dlen]
4875 */
4876 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
4877 goto exit;
4878
4879 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
4880 if( ret != 0 )
4881 goto exit;
4882 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
4883 if( ret != 0 )
4884 goto exit;
4885 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
4886 if( ret != 0 )
4887 goto exit;
4888
4889 for( i = 0; i < dlen; i += md_len )
4890 {
4891 ret = mbedtls_md_hmac_reset ( &md_ctx );
4892 if( ret != 0 )
4893 goto exit;
4894 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
4895 if( ret != 0 )
4896 goto exit;
4897 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
4898 if( ret != 0 )
4899 goto exit;
4900
4901 ret = mbedtls_md_hmac_reset ( &md_ctx );
4902 if( ret != 0 )
4903 goto exit;
4904 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
4905 if( ret != 0 )
4906 goto exit;
4907 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
4908 if( ret != 0 )
4909 goto exit;
4910
4911 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
4912
4913 for( j = 0; j < k; j++ )
4914 dstbuf[i + j] = h_i[j];
4915 }
4916
4917exit:
4918 mbedtls_md_free( &md_ctx );
4919
4920 mbedtls_platform_zeroize( tmp, tmp_len );
4921 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
4922
4923 mbedtls_free( tmp );
4924
4925 return( ret );
4926}
4927#endif /* MBEDTLS_USE_PSA_CRYPTO */
4928
4929#if defined(MBEDTLS_SHA256_C)
4930static int tls_prf_sha256( const unsigned char *secret, size_t slen,
4931 const char *label,
4932 const unsigned char *random, size_t rlen,
4933 unsigned char *dstbuf, size_t dlen )
4934{
4935 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
4936 label, random, rlen, dstbuf, dlen ) );
4937}
4938#endif /* MBEDTLS_SHA256_C */
4939
4940#if defined(MBEDTLS_SHA384_C)
4941static int tls_prf_sha384( const unsigned char *secret, size_t slen,
4942 const char *label,
4943 const unsigned char *random, size_t rlen,
4944 unsigned char *dstbuf, size_t dlen )
4945{
4946 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
4947 label, random, rlen, dstbuf, dlen ) );
4948}
4949#endif /* MBEDTLS_SHA384_C */
4950
Jerry Yuf009d862022-02-17 14:01:37 +08004951/*
4952 * Set appropriate PRF function and other SSL / TLS1.2 functions
4953 *
4954 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08004955 * - hash associated with the ciphersuite (only used by TLS 1.2)
4956 *
4957 * Outputs:
4958 * - the tls_prf, calc_verify and calc_finished members of handshake structure
4959 */
4960static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08004961 mbedtls_md_type_t hash )
4962{
Jerry Yuf009d862022-02-17 14:01:37 +08004963#if defined(MBEDTLS_SHA384_C)
Ronald Cron81591aa2022-03-07 09:05:51 +01004964 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08004965 {
4966 handshake->tls_prf = tls_prf_sha384;
4967 handshake->calc_verify = ssl_calc_verify_tls_sha384;
4968 handshake->calc_finished = ssl_calc_finished_tls_sha384;
4969 }
4970 else
4971#endif
4972#if defined(MBEDTLS_SHA256_C)
Jerry Yuf009d862022-02-17 14:01:37 +08004973 {
Ronald Cron81591aa2022-03-07 09:05:51 +01004974 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08004975 handshake->tls_prf = tls_prf_sha256;
4976 handshake->calc_verify = ssl_calc_verify_tls_sha256;
4977 handshake->calc_finished = ssl_calc_finished_tls_sha256;
4978 }
Ronald Cron81591aa2022-03-07 09:05:51 +01004979#else
Jerry Yuf009d862022-02-17 14:01:37 +08004980 {
Jerry Yuf009d862022-02-17 14:01:37 +08004981 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01004982 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08004983 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4984 }
Ronald Cron81591aa2022-03-07 09:05:51 +01004985#endif
Jerry Yuf009d862022-02-17 14:01:37 +08004986
4987 return( 0 );
4988}
Jerry Yud6ab2352022-02-17 14:03:43 +08004989
4990#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
4991 defined(MBEDTLS_USE_PSA_CRYPTO)
4992static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
4993{
4994 if( ssl->conf->f_psk != NULL )
4995 {
4996 /* If we've used a callback to select the PSK,
4997 * the static configuration is irrelevant. */
4998 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
4999 return( 1 );
5000
5001 return( 0 );
5002 }
5003
5004 if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) )
5005 return( 1 );
5006
5007 return( 0 );
5008}
5009#endif /* MBEDTLS_USE_PSA_CRYPTO &&
5010 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5011
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005012/*
5013 * Compute master secret if needed
5014 *
5015 * Parameters:
5016 * [in/out] handshake
5017 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5018 * (PSA-PSK) ciphersuite_info, psk_opaque
5019 * [out] premaster (cleared)
5020 * [out] master
5021 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5022 * debug: conf->f_dbg, conf->p_dbg
5023 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005024 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005025 */
5026static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5027 unsigned char *master,
5028 const mbedtls_ssl_context *ssl )
5029{
5030 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5031
5032 /* cf. RFC 5246, Section 8.1:
5033 * "The master secret is always exactly 48 bytes in length." */
5034 size_t const master_secret_len = 48;
5035
5036#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5037 unsigned char session_hash[48];
5038#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5039
5040 /* The label for the KDF used for key expansion.
5041 * This is either "master secret" or "extended master secret"
5042 * depending on whether the Extended Master Secret extension
5043 * is used. */
5044 char const *lbl = "master secret";
5045
5046 /* The salt for the KDF used for key expansion.
5047 * - If the Extended Master Secret extension is not used,
5048 * this is ClientHello.Random + ServerHello.Random
5049 * (see Sect. 8.1 in RFC 5246).
5050 * - If the Extended Master Secret extension is used,
5051 * this is the transcript of the handshake so far.
5052 * (see Sect. 4 in RFC 7627). */
5053 unsigned char const *salt = handshake->randbytes;
5054 size_t salt_len = 64;
5055
5056#if !defined(MBEDTLS_DEBUG_C) && \
5057 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5058 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5059 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5060 ssl = NULL; /* make sure we don't use it except for those cases */
5061 (void) ssl;
5062#endif
5063
5064 if( handshake->resume != 0 )
5065 {
5066 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5067 return( 0 );
5068 }
5069
5070#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5071 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5072 {
5073 lbl = "extended master secret";
5074 salt = session_hash;
5075 handshake->calc_verify( ssl, session_hash, &salt_len );
5076
5077 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
5078 session_hash, salt_len );
5079 }
5080#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
5081
5082#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
5083 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5084 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005085 ssl_use_opaque_psk( ssl ) == 1 )
5086 {
5087 /* Perform PSK-to-MS expansion in a single step. */
5088 psa_status_t status;
5089 psa_algorithm_t alg;
5090 mbedtls_svc_key_id_t psk;
5091 psa_key_derivation_operation_t derivation =
5092 PSA_KEY_DERIVATION_OPERATION_INIT;
5093 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5094
5095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5096
5097 psk = mbedtls_ssl_get_opaque_psk( ssl );
5098
5099 if( hash_alg == MBEDTLS_MD_SHA384 )
5100 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5101 else
5102 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5103
5104 status = setup_psa_key_derivation( &derivation, psk, alg,
5105 salt, salt_len,
5106 (unsigned char const *) lbl,
5107 (size_t) strlen( lbl ),
5108 master_secret_len );
5109 if( status != PSA_SUCCESS )
5110 {
5111 psa_key_derivation_abort( &derivation );
5112 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5113 }
5114
5115 status = psa_key_derivation_output_bytes( &derivation,
5116 master,
5117 master_secret_len );
5118 if( status != PSA_SUCCESS )
5119 {
5120 psa_key_derivation_abort( &derivation );
5121 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5122 }
5123
5124 status = psa_key_derivation_abort( &derivation );
5125 if( status != PSA_SUCCESS )
5126 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5127 }
5128 else
5129#endif
5130 {
5131 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
5132 lbl, salt, salt_len,
5133 master,
5134 master_secret_len );
5135 if( ret != 0 )
5136 {
5137 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5138 return( ret );
5139 }
5140
5141 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5142 handshake->premaster,
5143 handshake->pmslen );
5144
5145 mbedtls_platform_zeroize( handshake->premaster,
5146 sizeof(handshake->premaster) );
5147 }
5148
5149 return( 0 );
5150}
5151
Jerry Yud62f87e2022-02-17 14:09:02 +08005152int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5153{
5154 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5155 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5156 ssl->handshake->ciphersuite_info;
5157
5158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5159
5160 /* Set PRF, calc_verify and calc_finished function pointers */
5161 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005162 ciphersuite_info->mac );
5163 if( ret != 0 )
5164 {
5165 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5166 return( ret );
5167 }
5168
5169 /* Compute master secret if needed */
5170 ret = ssl_compute_master( ssl->handshake,
5171 ssl->session_negotiate->master,
5172 ssl );
5173 if( ret != 0 )
5174 {
5175 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5176 return( ret );
5177 }
5178
5179 /* Swap the client and server random values:
5180 * - MS derivation wanted client+server (RFC 5246 8.1)
5181 * - key derivation wants server+client (RFC 5246 6.3) */
5182 {
5183 unsigned char tmp[64];
5184 memcpy( tmp, ssl->handshake->randbytes, 64 );
5185 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5186 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5187 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5188 }
5189
5190 /* Populate transform structure */
5191 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5192 ssl->session_negotiate->ciphersuite,
5193 ssl->session_negotiate->master,
5194#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
5195 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5196 ssl->session_negotiate->encrypt_then_mac,
5197#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
5198 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
5199 ssl->handshake->tls_prf,
5200 ssl->handshake->randbytes,
5201 ssl->minor_ver,
5202 ssl->conf->endpoint,
5203 ssl );
5204 if( ret != 0 )
5205 {
5206 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5207 return( ret );
5208 }
5209
5210 /* We no longer need Server/ClientHello.random values */
5211 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5212 sizeof( ssl->handshake->randbytes ) );
5213
5214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5215
5216 return( 0 );
5217}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005218
Ronald Cron4dcbca92022-03-07 10:21:40 +01005219int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5220{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005221 switch( md )
5222 {
5223#if defined(MBEDTLS_SHA384_C)
5224 case MBEDTLS_SSL_HASH_SHA384:
5225 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5226 break;
5227#endif
5228#if defined(MBEDTLS_SHA256_C)
5229 case MBEDTLS_SSL_HASH_SHA256:
5230 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5231 break;
5232#endif
5233 default:
5234 return( -1 );
5235 }
5236
Ronald Cronc2f13a02022-03-07 10:25:24 +01005237 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005238}
5239
Jerry Yu8392e0d2022-02-17 14:10:24 +08005240#if defined(MBEDTLS_SHA256_C)
5241void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5242 unsigned char *hash,
5243 size_t *hlen )
5244{
5245#if defined(MBEDTLS_USE_PSA_CRYPTO)
5246 size_t hash_size;
5247 psa_status_t status;
5248 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5249
5250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5251 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5252 if( status != PSA_SUCCESS )
5253 {
5254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5255 return;
5256 }
5257
5258 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5259 if( status != PSA_SUCCESS )
5260 {
5261 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5262 return;
5263 }
5264
5265 *hlen = 32;
5266 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5268#else
5269 mbedtls_sha256_context sha256;
5270
5271 mbedtls_sha256_init( &sha256 );
5272
5273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5274
5275 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5276 mbedtls_sha256_finish( &sha256, hash );
5277
5278 *hlen = 32;
5279
5280 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5282
5283 mbedtls_sha256_free( &sha256 );
5284#endif /* MBEDTLS_USE_PSA_CRYPTO */
5285 return;
5286}
5287#endif /* MBEDTLS_SHA256_C */
5288
Jerry Yuc1cb3842022-02-17 14:13:48 +08005289#if defined(MBEDTLS_SHA384_C)
5290void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5291 unsigned char *hash,
5292 size_t *hlen )
5293{
5294#if defined(MBEDTLS_USE_PSA_CRYPTO)
5295 size_t hash_size;
5296 psa_status_t status;
5297 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5298
5299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5300 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5301 if( status != PSA_SUCCESS )
5302 {
5303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5304 return;
5305 }
5306
5307 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5308 if( status != PSA_SUCCESS )
5309 {
5310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5311 return;
5312 }
5313
5314 *hlen = 48;
5315 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5317#else
5318 mbedtls_sha512_context sha512;
5319
5320 mbedtls_sha512_init( &sha512 );
5321
5322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
5323
5324 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
5325 mbedtls_sha512_finish( &sha512, hash );
5326
5327 *hlen = 48;
5328
5329 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5330 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5331
5332 mbedtls_sha512_free( &sha512 );
5333#endif /* MBEDTLS_USE_PSA_CRYPTO */
5334 return;
5335}
5336#endif /* MBEDTLS_SHA384_C */
5337
Jerry Yuce3dca42022-02-17 14:16:37 +08005338#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
5339int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
5340{
5341 unsigned char *p = ssl->handshake->premaster;
5342 unsigned char *end = p + sizeof( ssl->handshake->premaster );
5343 const unsigned char *psk = NULL;
5344 size_t psk_len = 0;
5345
5346 if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len )
5347 == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
5348 {
5349 /*
5350 * This should never happen because the existence of a PSK is always
5351 * checked before calling this function
5352 */
5353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5354 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5355 }
5356
5357 /*
5358 * PMS = struct {
5359 * opaque other_secret<0..2^16-1>;
5360 * opaque psk<0..2^16-1>;
5361 * };
5362 * with "other_secret" depending on the particular key exchange
5363 */
5364#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5365 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
5366 {
5367 if( end - p < 2 )
5368 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5369
5370 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5371 p += 2;
5372
5373 if( end < p || (size_t)( end - p ) < psk_len )
5374 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5375
5376 memset( p, 0, psk_len );
5377 p += psk_len;
5378 }
5379 else
5380#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5381#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
5382 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5383 {
5384 /*
5385 * other_secret already set by the ClientKeyExchange message,
5386 * and is 48 bytes long
5387 */
5388 if( end - p < 2 )
5389 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5390
5391 *p++ = 0;
5392 *p++ = 48;
5393 p += 48;
5394 }
5395 else
5396#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
5397#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
5398 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5399 {
5400 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5401 size_t len;
5402
5403 /* Write length only when we know the actual value */
5404 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
5405 p + 2, end - ( p + 2 ), &len,
5406 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5407 {
5408 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
5409 return( ret );
5410 }
5411 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
5412 p += 2 + len;
5413
5414 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
5415 }
5416 else
5417#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
5418#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
5419 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
5420 {
5421 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5422 size_t zlen;
5423
5424 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
5425 p + 2, end - ( p + 2 ),
5426 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5427 {
5428 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
5429 return( ret );
5430 }
5431
5432 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
5433 p += 2 + zlen;
5434
5435 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
5436 MBEDTLS_DEBUG_ECDH_Z );
5437 }
5438 else
5439#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
5440 {
5441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5442 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5443 }
5444
5445 /* opaque psk<0..2^16-1>; */
5446 if( end - p < 2 )
5447 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5448
5449 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5450 p += 2;
5451
5452 if( end < p || (size_t)( end - p ) < psk_len )
5453 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5454
5455 memcpy( p, psk, psk_len );
5456 p += psk_len;
5457
5458 ssl->handshake->pmslen = p - ssl->handshake->premaster;
5459
5460 return( 0 );
5461}
5462#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08005463
5464#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
5465static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
5466
5467#if defined(MBEDTLS_SSL_PROTO_DTLS)
5468int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
5469{
5470 /* If renegotiation is not enforced, retransmit until we would reach max
5471 * timeout if we were using the usual handshake doubling scheme */
5472 if( ssl->conf->renego_max_records < 0 )
5473 {
5474 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
5475 unsigned char doublings = 1;
5476
5477 while( ratio != 0 )
5478 {
5479 ++doublings;
5480 ratio >>= 1;
5481 }
5482
5483 if( ++ssl->renego_records_seen > doublings )
5484 {
5485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
5486 return( 0 );
5487 }
5488 }
5489
5490 return( ssl_write_hello_request( ssl ) );
5491}
5492#endif
5493#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08005494
Jerry Yud9526692022-02-17 14:23:47 +08005495/*
5496 * Handshake functions
5497 */
5498#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5499/* No certificate support -> dummy functions */
5500int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
5501{
5502 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5503 ssl->handshake->ciphersuite_info;
5504
5505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
5506
5507 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5508 {
5509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5510 ssl->state++;
5511 return( 0 );
5512 }
5513
5514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5515 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5516}
5517
5518int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5519{
5520 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5521 ssl->handshake->ciphersuite_info;
5522
5523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5524
5525 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5526 {
5527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5528 ssl->state++;
5529 return( 0 );
5530 }
5531
5532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5533 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5534}
5535
5536#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5537/* Some certificate support -> implement write and parse */
5538
5539int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
5540{
5541 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
5542 size_t i, n;
5543 const mbedtls_x509_crt *crt;
5544 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5545 ssl->handshake->ciphersuite_info;
5546
5547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
5548
5549 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5550 {
5551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5552 ssl->state++;
5553 return( 0 );
5554 }
5555
5556#if defined(MBEDTLS_SSL_CLI_C)
5557 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5558 {
5559 if( ssl->handshake->client_auth == 0 )
5560 {
5561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5562 ssl->state++;
5563 return( 0 );
5564 }
5565 }
5566#endif /* MBEDTLS_SSL_CLI_C */
5567#if defined(MBEDTLS_SSL_SRV_C)
5568 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
5569 {
5570 if( mbedtls_ssl_own_cert( ssl ) == NULL )
5571 {
5572 /* Should never happen because we shouldn't have picked the
5573 * ciphersuite if we don't have a certificate. */
5574 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5575 }
5576 }
5577#endif
5578
5579 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
5580
5581 /*
5582 * 0 . 0 handshake type
5583 * 1 . 3 handshake length
5584 * 4 . 6 length of all certs
5585 * 7 . 9 length of cert. 1
5586 * 10 . n-1 peer certificate
5587 * n . n+2 length of cert. 2
5588 * n+3 . ... upper level cert, etc.
5589 */
5590 i = 7;
5591 crt = mbedtls_ssl_own_cert( ssl );
5592
5593 while( crt != NULL )
5594 {
5595 n = crt->raw.len;
5596 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
5597 {
5598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
5599 " > %" MBEDTLS_PRINTF_SIZET,
5600 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
5601 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5602 }
5603
5604 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
5605 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
5606 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
5607
5608 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5609 i += n; crt = crt->next;
5610 }
5611
5612 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
5613 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
5614 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
5615
5616 ssl->out_msglen = i;
5617 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5618 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
5619
5620 ssl->state++;
5621
5622 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
5623 {
5624 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
5625 return( ret );
5626 }
5627
5628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
5629
5630 return( ret );
5631}
5632
5633#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5634
5635#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5636static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5637 unsigned char *crt_buf,
5638 size_t crt_buf_len )
5639{
5640 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5641
5642 if( peer_crt == NULL )
5643 return( -1 );
5644
5645 if( peer_crt->raw.len != crt_buf_len )
5646 return( -1 );
5647
5648 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
5649}
5650#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5651static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5652 unsigned char *crt_buf,
5653 size_t crt_buf_len )
5654{
5655 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5656 unsigned char const * const peer_cert_digest =
5657 ssl->session->peer_cert_digest;
5658 mbedtls_md_type_t const peer_cert_digest_type =
5659 ssl->session->peer_cert_digest_type;
5660 mbedtls_md_info_t const * const digest_info =
5661 mbedtls_md_info_from_type( peer_cert_digest_type );
5662 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
5663 size_t digest_len;
5664
5665 if( peer_cert_digest == NULL || digest_info == NULL )
5666 return( -1 );
5667
5668 digest_len = mbedtls_md_get_size( digest_info );
5669 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
5670 return( -1 );
5671
5672 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
5673 if( ret != 0 )
5674 return( -1 );
5675
5676 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
5677}
5678#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5679#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5680
5681/*
5682 * Once the certificate message is read, parse it into a cert chain and
5683 * perform basic checks, but leave actual verification to the caller
5684 */
5685static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5686 mbedtls_x509_crt *chain )
5687{
5688 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5689#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5690 int crt_cnt=0;
5691#endif
5692 size_t i, n;
5693 uint8_t alert;
5694
5695 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5696 {
5697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5698 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5699 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5700 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5701 }
5702
5703 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
5704 {
5705 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5706 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5707 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5708 }
5709
5710 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
5711 {
5712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5713 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5714 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5715 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5716 }
5717
5718 i = mbedtls_ssl_hs_hdr_len( ssl );
5719
5720 /*
5721 * Same message structure as in mbedtls_ssl_write_certificate()
5722 */
5723 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
5724
5725 if( ssl->in_msg[i] != 0 ||
5726 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
5727 {
5728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5729 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5730 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5731 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5732 }
5733
5734 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5735 i += 3;
5736
5737 /* Iterate through and parse the CRTs in the provided chain. */
5738 while( i < ssl->in_hslen )
5739 {
5740 /* Check that there's room for the next CRT's length fields. */
5741 if ( i + 3 > ssl->in_hslen ) {
5742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5743 mbedtls_ssl_send_alert_message( ssl,
5744 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5745 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5746 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5747 }
5748 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
5749 * anything beyond 2**16 ~ 64K. */
5750 if( ssl->in_msg[i] != 0 )
5751 {
5752 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5753 mbedtls_ssl_send_alert_message( ssl,
5754 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5755 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
5756 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
5757 }
5758
5759 /* Read length of the next CRT in the chain. */
5760 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5761 | (unsigned int) ssl->in_msg[i + 2];
5762 i += 3;
5763
5764 if( n < 128 || i + n > ssl->in_hslen )
5765 {
5766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5767 mbedtls_ssl_send_alert_message( ssl,
5768 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5769 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5770 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5771 }
5772
5773 /* Check if we're handling the first CRT in the chain. */
5774#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5775 if( crt_cnt++ == 0 &&
5776 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5777 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
5778 {
5779 /* During client-side renegotiation, check that the server's
5780 * end-CRTs hasn't changed compared to the initial handshake,
5781 * mitigating the triple handshake attack. On success, reuse
5782 * the original end-CRT instead of parsing it again. */
5783 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
5784 if( ssl_check_peer_crt_unchanged( ssl,
5785 &ssl->in_msg[i],
5786 n ) != 0 )
5787 {
5788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
5789 mbedtls_ssl_send_alert_message( ssl,
5790 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5791 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
5792 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
5793 }
5794
5795 /* Now we can safely free the original chain. */
5796 ssl_clear_peer_cert( ssl->session );
5797 }
5798#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5799
5800 /* Parse the next certificate in the chain. */
5801#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5802 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
5803#else
5804 /* If we don't need to store the CRT chain permanently, parse
5805 * it in-place from the input buffer instead of making a copy. */
5806 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
5807#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5808 switch( ret )
5809 {
5810 case 0: /*ok*/
5811 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5812 /* Ignore certificate with an unknown algorithm: maybe a
5813 prior certificate was already trusted. */
5814 break;
5815
5816 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5817 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5818 goto crt_parse_der_failed;
5819
5820 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5821 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5822 goto crt_parse_der_failed;
5823
5824 default:
5825 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5826 crt_parse_der_failed:
5827 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
5828 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
5829 return( ret );
5830 }
5831
5832 i += n;
5833 }
5834
5835 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
5836 return( 0 );
5837}
5838
5839#if defined(MBEDTLS_SSL_SRV_C)
5840static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
5841{
5842 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5843 return( -1 );
5844
5845 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5846 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5847 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5848 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
5849 {
5850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
5851 return( 0 );
5852 }
5853 return( -1 );
5854}
5855#endif /* MBEDTLS_SSL_SRV_C */
5856
5857/* Check if a certificate message is expected.
5858 * Return either
5859 * - SSL_CERTIFICATE_EXPECTED, or
5860 * - SSL_CERTIFICATE_SKIP
5861 * indicating whether a Certificate message is expected or not.
5862 */
5863#define SSL_CERTIFICATE_EXPECTED 0
5864#define SSL_CERTIFICATE_SKIP 1
5865static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
5866 int authmode )
5867{
5868 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5869 ssl->handshake->ciphersuite_info;
5870
5871 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5872 return( SSL_CERTIFICATE_SKIP );
5873
5874#if defined(MBEDTLS_SSL_SRV_C)
5875 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
5876 {
5877 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5878 return( SSL_CERTIFICATE_SKIP );
5879
5880 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
5881 {
5882 ssl->session_negotiate->verify_result =
5883 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5884 return( SSL_CERTIFICATE_SKIP );
5885 }
5886 }
5887#else
5888 ((void) authmode);
5889#endif /* MBEDTLS_SSL_SRV_C */
5890
5891 return( SSL_CERTIFICATE_EXPECTED );
5892}
5893
5894static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
5895 int authmode,
5896 mbedtls_x509_crt *chain,
5897 void *rs_ctx )
5898{
5899 int ret = 0;
5900 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5901 ssl->handshake->ciphersuite_info;
5902 int have_ca_chain = 0;
5903
5904 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
5905 void *p_vrfy;
5906
5907 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
5908 return( 0 );
5909
5910 if( ssl->f_vrfy != NULL )
5911 {
5912 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
5913 f_vrfy = ssl->f_vrfy;
5914 p_vrfy = ssl->p_vrfy;
5915 }
5916 else
5917 {
5918 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
5919 f_vrfy = ssl->conf->f_vrfy;
5920 p_vrfy = ssl->conf->p_vrfy;
5921 }
5922
5923 /*
5924 * Main check: verify certificate
5925 */
5926#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
5927 if( ssl->conf->f_ca_cb != NULL )
5928 {
5929 ((void) rs_ctx);
5930 have_ca_chain = 1;
5931
5932 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
5933 ret = mbedtls_x509_crt_verify_with_ca_cb(
5934 chain,
5935 ssl->conf->f_ca_cb,
5936 ssl->conf->p_ca_cb,
5937 ssl->conf->cert_profile,
5938 ssl->hostname,
5939 &ssl->session_negotiate->verify_result,
5940 f_vrfy, p_vrfy );
5941 }
5942 else
5943#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
5944 {
5945 mbedtls_x509_crt *ca_chain;
5946 mbedtls_x509_crl *ca_crl;
5947
5948#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5949 if( ssl->handshake->sni_ca_chain != NULL )
5950 {
5951 ca_chain = ssl->handshake->sni_ca_chain;
5952 ca_crl = ssl->handshake->sni_ca_crl;
5953 }
5954 else
5955#endif
5956 {
5957 ca_chain = ssl->conf->ca_chain;
5958 ca_crl = ssl->conf->ca_crl;
5959 }
5960
5961 if( ca_chain != NULL )
5962 have_ca_chain = 1;
5963
5964 ret = mbedtls_x509_crt_verify_restartable(
5965 chain,
5966 ca_chain, ca_crl,
5967 ssl->conf->cert_profile,
5968 ssl->hostname,
5969 &ssl->session_negotiate->verify_result,
5970 f_vrfy, p_vrfy, rs_ctx );
5971 }
5972
5973 if( ret != 0 )
5974 {
5975 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
5976 }
5977
5978#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
5979 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
5980 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
5981#endif
5982
5983 /*
5984 * Secondary checks: always done, but change 'ret' only if it was 0
5985 */
5986
5987#if defined(MBEDTLS_ECP_C)
5988 {
5989 const mbedtls_pk_context *pk = &chain->pk;
5990
5991 /* If certificate uses an EC key, make sure the curve is OK */
5992 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
5993 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
5994 {
5995 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5996
5997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
5998 if( ret == 0 )
5999 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6000 }
6001 }
6002#endif /* MBEDTLS_ECP_C */
6003
6004 if( mbedtls_ssl_check_cert_usage( chain,
6005 ciphersuite_info,
6006 ! ssl->conf->endpoint,
6007 &ssl->session_negotiate->verify_result ) != 0 )
6008 {
6009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6010 if( ret == 0 )
6011 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6012 }
6013
6014 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6015 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6016 * with details encoded in the verification flags. All other kinds
6017 * of error codes, including those from the user provided f_vrfy
6018 * functions, are treated as fatal and lead to a failure of
6019 * ssl_parse_certificate even if verification was optional. */
6020 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6021 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6022 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6023 {
6024 ret = 0;
6025 }
6026
6027 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6028 {
6029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6030 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6031 }
6032
6033 if( ret != 0 )
6034 {
6035 uint8_t alert;
6036
6037 /* The certificate may have been rejected for several reasons.
6038 Pick one and send the corresponding alert. Which alert to send
6039 may be a subject of debate in some cases. */
6040 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6041 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6042 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6043 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6044 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6045 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6046 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6047 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6048 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6049 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6050 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6051 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6052 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6053 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6054 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6055 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6056 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6057 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6058 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6059 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6060 else
6061 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6062 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6063 alert );
6064 }
6065
6066#if defined(MBEDTLS_DEBUG_C)
6067 if( ssl->session_negotiate->verify_result != 0 )
6068 {
6069 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6070 (unsigned int) ssl->session_negotiate->verify_result ) );
6071 }
6072 else
6073 {
6074 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6075 }
6076#endif /* MBEDTLS_DEBUG_C */
6077
6078 return( ret );
6079}
6080
6081#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6082static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6083 unsigned char *start, size_t len )
6084{
6085 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6086 /* Remember digest of the peer's end-CRT. */
6087 ssl->session_negotiate->peer_cert_digest =
6088 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6089 if( ssl->session_negotiate->peer_cert_digest == NULL )
6090 {
6091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6092 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6093 mbedtls_ssl_send_alert_message( ssl,
6094 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6095 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6096
6097 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6098 }
6099
6100 ret = mbedtls_md( mbedtls_md_info_from_type(
6101 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6102 start, len,
6103 ssl->session_negotiate->peer_cert_digest );
6104
6105 ssl->session_negotiate->peer_cert_digest_type =
6106 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6107 ssl->session_negotiate->peer_cert_digest_len =
6108 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6109
6110 return( ret );
6111}
6112
6113static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6114 unsigned char *start, size_t len )
6115{
6116 unsigned char *end = start + len;
6117 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6118
6119 /* Make a copy of the peer's raw public key. */
6120 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6121 ret = mbedtls_pk_parse_subpubkey( &start, end,
6122 &ssl->handshake->peer_pubkey );
6123 if( ret != 0 )
6124 {
6125 /* We should have parsed the public key before. */
6126 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6127 }
6128
6129 return( 0 );
6130}
6131#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6132
6133int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6134{
6135 int ret = 0;
6136 int crt_expected;
6137#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6138 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6139 ? ssl->handshake->sni_authmode
6140 : ssl->conf->authmode;
6141#else
6142 const int authmode = ssl->conf->authmode;
6143#endif
6144 void *rs_ctx = NULL;
6145 mbedtls_x509_crt *chain = NULL;
6146
6147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6148
6149 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6150 if( crt_expected == SSL_CERTIFICATE_SKIP )
6151 {
6152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6153 goto exit;
6154 }
6155
6156#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6157 if( ssl->handshake->ecrs_enabled &&
6158 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6159 {
6160 chain = ssl->handshake->ecrs_peer_cert;
6161 ssl->handshake->ecrs_peer_cert = NULL;
6162 goto crt_verify;
6163 }
6164#endif
6165
6166 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6167 {
6168 /* mbedtls_ssl_read_record may have sent an alert already. We
6169 let it decide whether to alert. */
6170 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6171 goto exit;
6172 }
6173
6174#if defined(MBEDTLS_SSL_SRV_C)
6175 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6176 {
6177 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6178
6179 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6180 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6181
6182 goto exit;
6183 }
6184#endif /* MBEDTLS_SSL_SRV_C */
6185
6186 /* Clear existing peer CRT structure in case we tried to
6187 * reuse a session but it failed, and allocate a new one. */
6188 ssl_clear_peer_cert( ssl->session_negotiate );
6189
6190 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6191 if( chain == NULL )
6192 {
6193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6194 sizeof( mbedtls_x509_crt ) ) );
6195 mbedtls_ssl_send_alert_message( ssl,
6196 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6197 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6198
6199 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6200 goto exit;
6201 }
6202 mbedtls_x509_crt_init( chain );
6203
6204 ret = ssl_parse_certificate_chain( ssl, chain );
6205 if( ret != 0 )
6206 goto exit;
6207
6208#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6209 if( ssl->handshake->ecrs_enabled)
6210 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6211
6212crt_verify:
6213 if( ssl->handshake->ecrs_enabled)
6214 rs_ctx = &ssl->handshake->ecrs_ctx;
6215#endif
6216
6217 ret = ssl_parse_certificate_verify( ssl, authmode,
6218 chain, rs_ctx );
6219 if( ret != 0 )
6220 goto exit;
6221
6222#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6223 {
6224 unsigned char *crt_start, *pk_start;
6225 size_t crt_len, pk_len;
6226
6227 /* We parse the CRT chain without copying, so
6228 * these pointers point into the input buffer,
6229 * and are hence still valid after freeing the
6230 * CRT chain. */
6231
6232 crt_start = chain->raw.p;
6233 crt_len = chain->raw.len;
6234
6235 pk_start = chain->pk_raw.p;
6236 pk_len = chain->pk_raw.len;
6237
6238 /* Free the CRT structures before computing
6239 * digest and copying the peer's public key. */
6240 mbedtls_x509_crt_free( chain );
6241 mbedtls_free( chain );
6242 chain = NULL;
6243
6244 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6245 if( ret != 0 )
6246 goto exit;
6247
6248 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6249 if( ret != 0 )
6250 goto exit;
6251 }
6252#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6253 /* Pass ownership to session structure. */
6254 ssl->session_negotiate->peer_cert = chain;
6255 chain = NULL;
6256#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6257
6258 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6259
6260exit:
6261
6262 if( ret == 0 )
6263 ssl->state++;
6264
6265#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6266 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6267 {
6268 ssl->handshake->ecrs_peer_cert = chain;
6269 chain = NULL;
6270 }
6271#endif
6272
6273 if( chain != NULL )
6274 {
6275 mbedtls_x509_crt_free( chain );
6276 mbedtls_free( chain );
6277 }
6278
6279 return( ret );
6280}
6281#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6282
Jerry Yu615bd6f2022-02-17 14:25:15 +08006283#if defined(MBEDTLS_SHA256_C)
6284static void ssl_calc_finished_tls_sha256(
6285 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6286{
6287 int len = 12;
6288 const char *sender;
6289 unsigned char padbuf[32];
6290#if defined(MBEDTLS_USE_PSA_CRYPTO)
6291 size_t hash_size;
6292 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
6293 psa_status_t status;
6294#else
6295 mbedtls_sha256_context sha256;
6296#endif
6297
6298 mbedtls_ssl_session *session = ssl->session_negotiate;
6299 if( !session )
6300 session = ssl->session;
6301
6302 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6303 ? "client finished"
6304 : "server finished";
6305
6306#if defined(MBEDTLS_USE_PSA_CRYPTO)
6307 sha256_psa = psa_hash_operation_init();
6308
6309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6310
6311 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6312 if( status != PSA_SUCCESS )
6313 {
6314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6315 return;
6316 }
6317
6318 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6319 if( status != PSA_SUCCESS )
6320 {
6321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6322 return;
6323 }
6324 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6325#else
6326
6327 mbedtls_sha256_init( &sha256 );
6328
6329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
6330
6331 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6332
6333 /*
6334 * TLSv1.2:
6335 * hash = PRF( master, finished_label,
6336 * Hash( handshake ) )[0.11]
6337 */
6338
6339#if !defined(MBEDTLS_SHA256_ALT)
6340 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
6341 sha256.state, sizeof( sha256.state ) );
6342#endif
6343
6344 mbedtls_sha256_finish( &sha256, padbuf );
6345 mbedtls_sha256_free( &sha256 );
6346#endif /* MBEDTLS_USE_PSA_CRYPTO */
6347
6348 ssl->handshake->tls_prf( session->master, 48, sender,
6349 padbuf, 32, buf, len );
6350
6351 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6352
6353 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6354
6355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6356}
6357#endif /* MBEDTLS_SHA256_C */
6358
Jerry Yub7ba49e2022-02-17 14:25:53 +08006359
6360#if defined(MBEDTLS_SHA384_C)
6361
6362static void ssl_calc_finished_tls_sha384(
6363 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6364{
6365 int len = 12;
6366 const char *sender;
6367 unsigned char padbuf[48];
6368#if defined(MBEDTLS_USE_PSA_CRYPTO)
6369 size_t hash_size;
6370 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
6371 psa_status_t status;
6372#else
6373 mbedtls_sha512_context sha512;
6374#endif
6375
6376 mbedtls_ssl_session *session = ssl->session_negotiate;
6377 if( !session )
6378 session = ssl->session;
6379
6380 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6381 ? "client finished"
6382 : "server finished";
6383
6384#if defined(MBEDTLS_USE_PSA_CRYPTO)
6385 sha384_psa = psa_hash_operation_init();
6386
6387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6388
6389 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6390 if( status != PSA_SUCCESS )
6391 {
6392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6393 return;
6394 }
6395
6396 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
6397 if( status != PSA_SUCCESS )
6398 {
6399 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6400 return;
6401 }
6402 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
6403#else
6404 mbedtls_sha512_init( &sha512 );
6405
6406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
6407
6408 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
6409
6410 /*
6411 * TLSv1.2:
6412 * hash = PRF( master, finished_label,
6413 * Hash( handshake ) )[0.11]
6414 */
6415
6416#if !defined(MBEDTLS_SHA512_ALT)
6417 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6418 sha512.state, sizeof( sha512.state ) );
6419#endif
6420 mbedtls_sha512_finish( &sha512, padbuf );
6421
6422 mbedtls_sha512_free( &sha512 );
6423#endif
6424
6425 ssl->handshake->tls_prf( session->master, 48, sender,
6426 padbuf, 48, buf, len );
6427
6428 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6429
6430 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6431
6432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6433}
6434#endif /* MBEDTLS_SHA384_C */
6435
Jerry Yuaef00152022-02-17 14:27:31 +08006436void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
6437{
6438 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
6439
6440 /*
6441 * Free our handshake params
6442 */
6443 mbedtls_ssl_handshake_free( ssl );
6444 mbedtls_free( ssl->handshake );
6445 ssl->handshake = NULL;
6446
6447 /*
6448 * Free the previous transform and swith in the current one
6449 */
6450 if( ssl->transform )
6451 {
6452 mbedtls_ssl_transform_free( ssl->transform );
6453 mbedtls_free( ssl->transform );
6454 }
6455 ssl->transform = ssl->transform_negotiate;
6456 ssl->transform_negotiate = NULL;
6457
6458 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
6459}
6460
Jerry Yu2a9fff52022-02-17 14:28:51 +08006461void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
6462{
6463 int resume = ssl->handshake->resume;
6464
6465 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
6466
6467#if defined(MBEDTLS_SSL_RENEGOTIATION)
6468 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6469 {
6470 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
6471 ssl->renego_records_seen = 0;
6472 }
6473#endif
6474
6475 /*
6476 * Free the previous session and switch in the current one
6477 */
6478 if( ssl->session )
6479 {
6480#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6481 /* RFC 7366 3.1: keep the EtM state */
6482 ssl->session_negotiate->encrypt_then_mac =
6483 ssl->session->encrypt_then_mac;
6484#endif
6485
6486 mbedtls_ssl_session_free( ssl->session );
6487 mbedtls_free( ssl->session );
6488 }
6489 ssl->session = ssl->session_negotiate;
6490 ssl->session_negotiate = NULL;
6491
6492 /*
6493 * Add cache entry
6494 */
6495 if( ssl->conf->f_set_cache != NULL &&
6496 ssl->session->id_len != 0 &&
6497 resume == 0 )
6498 {
6499 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
6500 ssl->session->id,
6501 ssl->session->id_len,
6502 ssl->session ) != 0 )
6503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
6504 }
6505
6506#if defined(MBEDTLS_SSL_PROTO_DTLS)
6507 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6508 ssl->handshake->flight != NULL )
6509 {
6510 /* Cancel handshake timer */
6511 mbedtls_ssl_set_timer( ssl, 0 );
6512
6513 /* Keep last flight around in case we need to resend it:
6514 * we need the handshake and transform structures for that */
6515 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
6516 }
6517 else
6518#endif
6519 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
6520
6521 ssl->state++;
6522
6523 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
6524}
6525
Jerry Yu3c8e47b2022-02-17 14:30:01 +08006526int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
6527{
6528 int ret, hash_len;
6529
6530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
6531
6532 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
6533
6534 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
6535
6536 /*
6537 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6538 * may define some other value. Currently (early 2016), no defined
6539 * ciphersuite does this (and this is unlikely to change as activity has
6540 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6541 */
6542 hash_len = 12;
6543
6544#if defined(MBEDTLS_SSL_RENEGOTIATION)
6545 ssl->verify_data_len = hash_len;
6546 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
6547#endif
6548
6549 ssl->out_msglen = 4 + hash_len;
6550 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6551 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
6552
6553 /*
6554 * In case of session resuming, invert the client and server
6555 * ChangeCipherSpec messages order.
6556 */
6557 if( ssl->handshake->resume != 0 )
6558 {
6559#if defined(MBEDTLS_SSL_CLI_C)
6560 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6561 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
6562#endif
6563#if defined(MBEDTLS_SSL_SRV_C)
6564 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6565 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
6566#endif
6567 }
6568 else
6569 ssl->state++;
6570
6571 /*
6572 * Switch to our negotiated transform and session parameters for outbound
6573 * data.
6574 */
6575 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
6576
6577#if defined(MBEDTLS_SSL_PROTO_DTLS)
6578 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6579 {
6580 unsigned char i;
6581
6582 /* Remember current epoch settings for resending */
6583 ssl->handshake->alt_transform_out = ssl->transform_out;
6584 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
6585 sizeof( ssl->handshake->alt_out_ctr ) );
6586
6587 /* Set sequence_number to zero */
6588 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
6589
6590
6591 /* Increment epoch */
6592 for( i = 2; i > 0; i-- )
6593 if( ++ssl->cur_out_ctr[i - 1] != 0 )
6594 break;
6595
6596 /* The loop goes to its end iff the counter is wrapping */
6597 if( i == 0 )
6598 {
6599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6600 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
6601 }
6602 }
6603 else
6604#endif /* MBEDTLS_SSL_PROTO_DTLS */
6605 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6606
6607 ssl->transform_out = ssl->transform_negotiate;
6608 ssl->session_out = ssl->session_negotiate;
6609
6610#if defined(MBEDTLS_SSL_PROTO_DTLS)
6611 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6612 mbedtls_ssl_send_flight_completed( ssl );
6613#endif
6614
6615 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6616 {
6617 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6618 return( ret );
6619 }
6620
6621#if defined(MBEDTLS_SSL_PROTO_DTLS)
6622 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6623 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6624 {
6625 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6626 return( ret );
6627 }
6628#endif
6629
6630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
6631
6632 return( 0 );
6633}
6634
Jerry Yu0b3d7c12022-02-17 14:30:51 +08006635#define SSL_MAX_HASH_LEN 12
6636
6637int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
6638{
6639 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6640 unsigned int hash_len = 12;
6641 unsigned char buf[SSL_MAX_HASH_LEN];
6642
6643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
6644
6645 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
6646
6647 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6648 {
6649 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6650 goto exit;
6651 }
6652
6653 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6654 {
6655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6656 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6657 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6658 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
6659 goto exit;
6660 }
6661
6662 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
6663 {
6664 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6665 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6666 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
6667 goto exit;
6668 }
6669
6670 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
6671 {
6672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6673 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6674 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6675 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
6676 goto exit;
6677 }
6678
6679 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
6680 buf, hash_len ) != 0 )
6681 {
6682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6683 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6684 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
6685 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
6686 goto exit;
6687 }
6688
6689#if defined(MBEDTLS_SSL_RENEGOTIATION)
6690 ssl->verify_data_len = hash_len;
6691 memcpy( ssl->peer_verify_data, buf, hash_len );
6692#endif
6693
6694 if( ssl->handshake->resume != 0 )
6695 {
6696#if defined(MBEDTLS_SSL_CLI_C)
6697 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6698 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
6699#endif
6700#if defined(MBEDTLS_SSL_SRV_C)
6701 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6702 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
6703#endif
6704 }
6705 else
6706 ssl->state++;
6707
6708#if defined(MBEDTLS_SSL_PROTO_DTLS)
6709 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6710 mbedtls_ssl_recv_flight_completed( ssl );
6711#endif
6712
6713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
6714
6715exit:
6716 mbedtls_platform_zeroize( buf, hash_len );
6717 return( ret );
6718}
6719
Jerry Yu392112c2022-02-17 14:34:10 +08006720#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
6721/*
6722 * Helper to get TLS 1.2 PRF from ciphersuite
6723 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
6724 */
6725static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
6726{
6727#if defined(MBEDTLS_SHA384_C)
6728 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6729 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
6730
6731 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
6732 return( tls_prf_sha384 );
6733#else
6734 (void) ciphersuite_id;
6735#endif
6736 return( tls_prf_sha256 );
6737}
6738#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08006739
6740static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
6741{
6742 ((void) tls_prf);
6743#if defined(MBEDTLS_SHA384_C)
6744 if( tls_prf == tls_prf_sha384 )
6745 {
6746 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
6747 }
6748 else
6749#endif
6750#if defined(MBEDTLS_SHA256_C)
6751 if( tls_prf == tls_prf_sha256 )
6752 {
6753 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
6754 }
6755 else
6756#endif
6757 return( MBEDTLS_SSL_TLS_PRF_NONE );
6758}
6759
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006760/*
6761 * Populate a transform structure with session keys and all the other
6762 * necessary information.
6763 *
6764 * Parameters:
6765 * - [in/out]: transform: structure to populate
6766 * [in] must be just initialised with mbedtls_ssl_transform_init()
6767 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
6768 * - [in] ciphersuite
6769 * - [in] master
6770 * - [in] encrypt_then_mac
6771 * - [in] compression
6772 * - [in] tls_prf: pointer to PRF to use for key derivation
6773 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
6774 * - [in] minor_ver: SSL/TLS minor version
6775 * - [in] endpoint: client or server
6776 * - [in] ssl: used for:
6777 * - ssl->conf->{f,p}_export_keys
6778 * [in] optionally used for:
6779 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
6780 */
6781static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
6782 int ciphersuite,
6783 const unsigned char master[48],
6784#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
6785 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6786 int encrypt_then_mac,
6787#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
6788 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
6789 ssl_tls_prf_t tls_prf,
6790 const unsigned char randbytes[64],
6791 int minor_ver,
6792 unsigned endpoint,
6793 const mbedtls_ssl_context *ssl )
6794{
6795 int ret = 0;
6796 unsigned char keyblk[256];
6797 unsigned char *key1;
6798 unsigned char *key2;
6799 unsigned char *mac_enc;
6800 unsigned char *mac_dec;
6801 size_t mac_key_len = 0;
6802 size_t iv_copy_len;
6803 size_t keylen;
6804 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
6805 const mbedtls_cipher_info_t *cipher_info;
6806 const mbedtls_md_info_t *md_info;
6807
6808#if defined(MBEDTLS_USE_PSA_CRYPTO)
6809 psa_key_type_t key_type;
6810 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6811 psa_algorithm_t alg;
6812 size_t key_bits;
6813 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
6814#endif
6815
6816#if !defined(MBEDTLS_DEBUG_C) && \
6817 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6818 if( ssl->f_export_keys == NULL )
6819 {
6820 ssl = NULL; /* make sure we don't use it except for these cases */
6821 (void) ssl;
6822 }
6823#endif
6824
6825 /*
6826 * Some data just needs copying into the structure
6827 */
6828#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
6829 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
6830 transform->encrypt_then_mac = encrypt_then_mac;
6831#endif
6832 transform->minor_ver = minor_ver;
6833
6834#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
6835 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
6836#endif
6837
6838#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
6839 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
6840 {
6841 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
6842 * generation separate. This should never happen. */
6843 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6844 }
6845#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
6846
6847 /*
6848 * Get various info structures
6849 */
6850 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
6851 if( ciphersuite_info == NULL )
6852 {
6853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
6854 ciphersuite ) );
6855 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6856 }
6857
6858 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
6859 if( cipher_info == NULL )
6860 {
6861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
6862 ciphersuite_info->cipher ) );
6863 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6864 }
6865
6866 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
6867 if( md_info == NULL )
6868 {
6869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
6870 (unsigned) ciphersuite_info->mac ) );
6871 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6872 }
6873
6874#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6875 /* Copy own and peer's CID if the use of the CID
6876 * extension has been negotiated. */
6877 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
6878 {
6879 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
6880
6881 transform->in_cid_len = ssl->own_cid_len;
6882 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
6883 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
6884 transform->in_cid_len );
6885
6886 transform->out_cid_len = ssl->handshake->peer_cid_len;
6887 memcpy( transform->out_cid, ssl->handshake->peer_cid,
6888 ssl->handshake->peer_cid_len );
6889 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
6890 transform->out_cid_len );
6891 }
6892#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6893
6894 /*
6895 * Compute key block using the PRF
6896 */
6897 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
6898 if( ret != 0 )
6899 {
6900 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
6901 return( ret );
6902 }
6903
6904 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
6905 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
6906 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
6907 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
6908 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
6909
6910 /*
6911 * Determine the appropriate key, IV and MAC length.
6912 */
6913
6914 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
6915
6916#if defined(MBEDTLS_GCM_C) || \
6917 defined(MBEDTLS_CCM_C) || \
6918 defined(MBEDTLS_CHACHAPOLY_C)
6919 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_GCM ||
6920 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CCM ||
6921 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
6922 {
6923 size_t explicit_ivlen;
6924
6925 transform->maclen = 0;
6926 mac_key_len = 0;
6927 transform->taglen =
6928 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
6929
6930 /* All modes haves 96-bit IVs, but the length of the static parts vary
6931 * with mode and version:
6932 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
6933 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
6934 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
6935 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
6936 * sequence number).
6937 */
6938 transform->ivlen = 12;
6939 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
6940 transform->fixed_ivlen = 12;
6941 else
6942 transform->fixed_ivlen = 4;
6943
6944 /* Minimum length of encrypted record */
6945 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
6946 transform->minlen = explicit_ivlen + transform->taglen;
6947 }
6948 else
6949#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
6950#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
6951 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM ||
6952 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
6953 {
Neil Armstrongcf8841a2022-02-24 11:17:45 +01006954#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006955 /* Initialize HMAC contexts */
6956 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
6957 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
6958 {
6959 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
6960 goto end;
6961 }
Neil Armstrongcf8841a2022-02-24 11:17:45 +01006962#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006963
6964 /* Get MAC length */
6965 mac_key_len = mbedtls_md_get_size( md_info );
6966 transform->maclen = mac_key_len;
6967
6968 /* IV length */
6969 transform->ivlen = cipher_info->iv_size;
6970
6971 /* Minimum length */
6972 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM )
6973 transform->minlen = transform->maclen;
6974 else
6975 {
6976 /*
6977 * GenericBlockCipher:
6978 * 1. if EtM is in use: one block plus MAC
6979 * otherwise: * first multiple of blocklen greater than maclen
6980 * 2. IV
6981 */
6982#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6983 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
6984 {
6985 transform->minlen = transform->maclen
6986 + cipher_info->block_size;
6987 }
6988 else
6989#endif
6990 {
6991 transform->minlen = transform->maclen
6992 + cipher_info->block_size
6993 - transform->maclen % cipher_info->block_size;
6994 }
6995
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006996 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
6997 {
6998 transform->minlen += transform->ivlen;
6999 }
7000 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007001 {
7002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7003 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7004 goto end;
7005 }
7006 }
7007 }
7008 else
7009#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7010 {
7011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7012 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7013 }
7014
7015 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7016 (unsigned) keylen,
7017 (unsigned) transform->minlen,
7018 (unsigned) transform->ivlen,
7019 (unsigned) transform->maclen ) );
7020
7021 /*
7022 * Finally setup the cipher contexts, IVs and MAC secrets.
7023 */
7024#if defined(MBEDTLS_SSL_CLI_C)
7025 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7026 {
7027 key1 = keyblk + mac_key_len * 2;
7028 key2 = keyblk + mac_key_len * 2 + keylen;
7029
7030 mac_enc = keyblk;
7031 mac_dec = keyblk + mac_key_len;
7032
7033 /*
7034 * This is not used in TLS v1.1.
7035 */
7036 iv_copy_len = ( transform->fixed_ivlen ) ?
7037 transform->fixed_ivlen : transform->ivlen;
7038 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7039 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7040 iv_copy_len );
7041 }
7042 else
7043#endif /* MBEDTLS_SSL_CLI_C */
7044#if defined(MBEDTLS_SSL_SRV_C)
7045 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7046 {
7047 key1 = keyblk + mac_key_len * 2 + keylen;
7048 key2 = keyblk + mac_key_len * 2;
7049
7050 mac_enc = keyblk + mac_key_len;
7051 mac_dec = keyblk;
7052
7053 /*
7054 * This is not used in TLS v1.1.
7055 */
7056 iv_copy_len = ( transform->fixed_ivlen ) ?
7057 transform->fixed_ivlen : transform->ivlen;
7058 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7059 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7060 iv_copy_len );
7061 }
7062 else
7063#endif /* MBEDTLS_SSL_SRV_C */
7064 {
7065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7066 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7067 goto end;
7068 }
7069
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007070 if( ssl != NULL && ssl->f_export_keys != NULL )
7071 {
7072 ssl->f_export_keys( ssl->p_export_keys,
7073 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7074 master, 48,
7075 randbytes + 32,
7076 randbytes,
7077 tls_prf_get_type( tls_prf ) );
7078 }
7079
7080#if defined(MBEDTLS_USE_PSA_CRYPTO)
7081 if( ( status = mbedtls_ssl_cipher_to_psa( cipher_info->type,
7082 transform->taglen,
7083 &alg,
7084 &key_type,
7085 &key_bits ) ) != PSA_SUCCESS )
7086 {
7087 ret = psa_ssl_status_to_mbedtls( status );
7088 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7089 goto end;
7090 }
7091
7092 transform->psa_alg = alg;
7093
7094 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7095 {
7096 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7097 psa_set_key_algorithm( &attributes, alg );
7098 psa_set_key_type( &attributes, key_type );
7099
7100 if( ( status = psa_import_key( &attributes,
7101 key1,
7102 PSA_BITS_TO_BYTES( key_bits ),
7103 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7104 {
7105 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7106 ret = psa_ssl_status_to_mbedtls( status );
7107 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7108 goto end;
7109 }
7110
7111 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7112
7113 if( ( status = psa_import_key( &attributes,
7114 key2,
7115 PSA_BITS_TO_BYTES( key_bits ),
7116 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7117 {
7118 ret = psa_ssl_status_to_mbedtls( status );
7119 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7120 goto end;
7121 }
7122 }
7123#else
7124 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7125 cipher_info ) ) != 0 )
7126 {
7127 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7128 goto end;
7129 }
7130
7131 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7132 cipher_info ) ) != 0 )
7133 {
7134 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7135 goto end;
7136 }
7137
7138 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7139 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7140 MBEDTLS_ENCRYPT ) ) != 0 )
7141 {
7142 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7143 goto end;
7144 }
7145
7146 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7147 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7148 MBEDTLS_DECRYPT ) ) != 0 )
7149 {
7150 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7151 goto end;
7152 }
7153
7154#if defined(MBEDTLS_CIPHER_MODE_CBC)
7155 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7156 {
7157 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7158 MBEDTLS_PADDING_NONE ) ) != 0 )
7159 {
7160 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7161 goto end;
7162 }
7163
7164 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7165 MBEDTLS_PADDING_NONE ) ) != 0 )
7166 {
7167 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7168 goto end;
7169 }
7170 }
7171#endif /* MBEDTLS_CIPHER_MODE_CBC */
7172#endif /* MBEDTLS_USE_PSA_CRYPTO */
7173
Neil Armstrong29c0c042022-03-17 17:47:28 +01007174#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7175 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7176 For AEAD-based ciphersuites, there is nothing to do here. */
7177 if( mac_key_len != 0 )
7178 {
7179#if defined(MBEDTLS_USE_PSA_CRYPTO)
7180 alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
7181 if( alg == 0 )
7182 {
7183 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7184 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_md_type_to_psa", ret );
7185 goto end;
7186 }
7187
7188 transform->psa_mac_alg = PSA_ALG_HMAC( alg );
7189
7190 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
7191 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( alg ) );
7192 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7193
7194 if( ( status = psa_import_key( &attributes,
7195 mac_enc, mac_key_len,
7196 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7197 {
7198 ret = psa_ssl_status_to_mbedtls( status );
7199 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7200 goto end;
7201 }
7202
Ronald Cronfb39f152022-03-25 14:36:28 +01007203 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
7204 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) &&
7205 ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007206 /* mbedtls_ct_hmac() requires the key to be exportable */
7207 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7208 PSA_KEY_USAGE_VERIFY_HASH );
7209 else
7210 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7211
7212 if( ( status = psa_import_key( &attributes,
7213 mac_dec, mac_key_len,
7214 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7215 {
7216 ret = psa_ssl_status_to_mbedtls( status );
7217 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7218 goto end;
7219 }
7220#else
7221 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7222 if( ret != 0 )
7223 goto end;
7224 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7225 if( ret != 0 )
7226 goto end;
7227#endif /* MBEDTLS_USE_PSA_CRYPTO */
7228 }
7229#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7230
7231 ((void) mac_dec);
7232 ((void) mac_enc);
7233
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007234end:
7235 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7236 return( ret );
7237}
7238
Jerry Yuee40f9d2022-02-17 14:55:16 +08007239#if defined(MBEDTLS_USE_PSA_CRYPTO)
7240int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7241 unsigned char *hash, size_t *hashlen,
7242 unsigned char *data, size_t data_len,
7243 mbedtls_md_type_t md_alg )
7244{
7245 psa_status_t status;
7246 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
7247 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
7248
7249 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7250
7251 if( ( status = psa_hash_setup( &hash_operation,
7252 hash_alg ) ) != PSA_SUCCESS )
7253 {
7254 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7255 goto exit;
7256 }
7257
7258 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
7259 64 ) ) != PSA_SUCCESS )
7260 {
7261 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7262 goto exit;
7263 }
7264
7265 if( ( status = psa_hash_update( &hash_operation,
7266 data, data_len ) ) != PSA_SUCCESS )
7267 {
7268 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7269 goto exit;
7270 }
7271
7272 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
7273 hashlen ) ) != PSA_SUCCESS )
7274 {
7275 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
7276 goto exit;
7277 }
7278
7279exit:
7280 if( status != PSA_SUCCESS )
7281 {
7282 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7283 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7284 switch( status )
7285 {
7286 case PSA_ERROR_NOT_SUPPORTED:
7287 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
7288 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
7289 case PSA_ERROR_BUFFER_TOO_SMALL:
7290 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
7291 case PSA_ERROR_INSUFFICIENT_MEMORY:
7292 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
7293 default:
7294 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
7295 }
7296 }
7297 return( 0 );
7298}
7299
7300#else
7301
7302int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7303 unsigned char *hash, size_t *hashlen,
7304 unsigned char *data, size_t data_len,
7305 mbedtls_md_type_t md_alg )
7306{
7307 int ret = 0;
7308 mbedtls_md_context_t ctx;
7309 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
7310 *hashlen = mbedtls_md_get_size( md_info );
7311
7312 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
7313
7314 mbedtls_md_init( &ctx );
7315
7316 /*
7317 * digitally-signed struct {
7318 * opaque client_random[32];
7319 * opaque server_random[32];
7320 * ServerDHParams params;
7321 * };
7322 */
7323 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
7324 {
7325 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7326 goto exit;
7327 }
7328 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
7329 {
7330 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
7331 goto exit;
7332 }
7333 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
7334 {
7335 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7336 goto exit;
7337 }
7338 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
7339 {
7340 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7341 goto exit;
7342 }
7343 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
7344 {
7345 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
7346 goto exit;
7347 }
7348
7349exit:
7350 mbedtls_md_free( &ctx );
7351
7352 if( ret != 0 )
7353 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7354 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7355
7356 return( ret );
7357}
7358#endif /* MBEDTLS_USE_PSA_CRYPTO */
7359
Jerry Yud9d91da2022-02-17 14:57:06 +08007360#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
7361
7362/* Find an entry in a signature-hash set matching a given hash algorithm. */
7363mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
7364 mbedtls_pk_type_t sig_alg )
7365{
7366 switch( sig_alg )
7367 {
7368 case MBEDTLS_PK_RSA:
7369 return( set->rsa );
7370 case MBEDTLS_PK_ECDSA:
7371 return( set->ecdsa );
7372 default:
7373 return( MBEDTLS_MD_NONE );
7374 }
7375}
7376
7377/* Add a signature-hash-pair to a signature-hash set */
7378void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
7379 mbedtls_pk_type_t sig_alg,
7380 mbedtls_md_type_t md_alg )
7381{
7382 switch( sig_alg )
7383 {
7384 case MBEDTLS_PK_RSA:
7385 if( set->rsa == MBEDTLS_MD_NONE )
7386 set->rsa = md_alg;
7387 break;
7388
7389 case MBEDTLS_PK_ECDSA:
7390 if( set->ecdsa == MBEDTLS_MD_NONE )
7391 set->ecdsa = md_alg;
7392 break;
7393
7394 default:
7395 break;
7396 }
7397}
7398
7399/* Allow exactly one hash algorithm for each signature. */
7400void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
7401 mbedtls_md_type_t md_alg )
7402{
7403 set->rsa = md_alg;
7404 set->ecdsa = md_alg;
7405}
7406
7407#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7408
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08007409/* Serialization of TLS 1.2 sessions:
7410 *
7411 * struct {
7412 * uint64 start_time;
7413 * uint8 ciphersuite[2]; // defined by the standard
7414 * uint8 compression; // 0 or 1
7415 * uint8 session_id_len; // at most 32
7416 * opaque session_id[32];
7417 * opaque master[48]; // fixed length in the standard
7418 * uint32 verify_result;
7419 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
7420 * opaque ticket<0..2^24-1>; // length 0 means no ticket
7421 * uint32 ticket_lifetime;
7422 * uint8 mfl_code; // up to 255 according to standard
7423 * uint8 encrypt_then_mac; // 0 or 1
7424 * } serialized_session_tls12;
7425 *
7426 */
7427static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session,
7428 unsigned char *buf,
7429 size_t buf_len )
7430{
7431 unsigned char *p = buf;
7432 size_t used = 0;
7433
7434#if defined(MBEDTLS_HAVE_TIME)
7435 uint64_t start;
7436#endif
7437#if defined(MBEDTLS_X509_CRT_PARSE_C)
7438#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7439 size_t cert_len;
7440#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7441#endif /* MBEDTLS_X509_CRT_PARSE_C */
7442
7443 /*
7444 * Time
7445 */
7446#if defined(MBEDTLS_HAVE_TIME)
7447 used += 8;
7448
7449 if( used <= buf_len )
7450 {
7451 start = (uint64_t) session->start;
7452
7453 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
7454 p += 8;
7455 }
7456#endif /* MBEDTLS_HAVE_TIME */
7457
7458 /*
7459 * Basic mandatory fields
7460 */
7461 used += 2 /* ciphersuite */
7462 + 1 /* compression */
7463 + 1 /* id_len */
7464 + sizeof( session->id )
7465 + sizeof( session->master )
7466 + 4; /* verify_result */
7467
7468 if( used <= buf_len )
7469 {
7470 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
7471 p += 2;
7472
7473 *p++ = MBEDTLS_BYTE_0( session->compression );
7474
7475 *p++ = MBEDTLS_BYTE_0( session->id_len );
7476 memcpy( p, session->id, 32 );
7477 p += 32;
7478
7479 memcpy( p, session->master, 48 );
7480 p += 48;
7481
7482 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
7483 p += 4;
7484 }
7485
7486 /*
7487 * Peer's end-entity certificate
7488 */
7489#if defined(MBEDTLS_X509_CRT_PARSE_C)
7490#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7491 if( session->peer_cert == NULL )
7492 cert_len = 0;
7493 else
7494 cert_len = session->peer_cert->raw.len;
7495
7496 used += 3 + cert_len;
7497
7498 if( used <= buf_len )
7499 {
7500 *p++ = MBEDTLS_BYTE_2( cert_len );
7501 *p++ = MBEDTLS_BYTE_1( cert_len );
7502 *p++ = MBEDTLS_BYTE_0( cert_len );
7503
7504 if( session->peer_cert != NULL )
7505 {
7506 memcpy( p, session->peer_cert->raw.p, cert_len );
7507 p += cert_len;
7508 }
7509 }
7510#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7511 if( session->peer_cert_digest != NULL )
7512 {
7513 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
7514 if( used <= buf_len )
7515 {
7516 *p++ = (unsigned char) session->peer_cert_digest_type;
7517 *p++ = (unsigned char) session->peer_cert_digest_len;
7518 memcpy( p, session->peer_cert_digest,
7519 session->peer_cert_digest_len );
7520 p += session->peer_cert_digest_len;
7521 }
7522 }
7523 else
7524 {
7525 used += 2;
7526 if( used <= buf_len )
7527 {
7528 *p++ = (unsigned char) MBEDTLS_MD_NONE;
7529 *p++ = 0;
7530 }
7531 }
7532#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7533#endif /* MBEDTLS_X509_CRT_PARSE_C */
7534
7535 /*
7536 * Session ticket if any, plus associated data
7537 */
7538#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7539 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
7540
7541 if( used <= buf_len )
7542 {
7543 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
7544 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
7545 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
7546
7547 if( session->ticket != NULL )
7548 {
7549 memcpy( p, session->ticket, session->ticket_len );
7550 p += session->ticket_len;
7551 }
7552
7553 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
7554 p += 4;
7555 }
7556#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7557
7558 /*
7559 * Misc extension-related info
7560 */
7561#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7562 used += 1;
7563
7564 if( used <= buf_len )
7565 *p++ = session->mfl_code;
7566#endif
7567
7568#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7569 used += 1;
7570
7571 if( used <= buf_len )
7572 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
7573#endif
7574
7575 return( used );
7576}
7577
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08007578static int ssl_session_load_tls12( mbedtls_ssl_session *session,
7579 const unsigned char *buf,
7580 size_t len )
7581{
7582#if defined(MBEDTLS_HAVE_TIME)
7583 uint64_t start;
7584#endif
7585#if defined(MBEDTLS_X509_CRT_PARSE_C)
7586#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7587 size_t cert_len;
7588#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7589#endif /* MBEDTLS_X509_CRT_PARSE_C */
7590
7591 const unsigned char *p = buf;
7592 const unsigned char * const end = buf + len;
7593
7594 /*
7595 * Time
7596 */
7597#if defined(MBEDTLS_HAVE_TIME)
7598 if( 8 > (size_t)( end - p ) )
7599 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7600
7601 start = ( (uint64_t) p[0] << 56 ) |
7602 ( (uint64_t) p[1] << 48 ) |
7603 ( (uint64_t) p[2] << 40 ) |
7604 ( (uint64_t) p[3] << 32 ) |
7605 ( (uint64_t) p[4] << 24 ) |
7606 ( (uint64_t) p[5] << 16 ) |
7607 ( (uint64_t) p[6] << 8 ) |
7608 ( (uint64_t) p[7] );
7609 p += 8;
7610
7611 session->start = (time_t) start;
7612#endif /* MBEDTLS_HAVE_TIME */
7613
7614 /*
7615 * Basic mandatory fields
7616 */
7617 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
7618 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7619
7620 session->ciphersuite = ( p[0] << 8 ) | p[1];
7621 p += 2;
7622
7623 session->compression = *p++;
7624
7625 session->id_len = *p++;
7626 memcpy( session->id, p, 32 );
7627 p += 32;
7628
7629 memcpy( session->master, p, 48 );
7630 p += 48;
7631
7632 session->verify_result = ( (uint32_t) p[0] << 24 ) |
7633 ( (uint32_t) p[1] << 16 ) |
7634 ( (uint32_t) p[2] << 8 ) |
7635 ( (uint32_t) p[3] );
7636 p += 4;
7637
7638 /* Immediately clear invalid pointer values that have been read, in case
7639 * we exit early before we replaced them with valid ones. */
7640#if defined(MBEDTLS_X509_CRT_PARSE_C)
7641#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7642 session->peer_cert = NULL;
7643#else
7644 session->peer_cert_digest = NULL;
7645#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7646#endif /* MBEDTLS_X509_CRT_PARSE_C */
7647#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7648 session->ticket = NULL;
7649#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7650
7651 /*
7652 * Peer certificate
7653 */
7654#if defined(MBEDTLS_X509_CRT_PARSE_C)
7655#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7656 /* Deserialize CRT from the end of the ticket. */
7657 if( 3 > (size_t)( end - p ) )
7658 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7659
7660 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
7661 p += 3;
7662
7663 if( cert_len != 0 )
7664 {
7665 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7666
7667 if( cert_len > (size_t)( end - p ) )
7668 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7669
7670 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7671
7672 if( session->peer_cert == NULL )
7673 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7674
7675 mbedtls_x509_crt_init( session->peer_cert );
7676
7677 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
7678 p, cert_len ) ) != 0 )
7679 {
7680 mbedtls_x509_crt_free( session->peer_cert );
7681 mbedtls_free( session->peer_cert );
7682 session->peer_cert = NULL;
7683 return( ret );
7684 }
7685
7686 p += cert_len;
7687 }
7688#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7689 /* Deserialize CRT digest from the end of the ticket. */
7690 if( 2 > (size_t)( end - p ) )
7691 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7692
7693 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
7694 session->peer_cert_digest_len = (size_t) *p++;
7695
7696 if( session->peer_cert_digest_len != 0 )
7697 {
7698 const mbedtls_md_info_t *md_info =
7699 mbedtls_md_info_from_type( session->peer_cert_digest_type );
7700 if( md_info == NULL )
7701 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7702 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
7703 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7704
7705 if( session->peer_cert_digest_len > (size_t)( end - p ) )
7706 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7707
7708 session->peer_cert_digest =
7709 mbedtls_calloc( 1, session->peer_cert_digest_len );
7710 if( session->peer_cert_digest == NULL )
7711 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7712
7713 memcpy( session->peer_cert_digest, p,
7714 session->peer_cert_digest_len );
7715 p += session->peer_cert_digest_len;
7716 }
7717#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7718#endif /* MBEDTLS_X509_CRT_PARSE_C */
7719
7720 /*
7721 * Session ticket and associated data
7722 */
7723#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7724 if( 3 > (size_t)( end - p ) )
7725 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7726
7727 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
7728 p += 3;
7729
7730 if( session->ticket_len != 0 )
7731 {
7732 if( session->ticket_len > (size_t)( end - p ) )
7733 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7734
7735 session->ticket = mbedtls_calloc( 1, session->ticket_len );
7736 if( session->ticket == NULL )
7737 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7738
7739 memcpy( session->ticket, p, session->ticket_len );
7740 p += session->ticket_len;
7741 }
7742
7743 if( 4 > (size_t)( end - p ) )
7744 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7745
7746 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
7747 ( (uint32_t) p[1] << 16 ) |
7748 ( (uint32_t) p[2] << 8 ) |
7749 ( (uint32_t) p[3] );
7750 p += 4;
7751#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7752
7753 /*
7754 * Misc extension-related info
7755 */
7756#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7757 if( 1 > (size_t)( end - p ) )
7758 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7759
7760 session->mfl_code = *p++;
7761#endif
7762
7763#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7764 if( 1 > (size_t)( end - p ) )
7765 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7766
7767 session->encrypt_then_mac = *p++;
7768#endif
7769
7770 /* Done, should have consumed entire buffer */
7771 if( p != end )
7772 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7773
7774 return( 0 );
7775}
Jerry Yudc7bd172022-02-17 13:44:15 +08007776#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007778#endif /* MBEDTLS_SSL_TLS_C */