blob: cc7d7e143c81f20f99384f44f1690ee4b1208e3c [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"
Chris Jones84a773f2021-03-05 18:38:47 +000041#include "ssl_misc.h"
Janos Follath73c616b2019-12-18 15:07:04 +000042#include "mbedtls/debug.h"
43#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050044#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010045#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020046#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020047
Rich Evans00ab4702015-02-06 13:43:58 +000048#include <string.h>
49
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050050#if defined(MBEDTLS_USE_PSA_CRYPTO)
51#include "mbedtls/psa_util.h"
52#include "psa/crypto.h"
53#endif
54
Janos Follath23bdca02016-10-07 14:47:14 +010055#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020057#endif
58
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020059#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010060
Hanno Beckera0e20d02019-05-15 14:03:01 +010061#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010062/* Top-level Connection ID API */
63
Hanno Becker8367ccc2019-05-14 11:30:10 +010064int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
65 size_t len,
66 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +010067{
68 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
69 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
70
Hanno Becker611ac772019-05-14 11:45:26 +010071 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
72 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
73 {
74 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
75 }
76
77 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010078 conf->cid_len = len;
79 return( 0 );
80}
81
Hanno Beckerf8542cf2019-04-09 15:22:03 +010082int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
83 int enable,
84 unsigned char const *own_cid,
85 size_t own_cid_len )
86{
Hanno Becker76a79ab2019-05-03 14:38:32 +010087 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
88 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
89
Hanno Beckerca092242019-04-25 16:01:49 +010090 ssl->negotiate_cid = enable;
91 if( enable == MBEDTLS_SSL_CID_DISABLED )
92 {
93 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
94 return( 0 );
95 }
96 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +010097 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +010098
Hanno Beckerad4a1372019-05-03 13:06:44 +010099 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100100 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100101 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
102 (unsigned) own_cid_len,
103 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100104 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
105 }
106
107 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100108 /* Truncation is not an issue here because
109 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
110 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100111
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100112 return( 0 );
113}
114
115int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
116 int *enabled,
117 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
118 size_t *peer_cid_len )
119{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100120 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100121
Hanno Becker76a79ab2019-05-03 14:38:32 +0100122 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Paul Elliott27b0d942022-03-18 21:55:32 +0000123 mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Hanno Becker76a79ab2019-05-03 14:38:32 +0100124 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100126 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100127
Hanno Beckerc5f24222019-05-03 12:54:52 +0100128 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
129 * were used, but client and server requested the empty CID.
130 * This is indistinguishable from not using the CID extension
131 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100132 if( ssl->transform_in->in_cid_len == 0 &&
133 ssl->transform_in->out_cid_len == 0 )
134 {
135 return( 0 );
136 }
137
Hanno Becker615ef172019-05-22 16:50:35 +0100138 if( peer_cid_len != NULL )
139 {
140 *peer_cid_len = ssl->transform_in->out_cid_len;
141 if( peer_cid != NULL )
142 {
143 memcpy( peer_cid, ssl->transform_in->out_cid,
144 ssl->transform_in->out_cid_len );
145 }
146 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100147
148 *enabled = MBEDTLS_SSL_CID_ENABLED;
149
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100150 return( 0 );
151}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100152#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200157/*
158 * Convert max_fragment_length codes to length.
159 * RFC 6066 says:
160 * enum{
161 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
162 * } MaxFragmentLength;
163 * and we add 0 -> extension unused
164 */
Angus Grattond8213d02016-05-25 20:56:48 +1000165static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200166{
Angus Grattond8213d02016-05-25 20:56:48 +1000167 switch( mfl )
168 {
169 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
170 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
171 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
172 return 512;
173 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
174 return 1024;
175 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
176 return 2048;
177 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
178 return 4096;
179 default:
180 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
181 }
182}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200184
Hanno Becker52055ae2019-02-06 14:30:46 +0000185int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
186 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200187{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_ssl_session_free( dst );
189 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200190
吴敬辉0b716112021-11-29 10:46:35 +0800191#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
192 dst->ticket = NULL;
193#endif
194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000196
197#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200198 if( src->peer_cert != NULL )
199 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000200 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200201
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200202 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200203 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200204 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200209 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200212 dst->peer_cert = NULL;
213 return( ret );
214 }
215 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000216#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000217 if( src->peer_cert_digest != NULL )
218 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000219 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000220 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000221 if( dst->peer_cert_digest == NULL )
222 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
223
224 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
225 src->peer_cert_digest_len );
226 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000227 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000228 }
229#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200232
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200233#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200234 if( src->ticket != NULL )
235 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200236 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200237 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200238 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200239
240 memcpy( dst->ticket, src->ticket, src->ticket_len );
241 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200242#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200243
244 return( 0 );
245}
246
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500247#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
248static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
249{
250 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
251 if( resized_buffer == NULL )
252 return -1;
253
254 /* We want to copy len_new bytes when downsizing the buffer, and
255 * len_old bytes when upsizing, so we choose the smaller of two sizes,
256 * to fit one buffer into another. Size checks, ensuring that no data is
257 * lost, are done outside of this function. */
258 memcpy( resized_buffer, *buffer,
259 ( len_new < *len_old ) ? len_new : *len_old );
260 mbedtls_platform_zeroize( *buffer, *len_old );
261 mbedtls_free( *buffer );
262
263 *buffer = resized_buffer;
264 *len_old = len_new;
265
266 return 0;
267}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200268
269static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500270 size_t in_buf_new_len,
271 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200272{
273 int modified = 0;
274 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
275 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
276 if( ssl->in_buf != NULL )
277 {
278 written_in = ssl->in_msg - ssl->in_buf;
279 iv_offset_in = ssl->in_iv - ssl->in_buf;
280 len_offset_in = ssl->in_len - ssl->in_buf;
281 if( downsizing ?
282 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
283 ssl->in_buf_len < in_buf_new_len )
284 {
285 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
286 {
287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
288 }
289 else
290 {
Paul Elliottb7449902021-03-10 18:14:58 +0000291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
292 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200293 modified = 1;
294 }
295 }
296 }
297
298 if( ssl->out_buf != NULL )
299 {
300 written_out = ssl->out_msg - ssl->out_buf;
301 iv_offset_out = ssl->out_iv - ssl->out_buf;
302 len_offset_out = ssl->out_len - ssl->out_buf;
303 if( downsizing ?
304 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
305 ssl->out_buf_len < out_buf_new_len )
306 {
307 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
308 {
309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
310 }
311 else
312 {
Paul Elliottb7449902021-03-10 18:14:58 +0000313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
314 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200315 modified = 1;
316 }
317 }
318 }
319 if( modified )
320 {
321 /* Update pointers here to avoid doing it twice. */
322 mbedtls_ssl_reset_in_out_pointers( ssl );
323 /* Fields below might not be properly updated with record
324 * splitting or with CID, so they are manually updated here. */
325 ssl->out_msg = ssl->out_buf + written_out;
326 ssl->out_len = ssl->out_buf + len_offset_out;
327 ssl->out_iv = ssl->out_buf + iv_offset_out;
328
329 ssl->in_msg = ssl->in_buf + written_in;
330 ssl->in_len = ssl->in_buf + len_offset_in;
331 ssl->in_iv = ssl->in_buf + iv_offset_in;
332 }
333}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500334#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
335
Jerry Yudb8c48a2022-01-27 14:54:54 +0800336#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800337
338#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
339typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
340 const char *label,
341 const unsigned char *random, size_t rlen,
342 unsigned char *dstbuf, size_t dlen );
343
344static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
345
346#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
347
348/* Type for the TLS PRF */
349typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
350 const unsigned char *, size_t,
351 unsigned char *, size_t);
352
353static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
354 int ciphersuite,
355 const unsigned char master[48],
356#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
357 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
358 int encrypt_then_mac,
359#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
360 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
361 ssl_tls_prf_t tls_prf,
362 const unsigned char randbytes[64],
363 int minor_ver,
364 unsigned endpoint,
365 const mbedtls_ssl_context *ssl );
366
367#if defined(MBEDTLS_SHA256_C)
368static int tls_prf_sha256( const unsigned char *secret, size_t slen,
369 const char *label,
370 const unsigned char *random, size_t rlen,
371 unsigned char *dstbuf, size_t dlen );
372static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
373static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
374
375#endif /* MBEDTLS_SHA256_C */
376
377#if defined(MBEDTLS_SHA384_C)
378static int tls_prf_sha384( const unsigned char *secret, size_t slen,
379 const char *label,
380 const unsigned char *random, size_t rlen,
381 unsigned char *dstbuf, size_t dlen );
382
383static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
384static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
385#endif /* MBEDTLS_SHA384_C */
386
387static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session,
388 unsigned char *buf,
389 size_t buf_len );
390static int ssl_session_load_tls12( mbedtls_ssl_session *session,
391 const unsigned char *buf,
392 size_t len );
393#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
394
Jerry Yu53d23e22022-02-09 16:25:09 +0800395static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
396
Jerry Yudb8c48a2022-01-27 14:54:54 +0800397#if defined(MBEDTLS_SHA256_C)
398static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Jerry Yu53d23e22022-02-09 16:25:09 +0800399#endif /* MBEDTLS_SHA256_C */
Jerry Yudb8c48a2022-01-27 14:54:54 +0800400
401#if defined(MBEDTLS_SHA384_C)
402static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Jerry Yu53d23e22022-02-09 16:25:09 +0800403#endif /* MBEDTLS_SHA384_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000404
Ron Eldor51d3ab52019-05-12 14:54:30 +0300405int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
406 const unsigned char *secret, size_t slen,
407 const char *label,
408 const unsigned char *random, size_t rlen,
409 unsigned char *dstbuf, size_t dlen )
410{
411 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
412
413 switch( prf )
414 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300415#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +0200416#if defined(MBEDTLS_SHA384_C)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300417 case MBEDTLS_SSL_TLS_PRF_SHA384:
418 tls_prf = tls_prf_sha384;
419 break;
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +0200420#endif /* MBEDTLS_SHA384_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300421#if defined(MBEDTLS_SHA256_C)
422 case MBEDTLS_SSL_TLS_PRF_SHA256:
423 tls_prf = tls_prf_sha256;
424 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300425#endif /* MBEDTLS_SHA256_C */
426#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300427 default:
428 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
429 }
430
431 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
432}
433
Jerry Yuc73c6182022-02-08 20:29:25 +0800434#if defined(MBEDTLS_X509_CRT_PARSE_C)
435static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
436{
437#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
438 if( session->peer_cert != NULL )
439 {
440 mbedtls_x509_crt_free( session->peer_cert );
441 mbedtls_free( session->peer_cert );
442 session->peer_cert = NULL;
443 }
444#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
445 if( session->peer_cert_digest != NULL )
446 {
447 /* Zeroization is not necessary. */
448 mbedtls_free( session->peer_cert_digest );
449 session->peer_cert_digest = NULL;
450 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
451 session->peer_cert_digest_len = 0;
452 }
453#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
454}
455#endif /* MBEDTLS_X509_CRT_PARSE_C */
456
457void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
458 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
459{
460 ((void) ciphersuite_info);
461
462#if defined(MBEDTLS_SHA384_C)
463 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
464 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
465 else
466#endif
467#if defined(MBEDTLS_SHA256_C)
468 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
469 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
470 else
471#endif
472 {
473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
474 return;
475 }
476}
477
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100478static void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
479 unsigned hs_type,
480 size_t total_hs_len )
481{
482 unsigned char hs_hdr[4];
483
484 /* Build HS header for checksum update. */
485 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
486 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
487 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
488 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
489
490 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
491}
492
493void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
494 unsigned hs_type,
495 unsigned char const *msg,
496 size_t msg_len )
497{
498 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
499 ssl->handshake->update_checksum( ssl, msg, msg_len );
500}
501
Jerry Yuc73c6182022-02-08 20:29:25 +0800502void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
503{
504 ((void) ssl);
505#if defined(MBEDTLS_SHA256_C)
506#if defined(MBEDTLS_USE_PSA_CRYPTO)
507 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
508 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
509#else
510 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
511#endif
512#endif
513#if defined(MBEDTLS_SHA384_C)
514#if defined(MBEDTLS_USE_PSA_CRYPTO)
515 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
516 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
517#else
518 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
519#endif
520#endif
521}
522
523static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
524 const unsigned char *buf, size_t len )
525{
526#if defined(MBEDTLS_SHA256_C)
527#if defined(MBEDTLS_USE_PSA_CRYPTO)
528 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
529#else
530 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
531#endif
532#endif
533#if defined(MBEDTLS_SHA384_C)
534#if defined(MBEDTLS_USE_PSA_CRYPTO)
535 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
536#else
537 mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len );
538#endif
539#endif
540}
541
542#if defined(MBEDTLS_SHA256_C)
543static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
544 const unsigned char *buf, size_t len )
545{
546#if defined(MBEDTLS_USE_PSA_CRYPTO)
547 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
548#else
549 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
550#endif
551}
552#endif
553
554#if defined(MBEDTLS_SHA384_C)
555static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
556 const unsigned char *buf, size_t len )
557{
558#if defined(MBEDTLS_USE_PSA_CRYPTO)
559 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
560#else
561 mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len );
562#endif
563}
564#endif
565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200567{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500571#if defined(MBEDTLS_USE_PSA_CRYPTO)
572 handshake->fin_sha256_psa = psa_hash_operation_init();
573 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
574#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200576 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200577#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500578#endif
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +0200579#if defined(MBEDTLS_SHA384_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500580#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500581 handshake->fin_sha384_psa = psa_hash_operation_init();
582 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500583#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 mbedtls_sha512_init( &handshake->fin_sha512 );
TRodziewicz26371e42021-06-08 16:45:41 +0200585 mbedtls_sha512_starts( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200586#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500587#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200588
589 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100590
591#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +0100592 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +0100593 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
594#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596#if defined(MBEDTLS_DHM_C)
597 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200598#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599#if defined(MBEDTLS_ECDH_C)
600 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200601#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200602#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200603 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200604#if defined(MBEDTLS_SSL_CLI_C)
605 handshake->ecjpake_cache = NULL;
606 handshake->ecjpake_cache_len = 0;
607#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200608#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200609
Gilles Peskineeccd8882020-03-10 12:19:08 +0100610#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200611 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200612#endif
613
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200614#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
615 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
616#endif
Hanno Becker75173122019-02-06 16:18:31 +0000617
618#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
619 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
620 mbedtls_pk_init( &handshake->peer_pubkey );
621#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200622}
623
Hanno Beckera18d1322018-01-03 14:27:32 +0000624void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200625{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200627
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100628#if defined(MBEDTLS_USE_PSA_CRYPTO)
629 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
630 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100631#else
632 mbedtls_cipher_init( &transform->cipher_ctx_enc );
633 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100634#endif
635
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000636#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100637#if defined(MBEDTLS_USE_PSA_CRYPTO)
638 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
639 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100640#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_md_init( &transform->md_ctx_enc );
642 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000643#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100644#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200645}
646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200648{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200650}
651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000653{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200654 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000655 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200657 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200659 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200660 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200661
662 /*
663 * Either the pointers are now NULL or cleared properly and can be freed.
664 * Now allocate missing structures.
665 */
666 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200667 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200668 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200669 }
Paul Bakker48916f92012-09-16 19:57:18 +0000670
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200671 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200672 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200673 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200674 }
Paul Bakker48916f92012-09-16 19:57:18 +0000675
Paul Bakker82788fb2014-10-20 13:59:19 +0200676 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200677 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200678 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200679 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500680#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
681 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400682
Andrzej Kurek4a063792020-10-21 15:08:44 +0200683 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
684 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500685#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000686
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200687 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000688 if( ssl->handshake == NULL ||
689 ssl->transform_negotiate == NULL ||
690 ssl->session_negotiate == NULL )
691 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200692 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 mbedtls_free( ssl->handshake );
695 mbedtls_free( ssl->transform_negotiate );
696 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200697
698 ssl->handshake = NULL;
699 ssl->transform_negotiate = NULL;
700 ssl->session_negotiate = NULL;
701
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200702 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000703 }
704
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200705 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000707 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200708 ssl_handshake_params_init( ssl->handshake );
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200711 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
712 {
713 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200714
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200715 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
716 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
717 else
718 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200719
Hanno Becker0f57a652020-02-05 10:37:26 +0000720 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200721 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200722#endif
723
Brett Warrene0edc842021-08-17 09:53:13 +0100724/*
725 * curve_list is translated to IANA TLS group identifiers here because
726 * mbedtls_ssl_conf_curves returns void and so can't return
727 * any error codes.
728 */
729#if defined(MBEDTLS_ECP_C)
730#if !defined(MBEDTLS_DEPRECATED_REMOVED)
731 /* Heap allocate and translate curve_list from internal to IANA group ids */
732 if ( ssl->conf->curve_list != NULL )
733 {
734 size_t length;
735 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
736
737 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
738 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
739
740 /* Leave room for zero termination */
741 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
742 if ( group_list == NULL )
743 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
744
745 for( size_t i = 0; i < length; i++ )
746 {
747 const mbedtls_ecp_curve_info *info =
748 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
749 if ( info == NULL )
750 {
751 mbedtls_free( group_list );
752 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
753 }
754 group_list[i] = info->tls_id;
755 }
756
757 group_list[length] = 0;
758
759 ssl->handshake->group_list = group_list;
760 ssl->handshake->group_list_heap_allocated = 1;
761 }
762 else
763 {
764 ssl->handshake->group_list = ssl->conf->group_list;
765 ssl->handshake->group_list_heap_allocated = 0;
766 }
767#endif /* MBEDTLS_DEPRECATED_REMOVED */
768#endif /* MBEDTLS_ECP_C */
769
Jerry Yuf017ee42022-01-12 15:49:48 +0800770#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
771#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800772#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800773 /* Heap allocate and translate sig_hashes from internal hash identifiers to
774 signature algorithms IANA identifiers. */
775 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800776 ssl->conf->sig_hashes != NULL )
777 {
778 const int *md;
779 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800780 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800781 uint16_t *p;
782
Jerry Yub476a442022-01-21 18:14:45 +0800783#if defined(static_assert)
784 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
785 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
786 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800787#endif
788
Jerry Yuf017ee42022-01-12 15:49:48 +0800789 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
790 {
791 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
792 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800793#if defined(MBEDTLS_ECDSA_C)
794 sig_algs_len += sizeof( uint16_t );
795#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800796
Jerry Yub476a442022-01-21 18:14:45 +0800797#if defined(MBEDTLS_RSA_C)
798 sig_algs_len += sizeof( uint16_t );
799#endif
800 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
801 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800802 }
803
Jerry Yub476a442022-01-21 18:14:45 +0800804 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800805 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
806
Jerry Yub476a442022-01-21 18:14:45 +0800807 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
808 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800809 if( ssl->handshake->sig_algs == NULL )
810 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
811
812 p = (uint16_t *)ssl->handshake->sig_algs;
813 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
814 {
815 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
816 if( hash == MBEDTLS_SSL_HASH_NONE )
817 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800818#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800819 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
820 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800821#endif
822#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800823 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
824 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800825#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800826 }
827 *p = MBEDTLS_TLS1_3_SIG_NONE;
828 ssl->handshake->sig_algs_heap_allocated = 1;
829 }
830 else
Jerry Yua69269a2022-01-17 21:06:01 +0800831#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800832 {
833 ssl->handshake->sig_algs = ssl->conf->sig_algs;
834 ssl->handshake->sig_algs_heap_allocated = 0;
835 }
836#endif /* MBEDTLS_DEPRECATED_REMOVED */
837#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000838 return( 0 );
839}
840
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200841#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200842/* Dummy cookie callbacks for defaults */
843static int ssl_cookie_write_dummy( void *ctx,
844 unsigned char **p, unsigned char *end,
845 const unsigned char *cli_id, size_t cli_id_len )
846{
847 ((void) ctx);
848 ((void) p);
849 ((void) end);
850 ((void) cli_id);
851 ((void) cli_id_len);
852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200854}
855
856static int ssl_cookie_check_dummy( void *ctx,
857 const unsigned char *cookie, size_t cookie_len,
858 const unsigned char *cli_id, size_t cli_id_len )
859{
860 ((void) ctx);
861 ((void) cookie);
862 ((void) cookie_len);
863 ((void) cli_id);
864 ((void) cli_id_len);
865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200867}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200868#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200869
Paul Bakker5121ce52009-01-03 21:22:43 +0000870/*
871 * Initialize an SSL context
872 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200873void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
874{
875 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
876}
877
Jerry Yu60835a82021-08-04 10:13:52 +0800878static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
879{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100880 const mbedtls_ssl_config *conf = ssl->conf;
881
Ronald Cron6f135e12021-12-08 16:57:54 +0100882#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100883 if( mbedtls_ssl_conf_is_tls13_enabled( conf ) &&
884 ( conf->endpoint == MBEDTLS_SSL_IS_SERVER ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800885 {
Ronald Cron086ee0b2022-03-15 15:18:51 +0100886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
887 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
888 }
889
890 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
891 {
892 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jerry Yu60835a82021-08-04 10:13:52 +0800893 {
Ronald Cron086ee0b2022-03-15 15:18:51 +0100894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
Jerry Yu60835a82021-08-04 10:13:52 +0800895 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
896 }
897 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
898 return( 0 );
899 }
900#endif
901
902#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100903 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800904 {
905 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
906 return( 0 );
907 }
908#endif
909
Ronald Cron6f135e12021-12-08 16:57:54 +0100910#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100911 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800912 {
913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Hybrid TLS 1.2 + TLS 1.3 configurations are not yet supported" ) );
914 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
915 }
916#endif
917
918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
919 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
920}
921
922static int ssl_conf_check(const mbedtls_ssl_context *ssl)
923{
924 int ret;
925 ret = ssl_conf_version_check( ssl );
926 if( ret != 0 )
927 return( ret );
928
929 /* Space for further checks */
930
931 return( 0 );
932}
933
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200934/*
935 * Setup an SSL context
936 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100937
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200938int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +0200939 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +0000940{
Janos Follath865b3eb2019-12-16 11:46:15 +0000941 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +0000942 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
943 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +0000944
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200945 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +0000946
Jerry Yu60835a82021-08-04 10:13:52 +0800947 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
948 return( ret );
949
Paul Bakker62f2dee2012-09-28 07:31:51 +0000950 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +0100951 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +0000952 */
k-stachowiakc9a5f022018-07-24 13:53:31 +0200953
954 /* Set to NULL in case of an error condition */
955 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +0200956
Darryl Greenb33cc762019-11-28 14:29:44 +0000957#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
958 ssl->in_buf_len = in_buf_len;
959#endif
960 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +1000961 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000962 {
Paul Elliottd48d5c62021-01-07 14:47:05 +0000963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +0200964 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +0200965 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +1000966 }
967
Darryl Greenb33cc762019-11-28 14:29:44 +0000968#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
969 ssl->out_buf_len = out_buf_len;
970#endif
971 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +1000972 if( ssl->out_buf == NULL )
973 {
Paul Elliottd48d5c62021-01-07 14:47:05 +0000974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +0200975 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +0200976 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +0000977 }
978
Hanno Becker3e6f8ab2020-02-05 10:40:57 +0000979 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200980
Johan Pascalb62bb512015-12-03 21:56:45 +0100981#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +0200982 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +0100983#endif
984
Paul Bakker48916f92012-09-16 19:57:18 +0000985 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +0200986 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +0000987
988 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +0200989
990error:
991 mbedtls_free( ssl->in_buf );
992 mbedtls_free( ssl->out_buf );
993
994 ssl->conf = NULL;
995
Darryl Greenb33cc762019-11-28 14:29:44 +0000996#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
997 ssl->in_buf_len = 0;
998 ssl->out_buf_len = 0;
999#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001000 ssl->in_buf = NULL;
1001 ssl->out_buf = NULL;
1002
1003 ssl->in_hdr = NULL;
1004 ssl->in_ctr = NULL;
1005 ssl->in_len = NULL;
1006 ssl->in_iv = NULL;
1007 ssl->in_msg = NULL;
1008
1009 ssl->out_hdr = NULL;
1010 ssl->out_ctr = NULL;
1011 ssl->out_len = NULL;
1012 ssl->out_iv = NULL;
1013 ssl->out_msg = NULL;
1014
k-stachowiak9f7798e2018-07-31 16:52:32 +02001015 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001016}
1017
1018/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001019 * Reset an initialized and used SSL context for re-use while retaining
1020 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001021 *
1022 * If partial is non-zero, keep data in the input buffer and client ID.
1023 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001024 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001025void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1026 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001027{
Darryl Greenb33cc762019-11-28 14:29:44 +00001028#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1029 size_t in_buf_len = ssl->in_buf_len;
1030 size_t out_buf_len = ssl->out_buf_len;
1031#else
1032 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1033 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1034#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001035
Hanno Beckerb0302c42021-08-03 09:39:42 +01001036#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1037 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001038#endif
1039
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001040 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001041 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001042
Hanno Beckerb0302c42021-08-03 09:39:42 +01001043 mbedtls_ssl_reset_in_out_pointers( ssl );
1044
1045 /* Reset incoming message parsing */
1046 ssl->in_offt = NULL;
1047 ssl->nb_zero = 0;
1048 ssl->in_msgtype = 0;
1049 ssl->in_msglen = 0;
1050 ssl->in_hslen = 0;
1051 ssl->keep_current_message = 0;
1052 ssl->transform_in = NULL;
1053
1054#if defined(MBEDTLS_SSL_PROTO_DTLS)
1055 ssl->next_record_offset = 0;
1056 ssl->in_epoch = 0;
1057#endif
1058
1059 /* Keep current datagram if partial == 1 */
1060 if( partial == 0 )
1061 {
1062 ssl->in_left = 0;
1063 memset( ssl->in_buf, 0, in_buf_len );
1064 }
1065
1066 /* Reset outgoing message writing */
1067 ssl->out_msgtype = 0;
1068 ssl->out_msglen = 0;
1069 ssl->out_left = 0;
1070 memset( ssl->out_buf, 0, out_buf_len );
1071 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1072 ssl->transform_out = NULL;
1073
1074#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1075 mbedtls_ssl_dtls_replay_reset( ssl );
1076#endif
1077
XiaokangQian2b01dc32022-01-21 02:53:13 +00001078#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001079 if( ssl->transform )
1080 {
1081 mbedtls_ssl_transform_free( ssl->transform );
1082 mbedtls_free( ssl->transform );
1083 ssl->transform = NULL;
1084 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001085#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1086
XiaokangQian2b01dc32022-01-21 02:53:13 +00001087#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1088 mbedtls_ssl_transform_free( ssl->transform_application );
1089 mbedtls_free( ssl->transform_application );
1090 ssl->transform_application = NULL;
1091
1092 if( ssl->handshake != NULL )
1093 {
1094 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1095 mbedtls_free( ssl->handshake->transform_earlydata );
1096 ssl->handshake->transform_earlydata = NULL;
1097
1098 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1099 mbedtls_free( ssl->handshake->transform_handshake );
1100 ssl->handshake->transform_handshake = NULL;
1101 }
1102
XiaokangQian2b01dc32022-01-21 02:53:13 +00001103#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001104}
1105
1106int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1107{
1108 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1109
1110 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1111
XiaokangQian78b1fa72022-01-19 06:56:30 +00001112 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001113
1114 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115#if defined(MBEDTLS_SSL_RENEGOTIATION)
1116 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001117 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001118
1119 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1121 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001122#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001124
Hanno Beckerb0302c42021-08-03 09:39:42 +01001125 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001126 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001127 if( ssl->session )
1128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 mbedtls_ssl_session_free( ssl->session );
1130 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001131 ssl->session = NULL;
1132 }
1133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001135 ssl->alpn_chosen = NULL;
1136#endif
1137
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001138#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01001139#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001140 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01001141#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001142 {
1143 mbedtls_free( ssl->cli_id );
1144 ssl->cli_id = NULL;
1145 ssl->cli_id_len = 0;
1146 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001147#endif
1148
Paul Bakker48916f92012-09-16 19:57:18 +00001149 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1150 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001151
1152 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001153}
1154
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001155/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001156 * Reset an initialized and used SSL context for re-use while retaining
1157 * all application-set variables, function pointers and data.
1158 */
1159int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1160{
Hanno Becker43aefe22020-02-05 10:44:56 +00001161 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001162}
1163
1164/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001165 * SSL set accessors
1166 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001167void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001168{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001169 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001170}
1171
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001172void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001173{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001174 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001175}
1176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001178void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001179{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001180 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001181}
1182#endif
1183
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001184void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001185{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001186 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001187}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001190
Hanno Becker1841b0a2018-08-24 11:13:57 +01001191void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1192 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001193{
1194 ssl->disable_datagram_packing = !allow_packing;
1195}
1196
1197void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1198 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001199{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001200 conf->hs_timeout_min = min;
1201 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001202}
1203#endif
1204
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001205void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001206{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001207 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001208}
1209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001211void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001212 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001213 void *p_vrfy )
1214{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001215 conf->f_vrfy = f_vrfy;
1216 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001217}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001219
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001220void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001221 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001222 void *p_rng )
1223{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001224 conf->f_rng = f_rng;
1225 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001226}
1227
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001228void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001229 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001230 void *p_dbg )
1231{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001232 conf->f_dbg = f_dbg;
1233 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001234}
1235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001237 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001238 mbedtls_ssl_send_t *f_send,
1239 mbedtls_ssl_recv_t *f_recv,
1240 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001241{
1242 ssl->p_bio = p_bio;
1243 ssl->f_send = f_send;
1244 ssl->f_recv = f_recv;
1245 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001246}
1247
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001248#if defined(MBEDTLS_SSL_PROTO_DTLS)
1249void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1250{
1251 ssl->mtu = mtu;
1252}
1253#endif
1254
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001255void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001256{
1257 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001258}
1259
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001260void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1261 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001262 mbedtls_ssl_set_timer_t *f_set_timer,
1263 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001264{
1265 ssl->p_timer = p_timer;
1266 ssl->f_set_timer = f_set_timer;
1267 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001268
1269 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001270 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001271}
1272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273#if defined(MBEDTLS_SSL_SRV_C)
Glenn Strauss2ed95272022-01-21 18:02:17 -05001274void mbedtls_ssl_conf_cert_cb( mbedtls_ssl_config *conf,
1275 int (*f_cert_cb)(mbedtls_ssl_context *) )
1276{
1277 conf->f_cert_cb = f_cert_cb;
1278}
1279#endif /* MBEDTLS_SSL_SRV_C */
1280
1281#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001282void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001283 void *p_cache,
1284 mbedtls_ssl_cache_get_t *f_get_cache,
1285 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001286{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001287 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001288 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001289 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001290}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293#if defined(MBEDTLS_SSL_CLI_C)
1294int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001295{
Janos Follath865b3eb2019-12-16 11:46:15 +00001296 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001297
1298 if( ssl == NULL ||
1299 session == NULL ||
1300 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001301 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001304 }
1305
Hanno Beckere810bbc2021-05-14 16:01:05 +01001306 if( ssl->handshake->resume == 1 )
1307 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1308
Hanno Becker52055ae2019-02-06 14:30:46 +00001309 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1310 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001311 return( ret );
1312
Paul Bakker0a597072012-09-25 21:55:46 +00001313 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001314
1315 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001316}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001318
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001319void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1320 const int *ciphersuites )
1321{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001322 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001323}
1324
Ronald Cron6f135e12021-12-08 16:57:54 +01001325#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001326void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001327 const int kex_modes )
1328{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001329 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001330}
Ronald Cron6f135e12021-12-08 16:57:54 +01001331#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001334void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001335 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001336{
1337 conf->cert_profile = profile;
1338}
1339
Glenn Strauss36872db2022-01-22 05:06:31 -05001340static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1341{
1342 mbedtls_ssl_key_cert *cur = key_cert, *next;
1343
1344 while( cur != NULL )
1345 {
1346 next = cur->next;
1347 mbedtls_free( cur );
1348 cur = next;
1349 }
1350}
1351
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001352/* Append a new keycert entry to a (possibly empty) list */
1353static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1354 mbedtls_x509_crt *cert,
1355 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001356{
niisato8ee24222018-06-25 19:05:48 +09001357 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001358
Glenn Strauss36872db2022-01-22 05:06:31 -05001359 if( cert == NULL )
1360 {
1361 /* Free list if cert is null */
1362 ssl_key_cert_free( *head );
1363 *head = NULL;
1364 return( 0 );
1365 }
1366
niisato8ee24222018-06-25 19:05:48 +09001367 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1368 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001369 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001370
niisato8ee24222018-06-25 19:05:48 +09001371 new_cert->cert = cert;
1372 new_cert->key = key;
1373 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001374
Glenn Strauss36872db2022-01-22 05:06:31 -05001375 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001376 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001377 {
niisato8ee24222018-06-25 19:05:48 +09001378 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001379 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001380 else
1381 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001382 mbedtls_ssl_key_cert *cur = *head;
1383 while( cur->next != NULL )
1384 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001385 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001386 }
1387
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001388 return( 0 );
1389}
1390
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001391int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001392 mbedtls_x509_crt *own_cert,
1393 mbedtls_pk_context *pk_key )
1394{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001395 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001396}
1397
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001398void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001399 mbedtls_x509_crt *ca_chain,
1400 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001401{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001402 conf->ca_chain = ca_chain;
1403 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001404
1405#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1406 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1407 * cannot be used together. */
1408 conf->f_ca_cb = NULL;
1409 conf->p_ca_cb = NULL;
1410#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001411}
Hanno Becker5adaad92019-03-27 16:54:37 +00001412
1413#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1414void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001415 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001416 void *p_ca_cb )
1417{
1418 conf->f_ca_cb = f_ca_cb;
1419 conf->p_ca_cb = p_ca_cb;
1420
1421 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1422 * cannot be used together. */
1423 conf->ca_chain = NULL;
1424 conf->ca_crl = NULL;
1425}
1426#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001428
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001429#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001430const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1431 size_t *name_len )
1432{
1433 *name_len = ssl->handshake->sni_name_len;
1434 return( ssl->handshake->sni_name );
1435}
1436
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001437int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1438 mbedtls_x509_crt *own_cert,
1439 mbedtls_pk_context *pk_key )
1440{
1441 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1442 own_cert, pk_key ) );
1443}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001444
1445void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1446 mbedtls_x509_crt *ca_chain,
1447 mbedtls_x509_crl *ca_crl )
1448{
1449 ssl->handshake->sni_ca_chain = ca_chain;
1450 ssl->handshake->sni_ca_crl = ca_crl;
1451}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001452
1453void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1454 int authmode )
1455{
1456 ssl->handshake->sni_authmode = authmode;
1457}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001458#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1459
Hanno Becker8927c832019-04-03 12:52:50 +01001460#if defined(MBEDTLS_X509_CRT_PARSE_C)
1461void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1462 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1463 void *p_vrfy )
1464{
1465 ssl->f_vrfy = f_vrfy;
1466 ssl->p_vrfy = p_vrfy;
1467}
1468#endif
1469
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001470#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001471/*
1472 * Set EC J-PAKE password for current handshake
1473 */
1474int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1475 const unsigned char *pw,
1476 size_t pw_len )
1477{
1478 mbedtls_ecjpake_role role;
1479
Janos Follath8eb64132016-06-03 15:40:57 +01001480 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001481 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1482
1483 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1484 role = MBEDTLS_ECJPAKE_SERVER;
1485 else
1486 role = MBEDTLS_ECJPAKE_CLIENT;
1487
1488 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1489 role,
1490 MBEDTLS_MD_SHA256,
1491 MBEDTLS_ECP_DP_SECP256R1,
1492 pw, pw_len ) );
1493}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001494#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001495
Gilles Peskineeccd8882020-03-10 12:19:08 +01001496#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001497
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001498static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1499{
1500#if defined(MBEDTLS_USE_PSA_CRYPTO)
1501 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1502 return( 1 );
1503#endif /* MBEDTLS_USE_PSA_CRYPTO */
1504
1505 if( conf->psk != NULL )
1506 return( 1 );
1507
1508 return( 0 );
1509}
1510
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001511static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1512{
1513 /* Remove reference to existing PSK, if any. */
1514#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001515 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001516 {
1517 /* The maintenance of the PSK key slot is the
1518 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001519 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001520 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00001521 /* This and the following branch should never
1522 * be taken simultaenously as we maintain the
1523 * invariant that raw and opaque PSKs are never
1524 * configured simultaneously. As a safeguard,
1525 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001526#endif /* MBEDTLS_USE_PSA_CRYPTO */
1527 if( conf->psk != NULL )
1528 {
1529 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1530
1531 mbedtls_free( conf->psk );
1532 conf->psk = NULL;
1533 conf->psk_len = 0;
1534 }
1535
1536 /* Remove reference to PSK identity, if any. */
1537 if( conf->psk_identity != NULL )
1538 {
1539 mbedtls_free( conf->psk_identity );
1540 conf->psk_identity = NULL;
1541 conf->psk_identity_len = 0;
1542 }
1543}
1544
Hanno Becker7390c712018-11-15 13:33:04 +00001545/* This function assumes that PSK identity in the SSL config is unset.
1546 * It checks that the provided identity is well-formed and attempts
1547 * to make a copy of it in the SSL config.
1548 * On failure, the PSK identity in the config remains unset. */
1549static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1550 unsigned char const *psk_identity,
1551 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001552{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001553 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001554 if( psk_identity == NULL ||
1555 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001556 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001557 {
1558 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1559 }
1560
Hanno Becker7390c712018-11-15 13:33:04 +00001561 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1562 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001563 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001564
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001565 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001566 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001567
1568 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001569}
1570
Hanno Becker7390c712018-11-15 13:33:04 +00001571int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1572 const unsigned char *psk, size_t psk_len,
1573 const unsigned char *psk_identity, size_t psk_identity_len )
1574{
Janos Follath865b3eb2019-12-16 11:46:15 +00001575 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001576
1577 /* We currently only support one PSK, raw or opaque. */
1578 if( ssl_conf_psk_is_configured( conf ) )
1579 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001580
1581 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001582 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001583 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001584 if( psk_len == 0 )
1585 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1586 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1587 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1588
Hanno Becker7390c712018-11-15 13:33:04 +00001589 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1590 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1591 conf->psk_len = psk_len;
1592 memcpy( conf->psk, psk, conf->psk_len );
1593
1594 /* Check and set PSK Identity */
1595 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1596 if( ret != 0 )
1597 ssl_conf_remove_psk( conf );
1598
1599 return( ret );
1600}
1601
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001602static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1603{
1604#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001605 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001606 {
Ronald Croncf56a0a2020-08-04 09:51:30 +02001607 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001608 }
1609 else
1610#endif /* MBEDTLS_USE_PSA_CRYPTO */
1611 if( ssl->handshake->psk != NULL )
1612 {
1613 mbedtls_platform_zeroize( ssl->handshake->psk,
1614 ssl->handshake->psk_len );
1615 mbedtls_free( ssl->handshake->psk );
1616 ssl->handshake->psk_len = 0;
1617 }
1618}
1619
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001620int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1621 const unsigned char *psk, size_t psk_len )
1622{
1623 if( psk == NULL || ssl->handshake == NULL )
1624 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1625
1626 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1627 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1628
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001629 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001630
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001631 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001632 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001633
1634 ssl->handshake->psk_len = psk_len;
1635 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1636
1637 return( 0 );
1638}
1639
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001640#if defined(MBEDTLS_USE_PSA_CRYPTO)
1641int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001642 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001643 const unsigned char *psk_identity,
1644 size_t psk_identity_len )
1645{
Janos Follath865b3eb2019-12-16 11:46:15 +00001646 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001647
1648 /* We currently only support one PSK, raw or opaque. */
1649 if( ssl_conf_psk_is_configured( conf ) )
1650 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001651
Hanno Becker7390c712018-11-15 13:33:04 +00001652 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001653 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001654 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001655 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001656
1657 /* Check and set PSK Identity */
1658 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1659 psk_identity_len );
1660 if( ret != 0 )
1661 ssl_conf_remove_psk( conf );
1662
1663 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001664}
1665
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001666int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1667 mbedtls_svc_key_id_t psk )
1668{
1669 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1670 ( ssl->handshake == NULL ) )
1671 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1672
1673 ssl_remove_psk( ssl );
1674 ssl->handshake->psk_opaque = psk;
1675 return( 0 );
1676}
1677#endif /* MBEDTLS_USE_PSA_CRYPTO */
1678
1679void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1680 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1681 size_t),
1682 void *p_psk )
1683{
1684 conf->f_psk = f_psk;
1685 conf->p_psk = p_psk;
1686}
1687#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1688
1689#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01001690psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01001691 size_t taglen,
1692 psa_algorithm_t *alg,
1693 psa_key_type_t *key_type,
1694 size_t *key_size )
1695{
1696 switch ( mbedtls_cipher_type )
1697 {
1698 case MBEDTLS_CIPHER_AES_128_CBC:
1699 *alg = PSA_ALG_CBC_NO_PADDING;
1700 *key_type = PSA_KEY_TYPE_AES;
1701 *key_size = 128;
1702 break;
1703 case MBEDTLS_CIPHER_AES_128_CCM:
1704 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1705 *key_type = PSA_KEY_TYPE_AES;
1706 *key_size = 128;
1707 break;
1708 case MBEDTLS_CIPHER_AES_128_GCM:
1709 *alg = PSA_ALG_GCM;
1710 *key_type = PSA_KEY_TYPE_AES;
1711 *key_size = 128;
1712 break;
1713 case MBEDTLS_CIPHER_AES_192_CCM:
1714 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1715 *key_type = PSA_KEY_TYPE_AES;
1716 *key_size = 192;
1717 break;
1718 case MBEDTLS_CIPHER_AES_192_GCM:
1719 *alg = PSA_ALG_GCM;
1720 *key_type = PSA_KEY_TYPE_AES;
1721 *key_size = 192;
1722 break;
1723 case MBEDTLS_CIPHER_AES_256_CBC:
1724 *alg = PSA_ALG_CBC_NO_PADDING;
1725 *key_type = PSA_KEY_TYPE_AES;
1726 *key_size = 256;
1727 break;
1728 case MBEDTLS_CIPHER_AES_256_CCM:
1729 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1730 *key_type = PSA_KEY_TYPE_AES;
1731 *key_size = 256;
1732 break;
1733 case MBEDTLS_CIPHER_AES_256_GCM:
1734 *alg = PSA_ALG_GCM;
1735 *key_type = PSA_KEY_TYPE_AES;
1736 *key_size = 256;
1737 break;
1738 case MBEDTLS_CIPHER_ARIA_128_CBC:
1739 *alg = PSA_ALG_CBC_NO_PADDING;
1740 *key_type = PSA_KEY_TYPE_ARIA;
1741 *key_size = 128;
1742 break;
1743 case MBEDTLS_CIPHER_ARIA_128_CCM:
1744 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1745 *key_type = PSA_KEY_TYPE_ARIA;
1746 *key_size = 128;
1747 break;
1748 case MBEDTLS_CIPHER_ARIA_128_GCM:
1749 *alg = PSA_ALG_GCM;
1750 *key_type = PSA_KEY_TYPE_ARIA;
1751 *key_size = 128;
1752 break;
1753 case MBEDTLS_CIPHER_ARIA_192_CCM:
1754 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1755 *key_type = PSA_KEY_TYPE_ARIA;
1756 *key_size = 192;
1757 break;
1758 case MBEDTLS_CIPHER_ARIA_192_GCM:
1759 *alg = PSA_ALG_GCM;
1760 *key_type = PSA_KEY_TYPE_ARIA;
1761 *key_size = 192;
1762 break;
1763 case MBEDTLS_CIPHER_ARIA_256_CBC:
1764 *alg = PSA_ALG_CBC_NO_PADDING;
1765 *key_type = PSA_KEY_TYPE_ARIA;
1766 *key_size = 256;
1767 break;
1768 case MBEDTLS_CIPHER_ARIA_256_CCM:
1769 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1770 *key_type = PSA_KEY_TYPE_ARIA;
1771 *key_size = 256;
1772 break;
1773 case MBEDTLS_CIPHER_ARIA_256_GCM:
1774 *alg = PSA_ALG_GCM;
1775 *key_type = PSA_KEY_TYPE_ARIA;
1776 *key_size = 256;
1777 break;
1778 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
1779 *alg = PSA_ALG_CBC_NO_PADDING;
1780 *key_type = PSA_KEY_TYPE_CAMELLIA;
1781 *key_size = 128;
1782 break;
1783 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
1784 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1785 *key_type = PSA_KEY_TYPE_CAMELLIA;
1786 *key_size = 128;
1787 break;
1788 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
1789 *alg = PSA_ALG_GCM;
1790 *key_type = PSA_KEY_TYPE_CAMELLIA;
1791 *key_size = 128;
1792 break;
1793 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
1794 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1795 *key_type = PSA_KEY_TYPE_CAMELLIA;
1796 *key_size = 192;
1797 break;
1798 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
1799 *alg = PSA_ALG_GCM;
1800 *key_type = PSA_KEY_TYPE_CAMELLIA;
1801 *key_size = 192;
1802 break;
1803 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
1804 *alg = PSA_ALG_CBC_NO_PADDING;
1805 *key_type = PSA_KEY_TYPE_CAMELLIA;
1806 *key_size = 256;
1807 break;
1808 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
1809 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1810 *key_type = PSA_KEY_TYPE_CAMELLIA;
1811 *key_size = 256;
1812 break;
1813 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
1814 *alg = PSA_ALG_GCM;
1815 *key_type = PSA_KEY_TYPE_CAMELLIA;
1816 *key_size = 256;
1817 break;
1818 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
1819 *alg = PSA_ALG_CHACHA20_POLY1305;
1820 *key_type = PSA_KEY_TYPE_CHACHA20;
1821 *key_size = 256;
1822 break;
1823 case MBEDTLS_CIPHER_NULL:
1824 *alg = MBEDTLS_SSL_NULL_CIPHER;
1825 *key_type = 0;
1826 *key_size = 0;
1827 break;
1828 default:
1829 return PSA_ERROR_NOT_SUPPORTED;
1830 }
1831
1832 return PSA_SUCCESS;
1833}
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001834#endif /* MBEDTLS_USE_PSA_CRYPTO */
1835
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02001836#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01001837int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
1838 const unsigned char *dhm_P, size_t P_len,
1839 const unsigned char *dhm_G, size_t G_len )
1840{
Janos Follath865b3eb2019-12-16 11:46:15 +00001841 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01001842
Glenn Strausscee11292021-12-20 01:43:17 -05001843 mbedtls_mpi_free( &conf->dhm_P );
1844 mbedtls_mpi_free( &conf->dhm_G );
1845
Hanno Beckera90658f2017-10-04 15:29:08 +01001846 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
1847 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
1848 {
1849 mbedtls_mpi_free( &conf->dhm_P );
1850 mbedtls_mpi_free( &conf->dhm_G );
1851 return( ret );
1852 }
1853
1854 return( 0 );
1855}
Paul Bakker5121ce52009-01-03 21:22:43 +00001856
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001857int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00001858{
Janos Follath865b3eb2019-12-16 11:46:15 +00001859 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00001860
Glenn Strausscee11292021-12-20 01:43:17 -05001861 mbedtls_mpi_free( &conf->dhm_P );
1862 mbedtls_mpi_free( &conf->dhm_G );
1863
Gilles Peskinee5702482021-06-11 21:59:08 +02001864 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
1865 &conf->dhm_P ) ) != 0 ||
1866 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
1867 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01001868 {
1869 mbedtls_mpi_free( &conf->dhm_P );
1870 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00001871 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01001872 }
Paul Bakker1b57b062011-01-06 15:48:19 +00001873
1874 return( 0 );
1875}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02001876#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00001877
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02001878#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
1879/*
1880 * Set the minimum length for Diffie-Hellman parameters
1881 */
1882void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
1883 unsigned int bitlen )
1884{
1885 conf->dhm_min_bitlen = bitlen;
1886}
1887#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
1888
Gilles Peskineeccd8882020-03-10 12:19:08 +01001889#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08001890#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02001891/*
1892 * Set allowed/preferred hashes for handshake signatures
1893 */
1894void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
1895 const int *hashes )
1896{
1897 conf->sig_hashes = hashes;
1898}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08001899#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01001900
Jerry Yuf017ee42022-01-12 15:49:48 +08001901/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01001902void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
1903 const uint16_t* sig_algs )
1904{
Jerry Yuf017ee42022-01-12 15:49:48 +08001905#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1906 conf->sig_hashes = NULL;
1907#endif /* !MBEDTLS_DEPRECATED_REMOVED */
1908 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01001909}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001910#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02001911
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02001912#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01001913#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001914/*
1915 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01001916 *
1917 * mbedtls_ssl_setup() takes the provided list
1918 * and translates it to a list of IANA TLS group identifiers,
1919 * stored in ssl->handshake->group_list.
1920 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001921 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001922void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001923 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001924{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001925 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01001926 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001927}
Brett Warrene0edc842021-08-17 09:53:13 +01001928#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01001929#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001930
Brett Warrene0edc842021-08-17 09:53:13 +01001931/*
1932 * Set the allowed groups
1933 */
1934void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
1935 const uint16_t *group_list )
1936{
1937#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
1938 conf->curve_list = NULL;
1939#endif
1940 conf->group_list = group_list;
1941}
1942
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001943#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00001945{
Hanno Becker947194e2017-04-07 13:25:49 +01001946 /* Initialize to suppress unnecessary compiler warning */
1947 size_t hostname_len = 0;
1948
1949 /* Check if new hostname is valid before
1950 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01001951 if( hostname != NULL )
1952 {
1953 hostname_len = strlen( hostname );
1954
1955 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
1956 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1957 }
1958
1959 /* Now it's clear that we will overwrite the old hostname,
1960 * so we can free it safely */
1961
1962 if( ssl->hostname != NULL )
1963 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001964 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01001965 mbedtls_free( ssl->hostname );
1966 }
1967
1968 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01001969
Paul Bakker5121ce52009-01-03 21:22:43 +00001970 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01001971 {
1972 ssl->hostname = NULL;
1973 }
1974 else
1975 {
1976 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01001977 if( ssl->hostname == NULL )
1978 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02001979
Hanno Becker947194e2017-04-07 13:25:49 +01001980 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02001981
Hanno Becker947194e2017-04-07 13:25:49 +01001982 ssl->hostname[hostname_len] = '\0';
1983 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001984
1985 return( 0 );
1986}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01001987#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001988
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001989#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001990void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00001992 const unsigned char *, size_t),
1993 void *p_sni )
1994{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001995 conf->f_sni = f_sni;
1996 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00001997}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002001int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002002{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002003 size_t cur_len, tot_len;
2004 const char **p;
2005
2006 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002007 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2008 * MUST NOT be truncated."
2009 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002010 */
2011 tot_len = 0;
2012 for( p = protos; *p != NULL; p++ )
2013 {
2014 cur_len = strlen( *p );
2015 tot_len += cur_len;
2016
Ronald Cron8216dd32020-04-23 16:41:44 +02002017 if( ( cur_len == 0 ) ||
2018 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2019 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002021 }
2022
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002023 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002024
2025 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002026}
2027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002029{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002030 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002031}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002033
Johan Pascalb62bb512015-12-03 21:56:45 +01002034#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002035void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2036 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002037{
2038 conf->dtls_srtp_mki_support = support_mki_value;
2039}
2040
Ron Eldoref72faf2018-07-12 11:54:20 +03002041int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2042 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002043 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002044{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002045 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002046 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002047 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002048 }
Ron Eldor591f1622018-01-22 12:30:04 +02002049
2050 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002051 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002052 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002053 }
Ron Eldor591f1622018-01-22 12:30:04 +02002054
2055 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2056 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002057 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002058}
2059
Ron Eldoref72faf2018-07-12 11:54:20 +03002060int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002061 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002062{
Johan Pascal253d0262020-09-22 13:04:45 +02002063 const mbedtls_ssl_srtp_profile *p;
2064 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002065
Johan Pascal253d0262020-09-22 13:04:45 +02002066 /* check the profiles list: all entry must be valid,
2067 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002068 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2069 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2070 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002071 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002072 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002073 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002074 list_size++;
2075 }
2076 else
2077 {
2078 /* unsupported value, stop parsing and set the size to an error value */
2079 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002080 }
2081 }
2082
Johan Pascal5ef72d22020-10-28 17:05:47 +01002083 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002084 {
Johan Pascal253d0262020-09-22 13:04:45 +02002085 conf->dtls_srtp_profile_list = NULL;
2086 conf->dtls_srtp_profile_list_len = 0;
2087 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2088 }
2089
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002090 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002091 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002092
2093 return( 0 );
2094}
2095
Johan Pascal5ef72d22020-10-28 17:05:47 +01002096void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2097 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002098{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002099 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2100 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002101 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002102 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002103 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002104 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002105 else
2106 {
2107 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002108 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2109 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002110 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002111}
Johan Pascalb62bb512015-12-03 21:56:45 +01002112#endif /* MBEDTLS_SSL_DTLS_SRTP */
2113
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002114void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002115{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002116 conf->max_major_ver = major;
2117 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002118}
2119
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002120void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002121{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002122 conf->min_major_ver = major;
2123 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002124}
2125
Janos Follath088ce432017-04-10 12:42:31 +01002126#if defined(MBEDTLS_SSL_SRV_C)
2127void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2128 char cert_req_ca_list )
2129{
2130 conf->cert_req_ca_list = cert_req_ca_list;
2131}
2132#endif
2133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002135void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002136{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002137 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002138}
2139#endif
2140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002142void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002143{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002144 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002145}
2146#endif
2147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002149int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002150{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002152 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002155 }
2156
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002157 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002158
2159 return( 0 );
2160}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002162
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002163void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002164{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002165 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002166}
2167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002169void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002170{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002171 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002172}
2173
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002174void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002175{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002176 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002177}
2178
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002179void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002180 const unsigned char period[8] )
2181{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002182 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002183}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002187#if defined(MBEDTLS_SSL_CLI_C)
2188void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002189{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002190 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002191}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002192#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002193
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002194#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002195void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2196 mbedtls_ssl_ticket_write_t *f_ticket_write,
2197 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2198 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002199{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002200 conf->f_ticket_write = f_ticket_write;
2201 conf->f_ticket_parse = f_ticket_parse;
2202 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002203}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002204#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002206
Hanno Becker7e6c1782021-06-08 09:24:55 +01002207void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2208 mbedtls_ssl_export_keys_t *f_export_keys,
2209 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002210{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002211 ssl->f_export_keys = f_export_keys;
2212 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002213}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002214
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002215#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002216void mbedtls_ssl_conf_async_private_cb(
2217 mbedtls_ssl_config *conf,
2218 mbedtls_ssl_async_sign_t *f_async_sign,
2219 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2220 mbedtls_ssl_async_resume_t *f_async_resume,
2221 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002222 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002223{
2224 conf->f_async_sign_start = f_async_sign;
2225 conf->f_async_decrypt_start = f_async_decrypt;
2226 conf->f_async_resume = f_async_resume;
2227 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002228 conf->p_async_config_data = async_config_data;
2229}
2230
Gilles Peskine8f97af72018-04-26 11:46:10 +02002231void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2232{
2233 return( conf->p_async_config_data );
2234}
2235
Gilles Peskine1febfef2018-04-30 11:54:39 +02002236void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002237{
2238 if( ssl->handshake == NULL )
2239 return( NULL );
2240 else
2241 return( ssl->handshake->user_async_ctx );
2242}
2243
Gilles Peskine1febfef2018-04-30 11:54:39 +02002244void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002245 void *ctx )
2246{
2247 if( ssl->handshake != NULL )
2248 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002249}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002250#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002251
Paul Bakker5121ce52009-01-03 21:22:43 +00002252/*
2253 * SSL get accessors
2254 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002255uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002256{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002257 if( ssl->session != NULL )
2258 return( ssl->session->verify_result );
2259
2260 if( ssl->session_negotiate != NULL )
2261 return( ssl->session_negotiate->verify_result );
2262
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002263 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002264}
2265
Glenn Strauss8f526902022-01-13 00:04:49 -05002266int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2267{
2268 if( ssl == NULL || ssl->session == NULL )
2269 return( 0 );
2270
2271 return( ssl->session->ciphersuite );
2272}
2273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002275{
Paul Bakker926c8e42013-03-06 10:23:34 +01002276 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002277 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002280}
2281
Gilles Peskinee1a0c252022-01-13 01:08:03 +01002282mbedtls_ssl_protocol_version mbedtls_ssl_get_version_number(
2283 const mbedtls_ssl_context *ssl )
2284{
2285 /* For major_ver, only 3 is supported, so skip checking it. */
2286 switch( ssl->minor_ver )
2287 {
2288 case MBEDTLS_SSL_MINOR_VERSION_3:
2289 return( MBEDTLS_SSL_VERSION_1_2 );
2290 case MBEDTLS_SSL_MINOR_VERSION_4:
2291 return( MBEDTLS_SSL_VERSION_1_3 );
2292 default:
2293 return( MBEDTLS_SSL_VERSION_UNKNOWN );
2294 }
2295}
2296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002298{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002300 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002301 {
2302 switch( ssl->minor_ver )
2303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002305 return( "DTLSv1.2" );
2306
2307 default:
2308 return( "unknown (DTLS)" );
2309 }
2310 }
2311#endif
2312
Paul Bakker43ca69c2011-01-15 17:35:19 +00002313 switch( ssl->minor_ver )
2314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002316 return( "TLSv1.2" );
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002317 case MBEDTLS_SSL_MINOR_VERSION_4:
2318 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002319 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002320 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002321 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002322}
2323
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002324#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002325size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2326{
David Horstmann95d516f2021-05-04 18:36:56 +01002327 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002328 size_t read_mfl;
2329
2330 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2331 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2332 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2333 {
2334 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2335 }
2336
2337 /* Check if a smaller max length was negotiated */
2338 if( ssl->session_out != NULL )
2339 {
2340 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2341 if( read_mfl < max_len )
2342 {
2343 max_len = read_mfl;
2344 }
2345 }
2346
2347 // During a handshake, use the value being negotiated
2348 if( ssl->session_negotiate != NULL )
2349 {
2350 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2351 if( read_mfl < max_len )
2352 {
2353 max_len = read_mfl;
2354 }
2355 }
2356
2357 return( max_len );
2358}
2359
2360size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002361{
2362 size_t max_len;
2363
2364 /*
2365 * Assume mfl_code is correct since it was checked when set
2366 */
Angus Grattond8213d02016-05-25 20:56:48 +10002367 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002368
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002369 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002370 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002371 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002372 {
Angus Grattond8213d02016-05-25 20:56:48 +10002373 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002374 }
2375
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002376 /* During a handshake, use the value being negotiated */
2377 if( ssl->session_negotiate != NULL &&
2378 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2379 {
2380 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2381 }
2382
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002383 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002384}
2385#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2386
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002387#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002388size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002389{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002390 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2391 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2392 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2393 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2394 return ( 0 );
2395
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002396 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2397 return( ssl->mtu );
2398
2399 if( ssl->mtu == 0 )
2400 return( ssl->handshake->mtu );
2401
2402 return( ssl->mtu < ssl->handshake->mtu ?
2403 ssl->mtu : ssl->handshake->mtu );
2404}
2405#endif /* MBEDTLS_SSL_PROTO_DTLS */
2406
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002407int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2408{
2409 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2410
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002411#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2412 !defined(MBEDTLS_SSL_PROTO_DTLS)
2413 (void) ssl;
2414#endif
2415
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002416#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002417 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002418
2419 if( max_len > mfl )
2420 max_len = mfl;
2421#endif
2422
2423#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002424 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002425 {
Hanno Becker89490712020-02-05 10:50:12 +00002426 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002427 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2428 const size_t overhead = (size_t) ret;
2429
2430 if( ret < 0 )
2431 return( ret );
2432
2433 if( mtu <= overhead )
2434 {
2435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2436 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2437 }
2438
2439 if( max_len > mtu - overhead )
2440 max_len = mtu - overhead;
2441 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002442#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002443
Hanno Becker0defedb2018-08-10 12:35:02 +01002444#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2445 !defined(MBEDTLS_SSL_PROTO_DTLS)
2446 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002447#endif
2448
2449 return( (int) max_len );
2450}
2451
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002452int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2453{
2454 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2455
2456#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2457 (void) ssl;
2458#endif
2459
2460#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2461 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2462
2463 if( max_len > mfl )
2464 max_len = mfl;
2465#endif
2466
2467 return( (int) max_len );
2468}
2469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470#if defined(MBEDTLS_X509_CRT_PARSE_C)
2471const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002472{
2473 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002474 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002475
Hanno Beckere6824572019-02-07 13:18:46 +00002476#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002477 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002478#else
2479 return( NULL );
2480#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002481}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002485int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2486 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002487{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002488 int ret;
2489
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002490 if( ssl == NULL ||
2491 dst == NULL ||
2492 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002493 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002496 }
2497
Hanno Beckere810bbc2021-05-14 16:01:05 +01002498 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2499 * idempotent: Each session can only be exported once.
2500 *
2501 * (This is in preparation for TLS 1.3 support where we will
2502 * need the ability to export multiple sessions (aka tickets),
2503 * which will be achieved by calling mbedtls_ssl_get_session()
2504 * multiple times until it fails.)
2505 *
2506 * Check whether we have already exported the current session,
2507 * and fail if so.
2508 */
2509 if( ssl->session->exported == 1 )
2510 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2511
2512 ret = mbedtls_ssl_session_copy( dst, ssl->session );
2513 if( ret != 0 )
2514 return( ret );
2515
2516 /* Remember that we've exported the session. */
2517 ssl->session->exported = 1;
2518 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002519}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002521
Paul Bakker5121ce52009-01-03 21:22:43 +00002522/*
Hanno Beckera835da52019-05-16 12:39:07 +01002523 * Define ticket header determining Mbed TLS version
2524 * and structure of the ticket.
2525 */
2526
Hanno Becker94ef3b32019-05-16 12:50:45 +01002527/*
Hanno Becker50b59662019-05-28 14:30:45 +01002528 * Define bitflag determining compile-time settings influencing
2529 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01002530 */
2531
Hanno Becker50b59662019-05-28 14:30:45 +01002532#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01002533#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01002534#else
Hanno Becker3e088662019-05-29 11:10:18 +01002535#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002536#endif /* MBEDTLS_HAVE_TIME */
2537
2538#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01002539#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002540#else
Hanno Becker3e088662019-05-29 11:10:18 +01002541#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002542#endif /* MBEDTLS_X509_CRT_PARSE_C */
2543
2544#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01002545#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002546#else
Hanno Becker3e088662019-05-29 11:10:18 +01002547#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002548#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
2549
2550#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01002551#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002552#else
Hanno Becker3e088662019-05-29 11:10:18 +01002553#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002554#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2555
Hanno Becker94ef3b32019-05-16 12:50:45 +01002556#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01002557#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002558#else
Hanno Becker3e088662019-05-29 11:10:18 +01002559#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002560#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2561
Hanno Becker94ef3b32019-05-16 12:50:45 +01002562#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2563#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
2564#else
2565#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
2566#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2567
Hanno Becker3e088662019-05-29 11:10:18 +01002568#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
2569#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
2570#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
2571#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01002572#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
2573#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01002574
Hanno Becker50b59662019-05-28 14:30:45 +01002575#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01002576 ( (uint16_t) ( \
2577 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
2578 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
2579 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
2580 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01002581 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01002582 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01002583
Hanno Beckerf8787072019-05-16 12:41:07 +01002584static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01002585 MBEDTLS_VERSION_MAJOR,
2586 MBEDTLS_VERSION_MINOR,
2587 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01002588 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
2589 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01002590};
Hanno Beckera835da52019-05-16 12:39:07 +01002591
2592/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002593 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002594 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002595 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002596 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002597 *
Hanno Beckerdce50972021-08-01 05:39:23 +01002598 * opaque mbedtls_version[3]; // library version: major, minor, patch
2599 * opaque session_format[2]; // library-version specific 16-bit field
2600 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002601 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002602 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002603 * Note: When updating the format, remember to keep
2604 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002605 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002606 * // In this version, `session_format` determines
2607 * // the setting of those compile-time
2608 * // configuration options which influence
2609 * // the structure of mbedtls_ssl_session.
2610 *
Hanno Beckerfa0d61e2021-08-02 08:56:14 +01002611 * uint8_t minor_ver; // Protocol-version. Possible values:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002612 * // - TLS 1.2 (MBEDTLS_SSL_MINOR_VERSION_3)
2613 *
2614 * select (serialized_session.minor_ver) {
2615 *
2616 * case MBEDTLS_SSL_MINOR_VERSION_3: // TLS 1.2
2617 * serialized_session_tls12 data;
2618 *
2619 * };
2620 *
2621 * } serialized_session;
2622 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002623 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002624
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002625static int ssl_session_save( const mbedtls_ssl_session *session,
2626 unsigned char omit_header,
2627 unsigned char *buf,
2628 size_t buf_len,
2629 size_t *olen )
2630{
2631 unsigned char *p = buf;
2632 size_t used = 0;
2633
2634 if( !omit_header )
2635 {
2636 /*
2637 * Add Mbed TLS version identifier
2638 */
2639
2640 used += sizeof( ssl_serialized_session_header );
2641
2642 if( used <= buf_len )
2643 {
2644 memcpy( p, ssl_serialized_session_header,
2645 sizeof( ssl_serialized_session_header ) );
2646 p += sizeof( ssl_serialized_session_header );
2647 }
2648 }
2649
2650 /*
2651 * TLS version identifier
2652 */
2653 used += 1;
2654 if( used <= buf_len )
2655 {
2656 *p++ = session->minor_ver;
2657 }
2658
2659 /* Forward to version-specific serialization routine. */
2660 switch( session->minor_ver )
2661 {
2662#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2663 case MBEDTLS_SSL_MINOR_VERSION_3:
2664 {
2665 size_t remaining_len = used <= buf_len ? buf_len - used : 0;
2666 used += ssl_session_save_tls12( session, p, remaining_len );
2667 break;
2668 }
2669#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2670
2671 default:
2672 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2673 }
2674
2675 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02002676 if( used > buf_len )
2677 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002678
2679 return( 0 );
2680}
2681
2682/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02002683 * Public wrapper for ssl_session_save()
2684 */
2685int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
2686 unsigned char *buf,
2687 size_t buf_len,
2688 size_t *olen )
2689{
2690 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
2691}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002692
Jerry Yuf1b23ca2022-02-18 11:48:47 +08002693/*
2694 * Deserialize session, see mbedtls_ssl_session_save() for format.
2695 *
2696 * This internal version is wrapped by a public function that cleans up in
2697 * case of error, and has an extra option omit_header.
2698 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002699static int ssl_session_load( mbedtls_ssl_session *session,
2700 unsigned char omit_header,
2701 const unsigned char *buf,
2702 size_t len )
2703{
2704 const unsigned char *p = buf;
2705 const unsigned char * const end = buf + len;
2706
2707 if( !omit_header )
2708 {
2709 /*
2710 * Check Mbed TLS version identifier
2711 */
2712
2713 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
2714 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2715
2716 if( memcmp( p, ssl_serialized_session_header,
2717 sizeof( ssl_serialized_session_header ) ) != 0 )
2718 {
2719 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
2720 }
2721 p += sizeof( ssl_serialized_session_header );
2722 }
2723
2724 /*
2725 * TLS version identifier
2726 */
2727 if( 1 > (size_t)( end - p ) )
2728 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2729 session->minor_ver = *p++;
2730
2731 /* Dispatch according to TLS version. */
2732 switch( session->minor_ver )
2733 {
2734#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2735 case MBEDTLS_SSL_MINOR_VERSION_3: /* TLS 1.2 */
2736 {
2737 size_t remaining_len = ( end - p );
2738 return( ssl_session_load_tls12( session, p, remaining_len ) );
2739 }
2740#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2741
2742 default:
2743 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2744 }
2745}
2746
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002747/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02002748 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02002749 */
2750int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
2751 const unsigned char *buf,
2752 size_t len )
2753{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02002754 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02002755
2756 if( ret != 0 )
2757 mbedtls_ssl_session_free( session );
2758
2759 return( ret );
2760}
2761
2762/*
Paul Bakker1961b702013-01-25 14:49:24 +01002763 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00002764 */
Hanno Becker41934dd2021-08-07 19:13:43 +01002765static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
2766{
2767 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2768
Ronald Cron66dbf912022-02-02 15:33:46 +01002769 /*
2770 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01002771 * that were written into the output buffer by the previous handshake step,
2772 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01002773 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
2774 * We proceed to the next handshake step only when all data from the
2775 * previous one have been sent to the peer, thus we make sure that this is
2776 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
2777 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
2778 * we have to wait before to go ahead.
2779 * In the case of TLS 1.3, handshake step handlers do not send data to the
2780 * peer. Data are only sent here and through
2781 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
2782 * alert occured.
2783 */
Hanno Becker41934dd2021-08-07 19:13:43 +01002784 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2785 return( ret );
2786
2787#if defined(MBEDTLS_SSL_PROTO_DTLS)
2788 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2789 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
2790 {
2791 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
2792 return( ret );
2793 }
2794#endif /* MBEDTLS_SSL_PROTO_DTLS */
2795
2796 return( ret );
2797}
2798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002800{
Hanno Becker41934dd2021-08-07 19:13:43 +01002801 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002802
Hanno Becker41934dd2021-08-07 19:13:43 +01002803 if( ssl == NULL ||
2804 ssl->conf == NULL ||
2805 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00002806 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01002807 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002808 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01002809 }
2810
2811 ret = ssl_prepare_handshake_step( ssl );
2812 if( ret != 0 )
2813 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002814
Jerry Yue7047812021-09-13 19:26:39 +08002815 ret = mbedtls_ssl_handle_pending_alert( ssl );
2816 if( ret != 0 )
2817 goto cleanup;
2818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002820 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08002821 {
Ronald Cron6f135e12021-12-08 16:57:54 +01002822#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08002823 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yuf4436812021-08-26 22:59:56 +08002824 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01002825#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08002826
2827#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2828 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
2829 ret = mbedtls_ssl_handshake_client_step( ssl );
2830#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2831 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002832#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002834 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08002835 {
Ronald Cron6f135e12021-12-08 16:57:54 +01002836#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08002837 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08002838 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01002839#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08002840
2841#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2842 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
2843 ret = mbedtls_ssl_handshake_server_step( ssl );
2844#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2845 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002846#endif
2847
Jerry Yue7047812021-09-13 19:26:39 +08002848 if( ret != 0 )
2849 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08002850 /* handshake_step return error. And it is same
2851 * with alert_reason.
2852 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08002853 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08002854 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08002855 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08002856 goto cleanup;
2857 }
2858 }
2859
2860cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01002861 return( ret );
2862}
2863
2864/*
2865 * Perform the SSL handshake
2866 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01002868{
2869 int ret = 0;
2870
Hanno Beckera817ea42020-10-20 15:20:23 +01002871 /* Sanity checks */
2872
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002873 if( ssl == NULL || ssl->conf == NULL )
2874 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2875
Hanno Beckera817ea42020-10-20 15:20:23 +01002876#if defined(MBEDTLS_SSL_PROTO_DTLS)
2877 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2878 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
2879 {
2880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2881 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2882 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2883 }
2884#endif /* MBEDTLS_SSL_PROTO_DTLS */
2885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01002887
Hanno Beckera817ea42020-10-20 15:20:23 +01002888 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00002889 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01002890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002891 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002892
2893 if( ret != 0 )
2894 break;
2895 }
2896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002898
2899 return( ret );
2900}
2901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902#if defined(MBEDTLS_SSL_RENEGOTIATION)
2903#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002904/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002905 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00002906 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002908{
Janos Follath865b3eb2019-12-16 11:46:15 +00002909 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002912
2913 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2915 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002916
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002917 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002918 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002919 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002920 return( ret );
2921 }
2922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002924
2925 return( 0 );
2926}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002927#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002928
2929/*
2930 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 * - any side: calling mbedtls_ssl_renegotiate(),
2932 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
2933 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02002934 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002935 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002936 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002937 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00002938int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00002939{
Janos Follath865b3eb2019-12-16 11:46:15 +00002940 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00002941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002943
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002944 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
2945 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00002946
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02002947 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
2948 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002950 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02002952 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002953 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002954 ssl->handshake->out_msg_seq = 1;
2955 else
2956 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02002957 }
2958#endif
2959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
2961 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00002962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00002964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002965 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00002966 return( ret );
2967 }
2968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002969 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002970
2971 return( 0 );
2972}
2973
2974/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002975 * Renegotiate current connection on client,
2976 * or request renegotiation on server
2977 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002979{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002981
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002982 if( ssl == NULL || ssl->conf == NULL )
2983 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002986 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002987 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002988 {
Paul Elliott27b0d942022-03-18 21:55:32 +00002989 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002990 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02002993
2994 /* Did we already try/start sending HelloRequest? */
2995 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02002997
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002998 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002999 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003003 /*
3004 * On client, either start the renegotiation process or,
3005 * if already in progress, continue the handshake
3006 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003008 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003009 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003011
Hanno Becker40cdaa12020-02-05 10:48:27 +00003012 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003013 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003014 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003015 return( ret );
3016 }
3017 }
3018 else
3019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003020 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003023 return( ret );
3024 }
3025 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003027
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003028 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003029}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003031
Gilles Peskine9b562d52018-04-25 20:32:43 +02003032void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003033{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003034 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3035
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003036 if( handshake == NULL )
3037 return;
3038
Brett Warrene0edc842021-08-17 09:53:13 +01003039#if defined(MBEDTLS_ECP_C)
3040#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3041 if ( ssl->handshake->group_list_heap_allocated )
3042 mbedtls_free( (void*) handshake->group_list );
3043 handshake->group_list = NULL;
3044#endif /* MBEDTLS_DEPRECATED_REMOVED */
3045#endif /* MBEDTLS_ECP_C */
3046
Jerry Yuf017ee42022-01-12 15:49:48 +08003047#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3048#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3049 if ( ssl->handshake->sig_algs_heap_allocated )
3050 mbedtls_free( (void*) handshake->sig_algs );
3051 handshake->sig_algs = NULL;
3052#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003053#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3054 if( ssl->handshake->certificate_request_context )
3055 {
3056 mbedtls_free( (void*) handshake->certificate_request_context );
3057 }
3058#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003059#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3060
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003061#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3062 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3063 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003064 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003065 handshake->async_in_progress = 0;
3066 }
3067#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3068
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003069#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003070#if defined(MBEDTLS_USE_PSA_CRYPTO)
3071 psa_hash_abort( &handshake->fin_sha256_psa );
3072#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003073 mbedtls_sha256_free( &handshake->fin_sha256 );
3074#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003075#endif
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +02003076#if defined(MBEDTLS_SHA384_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003077#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003078 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003079#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003080 mbedtls_sha512_free( &handshake->fin_sha512 );
3081#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003082#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003084#if defined(MBEDTLS_DHM_C)
3085 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003086#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087#if defined(MBEDTLS_ECDH_C)
3088 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003089#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003090#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003091 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003092#if defined(MBEDTLS_SSL_CLI_C)
3093 mbedtls_free( handshake->ecjpake_cache );
3094 handshake->ecjpake_cache = NULL;
3095 handshake->ecjpake_cache_len = 0;
3096#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003097#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003098
Janos Follath4ae5c292016-02-10 11:27:43 +00003099#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3100 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003101 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003103#endif
3104
Gilles Peskineeccd8882020-03-10 12:19:08 +01003105#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003106 if( handshake->psk != NULL )
3107 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003108 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003109 mbedtls_free( handshake->psk );
3110 }
3111#endif
3112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003113#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3114 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003115 /*
3116 * Free only the linked list wrapper, not the keys themselves
3117 * since the belong to the SNI callback
3118 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003119 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003121
Gilles Peskineeccd8882020-03-10 12:19:08 +01003122#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003123 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003124 if( handshake->ecrs_peer_cert != NULL )
3125 {
3126 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3127 mbedtls_free( handshake->ecrs_peer_cert );
3128 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003129#endif
3130
Hanno Becker75173122019-02-06 16:18:31 +00003131#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3132 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3133 mbedtls_pk_free( &handshake->peer_pubkey );
3134#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3135
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003136#if defined(MBEDTLS_SSL_CLI_C) && \
3137 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3138 mbedtls_free( handshake->cookie );
3139#endif /* MBEDTLS_SSL_CLI_C &&
3140 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003141
3142#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003143 mbedtls_ssl_flight_free( handshake->flight );
3144 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003145#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003146
Ronald Cronf12b81d2022-03-15 10:42:41 +01003147#if defined(MBEDTLS_ECDH_C) && \
3148 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrong8113d252022-03-23 10:57:04 +01003149 if( handshake->ecdh_psa_shared_key == 0 )
3150 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003151#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3152
Ronald Cron6f135e12021-12-08 16:57:54 +01003153#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003154 mbedtls_ssl_transform_free( handshake->transform_handshake );
3155 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003156 mbedtls_free( handshake->transform_earlydata );
3157 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003158#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003159
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003160
3161#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3162 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3163 * processes datagrams and the fact that a datagram is allowed to have
3164 * several records in it, it is possible that the I/O buffers are not
3165 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003166 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3167 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003168#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003169
Jerry Yuba9c7272021-10-30 11:54:10 +08003170 /* mbedtls_platform_zeroize MUST be last one in this function */
3171 mbedtls_platform_zeroize( handshake,
3172 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003173}
3174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003175void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003176{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003177 if( session == NULL )
3178 return;
3179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003181 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003182#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003183
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003184#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003186#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003187
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003188 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003189}
3190
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003191#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003192
3193#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3194#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3195#else
3196#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3197#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3198
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003199#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003200
3201#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3202#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3203#else
3204#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3205#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3206
3207#if defined(MBEDTLS_SSL_ALPN)
3208#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3209#else
3210#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3211#endif /* MBEDTLS_SSL_ALPN */
3212
3213#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3214#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3215#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3216#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3217
3218#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3219 ( (uint32_t) ( \
3220 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3221 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3222 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3223 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3224 0u ) )
3225
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003226static unsigned char ssl_serialized_context_header[] = {
3227 MBEDTLS_VERSION_MAJOR,
3228 MBEDTLS_VERSION_MINOR,
3229 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003230 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3231 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3232 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3233 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3234 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003235};
3236
Paul Bakker5121ce52009-01-03 21:22:43 +00003237/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003238 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003239 *
3240 * The format of the serialized data is:
3241 * (in the presentation language of TLS, RFC 8446 section 3)
3242 *
3243 * // header
3244 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003245 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003246 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003247 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003248 * Note: When updating the format, remember to keep these
3249 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003250 *
3251 * // session sub-structure
3252 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3253 * // transform sub-structure
3254 * uint8 random[64]; // ServerHello.random+ClientHello.random
3255 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3256 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3257 * // fields from ssl_context
3258 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3259 * uint64 in_window_top; // DTLS: last validated record seq_num
3260 * uint64 in_window; // DTLS: bitmask for replay protection
3261 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3262 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3263 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3264 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3265 *
3266 * Note that many fields of the ssl_context or sub-structures are not
3267 * serialized, as they fall in one of the following categories:
3268 *
3269 * 1. forced value (eg in_left must be 0)
3270 * 2. pointer to dynamically-allocated memory (eg session, transform)
3271 * 3. value can be re-derived from other data (eg session keys from MS)
3272 * 4. value was temporary (eg content of input buffer)
3273 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003274 */
3275int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3276 unsigned char *buf,
3277 size_t buf_len,
3278 size_t *olen )
3279{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003280 unsigned char *p = buf;
3281 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003282 size_t session_len;
3283 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003284
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003285 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003286 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3287 * this function's documentation.
3288 *
3289 * These are due to assumptions/limitations in the implementation. Some of
3290 * them are likely to stay (no handshake in progress) some might go away
3291 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003292 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003293 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003294 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003295 {
3296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003297 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003298 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003299 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003300 {
3301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003302 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003303 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003304 /* Double-check that sub-structures are indeed ready */
3305 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003306 {
3307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003308 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003309 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003310 /* There must be no pending incoming or outgoing data */
3311 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003312 {
3313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003314 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003315 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003316 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003317 {
3318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003319 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003320 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003321 /* Protocol must be DLTS, not TLS */
3322 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003323 {
3324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003325 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003326 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003327 /* Version must be 1.2 */
3328 if( ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003329 {
3330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003331 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003332 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003333 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003334 {
3335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003336 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003337 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003338 /* We must be using an AEAD ciphersuite */
3339 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003340 {
3341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003342 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003343 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003344 /* Renegotiation must not be enabled */
3345#if defined(MBEDTLS_SSL_RENEGOTIATION)
3346 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003347 {
3348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003349 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003350 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003351#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003352
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003353 /*
3354 * Version and format identifier
3355 */
3356 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003357
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003358 if( used <= buf_len )
3359 {
3360 memcpy( p, ssl_serialized_context_header,
3361 sizeof( ssl_serialized_context_header ) );
3362 p += sizeof( ssl_serialized_context_header );
3363 }
3364
3365 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003366 * Session (length + data)
3367 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003368 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003369 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3370 return( ret );
3371
3372 used += 4 + session_len;
3373 if( used <= buf_len )
3374 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003375 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3376 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003377
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003378 ret = ssl_session_save( ssl->session, 1,
3379 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003380 if( ret != 0 )
3381 return( ret );
3382
3383 p += session_len;
3384 }
3385
3386 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003387 * Transform
3388 */
3389 used += sizeof( ssl->transform->randbytes );
3390 if( used <= buf_len )
3391 {
3392 memcpy( p, ssl->transform->randbytes,
3393 sizeof( ssl->transform->randbytes ) );
3394 p += sizeof( ssl->transform->randbytes );
3395 }
3396
3397#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3398 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3399 if( used <= buf_len )
3400 {
3401 *p++ = ssl->transform->in_cid_len;
3402 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3403 p += ssl->transform->in_cid_len;
3404
3405 *p++ = ssl->transform->out_cid_len;
3406 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3407 p += ssl->transform->out_cid_len;
3408 }
3409#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3410
3411 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003412 * Saved fields from top-level ssl_context structure
3413 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003414 used += 4;
3415 if( used <= buf_len )
3416 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003417 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3418 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003419 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003420
3421#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3422 used += 16;
3423 if( used <= buf_len )
3424 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003425 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3426 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003427
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003428 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3429 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003430 }
3431#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3432
3433#if defined(MBEDTLS_SSL_PROTO_DTLS)
3434 used += 1;
3435 if( used <= buf_len )
3436 {
3437 *p++ = ssl->disable_datagram_packing;
3438 }
3439#endif /* MBEDTLS_SSL_PROTO_DTLS */
3440
Jerry Yuae0b2e22021-10-08 15:21:19 +08003441 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003442 if( used <= buf_len )
3443 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003444 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3445 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003446 }
3447
3448#if defined(MBEDTLS_SSL_PROTO_DTLS)
3449 used += 2;
3450 if( used <= buf_len )
3451 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003452 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
3453 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003454 }
3455#endif /* MBEDTLS_SSL_PROTO_DTLS */
3456
3457#if defined(MBEDTLS_SSL_ALPN)
3458 {
3459 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02003460 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003461 : 0;
3462
3463 used += 1 + alpn_len;
3464 if( used <= buf_len )
3465 {
3466 *p++ = alpn_len;
3467
3468 if( ssl->alpn_chosen != NULL )
3469 {
3470 memcpy( p, ssl->alpn_chosen, alpn_len );
3471 p += alpn_len;
3472 }
3473 }
3474 }
3475#endif /* MBEDTLS_SSL_ALPN */
3476
3477 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003478 * Done
3479 */
3480 *olen = used;
3481
3482 if( used > buf_len )
3483 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003484
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003485 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
3486
Hanno Becker43aefe22020-02-05 10:44:56 +00003487 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003488}
3489
3490/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003491 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003492 *
3493 * This internal version is wrapped by a public function that cleans up in
3494 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003495 */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003496static int ssl_context_load( mbedtls_ssl_context *ssl,
3497 const unsigned char *buf,
3498 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003499{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003500 const unsigned char *p = buf;
3501 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003502 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00003503 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003504
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003505 /*
3506 * The context should have been freshly setup or reset.
3507 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02003508 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003509 * renegotiating, or if the user mistakenly loaded a session first.)
3510 */
3511 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
3512 ssl->session != NULL )
3513 {
3514 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3515 }
3516
3517 /*
3518 * We can't check that the config matches the initial one, but we can at
3519 * least check it matches the requirements for serializing.
3520 */
3521 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
3522 ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
3523 ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 ||
3524 ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ||
3525 ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 ||
3526#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003527 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003528#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003529 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003530 {
3531 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3532 }
3533
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003534 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
3535
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003536 /*
3537 * Check version identifier
3538 */
3539 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
3540 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3541
3542 if( memcmp( p, ssl_serialized_context_header,
3543 sizeof( ssl_serialized_context_header ) ) != 0 )
3544 {
3545 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3546 }
3547 p += sizeof( ssl_serialized_context_header );
3548
3549 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003550 * Session
3551 */
3552 if( (size_t)( end - p ) < 4 )
3553 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3554
3555 session_len = ( (size_t) p[0] << 24 ) |
3556 ( (size_t) p[1] << 16 ) |
3557 ( (size_t) p[2] << 8 ) |
3558 ( (size_t) p[3] );
3559 p += 4;
3560
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003561 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00003562 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003563 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003564 ssl->session_in = ssl->session;
3565 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003566 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003567
3568 if( (size_t)( end - p ) < session_len )
3569 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3570
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003571 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003572 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003573 {
3574 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003575 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003576 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003577
3578 p += session_len;
3579
3580 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003581 * Transform
3582 */
3583
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003584 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00003585 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003586 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003587 ssl->transform_in = ssl->transform;
3588 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003589 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003590
3591 /* Read random bytes and populate structure */
3592 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
3593 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08003594#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00003595 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003596 ssl->session->ciphersuite,
3597 ssl->session->master,
Hanno Beckerbd257552021-03-22 06:59:27 +00003598#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
3599 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003600 ssl->session->encrypt_then_mac,
Hanno Beckerbd257552021-03-22 06:59:27 +00003601#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
3602 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003603 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
3604 p, /* currently pointing to randbytes */
3605 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
3606 ssl->conf->endpoint,
3607 ssl );
3608 if( ret != 0 )
3609 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08003610#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003611 p += sizeof( ssl->transform->randbytes );
3612
3613#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3614 /* Read connection IDs and store them */
3615 if( (size_t)( end - p ) < 1 )
3616 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3617
3618 ssl->transform->in_cid_len = *p++;
3619
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02003620 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003621 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3622
3623 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
3624 p += ssl->transform->in_cid_len;
3625
3626 ssl->transform->out_cid_len = *p++;
3627
3628 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
3629 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3630
3631 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
3632 p += ssl->transform->out_cid_len;
3633#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3634
3635 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003636 * Saved fields from top-level ssl_context structure
3637 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003638 if( (size_t)( end - p ) < 4 )
3639 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3640
3641 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
3642 ( (uint32_t) p[1] << 16 ) |
3643 ( (uint32_t) p[2] << 8 ) |
3644 ( (uint32_t) p[3] );
3645 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003646
3647#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3648 if( (size_t)( end - p ) < 16 )
3649 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3650
3651 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
3652 ( (uint64_t) p[1] << 48 ) |
3653 ( (uint64_t) p[2] << 40 ) |
3654 ( (uint64_t) p[3] << 32 ) |
3655 ( (uint64_t) p[4] << 24 ) |
3656 ( (uint64_t) p[5] << 16 ) |
3657 ( (uint64_t) p[6] << 8 ) |
3658 ( (uint64_t) p[7] );
3659 p += 8;
3660
3661 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
3662 ( (uint64_t) p[1] << 48 ) |
3663 ( (uint64_t) p[2] << 40 ) |
3664 ( (uint64_t) p[3] << 32 ) |
3665 ( (uint64_t) p[4] << 24 ) |
3666 ( (uint64_t) p[5] << 16 ) |
3667 ( (uint64_t) p[6] << 8 ) |
3668 ( (uint64_t) p[7] );
3669 p += 8;
3670#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3671
3672#if defined(MBEDTLS_SSL_PROTO_DTLS)
3673 if( (size_t)( end - p ) < 1 )
3674 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3675
3676 ssl->disable_datagram_packing = *p++;
3677#endif /* MBEDTLS_SSL_PROTO_DTLS */
3678
Jerry Yud9a94fe2021-09-28 18:58:59 +08003679 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003680 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08003681 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
3682 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003683
3684#if defined(MBEDTLS_SSL_PROTO_DTLS)
3685 if( (size_t)( end - p ) < 2 )
3686 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3687
3688 ssl->mtu = ( p[0] << 8 ) | p[1];
3689 p += 2;
3690#endif /* MBEDTLS_SSL_PROTO_DTLS */
3691
3692#if defined(MBEDTLS_SSL_ALPN)
3693 {
3694 uint8_t alpn_len;
3695 const char **cur;
3696
3697 if( (size_t)( end - p ) < 1 )
3698 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3699
3700 alpn_len = *p++;
3701
3702 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
3703 {
3704 /* alpn_chosen should point to an item in the configured list */
3705 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
3706 {
3707 if( strlen( *cur ) == alpn_len &&
3708 memcmp( p, cur, alpn_len ) == 0 )
3709 {
3710 ssl->alpn_chosen = *cur;
3711 break;
3712 }
3713 }
3714 }
3715
3716 /* can only happen on conf mismatch */
3717 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
3718 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3719
3720 p += alpn_len;
3721 }
3722#endif /* MBEDTLS_SSL_ALPN */
3723
3724 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003725 * Forced fields from top-level ssl_context structure
3726 *
3727 * Most of them already set to the correct value by mbedtls_ssl_init() and
3728 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
3729 */
3730 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
3731
3732 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
3733 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
3734
Hanno Becker361b10d2019-08-30 10:42:49 +01003735 /* Adjust pointers for header fields of outgoing records to
3736 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00003737 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01003738
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003739#if defined(MBEDTLS_SSL_PROTO_DTLS)
3740 ssl->in_epoch = 1;
3741#endif
3742
3743 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
3744 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00003745 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
3746 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003747 if( ssl->handshake != NULL )
3748 {
3749 mbedtls_ssl_handshake_free( ssl );
3750 mbedtls_free( ssl->handshake );
3751 ssl->handshake = NULL;
3752 }
3753
3754 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003755 * Done - should have consumed entire buffer
3756 */
3757 if( p != end )
3758 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003759
3760 return( 0 );
3761}
3762
3763/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003764 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003765 */
3766int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
3767 const unsigned char *buf,
3768 size_t len )
3769{
3770 int ret = ssl_context_load( context, buf, len );
3771
3772 if( ret != 0 )
3773 mbedtls_ssl_free( context );
3774
3775 return( ret );
3776}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003777#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003778
3779/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003780 * Free an SSL context
3781 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003782void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003783{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003784 if( ssl == NULL )
3785 return;
3786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003788
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003789 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003790 {
sander-visserb8aa2072020-05-06 22:05:13 +02003791#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3792 size_t out_buf_len = ssl->out_buf_len;
3793#else
3794 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
3795#endif
3796
Darryl Greenb33cc762019-11-28 14:29:44 +00003797 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003798 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003799 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003800 }
3801
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003802 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003803 {
sander-visserb8aa2072020-05-06 22:05:13 +02003804#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3805 size_t in_buf_len = ssl->in_buf_len;
3806#else
3807 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
3808#endif
3809
Darryl Greenb33cc762019-11-28 14:29:44 +00003810 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003811 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003812 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003813 }
3814
Paul Bakker48916f92012-09-16 19:57:18 +00003815 if( ssl->transform )
3816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003817 mbedtls_ssl_transform_free( ssl->transform );
3818 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003819 }
3820
3821 if( ssl->handshake )
3822 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02003823 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003824 mbedtls_ssl_transform_free( ssl->transform_negotiate );
3825 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00003826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003827 mbedtls_free( ssl->handshake );
3828 mbedtls_free( ssl->transform_negotiate );
3829 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00003830 }
3831
Ronald Cron6f135e12021-12-08 16:57:54 +01003832#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01003833 mbedtls_ssl_transform_free( ssl->transform_application );
3834 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01003835#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01003836
Paul Bakkerc0463502013-02-14 11:19:38 +01003837 if( ssl->session )
3838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003839 mbedtls_ssl_session_free( ssl->session );
3840 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003841 }
3842
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02003843#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02003844 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003845 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003846 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003847 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00003848 }
Paul Bakker0be444a2013-08-27 21:55:01 +02003849#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003850
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003851#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02003853#endif
3854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00003856
Paul Bakker86f04f42013-02-14 11:20:09 +01003857 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003858 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003859}
3860
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02003861/*
3862 * Initialze mbedtls_ssl_config
3863 */
3864void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
3865{
3866 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
3867}
3868
Gilles Peskineeccd8882020-03-10 12:19:08 +01003869#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003870#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskineae270bf2021-06-02 00:05:29 +02003871/* The selection should be the same as mbedtls_x509_crt_profile_default in
Gilles Peskinea28f0f52021-06-02 15:29:38 +02003872 * x509_crt.c. Here, the order matters. Currently we favor stronger hashes,
3873 * for no fundamental reason.
Gilles Peskineae270bf2021-06-02 00:05:29 +02003874 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
3875 * about this list. */
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003876static int ssl_preset_default_hashes[] = {
3877#if defined(MBEDTLS_SHA512_C)
3878 MBEDTLS_MD_SHA512,
Mateusz Starzyk3352a532021-04-06 14:28:22 +02003879#endif
3880#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003881 MBEDTLS_MD_SHA384,
3882#endif
3883#if defined(MBEDTLS_SHA256_C)
3884 MBEDTLS_MD_SHA256,
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02003885#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003886 MBEDTLS_MD_NONE
3887};
Jerry Yuf017ee42022-01-12 15:49:48 +08003888#endif /* !MBEDTLS_DEPRECATED_REMOVED */
3889#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003890
Gilles Peskineae270bf2021-06-02 00:05:29 +02003891/* The selection should be the same as mbedtls_x509_crt_profile_default in
3892 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02003893 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02003894 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02003895 * about this list.
3896 */
Brett Warrene0edc842021-08-17 09:53:13 +01003897static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02003898#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003899 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003900#endif
3901#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003902 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003903#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02003904#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003905 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003906#endif
3907#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003908 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003909#endif
3910#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003911 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003912#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02003913#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003914 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003915#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02003916#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003917 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003918#endif
3919#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003920 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003921#endif
Brett Warrene0edc842021-08-17 09:53:13 +01003922 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02003923};
Gilles Peskineae270bf2021-06-02 00:05:29 +02003924
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003925static int ssl_preset_suiteb_ciphersuites[] = {
3926 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
3927 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
3928 0
3929};
3930
Gilles Peskineeccd8882020-03-10 12:19:08 +01003931#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003932#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003933static int ssl_preset_suiteb_hashes[] = {
Jerry Yuf017ee42022-01-12 15:49:48 +08003934#if defined(MBEDTLS_SHA256_C)
3935 MBEDTLS_MD_SHA256,
3936#endif
Jerry Yu713013f2022-01-17 18:16:35 +08003937#if defined(MBEDTLS_SHA384_C)
3938 MBEDTLS_MD_SHA384,
3939#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003940 MBEDTLS_MD_NONE
3941};
Jerry Yuf017ee42022-01-12 15:49:48 +08003942#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01003943
Jerry Yu909df7b2022-01-22 11:56:27 +08003944/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08003945 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08003946 * rules SHOULD be upheld.
3947 * - No duplicate entries.
3948 * - But if there is a good reason, do not change the order of the algorithms.
3949 * - ssl_tls12_present* is for TLS 1.2 use only.
3950 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08003951 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01003952static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08003953
Jerry Yued5e9f42022-01-26 11:21:34 +08003954#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \
3955 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
3956 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
3957#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C &&
3958 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08003959
Jerry Yu909df7b2022-01-22 11:56:27 +08003960#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \
3961 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
3962 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
3963#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
3964 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
3965
Jerry Yued5e9f42022-01-26 11:21:34 +08003966#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA512_C) && \
3967 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
3968 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
3969#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
3970 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08003971
Jerry Yu909df7b2022-01-22 11:56:27 +08003972#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C)
3973 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
3974#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */
3975
Ronald Cron8540cf62022-03-16 08:01:09 +01003976#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA512_C)
3977 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
3978#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
3979
3980#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA384_C)
3981 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
3982#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
3983
Jerry Yu53037892022-01-25 11:02:06 +08003984#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C)
3985 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
3986#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
3987
Jerry Yu909df7b2022-01-22 11:56:27 +08003988 MBEDTLS_TLS1_3_SIG_NONE
3989};
3990
3991/* NOTICE: see above */
3992#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3993static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Jerry Yu713013f2022-01-17 18:16:35 +08003994#if defined(MBEDTLS_SHA512_C)
3995 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA512 )
3996#endif
Jerry Yu11f0a9c2022-01-12 18:43:08 +08003997#if defined(MBEDTLS_SHA384_C)
3998 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 )
3999#endif
4000#if defined(MBEDTLS_SHA256_C)
4001 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 )
4002#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08004003 MBEDTLS_TLS1_3_SIG_NONE
4004};
4005#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4006/* NOTICE: see above */
4007static uint16_t ssl_preset_suiteb_sig_algs[] = {
4008
Jerry Yu909df7b2022-01-22 11:56:27 +08004009#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \
4010 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4011 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
4012#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C &&
4013 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4014
Jerry Yu53037892022-01-25 11:02:06 +08004015#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \
4016 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4017 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
4018#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
4019 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004020
4021#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C)
4022 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4023#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004024
Jerry Yu53037892022-01-25 11:02:06 +08004025#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C)
4026 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4027#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4028
Jerry Yu713013f2022-01-17 18:16:35 +08004029 MBEDTLS_TLS1_3_SIG_NONE
4030};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004031
Jerry Yu909df7b2022-01-22 11:56:27 +08004032/* NOTICE: see above */
4033#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4034static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Jerry Yu713013f2022-01-17 18:16:35 +08004035#if defined(MBEDTLS_SHA256_C)
4036 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 )
4037#endif
Jerry Yu18c833e2022-01-25 10:55:47 +08004038#if defined(MBEDTLS_SHA384_C)
4039 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 )
4040#endif
Xiaofei Bai95395012021-11-25 08:51:30 +00004041 MBEDTLS_TLS1_3_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004042};
Jerry Yu909df7b2022-01-22 11:56:27 +08004043#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4044
Jerry Yu1a8b4812022-01-20 17:56:50 +08004045#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004046
Brett Warrene0edc842021-08-17 09:53:13 +01004047static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004048#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004049 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004050#endif
4051#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004052 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004053#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004054 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004055};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004056
Jerry Yu1a8b4812022-01-20 17:56:50 +08004057#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004058/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004059 * to make sure there are no duplicated signature algorithm entries. */
Jerry Yuf377d642022-01-25 10:43:59 +08004060static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004061{
4062 size_t i, j;
4063 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004064
Jerry Yuf377d642022-01-25 10:43:59 +08004065 for( i = 0; sig_algs[i] != MBEDTLS_TLS1_3_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004066 {
Jerry Yuf377d642022-01-25 10:43:59 +08004067 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004068 {
Jerry Yuf377d642022-01-25 10:43:59 +08004069 if( sig_algs[i] != sig_algs[j] )
4070 continue;
4071 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4072 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4073 sig_algs[i], j, i );
4074 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004075 }
4076 }
4077 return( ret );
4078}
4079
4080#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4081
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004082/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004083 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004084 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004085int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004086 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004087{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004088#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004089 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004090#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004091
Jerry Yu1a8b4812022-01-20 17:56:50 +08004092#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004093 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004094 {
4095 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4096 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4097 }
4098
Jerry Yuf377d642022-01-25 10:43:59 +08004099 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004100 {
4101 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4102 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4103 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004104
4105#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004106 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004107 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004108 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004109 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4110 }
4111
Jerry Yuf377d642022-01-25 10:43:59 +08004112 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004113 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004114 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004115 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4116 }
4117#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004118#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4119
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004120 /* Use the functions here so that they are covered in tests,
4121 * but otherwise access member directly for efficiency */
4122 mbedtls_ssl_conf_endpoint( conf, endpoint );
4123 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004124
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004125 /*
4126 * Things that are common to all presets
4127 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004128#if defined(MBEDTLS_SSL_CLI_C)
4129 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4130 {
4131 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4132#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4133 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4134#endif
4135 }
4136#endif
4137
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004138#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4139 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4140#endif
4141
4142#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4143 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4144#endif
4145
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004146#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004147 conf->f_cookie_write = ssl_cookie_write_dummy;
4148 conf->f_cookie_check = ssl_cookie_check_dummy;
4149#endif
4150
4151#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4152 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4153#endif
4154
Janos Follath088ce432017-04-10 12:42:31 +01004155#if defined(MBEDTLS_SSL_SRV_C)
4156 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004157 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004158#endif
4159
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004160#if defined(MBEDTLS_SSL_PROTO_DTLS)
4161 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4162 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4163#endif
4164
4165#if defined(MBEDTLS_SSL_RENEGOTIATION)
4166 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004167 memset( conf->renego_period, 0x00, 2 );
4168 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004169#endif
4170
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004171#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004172 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4173 {
4174 const unsigned char dhm_p[] =
4175 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4176 const unsigned char dhm_g[] =
4177 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004178
Hanno Beckere2defad2021-07-24 05:59:17 +01004179 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4180 dhm_p, sizeof( dhm_p ),
4181 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4182 {
4183 return( ret );
4184 }
4185 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004186#endif
4187
Ronald Cron6f135e12021-12-08 16:57:54 +01004188#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker71f1ed62021-07-24 06:01:47 +01004189 /*
4190 * Allow all TLS 1.3 key exchange modes by default.
4191 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004192 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004193#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004194
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004195 /*
4196 * Preset-specific defaults
4197 */
4198 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004199 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004200 /*
4201 * NSA Suite B
4202 */
4203 case MBEDTLS_SSL_PRESET_SUITEB:
4204 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
4205 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
4206 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004207#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4208 /* Hybrid TLS 1.2/1.3 is not supported yet */
4209 conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
4210#else
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004211 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004212#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3 */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004213
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004214 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004215
4216#if defined(MBEDTLS_X509_CRT_PARSE_C)
4217 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004218#endif
4219
Gilles Peskineeccd8882020-03-10 12:19:08 +01004220#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004221#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004222 conf->sig_hashes = ssl_preset_suiteb_hashes;
Jerry Yuf017ee42022-01-12 15:49:48 +08004223#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004224#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4225 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4226 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4227 else
4228#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4229 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004230#endif
4231
Brett Warrene0edc842021-08-17 09:53:13 +01004232#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4233 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004234#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004235 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004236 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004237
4238 /*
4239 * Default
4240 */
4241 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03004242 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
4243 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
4244 MBEDTLS_SSL_MIN_MAJOR_VERSION :
4245 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
4246 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
4247 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
4248 MBEDTLS_SSL_MIN_MINOR_VERSION :
4249 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004250 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004251#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4252 /* Hybrid TLS 1.2/1.3 is not supported yet */
4253 conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
4254#else
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004255 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004256#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3 */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004257
4258#if defined(MBEDTLS_SSL_PROTO_DTLS)
4259 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
TRodziewiczef73f012021-05-13 14:53:36 +02004260 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004261#endif
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004262 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004263
4264#if defined(MBEDTLS_X509_CRT_PARSE_C)
4265 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4266#endif
4267
Gilles Peskineeccd8882020-03-10 12:19:08 +01004268#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004269#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01004270 conf->sig_hashes = ssl_preset_default_hashes;
Jerry Yuf017ee42022-01-12 15:49:48 +08004271#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004272#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4273 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4274 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4275 else
4276#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4277 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004278#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004279
Brett Warrene0edc842021-08-17 09:53:13 +01004280#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4281 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004282#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004283 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004284
4285#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4286 conf->dhm_min_bitlen = 1024;
4287#endif
4288 }
4289
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004290 return( 0 );
4291}
4292
4293/*
4294 * Free mbedtls_ssl_config
4295 */
4296void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4297{
4298#if defined(MBEDTLS_DHM_C)
4299 mbedtls_mpi_free( &conf->dhm_P );
4300 mbedtls_mpi_free( &conf->dhm_G );
4301#endif
4302
Gilles Peskineeccd8882020-03-10 12:19:08 +01004303#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004304 if( conf->psk != NULL )
4305 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004306 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004307 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004308 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004309 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004310 }
4311
4312 if( conf->psk_identity != NULL )
4313 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004314 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004315 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004316 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004317 conf->psk_identity_len = 0;
4318 }
4319#endif
4320
4321#if defined(MBEDTLS_X509_CRT_PARSE_C)
4322 ssl_key_cert_free( conf->key_cert );
4323#endif
4324
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004325 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004326}
4327
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004328#if defined(MBEDTLS_PK_C) && \
4329 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004330/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004331 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004333unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004334{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004335#if defined(MBEDTLS_RSA_C)
4336 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4337 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004338#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004339#if defined(MBEDTLS_ECDSA_C)
4340 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4341 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004342#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004343 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004344}
4345
Hanno Becker7e5437a2017-04-28 17:15:26 +01004346unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4347{
4348 switch( type ) {
4349 case MBEDTLS_PK_RSA:
4350 return( MBEDTLS_SSL_SIG_RSA );
4351 case MBEDTLS_PK_ECDSA:
4352 case MBEDTLS_PK_ECKEY:
4353 return( MBEDTLS_SSL_SIG_ECDSA );
4354 default:
4355 return( MBEDTLS_SSL_SIG_ANON );
4356 }
4357}
4358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004359mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004360{
4361 switch( sig )
4362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004363#if defined(MBEDTLS_RSA_C)
4364 case MBEDTLS_SSL_SIG_RSA:
4365 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004366#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004367#if defined(MBEDTLS_ECDSA_C)
4368 case MBEDTLS_SSL_SIG_ECDSA:
4369 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004370#endif
4371 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004372 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004373 }
4374}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004375#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004376
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004377/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004378 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004379 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004380mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004381{
4382 switch( hash )
4383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004384#if defined(MBEDTLS_MD5_C)
4385 case MBEDTLS_SSL_HASH_MD5:
4386 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004387#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004388#if defined(MBEDTLS_SHA1_C)
4389 case MBEDTLS_SSL_HASH_SHA1:
4390 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004391#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004392#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004393 case MBEDTLS_SSL_HASH_SHA224:
4394 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004395#endif
4396#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004397 case MBEDTLS_SSL_HASH_SHA256:
4398 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004399#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004400#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004401 case MBEDTLS_SSL_HASH_SHA384:
4402 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004403#endif
4404#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004405 case MBEDTLS_SSL_HASH_SHA512:
4406 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004407#endif
4408 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004409 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004410 }
4411}
4412
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004413/*
4414 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4415 */
4416unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4417{
4418 switch( md )
4419 {
4420#if defined(MBEDTLS_MD5_C)
4421 case MBEDTLS_MD_MD5:
4422 return( MBEDTLS_SSL_HASH_MD5 );
4423#endif
4424#if defined(MBEDTLS_SHA1_C)
4425 case MBEDTLS_MD_SHA1:
4426 return( MBEDTLS_SSL_HASH_SHA1 );
4427#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004428#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004429 case MBEDTLS_MD_SHA224:
4430 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004431#endif
4432#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004433 case MBEDTLS_MD_SHA256:
4434 return( MBEDTLS_SSL_HASH_SHA256 );
4435#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004436#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004437 case MBEDTLS_MD_SHA384:
4438 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004439#endif
4440#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004441 case MBEDTLS_MD_SHA512:
4442 return( MBEDTLS_SSL_HASH_SHA512 );
4443#endif
4444 default:
4445 return( MBEDTLS_SSL_HASH_NONE );
4446 }
4447}
4448
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004449/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004450 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004451 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004452 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004453int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004454{
Brett Warrene0edc842021-08-17 09:53:13 +01004455 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004456
Brett Warrene0edc842021-08-17 09:53:13 +01004457 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004458 return( -1 );
4459
Brett Warrene0edc842021-08-17 09:53:13 +01004460 for( ; *group_list != 0; group_list++ )
4461 {
4462 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004463 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01004464 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004465
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004466 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004467}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004468
4469#if defined(MBEDTLS_ECP_C)
4470/*
4471 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
4472 */
4473int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
4474{
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01004475 uint16_t tls_id = mbedtls_ecp_curve_info_from_grp_id( grp_id )->tls_id;
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004476 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
4477}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004478#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004480#if defined(MBEDTLS_X509_CRT_PARSE_C)
4481int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
4482 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004483 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02004484 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004485{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004486 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004487 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004488 const char *ext_oid;
4489 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004491 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004492 {
4493 /* Server part of the key exchange */
4494 switch( ciphersuite->key_exchange )
4495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004496 case MBEDTLS_KEY_EXCHANGE_RSA:
4497 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004498 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004499 break;
4500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004501 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
4502 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
4503 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
4504 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004505 break;
4506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004507 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
4508 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004509 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004510 break;
4511
4512 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004513 case MBEDTLS_KEY_EXCHANGE_NONE:
4514 case MBEDTLS_KEY_EXCHANGE_PSK:
4515 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
4516 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02004517 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004518 usage = 0;
4519 }
4520 }
4521 else
4522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004523 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
4524 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004525 }
4526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004527 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004528 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004529 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004530 ret = -1;
4531 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004533 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004535 ext_oid = MBEDTLS_OID_SERVER_AUTH;
4536 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004537 }
4538 else
4539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004540 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
4541 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004542 }
4543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004544 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004545 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004546 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004547 ret = -1;
4548 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004549
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004550 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004551}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004552#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004553
Jerry Yu148165c2021-09-24 23:20:59 +08004554#if defined(MBEDTLS_USE_PSA_CRYPTO)
4555int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
4556 const mbedtls_md_type_t md,
4557 unsigned char *dst,
4558 size_t dst_len,
4559 size_t *olen )
4560{
Ronald Cronf6893e12022-01-07 22:09:01 +01004561 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4562 psa_hash_operation_t *hash_operation_to_clone;
4563 psa_hash_operation_t hash_operation = psa_hash_operation_init();
4564
Jerry Yu148165c2021-09-24 23:20:59 +08004565 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01004566
4567 switch( md )
4568 {
4569#if defined(MBEDTLS_SHA384_C)
4570 case MBEDTLS_MD_SHA384:
4571 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
4572 break;
4573#endif
4574
4575#if defined(MBEDTLS_SHA256_C)
4576 case MBEDTLS_MD_SHA256:
4577 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
4578 break;
4579#endif
4580
4581 default:
4582 goto exit;
4583 }
4584
4585 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
4586 if( status != PSA_SUCCESS )
4587 goto exit;
4588
4589 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
4590 if( status != PSA_SUCCESS )
4591 goto exit;
4592
4593exit:
Ronald Cronb788c042022-02-15 09:14:15 +01004594 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08004595}
4596#else /* MBEDTLS_USE_PSA_CRYPTO */
4597
Jerry Yu24c0ec32021-09-09 14:21:07 +08004598#if defined(MBEDTLS_SHA384_C)
Jerry Yu000f9762021-09-14 11:12:51 +08004599static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
4600 unsigned char *dst,
4601 size_t dst_len,
4602 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004603{
Jerry Yu24c0ec32021-09-09 14:21:07 +08004604 int ret;
4605 mbedtls_sha512_context sha512;
4606
4607 if( dst_len < 48 )
4608 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4609
4610 mbedtls_sha512_init( &sha512 );
4611 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
4612
4613 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
4614 {
4615 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
4616 goto exit;
4617 }
4618
4619 *olen = 48;
4620
4621exit:
4622
4623 mbedtls_sha512_free( &sha512 );
4624 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004625}
4626#endif /* MBEDTLS_SHA384_C */
4627
4628#if defined(MBEDTLS_SHA256_C)
Jerry Yu000f9762021-09-14 11:12:51 +08004629static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
4630 unsigned char *dst,
4631 size_t dst_len,
4632 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004633{
Jerry Yu24c0ec32021-09-09 14:21:07 +08004634 int ret;
4635 mbedtls_sha256_context sha256;
4636
4637 if( dst_len < 32 )
4638 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4639
4640 mbedtls_sha256_init( &sha256 );
4641 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08004642
Jerry Yu24c0ec32021-09-09 14:21:07 +08004643 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
4644 {
4645 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
4646 goto exit;
4647 }
4648
4649 *olen = 32;
4650
4651exit:
4652
4653 mbedtls_sha256_free( &sha256 );
4654 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004655}
4656#endif /* MBEDTLS_SHA256_C */
4657
Jerry Yu000f9762021-09-14 11:12:51 +08004658int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
4659 const mbedtls_md_type_t md,
4660 unsigned char *dst,
4661 size_t dst_len,
4662 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004663{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004664 switch( md )
4665 {
4666
Jerry Yu24c0ec32021-09-09 14:21:07 +08004667#if defined(MBEDTLS_SHA384_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004668 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08004669 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08004670#endif /* MBEDTLS_SHA384_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004671
Jerry Yu24c0ec32021-09-09 14:21:07 +08004672#if defined(MBEDTLS_SHA256_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004673 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08004674 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08004675#endif /* MBEDTLS_SHA256_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004676
4677 default:
4678 break;
4679 }
Jerry Yuc5aef882021-12-23 20:15:02 +08004680 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004681}
XiaokangQian647719a2021-12-07 09:16:29 +00004682
Jerry Yu148165c2021-09-24 23:20:59 +08004683#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08004684
Jerry Yu1ea9d102021-12-21 13:41:49 +08004685#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) || \
4686 defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4687 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jerry Yuba073422021-12-20 22:22:15 +08004688/*
Jerry Yub925f212022-01-12 11:17:02 +08004689 * Function for writing a supported groups (TLS 1.3) or supported elliptic
4690 * curves (TLS 1.2) extension.
Jerry Yuba073422021-12-20 22:22:15 +08004691 *
Jerry Yub925f212022-01-12 11:17:02 +08004692 * The "extension_data" field of a supported groups extension contains a
4693 * "NamedGroupList" value (TLS 1.3 RFC8446):
Jerry Yuba073422021-12-20 22:22:15 +08004694 * enum {
4695 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
4696 * x25519(0x001D), x448(0x001E),
4697 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
4698 * ffdhe6144(0x0103), ffdhe8192(0x0104),
4699 * ffdhe_private_use(0x01FC..0x01FF),
4700 * ecdhe_private_use(0xFE00..0xFEFF),
4701 * (0xFFFF)
4702 * } NamedGroup;
4703 * struct {
4704 * NamedGroup named_group_list<2..2^16-1>;
4705 * } NamedGroupList;
Jerry Yub925f212022-01-12 11:17:02 +08004706 *
4707 * The "extension_data" field of a supported elliptic curves extension contains
4708 * a "NamedCurveList" value (TLS 1.2 RFC 8422):
Jerry Yu63282b42022-01-11 15:43:53 +08004709 * enum {
4710 * deprecated(1..22),
4711 * secp256r1 (23), secp384r1 (24), secp521r1 (25),
4712 * x25519(29), x448(30),
4713 * reserved (0xFE00..0xFEFF),
4714 * deprecated(0xFF01..0xFF02),
4715 * (0xFFFF)
4716 * } NamedCurve;
4717 * struct {
4718 * NamedCurve named_curve_list<2..2^16-1>
4719 * } NamedCurveList;
4720 *
Jerry Yub925f212022-01-12 11:17:02 +08004721 * The TLS 1.3 supported groups extension was defined to be a compatible
4722 * generalization of the TLS 1.2 supported elliptic curves extension. They both
4723 * share the same extension identifier.
Jerry Yu63282b42022-01-11 15:43:53 +08004724 *
Jerry Yub925f212022-01-12 11:17:02 +08004725 * DHE groups are not supported yet.
Jerry Yuba073422021-12-20 22:22:15 +08004726 */
Jerry Yuba073422021-12-20 22:22:15 +08004727int mbedtls_ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl,
4728 unsigned char *buf,
Jerry Yu17532612021-12-20 22:32:09 +08004729 const unsigned char *end,
Jerry Yuba073422021-12-20 22:22:15 +08004730 size_t *out_len )
4731{
4732 unsigned char *p = buf ;
4733 unsigned char *named_group_list; /* Start of named_group_list */
4734 size_t named_group_list_len; /* Length of named_group_list */
4735 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
4736
4737 *out_len = 0;
Jerry Yuf46b0162022-01-11 16:28:00 +08004738
Jerry Yuba073422021-12-20 22:22:15 +08004739 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) );
4740
4741 /* Check if we have space for header and length fields:
4742 * - extension_type (2 bytes)
4743 * - extension_data_length (2 bytes)
4744 * - named_group_list_length (2 bytes)
4745 */
4746 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
4747 p += 6;
4748
4749 named_group_list = p;
4750
4751 if( group_list == NULL )
4752 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
4753
Jerry Yu1510cea2022-01-12 10:56:49 +08004754 for( ; *group_list != 0; group_list++ )
Jerry Yuba073422021-12-20 22:22:15 +08004755 {
Jerry Yu1510cea2022-01-12 10:56:49 +08004756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got supported group(%04x)", *group_list ) );
Jerry Yu63282b42022-01-11 15:43:53 +08004757
Jerry Yu1ea9d102021-12-21 13:41:49 +08004758#if defined(MBEDTLS_ECP_C)
Jerry Yu1510cea2022-01-12 10:56:49 +08004759 if( ( mbedtls_ssl_conf_is_tls13_enabled( ssl->conf ) &&
Jerry Yu3ad14ac2022-01-11 17:13:16 +08004760 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) ) ||
Jerry Yu1510cea2022-01-12 10:56:49 +08004761 ( mbedtls_ssl_conf_is_tls12_enabled( ssl->conf ) &&
Jerry Yu3ad14ac2022-01-11 17:13:16 +08004762 mbedtls_ssl_tls12_named_group_is_ecdhe( *group_list ) ) )
Jerry Yuba073422021-12-20 22:22:15 +08004763 {
Jerry Yu3ad14ac2022-01-11 17:13:16 +08004764 const mbedtls_ecp_curve_info *curve_info;
4765 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
Jerry Yub925f212022-01-12 11:17:02 +08004766 if( curve_info == NULL )
Jerry Yu3ad14ac2022-01-11 17:13:16 +08004767 continue;
Jerry Yub925f212022-01-12 11:17:02 +08004768 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
4769 MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 );
4770 p += 2;
4771 MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )",
4772 curve_info->name, *group_list ) );
Jerry Yuba073422021-12-20 22:22:15 +08004773 }
Jerry Yu63282b42022-01-11 15:43:53 +08004774#endif /* MBEDTLS_ECP_C */
4775 /* Add DHE groups here */
Jerry Yuba073422021-12-20 22:22:15 +08004776
4777 }
4778
Jerry Yu1510cea2022-01-12 10:56:49 +08004779 /* Length of named_group_list */
Jerry Yuba073422021-12-20 22:22:15 +08004780 named_group_list_len = p - named_group_list;
4781 if( named_group_list_len == 0 )
4782 {
4783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) );
4784 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4785 }
4786
4787 /* Write extension_type */
4788 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 );
4789 /* Write extension_data_length */
4790 MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 );
4791 /* Write length of named_group_list */
4792 MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 );
4793
Jerry Yu7f029d82022-01-11 11:08:53 +08004794 MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension",
4795 buf + 4, named_group_list_len + 2 );
Jerry Yuba073422021-12-20 22:22:15 +08004796
4797 *out_len = p - buf;
4798
4799#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4800 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
4801#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4802
4803 return( 0 );
4804}
4805
Jerry Yu1ea9d102021-12-21 13:41:49 +08004806#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED ||
4807 MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
4808 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Jerry Yuba073422021-12-20 22:22:15 +08004809
Jerry Yuf017ee42022-01-12 15:49:48 +08004810#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
4811/*
Jerry Yu7ddc38c2022-01-19 11:08:05 +08004812 * Function for writing a signature algorithm extension.
Jerry Yuf017ee42022-01-12 15:49:48 +08004813 *
Jerry Yu8afd6e42022-01-20 15:54:26 +08004814 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
Jerry Yu7ddc38c2022-01-19 11:08:05 +08004815 * value (TLS 1.3 RFC8446):
4816 * enum {
4817 * ....
4818 * ecdsa_secp256r1_sha256( 0x0403 ),
4819 * ecdsa_secp384r1_sha384( 0x0503 ),
4820 * ecdsa_secp521r1_sha512( 0x0603 ),
4821 * ....
4822 * } SignatureScheme;
Jerry Yuf017ee42022-01-12 15:49:48 +08004823 *
Jerry Yu7ddc38c2022-01-19 11:08:05 +08004824 * struct {
4825 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
4826 * } SignatureSchemeList;
Jerry Yuf017ee42022-01-12 15:49:48 +08004827 *
Jerry Yu8afd6e42022-01-20 15:54:26 +08004828 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
4829 * value (TLS 1.2 RFC5246):
Jerry Yu7ddc38c2022-01-19 11:08:05 +08004830 * enum {
4831 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
4832 * sha512(6), (255)
4833 * } HashAlgorithm;
4834 *
4835 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
4836 * SignatureAlgorithm;
4837 *
4838 * struct {
4839 * HashAlgorithm hash;
4840 * SignatureAlgorithm signature;
4841 * } SignatureAndHashAlgorithm;
4842 *
4843 * SignatureAndHashAlgorithm
4844 * supported_signature_algorithms<2..2^16-2>;
4845 *
4846 * The TLS 1.3 signature algorithm extension was defined to be a compatible
4847 * generalization of the TLS 1.2 signature algorithm extension.
4848 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
4849 * `SignatureScheme` field of TLS 1.3
4850 *
Jerry Yuf017ee42022-01-12 15:49:48 +08004851 */
4852int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
4853 const unsigned char *end, size_t *out_len )
4854{
4855 unsigned char *p = buf;
4856 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
4857 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
4858
4859 *out_len = 0;
4860
4861 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
4862
4863 /* Check if we have space for header and length field:
4864 * - extension_type (2 bytes)
4865 * - extension_data_length (2 bytes)
4866 * - supported_signature_algorithms_length (2 bytes)
4867 */
4868 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
4869 p += 6;
4870
4871 /*
4872 * Write supported_signature_algorithms
4873 */
4874 supported_sig_alg = p;
Jerry Yu6106fdc2022-01-12 16:36:14 +08004875 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
4876 if( sig_alg == NULL )
4877 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
4878
4879 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
Jerry Yuf017ee42022-01-12 15:49:48 +08004880 {
Jerry Yu1bab3012022-01-19 17:43:22 +08004881 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
4882 continue;
Jerry Yuf017ee42022-01-12 15:49:48 +08004883 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
4884 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
4885 p += 2;
4886 MBEDTLS_SSL_DEBUG_MSG( 3, ( "signature scheme [%x]", *sig_alg ) );
4887 }
4888
4889 /* Length of supported_signature_algorithms */
4890 supported_sig_alg_len = p - supported_sig_alg;
4891 if( supported_sig_alg_len == 0 )
4892 {
4893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
4894 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4895 }
4896
4897 /* Write extension_type */
4898 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
4899 /* Write extension_data_length */
4900 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
4901 /* Write length of supported_signature_algorithms */
4902 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
4903
4904 /* Output the total length of signature algorithms extension. */
4905 *out_len = p - buf;
4906
4907#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4908 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
4909#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4910 return( 0 );
4911}
4912#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yuc73c6182022-02-08 20:29:25 +08004913
Jerry Yuc5aef882021-12-23 20:15:02 +08004914#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4915int mbedtls_ssl_write_hostname_ext( mbedtls_ssl_context *ssl,
4916 unsigned char *buf,
4917 const unsigned char *end,
4918 size_t *olen )
4919{
4920 unsigned char *p = buf;
4921 size_t hostname_len;
4922
4923 *olen = 0;
4924
4925 if( ssl->hostname == NULL )
4926 return( 0 );
4927
4928 MBEDTLS_SSL_DEBUG_MSG( 3,
4929 ( "client hello, adding server name extension: %s",
4930 ssl->hostname ) );
4931
4932 hostname_len = strlen( ssl->hostname );
4933
4934 MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 );
4935
4936 /*
4937 * Sect. 3, RFC 6066 (TLS Extensions Definitions)
4938 *
4939 * In order to provide any of the server names, clients MAY include an
4940 * extension of type "server_name" in the (extended) client hello. The
4941 * "extension_data" field of this extension SHALL contain
4942 * "ServerNameList" where:
4943 *
4944 * struct {
4945 * NameType name_type;
4946 * select (name_type) {
4947 * case host_name: HostName;
4948 * } name;
4949 * } ServerName;
4950 *
4951 * enum {
4952 * host_name(0), (255)
4953 * } NameType;
4954 *
4955 * opaque HostName<1..2^16-1>;
4956 *
4957 * struct {
4958 * ServerName server_name_list<1..2^16-1>
4959 * } ServerNameList;
4960 *
4961 */
4962 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SERVERNAME, p, 0 );
4963 p += 2;
4964
4965 MBEDTLS_PUT_UINT16_BE( hostname_len + 5, p, 0 );
4966 p += 2;
4967
4968 MBEDTLS_PUT_UINT16_BE( hostname_len + 3, p, 0 );
4969 p += 2;
4970
4971 *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME );
4972
4973 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
4974 p += 2;
4975
4976 memcpy( p, ssl->hostname, hostname_len );
4977
4978 *olen = hostname_len + 9;
4979
4980 return( 0 );
4981}
4982#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Jerry Yuf017ee42022-01-12 15:49:48 +08004983
Jerry Yudc7bd172022-02-17 13:44:15 +08004984#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4985
4986#if defined(MBEDTLS_USE_PSA_CRYPTO)
4987
4988static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
4989 mbedtls_svc_key_id_t key,
4990 psa_algorithm_t alg,
4991 const unsigned char* seed, size_t seed_length,
4992 const unsigned char* label, size_t label_length,
4993 size_t capacity )
4994{
4995 psa_status_t status;
4996
4997 status = psa_key_derivation_setup( derivation, alg );
4998 if( status != PSA_SUCCESS )
4999 return( status );
5000
5001 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5002 {
5003 status = psa_key_derivation_input_bytes( derivation,
5004 PSA_KEY_DERIVATION_INPUT_SEED,
5005 seed, seed_length );
5006 if( status != PSA_SUCCESS )
5007 return( status );
5008
5009 if( mbedtls_svc_key_id_is_null( key ) )
5010 {
5011 status = psa_key_derivation_input_bytes(
5012 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
5013 NULL, 0 );
5014 }
5015 else
5016 {
5017 status = psa_key_derivation_input_key(
5018 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5019 }
5020 if( status != PSA_SUCCESS )
5021 return( status );
5022
5023 status = psa_key_derivation_input_bytes( derivation,
5024 PSA_KEY_DERIVATION_INPUT_LABEL,
5025 label, label_length );
5026 if( status != PSA_SUCCESS )
5027 return( status );
5028 }
5029 else
5030 {
5031 return( PSA_ERROR_NOT_SUPPORTED );
5032 }
5033
5034 status = psa_key_derivation_set_capacity( derivation, capacity );
5035 if( status != PSA_SUCCESS )
5036 return( status );
5037
5038 return( PSA_SUCCESS );
5039}
5040
5041static int tls_prf_generic( mbedtls_md_type_t md_type,
5042 const unsigned char *secret, size_t slen,
5043 const char *label,
5044 const unsigned char *random, size_t rlen,
5045 unsigned char *dstbuf, size_t dlen )
5046{
5047 psa_status_t status;
5048 psa_algorithm_t alg;
5049 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5050 psa_key_derivation_operation_t derivation =
5051 PSA_KEY_DERIVATION_OPERATION_INIT;
5052
5053 if( md_type == MBEDTLS_MD_SHA384 )
5054 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5055 else
5056 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5057
5058 /* Normally a "secret" should be long enough to be impossible to
5059 * find by brute force, and in particular should not be empty. But
5060 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5061 * and for this use case it makes sense to have a 0-length "secret".
5062 * Since the key API doesn't allow importing a key of length 0,
5063 * keep master_key=0, which setup_psa_key_derivation() understands
5064 * to mean a 0-length "secret" input. */
5065 if( slen != 0 )
5066 {
5067 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5068 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5069 psa_set_key_algorithm( &key_attributes, alg );
5070 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5071
5072 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5073 if( status != PSA_SUCCESS )
5074 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5075 }
5076
5077 status = setup_psa_key_derivation( &derivation,
5078 master_key, alg,
5079 random, rlen,
5080 (unsigned char const *) label,
5081 (size_t) strlen( label ),
5082 dlen );
5083 if( status != PSA_SUCCESS )
5084 {
5085 psa_key_derivation_abort( &derivation );
5086 psa_destroy_key( master_key );
5087 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5088 }
5089
5090 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5091 if( status != PSA_SUCCESS )
5092 {
5093 psa_key_derivation_abort( &derivation );
5094 psa_destroy_key( master_key );
5095 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5096 }
5097
5098 status = psa_key_derivation_abort( &derivation );
5099 if( status != PSA_SUCCESS )
5100 {
5101 psa_destroy_key( master_key );
5102 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5103 }
5104
5105 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5106 status = psa_destroy_key( master_key );
5107 if( status != PSA_SUCCESS )
5108 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5109
5110 return( 0 );
5111}
5112
5113#else /* MBEDTLS_USE_PSA_CRYPTO */
5114
5115static int tls_prf_generic( mbedtls_md_type_t md_type,
5116 const unsigned char *secret, size_t slen,
5117 const char *label,
5118 const unsigned char *random, size_t rlen,
5119 unsigned char *dstbuf, size_t dlen )
5120{
5121 size_t nb;
5122 size_t i, j, k, md_len;
5123 unsigned char *tmp;
5124 size_t tmp_len = 0;
5125 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5126 const mbedtls_md_info_t *md_info;
5127 mbedtls_md_context_t md_ctx;
5128 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5129
5130 mbedtls_md_init( &md_ctx );
5131
5132 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5133 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5134
5135 md_len = mbedtls_md_get_size( md_info );
5136
5137 tmp_len = md_len + strlen( label ) + rlen;
5138 tmp = mbedtls_calloc( 1, tmp_len );
5139 if( tmp == NULL )
5140 {
5141 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5142 goto exit;
5143 }
5144
5145 nb = strlen( label );
5146 memcpy( tmp + md_len, label, nb );
5147 memcpy( tmp + md_len + nb, random, rlen );
5148 nb += rlen;
5149
5150 /*
5151 * Compute P_<hash>(secret, label + random)[0..dlen]
5152 */
5153 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5154 goto exit;
5155
5156 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5157 if( ret != 0 )
5158 goto exit;
5159 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5160 if( ret != 0 )
5161 goto exit;
5162 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5163 if( ret != 0 )
5164 goto exit;
5165
5166 for( i = 0; i < dlen; i += md_len )
5167 {
5168 ret = mbedtls_md_hmac_reset ( &md_ctx );
5169 if( ret != 0 )
5170 goto exit;
5171 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5172 if( ret != 0 )
5173 goto exit;
5174 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5175 if( ret != 0 )
5176 goto exit;
5177
5178 ret = mbedtls_md_hmac_reset ( &md_ctx );
5179 if( ret != 0 )
5180 goto exit;
5181 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5182 if( ret != 0 )
5183 goto exit;
5184 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5185 if( ret != 0 )
5186 goto exit;
5187
5188 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5189
5190 for( j = 0; j < k; j++ )
5191 dstbuf[i + j] = h_i[j];
5192 }
5193
5194exit:
5195 mbedtls_md_free( &md_ctx );
5196
5197 mbedtls_platform_zeroize( tmp, tmp_len );
5198 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5199
5200 mbedtls_free( tmp );
5201
5202 return( ret );
5203}
5204#endif /* MBEDTLS_USE_PSA_CRYPTO */
5205
5206#if defined(MBEDTLS_SHA256_C)
5207static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5208 const char *label,
5209 const unsigned char *random, size_t rlen,
5210 unsigned char *dstbuf, size_t dlen )
5211{
5212 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5213 label, random, rlen, dstbuf, dlen ) );
5214}
5215#endif /* MBEDTLS_SHA256_C */
5216
5217#if defined(MBEDTLS_SHA384_C)
5218static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5219 const char *label,
5220 const unsigned char *random, size_t rlen,
5221 unsigned char *dstbuf, size_t dlen )
5222{
5223 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5224 label, random, rlen, dstbuf, dlen ) );
5225}
5226#endif /* MBEDTLS_SHA384_C */
5227
Jerry Yuf009d862022-02-17 14:01:37 +08005228/*
5229 * Set appropriate PRF function and other SSL / TLS1.2 functions
5230 *
5231 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005232 * - hash associated with the ciphersuite (only used by TLS 1.2)
5233 *
5234 * Outputs:
5235 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5236 */
5237static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005238 mbedtls_md_type_t hash )
5239{
Jerry Yuf009d862022-02-17 14:01:37 +08005240#if defined(MBEDTLS_SHA384_C)
Ronald Cron81591aa2022-03-07 09:05:51 +01005241 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005242 {
5243 handshake->tls_prf = tls_prf_sha384;
5244 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5245 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5246 }
5247 else
5248#endif
5249#if defined(MBEDTLS_SHA256_C)
Jerry Yuf009d862022-02-17 14:01:37 +08005250 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005251 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005252 handshake->tls_prf = tls_prf_sha256;
5253 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5254 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5255 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005256#else
Jerry Yuf009d862022-02-17 14:01:37 +08005257 {
Jerry Yuf009d862022-02-17 14:01:37 +08005258 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005259 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005260 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5261 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005262#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005263
5264 return( 0 );
5265}
Jerry Yud6ab2352022-02-17 14:03:43 +08005266
5267#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
5268 defined(MBEDTLS_USE_PSA_CRYPTO)
5269static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
5270{
5271 if( ssl->conf->f_psk != NULL )
5272 {
5273 /* If we've used a callback to select the PSK,
5274 * the static configuration is irrelevant. */
5275 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
5276 return( 1 );
5277
5278 return( 0 );
5279 }
5280
5281 if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) )
5282 return( 1 );
5283
5284 return( 0 );
5285}
5286#endif /* MBEDTLS_USE_PSA_CRYPTO &&
5287 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5288
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005289/*
5290 * Compute master secret if needed
5291 *
5292 * Parameters:
5293 * [in/out] handshake
5294 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5295 * (PSA-PSK) ciphersuite_info, psk_opaque
5296 * [out] premaster (cleared)
5297 * [out] master
5298 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5299 * debug: conf->f_dbg, conf->p_dbg
5300 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005301 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005302 */
5303static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5304 unsigned char *master,
5305 const mbedtls_ssl_context *ssl )
5306{
5307 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5308
5309 /* cf. RFC 5246, Section 8.1:
5310 * "The master secret is always exactly 48 bytes in length." */
5311 size_t const master_secret_len = 48;
5312
5313#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5314 unsigned char session_hash[48];
5315#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5316
5317 /* The label for the KDF used for key expansion.
5318 * This is either "master secret" or "extended master secret"
5319 * depending on whether the Extended Master Secret extension
5320 * is used. */
5321 char const *lbl = "master secret";
5322
5323 /* The salt for the KDF used for key expansion.
5324 * - If the Extended Master Secret extension is not used,
5325 * this is ClientHello.Random + ServerHello.Random
5326 * (see Sect. 8.1 in RFC 5246).
5327 * - If the Extended Master Secret extension is used,
5328 * this is the transcript of the handshake so far.
5329 * (see Sect. 4 in RFC 7627). */
5330 unsigned char const *salt = handshake->randbytes;
5331 size_t salt_len = 64;
5332
5333#if !defined(MBEDTLS_DEBUG_C) && \
5334 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5335 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5336 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5337 ssl = NULL; /* make sure we don't use it except for those cases */
5338 (void) ssl;
5339#endif
5340
5341 if( handshake->resume != 0 )
5342 {
5343 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5344 return( 0 );
5345 }
5346
5347#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5348 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5349 {
5350 lbl = "extended master secret";
5351 salt = session_hash;
5352 handshake->calc_verify( ssl, session_hash, &salt_len );
5353
5354 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
5355 session_hash, salt_len );
5356 }
5357#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
5358
5359#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
5360 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5361 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005362 ssl_use_opaque_psk( ssl ) == 1 )
5363 {
5364 /* Perform PSK-to-MS expansion in a single step. */
5365 psa_status_t status;
5366 psa_algorithm_t alg;
5367 mbedtls_svc_key_id_t psk;
5368 psa_key_derivation_operation_t derivation =
5369 PSA_KEY_DERIVATION_OPERATION_INIT;
5370 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5371
5372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5373
5374 psk = mbedtls_ssl_get_opaque_psk( ssl );
5375
5376 if( hash_alg == MBEDTLS_MD_SHA384 )
5377 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5378 else
5379 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5380
5381 status = setup_psa_key_derivation( &derivation, psk, alg,
5382 salt, salt_len,
5383 (unsigned char const *) lbl,
5384 (size_t) strlen( lbl ),
5385 master_secret_len );
5386 if( status != PSA_SUCCESS )
5387 {
5388 psa_key_derivation_abort( &derivation );
5389 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5390 }
5391
5392 status = psa_key_derivation_output_bytes( &derivation,
5393 master,
5394 master_secret_len );
5395 if( status != PSA_SUCCESS )
5396 {
5397 psa_key_derivation_abort( &derivation );
5398 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5399 }
5400
5401 status = psa_key_derivation_abort( &derivation );
5402 if( status != PSA_SUCCESS )
5403 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5404 }
5405 else
5406#endif
5407 {
5408 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
5409 lbl, salt, salt_len,
5410 master,
5411 master_secret_len );
5412 if( ret != 0 )
5413 {
5414 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5415 return( ret );
5416 }
5417
5418 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5419 handshake->premaster,
5420 handshake->pmslen );
5421
5422 mbedtls_platform_zeroize( handshake->premaster,
5423 sizeof(handshake->premaster) );
5424 }
5425
5426 return( 0 );
5427}
5428
Jerry Yud62f87e2022-02-17 14:09:02 +08005429int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5430{
5431 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5432 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5433 ssl->handshake->ciphersuite_info;
5434
5435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5436
5437 /* Set PRF, calc_verify and calc_finished function pointers */
5438 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005439 ciphersuite_info->mac );
5440 if( ret != 0 )
5441 {
5442 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5443 return( ret );
5444 }
5445
5446 /* Compute master secret if needed */
5447 ret = ssl_compute_master( ssl->handshake,
5448 ssl->session_negotiate->master,
5449 ssl );
5450 if( ret != 0 )
5451 {
5452 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5453 return( ret );
5454 }
5455
5456 /* Swap the client and server random values:
5457 * - MS derivation wanted client+server (RFC 5246 8.1)
5458 * - key derivation wants server+client (RFC 5246 6.3) */
5459 {
5460 unsigned char tmp[64];
5461 memcpy( tmp, ssl->handshake->randbytes, 64 );
5462 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5463 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5464 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5465 }
5466
5467 /* Populate transform structure */
5468 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5469 ssl->session_negotiate->ciphersuite,
5470 ssl->session_negotiate->master,
5471#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
5472 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5473 ssl->session_negotiate->encrypt_then_mac,
5474#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
5475 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
5476 ssl->handshake->tls_prf,
5477 ssl->handshake->randbytes,
5478 ssl->minor_ver,
5479 ssl->conf->endpoint,
5480 ssl );
5481 if( ret != 0 )
5482 {
5483 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5484 return( ret );
5485 }
5486
5487 /* We no longer need Server/ClientHello.random values */
5488 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5489 sizeof( ssl->handshake->randbytes ) );
5490
5491 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5492
5493 return( 0 );
5494}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005495
Ronald Cron4dcbca92022-03-07 10:21:40 +01005496int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5497{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005498 switch( md )
5499 {
5500#if defined(MBEDTLS_SHA384_C)
5501 case MBEDTLS_SSL_HASH_SHA384:
5502 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5503 break;
5504#endif
5505#if defined(MBEDTLS_SHA256_C)
5506 case MBEDTLS_SSL_HASH_SHA256:
5507 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5508 break;
5509#endif
5510 default:
5511 return( -1 );
5512 }
5513
Ronald Cronc2f13a02022-03-07 10:25:24 +01005514 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005515}
5516
Jerry Yu8392e0d2022-02-17 14:10:24 +08005517#if defined(MBEDTLS_SHA256_C)
5518void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5519 unsigned char *hash,
5520 size_t *hlen )
5521{
5522#if defined(MBEDTLS_USE_PSA_CRYPTO)
5523 size_t hash_size;
5524 psa_status_t status;
5525 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5526
5527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5528 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5529 if( status != PSA_SUCCESS )
5530 {
5531 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5532 return;
5533 }
5534
5535 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5536 if( status != PSA_SUCCESS )
5537 {
5538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5539 return;
5540 }
5541
5542 *hlen = 32;
5543 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5544 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5545#else
5546 mbedtls_sha256_context sha256;
5547
5548 mbedtls_sha256_init( &sha256 );
5549
5550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5551
5552 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5553 mbedtls_sha256_finish( &sha256, hash );
5554
5555 *hlen = 32;
5556
5557 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5559
5560 mbedtls_sha256_free( &sha256 );
5561#endif /* MBEDTLS_USE_PSA_CRYPTO */
5562 return;
5563}
5564#endif /* MBEDTLS_SHA256_C */
5565
Jerry Yuc1cb3842022-02-17 14:13:48 +08005566#if defined(MBEDTLS_SHA384_C)
5567void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5568 unsigned char *hash,
5569 size_t *hlen )
5570{
5571#if defined(MBEDTLS_USE_PSA_CRYPTO)
5572 size_t hash_size;
5573 psa_status_t status;
5574 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5575
5576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5577 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5578 if( status != PSA_SUCCESS )
5579 {
5580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5581 return;
5582 }
5583
5584 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5585 if( status != PSA_SUCCESS )
5586 {
5587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5588 return;
5589 }
5590
5591 *hlen = 48;
5592 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5593 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5594#else
5595 mbedtls_sha512_context sha512;
5596
5597 mbedtls_sha512_init( &sha512 );
5598
5599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
5600
5601 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
5602 mbedtls_sha512_finish( &sha512, hash );
5603
5604 *hlen = 48;
5605
5606 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5607 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5608
5609 mbedtls_sha512_free( &sha512 );
5610#endif /* MBEDTLS_USE_PSA_CRYPTO */
5611 return;
5612}
5613#endif /* MBEDTLS_SHA384_C */
5614
Jerry Yuce3dca42022-02-17 14:16:37 +08005615#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
5616int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
5617{
5618 unsigned char *p = ssl->handshake->premaster;
5619 unsigned char *end = p + sizeof( ssl->handshake->premaster );
5620 const unsigned char *psk = NULL;
5621 size_t psk_len = 0;
5622
5623 if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len )
5624 == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
5625 {
5626 /*
5627 * This should never happen because the existence of a PSK is always
5628 * checked before calling this function
5629 */
5630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5631 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5632 }
5633
5634 /*
5635 * PMS = struct {
5636 * opaque other_secret<0..2^16-1>;
5637 * opaque psk<0..2^16-1>;
5638 * };
5639 * with "other_secret" depending on the particular key exchange
5640 */
5641#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5642 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
5643 {
5644 if( end - p < 2 )
5645 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5646
5647 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5648 p += 2;
5649
5650 if( end < p || (size_t)( end - p ) < psk_len )
5651 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5652
5653 memset( p, 0, psk_len );
5654 p += psk_len;
5655 }
5656 else
5657#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5658#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
5659 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5660 {
5661 /*
5662 * other_secret already set by the ClientKeyExchange message,
5663 * and is 48 bytes long
5664 */
5665 if( end - p < 2 )
5666 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5667
5668 *p++ = 0;
5669 *p++ = 48;
5670 p += 48;
5671 }
5672 else
5673#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
5674#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
5675 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5676 {
5677 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5678 size_t len;
5679
5680 /* Write length only when we know the actual value */
5681 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
5682 p + 2, end - ( p + 2 ), &len,
5683 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5684 {
5685 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
5686 return( ret );
5687 }
5688 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
5689 p += 2 + len;
5690
5691 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
5692 }
5693 else
5694#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
5695#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
5696 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
5697 {
5698 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5699 size_t zlen;
5700
5701 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
5702 p + 2, end - ( p + 2 ),
5703 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5704 {
5705 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
5706 return( ret );
5707 }
5708
5709 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
5710 p += 2 + zlen;
5711
5712 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
5713 MBEDTLS_DEBUG_ECDH_Z );
5714 }
5715 else
5716#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
5717 {
5718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5719 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5720 }
5721
5722 /* opaque psk<0..2^16-1>; */
5723 if( end - p < 2 )
5724 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5725
5726 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5727 p += 2;
5728
5729 if( end < p || (size_t)( end - p ) < psk_len )
5730 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5731
5732 memcpy( p, psk, psk_len );
5733 p += psk_len;
5734
5735 ssl->handshake->pmslen = p - ssl->handshake->premaster;
5736
5737 return( 0 );
5738}
5739#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08005740
5741#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
5742static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
5743
5744#if defined(MBEDTLS_SSL_PROTO_DTLS)
5745int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
5746{
5747 /* If renegotiation is not enforced, retransmit until we would reach max
5748 * timeout if we were using the usual handshake doubling scheme */
5749 if( ssl->conf->renego_max_records < 0 )
5750 {
5751 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
5752 unsigned char doublings = 1;
5753
5754 while( ratio != 0 )
5755 {
5756 ++doublings;
5757 ratio >>= 1;
5758 }
5759
5760 if( ++ssl->renego_records_seen > doublings )
5761 {
5762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
5763 return( 0 );
5764 }
5765 }
5766
5767 return( ssl_write_hello_request( ssl ) );
5768}
5769#endif
5770#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08005771
Jerry Yud9526692022-02-17 14:23:47 +08005772/*
5773 * Handshake functions
5774 */
5775#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5776/* No certificate support -> dummy functions */
5777int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
5778{
5779 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5780 ssl->handshake->ciphersuite_info;
5781
5782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
5783
5784 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5785 {
5786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5787 ssl->state++;
5788 return( 0 );
5789 }
5790
5791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5792 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5793}
5794
5795int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5796{
5797 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5798 ssl->handshake->ciphersuite_info;
5799
5800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5801
5802 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5803 {
5804 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5805 ssl->state++;
5806 return( 0 );
5807 }
5808
5809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5810 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5811}
5812
5813#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5814/* Some certificate support -> implement write and parse */
5815
5816int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
5817{
5818 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
5819 size_t i, n;
5820 const mbedtls_x509_crt *crt;
5821 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5822 ssl->handshake->ciphersuite_info;
5823
5824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
5825
5826 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5827 {
5828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5829 ssl->state++;
5830 return( 0 );
5831 }
5832
5833#if defined(MBEDTLS_SSL_CLI_C)
5834 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5835 {
5836 if( ssl->handshake->client_auth == 0 )
5837 {
5838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5839 ssl->state++;
5840 return( 0 );
5841 }
5842 }
5843#endif /* MBEDTLS_SSL_CLI_C */
5844#if defined(MBEDTLS_SSL_SRV_C)
5845 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
5846 {
5847 if( mbedtls_ssl_own_cert( ssl ) == NULL )
5848 {
5849 /* Should never happen because we shouldn't have picked the
5850 * ciphersuite if we don't have a certificate. */
5851 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5852 }
5853 }
5854#endif
5855
5856 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
5857
5858 /*
5859 * 0 . 0 handshake type
5860 * 1 . 3 handshake length
5861 * 4 . 6 length of all certs
5862 * 7 . 9 length of cert. 1
5863 * 10 . n-1 peer certificate
5864 * n . n+2 length of cert. 2
5865 * n+3 . ... upper level cert, etc.
5866 */
5867 i = 7;
5868 crt = mbedtls_ssl_own_cert( ssl );
5869
5870 while( crt != NULL )
5871 {
5872 n = crt->raw.len;
5873 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
5874 {
5875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
5876 " > %" MBEDTLS_PRINTF_SIZET,
5877 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
5878 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5879 }
5880
5881 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
5882 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
5883 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
5884
5885 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5886 i += n; crt = crt->next;
5887 }
5888
5889 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
5890 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
5891 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
5892
5893 ssl->out_msglen = i;
5894 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5895 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
5896
5897 ssl->state++;
5898
5899 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
5900 {
5901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
5902 return( ret );
5903 }
5904
5905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
5906
5907 return( ret );
5908}
5909
5910#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5911
5912#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5913static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5914 unsigned char *crt_buf,
5915 size_t crt_buf_len )
5916{
5917 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5918
5919 if( peer_crt == NULL )
5920 return( -1 );
5921
5922 if( peer_crt->raw.len != crt_buf_len )
5923 return( -1 );
5924
5925 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
5926}
5927#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5928static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5929 unsigned char *crt_buf,
5930 size_t crt_buf_len )
5931{
5932 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5933 unsigned char const * const peer_cert_digest =
5934 ssl->session->peer_cert_digest;
5935 mbedtls_md_type_t const peer_cert_digest_type =
5936 ssl->session->peer_cert_digest_type;
5937 mbedtls_md_info_t const * const digest_info =
5938 mbedtls_md_info_from_type( peer_cert_digest_type );
5939 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
5940 size_t digest_len;
5941
5942 if( peer_cert_digest == NULL || digest_info == NULL )
5943 return( -1 );
5944
5945 digest_len = mbedtls_md_get_size( digest_info );
5946 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
5947 return( -1 );
5948
5949 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
5950 if( ret != 0 )
5951 return( -1 );
5952
5953 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
5954}
5955#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5956#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5957
5958/*
5959 * Once the certificate message is read, parse it into a cert chain and
5960 * perform basic checks, but leave actual verification to the caller
5961 */
5962static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5963 mbedtls_x509_crt *chain )
5964{
5965 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5966#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5967 int crt_cnt=0;
5968#endif
5969 size_t i, n;
5970 uint8_t alert;
5971
5972 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5973 {
5974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5975 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5976 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5977 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5978 }
5979
5980 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
5981 {
5982 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5983 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5984 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5985 }
5986
5987 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
5988 {
5989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5990 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5991 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5992 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5993 }
5994
5995 i = mbedtls_ssl_hs_hdr_len( ssl );
5996
5997 /*
5998 * Same message structure as in mbedtls_ssl_write_certificate()
5999 */
6000 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6001
6002 if( ssl->in_msg[i] != 0 ||
6003 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6004 {
6005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6006 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6007 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6008 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6009 }
6010
6011 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6012 i += 3;
6013
6014 /* Iterate through and parse the CRTs in the provided chain. */
6015 while( i < ssl->in_hslen )
6016 {
6017 /* Check that there's room for the next CRT's length fields. */
6018 if ( i + 3 > ssl->in_hslen ) {
6019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6020 mbedtls_ssl_send_alert_message( ssl,
6021 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6022 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6023 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6024 }
6025 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6026 * anything beyond 2**16 ~ 64K. */
6027 if( ssl->in_msg[i] != 0 )
6028 {
6029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6030 mbedtls_ssl_send_alert_message( ssl,
6031 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6032 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6033 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6034 }
6035
6036 /* Read length of the next CRT in the chain. */
6037 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6038 | (unsigned int) ssl->in_msg[i + 2];
6039 i += 3;
6040
6041 if( n < 128 || i + n > ssl->in_hslen )
6042 {
6043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6044 mbedtls_ssl_send_alert_message( ssl,
6045 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6046 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6047 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6048 }
6049
6050 /* Check if we're handling the first CRT in the chain. */
6051#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6052 if( crt_cnt++ == 0 &&
6053 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6054 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6055 {
6056 /* During client-side renegotiation, check that the server's
6057 * end-CRTs hasn't changed compared to the initial handshake,
6058 * mitigating the triple handshake attack. On success, reuse
6059 * the original end-CRT instead of parsing it again. */
6060 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6061 if( ssl_check_peer_crt_unchanged( ssl,
6062 &ssl->in_msg[i],
6063 n ) != 0 )
6064 {
6065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6066 mbedtls_ssl_send_alert_message( ssl,
6067 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6068 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6069 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6070 }
6071
6072 /* Now we can safely free the original chain. */
6073 ssl_clear_peer_cert( ssl->session );
6074 }
6075#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6076
6077 /* Parse the next certificate in the chain. */
6078#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6079 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6080#else
6081 /* If we don't need to store the CRT chain permanently, parse
6082 * it in-place from the input buffer instead of making a copy. */
6083 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6084#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6085 switch( ret )
6086 {
6087 case 0: /*ok*/
6088 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6089 /* Ignore certificate with an unknown algorithm: maybe a
6090 prior certificate was already trusted. */
6091 break;
6092
6093 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6094 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6095 goto crt_parse_der_failed;
6096
6097 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6098 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6099 goto crt_parse_der_failed;
6100
6101 default:
6102 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6103 crt_parse_der_failed:
6104 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6105 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6106 return( ret );
6107 }
6108
6109 i += n;
6110 }
6111
6112 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6113 return( 0 );
6114}
6115
6116#if defined(MBEDTLS_SSL_SRV_C)
6117static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6118{
6119 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6120 return( -1 );
6121
6122 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6123 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6124 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6125 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6126 {
6127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6128 return( 0 );
6129 }
6130 return( -1 );
6131}
6132#endif /* MBEDTLS_SSL_SRV_C */
6133
6134/* Check if a certificate message is expected.
6135 * Return either
6136 * - SSL_CERTIFICATE_EXPECTED, or
6137 * - SSL_CERTIFICATE_SKIP
6138 * indicating whether a Certificate message is expected or not.
6139 */
6140#define SSL_CERTIFICATE_EXPECTED 0
6141#define SSL_CERTIFICATE_SKIP 1
6142static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6143 int authmode )
6144{
6145 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6146 ssl->handshake->ciphersuite_info;
6147
6148 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6149 return( SSL_CERTIFICATE_SKIP );
6150
6151#if defined(MBEDTLS_SSL_SRV_C)
6152 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6153 {
6154 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6155 return( SSL_CERTIFICATE_SKIP );
6156
6157 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6158 {
6159 ssl->session_negotiate->verify_result =
6160 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6161 return( SSL_CERTIFICATE_SKIP );
6162 }
6163 }
6164#else
6165 ((void) authmode);
6166#endif /* MBEDTLS_SSL_SRV_C */
6167
6168 return( SSL_CERTIFICATE_EXPECTED );
6169}
6170
6171static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6172 int authmode,
6173 mbedtls_x509_crt *chain,
6174 void *rs_ctx )
6175{
6176 int ret = 0;
6177 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6178 ssl->handshake->ciphersuite_info;
6179 int have_ca_chain = 0;
6180
6181 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6182 void *p_vrfy;
6183
6184 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6185 return( 0 );
6186
6187 if( ssl->f_vrfy != NULL )
6188 {
6189 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6190 f_vrfy = ssl->f_vrfy;
6191 p_vrfy = ssl->p_vrfy;
6192 }
6193 else
6194 {
6195 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6196 f_vrfy = ssl->conf->f_vrfy;
6197 p_vrfy = ssl->conf->p_vrfy;
6198 }
6199
6200 /*
6201 * Main check: verify certificate
6202 */
6203#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6204 if( ssl->conf->f_ca_cb != NULL )
6205 {
6206 ((void) rs_ctx);
6207 have_ca_chain = 1;
6208
6209 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6210 ret = mbedtls_x509_crt_verify_with_ca_cb(
6211 chain,
6212 ssl->conf->f_ca_cb,
6213 ssl->conf->p_ca_cb,
6214 ssl->conf->cert_profile,
6215 ssl->hostname,
6216 &ssl->session_negotiate->verify_result,
6217 f_vrfy, p_vrfy );
6218 }
6219 else
6220#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6221 {
6222 mbedtls_x509_crt *ca_chain;
6223 mbedtls_x509_crl *ca_crl;
6224
6225#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6226 if( ssl->handshake->sni_ca_chain != NULL )
6227 {
6228 ca_chain = ssl->handshake->sni_ca_chain;
6229 ca_crl = ssl->handshake->sni_ca_crl;
6230 }
6231 else
6232#endif
6233 {
6234 ca_chain = ssl->conf->ca_chain;
6235 ca_crl = ssl->conf->ca_crl;
6236 }
6237
6238 if( ca_chain != NULL )
6239 have_ca_chain = 1;
6240
6241 ret = mbedtls_x509_crt_verify_restartable(
6242 chain,
6243 ca_chain, ca_crl,
6244 ssl->conf->cert_profile,
6245 ssl->hostname,
6246 &ssl->session_negotiate->verify_result,
6247 f_vrfy, p_vrfy, rs_ctx );
6248 }
6249
6250 if( ret != 0 )
6251 {
6252 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6253 }
6254
6255#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6256 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6257 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6258#endif
6259
6260 /*
6261 * Secondary checks: always done, but change 'ret' only if it was 0
6262 */
6263
6264#if defined(MBEDTLS_ECP_C)
6265 {
6266 const mbedtls_pk_context *pk = &chain->pk;
6267
6268 /* If certificate uses an EC key, make sure the curve is OK */
6269 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6270 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6271 {
6272 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6273
6274 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6275 if( ret == 0 )
6276 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6277 }
6278 }
6279#endif /* MBEDTLS_ECP_C */
6280
6281 if( mbedtls_ssl_check_cert_usage( chain,
6282 ciphersuite_info,
6283 ! ssl->conf->endpoint,
6284 &ssl->session_negotiate->verify_result ) != 0 )
6285 {
6286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6287 if( ret == 0 )
6288 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6289 }
6290
6291 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6292 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6293 * with details encoded in the verification flags. All other kinds
6294 * of error codes, including those from the user provided f_vrfy
6295 * functions, are treated as fatal and lead to a failure of
6296 * ssl_parse_certificate even if verification was optional. */
6297 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6298 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6299 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6300 {
6301 ret = 0;
6302 }
6303
6304 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6305 {
6306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6307 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6308 }
6309
6310 if( ret != 0 )
6311 {
6312 uint8_t alert;
6313
6314 /* The certificate may have been rejected for several reasons.
6315 Pick one and send the corresponding alert. Which alert to send
6316 may be a subject of debate in some cases. */
6317 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6318 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6319 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6320 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6321 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6322 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6323 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6324 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6325 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6326 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6327 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6328 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6329 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6330 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6331 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6332 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6333 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6334 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6335 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6336 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6337 else
6338 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6339 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6340 alert );
6341 }
6342
6343#if defined(MBEDTLS_DEBUG_C)
6344 if( ssl->session_negotiate->verify_result != 0 )
6345 {
6346 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6347 (unsigned int) ssl->session_negotiate->verify_result ) );
6348 }
6349 else
6350 {
6351 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6352 }
6353#endif /* MBEDTLS_DEBUG_C */
6354
6355 return( ret );
6356}
6357
6358#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6359static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6360 unsigned char *start, size_t len )
6361{
6362 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6363 /* Remember digest of the peer's end-CRT. */
6364 ssl->session_negotiate->peer_cert_digest =
6365 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6366 if( ssl->session_negotiate->peer_cert_digest == NULL )
6367 {
6368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6369 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6370 mbedtls_ssl_send_alert_message( ssl,
6371 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6372 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6373
6374 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6375 }
6376
6377 ret = mbedtls_md( mbedtls_md_info_from_type(
6378 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6379 start, len,
6380 ssl->session_negotiate->peer_cert_digest );
6381
6382 ssl->session_negotiate->peer_cert_digest_type =
6383 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6384 ssl->session_negotiate->peer_cert_digest_len =
6385 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6386
6387 return( ret );
6388}
6389
6390static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6391 unsigned char *start, size_t len )
6392{
6393 unsigned char *end = start + len;
6394 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6395
6396 /* Make a copy of the peer's raw public key. */
6397 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6398 ret = mbedtls_pk_parse_subpubkey( &start, end,
6399 &ssl->handshake->peer_pubkey );
6400 if( ret != 0 )
6401 {
6402 /* We should have parsed the public key before. */
6403 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6404 }
6405
6406 return( 0 );
6407}
6408#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6409
6410int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6411{
6412 int ret = 0;
6413 int crt_expected;
6414#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6415 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6416 ? ssl->handshake->sni_authmode
6417 : ssl->conf->authmode;
6418#else
6419 const int authmode = ssl->conf->authmode;
6420#endif
6421 void *rs_ctx = NULL;
6422 mbedtls_x509_crt *chain = NULL;
6423
6424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6425
6426 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6427 if( crt_expected == SSL_CERTIFICATE_SKIP )
6428 {
6429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6430 goto exit;
6431 }
6432
6433#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6434 if( ssl->handshake->ecrs_enabled &&
6435 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6436 {
6437 chain = ssl->handshake->ecrs_peer_cert;
6438 ssl->handshake->ecrs_peer_cert = NULL;
6439 goto crt_verify;
6440 }
6441#endif
6442
6443 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6444 {
6445 /* mbedtls_ssl_read_record may have sent an alert already. We
6446 let it decide whether to alert. */
6447 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6448 goto exit;
6449 }
6450
6451#if defined(MBEDTLS_SSL_SRV_C)
6452 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6453 {
6454 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6455
6456 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6457 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6458
6459 goto exit;
6460 }
6461#endif /* MBEDTLS_SSL_SRV_C */
6462
6463 /* Clear existing peer CRT structure in case we tried to
6464 * reuse a session but it failed, and allocate a new one. */
6465 ssl_clear_peer_cert( ssl->session_negotiate );
6466
6467 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6468 if( chain == NULL )
6469 {
6470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6471 sizeof( mbedtls_x509_crt ) ) );
6472 mbedtls_ssl_send_alert_message( ssl,
6473 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6474 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6475
6476 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6477 goto exit;
6478 }
6479 mbedtls_x509_crt_init( chain );
6480
6481 ret = ssl_parse_certificate_chain( ssl, chain );
6482 if( ret != 0 )
6483 goto exit;
6484
6485#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6486 if( ssl->handshake->ecrs_enabled)
6487 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6488
6489crt_verify:
6490 if( ssl->handshake->ecrs_enabled)
6491 rs_ctx = &ssl->handshake->ecrs_ctx;
6492#endif
6493
6494 ret = ssl_parse_certificate_verify( ssl, authmode,
6495 chain, rs_ctx );
6496 if( ret != 0 )
6497 goto exit;
6498
6499#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6500 {
6501 unsigned char *crt_start, *pk_start;
6502 size_t crt_len, pk_len;
6503
6504 /* We parse the CRT chain without copying, so
6505 * these pointers point into the input buffer,
6506 * and are hence still valid after freeing the
6507 * CRT chain. */
6508
6509 crt_start = chain->raw.p;
6510 crt_len = chain->raw.len;
6511
6512 pk_start = chain->pk_raw.p;
6513 pk_len = chain->pk_raw.len;
6514
6515 /* Free the CRT structures before computing
6516 * digest and copying the peer's public key. */
6517 mbedtls_x509_crt_free( chain );
6518 mbedtls_free( chain );
6519 chain = NULL;
6520
6521 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6522 if( ret != 0 )
6523 goto exit;
6524
6525 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6526 if( ret != 0 )
6527 goto exit;
6528 }
6529#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6530 /* Pass ownership to session structure. */
6531 ssl->session_negotiate->peer_cert = chain;
6532 chain = NULL;
6533#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6534
6535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6536
6537exit:
6538
6539 if( ret == 0 )
6540 ssl->state++;
6541
6542#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6543 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6544 {
6545 ssl->handshake->ecrs_peer_cert = chain;
6546 chain = NULL;
6547 }
6548#endif
6549
6550 if( chain != NULL )
6551 {
6552 mbedtls_x509_crt_free( chain );
6553 mbedtls_free( chain );
6554 }
6555
6556 return( ret );
6557}
6558#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6559
Jerry Yu615bd6f2022-02-17 14:25:15 +08006560#if defined(MBEDTLS_SHA256_C)
6561static void ssl_calc_finished_tls_sha256(
6562 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6563{
6564 int len = 12;
6565 const char *sender;
6566 unsigned char padbuf[32];
6567#if defined(MBEDTLS_USE_PSA_CRYPTO)
6568 size_t hash_size;
6569 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
6570 psa_status_t status;
6571#else
6572 mbedtls_sha256_context sha256;
6573#endif
6574
6575 mbedtls_ssl_session *session = ssl->session_negotiate;
6576 if( !session )
6577 session = ssl->session;
6578
6579 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6580 ? "client finished"
6581 : "server finished";
6582
6583#if defined(MBEDTLS_USE_PSA_CRYPTO)
6584 sha256_psa = psa_hash_operation_init();
6585
6586 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6587
6588 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6589 if( status != PSA_SUCCESS )
6590 {
6591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6592 return;
6593 }
6594
6595 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6596 if( status != PSA_SUCCESS )
6597 {
6598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6599 return;
6600 }
6601 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6602#else
6603
6604 mbedtls_sha256_init( &sha256 );
6605
6606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
6607
6608 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6609
6610 /*
6611 * TLSv1.2:
6612 * hash = PRF( master, finished_label,
6613 * Hash( handshake ) )[0.11]
6614 */
6615
6616#if !defined(MBEDTLS_SHA256_ALT)
6617 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
6618 sha256.state, sizeof( sha256.state ) );
6619#endif
6620
6621 mbedtls_sha256_finish( &sha256, padbuf );
6622 mbedtls_sha256_free( &sha256 );
6623#endif /* MBEDTLS_USE_PSA_CRYPTO */
6624
6625 ssl->handshake->tls_prf( session->master, 48, sender,
6626 padbuf, 32, buf, len );
6627
6628 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6629
6630 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6631
6632 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6633}
6634#endif /* MBEDTLS_SHA256_C */
6635
Jerry Yub7ba49e2022-02-17 14:25:53 +08006636
6637#if defined(MBEDTLS_SHA384_C)
6638
6639static void ssl_calc_finished_tls_sha384(
6640 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6641{
6642 int len = 12;
6643 const char *sender;
6644 unsigned char padbuf[48];
6645#if defined(MBEDTLS_USE_PSA_CRYPTO)
6646 size_t hash_size;
6647 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
6648 psa_status_t status;
6649#else
6650 mbedtls_sha512_context sha512;
6651#endif
6652
6653 mbedtls_ssl_session *session = ssl->session_negotiate;
6654 if( !session )
6655 session = ssl->session;
6656
6657 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6658 ? "client finished"
6659 : "server finished";
6660
6661#if defined(MBEDTLS_USE_PSA_CRYPTO)
6662 sha384_psa = psa_hash_operation_init();
6663
6664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6665
6666 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6667 if( status != PSA_SUCCESS )
6668 {
6669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6670 return;
6671 }
6672
6673 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
6674 if( status != PSA_SUCCESS )
6675 {
6676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6677 return;
6678 }
6679 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
6680#else
6681 mbedtls_sha512_init( &sha512 );
6682
6683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
6684
6685 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
6686
6687 /*
6688 * TLSv1.2:
6689 * hash = PRF( master, finished_label,
6690 * Hash( handshake ) )[0.11]
6691 */
6692
6693#if !defined(MBEDTLS_SHA512_ALT)
6694 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6695 sha512.state, sizeof( sha512.state ) );
6696#endif
6697 mbedtls_sha512_finish( &sha512, padbuf );
6698
6699 mbedtls_sha512_free( &sha512 );
6700#endif
6701
6702 ssl->handshake->tls_prf( session->master, 48, sender,
6703 padbuf, 48, buf, len );
6704
6705 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6706
6707 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6708
6709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6710}
6711#endif /* MBEDTLS_SHA384_C */
6712
Jerry Yuaef00152022-02-17 14:27:31 +08006713void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
6714{
6715 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
6716
6717 /*
6718 * Free our handshake params
6719 */
6720 mbedtls_ssl_handshake_free( ssl );
6721 mbedtls_free( ssl->handshake );
6722 ssl->handshake = NULL;
6723
6724 /*
6725 * Free the previous transform and swith in the current one
6726 */
6727 if( ssl->transform )
6728 {
6729 mbedtls_ssl_transform_free( ssl->transform );
6730 mbedtls_free( ssl->transform );
6731 }
6732 ssl->transform = ssl->transform_negotiate;
6733 ssl->transform_negotiate = NULL;
6734
6735 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
6736}
6737
Jerry Yu2a9fff52022-02-17 14:28:51 +08006738void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
6739{
6740 int resume = ssl->handshake->resume;
6741
6742 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
6743
6744#if defined(MBEDTLS_SSL_RENEGOTIATION)
6745 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6746 {
6747 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
6748 ssl->renego_records_seen = 0;
6749 }
6750#endif
6751
6752 /*
6753 * Free the previous session and switch in the current one
6754 */
6755 if( ssl->session )
6756 {
6757#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6758 /* RFC 7366 3.1: keep the EtM state */
6759 ssl->session_negotiate->encrypt_then_mac =
6760 ssl->session->encrypt_then_mac;
6761#endif
6762
6763 mbedtls_ssl_session_free( ssl->session );
6764 mbedtls_free( ssl->session );
6765 }
6766 ssl->session = ssl->session_negotiate;
6767 ssl->session_negotiate = NULL;
6768
6769 /*
6770 * Add cache entry
6771 */
6772 if( ssl->conf->f_set_cache != NULL &&
6773 ssl->session->id_len != 0 &&
6774 resume == 0 )
6775 {
6776 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
6777 ssl->session->id,
6778 ssl->session->id_len,
6779 ssl->session ) != 0 )
6780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
6781 }
6782
6783#if defined(MBEDTLS_SSL_PROTO_DTLS)
6784 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6785 ssl->handshake->flight != NULL )
6786 {
6787 /* Cancel handshake timer */
6788 mbedtls_ssl_set_timer( ssl, 0 );
6789
6790 /* Keep last flight around in case we need to resend it:
6791 * we need the handshake and transform structures for that */
6792 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
6793 }
6794 else
6795#endif
6796 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
6797
6798 ssl->state++;
6799
6800 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
6801}
6802
Jerry Yu3c8e47b2022-02-17 14:30:01 +08006803int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
6804{
6805 int ret, hash_len;
6806
6807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
6808
6809 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
6810
6811 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
6812
6813 /*
6814 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6815 * may define some other value. Currently (early 2016), no defined
6816 * ciphersuite does this (and this is unlikely to change as activity has
6817 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6818 */
6819 hash_len = 12;
6820
6821#if defined(MBEDTLS_SSL_RENEGOTIATION)
6822 ssl->verify_data_len = hash_len;
6823 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
6824#endif
6825
6826 ssl->out_msglen = 4 + hash_len;
6827 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6828 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
6829
6830 /*
6831 * In case of session resuming, invert the client and server
6832 * ChangeCipherSpec messages order.
6833 */
6834 if( ssl->handshake->resume != 0 )
6835 {
6836#if defined(MBEDTLS_SSL_CLI_C)
6837 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6838 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
6839#endif
6840#if defined(MBEDTLS_SSL_SRV_C)
6841 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6842 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
6843#endif
6844 }
6845 else
6846 ssl->state++;
6847
6848 /*
6849 * Switch to our negotiated transform and session parameters for outbound
6850 * data.
6851 */
6852 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
6853
6854#if defined(MBEDTLS_SSL_PROTO_DTLS)
6855 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6856 {
6857 unsigned char i;
6858
6859 /* Remember current epoch settings for resending */
6860 ssl->handshake->alt_transform_out = ssl->transform_out;
6861 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
6862 sizeof( ssl->handshake->alt_out_ctr ) );
6863
6864 /* Set sequence_number to zero */
6865 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
6866
6867
6868 /* Increment epoch */
6869 for( i = 2; i > 0; i-- )
6870 if( ++ssl->cur_out_ctr[i - 1] != 0 )
6871 break;
6872
6873 /* The loop goes to its end iff the counter is wrapping */
6874 if( i == 0 )
6875 {
6876 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6877 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
6878 }
6879 }
6880 else
6881#endif /* MBEDTLS_SSL_PROTO_DTLS */
6882 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6883
6884 ssl->transform_out = ssl->transform_negotiate;
6885 ssl->session_out = ssl->session_negotiate;
6886
6887#if defined(MBEDTLS_SSL_PROTO_DTLS)
6888 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6889 mbedtls_ssl_send_flight_completed( ssl );
6890#endif
6891
6892 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6893 {
6894 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6895 return( ret );
6896 }
6897
6898#if defined(MBEDTLS_SSL_PROTO_DTLS)
6899 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6900 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6901 {
6902 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6903 return( ret );
6904 }
6905#endif
6906
6907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
6908
6909 return( 0 );
6910}
6911
Jerry Yu0b3d7c12022-02-17 14:30:51 +08006912#define SSL_MAX_HASH_LEN 12
6913
6914int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
6915{
6916 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6917 unsigned int hash_len = 12;
6918 unsigned char buf[SSL_MAX_HASH_LEN];
6919
6920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
6921
6922 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
6923
6924 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6925 {
6926 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6927 goto exit;
6928 }
6929
6930 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6931 {
6932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6933 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6934 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6935 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
6936 goto exit;
6937 }
6938
6939 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
6940 {
6941 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6942 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6943 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
6944 goto exit;
6945 }
6946
6947 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
6948 {
6949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6950 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6951 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6952 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
6953 goto exit;
6954 }
6955
6956 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
6957 buf, hash_len ) != 0 )
6958 {
6959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6960 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6961 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
6962 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
6963 goto exit;
6964 }
6965
6966#if defined(MBEDTLS_SSL_RENEGOTIATION)
6967 ssl->verify_data_len = hash_len;
6968 memcpy( ssl->peer_verify_data, buf, hash_len );
6969#endif
6970
6971 if( ssl->handshake->resume != 0 )
6972 {
6973#if defined(MBEDTLS_SSL_CLI_C)
6974 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6975 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
6976#endif
6977#if defined(MBEDTLS_SSL_SRV_C)
6978 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6979 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
6980#endif
6981 }
6982 else
6983 ssl->state++;
6984
6985#if defined(MBEDTLS_SSL_PROTO_DTLS)
6986 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6987 mbedtls_ssl_recv_flight_completed( ssl );
6988#endif
6989
6990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
6991
6992exit:
6993 mbedtls_platform_zeroize( buf, hash_len );
6994 return( ret );
6995}
6996
Jerry Yu392112c2022-02-17 14:34:10 +08006997#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
6998/*
6999 * Helper to get TLS 1.2 PRF from ciphersuite
7000 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7001 */
7002static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7003{
7004#if defined(MBEDTLS_SHA384_C)
7005 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7006 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7007
7008 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
7009 return( tls_prf_sha384 );
7010#else
7011 (void) ciphersuite_id;
7012#endif
7013 return( tls_prf_sha256 );
7014}
7015#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007016
7017static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7018{
7019 ((void) tls_prf);
7020#if defined(MBEDTLS_SHA384_C)
7021 if( tls_prf == tls_prf_sha384 )
7022 {
7023 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7024 }
7025 else
7026#endif
7027#if defined(MBEDTLS_SHA256_C)
7028 if( tls_prf == tls_prf_sha256 )
7029 {
7030 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7031 }
7032 else
7033#endif
7034 return( MBEDTLS_SSL_TLS_PRF_NONE );
7035}
7036
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007037/*
7038 * Populate a transform structure with session keys and all the other
7039 * necessary information.
7040 *
7041 * Parameters:
7042 * - [in/out]: transform: structure to populate
7043 * [in] must be just initialised with mbedtls_ssl_transform_init()
7044 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7045 * - [in] ciphersuite
7046 * - [in] master
7047 * - [in] encrypt_then_mac
7048 * - [in] compression
7049 * - [in] tls_prf: pointer to PRF to use for key derivation
7050 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
7051 * - [in] minor_ver: SSL/TLS minor version
7052 * - [in] endpoint: client or server
7053 * - [in] ssl: used for:
7054 * - ssl->conf->{f,p}_export_keys
7055 * [in] optionally used for:
7056 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7057 */
7058static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7059 int ciphersuite,
7060 const unsigned char master[48],
7061#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
7062 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7063 int encrypt_then_mac,
7064#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
7065 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7066 ssl_tls_prf_t tls_prf,
7067 const unsigned char randbytes[64],
7068 int minor_ver,
7069 unsigned endpoint,
7070 const mbedtls_ssl_context *ssl )
7071{
7072 int ret = 0;
7073 unsigned char keyblk[256];
7074 unsigned char *key1;
7075 unsigned char *key2;
7076 unsigned char *mac_enc;
7077 unsigned char *mac_dec;
7078 size_t mac_key_len = 0;
7079 size_t iv_copy_len;
7080 size_t keylen;
7081 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
7082 const mbedtls_cipher_info_t *cipher_info;
7083 const mbedtls_md_info_t *md_info;
7084
7085#if defined(MBEDTLS_USE_PSA_CRYPTO)
7086 psa_key_type_t key_type;
7087 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7088 psa_algorithm_t alg;
7089 size_t key_bits;
7090 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7091#endif
7092
7093#if !defined(MBEDTLS_DEBUG_C) && \
7094 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7095 if( ssl->f_export_keys == NULL )
7096 {
7097 ssl = NULL; /* make sure we don't use it except for these cases */
7098 (void) ssl;
7099 }
7100#endif
7101
7102 /*
7103 * Some data just needs copying into the structure
7104 */
7105#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
7106 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7107 transform->encrypt_then_mac = encrypt_then_mac;
7108#endif
7109 transform->minor_ver = minor_ver;
7110
7111#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7112 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7113#endif
7114
7115#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
7116 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
7117 {
7118 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7119 * generation separate. This should never happen. */
7120 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7121 }
7122#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7123
7124 /*
7125 * Get various info structures
7126 */
7127 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7128 if( ciphersuite_info == NULL )
7129 {
7130 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7131 ciphersuite ) );
7132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7133 }
7134
7135 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7136 if( cipher_info == NULL )
7137 {
7138 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7139 ciphersuite_info->cipher ) );
7140 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7141 }
7142
7143 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7144 if( md_info == NULL )
7145 {
7146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7147 (unsigned) ciphersuite_info->mac ) );
7148 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7149 }
7150
7151#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7152 /* Copy own and peer's CID if the use of the CID
7153 * extension has been negotiated. */
7154 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7155 {
7156 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7157
7158 transform->in_cid_len = ssl->own_cid_len;
7159 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7160 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7161 transform->in_cid_len );
7162
7163 transform->out_cid_len = ssl->handshake->peer_cid_len;
7164 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7165 ssl->handshake->peer_cid_len );
7166 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7167 transform->out_cid_len );
7168 }
7169#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7170
7171 /*
7172 * Compute key block using the PRF
7173 */
7174 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7175 if( ret != 0 )
7176 {
7177 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7178 return( ret );
7179 }
7180
7181 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7182 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7183 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7184 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7185 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7186
7187 /*
7188 * Determine the appropriate key, IV and MAC length.
7189 */
7190
7191 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
7192
7193#if defined(MBEDTLS_GCM_C) || \
7194 defined(MBEDTLS_CCM_C) || \
7195 defined(MBEDTLS_CHACHAPOLY_C)
7196 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_GCM ||
7197 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CCM ||
7198 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
7199 {
7200 size_t explicit_ivlen;
7201
7202 transform->maclen = 0;
7203 mac_key_len = 0;
7204 transform->taglen =
7205 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7206
7207 /* All modes haves 96-bit IVs, but the length of the static parts vary
7208 * with mode and version:
7209 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7210 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7211 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7212 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7213 * sequence number).
7214 */
7215 transform->ivlen = 12;
7216 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
7217 transform->fixed_ivlen = 12;
7218 else
7219 transform->fixed_ivlen = 4;
7220
7221 /* Minimum length of encrypted record */
7222 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7223 transform->minlen = explicit_ivlen + transform->taglen;
7224 }
7225 else
7226#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7227#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7228 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM ||
7229 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7230 {
Neil Armstrongcf8841a2022-02-24 11:17:45 +01007231#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007232 /* Initialize HMAC contexts */
7233 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7234 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7235 {
7236 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7237 goto end;
7238 }
Neil Armstrongcf8841a2022-02-24 11:17:45 +01007239#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007240
7241 /* Get MAC length */
7242 mac_key_len = mbedtls_md_get_size( md_info );
7243 transform->maclen = mac_key_len;
7244
7245 /* IV length */
7246 transform->ivlen = cipher_info->iv_size;
7247
7248 /* Minimum length */
7249 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM )
7250 transform->minlen = transform->maclen;
7251 else
7252 {
7253 /*
7254 * GenericBlockCipher:
7255 * 1. if EtM is in use: one block plus MAC
7256 * otherwise: * first multiple of blocklen greater than maclen
7257 * 2. IV
7258 */
7259#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7260 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
7261 {
7262 transform->minlen = transform->maclen
7263 + cipher_info->block_size;
7264 }
7265 else
7266#endif
7267 {
7268 transform->minlen = transform->maclen
7269 + cipher_info->block_size
7270 - transform->maclen % cipher_info->block_size;
7271 }
7272
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007273 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
7274 {
7275 transform->minlen += transform->ivlen;
7276 }
7277 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007278 {
7279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7280 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7281 goto end;
7282 }
7283 }
7284 }
7285 else
7286#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7287 {
7288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7289 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7290 }
7291
7292 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7293 (unsigned) keylen,
7294 (unsigned) transform->minlen,
7295 (unsigned) transform->ivlen,
7296 (unsigned) transform->maclen ) );
7297
7298 /*
7299 * Finally setup the cipher contexts, IVs and MAC secrets.
7300 */
7301#if defined(MBEDTLS_SSL_CLI_C)
7302 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7303 {
7304 key1 = keyblk + mac_key_len * 2;
7305 key2 = keyblk + mac_key_len * 2 + keylen;
7306
7307 mac_enc = keyblk;
7308 mac_dec = keyblk + mac_key_len;
7309
7310 /*
7311 * This is not used in TLS v1.1.
7312 */
7313 iv_copy_len = ( transform->fixed_ivlen ) ?
7314 transform->fixed_ivlen : transform->ivlen;
7315 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7316 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7317 iv_copy_len );
7318 }
7319 else
7320#endif /* MBEDTLS_SSL_CLI_C */
7321#if defined(MBEDTLS_SSL_SRV_C)
7322 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7323 {
7324 key1 = keyblk + mac_key_len * 2 + keylen;
7325 key2 = keyblk + mac_key_len * 2;
7326
7327 mac_enc = keyblk + mac_key_len;
7328 mac_dec = keyblk;
7329
7330 /*
7331 * This is not used in TLS v1.1.
7332 */
7333 iv_copy_len = ( transform->fixed_ivlen ) ?
7334 transform->fixed_ivlen : transform->ivlen;
7335 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7336 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7337 iv_copy_len );
7338 }
7339 else
7340#endif /* MBEDTLS_SSL_SRV_C */
7341 {
7342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7343 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7344 goto end;
7345 }
7346
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007347 if( ssl != NULL && ssl->f_export_keys != NULL )
7348 {
7349 ssl->f_export_keys( ssl->p_export_keys,
7350 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7351 master, 48,
7352 randbytes + 32,
7353 randbytes,
7354 tls_prf_get_type( tls_prf ) );
7355 }
7356
7357#if defined(MBEDTLS_USE_PSA_CRYPTO)
7358 if( ( status = mbedtls_ssl_cipher_to_psa( cipher_info->type,
7359 transform->taglen,
7360 &alg,
7361 &key_type,
7362 &key_bits ) ) != PSA_SUCCESS )
7363 {
7364 ret = psa_ssl_status_to_mbedtls( status );
7365 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7366 goto end;
7367 }
7368
7369 transform->psa_alg = alg;
7370
7371 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7372 {
7373 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7374 psa_set_key_algorithm( &attributes, alg );
7375 psa_set_key_type( &attributes, key_type );
7376
7377 if( ( status = psa_import_key( &attributes,
7378 key1,
7379 PSA_BITS_TO_BYTES( key_bits ),
7380 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7381 {
7382 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7383 ret = psa_ssl_status_to_mbedtls( status );
7384 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7385 goto end;
7386 }
7387
7388 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7389
7390 if( ( status = psa_import_key( &attributes,
7391 key2,
7392 PSA_BITS_TO_BYTES( key_bits ),
7393 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7394 {
7395 ret = psa_ssl_status_to_mbedtls( status );
7396 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7397 goto end;
7398 }
7399 }
7400#else
7401 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7402 cipher_info ) ) != 0 )
7403 {
7404 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7405 goto end;
7406 }
7407
7408 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7409 cipher_info ) ) != 0 )
7410 {
7411 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7412 goto end;
7413 }
7414
7415 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7416 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7417 MBEDTLS_ENCRYPT ) ) != 0 )
7418 {
7419 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7420 goto end;
7421 }
7422
7423 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7424 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7425 MBEDTLS_DECRYPT ) ) != 0 )
7426 {
7427 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7428 goto end;
7429 }
7430
7431#if defined(MBEDTLS_CIPHER_MODE_CBC)
7432 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7433 {
7434 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7435 MBEDTLS_PADDING_NONE ) ) != 0 )
7436 {
7437 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7438 goto end;
7439 }
7440
7441 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7442 MBEDTLS_PADDING_NONE ) ) != 0 )
7443 {
7444 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7445 goto end;
7446 }
7447 }
7448#endif /* MBEDTLS_CIPHER_MODE_CBC */
7449#endif /* MBEDTLS_USE_PSA_CRYPTO */
7450
Neil Armstrong29c0c042022-03-17 17:47:28 +01007451#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7452 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7453 For AEAD-based ciphersuites, there is nothing to do here. */
7454 if( mac_key_len != 0 )
7455 {
7456#if defined(MBEDTLS_USE_PSA_CRYPTO)
7457 alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
7458 if( alg == 0 )
7459 {
7460 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_md_type_to_psa", ret );
7462 goto end;
7463 }
7464
7465 transform->psa_mac_alg = PSA_ALG_HMAC( alg );
7466
7467 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
7468 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( alg ) );
7469 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7470
7471 if( ( status = psa_import_key( &attributes,
7472 mac_enc, mac_key_len,
7473 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7474 {
7475 ret = psa_ssl_status_to_mbedtls( status );
7476 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7477 goto end;
7478 }
7479
Ronald Cronfb39f152022-03-25 14:36:28 +01007480 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
7481 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) &&
7482 ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007483 /* mbedtls_ct_hmac() requires the key to be exportable */
7484 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7485 PSA_KEY_USAGE_VERIFY_HASH );
7486 else
7487 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7488
7489 if( ( status = psa_import_key( &attributes,
7490 mac_dec, mac_key_len,
7491 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7492 {
7493 ret = psa_ssl_status_to_mbedtls( status );
7494 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7495 goto end;
7496 }
7497#else
7498 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7499 if( ret != 0 )
7500 goto end;
7501 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7502 if( ret != 0 )
7503 goto end;
7504#endif /* MBEDTLS_USE_PSA_CRYPTO */
7505 }
7506#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7507
7508 ((void) mac_dec);
7509 ((void) mac_enc);
7510
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007511end:
7512 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7513 return( ret );
7514}
7515
Jerry Yuee40f9d2022-02-17 14:55:16 +08007516#if defined(MBEDTLS_USE_PSA_CRYPTO)
7517int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7518 unsigned char *hash, size_t *hashlen,
7519 unsigned char *data, size_t data_len,
7520 mbedtls_md_type_t md_alg )
7521{
7522 psa_status_t status;
7523 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
7524 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
7525
7526 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7527
7528 if( ( status = psa_hash_setup( &hash_operation,
7529 hash_alg ) ) != PSA_SUCCESS )
7530 {
7531 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7532 goto exit;
7533 }
7534
7535 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
7536 64 ) ) != PSA_SUCCESS )
7537 {
7538 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7539 goto exit;
7540 }
7541
7542 if( ( status = psa_hash_update( &hash_operation,
7543 data, data_len ) ) != PSA_SUCCESS )
7544 {
7545 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7546 goto exit;
7547 }
7548
7549 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
7550 hashlen ) ) != PSA_SUCCESS )
7551 {
7552 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
7553 goto exit;
7554 }
7555
7556exit:
7557 if( status != PSA_SUCCESS )
7558 {
7559 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7560 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7561 switch( status )
7562 {
7563 case PSA_ERROR_NOT_SUPPORTED:
7564 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
7565 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
7566 case PSA_ERROR_BUFFER_TOO_SMALL:
7567 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
7568 case PSA_ERROR_INSUFFICIENT_MEMORY:
7569 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
7570 default:
7571 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
7572 }
7573 }
7574 return( 0 );
7575}
7576
7577#else
7578
7579int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7580 unsigned char *hash, size_t *hashlen,
7581 unsigned char *data, size_t data_len,
7582 mbedtls_md_type_t md_alg )
7583{
7584 int ret = 0;
7585 mbedtls_md_context_t ctx;
7586 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
7587 *hashlen = mbedtls_md_get_size( md_info );
7588
7589 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
7590
7591 mbedtls_md_init( &ctx );
7592
7593 /*
7594 * digitally-signed struct {
7595 * opaque client_random[32];
7596 * opaque server_random[32];
7597 * ServerDHParams params;
7598 * };
7599 */
7600 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
7601 {
7602 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7603 goto exit;
7604 }
7605 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
7606 {
7607 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
7608 goto exit;
7609 }
7610 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
7611 {
7612 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7613 goto exit;
7614 }
7615 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
7616 {
7617 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7618 goto exit;
7619 }
7620 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
7621 {
7622 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
7623 goto exit;
7624 }
7625
7626exit:
7627 mbedtls_md_free( &ctx );
7628
7629 if( ret != 0 )
7630 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7631 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7632
7633 return( ret );
7634}
7635#endif /* MBEDTLS_USE_PSA_CRYPTO */
7636
Jerry Yud9d91da2022-02-17 14:57:06 +08007637#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
7638
7639/* Find an entry in a signature-hash set matching a given hash algorithm. */
7640mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
7641 mbedtls_pk_type_t sig_alg )
7642{
7643 switch( sig_alg )
7644 {
7645 case MBEDTLS_PK_RSA:
7646 return( set->rsa );
7647 case MBEDTLS_PK_ECDSA:
7648 return( set->ecdsa );
7649 default:
7650 return( MBEDTLS_MD_NONE );
7651 }
7652}
7653
7654/* Add a signature-hash-pair to a signature-hash set */
7655void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
7656 mbedtls_pk_type_t sig_alg,
7657 mbedtls_md_type_t md_alg )
7658{
7659 switch( sig_alg )
7660 {
7661 case MBEDTLS_PK_RSA:
7662 if( set->rsa == MBEDTLS_MD_NONE )
7663 set->rsa = md_alg;
7664 break;
7665
7666 case MBEDTLS_PK_ECDSA:
7667 if( set->ecdsa == MBEDTLS_MD_NONE )
7668 set->ecdsa = md_alg;
7669 break;
7670
7671 default:
7672 break;
7673 }
7674}
7675
7676/* Allow exactly one hash algorithm for each signature. */
7677void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
7678 mbedtls_md_type_t md_alg )
7679{
7680 set->rsa = md_alg;
7681 set->ecdsa = md_alg;
7682}
7683
7684#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7685
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08007686/* Serialization of TLS 1.2 sessions:
7687 *
7688 * struct {
7689 * uint64 start_time;
7690 * uint8 ciphersuite[2]; // defined by the standard
7691 * uint8 compression; // 0 or 1
7692 * uint8 session_id_len; // at most 32
7693 * opaque session_id[32];
7694 * opaque master[48]; // fixed length in the standard
7695 * uint32 verify_result;
7696 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
7697 * opaque ticket<0..2^24-1>; // length 0 means no ticket
7698 * uint32 ticket_lifetime;
7699 * uint8 mfl_code; // up to 255 according to standard
7700 * uint8 encrypt_then_mac; // 0 or 1
7701 * } serialized_session_tls12;
7702 *
7703 */
7704static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session,
7705 unsigned char *buf,
7706 size_t buf_len )
7707{
7708 unsigned char *p = buf;
7709 size_t used = 0;
7710
7711#if defined(MBEDTLS_HAVE_TIME)
7712 uint64_t start;
7713#endif
7714#if defined(MBEDTLS_X509_CRT_PARSE_C)
7715#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7716 size_t cert_len;
7717#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7718#endif /* MBEDTLS_X509_CRT_PARSE_C */
7719
7720 /*
7721 * Time
7722 */
7723#if defined(MBEDTLS_HAVE_TIME)
7724 used += 8;
7725
7726 if( used <= buf_len )
7727 {
7728 start = (uint64_t) session->start;
7729
7730 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
7731 p += 8;
7732 }
7733#endif /* MBEDTLS_HAVE_TIME */
7734
7735 /*
7736 * Basic mandatory fields
7737 */
7738 used += 2 /* ciphersuite */
7739 + 1 /* compression */
7740 + 1 /* id_len */
7741 + sizeof( session->id )
7742 + sizeof( session->master )
7743 + 4; /* verify_result */
7744
7745 if( used <= buf_len )
7746 {
7747 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
7748 p += 2;
7749
7750 *p++ = MBEDTLS_BYTE_0( session->compression );
7751
7752 *p++ = MBEDTLS_BYTE_0( session->id_len );
7753 memcpy( p, session->id, 32 );
7754 p += 32;
7755
7756 memcpy( p, session->master, 48 );
7757 p += 48;
7758
7759 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
7760 p += 4;
7761 }
7762
7763 /*
7764 * Peer's end-entity certificate
7765 */
7766#if defined(MBEDTLS_X509_CRT_PARSE_C)
7767#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7768 if( session->peer_cert == NULL )
7769 cert_len = 0;
7770 else
7771 cert_len = session->peer_cert->raw.len;
7772
7773 used += 3 + cert_len;
7774
7775 if( used <= buf_len )
7776 {
7777 *p++ = MBEDTLS_BYTE_2( cert_len );
7778 *p++ = MBEDTLS_BYTE_1( cert_len );
7779 *p++ = MBEDTLS_BYTE_0( cert_len );
7780
7781 if( session->peer_cert != NULL )
7782 {
7783 memcpy( p, session->peer_cert->raw.p, cert_len );
7784 p += cert_len;
7785 }
7786 }
7787#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7788 if( session->peer_cert_digest != NULL )
7789 {
7790 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
7791 if( used <= buf_len )
7792 {
7793 *p++ = (unsigned char) session->peer_cert_digest_type;
7794 *p++ = (unsigned char) session->peer_cert_digest_len;
7795 memcpy( p, session->peer_cert_digest,
7796 session->peer_cert_digest_len );
7797 p += session->peer_cert_digest_len;
7798 }
7799 }
7800 else
7801 {
7802 used += 2;
7803 if( used <= buf_len )
7804 {
7805 *p++ = (unsigned char) MBEDTLS_MD_NONE;
7806 *p++ = 0;
7807 }
7808 }
7809#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7810#endif /* MBEDTLS_X509_CRT_PARSE_C */
7811
7812 /*
7813 * Session ticket if any, plus associated data
7814 */
7815#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7816 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
7817
7818 if( used <= buf_len )
7819 {
7820 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
7821 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
7822 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
7823
7824 if( session->ticket != NULL )
7825 {
7826 memcpy( p, session->ticket, session->ticket_len );
7827 p += session->ticket_len;
7828 }
7829
7830 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
7831 p += 4;
7832 }
7833#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7834
7835 /*
7836 * Misc extension-related info
7837 */
7838#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7839 used += 1;
7840
7841 if( used <= buf_len )
7842 *p++ = session->mfl_code;
7843#endif
7844
7845#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7846 used += 1;
7847
7848 if( used <= buf_len )
7849 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
7850#endif
7851
7852 return( used );
7853}
7854
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08007855static int ssl_session_load_tls12( mbedtls_ssl_session *session,
7856 const unsigned char *buf,
7857 size_t len )
7858{
7859#if defined(MBEDTLS_HAVE_TIME)
7860 uint64_t start;
7861#endif
7862#if defined(MBEDTLS_X509_CRT_PARSE_C)
7863#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7864 size_t cert_len;
7865#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7866#endif /* MBEDTLS_X509_CRT_PARSE_C */
7867
7868 const unsigned char *p = buf;
7869 const unsigned char * const end = buf + len;
7870
7871 /*
7872 * Time
7873 */
7874#if defined(MBEDTLS_HAVE_TIME)
7875 if( 8 > (size_t)( end - p ) )
7876 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7877
7878 start = ( (uint64_t) p[0] << 56 ) |
7879 ( (uint64_t) p[1] << 48 ) |
7880 ( (uint64_t) p[2] << 40 ) |
7881 ( (uint64_t) p[3] << 32 ) |
7882 ( (uint64_t) p[4] << 24 ) |
7883 ( (uint64_t) p[5] << 16 ) |
7884 ( (uint64_t) p[6] << 8 ) |
7885 ( (uint64_t) p[7] );
7886 p += 8;
7887
7888 session->start = (time_t) start;
7889#endif /* MBEDTLS_HAVE_TIME */
7890
7891 /*
7892 * Basic mandatory fields
7893 */
7894 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
7895 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7896
7897 session->ciphersuite = ( p[0] << 8 ) | p[1];
7898 p += 2;
7899
7900 session->compression = *p++;
7901
7902 session->id_len = *p++;
7903 memcpy( session->id, p, 32 );
7904 p += 32;
7905
7906 memcpy( session->master, p, 48 );
7907 p += 48;
7908
7909 session->verify_result = ( (uint32_t) p[0] << 24 ) |
7910 ( (uint32_t) p[1] << 16 ) |
7911 ( (uint32_t) p[2] << 8 ) |
7912 ( (uint32_t) p[3] );
7913 p += 4;
7914
7915 /* Immediately clear invalid pointer values that have been read, in case
7916 * we exit early before we replaced them with valid ones. */
7917#if defined(MBEDTLS_X509_CRT_PARSE_C)
7918#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7919 session->peer_cert = NULL;
7920#else
7921 session->peer_cert_digest = NULL;
7922#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7923#endif /* MBEDTLS_X509_CRT_PARSE_C */
7924#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7925 session->ticket = NULL;
7926#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7927
7928 /*
7929 * Peer certificate
7930 */
7931#if defined(MBEDTLS_X509_CRT_PARSE_C)
7932#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7933 /* Deserialize CRT from the end of the ticket. */
7934 if( 3 > (size_t)( end - p ) )
7935 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7936
7937 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
7938 p += 3;
7939
7940 if( cert_len != 0 )
7941 {
7942 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7943
7944 if( cert_len > (size_t)( end - p ) )
7945 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7946
7947 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7948
7949 if( session->peer_cert == NULL )
7950 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7951
7952 mbedtls_x509_crt_init( session->peer_cert );
7953
7954 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
7955 p, cert_len ) ) != 0 )
7956 {
7957 mbedtls_x509_crt_free( session->peer_cert );
7958 mbedtls_free( session->peer_cert );
7959 session->peer_cert = NULL;
7960 return( ret );
7961 }
7962
7963 p += cert_len;
7964 }
7965#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7966 /* Deserialize CRT digest from the end of the ticket. */
7967 if( 2 > (size_t)( end - p ) )
7968 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7969
7970 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
7971 session->peer_cert_digest_len = (size_t) *p++;
7972
7973 if( session->peer_cert_digest_len != 0 )
7974 {
7975 const mbedtls_md_info_t *md_info =
7976 mbedtls_md_info_from_type( session->peer_cert_digest_type );
7977 if( md_info == NULL )
7978 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7979 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
7980 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7981
7982 if( session->peer_cert_digest_len > (size_t)( end - p ) )
7983 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7984
7985 session->peer_cert_digest =
7986 mbedtls_calloc( 1, session->peer_cert_digest_len );
7987 if( session->peer_cert_digest == NULL )
7988 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7989
7990 memcpy( session->peer_cert_digest, p,
7991 session->peer_cert_digest_len );
7992 p += session->peer_cert_digest_len;
7993 }
7994#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7995#endif /* MBEDTLS_X509_CRT_PARSE_C */
7996
7997 /*
7998 * Session ticket and associated data
7999 */
8000#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8001 if( 3 > (size_t)( end - p ) )
8002 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8003
8004 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8005 p += 3;
8006
8007 if( session->ticket_len != 0 )
8008 {
8009 if( session->ticket_len > (size_t)( end - p ) )
8010 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8011
8012 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8013 if( session->ticket == NULL )
8014 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8015
8016 memcpy( session->ticket, p, session->ticket_len );
8017 p += session->ticket_len;
8018 }
8019
8020 if( 4 > (size_t)( end - p ) )
8021 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8022
8023 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8024 ( (uint32_t) p[1] << 16 ) |
8025 ( (uint32_t) p[2] << 8 ) |
8026 ( (uint32_t) p[3] );
8027 p += 4;
8028#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8029
8030 /*
8031 * Misc extension-related info
8032 */
8033#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8034 if( 1 > (size_t)( end - p ) )
8035 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8036
8037 session->mfl_code = *p++;
8038#endif
8039
8040#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8041 if( 1 > (size_t)( end - p ) )
8042 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8043
8044 session->encrypt_then_mac = *p++;
8045#endif
8046
8047 /* Done, should have consumed entire buffer */
8048 if( p != end )
8049 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8050
8051 return( 0 );
8052}
Jerry Yudc7bd172022-02-17 13:44:15 +08008053#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008055#endif /* MBEDTLS_SSL_TLS_C */