blob: 0e1d0ead18e8025f12865856f7bbc04553b63098 [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_only( conf ) )
886 {
887 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jerry Yu60835a82021-08-04 10:13:52 +0800888 {
Ronald Cron086ee0b2022-03-15 15:18:51 +0100889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
Jerry Yu60835a82021-08-04 10:13:52 +0800890 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
891 }
Ronald Cron37bdaab2022-03-30 16:45:51 +0200892
893 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
894 {
895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
896 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
897 }
898
Jerry Yu60835a82021-08-04 10:13:52 +0800899 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 }
Ronald Cron37bdaab2022-03-30 16:45:51 +0200920
921 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
922 {
923 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
924 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
925 }
926
Ronald Crone1d3f062022-02-10 14:50:54 +0100927 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
928 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +0800929 }
930#endif
931
932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
933 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
934}
935
936static int ssl_conf_check(const mbedtls_ssl_context *ssl)
937{
938 int ret;
939 ret = ssl_conf_version_check( ssl );
940 if( ret != 0 )
941 return( ret );
942
943 /* Space for further checks */
944
945 return( 0 );
946}
947
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200948/*
949 * Setup an SSL context
950 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100951
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200952int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +0200953 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +0000954{
Janos Follath865b3eb2019-12-16 11:46:15 +0000955 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +0000956 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
957 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +0000958
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200959 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +0000960
Jerry Yu60835a82021-08-04 10:13:52 +0800961 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
962 return( ret );
963
Paul Bakker62f2dee2012-09-28 07:31:51 +0000964 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +0100965 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +0000966 */
k-stachowiakc9a5f022018-07-24 13:53:31 +0200967
968 /* Set to NULL in case of an error condition */
969 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +0200970
Darryl Greenb33cc762019-11-28 14:29:44 +0000971#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
972 ssl->in_buf_len = in_buf_len;
973#endif
974 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +1000975 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000976 {
Paul Elliottd48d5c62021-01-07 14:47:05 +0000977 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +0200978 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +0200979 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +1000980 }
981
Darryl Greenb33cc762019-11-28 14:29:44 +0000982#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
983 ssl->out_buf_len = out_buf_len;
984#endif
985 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +1000986 if( ssl->out_buf == NULL )
987 {
Paul Elliottd48d5c62021-01-07 14:47:05 +0000988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +0200989 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +0200990 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +0000991 }
992
Hanno Becker3e6f8ab2020-02-05 10:40:57 +0000993 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200994
Johan Pascalb62bb512015-12-03 21:56:45 +0100995#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +0200996 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +0100997#endif
998
Paul Bakker48916f92012-09-16 19:57:18 +0000999 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001000 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001001
1002 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001003
1004error:
1005 mbedtls_free( ssl->in_buf );
1006 mbedtls_free( ssl->out_buf );
1007
1008 ssl->conf = NULL;
1009
Darryl Greenb33cc762019-11-28 14:29:44 +00001010#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1011 ssl->in_buf_len = 0;
1012 ssl->out_buf_len = 0;
1013#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001014 ssl->in_buf = NULL;
1015 ssl->out_buf = NULL;
1016
1017 ssl->in_hdr = NULL;
1018 ssl->in_ctr = NULL;
1019 ssl->in_len = NULL;
1020 ssl->in_iv = NULL;
1021 ssl->in_msg = NULL;
1022
1023 ssl->out_hdr = NULL;
1024 ssl->out_ctr = NULL;
1025 ssl->out_len = NULL;
1026 ssl->out_iv = NULL;
1027 ssl->out_msg = NULL;
1028
k-stachowiak9f7798e2018-07-31 16:52:32 +02001029 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001030}
1031
1032/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001033 * Reset an initialized and used SSL context for re-use while retaining
1034 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001035 *
1036 * If partial is non-zero, keep data in the input buffer and client ID.
1037 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001038 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001039void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1040 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001041{
Darryl Greenb33cc762019-11-28 14:29:44 +00001042#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1043 size_t in_buf_len = ssl->in_buf_len;
1044 size_t out_buf_len = ssl->out_buf_len;
1045#else
1046 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1047 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1048#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001049
Hanno Beckerb0302c42021-08-03 09:39:42 +01001050#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1051 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001052#endif
1053
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001054 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001055 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001056
Hanno Beckerb0302c42021-08-03 09:39:42 +01001057 mbedtls_ssl_reset_in_out_pointers( ssl );
1058
1059 /* Reset incoming message parsing */
1060 ssl->in_offt = NULL;
1061 ssl->nb_zero = 0;
1062 ssl->in_msgtype = 0;
1063 ssl->in_msglen = 0;
1064 ssl->in_hslen = 0;
1065 ssl->keep_current_message = 0;
1066 ssl->transform_in = NULL;
1067
1068#if defined(MBEDTLS_SSL_PROTO_DTLS)
1069 ssl->next_record_offset = 0;
1070 ssl->in_epoch = 0;
1071#endif
1072
1073 /* Keep current datagram if partial == 1 */
1074 if( partial == 0 )
1075 {
1076 ssl->in_left = 0;
1077 memset( ssl->in_buf, 0, in_buf_len );
1078 }
1079
1080 /* Reset outgoing message writing */
1081 ssl->out_msgtype = 0;
1082 ssl->out_msglen = 0;
1083 ssl->out_left = 0;
1084 memset( ssl->out_buf, 0, out_buf_len );
1085 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1086 ssl->transform_out = NULL;
1087
1088#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1089 mbedtls_ssl_dtls_replay_reset( ssl );
1090#endif
1091
XiaokangQian2b01dc32022-01-21 02:53:13 +00001092#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001093 if( ssl->transform )
1094 {
1095 mbedtls_ssl_transform_free( ssl->transform );
1096 mbedtls_free( ssl->transform );
1097 ssl->transform = NULL;
1098 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001099#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1100
XiaokangQian2b01dc32022-01-21 02:53:13 +00001101#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1102 mbedtls_ssl_transform_free( ssl->transform_application );
1103 mbedtls_free( ssl->transform_application );
1104 ssl->transform_application = NULL;
1105
1106 if( ssl->handshake != NULL )
1107 {
1108 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1109 mbedtls_free( ssl->handshake->transform_earlydata );
1110 ssl->handshake->transform_earlydata = NULL;
1111
1112 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1113 mbedtls_free( ssl->handshake->transform_handshake );
1114 ssl->handshake->transform_handshake = NULL;
1115 }
1116
XiaokangQian2b01dc32022-01-21 02:53:13 +00001117#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001118}
1119
1120int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1121{
1122 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1123
1124 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1125
XiaokangQian78b1fa72022-01-19 06:56:30 +00001126 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001127
1128 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129#if defined(MBEDTLS_SSL_RENEGOTIATION)
1130 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001131 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001132
1133 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1135 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001136#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001138
Hanno Beckerb0302c42021-08-03 09:39:42 +01001139 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001140 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001141 if( ssl->session )
1142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 mbedtls_ssl_session_free( ssl->session );
1144 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001145 ssl->session = NULL;
1146 }
1147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001149 ssl->alpn_chosen = NULL;
1150#endif
1151
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001152#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01001153#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001154 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01001155#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001156 {
1157 mbedtls_free( ssl->cli_id );
1158 ssl->cli_id = NULL;
1159 ssl->cli_id_len = 0;
1160 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001161#endif
1162
Paul Bakker48916f92012-09-16 19:57:18 +00001163 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1164 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001165
1166 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001167}
1168
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001169/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001170 * Reset an initialized and used SSL context for re-use while retaining
1171 * all application-set variables, function pointers and data.
1172 */
1173int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1174{
Hanno Becker43aefe22020-02-05 10:44:56 +00001175 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001176}
1177
1178/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001179 * SSL set accessors
1180 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001181void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001182{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001183 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001184}
1185
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001186void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001187{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001188 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001189}
1190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001192void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001193{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001194 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001195}
1196#endif
1197
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001198void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001199{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001200 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001201}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001204
Hanno Becker1841b0a2018-08-24 11:13:57 +01001205void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1206 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001207{
1208 ssl->disable_datagram_packing = !allow_packing;
1209}
1210
1211void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1212 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001213{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001214 conf->hs_timeout_min = min;
1215 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001216}
1217#endif
1218
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001219void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001220{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001221 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001222}
1223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001225void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001226 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001227 void *p_vrfy )
1228{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001229 conf->f_vrfy = f_vrfy;
1230 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001231}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001233
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001234void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001235 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001236 void *p_rng )
1237{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001238 conf->f_rng = f_rng;
1239 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001240}
1241
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001242void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001243 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001244 void *p_dbg )
1245{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001246 conf->f_dbg = f_dbg;
1247 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001248}
1249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001251 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001252 mbedtls_ssl_send_t *f_send,
1253 mbedtls_ssl_recv_t *f_recv,
1254 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001255{
1256 ssl->p_bio = p_bio;
1257 ssl->f_send = f_send;
1258 ssl->f_recv = f_recv;
1259 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001260}
1261
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001262#if defined(MBEDTLS_SSL_PROTO_DTLS)
1263void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1264{
1265 ssl->mtu = mtu;
1266}
1267#endif
1268
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001269void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001270{
1271 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001272}
1273
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001274void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1275 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001276 mbedtls_ssl_set_timer_t *f_set_timer,
1277 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001278{
1279 ssl->p_timer = p_timer;
1280 ssl->f_set_timer = f_set_timer;
1281 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001282
1283 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001284 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001285}
1286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287#if defined(MBEDTLS_SSL_SRV_C)
Glenn Strauss2ed95272022-01-21 18:02:17 -05001288void mbedtls_ssl_conf_cert_cb( mbedtls_ssl_config *conf,
1289 int (*f_cert_cb)(mbedtls_ssl_context *) )
1290{
1291 conf->f_cert_cb = f_cert_cb;
1292}
1293#endif /* MBEDTLS_SSL_SRV_C */
1294
1295#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001296void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001297 void *p_cache,
1298 mbedtls_ssl_cache_get_t *f_get_cache,
1299 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001300{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001301 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001302 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001303 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001304}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307#if defined(MBEDTLS_SSL_CLI_C)
1308int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001309{
Janos Follath865b3eb2019-12-16 11:46:15 +00001310 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001311
1312 if( ssl == NULL ||
1313 session == NULL ||
1314 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001315 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001318 }
1319
Hanno Beckere810bbc2021-05-14 16:01:05 +01001320 if( ssl->handshake->resume == 1 )
1321 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1322
Hanno Becker52055ae2019-02-06 14:30:46 +00001323 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1324 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001325 return( ret );
1326
Paul Bakker0a597072012-09-25 21:55:46 +00001327 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001328
1329 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001330}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001332
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001333void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1334 const int *ciphersuites )
1335{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001336 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001337}
1338
Ronald Cron6f135e12021-12-08 16:57:54 +01001339#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001340void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001341 const int kex_modes )
1342{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001343 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001344}
Ronald Cron6f135e12021-12-08 16:57:54 +01001345#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001348void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001349 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001350{
1351 conf->cert_profile = profile;
1352}
1353
Glenn Strauss36872db2022-01-22 05:06:31 -05001354static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1355{
1356 mbedtls_ssl_key_cert *cur = key_cert, *next;
1357
1358 while( cur != NULL )
1359 {
1360 next = cur->next;
1361 mbedtls_free( cur );
1362 cur = next;
1363 }
1364}
1365
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001366/* Append a new keycert entry to a (possibly empty) list */
1367static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1368 mbedtls_x509_crt *cert,
1369 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001370{
niisato8ee24222018-06-25 19:05:48 +09001371 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001372
Glenn Strauss36872db2022-01-22 05:06:31 -05001373 if( cert == NULL )
1374 {
1375 /* Free list if cert is null */
1376 ssl_key_cert_free( *head );
1377 *head = NULL;
1378 return( 0 );
1379 }
1380
niisato8ee24222018-06-25 19:05:48 +09001381 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1382 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001383 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001384
niisato8ee24222018-06-25 19:05:48 +09001385 new_cert->cert = cert;
1386 new_cert->key = key;
1387 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001388
Glenn Strauss36872db2022-01-22 05:06:31 -05001389 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001390 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001391 {
niisato8ee24222018-06-25 19:05:48 +09001392 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001393 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001394 else
1395 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001396 mbedtls_ssl_key_cert *cur = *head;
1397 while( cur->next != NULL )
1398 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001399 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001400 }
1401
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001402 return( 0 );
1403}
1404
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001405int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001406 mbedtls_x509_crt *own_cert,
1407 mbedtls_pk_context *pk_key )
1408{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001409 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001410}
1411
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001412void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001413 mbedtls_x509_crt *ca_chain,
1414 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001415{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001416 conf->ca_chain = ca_chain;
1417 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001418
1419#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1420 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1421 * cannot be used together. */
1422 conf->f_ca_cb = NULL;
1423 conf->p_ca_cb = NULL;
1424#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001425}
Hanno Becker5adaad92019-03-27 16:54:37 +00001426
1427#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1428void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001429 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001430 void *p_ca_cb )
1431{
1432 conf->f_ca_cb = f_ca_cb;
1433 conf->p_ca_cb = p_ca_cb;
1434
1435 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1436 * cannot be used together. */
1437 conf->ca_chain = NULL;
1438 conf->ca_crl = NULL;
1439}
1440#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001442
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001443#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001444const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1445 size_t *name_len )
1446{
1447 *name_len = ssl->handshake->sni_name_len;
1448 return( ssl->handshake->sni_name );
1449}
1450
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001451int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1452 mbedtls_x509_crt *own_cert,
1453 mbedtls_pk_context *pk_key )
1454{
1455 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1456 own_cert, pk_key ) );
1457}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001458
1459void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1460 mbedtls_x509_crt *ca_chain,
1461 mbedtls_x509_crl *ca_crl )
1462{
1463 ssl->handshake->sni_ca_chain = ca_chain;
1464 ssl->handshake->sni_ca_crl = ca_crl;
1465}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001466
1467void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1468 int authmode )
1469{
1470 ssl->handshake->sni_authmode = authmode;
1471}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001472#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1473
Hanno Becker8927c832019-04-03 12:52:50 +01001474#if defined(MBEDTLS_X509_CRT_PARSE_C)
1475void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1476 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1477 void *p_vrfy )
1478{
1479 ssl->f_vrfy = f_vrfy;
1480 ssl->p_vrfy = p_vrfy;
1481}
1482#endif
1483
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001484#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001485/*
1486 * Set EC J-PAKE password for current handshake
1487 */
1488int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1489 const unsigned char *pw,
1490 size_t pw_len )
1491{
1492 mbedtls_ecjpake_role role;
1493
Janos Follath8eb64132016-06-03 15:40:57 +01001494 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001495 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1496
1497 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1498 role = MBEDTLS_ECJPAKE_SERVER;
1499 else
1500 role = MBEDTLS_ECJPAKE_CLIENT;
1501
1502 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1503 role,
1504 MBEDTLS_MD_SHA256,
1505 MBEDTLS_ECP_DP_SECP256R1,
1506 pw, pw_len ) );
1507}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001508#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001509
Gilles Peskineeccd8882020-03-10 12:19:08 +01001510#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001511
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001512static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1513{
1514#if defined(MBEDTLS_USE_PSA_CRYPTO)
1515 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1516 return( 1 );
1517#endif /* MBEDTLS_USE_PSA_CRYPTO */
1518
1519 if( conf->psk != NULL )
1520 return( 1 );
1521
1522 return( 0 );
1523}
1524
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001525static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1526{
1527 /* Remove reference to existing PSK, if any. */
1528#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001529 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001530 {
1531 /* The maintenance of the PSK key slot is the
1532 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001533 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001534 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00001535 /* This and the following branch should never
1536 * be taken simultaenously as we maintain the
1537 * invariant that raw and opaque PSKs are never
1538 * configured simultaneously. As a safeguard,
1539 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001540#endif /* MBEDTLS_USE_PSA_CRYPTO */
1541 if( conf->psk != NULL )
1542 {
1543 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1544
1545 mbedtls_free( conf->psk );
1546 conf->psk = NULL;
1547 conf->psk_len = 0;
1548 }
1549
1550 /* Remove reference to PSK identity, if any. */
1551 if( conf->psk_identity != NULL )
1552 {
1553 mbedtls_free( conf->psk_identity );
1554 conf->psk_identity = NULL;
1555 conf->psk_identity_len = 0;
1556 }
1557}
1558
Hanno Becker7390c712018-11-15 13:33:04 +00001559/* This function assumes that PSK identity in the SSL config is unset.
1560 * It checks that the provided identity is well-formed and attempts
1561 * to make a copy of it in the SSL config.
1562 * On failure, the PSK identity in the config remains unset. */
1563static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1564 unsigned char const *psk_identity,
1565 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001566{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001567 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001568 if( psk_identity == NULL ||
1569 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001570 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001571 {
1572 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1573 }
1574
Hanno Becker7390c712018-11-15 13:33:04 +00001575 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1576 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001577 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001578
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001579 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001580 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001581
1582 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001583}
1584
Hanno Becker7390c712018-11-15 13:33:04 +00001585int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1586 const unsigned char *psk, size_t psk_len,
1587 const unsigned char *psk_identity, size_t psk_identity_len )
1588{
Janos Follath865b3eb2019-12-16 11:46:15 +00001589 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001590
1591 /* We currently only support one PSK, raw or opaque. */
1592 if( ssl_conf_psk_is_configured( conf ) )
1593 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001594
1595 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001596 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001597 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001598 if( psk_len == 0 )
1599 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1600 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1601 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1602
Hanno Becker7390c712018-11-15 13:33:04 +00001603 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1604 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1605 conf->psk_len = psk_len;
1606 memcpy( conf->psk, psk, conf->psk_len );
1607
1608 /* Check and set PSK Identity */
1609 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1610 if( ret != 0 )
1611 ssl_conf_remove_psk( conf );
1612
1613 return( ret );
1614}
1615
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001616static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1617{
1618#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001619 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001620 {
Ronald Croncf56a0a2020-08-04 09:51:30 +02001621 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001622 }
1623 else
1624#endif /* MBEDTLS_USE_PSA_CRYPTO */
1625 if( ssl->handshake->psk != NULL )
1626 {
1627 mbedtls_platform_zeroize( ssl->handshake->psk,
1628 ssl->handshake->psk_len );
1629 mbedtls_free( ssl->handshake->psk );
1630 ssl->handshake->psk_len = 0;
1631 }
1632}
1633
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001634int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1635 const unsigned char *psk, size_t psk_len )
1636{
1637 if( psk == NULL || ssl->handshake == NULL )
1638 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1639
1640 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1641 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1642
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001643 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001644
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001645 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001646 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001647
1648 ssl->handshake->psk_len = psk_len;
1649 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1650
1651 return( 0 );
1652}
1653
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001654#if defined(MBEDTLS_USE_PSA_CRYPTO)
1655int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001656 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001657 const unsigned char *psk_identity,
1658 size_t psk_identity_len )
1659{
Janos Follath865b3eb2019-12-16 11:46:15 +00001660 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001661
1662 /* We currently only support one PSK, raw or opaque. */
1663 if( ssl_conf_psk_is_configured( conf ) )
1664 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001665
Hanno Becker7390c712018-11-15 13:33:04 +00001666 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001667 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001668 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001669 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001670
1671 /* Check and set PSK Identity */
1672 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1673 psk_identity_len );
1674 if( ret != 0 )
1675 ssl_conf_remove_psk( conf );
1676
1677 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001678}
1679
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001680int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1681 mbedtls_svc_key_id_t psk )
1682{
1683 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1684 ( ssl->handshake == NULL ) )
1685 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1686
1687 ssl_remove_psk( ssl );
1688 ssl->handshake->psk_opaque = psk;
1689 return( 0 );
1690}
1691#endif /* MBEDTLS_USE_PSA_CRYPTO */
1692
1693void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1694 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1695 size_t),
1696 void *p_psk )
1697{
1698 conf->f_psk = f_psk;
1699 conf->p_psk = p_psk;
1700}
1701#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1702
1703#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01001704psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01001705 size_t taglen,
1706 psa_algorithm_t *alg,
1707 psa_key_type_t *key_type,
1708 size_t *key_size )
1709{
1710 switch ( mbedtls_cipher_type )
1711 {
1712 case MBEDTLS_CIPHER_AES_128_CBC:
1713 *alg = PSA_ALG_CBC_NO_PADDING;
1714 *key_type = PSA_KEY_TYPE_AES;
1715 *key_size = 128;
1716 break;
1717 case MBEDTLS_CIPHER_AES_128_CCM:
1718 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1719 *key_type = PSA_KEY_TYPE_AES;
1720 *key_size = 128;
1721 break;
1722 case MBEDTLS_CIPHER_AES_128_GCM:
1723 *alg = PSA_ALG_GCM;
1724 *key_type = PSA_KEY_TYPE_AES;
1725 *key_size = 128;
1726 break;
1727 case MBEDTLS_CIPHER_AES_192_CCM:
1728 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1729 *key_type = PSA_KEY_TYPE_AES;
1730 *key_size = 192;
1731 break;
1732 case MBEDTLS_CIPHER_AES_192_GCM:
1733 *alg = PSA_ALG_GCM;
1734 *key_type = PSA_KEY_TYPE_AES;
1735 *key_size = 192;
1736 break;
1737 case MBEDTLS_CIPHER_AES_256_CBC:
1738 *alg = PSA_ALG_CBC_NO_PADDING;
1739 *key_type = PSA_KEY_TYPE_AES;
1740 *key_size = 256;
1741 break;
1742 case MBEDTLS_CIPHER_AES_256_CCM:
1743 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1744 *key_type = PSA_KEY_TYPE_AES;
1745 *key_size = 256;
1746 break;
1747 case MBEDTLS_CIPHER_AES_256_GCM:
1748 *alg = PSA_ALG_GCM;
1749 *key_type = PSA_KEY_TYPE_AES;
1750 *key_size = 256;
1751 break;
1752 case MBEDTLS_CIPHER_ARIA_128_CBC:
1753 *alg = PSA_ALG_CBC_NO_PADDING;
1754 *key_type = PSA_KEY_TYPE_ARIA;
1755 *key_size = 128;
1756 break;
1757 case MBEDTLS_CIPHER_ARIA_128_CCM:
1758 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1759 *key_type = PSA_KEY_TYPE_ARIA;
1760 *key_size = 128;
1761 break;
1762 case MBEDTLS_CIPHER_ARIA_128_GCM:
1763 *alg = PSA_ALG_GCM;
1764 *key_type = PSA_KEY_TYPE_ARIA;
1765 *key_size = 128;
1766 break;
1767 case MBEDTLS_CIPHER_ARIA_192_CCM:
1768 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1769 *key_type = PSA_KEY_TYPE_ARIA;
1770 *key_size = 192;
1771 break;
1772 case MBEDTLS_CIPHER_ARIA_192_GCM:
1773 *alg = PSA_ALG_GCM;
1774 *key_type = PSA_KEY_TYPE_ARIA;
1775 *key_size = 192;
1776 break;
1777 case MBEDTLS_CIPHER_ARIA_256_CBC:
1778 *alg = PSA_ALG_CBC_NO_PADDING;
1779 *key_type = PSA_KEY_TYPE_ARIA;
1780 *key_size = 256;
1781 break;
1782 case MBEDTLS_CIPHER_ARIA_256_CCM:
1783 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1784 *key_type = PSA_KEY_TYPE_ARIA;
1785 *key_size = 256;
1786 break;
1787 case MBEDTLS_CIPHER_ARIA_256_GCM:
1788 *alg = PSA_ALG_GCM;
1789 *key_type = PSA_KEY_TYPE_ARIA;
1790 *key_size = 256;
1791 break;
1792 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
1793 *alg = PSA_ALG_CBC_NO_PADDING;
1794 *key_type = PSA_KEY_TYPE_CAMELLIA;
1795 *key_size = 128;
1796 break;
1797 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
1798 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1799 *key_type = PSA_KEY_TYPE_CAMELLIA;
1800 *key_size = 128;
1801 break;
1802 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
1803 *alg = PSA_ALG_GCM;
1804 *key_type = PSA_KEY_TYPE_CAMELLIA;
1805 *key_size = 128;
1806 break;
1807 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
1808 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1809 *key_type = PSA_KEY_TYPE_CAMELLIA;
1810 *key_size = 192;
1811 break;
1812 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
1813 *alg = PSA_ALG_GCM;
1814 *key_type = PSA_KEY_TYPE_CAMELLIA;
1815 *key_size = 192;
1816 break;
1817 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
1818 *alg = PSA_ALG_CBC_NO_PADDING;
1819 *key_type = PSA_KEY_TYPE_CAMELLIA;
1820 *key_size = 256;
1821 break;
1822 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
1823 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1824 *key_type = PSA_KEY_TYPE_CAMELLIA;
1825 *key_size = 256;
1826 break;
1827 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
1828 *alg = PSA_ALG_GCM;
1829 *key_type = PSA_KEY_TYPE_CAMELLIA;
1830 *key_size = 256;
1831 break;
1832 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
1833 *alg = PSA_ALG_CHACHA20_POLY1305;
1834 *key_type = PSA_KEY_TYPE_CHACHA20;
1835 *key_size = 256;
1836 break;
1837 case MBEDTLS_CIPHER_NULL:
1838 *alg = MBEDTLS_SSL_NULL_CIPHER;
1839 *key_type = 0;
1840 *key_size = 0;
1841 break;
1842 default:
1843 return PSA_ERROR_NOT_SUPPORTED;
1844 }
1845
1846 return PSA_SUCCESS;
1847}
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001848#endif /* MBEDTLS_USE_PSA_CRYPTO */
1849
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02001850#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01001851int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
1852 const unsigned char *dhm_P, size_t P_len,
1853 const unsigned char *dhm_G, size_t G_len )
1854{
Janos Follath865b3eb2019-12-16 11:46:15 +00001855 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01001856
Glenn Strausscee11292021-12-20 01:43:17 -05001857 mbedtls_mpi_free( &conf->dhm_P );
1858 mbedtls_mpi_free( &conf->dhm_G );
1859
Hanno Beckera90658f2017-10-04 15:29:08 +01001860 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
1861 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
1862 {
1863 mbedtls_mpi_free( &conf->dhm_P );
1864 mbedtls_mpi_free( &conf->dhm_G );
1865 return( ret );
1866 }
1867
1868 return( 0 );
1869}
Paul Bakker5121ce52009-01-03 21:22:43 +00001870
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001871int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00001872{
Janos Follath865b3eb2019-12-16 11:46:15 +00001873 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00001874
Glenn Strausscee11292021-12-20 01:43:17 -05001875 mbedtls_mpi_free( &conf->dhm_P );
1876 mbedtls_mpi_free( &conf->dhm_G );
1877
Gilles Peskinee5702482021-06-11 21:59:08 +02001878 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
1879 &conf->dhm_P ) ) != 0 ||
1880 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
1881 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01001882 {
1883 mbedtls_mpi_free( &conf->dhm_P );
1884 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00001885 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01001886 }
Paul Bakker1b57b062011-01-06 15:48:19 +00001887
1888 return( 0 );
1889}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02001890#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00001891
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02001892#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
1893/*
1894 * Set the minimum length for Diffie-Hellman parameters
1895 */
1896void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
1897 unsigned int bitlen )
1898{
1899 conf->dhm_min_bitlen = bitlen;
1900}
1901#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
1902
Gilles Peskineeccd8882020-03-10 12:19:08 +01001903#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08001904#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02001905/*
1906 * Set allowed/preferred hashes for handshake signatures
1907 */
1908void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
1909 const int *hashes )
1910{
1911 conf->sig_hashes = hashes;
1912}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08001913#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01001914
Jerry Yuf017ee42022-01-12 15:49:48 +08001915/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01001916void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
1917 const uint16_t* sig_algs )
1918{
Jerry Yuf017ee42022-01-12 15:49:48 +08001919#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1920 conf->sig_hashes = NULL;
1921#endif /* !MBEDTLS_DEPRECATED_REMOVED */
1922 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01001923}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001924#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02001925
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02001926#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01001927#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001928/*
1929 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01001930 *
1931 * mbedtls_ssl_setup() takes the provided list
1932 * and translates it to a list of IANA TLS group identifiers,
1933 * stored in ssl->handshake->group_list.
1934 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001935 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001936void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001937 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001938{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001939 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01001940 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001941}
Brett Warrene0edc842021-08-17 09:53:13 +01001942#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01001943#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001944
Brett Warrene0edc842021-08-17 09:53:13 +01001945/*
1946 * Set the allowed groups
1947 */
1948void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
1949 const uint16_t *group_list )
1950{
1951#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
1952 conf->curve_list = NULL;
1953#endif
1954 conf->group_list = group_list;
1955}
1956
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001957#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00001959{
Hanno Becker947194e2017-04-07 13:25:49 +01001960 /* Initialize to suppress unnecessary compiler warning */
1961 size_t hostname_len = 0;
1962
1963 /* Check if new hostname is valid before
1964 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01001965 if( hostname != NULL )
1966 {
1967 hostname_len = strlen( hostname );
1968
1969 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
1970 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1971 }
1972
1973 /* Now it's clear that we will overwrite the old hostname,
1974 * so we can free it safely */
1975
1976 if( ssl->hostname != NULL )
1977 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001978 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01001979 mbedtls_free( ssl->hostname );
1980 }
1981
1982 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01001983
Paul Bakker5121ce52009-01-03 21:22:43 +00001984 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01001985 {
1986 ssl->hostname = NULL;
1987 }
1988 else
1989 {
1990 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01001991 if( ssl->hostname == NULL )
1992 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02001993
Hanno Becker947194e2017-04-07 13:25:49 +01001994 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02001995
Hanno Becker947194e2017-04-07 13:25:49 +01001996 ssl->hostname[hostname_len] = '\0';
1997 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001998
1999 return( 0 );
2000}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002001#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002002
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002003#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002004void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002006 const unsigned char *, size_t),
2007 void *p_sni )
2008{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002009 conf->f_sni = f_sni;
2010 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002011}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002015int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002016{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002017 size_t cur_len, tot_len;
2018 const char **p;
2019
2020 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002021 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2022 * MUST NOT be truncated."
2023 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002024 */
2025 tot_len = 0;
2026 for( p = protos; *p != NULL; p++ )
2027 {
2028 cur_len = strlen( *p );
2029 tot_len += cur_len;
2030
Ronald Cron8216dd32020-04-23 16:41:44 +02002031 if( ( cur_len == 0 ) ||
2032 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2033 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002035 }
2036
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002037 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002038
2039 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002040}
2041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002043{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002044 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002045}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002047
Johan Pascalb62bb512015-12-03 21:56:45 +01002048#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002049void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2050 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002051{
2052 conf->dtls_srtp_mki_support = support_mki_value;
2053}
2054
Ron Eldoref72faf2018-07-12 11:54:20 +03002055int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2056 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002057 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002058{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002059 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002060 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002061 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002062 }
Ron Eldor591f1622018-01-22 12:30:04 +02002063
2064 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002065 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002066 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002067 }
Ron Eldor591f1622018-01-22 12:30:04 +02002068
2069 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2070 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002071 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002072}
2073
Ron Eldoref72faf2018-07-12 11:54:20 +03002074int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002075 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002076{
Johan Pascal253d0262020-09-22 13:04:45 +02002077 const mbedtls_ssl_srtp_profile *p;
2078 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002079
Johan Pascal253d0262020-09-22 13:04:45 +02002080 /* check the profiles list: all entry must be valid,
2081 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002082 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2083 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2084 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002085 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002086 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002087 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002088 list_size++;
2089 }
2090 else
2091 {
2092 /* unsupported value, stop parsing and set the size to an error value */
2093 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002094 }
2095 }
2096
Johan Pascal5ef72d22020-10-28 17:05:47 +01002097 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002098 {
Johan Pascal253d0262020-09-22 13:04:45 +02002099 conf->dtls_srtp_profile_list = NULL;
2100 conf->dtls_srtp_profile_list_len = 0;
2101 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2102 }
2103
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002104 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002105 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002106
2107 return( 0 );
2108}
2109
Johan Pascal5ef72d22020-10-28 17:05:47 +01002110void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2111 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002112{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002113 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2114 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002115 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002116 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002117 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002118 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002119 else
2120 {
2121 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002122 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2123 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002124 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002125}
Johan Pascalb62bb512015-12-03 21:56:45 +01002126#endif /* MBEDTLS_SSL_DTLS_SRTP */
2127
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002128void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002129{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002130 conf->max_major_ver = major;
2131 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002132}
2133
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002134void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002135{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002136 conf->min_major_ver = major;
2137 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002138}
2139
Janos Follath088ce432017-04-10 12:42:31 +01002140#if defined(MBEDTLS_SSL_SRV_C)
2141void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2142 char cert_req_ca_list )
2143{
2144 conf->cert_req_ca_list = cert_req_ca_list;
2145}
2146#endif
2147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002149void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002150{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002151 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002152}
2153#endif
2154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002156void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002157{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002158 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002159}
2160#endif
2161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002163int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002164{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002166 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002169 }
2170
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002171 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002172
2173 return( 0 );
2174}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002176
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002177void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002178{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002179 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002180}
2181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002183void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002184{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002185 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002186}
2187
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002188void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002189{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002190 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002191}
2192
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002193void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002194 const unsigned char period[8] )
2195{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002196 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002197}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002201#if defined(MBEDTLS_SSL_CLI_C)
2202void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002203{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002204 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002205}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002206#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002207
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002208#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002209void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2210 mbedtls_ssl_ticket_write_t *f_ticket_write,
2211 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2212 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002213{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002214 conf->f_ticket_write = f_ticket_write;
2215 conf->f_ticket_parse = f_ticket_parse;
2216 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002217}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002218#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002220
Hanno Becker7e6c1782021-06-08 09:24:55 +01002221void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2222 mbedtls_ssl_export_keys_t *f_export_keys,
2223 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002224{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002225 ssl->f_export_keys = f_export_keys;
2226 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002227}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002228
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002229#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002230void mbedtls_ssl_conf_async_private_cb(
2231 mbedtls_ssl_config *conf,
2232 mbedtls_ssl_async_sign_t *f_async_sign,
2233 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2234 mbedtls_ssl_async_resume_t *f_async_resume,
2235 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002236 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002237{
2238 conf->f_async_sign_start = f_async_sign;
2239 conf->f_async_decrypt_start = f_async_decrypt;
2240 conf->f_async_resume = f_async_resume;
2241 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002242 conf->p_async_config_data = async_config_data;
2243}
2244
Gilles Peskine8f97af72018-04-26 11:46:10 +02002245void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2246{
2247 return( conf->p_async_config_data );
2248}
2249
Gilles Peskine1febfef2018-04-30 11:54:39 +02002250void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002251{
2252 if( ssl->handshake == NULL )
2253 return( NULL );
2254 else
2255 return( ssl->handshake->user_async_ctx );
2256}
2257
Gilles Peskine1febfef2018-04-30 11:54:39 +02002258void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002259 void *ctx )
2260{
2261 if( ssl->handshake != NULL )
2262 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002263}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002264#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002265
Paul Bakker5121ce52009-01-03 21:22:43 +00002266/*
2267 * SSL get accessors
2268 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002269uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002270{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002271 if( ssl->session != NULL )
2272 return( ssl->session->verify_result );
2273
2274 if( ssl->session_negotiate != NULL )
2275 return( ssl->session_negotiate->verify_result );
2276
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002277 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002278}
2279
Glenn Strauss8f526902022-01-13 00:04:49 -05002280int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2281{
2282 if( ssl == NULL || ssl->session == NULL )
2283 return( 0 );
2284
2285 return( ssl->session->ciphersuite );
2286}
2287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002289{
Paul Bakker926c8e42013-03-06 10:23:34 +01002290 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002291 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002294}
2295
Gilles Peskinee1a0c252022-01-13 01:08:03 +01002296mbedtls_ssl_protocol_version mbedtls_ssl_get_version_number(
2297 const mbedtls_ssl_context *ssl )
2298{
2299 /* For major_ver, only 3 is supported, so skip checking it. */
2300 switch( ssl->minor_ver )
2301 {
2302 case MBEDTLS_SSL_MINOR_VERSION_3:
2303 return( MBEDTLS_SSL_VERSION_1_2 );
2304 case MBEDTLS_SSL_MINOR_VERSION_4:
2305 return( MBEDTLS_SSL_VERSION_1_3 );
2306 default:
2307 return( MBEDTLS_SSL_VERSION_UNKNOWN );
2308 }
2309}
2310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002312{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002314 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002315 {
2316 switch( ssl->minor_ver )
2317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002319 return( "DTLSv1.2" );
2320
2321 default:
2322 return( "unknown (DTLS)" );
2323 }
2324 }
2325#endif
2326
Paul Bakker43ca69c2011-01-15 17:35:19 +00002327 switch( ssl->minor_ver )
2328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002330 return( "TLSv1.2" );
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002331 case MBEDTLS_SSL_MINOR_VERSION_4:
2332 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002333 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002334 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002335 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002336}
2337
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002338#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002339size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2340{
David Horstmann95d516f2021-05-04 18:36:56 +01002341 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002342 size_t read_mfl;
2343
2344 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2345 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2346 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2347 {
2348 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2349 }
2350
2351 /* Check if a smaller max length was negotiated */
2352 if( ssl->session_out != NULL )
2353 {
2354 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2355 if( read_mfl < max_len )
2356 {
2357 max_len = read_mfl;
2358 }
2359 }
2360
2361 // During a handshake, use the value being negotiated
2362 if( ssl->session_negotiate != NULL )
2363 {
2364 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2365 if( read_mfl < max_len )
2366 {
2367 max_len = read_mfl;
2368 }
2369 }
2370
2371 return( max_len );
2372}
2373
2374size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002375{
2376 size_t max_len;
2377
2378 /*
2379 * Assume mfl_code is correct since it was checked when set
2380 */
Angus Grattond8213d02016-05-25 20:56:48 +10002381 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002382
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002383 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002384 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002385 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002386 {
Angus Grattond8213d02016-05-25 20:56:48 +10002387 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002388 }
2389
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002390 /* During a handshake, use the value being negotiated */
2391 if( ssl->session_negotiate != NULL &&
2392 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2393 {
2394 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2395 }
2396
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002397 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002398}
2399#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2400
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002401#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002402size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002403{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002404 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2405 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2406 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2407 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2408 return ( 0 );
2409
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002410 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2411 return( ssl->mtu );
2412
2413 if( ssl->mtu == 0 )
2414 return( ssl->handshake->mtu );
2415
2416 return( ssl->mtu < ssl->handshake->mtu ?
2417 ssl->mtu : ssl->handshake->mtu );
2418}
2419#endif /* MBEDTLS_SSL_PROTO_DTLS */
2420
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002421int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2422{
2423 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2424
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002425#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2426 !defined(MBEDTLS_SSL_PROTO_DTLS)
2427 (void) ssl;
2428#endif
2429
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002430#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002431 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002432
2433 if( max_len > mfl )
2434 max_len = mfl;
2435#endif
2436
2437#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002438 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002439 {
Hanno Becker89490712020-02-05 10:50:12 +00002440 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002441 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2442 const size_t overhead = (size_t) ret;
2443
2444 if( ret < 0 )
2445 return( ret );
2446
2447 if( mtu <= overhead )
2448 {
2449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2450 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2451 }
2452
2453 if( max_len > mtu - overhead )
2454 max_len = mtu - overhead;
2455 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002456#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002457
Hanno Becker0defedb2018-08-10 12:35:02 +01002458#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2459 !defined(MBEDTLS_SSL_PROTO_DTLS)
2460 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002461#endif
2462
2463 return( (int) max_len );
2464}
2465
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002466int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2467{
2468 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2469
2470#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2471 (void) ssl;
2472#endif
2473
2474#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2475 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2476
2477 if( max_len > mfl )
2478 max_len = mfl;
2479#endif
2480
2481 return( (int) max_len );
2482}
2483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484#if defined(MBEDTLS_X509_CRT_PARSE_C)
2485const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002486{
2487 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002488 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002489
Hanno Beckere6824572019-02-07 13:18:46 +00002490#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002491 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002492#else
2493 return( NULL );
2494#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002495}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002499int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2500 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002501{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002502 int ret;
2503
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002504 if( ssl == NULL ||
2505 dst == NULL ||
2506 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002507 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002510 }
2511
Hanno Beckere810bbc2021-05-14 16:01:05 +01002512 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2513 * idempotent: Each session can only be exported once.
2514 *
2515 * (This is in preparation for TLS 1.3 support where we will
2516 * need the ability to export multiple sessions (aka tickets),
2517 * which will be achieved by calling mbedtls_ssl_get_session()
2518 * multiple times until it fails.)
2519 *
2520 * Check whether we have already exported the current session,
2521 * and fail if so.
2522 */
2523 if( ssl->session->exported == 1 )
2524 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2525
2526 ret = mbedtls_ssl_session_copy( dst, ssl->session );
2527 if( ret != 0 )
2528 return( ret );
2529
2530 /* Remember that we've exported the session. */
2531 ssl->session->exported = 1;
2532 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002533}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002535
Paul Bakker5121ce52009-01-03 21:22:43 +00002536/*
Hanno Beckera835da52019-05-16 12:39:07 +01002537 * Define ticket header determining Mbed TLS version
2538 * and structure of the ticket.
2539 */
2540
Hanno Becker94ef3b32019-05-16 12:50:45 +01002541/*
Hanno Becker50b59662019-05-28 14:30:45 +01002542 * Define bitflag determining compile-time settings influencing
2543 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01002544 */
2545
Hanno Becker50b59662019-05-28 14:30:45 +01002546#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01002547#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01002548#else
Hanno Becker3e088662019-05-29 11:10:18 +01002549#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002550#endif /* MBEDTLS_HAVE_TIME */
2551
2552#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01002553#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002554#else
Hanno Becker3e088662019-05-29 11:10:18 +01002555#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002556#endif /* MBEDTLS_X509_CRT_PARSE_C */
2557
2558#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01002559#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002560#else
Hanno Becker3e088662019-05-29 11:10:18 +01002561#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002562#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
2563
2564#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01002565#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002566#else
Hanno Becker3e088662019-05-29 11:10:18 +01002567#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002568#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2569
Hanno Becker94ef3b32019-05-16 12:50:45 +01002570#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01002571#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002572#else
Hanno Becker3e088662019-05-29 11:10:18 +01002573#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002574#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2575
Hanno Becker94ef3b32019-05-16 12:50:45 +01002576#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2577#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
2578#else
2579#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
2580#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2581
Hanno Becker3e088662019-05-29 11:10:18 +01002582#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
2583#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
2584#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
2585#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01002586#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
2587#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01002588
Hanno Becker50b59662019-05-28 14:30:45 +01002589#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01002590 ( (uint16_t) ( \
2591 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
2592 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
2593 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
2594 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01002595 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01002596 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01002597
Hanno Beckerf8787072019-05-16 12:41:07 +01002598static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01002599 MBEDTLS_VERSION_MAJOR,
2600 MBEDTLS_VERSION_MINOR,
2601 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01002602 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
2603 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01002604};
Hanno Beckera835da52019-05-16 12:39:07 +01002605
2606/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002607 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002608 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002609 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002610 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002611 *
Hanno Beckerdce50972021-08-01 05:39:23 +01002612 * opaque mbedtls_version[3]; // library version: major, minor, patch
2613 * opaque session_format[2]; // library-version specific 16-bit field
2614 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002615 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002616 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002617 * Note: When updating the format, remember to keep
2618 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002619 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002620 * // In this version, `session_format` determines
2621 * // the setting of those compile-time
2622 * // configuration options which influence
2623 * // the structure of mbedtls_ssl_session.
2624 *
Hanno Beckerfa0d61e2021-08-02 08:56:14 +01002625 * uint8_t minor_ver; // Protocol-version. Possible values:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002626 * // - TLS 1.2 (MBEDTLS_SSL_MINOR_VERSION_3)
2627 *
2628 * select (serialized_session.minor_ver) {
2629 *
2630 * case MBEDTLS_SSL_MINOR_VERSION_3: // TLS 1.2
2631 * serialized_session_tls12 data;
2632 *
2633 * };
2634 *
2635 * } serialized_session;
2636 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002637 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002638
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002639static int ssl_session_save( const mbedtls_ssl_session *session,
2640 unsigned char omit_header,
2641 unsigned char *buf,
2642 size_t buf_len,
2643 size_t *olen )
2644{
2645 unsigned char *p = buf;
2646 size_t used = 0;
2647
2648 if( !omit_header )
2649 {
2650 /*
2651 * Add Mbed TLS version identifier
2652 */
2653
2654 used += sizeof( ssl_serialized_session_header );
2655
2656 if( used <= buf_len )
2657 {
2658 memcpy( p, ssl_serialized_session_header,
2659 sizeof( ssl_serialized_session_header ) );
2660 p += sizeof( ssl_serialized_session_header );
2661 }
2662 }
2663
2664 /*
2665 * TLS version identifier
2666 */
2667 used += 1;
2668 if( used <= buf_len )
2669 {
2670 *p++ = session->minor_ver;
2671 }
2672
2673 /* Forward to version-specific serialization routine. */
2674 switch( session->minor_ver )
2675 {
2676#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2677 case MBEDTLS_SSL_MINOR_VERSION_3:
2678 {
2679 size_t remaining_len = used <= buf_len ? buf_len - used : 0;
2680 used += ssl_session_save_tls12( session, p, remaining_len );
2681 break;
2682 }
2683#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2684
2685 default:
2686 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2687 }
2688
2689 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02002690 if( used > buf_len )
2691 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002692
2693 return( 0 );
2694}
2695
2696/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02002697 * Public wrapper for ssl_session_save()
2698 */
2699int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
2700 unsigned char *buf,
2701 size_t buf_len,
2702 size_t *olen )
2703{
2704 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
2705}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002706
Jerry Yuf1b23ca2022-02-18 11:48:47 +08002707/*
2708 * Deserialize session, see mbedtls_ssl_session_save() for format.
2709 *
2710 * This internal version is wrapped by a public function that cleans up in
2711 * case of error, and has an extra option omit_header.
2712 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002713static int ssl_session_load( mbedtls_ssl_session *session,
2714 unsigned char omit_header,
2715 const unsigned char *buf,
2716 size_t len )
2717{
2718 const unsigned char *p = buf;
2719 const unsigned char * const end = buf + len;
2720
2721 if( !omit_header )
2722 {
2723 /*
2724 * Check Mbed TLS version identifier
2725 */
2726
2727 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
2728 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2729
2730 if( memcmp( p, ssl_serialized_session_header,
2731 sizeof( ssl_serialized_session_header ) ) != 0 )
2732 {
2733 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
2734 }
2735 p += sizeof( ssl_serialized_session_header );
2736 }
2737
2738 /*
2739 * TLS version identifier
2740 */
2741 if( 1 > (size_t)( end - p ) )
2742 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2743 session->minor_ver = *p++;
2744
2745 /* Dispatch according to TLS version. */
2746 switch( session->minor_ver )
2747 {
2748#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2749 case MBEDTLS_SSL_MINOR_VERSION_3: /* TLS 1.2 */
2750 {
2751 size_t remaining_len = ( end - p );
2752 return( ssl_session_load_tls12( session, p, remaining_len ) );
2753 }
2754#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2755
2756 default:
2757 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2758 }
2759}
2760
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002761/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02002762 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02002763 */
2764int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
2765 const unsigned char *buf,
2766 size_t len )
2767{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02002768 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02002769
2770 if( ret != 0 )
2771 mbedtls_ssl_session_free( session );
2772
2773 return( ret );
2774}
2775
2776/*
Paul Bakker1961b702013-01-25 14:49:24 +01002777 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 */
Hanno Becker41934dd2021-08-07 19:13:43 +01002779static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
2780{
2781 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2782
Ronald Cron66dbf912022-02-02 15:33:46 +01002783 /*
2784 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01002785 * that were written into the output buffer by the previous handshake step,
2786 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01002787 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
2788 * We proceed to the next handshake step only when all data from the
2789 * previous one have been sent to the peer, thus we make sure that this is
2790 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
2791 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
2792 * we have to wait before to go ahead.
2793 * In the case of TLS 1.3, handshake step handlers do not send data to the
2794 * peer. Data are only sent here and through
2795 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
2796 * alert occured.
2797 */
Hanno Becker41934dd2021-08-07 19:13:43 +01002798 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2799 return( ret );
2800
2801#if defined(MBEDTLS_SSL_PROTO_DTLS)
2802 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2803 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
2804 {
2805 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
2806 return( ret );
2807 }
2808#endif /* MBEDTLS_SSL_PROTO_DTLS */
2809
2810 return( ret );
2811}
2812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002813int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002814{
Hanno Becker41934dd2021-08-07 19:13:43 +01002815 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002816
Hanno Becker41934dd2021-08-07 19:13:43 +01002817 if( ssl == NULL ||
2818 ssl->conf == NULL ||
2819 ssl->handshake == NULL ||
2820 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2821 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002822 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01002823 }
2824
2825 ret = ssl_prepare_handshake_step( ssl );
2826 if( ret != 0 )
2827 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002828
Jerry Yue7047812021-09-13 19:26:39 +08002829 ret = mbedtls_ssl_handle_pending_alert( ssl );
2830 if( ret != 0 )
2831 goto cleanup;
2832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002834 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08002835 {
Ronald Cron27c85e72022-03-08 11:37:55 +01002836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
2837 mbedtls_ssl_states_str( ssl->state ) ) );
2838
Ronald Cron9f0fba32022-02-10 16:45:15 +01002839 switch( ssl->state )
2840 {
2841 case MBEDTLS_SSL_HELLO_REQUEST:
2842 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
2843 break;
Jerry Yub9930e72021-08-06 17:11:51 +08002844
Ronald Cron9f0fba32022-02-10 16:45:15 +01002845 case MBEDTLS_SSL_CLIENT_HELLO:
2846 ret = mbedtls_ssl_write_client_hello( ssl );
2847 break;
2848
2849 default:
2850#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
2851 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
2852 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
2853 else
2854 ret = mbedtls_ssl_handshake_client_step( ssl );
2855#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
2856 ret = mbedtls_ssl_handshake_client_step( ssl );
2857#else
2858 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
2859#endif
2860 }
Jerry Yub9930e72021-08-06 17:11:51 +08002861 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002862#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002864 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08002865 {
Ronald Cron6f135e12021-12-08 16:57:54 +01002866#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08002867 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08002868 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01002869#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08002870
2871#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2872 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
2873 ret = mbedtls_ssl_handshake_server_step( ssl );
2874#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2875 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002876#endif
2877
Jerry Yue7047812021-09-13 19:26:39 +08002878 if( ret != 0 )
2879 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08002880 /* handshake_step return error. And it is same
2881 * with alert_reason.
2882 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08002883 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08002884 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08002885 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08002886 goto cleanup;
2887 }
2888 }
2889
2890cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01002891 return( ret );
2892}
2893
2894/*
2895 * Perform the SSL handshake
2896 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01002898{
2899 int ret = 0;
2900
Hanno Beckera817ea42020-10-20 15:20:23 +01002901 /* Sanity checks */
2902
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002903 if( ssl == NULL || ssl->conf == NULL )
2904 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2905
Hanno Beckera817ea42020-10-20 15:20:23 +01002906#if defined(MBEDTLS_SSL_PROTO_DTLS)
2907 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2908 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
2909 {
2910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2911 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2912 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2913 }
2914#endif /* MBEDTLS_SSL_PROTO_DTLS */
2915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01002917
Hanno Beckera817ea42020-10-20 15:20:23 +01002918 /* Main handshake loop */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01002920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002922
2923 if( ret != 0 )
2924 break;
2925 }
2926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002927 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002928
2929 return( ret );
2930}
2931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932#if defined(MBEDTLS_SSL_RENEGOTIATION)
2933#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002934/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002935 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00002936 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002938{
Janos Follath865b3eb2019-12-16 11:46:15 +00002939 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002942
2943 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002944 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2945 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002946
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002947 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002948 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002949 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002950 return( ret );
2951 }
2952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002954
2955 return( 0 );
2956}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002958
2959/*
2960 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961 * - any side: calling mbedtls_ssl_renegotiate(),
2962 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
2963 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02002964 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002965 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002966 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002967 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00002968int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00002969{
Janos Follath865b3eb2019-12-16 11:46:15 +00002970 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00002971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002973
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002974 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
2975 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00002976
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02002977 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
2978 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002980 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02002982 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002983 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002984 ssl->handshake->out_msg_seq = 1;
2985 else
2986 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02002987 }
2988#endif
2989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002990 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
2991 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00002992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00002994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00002996 return( ret );
2997 }
2998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003000
3001 return( 0 );
3002}
3003
3004/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003005 * Renegotiate current connection on client,
3006 * or request renegotiation on server
3007 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003009{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003011
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003012 if( ssl == NULL || ssl->conf == NULL )
3013 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003016 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003017 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
3020 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003023
3024 /* Did we already try/start sending HelloRequest? */
3025 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003027
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003028 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003029 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003033 /*
3034 * On client, either start the renegotiation process or,
3035 * if already in progress, continue the handshake
3036 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003039 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
3040 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003041
Hanno Becker40cdaa12020-02-05 10:48:27 +00003042 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003043 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003044 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003045 return( ret );
3046 }
3047 }
3048 else
3049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003050 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003053 return( ret );
3054 }
3055 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003057
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003058 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003059}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003061
Gilles Peskine9b562d52018-04-25 20:32:43 +02003062void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003063{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003064 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3065
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003066 if( handshake == NULL )
3067 return;
3068
Brett Warrene0edc842021-08-17 09:53:13 +01003069#if defined(MBEDTLS_ECP_C)
3070#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3071 if ( ssl->handshake->group_list_heap_allocated )
3072 mbedtls_free( (void*) handshake->group_list );
3073 handshake->group_list = NULL;
3074#endif /* MBEDTLS_DEPRECATED_REMOVED */
3075#endif /* MBEDTLS_ECP_C */
3076
Jerry Yuf017ee42022-01-12 15:49:48 +08003077#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3078#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3079 if ( ssl->handshake->sig_algs_heap_allocated )
3080 mbedtls_free( (void*) handshake->sig_algs );
3081 handshake->sig_algs = NULL;
3082#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003083#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3084 if( ssl->handshake->certificate_request_context )
3085 {
3086 mbedtls_free( (void*) handshake->certificate_request_context );
3087 }
3088#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003089#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3090
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003091#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3092 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3093 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003094 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003095 handshake->async_in_progress = 0;
3096 }
3097#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3098
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003099#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003100#if defined(MBEDTLS_USE_PSA_CRYPTO)
3101 psa_hash_abort( &handshake->fin_sha256_psa );
3102#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003103 mbedtls_sha256_free( &handshake->fin_sha256 );
3104#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003105#endif
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +02003106#if defined(MBEDTLS_SHA384_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003107#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003108 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003109#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003110 mbedtls_sha512_free( &handshake->fin_sha512 );
3111#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003112#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003114#if defined(MBEDTLS_DHM_C)
3115 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003116#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117#if defined(MBEDTLS_ECDH_C)
3118 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003119#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003120#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003121 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003122#if defined(MBEDTLS_SSL_CLI_C)
3123 mbedtls_free( handshake->ecjpake_cache );
3124 handshake->ecjpake_cache = NULL;
3125 handshake->ecjpake_cache_len = 0;
3126#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003127#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003128
Janos Follath4ae5c292016-02-10 11:27:43 +00003129#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3130 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003131 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003133#endif
3134
Gilles Peskineeccd8882020-03-10 12:19:08 +01003135#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003136 if( handshake->psk != NULL )
3137 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003138 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003139 mbedtls_free( handshake->psk );
3140 }
3141#endif
3142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3144 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003145 /*
3146 * Free only the linked list wrapper, not the keys themselves
3147 * since the belong to the SNI callback
3148 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003149 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003151
Gilles Peskineeccd8882020-03-10 12:19:08 +01003152#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003153 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003154 if( handshake->ecrs_peer_cert != NULL )
3155 {
3156 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3157 mbedtls_free( handshake->ecrs_peer_cert );
3158 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003159#endif
3160
Hanno Becker75173122019-02-06 16:18:31 +00003161#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3162 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3163 mbedtls_pk_free( &handshake->peer_pubkey );
3164#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3165
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003166#if defined(MBEDTLS_SSL_CLI_C) && \
3167 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3168 mbedtls_free( handshake->cookie );
3169#endif /* MBEDTLS_SSL_CLI_C &&
3170 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003171
3172#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003173 mbedtls_ssl_flight_free( handshake->flight );
3174 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003175#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003176
Ronald Cronf12b81d2022-03-15 10:42:41 +01003177#if defined(MBEDTLS_ECDH_C) && \
3178 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Hanno Becker4a63ed42019-01-08 11:39:35 +00003179 psa_destroy_key( handshake->ecdh_psa_privkey );
3180#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3181
Ronald Cron6f135e12021-12-08 16:57:54 +01003182#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003183 mbedtls_ssl_transform_free( handshake->transform_handshake );
3184 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003185 mbedtls_free( handshake->transform_earlydata );
3186 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003187#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003188
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003189
3190#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3191 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3192 * processes datagrams and the fact that a datagram is allowed to have
3193 * several records in it, it is possible that the I/O buffers are not
3194 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003195 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3196 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003197#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003198
Jerry Yuba9c7272021-10-30 11:54:10 +08003199 /* mbedtls_platform_zeroize MUST be last one in this function */
3200 mbedtls_platform_zeroize( handshake,
3201 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003202}
3203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003205{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003206 if( session == NULL )
3207 return;
3208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003210 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003211#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003212
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003213#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003215#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003216
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003217 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003218}
3219
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003220#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003221
3222#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3223#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3224#else
3225#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3226#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3227
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003228#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003229
3230#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3231#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3232#else
3233#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3234#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3235
3236#if defined(MBEDTLS_SSL_ALPN)
3237#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3238#else
3239#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3240#endif /* MBEDTLS_SSL_ALPN */
3241
3242#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3243#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3244#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3245#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3246
3247#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3248 ( (uint32_t) ( \
3249 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3250 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3251 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3252 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3253 0u ) )
3254
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003255static unsigned char ssl_serialized_context_header[] = {
3256 MBEDTLS_VERSION_MAJOR,
3257 MBEDTLS_VERSION_MINOR,
3258 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003259 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3260 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3261 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3262 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3263 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003264};
3265
Paul Bakker5121ce52009-01-03 21:22:43 +00003266/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003267 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003268 *
3269 * The format of the serialized data is:
3270 * (in the presentation language of TLS, RFC 8446 section 3)
3271 *
3272 * // header
3273 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003274 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003275 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003276 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003277 * Note: When updating the format, remember to keep these
3278 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003279 *
3280 * // session sub-structure
3281 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3282 * // transform sub-structure
3283 * uint8 random[64]; // ServerHello.random+ClientHello.random
3284 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3285 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3286 * // fields from ssl_context
3287 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3288 * uint64 in_window_top; // DTLS: last validated record seq_num
3289 * uint64 in_window; // DTLS: bitmask for replay protection
3290 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3291 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3292 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3293 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3294 *
3295 * Note that many fields of the ssl_context or sub-structures are not
3296 * serialized, as they fall in one of the following categories:
3297 *
3298 * 1. forced value (eg in_left must be 0)
3299 * 2. pointer to dynamically-allocated memory (eg session, transform)
3300 * 3. value can be re-derived from other data (eg session keys from MS)
3301 * 4. value was temporary (eg content of input buffer)
3302 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003303 */
3304int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3305 unsigned char *buf,
3306 size_t buf_len,
3307 size_t *olen )
3308{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003309 unsigned char *p = buf;
3310 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003311 size_t session_len;
3312 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003313
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003314 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003315 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3316 * this function's documentation.
3317 *
3318 * These are due to assumptions/limitations in the implementation. Some of
3319 * them are likely to stay (no handshake in progress) some might go away
3320 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003321 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003322 /* The initial handshake must be over */
3323 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003324 {
3325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003326 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003327 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003328 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003329 {
3330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003331 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003332 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003333 /* Double-check that sub-structures are indeed ready */
3334 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003335 {
3336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003337 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003338 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003339 /* There must be no pending incoming or outgoing data */
3340 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003341 {
3342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003343 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003344 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003345 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003346 {
3347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003348 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003349 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003350 /* Protocol must be DLTS, not TLS */
3351 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003352 {
3353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003354 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003355 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003356 /* Version must be 1.2 */
3357 if( ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003358 {
3359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003360 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003361 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003362 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003363 {
3364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003365 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003366 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003367 /* We must be using an AEAD ciphersuite */
3368 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003369 {
3370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
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 /* Renegotiation must not be enabled */
3374#if defined(MBEDTLS_SSL_RENEGOTIATION)
3375 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003376 {
3377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003378 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003379 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003380#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003381
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003382 /*
3383 * Version and format identifier
3384 */
3385 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003386
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003387 if( used <= buf_len )
3388 {
3389 memcpy( p, ssl_serialized_context_header,
3390 sizeof( ssl_serialized_context_header ) );
3391 p += sizeof( ssl_serialized_context_header );
3392 }
3393
3394 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003395 * Session (length + data)
3396 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003397 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003398 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3399 return( ret );
3400
3401 used += 4 + session_len;
3402 if( used <= buf_len )
3403 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003404 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3405 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003406
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003407 ret = ssl_session_save( ssl->session, 1,
3408 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003409 if( ret != 0 )
3410 return( ret );
3411
3412 p += session_len;
3413 }
3414
3415 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003416 * Transform
3417 */
3418 used += sizeof( ssl->transform->randbytes );
3419 if( used <= buf_len )
3420 {
3421 memcpy( p, ssl->transform->randbytes,
3422 sizeof( ssl->transform->randbytes ) );
3423 p += sizeof( ssl->transform->randbytes );
3424 }
3425
3426#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3427 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3428 if( used <= buf_len )
3429 {
3430 *p++ = ssl->transform->in_cid_len;
3431 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3432 p += ssl->transform->in_cid_len;
3433
3434 *p++ = ssl->transform->out_cid_len;
3435 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3436 p += ssl->transform->out_cid_len;
3437 }
3438#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3439
3440 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003441 * Saved fields from top-level ssl_context structure
3442 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003443 used += 4;
3444 if( used <= buf_len )
3445 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003446 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3447 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003448 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003449
3450#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3451 used += 16;
3452 if( used <= buf_len )
3453 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003454 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3455 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003456
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003457 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3458 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003459 }
3460#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3461
3462#if defined(MBEDTLS_SSL_PROTO_DTLS)
3463 used += 1;
3464 if( used <= buf_len )
3465 {
3466 *p++ = ssl->disable_datagram_packing;
3467 }
3468#endif /* MBEDTLS_SSL_PROTO_DTLS */
3469
Jerry Yuae0b2e22021-10-08 15:21:19 +08003470 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003471 if( used <= buf_len )
3472 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003473 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3474 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003475 }
3476
3477#if defined(MBEDTLS_SSL_PROTO_DTLS)
3478 used += 2;
3479 if( used <= buf_len )
3480 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003481 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
3482 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003483 }
3484#endif /* MBEDTLS_SSL_PROTO_DTLS */
3485
3486#if defined(MBEDTLS_SSL_ALPN)
3487 {
3488 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02003489 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003490 : 0;
3491
3492 used += 1 + alpn_len;
3493 if( used <= buf_len )
3494 {
3495 *p++ = alpn_len;
3496
3497 if( ssl->alpn_chosen != NULL )
3498 {
3499 memcpy( p, ssl->alpn_chosen, alpn_len );
3500 p += alpn_len;
3501 }
3502 }
3503 }
3504#endif /* MBEDTLS_SSL_ALPN */
3505
3506 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003507 * Done
3508 */
3509 *olen = used;
3510
3511 if( used > buf_len )
3512 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003513
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003514 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
3515
Hanno Becker43aefe22020-02-05 10:44:56 +00003516 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003517}
3518
3519/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003520 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003521 *
3522 * This internal version is wrapped by a public function that cleans up in
3523 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003524 */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003525static int ssl_context_load( mbedtls_ssl_context *ssl,
3526 const unsigned char *buf,
3527 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003528{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003529 const unsigned char *p = buf;
3530 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003531 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00003532 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003533
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003534 /*
3535 * The context should have been freshly setup or reset.
3536 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02003537 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003538 * renegotiating, or if the user mistakenly loaded a session first.)
3539 */
3540 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
3541 ssl->session != NULL )
3542 {
3543 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3544 }
3545
3546 /*
3547 * We can't check that the config matches the initial one, but we can at
3548 * least check it matches the requirements for serializing.
3549 */
3550 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
3551 ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
3552 ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 ||
3553 ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ||
3554 ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 ||
3555#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003556 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003557#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003558 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003559 {
3560 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3561 }
3562
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003563 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
3564
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003565 /*
3566 * Check version identifier
3567 */
3568 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
3569 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3570
3571 if( memcmp( p, ssl_serialized_context_header,
3572 sizeof( ssl_serialized_context_header ) ) != 0 )
3573 {
3574 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3575 }
3576 p += sizeof( ssl_serialized_context_header );
3577
3578 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003579 * Session
3580 */
3581 if( (size_t)( end - p ) < 4 )
3582 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3583
3584 session_len = ( (size_t) p[0] << 24 ) |
3585 ( (size_t) p[1] << 16 ) |
3586 ( (size_t) p[2] << 8 ) |
3587 ( (size_t) p[3] );
3588 p += 4;
3589
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003590 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00003591 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003592 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003593 ssl->session_in = ssl->session;
3594 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003595 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003596
3597 if( (size_t)( end - p ) < session_len )
3598 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3599
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003600 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003601 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003602 {
3603 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003604 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003605 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003606
3607 p += session_len;
3608
3609 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003610 * Transform
3611 */
3612
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003613 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00003614 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003615 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003616 ssl->transform_in = ssl->transform;
3617 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003618 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003619
3620 /* Read random bytes and populate structure */
3621 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
3622 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08003623#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00003624 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003625 ssl->session->ciphersuite,
3626 ssl->session->master,
Hanno Beckerbd257552021-03-22 06:59:27 +00003627#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
3628 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003629 ssl->session->encrypt_then_mac,
Hanno Beckerbd257552021-03-22 06:59:27 +00003630#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
3631 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003632 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
3633 p, /* currently pointing to randbytes */
3634 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
3635 ssl->conf->endpoint,
3636 ssl );
3637 if( ret != 0 )
3638 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08003639#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003640 p += sizeof( ssl->transform->randbytes );
3641
3642#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3643 /* Read connection IDs and store them */
3644 if( (size_t)( end - p ) < 1 )
3645 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3646
3647 ssl->transform->in_cid_len = *p++;
3648
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02003649 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003650 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3651
3652 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
3653 p += ssl->transform->in_cid_len;
3654
3655 ssl->transform->out_cid_len = *p++;
3656
3657 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
3658 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3659
3660 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
3661 p += ssl->transform->out_cid_len;
3662#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3663
3664 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003665 * Saved fields from top-level ssl_context structure
3666 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003667 if( (size_t)( end - p ) < 4 )
3668 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3669
3670 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
3671 ( (uint32_t) p[1] << 16 ) |
3672 ( (uint32_t) p[2] << 8 ) |
3673 ( (uint32_t) p[3] );
3674 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003675
3676#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3677 if( (size_t)( end - p ) < 16 )
3678 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3679
3680 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
3681 ( (uint64_t) p[1] << 48 ) |
3682 ( (uint64_t) p[2] << 40 ) |
3683 ( (uint64_t) p[3] << 32 ) |
3684 ( (uint64_t) p[4] << 24 ) |
3685 ( (uint64_t) p[5] << 16 ) |
3686 ( (uint64_t) p[6] << 8 ) |
3687 ( (uint64_t) p[7] );
3688 p += 8;
3689
3690 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
3691 ( (uint64_t) p[1] << 48 ) |
3692 ( (uint64_t) p[2] << 40 ) |
3693 ( (uint64_t) p[3] << 32 ) |
3694 ( (uint64_t) p[4] << 24 ) |
3695 ( (uint64_t) p[5] << 16 ) |
3696 ( (uint64_t) p[6] << 8 ) |
3697 ( (uint64_t) p[7] );
3698 p += 8;
3699#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3700
3701#if defined(MBEDTLS_SSL_PROTO_DTLS)
3702 if( (size_t)( end - p ) < 1 )
3703 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3704
3705 ssl->disable_datagram_packing = *p++;
3706#endif /* MBEDTLS_SSL_PROTO_DTLS */
3707
Jerry Yud9a94fe2021-09-28 18:58:59 +08003708 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003709 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08003710 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
3711 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003712
3713#if defined(MBEDTLS_SSL_PROTO_DTLS)
3714 if( (size_t)( end - p ) < 2 )
3715 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3716
3717 ssl->mtu = ( p[0] << 8 ) | p[1];
3718 p += 2;
3719#endif /* MBEDTLS_SSL_PROTO_DTLS */
3720
3721#if defined(MBEDTLS_SSL_ALPN)
3722 {
3723 uint8_t alpn_len;
3724 const char **cur;
3725
3726 if( (size_t)( end - p ) < 1 )
3727 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3728
3729 alpn_len = *p++;
3730
3731 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
3732 {
3733 /* alpn_chosen should point to an item in the configured list */
3734 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
3735 {
3736 if( strlen( *cur ) == alpn_len &&
3737 memcmp( p, cur, alpn_len ) == 0 )
3738 {
3739 ssl->alpn_chosen = *cur;
3740 break;
3741 }
3742 }
3743 }
3744
3745 /* can only happen on conf mismatch */
3746 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
3747 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3748
3749 p += alpn_len;
3750 }
3751#endif /* MBEDTLS_SSL_ALPN */
3752
3753 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003754 * Forced fields from top-level ssl_context structure
3755 *
3756 * Most of them already set to the correct value by mbedtls_ssl_init() and
3757 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
3758 */
3759 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
3760
3761 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
3762 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
3763
Hanno Becker361b10d2019-08-30 10:42:49 +01003764 /* Adjust pointers for header fields of outgoing records to
3765 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00003766 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01003767
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003768#if defined(MBEDTLS_SSL_PROTO_DTLS)
3769 ssl->in_epoch = 1;
3770#endif
3771
3772 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
3773 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00003774 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
3775 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003776 if( ssl->handshake != NULL )
3777 {
3778 mbedtls_ssl_handshake_free( ssl );
3779 mbedtls_free( ssl->handshake );
3780 ssl->handshake = NULL;
3781 }
3782
3783 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003784 * Done - should have consumed entire buffer
3785 */
3786 if( p != end )
3787 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003788
3789 return( 0 );
3790}
3791
3792/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003793 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003794 */
3795int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
3796 const unsigned char *buf,
3797 size_t len )
3798{
3799 int ret = ssl_context_load( context, buf, len );
3800
3801 if( ret != 0 )
3802 mbedtls_ssl_free( context );
3803
3804 return( ret );
3805}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003806#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003807
3808/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003809 * Free an SSL context
3810 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003811void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003812{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003813 if( ssl == NULL )
3814 return;
3815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003817
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003818 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003819 {
sander-visserb8aa2072020-05-06 22:05:13 +02003820#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3821 size_t out_buf_len = ssl->out_buf_len;
3822#else
3823 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
3824#endif
3825
Darryl Greenb33cc762019-11-28 14:29:44 +00003826 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003827 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003828 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003829 }
3830
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003831 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003832 {
sander-visserb8aa2072020-05-06 22:05:13 +02003833#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3834 size_t in_buf_len = ssl->in_buf_len;
3835#else
3836 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
3837#endif
3838
Darryl Greenb33cc762019-11-28 14:29:44 +00003839 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003840 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003841 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003842 }
3843
Paul Bakker48916f92012-09-16 19:57:18 +00003844 if( ssl->transform )
3845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 mbedtls_ssl_transform_free( ssl->transform );
3847 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003848 }
3849
3850 if( ssl->handshake )
3851 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02003852 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003853 mbedtls_ssl_transform_free( ssl->transform_negotiate );
3854 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00003855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856 mbedtls_free( ssl->handshake );
3857 mbedtls_free( ssl->transform_negotiate );
3858 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00003859 }
3860
Ronald Cron6f135e12021-12-08 16:57:54 +01003861#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01003862 mbedtls_ssl_transform_free( ssl->transform_application );
3863 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01003864#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01003865
Paul Bakkerc0463502013-02-14 11:19:38 +01003866 if( ssl->session )
3867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003868 mbedtls_ssl_session_free( ssl->session );
3869 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003870 }
3871
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02003872#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02003873 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003874 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003875 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003876 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00003877 }
Paul Bakker0be444a2013-08-27 21:55:01 +02003878#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003879
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003880#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003881 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02003882#endif
3883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00003885
Paul Bakker86f04f42013-02-14 11:20:09 +01003886 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003887 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003888}
3889
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02003890/*
3891 * Initialze mbedtls_ssl_config
3892 */
3893void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
3894{
3895 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
3896}
3897
Gilles Peskineeccd8882020-03-10 12:19:08 +01003898#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003899#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskineae270bf2021-06-02 00:05:29 +02003900/* The selection should be the same as mbedtls_x509_crt_profile_default in
Gilles Peskinea28f0f52021-06-02 15:29:38 +02003901 * x509_crt.c. Here, the order matters. Currently we favor stronger hashes,
3902 * for no fundamental reason.
Gilles Peskineae270bf2021-06-02 00:05:29 +02003903 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
3904 * about this list. */
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003905static int ssl_preset_default_hashes[] = {
3906#if defined(MBEDTLS_SHA512_C)
3907 MBEDTLS_MD_SHA512,
Mateusz Starzyk3352a532021-04-06 14:28:22 +02003908#endif
3909#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003910 MBEDTLS_MD_SHA384,
3911#endif
3912#if defined(MBEDTLS_SHA256_C)
3913 MBEDTLS_MD_SHA256,
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02003914#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003915 MBEDTLS_MD_NONE
3916};
Jerry Yuf017ee42022-01-12 15:49:48 +08003917#endif /* !MBEDTLS_DEPRECATED_REMOVED */
3918#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003919
Gilles Peskineae270bf2021-06-02 00:05:29 +02003920/* The selection should be the same as mbedtls_x509_crt_profile_default in
3921 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02003922 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02003923 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02003924 * about this list.
3925 */
Brett Warrene0edc842021-08-17 09:53:13 +01003926static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02003927#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003928 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003929#endif
3930#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003931 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003932#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02003933#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003934 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003935#endif
3936#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003937 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003938#endif
3939#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003940 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003941#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02003942#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003943 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003944#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02003945#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003946 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003947#endif
3948#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003949 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003950#endif
Brett Warrene0edc842021-08-17 09:53:13 +01003951 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02003952};
Gilles Peskineae270bf2021-06-02 00:05:29 +02003953
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003954static int ssl_preset_suiteb_ciphersuites[] = {
3955 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
3956 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
3957 0
3958};
3959
Gilles Peskineeccd8882020-03-10 12:19:08 +01003960#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003961#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003962static int ssl_preset_suiteb_hashes[] = {
Jerry Yuf017ee42022-01-12 15:49:48 +08003963#if defined(MBEDTLS_SHA256_C)
3964 MBEDTLS_MD_SHA256,
3965#endif
Jerry Yu713013f2022-01-17 18:16:35 +08003966#if defined(MBEDTLS_SHA384_C)
3967 MBEDTLS_MD_SHA384,
3968#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003969 MBEDTLS_MD_NONE
3970};
Jerry Yuf017ee42022-01-12 15:49:48 +08003971#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01003972
Jerry Yu909df7b2022-01-22 11:56:27 +08003973/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08003974 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08003975 * rules SHOULD be upheld.
3976 * - No duplicate entries.
3977 * - But if there is a good reason, do not change the order of the algorithms.
3978 * - ssl_tls12_present* is for TLS 1.2 use only.
3979 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08003980 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01003981static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08003982
Jerry Yued5e9f42022-01-26 11:21:34 +08003983#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \
3984 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
3985 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
3986#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C &&
3987 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08003988
Jerry Yu909df7b2022-01-22 11:56:27 +08003989#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \
3990 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
3991 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
3992#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
3993 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
3994
Jerry Yued5e9f42022-01-26 11:21:34 +08003995#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA512_C) && \
3996 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
3997 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
3998#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
3999 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004000
Jerry Yu909df7b2022-01-22 11:56:27 +08004001#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C)
4002 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4003#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */
4004
Ronald Cron8540cf62022-03-16 08:01:09 +01004005#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA512_C)
4006 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4007#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4008
4009#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA384_C)
4010 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4011#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4012
Jerry Yu53037892022-01-25 11:02:06 +08004013#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C)
4014 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4015#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4016
Jerry Yu909df7b2022-01-22 11:56:27 +08004017 MBEDTLS_TLS1_3_SIG_NONE
4018};
4019
4020/* NOTICE: see above */
4021#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4022static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Jerry Yu713013f2022-01-17 18:16:35 +08004023#if defined(MBEDTLS_SHA512_C)
4024 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA512 )
4025#endif
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004026#if defined(MBEDTLS_SHA384_C)
4027 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 )
4028#endif
4029#if defined(MBEDTLS_SHA256_C)
4030 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 )
4031#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08004032 MBEDTLS_TLS1_3_SIG_NONE
4033};
4034#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4035/* NOTICE: see above */
4036static uint16_t ssl_preset_suiteb_sig_algs[] = {
4037
Jerry Yu909df7b2022-01-22 11:56:27 +08004038#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \
4039 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4040 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
4041#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C &&
4042 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4043
Jerry Yu53037892022-01-25 11:02:06 +08004044#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \
4045 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4046 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
4047#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
4048 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004049
4050#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C)
4051 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4052#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004053
Jerry Yu53037892022-01-25 11:02:06 +08004054#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C)
4055 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4056#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4057
Jerry Yu713013f2022-01-17 18:16:35 +08004058 MBEDTLS_TLS1_3_SIG_NONE
4059};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004060
Jerry Yu909df7b2022-01-22 11:56:27 +08004061/* NOTICE: see above */
4062#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4063static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Jerry Yu713013f2022-01-17 18:16:35 +08004064#if defined(MBEDTLS_SHA256_C)
4065 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 )
4066#endif
Jerry Yu18c833e2022-01-25 10:55:47 +08004067#if defined(MBEDTLS_SHA384_C)
4068 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 )
4069#endif
Xiaofei Bai95395012021-11-25 08:51:30 +00004070 MBEDTLS_TLS1_3_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004071};
Jerry Yu909df7b2022-01-22 11:56:27 +08004072#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4073
Jerry Yu1a8b4812022-01-20 17:56:50 +08004074#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004075
Brett Warrene0edc842021-08-17 09:53:13 +01004076static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004077#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004078 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004079#endif
4080#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004081 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004082#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004083 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004084};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004085
Jerry Yu1a8b4812022-01-20 17:56:50 +08004086#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004087/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004088 * to make sure there are no duplicated signature algorithm entries. */
Jerry Yuf377d642022-01-25 10:43:59 +08004089static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004090{
4091 size_t i, j;
4092 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004093
Jerry Yuf377d642022-01-25 10:43:59 +08004094 for( i = 0; sig_algs[i] != MBEDTLS_TLS1_3_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004095 {
Jerry Yuf377d642022-01-25 10:43:59 +08004096 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004097 {
Jerry Yuf377d642022-01-25 10:43:59 +08004098 if( sig_algs[i] != sig_algs[j] )
4099 continue;
4100 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4101 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4102 sig_algs[i], j, i );
4103 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004104 }
4105 }
4106 return( ret );
4107}
4108
4109#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4110
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004111/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004112 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004113 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004114int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004115 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004116{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004117#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004118 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004119#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004120
Jerry Yu1a8b4812022-01-20 17:56:50 +08004121#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004122 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004123 {
4124 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4125 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4126 }
4127
Jerry Yuf377d642022-01-25 10:43:59 +08004128 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004129 {
4130 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4131 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4132 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004133
4134#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004135 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004136 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004137 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004138 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4139 }
4140
Jerry Yuf377d642022-01-25 10:43:59 +08004141 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004142 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004143 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004144 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4145 }
4146#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004147#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4148
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004149 /* Use the functions here so that they are covered in tests,
4150 * but otherwise access member directly for efficiency */
4151 mbedtls_ssl_conf_endpoint( conf, endpoint );
4152 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004153
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004154 /*
4155 * Things that are common to all presets
4156 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004157#if defined(MBEDTLS_SSL_CLI_C)
4158 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4159 {
4160 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4161#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4162 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4163#endif
4164 }
4165#endif
4166
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004167#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4168 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4169#endif
4170
4171#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4172 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4173#endif
4174
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004175#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004176 conf->f_cookie_write = ssl_cookie_write_dummy;
4177 conf->f_cookie_check = ssl_cookie_check_dummy;
4178#endif
4179
4180#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4181 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4182#endif
4183
Janos Follath088ce432017-04-10 12:42:31 +01004184#if defined(MBEDTLS_SSL_SRV_C)
4185 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004186 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004187#endif
4188
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004189#if defined(MBEDTLS_SSL_PROTO_DTLS)
4190 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4191 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4192#endif
4193
4194#if defined(MBEDTLS_SSL_RENEGOTIATION)
4195 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004196 memset( conf->renego_period, 0x00, 2 );
4197 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004198#endif
4199
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004200#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004201 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4202 {
4203 const unsigned char dhm_p[] =
4204 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4205 const unsigned char dhm_g[] =
4206 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004207
Hanno Beckere2defad2021-07-24 05:59:17 +01004208 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4209 dhm_p, sizeof( dhm_p ),
4210 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4211 {
4212 return( ret );
4213 }
4214 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004215#endif
4216
Ronald Cron6f135e12021-12-08 16:57:54 +01004217#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker71f1ed62021-07-24 06:01:47 +01004218 /*
4219 * Allow all TLS 1.3 key exchange modes by default.
4220 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004221 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004222#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004223
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004224 /*
4225 * Preset-specific defaults
4226 */
4227 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004228 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004229 /*
4230 * NSA Suite B
4231 */
4232 case MBEDTLS_SSL_PRESET_SUITEB:
Ronald Crone71639d2022-03-11 11:31:31 +01004233 conf->min_major_ver = MBEDTLS_SSL_MIN_MAJOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004234 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Ronald Crone71639d2022-03-11 11:31:31 +01004235
Ronald Cronf6606552022-03-15 11:23:25 +01004236 if( ( endpoint == MBEDTLS_SSL_IS_SERVER ) ||
4237 ( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) )
4238#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4239 {
4240 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
4241 conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
4242 }
Jerry Yu7d239632022-01-27 14:16:44 +08004243#else
Ronald Cronf6606552022-03-15 11:23:25 +01004244 {
4245 conf->min_major_ver = 0;
4246 conf->max_major_ver = 0;
4247 conf->min_minor_ver = 0;
4248 conf->max_minor_ver = 0;
4249 }
4250#endif
4251 else
4252 {
4253 conf->min_minor_ver = MBEDTLS_SSL_MIN_MINOR_VERSION;
4254 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
4255 }
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004256 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004257
4258#if defined(MBEDTLS_X509_CRT_PARSE_C)
4259 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004260#endif
4261
Gilles Peskineeccd8882020-03-10 12:19:08 +01004262#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004263#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004264 conf->sig_hashes = ssl_preset_suiteb_hashes;
Jerry Yuf017ee42022-01-12 15:49:48 +08004265#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004266#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4267 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4268 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4269 else
4270#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4271 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004272#endif
4273
Brett Warrene0edc842021-08-17 09:53:13 +01004274#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4275 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004276#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004277 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004278 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004279
4280 /*
4281 * Default
4282 */
4283 default:
Ronald Crone71639d2022-03-11 11:31:31 +01004284 conf->min_major_ver = MBEDTLS_SSL_MIN_MAJOR_VERSION;
4285 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
4286
Ronald Cronf6606552022-03-15 11:23:25 +01004287 if( ( endpoint == MBEDTLS_SSL_IS_SERVER ) ||
4288 ( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) )
4289#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4290 {
TRodziewiczef73f012021-05-13 14:53:36 +02004291 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Ronald Cronf6606552022-03-15 11:23:25 +01004292 conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
4293 }
4294#else
4295 {
4296 conf->min_major_ver = 0;
4297 conf->max_major_ver = 0;
4298 conf->min_minor_ver = 0;
4299 conf->max_minor_ver = 0;
4300 }
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004301#endif
Ronald Cronf6606552022-03-15 11:23:25 +01004302 else
4303 {
4304 conf->min_minor_ver = MBEDTLS_SSL_MIN_MINOR_VERSION;
4305 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
4306 }
4307
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004308 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004309
4310#if defined(MBEDTLS_X509_CRT_PARSE_C)
4311 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4312#endif
4313
Gilles Peskineeccd8882020-03-10 12:19:08 +01004314#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004315#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01004316 conf->sig_hashes = ssl_preset_default_hashes;
Jerry Yuf017ee42022-01-12 15:49:48 +08004317#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004318#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4319 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4320 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4321 else
4322#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4323 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004324#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004325
Brett Warrene0edc842021-08-17 09:53:13 +01004326#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4327 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004328#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004329 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004330
4331#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4332 conf->dhm_min_bitlen = 1024;
4333#endif
4334 }
4335
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004336 return( 0 );
4337}
4338
4339/*
4340 * Free mbedtls_ssl_config
4341 */
4342void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4343{
4344#if defined(MBEDTLS_DHM_C)
4345 mbedtls_mpi_free( &conf->dhm_P );
4346 mbedtls_mpi_free( &conf->dhm_G );
4347#endif
4348
Gilles Peskineeccd8882020-03-10 12:19:08 +01004349#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004350 if( conf->psk != NULL )
4351 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004352 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004353 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004354 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004355 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004356 }
4357
4358 if( conf->psk_identity != NULL )
4359 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004360 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004361 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004362 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004363 conf->psk_identity_len = 0;
4364 }
4365#endif
4366
4367#if defined(MBEDTLS_X509_CRT_PARSE_C)
4368 ssl_key_cert_free( conf->key_cert );
4369#endif
4370
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004371 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004372}
4373
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004374#if defined(MBEDTLS_PK_C) && \
4375 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004376/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004377 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004378 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004379unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004380{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004381#if defined(MBEDTLS_RSA_C)
4382 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4383 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004384#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004385#if defined(MBEDTLS_ECDSA_C)
4386 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4387 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004388#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004389 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004390}
4391
Hanno Becker7e5437a2017-04-28 17:15:26 +01004392unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4393{
4394 switch( type ) {
4395 case MBEDTLS_PK_RSA:
4396 return( MBEDTLS_SSL_SIG_RSA );
4397 case MBEDTLS_PK_ECDSA:
4398 case MBEDTLS_PK_ECKEY:
4399 return( MBEDTLS_SSL_SIG_ECDSA );
4400 default:
4401 return( MBEDTLS_SSL_SIG_ANON );
4402 }
4403}
4404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004405mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004406{
4407 switch( sig )
4408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004409#if defined(MBEDTLS_RSA_C)
4410 case MBEDTLS_SSL_SIG_RSA:
4411 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004412#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004413#if defined(MBEDTLS_ECDSA_C)
4414 case MBEDTLS_SSL_SIG_ECDSA:
4415 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004416#endif
4417 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004418 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004419 }
4420}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004421#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004422
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004423/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004424 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004426mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004427{
4428 switch( hash )
4429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004430#if defined(MBEDTLS_MD5_C)
4431 case MBEDTLS_SSL_HASH_MD5:
4432 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004433#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004434#if defined(MBEDTLS_SHA1_C)
4435 case MBEDTLS_SSL_HASH_SHA1:
4436 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004437#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004438#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004439 case MBEDTLS_SSL_HASH_SHA224:
4440 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004441#endif
4442#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004443 case MBEDTLS_SSL_HASH_SHA256:
4444 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004445#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004446#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004447 case MBEDTLS_SSL_HASH_SHA384:
4448 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004449#endif
4450#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004451 case MBEDTLS_SSL_HASH_SHA512:
4452 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004453#endif
4454 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004455 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004456 }
4457}
4458
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004459/*
4460 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4461 */
4462unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4463{
4464 switch( md )
4465 {
4466#if defined(MBEDTLS_MD5_C)
4467 case MBEDTLS_MD_MD5:
4468 return( MBEDTLS_SSL_HASH_MD5 );
4469#endif
4470#if defined(MBEDTLS_SHA1_C)
4471 case MBEDTLS_MD_SHA1:
4472 return( MBEDTLS_SSL_HASH_SHA1 );
4473#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004474#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004475 case MBEDTLS_MD_SHA224:
4476 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004477#endif
4478#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004479 case MBEDTLS_MD_SHA256:
4480 return( MBEDTLS_SSL_HASH_SHA256 );
4481#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004482#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004483 case MBEDTLS_MD_SHA384:
4484 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004485#endif
4486#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004487 case MBEDTLS_MD_SHA512:
4488 return( MBEDTLS_SSL_HASH_SHA512 );
4489#endif
4490 default:
4491 return( MBEDTLS_SSL_HASH_NONE );
4492 }
4493}
4494
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004495/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004496 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004497 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004498 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004499int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004500{
Brett Warrene0edc842021-08-17 09:53:13 +01004501 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004502
Brett Warrene0edc842021-08-17 09:53:13 +01004503 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004504 return( -1 );
4505
Brett Warrene0edc842021-08-17 09:53:13 +01004506 for( ; *group_list != 0; group_list++ )
4507 {
4508 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004509 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01004510 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004511
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004512 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004513}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004514
4515#if defined(MBEDTLS_ECP_C)
4516/*
4517 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
4518 */
4519int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
4520{
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01004521 uint16_t tls_id = mbedtls_ecp_curve_info_from_grp_id( grp_id )->tls_id;
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004522 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
4523}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004524#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004526#if defined(MBEDTLS_X509_CRT_PARSE_C)
4527int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
4528 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004529 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02004530 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004531{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004532 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004533 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004534 const char *ext_oid;
4535 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004537 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004538 {
4539 /* Server part of the key exchange */
4540 switch( ciphersuite->key_exchange )
4541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004542 case MBEDTLS_KEY_EXCHANGE_RSA:
4543 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004544 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004545 break;
4546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004547 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
4548 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
4549 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
4550 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004551 break;
4552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004553 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
4554 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004555 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004556 break;
4557
4558 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004559 case MBEDTLS_KEY_EXCHANGE_NONE:
4560 case MBEDTLS_KEY_EXCHANGE_PSK:
4561 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
4562 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02004563 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004564 usage = 0;
4565 }
4566 }
4567 else
4568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004569 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
4570 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004571 }
4572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004573 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004574 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004575 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004576 ret = -1;
4577 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004579 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004581 ext_oid = MBEDTLS_OID_SERVER_AUTH;
4582 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004583 }
4584 else
4585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004586 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
4587 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004588 }
4589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004590 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004591 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004592 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004593 ret = -1;
4594 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004595
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004596 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004597}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004598#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004599
Jerry Yu148165c2021-09-24 23:20:59 +08004600#if defined(MBEDTLS_USE_PSA_CRYPTO)
4601int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
4602 const mbedtls_md_type_t md,
4603 unsigned char *dst,
4604 size_t dst_len,
4605 size_t *olen )
4606{
Ronald Cronf6893e12022-01-07 22:09:01 +01004607 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4608 psa_hash_operation_t *hash_operation_to_clone;
4609 psa_hash_operation_t hash_operation = psa_hash_operation_init();
4610
Jerry Yu148165c2021-09-24 23:20:59 +08004611 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01004612
4613 switch( md )
4614 {
4615#if defined(MBEDTLS_SHA384_C)
4616 case MBEDTLS_MD_SHA384:
4617 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
4618 break;
4619#endif
4620
4621#if defined(MBEDTLS_SHA256_C)
4622 case MBEDTLS_MD_SHA256:
4623 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
4624 break;
4625#endif
4626
4627 default:
4628 goto exit;
4629 }
4630
4631 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
4632 if( status != PSA_SUCCESS )
4633 goto exit;
4634
4635 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
4636 if( status != PSA_SUCCESS )
4637 goto exit;
4638
4639exit:
Ronald Cronb788c042022-02-15 09:14:15 +01004640 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08004641}
4642#else /* MBEDTLS_USE_PSA_CRYPTO */
4643
Jerry Yu24c0ec32021-09-09 14:21:07 +08004644#if defined(MBEDTLS_SHA384_C)
Jerry Yu000f9762021-09-14 11:12:51 +08004645static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
4646 unsigned char *dst,
4647 size_t dst_len,
4648 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004649{
Jerry Yu24c0ec32021-09-09 14:21:07 +08004650 int ret;
4651 mbedtls_sha512_context sha512;
4652
4653 if( dst_len < 48 )
4654 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4655
4656 mbedtls_sha512_init( &sha512 );
4657 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
4658
4659 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
4660 {
4661 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
4662 goto exit;
4663 }
4664
4665 *olen = 48;
4666
4667exit:
4668
4669 mbedtls_sha512_free( &sha512 );
4670 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004671}
4672#endif /* MBEDTLS_SHA384_C */
4673
4674#if defined(MBEDTLS_SHA256_C)
Jerry Yu000f9762021-09-14 11:12:51 +08004675static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
4676 unsigned char *dst,
4677 size_t dst_len,
4678 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004679{
Jerry Yu24c0ec32021-09-09 14:21:07 +08004680 int ret;
4681 mbedtls_sha256_context sha256;
4682
4683 if( dst_len < 32 )
4684 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4685
4686 mbedtls_sha256_init( &sha256 );
4687 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08004688
Jerry Yu24c0ec32021-09-09 14:21:07 +08004689 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
4690 {
4691 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
4692 goto exit;
4693 }
4694
4695 *olen = 32;
4696
4697exit:
4698
4699 mbedtls_sha256_free( &sha256 );
4700 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004701}
4702#endif /* MBEDTLS_SHA256_C */
4703
Jerry Yu000f9762021-09-14 11:12:51 +08004704int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
4705 const mbedtls_md_type_t md,
4706 unsigned char *dst,
4707 size_t dst_len,
4708 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004709{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004710 switch( md )
4711 {
4712
Jerry Yu24c0ec32021-09-09 14:21:07 +08004713#if defined(MBEDTLS_SHA384_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004714 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08004715 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08004716#endif /* MBEDTLS_SHA384_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004717
Jerry Yu24c0ec32021-09-09 14:21:07 +08004718#if defined(MBEDTLS_SHA256_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004719 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08004720 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08004721#endif /* MBEDTLS_SHA256_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004722
4723 default:
4724 break;
4725 }
Jerry Yuc5aef882021-12-23 20:15:02 +08004726 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004727}
XiaokangQian647719a2021-12-07 09:16:29 +00004728
Jerry Yu148165c2021-09-24 23:20:59 +08004729#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08004730
Jerry Yudc7bd172022-02-17 13:44:15 +08004731#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4732
4733#if defined(MBEDTLS_USE_PSA_CRYPTO)
4734
4735static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
4736 mbedtls_svc_key_id_t key,
4737 psa_algorithm_t alg,
4738 const unsigned char* seed, size_t seed_length,
4739 const unsigned char* label, size_t label_length,
4740 size_t capacity )
4741{
4742 psa_status_t status;
4743
4744 status = psa_key_derivation_setup( derivation, alg );
4745 if( status != PSA_SUCCESS )
4746 return( status );
4747
4748 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4749 {
4750 status = psa_key_derivation_input_bytes( derivation,
4751 PSA_KEY_DERIVATION_INPUT_SEED,
4752 seed, seed_length );
4753 if( status != PSA_SUCCESS )
4754 return( status );
4755
4756 if( mbedtls_svc_key_id_is_null( key ) )
4757 {
4758 status = psa_key_derivation_input_bytes(
4759 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
4760 NULL, 0 );
4761 }
4762 else
4763 {
4764 status = psa_key_derivation_input_key(
4765 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
4766 }
4767 if( status != PSA_SUCCESS )
4768 return( status );
4769
4770 status = psa_key_derivation_input_bytes( derivation,
4771 PSA_KEY_DERIVATION_INPUT_LABEL,
4772 label, label_length );
4773 if( status != PSA_SUCCESS )
4774 return( status );
4775 }
4776 else
4777 {
4778 return( PSA_ERROR_NOT_SUPPORTED );
4779 }
4780
4781 status = psa_key_derivation_set_capacity( derivation, capacity );
4782 if( status != PSA_SUCCESS )
4783 return( status );
4784
4785 return( PSA_SUCCESS );
4786}
4787
4788static int tls_prf_generic( mbedtls_md_type_t md_type,
4789 const unsigned char *secret, size_t slen,
4790 const char *label,
4791 const unsigned char *random, size_t rlen,
4792 unsigned char *dstbuf, size_t dlen )
4793{
4794 psa_status_t status;
4795 psa_algorithm_t alg;
4796 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
4797 psa_key_derivation_operation_t derivation =
4798 PSA_KEY_DERIVATION_OPERATION_INIT;
4799
4800 if( md_type == MBEDTLS_MD_SHA384 )
4801 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
4802 else
4803 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
4804
4805 /* Normally a "secret" should be long enough to be impossible to
4806 * find by brute force, and in particular should not be empty. But
4807 * this PRF is also used to derive an IV, in particular in EAP-TLS,
4808 * and for this use case it makes sense to have a 0-length "secret".
4809 * Since the key API doesn't allow importing a key of length 0,
4810 * keep master_key=0, which setup_psa_key_derivation() understands
4811 * to mean a 0-length "secret" input. */
4812 if( slen != 0 )
4813 {
4814 psa_key_attributes_t key_attributes = psa_key_attributes_init();
4815 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
4816 psa_set_key_algorithm( &key_attributes, alg );
4817 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
4818
4819 status = psa_import_key( &key_attributes, secret, slen, &master_key );
4820 if( status != PSA_SUCCESS )
4821 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4822 }
4823
4824 status = setup_psa_key_derivation( &derivation,
4825 master_key, alg,
4826 random, rlen,
4827 (unsigned char const *) label,
4828 (size_t) strlen( label ),
4829 dlen );
4830 if( status != PSA_SUCCESS )
4831 {
4832 psa_key_derivation_abort( &derivation );
4833 psa_destroy_key( master_key );
4834 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4835 }
4836
4837 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
4838 if( status != PSA_SUCCESS )
4839 {
4840 psa_key_derivation_abort( &derivation );
4841 psa_destroy_key( master_key );
4842 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4843 }
4844
4845 status = psa_key_derivation_abort( &derivation );
4846 if( status != PSA_SUCCESS )
4847 {
4848 psa_destroy_key( master_key );
4849 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4850 }
4851
4852 if( ! mbedtls_svc_key_id_is_null( master_key ) )
4853 status = psa_destroy_key( master_key );
4854 if( status != PSA_SUCCESS )
4855 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4856
4857 return( 0 );
4858}
4859
4860#else /* MBEDTLS_USE_PSA_CRYPTO */
4861
4862static int tls_prf_generic( mbedtls_md_type_t md_type,
4863 const unsigned char *secret, size_t slen,
4864 const char *label,
4865 const unsigned char *random, size_t rlen,
4866 unsigned char *dstbuf, size_t dlen )
4867{
4868 size_t nb;
4869 size_t i, j, k, md_len;
4870 unsigned char *tmp;
4871 size_t tmp_len = 0;
4872 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
4873 const mbedtls_md_info_t *md_info;
4874 mbedtls_md_context_t md_ctx;
4875 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4876
4877 mbedtls_md_init( &md_ctx );
4878
4879 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
4880 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4881
4882 md_len = mbedtls_md_get_size( md_info );
4883
4884 tmp_len = md_len + strlen( label ) + rlen;
4885 tmp = mbedtls_calloc( 1, tmp_len );
4886 if( tmp == NULL )
4887 {
4888 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
4889 goto exit;
4890 }
4891
4892 nb = strlen( label );
4893 memcpy( tmp + md_len, label, nb );
4894 memcpy( tmp + md_len + nb, random, rlen );
4895 nb += rlen;
4896
4897 /*
4898 * Compute P_<hash>(secret, label + random)[0..dlen]
4899 */
4900 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
4901 goto exit;
4902
4903 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
4904 if( ret != 0 )
4905 goto exit;
4906 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
4907 if( ret != 0 )
4908 goto exit;
4909 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
4910 if( ret != 0 )
4911 goto exit;
4912
4913 for( i = 0; i < dlen; i += md_len )
4914 {
4915 ret = mbedtls_md_hmac_reset ( &md_ctx );
4916 if( ret != 0 )
4917 goto exit;
4918 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
4919 if( ret != 0 )
4920 goto exit;
4921 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
4922 if( ret != 0 )
4923 goto exit;
4924
4925 ret = mbedtls_md_hmac_reset ( &md_ctx );
4926 if( ret != 0 )
4927 goto exit;
4928 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
4929 if( ret != 0 )
4930 goto exit;
4931 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
4932 if( ret != 0 )
4933 goto exit;
4934
4935 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
4936
4937 for( j = 0; j < k; j++ )
4938 dstbuf[i + j] = h_i[j];
4939 }
4940
4941exit:
4942 mbedtls_md_free( &md_ctx );
4943
4944 mbedtls_platform_zeroize( tmp, tmp_len );
4945 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
4946
4947 mbedtls_free( tmp );
4948
4949 return( ret );
4950}
4951#endif /* MBEDTLS_USE_PSA_CRYPTO */
4952
4953#if defined(MBEDTLS_SHA256_C)
4954static int tls_prf_sha256( const unsigned char *secret, size_t slen,
4955 const char *label,
4956 const unsigned char *random, size_t rlen,
4957 unsigned char *dstbuf, size_t dlen )
4958{
4959 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
4960 label, random, rlen, dstbuf, dlen ) );
4961}
4962#endif /* MBEDTLS_SHA256_C */
4963
4964#if defined(MBEDTLS_SHA384_C)
4965static int tls_prf_sha384( const unsigned char *secret, size_t slen,
4966 const char *label,
4967 const unsigned char *random, size_t rlen,
4968 unsigned char *dstbuf, size_t dlen )
4969{
4970 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
4971 label, random, rlen, dstbuf, dlen ) );
4972}
4973#endif /* MBEDTLS_SHA384_C */
4974
Jerry Yuf009d862022-02-17 14:01:37 +08004975/*
4976 * Set appropriate PRF function and other SSL / TLS1.2 functions
4977 *
4978 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08004979 * - hash associated with the ciphersuite (only used by TLS 1.2)
4980 *
4981 * Outputs:
4982 * - the tls_prf, calc_verify and calc_finished members of handshake structure
4983 */
4984static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08004985 mbedtls_md_type_t hash )
4986{
Jerry Yuf009d862022-02-17 14:01:37 +08004987#if defined(MBEDTLS_SHA384_C)
Ronald Cron81591aa2022-03-07 09:05:51 +01004988 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08004989 {
4990 handshake->tls_prf = tls_prf_sha384;
4991 handshake->calc_verify = ssl_calc_verify_tls_sha384;
4992 handshake->calc_finished = ssl_calc_finished_tls_sha384;
4993 }
4994 else
4995#endif
4996#if defined(MBEDTLS_SHA256_C)
Jerry Yuf009d862022-02-17 14:01:37 +08004997 {
Ronald Cron81591aa2022-03-07 09:05:51 +01004998 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08004999 handshake->tls_prf = tls_prf_sha256;
5000 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5001 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5002 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005003#else
Jerry Yuf009d862022-02-17 14:01:37 +08005004 {
Jerry Yuf009d862022-02-17 14:01:37 +08005005 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005006 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005007 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5008 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005009#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005010
5011 return( 0 );
5012}
Jerry Yud6ab2352022-02-17 14:03:43 +08005013
5014#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
5015 defined(MBEDTLS_USE_PSA_CRYPTO)
5016static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
5017{
5018 if( ssl->conf->f_psk != NULL )
5019 {
5020 /* If we've used a callback to select the PSK,
5021 * the static configuration is irrelevant. */
5022 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
5023 return( 1 );
5024
5025 return( 0 );
5026 }
5027
5028 if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) )
5029 return( 1 );
5030
5031 return( 0 );
5032}
5033#endif /* MBEDTLS_USE_PSA_CRYPTO &&
5034 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5035
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005036/*
5037 * Compute master secret if needed
5038 *
5039 * Parameters:
5040 * [in/out] handshake
5041 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5042 * (PSA-PSK) ciphersuite_info, psk_opaque
5043 * [out] premaster (cleared)
5044 * [out] master
5045 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5046 * debug: conf->f_dbg, conf->p_dbg
5047 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005048 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005049 */
5050static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5051 unsigned char *master,
5052 const mbedtls_ssl_context *ssl )
5053{
5054 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5055
5056 /* cf. RFC 5246, Section 8.1:
5057 * "The master secret is always exactly 48 bytes in length." */
5058 size_t const master_secret_len = 48;
5059
5060#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5061 unsigned char session_hash[48];
5062#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5063
5064 /* The label for the KDF used for key expansion.
5065 * This is either "master secret" or "extended master secret"
5066 * depending on whether the Extended Master Secret extension
5067 * is used. */
5068 char const *lbl = "master secret";
5069
5070 /* The salt for the KDF used for key expansion.
5071 * - If the Extended Master Secret extension is not used,
5072 * this is ClientHello.Random + ServerHello.Random
5073 * (see Sect. 8.1 in RFC 5246).
5074 * - If the Extended Master Secret extension is used,
5075 * this is the transcript of the handshake so far.
5076 * (see Sect. 4 in RFC 7627). */
5077 unsigned char const *salt = handshake->randbytes;
5078 size_t salt_len = 64;
5079
5080#if !defined(MBEDTLS_DEBUG_C) && \
5081 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5082 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5083 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5084 ssl = NULL; /* make sure we don't use it except for those cases */
5085 (void) ssl;
5086#endif
5087
5088 if( handshake->resume != 0 )
5089 {
5090 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5091 return( 0 );
5092 }
5093
5094#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5095 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5096 {
5097 lbl = "extended master secret";
5098 salt = session_hash;
5099 handshake->calc_verify( ssl, session_hash, &salt_len );
5100
5101 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
5102 session_hash, salt_len );
5103 }
5104#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
5105
5106#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
5107 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5108 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005109 ssl_use_opaque_psk( ssl ) == 1 )
5110 {
5111 /* Perform PSK-to-MS expansion in a single step. */
5112 psa_status_t status;
5113 psa_algorithm_t alg;
5114 mbedtls_svc_key_id_t psk;
5115 psa_key_derivation_operation_t derivation =
5116 PSA_KEY_DERIVATION_OPERATION_INIT;
5117 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5118
5119 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5120
5121 psk = mbedtls_ssl_get_opaque_psk( ssl );
5122
5123 if( hash_alg == MBEDTLS_MD_SHA384 )
5124 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5125 else
5126 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5127
5128 status = setup_psa_key_derivation( &derivation, psk, alg,
5129 salt, salt_len,
5130 (unsigned char const *) lbl,
5131 (size_t) strlen( lbl ),
5132 master_secret_len );
5133 if( status != PSA_SUCCESS )
5134 {
5135 psa_key_derivation_abort( &derivation );
5136 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5137 }
5138
5139 status = psa_key_derivation_output_bytes( &derivation,
5140 master,
5141 master_secret_len );
5142 if( status != PSA_SUCCESS )
5143 {
5144 psa_key_derivation_abort( &derivation );
5145 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5146 }
5147
5148 status = psa_key_derivation_abort( &derivation );
5149 if( status != PSA_SUCCESS )
5150 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5151 }
5152 else
5153#endif
5154 {
5155 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
5156 lbl, salt, salt_len,
5157 master,
5158 master_secret_len );
5159 if( ret != 0 )
5160 {
5161 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5162 return( ret );
5163 }
5164
5165 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5166 handshake->premaster,
5167 handshake->pmslen );
5168
5169 mbedtls_platform_zeroize( handshake->premaster,
5170 sizeof(handshake->premaster) );
5171 }
5172
5173 return( 0 );
5174}
5175
Jerry Yud62f87e2022-02-17 14:09:02 +08005176int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5177{
5178 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5179 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5180 ssl->handshake->ciphersuite_info;
5181
5182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5183
5184 /* Set PRF, calc_verify and calc_finished function pointers */
5185 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005186 ciphersuite_info->mac );
5187 if( ret != 0 )
5188 {
5189 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5190 return( ret );
5191 }
5192
5193 /* Compute master secret if needed */
5194 ret = ssl_compute_master( ssl->handshake,
5195 ssl->session_negotiate->master,
5196 ssl );
5197 if( ret != 0 )
5198 {
5199 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5200 return( ret );
5201 }
5202
5203 /* Swap the client and server random values:
5204 * - MS derivation wanted client+server (RFC 5246 8.1)
5205 * - key derivation wants server+client (RFC 5246 6.3) */
5206 {
5207 unsigned char tmp[64];
5208 memcpy( tmp, ssl->handshake->randbytes, 64 );
5209 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5210 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5211 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5212 }
5213
5214 /* Populate transform structure */
5215 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5216 ssl->session_negotiate->ciphersuite,
5217 ssl->session_negotiate->master,
5218#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
5219 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5220 ssl->session_negotiate->encrypt_then_mac,
5221#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
5222 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
5223 ssl->handshake->tls_prf,
5224 ssl->handshake->randbytes,
5225 ssl->minor_ver,
5226 ssl->conf->endpoint,
5227 ssl );
5228 if( ret != 0 )
5229 {
5230 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5231 return( ret );
5232 }
5233
5234 /* We no longer need Server/ClientHello.random values */
5235 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5236 sizeof( ssl->handshake->randbytes ) );
5237
5238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5239
5240 return( 0 );
5241}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005242
Ronald Cron4dcbca92022-03-07 10:21:40 +01005243int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5244{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005245 switch( md )
5246 {
5247#if defined(MBEDTLS_SHA384_C)
5248 case MBEDTLS_SSL_HASH_SHA384:
5249 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5250 break;
5251#endif
5252#if defined(MBEDTLS_SHA256_C)
5253 case MBEDTLS_SSL_HASH_SHA256:
5254 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5255 break;
5256#endif
5257 default:
5258 return( -1 );
5259 }
5260
Ronald Cronc2f13a02022-03-07 10:25:24 +01005261 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005262}
5263
Jerry Yu8392e0d2022-02-17 14:10:24 +08005264#if defined(MBEDTLS_SHA256_C)
5265void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5266 unsigned char *hash,
5267 size_t *hlen )
5268{
5269#if defined(MBEDTLS_USE_PSA_CRYPTO)
5270 size_t hash_size;
5271 psa_status_t status;
5272 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5273
5274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5275 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5276 if( status != PSA_SUCCESS )
5277 {
5278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5279 return;
5280 }
5281
5282 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5283 if( status != PSA_SUCCESS )
5284 {
5285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5286 return;
5287 }
5288
5289 *hlen = 32;
5290 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5292#else
5293 mbedtls_sha256_context sha256;
5294
5295 mbedtls_sha256_init( &sha256 );
5296
5297 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5298
5299 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5300 mbedtls_sha256_finish( &sha256, hash );
5301
5302 *hlen = 32;
5303
5304 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5306
5307 mbedtls_sha256_free( &sha256 );
5308#endif /* MBEDTLS_USE_PSA_CRYPTO */
5309 return;
5310}
5311#endif /* MBEDTLS_SHA256_C */
5312
Jerry Yuc1cb3842022-02-17 14:13:48 +08005313#if defined(MBEDTLS_SHA384_C)
5314void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5315 unsigned char *hash,
5316 size_t *hlen )
5317{
5318#if defined(MBEDTLS_USE_PSA_CRYPTO)
5319 size_t hash_size;
5320 psa_status_t status;
5321 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5322
5323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5324 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5325 if( status != PSA_SUCCESS )
5326 {
5327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5328 return;
5329 }
5330
5331 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5332 if( status != PSA_SUCCESS )
5333 {
5334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5335 return;
5336 }
5337
5338 *hlen = 48;
5339 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5341#else
5342 mbedtls_sha512_context sha512;
5343
5344 mbedtls_sha512_init( &sha512 );
5345
5346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
5347
5348 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
5349 mbedtls_sha512_finish( &sha512, hash );
5350
5351 *hlen = 48;
5352
5353 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5355
5356 mbedtls_sha512_free( &sha512 );
5357#endif /* MBEDTLS_USE_PSA_CRYPTO */
5358 return;
5359}
5360#endif /* MBEDTLS_SHA384_C */
5361
Jerry Yuce3dca42022-02-17 14:16:37 +08005362#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
5363int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
5364{
5365 unsigned char *p = ssl->handshake->premaster;
5366 unsigned char *end = p + sizeof( ssl->handshake->premaster );
5367 const unsigned char *psk = NULL;
5368 size_t psk_len = 0;
5369
5370 if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len )
5371 == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
5372 {
5373 /*
5374 * This should never happen because the existence of a PSK is always
5375 * checked before calling this function
5376 */
5377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5378 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5379 }
5380
5381 /*
5382 * PMS = struct {
5383 * opaque other_secret<0..2^16-1>;
5384 * opaque psk<0..2^16-1>;
5385 * };
5386 * with "other_secret" depending on the particular key exchange
5387 */
5388#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5389 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
5390 {
5391 if( end - p < 2 )
5392 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5393
5394 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5395 p += 2;
5396
5397 if( end < p || (size_t)( end - p ) < psk_len )
5398 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5399
5400 memset( p, 0, psk_len );
5401 p += psk_len;
5402 }
5403 else
5404#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5405#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
5406 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5407 {
5408 /*
5409 * other_secret already set by the ClientKeyExchange message,
5410 * and is 48 bytes long
5411 */
5412 if( end - p < 2 )
5413 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5414
5415 *p++ = 0;
5416 *p++ = 48;
5417 p += 48;
5418 }
5419 else
5420#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
5421#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
5422 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5423 {
5424 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5425 size_t len;
5426
5427 /* Write length only when we know the actual value */
5428 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
5429 p + 2, end - ( p + 2 ), &len,
5430 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5431 {
5432 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
5433 return( ret );
5434 }
5435 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
5436 p += 2 + len;
5437
5438 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
5439 }
5440 else
5441#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
5442#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
5443 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
5444 {
5445 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5446 size_t zlen;
5447
5448 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
5449 p + 2, end - ( p + 2 ),
5450 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5451 {
5452 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
5453 return( ret );
5454 }
5455
5456 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
5457 p += 2 + zlen;
5458
5459 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
5460 MBEDTLS_DEBUG_ECDH_Z );
5461 }
5462 else
5463#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
5464 {
5465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5466 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5467 }
5468
5469 /* opaque psk<0..2^16-1>; */
5470 if( end - p < 2 )
5471 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5472
5473 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5474 p += 2;
5475
5476 if( end < p || (size_t)( end - p ) < psk_len )
5477 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5478
5479 memcpy( p, psk, psk_len );
5480 p += psk_len;
5481
5482 ssl->handshake->pmslen = p - ssl->handshake->premaster;
5483
5484 return( 0 );
5485}
5486#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08005487
5488#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
5489static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
5490
5491#if defined(MBEDTLS_SSL_PROTO_DTLS)
5492int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
5493{
5494 /* If renegotiation is not enforced, retransmit until we would reach max
5495 * timeout if we were using the usual handshake doubling scheme */
5496 if( ssl->conf->renego_max_records < 0 )
5497 {
5498 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
5499 unsigned char doublings = 1;
5500
5501 while( ratio != 0 )
5502 {
5503 ++doublings;
5504 ratio >>= 1;
5505 }
5506
5507 if( ++ssl->renego_records_seen > doublings )
5508 {
5509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
5510 return( 0 );
5511 }
5512 }
5513
5514 return( ssl_write_hello_request( ssl ) );
5515}
5516#endif
5517#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08005518
Jerry Yud9526692022-02-17 14:23:47 +08005519/*
5520 * Handshake functions
5521 */
5522#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5523/* No certificate support -> dummy functions */
5524int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
5525{
5526 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5527 ssl->handshake->ciphersuite_info;
5528
5529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
5530
5531 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5532 {
5533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5534 ssl->state++;
5535 return( 0 );
5536 }
5537
5538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5539 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5540}
5541
5542int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5543{
5544 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5545 ssl->handshake->ciphersuite_info;
5546
5547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5548
5549 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5550 {
5551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5552 ssl->state++;
5553 return( 0 );
5554 }
5555
5556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5557 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5558}
5559
5560#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5561/* Some certificate support -> implement write and parse */
5562
5563int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
5564{
5565 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
5566 size_t i, n;
5567 const mbedtls_x509_crt *crt;
5568 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5569 ssl->handshake->ciphersuite_info;
5570
5571 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
5572
5573 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5574 {
5575 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5576 ssl->state++;
5577 return( 0 );
5578 }
5579
5580#if defined(MBEDTLS_SSL_CLI_C)
5581 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5582 {
5583 if( ssl->handshake->client_auth == 0 )
5584 {
5585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5586 ssl->state++;
5587 return( 0 );
5588 }
5589 }
5590#endif /* MBEDTLS_SSL_CLI_C */
5591#if defined(MBEDTLS_SSL_SRV_C)
5592 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
5593 {
5594 if( mbedtls_ssl_own_cert( ssl ) == NULL )
5595 {
5596 /* Should never happen because we shouldn't have picked the
5597 * ciphersuite if we don't have a certificate. */
5598 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5599 }
5600 }
5601#endif
5602
5603 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
5604
5605 /*
5606 * 0 . 0 handshake type
5607 * 1 . 3 handshake length
5608 * 4 . 6 length of all certs
5609 * 7 . 9 length of cert. 1
5610 * 10 . n-1 peer certificate
5611 * n . n+2 length of cert. 2
5612 * n+3 . ... upper level cert, etc.
5613 */
5614 i = 7;
5615 crt = mbedtls_ssl_own_cert( ssl );
5616
5617 while( crt != NULL )
5618 {
5619 n = crt->raw.len;
5620 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
5621 {
5622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
5623 " > %" MBEDTLS_PRINTF_SIZET,
5624 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
5625 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5626 }
5627
5628 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
5629 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
5630 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
5631
5632 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5633 i += n; crt = crt->next;
5634 }
5635
5636 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
5637 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
5638 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
5639
5640 ssl->out_msglen = i;
5641 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5642 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
5643
5644 ssl->state++;
5645
5646 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
5647 {
5648 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
5649 return( ret );
5650 }
5651
5652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
5653
5654 return( ret );
5655}
5656
5657#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5658
5659#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5660static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5661 unsigned char *crt_buf,
5662 size_t crt_buf_len )
5663{
5664 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5665
5666 if( peer_crt == NULL )
5667 return( -1 );
5668
5669 if( peer_crt->raw.len != crt_buf_len )
5670 return( -1 );
5671
5672 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
5673}
5674#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5675static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5676 unsigned char *crt_buf,
5677 size_t crt_buf_len )
5678{
5679 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5680 unsigned char const * const peer_cert_digest =
5681 ssl->session->peer_cert_digest;
5682 mbedtls_md_type_t const peer_cert_digest_type =
5683 ssl->session->peer_cert_digest_type;
5684 mbedtls_md_info_t const * const digest_info =
5685 mbedtls_md_info_from_type( peer_cert_digest_type );
5686 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
5687 size_t digest_len;
5688
5689 if( peer_cert_digest == NULL || digest_info == NULL )
5690 return( -1 );
5691
5692 digest_len = mbedtls_md_get_size( digest_info );
5693 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
5694 return( -1 );
5695
5696 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
5697 if( ret != 0 )
5698 return( -1 );
5699
5700 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
5701}
5702#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5703#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5704
5705/*
5706 * Once the certificate message is read, parse it into a cert chain and
5707 * perform basic checks, but leave actual verification to the caller
5708 */
5709static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5710 mbedtls_x509_crt *chain )
5711{
5712 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5713#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5714 int crt_cnt=0;
5715#endif
5716 size_t i, n;
5717 uint8_t alert;
5718
5719 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5720 {
5721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5722 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5723 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5724 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5725 }
5726
5727 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
5728 {
5729 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5730 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5731 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5732 }
5733
5734 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
5735 {
5736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5737 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5738 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5739 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5740 }
5741
5742 i = mbedtls_ssl_hs_hdr_len( ssl );
5743
5744 /*
5745 * Same message structure as in mbedtls_ssl_write_certificate()
5746 */
5747 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
5748
5749 if( ssl->in_msg[i] != 0 ||
5750 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
5751 {
5752 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5753 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5754 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5755 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5756 }
5757
5758 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5759 i += 3;
5760
5761 /* Iterate through and parse the CRTs in the provided chain. */
5762 while( i < ssl->in_hslen )
5763 {
5764 /* Check that there's room for the next CRT's length fields. */
5765 if ( i + 3 > ssl->in_hslen ) {
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 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
5773 * anything beyond 2**16 ~ 64K. */
5774 if( ssl->in_msg[i] != 0 )
5775 {
5776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5777 mbedtls_ssl_send_alert_message( ssl,
5778 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5779 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
5780 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
5781 }
5782
5783 /* Read length of the next CRT in the chain. */
5784 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5785 | (unsigned int) ssl->in_msg[i + 2];
5786 i += 3;
5787
5788 if( n < 128 || i + n > ssl->in_hslen )
5789 {
5790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5791 mbedtls_ssl_send_alert_message( ssl,
5792 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5793 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5794 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5795 }
5796
5797 /* Check if we're handling the first CRT in the chain. */
5798#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5799 if( crt_cnt++ == 0 &&
5800 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5801 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
5802 {
5803 /* During client-side renegotiation, check that the server's
5804 * end-CRTs hasn't changed compared to the initial handshake,
5805 * mitigating the triple handshake attack. On success, reuse
5806 * the original end-CRT instead of parsing it again. */
5807 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
5808 if( ssl_check_peer_crt_unchanged( ssl,
5809 &ssl->in_msg[i],
5810 n ) != 0 )
5811 {
5812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
5813 mbedtls_ssl_send_alert_message( ssl,
5814 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5815 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
5816 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
5817 }
5818
5819 /* Now we can safely free the original chain. */
5820 ssl_clear_peer_cert( ssl->session );
5821 }
5822#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5823
5824 /* Parse the next certificate in the chain. */
5825#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5826 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
5827#else
5828 /* If we don't need to store the CRT chain permanently, parse
5829 * it in-place from the input buffer instead of making a copy. */
5830 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
5831#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5832 switch( ret )
5833 {
5834 case 0: /*ok*/
5835 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5836 /* Ignore certificate with an unknown algorithm: maybe a
5837 prior certificate was already trusted. */
5838 break;
5839
5840 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5841 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5842 goto crt_parse_der_failed;
5843
5844 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5845 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5846 goto crt_parse_der_failed;
5847
5848 default:
5849 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5850 crt_parse_der_failed:
5851 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
5852 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
5853 return( ret );
5854 }
5855
5856 i += n;
5857 }
5858
5859 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
5860 return( 0 );
5861}
5862
5863#if defined(MBEDTLS_SSL_SRV_C)
5864static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
5865{
5866 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5867 return( -1 );
5868
5869 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5870 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5871 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5872 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
5873 {
5874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
5875 return( 0 );
5876 }
5877 return( -1 );
5878}
5879#endif /* MBEDTLS_SSL_SRV_C */
5880
5881/* Check if a certificate message is expected.
5882 * Return either
5883 * - SSL_CERTIFICATE_EXPECTED, or
5884 * - SSL_CERTIFICATE_SKIP
5885 * indicating whether a Certificate message is expected or not.
5886 */
5887#define SSL_CERTIFICATE_EXPECTED 0
5888#define SSL_CERTIFICATE_SKIP 1
5889static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
5890 int authmode )
5891{
5892 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5893 ssl->handshake->ciphersuite_info;
5894
5895 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5896 return( SSL_CERTIFICATE_SKIP );
5897
5898#if defined(MBEDTLS_SSL_SRV_C)
5899 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
5900 {
5901 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5902 return( SSL_CERTIFICATE_SKIP );
5903
5904 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
5905 {
5906 ssl->session_negotiate->verify_result =
5907 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5908 return( SSL_CERTIFICATE_SKIP );
5909 }
5910 }
5911#else
5912 ((void) authmode);
5913#endif /* MBEDTLS_SSL_SRV_C */
5914
5915 return( SSL_CERTIFICATE_EXPECTED );
5916}
5917
5918static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
5919 int authmode,
5920 mbedtls_x509_crt *chain,
5921 void *rs_ctx )
5922{
5923 int ret = 0;
5924 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5925 ssl->handshake->ciphersuite_info;
5926 int have_ca_chain = 0;
5927
5928 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
5929 void *p_vrfy;
5930
5931 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
5932 return( 0 );
5933
5934 if( ssl->f_vrfy != NULL )
5935 {
5936 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
5937 f_vrfy = ssl->f_vrfy;
5938 p_vrfy = ssl->p_vrfy;
5939 }
5940 else
5941 {
5942 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
5943 f_vrfy = ssl->conf->f_vrfy;
5944 p_vrfy = ssl->conf->p_vrfy;
5945 }
5946
5947 /*
5948 * Main check: verify certificate
5949 */
5950#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
5951 if( ssl->conf->f_ca_cb != NULL )
5952 {
5953 ((void) rs_ctx);
5954 have_ca_chain = 1;
5955
5956 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
5957 ret = mbedtls_x509_crt_verify_with_ca_cb(
5958 chain,
5959 ssl->conf->f_ca_cb,
5960 ssl->conf->p_ca_cb,
5961 ssl->conf->cert_profile,
5962 ssl->hostname,
5963 &ssl->session_negotiate->verify_result,
5964 f_vrfy, p_vrfy );
5965 }
5966 else
5967#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
5968 {
5969 mbedtls_x509_crt *ca_chain;
5970 mbedtls_x509_crl *ca_crl;
5971
5972#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5973 if( ssl->handshake->sni_ca_chain != NULL )
5974 {
5975 ca_chain = ssl->handshake->sni_ca_chain;
5976 ca_crl = ssl->handshake->sni_ca_crl;
5977 }
5978 else
5979#endif
5980 {
5981 ca_chain = ssl->conf->ca_chain;
5982 ca_crl = ssl->conf->ca_crl;
5983 }
5984
5985 if( ca_chain != NULL )
5986 have_ca_chain = 1;
5987
5988 ret = mbedtls_x509_crt_verify_restartable(
5989 chain,
5990 ca_chain, ca_crl,
5991 ssl->conf->cert_profile,
5992 ssl->hostname,
5993 &ssl->session_negotiate->verify_result,
5994 f_vrfy, p_vrfy, rs_ctx );
5995 }
5996
5997 if( ret != 0 )
5998 {
5999 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6000 }
6001
6002#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6003 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6004 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6005#endif
6006
6007 /*
6008 * Secondary checks: always done, but change 'ret' only if it was 0
6009 */
6010
6011#if defined(MBEDTLS_ECP_C)
6012 {
6013 const mbedtls_pk_context *pk = &chain->pk;
6014
6015 /* If certificate uses an EC key, make sure the curve is OK */
6016 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6017 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6018 {
6019 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6020
6021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6022 if( ret == 0 )
6023 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6024 }
6025 }
6026#endif /* MBEDTLS_ECP_C */
6027
6028 if( mbedtls_ssl_check_cert_usage( chain,
6029 ciphersuite_info,
6030 ! ssl->conf->endpoint,
6031 &ssl->session_negotiate->verify_result ) != 0 )
6032 {
6033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6034 if( ret == 0 )
6035 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6036 }
6037
6038 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6039 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6040 * with details encoded in the verification flags. All other kinds
6041 * of error codes, including those from the user provided f_vrfy
6042 * functions, are treated as fatal and lead to a failure of
6043 * ssl_parse_certificate even if verification was optional. */
6044 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6045 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6046 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6047 {
6048 ret = 0;
6049 }
6050
6051 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6052 {
6053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6054 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6055 }
6056
6057 if( ret != 0 )
6058 {
6059 uint8_t alert;
6060
6061 /* The certificate may have been rejected for several reasons.
6062 Pick one and send the corresponding alert. Which alert to send
6063 may be a subject of debate in some cases. */
6064 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6065 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6066 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6067 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6068 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6069 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6070 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6071 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6072 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6073 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6074 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6075 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6076 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6077 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6078 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6079 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6080 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6081 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6082 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6083 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6084 else
6085 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6086 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6087 alert );
6088 }
6089
6090#if defined(MBEDTLS_DEBUG_C)
6091 if( ssl->session_negotiate->verify_result != 0 )
6092 {
6093 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6094 (unsigned int) ssl->session_negotiate->verify_result ) );
6095 }
6096 else
6097 {
6098 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6099 }
6100#endif /* MBEDTLS_DEBUG_C */
6101
6102 return( ret );
6103}
6104
6105#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6106static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6107 unsigned char *start, size_t len )
6108{
6109 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6110 /* Remember digest of the peer's end-CRT. */
6111 ssl->session_negotiate->peer_cert_digest =
6112 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6113 if( ssl->session_negotiate->peer_cert_digest == NULL )
6114 {
6115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6116 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6117 mbedtls_ssl_send_alert_message( ssl,
6118 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6119 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6120
6121 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6122 }
6123
6124 ret = mbedtls_md( mbedtls_md_info_from_type(
6125 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6126 start, len,
6127 ssl->session_negotiate->peer_cert_digest );
6128
6129 ssl->session_negotiate->peer_cert_digest_type =
6130 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6131 ssl->session_negotiate->peer_cert_digest_len =
6132 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6133
6134 return( ret );
6135}
6136
6137static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6138 unsigned char *start, size_t len )
6139{
6140 unsigned char *end = start + len;
6141 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6142
6143 /* Make a copy of the peer's raw public key. */
6144 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6145 ret = mbedtls_pk_parse_subpubkey( &start, end,
6146 &ssl->handshake->peer_pubkey );
6147 if( ret != 0 )
6148 {
6149 /* We should have parsed the public key before. */
6150 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6151 }
6152
6153 return( 0 );
6154}
6155#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6156
6157int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6158{
6159 int ret = 0;
6160 int crt_expected;
6161#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6162 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6163 ? ssl->handshake->sni_authmode
6164 : ssl->conf->authmode;
6165#else
6166 const int authmode = ssl->conf->authmode;
6167#endif
6168 void *rs_ctx = NULL;
6169 mbedtls_x509_crt *chain = NULL;
6170
6171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6172
6173 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6174 if( crt_expected == SSL_CERTIFICATE_SKIP )
6175 {
6176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6177 goto exit;
6178 }
6179
6180#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6181 if( ssl->handshake->ecrs_enabled &&
6182 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6183 {
6184 chain = ssl->handshake->ecrs_peer_cert;
6185 ssl->handshake->ecrs_peer_cert = NULL;
6186 goto crt_verify;
6187 }
6188#endif
6189
6190 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6191 {
6192 /* mbedtls_ssl_read_record may have sent an alert already. We
6193 let it decide whether to alert. */
6194 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6195 goto exit;
6196 }
6197
6198#if defined(MBEDTLS_SSL_SRV_C)
6199 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6200 {
6201 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6202
6203 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6204 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6205
6206 goto exit;
6207 }
6208#endif /* MBEDTLS_SSL_SRV_C */
6209
6210 /* Clear existing peer CRT structure in case we tried to
6211 * reuse a session but it failed, and allocate a new one. */
6212 ssl_clear_peer_cert( ssl->session_negotiate );
6213
6214 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6215 if( chain == NULL )
6216 {
6217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6218 sizeof( mbedtls_x509_crt ) ) );
6219 mbedtls_ssl_send_alert_message( ssl,
6220 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6221 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6222
6223 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6224 goto exit;
6225 }
6226 mbedtls_x509_crt_init( chain );
6227
6228 ret = ssl_parse_certificate_chain( ssl, chain );
6229 if( ret != 0 )
6230 goto exit;
6231
6232#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6233 if( ssl->handshake->ecrs_enabled)
6234 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6235
6236crt_verify:
6237 if( ssl->handshake->ecrs_enabled)
6238 rs_ctx = &ssl->handshake->ecrs_ctx;
6239#endif
6240
6241 ret = ssl_parse_certificate_verify( ssl, authmode,
6242 chain, rs_ctx );
6243 if( ret != 0 )
6244 goto exit;
6245
6246#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6247 {
6248 unsigned char *crt_start, *pk_start;
6249 size_t crt_len, pk_len;
6250
6251 /* We parse the CRT chain without copying, so
6252 * these pointers point into the input buffer,
6253 * and are hence still valid after freeing the
6254 * CRT chain. */
6255
6256 crt_start = chain->raw.p;
6257 crt_len = chain->raw.len;
6258
6259 pk_start = chain->pk_raw.p;
6260 pk_len = chain->pk_raw.len;
6261
6262 /* Free the CRT structures before computing
6263 * digest and copying the peer's public key. */
6264 mbedtls_x509_crt_free( chain );
6265 mbedtls_free( chain );
6266 chain = NULL;
6267
6268 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6269 if( ret != 0 )
6270 goto exit;
6271
6272 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6273 if( ret != 0 )
6274 goto exit;
6275 }
6276#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6277 /* Pass ownership to session structure. */
6278 ssl->session_negotiate->peer_cert = chain;
6279 chain = NULL;
6280#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6281
6282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6283
6284exit:
6285
6286 if( ret == 0 )
6287 ssl->state++;
6288
6289#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6290 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6291 {
6292 ssl->handshake->ecrs_peer_cert = chain;
6293 chain = NULL;
6294 }
6295#endif
6296
6297 if( chain != NULL )
6298 {
6299 mbedtls_x509_crt_free( chain );
6300 mbedtls_free( chain );
6301 }
6302
6303 return( ret );
6304}
6305#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6306
Jerry Yu615bd6f2022-02-17 14:25:15 +08006307#if defined(MBEDTLS_SHA256_C)
6308static void ssl_calc_finished_tls_sha256(
6309 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6310{
6311 int len = 12;
6312 const char *sender;
6313 unsigned char padbuf[32];
6314#if defined(MBEDTLS_USE_PSA_CRYPTO)
6315 size_t hash_size;
6316 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
6317 psa_status_t status;
6318#else
6319 mbedtls_sha256_context sha256;
6320#endif
6321
6322 mbedtls_ssl_session *session = ssl->session_negotiate;
6323 if( !session )
6324 session = ssl->session;
6325
6326 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6327 ? "client finished"
6328 : "server finished";
6329
6330#if defined(MBEDTLS_USE_PSA_CRYPTO)
6331 sha256_psa = psa_hash_operation_init();
6332
6333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6334
6335 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6336 if( status != PSA_SUCCESS )
6337 {
6338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6339 return;
6340 }
6341
6342 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6343 if( status != PSA_SUCCESS )
6344 {
6345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6346 return;
6347 }
6348 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6349#else
6350
6351 mbedtls_sha256_init( &sha256 );
6352
6353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
6354
6355 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6356
6357 /*
6358 * TLSv1.2:
6359 * hash = PRF( master, finished_label,
6360 * Hash( handshake ) )[0.11]
6361 */
6362
6363#if !defined(MBEDTLS_SHA256_ALT)
6364 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
6365 sha256.state, sizeof( sha256.state ) );
6366#endif
6367
6368 mbedtls_sha256_finish( &sha256, padbuf );
6369 mbedtls_sha256_free( &sha256 );
6370#endif /* MBEDTLS_USE_PSA_CRYPTO */
6371
6372 ssl->handshake->tls_prf( session->master, 48, sender,
6373 padbuf, 32, buf, len );
6374
6375 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6376
6377 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6378
6379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6380}
6381#endif /* MBEDTLS_SHA256_C */
6382
Jerry Yub7ba49e2022-02-17 14:25:53 +08006383
6384#if defined(MBEDTLS_SHA384_C)
6385
6386static void ssl_calc_finished_tls_sha384(
6387 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6388{
6389 int len = 12;
6390 const char *sender;
6391 unsigned char padbuf[48];
6392#if defined(MBEDTLS_USE_PSA_CRYPTO)
6393 size_t hash_size;
6394 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
6395 psa_status_t status;
6396#else
6397 mbedtls_sha512_context sha512;
6398#endif
6399
6400 mbedtls_ssl_session *session = ssl->session_negotiate;
6401 if( !session )
6402 session = ssl->session;
6403
6404 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6405 ? "client finished"
6406 : "server finished";
6407
6408#if defined(MBEDTLS_USE_PSA_CRYPTO)
6409 sha384_psa = psa_hash_operation_init();
6410
6411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6412
6413 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6414 if( status != PSA_SUCCESS )
6415 {
6416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6417 return;
6418 }
6419
6420 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
6421 if( status != PSA_SUCCESS )
6422 {
6423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6424 return;
6425 }
6426 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
6427#else
6428 mbedtls_sha512_init( &sha512 );
6429
6430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
6431
6432 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
6433
6434 /*
6435 * TLSv1.2:
6436 * hash = PRF( master, finished_label,
6437 * Hash( handshake ) )[0.11]
6438 */
6439
6440#if !defined(MBEDTLS_SHA512_ALT)
6441 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6442 sha512.state, sizeof( sha512.state ) );
6443#endif
6444 mbedtls_sha512_finish( &sha512, padbuf );
6445
6446 mbedtls_sha512_free( &sha512 );
6447#endif
6448
6449 ssl->handshake->tls_prf( session->master, 48, sender,
6450 padbuf, 48, buf, len );
6451
6452 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6453
6454 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6455
6456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6457}
6458#endif /* MBEDTLS_SHA384_C */
6459
Jerry Yuaef00152022-02-17 14:27:31 +08006460void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
6461{
6462 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
6463
6464 /*
6465 * Free our handshake params
6466 */
6467 mbedtls_ssl_handshake_free( ssl );
6468 mbedtls_free( ssl->handshake );
6469 ssl->handshake = NULL;
6470
6471 /*
6472 * Free the previous transform and swith in the current one
6473 */
6474 if( ssl->transform )
6475 {
6476 mbedtls_ssl_transform_free( ssl->transform );
6477 mbedtls_free( ssl->transform );
6478 }
6479 ssl->transform = ssl->transform_negotiate;
6480 ssl->transform_negotiate = NULL;
6481
6482 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
6483}
6484
Jerry Yu2a9fff52022-02-17 14:28:51 +08006485void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
6486{
6487 int resume = ssl->handshake->resume;
6488
6489 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
6490
6491#if defined(MBEDTLS_SSL_RENEGOTIATION)
6492 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6493 {
6494 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
6495 ssl->renego_records_seen = 0;
6496 }
6497#endif
6498
6499 /*
6500 * Free the previous session and switch in the current one
6501 */
6502 if( ssl->session )
6503 {
6504#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6505 /* RFC 7366 3.1: keep the EtM state */
6506 ssl->session_negotiate->encrypt_then_mac =
6507 ssl->session->encrypt_then_mac;
6508#endif
6509
6510 mbedtls_ssl_session_free( ssl->session );
6511 mbedtls_free( ssl->session );
6512 }
6513 ssl->session = ssl->session_negotiate;
6514 ssl->session_negotiate = NULL;
6515
6516 /*
6517 * Add cache entry
6518 */
6519 if( ssl->conf->f_set_cache != NULL &&
6520 ssl->session->id_len != 0 &&
6521 resume == 0 )
6522 {
6523 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
6524 ssl->session->id,
6525 ssl->session->id_len,
6526 ssl->session ) != 0 )
6527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
6528 }
6529
6530#if defined(MBEDTLS_SSL_PROTO_DTLS)
6531 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6532 ssl->handshake->flight != NULL )
6533 {
6534 /* Cancel handshake timer */
6535 mbedtls_ssl_set_timer( ssl, 0 );
6536
6537 /* Keep last flight around in case we need to resend it:
6538 * we need the handshake and transform structures for that */
6539 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
6540 }
6541 else
6542#endif
6543 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
6544
6545 ssl->state++;
6546
6547 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
6548}
6549
Jerry Yu3c8e47b2022-02-17 14:30:01 +08006550int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
6551{
6552 int ret, hash_len;
6553
6554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
6555
6556 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
6557
6558 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
6559
6560 /*
6561 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6562 * may define some other value. Currently (early 2016), no defined
6563 * ciphersuite does this (and this is unlikely to change as activity has
6564 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6565 */
6566 hash_len = 12;
6567
6568#if defined(MBEDTLS_SSL_RENEGOTIATION)
6569 ssl->verify_data_len = hash_len;
6570 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
6571#endif
6572
6573 ssl->out_msglen = 4 + hash_len;
6574 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6575 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
6576
6577 /*
6578 * In case of session resuming, invert the client and server
6579 * ChangeCipherSpec messages order.
6580 */
6581 if( ssl->handshake->resume != 0 )
6582 {
6583#if defined(MBEDTLS_SSL_CLI_C)
6584 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6585 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
6586#endif
6587#if defined(MBEDTLS_SSL_SRV_C)
6588 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6589 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
6590#endif
6591 }
6592 else
6593 ssl->state++;
6594
6595 /*
6596 * Switch to our negotiated transform and session parameters for outbound
6597 * data.
6598 */
6599 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
6600
6601#if defined(MBEDTLS_SSL_PROTO_DTLS)
6602 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6603 {
6604 unsigned char i;
6605
6606 /* Remember current epoch settings for resending */
6607 ssl->handshake->alt_transform_out = ssl->transform_out;
6608 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
6609 sizeof( ssl->handshake->alt_out_ctr ) );
6610
6611 /* Set sequence_number to zero */
6612 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
6613
6614
6615 /* Increment epoch */
6616 for( i = 2; i > 0; i-- )
6617 if( ++ssl->cur_out_ctr[i - 1] != 0 )
6618 break;
6619
6620 /* The loop goes to its end iff the counter is wrapping */
6621 if( i == 0 )
6622 {
6623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6624 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
6625 }
6626 }
6627 else
6628#endif /* MBEDTLS_SSL_PROTO_DTLS */
6629 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6630
6631 ssl->transform_out = ssl->transform_negotiate;
6632 ssl->session_out = ssl->session_negotiate;
6633
6634#if defined(MBEDTLS_SSL_PROTO_DTLS)
6635 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6636 mbedtls_ssl_send_flight_completed( ssl );
6637#endif
6638
6639 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6640 {
6641 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6642 return( ret );
6643 }
6644
6645#if defined(MBEDTLS_SSL_PROTO_DTLS)
6646 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6647 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6648 {
6649 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6650 return( ret );
6651 }
6652#endif
6653
6654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
6655
6656 return( 0 );
6657}
6658
Jerry Yu0b3d7c12022-02-17 14:30:51 +08006659#define SSL_MAX_HASH_LEN 12
6660
6661int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
6662{
6663 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6664 unsigned int hash_len = 12;
6665 unsigned char buf[SSL_MAX_HASH_LEN];
6666
6667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
6668
6669 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
6670
6671 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6672 {
6673 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6674 goto exit;
6675 }
6676
6677 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6678 {
6679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6680 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6681 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6682 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
6683 goto exit;
6684 }
6685
6686 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
6687 {
6688 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6689 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6690 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
6691 goto exit;
6692 }
6693
6694 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
6695 {
6696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6697 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6698 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6699 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
6700 goto exit;
6701 }
6702
6703 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
6704 buf, hash_len ) != 0 )
6705 {
6706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6707 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6708 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
6709 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
6710 goto exit;
6711 }
6712
6713#if defined(MBEDTLS_SSL_RENEGOTIATION)
6714 ssl->verify_data_len = hash_len;
6715 memcpy( ssl->peer_verify_data, buf, hash_len );
6716#endif
6717
6718 if( ssl->handshake->resume != 0 )
6719 {
6720#if defined(MBEDTLS_SSL_CLI_C)
6721 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6722 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
6723#endif
6724#if defined(MBEDTLS_SSL_SRV_C)
6725 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6726 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
6727#endif
6728 }
6729 else
6730 ssl->state++;
6731
6732#if defined(MBEDTLS_SSL_PROTO_DTLS)
6733 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6734 mbedtls_ssl_recv_flight_completed( ssl );
6735#endif
6736
6737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
6738
6739exit:
6740 mbedtls_platform_zeroize( buf, hash_len );
6741 return( ret );
6742}
6743
Jerry Yu392112c2022-02-17 14:34:10 +08006744#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
6745/*
6746 * Helper to get TLS 1.2 PRF from ciphersuite
6747 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
6748 */
6749static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
6750{
6751#if defined(MBEDTLS_SHA384_C)
6752 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6753 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
6754
6755 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
6756 return( tls_prf_sha384 );
6757#else
6758 (void) ciphersuite_id;
6759#endif
6760 return( tls_prf_sha256 );
6761}
6762#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08006763
6764static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
6765{
6766 ((void) tls_prf);
6767#if defined(MBEDTLS_SHA384_C)
6768 if( tls_prf == tls_prf_sha384 )
6769 {
6770 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
6771 }
6772 else
6773#endif
6774#if defined(MBEDTLS_SHA256_C)
6775 if( tls_prf == tls_prf_sha256 )
6776 {
6777 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
6778 }
6779 else
6780#endif
6781 return( MBEDTLS_SSL_TLS_PRF_NONE );
6782}
6783
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006784/*
6785 * Populate a transform structure with session keys and all the other
6786 * necessary information.
6787 *
6788 * Parameters:
6789 * - [in/out]: transform: structure to populate
6790 * [in] must be just initialised with mbedtls_ssl_transform_init()
6791 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
6792 * - [in] ciphersuite
6793 * - [in] master
6794 * - [in] encrypt_then_mac
6795 * - [in] compression
6796 * - [in] tls_prf: pointer to PRF to use for key derivation
6797 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
6798 * - [in] minor_ver: SSL/TLS minor version
6799 * - [in] endpoint: client or server
6800 * - [in] ssl: used for:
6801 * - ssl->conf->{f,p}_export_keys
6802 * [in] optionally used for:
6803 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
6804 */
6805static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
6806 int ciphersuite,
6807 const unsigned char master[48],
6808#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
6809 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6810 int encrypt_then_mac,
6811#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
6812 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
6813 ssl_tls_prf_t tls_prf,
6814 const unsigned char randbytes[64],
6815 int minor_ver,
6816 unsigned endpoint,
6817 const mbedtls_ssl_context *ssl )
6818{
6819 int ret = 0;
6820 unsigned char keyblk[256];
6821 unsigned char *key1;
6822 unsigned char *key2;
6823 unsigned char *mac_enc;
6824 unsigned char *mac_dec;
6825 size_t mac_key_len = 0;
6826 size_t iv_copy_len;
6827 size_t keylen;
6828 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
6829 const mbedtls_cipher_info_t *cipher_info;
6830 const mbedtls_md_info_t *md_info;
6831
6832#if defined(MBEDTLS_USE_PSA_CRYPTO)
6833 psa_key_type_t key_type;
6834 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6835 psa_algorithm_t alg;
6836 size_t key_bits;
6837 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
6838#endif
6839
6840#if !defined(MBEDTLS_DEBUG_C) && \
6841 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6842 if( ssl->f_export_keys == NULL )
6843 {
6844 ssl = NULL; /* make sure we don't use it except for these cases */
6845 (void) ssl;
6846 }
6847#endif
6848
6849 /*
6850 * Some data just needs copying into the structure
6851 */
6852#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
6853 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
6854 transform->encrypt_then_mac = encrypt_then_mac;
6855#endif
6856 transform->minor_ver = minor_ver;
6857
6858#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
6859 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
6860#endif
6861
6862#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
6863 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
6864 {
6865 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
6866 * generation separate. This should never happen. */
6867 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6868 }
6869#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
6870
6871 /*
6872 * Get various info structures
6873 */
6874 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
6875 if( ciphersuite_info == NULL )
6876 {
6877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
6878 ciphersuite ) );
6879 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6880 }
6881
6882 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
6883 if( cipher_info == NULL )
6884 {
6885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
6886 ciphersuite_info->cipher ) );
6887 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6888 }
6889
6890 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
6891 if( md_info == NULL )
6892 {
6893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
6894 (unsigned) ciphersuite_info->mac ) );
6895 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6896 }
6897
6898#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6899 /* Copy own and peer's CID if the use of the CID
6900 * extension has been negotiated. */
6901 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
6902 {
6903 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
6904
6905 transform->in_cid_len = ssl->own_cid_len;
6906 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
6907 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
6908 transform->in_cid_len );
6909
6910 transform->out_cid_len = ssl->handshake->peer_cid_len;
6911 memcpy( transform->out_cid, ssl->handshake->peer_cid,
6912 ssl->handshake->peer_cid_len );
6913 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
6914 transform->out_cid_len );
6915 }
6916#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6917
6918 /*
6919 * Compute key block using the PRF
6920 */
6921 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
6922 if( ret != 0 )
6923 {
6924 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
6925 return( ret );
6926 }
6927
6928 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
6929 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
6930 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
6931 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
6932 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
6933
6934 /*
6935 * Determine the appropriate key, IV and MAC length.
6936 */
6937
6938 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
6939
6940#if defined(MBEDTLS_GCM_C) || \
6941 defined(MBEDTLS_CCM_C) || \
6942 defined(MBEDTLS_CHACHAPOLY_C)
6943 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_GCM ||
6944 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CCM ||
6945 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
6946 {
6947 size_t explicit_ivlen;
6948
6949 transform->maclen = 0;
6950 mac_key_len = 0;
6951 transform->taglen =
6952 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
6953
6954 /* All modes haves 96-bit IVs, but the length of the static parts vary
6955 * with mode and version:
6956 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
6957 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
6958 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
6959 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
6960 * sequence number).
6961 */
6962 transform->ivlen = 12;
6963 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
6964 transform->fixed_ivlen = 12;
6965 else
6966 transform->fixed_ivlen = 4;
6967
6968 /* Minimum length of encrypted record */
6969 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
6970 transform->minlen = explicit_ivlen + transform->taglen;
6971 }
6972 else
6973#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
6974#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
6975 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM ||
6976 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
6977 {
Neil Armstrongcf8841a2022-02-24 11:17:45 +01006978#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006979 /* Initialize HMAC contexts */
6980 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
6981 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
6982 {
6983 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
6984 goto end;
6985 }
Neil Armstrongcf8841a2022-02-24 11:17:45 +01006986#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006987
6988 /* Get MAC length */
6989 mac_key_len = mbedtls_md_get_size( md_info );
6990 transform->maclen = mac_key_len;
6991
6992 /* IV length */
6993 transform->ivlen = cipher_info->iv_size;
6994
6995 /* Minimum length */
6996 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM )
6997 transform->minlen = transform->maclen;
6998 else
6999 {
7000 /*
7001 * GenericBlockCipher:
7002 * 1. if EtM is in use: one block plus MAC
7003 * otherwise: * first multiple of blocklen greater than maclen
7004 * 2. IV
7005 */
7006#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7007 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
7008 {
7009 transform->minlen = transform->maclen
7010 + cipher_info->block_size;
7011 }
7012 else
7013#endif
7014 {
7015 transform->minlen = transform->maclen
7016 + cipher_info->block_size
7017 - transform->maclen % cipher_info->block_size;
7018 }
7019
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007020 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
7021 {
7022 transform->minlen += transform->ivlen;
7023 }
7024 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007025 {
7026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7027 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7028 goto end;
7029 }
7030 }
7031 }
7032 else
7033#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7034 {
7035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7036 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7037 }
7038
7039 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7040 (unsigned) keylen,
7041 (unsigned) transform->minlen,
7042 (unsigned) transform->ivlen,
7043 (unsigned) transform->maclen ) );
7044
7045 /*
7046 * Finally setup the cipher contexts, IVs and MAC secrets.
7047 */
7048#if defined(MBEDTLS_SSL_CLI_C)
7049 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7050 {
7051 key1 = keyblk + mac_key_len * 2;
7052 key2 = keyblk + mac_key_len * 2 + keylen;
7053
7054 mac_enc = keyblk;
7055 mac_dec = keyblk + mac_key_len;
7056
7057 /*
7058 * This is not used in TLS v1.1.
7059 */
7060 iv_copy_len = ( transform->fixed_ivlen ) ?
7061 transform->fixed_ivlen : transform->ivlen;
7062 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7063 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7064 iv_copy_len );
7065 }
7066 else
7067#endif /* MBEDTLS_SSL_CLI_C */
7068#if defined(MBEDTLS_SSL_SRV_C)
7069 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7070 {
7071 key1 = keyblk + mac_key_len * 2 + keylen;
7072 key2 = keyblk + mac_key_len * 2;
7073
7074 mac_enc = keyblk + mac_key_len;
7075 mac_dec = keyblk;
7076
7077 /*
7078 * This is not used in TLS v1.1.
7079 */
7080 iv_copy_len = ( transform->fixed_ivlen ) ?
7081 transform->fixed_ivlen : transform->ivlen;
7082 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7083 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7084 iv_copy_len );
7085 }
7086 else
7087#endif /* MBEDTLS_SSL_SRV_C */
7088 {
7089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7090 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7091 goto end;
7092 }
7093
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007094 if( ssl != NULL && ssl->f_export_keys != NULL )
7095 {
7096 ssl->f_export_keys( ssl->p_export_keys,
7097 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7098 master, 48,
7099 randbytes + 32,
7100 randbytes,
7101 tls_prf_get_type( tls_prf ) );
7102 }
7103
7104#if defined(MBEDTLS_USE_PSA_CRYPTO)
7105 if( ( status = mbedtls_ssl_cipher_to_psa( cipher_info->type,
7106 transform->taglen,
7107 &alg,
7108 &key_type,
7109 &key_bits ) ) != PSA_SUCCESS )
7110 {
7111 ret = psa_ssl_status_to_mbedtls( status );
7112 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7113 goto end;
7114 }
7115
7116 transform->psa_alg = alg;
7117
7118 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7119 {
7120 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7121 psa_set_key_algorithm( &attributes, alg );
7122 psa_set_key_type( &attributes, key_type );
7123
7124 if( ( status = psa_import_key( &attributes,
7125 key1,
7126 PSA_BITS_TO_BYTES( key_bits ),
7127 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7128 {
7129 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7130 ret = psa_ssl_status_to_mbedtls( status );
7131 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7132 goto end;
7133 }
7134
7135 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7136
7137 if( ( status = psa_import_key( &attributes,
7138 key2,
7139 PSA_BITS_TO_BYTES( key_bits ),
7140 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7141 {
7142 ret = psa_ssl_status_to_mbedtls( status );
7143 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7144 goto end;
7145 }
7146 }
7147#else
7148 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7149 cipher_info ) ) != 0 )
7150 {
7151 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7152 goto end;
7153 }
7154
7155 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7156 cipher_info ) ) != 0 )
7157 {
7158 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7159 goto end;
7160 }
7161
7162 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7163 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7164 MBEDTLS_ENCRYPT ) ) != 0 )
7165 {
7166 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7167 goto end;
7168 }
7169
7170 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7171 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7172 MBEDTLS_DECRYPT ) ) != 0 )
7173 {
7174 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7175 goto end;
7176 }
7177
7178#if defined(MBEDTLS_CIPHER_MODE_CBC)
7179 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7180 {
7181 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7182 MBEDTLS_PADDING_NONE ) ) != 0 )
7183 {
7184 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7185 goto end;
7186 }
7187
7188 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7189 MBEDTLS_PADDING_NONE ) ) != 0 )
7190 {
7191 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7192 goto end;
7193 }
7194 }
7195#endif /* MBEDTLS_CIPHER_MODE_CBC */
7196#endif /* MBEDTLS_USE_PSA_CRYPTO */
7197
Neil Armstrong29c0c042022-03-17 17:47:28 +01007198#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7199 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7200 For AEAD-based ciphersuites, there is nothing to do here. */
7201 if( mac_key_len != 0 )
7202 {
7203#if defined(MBEDTLS_USE_PSA_CRYPTO)
7204 alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
7205 if( alg == 0 )
7206 {
7207 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7208 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_md_type_to_psa", ret );
7209 goto end;
7210 }
7211
7212 transform->psa_mac_alg = PSA_ALG_HMAC( alg );
7213
7214 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
7215 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( alg ) );
7216 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7217
7218 if( ( status = psa_import_key( &attributes,
7219 mac_enc, mac_key_len,
7220 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7221 {
7222 ret = psa_ssl_status_to_mbedtls( status );
7223 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7224 goto end;
7225 }
7226
Ronald Cronfb39f152022-03-25 14:36:28 +01007227 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
7228 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) &&
7229 ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007230 /* mbedtls_ct_hmac() requires the key to be exportable */
7231 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7232 PSA_KEY_USAGE_VERIFY_HASH );
7233 else
7234 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7235
7236 if( ( status = psa_import_key( &attributes,
7237 mac_dec, mac_key_len,
7238 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7239 {
7240 ret = psa_ssl_status_to_mbedtls( status );
7241 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7242 goto end;
7243 }
7244#else
7245 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7246 if( ret != 0 )
7247 goto end;
7248 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7249 if( ret != 0 )
7250 goto end;
7251#endif /* MBEDTLS_USE_PSA_CRYPTO */
7252 }
7253#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7254
7255 ((void) mac_dec);
7256 ((void) mac_enc);
7257
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007258end:
7259 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7260 return( ret );
7261}
7262
Jerry Yuee40f9d2022-02-17 14:55:16 +08007263#if defined(MBEDTLS_USE_PSA_CRYPTO)
7264int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7265 unsigned char *hash, size_t *hashlen,
7266 unsigned char *data, size_t data_len,
7267 mbedtls_md_type_t md_alg )
7268{
7269 psa_status_t status;
7270 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
7271 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
7272
7273 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7274
7275 if( ( status = psa_hash_setup( &hash_operation,
7276 hash_alg ) ) != PSA_SUCCESS )
7277 {
7278 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7279 goto exit;
7280 }
7281
7282 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
7283 64 ) ) != PSA_SUCCESS )
7284 {
7285 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7286 goto exit;
7287 }
7288
7289 if( ( status = psa_hash_update( &hash_operation,
7290 data, data_len ) ) != PSA_SUCCESS )
7291 {
7292 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7293 goto exit;
7294 }
7295
7296 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
7297 hashlen ) ) != PSA_SUCCESS )
7298 {
7299 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
7300 goto exit;
7301 }
7302
7303exit:
7304 if( status != PSA_SUCCESS )
7305 {
7306 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7307 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7308 switch( status )
7309 {
7310 case PSA_ERROR_NOT_SUPPORTED:
7311 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
7312 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
7313 case PSA_ERROR_BUFFER_TOO_SMALL:
7314 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
7315 case PSA_ERROR_INSUFFICIENT_MEMORY:
7316 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
7317 default:
7318 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
7319 }
7320 }
7321 return( 0 );
7322}
7323
7324#else
7325
7326int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7327 unsigned char *hash, size_t *hashlen,
7328 unsigned char *data, size_t data_len,
7329 mbedtls_md_type_t md_alg )
7330{
7331 int ret = 0;
7332 mbedtls_md_context_t ctx;
7333 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
7334 *hashlen = mbedtls_md_get_size( md_info );
7335
7336 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
7337
7338 mbedtls_md_init( &ctx );
7339
7340 /*
7341 * digitally-signed struct {
7342 * opaque client_random[32];
7343 * opaque server_random[32];
7344 * ServerDHParams params;
7345 * };
7346 */
7347 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
7348 {
7349 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7350 goto exit;
7351 }
7352 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
7353 {
7354 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
7355 goto exit;
7356 }
7357 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
7358 {
7359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7360 goto exit;
7361 }
7362 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
7363 {
7364 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7365 goto exit;
7366 }
7367 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
7368 {
7369 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
7370 goto exit;
7371 }
7372
7373exit:
7374 mbedtls_md_free( &ctx );
7375
7376 if( ret != 0 )
7377 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7378 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7379
7380 return( ret );
7381}
7382#endif /* MBEDTLS_USE_PSA_CRYPTO */
7383
Jerry Yud9d91da2022-02-17 14:57:06 +08007384#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
7385
7386/* Find an entry in a signature-hash set matching a given hash algorithm. */
7387mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
7388 mbedtls_pk_type_t sig_alg )
7389{
7390 switch( sig_alg )
7391 {
7392 case MBEDTLS_PK_RSA:
7393 return( set->rsa );
7394 case MBEDTLS_PK_ECDSA:
7395 return( set->ecdsa );
7396 default:
7397 return( MBEDTLS_MD_NONE );
7398 }
7399}
7400
7401/* Add a signature-hash-pair to a signature-hash set */
7402void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
7403 mbedtls_pk_type_t sig_alg,
7404 mbedtls_md_type_t md_alg )
7405{
7406 switch( sig_alg )
7407 {
7408 case MBEDTLS_PK_RSA:
7409 if( set->rsa == MBEDTLS_MD_NONE )
7410 set->rsa = md_alg;
7411 break;
7412
7413 case MBEDTLS_PK_ECDSA:
7414 if( set->ecdsa == MBEDTLS_MD_NONE )
7415 set->ecdsa = md_alg;
7416 break;
7417
7418 default:
7419 break;
7420 }
7421}
7422
7423/* Allow exactly one hash algorithm for each signature. */
7424void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
7425 mbedtls_md_type_t md_alg )
7426{
7427 set->rsa = md_alg;
7428 set->ecdsa = md_alg;
7429}
7430
7431#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7432
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08007433/* Serialization of TLS 1.2 sessions:
7434 *
7435 * struct {
7436 * uint64 start_time;
7437 * uint8 ciphersuite[2]; // defined by the standard
7438 * uint8 compression; // 0 or 1
7439 * uint8 session_id_len; // at most 32
7440 * opaque session_id[32];
7441 * opaque master[48]; // fixed length in the standard
7442 * uint32 verify_result;
7443 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
7444 * opaque ticket<0..2^24-1>; // length 0 means no ticket
7445 * uint32 ticket_lifetime;
7446 * uint8 mfl_code; // up to 255 according to standard
7447 * uint8 encrypt_then_mac; // 0 or 1
7448 * } serialized_session_tls12;
7449 *
7450 */
7451static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session,
7452 unsigned char *buf,
7453 size_t buf_len )
7454{
7455 unsigned char *p = buf;
7456 size_t used = 0;
7457
7458#if defined(MBEDTLS_HAVE_TIME)
7459 uint64_t start;
7460#endif
7461#if defined(MBEDTLS_X509_CRT_PARSE_C)
7462#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7463 size_t cert_len;
7464#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7465#endif /* MBEDTLS_X509_CRT_PARSE_C */
7466
7467 /*
7468 * Time
7469 */
7470#if defined(MBEDTLS_HAVE_TIME)
7471 used += 8;
7472
7473 if( used <= buf_len )
7474 {
7475 start = (uint64_t) session->start;
7476
7477 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
7478 p += 8;
7479 }
7480#endif /* MBEDTLS_HAVE_TIME */
7481
7482 /*
7483 * Basic mandatory fields
7484 */
7485 used += 2 /* ciphersuite */
7486 + 1 /* compression */
7487 + 1 /* id_len */
7488 + sizeof( session->id )
7489 + sizeof( session->master )
7490 + 4; /* verify_result */
7491
7492 if( used <= buf_len )
7493 {
7494 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
7495 p += 2;
7496
7497 *p++ = MBEDTLS_BYTE_0( session->compression );
7498
7499 *p++ = MBEDTLS_BYTE_0( session->id_len );
7500 memcpy( p, session->id, 32 );
7501 p += 32;
7502
7503 memcpy( p, session->master, 48 );
7504 p += 48;
7505
7506 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
7507 p += 4;
7508 }
7509
7510 /*
7511 * Peer's end-entity certificate
7512 */
7513#if defined(MBEDTLS_X509_CRT_PARSE_C)
7514#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7515 if( session->peer_cert == NULL )
7516 cert_len = 0;
7517 else
7518 cert_len = session->peer_cert->raw.len;
7519
7520 used += 3 + cert_len;
7521
7522 if( used <= buf_len )
7523 {
7524 *p++ = MBEDTLS_BYTE_2( cert_len );
7525 *p++ = MBEDTLS_BYTE_1( cert_len );
7526 *p++ = MBEDTLS_BYTE_0( cert_len );
7527
7528 if( session->peer_cert != NULL )
7529 {
7530 memcpy( p, session->peer_cert->raw.p, cert_len );
7531 p += cert_len;
7532 }
7533 }
7534#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7535 if( session->peer_cert_digest != NULL )
7536 {
7537 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
7538 if( used <= buf_len )
7539 {
7540 *p++ = (unsigned char) session->peer_cert_digest_type;
7541 *p++ = (unsigned char) session->peer_cert_digest_len;
7542 memcpy( p, session->peer_cert_digest,
7543 session->peer_cert_digest_len );
7544 p += session->peer_cert_digest_len;
7545 }
7546 }
7547 else
7548 {
7549 used += 2;
7550 if( used <= buf_len )
7551 {
7552 *p++ = (unsigned char) MBEDTLS_MD_NONE;
7553 *p++ = 0;
7554 }
7555 }
7556#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7557#endif /* MBEDTLS_X509_CRT_PARSE_C */
7558
7559 /*
7560 * Session ticket if any, plus associated data
7561 */
7562#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7563 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
7564
7565 if( used <= buf_len )
7566 {
7567 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
7568 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
7569 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
7570
7571 if( session->ticket != NULL )
7572 {
7573 memcpy( p, session->ticket, session->ticket_len );
7574 p += session->ticket_len;
7575 }
7576
7577 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
7578 p += 4;
7579 }
7580#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7581
7582 /*
7583 * Misc extension-related info
7584 */
7585#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7586 used += 1;
7587
7588 if( used <= buf_len )
7589 *p++ = session->mfl_code;
7590#endif
7591
7592#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7593 used += 1;
7594
7595 if( used <= buf_len )
7596 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
7597#endif
7598
7599 return( used );
7600}
7601
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08007602static int ssl_session_load_tls12( mbedtls_ssl_session *session,
7603 const unsigned char *buf,
7604 size_t len )
7605{
7606#if defined(MBEDTLS_HAVE_TIME)
7607 uint64_t start;
7608#endif
7609#if defined(MBEDTLS_X509_CRT_PARSE_C)
7610#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7611 size_t cert_len;
7612#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7613#endif /* MBEDTLS_X509_CRT_PARSE_C */
7614
7615 const unsigned char *p = buf;
7616 const unsigned char * const end = buf + len;
7617
7618 /*
7619 * Time
7620 */
7621#if defined(MBEDTLS_HAVE_TIME)
7622 if( 8 > (size_t)( end - p ) )
7623 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7624
7625 start = ( (uint64_t) p[0] << 56 ) |
7626 ( (uint64_t) p[1] << 48 ) |
7627 ( (uint64_t) p[2] << 40 ) |
7628 ( (uint64_t) p[3] << 32 ) |
7629 ( (uint64_t) p[4] << 24 ) |
7630 ( (uint64_t) p[5] << 16 ) |
7631 ( (uint64_t) p[6] << 8 ) |
7632 ( (uint64_t) p[7] );
7633 p += 8;
7634
7635 session->start = (time_t) start;
7636#endif /* MBEDTLS_HAVE_TIME */
7637
7638 /*
7639 * Basic mandatory fields
7640 */
7641 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
7642 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7643
7644 session->ciphersuite = ( p[0] << 8 ) | p[1];
7645 p += 2;
7646
7647 session->compression = *p++;
7648
7649 session->id_len = *p++;
7650 memcpy( session->id, p, 32 );
7651 p += 32;
7652
7653 memcpy( session->master, p, 48 );
7654 p += 48;
7655
7656 session->verify_result = ( (uint32_t) p[0] << 24 ) |
7657 ( (uint32_t) p[1] << 16 ) |
7658 ( (uint32_t) p[2] << 8 ) |
7659 ( (uint32_t) p[3] );
7660 p += 4;
7661
7662 /* Immediately clear invalid pointer values that have been read, in case
7663 * we exit early before we replaced them with valid ones. */
7664#if defined(MBEDTLS_X509_CRT_PARSE_C)
7665#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7666 session->peer_cert = NULL;
7667#else
7668 session->peer_cert_digest = NULL;
7669#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7670#endif /* MBEDTLS_X509_CRT_PARSE_C */
7671#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7672 session->ticket = NULL;
7673#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7674
7675 /*
7676 * Peer certificate
7677 */
7678#if defined(MBEDTLS_X509_CRT_PARSE_C)
7679#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7680 /* Deserialize CRT from the end of the ticket. */
7681 if( 3 > (size_t)( end - p ) )
7682 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7683
7684 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
7685 p += 3;
7686
7687 if( cert_len != 0 )
7688 {
7689 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7690
7691 if( cert_len > (size_t)( end - p ) )
7692 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7693
7694 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7695
7696 if( session->peer_cert == NULL )
7697 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7698
7699 mbedtls_x509_crt_init( session->peer_cert );
7700
7701 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
7702 p, cert_len ) ) != 0 )
7703 {
7704 mbedtls_x509_crt_free( session->peer_cert );
7705 mbedtls_free( session->peer_cert );
7706 session->peer_cert = NULL;
7707 return( ret );
7708 }
7709
7710 p += cert_len;
7711 }
7712#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7713 /* Deserialize CRT digest from the end of the ticket. */
7714 if( 2 > (size_t)( end - p ) )
7715 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7716
7717 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
7718 session->peer_cert_digest_len = (size_t) *p++;
7719
7720 if( session->peer_cert_digest_len != 0 )
7721 {
7722 const mbedtls_md_info_t *md_info =
7723 mbedtls_md_info_from_type( session->peer_cert_digest_type );
7724 if( md_info == NULL )
7725 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7726 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
7727 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7728
7729 if( session->peer_cert_digest_len > (size_t)( end - p ) )
7730 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7731
7732 session->peer_cert_digest =
7733 mbedtls_calloc( 1, session->peer_cert_digest_len );
7734 if( session->peer_cert_digest == NULL )
7735 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7736
7737 memcpy( session->peer_cert_digest, p,
7738 session->peer_cert_digest_len );
7739 p += session->peer_cert_digest_len;
7740 }
7741#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7742#endif /* MBEDTLS_X509_CRT_PARSE_C */
7743
7744 /*
7745 * Session ticket and associated data
7746 */
7747#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7748 if( 3 > (size_t)( end - p ) )
7749 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7750
7751 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
7752 p += 3;
7753
7754 if( session->ticket_len != 0 )
7755 {
7756 if( session->ticket_len > (size_t)( end - p ) )
7757 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7758
7759 session->ticket = mbedtls_calloc( 1, session->ticket_len );
7760 if( session->ticket == NULL )
7761 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7762
7763 memcpy( session->ticket, p, session->ticket_len );
7764 p += session->ticket_len;
7765 }
7766
7767 if( 4 > (size_t)( end - p ) )
7768 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7769
7770 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
7771 ( (uint32_t) p[1] << 16 ) |
7772 ( (uint32_t) p[2] << 8 ) |
7773 ( (uint32_t) p[3] );
7774 p += 4;
7775#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7776
7777 /*
7778 * Misc extension-related info
7779 */
7780#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7781 if( 1 > (size_t)( end - p ) )
7782 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7783
7784 session->mfl_code = *p++;
7785#endif
7786
7787#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7788 if( 1 > (size_t)( end - p ) )
7789 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7790
7791 session->encrypt_then_mac = *p++;
7792#endif
7793
7794 /* Done, should have consumed entire buffer */
7795 if( p != end )
7796 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7797
7798 return( 0 );
7799}
Jerry Yudc7bd172022-02-17 13:44:15 +08007800#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007802#endif /* MBEDTLS_SSL_TLS_C */