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