blob: 211d23a64244ccf33a396fa102e4eef6f67fd8e6 [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 {
Ronald Crone1d3f062022-02-10 14:50:54 +0100914 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
915 {
916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
917 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
918 }
919 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
920 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +0800921 }
922#endif
923
924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
925 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
926}
927
928static int ssl_conf_check(const mbedtls_ssl_context *ssl)
929{
930 int ret;
931 ret = ssl_conf_version_check( ssl );
932 if( ret != 0 )
933 return( ret );
934
935 /* Space for further checks */
936
937 return( 0 );
938}
939
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200940/*
941 * Setup an SSL context
942 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100943
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200944int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +0200945 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +0000946{
Janos Follath865b3eb2019-12-16 11:46:15 +0000947 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +0000948 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
949 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +0000950
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200951 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +0000952
Jerry Yu60835a82021-08-04 10:13:52 +0800953 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
954 return( ret );
955
Paul Bakker62f2dee2012-09-28 07:31:51 +0000956 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +0100957 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +0000958 */
k-stachowiakc9a5f022018-07-24 13:53:31 +0200959
960 /* Set to NULL in case of an error condition */
961 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +0200962
Darryl Greenb33cc762019-11-28 14:29:44 +0000963#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
964 ssl->in_buf_len = in_buf_len;
965#endif
966 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +1000967 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000968 {
Paul Elliottd48d5c62021-01-07 14:47:05 +0000969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +0200970 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +0200971 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +1000972 }
973
Darryl Greenb33cc762019-11-28 14:29:44 +0000974#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
975 ssl->out_buf_len = out_buf_len;
976#endif
977 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +1000978 if( ssl->out_buf == NULL )
979 {
Paul Elliottd48d5c62021-01-07 14:47:05 +0000980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +0200981 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +0200982 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +0000983 }
984
Hanno Becker3e6f8ab2020-02-05 10:40:57 +0000985 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200986
Johan Pascalb62bb512015-12-03 21:56:45 +0100987#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +0200988 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +0100989#endif
990
Paul Bakker48916f92012-09-16 19:57:18 +0000991 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +0200992 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +0000993
994 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +0200995
996error:
997 mbedtls_free( ssl->in_buf );
998 mbedtls_free( ssl->out_buf );
999
1000 ssl->conf = NULL;
1001
Darryl Greenb33cc762019-11-28 14:29:44 +00001002#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1003 ssl->in_buf_len = 0;
1004 ssl->out_buf_len = 0;
1005#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001006 ssl->in_buf = NULL;
1007 ssl->out_buf = NULL;
1008
1009 ssl->in_hdr = NULL;
1010 ssl->in_ctr = NULL;
1011 ssl->in_len = NULL;
1012 ssl->in_iv = NULL;
1013 ssl->in_msg = NULL;
1014
1015 ssl->out_hdr = NULL;
1016 ssl->out_ctr = NULL;
1017 ssl->out_len = NULL;
1018 ssl->out_iv = NULL;
1019 ssl->out_msg = NULL;
1020
k-stachowiak9f7798e2018-07-31 16:52:32 +02001021 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001022}
1023
1024/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001025 * Reset an initialized and used SSL context for re-use while retaining
1026 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001027 *
1028 * If partial is non-zero, keep data in the input buffer and client ID.
1029 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001030 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001031void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1032 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001033{
Darryl Greenb33cc762019-11-28 14:29:44 +00001034#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1035 size_t in_buf_len = ssl->in_buf_len;
1036 size_t out_buf_len = ssl->out_buf_len;
1037#else
1038 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1039 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1040#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001041
Hanno Beckerb0302c42021-08-03 09:39:42 +01001042#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1043 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001044#endif
1045
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001046 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001047 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001048
Hanno Beckerb0302c42021-08-03 09:39:42 +01001049 mbedtls_ssl_reset_in_out_pointers( ssl );
1050
1051 /* Reset incoming message parsing */
1052 ssl->in_offt = NULL;
1053 ssl->nb_zero = 0;
1054 ssl->in_msgtype = 0;
1055 ssl->in_msglen = 0;
1056 ssl->in_hslen = 0;
1057 ssl->keep_current_message = 0;
1058 ssl->transform_in = NULL;
1059
1060#if defined(MBEDTLS_SSL_PROTO_DTLS)
1061 ssl->next_record_offset = 0;
1062 ssl->in_epoch = 0;
1063#endif
1064
1065 /* Keep current datagram if partial == 1 */
1066 if( partial == 0 )
1067 {
1068 ssl->in_left = 0;
1069 memset( ssl->in_buf, 0, in_buf_len );
1070 }
1071
1072 /* Reset outgoing message writing */
1073 ssl->out_msgtype = 0;
1074 ssl->out_msglen = 0;
1075 ssl->out_left = 0;
1076 memset( ssl->out_buf, 0, out_buf_len );
1077 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1078 ssl->transform_out = NULL;
1079
1080#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1081 mbedtls_ssl_dtls_replay_reset( ssl );
1082#endif
1083
XiaokangQian2b01dc32022-01-21 02:53:13 +00001084#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001085 if( ssl->transform )
1086 {
1087 mbedtls_ssl_transform_free( ssl->transform );
1088 mbedtls_free( ssl->transform );
1089 ssl->transform = NULL;
1090 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001091#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1092
XiaokangQian2b01dc32022-01-21 02:53:13 +00001093#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1094 mbedtls_ssl_transform_free( ssl->transform_application );
1095 mbedtls_free( ssl->transform_application );
1096 ssl->transform_application = NULL;
1097
1098 if( ssl->handshake != NULL )
1099 {
1100 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1101 mbedtls_free( ssl->handshake->transform_earlydata );
1102 ssl->handshake->transform_earlydata = NULL;
1103
1104 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1105 mbedtls_free( ssl->handshake->transform_handshake );
1106 ssl->handshake->transform_handshake = NULL;
1107 }
1108
XiaokangQian2b01dc32022-01-21 02:53:13 +00001109#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001110}
1111
1112int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1113{
1114 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1115
1116 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1117
XiaokangQian78b1fa72022-01-19 06:56:30 +00001118 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001119
1120 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121#if defined(MBEDTLS_SSL_RENEGOTIATION)
1122 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001123 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001124
1125 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1127 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001128#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001130
Hanno Beckerb0302c42021-08-03 09:39:42 +01001131 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001132 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001133 if( ssl->session )
1134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 mbedtls_ssl_session_free( ssl->session );
1136 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001137 ssl->session = NULL;
1138 }
1139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001141 ssl->alpn_chosen = NULL;
1142#endif
1143
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001144#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01001145#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001146 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01001147#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001148 {
1149 mbedtls_free( ssl->cli_id );
1150 ssl->cli_id = NULL;
1151 ssl->cli_id_len = 0;
1152 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001153#endif
1154
Paul Bakker48916f92012-09-16 19:57:18 +00001155 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1156 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001157
1158 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001159}
1160
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001161/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001162 * Reset an initialized and used SSL context for re-use while retaining
1163 * all application-set variables, function pointers and data.
1164 */
1165int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1166{
Hanno Becker43aefe22020-02-05 10:44:56 +00001167 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001168}
1169
1170/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001171 * SSL set accessors
1172 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001173void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001174{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001175 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001176}
1177
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001178void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001179{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001180 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001181}
1182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001184void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001185{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001186 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001187}
1188#endif
1189
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001190void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001191{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001192 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001193}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001196
Hanno Becker1841b0a2018-08-24 11:13:57 +01001197void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1198 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001199{
1200 ssl->disable_datagram_packing = !allow_packing;
1201}
1202
1203void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1204 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001205{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001206 conf->hs_timeout_min = min;
1207 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001208}
1209#endif
1210
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001211void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001212{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001213 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001214}
1215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001217void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001218 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001219 void *p_vrfy )
1220{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001221 conf->f_vrfy = f_vrfy;
1222 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001223}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001225
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001226void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001227 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001228 void *p_rng )
1229{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001230 conf->f_rng = f_rng;
1231 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001232}
1233
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001234void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001235 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001236 void *p_dbg )
1237{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001238 conf->f_dbg = f_dbg;
1239 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001240}
1241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001243 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001244 mbedtls_ssl_send_t *f_send,
1245 mbedtls_ssl_recv_t *f_recv,
1246 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001247{
1248 ssl->p_bio = p_bio;
1249 ssl->f_send = f_send;
1250 ssl->f_recv = f_recv;
1251 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001252}
1253
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001254#if defined(MBEDTLS_SSL_PROTO_DTLS)
1255void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1256{
1257 ssl->mtu = mtu;
1258}
1259#endif
1260
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001261void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001262{
1263 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001264}
1265
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001266void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1267 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001268 mbedtls_ssl_set_timer_t *f_set_timer,
1269 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001270{
1271 ssl->p_timer = p_timer;
1272 ssl->f_set_timer = f_set_timer;
1273 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001274
1275 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001276 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001277}
1278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279#if defined(MBEDTLS_SSL_SRV_C)
Glenn Strauss2ed95272022-01-21 18:02:17 -05001280void mbedtls_ssl_conf_cert_cb( mbedtls_ssl_config *conf,
1281 int (*f_cert_cb)(mbedtls_ssl_context *) )
1282{
1283 conf->f_cert_cb = f_cert_cb;
1284}
1285#endif /* MBEDTLS_SSL_SRV_C */
1286
1287#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001288void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001289 void *p_cache,
1290 mbedtls_ssl_cache_get_t *f_get_cache,
1291 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001292{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001293 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001294 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001295 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001296}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299#if defined(MBEDTLS_SSL_CLI_C)
1300int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001301{
Janos Follath865b3eb2019-12-16 11:46:15 +00001302 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001303
1304 if( ssl == NULL ||
1305 session == NULL ||
1306 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001307 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001310 }
1311
Hanno Beckere810bbc2021-05-14 16:01:05 +01001312 if( ssl->handshake->resume == 1 )
1313 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1314
Hanno Becker52055ae2019-02-06 14:30:46 +00001315 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1316 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001317 return( ret );
1318
Paul Bakker0a597072012-09-25 21:55:46 +00001319 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001320
1321 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001322}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001324
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001325void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1326 const int *ciphersuites )
1327{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001328 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001329}
1330
Ronald Cron6f135e12021-12-08 16:57:54 +01001331#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001332void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001333 const int kex_modes )
1334{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001335 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001336}
Ronald Cron6f135e12021-12-08 16:57:54 +01001337#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001340void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001341 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001342{
1343 conf->cert_profile = profile;
1344}
1345
Glenn Strauss36872db2022-01-22 05:06:31 -05001346static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1347{
1348 mbedtls_ssl_key_cert *cur = key_cert, *next;
1349
1350 while( cur != NULL )
1351 {
1352 next = cur->next;
1353 mbedtls_free( cur );
1354 cur = next;
1355 }
1356}
1357
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001358/* Append a new keycert entry to a (possibly empty) list */
1359static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1360 mbedtls_x509_crt *cert,
1361 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001362{
niisato8ee24222018-06-25 19:05:48 +09001363 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001364
Glenn Strauss36872db2022-01-22 05:06:31 -05001365 if( cert == NULL )
1366 {
1367 /* Free list if cert is null */
1368 ssl_key_cert_free( *head );
1369 *head = NULL;
1370 return( 0 );
1371 }
1372
niisato8ee24222018-06-25 19:05:48 +09001373 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1374 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001375 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001376
niisato8ee24222018-06-25 19:05:48 +09001377 new_cert->cert = cert;
1378 new_cert->key = key;
1379 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001380
Glenn Strauss36872db2022-01-22 05:06:31 -05001381 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001382 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001383 {
niisato8ee24222018-06-25 19:05:48 +09001384 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001385 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001386 else
1387 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001388 mbedtls_ssl_key_cert *cur = *head;
1389 while( cur->next != NULL )
1390 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001391 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001392 }
1393
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001394 return( 0 );
1395}
1396
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001397int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001398 mbedtls_x509_crt *own_cert,
1399 mbedtls_pk_context *pk_key )
1400{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001401 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001402}
1403
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001404void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001405 mbedtls_x509_crt *ca_chain,
1406 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001407{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001408 conf->ca_chain = ca_chain;
1409 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001410
1411#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1412 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1413 * cannot be used together. */
1414 conf->f_ca_cb = NULL;
1415 conf->p_ca_cb = NULL;
1416#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001417}
Hanno Becker5adaad92019-03-27 16:54:37 +00001418
1419#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1420void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001421 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001422 void *p_ca_cb )
1423{
1424 conf->f_ca_cb = f_ca_cb;
1425 conf->p_ca_cb = p_ca_cb;
1426
1427 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1428 * cannot be used together. */
1429 conf->ca_chain = NULL;
1430 conf->ca_crl = NULL;
1431}
1432#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001434
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001435#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001436const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1437 size_t *name_len )
1438{
1439 *name_len = ssl->handshake->sni_name_len;
1440 return( ssl->handshake->sni_name );
1441}
1442
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001443int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1444 mbedtls_x509_crt *own_cert,
1445 mbedtls_pk_context *pk_key )
1446{
1447 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1448 own_cert, pk_key ) );
1449}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001450
1451void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1452 mbedtls_x509_crt *ca_chain,
1453 mbedtls_x509_crl *ca_crl )
1454{
1455 ssl->handshake->sni_ca_chain = ca_chain;
1456 ssl->handshake->sni_ca_crl = ca_crl;
1457}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001458
1459void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1460 int authmode )
1461{
1462 ssl->handshake->sni_authmode = authmode;
1463}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001464#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1465
Hanno Becker8927c832019-04-03 12:52:50 +01001466#if defined(MBEDTLS_X509_CRT_PARSE_C)
1467void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1468 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1469 void *p_vrfy )
1470{
1471 ssl->f_vrfy = f_vrfy;
1472 ssl->p_vrfy = p_vrfy;
1473}
1474#endif
1475
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001476#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001477/*
1478 * Set EC J-PAKE password for current handshake
1479 */
1480int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1481 const unsigned char *pw,
1482 size_t pw_len )
1483{
1484 mbedtls_ecjpake_role role;
1485
Janos Follath8eb64132016-06-03 15:40:57 +01001486 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001487 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1488
1489 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1490 role = MBEDTLS_ECJPAKE_SERVER;
1491 else
1492 role = MBEDTLS_ECJPAKE_CLIENT;
1493
1494 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1495 role,
1496 MBEDTLS_MD_SHA256,
1497 MBEDTLS_ECP_DP_SECP256R1,
1498 pw, pw_len ) );
1499}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001500#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001501
Gilles Peskineeccd8882020-03-10 12:19:08 +01001502#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001503
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001504static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1505{
1506#if defined(MBEDTLS_USE_PSA_CRYPTO)
1507 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1508 return( 1 );
1509#endif /* MBEDTLS_USE_PSA_CRYPTO */
1510
1511 if( conf->psk != NULL )
1512 return( 1 );
1513
1514 return( 0 );
1515}
1516
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001517static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1518{
1519 /* Remove reference to existing PSK, if any. */
1520#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001521 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001522 {
1523 /* The maintenance of the PSK key slot is the
1524 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001525 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001526 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00001527 /* This and the following branch should never
1528 * be taken simultaenously as we maintain the
1529 * invariant that raw and opaque PSKs are never
1530 * configured simultaneously. As a safeguard,
1531 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001532#endif /* MBEDTLS_USE_PSA_CRYPTO */
1533 if( conf->psk != NULL )
1534 {
1535 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1536
1537 mbedtls_free( conf->psk );
1538 conf->psk = NULL;
1539 conf->psk_len = 0;
1540 }
1541
1542 /* Remove reference to PSK identity, if any. */
1543 if( conf->psk_identity != NULL )
1544 {
1545 mbedtls_free( conf->psk_identity );
1546 conf->psk_identity = NULL;
1547 conf->psk_identity_len = 0;
1548 }
1549}
1550
Hanno Becker7390c712018-11-15 13:33:04 +00001551/* This function assumes that PSK identity in the SSL config is unset.
1552 * It checks that the provided identity is well-formed and attempts
1553 * to make a copy of it in the SSL config.
1554 * On failure, the PSK identity in the config remains unset. */
1555static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1556 unsigned char const *psk_identity,
1557 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001558{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001559 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001560 if( psk_identity == NULL ||
1561 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001562 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001563 {
1564 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1565 }
1566
Hanno Becker7390c712018-11-15 13:33:04 +00001567 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1568 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001569 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001570
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001571 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001572 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001573
1574 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001575}
1576
Hanno Becker7390c712018-11-15 13:33:04 +00001577int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1578 const unsigned char *psk, size_t psk_len,
1579 const unsigned char *psk_identity, size_t psk_identity_len )
1580{
Janos Follath865b3eb2019-12-16 11:46:15 +00001581 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001582
1583 /* We currently only support one PSK, raw or opaque. */
1584 if( ssl_conf_psk_is_configured( conf ) )
1585 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001586
1587 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001588 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001589 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001590 if( psk_len == 0 )
1591 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1592 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1593 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1594
Hanno Becker7390c712018-11-15 13:33:04 +00001595 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1596 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1597 conf->psk_len = psk_len;
1598 memcpy( conf->psk, psk, conf->psk_len );
1599
1600 /* Check and set PSK Identity */
1601 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1602 if( ret != 0 )
1603 ssl_conf_remove_psk( conf );
1604
1605 return( ret );
1606}
1607
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001608static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1609{
1610#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001611 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001612 {
Ronald Croncf56a0a2020-08-04 09:51:30 +02001613 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001614 }
1615 else
1616#endif /* MBEDTLS_USE_PSA_CRYPTO */
1617 if( ssl->handshake->psk != NULL )
1618 {
1619 mbedtls_platform_zeroize( ssl->handshake->psk,
1620 ssl->handshake->psk_len );
1621 mbedtls_free( ssl->handshake->psk );
1622 ssl->handshake->psk_len = 0;
1623 }
1624}
1625
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001626int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1627 const unsigned char *psk, size_t psk_len )
1628{
1629 if( psk == NULL || ssl->handshake == NULL )
1630 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1631
1632 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1633 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1634
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001635 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001636
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001637 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001638 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001639
1640 ssl->handshake->psk_len = psk_len;
1641 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1642
1643 return( 0 );
1644}
1645
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001646#if defined(MBEDTLS_USE_PSA_CRYPTO)
1647int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001648 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001649 const unsigned char *psk_identity,
1650 size_t psk_identity_len )
1651{
Janos Follath865b3eb2019-12-16 11:46:15 +00001652 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001653
1654 /* We currently only support one PSK, raw or opaque. */
1655 if( ssl_conf_psk_is_configured( conf ) )
1656 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001657
Hanno Becker7390c712018-11-15 13:33:04 +00001658 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001659 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001660 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001661 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001662
1663 /* Check and set PSK Identity */
1664 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1665 psk_identity_len );
1666 if( ret != 0 )
1667 ssl_conf_remove_psk( conf );
1668
1669 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001670}
1671
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001672int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1673 mbedtls_svc_key_id_t psk )
1674{
1675 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1676 ( ssl->handshake == NULL ) )
1677 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1678
1679 ssl_remove_psk( ssl );
1680 ssl->handshake->psk_opaque = psk;
1681 return( 0 );
1682}
1683#endif /* MBEDTLS_USE_PSA_CRYPTO */
1684
1685void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1686 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1687 size_t),
1688 void *p_psk )
1689{
1690 conf->f_psk = f_psk;
1691 conf->p_psk = p_psk;
1692}
1693#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1694
1695#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01001696psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01001697 size_t taglen,
1698 psa_algorithm_t *alg,
1699 psa_key_type_t *key_type,
1700 size_t *key_size )
1701{
1702 switch ( mbedtls_cipher_type )
1703 {
1704 case MBEDTLS_CIPHER_AES_128_CBC:
1705 *alg = PSA_ALG_CBC_NO_PADDING;
1706 *key_type = PSA_KEY_TYPE_AES;
1707 *key_size = 128;
1708 break;
1709 case MBEDTLS_CIPHER_AES_128_CCM:
1710 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1711 *key_type = PSA_KEY_TYPE_AES;
1712 *key_size = 128;
1713 break;
1714 case MBEDTLS_CIPHER_AES_128_GCM:
1715 *alg = PSA_ALG_GCM;
1716 *key_type = PSA_KEY_TYPE_AES;
1717 *key_size = 128;
1718 break;
1719 case MBEDTLS_CIPHER_AES_192_CCM:
1720 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1721 *key_type = PSA_KEY_TYPE_AES;
1722 *key_size = 192;
1723 break;
1724 case MBEDTLS_CIPHER_AES_192_GCM:
1725 *alg = PSA_ALG_GCM;
1726 *key_type = PSA_KEY_TYPE_AES;
1727 *key_size = 192;
1728 break;
1729 case MBEDTLS_CIPHER_AES_256_CBC:
1730 *alg = PSA_ALG_CBC_NO_PADDING;
1731 *key_type = PSA_KEY_TYPE_AES;
1732 *key_size = 256;
1733 break;
1734 case MBEDTLS_CIPHER_AES_256_CCM:
1735 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1736 *key_type = PSA_KEY_TYPE_AES;
1737 *key_size = 256;
1738 break;
1739 case MBEDTLS_CIPHER_AES_256_GCM:
1740 *alg = PSA_ALG_GCM;
1741 *key_type = PSA_KEY_TYPE_AES;
1742 *key_size = 256;
1743 break;
1744 case MBEDTLS_CIPHER_ARIA_128_CBC:
1745 *alg = PSA_ALG_CBC_NO_PADDING;
1746 *key_type = PSA_KEY_TYPE_ARIA;
1747 *key_size = 128;
1748 break;
1749 case MBEDTLS_CIPHER_ARIA_128_CCM:
1750 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1751 *key_type = PSA_KEY_TYPE_ARIA;
1752 *key_size = 128;
1753 break;
1754 case MBEDTLS_CIPHER_ARIA_128_GCM:
1755 *alg = PSA_ALG_GCM;
1756 *key_type = PSA_KEY_TYPE_ARIA;
1757 *key_size = 128;
1758 break;
1759 case MBEDTLS_CIPHER_ARIA_192_CCM:
1760 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1761 *key_type = PSA_KEY_TYPE_ARIA;
1762 *key_size = 192;
1763 break;
1764 case MBEDTLS_CIPHER_ARIA_192_GCM:
1765 *alg = PSA_ALG_GCM;
1766 *key_type = PSA_KEY_TYPE_ARIA;
1767 *key_size = 192;
1768 break;
1769 case MBEDTLS_CIPHER_ARIA_256_CBC:
1770 *alg = PSA_ALG_CBC_NO_PADDING;
1771 *key_type = PSA_KEY_TYPE_ARIA;
1772 *key_size = 256;
1773 break;
1774 case MBEDTLS_CIPHER_ARIA_256_CCM:
1775 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1776 *key_type = PSA_KEY_TYPE_ARIA;
1777 *key_size = 256;
1778 break;
1779 case MBEDTLS_CIPHER_ARIA_256_GCM:
1780 *alg = PSA_ALG_GCM;
1781 *key_type = PSA_KEY_TYPE_ARIA;
1782 *key_size = 256;
1783 break;
1784 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
1785 *alg = PSA_ALG_CBC_NO_PADDING;
1786 *key_type = PSA_KEY_TYPE_CAMELLIA;
1787 *key_size = 128;
1788 break;
1789 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
1790 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1791 *key_type = PSA_KEY_TYPE_CAMELLIA;
1792 *key_size = 128;
1793 break;
1794 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
1795 *alg = PSA_ALG_GCM;
1796 *key_type = PSA_KEY_TYPE_CAMELLIA;
1797 *key_size = 128;
1798 break;
1799 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
1800 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1801 *key_type = PSA_KEY_TYPE_CAMELLIA;
1802 *key_size = 192;
1803 break;
1804 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
1805 *alg = PSA_ALG_GCM;
1806 *key_type = PSA_KEY_TYPE_CAMELLIA;
1807 *key_size = 192;
1808 break;
1809 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
1810 *alg = PSA_ALG_CBC_NO_PADDING;
1811 *key_type = PSA_KEY_TYPE_CAMELLIA;
1812 *key_size = 256;
1813 break;
1814 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
1815 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1816 *key_type = PSA_KEY_TYPE_CAMELLIA;
1817 *key_size = 256;
1818 break;
1819 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
1820 *alg = PSA_ALG_GCM;
1821 *key_type = PSA_KEY_TYPE_CAMELLIA;
1822 *key_size = 256;
1823 break;
1824 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
1825 *alg = PSA_ALG_CHACHA20_POLY1305;
1826 *key_type = PSA_KEY_TYPE_CHACHA20;
1827 *key_size = 256;
1828 break;
1829 case MBEDTLS_CIPHER_NULL:
1830 *alg = MBEDTLS_SSL_NULL_CIPHER;
1831 *key_type = 0;
1832 *key_size = 0;
1833 break;
1834 default:
1835 return PSA_ERROR_NOT_SUPPORTED;
1836 }
1837
1838 return PSA_SUCCESS;
1839}
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001840#endif /* MBEDTLS_USE_PSA_CRYPTO */
1841
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02001842#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01001843int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
1844 const unsigned char *dhm_P, size_t P_len,
1845 const unsigned char *dhm_G, size_t G_len )
1846{
Janos Follath865b3eb2019-12-16 11:46:15 +00001847 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01001848
Glenn Strausscee11292021-12-20 01:43:17 -05001849 mbedtls_mpi_free( &conf->dhm_P );
1850 mbedtls_mpi_free( &conf->dhm_G );
1851
Hanno Beckera90658f2017-10-04 15:29:08 +01001852 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
1853 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
1854 {
1855 mbedtls_mpi_free( &conf->dhm_P );
1856 mbedtls_mpi_free( &conf->dhm_G );
1857 return( ret );
1858 }
1859
1860 return( 0 );
1861}
Paul Bakker5121ce52009-01-03 21:22:43 +00001862
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001863int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00001864{
Janos Follath865b3eb2019-12-16 11:46:15 +00001865 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00001866
Glenn Strausscee11292021-12-20 01:43:17 -05001867 mbedtls_mpi_free( &conf->dhm_P );
1868 mbedtls_mpi_free( &conf->dhm_G );
1869
Gilles Peskinee5702482021-06-11 21:59:08 +02001870 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
1871 &conf->dhm_P ) ) != 0 ||
1872 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
1873 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01001874 {
1875 mbedtls_mpi_free( &conf->dhm_P );
1876 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00001877 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01001878 }
Paul Bakker1b57b062011-01-06 15:48:19 +00001879
1880 return( 0 );
1881}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02001882#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00001883
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02001884#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
1885/*
1886 * Set the minimum length for Diffie-Hellman parameters
1887 */
1888void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
1889 unsigned int bitlen )
1890{
1891 conf->dhm_min_bitlen = bitlen;
1892}
1893#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
1894
Gilles Peskineeccd8882020-03-10 12:19:08 +01001895#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08001896#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02001897/*
1898 * Set allowed/preferred hashes for handshake signatures
1899 */
1900void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
1901 const int *hashes )
1902{
1903 conf->sig_hashes = hashes;
1904}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08001905#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01001906
Jerry Yuf017ee42022-01-12 15:49:48 +08001907/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01001908void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
1909 const uint16_t* sig_algs )
1910{
Jerry Yuf017ee42022-01-12 15:49:48 +08001911#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1912 conf->sig_hashes = NULL;
1913#endif /* !MBEDTLS_DEPRECATED_REMOVED */
1914 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01001915}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001916#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02001917
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02001918#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01001919#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001920/*
1921 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01001922 *
1923 * mbedtls_ssl_setup() takes the provided list
1924 * and translates it to a list of IANA TLS group identifiers,
1925 * stored in ssl->handshake->group_list.
1926 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001927 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001928void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001929 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001930{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001931 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01001932 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001933}
Brett Warrene0edc842021-08-17 09:53:13 +01001934#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01001935#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001936
Brett Warrene0edc842021-08-17 09:53:13 +01001937/*
1938 * Set the allowed groups
1939 */
1940void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
1941 const uint16_t *group_list )
1942{
1943#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
1944 conf->curve_list = NULL;
1945#endif
1946 conf->group_list = group_list;
1947}
1948
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001949#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00001951{
Hanno Becker947194e2017-04-07 13:25:49 +01001952 /* Initialize to suppress unnecessary compiler warning */
1953 size_t hostname_len = 0;
1954
1955 /* Check if new hostname is valid before
1956 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01001957 if( hostname != NULL )
1958 {
1959 hostname_len = strlen( hostname );
1960
1961 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
1962 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1963 }
1964
1965 /* Now it's clear that we will overwrite the old hostname,
1966 * so we can free it safely */
1967
1968 if( ssl->hostname != NULL )
1969 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001970 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01001971 mbedtls_free( ssl->hostname );
1972 }
1973
1974 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01001975
Paul Bakker5121ce52009-01-03 21:22:43 +00001976 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01001977 {
1978 ssl->hostname = NULL;
1979 }
1980 else
1981 {
1982 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01001983 if( ssl->hostname == NULL )
1984 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02001985
Hanno Becker947194e2017-04-07 13:25:49 +01001986 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02001987
Hanno Becker947194e2017-04-07 13:25:49 +01001988 ssl->hostname[hostname_len] = '\0';
1989 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001990
1991 return( 0 );
1992}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01001993#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001994
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001995#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001996void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00001998 const unsigned char *, size_t),
1999 void *p_sni )
2000{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002001 conf->f_sni = f_sni;
2002 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002003}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002007int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002008{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002009 size_t cur_len, tot_len;
2010 const char **p;
2011
2012 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002013 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2014 * MUST NOT be truncated."
2015 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002016 */
2017 tot_len = 0;
2018 for( p = protos; *p != NULL; p++ )
2019 {
2020 cur_len = strlen( *p );
2021 tot_len += cur_len;
2022
Ronald Cron8216dd32020-04-23 16:41:44 +02002023 if( ( cur_len == 0 ) ||
2024 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2025 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002027 }
2028
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002029 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002030
2031 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002032}
2033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002035{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002036 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002037}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002039
Johan Pascalb62bb512015-12-03 21:56:45 +01002040#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002041void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2042 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002043{
2044 conf->dtls_srtp_mki_support = support_mki_value;
2045}
2046
Ron Eldoref72faf2018-07-12 11:54:20 +03002047int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2048 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002049 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002050{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002051 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002052 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002053 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002054 }
Ron Eldor591f1622018-01-22 12:30:04 +02002055
2056 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002057 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002058 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002059 }
Ron Eldor591f1622018-01-22 12:30:04 +02002060
2061 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2062 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002063 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002064}
2065
Ron Eldoref72faf2018-07-12 11:54:20 +03002066int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002067 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002068{
Johan Pascal253d0262020-09-22 13:04:45 +02002069 const mbedtls_ssl_srtp_profile *p;
2070 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002071
Johan Pascal253d0262020-09-22 13:04:45 +02002072 /* check the profiles list: all entry must be valid,
2073 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002074 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2075 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2076 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002077 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002078 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002079 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002080 list_size++;
2081 }
2082 else
2083 {
2084 /* unsupported value, stop parsing and set the size to an error value */
2085 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002086 }
2087 }
2088
Johan Pascal5ef72d22020-10-28 17:05:47 +01002089 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002090 {
Johan Pascal253d0262020-09-22 13:04:45 +02002091 conf->dtls_srtp_profile_list = NULL;
2092 conf->dtls_srtp_profile_list_len = 0;
2093 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2094 }
2095
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002096 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002097 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002098
2099 return( 0 );
2100}
2101
Johan Pascal5ef72d22020-10-28 17:05:47 +01002102void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2103 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002104{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002105 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2106 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002107 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002108 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002109 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002110 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002111 else
2112 {
2113 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002114 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2115 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002116 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002117}
Johan Pascalb62bb512015-12-03 21:56:45 +01002118#endif /* MBEDTLS_SSL_DTLS_SRTP */
2119
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002120void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002121{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002122 conf->max_major_ver = major;
2123 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002124}
2125
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002126void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002127{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002128 conf->min_major_ver = major;
2129 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002130}
2131
Janos Follath088ce432017-04-10 12:42:31 +01002132#if defined(MBEDTLS_SSL_SRV_C)
2133void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2134 char cert_req_ca_list )
2135{
2136 conf->cert_req_ca_list = cert_req_ca_list;
2137}
2138#endif
2139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002141void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002142{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002143 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002144}
2145#endif
2146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002148void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002149{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002150 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002151}
2152#endif
2153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002155int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002156{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002158 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002161 }
2162
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002163 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002164
2165 return( 0 );
2166}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002168
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002169void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002170{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002171 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002172}
2173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002175void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002176{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002177 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002178}
2179
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002180void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002181{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002182 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002183}
2184
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002185void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002186 const unsigned char period[8] )
2187{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002188 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002189}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002193#if defined(MBEDTLS_SSL_CLI_C)
2194void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002195{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002196 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002197}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002198#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002199
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002200#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002201void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2202 mbedtls_ssl_ticket_write_t *f_ticket_write,
2203 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2204 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002205{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002206 conf->f_ticket_write = f_ticket_write;
2207 conf->f_ticket_parse = f_ticket_parse;
2208 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002209}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002210#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002212
Hanno Becker7e6c1782021-06-08 09:24:55 +01002213void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2214 mbedtls_ssl_export_keys_t *f_export_keys,
2215 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002216{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002217 ssl->f_export_keys = f_export_keys;
2218 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002219}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002220
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002221#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002222void mbedtls_ssl_conf_async_private_cb(
2223 mbedtls_ssl_config *conf,
2224 mbedtls_ssl_async_sign_t *f_async_sign,
2225 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2226 mbedtls_ssl_async_resume_t *f_async_resume,
2227 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002228 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002229{
2230 conf->f_async_sign_start = f_async_sign;
2231 conf->f_async_decrypt_start = f_async_decrypt;
2232 conf->f_async_resume = f_async_resume;
2233 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002234 conf->p_async_config_data = async_config_data;
2235}
2236
Gilles Peskine8f97af72018-04-26 11:46:10 +02002237void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2238{
2239 return( conf->p_async_config_data );
2240}
2241
Gilles Peskine1febfef2018-04-30 11:54:39 +02002242void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002243{
2244 if( ssl->handshake == NULL )
2245 return( NULL );
2246 else
2247 return( ssl->handshake->user_async_ctx );
2248}
2249
Gilles Peskine1febfef2018-04-30 11:54:39 +02002250void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002251 void *ctx )
2252{
2253 if( ssl->handshake != NULL )
2254 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002255}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002256#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002257
Paul Bakker5121ce52009-01-03 21:22:43 +00002258/*
2259 * SSL get accessors
2260 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002261uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002262{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002263 if( ssl->session != NULL )
2264 return( ssl->session->verify_result );
2265
2266 if( ssl->session_negotiate != NULL )
2267 return( ssl->session_negotiate->verify_result );
2268
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002269 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002270}
2271
Glenn Strauss8f526902022-01-13 00:04:49 -05002272int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2273{
2274 if( ssl == NULL || ssl->session == NULL )
2275 return( 0 );
2276
2277 return( ssl->session->ciphersuite );
2278}
2279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002281{
Paul Bakker926c8e42013-03-06 10:23:34 +01002282 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002283 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002286}
2287
Gilles Peskinee1a0c252022-01-13 01:08:03 +01002288mbedtls_ssl_protocol_version mbedtls_ssl_get_version_number(
2289 const mbedtls_ssl_context *ssl )
2290{
2291 /* For major_ver, only 3 is supported, so skip checking it. */
2292 switch( ssl->minor_ver )
2293 {
2294 case MBEDTLS_SSL_MINOR_VERSION_3:
2295 return( MBEDTLS_SSL_VERSION_1_2 );
2296 case MBEDTLS_SSL_MINOR_VERSION_4:
2297 return( MBEDTLS_SSL_VERSION_1_3 );
2298 default:
2299 return( MBEDTLS_SSL_VERSION_UNKNOWN );
2300 }
2301}
2302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002304{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002306 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002307 {
2308 switch( ssl->minor_ver )
2309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002311 return( "DTLSv1.2" );
2312
2313 default:
2314 return( "unknown (DTLS)" );
2315 }
2316 }
2317#endif
2318
Paul Bakker43ca69c2011-01-15 17:35:19 +00002319 switch( ssl->minor_ver )
2320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002322 return( "TLSv1.2" );
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002323 case MBEDTLS_SSL_MINOR_VERSION_4:
2324 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002325 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002326 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002327 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002328}
2329
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002330#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002331size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2332{
David Horstmann95d516f2021-05-04 18:36:56 +01002333 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002334 size_t read_mfl;
2335
2336 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2337 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2338 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2339 {
2340 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2341 }
2342
2343 /* Check if a smaller max length was negotiated */
2344 if( ssl->session_out != NULL )
2345 {
2346 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2347 if( read_mfl < max_len )
2348 {
2349 max_len = read_mfl;
2350 }
2351 }
2352
2353 // During a handshake, use the value being negotiated
2354 if( ssl->session_negotiate != NULL )
2355 {
2356 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2357 if( read_mfl < max_len )
2358 {
2359 max_len = read_mfl;
2360 }
2361 }
2362
2363 return( max_len );
2364}
2365
2366size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002367{
2368 size_t max_len;
2369
2370 /*
2371 * Assume mfl_code is correct since it was checked when set
2372 */
Angus Grattond8213d02016-05-25 20:56:48 +10002373 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002374
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002375 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002376 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002377 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002378 {
Angus Grattond8213d02016-05-25 20:56:48 +10002379 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002380 }
2381
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002382 /* During a handshake, use the value being negotiated */
2383 if( ssl->session_negotiate != NULL &&
2384 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2385 {
2386 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2387 }
2388
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002389 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002390}
2391#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2392
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002393#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002394size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002395{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002396 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2397 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2398 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2399 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2400 return ( 0 );
2401
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002402 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2403 return( ssl->mtu );
2404
2405 if( ssl->mtu == 0 )
2406 return( ssl->handshake->mtu );
2407
2408 return( ssl->mtu < ssl->handshake->mtu ?
2409 ssl->mtu : ssl->handshake->mtu );
2410}
2411#endif /* MBEDTLS_SSL_PROTO_DTLS */
2412
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002413int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2414{
2415 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2416
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002417#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2418 !defined(MBEDTLS_SSL_PROTO_DTLS)
2419 (void) ssl;
2420#endif
2421
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002422#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002423 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002424
2425 if( max_len > mfl )
2426 max_len = mfl;
2427#endif
2428
2429#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002430 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002431 {
Hanno Becker89490712020-02-05 10:50:12 +00002432 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002433 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2434 const size_t overhead = (size_t) ret;
2435
2436 if( ret < 0 )
2437 return( ret );
2438
2439 if( mtu <= overhead )
2440 {
2441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2442 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2443 }
2444
2445 if( max_len > mtu - overhead )
2446 max_len = mtu - overhead;
2447 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002448#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002449
Hanno Becker0defedb2018-08-10 12:35:02 +01002450#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2451 !defined(MBEDTLS_SSL_PROTO_DTLS)
2452 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002453#endif
2454
2455 return( (int) max_len );
2456}
2457
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002458int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2459{
2460 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2461
2462#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2463 (void) ssl;
2464#endif
2465
2466#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2467 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2468
2469 if( max_len > mfl )
2470 max_len = mfl;
2471#endif
2472
2473 return( (int) max_len );
2474}
2475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476#if defined(MBEDTLS_X509_CRT_PARSE_C)
2477const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002478{
2479 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002480 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002481
Hanno Beckere6824572019-02-07 13:18:46 +00002482#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002483 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002484#else
2485 return( NULL );
2486#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002487}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002491int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2492 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002493{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002494 int ret;
2495
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002496 if( ssl == NULL ||
2497 dst == NULL ||
2498 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002499 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002502 }
2503
Hanno Beckere810bbc2021-05-14 16:01:05 +01002504 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2505 * idempotent: Each session can only be exported once.
2506 *
2507 * (This is in preparation for TLS 1.3 support where we will
2508 * need the ability to export multiple sessions (aka tickets),
2509 * which will be achieved by calling mbedtls_ssl_get_session()
2510 * multiple times until it fails.)
2511 *
2512 * Check whether we have already exported the current session,
2513 * and fail if so.
2514 */
2515 if( ssl->session->exported == 1 )
2516 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2517
2518 ret = mbedtls_ssl_session_copy( dst, ssl->session );
2519 if( ret != 0 )
2520 return( ret );
2521
2522 /* Remember that we've exported the session. */
2523 ssl->session->exported = 1;
2524 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002525}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002527
Paul Bakker5121ce52009-01-03 21:22:43 +00002528/*
Hanno Beckera835da52019-05-16 12:39:07 +01002529 * Define ticket header determining Mbed TLS version
2530 * and structure of the ticket.
2531 */
2532
Hanno Becker94ef3b32019-05-16 12:50:45 +01002533/*
Hanno Becker50b59662019-05-28 14:30:45 +01002534 * Define bitflag determining compile-time settings influencing
2535 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01002536 */
2537
Hanno Becker50b59662019-05-28 14:30:45 +01002538#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01002539#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01002540#else
Hanno Becker3e088662019-05-29 11:10:18 +01002541#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002542#endif /* MBEDTLS_HAVE_TIME */
2543
2544#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01002545#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002546#else
Hanno Becker3e088662019-05-29 11:10:18 +01002547#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002548#endif /* MBEDTLS_X509_CRT_PARSE_C */
2549
2550#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01002551#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002552#else
Hanno Becker3e088662019-05-29 11:10:18 +01002553#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002554#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
2555
2556#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01002557#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002558#else
Hanno Becker3e088662019-05-29 11:10:18 +01002559#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002560#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2561
Hanno Becker94ef3b32019-05-16 12:50:45 +01002562#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01002563#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002564#else
Hanno Becker3e088662019-05-29 11:10:18 +01002565#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002566#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2567
Hanno Becker94ef3b32019-05-16 12:50:45 +01002568#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2569#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
2570#else
2571#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
2572#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2573
Hanno Becker3e088662019-05-29 11:10:18 +01002574#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
2575#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
2576#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
2577#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01002578#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
2579#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01002580
Hanno Becker50b59662019-05-28 14:30:45 +01002581#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01002582 ( (uint16_t) ( \
2583 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
2584 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
2585 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
2586 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01002587 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01002588 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01002589
Hanno Beckerf8787072019-05-16 12:41:07 +01002590static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01002591 MBEDTLS_VERSION_MAJOR,
2592 MBEDTLS_VERSION_MINOR,
2593 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01002594 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
2595 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01002596};
Hanno Beckera835da52019-05-16 12:39:07 +01002597
2598/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002599 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002600 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002601 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002602 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002603 *
Hanno Beckerdce50972021-08-01 05:39:23 +01002604 * opaque mbedtls_version[3]; // library version: major, minor, patch
2605 * opaque session_format[2]; // library-version specific 16-bit field
2606 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002607 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002608 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002609 * Note: When updating the format, remember to keep
2610 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002611 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002612 * // In this version, `session_format` determines
2613 * // the setting of those compile-time
2614 * // configuration options which influence
2615 * // the structure of mbedtls_ssl_session.
2616 *
Hanno Beckerfa0d61e2021-08-02 08:56:14 +01002617 * uint8_t minor_ver; // Protocol-version. Possible values:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002618 * // - TLS 1.2 (MBEDTLS_SSL_MINOR_VERSION_3)
2619 *
2620 * select (serialized_session.minor_ver) {
2621 *
2622 * case MBEDTLS_SSL_MINOR_VERSION_3: // TLS 1.2
2623 * serialized_session_tls12 data;
2624 *
2625 * };
2626 *
2627 * } serialized_session;
2628 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002629 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002630
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002631static int ssl_session_save( const mbedtls_ssl_session *session,
2632 unsigned char omit_header,
2633 unsigned char *buf,
2634 size_t buf_len,
2635 size_t *olen )
2636{
2637 unsigned char *p = buf;
2638 size_t used = 0;
2639
2640 if( !omit_header )
2641 {
2642 /*
2643 * Add Mbed TLS version identifier
2644 */
2645
2646 used += sizeof( ssl_serialized_session_header );
2647
2648 if( used <= buf_len )
2649 {
2650 memcpy( p, ssl_serialized_session_header,
2651 sizeof( ssl_serialized_session_header ) );
2652 p += sizeof( ssl_serialized_session_header );
2653 }
2654 }
2655
2656 /*
2657 * TLS version identifier
2658 */
2659 used += 1;
2660 if( used <= buf_len )
2661 {
2662 *p++ = session->minor_ver;
2663 }
2664
2665 /* Forward to version-specific serialization routine. */
2666 switch( session->minor_ver )
2667 {
2668#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2669 case MBEDTLS_SSL_MINOR_VERSION_3:
2670 {
2671 size_t remaining_len = used <= buf_len ? buf_len - used : 0;
2672 used += ssl_session_save_tls12( session, p, remaining_len );
2673 break;
2674 }
2675#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2676
2677 default:
2678 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2679 }
2680
2681 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02002682 if( used > buf_len )
2683 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002684
2685 return( 0 );
2686}
2687
2688/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02002689 * Public wrapper for ssl_session_save()
2690 */
2691int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
2692 unsigned char *buf,
2693 size_t buf_len,
2694 size_t *olen )
2695{
2696 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
2697}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002698
Jerry Yuf1b23ca2022-02-18 11:48:47 +08002699/*
2700 * Deserialize session, see mbedtls_ssl_session_save() for format.
2701 *
2702 * This internal version is wrapped by a public function that cleans up in
2703 * case of error, and has an extra option omit_header.
2704 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002705static int ssl_session_load( mbedtls_ssl_session *session,
2706 unsigned char omit_header,
2707 const unsigned char *buf,
2708 size_t len )
2709{
2710 const unsigned char *p = buf;
2711 const unsigned char * const end = buf + len;
2712
2713 if( !omit_header )
2714 {
2715 /*
2716 * Check Mbed TLS version identifier
2717 */
2718
2719 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
2720 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2721
2722 if( memcmp( p, ssl_serialized_session_header,
2723 sizeof( ssl_serialized_session_header ) ) != 0 )
2724 {
2725 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
2726 }
2727 p += sizeof( ssl_serialized_session_header );
2728 }
2729
2730 /*
2731 * TLS version identifier
2732 */
2733 if( 1 > (size_t)( end - p ) )
2734 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2735 session->minor_ver = *p++;
2736
2737 /* Dispatch according to TLS version. */
2738 switch( session->minor_ver )
2739 {
2740#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2741 case MBEDTLS_SSL_MINOR_VERSION_3: /* TLS 1.2 */
2742 {
2743 size_t remaining_len = ( end - p );
2744 return( ssl_session_load_tls12( session, p, remaining_len ) );
2745 }
2746#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2747
2748 default:
2749 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2750 }
2751}
2752
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002753/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02002754 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02002755 */
2756int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
2757 const unsigned char *buf,
2758 size_t len )
2759{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02002760 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02002761
2762 if( ret != 0 )
2763 mbedtls_ssl_session_free( session );
2764
2765 return( ret );
2766}
2767
2768/*
Paul Bakker1961b702013-01-25 14:49:24 +01002769 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00002770 */
Hanno Becker41934dd2021-08-07 19:13:43 +01002771static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
2772{
2773 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2774
Ronald Cron66dbf912022-02-02 15:33:46 +01002775 /*
2776 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01002777 * that were written into the output buffer by the previous handshake step,
2778 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01002779 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
2780 * We proceed to the next handshake step only when all data from the
2781 * previous one have been sent to the peer, thus we make sure that this is
2782 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
2783 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
2784 * we have to wait before to go ahead.
2785 * In the case of TLS 1.3, handshake step handlers do not send data to the
2786 * peer. Data are only sent here and through
2787 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
2788 * alert occured.
2789 */
Hanno Becker41934dd2021-08-07 19:13:43 +01002790 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2791 return( ret );
2792
2793#if defined(MBEDTLS_SSL_PROTO_DTLS)
2794 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2795 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
2796 {
2797 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
2798 return( ret );
2799 }
2800#endif /* MBEDTLS_SSL_PROTO_DTLS */
2801
2802 return( ret );
2803}
2804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002805int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002806{
Hanno Becker41934dd2021-08-07 19:13:43 +01002807 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002808
Hanno Becker41934dd2021-08-07 19:13:43 +01002809 if( ssl == NULL ||
2810 ssl->conf == NULL ||
2811 ssl->handshake == NULL ||
2812 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2813 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002814 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01002815 }
2816
2817 ret = ssl_prepare_handshake_step( ssl );
2818 if( ret != 0 )
2819 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002820
Jerry Yue7047812021-09-13 19:26:39 +08002821 ret = mbedtls_ssl_handle_pending_alert( ssl );
2822 if( ret != 0 )
2823 goto cleanup;
2824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002826 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08002827 {
Ronald Cron27c85e72022-03-08 11:37:55 +01002828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
2829 mbedtls_ssl_states_str( ssl->state ) ) );
2830
Ronald Cron6f135e12021-12-08 16:57:54 +01002831#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08002832 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yuf4436812021-08-26 22:59:56 +08002833 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01002834#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08002835
2836#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2837 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
2838 ret = mbedtls_ssl_handshake_client_step( ssl );
2839#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2840 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002841#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002843 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08002844 {
Ronald Cron6f135e12021-12-08 16:57:54 +01002845#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08002846 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08002847 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01002848#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08002849
2850#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2851 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
2852 ret = mbedtls_ssl_handshake_server_step( ssl );
2853#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2854 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002855#endif
2856
Jerry Yue7047812021-09-13 19:26:39 +08002857 if( ret != 0 )
2858 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08002859 /* handshake_step return error. And it is same
2860 * with alert_reason.
2861 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08002862 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08002863 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08002864 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08002865 goto cleanup;
2866 }
2867 }
2868
2869cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01002870 return( ret );
2871}
2872
2873/*
2874 * Perform the SSL handshake
2875 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01002877{
2878 int ret = 0;
2879
Hanno Beckera817ea42020-10-20 15:20:23 +01002880 /* Sanity checks */
2881
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002882 if( ssl == NULL || ssl->conf == NULL )
2883 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2884
Hanno Beckera817ea42020-10-20 15:20:23 +01002885#if defined(MBEDTLS_SSL_PROTO_DTLS)
2886 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2887 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
2888 {
2889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2890 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2891 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2892 }
2893#endif /* MBEDTLS_SSL_PROTO_DTLS */
2894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01002896
Hanno Beckera817ea42020-10-20 15:20:23 +01002897 /* Main handshake loop */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01002899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002901
2902 if( ret != 0 )
2903 break;
2904 }
2905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002907
2908 return( ret );
2909}
2910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911#if defined(MBEDTLS_SSL_RENEGOTIATION)
2912#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002913/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002914 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00002915 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002917{
Janos Follath865b3eb2019-12-16 11:46:15 +00002918 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002921
2922 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2924 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002925
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002926 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002927 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002928 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002929 return( ret );
2930 }
2931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002933
2934 return( 0 );
2935}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002936#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002937
2938/*
2939 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002940 * - any side: calling mbedtls_ssl_renegotiate(),
2941 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
2942 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02002943 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002944 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002946 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00002947int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00002948{
Janos Follath865b3eb2019-12-16 11:46:15 +00002949 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00002950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002952
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002953 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
2954 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00002955
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02002956 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
2957 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002959 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02002961 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002962 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002963 ssl->handshake->out_msg_seq = 1;
2964 else
2965 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02002966 }
2967#endif
2968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002969 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
2970 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00002971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00002973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00002975 return( ret );
2976 }
2977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002979
2980 return( 0 );
2981}
2982
2983/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002984 * Renegotiate current connection on client,
2985 * or request renegotiation on server
2986 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002988{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002990
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002991 if( ssl == NULL || ssl->conf == NULL )
2992 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002995 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002996 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002998 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
2999 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003001 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003002
3003 /* Did we already try/start sending HelloRequest? */
3004 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003006
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003007 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003008 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003012 /*
3013 * On client, either start the renegotiation process or,
3014 * if already in progress, continue the handshake
3015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
3019 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003020
Hanno Becker40cdaa12020-02-05 10:48:27 +00003021 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003022 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003023 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003024 return( ret );
3025 }
3026 }
3027 else
3028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003029 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003032 return( ret );
3033 }
3034 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003036
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003037 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003038}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003039#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003040
Gilles Peskine9b562d52018-04-25 20:32:43 +02003041void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003042{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003043 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3044
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003045 if( handshake == NULL )
3046 return;
3047
Brett Warrene0edc842021-08-17 09:53:13 +01003048#if defined(MBEDTLS_ECP_C)
3049#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3050 if ( ssl->handshake->group_list_heap_allocated )
3051 mbedtls_free( (void*) handshake->group_list );
3052 handshake->group_list = NULL;
3053#endif /* MBEDTLS_DEPRECATED_REMOVED */
3054#endif /* MBEDTLS_ECP_C */
3055
Jerry Yuf017ee42022-01-12 15:49:48 +08003056#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3057#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3058 if ( ssl->handshake->sig_algs_heap_allocated )
3059 mbedtls_free( (void*) handshake->sig_algs );
3060 handshake->sig_algs = NULL;
3061#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003062#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3063 if( ssl->handshake->certificate_request_context )
3064 {
3065 mbedtls_free( (void*) handshake->certificate_request_context );
3066 }
3067#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003068#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3069
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003070#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3071 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3072 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003073 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003074 handshake->async_in_progress = 0;
3075 }
3076#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3077
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003078#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003079#if defined(MBEDTLS_USE_PSA_CRYPTO)
3080 psa_hash_abort( &handshake->fin_sha256_psa );
3081#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003082 mbedtls_sha256_free( &handshake->fin_sha256 );
3083#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003084#endif
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +02003085#if defined(MBEDTLS_SHA384_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003086#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003087 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003088#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003089 mbedtls_sha512_free( &handshake->fin_sha512 );
3090#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003091#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003093#if defined(MBEDTLS_DHM_C)
3094 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003095#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096#if defined(MBEDTLS_ECDH_C)
3097 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003098#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003099#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003100 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003101#if defined(MBEDTLS_SSL_CLI_C)
3102 mbedtls_free( handshake->ecjpake_cache );
3103 handshake->ecjpake_cache = NULL;
3104 handshake->ecjpake_cache_len = 0;
3105#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003106#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003107
Janos Follath4ae5c292016-02-10 11:27:43 +00003108#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3109 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003110 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003112#endif
3113
Gilles Peskineeccd8882020-03-10 12:19:08 +01003114#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003115 if( handshake->psk != NULL )
3116 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003117 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003118 mbedtls_free( handshake->psk );
3119 }
3120#endif
3121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3123 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003124 /*
3125 * Free only the linked list wrapper, not the keys themselves
3126 * since the belong to the SNI callback
3127 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003128 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003130
Gilles Peskineeccd8882020-03-10 12:19:08 +01003131#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003132 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003133 if( handshake->ecrs_peer_cert != NULL )
3134 {
3135 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3136 mbedtls_free( handshake->ecrs_peer_cert );
3137 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003138#endif
3139
Hanno Becker75173122019-02-06 16:18:31 +00003140#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3141 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3142 mbedtls_pk_free( &handshake->peer_pubkey );
3143#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3144
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003145#if defined(MBEDTLS_SSL_CLI_C) && \
3146 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3147 mbedtls_free( handshake->cookie );
3148#endif /* MBEDTLS_SSL_CLI_C &&
3149 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003150
3151#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003152 mbedtls_ssl_flight_free( handshake->flight );
3153 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003154#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003155
Ronald Cronf12b81d2022-03-15 10:42:41 +01003156#if defined(MBEDTLS_ECDH_C) && \
3157 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Hanno Becker4a63ed42019-01-08 11:39:35 +00003158 psa_destroy_key( handshake->ecdh_psa_privkey );
3159#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3160
Ronald Cron6f135e12021-12-08 16:57:54 +01003161#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003162 mbedtls_ssl_transform_free( handshake->transform_handshake );
3163 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003164 mbedtls_free( handshake->transform_earlydata );
3165 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003166#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003167
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003168
3169#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3170 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3171 * processes datagrams and the fact that a datagram is allowed to have
3172 * several records in it, it is possible that the I/O buffers are not
3173 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003174 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3175 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003176#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003177
Jerry Yuba9c7272021-10-30 11:54:10 +08003178 /* mbedtls_platform_zeroize MUST be last one in this function */
3179 mbedtls_platform_zeroize( handshake,
3180 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003181}
3182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003183void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003184{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003185 if( session == NULL )
3186 return;
3187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003189 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003190#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003191
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003192#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003194#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003195
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003196 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003197}
3198
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003199#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003200
3201#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3202#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3203#else
3204#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3205#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3206
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003207#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003208
3209#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3210#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3211#else
3212#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3213#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3214
3215#if defined(MBEDTLS_SSL_ALPN)
3216#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3217#else
3218#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3219#endif /* MBEDTLS_SSL_ALPN */
3220
3221#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3222#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3223#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3224#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3225
3226#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3227 ( (uint32_t) ( \
3228 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3229 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3230 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3231 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3232 0u ) )
3233
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003234static unsigned char ssl_serialized_context_header[] = {
3235 MBEDTLS_VERSION_MAJOR,
3236 MBEDTLS_VERSION_MINOR,
3237 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003238 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3239 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3240 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3241 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3242 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003243};
3244
Paul Bakker5121ce52009-01-03 21:22:43 +00003245/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003246 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003247 *
3248 * The format of the serialized data is:
3249 * (in the presentation language of TLS, RFC 8446 section 3)
3250 *
3251 * // header
3252 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003253 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003254 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003255 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003256 * Note: When updating the format, remember to keep these
3257 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003258 *
3259 * // session sub-structure
3260 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3261 * // transform sub-structure
3262 * uint8 random[64]; // ServerHello.random+ClientHello.random
3263 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3264 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3265 * // fields from ssl_context
3266 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3267 * uint64 in_window_top; // DTLS: last validated record seq_num
3268 * uint64 in_window; // DTLS: bitmask for replay protection
3269 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3270 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3271 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3272 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3273 *
3274 * Note that many fields of the ssl_context or sub-structures are not
3275 * serialized, as they fall in one of the following categories:
3276 *
3277 * 1. forced value (eg in_left must be 0)
3278 * 2. pointer to dynamically-allocated memory (eg session, transform)
3279 * 3. value can be re-derived from other data (eg session keys from MS)
3280 * 4. value was temporary (eg content of input buffer)
3281 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003282 */
3283int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3284 unsigned char *buf,
3285 size_t buf_len,
3286 size_t *olen )
3287{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003288 unsigned char *p = buf;
3289 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003290 size_t session_len;
3291 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003292
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003293 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003294 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3295 * this function's documentation.
3296 *
3297 * These are due to assumptions/limitations in the implementation. Some of
3298 * them are likely to stay (no handshake in progress) some might go away
3299 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003300 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003301 /* The initial handshake must be over */
3302 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003303 {
3304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +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 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003308 {
3309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003310 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003311 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003312 /* Double-check that sub-structures are indeed ready */
3313 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003314 {
3315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003316 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003317 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003318 /* There must be no pending incoming or outgoing data */
3319 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003320 {
3321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming 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 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003325 {
3326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003327 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003328 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003329 /* Protocol must be DLTS, not TLS */
3330 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003331 {
3332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003333 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003334 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003335 /* Version must be 1.2 */
3336 if( ssl->major_ver != MBEDTLS_SSL_MAJOR_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 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003342 {
3343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003344 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003345 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003346 /* We must be using an AEAD ciphersuite */
3347 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003348 {
3349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003350 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003351 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003352 /* Renegotiation must not be enabled */
3353#if defined(MBEDTLS_SSL_RENEGOTIATION)
3354 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003355 {
3356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003357 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003358 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003359#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003360
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003361 /*
3362 * Version and format identifier
3363 */
3364 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003365
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003366 if( used <= buf_len )
3367 {
3368 memcpy( p, ssl_serialized_context_header,
3369 sizeof( ssl_serialized_context_header ) );
3370 p += sizeof( ssl_serialized_context_header );
3371 }
3372
3373 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003374 * Session (length + data)
3375 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003376 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003377 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3378 return( ret );
3379
3380 used += 4 + session_len;
3381 if( used <= buf_len )
3382 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003383 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3384 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003385
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003386 ret = ssl_session_save( ssl->session, 1,
3387 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003388 if( ret != 0 )
3389 return( ret );
3390
3391 p += session_len;
3392 }
3393
3394 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003395 * Transform
3396 */
3397 used += sizeof( ssl->transform->randbytes );
3398 if( used <= buf_len )
3399 {
3400 memcpy( p, ssl->transform->randbytes,
3401 sizeof( ssl->transform->randbytes ) );
3402 p += sizeof( ssl->transform->randbytes );
3403 }
3404
3405#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3406 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3407 if( used <= buf_len )
3408 {
3409 *p++ = ssl->transform->in_cid_len;
3410 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3411 p += ssl->transform->in_cid_len;
3412
3413 *p++ = ssl->transform->out_cid_len;
3414 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3415 p += ssl->transform->out_cid_len;
3416 }
3417#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3418
3419 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003420 * Saved fields from top-level ssl_context structure
3421 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003422 used += 4;
3423 if( used <= buf_len )
3424 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003425 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3426 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003427 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003428
3429#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3430 used += 16;
3431 if( used <= buf_len )
3432 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003433 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3434 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003435
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003436 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3437 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003438 }
3439#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3440
3441#if defined(MBEDTLS_SSL_PROTO_DTLS)
3442 used += 1;
3443 if( used <= buf_len )
3444 {
3445 *p++ = ssl->disable_datagram_packing;
3446 }
3447#endif /* MBEDTLS_SSL_PROTO_DTLS */
3448
Jerry Yuae0b2e22021-10-08 15:21:19 +08003449 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003450 if( used <= buf_len )
3451 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003452 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3453 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003454 }
3455
3456#if defined(MBEDTLS_SSL_PROTO_DTLS)
3457 used += 2;
3458 if( used <= buf_len )
3459 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003460 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
3461 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003462 }
3463#endif /* MBEDTLS_SSL_PROTO_DTLS */
3464
3465#if defined(MBEDTLS_SSL_ALPN)
3466 {
3467 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02003468 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003469 : 0;
3470
3471 used += 1 + alpn_len;
3472 if( used <= buf_len )
3473 {
3474 *p++ = alpn_len;
3475
3476 if( ssl->alpn_chosen != NULL )
3477 {
3478 memcpy( p, ssl->alpn_chosen, alpn_len );
3479 p += alpn_len;
3480 }
3481 }
3482 }
3483#endif /* MBEDTLS_SSL_ALPN */
3484
3485 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003486 * Done
3487 */
3488 *olen = used;
3489
3490 if( used > buf_len )
3491 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003492
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003493 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
3494
Hanno Becker43aefe22020-02-05 10:44:56 +00003495 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003496}
3497
3498/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003499 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003500 *
3501 * This internal version is wrapped by a public function that cleans up in
3502 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003503 */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003504static int ssl_context_load( mbedtls_ssl_context *ssl,
3505 const unsigned char *buf,
3506 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003507{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003508 const unsigned char *p = buf;
3509 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003510 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00003511 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003512
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003513 /*
3514 * The context should have been freshly setup or reset.
3515 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02003516 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003517 * renegotiating, or if the user mistakenly loaded a session first.)
3518 */
3519 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
3520 ssl->session != NULL )
3521 {
3522 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3523 }
3524
3525 /*
3526 * We can't check that the config matches the initial one, but we can at
3527 * least check it matches the requirements for serializing.
3528 */
3529 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
3530 ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
3531 ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 ||
3532 ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ||
3533 ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 ||
3534#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003535 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003536#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003537 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003538 {
3539 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3540 }
3541
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003542 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
3543
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003544 /*
3545 * Check version identifier
3546 */
3547 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
3548 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3549
3550 if( memcmp( p, ssl_serialized_context_header,
3551 sizeof( ssl_serialized_context_header ) ) != 0 )
3552 {
3553 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3554 }
3555 p += sizeof( ssl_serialized_context_header );
3556
3557 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003558 * Session
3559 */
3560 if( (size_t)( end - p ) < 4 )
3561 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3562
3563 session_len = ( (size_t) p[0] << 24 ) |
3564 ( (size_t) p[1] << 16 ) |
3565 ( (size_t) p[2] << 8 ) |
3566 ( (size_t) p[3] );
3567 p += 4;
3568
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003569 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00003570 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003571 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003572 ssl->session_in = ssl->session;
3573 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003574 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003575
3576 if( (size_t)( end - p ) < session_len )
3577 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3578
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003579 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003580 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003581 {
3582 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003583 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003584 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003585
3586 p += session_len;
3587
3588 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003589 * Transform
3590 */
3591
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003592 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00003593 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003594 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003595 ssl->transform_in = ssl->transform;
3596 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003597 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003598
3599 /* Read random bytes and populate structure */
3600 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
3601 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08003602#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00003603 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003604 ssl->session->ciphersuite,
3605 ssl->session->master,
Hanno Beckerbd257552021-03-22 06:59:27 +00003606#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
3607 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003608 ssl->session->encrypt_then_mac,
Hanno Beckerbd257552021-03-22 06:59:27 +00003609#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
3610 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003611 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
3612 p, /* currently pointing to randbytes */
3613 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
3614 ssl->conf->endpoint,
3615 ssl );
3616 if( ret != 0 )
3617 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08003618#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003619 p += sizeof( ssl->transform->randbytes );
3620
3621#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3622 /* Read connection IDs and store them */
3623 if( (size_t)( end - p ) < 1 )
3624 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3625
3626 ssl->transform->in_cid_len = *p++;
3627
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02003628 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003629 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3630
3631 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
3632 p += ssl->transform->in_cid_len;
3633
3634 ssl->transform->out_cid_len = *p++;
3635
3636 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
3637 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3638
3639 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
3640 p += ssl->transform->out_cid_len;
3641#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3642
3643 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003644 * Saved fields from top-level ssl_context structure
3645 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003646 if( (size_t)( end - p ) < 4 )
3647 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3648
3649 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
3650 ( (uint32_t) p[1] << 16 ) |
3651 ( (uint32_t) p[2] << 8 ) |
3652 ( (uint32_t) p[3] );
3653 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003654
3655#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3656 if( (size_t)( end - p ) < 16 )
3657 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3658
3659 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
3660 ( (uint64_t) p[1] << 48 ) |
3661 ( (uint64_t) p[2] << 40 ) |
3662 ( (uint64_t) p[3] << 32 ) |
3663 ( (uint64_t) p[4] << 24 ) |
3664 ( (uint64_t) p[5] << 16 ) |
3665 ( (uint64_t) p[6] << 8 ) |
3666 ( (uint64_t) p[7] );
3667 p += 8;
3668
3669 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
3670 ( (uint64_t) p[1] << 48 ) |
3671 ( (uint64_t) p[2] << 40 ) |
3672 ( (uint64_t) p[3] << 32 ) |
3673 ( (uint64_t) p[4] << 24 ) |
3674 ( (uint64_t) p[5] << 16 ) |
3675 ( (uint64_t) p[6] << 8 ) |
3676 ( (uint64_t) p[7] );
3677 p += 8;
3678#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3679
3680#if defined(MBEDTLS_SSL_PROTO_DTLS)
3681 if( (size_t)( end - p ) < 1 )
3682 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3683
3684 ssl->disable_datagram_packing = *p++;
3685#endif /* MBEDTLS_SSL_PROTO_DTLS */
3686
Jerry Yud9a94fe2021-09-28 18:58:59 +08003687 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003688 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08003689 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
3690 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003691
3692#if defined(MBEDTLS_SSL_PROTO_DTLS)
3693 if( (size_t)( end - p ) < 2 )
3694 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3695
3696 ssl->mtu = ( p[0] << 8 ) | p[1];
3697 p += 2;
3698#endif /* MBEDTLS_SSL_PROTO_DTLS */
3699
3700#if defined(MBEDTLS_SSL_ALPN)
3701 {
3702 uint8_t alpn_len;
3703 const char **cur;
3704
3705 if( (size_t)( end - p ) < 1 )
3706 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3707
3708 alpn_len = *p++;
3709
3710 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
3711 {
3712 /* alpn_chosen should point to an item in the configured list */
3713 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
3714 {
3715 if( strlen( *cur ) == alpn_len &&
3716 memcmp( p, cur, alpn_len ) == 0 )
3717 {
3718 ssl->alpn_chosen = *cur;
3719 break;
3720 }
3721 }
3722 }
3723
3724 /* can only happen on conf mismatch */
3725 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
3726 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3727
3728 p += alpn_len;
3729 }
3730#endif /* MBEDTLS_SSL_ALPN */
3731
3732 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003733 * Forced fields from top-level ssl_context structure
3734 *
3735 * Most of them already set to the correct value by mbedtls_ssl_init() and
3736 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
3737 */
3738 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
3739
3740 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
3741 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
3742
Hanno Becker361b10d2019-08-30 10:42:49 +01003743 /* Adjust pointers for header fields of outgoing records to
3744 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00003745 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01003746
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003747#if defined(MBEDTLS_SSL_PROTO_DTLS)
3748 ssl->in_epoch = 1;
3749#endif
3750
3751 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
3752 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00003753 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
3754 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003755 if( ssl->handshake != NULL )
3756 {
3757 mbedtls_ssl_handshake_free( ssl );
3758 mbedtls_free( ssl->handshake );
3759 ssl->handshake = NULL;
3760 }
3761
3762 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003763 * Done - should have consumed entire buffer
3764 */
3765 if( p != end )
3766 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003767
3768 return( 0 );
3769}
3770
3771/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003772 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003773 */
3774int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
3775 const unsigned char *buf,
3776 size_t len )
3777{
3778 int ret = ssl_context_load( context, buf, len );
3779
3780 if( ret != 0 )
3781 mbedtls_ssl_free( context );
3782
3783 return( ret );
3784}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003785#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003786
3787/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003788 * Free an SSL context
3789 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003791{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003792 if( ssl == NULL )
3793 return;
3794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003796
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003797 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003798 {
sander-visserb8aa2072020-05-06 22:05:13 +02003799#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3800 size_t out_buf_len = ssl->out_buf_len;
3801#else
3802 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
3803#endif
3804
Darryl Greenb33cc762019-11-28 14:29:44 +00003805 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003806 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003807 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003808 }
3809
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003810 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003811 {
sander-visserb8aa2072020-05-06 22:05:13 +02003812#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3813 size_t in_buf_len = ssl->in_buf_len;
3814#else
3815 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
3816#endif
3817
Darryl Greenb33cc762019-11-28 14:29:44 +00003818 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003819 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003820 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003821 }
3822
Paul Bakker48916f92012-09-16 19:57:18 +00003823 if( ssl->transform )
3824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003825 mbedtls_ssl_transform_free( ssl->transform );
3826 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003827 }
3828
3829 if( ssl->handshake )
3830 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02003831 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003832 mbedtls_ssl_transform_free( ssl->transform_negotiate );
3833 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00003834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003835 mbedtls_free( ssl->handshake );
3836 mbedtls_free( ssl->transform_negotiate );
3837 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00003838 }
3839
Ronald Cron6f135e12021-12-08 16:57:54 +01003840#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01003841 mbedtls_ssl_transform_free( ssl->transform_application );
3842 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01003843#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01003844
Paul Bakkerc0463502013-02-14 11:19:38 +01003845 if( ssl->session )
3846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003847 mbedtls_ssl_session_free( ssl->session );
3848 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003849 }
3850
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02003851#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02003852 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003853 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003854 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00003856 }
Paul Bakker0be444a2013-08-27 21:55:01 +02003857#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003858
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003859#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02003861#endif
3862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00003864
Paul Bakker86f04f42013-02-14 11:20:09 +01003865 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003866 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003867}
3868
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02003869/*
3870 * Initialze mbedtls_ssl_config
3871 */
3872void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
3873{
3874 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
3875}
3876
Gilles Peskineeccd8882020-03-10 12:19:08 +01003877#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003878#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskineae270bf2021-06-02 00:05:29 +02003879/* The selection should be the same as mbedtls_x509_crt_profile_default in
Gilles Peskinea28f0f52021-06-02 15:29:38 +02003880 * x509_crt.c. Here, the order matters. Currently we favor stronger hashes,
3881 * for no fundamental reason.
Gilles Peskineae270bf2021-06-02 00:05:29 +02003882 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
3883 * about this list. */
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003884static int ssl_preset_default_hashes[] = {
3885#if defined(MBEDTLS_SHA512_C)
3886 MBEDTLS_MD_SHA512,
Mateusz Starzyk3352a532021-04-06 14:28:22 +02003887#endif
3888#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003889 MBEDTLS_MD_SHA384,
3890#endif
3891#if defined(MBEDTLS_SHA256_C)
3892 MBEDTLS_MD_SHA256,
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02003893#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003894 MBEDTLS_MD_NONE
3895};
Jerry Yuf017ee42022-01-12 15:49:48 +08003896#endif /* !MBEDTLS_DEPRECATED_REMOVED */
3897#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003898
Gilles Peskineae270bf2021-06-02 00:05:29 +02003899/* The selection should be the same as mbedtls_x509_crt_profile_default in
3900 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02003901 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02003902 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02003903 * about this list.
3904 */
Brett Warrene0edc842021-08-17 09:53:13 +01003905static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02003906#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003907 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003908#endif
3909#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003910 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003911#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02003912#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003913 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003914#endif
3915#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003916 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003917#endif
3918#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003919 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003920#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02003921#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003922 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003923#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02003924#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003925 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003926#endif
3927#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003928 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003929#endif
Brett Warrene0edc842021-08-17 09:53:13 +01003930 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02003931};
Gilles Peskineae270bf2021-06-02 00:05:29 +02003932
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003933static int ssl_preset_suiteb_ciphersuites[] = {
3934 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
3935 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
3936 0
3937};
3938
Gilles Peskineeccd8882020-03-10 12:19:08 +01003939#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003940#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003941static int ssl_preset_suiteb_hashes[] = {
Jerry Yuf017ee42022-01-12 15:49:48 +08003942#if defined(MBEDTLS_SHA256_C)
3943 MBEDTLS_MD_SHA256,
3944#endif
Jerry Yu713013f2022-01-17 18:16:35 +08003945#if defined(MBEDTLS_SHA384_C)
3946 MBEDTLS_MD_SHA384,
3947#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003948 MBEDTLS_MD_NONE
3949};
Jerry Yuf017ee42022-01-12 15:49:48 +08003950#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01003951
Jerry Yu909df7b2022-01-22 11:56:27 +08003952/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08003953 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08003954 * rules SHOULD be upheld.
3955 * - No duplicate entries.
3956 * - But if there is a good reason, do not change the order of the algorithms.
3957 * - ssl_tls12_present* is for TLS 1.2 use only.
3958 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08003959 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01003960static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08003961
Jerry Yued5e9f42022-01-26 11:21:34 +08003962#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \
3963 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
3964 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
3965#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C &&
3966 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08003967
Jerry Yu909df7b2022-01-22 11:56:27 +08003968#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \
3969 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
3970 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
3971#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
3972 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
3973
Jerry Yued5e9f42022-01-26 11:21:34 +08003974#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA512_C) && \
3975 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
3976 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
3977#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
3978 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08003979
Jerry Yu909df7b2022-01-22 11:56:27 +08003980#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C)
3981 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
3982#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */
3983
Ronald Cron8540cf62022-03-16 08:01:09 +01003984#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA512_C)
3985 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
3986#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
3987
3988#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA384_C)
3989 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
3990#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
3991
Jerry Yu53037892022-01-25 11:02:06 +08003992#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C)
3993 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
3994#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
3995
Jerry Yu909df7b2022-01-22 11:56:27 +08003996 MBEDTLS_TLS1_3_SIG_NONE
3997};
3998
3999/* NOTICE: see above */
4000#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4001static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Jerry Yu713013f2022-01-17 18:16:35 +08004002#if defined(MBEDTLS_SHA512_C)
4003 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA512 )
4004#endif
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004005#if defined(MBEDTLS_SHA384_C)
4006 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 )
4007#endif
4008#if defined(MBEDTLS_SHA256_C)
4009 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 )
4010#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08004011 MBEDTLS_TLS1_3_SIG_NONE
4012};
4013#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4014/* NOTICE: see above */
4015static uint16_t ssl_preset_suiteb_sig_algs[] = {
4016
Jerry Yu909df7b2022-01-22 11:56:27 +08004017#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \
4018 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4019 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
4020#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C &&
4021 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4022
Jerry Yu53037892022-01-25 11:02:06 +08004023#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \
4024 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4025 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
4026#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
4027 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004028
4029#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C)
4030 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4031#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004032
Jerry Yu53037892022-01-25 11:02:06 +08004033#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C)
4034 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4035#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4036
Jerry Yu713013f2022-01-17 18:16:35 +08004037 MBEDTLS_TLS1_3_SIG_NONE
4038};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004039
Jerry Yu909df7b2022-01-22 11:56:27 +08004040/* NOTICE: see above */
4041#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4042static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Jerry Yu713013f2022-01-17 18:16:35 +08004043#if defined(MBEDTLS_SHA256_C)
4044 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 )
4045#endif
Jerry Yu18c833e2022-01-25 10:55:47 +08004046#if defined(MBEDTLS_SHA384_C)
4047 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 )
4048#endif
Xiaofei Bai95395012021-11-25 08:51:30 +00004049 MBEDTLS_TLS1_3_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004050};
Jerry Yu909df7b2022-01-22 11:56:27 +08004051#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4052
Jerry Yu1a8b4812022-01-20 17:56:50 +08004053#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004054
Brett Warrene0edc842021-08-17 09:53:13 +01004055static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004056#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004057 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004058#endif
4059#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004060 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004061#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004062 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004063};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004064
Jerry Yu1a8b4812022-01-20 17:56:50 +08004065#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004066/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004067 * to make sure there are no duplicated signature algorithm entries. */
Jerry Yuf377d642022-01-25 10:43:59 +08004068static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004069{
4070 size_t i, j;
4071 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004072
Jerry Yuf377d642022-01-25 10:43:59 +08004073 for( i = 0; sig_algs[i] != MBEDTLS_TLS1_3_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004074 {
Jerry Yuf377d642022-01-25 10:43:59 +08004075 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004076 {
Jerry Yuf377d642022-01-25 10:43:59 +08004077 if( sig_algs[i] != sig_algs[j] )
4078 continue;
4079 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4080 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4081 sig_algs[i], j, i );
4082 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004083 }
4084 }
4085 return( ret );
4086}
4087
4088#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4089
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004090/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004091 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004092 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004093int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004094 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004095{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004096#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004097 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004098#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004099
Jerry Yu1a8b4812022-01-20 17:56:50 +08004100#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004101 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004102 {
4103 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4104 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4105 }
4106
Jerry Yuf377d642022-01-25 10:43:59 +08004107 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004108 {
4109 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4110 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4111 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004112
4113#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004114 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004115 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004116 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004117 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4118 }
4119
Jerry Yuf377d642022-01-25 10:43:59 +08004120 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004121 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004122 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004123 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4124 }
4125#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004126#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4127
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004128 /* Use the functions here so that they are covered in tests,
4129 * but otherwise access member directly for efficiency */
4130 mbedtls_ssl_conf_endpoint( conf, endpoint );
4131 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004132
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004133 /*
4134 * Things that are common to all presets
4135 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004136#if defined(MBEDTLS_SSL_CLI_C)
4137 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4138 {
4139 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4140#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4141 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4142#endif
4143 }
4144#endif
4145
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004146#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4147 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4148#endif
4149
4150#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4151 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4152#endif
4153
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004154#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004155 conf->f_cookie_write = ssl_cookie_write_dummy;
4156 conf->f_cookie_check = ssl_cookie_check_dummy;
4157#endif
4158
4159#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4160 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4161#endif
4162
Janos Follath088ce432017-04-10 12:42:31 +01004163#if defined(MBEDTLS_SSL_SRV_C)
4164 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004165 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004166#endif
4167
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004168#if defined(MBEDTLS_SSL_PROTO_DTLS)
4169 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4170 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4171#endif
4172
4173#if defined(MBEDTLS_SSL_RENEGOTIATION)
4174 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004175 memset( conf->renego_period, 0x00, 2 );
4176 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004177#endif
4178
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004179#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004180 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4181 {
4182 const unsigned char dhm_p[] =
4183 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4184 const unsigned char dhm_g[] =
4185 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004186
Hanno Beckere2defad2021-07-24 05:59:17 +01004187 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4188 dhm_p, sizeof( dhm_p ),
4189 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4190 {
4191 return( ret );
4192 }
4193 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004194#endif
4195
Ronald Cron6f135e12021-12-08 16:57:54 +01004196#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker71f1ed62021-07-24 06:01:47 +01004197 /*
4198 * Allow all TLS 1.3 key exchange modes by default.
4199 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004200 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004201#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004202
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004203 /*
4204 * Preset-specific defaults
4205 */
4206 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004207 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004208 /*
4209 * NSA Suite B
4210 */
4211 case MBEDTLS_SSL_PRESET_SUITEB:
4212 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
4213 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
4214 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004215#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4216 /* Hybrid TLS 1.2/1.3 is not supported yet */
4217 conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
4218#else
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004219 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004220#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3 */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004221
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004222 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004223
4224#if defined(MBEDTLS_X509_CRT_PARSE_C)
4225 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004226#endif
4227
Gilles Peskineeccd8882020-03-10 12:19:08 +01004228#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004229#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004230 conf->sig_hashes = ssl_preset_suiteb_hashes;
Jerry Yuf017ee42022-01-12 15:49:48 +08004231#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004232#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4233 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4234 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4235 else
4236#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4237 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004238#endif
4239
Brett Warrene0edc842021-08-17 09:53:13 +01004240#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4241 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004242#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004243 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004244 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004245
4246 /*
4247 * Default
4248 */
4249 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03004250 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
4251 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
4252 MBEDTLS_SSL_MIN_MAJOR_VERSION :
4253 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
4254 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
4255 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
4256 MBEDTLS_SSL_MIN_MINOR_VERSION :
4257 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004258 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004259#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4260 /* Hybrid TLS 1.2/1.3 is not supported yet */
4261 conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
4262#else
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004263 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004264#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3 */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004265
4266#if defined(MBEDTLS_SSL_PROTO_DTLS)
4267 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
TRodziewiczef73f012021-05-13 14:53:36 +02004268 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004269#endif
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004270 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004271
4272#if defined(MBEDTLS_X509_CRT_PARSE_C)
4273 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4274#endif
4275
Gilles Peskineeccd8882020-03-10 12:19:08 +01004276#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004277#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01004278 conf->sig_hashes = ssl_preset_default_hashes;
Jerry Yuf017ee42022-01-12 15:49:48 +08004279#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004280#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4281 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4282 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4283 else
4284#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4285 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004286#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004287
Brett Warrene0edc842021-08-17 09:53:13 +01004288#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4289 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004290#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004291 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004292
4293#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4294 conf->dhm_min_bitlen = 1024;
4295#endif
4296 }
4297
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004298 return( 0 );
4299}
4300
4301/*
4302 * Free mbedtls_ssl_config
4303 */
4304void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4305{
4306#if defined(MBEDTLS_DHM_C)
4307 mbedtls_mpi_free( &conf->dhm_P );
4308 mbedtls_mpi_free( &conf->dhm_G );
4309#endif
4310
Gilles Peskineeccd8882020-03-10 12:19:08 +01004311#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004312 if( conf->psk != NULL )
4313 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004314 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004315 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004316 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004317 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004318 }
4319
4320 if( conf->psk_identity != NULL )
4321 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004322 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004323 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004324 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004325 conf->psk_identity_len = 0;
4326 }
4327#endif
4328
4329#if defined(MBEDTLS_X509_CRT_PARSE_C)
4330 ssl_key_cert_free( conf->key_cert );
4331#endif
4332
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004333 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004334}
4335
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004336#if defined(MBEDTLS_PK_C) && \
4337 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004338/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004339 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004340 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004341unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004342{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004343#if defined(MBEDTLS_RSA_C)
4344 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4345 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004346#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004347#if defined(MBEDTLS_ECDSA_C)
4348 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4349 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004350#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004351 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004352}
4353
Hanno Becker7e5437a2017-04-28 17:15:26 +01004354unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4355{
4356 switch( type ) {
4357 case MBEDTLS_PK_RSA:
4358 return( MBEDTLS_SSL_SIG_RSA );
4359 case MBEDTLS_PK_ECDSA:
4360 case MBEDTLS_PK_ECKEY:
4361 return( MBEDTLS_SSL_SIG_ECDSA );
4362 default:
4363 return( MBEDTLS_SSL_SIG_ANON );
4364 }
4365}
4366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004367mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004368{
4369 switch( sig )
4370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004371#if defined(MBEDTLS_RSA_C)
4372 case MBEDTLS_SSL_SIG_RSA:
4373 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004374#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004375#if defined(MBEDTLS_ECDSA_C)
4376 case MBEDTLS_SSL_SIG_ECDSA:
4377 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004378#endif
4379 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004380 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004381 }
4382}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004383#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004384
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004385/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004386 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004388mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004389{
4390 switch( hash )
4391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004392#if defined(MBEDTLS_MD5_C)
4393 case MBEDTLS_SSL_HASH_MD5:
4394 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004395#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004396#if defined(MBEDTLS_SHA1_C)
4397 case MBEDTLS_SSL_HASH_SHA1:
4398 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004399#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004400#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004401 case MBEDTLS_SSL_HASH_SHA224:
4402 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004403#endif
4404#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004405 case MBEDTLS_SSL_HASH_SHA256:
4406 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004407#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004408#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004409 case MBEDTLS_SSL_HASH_SHA384:
4410 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004411#endif
4412#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004413 case MBEDTLS_SSL_HASH_SHA512:
4414 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004415#endif
4416 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004417 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004418 }
4419}
4420
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004421/*
4422 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4423 */
4424unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4425{
4426 switch( md )
4427 {
4428#if defined(MBEDTLS_MD5_C)
4429 case MBEDTLS_MD_MD5:
4430 return( MBEDTLS_SSL_HASH_MD5 );
4431#endif
4432#if defined(MBEDTLS_SHA1_C)
4433 case MBEDTLS_MD_SHA1:
4434 return( MBEDTLS_SSL_HASH_SHA1 );
4435#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004436#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004437 case MBEDTLS_MD_SHA224:
4438 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004439#endif
4440#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004441 case MBEDTLS_MD_SHA256:
4442 return( MBEDTLS_SSL_HASH_SHA256 );
4443#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004444#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004445 case MBEDTLS_MD_SHA384:
4446 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004447#endif
4448#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004449 case MBEDTLS_MD_SHA512:
4450 return( MBEDTLS_SSL_HASH_SHA512 );
4451#endif
4452 default:
4453 return( MBEDTLS_SSL_HASH_NONE );
4454 }
4455}
4456
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004457/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004458 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004459 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004460 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004461int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004462{
Brett Warrene0edc842021-08-17 09:53:13 +01004463 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004464
Brett Warrene0edc842021-08-17 09:53:13 +01004465 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004466 return( -1 );
4467
Brett Warrene0edc842021-08-17 09:53:13 +01004468 for( ; *group_list != 0; group_list++ )
4469 {
4470 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004471 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01004472 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004473
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004474 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004475}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004476
4477#if defined(MBEDTLS_ECP_C)
4478/*
4479 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
4480 */
4481int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
4482{
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01004483 uint16_t tls_id = mbedtls_ecp_curve_info_from_grp_id( grp_id )->tls_id;
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004484 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
4485}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004486#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004488#if defined(MBEDTLS_X509_CRT_PARSE_C)
4489int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
4490 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004491 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02004492 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004493{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004494 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004495 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004496 const char *ext_oid;
4497 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004499 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004500 {
4501 /* Server part of the key exchange */
4502 switch( ciphersuite->key_exchange )
4503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004504 case MBEDTLS_KEY_EXCHANGE_RSA:
4505 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004506 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004507 break;
4508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004509 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
4510 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
4511 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
4512 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004513 break;
4514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004515 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
4516 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004517 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004518 break;
4519
4520 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004521 case MBEDTLS_KEY_EXCHANGE_NONE:
4522 case MBEDTLS_KEY_EXCHANGE_PSK:
4523 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
4524 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02004525 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004526 usage = 0;
4527 }
4528 }
4529 else
4530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004531 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
4532 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004533 }
4534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004535 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004536 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004537 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004538 ret = -1;
4539 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004541 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004543 ext_oid = MBEDTLS_OID_SERVER_AUTH;
4544 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004545 }
4546 else
4547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004548 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
4549 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004550 }
4551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004552 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004553 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004554 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004555 ret = -1;
4556 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004557
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004558 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004559}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004560#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004561
Jerry Yu148165c2021-09-24 23:20:59 +08004562#if defined(MBEDTLS_USE_PSA_CRYPTO)
4563int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
4564 const mbedtls_md_type_t md,
4565 unsigned char *dst,
4566 size_t dst_len,
4567 size_t *olen )
4568{
Ronald Cronf6893e12022-01-07 22:09:01 +01004569 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4570 psa_hash_operation_t *hash_operation_to_clone;
4571 psa_hash_operation_t hash_operation = psa_hash_operation_init();
4572
Jerry Yu148165c2021-09-24 23:20:59 +08004573 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01004574
4575 switch( md )
4576 {
4577#if defined(MBEDTLS_SHA384_C)
4578 case MBEDTLS_MD_SHA384:
4579 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
4580 break;
4581#endif
4582
4583#if defined(MBEDTLS_SHA256_C)
4584 case MBEDTLS_MD_SHA256:
4585 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
4586 break;
4587#endif
4588
4589 default:
4590 goto exit;
4591 }
4592
4593 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
4594 if( status != PSA_SUCCESS )
4595 goto exit;
4596
4597 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
4598 if( status != PSA_SUCCESS )
4599 goto exit;
4600
4601exit:
Ronald Cronb788c042022-02-15 09:14:15 +01004602 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08004603}
4604#else /* MBEDTLS_USE_PSA_CRYPTO */
4605
Jerry Yu24c0ec32021-09-09 14:21:07 +08004606#if defined(MBEDTLS_SHA384_C)
Jerry Yu000f9762021-09-14 11:12:51 +08004607static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
4608 unsigned char *dst,
4609 size_t dst_len,
4610 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004611{
Jerry Yu24c0ec32021-09-09 14:21:07 +08004612 int ret;
4613 mbedtls_sha512_context sha512;
4614
4615 if( dst_len < 48 )
4616 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4617
4618 mbedtls_sha512_init( &sha512 );
4619 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
4620
4621 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
4622 {
4623 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
4624 goto exit;
4625 }
4626
4627 *olen = 48;
4628
4629exit:
4630
4631 mbedtls_sha512_free( &sha512 );
4632 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004633}
4634#endif /* MBEDTLS_SHA384_C */
4635
4636#if defined(MBEDTLS_SHA256_C)
Jerry Yu000f9762021-09-14 11:12:51 +08004637static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
4638 unsigned char *dst,
4639 size_t dst_len,
4640 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004641{
Jerry Yu24c0ec32021-09-09 14:21:07 +08004642 int ret;
4643 mbedtls_sha256_context sha256;
4644
4645 if( dst_len < 32 )
4646 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4647
4648 mbedtls_sha256_init( &sha256 );
4649 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08004650
Jerry Yu24c0ec32021-09-09 14:21:07 +08004651 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
4652 {
4653 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
4654 goto exit;
4655 }
4656
4657 *olen = 32;
4658
4659exit:
4660
4661 mbedtls_sha256_free( &sha256 );
4662 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004663}
4664#endif /* MBEDTLS_SHA256_C */
4665
Jerry Yu000f9762021-09-14 11:12:51 +08004666int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
4667 const mbedtls_md_type_t md,
4668 unsigned char *dst,
4669 size_t dst_len,
4670 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004671{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004672 switch( md )
4673 {
4674
Jerry Yu24c0ec32021-09-09 14:21:07 +08004675#if defined(MBEDTLS_SHA384_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004676 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08004677 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08004678#endif /* MBEDTLS_SHA384_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004679
Jerry Yu24c0ec32021-09-09 14:21:07 +08004680#if defined(MBEDTLS_SHA256_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004681 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08004682 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08004683#endif /* MBEDTLS_SHA256_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004684
4685 default:
4686 break;
4687 }
Jerry Yuc5aef882021-12-23 20:15:02 +08004688 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004689}
XiaokangQian647719a2021-12-07 09:16:29 +00004690
Jerry Yu148165c2021-09-24 23:20:59 +08004691#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08004692
Jerry Yudc7bd172022-02-17 13:44:15 +08004693#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4694
4695#if defined(MBEDTLS_USE_PSA_CRYPTO)
4696
4697static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
4698 mbedtls_svc_key_id_t key,
4699 psa_algorithm_t alg,
4700 const unsigned char* seed, size_t seed_length,
4701 const unsigned char* label, size_t label_length,
4702 size_t capacity )
4703{
4704 psa_status_t status;
4705
4706 status = psa_key_derivation_setup( derivation, alg );
4707 if( status != PSA_SUCCESS )
4708 return( status );
4709
4710 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4711 {
4712 status = psa_key_derivation_input_bytes( derivation,
4713 PSA_KEY_DERIVATION_INPUT_SEED,
4714 seed, seed_length );
4715 if( status != PSA_SUCCESS )
4716 return( status );
4717
4718 if( mbedtls_svc_key_id_is_null( key ) )
4719 {
4720 status = psa_key_derivation_input_bytes(
4721 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
4722 NULL, 0 );
4723 }
4724 else
4725 {
4726 status = psa_key_derivation_input_key(
4727 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
4728 }
4729 if( status != PSA_SUCCESS )
4730 return( status );
4731
4732 status = psa_key_derivation_input_bytes( derivation,
4733 PSA_KEY_DERIVATION_INPUT_LABEL,
4734 label, label_length );
4735 if( status != PSA_SUCCESS )
4736 return( status );
4737 }
4738 else
4739 {
4740 return( PSA_ERROR_NOT_SUPPORTED );
4741 }
4742
4743 status = psa_key_derivation_set_capacity( derivation, capacity );
4744 if( status != PSA_SUCCESS )
4745 return( status );
4746
4747 return( PSA_SUCCESS );
4748}
4749
4750static int tls_prf_generic( mbedtls_md_type_t md_type,
4751 const unsigned char *secret, size_t slen,
4752 const char *label,
4753 const unsigned char *random, size_t rlen,
4754 unsigned char *dstbuf, size_t dlen )
4755{
4756 psa_status_t status;
4757 psa_algorithm_t alg;
4758 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
4759 psa_key_derivation_operation_t derivation =
4760 PSA_KEY_DERIVATION_OPERATION_INIT;
4761
4762 if( md_type == MBEDTLS_MD_SHA384 )
4763 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
4764 else
4765 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
4766
4767 /* Normally a "secret" should be long enough to be impossible to
4768 * find by brute force, and in particular should not be empty. But
4769 * this PRF is also used to derive an IV, in particular in EAP-TLS,
4770 * and for this use case it makes sense to have a 0-length "secret".
4771 * Since the key API doesn't allow importing a key of length 0,
4772 * keep master_key=0, which setup_psa_key_derivation() understands
4773 * to mean a 0-length "secret" input. */
4774 if( slen != 0 )
4775 {
4776 psa_key_attributes_t key_attributes = psa_key_attributes_init();
4777 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
4778 psa_set_key_algorithm( &key_attributes, alg );
4779 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
4780
4781 status = psa_import_key( &key_attributes, secret, slen, &master_key );
4782 if( status != PSA_SUCCESS )
4783 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4784 }
4785
4786 status = setup_psa_key_derivation( &derivation,
4787 master_key, alg,
4788 random, rlen,
4789 (unsigned char const *) label,
4790 (size_t) strlen( label ),
4791 dlen );
4792 if( status != PSA_SUCCESS )
4793 {
4794 psa_key_derivation_abort( &derivation );
4795 psa_destroy_key( master_key );
4796 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4797 }
4798
4799 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
4800 if( status != PSA_SUCCESS )
4801 {
4802 psa_key_derivation_abort( &derivation );
4803 psa_destroy_key( master_key );
4804 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4805 }
4806
4807 status = psa_key_derivation_abort( &derivation );
4808 if( status != PSA_SUCCESS )
4809 {
4810 psa_destroy_key( master_key );
4811 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4812 }
4813
4814 if( ! mbedtls_svc_key_id_is_null( master_key ) )
4815 status = psa_destroy_key( master_key );
4816 if( status != PSA_SUCCESS )
4817 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
4818
4819 return( 0 );
4820}
4821
4822#else /* MBEDTLS_USE_PSA_CRYPTO */
4823
4824static int tls_prf_generic( mbedtls_md_type_t md_type,
4825 const unsigned char *secret, size_t slen,
4826 const char *label,
4827 const unsigned char *random, size_t rlen,
4828 unsigned char *dstbuf, size_t dlen )
4829{
4830 size_t nb;
4831 size_t i, j, k, md_len;
4832 unsigned char *tmp;
4833 size_t tmp_len = 0;
4834 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
4835 const mbedtls_md_info_t *md_info;
4836 mbedtls_md_context_t md_ctx;
4837 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4838
4839 mbedtls_md_init( &md_ctx );
4840
4841 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
4842 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4843
4844 md_len = mbedtls_md_get_size( md_info );
4845
4846 tmp_len = md_len + strlen( label ) + rlen;
4847 tmp = mbedtls_calloc( 1, tmp_len );
4848 if( tmp == NULL )
4849 {
4850 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
4851 goto exit;
4852 }
4853
4854 nb = strlen( label );
4855 memcpy( tmp + md_len, label, nb );
4856 memcpy( tmp + md_len + nb, random, rlen );
4857 nb += rlen;
4858
4859 /*
4860 * Compute P_<hash>(secret, label + random)[0..dlen]
4861 */
4862 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
4863 goto exit;
4864
4865 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
4866 if( ret != 0 )
4867 goto exit;
4868 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
4869 if( ret != 0 )
4870 goto exit;
4871 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
4872 if( ret != 0 )
4873 goto exit;
4874
4875 for( i = 0; i < dlen; i += md_len )
4876 {
4877 ret = mbedtls_md_hmac_reset ( &md_ctx );
4878 if( ret != 0 )
4879 goto exit;
4880 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
4881 if( ret != 0 )
4882 goto exit;
4883 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
4884 if( ret != 0 )
4885 goto exit;
4886
4887 ret = mbedtls_md_hmac_reset ( &md_ctx );
4888 if( ret != 0 )
4889 goto exit;
4890 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
4891 if( ret != 0 )
4892 goto exit;
4893 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
4894 if( ret != 0 )
4895 goto exit;
4896
4897 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
4898
4899 for( j = 0; j < k; j++ )
4900 dstbuf[i + j] = h_i[j];
4901 }
4902
4903exit:
4904 mbedtls_md_free( &md_ctx );
4905
4906 mbedtls_platform_zeroize( tmp, tmp_len );
4907 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
4908
4909 mbedtls_free( tmp );
4910
4911 return( ret );
4912}
4913#endif /* MBEDTLS_USE_PSA_CRYPTO */
4914
4915#if defined(MBEDTLS_SHA256_C)
4916static int tls_prf_sha256( const unsigned char *secret, size_t slen,
4917 const char *label,
4918 const unsigned char *random, size_t rlen,
4919 unsigned char *dstbuf, size_t dlen )
4920{
4921 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
4922 label, random, rlen, dstbuf, dlen ) );
4923}
4924#endif /* MBEDTLS_SHA256_C */
4925
4926#if defined(MBEDTLS_SHA384_C)
4927static int tls_prf_sha384( const unsigned char *secret, size_t slen,
4928 const char *label,
4929 const unsigned char *random, size_t rlen,
4930 unsigned char *dstbuf, size_t dlen )
4931{
4932 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
4933 label, random, rlen, dstbuf, dlen ) );
4934}
4935#endif /* MBEDTLS_SHA384_C */
4936
Jerry Yuf009d862022-02-17 14:01:37 +08004937/*
4938 * Set appropriate PRF function and other SSL / TLS1.2 functions
4939 *
4940 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08004941 * - hash associated with the ciphersuite (only used by TLS 1.2)
4942 *
4943 * Outputs:
4944 * - the tls_prf, calc_verify and calc_finished members of handshake structure
4945 */
4946static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08004947 mbedtls_md_type_t hash )
4948{
Jerry Yuf009d862022-02-17 14:01:37 +08004949#if defined(MBEDTLS_SHA384_C)
Ronald Cron81591aa2022-03-07 09:05:51 +01004950 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08004951 {
4952 handshake->tls_prf = tls_prf_sha384;
4953 handshake->calc_verify = ssl_calc_verify_tls_sha384;
4954 handshake->calc_finished = ssl_calc_finished_tls_sha384;
4955 }
4956 else
4957#endif
4958#if defined(MBEDTLS_SHA256_C)
Jerry Yuf009d862022-02-17 14:01:37 +08004959 {
Ronald Cron81591aa2022-03-07 09:05:51 +01004960 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08004961 handshake->tls_prf = tls_prf_sha256;
4962 handshake->calc_verify = ssl_calc_verify_tls_sha256;
4963 handshake->calc_finished = ssl_calc_finished_tls_sha256;
4964 }
Ronald Cron81591aa2022-03-07 09:05:51 +01004965#else
Jerry Yuf009d862022-02-17 14:01:37 +08004966 {
Jerry Yuf009d862022-02-17 14:01:37 +08004967 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01004968 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08004969 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4970 }
Ronald Cron81591aa2022-03-07 09:05:51 +01004971#endif
Jerry Yuf009d862022-02-17 14:01:37 +08004972
4973 return( 0 );
4974}
Jerry Yud6ab2352022-02-17 14:03:43 +08004975
4976#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
4977 defined(MBEDTLS_USE_PSA_CRYPTO)
4978static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
4979{
4980 if( ssl->conf->f_psk != NULL )
4981 {
4982 /* If we've used a callback to select the PSK,
4983 * the static configuration is irrelevant. */
4984 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
4985 return( 1 );
4986
4987 return( 0 );
4988 }
4989
4990 if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) )
4991 return( 1 );
4992
4993 return( 0 );
4994}
4995#endif /* MBEDTLS_USE_PSA_CRYPTO &&
4996 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
4997
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08004998/*
4999 * Compute master secret if needed
5000 *
5001 * Parameters:
5002 * [in/out] handshake
5003 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5004 * (PSA-PSK) ciphersuite_info, psk_opaque
5005 * [out] premaster (cleared)
5006 * [out] master
5007 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5008 * debug: conf->f_dbg, conf->p_dbg
5009 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005010 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005011 */
5012static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5013 unsigned char *master,
5014 const mbedtls_ssl_context *ssl )
5015{
5016 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5017
5018 /* cf. RFC 5246, Section 8.1:
5019 * "The master secret is always exactly 48 bytes in length." */
5020 size_t const master_secret_len = 48;
5021
5022#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5023 unsigned char session_hash[48];
5024#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5025
5026 /* The label for the KDF used for key expansion.
5027 * This is either "master secret" or "extended master secret"
5028 * depending on whether the Extended Master Secret extension
5029 * is used. */
5030 char const *lbl = "master secret";
5031
5032 /* The salt for the KDF used for key expansion.
5033 * - If the Extended Master Secret extension is not used,
5034 * this is ClientHello.Random + ServerHello.Random
5035 * (see Sect. 8.1 in RFC 5246).
5036 * - If the Extended Master Secret extension is used,
5037 * this is the transcript of the handshake so far.
5038 * (see Sect. 4 in RFC 7627). */
5039 unsigned char const *salt = handshake->randbytes;
5040 size_t salt_len = 64;
5041
5042#if !defined(MBEDTLS_DEBUG_C) && \
5043 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5044 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5045 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5046 ssl = NULL; /* make sure we don't use it except for those cases */
5047 (void) ssl;
5048#endif
5049
5050 if( handshake->resume != 0 )
5051 {
5052 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5053 return( 0 );
5054 }
5055
5056#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5057 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5058 {
5059 lbl = "extended master secret";
5060 salt = session_hash;
5061 handshake->calc_verify( ssl, session_hash, &salt_len );
5062
5063 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
5064 session_hash, salt_len );
5065 }
5066#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
5067
5068#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
5069 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5070 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005071 ssl_use_opaque_psk( ssl ) == 1 )
5072 {
5073 /* Perform PSK-to-MS expansion in a single step. */
5074 psa_status_t status;
5075 psa_algorithm_t alg;
5076 mbedtls_svc_key_id_t psk;
5077 psa_key_derivation_operation_t derivation =
5078 PSA_KEY_DERIVATION_OPERATION_INIT;
5079 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5080
5081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5082
5083 psk = mbedtls_ssl_get_opaque_psk( ssl );
5084
5085 if( hash_alg == MBEDTLS_MD_SHA384 )
5086 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5087 else
5088 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5089
5090 status = setup_psa_key_derivation( &derivation, psk, alg,
5091 salt, salt_len,
5092 (unsigned char const *) lbl,
5093 (size_t) strlen( lbl ),
5094 master_secret_len );
5095 if( status != PSA_SUCCESS )
5096 {
5097 psa_key_derivation_abort( &derivation );
5098 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5099 }
5100
5101 status = psa_key_derivation_output_bytes( &derivation,
5102 master,
5103 master_secret_len );
5104 if( status != PSA_SUCCESS )
5105 {
5106 psa_key_derivation_abort( &derivation );
5107 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5108 }
5109
5110 status = psa_key_derivation_abort( &derivation );
5111 if( status != PSA_SUCCESS )
5112 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5113 }
5114 else
5115#endif
5116 {
5117 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
5118 lbl, salt, salt_len,
5119 master,
5120 master_secret_len );
5121 if( ret != 0 )
5122 {
5123 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5124 return( ret );
5125 }
5126
5127 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5128 handshake->premaster,
5129 handshake->pmslen );
5130
5131 mbedtls_platform_zeroize( handshake->premaster,
5132 sizeof(handshake->premaster) );
5133 }
5134
5135 return( 0 );
5136}
5137
Jerry Yud62f87e2022-02-17 14:09:02 +08005138int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5139{
5140 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5141 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5142 ssl->handshake->ciphersuite_info;
5143
5144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5145
5146 /* Set PRF, calc_verify and calc_finished function pointers */
5147 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005148 ciphersuite_info->mac );
5149 if( ret != 0 )
5150 {
5151 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5152 return( ret );
5153 }
5154
5155 /* Compute master secret if needed */
5156 ret = ssl_compute_master( ssl->handshake,
5157 ssl->session_negotiate->master,
5158 ssl );
5159 if( ret != 0 )
5160 {
5161 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5162 return( ret );
5163 }
5164
5165 /* Swap the client and server random values:
5166 * - MS derivation wanted client+server (RFC 5246 8.1)
5167 * - key derivation wants server+client (RFC 5246 6.3) */
5168 {
5169 unsigned char tmp[64];
5170 memcpy( tmp, ssl->handshake->randbytes, 64 );
5171 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5172 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5173 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5174 }
5175
5176 /* Populate transform structure */
5177 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5178 ssl->session_negotiate->ciphersuite,
5179 ssl->session_negotiate->master,
5180#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
5181 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5182 ssl->session_negotiate->encrypt_then_mac,
5183#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
5184 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
5185 ssl->handshake->tls_prf,
5186 ssl->handshake->randbytes,
5187 ssl->minor_ver,
5188 ssl->conf->endpoint,
5189 ssl );
5190 if( ret != 0 )
5191 {
5192 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5193 return( ret );
5194 }
5195
5196 /* We no longer need Server/ClientHello.random values */
5197 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5198 sizeof( ssl->handshake->randbytes ) );
5199
5200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5201
5202 return( 0 );
5203}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005204
Ronald Cron4dcbca92022-03-07 10:21:40 +01005205int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5206{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005207 switch( md )
5208 {
5209#if defined(MBEDTLS_SHA384_C)
5210 case MBEDTLS_SSL_HASH_SHA384:
5211 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5212 break;
5213#endif
5214#if defined(MBEDTLS_SHA256_C)
5215 case MBEDTLS_SSL_HASH_SHA256:
5216 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5217 break;
5218#endif
5219 default:
5220 return( -1 );
5221 }
5222
Ronald Cronc2f13a02022-03-07 10:25:24 +01005223 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005224}
5225
Jerry Yu8392e0d2022-02-17 14:10:24 +08005226#if defined(MBEDTLS_SHA256_C)
5227void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5228 unsigned char *hash,
5229 size_t *hlen )
5230{
5231#if defined(MBEDTLS_USE_PSA_CRYPTO)
5232 size_t hash_size;
5233 psa_status_t status;
5234 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5235
5236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5237 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5238 if( status != PSA_SUCCESS )
5239 {
5240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5241 return;
5242 }
5243
5244 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5245 if( status != PSA_SUCCESS )
5246 {
5247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5248 return;
5249 }
5250
5251 *hlen = 32;
5252 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5254#else
5255 mbedtls_sha256_context sha256;
5256
5257 mbedtls_sha256_init( &sha256 );
5258
5259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5260
5261 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5262 mbedtls_sha256_finish( &sha256, hash );
5263
5264 *hlen = 32;
5265
5266 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5268
5269 mbedtls_sha256_free( &sha256 );
5270#endif /* MBEDTLS_USE_PSA_CRYPTO */
5271 return;
5272}
5273#endif /* MBEDTLS_SHA256_C */
5274
Jerry Yuc1cb3842022-02-17 14:13:48 +08005275#if defined(MBEDTLS_SHA384_C)
5276void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5277 unsigned char *hash,
5278 size_t *hlen )
5279{
5280#if defined(MBEDTLS_USE_PSA_CRYPTO)
5281 size_t hash_size;
5282 psa_status_t status;
5283 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5284
5285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5286 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5287 if( status != PSA_SUCCESS )
5288 {
5289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5290 return;
5291 }
5292
5293 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5294 if( status != PSA_SUCCESS )
5295 {
5296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5297 return;
5298 }
5299
5300 *hlen = 48;
5301 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5303#else
5304 mbedtls_sha512_context sha512;
5305
5306 mbedtls_sha512_init( &sha512 );
5307
5308 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
5309
5310 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
5311 mbedtls_sha512_finish( &sha512, hash );
5312
5313 *hlen = 48;
5314
5315 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5317
5318 mbedtls_sha512_free( &sha512 );
5319#endif /* MBEDTLS_USE_PSA_CRYPTO */
5320 return;
5321}
5322#endif /* MBEDTLS_SHA384_C */
5323
Jerry Yuce3dca42022-02-17 14:16:37 +08005324#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
5325int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
5326{
5327 unsigned char *p = ssl->handshake->premaster;
5328 unsigned char *end = p + sizeof( ssl->handshake->premaster );
5329 const unsigned char *psk = NULL;
5330 size_t psk_len = 0;
5331
5332 if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len )
5333 == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
5334 {
5335 /*
5336 * This should never happen because the existence of a PSK is always
5337 * checked before calling this function
5338 */
5339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5340 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5341 }
5342
5343 /*
5344 * PMS = struct {
5345 * opaque other_secret<0..2^16-1>;
5346 * opaque psk<0..2^16-1>;
5347 * };
5348 * with "other_secret" depending on the particular key exchange
5349 */
5350#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5351 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
5352 {
5353 if( end - p < 2 )
5354 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5355
5356 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5357 p += 2;
5358
5359 if( end < p || (size_t)( end - p ) < psk_len )
5360 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5361
5362 memset( p, 0, psk_len );
5363 p += psk_len;
5364 }
5365 else
5366#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5367#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
5368 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5369 {
5370 /*
5371 * other_secret already set by the ClientKeyExchange message,
5372 * and is 48 bytes long
5373 */
5374 if( end - p < 2 )
5375 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5376
5377 *p++ = 0;
5378 *p++ = 48;
5379 p += 48;
5380 }
5381 else
5382#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
5383#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
5384 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5385 {
5386 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5387 size_t len;
5388
5389 /* Write length only when we know the actual value */
5390 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
5391 p + 2, end - ( p + 2 ), &len,
5392 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5393 {
5394 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
5395 return( ret );
5396 }
5397 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
5398 p += 2 + len;
5399
5400 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
5401 }
5402 else
5403#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
5404#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
5405 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
5406 {
5407 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5408 size_t zlen;
5409
5410 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
5411 p + 2, end - ( p + 2 ),
5412 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5413 {
5414 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
5415 return( ret );
5416 }
5417
5418 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
5419 p += 2 + zlen;
5420
5421 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
5422 MBEDTLS_DEBUG_ECDH_Z );
5423 }
5424 else
5425#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
5426 {
5427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5428 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5429 }
5430
5431 /* opaque psk<0..2^16-1>; */
5432 if( end - p < 2 )
5433 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5434
5435 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5436 p += 2;
5437
5438 if( end < p || (size_t)( end - p ) < psk_len )
5439 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5440
5441 memcpy( p, psk, psk_len );
5442 p += psk_len;
5443
5444 ssl->handshake->pmslen = p - ssl->handshake->premaster;
5445
5446 return( 0 );
5447}
5448#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08005449
5450#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
5451static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
5452
5453#if defined(MBEDTLS_SSL_PROTO_DTLS)
5454int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
5455{
5456 /* If renegotiation is not enforced, retransmit until we would reach max
5457 * timeout if we were using the usual handshake doubling scheme */
5458 if( ssl->conf->renego_max_records < 0 )
5459 {
5460 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
5461 unsigned char doublings = 1;
5462
5463 while( ratio != 0 )
5464 {
5465 ++doublings;
5466 ratio >>= 1;
5467 }
5468
5469 if( ++ssl->renego_records_seen > doublings )
5470 {
5471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
5472 return( 0 );
5473 }
5474 }
5475
5476 return( ssl_write_hello_request( ssl ) );
5477}
5478#endif
5479#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08005480
Jerry Yud9526692022-02-17 14:23:47 +08005481/*
5482 * Handshake functions
5483 */
5484#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5485/* No certificate support -> dummy functions */
5486int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
5487{
5488 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5489 ssl->handshake->ciphersuite_info;
5490
5491 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
5492
5493 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5494 {
5495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5496 ssl->state++;
5497 return( 0 );
5498 }
5499
5500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5501 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5502}
5503
5504int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5505{
5506 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5507 ssl->handshake->ciphersuite_info;
5508
5509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5510
5511 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5512 {
5513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5514 ssl->state++;
5515 return( 0 );
5516 }
5517
5518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5519 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5520}
5521
5522#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5523/* Some certificate support -> implement write and parse */
5524
5525int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
5526{
5527 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
5528 size_t i, n;
5529 const mbedtls_x509_crt *crt;
5530 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5531 ssl->handshake->ciphersuite_info;
5532
5533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
5534
5535 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5536 {
5537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5538 ssl->state++;
5539 return( 0 );
5540 }
5541
5542#if defined(MBEDTLS_SSL_CLI_C)
5543 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5544 {
5545 if( ssl->handshake->client_auth == 0 )
5546 {
5547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5548 ssl->state++;
5549 return( 0 );
5550 }
5551 }
5552#endif /* MBEDTLS_SSL_CLI_C */
5553#if defined(MBEDTLS_SSL_SRV_C)
5554 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
5555 {
5556 if( mbedtls_ssl_own_cert( ssl ) == NULL )
5557 {
5558 /* Should never happen because we shouldn't have picked the
5559 * ciphersuite if we don't have a certificate. */
5560 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5561 }
5562 }
5563#endif
5564
5565 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
5566
5567 /*
5568 * 0 . 0 handshake type
5569 * 1 . 3 handshake length
5570 * 4 . 6 length of all certs
5571 * 7 . 9 length of cert. 1
5572 * 10 . n-1 peer certificate
5573 * n . n+2 length of cert. 2
5574 * n+3 . ... upper level cert, etc.
5575 */
5576 i = 7;
5577 crt = mbedtls_ssl_own_cert( ssl );
5578
5579 while( crt != NULL )
5580 {
5581 n = crt->raw.len;
5582 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
5583 {
5584 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
5585 " > %" MBEDTLS_PRINTF_SIZET,
5586 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
5587 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5588 }
5589
5590 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
5591 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
5592 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
5593
5594 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5595 i += n; crt = crt->next;
5596 }
5597
5598 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
5599 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
5600 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
5601
5602 ssl->out_msglen = i;
5603 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5604 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
5605
5606 ssl->state++;
5607
5608 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
5609 {
5610 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
5611 return( ret );
5612 }
5613
5614 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
5615
5616 return( ret );
5617}
5618
5619#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5620
5621#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5622static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5623 unsigned char *crt_buf,
5624 size_t crt_buf_len )
5625{
5626 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5627
5628 if( peer_crt == NULL )
5629 return( -1 );
5630
5631 if( peer_crt->raw.len != crt_buf_len )
5632 return( -1 );
5633
5634 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
5635}
5636#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5637static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5638 unsigned char *crt_buf,
5639 size_t crt_buf_len )
5640{
5641 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5642 unsigned char const * const peer_cert_digest =
5643 ssl->session->peer_cert_digest;
5644 mbedtls_md_type_t const peer_cert_digest_type =
5645 ssl->session->peer_cert_digest_type;
5646 mbedtls_md_info_t const * const digest_info =
5647 mbedtls_md_info_from_type( peer_cert_digest_type );
5648 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
5649 size_t digest_len;
5650
5651 if( peer_cert_digest == NULL || digest_info == NULL )
5652 return( -1 );
5653
5654 digest_len = mbedtls_md_get_size( digest_info );
5655 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
5656 return( -1 );
5657
5658 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
5659 if( ret != 0 )
5660 return( -1 );
5661
5662 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
5663}
5664#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5665#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5666
5667/*
5668 * Once the certificate message is read, parse it into a cert chain and
5669 * perform basic checks, but leave actual verification to the caller
5670 */
5671static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5672 mbedtls_x509_crt *chain )
5673{
5674 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5675#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5676 int crt_cnt=0;
5677#endif
5678 size_t i, n;
5679 uint8_t alert;
5680
5681 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5682 {
5683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5684 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5685 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5686 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5687 }
5688
5689 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
5690 {
5691 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5692 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5693 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5694 }
5695
5696 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
5697 {
5698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5699 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5700 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5701 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5702 }
5703
5704 i = mbedtls_ssl_hs_hdr_len( ssl );
5705
5706 /*
5707 * Same message structure as in mbedtls_ssl_write_certificate()
5708 */
5709 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
5710
5711 if( ssl->in_msg[i] != 0 ||
5712 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
5713 {
5714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5715 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5716 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5717 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5718 }
5719
5720 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5721 i += 3;
5722
5723 /* Iterate through and parse the CRTs in the provided chain. */
5724 while( i < ssl->in_hslen )
5725 {
5726 /* Check that there's room for the next CRT's length fields. */
5727 if ( i + 3 > ssl->in_hslen ) {
5728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5729 mbedtls_ssl_send_alert_message( ssl,
5730 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5731 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5732 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5733 }
5734 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
5735 * anything beyond 2**16 ~ 64K. */
5736 if( ssl->in_msg[i] != 0 )
5737 {
5738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5739 mbedtls_ssl_send_alert_message( ssl,
5740 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5741 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
5742 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
5743 }
5744
5745 /* Read length of the next CRT in the chain. */
5746 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5747 | (unsigned int) ssl->in_msg[i + 2];
5748 i += 3;
5749
5750 if( n < 128 || i + n > ssl->in_hslen )
5751 {
5752 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5753 mbedtls_ssl_send_alert_message( ssl,
5754 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5755 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5756 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5757 }
5758
5759 /* Check if we're handling the first CRT in the chain. */
5760#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5761 if( crt_cnt++ == 0 &&
5762 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5763 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
5764 {
5765 /* During client-side renegotiation, check that the server's
5766 * end-CRTs hasn't changed compared to the initial handshake,
5767 * mitigating the triple handshake attack. On success, reuse
5768 * the original end-CRT instead of parsing it again. */
5769 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
5770 if( ssl_check_peer_crt_unchanged( ssl,
5771 &ssl->in_msg[i],
5772 n ) != 0 )
5773 {
5774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
5775 mbedtls_ssl_send_alert_message( ssl,
5776 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5777 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
5778 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
5779 }
5780
5781 /* Now we can safely free the original chain. */
5782 ssl_clear_peer_cert( ssl->session );
5783 }
5784#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5785
5786 /* Parse the next certificate in the chain. */
5787#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5788 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
5789#else
5790 /* If we don't need to store the CRT chain permanently, parse
5791 * it in-place from the input buffer instead of making a copy. */
5792 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
5793#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5794 switch( ret )
5795 {
5796 case 0: /*ok*/
5797 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5798 /* Ignore certificate with an unknown algorithm: maybe a
5799 prior certificate was already trusted. */
5800 break;
5801
5802 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5803 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5804 goto crt_parse_der_failed;
5805
5806 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5807 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5808 goto crt_parse_der_failed;
5809
5810 default:
5811 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5812 crt_parse_der_failed:
5813 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
5814 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
5815 return( ret );
5816 }
5817
5818 i += n;
5819 }
5820
5821 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
5822 return( 0 );
5823}
5824
5825#if defined(MBEDTLS_SSL_SRV_C)
5826static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
5827{
5828 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5829 return( -1 );
5830
5831 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5832 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5833 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5834 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
5835 {
5836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
5837 return( 0 );
5838 }
5839 return( -1 );
5840}
5841#endif /* MBEDTLS_SSL_SRV_C */
5842
5843/* Check if a certificate message is expected.
5844 * Return either
5845 * - SSL_CERTIFICATE_EXPECTED, or
5846 * - SSL_CERTIFICATE_SKIP
5847 * indicating whether a Certificate message is expected or not.
5848 */
5849#define SSL_CERTIFICATE_EXPECTED 0
5850#define SSL_CERTIFICATE_SKIP 1
5851static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
5852 int authmode )
5853{
5854 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5855 ssl->handshake->ciphersuite_info;
5856
5857 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5858 return( SSL_CERTIFICATE_SKIP );
5859
5860#if defined(MBEDTLS_SSL_SRV_C)
5861 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
5862 {
5863 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5864 return( SSL_CERTIFICATE_SKIP );
5865
5866 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
5867 {
5868 ssl->session_negotiate->verify_result =
5869 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5870 return( SSL_CERTIFICATE_SKIP );
5871 }
5872 }
5873#else
5874 ((void) authmode);
5875#endif /* MBEDTLS_SSL_SRV_C */
5876
5877 return( SSL_CERTIFICATE_EXPECTED );
5878}
5879
5880static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
5881 int authmode,
5882 mbedtls_x509_crt *chain,
5883 void *rs_ctx )
5884{
5885 int ret = 0;
5886 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5887 ssl->handshake->ciphersuite_info;
5888 int have_ca_chain = 0;
5889
5890 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
5891 void *p_vrfy;
5892
5893 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
5894 return( 0 );
5895
5896 if( ssl->f_vrfy != NULL )
5897 {
5898 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
5899 f_vrfy = ssl->f_vrfy;
5900 p_vrfy = ssl->p_vrfy;
5901 }
5902 else
5903 {
5904 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
5905 f_vrfy = ssl->conf->f_vrfy;
5906 p_vrfy = ssl->conf->p_vrfy;
5907 }
5908
5909 /*
5910 * Main check: verify certificate
5911 */
5912#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
5913 if( ssl->conf->f_ca_cb != NULL )
5914 {
5915 ((void) rs_ctx);
5916 have_ca_chain = 1;
5917
5918 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
5919 ret = mbedtls_x509_crt_verify_with_ca_cb(
5920 chain,
5921 ssl->conf->f_ca_cb,
5922 ssl->conf->p_ca_cb,
5923 ssl->conf->cert_profile,
5924 ssl->hostname,
5925 &ssl->session_negotiate->verify_result,
5926 f_vrfy, p_vrfy );
5927 }
5928 else
5929#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
5930 {
5931 mbedtls_x509_crt *ca_chain;
5932 mbedtls_x509_crl *ca_crl;
5933
5934#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5935 if( ssl->handshake->sni_ca_chain != NULL )
5936 {
5937 ca_chain = ssl->handshake->sni_ca_chain;
5938 ca_crl = ssl->handshake->sni_ca_crl;
5939 }
5940 else
5941#endif
5942 {
5943 ca_chain = ssl->conf->ca_chain;
5944 ca_crl = ssl->conf->ca_crl;
5945 }
5946
5947 if( ca_chain != NULL )
5948 have_ca_chain = 1;
5949
5950 ret = mbedtls_x509_crt_verify_restartable(
5951 chain,
5952 ca_chain, ca_crl,
5953 ssl->conf->cert_profile,
5954 ssl->hostname,
5955 &ssl->session_negotiate->verify_result,
5956 f_vrfy, p_vrfy, rs_ctx );
5957 }
5958
5959 if( ret != 0 )
5960 {
5961 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
5962 }
5963
5964#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
5965 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
5966 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
5967#endif
5968
5969 /*
5970 * Secondary checks: always done, but change 'ret' only if it was 0
5971 */
5972
5973#if defined(MBEDTLS_ECP_C)
5974 {
5975 const mbedtls_pk_context *pk = &chain->pk;
5976
5977 /* If certificate uses an EC key, make sure the curve is OK */
5978 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
5979 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
5980 {
5981 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5982
5983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
5984 if( ret == 0 )
5985 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
5986 }
5987 }
5988#endif /* MBEDTLS_ECP_C */
5989
5990 if( mbedtls_ssl_check_cert_usage( chain,
5991 ciphersuite_info,
5992 ! ssl->conf->endpoint,
5993 &ssl->session_negotiate->verify_result ) != 0 )
5994 {
5995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
5996 if( ret == 0 )
5997 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
5998 }
5999
6000 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6001 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6002 * with details encoded in the verification flags. All other kinds
6003 * of error codes, including those from the user provided f_vrfy
6004 * functions, are treated as fatal and lead to a failure of
6005 * ssl_parse_certificate even if verification was optional. */
6006 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6007 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6008 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6009 {
6010 ret = 0;
6011 }
6012
6013 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6014 {
6015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6016 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6017 }
6018
6019 if( ret != 0 )
6020 {
6021 uint8_t alert;
6022
6023 /* The certificate may have been rejected for several reasons.
6024 Pick one and send the corresponding alert. Which alert to send
6025 may be a subject of debate in some cases. */
6026 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6027 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6028 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6029 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6030 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6031 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6032 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6033 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6034 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6035 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6036 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6037 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6038 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6039 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6040 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6041 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6042 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6043 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6044 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6045 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6046 else
6047 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6048 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6049 alert );
6050 }
6051
6052#if defined(MBEDTLS_DEBUG_C)
6053 if( ssl->session_negotiate->verify_result != 0 )
6054 {
6055 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6056 (unsigned int) ssl->session_negotiate->verify_result ) );
6057 }
6058 else
6059 {
6060 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6061 }
6062#endif /* MBEDTLS_DEBUG_C */
6063
6064 return( ret );
6065}
6066
6067#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6068static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6069 unsigned char *start, size_t len )
6070{
6071 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6072 /* Remember digest of the peer's end-CRT. */
6073 ssl->session_negotiate->peer_cert_digest =
6074 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6075 if( ssl->session_negotiate->peer_cert_digest == NULL )
6076 {
6077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6078 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6079 mbedtls_ssl_send_alert_message( ssl,
6080 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6081 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6082
6083 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6084 }
6085
6086 ret = mbedtls_md( mbedtls_md_info_from_type(
6087 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6088 start, len,
6089 ssl->session_negotiate->peer_cert_digest );
6090
6091 ssl->session_negotiate->peer_cert_digest_type =
6092 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6093 ssl->session_negotiate->peer_cert_digest_len =
6094 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6095
6096 return( ret );
6097}
6098
6099static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6100 unsigned char *start, size_t len )
6101{
6102 unsigned char *end = start + len;
6103 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6104
6105 /* Make a copy of the peer's raw public key. */
6106 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6107 ret = mbedtls_pk_parse_subpubkey( &start, end,
6108 &ssl->handshake->peer_pubkey );
6109 if( ret != 0 )
6110 {
6111 /* We should have parsed the public key before. */
6112 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6113 }
6114
6115 return( 0 );
6116}
6117#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6118
6119int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6120{
6121 int ret = 0;
6122 int crt_expected;
6123#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6124 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6125 ? ssl->handshake->sni_authmode
6126 : ssl->conf->authmode;
6127#else
6128 const int authmode = ssl->conf->authmode;
6129#endif
6130 void *rs_ctx = NULL;
6131 mbedtls_x509_crt *chain = NULL;
6132
6133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6134
6135 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6136 if( crt_expected == SSL_CERTIFICATE_SKIP )
6137 {
6138 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6139 goto exit;
6140 }
6141
6142#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6143 if( ssl->handshake->ecrs_enabled &&
6144 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6145 {
6146 chain = ssl->handshake->ecrs_peer_cert;
6147 ssl->handshake->ecrs_peer_cert = NULL;
6148 goto crt_verify;
6149 }
6150#endif
6151
6152 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6153 {
6154 /* mbedtls_ssl_read_record may have sent an alert already. We
6155 let it decide whether to alert. */
6156 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6157 goto exit;
6158 }
6159
6160#if defined(MBEDTLS_SSL_SRV_C)
6161 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6162 {
6163 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6164
6165 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6166 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6167
6168 goto exit;
6169 }
6170#endif /* MBEDTLS_SSL_SRV_C */
6171
6172 /* Clear existing peer CRT structure in case we tried to
6173 * reuse a session but it failed, and allocate a new one. */
6174 ssl_clear_peer_cert( ssl->session_negotiate );
6175
6176 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6177 if( chain == NULL )
6178 {
6179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6180 sizeof( mbedtls_x509_crt ) ) );
6181 mbedtls_ssl_send_alert_message( ssl,
6182 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6183 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6184
6185 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6186 goto exit;
6187 }
6188 mbedtls_x509_crt_init( chain );
6189
6190 ret = ssl_parse_certificate_chain( ssl, chain );
6191 if( ret != 0 )
6192 goto exit;
6193
6194#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6195 if( ssl->handshake->ecrs_enabled)
6196 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6197
6198crt_verify:
6199 if( ssl->handshake->ecrs_enabled)
6200 rs_ctx = &ssl->handshake->ecrs_ctx;
6201#endif
6202
6203 ret = ssl_parse_certificate_verify( ssl, authmode,
6204 chain, rs_ctx );
6205 if( ret != 0 )
6206 goto exit;
6207
6208#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6209 {
6210 unsigned char *crt_start, *pk_start;
6211 size_t crt_len, pk_len;
6212
6213 /* We parse the CRT chain without copying, so
6214 * these pointers point into the input buffer,
6215 * and are hence still valid after freeing the
6216 * CRT chain. */
6217
6218 crt_start = chain->raw.p;
6219 crt_len = chain->raw.len;
6220
6221 pk_start = chain->pk_raw.p;
6222 pk_len = chain->pk_raw.len;
6223
6224 /* Free the CRT structures before computing
6225 * digest and copying the peer's public key. */
6226 mbedtls_x509_crt_free( chain );
6227 mbedtls_free( chain );
6228 chain = NULL;
6229
6230 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6231 if( ret != 0 )
6232 goto exit;
6233
6234 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6235 if( ret != 0 )
6236 goto exit;
6237 }
6238#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6239 /* Pass ownership to session structure. */
6240 ssl->session_negotiate->peer_cert = chain;
6241 chain = NULL;
6242#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6243
6244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6245
6246exit:
6247
6248 if( ret == 0 )
6249 ssl->state++;
6250
6251#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6252 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6253 {
6254 ssl->handshake->ecrs_peer_cert = chain;
6255 chain = NULL;
6256 }
6257#endif
6258
6259 if( chain != NULL )
6260 {
6261 mbedtls_x509_crt_free( chain );
6262 mbedtls_free( chain );
6263 }
6264
6265 return( ret );
6266}
6267#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6268
Jerry Yu615bd6f2022-02-17 14:25:15 +08006269#if defined(MBEDTLS_SHA256_C)
6270static void ssl_calc_finished_tls_sha256(
6271 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6272{
6273 int len = 12;
6274 const char *sender;
6275 unsigned char padbuf[32];
6276#if defined(MBEDTLS_USE_PSA_CRYPTO)
6277 size_t hash_size;
6278 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
6279 psa_status_t status;
6280#else
6281 mbedtls_sha256_context sha256;
6282#endif
6283
6284 mbedtls_ssl_session *session = ssl->session_negotiate;
6285 if( !session )
6286 session = ssl->session;
6287
6288 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6289 ? "client finished"
6290 : "server finished";
6291
6292#if defined(MBEDTLS_USE_PSA_CRYPTO)
6293 sha256_psa = psa_hash_operation_init();
6294
6295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6296
6297 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6298 if( status != PSA_SUCCESS )
6299 {
6300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6301 return;
6302 }
6303
6304 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6305 if( status != PSA_SUCCESS )
6306 {
6307 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6308 return;
6309 }
6310 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6311#else
6312
6313 mbedtls_sha256_init( &sha256 );
6314
6315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
6316
6317 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6318
6319 /*
6320 * TLSv1.2:
6321 * hash = PRF( master, finished_label,
6322 * Hash( handshake ) )[0.11]
6323 */
6324
6325#if !defined(MBEDTLS_SHA256_ALT)
6326 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
6327 sha256.state, sizeof( sha256.state ) );
6328#endif
6329
6330 mbedtls_sha256_finish( &sha256, padbuf );
6331 mbedtls_sha256_free( &sha256 );
6332#endif /* MBEDTLS_USE_PSA_CRYPTO */
6333
6334 ssl->handshake->tls_prf( session->master, 48, sender,
6335 padbuf, 32, buf, len );
6336
6337 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6338
6339 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6340
6341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6342}
6343#endif /* MBEDTLS_SHA256_C */
6344
Jerry Yub7ba49e2022-02-17 14:25:53 +08006345
6346#if defined(MBEDTLS_SHA384_C)
6347
6348static void ssl_calc_finished_tls_sha384(
6349 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6350{
6351 int len = 12;
6352 const char *sender;
6353 unsigned char padbuf[48];
6354#if defined(MBEDTLS_USE_PSA_CRYPTO)
6355 size_t hash_size;
6356 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
6357 psa_status_t status;
6358#else
6359 mbedtls_sha512_context sha512;
6360#endif
6361
6362 mbedtls_ssl_session *session = ssl->session_negotiate;
6363 if( !session )
6364 session = ssl->session;
6365
6366 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6367 ? "client finished"
6368 : "server finished";
6369
6370#if defined(MBEDTLS_USE_PSA_CRYPTO)
6371 sha384_psa = psa_hash_operation_init();
6372
6373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6374
6375 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6376 if( status != PSA_SUCCESS )
6377 {
6378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6379 return;
6380 }
6381
6382 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
6383 if( status != PSA_SUCCESS )
6384 {
6385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6386 return;
6387 }
6388 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
6389#else
6390 mbedtls_sha512_init( &sha512 );
6391
6392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
6393
6394 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
6395
6396 /*
6397 * TLSv1.2:
6398 * hash = PRF( master, finished_label,
6399 * Hash( handshake ) )[0.11]
6400 */
6401
6402#if !defined(MBEDTLS_SHA512_ALT)
6403 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6404 sha512.state, sizeof( sha512.state ) );
6405#endif
6406 mbedtls_sha512_finish( &sha512, padbuf );
6407
6408 mbedtls_sha512_free( &sha512 );
6409#endif
6410
6411 ssl->handshake->tls_prf( session->master, 48, sender,
6412 padbuf, 48, buf, len );
6413
6414 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6415
6416 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6417
6418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6419}
6420#endif /* MBEDTLS_SHA384_C */
6421
Jerry Yuaef00152022-02-17 14:27:31 +08006422void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
6423{
6424 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
6425
6426 /*
6427 * Free our handshake params
6428 */
6429 mbedtls_ssl_handshake_free( ssl );
6430 mbedtls_free( ssl->handshake );
6431 ssl->handshake = NULL;
6432
6433 /*
6434 * Free the previous transform and swith in the current one
6435 */
6436 if( ssl->transform )
6437 {
6438 mbedtls_ssl_transform_free( ssl->transform );
6439 mbedtls_free( ssl->transform );
6440 }
6441 ssl->transform = ssl->transform_negotiate;
6442 ssl->transform_negotiate = NULL;
6443
6444 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
6445}
6446
Jerry Yu2a9fff52022-02-17 14:28:51 +08006447void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
6448{
6449 int resume = ssl->handshake->resume;
6450
6451 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
6452
6453#if defined(MBEDTLS_SSL_RENEGOTIATION)
6454 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6455 {
6456 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
6457 ssl->renego_records_seen = 0;
6458 }
6459#endif
6460
6461 /*
6462 * Free the previous session and switch in the current one
6463 */
6464 if( ssl->session )
6465 {
6466#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6467 /* RFC 7366 3.1: keep the EtM state */
6468 ssl->session_negotiate->encrypt_then_mac =
6469 ssl->session->encrypt_then_mac;
6470#endif
6471
6472 mbedtls_ssl_session_free( ssl->session );
6473 mbedtls_free( ssl->session );
6474 }
6475 ssl->session = ssl->session_negotiate;
6476 ssl->session_negotiate = NULL;
6477
6478 /*
6479 * Add cache entry
6480 */
6481 if( ssl->conf->f_set_cache != NULL &&
6482 ssl->session->id_len != 0 &&
6483 resume == 0 )
6484 {
6485 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
6486 ssl->session->id,
6487 ssl->session->id_len,
6488 ssl->session ) != 0 )
6489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
6490 }
6491
6492#if defined(MBEDTLS_SSL_PROTO_DTLS)
6493 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6494 ssl->handshake->flight != NULL )
6495 {
6496 /* Cancel handshake timer */
6497 mbedtls_ssl_set_timer( ssl, 0 );
6498
6499 /* Keep last flight around in case we need to resend it:
6500 * we need the handshake and transform structures for that */
6501 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
6502 }
6503 else
6504#endif
6505 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
6506
6507 ssl->state++;
6508
6509 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
6510}
6511
Jerry Yu3c8e47b2022-02-17 14:30:01 +08006512int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
6513{
6514 int ret, hash_len;
6515
6516 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
6517
6518 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
6519
6520 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
6521
6522 /*
6523 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6524 * may define some other value. Currently (early 2016), no defined
6525 * ciphersuite does this (and this is unlikely to change as activity has
6526 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6527 */
6528 hash_len = 12;
6529
6530#if defined(MBEDTLS_SSL_RENEGOTIATION)
6531 ssl->verify_data_len = hash_len;
6532 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
6533#endif
6534
6535 ssl->out_msglen = 4 + hash_len;
6536 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6537 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
6538
6539 /*
6540 * In case of session resuming, invert the client and server
6541 * ChangeCipherSpec messages order.
6542 */
6543 if( ssl->handshake->resume != 0 )
6544 {
6545#if defined(MBEDTLS_SSL_CLI_C)
6546 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6547 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
6548#endif
6549#if defined(MBEDTLS_SSL_SRV_C)
6550 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6551 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
6552#endif
6553 }
6554 else
6555 ssl->state++;
6556
6557 /*
6558 * Switch to our negotiated transform and session parameters for outbound
6559 * data.
6560 */
6561 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
6562
6563#if defined(MBEDTLS_SSL_PROTO_DTLS)
6564 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6565 {
6566 unsigned char i;
6567
6568 /* Remember current epoch settings for resending */
6569 ssl->handshake->alt_transform_out = ssl->transform_out;
6570 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
6571 sizeof( ssl->handshake->alt_out_ctr ) );
6572
6573 /* Set sequence_number to zero */
6574 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
6575
6576
6577 /* Increment epoch */
6578 for( i = 2; i > 0; i-- )
6579 if( ++ssl->cur_out_ctr[i - 1] != 0 )
6580 break;
6581
6582 /* The loop goes to its end iff the counter is wrapping */
6583 if( i == 0 )
6584 {
6585 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6586 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
6587 }
6588 }
6589 else
6590#endif /* MBEDTLS_SSL_PROTO_DTLS */
6591 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6592
6593 ssl->transform_out = ssl->transform_negotiate;
6594 ssl->session_out = ssl->session_negotiate;
6595
6596#if defined(MBEDTLS_SSL_PROTO_DTLS)
6597 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6598 mbedtls_ssl_send_flight_completed( ssl );
6599#endif
6600
6601 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6602 {
6603 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6604 return( ret );
6605 }
6606
6607#if defined(MBEDTLS_SSL_PROTO_DTLS)
6608 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6609 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6610 {
6611 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6612 return( ret );
6613 }
6614#endif
6615
6616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
6617
6618 return( 0 );
6619}
6620
Jerry Yu0b3d7c12022-02-17 14:30:51 +08006621#define SSL_MAX_HASH_LEN 12
6622
6623int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
6624{
6625 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6626 unsigned int hash_len = 12;
6627 unsigned char buf[SSL_MAX_HASH_LEN];
6628
6629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
6630
6631 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
6632
6633 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6634 {
6635 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6636 goto exit;
6637 }
6638
6639 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6640 {
6641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6642 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6643 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6644 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
6645 goto exit;
6646 }
6647
6648 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
6649 {
6650 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6651 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6652 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
6653 goto exit;
6654 }
6655
6656 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
6657 {
6658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6659 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6660 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6661 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
6662 goto exit;
6663 }
6664
6665 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
6666 buf, hash_len ) != 0 )
6667 {
6668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6669 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6670 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
6671 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
6672 goto exit;
6673 }
6674
6675#if defined(MBEDTLS_SSL_RENEGOTIATION)
6676 ssl->verify_data_len = hash_len;
6677 memcpy( ssl->peer_verify_data, buf, hash_len );
6678#endif
6679
6680 if( ssl->handshake->resume != 0 )
6681 {
6682#if defined(MBEDTLS_SSL_CLI_C)
6683 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6684 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
6685#endif
6686#if defined(MBEDTLS_SSL_SRV_C)
6687 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6688 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
6689#endif
6690 }
6691 else
6692 ssl->state++;
6693
6694#if defined(MBEDTLS_SSL_PROTO_DTLS)
6695 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6696 mbedtls_ssl_recv_flight_completed( ssl );
6697#endif
6698
6699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
6700
6701exit:
6702 mbedtls_platform_zeroize( buf, hash_len );
6703 return( ret );
6704}
6705
Jerry Yu392112c2022-02-17 14:34:10 +08006706#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
6707/*
6708 * Helper to get TLS 1.2 PRF from ciphersuite
6709 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
6710 */
6711static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
6712{
6713#if defined(MBEDTLS_SHA384_C)
6714 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6715 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
6716
6717 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
6718 return( tls_prf_sha384 );
6719#else
6720 (void) ciphersuite_id;
6721#endif
6722 return( tls_prf_sha256 );
6723}
6724#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08006725
6726static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
6727{
6728 ((void) tls_prf);
6729#if defined(MBEDTLS_SHA384_C)
6730 if( tls_prf == tls_prf_sha384 )
6731 {
6732 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
6733 }
6734 else
6735#endif
6736#if defined(MBEDTLS_SHA256_C)
6737 if( tls_prf == tls_prf_sha256 )
6738 {
6739 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
6740 }
6741 else
6742#endif
6743 return( MBEDTLS_SSL_TLS_PRF_NONE );
6744}
6745
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006746/*
6747 * Populate a transform structure with session keys and all the other
6748 * necessary information.
6749 *
6750 * Parameters:
6751 * - [in/out]: transform: structure to populate
6752 * [in] must be just initialised with mbedtls_ssl_transform_init()
6753 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
6754 * - [in] ciphersuite
6755 * - [in] master
6756 * - [in] encrypt_then_mac
6757 * - [in] compression
6758 * - [in] tls_prf: pointer to PRF to use for key derivation
6759 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
6760 * - [in] minor_ver: SSL/TLS minor version
6761 * - [in] endpoint: client or server
6762 * - [in] ssl: used for:
6763 * - ssl->conf->{f,p}_export_keys
6764 * [in] optionally used for:
6765 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
6766 */
6767static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
6768 int ciphersuite,
6769 const unsigned char master[48],
6770#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
6771 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6772 int encrypt_then_mac,
6773#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
6774 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
6775 ssl_tls_prf_t tls_prf,
6776 const unsigned char randbytes[64],
6777 int minor_ver,
6778 unsigned endpoint,
6779 const mbedtls_ssl_context *ssl )
6780{
6781 int ret = 0;
6782 unsigned char keyblk[256];
6783 unsigned char *key1;
6784 unsigned char *key2;
6785 unsigned char *mac_enc;
6786 unsigned char *mac_dec;
6787 size_t mac_key_len = 0;
6788 size_t iv_copy_len;
6789 size_t keylen;
6790 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
6791 const mbedtls_cipher_info_t *cipher_info;
6792 const mbedtls_md_info_t *md_info;
6793
6794#if defined(MBEDTLS_USE_PSA_CRYPTO)
6795 psa_key_type_t key_type;
6796 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6797 psa_algorithm_t alg;
6798 size_t key_bits;
6799 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
6800#endif
6801
6802#if !defined(MBEDTLS_DEBUG_C) && \
6803 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6804 if( ssl->f_export_keys == NULL )
6805 {
6806 ssl = NULL; /* make sure we don't use it except for these cases */
6807 (void) ssl;
6808 }
6809#endif
6810
6811 /*
6812 * Some data just needs copying into the structure
6813 */
6814#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
6815 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
6816 transform->encrypt_then_mac = encrypt_then_mac;
6817#endif
6818 transform->minor_ver = minor_ver;
6819
6820#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
6821 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
6822#endif
6823
6824#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
6825 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
6826 {
6827 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
6828 * generation separate. This should never happen. */
6829 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6830 }
6831#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
6832
6833 /*
6834 * Get various info structures
6835 */
6836 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
6837 if( ciphersuite_info == NULL )
6838 {
6839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
6840 ciphersuite ) );
6841 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6842 }
6843
6844 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
6845 if( cipher_info == NULL )
6846 {
6847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
6848 ciphersuite_info->cipher ) );
6849 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6850 }
6851
6852 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
6853 if( md_info == NULL )
6854 {
6855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
6856 (unsigned) ciphersuite_info->mac ) );
6857 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6858 }
6859
6860#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6861 /* Copy own and peer's CID if the use of the CID
6862 * extension has been negotiated. */
6863 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
6864 {
6865 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
6866
6867 transform->in_cid_len = ssl->own_cid_len;
6868 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
6869 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
6870 transform->in_cid_len );
6871
6872 transform->out_cid_len = ssl->handshake->peer_cid_len;
6873 memcpy( transform->out_cid, ssl->handshake->peer_cid,
6874 ssl->handshake->peer_cid_len );
6875 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
6876 transform->out_cid_len );
6877 }
6878#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6879
6880 /*
6881 * Compute key block using the PRF
6882 */
6883 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
6884 if( ret != 0 )
6885 {
6886 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
6887 return( ret );
6888 }
6889
6890 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
6891 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
6892 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
6893 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
6894 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
6895
6896 /*
6897 * Determine the appropriate key, IV and MAC length.
6898 */
6899
6900 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
6901
6902#if defined(MBEDTLS_GCM_C) || \
6903 defined(MBEDTLS_CCM_C) || \
6904 defined(MBEDTLS_CHACHAPOLY_C)
6905 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_GCM ||
6906 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CCM ||
6907 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
6908 {
6909 size_t explicit_ivlen;
6910
6911 transform->maclen = 0;
6912 mac_key_len = 0;
6913 transform->taglen =
6914 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
6915
6916 /* All modes haves 96-bit IVs, but the length of the static parts vary
6917 * with mode and version:
6918 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
6919 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
6920 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
6921 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
6922 * sequence number).
6923 */
6924 transform->ivlen = 12;
6925 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
6926 transform->fixed_ivlen = 12;
6927 else
6928 transform->fixed_ivlen = 4;
6929
6930 /* Minimum length of encrypted record */
6931 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
6932 transform->minlen = explicit_ivlen + transform->taglen;
6933 }
6934 else
6935#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
6936#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
6937 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM ||
6938 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
6939 {
Neil Armstrongcf8841a2022-02-24 11:17:45 +01006940#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006941 /* Initialize HMAC contexts */
6942 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
6943 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
6944 {
6945 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
6946 goto end;
6947 }
Neil Armstrongcf8841a2022-02-24 11:17:45 +01006948#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006949
6950 /* Get MAC length */
6951 mac_key_len = mbedtls_md_get_size( md_info );
6952 transform->maclen = mac_key_len;
6953
6954 /* IV length */
6955 transform->ivlen = cipher_info->iv_size;
6956
6957 /* Minimum length */
6958 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM )
6959 transform->minlen = transform->maclen;
6960 else
6961 {
6962 /*
6963 * GenericBlockCipher:
6964 * 1. if EtM is in use: one block plus MAC
6965 * otherwise: * first multiple of blocklen greater than maclen
6966 * 2. IV
6967 */
6968#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6969 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
6970 {
6971 transform->minlen = transform->maclen
6972 + cipher_info->block_size;
6973 }
6974 else
6975#endif
6976 {
6977 transform->minlen = transform->maclen
6978 + cipher_info->block_size
6979 - transform->maclen % cipher_info->block_size;
6980 }
6981
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006982 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
6983 {
6984 transform->minlen += transform->ivlen;
6985 }
6986 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08006987 {
6988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6989 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6990 goto end;
6991 }
6992 }
6993 }
6994 else
6995#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
6996 {
6997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6998 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6999 }
7000
7001 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7002 (unsigned) keylen,
7003 (unsigned) transform->minlen,
7004 (unsigned) transform->ivlen,
7005 (unsigned) transform->maclen ) );
7006
7007 /*
7008 * Finally setup the cipher contexts, IVs and MAC secrets.
7009 */
7010#if defined(MBEDTLS_SSL_CLI_C)
7011 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7012 {
7013 key1 = keyblk + mac_key_len * 2;
7014 key2 = keyblk + mac_key_len * 2 + keylen;
7015
7016 mac_enc = keyblk;
7017 mac_dec = keyblk + mac_key_len;
7018
7019 /*
7020 * This is not used in TLS v1.1.
7021 */
7022 iv_copy_len = ( transform->fixed_ivlen ) ?
7023 transform->fixed_ivlen : transform->ivlen;
7024 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7025 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7026 iv_copy_len );
7027 }
7028 else
7029#endif /* MBEDTLS_SSL_CLI_C */
7030#if defined(MBEDTLS_SSL_SRV_C)
7031 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7032 {
7033 key1 = keyblk + mac_key_len * 2 + keylen;
7034 key2 = keyblk + mac_key_len * 2;
7035
7036 mac_enc = keyblk + mac_key_len;
7037 mac_dec = keyblk;
7038
7039 /*
7040 * This is not used in TLS v1.1.
7041 */
7042 iv_copy_len = ( transform->fixed_ivlen ) ?
7043 transform->fixed_ivlen : transform->ivlen;
7044 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7045 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7046 iv_copy_len );
7047 }
7048 else
7049#endif /* MBEDTLS_SSL_SRV_C */
7050 {
7051 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7052 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7053 goto end;
7054 }
7055
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007056 if( ssl != NULL && ssl->f_export_keys != NULL )
7057 {
7058 ssl->f_export_keys( ssl->p_export_keys,
7059 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7060 master, 48,
7061 randbytes + 32,
7062 randbytes,
7063 tls_prf_get_type( tls_prf ) );
7064 }
7065
7066#if defined(MBEDTLS_USE_PSA_CRYPTO)
7067 if( ( status = mbedtls_ssl_cipher_to_psa( cipher_info->type,
7068 transform->taglen,
7069 &alg,
7070 &key_type,
7071 &key_bits ) ) != PSA_SUCCESS )
7072 {
7073 ret = psa_ssl_status_to_mbedtls( status );
7074 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7075 goto end;
7076 }
7077
7078 transform->psa_alg = alg;
7079
7080 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7081 {
7082 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7083 psa_set_key_algorithm( &attributes, alg );
7084 psa_set_key_type( &attributes, key_type );
7085
7086 if( ( status = psa_import_key( &attributes,
7087 key1,
7088 PSA_BITS_TO_BYTES( key_bits ),
7089 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7090 {
7091 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7092 ret = psa_ssl_status_to_mbedtls( status );
7093 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7094 goto end;
7095 }
7096
7097 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7098
7099 if( ( status = psa_import_key( &attributes,
7100 key2,
7101 PSA_BITS_TO_BYTES( key_bits ),
7102 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7103 {
7104 ret = psa_ssl_status_to_mbedtls( status );
7105 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7106 goto end;
7107 }
7108 }
7109#else
7110 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7111 cipher_info ) ) != 0 )
7112 {
7113 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7114 goto end;
7115 }
7116
7117 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7118 cipher_info ) ) != 0 )
7119 {
7120 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7121 goto end;
7122 }
7123
7124 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7125 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7126 MBEDTLS_ENCRYPT ) ) != 0 )
7127 {
7128 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7129 goto end;
7130 }
7131
7132 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7133 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7134 MBEDTLS_DECRYPT ) ) != 0 )
7135 {
7136 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7137 goto end;
7138 }
7139
7140#if defined(MBEDTLS_CIPHER_MODE_CBC)
7141 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7142 {
7143 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7144 MBEDTLS_PADDING_NONE ) ) != 0 )
7145 {
7146 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7147 goto end;
7148 }
7149
7150 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7151 MBEDTLS_PADDING_NONE ) ) != 0 )
7152 {
7153 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7154 goto end;
7155 }
7156 }
7157#endif /* MBEDTLS_CIPHER_MODE_CBC */
7158#endif /* MBEDTLS_USE_PSA_CRYPTO */
7159
Neil Armstrong29c0c042022-03-17 17:47:28 +01007160#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7161 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7162 For AEAD-based ciphersuites, there is nothing to do here. */
7163 if( mac_key_len != 0 )
7164 {
7165#if defined(MBEDTLS_USE_PSA_CRYPTO)
7166 alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
7167 if( alg == 0 )
7168 {
7169 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7170 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_md_type_to_psa", ret );
7171 goto end;
7172 }
7173
7174 transform->psa_mac_alg = PSA_ALG_HMAC( alg );
7175
7176 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
7177 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( alg ) );
7178 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7179
7180 if( ( status = psa_import_key( &attributes,
7181 mac_enc, mac_key_len,
7182 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7183 {
7184 ret = psa_ssl_status_to_mbedtls( status );
7185 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7186 goto end;
7187 }
7188
Ronald Cronfb39f152022-03-25 14:36:28 +01007189 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
7190 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) &&
7191 ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007192 /* mbedtls_ct_hmac() requires the key to be exportable */
7193 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7194 PSA_KEY_USAGE_VERIFY_HASH );
7195 else
7196 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7197
7198 if( ( status = psa_import_key( &attributes,
7199 mac_dec, mac_key_len,
7200 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7201 {
7202 ret = psa_ssl_status_to_mbedtls( status );
7203 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7204 goto end;
7205 }
7206#else
7207 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7208 if( ret != 0 )
7209 goto end;
7210 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7211 if( ret != 0 )
7212 goto end;
7213#endif /* MBEDTLS_USE_PSA_CRYPTO */
7214 }
7215#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7216
7217 ((void) mac_dec);
7218 ((void) mac_enc);
7219
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007220end:
7221 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7222 return( ret );
7223}
7224
Jerry Yuee40f9d2022-02-17 14:55:16 +08007225#if defined(MBEDTLS_USE_PSA_CRYPTO)
7226int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7227 unsigned char *hash, size_t *hashlen,
7228 unsigned char *data, size_t data_len,
7229 mbedtls_md_type_t md_alg )
7230{
7231 psa_status_t status;
7232 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
7233 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
7234
7235 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7236
7237 if( ( status = psa_hash_setup( &hash_operation,
7238 hash_alg ) ) != PSA_SUCCESS )
7239 {
7240 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7241 goto exit;
7242 }
7243
7244 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
7245 64 ) ) != PSA_SUCCESS )
7246 {
7247 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7248 goto exit;
7249 }
7250
7251 if( ( status = psa_hash_update( &hash_operation,
7252 data, data_len ) ) != PSA_SUCCESS )
7253 {
7254 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7255 goto exit;
7256 }
7257
7258 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
7259 hashlen ) ) != PSA_SUCCESS )
7260 {
7261 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
7262 goto exit;
7263 }
7264
7265exit:
7266 if( status != PSA_SUCCESS )
7267 {
7268 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7269 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7270 switch( status )
7271 {
7272 case PSA_ERROR_NOT_SUPPORTED:
7273 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
7274 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
7275 case PSA_ERROR_BUFFER_TOO_SMALL:
7276 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
7277 case PSA_ERROR_INSUFFICIENT_MEMORY:
7278 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
7279 default:
7280 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
7281 }
7282 }
7283 return( 0 );
7284}
7285
7286#else
7287
7288int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7289 unsigned char *hash, size_t *hashlen,
7290 unsigned char *data, size_t data_len,
7291 mbedtls_md_type_t md_alg )
7292{
7293 int ret = 0;
7294 mbedtls_md_context_t ctx;
7295 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
7296 *hashlen = mbedtls_md_get_size( md_info );
7297
7298 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
7299
7300 mbedtls_md_init( &ctx );
7301
7302 /*
7303 * digitally-signed struct {
7304 * opaque client_random[32];
7305 * opaque server_random[32];
7306 * ServerDHParams params;
7307 * };
7308 */
7309 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
7310 {
7311 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7312 goto exit;
7313 }
7314 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
7315 {
7316 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
7317 goto exit;
7318 }
7319 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
7320 {
7321 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7322 goto exit;
7323 }
7324 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
7325 {
7326 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7327 goto exit;
7328 }
7329 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
7330 {
7331 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
7332 goto exit;
7333 }
7334
7335exit:
7336 mbedtls_md_free( &ctx );
7337
7338 if( ret != 0 )
7339 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7340 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7341
7342 return( ret );
7343}
7344#endif /* MBEDTLS_USE_PSA_CRYPTO */
7345
Jerry Yud9d91da2022-02-17 14:57:06 +08007346#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
7347
7348/* Find an entry in a signature-hash set matching a given hash algorithm. */
7349mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
7350 mbedtls_pk_type_t sig_alg )
7351{
7352 switch( sig_alg )
7353 {
7354 case MBEDTLS_PK_RSA:
7355 return( set->rsa );
7356 case MBEDTLS_PK_ECDSA:
7357 return( set->ecdsa );
7358 default:
7359 return( MBEDTLS_MD_NONE );
7360 }
7361}
7362
7363/* Add a signature-hash-pair to a signature-hash set */
7364void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
7365 mbedtls_pk_type_t sig_alg,
7366 mbedtls_md_type_t md_alg )
7367{
7368 switch( sig_alg )
7369 {
7370 case MBEDTLS_PK_RSA:
7371 if( set->rsa == MBEDTLS_MD_NONE )
7372 set->rsa = md_alg;
7373 break;
7374
7375 case MBEDTLS_PK_ECDSA:
7376 if( set->ecdsa == MBEDTLS_MD_NONE )
7377 set->ecdsa = md_alg;
7378 break;
7379
7380 default:
7381 break;
7382 }
7383}
7384
7385/* Allow exactly one hash algorithm for each signature. */
7386void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
7387 mbedtls_md_type_t md_alg )
7388{
7389 set->rsa = md_alg;
7390 set->ecdsa = md_alg;
7391}
7392
7393#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7394
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08007395/* Serialization of TLS 1.2 sessions:
7396 *
7397 * struct {
7398 * uint64 start_time;
7399 * uint8 ciphersuite[2]; // defined by the standard
7400 * uint8 compression; // 0 or 1
7401 * uint8 session_id_len; // at most 32
7402 * opaque session_id[32];
7403 * opaque master[48]; // fixed length in the standard
7404 * uint32 verify_result;
7405 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
7406 * opaque ticket<0..2^24-1>; // length 0 means no ticket
7407 * uint32 ticket_lifetime;
7408 * uint8 mfl_code; // up to 255 according to standard
7409 * uint8 encrypt_then_mac; // 0 or 1
7410 * } serialized_session_tls12;
7411 *
7412 */
7413static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session,
7414 unsigned char *buf,
7415 size_t buf_len )
7416{
7417 unsigned char *p = buf;
7418 size_t used = 0;
7419
7420#if defined(MBEDTLS_HAVE_TIME)
7421 uint64_t start;
7422#endif
7423#if defined(MBEDTLS_X509_CRT_PARSE_C)
7424#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7425 size_t cert_len;
7426#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7427#endif /* MBEDTLS_X509_CRT_PARSE_C */
7428
7429 /*
7430 * Time
7431 */
7432#if defined(MBEDTLS_HAVE_TIME)
7433 used += 8;
7434
7435 if( used <= buf_len )
7436 {
7437 start = (uint64_t) session->start;
7438
7439 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
7440 p += 8;
7441 }
7442#endif /* MBEDTLS_HAVE_TIME */
7443
7444 /*
7445 * Basic mandatory fields
7446 */
7447 used += 2 /* ciphersuite */
7448 + 1 /* compression */
7449 + 1 /* id_len */
7450 + sizeof( session->id )
7451 + sizeof( session->master )
7452 + 4; /* verify_result */
7453
7454 if( used <= buf_len )
7455 {
7456 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
7457 p += 2;
7458
7459 *p++ = MBEDTLS_BYTE_0( session->compression );
7460
7461 *p++ = MBEDTLS_BYTE_0( session->id_len );
7462 memcpy( p, session->id, 32 );
7463 p += 32;
7464
7465 memcpy( p, session->master, 48 );
7466 p += 48;
7467
7468 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
7469 p += 4;
7470 }
7471
7472 /*
7473 * Peer's end-entity certificate
7474 */
7475#if defined(MBEDTLS_X509_CRT_PARSE_C)
7476#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7477 if( session->peer_cert == NULL )
7478 cert_len = 0;
7479 else
7480 cert_len = session->peer_cert->raw.len;
7481
7482 used += 3 + cert_len;
7483
7484 if( used <= buf_len )
7485 {
7486 *p++ = MBEDTLS_BYTE_2( cert_len );
7487 *p++ = MBEDTLS_BYTE_1( cert_len );
7488 *p++ = MBEDTLS_BYTE_0( cert_len );
7489
7490 if( session->peer_cert != NULL )
7491 {
7492 memcpy( p, session->peer_cert->raw.p, cert_len );
7493 p += cert_len;
7494 }
7495 }
7496#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7497 if( session->peer_cert_digest != NULL )
7498 {
7499 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
7500 if( used <= buf_len )
7501 {
7502 *p++ = (unsigned char) session->peer_cert_digest_type;
7503 *p++ = (unsigned char) session->peer_cert_digest_len;
7504 memcpy( p, session->peer_cert_digest,
7505 session->peer_cert_digest_len );
7506 p += session->peer_cert_digest_len;
7507 }
7508 }
7509 else
7510 {
7511 used += 2;
7512 if( used <= buf_len )
7513 {
7514 *p++ = (unsigned char) MBEDTLS_MD_NONE;
7515 *p++ = 0;
7516 }
7517 }
7518#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7519#endif /* MBEDTLS_X509_CRT_PARSE_C */
7520
7521 /*
7522 * Session ticket if any, plus associated data
7523 */
7524#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7525 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
7526
7527 if( used <= buf_len )
7528 {
7529 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
7530 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
7531 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
7532
7533 if( session->ticket != NULL )
7534 {
7535 memcpy( p, session->ticket, session->ticket_len );
7536 p += session->ticket_len;
7537 }
7538
7539 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
7540 p += 4;
7541 }
7542#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7543
7544 /*
7545 * Misc extension-related info
7546 */
7547#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7548 used += 1;
7549
7550 if( used <= buf_len )
7551 *p++ = session->mfl_code;
7552#endif
7553
7554#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7555 used += 1;
7556
7557 if( used <= buf_len )
7558 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
7559#endif
7560
7561 return( used );
7562}
7563
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08007564static int ssl_session_load_tls12( mbedtls_ssl_session *session,
7565 const unsigned char *buf,
7566 size_t len )
7567{
7568#if defined(MBEDTLS_HAVE_TIME)
7569 uint64_t start;
7570#endif
7571#if defined(MBEDTLS_X509_CRT_PARSE_C)
7572#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7573 size_t cert_len;
7574#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7575#endif /* MBEDTLS_X509_CRT_PARSE_C */
7576
7577 const unsigned char *p = buf;
7578 const unsigned char * const end = buf + len;
7579
7580 /*
7581 * Time
7582 */
7583#if defined(MBEDTLS_HAVE_TIME)
7584 if( 8 > (size_t)( end - p ) )
7585 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7586
7587 start = ( (uint64_t) p[0] << 56 ) |
7588 ( (uint64_t) p[1] << 48 ) |
7589 ( (uint64_t) p[2] << 40 ) |
7590 ( (uint64_t) p[3] << 32 ) |
7591 ( (uint64_t) p[4] << 24 ) |
7592 ( (uint64_t) p[5] << 16 ) |
7593 ( (uint64_t) p[6] << 8 ) |
7594 ( (uint64_t) p[7] );
7595 p += 8;
7596
7597 session->start = (time_t) start;
7598#endif /* MBEDTLS_HAVE_TIME */
7599
7600 /*
7601 * Basic mandatory fields
7602 */
7603 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
7604 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7605
7606 session->ciphersuite = ( p[0] << 8 ) | p[1];
7607 p += 2;
7608
7609 session->compression = *p++;
7610
7611 session->id_len = *p++;
7612 memcpy( session->id, p, 32 );
7613 p += 32;
7614
7615 memcpy( session->master, p, 48 );
7616 p += 48;
7617
7618 session->verify_result = ( (uint32_t) p[0] << 24 ) |
7619 ( (uint32_t) p[1] << 16 ) |
7620 ( (uint32_t) p[2] << 8 ) |
7621 ( (uint32_t) p[3] );
7622 p += 4;
7623
7624 /* Immediately clear invalid pointer values that have been read, in case
7625 * we exit early before we replaced them with valid ones. */
7626#if defined(MBEDTLS_X509_CRT_PARSE_C)
7627#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7628 session->peer_cert = NULL;
7629#else
7630 session->peer_cert_digest = NULL;
7631#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7632#endif /* MBEDTLS_X509_CRT_PARSE_C */
7633#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7634 session->ticket = NULL;
7635#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7636
7637 /*
7638 * Peer certificate
7639 */
7640#if defined(MBEDTLS_X509_CRT_PARSE_C)
7641#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7642 /* Deserialize CRT from the end of the ticket. */
7643 if( 3 > (size_t)( end - p ) )
7644 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7645
7646 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
7647 p += 3;
7648
7649 if( cert_len != 0 )
7650 {
7651 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7652
7653 if( cert_len > (size_t)( end - p ) )
7654 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7655
7656 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7657
7658 if( session->peer_cert == NULL )
7659 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7660
7661 mbedtls_x509_crt_init( session->peer_cert );
7662
7663 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
7664 p, cert_len ) ) != 0 )
7665 {
7666 mbedtls_x509_crt_free( session->peer_cert );
7667 mbedtls_free( session->peer_cert );
7668 session->peer_cert = NULL;
7669 return( ret );
7670 }
7671
7672 p += cert_len;
7673 }
7674#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7675 /* Deserialize CRT digest from the end of the ticket. */
7676 if( 2 > (size_t)( end - p ) )
7677 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7678
7679 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
7680 session->peer_cert_digest_len = (size_t) *p++;
7681
7682 if( session->peer_cert_digest_len != 0 )
7683 {
7684 const mbedtls_md_info_t *md_info =
7685 mbedtls_md_info_from_type( session->peer_cert_digest_type );
7686 if( md_info == NULL )
7687 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7688 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
7689 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7690
7691 if( session->peer_cert_digest_len > (size_t)( end - p ) )
7692 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7693
7694 session->peer_cert_digest =
7695 mbedtls_calloc( 1, session->peer_cert_digest_len );
7696 if( session->peer_cert_digest == NULL )
7697 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7698
7699 memcpy( session->peer_cert_digest, p,
7700 session->peer_cert_digest_len );
7701 p += session->peer_cert_digest_len;
7702 }
7703#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7704#endif /* MBEDTLS_X509_CRT_PARSE_C */
7705
7706 /*
7707 * Session ticket and associated data
7708 */
7709#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7710 if( 3 > (size_t)( end - p ) )
7711 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7712
7713 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
7714 p += 3;
7715
7716 if( session->ticket_len != 0 )
7717 {
7718 if( session->ticket_len > (size_t)( end - p ) )
7719 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7720
7721 session->ticket = mbedtls_calloc( 1, session->ticket_len );
7722 if( session->ticket == NULL )
7723 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7724
7725 memcpy( session->ticket, p, session->ticket_len );
7726 p += session->ticket_len;
7727 }
7728
7729 if( 4 > (size_t)( end - p ) )
7730 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7731
7732 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
7733 ( (uint32_t) p[1] << 16 ) |
7734 ( (uint32_t) p[2] << 8 ) |
7735 ( (uint32_t) p[3] );
7736 p += 4;
7737#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7738
7739 /*
7740 * Misc extension-related info
7741 */
7742#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7743 if( 1 > (size_t)( end - p ) )
7744 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7745
7746 session->mfl_code = *p++;
7747#endif
7748
7749#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7750 if( 1 > (size_t)( end - p ) )
7751 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7752
7753 session->encrypt_then_mac = *p++;
7754#endif
7755
7756 /* Done, should have consumed entire buffer */
7757 if( p != end )
7758 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7759
7760 return( 0 );
7761}
Jerry Yudc7bd172022-02-17 13:44:15 +08007762#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007764#endif /* MBEDTLS_SSL_TLS_C */