blob: 15784e6dd11498d8ad8def322f736134c30eec08 [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 Cron27c85e72022-03-08 11:37:55 +010041#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000042#include "ssl_misc.h"
Janos Follath73c616b2019-12-18 15:07:04 +000043#include "mbedtls/debug.h"
44#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050045#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010046#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020047#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020048
Rich Evans00ab4702015-02-06 13:43:58 +000049#include <string.h>
50
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050051#if defined(MBEDTLS_USE_PSA_CRYPTO)
52#include "mbedtls/psa_util.h"
53#include "psa/crypto.h"
54#endif
55
Janos Follath23bdca02016-10-07 14:47:14 +010056#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020058#endif
59
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020060#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010061
Hanno Beckera0e20d02019-05-15 14:03:01 +010062#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010063/* Top-level Connection ID API */
64
Hanno Becker8367ccc2019-05-14 11:30:10 +010065int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
66 size_t len,
67 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +010068{
69 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
70 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
71
Hanno Becker611ac772019-05-14 11:45:26 +010072 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
73 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
74 {
75 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
76 }
77
78 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010079 conf->cid_len = len;
80 return( 0 );
81}
82
Hanno Beckerf8542cf2019-04-09 15:22:03 +010083int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
84 int enable,
85 unsigned char const *own_cid,
86 size_t own_cid_len )
87{
Hanno Becker76a79ab2019-05-03 14:38:32 +010088 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
89 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
90
Hanno Beckerca092242019-04-25 16:01:49 +010091 ssl->negotiate_cid = enable;
92 if( enable == MBEDTLS_SSL_CID_DISABLED )
93 {
94 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
95 return( 0 );
96 }
97 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +010098 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +010099
Hanno Beckerad4a1372019-05-03 13:06:44 +0100100 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100101 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100102 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
103 (unsigned) own_cid_len,
104 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100105 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
106 }
107
108 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100109 /* Truncation is not an issue here because
110 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
111 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100112
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100113 return( 0 );
114}
115
116int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
117 int *enabled,
118 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
119 size_t *peer_cid_len )
120{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100121 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100122
Hanno Becker76a79ab2019-05-03 14:38:32 +0100123 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
124 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
125 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100126 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100127 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100128
Hanno Beckerc5f24222019-05-03 12:54:52 +0100129 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
130 * were used, but client and server requested the empty CID.
131 * This is indistinguishable from not using the CID extension
132 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100133 if( ssl->transform_in->in_cid_len == 0 &&
134 ssl->transform_in->out_cid_len == 0 )
135 {
136 return( 0 );
137 }
138
Hanno Becker615ef172019-05-22 16:50:35 +0100139 if( peer_cid_len != NULL )
140 {
141 *peer_cid_len = ssl->transform_in->out_cid_len;
142 if( peer_cid != NULL )
143 {
144 memcpy( peer_cid, ssl->transform_in->out_cid,
145 ssl->transform_in->out_cid_len );
146 }
147 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100148
149 *enabled = MBEDTLS_SSL_CID_ENABLED;
150
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100151 return( 0 );
152}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100153#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200158/*
159 * Convert max_fragment_length codes to length.
160 * RFC 6066 says:
161 * enum{
162 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
163 * } MaxFragmentLength;
164 * and we add 0 -> extension unused
165 */
Angus Grattond8213d02016-05-25 20:56:48 +1000166static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200167{
Angus Grattond8213d02016-05-25 20:56:48 +1000168 switch( mfl )
169 {
170 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
171 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
172 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
173 return 512;
174 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
175 return 1024;
176 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
177 return 2048;
178 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
179 return 4096;
180 default:
181 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
182 }
183}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200185
Hanno Becker52055ae2019-02-06 14:30:46 +0000186int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
187 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200188{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_ssl_session_free( dst );
190 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200191
吴敬辉0b716112021-11-29 10:46:35 +0800192#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
193 dst->ticket = NULL;
194#endif
195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000197
198#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200199 if( src->peer_cert != NULL )
200 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000201 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200202
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200203 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200204 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200205 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200210 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200213 dst->peer_cert = NULL;
214 return( ret );
215 }
216 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000217#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000218 if( src->peer_cert_digest != NULL )
219 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000220 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000221 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000222 if( dst->peer_cert_digest == NULL )
223 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
224
225 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
226 src->peer_cert_digest_len );
227 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000228 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000229 }
230#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200233
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200234#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200235 if( src->ticket != NULL )
236 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200237 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200238 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200239 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200240
241 memcpy( dst->ticket, src->ticket, src->ticket_len );
242 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200243#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200244
245 return( 0 );
246}
247
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500248#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
249static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
250{
251 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
252 if( resized_buffer == NULL )
253 return -1;
254
255 /* We want to copy len_new bytes when downsizing the buffer, and
256 * len_old bytes when upsizing, so we choose the smaller of two sizes,
257 * to fit one buffer into another. Size checks, ensuring that no data is
258 * lost, are done outside of this function. */
259 memcpy( resized_buffer, *buffer,
260 ( len_new < *len_old ) ? len_new : *len_old );
261 mbedtls_platform_zeroize( *buffer, *len_old );
262 mbedtls_free( *buffer );
263
264 *buffer = resized_buffer;
265 *len_old = len_new;
266
267 return 0;
268}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200269
270static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500271 size_t in_buf_new_len,
272 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200273{
274 int modified = 0;
275 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
276 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
277 if( ssl->in_buf != NULL )
278 {
279 written_in = ssl->in_msg - ssl->in_buf;
280 iv_offset_in = ssl->in_iv - ssl->in_buf;
281 len_offset_in = ssl->in_len - ssl->in_buf;
282 if( downsizing ?
283 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
284 ssl->in_buf_len < in_buf_new_len )
285 {
286 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
287 {
288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
289 }
290 else
291 {
Paul Elliottb7449902021-03-10 18:14:58 +0000292 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
293 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200294 modified = 1;
295 }
296 }
297 }
298
299 if( ssl->out_buf != NULL )
300 {
301 written_out = ssl->out_msg - ssl->out_buf;
302 iv_offset_out = ssl->out_iv - ssl->out_buf;
303 len_offset_out = ssl->out_len - ssl->out_buf;
304 if( downsizing ?
305 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
306 ssl->out_buf_len < out_buf_new_len )
307 {
308 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
309 {
310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
311 }
312 else
313 {
Paul Elliottb7449902021-03-10 18:14:58 +0000314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
315 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200316 modified = 1;
317 }
318 }
319 }
320 if( modified )
321 {
322 /* Update pointers here to avoid doing it twice. */
323 mbedtls_ssl_reset_in_out_pointers( ssl );
324 /* Fields below might not be properly updated with record
325 * splitting or with CID, so they are manually updated here. */
326 ssl->out_msg = ssl->out_buf + written_out;
327 ssl->out_len = ssl->out_buf + len_offset_out;
328 ssl->out_iv = ssl->out_buf + iv_offset_out;
329
330 ssl->in_msg = ssl->in_buf + written_in;
331 ssl->in_len = ssl->in_buf + len_offset_in;
332 ssl->in_iv = ssl->in_buf + iv_offset_in;
333 }
334}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500335#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
336
Jerry Yudb8c48a2022-01-27 14:54:54 +0800337#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800338
339#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
340typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
341 const char *label,
342 const unsigned char *random, size_t rlen,
343 unsigned char *dstbuf, size_t dlen );
344
345static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
346
347#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
348
349/* Type for the TLS PRF */
350typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
351 const unsigned char *, size_t,
352 unsigned char *, size_t);
353
354static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
355 int ciphersuite,
356 const unsigned char master[48],
357#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
358 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
359 int encrypt_then_mac,
360#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
361 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
362 ssl_tls_prf_t tls_prf,
363 const unsigned char randbytes[64],
364 int minor_ver,
365 unsigned endpoint,
366 const mbedtls_ssl_context *ssl );
367
368#if defined(MBEDTLS_SHA256_C)
369static int tls_prf_sha256( const unsigned char *secret, size_t slen,
370 const char *label,
371 const unsigned char *random, size_t rlen,
372 unsigned char *dstbuf, size_t dlen );
373static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
374static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
375
376#endif /* MBEDTLS_SHA256_C */
377
378#if defined(MBEDTLS_SHA384_C)
379static int tls_prf_sha384( const unsigned char *secret, size_t slen,
380 const char *label,
381 const unsigned char *random, size_t rlen,
382 unsigned char *dstbuf, size_t dlen );
383
384static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
385static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
386#endif /* MBEDTLS_SHA384_C */
387
388static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session,
389 unsigned char *buf,
390 size_t buf_len );
391static int ssl_session_load_tls12( mbedtls_ssl_session *session,
392 const unsigned char *buf,
393 size_t len );
394#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
395
Jerry Yu53d23e22022-02-09 16:25:09 +0800396static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
397
Jerry Yudb8c48a2022-01-27 14:54:54 +0800398#if defined(MBEDTLS_SHA256_C)
399static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Jerry Yu53d23e22022-02-09 16:25:09 +0800400#endif /* MBEDTLS_SHA256_C */
Jerry Yudb8c48a2022-01-27 14:54:54 +0800401
402#if defined(MBEDTLS_SHA384_C)
403static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Jerry Yu53d23e22022-02-09 16:25:09 +0800404#endif /* MBEDTLS_SHA384_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000405
Ron Eldor51d3ab52019-05-12 14:54:30 +0300406int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
407 const unsigned char *secret, size_t slen,
408 const char *label,
409 const unsigned char *random, size_t rlen,
410 unsigned char *dstbuf, size_t dlen )
411{
412 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
413
414 switch( prf )
415 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300416#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +0200417#if defined(MBEDTLS_SHA384_C)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300418 case MBEDTLS_SSL_TLS_PRF_SHA384:
419 tls_prf = tls_prf_sha384;
420 break;
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +0200421#endif /* MBEDTLS_SHA384_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300422#if defined(MBEDTLS_SHA256_C)
423 case MBEDTLS_SSL_TLS_PRF_SHA256:
424 tls_prf = tls_prf_sha256;
425 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300426#endif /* MBEDTLS_SHA256_C */
427#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300428 default:
429 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
430 }
431
432 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
433}
434
Jerry Yuc73c6182022-02-08 20:29:25 +0800435#if defined(MBEDTLS_X509_CRT_PARSE_C)
436static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
437{
438#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
439 if( session->peer_cert != NULL )
440 {
441 mbedtls_x509_crt_free( session->peer_cert );
442 mbedtls_free( session->peer_cert );
443 session->peer_cert = NULL;
444 }
445#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
446 if( session->peer_cert_digest != NULL )
447 {
448 /* Zeroization is not necessary. */
449 mbedtls_free( session->peer_cert_digest );
450 session->peer_cert_digest = NULL;
451 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
452 session->peer_cert_digest_len = 0;
453 }
454#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
455}
456#endif /* MBEDTLS_X509_CRT_PARSE_C */
457
458void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
459 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
460{
461 ((void) ciphersuite_info);
462
463#if defined(MBEDTLS_SHA384_C)
464 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
465 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
466 else
467#endif
468#if defined(MBEDTLS_SHA256_C)
469 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
470 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
471 else
472#endif
473 {
474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
475 return;
476 }
477}
478
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100479static void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
480 unsigned hs_type,
481 size_t total_hs_len )
482{
483 unsigned char hs_hdr[4];
484
485 /* Build HS header for checksum update. */
486 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
487 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
488 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
489 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
490
491 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
492}
493
494void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
495 unsigned hs_type,
496 unsigned char const *msg,
497 size_t msg_len )
498{
499 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
500 ssl->handshake->update_checksum( ssl, msg, msg_len );
501}
502
Jerry Yuc73c6182022-02-08 20:29:25 +0800503void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
504{
505 ((void) ssl);
506#if defined(MBEDTLS_SHA256_C)
507#if defined(MBEDTLS_USE_PSA_CRYPTO)
508 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
509 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
510#else
511 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
512#endif
513#endif
514#if defined(MBEDTLS_SHA384_C)
515#if defined(MBEDTLS_USE_PSA_CRYPTO)
516 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
517 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
518#else
519 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
520#endif
521#endif
522}
523
524static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
525 const unsigned char *buf, size_t len )
526{
527#if defined(MBEDTLS_SHA256_C)
528#if defined(MBEDTLS_USE_PSA_CRYPTO)
529 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
530#else
531 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
532#endif
533#endif
534#if defined(MBEDTLS_SHA384_C)
535#if defined(MBEDTLS_USE_PSA_CRYPTO)
536 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
537#else
538 mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len );
539#endif
540#endif
541}
542
543#if defined(MBEDTLS_SHA256_C)
544static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
545 const unsigned char *buf, size_t len )
546{
547#if defined(MBEDTLS_USE_PSA_CRYPTO)
548 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
549#else
550 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
551#endif
552}
553#endif
554
555#if defined(MBEDTLS_SHA384_C)
556static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
557 const unsigned char *buf, size_t len )
558{
559#if defined(MBEDTLS_USE_PSA_CRYPTO)
560 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
561#else
562 mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len );
563#endif
564}
565#endif
566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200568{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500572#if defined(MBEDTLS_USE_PSA_CRYPTO)
573 handshake->fin_sha256_psa = psa_hash_operation_init();
574 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
575#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200577 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200578#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500579#endif
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +0200580#if defined(MBEDTLS_SHA384_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500581#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500582 handshake->fin_sha384_psa = psa_hash_operation_init();
583 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500584#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 mbedtls_sha512_init( &handshake->fin_sha512 );
TRodziewicz26371e42021-06-08 16:45:41 +0200586 mbedtls_sha512_starts( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200587#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500588#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200589
590 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100591
592#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +0100593 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +0100594 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
595#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597#if defined(MBEDTLS_DHM_C)
598 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200599#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600#if defined(MBEDTLS_ECDH_C)
601 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200602#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200603#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200604 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200605#if defined(MBEDTLS_SSL_CLI_C)
606 handshake->ecjpake_cache = NULL;
607 handshake->ecjpake_cache_len = 0;
608#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200609#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200610
Gilles Peskineeccd8882020-03-10 12:19:08 +0100611#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200612 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200613#endif
614
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200615#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
616 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
617#endif
Hanno Becker75173122019-02-06 16:18:31 +0000618
619#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
620 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
621 mbedtls_pk_init( &handshake->peer_pubkey );
622#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200623}
624
Hanno Beckera18d1322018-01-03 14:27:32 +0000625void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200626{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200628
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100629#if defined(MBEDTLS_USE_PSA_CRYPTO)
630 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
631 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100632#else
633 mbedtls_cipher_init( &transform->cipher_ctx_enc );
634 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100635#endif
636
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000637#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100638#if defined(MBEDTLS_USE_PSA_CRYPTO)
639 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
640 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100641#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_md_init( &transform->md_ctx_enc );
643 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000644#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100645#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200646}
647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200649{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200651}
652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000654{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200655 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000656 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200658 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200660 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200661 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200662
663 /*
664 * Either the pointers are now NULL or cleared properly and can be freed.
665 * Now allocate missing structures.
666 */
667 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200668 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200669 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200670 }
Paul Bakker48916f92012-09-16 19:57:18 +0000671
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200672 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200673 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200674 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200675 }
Paul Bakker48916f92012-09-16 19:57:18 +0000676
Paul Bakker82788fb2014-10-20 13:59:19 +0200677 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200678 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200679 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200680 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500681#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
682 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400683
Andrzej Kurek4a063792020-10-21 15:08:44 +0200684 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
685 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500686#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000687
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200688 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000689 if( ssl->handshake == NULL ||
690 ssl->transform_negotiate == NULL ||
691 ssl->session_negotiate == NULL )
692 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 mbedtls_free( ssl->handshake );
696 mbedtls_free( ssl->transform_negotiate );
697 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200698
699 ssl->handshake = NULL;
700 ssl->transform_negotiate = NULL;
701 ssl->session_negotiate = NULL;
702
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200703 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000704 }
705
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200706 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000708 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200709 ssl_handshake_params_init( ssl->handshake );
710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200712 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
713 {
714 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200715
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200716 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
717 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
718 else
719 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200720
Hanno Becker0f57a652020-02-05 10:37:26 +0000721 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200722 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200723#endif
724
Brett Warrene0edc842021-08-17 09:53:13 +0100725/*
726 * curve_list is translated to IANA TLS group identifiers here because
727 * mbedtls_ssl_conf_curves returns void and so can't return
728 * any error codes.
729 */
730#if defined(MBEDTLS_ECP_C)
731#if !defined(MBEDTLS_DEPRECATED_REMOVED)
732 /* Heap allocate and translate curve_list from internal to IANA group ids */
733 if ( ssl->conf->curve_list != NULL )
734 {
735 size_t length;
736 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
737
738 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
739 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
740
741 /* Leave room for zero termination */
742 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
743 if ( group_list == NULL )
744 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
745
746 for( size_t i = 0; i < length; i++ )
747 {
748 const mbedtls_ecp_curve_info *info =
749 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
750 if ( info == NULL )
751 {
752 mbedtls_free( group_list );
753 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
754 }
755 group_list[i] = info->tls_id;
756 }
757
758 group_list[length] = 0;
759
760 ssl->handshake->group_list = group_list;
761 ssl->handshake->group_list_heap_allocated = 1;
762 }
763 else
764 {
765 ssl->handshake->group_list = ssl->conf->group_list;
766 ssl->handshake->group_list_heap_allocated = 0;
767 }
768#endif /* MBEDTLS_DEPRECATED_REMOVED */
769#endif /* MBEDTLS_ECP_C */
770
Jerry Yuf017ee42022-01-12 15:49:48 +0800771#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
772#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800773#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800774 /* Heap allocate and translate sig_hashes from internal hash identifiers to
775 signature algorithms IANA identifiers. */
776 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800777 ssl->conf->sig_hashes != NULL )
778 {
779 const int *md;
780 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800781 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800782 uint16_t *p;
783
Jerry Yub476a442022-01-21 18:14:45 +0800784#if defined(static_assert)
785 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
786 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
787 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800788#endif
789
Jerry Yuf017ee42022-01-12 15:49:48 +0800790 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
791 {
792 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
793 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800794#if defined(MBEDTLS_ECDSA_C)
795 sig_algs_len += sizeof( uint16_t );
796#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800797
Jerry Yub476a442022-01-21 18:14:45 +0800798#if defined(MBEDTLS_RSA_C)
799 sig_algs_len += sizeof( uint16_t );
800#endif
801 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
802 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800803 }
804
Jerry Yub476a442022-01-21 18:14:45 +0800805 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800806 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
807
Jerry Yub476a442022-01-21 18:14:45 +0800808 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
809 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800810 if( ssl->handshake->sig_algs == NULL )
811 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
812
813 p = (uint16_t *)ssl->handshake->sig_algs;
814 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
815 {
816 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
817 if( hash == MBEDTLS_SSL_HASH_NONE )
818 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800819#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800820 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
821 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800822#endif
823#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800824 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
825 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800826#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800827 }
828 *p = MBEDTLS_TLS1_3_SIG_NONE;
829 ssl->handshake->sig_algs_heap_allocated = 1;
830 }
831 else
Jerry Yua69269a2022-01-17 21:06:01 +0800832#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800833 {
834 ssl->handshake->sig_algs = ssl->conf->sig_algs;
835 ssl->handshake->sig_algs_heap_allocated = 0;
836 }
837#endif /* MBEDTLS_DEPRECATED_REMOVED */
838#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000839 return( 0 );
840}
841
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200842#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200843/* Dummy cookie callbacks for defaults */
844static int ssl_cookie_write_dummy( void *ctx,
845 unsigned char **p, unsigned char *end,
846 const unsigned char *cli_id, size_t cli_id_len )
847{
848 ((void) ctx);
849 ((void) p);
850 ((void) end);
851 ((void) cli_id);
852 ((void) cli_id_len);
853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200855}
856
857static int ssl_cookie_check_dummy( void *ctx,
858 const unsigned char *cookie, size_t cookie_len,
859 const unsigned char *cli_id, size_t cli_id_len )
860{
861 ((void) ctx);
862 ((void) cookie);
863 ((void) cookie_len);
864 ((void) cli_id);
865 ((void) cli_id_len);
866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200868}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200869#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200870
Paul Bakker5121ce52009-01-03 21:22:43 +0000871/*
872 * Initialize an SSL context
873 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200874void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
875{
876 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
877}
878
Jerry Yu60835a82021-08-04 10:13:52 +0800879static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
880{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100881 const mbedtls_ssl_config *conf = ssl->conf;
882
Ronald Cron6f135e12021-12-08 16:57:54 +0100883#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100884 if( mbedtls_ssl_conf_is_tls13_enabled( conf ) &&
885 ( conf->endpoint == MBEDTLS_SSL_IS_SERVER ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800886 {
Ronald Cron086ee0b2022-03-15 15:18:51 +0100887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
888 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
889 }
890
891 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
892 {
893 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jerry Yu60835a82021-08-04 10:13:52 +0800894 {
Ronald Cron086ee0b2022-03-15 15:18:51 +0100895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
Jerry Yu60835a82021-08-04 10:13:52 +0800896 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
897 }
898 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
899 return( 0 );
900 }
901#endif
902
903#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100904 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800905 {
906 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
907 return( 0 );
908 }
909#endif
910
Ronald Cron6f135e12021-12-08 16:57:54 +0100911#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100912 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800913 {
914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Hybrid TLS 1.2 + TLS 1.3 configurations are not yet supported" ) );
915 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
916 }
917#endif
918
919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
920 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
921}
922
923static int ssl_conf_check(const mbedtls_ssl_context *ssl)
924{
925 int ret;
926 ret = ssl_conf_version_check( ssl );
927 if( ret != 0 )
928 return( ret );
929
930 /* Space for further checks */
931
932 return( 0 );
933}
934
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200935/*
936 * Setup an SSL context
937 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100938
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200939int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +0200940 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +0000941{
Janos Follath865b3eb2019-12-16 11:46:15 +0000942 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +0000943 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
944 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +0000945
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200946 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +0000947
Jerry Yu60835a82021-08-04 10:13:52 +0800948 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
949 return( ret );
950
Paul Bakker62f2dee2012-09-28 07:31:51 +0000951 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +0100952 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +0000953 */
k-stachowiakc9a5f022018-07-24 13:53:31 +0200954
955 /* Set to NULL in case of an error condition */
956 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +0200957
Darryl Greenb33cc762019-11-28 14:29:44 +0000958#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
959 ssl->in_buf_len = in_buf_len;
960#endif
961 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +1000962 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000963 {
Paul Elliottd48d5c62021-01-07 14:47:05 +0000964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +0200965 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +0200966 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +1000967 }
968
Darryl Greenb33cc762019-11-28 14:29:44 +0000969#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
970 ssl->out_buf_len = out_buf_len;
971#endif
972 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +1000973 if( ssl->out_buf == NULL )
974 {
Paul Elliottd48d5c62021-01-07 14:47:05 +0000975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +0200976 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +0200977 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +0000978 }
979
Hanno Becker3e6f8ab2020-02-05 10:40:57 +0000980 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200981
Johan Pascalb62bb512015-12-03 21:56:45 +0100982#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +0200983 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +0100984#endif
985
Paul Bakker48916f92012-09-16 19:57:18 +0000986 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +0200987 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +0000988
989 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +0200990
991error:
992 mbedtls_free( ssl->in_buf );
993 mbedtls_free( ssl->out_buf );
994
995 ssl->conf = NULL;
996
Darryl Greenb33cc762019-11-28 14:29:44 +0000997#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
998 ssl->in_buf_len = 0;
999 ssl->out_buf_len = 0;
1000#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001001 ssl->in_buf = NULL;
1002 ssl->out_buf = NULL;
1003
1004 ssl->in_hdr = NULL;
1005 ssl->in_ctr = NULL;
1006 ssl->in_len = NULL;
1007 ssl->in_iv = NULL;
1008 ssl->in_msg = NULL;
1009
1010 ssl->out_hdr = NULL;
1011 ssl->out_ctr = NULL;
1012 ssl->out_len = NULL;
1013 ssl->out_iv = NULL;
1014 ssl->out_msg = NULL;
1015
k-stachowiak9f7798e2018-07-31 16:52:32 +02001016 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001017}
1018
1019/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001020 * Reset an initialized and used SSL context for re-use while retaining
1021 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001022 *
1023 * If partial is non-zero, keep data in the input buffer and client ID.
1024 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001025 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001026void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1027 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001028{
Darryl Greenb33cc762019-11-28 14:29:44 +00001029#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1030 size_t in_buf_len = ssl->in_buf_len;
1031 size_t out_buf_len = ssl->out_buf_len;
1032#else
1033 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1034 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1035#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001036
Hanno Beckerb0302c42021-08-03 09:39:42 +01001037#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1038 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001039#endif
1040
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001041 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001042 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001043
Hanno Beckerb0302c42021-08-03 09:39:42 +01001044 mbedtls_ssl_reset_in_out_pointers( ssl );
1045
1046 /* Reset incoming message parsing */
1047 ssl->in_offt = NULL;
1048 ssl->nb_zero = 0;
1049 ssl->in_msgtype = 0;
1050 ssl->in_msglen = 0;
1051 ssl->in_hslen = 0;
1052 ssl->keep_current_message = 0;
1053 ssl->transform_in = NULL;
1054
1055#if defined(MBEDTLS_SSL_PROTO_DTLS)
1056 ssl->next_record_offset = 0;
1057 ssl->in_epoch = 0;
1058#endif
1059
1060 /* Keep current datagram if partial == 1 */
1061 if( partial == 0 )
1062 {
1063 ssl->in_left = 0;
1064 memset( ssl->in_buf, 0, in_buf_len );
1065 }
1066
1067 /* Reset outgoing message writing */
1068 ssl->out_msgtype = 0;
1069 ssl->out_msglen = 0;
1070 ssl->out_left = 0;
1071 memset( ssl->out_buf, 0, out_buf_len );
1072 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1073 ssl->transform_out = NULL;
1074
1075#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1076 mbedtls_ssl_dtls_replay_reset( ssl );
1077#endif
1078
XiaokangQian2b01dc32022-01-21 02:53:13 +00001079#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001080 if( ssl->transform )
1081 {
1082 mbedtls_ssl_transform_free( ssl->transform );
1083 mbedtls_free( ssl->transform );
1084 ssl->transform = NULL;
1085 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001086#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1087
XiaokangQian2b01dc32022-01-21 02:53:13 +00001088#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1089 mbedtls_ssl_transform_free( ssl->transform_application );
1090 mbedtls_free( ssl->transform_application );
1091 ssl->transform_application = NULL;
1092
1093 if( ssl->handshake != NULL )
1094 {
1095 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1096 mbedtls_free( ssl->handshake->transform_earlydata );
1097 ssl->handshake->transform_earlydata = NULL;
1098
1099 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1100 mbedtls_free( ssl->handshake->transform_handshake );
1101 ssl->handshake->transform_handshake = NULL;
1102 }
1103
XiaokangQian2b01dc32022-01-21 02:53:13 +00001104#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001105}
1106
1107int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1108{
1109 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1110
1111 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1112
XiaokangQian78b1fa72022-01-19 06:56:30 +00001113 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001114
1115 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116#if defined(MBEDTLS_SSL_RENEGOTIATION)
1117 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001118 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001119
1120 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1122 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001123#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001125
Hanno Beckerb0302c42021-08-03 09:39:42 +01001126 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001127 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001128 if( ssl->session )
1129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 mbedtls_ssl_session_free( ssl->session );
1131 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001132 ssl->session = NULL;
1133 }
1134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001136 ssl->alpn_chosen = NULL;
1137#endif
1138
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001139#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01001140#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001141 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01001142#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001143 {
1144 mbedtls_free( ssl->cli_id );
1145 ssl->cli_id = NULL;
1146 ssl->cli_id_len = 0;
1147 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001148#endif
1149
Paul Bakker48916f92012-09-16 19:57:18 +00001150 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1151 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001152
1153 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001154}
1155
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001156/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001157 * Reset an initialized and used SSL context for re-use while retaining
1158 * all application-set variables, function pointers and data.
1159 */
1160int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1161{
Hanno Becker43aefe22020-02-05 10:44:56 +00001162 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001163}
1164
1165/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001166 * SSL set accessors
1167 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001168void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001169{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001170 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001171}
1172
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001173void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001174{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001175 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001176}
1177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001179void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001180{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001181 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001182}
1183#endif
1184
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001185void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001186{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001187 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001188}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001191
Hanno Becker1841b0a2018-08-24 11:13:57 +01001192void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1193 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001194{
1195 ssl->disable_datagram_packing = !allow_packing;
1196}
1197
1198void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1199 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001200{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001201 conf->hs_timeout_min = min;
1202 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001203}
1204#endif
1205
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001206void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001207{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001208 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001209}
1210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001212void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001213 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001214 void *p_vrfy )
1215{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001216 conf->f_vrfy = f_vrfy;
1217 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001218}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001220
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001221void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001222 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001223 void *p_rng )
1224{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001225 conf->f_rng = f_rng;
1226 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001227}
1228
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001229void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001230 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001231 void *p_dbg )
1232{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001233 conf->f_dbg = f_dbg;
1234 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001235}
1236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001238 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001239 mbedtls_ssl_send_t *f_send,
1240 mbedtls_ssl_recv_t *f_recv,
1241 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001242{
1243 ssl->p_bio = p_bio;
1244 ssl->f_send = f_send;
1245 ssl->f_recv = f_recv;
1246 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001247}
1248
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001249#if defined(MBEDTLS_SSL_PROTO_DTLS)
1250void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1251{
1252 ssl->mtu = mtu;
1253}
1254#endif
1255
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001256void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001257{
1258 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001259}
1260
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001261void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1262 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001263 mbedtls_ssl_set_timer_t *f_set_timer,
1264 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001265{
1266 ssl->p_timer = p_timer;
1267 ssl->f_set_timer = f_set_timer;
1268 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001269
1270 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001271 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001272}
1273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274#if defined(MBEDTLS_SSL_SRV_C)
Glenn Strauss2ed95272022-01-21 18:02:17 -05001275void mbedtls_ssl_conf_cert_cb( mbedtls_ssl_config *conf,
1276 int (*f_cert_cb)(mbedtls_ssl_context *) )
1277{
1278 conf->f_cert_cb = f_cert_cb;
1279}
1280#endif /* MBEDTLS_SSL_SRV_C */
1281
1282#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001283void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001284 void *p_cache,
1285 mbedtls_ssl_cache_get_t *f_get_cache,
1286 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001287{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001288 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001289 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001290 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001291}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294#if defined(MBEDTLS_SSL_CLI_C)
1295int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001296{
Janos Follath865b3eb2019-12-16 11:46:15 +00001297 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001298
1299 if( ssl == NULL ||
1300 session == NULL ||
1301 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001302 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001305 }
1306
Hanno Beckere810bbc2021-05-14 16:01:05 +01001307 if( ssl->handshake->resume == 1 )
1308 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1309
Hanno Becker52055ae2019-02-06 14:30:46 +00001310 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1311 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001312 return( ret );
1313
Paul Bakker0a597072012-09-25 21:55:46 +00001314 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001315
1316 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001317}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001319
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001320void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1321 const int *ciphersuites )
1322{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001323 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001324}
1325
Ronald Cron6f135e12021-12-08 16:57:54 +01001326#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001327void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001328 const int kex_modes )
1329{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001330 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001331}
Ronald Cron6f135e12021-12-08 16:57:54 +01001332#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001335void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001336 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001337{
1338 conf->cert_profile = profile;
1339}
1340
Glenn Strauss36872db2022-01-22 05:06:31 -05001341static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1342{
1343 mbedtls_ssl_key_cert *cur = key_cert, *next;
1344
1345 while( cur != NULL )
1346 {
1347 next = cur->next;
1348 mbedtls_free( cur );
1349 cur = next;
1350 }
1351}
1352
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001353/* Append a new keycert entry to a (possibly empty) list */
1354static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1355 mbedtls_x509_crt *cert,
1356 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001357{
niisato8ee24222018-06-25 19:05:48 +09001358 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001359
Glenn Strauss36872db2022-01-22 05:06:31 -05001360 if( cert == NULL )
1361 {
1362 /* Free list if cert is null */
1363 ssl_key_cert_free( *head );
1364 *head = NULL;
1365 return( 0 );
1366 }
1367
niisato8ee24222018-06-25 19:05:48 +09001368 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1369 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001370 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001371
niisato8ee24222018-06-25 19:05:48 +09001372 new_cert->cert = cert;
1373 new_cert->key = key;
1374 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001375
Glenn Strauss36872db2022-01-22 05:06:31 -05001376 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001377 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001378 {
niisato8ee24222018-06-25 19:05:48 +09001379 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001380 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001381 else
1382 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001383 mbedtls_ssl_key_cert *cur = *head;
1384 while( cur->next != NULL )
1385 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001386 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001387 }
1388
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001389 return( 0 );
1390}
1391
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001392int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001393 mbedtls_x509_crt *own_cert,
1394 mbedtls_pk_context *pk_key )
1395{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001396 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001397}
1398
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001399void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001400 mbedtls_x509_crt *ca_chain,
1401 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001402{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001403 conf->ca_chain = ca_chain;
1404 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001405
1406#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1407 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1408 * cannot be used together. */
1409 conf->f_ca_cb = NULL;
1410 conf->p_ca_cb = NULL;
1411#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001412}
Hanno Becker5adaad92019-03-27 16:54:37 +00001413
1414#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1415void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001416 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001417 void *p_ca_cb )
1418{
1419 conf->f_ca_cb = f_ca_cb;
1420 conf->p_ca_cb = p_ca_cb;
1421
1422 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1423 * cannot be used together. */
1424 conf->ca_chain = NULL;
1425 conf->ca_crl = NULL;
1426}
1427#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001429
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001430#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001431const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1432 size_t *name_len )
1433{
1434 *name_len = ssl->handshake->sni_name_len;
1435 return( ssl->handshake->sni_name );
1436}
1437
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001438int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1439 mbedtls_x509_crt *own_cert,
1440 mbedtls_pk_context *pk_key )
1441{
1442 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1443 own_cert, pk_key ) );
1444}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001445
1446void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1447 mbedtls_x509_crt *ca_chain,
1448 mbedtls_x509_crl *ca_crl )
1449{
1450 ssl->handshake->sni_ca_chain = ca_chain;
1451 ssl->handshake->sni_ca_crl = ca_crl;
1452}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001453
1454void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1455 int authmode )
1456{
1457 ssl->handshake->sni_authmode = authmode;
1458}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001459#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1460
Hanno Becker8927c832019-04-03 12:52:50 +01001461#if defined(MBEDTLS_X509_CRT_PARSE_C)
1462void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1463 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1464 void *p_vrfy )
1465{
1466 ssl->f_vrfy = f_vrfy;
1467 ssl->p_vrfy = p_vrfy;
1468}
1469#endif
1470
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001471#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001472/*
1473 * Set EC J-PAKE password for current handshake
1474 */
1475int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1476 const unsigned char *pw,
1477 size_t pw_len )
1478{
1479 mbedtls_ecjpake_role role;
1480
Janos Follath8eb64132016-06-03 15:40:57 +01001481 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001482 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1483
1484 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1485 role = MBEDTLS_ECJPAKE_SERVER;
1486 else
1487 role = MBEDTLS_ECJPAKE_CLIENT;
1488
1489 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1490 role,
1491 MBEDTLS_MD_SHA256,
1492 MBEDTLS_ECP_DP_SECP256R1,
1493 pw, pw_len ) );
1494}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001495#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001496
Gilles Peskineeccd8882020-03-10 12:19:08 +01001497#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001498
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001499static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1500{
1501#if defined(MBEDTLS_USE_PSA_CRYPTO)
1502 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1503 return( 1 );
1504#endif /* MBEDTLS_USE_PSA_CRYPTO */
1505
1506 if( conf->psk != NULL )
1507 return( 1 );
1508
1509 return( 0 );
1510}
1511
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001512static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1513{
1514 /* Remove reference to existing PSK, if any. */
1515#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001516 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001517 {
1518 /* The maintenance of the PSK key slot is the
1519 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001520 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001521 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00001522 /* This and the following branch should never
1523 * be taken simultaenously as we maintain the
1524 * invariant that raw and opaque PSKs are never
1525 * configured simultaneously. As a safeguard,
1526 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001527#endif /* MBEDTLS_USE_PSA_CRYPTO */
1528 if( conf->psk != NULL )
1529 {
1530 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1531
1532 mbedtls_free( conf->psk );
1533 conf->psk = NULL;
1534 conf->psk_len = 0;
1535 }
1536
1537 /* Remove reference to PSK identity, if any. */
1538 if( conf->psk_identity != NULL )
1539 {
1540 mbedtls_free( conf->psk_identity );
1541 conf->psk_identity = NULL;
1542 conf->psk_identity_len = 0;
1543 }
1544}
1545
Hanno Becker7390c712018-11-15 13:33:04 +00001546/* This function assumes that PSK identity in the SSL config is unset.
1547 * It checks that the provided identity is well-formed and attempts
1548 * to make a copy of it in the SSL config.
1549 * On failure, the PSK identity in the config remains unset. */
1550static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1551 unsigned char const *psk_identity,
1552 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001553{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001554 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001555 if( psk_identity == NULL ||
1556 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001557 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001558 {
1559 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1560 }
1561
Hanno Becker7390c712018-11-15 13:33:04 +00001562 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1563 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001564 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001565
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001566 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001567 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001568
1569 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001570}
1571
Hanno Becker7390c712018-11-15 13:33:04 +00001572int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1573 const unsigned char *psk, size_t psk_len,
1574 const unsigned char *psk_identity, size_t psk_identity_len )
1575{
Janos Follath865b3eb2019-12-16 11:46:15 +00001576 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001577
1578 /* We currently only support one PSK, raw or opaque. */
1579 if( ssl_conf_psk_is_configured( conf ) )
1580 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001581
1582 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001583 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001584 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001585 if( psk_len == 0 )
1586 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1587 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1588 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1589
Hanno Becker7390c712018-11-15 13:33:04 +00001590 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1591 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1592 conf->psk_len = psk_len;
1593 memcpy( conf->psk, psk, conf->psk_len );
1594
1595 /* Check and set PSK Identity */
1596 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1597 if( ret != 0 )
1598 ssl_conf_remove_psk( conf );
1599
1600 return( ret );
1601}
1602
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001603static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1604{
1605#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001606 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001607 {
Ronald Croncf56a0a2020-08-04 09:51:30 +02001608 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001609 }
1610 else
1611#endif /* MBEDTLS_USE_PSA_CRYPTO */
1612 if( ssl->handshake->psk != NULL )
1613 {
1614 mbedtls_platform_zeroize( ssl->handshake->psk,
1615 ssl->handshake->psk_len );
1616 mbedtls_free( ssl->handshake->psk );
1617 ssl->handshake->psk_len = 0;
1618 }
1619}
1620
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001621int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1622 const unsigned char *psk, size_t psk_len )
1623{
1624 if( psk == NULL || ssl->handshake == NULL )
1625 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1626
1627 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1628 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1629
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001630 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001631
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001632 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001633 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001634
1635 ssl->handshake->psk_len = psk_len;
1636 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1637
1638 return( 0 );
1639}
1640
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001641#if defined(MBEDTLS_USE_PSA_CRYPTO)
1642int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001643 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001644 const unsigned char *psk_identity,
1645 size_t psk_identity_len )
1646{
Janos Follath865b3eb2019-12-16 11:46:15 +00001647 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001648
1649 /* We currently only support one PSK, raw or opaque. */
1650 if( ssl_conf_psk_is_configured( conf ) )
1651 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001652
Hanno Becker7390c712018-11-15 13:33:04 +00001653 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001654 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001656 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001657
1658 /* Check and set PSK Identity */
1659 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1660 psk_identity_len );
1661 if( ret != 0 )
1662 ssl_conf_remove_psk( conf );
1663
1664 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001665}
1666
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001667int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1668 mbedtls_svc_key_id_t psk )
1669{
1670 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1671 ( ssl->handshake == NULL ) )
1672 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1673
1674 ssl_remove_psk( ssl );
1675 ssl->handshake->psk_opaque = psk;
1676 return( 0 );
1677}
1678#endif /* MBEDTLS_USE_PSA_CRYPTO */
1679
1680void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1681 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1682 size_t),
1683 void *p_psk )
1684{
1685 conf->f_psk = f_psk;
1686 conf->p_psk = p_psk;
1687}
1688#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1689
1690#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01001691psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01001692 size_t taglen,
1693 psa_algorithm_t *alg,
1694 psa_key_type_t *key_type,
1695 size_t *key_size )
1696{
1697 switch ( mbedtls_cipher_type )
1698 {
1699 case MBEDTLS_CIPHER_AES_128_CBC:
1700 *alg = PSA_ALG_CBC_NO_PADDING;
1701 *key_type = PSA_KEY_TYPE_AES;
1702 *key_size = 128;
1703 break;
1704 case MBEDTLS_CIPHER_AES_128_CCM:
1705 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1706 *key_type = PSA_KEY_TYPE_AES;
1707 *key_size = 128;
1708 break;
1709 case MBEDTLS_CIPHER_AES_128_GCM:
1710 *alg = PSA_ALG_GCM;
1711 *key_type = PSA_KEY_TYPE_AES;
1712 *key_size = 128;
1713 break;
1714 case MBEDTLS_CIPHER_AES_192_CCM:
1715 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1716 *key_type = PSA_KEY_TYPE_AES;
1717 *key_size = 192;
1718 break;
1719 case MBEDTLS_CIPHER_AES_192_GCM:
1720 *alg = PSA_ALG_GCM;
1721 *key_type = PSA_KEY_TYPE_AES;
1722 *key_size = 192;
1723 break;
1724 case MBEDTLS_CIPHER_AES_256_CBC:
1725 *alg = PSA_ALG_CBC_NO_PADDING;
1726 *key_type = PSA_KEY_TYPE_AES;
1727 *key_size = 256;
1728 break;
1729 case MBEDTLS_CIPHER_AES_256_CCM:
1730 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1731 *key_type = PSA_KEY_TYPE_AES;
1732 *key_size = 256;
1733 break;
1734 case MBEDTLS_CIPHER_AES_256_GCM:
1735 *alg = PSA_ALG_GCM;
1736 *key_type = PSA_KEY_TYPE_AES;
1737 *key_size = 256;
1738 break;
1739 case MBEDTLS_CIPHER_ARIA_128_CBC:
1740 *alg = PSA_ALG_CBC_NO_PADDING;
1741 *key_type = PSA_KEY_TYPE_ARIA;
1742 *key_size = 128;
1743 break;
1744 case MBEDTLS_CIPHER_ARIA_128_CCM:
1745 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1746 *key_type = PSA_KEY_TYPE_ARIA;
1747 *key_size = 128;
1748 break;
1749 case MBEDTLS_CIPHER_ARIA_128_GCM:
1750 *alg = PSA_ALG_GCM;
1751 *key_type = PSA_KEY_TYPE_ARIA;
1752 *key_size = 128;
1753 break;
1754 case MBEDTLS_CIPHER_ARIA_192_CCM:
1755 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1756 *key_type = PSA_KEY_TYPE_ARIA;
1757 *key_size = 192;
1758 break;
1759 case MBEDTLS_CIPHER_ARIA_192_GCM:
1760 *alg = PSA_ALG_GCM;
1761 *key_type = PSA_KEY_TYPE_ARIA;
1762 *key_size = 192;
1763 break;
1764 case MBEDTLS_CIPHER_ARIA_256_CBC:
1765 *alg = PSA_ALG_CBC_NO_PADDING;
1766 *key_type = PSA_KEY_TYPE_ARIA;
1767 *key_size = 256;
1768 break;
1769 case MBEDTLS_CIPHER_ARIA_256_CCM:
1770 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1771 *key_type = PSA_KEY_TYPE_ARIA;
1772 *key_size = 256;
1773 break;
1774 case MBEDTLS_CIPHER_ARIA_256_GCM:
1775 *alg = PSA_ALG_GCM;
1776 *key_type = PSA_KEY_TYPE_ARIA;
1777 *key_size = 256;
1778 break;
1779 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
1780 *alg = PSA_ALG_CBC_NO_PADDING;
1781 *key_type = PSA_KEY_TYPE_CAMELLIA;
1782 *key_size = 128;
1783 break;
1784 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
1785 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1786 *key_type = PSA_KEY_TYPE_CAMELLIA;
1787 *key_size = 128;
1788 break;
1789 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
1790 *alg = PSA_ALG_GCM;
1791 *key_type = PSA_KEY_TYPE_CAMELLIA;
1792 *key_size = 128;
1793 break;
1794 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
1795 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1796 *key_type = PSA_KEY_TYPE_CAMELLIA;
1797 *key_size = 192;
1798 break;
1799 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
1800 *alg = PSA_ALG_GCM;
1801 *key_type = PSA_KEY_TYPE_CAMELLIA;
1802 *key_size = 192;
1803 break;
1804 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
1805 *alg = PSA_ALG_CBC_NO_PADDING;
1806 *key_type = PSA_KEY_TYPE_CAMELLIA;
1807 *key_size = 256;
1808 break;
1809 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
1810 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1811 *key_type = PSA_KEY_TYPE_CAMELLIA;
1812 *key_size = 256;
1813 break;
1814 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
1815 *alg = PSA_ALG_GCM;
1816 *key_type = PSA_KEY_TYPE_CAMELLIA;
1817 *key_size = 256;
1818 break;
1819 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
1820 *alg = PSA_ALG_CHACHA20_POLY1305;
1821 *key_type = PSA_KEY_TYPE_CHACHA20;
1822 *key_size = 256;
1823 break;
1824 case MBEDTLS_CIPHER_NULL:
1825 *alg = MBEDTLS_SSL_NULL_CIPHER;
1826 *key_type = 0;
1827 *key_size = 0;
1828 break;
1829 default:
1830 return PSA_ERROR_NOT_SUPPORTED;
1831 }
1832
1833 return PSA_SUCCESS;
1834}
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001835#endif /* MBEDTLS_USE_PSA_CRYPTO */
1836
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02001837#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01001838int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
1839 const unsigned char *dhm_P, size_t P_len,
1840 const unsigned char *dhm_G, size_t G_len )
1841{
Janos Follath865b3eb2019-12-16 11:46:15 +00001842 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01001843
Glenn Strausscee11292021-12-20 01:43:17 -05001844 mbedtls_mpi_free( &conf->dhm_P );
1845 mbedtls_mpi_free( &conf->dhm_G );
1846
Hanno Beckera90658f2017-10-04 15:29:08 +01001847 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
1848 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
1849 {
1850 mbedtls_mpi_free( &conf->dhm_P );
1851 mbedtls_mpi_free( &conf->dhm_G );
1852 return( ret );
1853 }
1854
1855 return( 0 );
1856}
Paul Bakker5121ce52009-01-03 21:22:43 +00001857
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001858int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00001859{
Janos Follath865b3eb2019-12-16 11:46:15 +00001860 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00001861
Glenn Strausscee11292021-12-20 01:43:17 -05001862 mbedtls_mpi_free( &conf->dhm_P );
1863 mbedtls_mpi_free( &conf->dhm_G );
1864
Gilles Peskinee5702482021-06-11 21:59:08 +02001865 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
1866 &conf->dhm_P ) ) != 0 ||
1867 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
1868 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01001869 {
1870 mbedtls_mpi_free( &conf->dhm_P );
1871 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00001872 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01001873 }
Paul Bakker1b57b062011-01-06 15:48:19 +00001874
1875 return( 0 );
1876}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02001877#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00001878
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02001879#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
1880/*
1881 * Set the minimum length for Diffie-Hellman parameters
1882 */
1883void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
1884 unsigned int bitlen )
1885{
1886 conf->dhm_min_bitlen = bitlen;
1887}
1888#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
1889
Gilles Peskineeccd8882020-03-10 12:19:08 +01001890#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08001891#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02001892/*
1893 * Set allowed/preferred hashes for handshake signatures
1894 */
1895void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
1896 const int *hashes )
1897{
1898 conf->sig_hashes = hashes;
1899}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08001900#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01001901
Jerry Yuf017ee42022-01-12 15:49:48 +08001902/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01001903void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
1904 const uint16_t* sig_algs )
1905{
Jerry Yuf017ee42022-01-12 15:49:48 +08001906#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1907 conf->sig_hashes = NULL;
1908#endif /* !MBEDTLS_DEPRECATED_REMOVED */
1909 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01001910}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001911#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02001912
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02001913#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01001914#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001915/*
1916 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01001917 *
1918 * mbedtls_ssl_setup() takes the provided list
1919 * and translates it to a list of IANA TLS group identifiers,
1920 * stored in ssl->handshake->group_list.
1921 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001922 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001923void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001924 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001925{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001926 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01001927 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001928}
Brett Warrene0edc842021-08-17 09:53:13 +01001929#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01001930#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001931
Brett Warrene0edc842021-08-17 09:53:13 +01001932/*
1933 * Set the allowed groups
1934 */
1935void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
1936 const uint16_t *group_list )
1937{
1938#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
1939 conf->curve_list = NULL;
1940#endif
1941 conf->group_list = group_list;
1942}
1943
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001944#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001945int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00001946{
Hanno Becker947194e2017-04-07 13:25:49 +01001947 /* Initialize to suppress unnecessary compiler warning */
1948 size_t hostname_len = 0;
1949
1950 /* Check if new hostname is valid before
1951 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01001952 if( hostname != NULL )
1953 {
1954 hostname_len = strlen( hostname );
1955
1956 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
1957 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1958 }
1959
1960 /* Now it's clear that we will overwrite the old hostname,
1961 * so we can free it safely */
1962
1963 if( ssl->hostname != NULL )
1964 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001965 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01001966 mbedtls_free( ssl->hostname );
1967 }
1968
1969 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01001970
Paul Bakker5121ce52009-01-03 21:22:43 +00001971 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01001972 {
1973 ssl->hostname = NULL;
1974 }
1975 else
1976 {
1977 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01001978 if( ssl->hostname == NULL )
1979 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02001980
Hanno Becker947194e2017-04-07 13:25:49 +01001981 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02001982
Hanno Becker947194e2017-04-07 13:25:49 +01001983 ssl->hostname[hostname_len] = '\0';
1984 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001985
1986 return( 0 );
1987}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01001988#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001989
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001990#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001991void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00001993 const unsigned char *, size_t),
1994 void *p_sni )
1995{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001996 conf->f_sni = f_sni;
1997 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00001998}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002002int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002003{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002004 size_t cur_len, tot_len;
2005 const char **p;
2006
2007 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002008 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2009 * MUST NOT be truncated."
2010 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002011 */
2012 tot_len = 0;
2013 for( p = protos; *p != NULL; p++ )
2014 {
2015 cur_len = strlen( *p );
2016 tot_len += cur_len;
2017
Ronald Cron8216dd32020-04-23 16:41:44 +02002018 if( ( cur_len == 0 ) ||
2019 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2020 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002022 }
2023
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002024 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002025
2026 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002027}
2028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002029const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002030{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002031 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002032}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002034
Johan Pascalb62bb512015-12-03 21:56:45 +01002035#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002036void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2037 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002038{
2039 conf->dtls_srtp_mki_support = support_mki_value;
2040}
2041
Ron Eldoref72faf2018-07-12 11:54:20 +03002042int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2043 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002044 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002045{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002046 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002047 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002048 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002049 }
Ron Eldor591f1622018-01-22 12:30:04 +02002050
2051 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002052 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002053 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002054 }
Ron Eldor591f1622018-01-22 12:30:04 +02002055
2056 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2057 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002058 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002059}
2060
Ron Eldoref72faf2018-07-12 11:54:20 +03002061int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002062 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002063{
Johan Pascal253d0262020-09-22 13:04:45 +02002064 const mbedtls_ssl_srtp_profile *p;
2065 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002066
Johan Pascal253d0262020-09-22 13:04:45 +02002067 /* check the profiles list: all entry must be valid,
2068 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002069 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2070 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2071 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002072 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002073 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002074 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002075 list_size++;
2076 }
2077 else
2078 {
2079 /* unsupported value, stop parsing and set the size to an error value */
2080 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002081 }
2082 }
2083
Johan Pascal5ef72d22020-10-28 17:05:47 +01002084 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002085 {
Johan Pascal253d0262020-09-22 13:04:45 +02002086 conf->dtls_srtp_profile_list = NULL;
2087 conf->dtls_srtp_profile_list_len = 0;
2088 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2089 }
2090
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002091 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002092 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002093
2094 return( 0 );
2095}
2096
Johan Pascal5ef72d22020-10-28 17:05:47 +01002097void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2098 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002099{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002100 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2101 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002102 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002103 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002104 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002105 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002106 else
2107 {
2108 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002109 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2110 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002111 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002112}
Johan Pascalb62bb512015-12-03 21:56:45 +01002113#endif /* MBEDTLS_SSL_DTLS_SRTP */
2114
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002115void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002116{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002117 conf->max_major_ver = major;
2118 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002119}
2120
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002121void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002122{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002123 conf->min_major_ver = major;
2124 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002125}
2126
Janos Follath088ce432017-04-10 12:42:31 +01002127#if defined(MBEDTLS_SSL_SRV_C)
2128void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2129 char cert_req_ca_list )
2130{
2131 conf->cert_req_ca_list = cert_req_ca_list;
2132}
2133#endif
2134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002136void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002137{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002138 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002139}
2140#endif
2141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002143void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002144{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002145 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002146}
2147#endif
2148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002150int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002151{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002153 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002156 }
2157
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002158 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002159
2160 return( 0 );
2161}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002163
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002164void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002165{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002166 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002167}
2168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002170void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002171{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002172 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002173}
2174
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002175void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002176{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002177 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002178}
2179
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002180void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002181 const unsigned char period[8] )
2182{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002183 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002184}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002188#if defined(MBEDTLS_SSL_CLI_C)
2189void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002190{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002191 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002192}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002193#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002194
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002195#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002196void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2197 mbedtls_ssl_ticket_write_t *f_ticket_write,
2198 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2199 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002200{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002201 conf->f_ticket_write = f_ticket_write;
2202 conf->f_ticket_parse = f_ticket_parse;
2203 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002204}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002205#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002207
Hanno Becker7e6c1782021-06-08 09:24:55 +01002208void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2209 mbedtls_ssl_export_keys_t *f_export_keys,
2210 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002211{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002212 ssl->f_export_keys = f_export_keys;
2213 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002214}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002215
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002216#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002217void mbedtls_ssl_conf_async_private_cb(
2218 mbedtls_ssl_config *conf,
2219 mbedtls_ssl_async_sign_t *f_async_sign,
2220 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2221 mbedtls_ssl_async_resume_t *f_async_resume,
2222 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002223 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002224{
2225 conf->f_async_sign_start = f_async_sign;
2226 conf->f_async_decrypt_start = f_async_decrypt;
2227 conf->f_async_resume = f_async_resume;
2228 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002229 conf->p_async_config_data = async_config_data;
2230}
2231
Gilles Peskine8f97af72018-04-26 11:46:10 +02002232void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2233{
2234 return( conf->p_async_config_data );
2235}
2236
Gilles Peskine1febfef2018-04-30 11:54:39 +02002237void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002238{
2239 if( ssl->handshake == NULL )
2240 return( NULL );
2241 else
2242 return( ssl->handshake->user_async_ctx );
2243}
2244
Gilles Peskine1febfef2018-04-30 11:54:39 +02002245void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002246 void *ctx )
2247{
2248 if( ssl->handshake != NULL )
2249 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002250}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002251#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002252
Paul Bakker5121ce52009-01-03 21:22:43 +00002253/*
2254 * SSL get accessors
2255 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002256uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002257{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002258 if( ssl->session != NULL )
2259 return( ssl->session->verify_result );
2260
2261 if( ssl->session_negotiate != NULL )
2262 return( ssl->session_negotiate->verify_result );
2263
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002264 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002265}
2266
Glenn Strauss8f526902022-01-13 00:04:49 -05002267int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2268{
2269 if( ssl == NULL || ssl->session == NULL )
2270 return( 0 );
2271
2272 return( ssl->session->ciphersuite );
2273}
2274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002276{
Paul Bakker926c8e42013-03-06 10:23:34 +01002277 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002278 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002281}
2282
Gilles Peskinee1a0c252022-01-13 01:08:03 +01002283mbedtls_ssl_protocol_version mbedtls_ssl_get_version_number(
2284 const mbedtls_ssl_context *ssl )
2285{
2286 /* For major_ver, only 3 is supported, so skip checking it. */
2287 switch( ssl->minor_ver )
2288 {
2289 case MBEDTLS_SSL_MINOR_VERSION_3:
2290 return( MBEDTLS_SSL_VERSION_1_2 );
2291 case MBEDTLS_SSL_MINOR_VERSION_4:
2292 return( MBEDTLS_SSL_VERSION_1_3 );
2293 default:
2294 return( MBEDTLS_SSL_VERSION_UNKNOWN );
2295 }
2296}
2297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002299{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002301 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002302 {
2303 switch( ssl->minor_ver )
2304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002306 return( "DTLSv1.2" );
2307
2308 default:
2309 return( "unknown (DTLS)" );
2310 }
2311 }
2312#endif
2313
Paul Bakker43ca69c2011-01-15 17:35:19 +00002314 switch( ssl->minor_ver )
2315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002317 return( "TLSv1.2" );
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002318 case MBEDTLS_SSL_MINOR_VERSION_4:
2319 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002320 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002321 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002322 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002323}
2324
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002325#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002326size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2327{
David Horstmann95d516f2021-05-04 18:36:56 +01002328 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002329 size_t read_mfl;
2330
2331 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2332 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2333 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2334 {
2335 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2336 }
2337
2338 /* Check if a smaller max length was negotiated */
2339 if( ssl->session_out != NULL )
2340 {
2341 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2342 if( read_mfl < max_len )
2343 {
2344 max_len = read_mfl;
2345 }
2346 }
2347
2348 // During a handshake, use the value being negotiated
2349 if( ssl->session_negotiate != NULL )
2350 {
2351 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2352 if( read_mfl < max_len )
2353 {
2354 max_len = read_mfl;
2355 }
2356 }
2357
2358 return( max_len );
2359}
2360
2361size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002362{
2363 size_t max_len;
2364
2365 /*
2366 * Assume mfl_code is correct since it was checked when set
2367 */
Angus Grattond8213d02016-05-25 20:56:48 +10002368 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002369
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002370 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002371 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002372 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002373 {
Angus Grattond8213d02016-05-25 20:56:48 +10002374 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002375 }
2376
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002377 /* During a handshake, use the value being negotiated */
2378 if( ssl->session_negotiate != NULL &&
2379 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2380 {
2381 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2382 }
2383
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002384 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002385}
2386#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2387
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002388#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002389size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002390{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002391 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2392 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2393 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2394 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2395 return ( 0 );
2396
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002397 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2398 return( ssl->mtu );
2399
2400 if( ssl->mtu == 0 )
2401 return( ssl->handshake->mtu );
2402
2403 return( ssl->mtu < ssl->handshake->mtu ?
2404 ssl->mtu : ssl->handshake->mtu );
2405}
2406#endif /* MBEDTLS_SSL_PROTO_DTLS */
2407
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002408int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2409{
2410 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2411
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002412#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2413 !defined(MBEDTLS_SSL_PROTO_DTLS)
2414 (void) ssl;
2415#endif
2416
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002417#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002418 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002419
2420 if( max_len > mfl )
2421 max_len = mfl;
2422#endif
2423
2424#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002425 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002426 {
Hanno Becker89490712020-02-05 10:50:12 +00002427 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002428 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2429 const size_t overhead = (size_t) ret;
2430
2431 if( ret < 0 )
2432 return( ret );
2433
2434 if( mtu <= overhead )
2435 {
2436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2437 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2438 }
2439
2440 if( max_len > mtu - overhead )
2441 max_len = mtu - overhead;
2442 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002443#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002444
Hanno Becker0defedb2018-08-10 12:35:02 +01002445#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2446 !defined(MBEDTLS_SSL_PROTO_DTLS)
2447 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002448#endif
2449
2450 return( (int) max_len );
2451}
2452
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002453int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2454{
2455 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2456
2457#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2458 (void) ssl;
2459#endif
2460
2461#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2462 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2463
2464 if( max_len > mfl )
2465 max_len = mfl;
2466#endif
2467
2468 return( (int) max_len );
2469}
2470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471#if defined(MBEDTLS_X509_CRT_PARSE_C)
2472const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002473{
2474 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002475 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002476
Hanno Beckere6824572019-02-07 13:18:46 +00002477#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002478 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002479#else
2480 return( NULL );
2481#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002482}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002486int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2487 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002488{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002489 int ret;
2490
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002491 if( ssl == NULL ||
2492 dst == NULL ||
2493 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002494 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002497 }
2498
Hanno Beckere810bbc2021-05-14 16:01:05 +01002499 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2500 * idempotent: Each session can only be exported once.
2501 *
2502 * (This is in preparation for TLS 1.3 support where we will
2503 * need the ability to export multiple sessions (aka tickets),
2504 * which will be achieved by calling mbedtls_ssl_get_session()
2505 * multiple times until it fails.)
2506 *
2507 * Check whether we have already exported the current session,
2508 * and fail if so.
2509 */
2510 if( ssl->session->exported == 1 )
2511 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2512
2513 ret = mbedtls_ssl_session_copy( dst, ssl->session );
2514 if( ret != 0 )
2515 return( ret );
2516
2517 /* Remember that we've exported the session. */
2518 ssl->session->exported = 1;
2519 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002520}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002522
Paul Bakker5121ce52009-01-03 21:22:43 +00002523/*
Hanno Beckera835da52019-05-16 12:39:07 +01002524 * Define ticket header determining Mbed TLS version
2525 * and structure of the ticket.
2526 */
2527
Hanno Becker94ef3b32019-05-16 12:50:45 +01002528/*
Hanno Becker50b59662019-05-28 14:30:45 +01002529 * Define bitflag determining compile-time settings influencing
2530 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01002531 */
2532
Hanno Becker50b59662019-05-28 14:30:45 +01002533#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01002534#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01002535#else
Hanno Becker3e088662019-05-29 11:10:18 +01002536#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002537#endif /* MBEDTLS_HAVE_TIME */
2538
2539#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01002540#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002541#else
Hanno Becker3e088662019-05-29 11:10:18 +01002542#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002543#endif /* MBEDTLS_X509_CRT_PARSE_C */
2544
2545#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01002546#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002547#else
Hanno Becker3e088662019-05-29 11:10:18 +01002548#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002549#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
2550
2551#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01002552#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002553#else
Hanno Becker3e088662019-05-29 11:10:18 +01002554#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002555#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2556
Hanno Becker94ef3b32019-05-16 12:50:45 +01002557#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01002558#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002559#else
Hanno Becker3e088662019-05-29 11:10:18 +01002560#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002561#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2562
Hanno Becker94ef3b32019-05-16 12:50:45 +01002563#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2564#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
2565#else
2566#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
2567#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2568
Hanno Becker3e088662019-05-29 11:10:18 +01002569#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
2570#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
2571#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
2572#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01002573#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
2574#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01002575
Hanno Becker50b59662019-05-28 14:30:45 +01002576#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01002577 ( (uint16_t) ( \
2578 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
2579 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
2580 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
2581 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01002582 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01002583 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01002584
Hanno Beckerf8787072019-05-16 12:41:07 +01002585static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01002586 MBEDTLS_VERSION_MAJOR,
2587 MBEDTLS_VERSION_MINOR,
2588 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01002589 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
2590 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01002591};
Hanno Beckera835da52019-05-16 12:39:07 +01002592
2593/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002594 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002595 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002596 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002597 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002598 *
Hanno Beckerdce50972021-08-01 05:39:23 +01002599 * opaque mbedtls_version[3]; // library version: major, minor, patch
2600 * opaque session_format[2]; // library-version specific 16-bit field
2601 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002602 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002603 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002604 * Note: When updating the format, remember to keep
2605 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002606 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002607 * // In this version, `session_format` determines
2608 * // the setting of those compile-time
2609 * // configuration options which influence
2610 * // the structure of mbedtls_ssl_session.
2611 *
Hanno Beckerfa0d61e2021-08-02 08:56:14 +01002612 * uint8_t minor_ver; // Protocol-version. Possible values:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002613 * // - TLS 1.2 (MBEDTLS_SSL_MINOR_VERSION_3)
2614 *
2615 * select (serialized_session.minor_ver) {
2616 *
2617 * case MBEDTLS_SSL_MINOR_VERSION_3: // TLS 1.2
2618 * serialized_session_tls12 data;
2619 *
2620 * };
2621 *
2622 * } serialized_session;
2623 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002624 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002625
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002626static int ssl_session_save( const mbedtls_ssl_session *session,
2627 unsigned char omit_header,
2628 unsigned char *buf,
2629 size_t buf_len,
2630 size_t *olen )
2631{
2632 unsigned char *p = buf;
2633 size_t used = 0;
2634
2635 if( !omit_header )
2636 {
2637 /*
2638 * Add Mbed TLS version identifier
2639 */
2640
2641 used += sizeof( ssl_serialized_session_header );
2642
2643 if( used <= buf_len )
2644 {
2645 memcpy( p, ssl_serialized_session_header,
2646 sizeof( ssl_serialized_session_header ) );
2647 p += sizeof( ssl_serialized_session_header );
2648 }
2649 }
2650
2651 /*
2652 * TLS version identifier
2653 */
2654 used += 1;
2655 if( used <= buf_len )
2656 {
2657 *p++ = session->minor_ver;
2658 }
2659
2660 /* Forward to version-specific serialization routine. */
2661 switch( session->minor_ver )
2662 {
2663#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2664 case MBEDTLS_SSL_MINOR_VERSION_3:
2665 {
2666 size_t remaining_len = used <= buf_len ? buf_len - used : 0;
2667 used += ssl_session_save_tls12( session, p, remaining_len );
2668 break;
2669 }
2670#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2671
2672 default:
2673 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2674 }
2675
2676 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02002677 if( used > buf_len )
2678 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002679
2680 return( 0 );
2681}
2682
2683/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02002684 * Public wrapper for ssl_session_save()
2685 */
2686int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
2687 unsigned char *buf,
2688 size_t buf_len,
2689 size_t *olen )
2690{
2691 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
2692}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002693
Jerry Yuf1b23ca2022-02-18 11:48:47 +08002694/*
2695 * Deserialize session, see mbedtls_ssl_session_save() for format.
2696 *
2697 * This internal version is wrapped by a public function that cleans up in
2698 * case of error, and has an extra option omit_header.
2699 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002700static int ssl_session_load( mbedtls_ssl_session *session,
2701 unsigned char omit_header,
2702 const unsigned char *buf,
2703 size_t len )
2704{
2705 const unsigned char *p = buf;
2706 const unsigned char * const end = buf + len;
2707
2708 if( !omit_header )
2709 {
2710 /*
2711 * Check Mbed TLS version identifier
2712 */
2713
2714 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
2715 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2716
2717 if( memcmp( p, ssl_serialized_session_header,
2718 sizeof( ssl_serialized_session_header ) ) != 0 )
2719 {
2720 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
2721 }
2722 p += sizeof( ssl_serialized_session_header );
2723 }
2724
2725 /*
2726 * TLS version identifier
2727 */
2728 if( 1 > (size_t)( end - p ) )
2729 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2730 session->minor_ver = *p++;
2731
2732 /* Dispatch according to TLS version. */
2733 switch( session->minor_ver )
2734 {
2735#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2736 case MBEDTLS_SSL_MINOR_VERSION_3: /* TLS 1.2 */
2737 {
2738 size_t remaining_len = ( end - p );
2739 return( ssl_session_load_tls12( session, p, remaining_len ) );
2740 }
2741#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2742
2743 default:
2744 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2745 }
2746}
2747
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002748/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02002749 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02002750 */
2751int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
2752 const unsigned char *buf,
2753 size_t len )
2754{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02002755 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02002756
2757 if( ret != 0 )
2758 mbedtls_ssl_session_free( session );
2759
2760 return( ret );
2761}
2762
2763/*
Paul Bakker1961b702013-01-25 14:49:24 +01002764 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00002765 */
Hanno Becker41934dd2021-08-07 19:13:43 +01002766static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
2767{
2768 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2769
Ronald Cron66dbf912022-02-02 15:33:46 +01002770 /*
2771 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01002772 * that were written into the output buffer by the previous handshake step,
2773 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01002774 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
2775 * We proceed to the next handshake step only when all data from the
2776 * previous one have been sent to the peer, thus we make sure that this is
2777 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
2778 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
2779 * we have to wait before to go ahead.
2780 * In the case of TLS 1.3, handshake step handlers do not send data to the
2781 * peer. Data are only sent here and through
2782 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
2783 * alert occured.
2784 */
Hanno Becker41934dd2021-08-07 19:13:43 +01002785 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2786 return( ret );
2787
2788#if defined(MBEDTLS_SSL_PROTO_DTLS)
2789 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2790 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
2791 {
2792 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
2793 return( ret );
2794 }
2795#endif /* MBEDTLS_SSL_PROTO_DTLS */
2796
2797 return( ret );
2798}
2799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002801{
Hanno Becker41934dd2021-08-07 19:13:43 +01002802 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002803
Hanno Becker41934dd2021-08-07 19:13:43 +01002804 if( ssl == NULL ||
2805 ssl->conf == NULL ||
2806 ssl->handshake == NULL ||
2807 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2808 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002809 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01002810 }
2811
2812 ret = ssl_prepare_handshake_step( ssl );
2813 if( ret != 0 )
2814 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002815
Jerry Yue7047812021-09-13 19:26:39 +08002816 ret = mbedtls_ssl_handle_pending_alert( ssl );
2817 if( ret != 0 )
2818 goto cleanup;
2819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002821 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08002822 {
Ronald Cron27c85e72022-03-08 11:37:55 +01002823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
2824 mbedtls_ssl_states_str( ssl->state ) ) );
2825
Ronald Cron6f135e12021-12-08 16:57:54 +01002826#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08002827 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yuf4436812021-08-26 22:59:56 +08002828 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01002829#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08002830
2831#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2832 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
2833 ret = mbedtls_ssl_handshake_client_step( ssl );
2834#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2835 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002836#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002838 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08002839 {
Ronald Cron6f135e12021-12-08 16:57:54 +01002840#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08002841 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08002842 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01002843#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08002844
2845#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2846 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
2847 ret = mbedtls_ssl_handshake_server_step( ssl );
2848#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2849 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002850#endif
2851
Jerry Yue7047812021-09-13 19:26:39 +08002852 if( ret != 0 )
2853 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08002854 /* handshake_step return error. And it is same
2855 * with alert_reason.
2856 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08002857 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08002858 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08002859 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08002860 goto cleanup;
2861 }
2862 }
2863
2864cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01002865 return( ret );
2866}
2867
2868/*
2869 * Perform the SSL handshake
2870 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01002872{
2873 int ret = 0;
2874
Hanno Beckera817ea42020-10-20 15:20:23 +01002875 /* Sanity checks */
2876
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002877 if( ssl == NULL || ssl->conf == NULL )
2878 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2879
Hanno Beckera817ea42020-10-20 15:20:23 +01002880#if defined(MBEDTLS_SSL_PROTO_DTLS)
2881 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2882 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
2883 {
2884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2885 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2886 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2887 }
2888#endif /* MBEDTLS_SSL_PROTO_DTLS */
2889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01002891
Hanno Beckera817ea42020-10-20 15:20:23 +01002892 /* Main handshake loop */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002893 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01002894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002895 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002896
2897 if( ret != 0 )
2898 break;
2899 }
2900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002902
2903 return( ret );
2904}
2905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906#if defined(MBEDTLS_SSL_RENEGOTIATION)
2907#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002908/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002909 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00002910 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002912{
Janos Follath865b3eb2019-12-16 11:46:15 +00002913 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002916
2917 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2919 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002920
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002921 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002922 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002923 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002924 return( ret );
2925 }
2926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002927 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002928
2929 return( 0 );
2930}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002932
2933/*
2934 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935 * - any side: calling mbedtls_ssl_renegotiate(),
2936 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
2937 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02002938 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002939 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002940 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002941 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00002942int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00002943{
Janos Follath865b3eb2019-12-16 11:46:15 +00002944 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00002945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002947
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002948 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
2949 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00002950
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02002951 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
2952 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002954 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02002956 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002957 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002958 ssl->handshake->out_msg_seq = 1;
2959 else
2960 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02002961 }
2962#endif
2963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
2965 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00002966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00002968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002969 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00002970 return( ret );
2971 }
2972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002974
2975 return( 0 );
2976}
2977
2978/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002979 * Renegotiate current connection on client,
2980 * or request renegotiation on server
2981 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002983{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002984 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002985
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002986 if( ssl == NULL || ssl->conf == NULL )
2987 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002990 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002991 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
2994 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02002997
2998 /* Did we already try/start sending HelloRequest? */
2999 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003001
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003002 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003003 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003004#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003006#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003007 /*
3008 * On client, either start the renegotiation process or,
3009 * if already in progress, continue the handshake
3010 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
3014 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003015
Hanno Becker40cdaa12020-02-05 10:48:27 +00003016 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003017 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003019 return( ret );
3020 }
3021 }
3022 else
3023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003024 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003027 return( ret );
3028 }
3029 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003031
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003032 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003033}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003035
Gilles Peskine9b562d52018-04-25 20:32:43 +02003036void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003037{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003038 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3039
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003040 if( handshake == NULL )
3041 return;
3042
Brett Warrene0edc842021-08-17 09:53:13 +01003043#if defined(MBEDTLS_ECP_C)
3044#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3045 if ( ssl->handshake->group_list_heap_allocated )
3046 mbedtls_free( (void*) handshake->group_list );
3047 handshake->group_list = NULL;
3048#endif /* MBEDTLS_DEPRECATED_REMOVED */
3049#endif /* MBEDTLS_ECP_C */
3050
Jerry Yuf017ee42022-01-12 15:49:48 +08003051#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3052#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3053 if ( ssl->handshake->sig_algs_heap_allocated )
3054 mbedtls_free( (void*) handshake->sig_algs );
3055 handshake->sig_algs = NULL;
3056#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003057#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3058 if( ssl->handshake->certificate_request_context )
3059 {
3060 mbedtls_free( (void*) handshake->certificate_request_context );
3061 }
3062#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003063#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3064
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003065#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3066 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3067 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003068 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003069 handshake->async_in_progress = 0;
3070 }
3071#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3072
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003073#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003074#if defined(MBEDTLS_USE_PSA_CRYPTO)
3075 psa_hash_abort( &handshake->fin_sha256_psa );
3076#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003077 mbedtls_sha256_free( &handshake->fin_sha256 );
3078#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003079#endif
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +02003080#if defined(MBEDTLS_SHA384_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003081#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003082 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003083#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003084 mbedtls_sha512_free( &handshake->fin_sha512 );
3085#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003086#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003088#if defined(MBEDTLS_DHM_C)
3089 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003090#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091#if defined(MBEDTLS_ECDH_C)
3092 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003093#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003094#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003095 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003096#if defined(MBEDTLS_SSL_CLI_C)
3097 mbedtls_free( handshake->ecjpake_cache );
3098 handshake->ecjpake_cache = NULL;
3099 handshake->ecjpake_cache_len = 0;
3100#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003101#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003102
Janos Follath4ae5c292016-02-10 11:27:43 +00003103#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3104 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003105 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003107#endif
3108
Gilles Peskineeccd8882020-03-10 12:19:08 +01003109#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003110 if( handshake->psk != NULL )
3111 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003112 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003113 mbedtls_free( handshake->psk );
3114 }
3115#endif
3116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3118 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003119 /*
3120 * Free only the linked list wrapper, not the keys themselves
3121 * since the belong to the SNI callback
3122 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003123 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003125
Gilles Peskineeccd8882020-03-10 12:19:08 +01003126#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003127 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003128 if( handshake->ecrs_peer_cert != NULL )
3129 {
3130 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3131 mbedtls_free( handshake->ecrs_peer_cert );
3132 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003133#endif
3134
Hanno Becker75173122019-02-06 16:18:31 +00003135#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3136 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3137 mbedtls_pk_free( &handshake->peer_pubkey );
3138#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3139
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003140#if defined(MBEDTLS_SSL_CLI_C) && \
3141 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3142 mbedtls_free( handshake->cookie );
3143#endif /* MBEDTLS_SSL_CLI_C &&
3144 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003145
3146#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003147 mbedtls_ssl_flight_free( handshake->flight );
3148 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003149#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003150
Ronald Cronf12b81d2022-03-15 10:42:41 +01003151#if defined(MBEDTLS_ECDH_C) && \
3152 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Hanno Becker4a63ed42019-01-08 11:39:35 +00003153 psa_destroy_key( handshake->ecdh_psa_privkey );
3154#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3155
Ronald Cron6f135e12021-12-08 16:57:54 +01003156#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003157 mbedtls_ssl_transform_free( handshake->transform_handshake );
3158 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003159 mbedtls_free( handshake->transform_earlydata );
3160 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003161#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003162
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003163
3164#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3165 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3166 * processes datagrams and the fact that a datagram is allowed to have
3167 * several records in it, it is possible that the I/O buffers are not
3168 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003169 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3170 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003171#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003172
Jerry Yuba9c7272021-10-30 11:54:10 +08003173 /* mbedtls_platform_zeroize MUST be last one in this function */
3174 mbedtls_platform_zeroize( handshake,
3175 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003176}
3177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003179{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003180 if( session == NULL )
3181 return;
3182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003183#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003184 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003185#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003186
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003187#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003189#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003190
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003191 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003192}
3193
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003194#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003195
3196#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3197#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3198#else
3199#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3200#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3201
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003202#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003203
3204#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3205#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3206#else
3207#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3208#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3209
3210#if defined(MBEDTLS_SSL_ALPN)
3211#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3212#else
3213#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3214#endif /* MBEDTLS_SSL_ALPN */
3215
3216#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3217#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3218#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3219#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3220
3221#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3222 ( (uint32_t) ( \
3223 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3224 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3225 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3226 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3227 0u ) )
3228
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003229static unsigned char ssl_serialized_context_header[] = {
3230 MBEDTLS_VERSION_MAJOR,
3231 MBEDTLS_VERSION_MINOR,
3232 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003233 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3234 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3235 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3236 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3237 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003238};
3239
Paul Bakker5121ce52009-01-03 21:22:43 +00003240/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003241 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003242 *
3243 * The format of the serialized data is:
3244 * (in the presentation language of TLS, RFC 8446 section 3)
3245 *
3246 * // header
3247 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003248 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003249 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003250 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003251 * Note: When updating the format, remember to keep these
3252 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003253 *
3254 * // session sub-structure
3255 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3256 * // transform sub-structure
3257 * uint8 random[64]; // ServerHello.random+ClientHello.random
3258 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3259 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3260 * // fields from ssl_context
3261 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3262 * uint64 in_window_top; // DTLS: last validated record seq_num
3263 * uint64 in_window; // DTLS: bitmask for replay protection
3264 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3265 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3266 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3267 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3268 *
3269 * Note that many fields of the ssl_context or sub-structures are not
3270 * serialized, as they fall in one of the following categories:
3271 *
3272 * 1. forced value (eg in_left must be 0)
3273 * 2. pointer to dynamically-allocated memory (eg session, transform)
3274 * 3. value can be re-derived from other data (eg session keys from MS)
3275 * 4. value was temporary (eg content of input buffer)
3276 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003277 */
3278int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3279 unsigned char *buf,
3280 size_t buf_len,
3281 size_t *olen )
3282{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003283 unsigned char *p = buf;
3284 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003285 size_t session_len;
3286 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003287
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003288 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003289 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3290 * this function's documentation.
3291 *
3292 * These are due to assumptions/limitations in the implementation. Some of
3293 * them are likely to stay (no handshake in progress) some might go away
3294 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003295 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003296 /* The initial handshake must be over */
3297 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003298 {
3299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003300 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003301 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003302 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003303 {
3304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003305 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003306 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003307 /* Double-check that sub-structures are indeed ready */
3308 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003309 {
3310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003311 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003312 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003313 /* There must be no pending incoming or outgoing data */
3314 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003315 {
3316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003317 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003318 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003319 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003320 {
3321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003322 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003323 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003324 /* Protocol must be DLTS, not TLS */
3325 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003326 {
3327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003328 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003329 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003330 /* Version must be 1.2 */
3331 if( ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003332 {
3333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003334 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003335 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003336 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003337 {
3338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003339 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003340 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003341 /* We must be using an AEAD ciphersuite */
3342 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003343 {
3344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003345 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003346 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003347 /* Renegotiation must not be enabled */
3348#if defined(MBEDTLS_SSL_RENEGOTIATION)
3349 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003350 {
3351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003352 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003353 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003354#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003355
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003356 /*
3357 * Version and format identifier
3358 */
3359 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003360
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003361 if( used <= buf_len )
3362 {
3363 memcpy( p, ssl_serialized_context_header,
3364 sizeof( ssl_serialized_context_header ) );
3365 p += sizeof( ssl_serialized_context_header );
3366 }
3367
3368 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003369 * Session (length + data)
3370 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003371 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003372 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3373 return( ret );
3374
3375 used += 4 + session_len;
3376 if( used <= buf_len )
3377 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003378 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3379 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003380
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003381 ret = ssl_session_save( ssl->session, 1,
3382 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003383 if( ret != 0 )
3384 return( ret );
3385
3386 p += session_len;
3387 }
3388
3389 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003390 * Transform
3391 */
3392 used += sizeof( ssl->transform->randbytes );
3393 if( used <= buf_len )
3394 {
3395 memcpy( p, ssl->transform->randbytes,
3396 sizeof( ssl->transform->randbytes ) );
3397 p += sizeof( ssl->transform->randbytes );
3398 }
3399
3400#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3401 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3402 if( used <= buf_len )
3403 {
3404 *p++ = ssl->transform->in_cid_len;
3405 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3406 p += ssl->transform->in_cid_len;
3407
3408 *p++ = ssl->transform->out_cid_len;
3409 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3410 p += ssl->transform->out_cid_len;
3411 }
3412#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3413
3414 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003415 * Saved fields from top-level ssl_context structure
3416 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003417 used += 4;
3418 if( used <= buf_len )
3419 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003420 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3421 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003422 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003423
3424#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3425 used += 16;
3426 if( used <= buf_len )
3427 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003428 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3429 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003430
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003431 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3432 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003433 }
3434#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3435
3436#if defined(MBEDTLS_SSL_PROTO_DTLS)
3437 used += 1;
3438 if( used <= buf_len )
3439 {
3440 *p++ = ssl->disable_datagram_packing;
3441 }
3442#endif /* MBEDTLS_SSL_PROTO_DTLS */
3443
Jerry Yuae0b2e22021-10-08 15:21:19 +08003444 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003445 if( used <= buf_len )
3446 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003447 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3448 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003449 }
3450
3451#if defined(MBEDTLS_SSL_PROTO_DTLS)
3452 used += 2;
3453 if( used <= buf_len )
3454 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003455 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
3456 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003457 }
3458#endif /* MBEDTLS_SSL_PROTO_DTLS */
3459
3460#if defined(MBEDTLS_SSL_ALPN)
3461 {
3462 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02003463 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003464 : 0;
3465
3466 used += 1 + alpn_len;
3467 if( used <= buf_len )
3468 {
3469 *p++ = alpn_len;
3470
3471 if( ssl->alpn_chosen != NULL )
3472 {
3473 memcpy( p, ssl->alpn_chosen, alpn_len );
3474 p += alpn_len;
3475 }
3476 }
3477 }
3478#endif /* MBEDTLS_SSL_ALPN */
3479
3480 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003481 * Done
3482 */
3483 *olen = used;
3484
3485 if( used > buf_len )
3486 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003487
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003488 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
3489
Hanno Becker43aefe22020-02-05 10:44:56 +00003490 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003491}
3492
3493/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003494 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003495 *
3496 * This internal version is wrapped by a public function that cleans up in
3497 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003498 */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003499static int ssl_context_load( mbedtls_ssl_context *ssl,
3500 const unsigned char *buf,
3501 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003502{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003503 const unsigned char *p = buf;
3504 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003505 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00003506 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003507
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003508 /*
3509 * The context should have been freshly setup or reset.
3510 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02003511 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003512 * renegotiating, or if the user mistakenly loaded a session first.)
3513 */
3514 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
3515 ssl->session != NULL )
3516 {
3517 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3518 }
3519
3520 /*
3521 * We can't check that the config matches the initial one, but we can at
3522 * least check it matches the requirements for serializing.
3523 */
3524 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
3525 ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
3526 ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 ||
3527 ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ||
3528 ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 ||
3529#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003530 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003531#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003532 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003533 {
3534 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3535 }
3536
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003537 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
3538
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003539 /*
3540 * Check version identifier
3541 */
3542 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
3543 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3544
3545 if( memcmp( p, ssl_serialized_context_header,
3546 sizeof( ssl_serialized_context_header ) ) != 0 )
3547 {
3548 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3549 }
3550 p += sizeof( ssl_serialized_context_header );
3551
3552 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003553 * Session
3554 */
3555 if( (size_t)( end - p ) < 4 )
3556 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3557
3558 session_len = ( (size_t) p[0] << 24 ) |
3559 ( (size_t) p[1] << 16 ) |
3560 ( (size_t) p[2] << 8 ) |
3561 ( (size_t) p[3] );
3562 p += 4;
3563
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003564 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00003565 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003566 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003567 ssl->session_in = ssl->session;
3568 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003569 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003570
3571 if( (size_t)( end - p ) < session_len )
3572 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3573
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003574 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003575 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003576 {
3577 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003578 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003579 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003580
3581 p += session_len;
3582
3583 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003584 * Transform
3585 */
3586
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003587 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00003588 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003589 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003590 ssl->transform_in = ssl->transform;
3591 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003592 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003593
3594 /* Read random bytes and populate structure */
3595 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
3596 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08003597#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00003598 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003599 ssl->session->ciphersuite,
3600 ssl->session->master,
Hanno Beckerbd257552021-03-22 06:59:27 +00003601#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
3602 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003603 ssl->session->encrypt_then_mac,
Hanno Beckerbd257552021-03-22 06:59:27 +00003604#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
3605 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003606 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
3607 p, /* currently pointing to randbytes */
3608 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
3609 ssl->conf->endpoint,
3610 ssl );
3611 if( ret != 0 )
3612 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08003613#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003614 p += sizeof( ssl->transform->randbytes );
3615
3616#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3617 /* Read connection IDs and store them */
3618 if( (size_t)( end - p ) < 1 )
3619 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3620
3621 ssl->transform->in_cid_len = *p++;
3622
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02003623 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003624 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3625
3626 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
3627 p += ssl->transform->in_cid_len;
3628
3629 ssl->transform->out_cid_len = *p++;
3630
3631 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
3632 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3633
3634 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
3635 p += ssl->transform->out_cid_len;
3636#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3637
3638 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003639 * Saved fields from top-level ssl_context structure
3640 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003641 if( (size_t)( end - p ) < 4 )
3642 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3643
3644 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
3645 ( (uint32_t) p[1] << 16 ) |
3646 ( (uint32_t) p[2] << 8 ) |
3647 ( (uint32_t) p[3] );
3648 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003649
3650#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3651 if( (size_t)( end - p ) < 16 )
3652 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3653
3654 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
3655 ( (uint64_t) p[1] << 48 ) |
3656 ( (uint64_t) p[2] << 40 ) |
3657 ( (uint64_t) p[3] << 32 ) |
3658 ( (uint64_t) p[4] << 24 ) |
3659 ( (uint64_t) p[5] << 16 ) |
3660 ( (uint64_t) p[6] << 8 ) |
3661 ( (uint64_t) p[7] );
3662 p += 8;
3663
3664 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
3665 ( (uint64_t) p[1] << 48 ) |
3666 ( (uint64_t) p[2] << 40 ) |
3667 ( (uint64_t) p[3] << 32 ) |
3668 ( (uint64_t) p[4] << 24 ) |
3669 ( (uint64_t) p[5] << 16 ) |
3670 ( (uint64_t) p[6] << 8 ) |
3671 ( (uint64_t) p[7] );
3672 p += 8;
3673#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3674
3675#if defined(MBEDTLS_SSL_PROTO_DTLS)
3676 if( (size_t)( end - p ) < 1 )
3677 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3678
3679 ssl->disable_datagram_packing = *p++;
3680#endif /* MBEDTLS_SSL_PROTO_DTLS */
3681
Jerry Yud9a94fe2021-09-28 18:58:59 +08003682 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003683 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08003684 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
3685 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003686
3687#if defined(MBEDTLS_SSL_PROTO_DTLS)
3688 if( (size_t)( end - p ) < 2 )
3689 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3690
3691 ssl->mtu = ( p[0] << 8 ) | p[1];
3692 p += 2;
3693#endif /* MBEDTLS_SSL_PROTO_DTLS */
3694
3695#if defined(MBEDTLS_SSL_ALPN)
3696 {
3697 uint8_t alpn_len;
3698 const char **cur;
3699
3700 if( (size_t)( end - p ) < 1 )
3701 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3702
3703 alpn_len = *p++;
3704
3705 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
3706 {
3707 /* alpn_chosen should point to an item in the configured list */
3708 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
3709 {
3710 if( strlen( *cur ) == alpn_len &&
3711 memcmp( p, cur, alpn_len ) == 0 )
3712 {
3713 ssl->alpn_chosen = *cur;
3714 break;
3715 }
3716 }
3717 }
3718
3719 /* can only happen on conf mismatch */
3720 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
3721 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3722
3723 p += alpn_len;
3724 }
3725#endif /* MBEDTLS_SSL_ALPN */
3726
3727 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003728 * Forced fields from top-level ssl_context structure
3729 *
3730 * Most of them already set to the correct value by mbedtls_ssl_init() and
3731 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
3732 */
3733 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
3734
3735 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
3736 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
3737
Hanno Becker361b10d2019-08-30 10:42:49 +01003738 /* Adjust pointers for header fields of outgoing records to
3739 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00003740 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01003741
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003742#if defined(MBEDTLS_SSL_PROTO_DTLS)
3743 ssl->in_epoch = 1;
3744#endif
3745
3746 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
3747 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00003748 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
3749 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003750 if( ssl->handshake != NULL )
3751 {
3752 mbedtls_ssl_handshake_free( ssl );
3753 mbedtls_free( ssl->handshake );
3754 ssl->handshake = NULL;
3755 }
3756
3757 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003758 * Done - should have consumed entire buffer
3759 */
3760 if( p != end )
3761 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003762
3763 return( 0 );
3764}
3765
3766/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003767 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003768 */
3769int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
3770 const unsigned char *buf,
3771 size_t len )
3772{
3773 int ret = ssl_context_load( context, buf, len );
3774
3775 if( ret != 0 )
3776 mbedtls_ssl_free( context );
3777
3778 return( ret );
3779}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003780#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003781
3782/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003783 * Free an SSL context
3784 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003785void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003786{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003787 if( ssl == NULL )
3788 return;
3789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003791
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003792 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003793 {
sander-visserb8aa2072020-05-06 22:05:13 +02003794#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3795 size_t out_buf_len = ssl->out_buf_len;
3796#else
3797 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
3798#endif
3799
Darryl Greenb33cc762019-11-28 14:29:44 +00003800 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003801 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003802 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003803 }
3804
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003805 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003806 {
sander-visserb8aa2072020-05-06 22:05:13 +02003807#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3808 size_t in_buf_len = ssl->in_buf_len;
3809#else
3810 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
3811#endif
3812
Darryl Greenb33cc762019-11-28 14:29:44 +00003813 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003814 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003815 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003816 }
3817
Paul Bakker48916f92012-09-16 19:57:18 +00003818 if( ssl->transform )
3819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003820 mbedtls_ssl_transform_free( ssl->transform );
3821 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003822 }
3823
3824 if( ssl->handshake )
3825 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02003826 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003827 mbedtls_ssl_transform_free( ssl->transform_negotiate );
3828 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00003829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003830 mbedtls_free( ssl->handshake );
3831 mbedtls_free( ssl->transform_negotiate );
3832 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00003833 }
3834
Ronald Cron6f135e12021-12-08 16:57:54 +01003835#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01003836 mbedtls_ssl_transform_free( ssl->transform_application );
3837 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01003838#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01003839
Paul Bakkerc0463502013-02-14 11:19:38 +01003840 if( ssl->session )
3841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842 mbedtls_ssl_session_free( ssl->session );
3843 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003844 }
3845
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02003846#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02003847 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003848 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003849 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00003851 }
Paul Bakker0be444a2013-08-27 21:55:01 +02003852#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003853
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003854#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02003856#endif
3857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00003859
Paul Bakker86f04f42013-02-14 11:20:09 +01003860 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003861 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003862}
3863
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02003864/*
3865 * Initialze mbedtls_ssl_config
3866 */
3867void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
3868{
3869 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
3870}
3871
Gilles Peskineeccd8882020-03-10 12:19:08 +01003872#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003873#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskineae270bf2021-06-02 00:05:29 +02003874/* The selection should be the same as mbedtls_x509_crt_profile_default in
Gilles Peskinea28f0f52021-06-02 15:29:38 +02003875 * x509_crt.c. Here, the order matters. Currently we favor stronger hashes,
3876 * for no fundamental reason.
Gilles Peskineae270bf2021-06-02 00:05:29 +02003877 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
3878 * about this list. */
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003879static int ssl_preset_default_hashes[] = {
3880#if defined(MBEDTLS_SHA512_C)
3881 MBEDTLS_MD_SHA512,
Mateusz Starzyk3352a532021-04-06 14:28:22 +02003882#endif
3883#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003884 MBEDTLS_MD_SHA384,
3885#endif
3886#if defined(MBEDTLS_SHA256_C)
3887 MBEDTLS_MD_SHA256,
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02003888#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003889 MBEDTLS_MD_NONE
3890};
Jerry Yuf017ee42022-01-12 15:49:48 +08003891#endif /* !MBEDTLS_DEPRECATED_REMOVED */
3892#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003893
Gilles Peskineae270bf2021-06-02 00:05:29 +02003894/* The selection should be the same as mbedtls_x509_crt_profile_default in
3895 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02003896 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02003897 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02003898 * about this list.
3899 */
Brett Warrene0edc842021-08-17 09:53:13 +01003900static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02003901#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003902 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003903#endif
3904#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003905 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003906#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02003907#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003908 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003909#endif
3910#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003911 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003912#endif
3913#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003914 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003915#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02003916#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003917 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003918#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02003919#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003920 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003921#endif
3922#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003923 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003924#endif
Brett Warrene0edc842021-08-17 09:53:13 +01003925 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02003926};
Gilles Peskineae270bf2021-06-02 00:05:29 +02003927
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003928static int ssl_preset_suiteb_ciphersuites[] = {
3929 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
3930 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
3931 0
3932};
3933
Gilles Peskineeccd8882020-03-10 12:19:08 +01003934#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003935#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003936static int ssl_preset_suiteb_hashes[] = {
Jerry Yuf017ee42022-01-12 15:49:48 +08003937#if defined(MBEDTLS_SHA256_C)
3938 MBEDTLS_MD_SHA256,
3939#endif
Jerry Yu713013f2022-01-17 18:16:35 +08003940#if defined(MBEDTLS_SHA384_C)
3941 MBEDTLS_MD_SHA384,
3942#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003943 MBEDTLS_MD_NONE
3944};
Jerry Yuf017ee42022-01-12 15:49:48 +08003945#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01003946
Jerry Yu909df7b2022-01-22 11:56:27 +08003947/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08003948 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08003949 * rules SHOULD be upheld.
3950 * - No duplicate entries.
3951 * - But if there is a good reason, do not change the order of the algorithms.
3952 * - ssl_tls12_present* is for TLS 1.2 use only.
3953 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08003954 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01003955static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08003956
Jerry Yued5e9f42022-01-26 11:21:34 +08003957#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \
3958 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
3959 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
3960#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C &&
3961 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08003962
Jerry Yu909df7b2022-01-22 11:56:27 +08003963#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \
3964 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
3965 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
3966#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
3967 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
3968
Jerry Yued5e9f42022-01-26 11:21:34 +08003969#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA512_C) && \
3970 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
3971 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
3972#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
3973 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08003974
Jerry Yu909df7b2022-01-22 11:56:27 +08003975#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C)
3976 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
3977#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */
3978
Ronald Cron8540cf62022-03-16 08:01:09 +01003979#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA512_C)
3980 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
3981#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
3982
3983#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA384_C)
3984 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
3985#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
3986
Jerry Yu53037892022-01-25 11:02:06 +08003987#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C)
3988 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
3989#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
3990
Jerry Yu909df7b2022-01-22 11:56:27 +08003991 MBEDTLS_TLS1_3_SIG_NONE
3992};
3993
3994/* NOTICE: see above */
3995#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3996static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Jerry Yu713013f2022-01-17 18:16:35 +08003997#if defined(MBEDTLS_SHA512_C)
3998 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA512 )
3999#endif
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004000#if defined(MBEDTLS_SHA384_C)
4001 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 )
4002#endif
4003#if defined(MBEDTLS_SHA256_C)
4004 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 )
4005#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08004006 MBEDTLS_TLS1_3_SIG_NONE
4007};
4008#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4009/* NOTICE: see above */
4010static uint16_t ssl_preset_suiteb_sig_algs[] = {
4011
Jerry Yu909df7b2022-01-22 11:56:27 +08004012#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \
4013 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4014 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
4015#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C &&
4016 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4017
Jerry Yu53037892022-01-25 11:02:06 +08004018#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \
4019 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4020 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
4021#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
4022 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004023
4024#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C)
4025 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4026#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004027
Jerry Yu53037892022-01-25 11:02:06 +08004028#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C)
4029 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4030#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4031
Jerry Yu713013f2022-01-17 18:16:35 +08004032 MBEDTLS_TLS1_3_SIG_NONE
4033};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004034
Jerry Yu909df7b2022-01-22 11:56:27 +08004035/* NOTICE: see above */
4036#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4037static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Jerry Yu713013f2022-01-17 18:16:35 +08004038#if defined(MBEDTLS_SHA256_C)
4039 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 )
4040#endif
Jerry Yu18c833e2022-01-25 10:55:47 +08004041#if defined(MBEDTLS_SHA384_C)
4042 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 )
4043#endif
Xiaofei Bai95395012021-11-25 08:51:30 +00004044 MBEDTLS_TLS1_3_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004045};
Jerry Yu909df7b2022-01-22 11:56:27 +08004046#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4047
Jerry Yu1a8b4812022-01-20 17:56:50 +08004048#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004049
Brett Warrene0edc842021-08-17 09:53:13 +01004050static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004051#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004052 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004053#endif
4054#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004055 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004056#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004057 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004058};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004059
Jerry Yu1a8b4812022-01-20 17:56:50 +08004060#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004061/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004062 * to make sure there are no duplicated signature algorithm entries. */
Jerry Yuf377d642022-01-25 10:43:59 +08004063static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004064{
4065 size_t i, j;
4066 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004067
Jerry Yuf377d642022-01-25 10:43:59 +08004068 for( i = 0; sig_algs[i] != MBEDTLS_TLS1_3_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004069 {
Jerry Yuf377d642022-01-25 10:43:59 +08004070 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004071 {
Jerry Yuf377d642022-01-25 10:43:59 +08004072 if( sig_algs[i] != sig_algs[j] )
4073 continue;
4074 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4075 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4076 sig_algs[i], j, i );
4077 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004078 }
4079 }
4080 return( ret );
4081}
4082
4083#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4084
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004085/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004086 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004087 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004088int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004089 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004090{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004091#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004092 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004093#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004094
Jerry Yu1a8b4812022-01-20 17:56:50 +08004095#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004096 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004097 {
4098 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4099 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4100 }
4101
Jerry Yuf377d642022-01-25 10:43:59 +08004102 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004103 {
4104 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4105 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4106 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004107
4108#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004109 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004110 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004111 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004112 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4113 }
4114
Jerry Yuf377d642022-01-25 10:43:59 +08004115 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004116 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004117 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004118 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4119 }
4120#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004121#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4122
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004123 /* Use the functions here so that they are covered in tests,
4124 * but otherwise access member directly for efficiency */
4125 mbedtls_ssl_conf_endpoint( conf, endpoint );
4126 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004127
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004128 /*
4129 * Things that are common to all presets
4130 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004131#if defined(MBEDTLS_SSL_CLI_C)
4132 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4133 {
4134 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4135#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4136 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4137#endif
4138 }
4139#endif
4140
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004141#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4142 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4143#endif
4144
4145#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4146 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4147#endif
4148
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004149#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004150 conf->f_cookie_write = ssl_cookie_write_dummy;
4151 conf->f_cookie_check = ssl_cookie_check_dummy;
4152#endif
4153
4154#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4155 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4156#endif
4157
Janos Follath088ce432017-04-10 12:42:31 +01004158#if defined(MBEDTLS_SSL_SRV_C)
4159 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004160 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004161#endif
4162
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004163#if defined(MBEDTLS_SSL_PROTO_DTLS)
4164 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4165 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4166#endif
4167
4168#if defined(MBEDTLS_SSL_RENEGOTIATION)
4169 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004170 memset( conf->renego_period, 0x00, 2 );
4171 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004172#endif
4173
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004174#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004175 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4176 {
4177 const unsigned char dhm_p[] =
4178 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4179 const unsigned char dhm_g[] =
4180 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004181
Hanno Beckere2defad2021-07-24 05:59:17 +01004182 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4183 dhm_p, sizeof( dhm_p ),
4184 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4185 {
4186 return( ret );
4187 }
4188 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004189#endif
4190
Ronald Cron6f135e12021-12-08 16:57:54 +01004191#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker71f1ed62021-07-24 06:01:47 +01004192 /*
4193 * Allow all TLS 1.3 key exchange modes by default.
4194 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004195 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004196#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004197
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004198 /*
4199 * Preset-specific defaults
4200 */
4201 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004202 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004203 /*
4204 * NSA Suite B
4205 */
4206 case MBEDTLS_SSL_PRESET_SUITEB:
4207 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
4208 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
4209 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004210#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4211 /* Hybrid TLS 1.2/1.3 is not supported yet */
4212 conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
4213#else
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004214 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004215#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3 */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004216
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004217 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004218
4219#if defined(MBEDTLS_X509_CRT_PARSE_C)
4220 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004221#endif
4222
Gilles Peskineeccd8882020-03-10 12:19:08 +01004223#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004224#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004225 conf->sig_hashes = ssl_preset_suiteb_hashes;
Jerry Yuf017ee42022-01-12 15:49:48 +08004226#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004227#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4228 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4229 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4230 else
4231#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4232 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004233#endif
4234
Brett Warrene0edc842021-08-17 09:53:13 +01004235#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4236 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004237#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004238 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004239 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004240
4241 /*
4242 * Default
4243 */
4244 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03004245 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
4246 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
4247 MBEDTLS_SSL_MIN_MAJOR_VERSION :
4248 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
4249 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
4250 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
4251 MBEDTLS_SSL_MIN_MINOR_VERSION :
4252 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004253 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004254#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4255 /* Hybrid TLS 1.2/1.3 is not supported yet */
4256 conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
4257#else
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004258 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004259#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3 */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004260
4261#if defined(MBEDTLS_SSL_PROTO_DTLS)
4262 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
TRodziewiczef73f012021-05-13 14:53:36 +02004263 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004264#endif
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004265 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004266
4267#if defined(MBEDTLS_X509_CRT_PARSE_C)
4268 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4269#endif
4270
Gilles Peskineeccd8882020-03-10 12:19:08 +01004271#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004272#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01004273 conf->sig_hashes = ssl_preset_default_hashes;
Jerry Yuf017ee42022-01-12 15:49:48 +08004274#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004275#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4276 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4277 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4278 else
4279#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4280 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004281#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004282
Brett Warrene0edc842021-08-17 09:53:13 +01004283#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4284 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004285#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004286 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004287
4288#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4289 conf->dhm_min_bitlen = 1024;
4290#endif
4291 }
4292
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004293 return( 0 );
4294}
4295
4296/*
4297 * Free mbedtls_ssl_config
4298 */
4299void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4300{
4301#if defined(MBEDTLS_DHM_C)
4302 mbedtls_mpi_free( &conf->dhm_P );
4303 mbedtls_mpi_free( &conf->dhm_G );
4304#endif
4305
Gilles Peskineeccd8882020-03-10 12:19:08 +01004306#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004307 if( conf->psk != NULL )
4308 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004309 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004310 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004311 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004312 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004313 }
4314
4315 if( conf->psk_identity != NULL )
4316 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004317 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004318 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004319 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004320 conf->psk_identity_len = 0;
4321 }
4322#endif
4323
4324#if defined(MBEDTLS_X509_CRT_PARSE_C)
4325 ssl_key_cert_free( conf->key_cert );
4326#endif
4327
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004328 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004329}
4330
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004331#if defined(MBEDTLS_PK_C) && \
4332 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004333/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004334 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004336unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004337{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004338#if defined(MBEDTLS_RSA_C)
4339 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4340 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004341#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004342#if defined(MBEDTLS_ECDSA_C)
4343 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4344 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004345#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004346 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004347}
4348
Hanno Becker7e5437a2017-04-28 17:15:26 +01004349unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4350{
4351 switch( type ) {
4352 case MBEDTLS_PK_RSA:
4353 return( MBEDTLS_SSL_SIG_RSA );
4354 case MBEDTLS_PK_ECDSA:
4355 case MBEDTLS_PK_ECKEY:
4356 return( MBEDTLS_SSL_SIG_ECDSA );
4357 default:
4358 return( MBEDTLS_SSL_SIG_ANON );
4359 }
4360}
4361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004362mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004363{
4364 switch( sig )
4365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004366#if defined(MBEDTLS_RSA_C)
4367 case MBEDTLS_SSL_SIG_RSA:
4368 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004369#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004370#if defined(MBEDTLS_ECDSA_C)
4371 case MBEDTLS_SSL_SIG_ECDSA:
4372 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004373#endif
4374 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004375 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004376 }
4377}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004378#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004379
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004380/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004381 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004382 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004383mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004384{
4385 switch( hash )
4386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004387#if defined(MBEDTLS_MD5_C)
4388 case MBEDTLS_SSL_HASH_MD5:
4389 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004390#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004391#if defined(MBEDTLS_SHA1_C)
4392 case MBEDTLS_SSL_HASH_SHA1:
4393 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004394#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004395#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004396 case MBEDTLS_SSL_HASH_SHA224:
4397 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004398#endif
4399#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004400 case MBEDTLS_SSL_HASH_SHA256:
4401 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004402#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004403#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004404 case MBEDTLS_SSL_HASH_SHA384:
4405 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004406#endif
4407#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004408 case MBEDTLS_SSL_HASH_SHA512:
4409 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004410#endif
4411 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004412 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004413 }
4414}
4415
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004416/*
4417 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4418 */
4419unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4420{
4421 switch( md )
4422 {
4423#if defined(MBEDTLS_MD5_C)
4424 case MBEDTLS_MD_MD5:
4425 return( MBEDTLS_SSL_HASH_MD5 );
4426#endif
4427#if defined(MBEDTLS_SHA1_C)
4428 case MBEDTLS_MD_SHA1:
4429 return( MBEDTLS_SSL_HASH_SHA1 );
4430#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004431#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004432 case MBEDTLS_MD_SHA224:
4433 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004434#endif
4435#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004436 case MBEDTLS_MD_SHA256:
4437 return( MBEDTLS_SSL_HASH_SHA256 );
4438#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004439#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004440 case MBEDTLS_MD_SHA384:
4441 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004442#endif
4443#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004444 case MBEDTLS_MD_SHA512:
4445 return( MBEDTLS_SSL_HASH_SHA512 );
4446#endif
4447 default:
4448 return( MBEDTLS_SSL_HASH_NONE );
4449 }
4450}
4451
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004452/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004453 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004454 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004455 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004456int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004457{
Brett Warrene0edc842021-08-17 09:53:13 +01004458 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004459
Brett Warrene0edc842021-08-17 09:53:13 +01004460 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004461 return( -1 );
4462
Brett Warrene0edc842021-08-17 09:53:13 +01004463 for( ; *group_list != 0; group_list++ )
4464 {
4465 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004466 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01004467 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004468
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004469 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004470}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004471
4472#if defined(MBEDTLS_ECP_C)
4473/*
4474 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
4475 */
4476int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
4477{
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01004478 uint16_t tls_id = mbedtls_ecp_curve_info_from_grp_id( grp_id )->tls_id;
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004479 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
4480}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004481#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004483#if defined(MBEDTLS_X509_CRT_PARSE_C)
4484int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
4485 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004486 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02004487 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004488{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004489 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004490 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004491 const char *ext_oid;
4492 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004494 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004495 {
4496 /* Server part of the key exchange */
4497 switch( ciphersuite->key_exchange )
4498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004499 case MBEDTLS_KEY_EXCHANGE_RSA:
4500 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004501 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004502 break;
4503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004504 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
4505 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
4506 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
4507 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004508 break;
4509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004510 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
4511 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004512 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004513 break;
4514
4515 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004516 case MBEDTLS_KEY_EXCHANGE_NONE:
4517 case MBEDTLS_KEY_EXCHANGE_PSK:
4518 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
4519 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02004520 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004521 usage = 0;
4522 }
4523 }
4524 else
4525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004526 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
4527 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004528 }
4529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004530 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004531 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004532 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004533 ret = -1;
4534 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004536 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004538 ext_oid = MBEDTLS_OID_SERVER_AUTH;
4539 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004540 }
4541 else
4542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004543 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
4544 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004545 }
4546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004547 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004548 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004549 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004550 ret = -1;
4551 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004552
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004553 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004554}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004555#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004556
Jerry Yu148165c2021-09-24 23:20:59 +08004557#if defined(MBEDTLS_USE_PSA_CRYPTO)
4558int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
4559 const mbedtls_md_type_t md,
4560 unsigned char *dst,
4561 size_t dst_len,
4562 size_t *olen )
4563{
Ronald Cronf6893e12022-01-07 22:09:01 +01004564 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4565 psa_hash_operation_t *hash_operation_to_clone;
4566 psa_hash_operation_t hash_operation = psa_hash_operation_init();
4567
Jerry Yu148165c2021-09-24 23:20:59 +08004568 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01004569
4570 switch( md )
4571 {
4572#if defined(MBEDTLS_SHA384_C)
4573 case MBEDTLS_MD_SHA384:
4574 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
4575 break;
4576#endif
4577
4578#if defined(MBEDTLS_SHA256_C)
4579 case MBEDTLS_MD_SHA256:
4580 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
4581 break;
4582#endif
4583
4584 default:
4585 goto exit;
4586 }
4587
4588 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
4589 if( status != PSA_SUCCESS )
4590 goto exit;
4591
4592 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
4593 if( status != PSA_SUCCESS )
4594 goto exit;
4595
4596exit:
Ronald Cronb788c042022-02-15 09:14:15 +01004597 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08004598}
4599#else /* MBEDTLS_USE_PSA_CRYPTO */
4600
Jerry Yu24c0ec32021-09-09 14:21:07 +08004601#if defined(MBEDTLS_SHA384_C)
Jerry Yu000f9762021-09-14 11:12:51 +08004602static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
4603 unsigned char *dst,
4604 size_t dst_len,
4605 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004606{
Jerry Yu24c0ec32021-09-09 14:21:07 +08004607 int ret;
4608 mbedtls_sha512_context sha512;
4609
4610 if( dst_len < 48 )
4611 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4612
4613 mbedtls_sha512_init( &sha512 );
4614 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
4615
4616 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
4617 {
4618 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
4619 goto exit;
4620 }
4621
4622 *olen = 48;
4623
4624exit:
4625
4626 mbedtls_sha512_free( &sha512 );
4627 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004628}
4629#endif /* MBEDTLS_SHA384_C */
4630
4631#if defined(MBEDTLS_SHA256_C)
Jerry Yu000f9762021-09-14 11:12:51 +08004632static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
4633 unsigned char *dst,
4634 size_t dst_len,
4635 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004636{
Jerry Yu24c0ec32021-09-09 14:21:07 +08004637 int ret;
4638 mbedtls_sha256_context sha256;
4639
4640 if( dst_len < 32 )
4641 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4642
4643 mbedtls_sha256_init( &sha256 );
4644 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08004645
Jerry Yu24c0ec32021-09-09 14:21:07 +08004646 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
4647 {
4648 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
4649 goto exit;
4650 }
4651
4652 *olen = 32;
4653
4654exit:
4655
4656 mbedtls_sha256_free( &sha256 );
4657 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004658}
4659#endif /* MBEDTLS_SHA256_C */
4660
Jerry Yu000f9762021-09-14 11:12:51 +08004661int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
4662 const mbedtls_md_type_t md,
4663 unsigned char *dst,
4664 size_t dst_len,
4665 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004666{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004667 switch( md )
4668 {
4669
Jerry Yu24c0ec32021-09-09 14:21:07 +08004670#if defined(MBEDTLS_SHA384_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004671 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08004672 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08004673#endif /* MBEDTLS_SHA384_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004674
Jerry Yu24c0ec32021-09-09 14:21:07 +08004675#if defined(MBEDTLS_SHA256_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004676 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08004677 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08004678#endif /* MBEDTLS_SHA256_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004679
4680 default:
4681 break;
4682 }
Jerry Yuc5aef882021-12-23 20:15:02 +08004683 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004684}
XiaokangQian647719a2021-12-07 09:16:29 +00004685
Jerry Yu148165c2021-09-24 23:20:59 +08004686#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08004687
Jerry Yudc7bd172022-02-17 13:44:15 +08004688#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4689
4690#if defined(MBEDTLS_USE_PSA_CRYPTO)
4691
4692static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
4693 mbedtls_svc_key_id_t key,
4694 psa_algorithm_t alg,
4695 const unsigned char* seed, size_t seed_length,
4696 const unsigned char* label, size_t label_length,
4697 size_t capacity )
4698{
4699 psa_status_t status;
4700
4701 status = psa_key_derivation_setup( derivation, alg );
4702 if( status != PSA_SUCCESS )
4703 return( status );
4704
4705 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4706 {
4707 status = psa_key_derivation_input_bytes( derivation,
4708 PSA_KEY_DERIVATION_INPUT_SEED,
4709 seed, seed_length );
4710 if( status != PSA_SUCCESS )
4711 return( status );
4712
4713 if( mbedtls_svc_key_id_is_null( key ) )
4714 {
4715 status = psa_key_derivation_input_bytes(
4716 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
4717 NULL, 0 );
4718 }
4719 else
4720 {
4721 status = psa_key_derivation_input_key(
4722 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
4723 }
4724 if( status != PSA_SUCCESS )
4725 return( status );
4726
4727 status = psa_key_derivation_input_bytes( derivation,
4728 PSA_KEY_DERIVATION_INPUT_LABEL,
4729 label, label_length );
4730 if( status != PSA_SUCCESS )
4731 return( status );
4732 }
4733 else
4734 {
4735 return( PSA_ERROR_NOT_SUPPORTED );
4736 }
4737
4738 status = psa_key_derivation_set_capacity( derivation, capacity );
4739 if( status != PSA_SUCCESS )
4740 return( status );
4741
4742 return( PSA_SUCCESS );
4743}
4744
4745static int tls_prf_generic( mbedtls_md_type_t md_type,
4746 const unsigned char *secret, size_t slen,
4747 const char *label,
4748 const unsigned char *random, size_t rlen,
4749 unsigned char *dstbuf, size_t dlen )
4750{
4751 psa_status_t status;
4752 psa_algorithm_t alg;
4753 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
4754 psa_key_derivation_operation_t derivation =
4755 PSA_KEY_DERIVATION_OPERATION_INIT;
4756
4757 if( md_type == MBEDTLS_MD_SHA384 )
4758 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
4759 else
4760 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
4761
4762 /* Normally a "secret" should be long enough to be impossible to
4763 * find by brute force, and in particular should not be empty. But
4764 * this PRF is also used to derive an IV, in particular in EAP-TLS,
4765 * and for this use case it makes sense to have a 0-length "secret".
4766 * Since the key API doesn't allow importing a key of length 0,
4767 * keep master_key=0, which setup_psa_key_derivation() understands
4768 * to mean a 0-length "secret" input. */
4769 if( slen != 0 )
4770 {
4771 psa_key_attributes_t key_attributes = psa_key_attributes_init();
4772 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
4773 psa_set_key_algorithm( &key_attributes, alg );
4774 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
4775
4776 status = psa_import_key( &key_attributes, secret, slen, &master_key );
4777 if( status != PSA_SUCCESS )
4778 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4779 }
4780
4781 status = setup_psa_key_derivation( &derivation,
4782 master_key, alg,
4783 random, rlen,
4784 (unsigned char const *) label,
4785 (size_t) strlen( label ),
4786 dlen );
4787 if( status != PSA_SUCCESS )
4788 {
4789 psa_key_derivation_abort( &derivation );
4790 psa_destroy_key( master_key );
4791 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4792 }
4793
4794 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
4795 if( status != PSA_SUCCESS )
4796 {
4797 psa_key_derivation_abort( &derivation );
4798 psa_destroy_key( master_key );
4799 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4800 }
4801
4802 status = psa_key_derivation_abort( &derivation );
4803 if( status != PSA_SUCCESS )
4804 {
4805 psa_destroy_key( master_key );
4806 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4807 }
4808
4809 if( ! mbedtls_svc_key_id_is_null( master_key ) )
4810 status = psa_destroy_key( master_key );
4811 if( status != PSA_SUCCESS )
4812 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4813
4814 return( 0 );
4815}
4816
4817#else /* MBEDTLS_USE_PSA_CRYPTO */
4818
4819static int tls_prf_generic( mbedtls_md_type_t md_type,
4820 const unsigned char *secret, size_t slen,
4821 const char *label,
4822 const unsigned char *random, size_t rlen,
4823 unsigned char *dstbuf, size_t dlen )
4824{
4825 size_t nb;
4826 size_t i, j, k, md_len;
4827 unsigned char *tmp;
4828 size_t tmp_len = 0;
4829 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
4830 const mbedtls_md_info_t *md_info;
4831 mbedtls_md_context_t md_ctx;
4832 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4833
4834 mbedtls_md_init( &md_ctx );
4835
4836 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
4837 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4838
4839 md_len = mbedtls_md_get_size( md_info );
4840
4841 tmp_len = md_len + strlen( label ) + rlen;
4842 tmp = mbedtls_calloc( 1, tmp_len );
4843 if( tmp == NULL )
4844 {
4845 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
4846 goto exit;
4847 }
4848
4849 nb = strlen( label );
4850 memcpy( tmp + md_len, label, nb );
4851 memcpy( tmp + md_len + nb, random, rlen );
4852 nb += rlen;
4853
4854 /*
4855 * Compute P_<hash>(secret, label + random)[0..dlen]
4856 */
4857 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
4858 goto exit;
4859
4860 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
4861 if( ret != 0 )
4862 goto exit;
4863 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
4864 if( ret != 0 )
4865 goto exit;
4866 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
4867 if( ret != 0 )
4868 goto exit;
4869
4870 for( i = 0; i < dlen; i += md_len )
4871 {
4872 ret = mbedtls_md_hmac_reset ( &md_ctx );
4873 if( ret != 0 )
4874 goto exit;
4875 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
4876 if( ret != 0 )
4877 goto exit;
4878 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
4879 if( ret != 0 )
4880 goto exit;
4881
4882 ret = mbedtls_md_hmac_reset ( &md_ctx );
4883 if( ret != 0 )
4884 goto exit;
4885 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
4886 if( ret != 0 )
4887 goto exit;
4888 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
4889 if( ret != 0 )
4890 goto exit;
4891
4892 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
4893
4894 for( j = 0; j < k; j++ )
4895 dstbuf[i + j] = h_i[j];
4896 }
4897
4898exit:
4899 mbedtls_md_free( &md_ctx );
4900
4901 mbedtls_platform_zeroize( tmp, tmp_len );
4902 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
4903
4904 mbedtls_free( tmp );
4905
4906 return( ret );
4907}
4908#endif /* MBEDTLS_USE_PSA_CRYPTO */
4909
4910#if defined(MBEDTLS_SHA256_C)
4911static int tls_prf_sha256( const unsigned char *secret, size_t slen,
4912 const char *label,
4913 const unsigned char *random, size_t rlen,
4914 unsigned char *dstbuf, size_t dlen )
4915{
4916 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
4917 label, random, rlen, dstbuf, dlen ) );
4918}
4919#endif /* MBEDTLS_SHA256_C */
4920
4921#if defined(MBEDTLS_SHA384_C)
4922static int tls_prf_sha384( const unsigned char *secret, size_t slen,
4923 const char *label,
4924 const unsigned char *random, size_t rlen,
4925 unsigned char *dstbuf, size_t dlen )
4926{
4927 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
4928 label, random, rlen, dstbuf, dlen ) );
4929}
4930#endif /* MBEDTLS_SHA384_C */
4931
Jerry Yuf009d862022-02-17 14:01:37 +08004932/*
4933 * Set appropriate PRF function and other SSL / TLS1.2 functions
4934 *
4935 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08004936 * - hash associated with the ciphersuite (only used by TLS 1.2)
4937 *
4938 * Outputs:
4939 * - the tls_prf, calc_verify and calc_finished members of handshake structure
4940 */
4941static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08004942 mbedtls_md_type_t hash )
4943{
Jerry Yuf009d862022-02-17 14:01:37 +08004944#if defined(MBEDTLS_SHA384_C)
Ronald Cron81591aa2022-03-07 09:05:51 +01004945 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08004946 {
4947 handshake->tls_prf = tls_prf_sha384;
4948 handshake->calc_verify = ssl_calc_verify_tls_sha384;
4949 handshake->calc_finished = ssl_calc_finished_tls_sha384;
4950 }
4951 else
4952#endif
4953#if defined(MBEDTLS_SHA256_C)
Jerry Yuf009d862022-02-17 14:01:37 +08004954 {
Ronald Cron81591aa2022-03-07 09:05:51 +01004955 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08004956 handshake->tls_prf = tls_prf_sha256;
4957 handshake->calc_verify = ssl_calc_verify_tls_sha256;
4958 handshake->calc_finished = ssl_calc_finished_tls_sha256;
4959 }
Ronald Cron81591aa2022-03-07 09:05:51 +01004960#else
Jerry Yuf009d862022-02-17 14:01:37 +08004961 {
Jerry Yuf009d862022-02-17 14:01:37 +08004962 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01004963 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08004964 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4965 }
Ronald Cron81591aa2022-03-07 09:05:51 +01004966#endif
Jerry Yuf009d862022-02-17 14:01:37 +08004967
4968 return( 0 );
4969}
Jerry Yud6ab2352022-02-17 14:03:43 +08004970
4971#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
4972 defined(MBEDTLS_USE_PSA_CRYPTO)
4973static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
4974{
4975 if( ssl->conf->f_psk != NULL )
4976 {
4977 /* If we've used a callback to select the PSK,
4978 * the static configuration is irrelevant. */
4979 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
4980 return( 1 );
4981
4982 return( 0 );
4983 }
4984
4985 if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) )
4986 return( 1 );
4987
4988 return( 0 );
4989}
4990#endif /* MBEDTLS_USE_PSA_CRYPTO &&
4991 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
4992
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08004993/*
4994 * Compute master secret if needed
4995 *
4996 * Parameters:
4997 * [in/out] handshake
4998 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
4999 * (PSA-PSK) ciphersuite_info, psk_opaque
5000 * [out] premaster (cleared)
5001 * [out] master
5002 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5003 * debug: conf->f_dbg, conf->p_dbg
5004 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005005 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005006 */
5007static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5008 unsigned char *master,
5009 const mbedtls_ssl_context *ssl )
5010{
5011 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5012
5013 /* cf. RFC 5246, Section 8.1:
5014 * "The master secret is always exactly 48 bytes in length." */
5015 size_t const master_secret_len = 48;
5016
5017#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5018 unsigned char session_hash[48];
5019#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5020
5021 /* The label for the KDF used for key expansion.
5022 * This is either "master secret" or "extended master secret"
5023 * depending on whether the Extended Master Secret extension
5024 * is used. */
5025 char const *lbl = "master secret";
5026
5027 /* The salt for the KDF used for key expansion.
5028 * - If the Extended Master Secret extension is not used,
5029 * this is ClientHello.Random + ServerHello.Random
5030 * (see Sect. 8.1 in RFC 5246).
5031 * - If the Extended Master Secret extension is used,
5032 * this is the transcript of the handshake so far.
5033 * (see Sect. 4 in RFC 7627). */
5034 unsigned char const *salt = handshake->randbytes;
5035 size_t salt_len = 64;
5036
5037#if !defined(MBEDTLS_DEBUG_C) && \
5038 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5039 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5040 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5041 ssl = NULL; /* make sure we don't use it except for those cases */
5042 (void) ssl;
5043#endif
5044
5045 if( handshake->resume != 0 )
5046 {
5047 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5048 return( 0 );
5049 }
5050
5051#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5052 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5053 {
5054 lbl = "extended master secret";
5055 salt = session_hash;
5056 handshake->calc_verify( ssl, session_hash, &salt_len );
5057
5058 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
5059 session_hash, salt_len );
5060 }
5061#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
5062
5063#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
5064 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5065 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005066 ssl_use_opaque_psk( ssl ) == 1 )
5067 {
5068 /* Perform PSK-to-MS expansion in a single step. */
5069 psa_status_t status;
5070 psa_algorithm_t alg;
5071 mbedtls_svc_key_id_t psk;
5072 psa_key_derivation_operation_t derivation =
5073 PSA_KEY_DERIVATION_OPERATION_INIT;
5074 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5075
5076 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5077
5078 psk = mbedtls_ssl_get_opaque_psk( ssl );
5079
5080 if( hash_alg == MBEDTLS_MD_SHA384 )
5081 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5082 else
5083 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5084
5085 status = setup_psa_key_derivation( &derivation, psk, alg,
5086 salt, salt_len,
5087 (unsigned char const *) lbl,
5088 (size_t) strlen( lbl ),
5089 master_secret_len );
5090 if( status != PSA_SUCCESS )
5091 {
5092 psa_key_derivation_abort( &derivation );
5093 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5094 }
5095
5096 status = psa_key_derivation_output_bytes( &derivation,
5097 master,
5098 master_secret_len );
5099 if( status != PSA_SUCCESS )
5100 {
5101 psa_key_derivation_abort( &derivation );
5102 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5103 }
5104
5105 status = psa_key_derivation_abort( &derivation );
5106 if( status != PSA_SUCCESS )
5107 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5108 }
5109 else
5110#endif
5111 {
5112 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
5113 lbl, salt, salt_len,
5114 master,
5115 master_secret_len );
5116 if( ret != 0 )
5117 {
5118 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5119 return( ret );
5120 }
5121
5122 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5123 handshake->premaster,
5124 handshake->pmslen );
5125
5126 mbedtls_platform_zeroize( handshake->premaster,
5127 sizeof(handshake->premaster) );
5128 }
5129
5130 return( 0 );
5131}
5132
Jerry Yud62f87e2022-02-17 14:09:02 +08005133int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5134{
5135 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5136 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5137 ssl->handshake->ciphersuite_info;
5138
5139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5140
5141 /* Set PRF, calc_verify and calc_finished function pointers */
5142 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005143 ciphersuite_info->mac );
5144 if( ret != 0 )
5145 {
5146 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5147 return( ret );
5148 }
5149
5150 /* Compute master secret if needed */
5151 ret = ssl_compute_master( ssl->handshake,
5152 ssl->session_negotiate->master,
5153 ssl );
5154 if( ret != 0 )
5155 {
5156 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5157 return( ret );
5158 }
5159
5160 /* Swap the client and server random values:
5161 * - MS derivation wanted client+server (RFC 5246 8.1)
5162 * - key derivation wants server+client (RFC 5246 6.3) */
5163 {
5164 unsigned char tmp[64];
5165 memcpy( tmp, ssl->handshake->randbytes, 64 );
5166 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5167 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5168 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5169 }
5170
5171 /* Populate transform structure */
5172 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5173 ssl->session_negotiate->ciphersuite,
5174 ssl->session_negotiate->master,
5175#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
5176 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5177 ssl->session_negotiate->encrypt_then_mac,
5178#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
5179 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
5180 ssl->handshake->tls_prf,
5181 ssl->handshake->randbytes,
5182 ssl->minor_ver,
5183 ssl->conf->endpoint,
5184 ssl );
5185 if( ret != 0 )
5186 {
5187 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5188 return( ret );
5189 }
5190
5191 /* We no longer need Server/ClientHello.random values */
5192 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5193 sizeof( ssl->handshake->randbytes ) );
5194
5195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5196
5197 return( 0 );
5198}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005199
Ronald Cron4dcbca92022-03-07 10:21:40 +01005200int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5201{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005202 switch( md )
5203 {
5204#if defined(MBEDTLS_SHA384_C)
5205 case MBEDTLS_SSL_HASH_SHA384:
5206 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5207 break;
5208#endif
5209#if defined(MBEDTLS_SHA256_C)
5210 case MBEDTLS_SSL_HASH_SHA256:
5211 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5212 break;
5213#endif
5214 default:
5215 return( -1 );
5216 }
5217
Ronald Cronc2f13a02022-03-07 10:25:24 +01005218 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005219}
5220
Jerry Yu8392e0d2022-02-17 14:10:24 +08005221#if defined(MBEDTLS_SHA256_C)
5222void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5223 unsigned char *hash,
5224 size_t *hlen )
5225{
5226#if defined(MBEDTLS_USE_PSA_CRYPTO)
5227 size_t hash_size;
5228 psa_status_t status;
5229 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5230
5231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5232 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5233 if( status != PSA_SUCCESS )
5234 {
5235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5236 return;
5237 }
5238
5239 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5240 if( status != PSA_SUCCESS )
5241 {
5242 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5243 return;
5244 }
5245
5246 *hlen = 32;
5247 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5249#else
5250 mbedtls_sha256_context sha256;
5251
5252 mbedtls_sha256_init( &sha256 );
5253
5254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5255
5256 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5257 mbedtls_sha256_finish( &sha256, hash );
5258
5259 *hlen = 32;
5260
5261 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5262 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5263
5264 mbedtls_sha256_free( &sha256 );
5265#endif /* MBEDTLS_USE_PSA_CRYPTO */
5266 return;
5267}
5268#endif /* MBEDTLS_SHA256_C */
5269
Jerry Yuc1cb3842022-02-17 14:13:48 +08005270#if defined(MBEDTLS_SHA384_C)
5271void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5272 unsigned char *hash,
5273 size_t *hlen )
5274{
5275#if defined(MBEDTLS_USE_PSA_CRYPTO)
5276 size_t hash_size;
5277 psa_status_t status;
5278 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5279
5280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5281 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5282 if( status != PSA_SUCCESS )
5283 {
5284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5285 return;
5286 }
5287
5288 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5289 if( status != PSA_SUCCESS )
5290 {
5291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5292 return;
5293 }
5294
5295 *hlen = 48;
5296 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5297 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5298#else
5299 mbedtls_sha512_context sha512;
5300
5301 mbedtls_sha512_init( &sha512 );
5302
5303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
5304
5305 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
5306 mbedtls_sha512_finish( &sha512, hash );
5307
5308 *hlen = 48;
5309
5310 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5312
5313 mbedtls_sha512_free( &sha512 );
5314#endif /* MBEDTLS_USE_PSA_CRYPTO */
5315 return;
5316}
5317#endif /* MBEDTLS_SHA384_C */
5318
Jerry Yuce3dca42022-02-17 14:16:37 +08005319#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
5320int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
5321{
5322 unsigned char *p = ssl->handshake->premaster;
5323 unsigned char *end = p + sizeof( ssl->handshake->premaster );
5324 const unsigned char *psk = NULL;
5325 size_t psk_len = 0;
5326
5327 if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len )
5328 == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
5329 {
5330 /*
5331 * This should never happen because the existence of a PSK is always
5332 * checked before calling this function
5333 */
5334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5335 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5336 }
5337
5338 /*
5339 * PMS = struct {
5340 * opaque other_secret<0..2^16-1>;
5341 * opaque psk<0..2^16-1>;
5342 * };
5343 * with "other_secret" depending on the particular key exchange
5344 */
5345#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5346 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
5347 {
5348 if( end - p < 2 )
5349 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5350
5351 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5352 p += 2;
5353
5354 if( end < p || (size_t)( end - p ) < psk_len )
5355 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5356
5357 memset( p, 0, psk_len );
5358 p += psk_len;
5359 }
5360 else
5361#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5362#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
5363 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5364 {
5365 /*
5366 * other_secret already set by the ClientKeyExchange message,
5367 * and is 48 bytes long
5368 */
5369 if( end - p < 2 )
5370 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5371
5372 *p++ = 0;
5373 *p++ = 48;
5374 p += 48;
5375 }
5376 else
5377#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
5378#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
5379 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5380 {
5381 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5382 size_t len;
5383
5384 /* Write length only when we know the actual value */
5385 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
5386 p + 2, end - ( p + 2 ), &len,
5387 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5388 {
5389 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
5390 return( ret );
5391 }
5392 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
5393 p += 2 + len;
5394
5395 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
5396 }
5397 else
5398#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
5399#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
5400 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
5401 {
5402 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5403 size_t zlen;
5404
5405 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
5406 p + 2, end - ( p + 2 ),
5407 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5408 {
5409 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
5410 return( ret );
5411 }
5412
5413 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
5414 p += 2 + zlen;
5415
5416 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
5417 MBEDTLS_DEBUG_ECDH_Z );
5418 }
5419 else
5420#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
5421 {
5422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5423 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5424 }
5425
5426 /* opaque psk<0..2^16-1>; */
5427 if( end - p < 2 )
5428 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5429
5430 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5431 p += 2;
5432
5433 if( end < p || (size_t)( end - p ) < psk_len )
5434 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5435
5436 memcpy( p, psk, psk_len );
5437 p += psk_len;
5438
5439 ssl->handshake->pmslen = p - ssl->handshake->premaster;
5440
5441 return( 0 );
5442}
5443#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08005444
5445#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
5446static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
5447
5448#if defined(MBEDTLS_SSL_PROTO_DTLS)
5449int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
5450{
5451 /* If renegotiation is not enforced, retransmit until we would reach max
5452 * timeout if we were using the usual handshake doubling scheme */
5453 if( ssl->conf->renego_max_records < 0 )
5454 {
5455 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
5456 unsigned char doublings = 1;
5457
5458 while( ratio != 0 )
5459 {
5460 ++doublings;
5461 ratio >>= 1;
5462 }
5463
5464 if( ++ssl->renego_records_seen > doublings )
5465 {
5466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
5467 return( 0 );
5468 }
5469 }
5470
5471 return( ssl_write_hello_request( ssl ) );
5472}
5473#endif
5474#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08005475
Jerry Yud9526692022-02-17 14:23:47 +08005476/*
5477 * Handshake functions
5478 */
5479#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5480/* No certificate support -> dummy functions */
5481int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
5482{
5483 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5484 ssl->handshake->ciphersuite_info;
5485
5486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
5487
5488 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5489 {
5490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5491 ssl->state++;
5492 return( 0 );
5493 }
5494
5495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5496 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5497}
5498
5499int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5500{
5501 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5502 ssl->handshake->ciphersuite_info;
5503
5504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5505
5506 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5507 {
5508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5509 ssl->state++;
5510 return( 0 );
5511 }
5512
5513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5514 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5515}
5516
5517#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5518/* Some certificate support -> implement write and parse */
5519
5520int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
5521{
5522 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
5523 size_t i, n;
5524 const mbedtls_x509_crt *crt;
5525 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5526 ssl->handshake->ciphersuite_info;
5527
5528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
5529
5530 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5531 {
5532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5533 ssl->state++;
5534 return( 0 );
5535 }
5536
5537#if defined(MBEDTLS_SSL_CLI_C)
5538 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5539 {
5540 if( ssl->handshake->client_auth == 0 )
5541 {
5542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5543 ssl->state++;
5544 return( 0 );
5545 }
5546 }
5547#endif /* MBEDTLS_SSL_CLI_C */
5548#if defined(MBEDTLS_SSL_SRV_C)
5549 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
5550 {
5551 if( mbedtls_ssl_own_cert( ssl ) == NULL )
5552 {
5553 /* Should never happen because we shouldn't have picked the
5554 * ciphersuite if we don't have a certificate. */
5555 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5556 }
5557 }
5558#endif
5559
5560 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
5561
5562 /*
5563 * 0 . 0 handshake type
5564 * 1 . 3 handshake length
5565 * 4 . 6 length of all certs
5566 * 7 . 9 length of cert. 1
5567 * 10 . n-1 peer certificate
5568 * n . n+2 length of cert. 2
5569 * n+3 . ... upper level cert, etc.
5570 */
5571 i = 7;
5572 crt = mbedtls_ssl_own_cert( ssl );
5573
5574 while( crt != NULL )
5575 {
5576 n = crt->raw.len;
5577 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
5578 {
5579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
5580 " > %" MBEDTLS_PRINTF_SIZET,
5581 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
5582 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5583 }
5584
5585 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
5586 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
5587 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
5588
5589 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5590 i += n; crt = crt->next;
5591 }
5592
5593 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
5594 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
5595 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
5596
5597 ssl->out_msglen = i;
5598 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5599 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
5600
5601 ssl->state++;
5602
5603 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
5604 {
5605 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
5606 return( ret );
5607 }
5608
5609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
5610
5611 return( ret );
5612}
5613
5614#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5615
5616#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5617static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5618 unsigned char *crt_buf,
5619 size_t crt_buf_len )
5620{
5621 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5622
5623 if( peer_crt == NULL )
5624 return( -1 );
5625
5626 if( peer_crt->raw.len != crt_buf_len )
5627 return( -1 );
5628
5629 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
5630}
5631#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5632static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5633 unsigned char *crt_buf,
5634 size_t crt_buf_len )
5635{
5636 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5637 unsigned char const * const peer_cert_digest =
5638 ssl->session->peer_cert_digest;
5639 mbedtls_md_type_t const peer_cert_digest_type =
5640 ssl->session->peer_cert_digest_type;
5641 mbedtls_md_info_t const * const digest_info =
5642 mbedtls_md_info_from_type( peer_cert_digest_type );
5643 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
5644 size_t digest_len;
5645
5646 if( peer_cert_digest == NULL || digest_info == NULL )
5647 return( -1 );
5648
5649 digest_len = mbedtls_md_get_size( digest_info );
5650 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
5651 return( -1 );
5652
5653 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
5654 if( ret != 0 )
5655 return( -1 );
5656
5657 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
5658}
5659#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5660#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5661
5662/*
5663 * Once the certificate message is read, parse it into a cert chain and
5664 * perform basic checks, but leave actual verification to the caller
5665 */
5666static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5667 mbedtls_x509_crt *chain )
5668{
5669 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5670#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5671 int crt_cnt=0;
5672#endif
5673 size_t i, n;
5674 uint8_t alert;
5675
5676 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5677 {
5678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5679 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5680 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5681 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5682 }
5683
5684 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
5685 {
5686 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5687 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5688 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5689 }
5690
5691 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
5692 {
5693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5694 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5695 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5696 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5697 }
5698
5699 i = mbedtls_ssl_hs_hdr_len( ssl );
5700
5701 /*
5702 * Same message structure as in mbedtls_ssl_write_certificate()
5703 */
5704 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
5705
5706 if( ssl->in_msg[i] != 0 ||
5707 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
5708 {
5709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5710 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5711 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5712 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5713 }
5714
5715 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5716 i += 3;
5717
5718 /* Iterate through and parse the CRTs in the provided chain. */
5719 while( i < ssl->in_hslen )
5720 {
5721 /* Check that there's room for the next CRT's length fields. */
5722 if ( i + 3 > ssl->in_hslen ) {
5723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5724 mbedtls_ssl_send_alert_message( ssl,
5725 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5726 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5727 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5728 }
5729 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
5730 * anything beyond 2**16 ~ 64K. */
5731 if( ssl->in_msg[i] != 0 )
5732 {
5733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5734 mbedtls_ssl_send_alert_message( ssl,
5735 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5736 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
5737 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
5738 }
5739
5740 /* Read length of the next CRT in the chain. */
5741 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5742 | (unsigned int) ssl->in_msg[i + 2];
5743 i += 3;
5744
5745 if( n < 128 || i + n > ssl->in_hslen )
5746 {
5747 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5748 mbedtls_ssl_send_alert_message( ssl,
5749 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5750 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5751 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5752 }
5753
5754 /* Check if we're handling the first CRT in the chain. */
5755#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5756 if( crt_cnt++ == 0 &&
5757 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5758 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
5759 {
5760 /* During client-side renegotiation, check that the server's
5761 * end-CRTs hasn't changed compared to the initial handshake,
5762 * mitigating the triple handshake attack. On success, reuse
5763 * the original end-CRT instead of parsing it again. */
5764 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
5765 if( ssl_check_peer_crt_unchanged( ssl,
5766 &ssl->in_msg[i],
5767 n ) != 0 )
5768 {
5769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
5770 mbedtls_ssl_send_alert_message( ssl,
5771 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5772 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
5773 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
5774 }
5775
5776 /* Now we can safely free the original chain. */
5777 ssl_clear_peer_cert( ssl->session );
5778 }
5779#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5780
5781 /* Parse the next certificate in the chain. */
5782#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5783 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
5784#else
5785 /* If we don't need to store the CRT chain permanently, parse
5786 * it in-place from the input buffer instead of making a copy. */
5787 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
5788#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5789 switch( ret )
5790 {
5791 case 0: /*ok*/
5792 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5793 /* Ignore certificate with an unknown algorithm: maybe a
5794 prior certificate was already trusted. */
5795 break;
5796
5797 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5798 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5799 goto crt_parse_der_failed;
5800
5801 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5802 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5803 goto crt_parse_der_failed;
5804
5805 default:
5806 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5807 crt_parse_der_failed:
5808 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
5809 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
5810 return( ret );
5811 }
5812
5813 i += n;
5814 }
5815
5816 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
5817 return( 0 );
5818}
5819
5820#if defined(MBEDTLS_SSL_SRV_C)
5821static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
5822{
5823 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5824 return( -1 );
5825
5826 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5827 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5828 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5829 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
5830 {
5831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
5832 return( 0 );
5833 }
5834 return( -1 );
5835}
5836#endif /* MBEDTLS_SSL_SRV_C */
5837
5838/* Check if a certificate message is expected.
5839 * Return either
5840 * - SSL_CERTIFICATE_EXPECTED, or
5841 * - SSL_CERTIFICATE_SKIP
5842 * indicating whether a Certificate message is expected or not.
5843 */
5844#define SSL_CERTIFICATE_EXPECTED 0
5845#define SSL_CERTIFICATE_SKIP 1
5846static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
5847 int authmode )
5848{
5849 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5850 ssl->handshake->ciphersuite_info;
5851
5852 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5853 return( SSL_CERTIFICATE_SKIP );
5854
5855#if defined(MBEDTLS_SSL_SRV_C)
5856 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
5857 {
5858 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5859 return( SSL_CERTIFICATE_SKIP );
5860
5861 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
5862 {
5863 ssl->session_negotiate->verify_result =
5864 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5865 return( SSL_CERTIFICATE_SKIP );
5866 }
5867 }
5868#else
5869 ((void) authmode);
5870#endif /* MBEDTLS_SSL_SRV_C */
5871
5872 return( SSL_CERTIFICATE_EXPECTED );
5873}
5874
5875static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
5876 int authmode,
5877 mbedtls_x509_crt *chain,
5878 void *rs_ctx )
5879{
5880 int ret = 0;
5881 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5882 ssl->handshake->ciphersuite_info;
5883 int have_ca_chain = 0;
5884
5885 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
5886 void *p_vrfy;
5887
5888 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
5889 return( 0 );
5890
5891 if( ssl->f_vrfy != NULL )
5892 {
5893 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
5894 f_vrfy = ssl->f_vrfy;
5895 p_vrfy = ssl->p_vrfy;
5896 }
5897 else
5898 {
5899 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
5900 f_vrfy = ssl->conf->f_vrfy;
5901 p_vrfy = ssl->conf->p_vrfy;
5902 }
5903
5904 /*
5905 * Main check: verify certificate
5906 */
5907#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
5908 if( ssl->conf->f_ca_cb != NULL )
5909 {
5910 ((void) rs_ctx);
5911 have_ca_chain = 1;
5912
5913 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
5914 ret = mbedtls_x509_crt_verify_with_ca_cb(
5915 chain,
5916 ssl->conf->f_ca_cb,
5917 ssl->conf->p_ca_cb,
5918 ssl->conf->cert_profile,
5919 ssl->hostname,
5920 &ssl->session_negotiate->verify_result,
5921 f_vrfy, p_vrfy );
5922 }
5923 else
5924#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
5925 {
5926 mbedtls_x509_crt *ca_chain;
5927 mbedtls_x509_crl *ca_crl;
5928
5929#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5930 if( ssl->handshake->sni_ca_chain != NULL )
5931 {
5932 ca_chain = ssl->handshake->sni_ca_chain;
5933 ca_crl = ssl->handshake->sni_ca_crl;
5934 }
5935 else
5936#endif
5937 {
5938 ca_chain = ssl->conf->ca_chain;
5939 ca_crl = ssl->conf->ca_crl;
5940 }
5941
5942 if( ca_chain != NULL )
5943 have_ca_chain = 1;
5944
5945 ret = mbedtls_x509_crt_verify_restartable(
5946 chain,
5947 ca_chain, ca_crl,
5948 ssl->conf->cert_profile,
5949 ssl->hostname,
5950 &ssl->session_negotiate->verify_result,
5951 f_vrfy, p_vrfy, rs_ctx );
5952 }
5953
5954 if( ret != 0 )
5955 {
5956 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
5957 }
5958
5959#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
5960 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
5961 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
5962#endif
5963
5964 /*
5965 * Secondary checks: always done, but change 'ret' only if it was 0
5966 */
5967
5968#if defined(MBEDTLS_ECP_C)
5969 {
5970 const mbedtls_pk_context *pk = &chain->pk;
5971
5972 /* If certificate uses an EC key, make sure the curve is OK */
5973 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
5974 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
5975 {
5976 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5977
5978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
5979 if( ret == 0 )
5980 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
5981 }
5982 }
5983#endif /* MBEDTLS_ECP_C */
5984
5985 if( mbedtls_ssl_check_cert_usage( chain,
5986 ciphersuite_info,
5987 ! ssl->conf->endpoint,
5988 &ssl->session_negotiate->verify_result ) != 0 )
5989 {
5990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
5991 if( ret == 0 )
5992 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
5993 }
5994
5995 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5996 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5997 * with details encoded in the verification flags. All other kinds
5998 * of error codes, including those from the user provided f_vrfy
5999 * functions, are treated as fatal and lead to a failure of
6000 * ssl_parse_certificate even if verification was optional. */
6001 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6002 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6003 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6004 {
6005 ret = 0;
6006 }
6007
6008 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6009 {
6010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6011 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6012 }
6013
6014 if( ret != 0 )
6015 {
6016 uint8_t alert;
6017
6018 /* The certificate may have been rejected for several reasons.
6019 Pick one and send the corresponding alert. Which alert to send
6020 may be a subject of debate in some cases. */
6021 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6022 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6023 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6024 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6025 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6026 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6027 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6028 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6029 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6030 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6031 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6032 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6033 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6034 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6035 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6036 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6037 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6038 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6039 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6040 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6041 else
6042 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6043 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6044 alert );
6045 }
6046
6047#if defined(MBEDTLS_DEBUG_C)
6048 if( ssl->session_negotiate->verify_result != 0 )
6049 {
6050 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6051 (unsigned int) ssl->session_negotiate->verify_result ) );
6052 }
6053 else
6054 {
6055 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6056 }
6057#endif /* MBEDTLS_DEBUG_C */
6058
6059 return( ret );
6060}
6061
6062#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6063static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6064 unsigned char *start, size_t len )
6065{
6066 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6067 /* Remember digest of the peer's end-CRT. */
6068 ssl->session_negotiate->peer_cert_digest =
6069 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6070 if( ssl->session_negotiate->peer_cert_digest == NULL )
6071 {
6072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6073 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6074 mbedtls_ssl_send_alert_message( ssl,
6075 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6076 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6077
6078 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6079 }
6080
6081 ret = mbedtls_md( mbedtls_md_info_from_type(
6082 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6083 start, len,
6084 ssl->session_negotiate->peer_cert_digest );
6085
6086 ssl->session_negotiate->peer_cert_digest_type =
6087 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6088 ssl->session_negotiate->peer_cert_digest_len =
6089 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6090
6091 return( ret );
6092}
6093
6094static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6095 unsigned char *start, size_t len )
6096{
6097 unsigned char *end = start + len;
6098 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6099
6100 /* Make a copy of the peer's raw public key. */
6101 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6102 ret = mbedtls_pk_parse_subpubkey( &start, end,
6103 &ssl->handshake->peer_pubkey );
6104 if( ret != 0 )
6105 {
6106 /* We should have parsed the public key before. */
6107 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6108 }
6109
6110 return( 0 );
6111}
6112#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6113
6114int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6115{
6116 int ret = 0;
6117 int crt_expected;
6118#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6119 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6120 ? ssl->handshake->sni_authmode
6121 : ssl->conf->authmode;
6122#else
6123 const int authmode = ssl->conf->authmode;
6124#endif
6125 void *rs_ctx = NULL;
6126 mbedtls_x509_crt *chain = NULL;
6127
6128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6129
6130 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6131 if( crt_expected == SSL_CERTIFICATE_SKIP )
6132 {
6133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6134 goto exit;
6135 }
6136
6137#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6138 if( ssl->handshake->ecrs_enabled &&
6139 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6140 {
6141 chain = ssl->handshake->ecrs_peer_cert;
6142 ssl->handshake->ecrs_peer_cert = NULL;
6143 goto crt_verify;
6144 }
6145#endif
6146
6147 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6148 {
6149 /* mbedtls_ssl_read_record may have sent an alert already. We
6150 let it decide whether to alert. */
6151 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6152 goto exit;
6153 }
6154
6155#if defined(MBEDTLS_SSL_SRV_C)
6156 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6157 {
6158 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6159
6160 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6161 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6162
6163 goto exit;
6164 }
6165#endif /* MBEDTLS_SSL_SRV_C */
6166
6167 /* Clear existing peer CRT structure in case we tried to
6168 * reuse a session but it failed, and allocate a new one. */
6169 ssl_clear_peer_cert( ssl->session_negotiate );
6170
6171 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6172 if( chain == NULL )
6173 {
6174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6175 sizeof( mbedtls_x509_crt ) ) );
6176 mbedtls_ssl_send_alert_message( ssl,
6177 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6178 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6179
6180 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6181 goto exit;
6182 }
6183 mbedtls_x509_crt_init( chain );
6184
6185 ret = ssl_parse_certificate_chain( ssl, chain );
6186 if( ret != 0 )
6187 goto exit;
6188
6189#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6190 if( ssl->handshake->ecrs_enabled)
6191 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6192
6193crt_verify:
6194 if( ssl->handshake->ecrs_enabled)
6195 rs_ctx = &ssl->handshake->ecrs_ctx;
6196#endif
6197
6198 ret = ssl_parse_certificate_verify( ssl, authmode,
6199 chain, rs_ctx );
6200 if( ret != 0 )
6201 goto exit;
6202
6203#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6204 {
6205 unsigned char *crt_start, *pk_start;
6206 size_t crt_len, pk_len;
6207
6208 /* We parse the CRT chain without copying, so
6209 * these pointers point into the input buffer,
6210 * and are hence still valid after freeing the
6211 * CRT chain. */
6212
6213 crt_start = chain->raw.p;
6214 crt_len = chain->raw.len;
6215
6216 pk_start = chain->pk_raw.p;
6217 pk_len = chain->pk_raw.len;
6218
6219 /* Free the CRT structures before computing
6220 * digest and copying the peer's public key. */
6221 mbedtls_x509_crt_free( chain );
6222 mbedtls_free( chain );
6223 chain = NULL;
6224
6225 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6226 if( ret != 0 )
6227 goto exit;
6228
6229 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6230 if( ret != 0 )
6231 goto exit;
6232 }
6233#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6234 /* Pass ownership to session structure. */
6235 ssl->session_negotiate->peer_cert = chain;
6236 chain = NULL;
6237#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6238
6239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6240
6241exit:
6242
6243 if( ret == 0 )
6244 ssl->state++;
6245
6246#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6247 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6248 {
6249 ssl->handshake->ecrs_peer_cert = chain;
6250 chain = NULL;
6251 }
6252#endif
6253
6254 if( chain != NULL )
6255 {
6256 mbedtls_x509_crt_free( chain );
6257 mbedtls_free( chain );
6258 }
6259
6260 return( ret );
6261}
6262#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6263
Jerry Yu615bd6f2022-02-17 14:25:15 +08006264#if defined(MBEDTLS_SHA256_C)
6265static void ssl_calc_finished_tls_sha256(
6266 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6267{
6268 int len = 12;
6269 const char *sender;
6270 unsigned char padbuf[32];
6271#if defined(MBEDTLS_USE_PSA_CRYPTO)
6272 size_t hash_size;
6273 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
6274 psa_status_t status;
6275#else
6276 mbedtls_sha256_context sha256;
6277#endif
6278
6279 mbedtls_ssl_session *session = ssl->session_negotiate;
6280 if( !session )
6281 session = ssl->session;
6282
6283 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6284 ? "client finished"
6285 : "server finished";
6286
6287#if defined(MBEDTLS_USE_PSA_CRYPTO)
6288 sha256_psa = psa_hash_operation_init();
6289
6290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6291
6292 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6293 if( status != PSA_SUCCESS )
6294 {
6295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6296 return;
6297 }
6298
6299 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6300 if( status != PSA_SUCCESS )
6301 {
6302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6303 return;
6304 }
6305 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6306#else
6307
6308 mbedtls_sha256_init( &sha256 );
6309
6310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
6311
6312 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6313
6314 /*
6315 * TLSv1.2:
6316 * hash = PRF( master, finished_label,
6317 * Hash( handshake ) )[0.11]
6318 */
6319
6320#if !defined(MBEDTLS_SHA256_ALT)
6321 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
6322 sha256.state, sizeof( sha256.state ) );
6323#endif
6324
6325 mbedtls_sha256_finish( &sha256, padbuf );
6326 mbedtls_sha256_free( &sha256 );
6327#endif /* MBEDTLS_USE_PSA_CRYPTO */
6328
6329 ssl->handshake->tls_prf( session->master, 48, sender,
6330 padbuf, 32, buf, len );
6331
6332 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6333
6334 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6335
6336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6337}
6338#endif /* MBEDTLS_SHA256_C */
6339
Jerry Yub7ba49e2022-02-17 14:25:53 +08006340
6341#if defined(MBEDTLS_SHA384_C)
6342
6343static void ssl_calc_finished_tls_sha384(
6344 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6345{
6346 int len = 12;
6347 const char *sender;
6348 unsigned char padbuf[48];
6349#if defined(MBEDTLS_USE_PSA_CRYPTO)
6350 size_t hash_size;
6351 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
6352 psa_status_t status;
6353#else
6354 mbedtls_sha512_context sha512;
6355#endif
6356
6357 mbedtls_ssl_session *session = ssl->session_negotiate;
6358 if( !session )
6359 session = ssl->session;
6360
6361 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6362 ? "client finished"
6363 : "server finished";
6364
6365#if defined(MBEDTLS_USE_PSA_CRYPTO)
6366 sha384_psa = psa_hash_operation_init();
6367
6368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6369
6370 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6371 if( status != PSA_SUCCESS )
6372 {
6373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6374 return;
6375 }
6376
6377 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
6378 if( status != PSA_SUCCESS )
6379 {
6380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6381 return;
6382 }
6383 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
6384#else
6385 mbedtls_sha512_init( &sha512 );
6386
6387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
6388
6389 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
6390
6391 /*
6392 * TLSv1.2:
6393 * hash = PRF( master, finished_label,
6394 * Hash( handshake ) )[0.11]
6395 */
6396
6397#if !defined(MBEDTLS_SHA512_ALT)
6398 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6399 sha512.state, sizeof( sha512.state ) );
6400#endif
6401 mbedtls_sha512_finish( &sha512, padbuf );
6402
6403 mbedtls_sha512_free( &sha512 );
6404#endif
6405
6406 ssl->handshake->tls_prf( session->master, 48, sender,
6407 padbuf, 48, buf, len );
6408
6409 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6410
6411 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6412
6413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6414}
6415#endif /* MBEDTLS_SHA384_C */
6416
Jerry Yuaef00152022-02-17 14:27:31 +08006417void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
6418{
6419 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
6420
6421 /*
6422 * Free our handshake params
6423 */
6424 mbedtls_ssl_handshake_free( ssl );
6425 mbedtls_free( ssl->handshake );
6426 ssl->handshake = NULL;
6427
6428 /*
6429 * Free the previous transform and swith in the current one
6430 */
6431 if( ssl->transform )
6432 {
6433 mbedtls_ssl_transform_free( ssl->transform );
6434 mbedtls_free( ssl->transform );
6435 }
6436 ssl->transform = ssl->transform_negotiate;
6437 ssl->transform_negotiate = NULL;
6438
6439 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
6440}
6441
Jerry Yu2a9fff52022-02-17 14:28:51 +08006442void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
6443{
6444 int resume = ssl->handshake->resume;
6445
6446 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
6447
6448#if defined(MBEDTLS_SSL_RENEGOTIATION)
6449 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6450 {
6451 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
6452 ssl->renego_records_seen = 0;
6453 }
6454#endif
6455
6456 /*
6457 * Free the previous session and switch in the current one
6458 */
6459 if( ssl->session )
6460 {
6461#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6462 /* RFC 7366 3.1: keep the EtM state */
6463 ssl->session_negotiate->encrypt_then_mac =
6464 ssl->session->encrypt_then_mac;
6465#endif
6466
6467 mbedtls_ssl_session_free( ssl->session );
6468 mbedtls_free( ssl->session );
6469 }
6470 ssl->session = ssl->session_negotiate;
6471 ssl->session_negotiate = NULL;
6472
6473 /*
6474 * Add cache entry
6475 */
6476 if( ssl->conf->f_set_cache != NULL &&
6477 ssl->session->id_len != 0 &&
6478 resume == 0 )
6479 {
6480 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
6481 ssl->session->id,
6482 ssl->session->id_len,
6483 ssl->session ) != 0 )
6484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
6485 }
6486
6487#if defined(MBEDTLS_SSL_PROTO_DTLS)
6488 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6489 ssl->handshake->flight != NULL )
6490 {
6491 /* Cancel handshake timer */
6492 mbedtls_ssl_set_timer( ssl, 0 );
6493
6494 /* Keep last flight around in case we need to resend it:
6495 * we need the handshake and transform structures for that */
6496 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
6497 }
6498 else
6499#endif
6500 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
6501
6502 ssl->state++;
6503
6504 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
6505}
6506
Jerry Yu3c8e47b2022-02-17 14:30:01 +08006507int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
6508{
6509 int ret, hash_len;
6510
6511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
6512
6513 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
6514
6515 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
6516
6517 /*
6518 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6519 * may define some other value. Currently (early 2016), no defined
6520 * ciphersuite does this (and this is unlikely to change as activity has
6521 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6522 */
6523 hash_len = 12;
6524
6525#if defined(MBEDTLS_SSL_RENEGOTIATION)
6526 ssl->verify_data_len = hash_len;
6527 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
6528#endif
6529
6530 ssl->out_msglen = 4 + hash_len;
6531 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6532 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
6533
6534 /*
6535 * In case of session resuming, invert the client and server
6536 * ChangeCipherSpec messages order.
6537 */
6538 if( ssl->handshake->resume != 0 )
6539 {
6540#if defined(MBEDTLS_SSL_CLI_C)
6541 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6542 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
6543#endif
6544#if defined(MBEDTLS_SSL_SRV_C)
6545 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6546 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
6547#endif
6548 }
6549 else
6550 ssl->state++;
6551
6552 /*
6553 * Switch to our negotiated transform and session parameters for outbound
6554 * data.
6555 */
6556 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
6557
6558#if defined(MBEDTLS_SSL_PROTO_DTLS)
6559 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6560 {
6561 unsigned char i;
6562
6563 /* Remember current epoch settings for resending */
6564 ssl->handshake->alt_transform_out = ssl->transform_out;
6565 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
6566 sizeof( ssl->handshake->alt_out_ctr ) );
6567
6568 /* Set sequence_number to zero */
6569 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
6570
6571
6572 /* Increment epoch */
6573 for( i = 2; i > 0; i-- )
6574 if( ++ssl->cur_out_ctr[i - 1] != 0 )
6575 break;
6576
6577 /* The loop goes to its end iff the counter is wrapping */
6578 if( i == 0 )
6579 {
6580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6581 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
6582 }
6583 }
6584 else
6585#endif /* MBEDTLS_SSL_PROTO_DTLS */
6586 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6587
6588 ssl->transform_out = ssl->transform_negotiate;
6589 ssl->session_out = ssl->session_negotiate;
6590
6591#if defined(MBEDTLS_SSL_PROTO_DTLS)
6592 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6593 mbedtls_ssl_send_flight_completed( ssl );
6594#endif
6595
6596 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6597 {
6598 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6599 return( ret );
6600 }
6601
6602#if defined(MBEDTLS_SSL_PROTO_DTLS)
6603 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6604 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6605 {
6606 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6607 return( ret );
6608 }
6609#endif
6610
6611 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
6612
6613 return( 0 );
6614}
6615
Jerry Yu0b3d7c12022-02-17 14:30:51 +08006616#define SSL_MAX_HASH_LEN 12
6617
6618int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
6619{
6620 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6621 unsigned int hash_len = 12;
6622 unsigned char buf[SSL_MAX_HASH_LEN];
6623
6624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
6625
6626 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
6627
6628 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6629 {
6630 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6631 goto exit;
6632 }
6633
6634 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6635 {
6636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6637 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6638 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6639 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
6640 goto exit;
6641 }
6642
6643 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
6644 {
6645 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6646 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6647 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
6648 goto exit;
6649 }
6650
6651 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
6652 {
6653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6654 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6655 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6656 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
6657 goto exit;
6658 }
6659
6660 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
6661 buf, hash_len ) != 0 )
6662 {
6663 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6664 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6665 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
6666 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
6667 goto exit;
6668 }
6669
6670#if defined(MBEDTLS_SSL_RENEGOTIATION)
6671 ssl->verify_data_len = hash_len;
6672 memcpy( ssl->peer_verify_data, buf, hash_len );
6673#endif
6674
6675 if( ssl->handshake->resume != 0 )
6676 {
6677#if defined(MBEDTLS_SSL_CLI_C)
6678 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6679 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
6680#endif
6681#if defined(MBEDTLS_SSL_SRV_C)
6682 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6683 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
6684#endif
6685 }
6686 else
6687 ssl->state++;
6688
6689#if defined(MBEDTLS_SSL_PROTO_DTLS)
6690 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6691 mbedtls_ssl_recv_flight_completed( ssl );
6692#endif
6693
6694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
6695
6696exit:
6697 mbedtls_platform_zeroize( buf, hash_len );
6698 return( ret );
6699}
6700
Jerry Yu392112c2022-02-17 14:34:10 +08006701#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
6702/*
6703 * Helper to get TLS 1.2 PRF from ciphersuite
6704 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
6705 */
6706static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
6707{
6708#if defined(MBEDTLS_SHA384_C)
6709 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6710 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
6711
6712 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
6713 return( tls_prf_sha384 );
6714#else
6715 (void) ciphersuite_id;
6716#endif
6717 return( tls_prf_sha256 );
6718}
6719#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08006720
6721static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
6722{
6723 ((void) tls_prf);
6724#if defined(MBEDTLS_SHA384_C)
6725 if( tls_prf == tls_prf_sha384 )
6726 {
6727 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
6728 }
6729 else
6730#endif
6731#if defined(MBEDTLS_SHA256_C)
6732 if( tls_prf == tls_prf_sha256 )
6733 {
6734 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
6735 }
6736 else
6737#endif
6738 return( MBEDTLS_SSL_TLS_PRF_NONE );
6739}
6740
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006741/*
6742 * Populate a transform structure with session keys and all the other
6743 * necessary information.
6744 *
6745 * Parameters:
6746 * - [in/out]: transform: structure to populate
6747 * [in] must be just initialised with mbedtls_ssl_transform_init()
6748 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
6749 * - [in] ciphersuite
6750 * - [in] master
6751 * - [in] encrypt_then_mac
6752 * - [in] compression
6753 * - [in] tls_prf: pointer to PRF to use for key derivation
6754 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
6755 * - [in] minor_ver: SSL/TLS minor version
6756 * - [in] endpoint: client or server
6757 * - [in] ssl: used for:
6758 * - ssl->conf->{f,p}_export_keys
6759 * [in] optionally used for:
6760 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
6761 */
6762static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
6763 int ciphersuite,
6764 const unsigned char master[48],
6765#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
6766 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6767 int encrypt_then_mac,
6768#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
6769 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
6770 ssl_tls_prf_t tls_prf,
6771 const unsigned char randbytes[64],
6772 int minor_ver,
6773 unsigned endpoint,
6774 const mbedtls_ssl_context *ssl )
6775{
6776 int ret = 0;
6777 unsigned char keyblk[256];
6778 unsigned char *key1;
6779 unsigned char *key2;
6780 unsigned char *mac_enc;
6781 unsigned char *mac_dec;
6782 size_t mac_key_len = 0;
6783 size_t iv_copy_len;
6784 size_t keylen;
6785 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
6786 const mbedtls_cipher_info_t *cipher_info;
6787 const mbedtls_md_info_t *md_info;
6788
6789#if defined(MBEDTLS_USE_PSA_CRYPTO)
6790 psa_key_type_t key_type;
6791 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6792 psa_algorithm_t alg;
6793 size_t key_bits;
6794 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
6795#endif
6796
6797#if !defined(MBEDTLS_DEBUG_C) && \
6798 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6799 if( ssl->f_export_keys == NULL )
6800 {
6801 ssl = NULL; /* make sure we don't use it except for these cases */
6802 (void) ssl;
6803 }
6804#endif
6805
6806 /*
6807 * Some data just needs copying into the structure
6808 */
6809#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
6810 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
6811 transform->encrypt_then_mac = encrypt_then_mac;
6812#endif
6813 transform->minor_ver = minor_ver;
6814
6815#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
6816 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
6817#endif
6818
6819#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
6820 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
6821 {
6822 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
6823 * generation separate. This should never happen. */
6824 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6825 }
6826#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
6827
6828 /*
6829 * Get various info structures
6830 */
6831 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
6832 if( ciphersuite_info == NULL )
6833 {
6834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
6835 ciphersuite ) );
6836 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6837 }
6838
6839 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
6840 if( cipher_info == NULL )
6841 {
6842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
6843 ciphersuite_info->cipher ) );
6844 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6845 }
6846
6847 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
6848 if( md_info == NULL )
6849 {
6850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
6851 (unsigned) ciphersuite_info->mac ) );
6852 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6853 }
6854
6855#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6856 /* Copy own and peer's CID if the use of the CID
6857 * extension has been negotiated. */
6858 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
6859 {
6860 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
6861
6862 transform->in_cid_len = ssl->own_cid_len;
6863 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
6864 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
6865 transform->in_cid_len );
6866
6867 transform->out_cid_len = ssl->handshake->peer_cid_len;
6868 memcpy( transform->out_cid, ssl->handshake->peer_cid,
6869 ssl->handshake->peer_cid_len );
6870 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
6871 transform->out_cid_len );
6872 }
6873#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6874
6875 /*
6876 * Compute key block using the PRF
6877 */
6878 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
6879 if( ret != 0 )
6880 {
6881 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
6882 return( ret );
6883 }
6884
6885 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
6886 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
6887 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
6888 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
6889 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
6890
6891 /*
6892 * Determine the appropriate key, IV and MAC length.
6893 */
6894
6895 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
6896
6897#if defined(MBEDTLS_GCM_C) || \
6898 defined(MBEDTLS_CCM_C) || \
6899 defined(MBEDTLS_CHACHAPOLY_C)
6900 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_GCM ||
6901 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CCM ||
6902 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
6903 {
6904 size_t explicit_ivlen;
6905
6906 transform->maclen = 0;
6907 mac_key_len = 0;
6908 transform->taglen =
6909 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
6910
6911 /* All modes haves 96-bit IVs, but the length of the static parts vary
6912 * with mode and version:
6913 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
6914 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
6915 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
6916 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
6917 * sequence number).
6918 */
6919 transform->ivlen = 12;
6920 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
6921 transform->fixed_ivlen = 12;
6922 else
6923 transform->fixed_ivlen = 4;
6924
6925 /* Minimum length of encrypted record */
6926 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
6927 transform->minlen = explicit_ivlen + transform->taglen;
6928 }
6929 else
6930#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
6931#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
6932 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM ||
6933 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
6934 {
Neil Armstrongcf8841a2022-02-24 11:17:45 +01006935#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006936 /* Initialize HMAC contexts */
6937 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
6938 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
6939 {
6940 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
6941 goto end;
6942 }
Neil Armstrongcf8841a2022-02-24 11:17:45 +01006943#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006944
6945 /* Get MAC length */
6946 mac_key_len = mbedtls_md_get_size( md_info );
6947 transform->maclen = mac_key_len;
6948
6949 /* IV length */
6950 transform->ivlen = cipher_info->iv_size;
6951
6952 /* Minimum length */
6953 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM )
6954 transform->minlen = transform->maclen;
6955 else
6956 {
6957 /*
6958 * GenericBlockCipher:
6959 * 1. if EtM is in use: one block plus MAC
6960 * otherwise: * first multiple of blocklen greater than maclen
6961 * 2. IV
6962 */
6963#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6964 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
6965 {
6966 transform->minlen = transform->maclen
6967 + cipher_info->block_size;
6968 }
6969 else
6970#endif
6971 {
6972 transform->minlen = transform->maclen
6973 + cipher_info->block_size
6974 - transform->maclen % cipher_info->block_size;
6975 }
6976
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006977 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
6978 {
6979 transform->minlen += transform->ivlen;
6980 }
6981 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006982 {
6983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6984 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6985 goto end;
6986 }
6987 }
6988 }
6989 else
6990#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
6991 {
6992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6993 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6994 }
6995
6996 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
6997 (unsigned) keylen,
6998 (unsigned) transform->minlen,
6999 (unsigned) transform->ivlen,
7000 (unsigned) transform->maclen ) );
7001
7002 /*
7003 * Finally setup the cipher contexts, IVs and MAC secrets.
7004 */
7005#if defined(MBEDTLS_SSL_CLI_C)
7006 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7007 {
7008 key1 = keyblk + mac_key_len * 2;
7009 key2 = keyblk + mac_key_len * 2 + keylen;
7010
7011 mac_enc = keyblk;
7012 mac_dec = keyblk + mac_key_len;
7013
7014 /*
7015 * This is not used in TLS v1.1.
7016 */
7017 iv_copy_len = ( transform->fixed_ivlen ) ?
7018 transform->fixed_ivlen : transform->ivlen;
7019 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7020 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7021 iv_copy_len );
7022 }
7023 else
7024#endif /* MBEDTLS_SSL_CLI_C */
7025#if defined(MBEDTLS_SSL_SRV_C)
7026 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7027 {
7028 key1 = keyblk + mac_key_len * 2 + keylen;
7029 key2 = keyblk + mac_key_len * 2;
7030
7031 mac_enc = keyblk + mac_key_len;
7032 mac_dec = keyblk;
7033
7034 /*
7035 * This is not used in TLS v1.1.
7036 */
7037 iv_copy_len = ( transform->fixed_ivlen ) ?
7038 transform->fixed_ivlen : transform->ivlen;
7039 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7040 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7041 iv_copy_len );
7042 }
7043 else
7044#endif /* MBEDTLS_SSL_SRV_C */
7045 {
7046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7047 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7048 goto end;
7049 }
7050
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007051 if( ssl != NULL && ssl->f_export_keys != NULL )
7052 {
7053 ssl->f_export_keys( ssl->p_export_keys,
7054 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7055 master, 48,
7056 randbytes + 32,
7057 randbytes,
7058 tls_prf_get_type( tls_prf ) );
7059 }
7060
7061#if defined(MBEDTLS_USE_PSA_CRYPTO)
7062 if( ( status = mbedtls_ssl_cipher_to_psa( cipher_info->type,
7063 transform->taglen,
7064 &alg,
7065 &key_type,
7066 &key_bits ) ) != PSA_SUCCESS )
7067 {
7068 ret = psa_ssl_status_to_mbedtls( status );
7069 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7070 goto end;
7071 }
7072
7073 transform->psa_alg = alg;
7074
7075 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7076 {
7077 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7078 psa_set_key_algorithm( &attributes, alg );
7079 psa_set_key_type( &attributes, key_type );
7080
7081 if( ( status = psa_import_key( &attributes,
7082 key1,
7083 PSA_BITS_TO_BYTES( key_bits ),
7084 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7085 {
7086 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7087 ret = psa_ssl_status_to_mbedtls( status );
7088 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7089 goto end;
7090 }
7091
7092 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7093
7094 if( ( status = psa_import_key( &attributes,
7095 key2,
7096 PSA_BITS_TO_BYTES( key_bits ),
7097 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7098 {
7099 ret = psa_ssl_status_to_mbedtls( status );
7100 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7101 goto end;
7102 }
7103 }
7104#else
7105 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7106 cipher_info ) ) != 0 )
7107 {
7108 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7109 goto end;
7110 }
7111
7112 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7113 cipher_info ) ) != 0 )
7114 {
7115 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7116 goto end;
7117 }
7118
7119 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7120 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7121 MBEDTLS_ENCRYPT ) ) != 0 )
7122 {
7123 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7124 goto end;
7125 }
7126
7127 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7128 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7129 MBEDTLS_DECRYPT ) ) != 0 )
7130 {
7131 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7132 goto end;
7133 }
7134
7135#if defined(MBEDTLS_CIPHER_MODE_CBC)
7136 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7137 {
7138 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7139 MBEDTLS_PADDING_NONE ) ) != 0 )
7140 {
7141 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7142 goto end;
7143 }
7144
7145 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7146 MBEDTLS_PADDING_NONE ) ) != 0 )
7147 {
7148 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7149 goto end;
7150 }
7151 }
7152#endif /* MBEDTLS_CIPHER_MODE_CBC */
7153#endif /* MBEDTLS_USE_PSA_CRYPTO */
7154
Neil Armstrong29c0c042022-03-17 17:47:28 +01007155#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7156 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7157 For AEAD-based ciphersuites, there is nothing to do here. */
7158 if( mac_key_len != 0 )
7159 {
7160#if defined(MBEDTLS_USE_PSA_CRYPTO)
7161 alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
7162 if( alg == 0 )
7163 {
7164 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7165 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_md_type_to_psa", ret );
7166 goto end;
7167 }
7168
7169 transform->psa_mac_alg = PSA_ALG_HMAC( alg );
7170
7171 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
7172 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( alg ) );
7173 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7174
7175 if( ( status = psa_import_key( &attributes,
7176 mac_enc, mac_key_len,
7177 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7178 {
7179 ret = psa_ssl_status_to_mbedtls( status );
7180 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7181 goto end;
7182 }
7183
Ronald Cronfb39f152022-03-25 14:36:28 +01007184 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
7185 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) &&
7186 ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007187 /* mbedtls_ct_hmac() requires the key to be exportable */
7188 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7189 PSA_KEY_USAGE_VERIFY_HASH );
7190 else
7191 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7192
7193 if( ( status = psa_import_key( &attributes,
7194 mac_dec, mac_key_len,
7195 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7196 {
7197 ret = psa_ssl_status_to_mbedtls( status );
7198 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7199 goto end;
7200 }
7201#else
7202 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7203 if( ret != 0 )
7204 goto end;
7205 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7206 if( ret != 0 )
7207 goto end;
7208#endif /* MBEDTLS_USE_PSA_CRYPTO */
7209 }
7210#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7211
7212 ((void) mac_dec);
7213 ((void) mac_enc);
7214
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007215end:
7216 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7217 return( ret );
7218}
7219
Jerry Yuee40f9d2022-02-17 14:55:16 +08007220#if defined(MBEDTLS_USE_PSA_CRYPTO)
7221int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7222 unsigned char *hash, size_t *hashlen,
7223 unsigned char *data, size_t data_len,
7224 mbedtls_md_type_t md_alg )
7225{
7226 psa_status_t status;
7227 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
7228 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
7229
7230 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7231
7232 if( ( status = psa_hash_setup( &hash_operation,
7233 hash_alg ) ) != PSA_SUCCESS )
7234 {
7235 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7236 goto exit;
7237 }
7238
7239 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
7240 64 ) ) != PSA_SUCCESS )
7241 {
7242 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7243 goto exit;
7244 }
7245
7246 if( ( status = psa_hash_update( &hash_operation,
7247 data, data_len ) ) != PSA_SUCCESS )
7248 {
7249 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7250 goto exit;
7251 }
7252
7253 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
7254 hashlen ) ) != PSA_SUCCESS )
7255 {
7256 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
7257 goto exit;
7258 }
7259
7260exit:
7261 if( status != PSA_SUCCESS )
7262 {
7263 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7264 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7265 switch( status )
7266 {
7267 case PSA_ERROR_NOT_SUPPORTED:
7268 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
7269 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
7270 case PSA_ERROR_BUFFER_TOO_SMALL:
7271 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
7272 case PSA_ERROR_INSUFFICIENT_MEMORY:
7273 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
7274 default:
7275 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
7276 }
7277 }
7278 return( 0 );
7279}
7280
7281#else
7282
7283int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7284 unsigned char *hash, size_t *hashlen,
7285 unsigned char *data, size_t data_len,
7286 mbedtls_md_type_t md_alg )
7287{
7288 int ret = 0;
7289 mbedtls_md_context_t ctx;
7290 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
7291 *hashlen = mbedtls_md_get_size( md_info );
7292
7293 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
7294
7295 mbedtls_md_init( &ctx );
7296
7297 /*
7298 * digitally-signed struct {
7299 * opaque client_random[32];
7300 * opaque server_random[32];
7301 * ServerDHParams params;
7302 * };
7303 */
7304 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
7305 {
7306 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7307 goto exit;
7308 }
7309 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
7310 {
7311 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
7312 goto exit;
7313 }
7314 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
7315 {
7316 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7317 goto exit;
7318 }
7319 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
7320 {
7321 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7322 goto exit;
7323 }
7324 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
7325 {
7326 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
7327 goto exit;
7328 }
7329
7330exit:
7331 mbedtls_md_free( &ctx );
7332
7333 if( ret != 0 )
7334 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7335 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7336
7337 return( ret );
7338}
7339#endif /* MBEDTLS_USE_PSA_CRYPTO */
7340
Jerry Yud9d91da2022-02-17 14:57:06 +08007341#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
7342
7343/* Find an entry in a signature-hash set matching a given hash algorithm. */
7344mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
7345 mbedtls_pk_type_t sig_alg )
7346{
7347 switch( sig_alg )
7348 {
7349 case MBEDTLS_PK_RSA:
7350 return( set->rsa );
7351 case MBEDTLS_PK_ECDSA:
7352 return( set->ecdsa );
7353 default:
7354 return( MBEDTLS_MD_NONE );
7355 }
7356}
7357
7358/* Add a signature-hash-pair to a signature-hash set */
7359void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
7360 mbedtls_pk_type_t sig_alg,
7361 mbedtls_md_type_t md_alg )
7362{
7363 switch( sig_alg )
7364 {
7365 case MBEDTLS_PK_RSA:
7366 if( set->rsa == MBEDTLS_MD_NONE )
7367 set->rsa = md_alg;
7368 break;
7369
7370 case MBEDTLS_PK_ECDSA:
7371 if( set->ecdsa == MBEDTLS_MD_NONE )
7372 set->ecdsa = md_alg;
7373 break;
7374
7375 default:
7376 break;
7377 }
7378}
7379
7380/* Allow exactly one hash algorithm for each signature. */
7381void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
7382 mbedtls_md_type_t md_alg )
7383{
7384 set->rsa = md_alg;
7385 set->ecdsa = md_alg;
7386}
7387
7388#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7389
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08007390/* Serialization of TLS 1.2 sessions:
7391 *
7392 * struct {
7393 * uint64 start_time;
7394 * uint8 ciphersuite[2]; // defined by the standard
7395 * uint8 compression; // 0 or 1
7396 * uint8 session_id_len; // at most 32
7397 * opaque session_id[32];
7398 * opaque master[48]; // fixed length in the standard
7399 * uint32 verify_result;
7400 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
7401 * opaque ticket<0..2^24-1>; // length 0 means no ticket
7402 * uint32 ticket_lifetime;
7403 * uint8 mfl_code; // up to 255 according to standard
7404 * uint8 encrypt_then_mac; // 0 or 1
7405 * } serialized_session_tls12;
7406 *
7407 */
7408static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session,
7409 unsigned char *buf,
7410 size_t buf_len )
7411{
7412 unsigned char *p = buf;
7413 size_t used = 0;
7414
7415#if defined(MBEDTLS_HAVE_TIME)
7416 uint64_t start;
7417#endif
7418#if defined(MBEDTLS_X509_CRT_PARSE_C)
7419#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7420 size_t cert_len;
7421#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7422#endif /* MBEDTLS_X509_CRT_PARSE_C */
7423
7424 /*
7425 * Time
7426 */
7427#if defined(MBEDTLS_HAVE_TIME)
7428 used += 8;
7429
7430 if( used <= buf_len )
7431 {
7432 start = (uint64_t) session->start;
7433
7434 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
7435 p += 8;
7436 }
7437#endif /* MBEDTLS_HAVE_TIME */
7438
7439 /*
7440 * Basic mandatory fields
7441 */
7442 used += 2 /* ciphersuite */
7443 + 1 /* compression */
7444 + 1 /* id_len */
7445 + sizeof( session->id )
7446 + sizeof( session->master )
7447 + 4; /* verify_result */
7448
7449 if( used <= buf_len )
7450 {
7451 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
7452 p += 2;
7453
7454 *p++ = MBEDTLS_BYTE_0( session->compression );
7455
7456 *p++ = MBEDTLS_BYTE_0( session->id_len );
7457 memcpy( p, session->id, 32 );
7458 p += 32;
7459
7460 memcpy( p, session->master, 48 );
7461 p += 48;
7462
7463 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
7464 p += 4;
7465 }
7466
7467 /*
7468 * Peer's end-entity certificate
7469 */
7470#if defined(MBEDTLS_X509_CRT_PARSE_C)
7471#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7472 if( session->peer_cert == NULL )
7473 cert_len = 0;
7474 else
7475 cert_len = session->peer_cert->raw.len;
7476
7477 used += 3 + cert_len;
7478
7479 if( used <= buf_len )
7480 {
7481 *p++ = MBEDTLS_BYTE_2( cert_len );
7482 *p++ = MBEDTLS_BYTE_1( cert_len );
7483 *p++ = MBEDTLS_BYTE_0( cert_len );
7484
7485 if( session->peer_cert != NULL )
7486 {
7487 memcpy( p, session->peer_cert->raw.p, cert_len );
7488 p += cert_len;
7489 }
7490 }
7491#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7492 if( session->peer_cert_digest != NULL )
7493 {
7494 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
7495 if( used <= buf_len )
7496 {
7497 *p++ = (unsigned char) session->peer_cert_digest_type;
7498 *p++ = (unsigned char) session->peer_cert_digest_len;
7499 memcpy( p, session->peer_cert_digest,
7500 session->peer_cert_digest_len );
7501 p += session->peer_cert_digest_len;
7502 }
7503 }
7504 else
7505 {
7506 used += 2;
7507 if( used <= buf_len )
7508 {
7509 *p++ = (unsigned char) MBEDTLS_MD_NONE;
7510 *p++ = 0;
7511 }
7512 }
7513#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7514#endif /* MBEDTLS_X509_CRT_PARSE_C */
7515
7516 /*
7517 * Session ticket if any, plus associated data
7518 */
7519#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7520 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
7521
7522 if( used <= buf_len )
7523 {
7524 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
7525 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
7526 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
7527
7528 if( session->ticket != NULL )
7529 {
7530 memcpy( p, session->ticket, session->ticket_len );
7531 p += session->ticket_len;
7532 }
7533
7534 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
7535 p += 4;
7536 }
7537#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7538
7539 /*
7540 * Misc extension-related info
7541 */
7542#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7543 used += 1;
7544
7545 if( used <= buf_len )
7546 *p++ = session->mfl_code;
7547#endif
7548
7549#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7550 used += 1;
7551
7552 if( used <= buf_len )
7553 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
7554#endif
7555
7556 return( used );
7557}
7558
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08007559static int ssl_session_load_tls12( mbedtls_ssl_session *session,
7560 const unsigned char *buf,
7561 size_t len )
7562{
7563#if defined(MBEDTLS_HAVE_TIME)
7564 uint64_t start;
7565#endif
7566#if defined(MBEDTLS_X509_CRT_PARSE_C)
7567#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7568 size_t cert_len;
7569#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7570#endif /* MBEDTLS_X509_CRT_PARSE_C */
7571
7572 const unsigned char *p = buf;
7573 const unsigned char * const end = buf + len;
7574
7575 /*
7576 * Time
7577 */
7578#if defined(MBEDTLS_HAVE_TIME)
7579 if( 8 > (size_t)( end - p ) )
7580 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7581
7582 start = ( (uint64_t) p[0] << 56 ) |
7583 ( (uint64_t) p[1] << 48 ) |
7584 ( (uint64_t) p[2] << 40 ) |
7585 ( (uint64_t) p[3] << 32 ) |
7586 ( (uint64_t) p[4] << 24 ) |
7587 ( (uint64_t) p[5] << 16 ) |
7588 ( (uint64_t) p[6] << 8 ) |
7589 ( (uint64_t) p[7] );
7590 p += 8;
7591
7592 session->start = (time_t) start;
7593#endif /* MBEDTLS_HAVE_TIME */
7594
7595 /*
7596 * Basic mandatory fields
7597 */
7598 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
7599 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7600
7601 session->ciphersuite = ( p[0] << 8 ) | p[1];
7602 p += 2;
7603
7604 session->compression = *p++;
7605
7606 session->id_len = *p++;
7607 memcpy( session->id, p, 32 );
7608 p += 32;
7609
7610 memcpy( session->master, p, 48 );
7611 p += 48;
7612
7613 session->verify_result = ( (uint32_t) p[0] << 24 ) |
7614 ( (uint32_t) p[1] << 16 ) |
7615 ( (uint32_t) p[2] << 8 ) |
7616 ( (uint32_t) p[3] );
7617 p += 4;
7618
7619 /* Immediately clear invalid pointer values that have been read, in case
7620 * we exit early before we replaced them with valid ones. */
7621#if defined(MBEDTLS_X509_CRT_PARSE_C)
7622#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7623 session->peer_cert = NULL;
7624#else
7625 session->peer_cert_digest = NULL;
7626#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7627#endif /* MBEDTLS_X509_CRT_PARSE_C */
7628#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7629 session->ticket = NULL;
7630#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7631
7632 /*
7633 * Peer certificate
7634 */
7635#if defined(MBEDTLS_X509_CRT_PARSE_C)
7636#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7637 /* Deserialize CRT from the end of the ticket. */
7638 if( 3 > (size_t)( end - p ) )
7639 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7640
7641 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
7642 p += 3;
7643
7644 if( cert_len != 0 )
7645 {
7646 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7647
7648 if( cert_len > (size_t)( end - p ) )
7649 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7650
7651 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7652
7653 if( session->peer_cert == NULL )
7654 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7655
7656 mbedtls_x509_crt_init( session->peer_cert );
7657
7658 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
7659 p, cert_len ) ) != 0 )
7660 {
7661 mbedtls_x509_crt_free( session->peer_cert );
7662 mbedtls_free( session->peer_cert );
7663 session->peer_cert = NULL;
7664 return( ret );
7665 }
7666
7667 p += cert_len;
7668 }
7669#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7670 /* Deserialize CRT digest from the end of the ticket. */
7671 if( 2 > (size_t)( end - p ) )
7672 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7673
7674 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
7675 session->peer_cert_digest_len = (size_t) *p++;
7676
7677 if( session->peer_cert_digest_len != 0 )
7678 {
7679 const mbedtls_md_info_t *md_info =
7680 mbedtls_md_info_from_type( session->peer_cert_digest_type );
7681 if( md_info == NULL )
7682 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7683 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
7684 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7685
7686 if( session->peer_cert_digest_len > (size_t)( end - p ) )
7687 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7688
7689 session->peer_cert_digest =
7690 mbedtls_calloc( 1, session->peer_cert_digest_len );
7691 if( session->peer_cert_digest == NULL )
7692 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7693
7694 memcpy( session->peer_cert_digest, p,
7695 session->peer_cert_digest_len );
7696 p += session->peer_cert_digest_len;
7697 }
7698#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7699#endif /* MBEDTLS_X509_CRT_PARSE_C */
7700
7701 /*
7702 * Session ticket and associated data
7703 */
7704#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7705 if( 3 > (size_t)( end - p ) )
7706 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7707
7708 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
7709 p += 3;
7710
7711 if( session->ticket_len != 0 )
7712 {
7713 if( session->ticket_len > (size_t)( end - p ) )
7714 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7715
7716 session->ticket = mbedtls_calloc( 1, session->ticket_len );
7717 if( session->ticket == NULL )
7718 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7719
7720 memcpy( session->ticket, p, session->ticket_len );
7721 p += session->ticket_len;
7722 }
7723
7724 if( 4 > (size_t)( end - p ) )
7725 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7726
7727 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
7728 ( (uint32_t) p[1] << 16 ) |
7729 ( (uint32_t) p[2] << 8 ) |
7730 ( (uint32_t) p[3] );
7731 p += 4;
7732#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7733
7734 /*
7735 * Misc extension-related info
7736 */
7737#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7738 if( 1 > (size_t)( end - p ) )
7739 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7740
7741 session->mfl_code = *p++;
7742#endif
7743
7744#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7745 if( 1 > (size_t)( end - p ) )
7746 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7747
7748 session->encrypt_then_mac = *p++;
7749#endif
7750
7751 /* Done, should have consumed entire buffer */
7752 if( p != end )
7753 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7754
7755 return( 0 );
7756}
Jerry Yudc7bd172022-02-17 13:44:15 +08007757#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007759#endif /* MBEDTLS_SSL_TLS_C */