blob: 86445de2477e0ba09890591679f407895699b73d [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) )
Hanno Becker4a63ed42019-01-08 11:39:35 +00003149 psa_destroy_key( handshake->ecdh_psa_privkey );
3150#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3151
Ronald Cron6f135e12021-12-08 16:57:54 +01003152#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003153 mbedtls_ssl_transform_free( handshake->transform_handshake );
3154 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003155 mbedtls_free( handshake->transform_earlydata );
3156 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003157#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003158
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003159
3160#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3161 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3162 * processes datagrams and the fact that a datagram is allowed to have
3163 * several records in it, it is possible that the I/O buffers are not
3164 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003165 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3166 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003167#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003168
Jerry Yuba9c7272021-10-30 11:54:10 +08003169 /* mbedtls_platform_zeroize MUST be last one in this function */
3170 mbedtls_platform_zeroize( handshake,
3171 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003172}
3173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003174void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003175{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003176 if( session == NULL )
3177 return;
3178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003180 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003181#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003182
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003183#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003185#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003186
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003187 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003188}
3189
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003190#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003191
3192#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3193#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3194#else
3195#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3196#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3197
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003198#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003199
3200#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3201#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3202#else
3203#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3204#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3205
3206#if defined(MBEDTLS_SSL_ALPN)
3207#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3208#else
3209#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3210#endif /* MBEDTLS_SSL_ALPN */
3211
3212#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3213#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3214#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3215#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3216
3217#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3218 ( (uint32_t) ( \
3219 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3220 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3221 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3222 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3223 0u ) )
3224
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003225static unsigned char ssl_serialized_context_header[] = {
3226 MBEDTLS_VERSION_MAJOR,
3227 MBEDTLS_VERSION_MINOR,
3228 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003229 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3230 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3231 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3232 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3233 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003234};
3235
Paul Bakker5121ce52009-01-03 21:22:43 +00003236/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003237 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003238 *
3239 * The format of the serialized data is:
3240 * (in the presentation language of TLS, RFC 8446 section 3)
3241 *
3242 * // header
3243 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003244 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003245 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003246 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003247 * Note: When updating the format, remember to keep these
3248 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003249 *
3250 * // session sub-structure
3251 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3252 * // transform sub-structure
3253 * uint8 random[64]; // ServerHello.random+ClientHello.random
3254 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3255 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3256 * // fields from ssl_context
3257 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3258 * uint64 in_window_top; // DTLS: last validated record seq_num
3259 * uint64 in_window; // DTLS: bitmask for replay protection
3260 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3261 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3262 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3263 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3264 *
3265 * Note that many fields of the ssl_context or sub-structures are not
3266 * serialized, as they fall in one of the following categories:
3267 *
3268 * 1. forced value (eg in_left must be 0)
3269 * 2. pointer to dynamically-allocated memory (eg session, transform)
3270 * 3. value can be re-derived from other data (eg session keys from MS)
3271 * 4. value was temporary (eg content of input buffer)
3272 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003273 */
3274int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3275 unsigned char *buf,
3276 size_t buf_len,
3277 size_t *olen )
3278{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003279 unsigned char *p = buf;
3280 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003281 size_t session_len;
3282 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003283
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003284 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003285 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3286 * this function's documentation.
3287 *
3288 * These are due to assumptions/limitations in the implementation. Some of
3289 * them are likely to stay (no handshake in progress) some might go away
3290 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003291 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003292 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003293 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003294 {
3295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003296 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003297 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003298 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003299 {
3300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003301 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003302 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003303 /* Double-check that sub-structures are indeed ready */
3304 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003305 {
3306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003307 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003308 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003309 /* There must be no pending incoming or outgoing data */
3310 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003311 {
3312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003313 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003314 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003315 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003316 {
3317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003318 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003319 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003320 /* Protocol must be DLTS, not TLS */
3321 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003322 {
3323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003324 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003325 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003326 /* Version must be 1.2 */
3327 if( ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003328 {
3329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003330 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003331 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003332 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003333 {
3334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003335 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003336 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003337 /* We must be using an AEAD ciphersuite */
3338 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003339 {
3340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003341 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003342 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003343 /* Renegotiation must not be enabled */
3344#if defined(MBEDTLS_SSL_RENEGOTIATION)
3345 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003346 {
3347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003348 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003349 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003350#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003351
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003352 /*
3353 * Version and format identifier
3354 */
3355 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003356
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003357 if( used <= buf_len )
3358 {
3359 memcpy( p, ssl_serialized_context_header,
3360 sizeof( ssl_serialized_context_header ) );
3361 p += sizeof( ssl_serialized_context_header );
3362 }
3363
3364 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003365 * Session (length + data)
3366 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003367 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003368 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3369 return( ret );
3370
3371 used += 4 + session_len;
3372 if( used <= buf_len )
3373 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003374 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3375 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003376
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003377 ret = ssl_session_save( ssl->session, 1,
3378 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003379 if( ret != 0 )
3380 return( ret );
3381
3382 p += session_len;
3383 }
3384
3385 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003386 * Transform
3387 */
3388 used += sizeof( ssl->transform->randbytes );
3389 if( used <= buf_len )
3390 {
3391 memcpy( p, ssl->transform->randbytes,
3392 sizeof( ssl->transform->randbytes ) );
3393 p += sizeof( ssl->transform->randbytes );
3394 }
3395
3396#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3397 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3398 if( used <= buf_len )
3399 {
3400 *p++ = ssl->transform->in_cid_len;
3401 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3402 p += ssl->transform->in_cid_len;
3403
3404 *p++ = ssl->transform->out_cid_len;
3405 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3406 p += ssl->transform->out_cid_len;
3407 }
3408#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3409
3410 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003411 * Saved fields from top-level ssl_context structure
3412 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003413 used += 4;
3414 if( used <= buf_len )
3415 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003416 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3417 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003418 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003419
3420#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3421 used += 16;
3422 if( used <= buf_len )
3423 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003424 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3425 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003426
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003427 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3428 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003429 }
3430#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3431
3432#if defined(MBEDTLS_SSL_PROTO_DTLS)
3433 used += 1;
3434 if( used <= buf_len )
3435 {
3436 *p++ = ssl->disable_datagram_packing;
3437 }
3438#endif /* MBEDTLS_SSL_PROTO_DTLS */
3439
Jerry Yuae0b2e22021-10-08 15:21:19 +08003440 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003441 if( used <= buf_len )
3442 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003443 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3444 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003445 }
3446
3447#if defined(MBEDTLS_SSL_PROTO_DTLS)
3448 used += 2;
3449 if( used <= buf_len )
3450 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003451 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
3452 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003453 }
3454#endif /* MBEDTLS_SSL_PROTO_DTLS */
3455
3456#if defined(MBEDTLS_SSL_ALPN)
3457 {
3458 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02003459 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003460 : 0;
3461
3462 used += 1 + alpn_len;
3463 if( used <= buf_len )
3464 {
3465 *p++ = alpn_len;
3466
3467 if( ssl->alpn_chosen != NULL )
3468 {
3469 memcpy( p, ssl->alpn_chosen, alpn_len );
3470 p += alpn_len;
3471 }
3472 }
3473 }
3474#endif /* MBEDTLS_SSL_ALPN */
3475
3476 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003477 * Done
3478 */
3479 *olen = used;
3480
3481 if( used > buf_len )
3482 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003483
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003484 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
3485
Hanno Becker43aefe22020-02-05 10:44:56 +00003486 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003487}
3488
3489/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003490 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003491 *
3492 * This internal version is wrapped by a public function that cleans up in
3493 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003494 */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003495static int ssl_context_load( mbedtls_ssl_context *ssl,
3496 const unsigned char *buf,
3497 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003498{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003499 const unsigned char *p = buf;
3500 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003501 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00003502 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003503
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003504 /*
3505 * The context should have been freshly setup or reset.
3506 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02003507 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003508 * renegotiating, or if the user mistakenly loaded a session first.)
3509 */
3510 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
3511 ssl->session != NULL )
3512 {
3513 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3514 }
3515
3516 /*
3517 * We can't check that the config matches the initial one, but we can at
3518 * least check it matches the requirements for serializing.
3519 */
3520 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
3521 ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
3522 ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 ||
3523 ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ||
3524 ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 ||
3525#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003526 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003527#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003528 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003529 {
3530 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3531 }
3532
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003533 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
3534
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003535 /*
3536 * Check version identifier
3537 */
3538 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
3539 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3540
3541 if( memcmp( p, ssl_serialized_context_header,
3542 sizeof( ssl_serialized_context_header ) ) != 0 )
3543 {
3544 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3545 }
3546 p += sizeof( ssl_serialized_context_header );
3547
3548 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003549 * Session
3550 */
3551 if( (size_t)( end - p ) < 4 )
3552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3553
3554 session_len = ( (size_t) p[0] << 24 ) |
3555 ( (size_t) p[1] << 16 ) |
3556 ( (size_t) p[2] << 8 ) |
3557 ( (size_t) p[3] );
3558 p += 4;
3559
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003560 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00003561 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003562 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003563 ssl->session_in = ssl->session;
3564 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003565 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003566
3567 if( (size_t)( end - p ) < session_len )
3568 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3569
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003570 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003571 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003572 {
3573 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003574 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003575 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003576
3577 p += session_len;
3578
3579 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003580 * Transform
3581 */
3582
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003583 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00003584 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003585 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003586 ssl->transform_in = ssl->transform;
3587 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003588 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003589
3590 /* Read random bytes and populate structure */
3591 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
3592 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08003593#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00003594 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003595 ssl->session->ciphersuite,
3596 ssl->session->master,
Hanno Beckerbd257552021-03-22 06:59:27 +00003597#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
3598 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003599 ssl->session->encrypt_then_mac,
Hanno Beckerbd257552021-03-22 06:59:27 +00003600#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
3601 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003602 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
3603 p, /* currently pointing to randbytes */
3604 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
3605 ssl->conf->endpoint,
3606 ssl );
3607 if( ret != 0 )
3608 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08003609#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003610 p += sizeof( ssl->transform->randbytes );
3611
3612#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3613 /* Read connection IDs and store them */
3614 if( (size_t)( end - p ) < 1 )
3615 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3616
3617 ssl->transform->in_cid_len = *p++;
3618
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02003619 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003620 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3621
3622 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
3623 p += ssl->transform->in_cid_len;
3624
3625 ssl->transform->out_cid_len = *p++;
3626
3627 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
3628 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3629
3630 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
3631 p += ssl->transform->out_cid_len;
3632#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3633
3634 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003635 * Saved fields from top-level ssl_context structure
3636 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003637 if( (size_t)( end - p ) < 4 )
3638 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3639
3640 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
3641 ( (uint32_t) p[1] << 16 ) |
3642 ( (uint32_t) p[2] << 8 ) |
3643 ( (uint32_t) p[3] );
3644 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003645
3646#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3647 if( (size_t)( end - p ) < 16 )
3648 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3649
3650 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
3651 ( (uint64_t) p[1] << 48 ) |
3652 ( (uint64_t) p[2] << 40 ) |
3653 ( (uint64_t) p[3] << 32 ) |
3654 ( (uint64_t) p[4] << 24 ) |
3655 ( (uint64_t) p[5] << 16 ) |
3656 ( (uint64_t) p[6] << 8 ) |
3657 ( (uint64_t) p[7] );
3658 p += 8;
3659
3660 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
3661 ( (uint64_t) p[1] << 48 ) |
3662 ( (uint64_t) p[2] << 40 ) |
3663 ( (uint64_t) p[3] << 32 ) |
3664 ( (uint64_t) p[4] << 24 ) |
3665 ( (uint64_t) p[5] << 16 ) |
3666 ( (uint64_t) p[6] << 8 ) |
3667 ( (uint64_t) p[7] );
3668 p += 8;
3669#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3670
3671#if defined(MBEDTLS_SSL_PROTO_DTLS)
3672 if( (size_t)( end - p ) < 1 )
3673 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3674
3675 ssl->disable_datagram_packing = *p++;
3676#endif /* MBEDTLS_SSL_PROTO_DTLS */
3677
Jerry Yud9a94fe2021-09-28 18:58:59 +08003678 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003679 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08003680 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
3681 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003682
3683#if defined(MBEDTLS_SSL_PROTO_DTLS)
3684 if( (size_t)( end - p ) < 2 )
3685 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3686
3687 ssl->mtu = ( p[0] << 8 ) | p[1];
3688 p += 2;
3689#endif /* MBEDTLS_SSL_PROTO_DTLS */
3690
3691#if defined(MBEDTLS_SSL_ALPN)
3692 {
3693 uint8_t alpn_len;
3694 const char **cur;
3695
3696 if( (size_t)( end - p ) < 1 )
3697 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3698
3699 alpn_len = *p++;
3700
3701 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
3702 {
3703 /* alpn_chosen should point to an item in the configured list */
3704 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
3705 {
3706 if( strlen( *cur ) == alpn_len &&
3707 memcmp( p, cur, alpn_len ) == 0 )
3708 {
3709 ssl->alpn_chosen = *cur;
3710 break;
3711 }
3712 }
3713 }
3714
3715 /* can only happen on conf mismatch */
3716 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
3717 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3718
3719 p += alpn_len;
3720 }
3721#endif /* MBEDTLS_SSL_ALPN */
3722
3723 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003724 * Forced fields from top-level ssl_context structure
3725 *
3726 * Most of them already set to the correct value by mbedtls_ssl_init() and
3727 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
3728 */
3729 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
3730
3731 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
3732 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
3733
Hanno Becker361b10d2019-08-30 10:42:49 +01003734 /* Adjust pointers for header fields of outgoing records to
3735 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00003736 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01003737
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003738#if defined(MBEDTLS_SSL_PROTO_DTLS)
3739 ssl->in_epoch = 1;
3740#endif
3741
3742 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
3743 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00003744 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
3745 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003746 if( ssl->handshake != NULL )
3747 {
3748 mbedtls_ssl_handshake_free( ssl );
3749 mbedtls_free( ssl->handshake );
3750 ssl->handshake = NULL;
3751 }
3752
3753 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003754 * Done - should have consumed entire buffer
3755 */
3756 if( p != end )
3757 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003758
3759 return( 0 );
3760}
3761
3762/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003763 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003764 */
3765int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
3766 const unsigned char *buf,
3767 size_t len )
3768{
3769 int ret = ssl_context_load( context, buf, len );
3770
3771 if( ret != 0 )
3772 mbedtls_ssl_free( context );
3773
3774 return( ret );
3775}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003776#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003777
3778/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003779 * Free an SSL context
3780 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003781void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003782{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003783 if( ssl == NULL )
3784 return;
3785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003787
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003788 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003789 {
sander-visserb8aa2072020-05-06 22:05:13 +02003790#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3791 size_t out_buf_len = ssl->out_buf_len;
3792#else
3793 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
3794#endif
3795
Darryl Greenb33cc762019-11-28 14:29:44 +00003796 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003797 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003798 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003799 }
3800
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003801 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003802 {
sander-visserb8aa2072020-05-06 22:05:13 +02003803#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3804 size_t in_buf_len = ssl->in_buf_len;
3805#else
3806 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
3807#endif
3808
Darryl Greenb33cc762019-11-28 14:29:44 +00003809 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003810 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003811 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003812 }
3813
Paul Bakker48916f92012-09-16 19:57:18 +00003814 if( ssl->transform )
3815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003816 mbedtls_ssl_transform_free( ssl->transform );
3817 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003818 }
3819
3820 if( ssl->handshake )
3821 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02003822 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003823 mbedtls_ssl_transform_free( ssl->transform_negotiate );
3824 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00003825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003826 mbedtls_free( ssl->handshake );
3827 mbedtls_free( ssl->transform_negotiate );
3828 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00003829 }
3830
Ronald Cron6f135e12021-12-08 16:57:54 +01003831#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01003832 mbedtls_ssl_transform_free( ssl->transform_application );
3833 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01003834#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01003835
Paul Bakkerc0463502013-02-14 11:19:38 +01003836 if( ssl->session )
3837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003838 mbedtls_ssl_session_free( ssl->session );
3839 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003840 }
3841
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02003842#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02003843 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003844 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003845 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00003847 }
Paul Bakker0be444a2013-08-27 21:55:01 +02003848#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003849
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003850#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003851 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02003852#endif
3853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00003855
Paul Bakker86f04f42013-02-14 11:20:09 +01003856 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003857 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003858}
3859
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02003860/*
3861 * Initialze mbedtls_ssl_config
3862 */
3863void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
3864{
3865 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
3866}
3867
Gilles Peskineeccd8882020-03-10 12:19:08 +01003868#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003869#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskineae270bf2021-06-02 00:05:29 +02003870/* The selection should be the same as mbedtls_x509_crt_profile_default in
Gilles Peskinea28f0f52021-06-02 15:29:38 +02003871 * x509_crt.c. Here, the order matters. Currently we favor stronger hashes,
3872 * for no fundamental reason.
Gilles Peskineae270bf2021-06-02 00:05:29 +02003873 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
3874 * about this list. */
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003875static int ssl_preset_default_hashes[] = {
3876#if defined(MBEDTLS_SHA512_C)
3877 MBEDTLS_MD_SHA512,
Mateusz Starzyk3352a532021-04-06 14:28:22 +02003878#endif
3879#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003880 MBEDTLS_MD_SHA384,
3881#endif
3882#if defined(MBEDTLS_SHA256_C)
3883 MBEDTLS_MD_SHA256,
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02003884#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003885 MBEDTLS_MD_NONE
3886};
Jerry Yuf017ee42022-01-12 15:49:48 +08003887#endif /* !MBEDTLS_DEPRECATED_REMOVED */
3888#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003889
Gilles Peskineae270bf2021-06-02 00:05:29 +02003890/* The selection should be the same as mbedtls_x509_crt_profile_default in
3891 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02003892 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02003893 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02003894 * about this list.
3895 */
Brett Warrene0edc842021-08-17 09:53:13 +01003896static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02003897#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003898 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003899#endif
3900#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003901 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003902#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02003903#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003904 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003905#endif
3906#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003907 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003908#endif
3909#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003910 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003911#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02003912#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003913 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003914#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02003915#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003916 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003917#endif
3918#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003919 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003920#endif
Brett Warrene0edc842021-08-17 09:53:13 +01003921 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02003922};
Gilles Peskineae270bf2021-06-02 00:05:29 +02003923
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003924static int ssl_preset_suiteb_ciphersuites[] = {
3925 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
3926 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
3927 0
3928};
3929
Gilles Peskineeccd8882020-03-10 12:19:08 +01003930#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003931#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003932static int ssl_preset_suiteb_hashes[] = {
Jerry Yuf017ee42022-01-12 15:49:48 +08003933#if defined(MBEDTLS_SHA256_C)
3934 MBEDTLS_MD_SHA256,
3935#endif
Jerry Yu713013f2022-01-17 18:16:35 +08003936#if defined(MBEDTLS_SHA384_C)
3937 MBEDTLS_MD_SHA384,
3938#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003939 MBEDTLS_MD_NONE
3940};
Jerry Yuf017ee42022-01-12 15:49:48 +08003941#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01003942
Jerry Yu909df7b2022-01-22 11:56:27 +08003943/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08003944 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08003945 * rules SHOULD be upheld.
3946 * - No duplicate entries.
3947 * - But if there is a good reason, do not change the order of the algorithms.
3948 * - ssl_tls12_present* is for TLS 1.2 use only.
3949 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08003950 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01003951static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08003952
Jerry Yued5e9f42022-01-26 11:21:34 +08003953#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \
3954 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
3955 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
3956#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C &&
3957 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08003958
Jerry Yu909df7b2022-01-22 11:56:27 +08003959#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \
3960 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
3961 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
3962#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
3963 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
3964
Jerry Yued5e9f42022-01-26 11:21:34 +08003965#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA512_C) && \
3966 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
3967 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
3968#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
3969 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08003970
Jerry Yu909df7b2022-01-22 11:56:27 +08003971#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C)
3972 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
3973#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */
3974
Ronald Cron8540cf62022-03-16 08:01:09 +01003975#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA512_C)
3976 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
3977#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
3978
3979#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA384_C)
3980 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
3981#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
3982
Jerry Yu53037892022-01-25 11:02:06 +08003983#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C)
3984 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
3985#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
3986
Jerry Yu909df7b2022-01-22 11:56:27 +08003987 MBEDTLS_TLS1_3_SIG_NONE
3988};
3989
3990/* NOTICE: see above */
3991#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3992static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Jerry Yu713013f2022-01-17 18:16:35 +08003993#if defined(MBEDTLS_SHA512_C)
3994 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA512 )
3995#endif
Jerry Yu11f0a9c2022-01-12 18:43:08 +08003996#if defined(MBEDTLS_SHA384_C)
3997 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 )
3998#endif
3999#if defined(MBEDTLS_SHA256_C)
4000 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 )
4001#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08004002 MBEDTLS_TLS1_3_SIG_NONE
4003};
4004#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4005/* NOTICE: see above */
4006static uint16_t ssl_preset_suiteb_sig_algs[] = {
4007
Jerry Yu909df7b2022-01-22 11:56:27 +08004008#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \
4009 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4010 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
4011#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C &&
4012 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4013
Jerry Yu53037892022-01-25 11:02:06 +08004014#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \
4015 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4016 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
4017#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
4018 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004019
4020#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C)
4021 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4022#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004023
Jerry Yu53037892022-01-25 11:02:06 +08004024#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C)
4025 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4026#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4027
Jerry Yu713013f2022-01-17 18:16:35 +08004028 MBEDTLS_TLS1_3_SIG_NONE
4029};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004030
Jerry Yu909df7b2022-01-22 11:56:27 +08004031/* NOTICE: see above */
4032#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4033static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Jerry Yu713013f2022-01-17 18:16:35 +08004034#if defined(MBEDTLS_SHA256_C)
4035 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 )
4036#endif
Jerry Yu18c833e2022-01-25 10:55:47 +08004037#if defined(MBEDTLS_SHA384_C)
4038 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 )
4039#endif
Xiaofei Bai95395012021-11-25 08:51:30 +00004040 MBEDTLS_TLS1_3_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004041};
Jerry Yu909df7b2022-01-22 11:56:27 +08004042#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4043
Jerry Yu1a8b4812022-01-20 17:56:50 +08004044#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004045
Brett Warrene0edc842021-08-17 09:53:13 +01004046static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004047#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004048 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004049#endif
4050#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004051 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004052#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004053 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004054};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004055
Jerry Yu1a8b4812022-01-20 17:56:50 +08004056#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004057/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004058 * to make sure there are no duplicated signature algorithm entries. */
Jerry Yuf377d642022-01-25 10:43:59 +08004059static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004060{
4061 size_t i, j;
4062 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004063
Jerry Yuf377d642022-01-25 10:43:59 +08004064 for( i = 0; sig_algs[i] != MBEDTLS_TLS1_3_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004065 {
Jerry Yuf377d642022-01-25 10:43:59 +08004066 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004067 {
Jerry Yuf377d642022-01-25 10:43:59 +08004068 if( sig_algs[i] != sig_algs[j] )
4069 continue;
4070 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4071 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4072 sig_algs[i], j, i );
4073 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004074 }
4075 }
4076 return( ret );
4077}
4078
4079#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4080
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004081/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004082 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004083 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004084int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004085 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004086{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004087#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004088 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004089#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004090
Jerry Yu1a8b4812022-01-20 17:56:50 +08004091#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004092 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004093 {
4094 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4095 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4096 }
4097
Jerry Yuf377d642022-01-25 10:43:59 +08004098 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004099 {
4100 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4101 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4102 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004103
4104#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004105 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004106 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004107 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004108 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4109 }
4110
Jerry Yuf377d642022-01-25 10:43:59 +08004111 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004112 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004113 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004114 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4115 }
4116#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004117#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4118
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004119 /* Use the functions here so that they are covered in tests,
4120 * but otherwise access member directly for efficiency */
4121 mbedtls_ssl_conf_endpoint( conf, endpoint );
4122 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004123
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004124 /*
4125 * Things that are common to all presets
4126 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004127#if defined(MBEDTLS_SSL_CLI_C)
4128 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4129 {
4130 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4131#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4132 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4133#endif
4134 }
4135#endif
4136
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004137#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4138 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4139#endif
4140
4141#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4142 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4143#endif
4144
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004145#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004146 conf->f_cookie_write = ssl_cookie_write_dummy;
4147 conf->f_cookie_check = ssl_cookie_check_dummy;
4148#endif
4149
4150#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4151 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4152#endif
4153
Janos Follath088ce432017-04-10 12:42:31 +01004154#if defined(MBEDTLS_SSL_SRV_C)
4155 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004156 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004157#endif
4158
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004159#if defined(MBEDTLS_SSL_PROTO_DTLS)
4160 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4161 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4162#endif
4163
4164#if defined(MBEDTLS_SSL_RENEGOTIATION)
4165 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004166 memset( conf->renego_period, 0x00, 2 );
4167 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004168#endif
4169
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004170#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004171 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4172 {
4173 const unsigned char dhm_p[] =
4174 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4175 const unsigned char dhm_g[] =
4176 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004177
Hanno Beckere2defad2021-07-24 05:59:17 +01004178 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4179 dhm_p, sizeof( dhm_p ),
4180 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4181 {
4182 return( ret );
4183 }
4184 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004185#endif
4186
Ronald Cron6f135e12021-12-08 16:57:54 +01004187#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker71f1ed62021-07-24 06:01:47 +01004188 /*
4189 * Allow all TLS 1.3 key exchange modes by default.
4190 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004191 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004192#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004193
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004194 /*
4195 * Preset-specific defaults
4196 */
4197 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004198 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004199 /*
4200 * NSA Suite B
4201 */
4202 case MBEDTLS_SSL_PRESET_SUITEB:
4203 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
4204 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
4205 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004206#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4207 /* Hybrid TLS 1.2/1.3 is not supported yet */
4208 conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
4209#else
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004210 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004211#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3 */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004212
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004213 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004214
4215#if defined(MBEDTLS_X509_CRT_PARSE_C)
4216 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004217#endif
4218
Gilles Peskineeccd8882020-03-10 12:19:08 +01004219#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004220#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004221 conf->sig_hashes = ssl_preset_suiteb_hashes;
Jerry Yuf017ee42022-01-12 15:49:48 +08004222#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004223#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4224 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4225 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4226 else
4227#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4228 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004229#endif
4230
Brett Warrene0edc842021-08-17 09:53:13 +01004231#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4232 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004233#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004234 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004235 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004236
4237 /*
4238 * Default
4239 */
4240 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03004241 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
4242 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
4243 MBEDTLS_SSL_MIN_MAJOR_VERSION :
4244 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
4245 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
4246 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
4247 MBEDTLS_SSL_MIN_MINOR_VERSION :
4248 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004249 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004250#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4251 /* Hybrid TLS 1.2/1.3 is not supported yet */
4252 conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
4253#else
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004254 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004255#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3 */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004256
4257#if defined(MBEDTLS_SSL_PROTO_DTLS)
4258 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
TRodziewiczef73f012021-05-13 14:53:36 +02004259 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004260#endif
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004261 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004262
4263#if defined(MBEDTLS_X509_CRT_PARSE_C)
4264 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4265#endif
4266
Gilles Peskineeccd8882020-03-10 12:19:08 +01004267#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004268#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01004269 conf->sig_hashes = ssl_preset_default_hashes;
Jerry Yuf017ee42022-01-12 15:49:48 +08004270#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004271#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4272 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4273 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4274 else
4275#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4276 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004277#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004278
Brett Warrene0edc842021-08-17 09:53:13 +01004279#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4280 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004281#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004282 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004283
4284#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4285 conf->dhm_min_bitlen = 1024;
4286#endif
4287 }
4288
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004289 return( 0 );
4290}
4291
4292/*
4293 * Free mbedtls_ssl_config
4294 */
4295void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4296{
4297#if defined(MBEDTLS_DHM_C)
4298 mbedtls_mpi_free( &conf->dhm_P );
4299 mbedtls_mpi_free( &conf->dhm_G );
4300#endif
4301
Gilles Peskineeccd8882020-03-10 12:19:08 +01004302#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004303 if( conf->psk != NULL )
4304 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004305 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004306 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004307 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004308 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004309 }
4310
4311 if( conf->psk_identity != NULL )
4312 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004313 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004314 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004315 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004316 conf->psk_identity_len = 0;
4317 }
4318#endif
4319
4320#if defined(MBEDTLS_X509_CRT_PARSE_C)
4321 ssl_key_cert_free( conf->key_cert );
4322#endif
4323
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004324 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004325}
4326
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004327#if defined(MBEDTLS_PK_C) && \
4328 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004329/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004330 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004332unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004333{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004334#if defined(MBEDTLS_RSA_C)
4335 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4336 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004337#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004338#if defined(MBEDTLS_ECDSA_C)
4339 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4340 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004341#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004342 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004343}
4344
Hanno Becker7e5437a2017-04-28 17:15:26 +01004345unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4346{
4347 switch( type ) {
4348 case MBEDTLS_PK_RSA:
4349 return( MBEDTLS_SSL_SIG_RSA );
4350 case MBEDTLS_PK_ECDSA:
4351 case MBEDTLS_PK_ECKEY:
4352 return( MBEDTLS_SSL_SIG_ECDSA );
4353 default:
4354 return( MBEDTLS_SSL_SIG_ANON );
4355 }
4356}
4357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004358mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004359{
4360 switch( sig )
4361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004362#if defined(MBEDTLS_RSA_C)
4363 case MBEDTLS_SSL_SIG_RSA:
4364 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004365#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004366#if defined(MBEDTLS_ECDSA_C)
4367 case MBEDTLS_SSL_SIG_ECDSA:
4368 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004369#endif
4370 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004371 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004372 }
4373}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004374#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004375
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004376/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004377 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004378 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004379mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004380{
4381 switch( hash )
4382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004383#if defined(MBEDTLS_MD5_C)
4384 case MBEDTLS_SSL_HASH_MD5:
4385 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004386#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004387#if defined(MBEDTLS_SHA1_C)
4388 case MBEDTLS_SSL_HASH_SHA1:
4389 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004390#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004391#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004392 case MBEDTLS_SSL_HASH_SHA224:
4393 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004394#endif
4395#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004396 case MBEDTLS_SSL_HASH_SHA256:
4397 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004398#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004399#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004400 case MBEDTLS_SSL_HASH_SHA384:
4401 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004402#endif
4403#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004404 case MBEDTLS_SSL_HASH_SHA512:
4405 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004406#endif
4407 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004408 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004409 }
4410}
4411
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004412/*
4413 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4414 */
4415unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4416{
4417 switch( md )
4418 {
4419#if defined(MBEDTLS_MD5_C)
4420 case MBEDTLS_MD_MD5:
4421 return( MBEDTLS_SSL_HASH_MD5 );
4422#endif
4423#if defined(MBEDTLS_SHA1_C)
4424 case MBEDTLS_MD_SHA1:
4425 return( MBEDTLS_SSL_HASH_SHA1 );
4426#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004427#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004428 case MBEDTLS_MD_SHA224:
4429 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004430#endif
4431#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004432 case MBEDTLS_MD_SHA256:
4433 return( MBEDTLS_SSL_HASH_SHA256 );
4434#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004435#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004436 case MBEDTLS_MD_SHA384:
4437 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004438#endif
4439#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004440 case MBEDTLS_MD_SHA512:
4441 return( MBEDTLS_SSL_HASH_SHA512 );
4442#endif
4443 default:
4444 return( MBEDTLS_SSL_HASH_NONE );
4445 }
4446}
4447
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004448/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004449 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004450 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004451 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004452int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004453{
Brett Warrene0edc842021-08-17 09:53:13 +01004454 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004455
Brett Warrene0edc842021-08-17 09:53:13 +01004456 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004457 return( -1 );
4458
Brett Warrene0edc842021-08-17 09:53:13 +01004459 for( ; *group_list != 0; group_list++ )
4460 {
4461 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004462 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01004463 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004464
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004465 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004466}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004467
4468#if defined(MBEDTLS_ECP_C)
4469/*
4470 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
4471 */
4472int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
4473{
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01004474 uint16_t tls_id = mbedtls_ecp_curve_info_from_grp_id( grp_id )->tls_id;
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004475 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
4476}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004477#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004479#if defined(MBEDTLS_X509_CRT_PARSE_C)
4480int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
4481 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004482 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02004483 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004484{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004485 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004486 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004487 const char *ext_oid;
4488 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004490 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004491 {
4492 /* Server part of the key exchange */
4493 switch( ciphersuite->key_exchange )
4494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004495 case MBEDTLS_KEY_EXCHANGE_RSA:
4496 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004497 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004498 break;
4499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004500 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
4501 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
4502 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
4503 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004504 break;
4505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004506 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
4507 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004508 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004509 break;
4510
4511 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004512 case MBEDTLS_KEY_EXCHANGE_NONE:
4513 case MBEDTLS_KEY_EXCHANGE_PSK:
4514 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
4515 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02004516 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004517 usage = 0;
4518 }
4519 }
4520 else
4521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004522 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
4523 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004524 }
4525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004526 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004527 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004528 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004529 ret = -1;
4530 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004532 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004534 ext_oid = MBEDTLS_OID_SERVER_AUTH;
4535 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004536 }
4537 else
4538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004539 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
4540 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004541 }
4542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004543 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004544 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004545 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004546 ret = -1;
4547 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004548
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004549 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004550}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004551#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004552
Jerry Yu148165c2021-09-24 23:20:59 +08004553#if defined(MBEDTLS_USE_PSA_CRYPTO)
4554int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
4555 const mbedtls_md_type_t md,
4556 unsigned char *dst,
4557 size_t dst_len,
4558 size_t *olen )
4559{
Ronald Cronf6893e12022-01-07 22:09:01 +01004560 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4561 psa_hash_operation_t *hash_operation_to_clone;
4562 psa_hash_operation_t hash_operation = psa_hash_operation_init();
4563
Jerry Yu148165c2021-09-24 23:20:59 +08004564 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01004565
4566 switch( md )
4567 {
4568#if defined(MBEDTLS_SHA384_C)
4569 case MBEDTLS_MD_SHA384:
4570 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
4571 break;
4572#endif
4573
4574#if defined(MBEDTLS_SHA256_C)
4575 case MBEDTLS_MD_SHA256:
4576 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
4577 break;
4578#endif
4579
4580 default:
4581 goto exit;
4582 }
4583
4584 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
4585 if( status != PSA_SUCCESS )
4586 goto exit;
4587
4588 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
4589 if( status != PSA_SUCCESS )
4590 goto exit;
4591
4592exit:
Ronald Cronb788c042022-02-15 09:14:15 +01004593 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08004594}
4595#else /* MBEDTLS_USE_PSA_CRYPTO */
4596
Jerry Yu24c0ec32021-09-09 14:21:07 +08004597#if defined(MBEDTLS_SHA384_C)
Jerry Yu000f9762021-09-14 11:12:51 +08004598static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
4599 unsigned char *dst,
4600 size_t dst_len,
4601 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004602{
Jerry Yu24c0ec32021-09-09 14:21:07 +08004603 int ret;
4604 mbedtls_sha512_context sha512;
4605
4606 if( dst_len < 48 )
4607 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4608
4609 mbedtls_sha512_init( &sha512 );
4610 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
4611
4612 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
4613 {
4614 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
4615 goto exit;
4616 }
4617
4618 *olen = 48;
4619
4620exit:
4621
4622 mbedtls_sha512_free( &sha512 );
4623 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004624}
4625#endif /* MBEDTLS_SHA384_C */
4626
4627#if defined(MBEDTLS_SHA256_C)
Jerry Yu000f9762021-09-14 11:12:51 +08004628static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
4629 unsigned char *dst,
4630 size_t dst_len,
4631 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004632{
Jerry Yu24c0ec32021-09-09 14:21:07 +08004633 int ret;
4634 mbedtls_sha256_context sha256;
4635
4636 if( dst_len < 32 )
4637 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4638
4639 mbedtls_sha256_init( &sha256 );
4640 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08004641
Jerry Yu24c0ec32021-09-09 14:21:07 +08004642 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
4643 {
4644 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
4645 goto exit;
4646 }
4647
4648 *olen = 32;
4649
4650exit:
4651
4652 mbedtls_sha256_free( &sha256 );
4653 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004654}
4655#endif /* MBEDTLS_SHA256_C */
4656
Jerry Yu000f9762021-09-14 11:12:51 +08004657int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
4658 const mbedtls_md_type_t md,
4659 unsigned char *dst,
4660 size_t dst_len,
4661 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004662{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004663 switch( md )
4664 {
4665
Jerry Yu24c0ec32021-09-09 14:21:07 +08004666#if defined(MBEDTLS_SHA384_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004667 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08004668 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08004669#endif /* MBEDTLS_SHA384_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004670
Jerry Yu24c0ec32021-09-09 14:21:07 +08004671#if defined(MBEDTLS_SHA256_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004672 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08004673 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08004674#endif /* MBEDTLS_SHA256_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004675
4676 default:
4677 break;
4678 }
Jerry Yuc5aef882021-12-23 20:15:02 +08004679 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004680}
XiaokangQian647719a2021-12-07 09:16:29 +00004681
Jerry Yu148165c2021-09-24 23:20:59 +08004682#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08004683
Jerry Yu1ea9d102021-12-21 13:41:49 +08004684#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) || \
4685 defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4686 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jerry Yuba073422021-12-20 22:22:15 +08004687/*
Jerry Yub925f212022-01-12 11:17:02 +08004688 * Function for writing a supported groups (TLS 1.3) or supported elliptic
4689 * curves (TLS 1.2) extension.
Jerry Yuba073422021-12-20 22:22:15 +08004690 *
Jerry Yub925f212022-01-12 11:17:02 +08004691 * The "extension_data" field of a supported groups extension contains a
4692 * "NamedGroupList" value (TLS 1.3 RFC8446):
Jerry Yuba073422021-12-20 22:22:15 +08004693 * enum {
4694 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
4695 * x25519(0x001D), x448(0x001E),
4696 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
4697 * ffdhe6144(0x0103), ffdhe8192(0x0104),
4698 * ffdhe_private_use(0x01FC..0x01FF),
4699 * ecdhe_private_use(0xFE00..0xFEFF),
4700 * (0xFFFF)
4701 * } NamedGroup;
4702 * struct {
4703 * NamedGroup named_group_list<2..2^16-1>;
4704 * } NamedGroupList;
Jerry Yub925f212022-01-12 11:17:02 +08004705 *
4706 * The "extension_data" field of a supported elliptic curves extension contains
4707 * a "NamedCurveList" value (TLS 1.2 RFC 8422):
Jerry Yu63282b42022-01-11 15:43:53 +08004708 * enum {
4709 * deprecated(1..22),
4710 * secp256r1 (23), secp384r1 (24), secp521r1 (25),
4711 * x25519(29), x448(30),
4712 * reserved (0xFE00..0xFEFF),
4713 * deprecated(0xFF01..0xFF02),
4714 * (0xFFFF)
4715 * } NamedCurve;
4716 * struct {
4717 * NamedCurve named_curve_list<2..2^16-1>
4718 * } NamedCurveList;
4719 *
Jerry Yub925f212022-01-12 11:17:02 +08004720 * The TLS 1.3 supported groups extension was defined to be a compatible
4721 * generalization of the TLS 1.2 supported elliptic curves extension. They both
4722 * share the same extension identifier.
Jerry Yu63282b42022-01-11 15:43:53 +08004723 *
Jerry Yub925f212022-01-12 11:17:02 +08004724 * DHE groups are not supported yet.
Jerry Yuba073422021-12-20 22:22:15 +08004725 */
Jerry Yuba073422021-12-20 22:22:15 +08004726int mbedtls_ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl,
4727 unsigned char *buf,
Jerry Yu17532612021-12-20 22:32:09 +08004728 const unsigned char *end,
Jerry Yuba073422021-12-20 22:22:15 +08004729 size_t *out_len )
4730{
4731 unsigned char *p = buf ;
4732 unsigned char *named_group_list; /* Start of named_group_list */
4733 size_t named_group_list_len; /* Length of named_group_list */
4734 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
4735
4736 *out_len = 0;
Jerry Yuf46b0162022-01-11 16:28:00 +08004737
Jerry Yuba073422021-12-20 22:22:15 +08004738 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) );
4739
4740 /* Check if we have space for header and length fields:
4741 * - extension_type (2 bytes)
4742 * - extension_data_length (2 bytes)
4743 * - named_group_list_length (2 bytes)
4744 */
4745 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
4746 p += 6;
4747
4748 named_group_list = p;
4749
4750 if( group_list == NULL )
4751 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
4752
Jerry Yu1510cea2022-01-12 10:56:49 +08004753 for( ; *group_list != 0; group_list++ )
Jerry Yuba073422021-12-20 22:22:15 +08004754 {
Jerry Yu1510cea2022-01-12 10:56:49 +08004755 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got supported group(%04x)", *group_list ) );
Jerry Yu63282b42022-01-11 15:43:53 +08004756
Jerry Yu1ea9d102021-12-21 13:41:49 +08004757#if defined(MBEDTLS_ECP_C)
Jerry Yu1510cea2022-01-12 10:56:49 +08004758 if( ( mbedtls_ssl_conf_is_tls13_enabled( ssl->conf ) &&
Jerry Yu3ad14ac2022-01-11 17:13:16 +08004759 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) ) ||
Jerry Yu1510cea2022-01-12 10:56:49 +08004760 ( mbedtls_ssl_conf_is_tls12_enabled( ssl->conf ) &&
Jerry Yu3ad14ac2022-01-11 17:13:16 +08004761 mbedtls_ssl_tls12_named_group_is_ecdhe( *group_list ) ) )
Jerry Yuba073422021-12-20 22:22:15 +08004762 {
Jerry Yu3ad14ac2022-01-11 17:13:16 +08004763 const mbedtls_ecp_curve_info *curve_info;
4764 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
Jerry Yub925f212022-01-12 11:17:02 +08004765 if( curve_info == NULL )
Jerry Yu3ad14ac2022-01-11 17:13:16 +08004766 continue;
Jerry Yub925f212022-01-12 11:17:02 +08004767 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
4768 MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 );
4769 p += 2;
4770 MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )",
4771 curve_info->name, *group_list ) );
Jerry Yuba073422021-12-20 22:22:15 +08004772 }
Jerry Yu63282b42022-01-11 15:43:53 +08004773#endif /* MBEDTLS_ECP_C */
4774 /* Add DHE groups here */
Jerry Yuba073422021-12-20 22:22:15 +08004775
4776 }
4777
Jerry Yu1510cea2022-01-12 10:56:49 +08004778 /* Length of named_group_list */
Jerry Yuba073422021-12-20 22:22:15 +08004779 named_group_list_len = p - named_group_list;
4780 if( named_group_list_len == 0 )
4781 {
4782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) );
4783 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4784 }
4785
4786 /* Write extension_type */
4787 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 );
4788 /* Write extension_data_length */
4789 MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 );
4790 /* Write length of named_group_list */
4791 MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 );
4792
Jerry Yu7f029d82022-01-11 11:08:53 +08004793 MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension",
4794 buf + 4, named_group_list_len + 2 );
Jerry Yuba073422021-12-20 22:22:15 +08004795
4796 *out_len = p - buf;
4797
4798#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4799 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
4800#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4801
4802 return( 0 );
4803}
4804
Jerry Yu1ea9d102021-12-21 13:41:49 +08004805#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED ||
4806 MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
4807 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Jerry Yuba073422021-12-20 22:22:15 +08004808
Jerry Yuf017ee42022-01-12 15:49:48 +08004809#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
4810/*
Jerry Yu7ddc38c2022-01-19 11:08:05 +08004811 * Function for writing a signature algorithm extension.
Jerry Yuf017ee42022-01-12 15:49:48 +08004812 *
Jerry Yu8afd6e42022-01-20 15:54:26 +08004813 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
Jerry Yu7ddc38c2022-01-19 11:08:05 +08004814 * value (TLS 1.3 RFC8446):
4815 * enum {
4816 * ....
4817 * ecdsa_secp256r1_sha256( 0x0403 ),
4818 * ecdsa_secp384r1_sha384( 0x0503 ),
4819 * ecdsa_secp521r1_sha512( 0x0603 ),
4820 * ....
4821 * } SignatureScheme;
Jerry Yuf017ee42022-01-12 15:49:48 +08004822 *
Jerry Yu7ddc38c2022-01-19 11:08:05 +08004823 * struct {
4824 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
4825 * } SignatureSchemeList;
Jerry Yuf017ee42022-01-12 15:49:48 +08004826 *
Jerry Yu8afd6e42022-01-20 15:54:26 +08004827 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
4828 * value (TLS 1.2 RFC5246):
Jerry Yu7ddc38c2022-01-19 11:08:05 +08004829 * enum {
4830 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
4831 * sha512(6), (255)
4832 * } HashAlgorithm;
4833 *
4834 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
4835 * SignatureAlgorithm;
4836 *
4837 * struct {
4838 * HashAlgorithm hash;
4839 * SignatureAlgorithm signature;
4840 * } SignatureAndHashAlgorithm;
4841 *
4842 * SignatureAndHashAlgorithm
4843 * supported_signature_algorithms<2..2^16-2>;
4844 *
4845 * The TLS 1.3 signature algorithm extension was defined to be a compatible
4846 * generalization of the TLS 1.2 signature algorithm extension.
4847 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
4848 * `SignatureScheme` field of TLS 1.3
4849 *
Jerry Yuf017ee42022-01-12 15:49:48 +08004850 */
4851int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
4852 const unsigned char *end, size_t *out_len )
4853{
4854 unsigned char *p = buf;
4855 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
4856 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
4857
4858 *out_len = 0;
4859
4860 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
4861
4862 /* Check if we have space for header and length field:
4863 * - extension_type (2 bytes)
4864 * - extension_data_length (2 bytes)
4865 * - supported_signature_algorithms_length (2 bytes)
4866 */
4867 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
4868 p += 6;
4869
4870 /*
4871 * Write supported_signature_algorithms
4872 */
4873 supported_sig_alg = p;
Jerry Yu6106fdc2022-01-12 16:36:14 +08004874 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
4875 if( sig_alg == NULL )
4876 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
4877
4878 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
Jerry Yuf017ee42022-01-12 15:49:48 +08004879 {
Jerry Yu1bab3012022-01-19 17:43:22 +08004880 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
4881 continue;
Jerry Yuf017ee42022-01-12 15:49:48 +08004882 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
4883 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
4884 p += 2;
4885 MBEDTLS_SSL_DEBUG_MSG( 3, ( "signature scheme [%x]", *sig_alg ) );
4886 }
4887
4888 /* Length of supported_signature_algorithms */
4889 supported_sig_alg_len = p - supported_sig_alg;
4890 if( supported_sig_alg_len == 0 )
4891 {
4892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
4893 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4894 }
4895
4896 /* Write extension_type */
4897 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
4898 /* Write extension_data_length */
4899 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
4900 /* Write length of supported_signature_algorithms */
4901 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
4902
4903 /* Output the total length of signature algorithms extension. */
4904 *out_len = p - buf;
4905
4906#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4907 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
4908#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4909 return( 0 );
4910}
4911#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yuc73c6182022-02-08 20:29:25 +08004912
Jerry Yuc5aef882021-12-23 20:15:02 +08004913#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4914int mbedtls_ssl_write_hostname_ext( mbedtls_ssl_context *ssl,
4915 unsigned char *buf,
4916 const unsigned char *end,
4917 size_t *olen )
4918{
4919 unsigned char *p = buf;
4920 size_t hostname_len;
4921
4922 *olen = 0;
4923
4924 if( ssl->hostname == NULL )
4925 return( 0 );
4926
4927 MBEDTLS_SSL_DEBUG_MSG( 3,
4928 ( "client hello, adding server name extension: %s",
4929 ssl->hostname ) );
4930
4931 hostname_len = strlen( ssl->hostname );
4932
4933 MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 );
4934
4935 /*
4936 * Sect. 3, RFC 6066 (TLS Extensions Definitions)
4937 *
4938 * In order to provide any of the server names, clients MAY include an
4939 * extension of type "server_name" in the (extended) client hello. The
4940 * "extension_data" field of this extension SHALL contain
4941 * "ServerNameList" where:
4942 *
4943 * struct {
4944 * NameType name_type;
4945 * select (name_type) {
4946 * case host_name: HostName;
4947 * } name;
4948 * } ServerName;
4949 *
4950 * enum {
4951 * host_name(0), (255)
4952 * } NameType;
4953 *
4954 * opaque HostName<1..2^16-1>;
4955 *
4956 * struct {
4957 * ServerName server_name_list<1..2^16-1>
4958 * } ServerNameList;
4959 *
4960 */
4961 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SERVERNAME, p, 0 );
4962 p += 2;
4963
4964 MBEDTLS_PUT_UINT16_BE( hostname_len + 5, p, 0 );
4965 p += 2;
4966
4967 MBEDTLS_PUT_UINT16_BE( hostname_len + 3, p, 0 );
4968 p += 2;
4969
4970 *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME );
4971
4972 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
4973 p += 2;
4974
4975 memcpy( p, ssl->hostname, hostname_len );
4976
4977 *olen = hostname_len + 9;
4978
4979 return( 0 );
4980}
4981#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Jerry Yuf017ee42022-01-12 15:49:48 +08004982
Jerry Yudc7bd172022-02-17 13:44:15 +08004983#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4984
4985#if defined(MBEDTLS_USE_PSA_CRYPTO)
4986
4987static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
4988 mbedtls_svc_key_id_t key,
4989 psa_algorithm_t alg,
4990 const unsigned char* seed, size_t seed_length,
4991 const unsigned char* label, size_t label_length,
4992 size_t capacity )
4993{
4994 psa_status_t status;
4995
4996 status = psa_key_derivation_setup( derivation, alg );
4997 if( status != PSA_SUCCESS )
4998 return( status );
4999
5000 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5001 {
5002 status = psa_key_derivation_input_bytes( derivation,
5003 PSA_KEY_DERIVATION_INPUT_SEED,
5004 seed, seed_length );
5005 if( status != PSA_SUCCESS )
5006 return( status );
5007
5008 if( mbedtls_svc_key_id_is_null( key ) )
5009 {
5010 status = psa_key_derivation_input_bytes(
5011 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
5012 NULL, 0 );
5013 }
5014 else
5015 {
5016 status = psa_key_derivation_input_key(
5017 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5018 }
5019 if( status != PSA_SUCCESS )
5020 return( status );
5021
5022 status = psa_key_derivation_input_bytes( derivation,
5023 PSA_KEY_DERIVATION_INPUT_LABEL,
5024 label, label_length );
5025 if( status != PSA_SUCCESS )
5026 return( status );
5027 }
5028 else
5029 {
5030 return( PSA_ERROR_NOT_SUPPORTED );
5031 }
5032
5033 status = psa_key_derivation_set_capacity( derivation, capacity );
5034 if( status != PSA_SUCCESS )
5035 return( status );
5036
5037 return( PSA_SUCCESS );
5038}
5039
5040static int tls_prf_generic( mbedtls_md_type_t md_type,
5041 const unsigned char *secret, size_t slen,
5042 const char *label,
5043 const unsigned char *random, size_t rlen,
5044 unsigned char *dstbuf, size_t dlen )
5045{
5046 psa_status_t status;
5047 psa_algorithm_t alg;
5048 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5049 psa_key_derivation_operation_t derivation =
5050 PSA_KEY_DERIVATION_OPERATION_INIT;
5051
5052 if( md_type == MBEDTLS_MD_SHA384 )
5053 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5054 else
5055 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5056
5057 /* Normally a "secret" should be long enough to be impossible to
5058 * find by brute force, and in particular should not be empty. But
5059 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5060 * and for this use case it makes sense to have a 0-length "secret".
5061 * Since the key API doesn't allow importing a key of length 0,
5062 * keep master_key=0, which setup_psa_key_derivation() understands
5063 * to mean a 0-length "secret" input. */
5064 if( slen != 0 )
5065 {
5066 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5067 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5068 psa_set_key_algorithm( &key_attributes, alg );
5069 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5070
5071 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5072 if( status != PSA_SUCCESS )
5073 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5074 }
5075
5076 status = setup_psa_key_derivation( &derivation,
5077 master_key, alg,
5078 random, rlen,
5079 (unsigned char const *) label,
5080 (size_t) strlen( label ),
5081 dlen );
5082 if( status != PSA_SUCCESS )
5083 {
5084 psa_key_derivation_abort( &derivation );
5085 psa_destroy_key( master_key );
5086 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5087 }
5088
5089 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5090 if( status != PSA_SUCCESS )
5091 {
5092 psa_key_derivation_abort( &derivation );
5093 psa_destroy_key( master_key );
5094 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5095 }
5096
5097 status = psa_key_derivation_abort( &derivation );
5098 if( status != PSA_SUCCESS )
5099 {
5100 psa_destroy_key( master_key );
5101 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5102 }
5103
5104 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5105 status = psa_destroy_key( master_key );
5106 if( status != PSA_SUCCESS )
5107 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5108
5109 return( 0 );
5110}
5111
5112#else /* MBEDTLS_USE_PSA_CRYPTO */
5113
5114static int tls_prf_generic( mbedtls_md_type_t md_type,
5115 const unsigned char *secret, size_t slen,
5116 const char *label,
5117 const unsigned char *random, size_t rlen,
5118 unsigned char *dstbuf, size_t dlen )
5119{
5120 size_t nb;
5121 size_t i, j, k, md_len;
5122 unsigned char *tmp;
5123 size_t tmp_len = 0;
5124 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5125 const mbedtls_md_info_t *md_info;
5126 mbedtls_md_context_t md_ctx;
5127 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5128
5129 mbedtls_md_init( &md_ctx );
5130
5131 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5132 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5133
5134 md_len = mbedtls_md_get_size( md_info );
5135
5136 tmp_len = md_len + strlen( label ) + rlen;
5137 tmp = mbedtls_calloc( 1, tmp_len );
5138 if( tmp == NULL )
5139 {
5140 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5141 goto exit;
5142 }
5143
5144 nb = strlen( label );
5145 memcpy( tmp + md_len, label, nb );
5146 memcpy( tmp + md_len + nb, random, rlen );
5147 nb += rlen;
5148
5149 /*
5150 * Compute P_<hash>(secret, label + random)[0..dlen]
5151 */
5152 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5153 goto exit;
5154
5155 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5156 if( ret != 0 )
5157 goto exit;
5158 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5159 if( ret != 0 )
5160 goto exit;
5161 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5162 if( ret != 0 )
5163 goto exit;
5164
5165 for( i = 0; i < dlen; i += md_len )
5166 {
5167 ret = mbedtls_md_hmac_reset ( &md_ctx );
5168 if( ret != 0 )
5169 goto exit;
5170 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5171 if( ret != 0 )
5172 goto exit;
5173 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5174 if( ret != 0 )
5175 goto exit;
5176
5177 ret = mbedtls_md_hmac_reset ( &md_ctx );
5178 if( ret != 0 )
5179 goto exit;
5180 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5181 if( ret != 0 )
5182 goto exit;
5183 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5184 if( ret != 0 )
5185 goto exit;
5186
5187 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5188
5189 for( j = 0; j < k; j++ )
5190 dstbuf[i + j] = h_i[j];
5191 }
5192
5193exit:
5194 mbedtls_md_free( &md_ctx );
5195
5196 mbedtls_platform_zeroize( tmp, tmp_len );
5197 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5198
5199 mbedtls_free( tmp );
5200
5201 return( ret );
5202}
5203#endif /* MBEDTLS_USE_PSA_CRYPTO */
5204
5205#if defined(MBEDTLS_SHA256_C)
5206static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5207 const char *label,
5208 const unsigned char *random, size_t rlen,
5209 unsigned char *dstbuf, size_t dlen )
5210{
5211 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5212 label, random, rlen, dstbuf, dlen ) );
5213}
5214#endif /* MBEDTLS_SHA256_C */
5215
5216#if defined(MBEDTLS_SHA384_C)
5217static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5218 const char *label,
5219 const unsigned char *random, size_t rlen,
5220 unsigned char *dstbuf, size_t dlen )
5221{
5222 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5223 label, random, rlen, dstbuf, dlen ) );
5224}
5225#endif /* MBEDTLS_SHA384_C */
5226
Jerry Yuf009d862022-02-17 14:01:37 +08005227/*
5228 * Set appropriate PRF function and other SSL / TLS1.2 functions
5229 *
5230 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005231 * - hash associated with the ciphersuite (only used by TLS 1.2)
5232 *
5233 * Outputs:
5234 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5235 */
5236static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005237 mbedtls_md_type_t hash )
5238{
Jerry Yuf009d862022-02-17 14:01:37 +08005239#if defined(MBEDTLS_SHA384_C)
Ronald Cron81591aa2022-03-07 09:05:51 +01005240 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005241 {
5242 handshake->tls_prf = tls_prf_sha384;
5243 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5244 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5245 }
5246 else
5247#endif
5248#if defined(MBEDTLS_SHA256_C)
Jerry Yuf009d862022-02-17 14:01:37 +08005249 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005250 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005251 handshake->tls_prf = tls_prf_sha256;
5252 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5253 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5254 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005255#else
Jerry Yuf009d862022-02-17 14:01:37 +08005256 {
Jerry Yuf009d862022-02-17 14:01:37 +08005257 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005258 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005259 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5260 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005261#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005262
5263 return( 0 );
5264}
Jerry Yud6ab2352022-02-17 14:03:43 +08005265
5266#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
5267 defined(MBEDTLS_USE_PSA_CRYPTO)
5268static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
5269{
5270 if( ssl->conf->f_psk != NULL )
5271 {
5272 /* If we've used a callback to select the PSK,
5273 * the static configuration is irrelevant. */
5274 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
5275 return( 1 );
5276
5277 return( 0 );
5278 }
5279
5280 if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) )
5281 return( 1 );
5282
5283 return( 0 );
5284}
5285#endif /* MBEDTLS_USE_PSA_CRYPTO &&
5286 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5287
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005288/*
5289 * Compute master secret if needed
5290 *
5291 * Parameters:
5292 * [in/out] handshake
5293 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5294 * (PSA-PSK) ciphersuite_info, psk_opaque
5295 * [out] premaster (cleared)
5296 * [out] master
5297 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5298 * debug: conf->f_dbg, conf->p_dbg
5299 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005300 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005301 */
5302static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5303 unsigned char *master,
5304 const mbedtls_ssl_context *ssl )
5305{
5306 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5307
5308 /* cf. RFC 5246, Section 8.1:
5309 * "The master secret is always exactly 48 bytes in length." */
5310 size_t const master_secret_len = 48;
5311
5312#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5313 unsigned char session_hash[48];
5314#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5315
5316 /* The label for the KDF used for key expansion.
5317 * This is either "master secret" or "extended master secret"
5318 * depending on whether the Extended Master Secret extension
5319 * is used. */
5320 char const *lbl = "master secret";
5321
5322 /* The salt for the KDF used for key expansion.
5323 * - If the Extended Master Secret extension is not used,
5324 * this is ClientHello.Random + ServerHello.Random
5325 * (see Sect. 8.1 in RFC 5246).
5326 * - If the Extended Master Secret extension is used,
5327 * this is the transcript of the handshake so far.
5328 * (see Sect. 4 in RFC 7627). */
5329 unsigned char const *salt = handshake->randbytes;
5330 size_t salt_len = 64;
5331
5332#if !defined(MBEDTLS_DEBUG_C) && \
5333 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5334 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5335 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5336 ssl = NULL; /* make sure we don't use it except for those cases */
5337 (void) ssl;
5338#endif
5339
5340 if( handshake->resume != 0 )
5341 {
5342 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5343 return( 0 );
5344 }
5345
5346#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5347 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5348 {
5349 lbl = "extended master secret";
5350 salt = session_hash;
5351 handshake->calc_verify( ssl, session_hash, &salt_len );
5352
5353 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
5354 session_hash, salt_len );
5355 }
5356#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
5357
5358#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
5359 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5360 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005361 ssl_use_opaque_psk( ssl ) == 1 )
5362 {
5363 /* Perform PSK-to-MS expansion in a single step. */
5364 psa_status_t status;
5365 psa_algorithm_t alg;
5366 mbedtls_svc_key_id_t psk;
5367 psa_key_derivation_operation_t derivation =
5368 PSA_KEY_DERIVATION_OPERATION_INIT;
5369 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5370
5371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5372
5373 psk = mbedtls_ssl_get_opaque_psk( ssl );
5374
5375 if( hash_alg == MBEDTLS_MD_SHA384 )
5376 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5377 else
5378 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5379
5380 status = setup_psa_key_derivation( &derivation, psk, alg,
5381 salt, salt_len,
5382 (unsigned char const *) lbl,
5383 (size_t) strlen( lbl ),
5384 master_secret_len );
5385 if( status != PSA_SUCCESS )
5386 {
5387 psa_key_derivation_abort( &derivation );
5388 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5389 }
5390
5391 status = psa_key_derivation_output_bytes( &derivation,
5392 master,
5393 master_secret_len );
5394 if( status != PSA_SUCCESS )
5395 {
5396 psa_key_derivation_abort( &derivation );
5397 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5398 }
5399
5400 status = psa_key_derivation_abort( &derivation );
5401 if( status != PSA_SUCCESS )
5402 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5403 }
5404 else
5405#endif
5406 {
5407 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
5408 lbl, salt, salt_len,
5409 master,
5410 master_secret_len );
5411 if( ret != 0 )
5412 {
5413 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5414 return( ret );
5415 }
5416
5417 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5418 handshake->premaster,
5419 handshake->pmslen );
5420
5421 mbedtls_platform_zeroize( handshake->premaster,
5422 sizeof(handshake->premaster) );
5423 }
5424
5425 return( 0 );
5426}
5427
Jerry Yud62f87e2022-02-17 14:09:02 +08005428int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5429{
5430 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5431 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5432 ssl->handshake->ciphersuite_info;
5433
5434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5435
5436 /* Set PRF, calc_verify and calc_finished function pointers */
5437 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005438 ciphersuite_info->mac );
5439 if( ret != 0 )
5440 {
5441 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5442 return( ret );
5443 }
5444
5445 /* Compute master secret if needed */
5446 ret = ssl_compute_master( ssl->handshake,
5447 ssl->session_negotiate->master,
5448 ssl );
5449 if( ret != 0 )
5450 {
5451 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5452 return( ret );
5453 }
5454
5455 /* Swap the client and server random values:
5456 * - MS derivation wanted client+server (RFC 5246 8.1)
5457 * - key derivation wants server+client (RFC 5246 6.3) */
5458 {
5459 unsigned char tmp[64];
5460 memcpy( tmp, ssl->handshake->randbytes, 64 );
5461 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5462 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5463 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5464 }
5465
5466 /* Populate transform structure */
5467 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5468 ssl->session_negotiate->ciphersuite,
5469 ssl->session_negotiate->master,
5470#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
5471 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5472 ssl->session_negotiate->encrypt_then_mac,
5473#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
5474 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
5475 ssl->handshake->tls_prf,
5476 ssl->handshake->randbytes,
5477 ssl->minor_ver,
5478 ssl->conf->endpoint,
5479 ssl );
5480 if( ret != 0 )
5481 {
5482 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5483 return( ret );
5484 }
5485
5486 /* We no longer need Server/ClientHello.random values */
5487 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5488 sizeof( ssl->handshake->randbytes ) );
5489
5490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5491
5492 return( 0 );
5493}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005494
Ronald Cron4dcbca92022-03-07 10:21:40 +01005495int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5496{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005497 switch( md )
5498 {
5499#if defined(MBEDTLS_SHA384_C)
5500 case MBEDTLS_SSL_HASH_SHA384:
5501 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5502 break;
5503#endif
5504#if defined(MBEDTLS_SHA256_C)
5505 case MBEDTLS_SSL_HASH_SHA256:
5506 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5507 break;
5508#endif
5509 default:
5510 return( -1 );
5511 }
5512
Ronald Cronc2f13a02022-03-07 10:25:24 +01005513 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005514}
5515
Jerry Yu8392e0d2022-02-17 14:10:24 +08005516#if defined(MBEDTLS_SHA256_C)
5517void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5518 unsigned char *hash,
5519 size_t *hlen )
5520{
5521#if defined(MBEDTLS_USE_PSA_CRYPTO)
5522 size_t hash_size;
5523 psa_status_t status;
5524 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5525
5526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5527 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5528 if( status != PSA_SUCCESS )
5529 {
5530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5531 return;
5532 }
5533
5534 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5535 if( status != PSA_SUCCESS )
5536 {
5537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5538 return;
5539 }
5540
5541 *hlen = 32;
5542 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5544#else
5545 mbedtls_sha256_context sha256;
5546
5547 mbedtls_sha256_init( &sha256 );
5548
5549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5550
5551 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5552 mbedtls_sha256_finish( &sha256, hash );
5553
5554 *hlen = 32;
5555
5556 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5558
5559 mbedtls_sha256_free( &sha256 );
5560#endif /* MBEDTLS_USE_PSA_CRYPTO */
5561 return;
5562}
5563#endif /* MBEDTLS_SHA256_C */
5564
Jerry Yuc1cb3842022-02-17 14:13:48 +08005565#if defined(MBEDTLS_SHA384_C)
5566void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5567 unsigned char *hash,
5568 size_t *hlen )
5569{
5570#if defined(MBEDTLS_USE_PSA_CRYPTO)
5571 size_t hash_size;
5572 psa_status_t status;
5573 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5574
5575 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5576 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5577 if( status != PSA_SUCCESS )
5578 {
5579 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5580 return;
5581 }
5582
5583 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5584 if( status != PSA_SUCCESS )
5585 {
5586 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5587 return;
5588 }
5589
5590 *hlen = 48;
5591 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5593#else
5594 mbedtls_sha512_context sha512;
5595
5596 mbedtls_sha512_init( &sha512 );
5597
5598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
5599
5600 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
5601 mbedtls_sha512_finish( &sha512, hash );
5602
5603 *hlen = 48;
5604
5605 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5607
5608 mbedtls_sha512_free( &sha512 );
5609#endif /* MBEDTLS_USE_PSA_CRYPTO */
5610 return;
5611}
5612#endif /* MBEDTLS_SHA384_C */
5613
Jerry Yuce3dca42022-02-17 14:16:37 +08005614#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
5615int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
5616{
5617 unsigned char *p = ssl->handshake->premaster;
5618 unsigned char *end = p + sizeof( ssl->handshake->premaster );
5619 const unsigned char *psk = NULL;
5620 size_t psk_len = 0;
5621
5622 if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len )
5623 == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
5624 {
5625 /*
5626 * This should never happen because the existence of a PSK is always
5627 * checked before calling this function
5628 */
5629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5630 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5631 }
5632
5633 /*
5634 * PMS = struct {
5635 * opaque other_secret<0..2^16-1>;
5636 * opaque psk<0..2^16-1>;
5637 * };
5638 * with "other_secret" depending on the particular key exchange
5639 */
5640#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5641 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
5642 {
5643 if( end - p < 2 )
5644 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5645
5646 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5647 p += 2;
5648
5649 if( end < p || (size_t)( end - p ) < psk_len )
5650 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5651
5652 memset( p, 0, psk_len );
5653 p += psk_len;
5654 }
5655 else
5656#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5657#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
5658 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5659 {
5660 /*
5661 * other_secret already set by the ClientKeyExchange message,
5662 * and is 48 bytes long
5663 */
5664 if( end - p < 2 )
5665 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5666
5667 *p++ = 0;
5668 *p++ = 48;
5669 p += 48;
5670 }
5671 else
5672#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
5673#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
5674 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5675 {
5676 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5677 size_t len;
5678
5679 /* Write length only when we know the actual value */
5680 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
5681 p + 2, end - ( p + 2 ), &len,
5682 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5683 {
5684 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
5685 return( ret );
5686 }
5687 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
5688 p += 2 + len;
5689
5690 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
5691 }
5692 else
5693#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
5694#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
5695 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
5696 {
5697 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5698 size_t zlen;
5699
5700 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
5701 p + 2, end - ( p + 2 ),
5702 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5703 {
5704 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
5705 return( ret );
5706 }
5707
5708 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
5709 p += 2 + zlen;
5710
5711 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
5712 MBEDTLS_DEBUG_ECDH_Z );
5713 }
5714 else
5715#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
5716 {
5717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5718 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5719 }
5720
5721 /* opaque psk<0..2^16-1>; */
5722 if( end - p < 2 )
5723 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5724
5725 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5726 p += 2;
5727
5728 if( end < p || (size_t)( end - p ) < psk_len )
5729 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5730
5731 memcpy( p, psk, psk_len );
5732 p += psk_len;
5733
5734 ssl->handshake->pmslen = p - ssl->handshake->premaster;
5735
5736 return( 0 );
5737}
5738#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08005739
5740#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
5741static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
5742
5743#if defined(MBEDTLS_SSL_PROTO_DTLS)
5744int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
5745{
5746 /* If renegotiation is not enforced, retransmit until we would reach max
5747 * timeout if we were using the usual handshake doubling scheme */
5748 if( ssl->conf->renego_max_records < 0 )
5749 {
5750 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
5751 unsigned char doublings = 1;
5752
5753 while( ratio != 0 )
5754 {
5755 ++doublings;
5756 ratio >>= 1;
5757 }
5758
5759 if( ++ssl->renego_records_seen > doublings )
5760 {
5761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
5762 return( 0 );
5763 }
5764 }
5765
5766 return( ssl_write_hello_request( ssl ) );
5767}
5768#endif
5769#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08005770
Jerry Yud9526692022-02-17 14:23:47 +08005771/*
5772 * Handshake functions
5773 */
5774#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5775/* No certificate support -> dummy functions */
5776int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
5777{
5778 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5779 ssl->handshake->ciphersuite_info;
5780
5781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
5782
5783 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5784 {
5785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5786 ssl->state++;
5787 return( 0 );
5788 }
5789
5790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5791 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5792}
5793
5794int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5795{
5796 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5797 ssl->handshake->ciphersuite_info;
5798
5799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5800
5801 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5802 {
5803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5804 ssl->state++;
5805 return( 0 );
5806 }
5807
5808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5809 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5810}
5811
5812#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5813/* Some certificate support -> implement write and parse */
5814
5815int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
5816{
5817 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
5818 size_t i, n;
5819 const mbedtls_x509_crt *crt;
5820 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5821 ssl->handshake->ciphersuite_info;
5822
5823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
5824
5825 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5826 {
5827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5828 ssl->state++;
5829 return( 0 );
5830 }
5831
5832#if defined(MBEDTLS_SSL_CLI_C)
5833 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5834 {
5835 if( ssl->handshake->client_auth == 0 )
5836 {
5837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5838 ssl->state++;
5839 return( 0 );
5840 }
5841 }
5842#endif /* MBEDTLS_SSL_CLI_C */
5843#if defined(MBEDTLS_SSL_SRV_C)
5844 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
5845 {
5846 if( mbedtls_ssl_own_cert( ssl ) == NULL )
5847 {
5848 /* Should never happen because we shouldn't have picked the
5849 * ciphersuite if we don't have a certificate. */
5850 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5851 }
5852 }
5853#endif
5854
5855 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
5856
5857 /*
5858 * 0 . 0 handshake type
5859 * 1 . 3 handshake length
5860 * 4 . 6 length of all certs
5861 * 7 . 9 length of cert. 1
5862 * 10 . n-1 peer certificate
5863 * n . n+2 length of cert. 2
5864 * n+3 . ... upper level cert, etc.
5865 */
5866 i = 7;
5867 crt = mbedtls_ssl_own_cert( ssl );
5868
5869 while( crt != NULL )
5870 {
5871 n = crt->raw.len;
5872 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
5873 {
5874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
5875 " > %" MBEDTLS_PRINTF_SIZET,
5876 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
5877 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5878 }
5879
5880 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
5881 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
5882 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
5883
5884 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5885 i += n; crt = crt->next;
5886 }
5887
5888 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
5889 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
5890 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
5891
5892 ssl->out_msglen = i;
5893 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5894 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
5895
5896 ssl->state++;
5897
5898 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
5899 {
5900 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
5901 return( ret );
5902 }
5903
5904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
5905
5906 return( ret );
5907}
5908
5909#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5910
5911#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5912static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5913 unsigned char *crt_buf,
5914 size_t crt_buf_len )
5915{
5916 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5917
5918 if( peer_crt == NULL )
5919 return( -1 );
5920
5921 if( peer_crt->raw.len != crt_buf_len )
5922 return( -1 );
5923
5924 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
5925}
5926#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5927static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5928 unsigned char *crt_buf,
5929 size_t crt_buf_len )
5930{
5931 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5932 unsigned char const * const peer_cert_digest =
5933 ssl->session->peer_cert_digest;
5934 mbedtls_md_type_t const peer_cert_digest_type =
5935 ssl->session->peer_cert_digest_type;
5936 mbedtls_md_info_t const * const digest_info =
5937 mbedtls_md_info_from_type( peer_cert_digest_type );
5938 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
5939 size_t digest_len;
5940
5941 if( peer_cert_digest == NULL || digest_info == NULL )
5942 return( -1 );
5943
5944 digest_len = mbedtls_md_get_size( digest_info );
5945 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
5946 return( -1 );
5947
5948 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
5949 if( ret != 0 )
5950 return( -1 );
5951
5952 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
5953}
5954#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5955#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5956
5957/*
5958 * Once the certificate message is read, parse it into a cert chain and
5959 * perform basic checks, but leave actual verification to the caller
5960 */
5961static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5962 mbedtls_x509_crt *chain )
5963{
5964 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5965#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5966 int crt_cnt=0;
5967#endif
5968 size_t i, n;
5969 uint8_t alert;
5970
5971 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5972 {
5973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5974 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5975 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5976 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5977 }
5978
5979 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
5980 {
5981 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5982 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5983 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5984 }
5985
5986 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
5987 {
5988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5989 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5990 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5991 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5992 }
5993
5994 i = mbedtls_ssl_hs_hdr_len( ssl );
5995
5996 /*
5997 * Same message structure as in mbedtls_ssl_write_certificate()
5998 */
5999 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6000
6001 if( ssl->in_msg[i] != 0 ||
6002 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6003 {
6004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6005 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6006 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6007 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6008 }
6009
6010 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6011 i += 3;
6012
6013 /* Iterate through and parse the CRTs in the provided chain. */
6014 while( i < ssl->in_hslen )
6015 {
6016 /* Check that there's room for the next CRT's length fields. */
6017 if ( i + 3 > ssl->in_hslen ) {
6018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6019 mbedtls_ssl_send_alert_message( ssl,
6020 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6021 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6022 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6023 }
6024 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6025 * anything beyond 2**16 ~ 64K. */
6026 if( ssl->in_msg[i] != 0 )
6027 {
6028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6029 mbedtls_ssl_send_alert_message( ssl,
6030 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6031 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6032 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6033 }
6034
6035 /* Read length of the next CRT in the chain. */
6036 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6037 | (unsigned int) ssl->in_msg[i + 2];
6038 i += 3;
6039
6040 if( n < 128 || i + n > ssl->in_hslen )
6041 {
6042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6043 mbedtls_ssl_send_alert_message( ssl,
6044 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6045 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6046 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6047 }
6048
6049 /* Check if we're handling the first CRT in the chain. */
6050#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6051 if( crt_cnt++ == 0 &&
6052 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6053 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6054 {
6055 /* During client-side renegotiation, check that the server's
6056 * end-CRTs hasn't changed compared to the initial handshake,
6057 * mitigating the triple handshake attack. On success, reuse
6058 * the original end-CRT instead of parsing it again. */
6059 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6060 if( ssl_check_peer_crt_unchanged( ssl,
6061 &ssl->in_msg[i],
6062 n ) != 0 )
6063 {
6064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6065 mbedtls_ssl_send_alert_message( ssl,
6066 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6067 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6068 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6069 }
6070
6071 /* Now we can safely free the original chain. */
6072 ssl_clear_peer_cert( ssl->session );
6073 }
6074#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6075
6076 /* Parse the next certificate in the chain. */
6077#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6078 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6079#else
6080 /* If we don't need to store the CRT chain permanently, parse
6081 * it in-place from the input buffer instead of making a copy. */
6082 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6083#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6084 switch( ret )
6085 {
6086 case 0: /*ok*/
6087 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6088 /* Ignore certificate with an unknown algorithm: maybe a
6089 prior certificate was already trusted. */
6090 break;
6091
6092 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6093 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6094 goto crt_parse_der_failed;
6095
6096 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6097 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6098 goto crt_parse_der_failed;
6099
6100 default:
6101 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6102 crt_parse_der_failed:
6103 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6104 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6105 return( ret );
6106 }
6107
6108 i += n;
6109 }
6110
6111 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6112 return( 0 );
6113}
6114
6115#if defined(MBEDTLS_SSL_SRV_C)
6116static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6117{
6118 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6119 return( -1 );
6120
6121 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6122 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6123 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6124 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6125 {
6126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6127 return( 0 );
6128 }
6129 return( -1 );
6130}
6131#endif /* MBEDTLS_SSL_SRV_C */
6132
6133/* Check if a certificate message is expected.
6134 * Return either
6135 * - SSL_CERTIFICATE_EXPECTED, or
6136 * - SSL_CERTIFICATE_SKIP
6137 * indicating whether a Certificate message is expected or not.
6138 */
6139#define SSL_CERTIFICATE_EXPECTED 0
6140#define SSL_CERTIFICATE_SKIP 1
6141static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6142 int authmode )
6143{
6144 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6145 ssl->handshake->ciphersuite_info;
6146
6147 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6148 return( SSL_CERTIFICATE_SKIP );
6149
6150#if defined(MBEDTLS_SSL_SRV_C)
6151 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6152 {
6153 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6154 return( SSL_CERTIFICATE_SKIP );
6155
6156 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6157 {
6158 ssl->session_negotiate->verify_result =
6159 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6160 return( SSL_CERTIFICATE_SKIP );
6161 }
6162 }
6163#else
6164 ((void) authmode);
6165#endif /* MBEDTLS_SSL_SRV_C */
6166
6167 return( SSL_CERTIFICATE_EXPECTED );
6168}
6169
6170static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6171 int authmode,
6172 mbedtls_x509_crt *chain,
6173 void *rs_ctx )
6174{
6175 int ret = 0;
6176 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6177 ssl->handshake->ciphersuite_info;
6178 int have_ca_chain = 0;
6179
6180 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6181 void *p_vrfy;
6182
6183 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6184 return( 0 );
6185
6186 if( ssl->f_vrfy != NULL )
6187 {
6188 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6189 f_vrfy = ssl->f_vrfy;
6190 p_vrfy = ssl->p_vrfy;
6191 }
6192 else
6193 {
6194 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6195 f_vrfy = ssl->conf->f_vrfy;
6196 p_vrfy = ssl->conf->p_vrfy;
6197 }
6198
6199 /*
6200 * Main check: verify certificate
6201 */
6202#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6203 if( ssl->conf->f_ca_cb != NULL )
6204 {
6205 ((void) rs_ctx);
6206 have_ca_chain = 1;
6207
6208 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6209 ret = mbedtls_x509_crt_verify_with_ca_cb(
6210 chain,
6211 ssl->conf->f_ca_cb,
6212 ssl->conf->p_ca_cb,
6213 ssl->conf->cert_profile,
6214 ssl->hostname,
6215 &ssl->session_negotiate->verify_result,
6216 f_vrfy, p_vrfy );
6217 }
6218 else
6219#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6220 {
6221 mbedtls_x509_crt *ca_chain;
6222 mbedtls_x509_crl *ca_crl;
6223
6224#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6225 if( ssl->handshake->sni_ca_chain != NULL )
6226 {
6227 ca_chain = ssl->handshake->sni_ca_chain;
6228 ca_crl = ssl->handshake->sni_ca_crl;
6229 }
6230 else
6231#endif
6232 {
6233 ca_chain = ssl->conf->ca_chain;
6234 ca_crl = ssl->conf->ca_crl;
6235 }
6236
6237 if( ca_chain != NULL )
6238 have_ca_chain = 1;
6239
6240 ret = mbedtls_x509_crt_verify_restartable(
6241 chain,
6242 ca_chain, ca_crl,
6243 ssl->conf->cert_profile,
6244 ssl->hostname,
6245 &ssl->session_negotiate->verify_result,
6246 f_vrfy, p_vrfy, rs_ctx );
6247 }
6248
6249 if( ret != 0 )
6250 {
6251 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6252 }
6253
6254#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6255 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6256 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6257#endif
6258
6259 /*
6260 * Secondary checks: always done, but change 'ret' only if it was 0
6261 */
6262
6263#if defined(MBEDTLS_ECP_C)
6264 {
6265 const mbedtls_pk_context *pk = &chain->pk;
6266
6267 /* If certificate uses an EC key, make sure the curve is OK */
6268 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6269 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6270 {
6271 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6272
6273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6274 if( ret == 0 )
6275 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6276 }
6277 }
6278#endif /* MBEDTLS_ECP_C */
6279
6280 if( mbedtls_ssl_check_cert_usage( chain,
6281 ciphersuite_info,
6282 ! ssl->conf->endpoint,
6283 &ssl->session_negotiate->verify_result ) != 0 )
6284 {
6285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6286 if( ret == 0 )
6287 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6288 }
6289
6290 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6291 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6292 * with details encoded in the verification flags. All other kinds
6293 * of error codes, including those from the user provided f_vrfy
6294 * functions, are treated as fatal and lead to a failure of
6295 * ssl_parse_certificate even if verification was optional. */
6296 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6297 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6298 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6299 {
6300 ret = 0;
6301 }
6302
6303 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6304 {
6305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6306 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6307 }
6308
6309 if( ret != 0 )
6310 {
6311 uint8_t alert;
6312
6313 /* The certificate may have been rejected for several reasons.
6314 Pick one and send the corresponding alert. Which alert to send
6315 may be a subject of debate in some cases. */
6316 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6317 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6318 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6319 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6320 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6321 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6322 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6323 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6324 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6325 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6326 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6327 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6328 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6329 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6330 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6331 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6332 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6333 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6334 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6335 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6336 else
6337 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6338 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6339 alert );
6340 }
6341
6342#if defined(MBEDTLS_DEBUG_C)
6343 if( ssl->session_negotiate->verify_result != 0 )
6344 {
6345 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6346 (unsigned int) ssl->session_negotiate->verify_result ) );
6347 }
6348 else
6349 {
6350 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6351 }
6352#endif /* MBEDTLS_DEBUG_C */
6353
6354 return( ret );
6355}
6356
6357#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6358static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6359 unsigned char *start, size_t len )
6360{
6361 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6362 /* Remember digest of the peer's end-CRT. */
6363 ssl->session_negotiate->peer_cert_digest =
6364 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6365 if( ssl->session_negotiate->peer_cert_digest == NULL )
6366 {
6367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6368 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6369 mbedtls_ssl_send_alert_message( ssl,
6370 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6371 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6372
6373 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6374 }
6375
6376 ret = mbedtls_md( mbedtls_md_info_from_type(
6377 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6378 start, len,
6379 ssl->session_negotiate->peer_cert_digest );
6380
6381 ssl->session_negotiate->peer_cert_digest_type =
6382 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6383 ssl->session_negotiate->peer_cert_digest_len =
6384 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6385
6386 return( ret );
6387}
6388
6389static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6390 unsigned char *start, size_t len )
6391{
6392 unsigned char *end = start + len;
6393 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6394
6395 /* Make a copy of the peer's raw public key. */
6396 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6397 ret = mbedtls_pk_parse_subpubkey( &start, end,
6398 &ssl->handshake->peer_pubkey );
6399 if( ret != 0 )
6400 {
6401 /* We should have parsed the public key before. */
6402 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6403 }
6404
6405 return( 0 );
6406}
6407#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6408
6409int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6410{
6411 int ret = 0;
6412 int crt_expected;
6413#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6414 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6415 ? ssl->handshake->sni_authmode
6416 : ssl->conf->authmode;
6417#else
6418 const int authmode = ssl->conf->authmode;
6419#endif
6420 void *rs_ctx = NULL;
6421 mbedtls_x509_crt *chain = NULL;
6422
6423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6424
6425 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6426 if( crt_expected == SSL_CERTIFICATE_SKIP )
6427 {
6428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6429 goto exit;
6430 }
6431
6432#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6433 if( ssl->handshake->ecrs_enabled &&
6434 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6435 {
6436 chain = ssl->handshake->ecrs_peer_cert;
6437 ssl->handshake->ecrs_peer_cert = NULL;
6438 goto crt_verify;
6439 }
6440#endif
6441
6442 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6443 {
6444 /* mbedtls_ssl_read_record may have sent an alert already. We
6445 let it decide whether to alert. */
6446 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6447 goto exit;
6448 }
6449
6450#if defined(MBEDTLS_SSL_SRV_C)
6451 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6452 {
6453 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6454
6455 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6456 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6457
6458 goto exit;
6459 }
6460#endif /* MBEDTLS_SSL_SRV_C */
6461
6462 /* Clear existing peer CRT structure in case we tried to
6463 * reuse a session but it failed, and allocate a new one. */
6464 ssl_clear_peer_cert( ssl->session_negotiate );
6465
6466 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6467 if( chain == NULL )
6468 {
6469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6470 sizeof( mbedtls_x509_crt ) ) );
6471 mbedtls_ssl_send_alert_message( ssl,
6472 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6473 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6474
6475 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6476 goto exit;
6477 }
6478 mbedtls_x509_crt_init( chain );
6479
6480 ret = ssl_parse_certificate_chain( ssl, chain );
6481 if( ret != 0 )
6482 goto exit;
6483
6484#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6485 if( ssl->handshake->ecrs_enabled)
6486 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6487
6488crt_verify:
6489 if( ssl->handshake->ecrs_enabled)
6490 rs_ctx = &ssl->handshake->ecrs_ctx;
6491#endif
6492
6493 ret = ssl_parse_certificate_verify( ssl, authmode,
6494 chain, rs_ctx );
6495 if( ret != 0 )
6496 goto exit;
6497
6498#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6499 {
6500 unsigned char *crt_start, *pk_start;
6501 size_t crt_len, pk_len;
6502
6503 /* We parse the CRT chain without copying, so
6504 * these pointers point into the input buffer,
6505 * and are hence still valid after freeing the
6506 * CRT chain. */
6507
6508 crt_start = chain->raw.p;
6509 crt_len = chain->raw.len;
6510
6511 pk_start = chain->pk_raw.p;
6512 pk_len = chain->pk_raw.len;
6513
6514 /* Free the CRT structures before computing
6515 * digest and copying the peer's public key. */
6516 mbedtls_x509_crt_free( chain );
6517 mbedtls_free( chain );
6518 chain = NULL;
6519
6520 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6521 if( ret != 0 )
6522 goto exit;
6523
6524 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6525 if( ret != 0 )
6526 goto exit;
6527 }
6528#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6529 /* Pass ownership to session structure. */
6530 ssl->session_negotiate->peer_cert = chain;
6531 chain = NULL;
6532#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6533
6534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6535
6536exit:
6537
6538 if( ret == 0 )
6539 ssl->state++;
6540
6541#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6542 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6543 {
6544 ssl->handshake->ecrs_peer_cert = chain;
6545 chain = NULL;
6546 }
6547#endif
6548
6549 if( chain != NULL )
6550 {
6551 mbedtls_x509_crt_free( chain );
6552 mbedtls_free( chain );
6553 }
6554
6555 return( ret );
6556}
6557#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6558
Jerry Yu615bd6f2022-02-17 14:25:15 +08006559#if defined(MBEDTLS_SHA256_C)
6560static void ssl_calc_finished_tls_sha256(
6561 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6562{
6563 int len = 12;
6564 const char *sender;
6565 unsigned char padbuf[32];
6566#if defined(MBEDTLS_USE_PSA_CRYPTO)
6567 size_t hash_size;
6568 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
6569 psa_status_t status;
6570#else
6571 mbedtls_sha256_context sha256;
6572#endif
6573
6574 mbedtls_ssl_session *session = ssl->session_negotiate;
6575 if( !session )
6576 session = ssl->session;
6577
6578 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6579 ? "client finished"
6580 : "server finished";
6581
6582#if defined(MBEDTLS_USE_PSA_CRYPTO)
6583 sha256_psa = psa_hash_operation_init();
6584
6585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6586
6587 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6588 if( status != PSA_SUCCESS )
6589 {
6590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6591 return;
6592 }
6593
6594 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6595 if( status != PSA_SUCCESS )
6596 {
6597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6598 return;
6599 }
6600 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6601#else
6602
6603 mbedtls_sha256_init( &sha256 );
6604
6605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
6606
6607 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6608
6609 /*
6610 * TLSv1.2:
6611 * hash = PRF( master, finished_label,
6612 * Hash( handshake ) )[0.11]
6613 */
6614
6615#if !defined(MBEDTLS_SHA256_ALT)
6616 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
6617 sha256.state, sizeof( sha256.state ) );
6618#endif
6619
6620 mbedtls_sha256_finish( &sha256, padbuf );
6621 mbedtls_sha256_free( &sha256 );
6622#endif /* MBEDTLS_USE_PSA_CRYPTO */
6623
6624 ssl->handshake->tls_prf( session->master, 48, sender,
6625 padbuf, 32, buf, len );
6626
6627 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6628
6629 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6630
6631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6632}
6633#endif /* MBEDTLS_SHA256_C */
6634
Jerry Yub7ba49e2022-02-17 14:25:53 +08006635
6636#if defined(MBEDTLS_SHA384_C)
6637
6638static void ssl_calc_finished_tls_sha384(
6639 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6640{
6641 int len = 12;
6642 const char *sender;
6643 unsigned char padbuf[48];
6644#if defined(MBEDTLS_USE_PSA_CRYPTO)
6645 size_t hash_size;
6646 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
6647 psa_status_t status;
6648#else
6649 mbedtls_sha512_context sha512;
6650#endif
6651
6652 mbedtls_ssl_session *session = ssl->session_negotiate;
6653 if( !session )
6654 session = ssl->session;
6655
6656 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6657 ? "client finished"
6658 : "server finished";
6659
6660#if defined(MBEDTLS_USE_PSA_CRYPTO)
6661 sha384_psa = psa_hash_operation_init();
6662
6663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6664
6665 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6666 if( status != PSA_SUCCESS )
6667 {
6668 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6669 return;
6670 }
6671
6672 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
6673 if( status != PSA_SUCCESS )
6674 {
6675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6676 return;
6677 }
6678 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
6679#else
6680 mbedtls_sha512_init( &sha512 );
6681
6682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
6683
6684 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
6685
6686 /*
6687 * TLSv1.2:
6688 * hash = PRF( master, finished_label,
6689 * Hash( handshake ) )[0.11]
6690 */
6691
6692#if !defined(MBEDTLS_SHA512_ALT)
6693 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6694 sha512.state, sizeof( sha512.state ) );
6695#endif
6696 mbedtls_sha512_finish( &sha512, padbuf );
6697
6698 mbedtls_sha512_free( &sha512 );
6699#endif
6700
6701 ssl->handshake->tls_prf( session->master, 48, sender,
6702 padbuf, 48, buf, len );
6703
6704 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6705
6706 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6707
6708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6709}
6710#endif /* MBEDTLS_SHA384_C */
6711
Jerry Yuaef00152022-02-17 14:27:31 +08006712void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
6713{
6714 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
6715
6716 /*
6717 * Free our handshake params
6718 */
6719 mbedtls_ssl_handshake_free( ssl );
6720 mbedtls_free( ssl->handshake );
6721 ssl->handshake = NULL;
6722
6723 /*
6724 * Free the previous transform and swith in the current one
6725 */
6726 if( ssl->transform )
6727 {
6728 mbedtls_ssl_transform_free( ssl->transform );
6729 mbedtls_free( ssl->transform );
6730 }
6731 ssl->transform = ssl->transform_negotiate;
6732 ssl->transform_negotiate = NULL;
6733
6734 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
6735}
6736
Jerry Yu2a9fff52022-02-17 14:28:51 +08006737void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
6738{
6739 int resume = ssl->handshake->resume;
6740
6741 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
6742
6743#if defined(MBEDTLS_SSL_RENEGOTIATION)
6744 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6745 {
6746 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
6747 ssl->renego_records_seen = 0;
6748 }
6749#endif
6750
6751 /*
6752 * Free the previous session and switch in the current one
6753 */
6754 if( ssl->session )
6755 {
6756#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6757 /* RFC 7366 3.1: keep the EtM state */
6758 ssl->session_negotiate->encrypt_then_mac =
6759 ssl->session->encrypt_then_mac;
6760#endif
6761
6762 mbedtls_ssl_session_free( ssl->session );
6763 mbedtls_free( ssl->session );
6764 }
6765 ssl->session = ssl->session_negotiate;
6766 ssl->session_negotiate = NULL;
6767
6768 /*
6769 * Add cache entry
6770 */
6771 if( ssl->conf->f_set_cache != NULL &&
6772 ssl->session->id_len != 0 &&
6773 resume == 0 )
6774 {
6775 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
6776 ssl->session->id,
6777 ssl->session->id_len,
6778 ssl->session ) != 0 )
6779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
6780 }
6781
6782#if defined(MBEDTLS_SSL_PROTO_DTLS)
6783 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6784 ssl->handshake->flight != NULL )
6785 {
6786 /* Cancel handshake timer */
6787 mbedtls_ssl_set_timer( ssl, 0 );
6788
6789 /* Keep last flight around in case we need to resend it:
6790 * we need the handshake and transform structures for that */
6791 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
6792 }
6793 else
6794#endif
6795 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
6796
6797 ssl->state++;
6798
6799 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
6800}
6801
Jerry Yu3c8e47b2022-02-17 14:30:01 +08006802int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
6803{
6804 int ret, hash_len;
6805
6806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
6807
6808 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
6809
6810 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
6811
6812 /*
6813 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6814 * may define some other value. Currently (early 2016), no defined
6815 * ciphersuite does this (and this is unlikely to change as activity has
6816 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6817 */
6818 hash_len = 12;
6819
6820#if defined(MBEDTLS_SSL_RENEGOTIATION)
6821 ssl->verify_data_len = hash_len;
6822 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
6823#endif
6824
6825 ssl->out_msglen = 4 + hash_len;
6826 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6827 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
6828
6829 /*
6830 * In case of session resuming, invert the client and server
6831 * ChangeCipherSpec messages order.
6832 */
6833 if( ssl->handshake->resume != 0 )
6834 {
6835#if defined(MBEDTLS_SSL_CLI_C)
6836 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6837 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
6838#endif
6839#if defined(MBEDTLS_SSL_SRV_C)
6840 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6841 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
6842#endif
6843 }
6844 else
6845 ssl->state++;
6846
6847 /*
6848 * Switch to our negotiated transform and session parameters for outbound
6849 * data.
6850 */
6851 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
6852
6853#if defined(MBEDTLS_SSL_PROTO_DTLS)
6854 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6855 {
6856 unsigned char i;
6857
6858 /* Remember current epoch settings for resending */
6859 ssl->handshake->alt_transform_out = ssl->transform_out;
6860 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
6861 sizeof( ssl->handshake->alt_out_ctr ) );
6862
6863 /* Set sequence_number to zero */
6864 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
6865
6866
6867 /* Increment epoch */
6868 for( i = 2; i > 0; i-- )
6869 if( ++ssl->cur_out_ctr[i - 1] != 0 )
6870 break;
6871
6872 /* The loop goes to its end iff the counter is wrapping */
6873 if( i == 0 )
6874 {
6875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6876 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
6877 }
6878 }
6879 else
6880#endif /* MBEDTLS_SSL_PROTO_DTLS */
6881 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6882
6883 ssl->transform_out = ssl->transform_negotiate;
6884 ssl->session_out = ssl->session_negotiate;
6885
6886#if defined(MBEDTLS_SSL_PROTO_DTLS)
6887 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6888 mbedtls_ssl_send_flight_completed( ssl );
6889#endif
6890
6891 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6892 {
6893 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6894 return( ret );
6895 }
6896
6897#if defined(MBEDTLS_SSL_PROTO_DTLS)
6898 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6899 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6900 {
6901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6902 return( ret );
6903 }
6904#endif
6905
6906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
6907
6908 return( 0 );
6909}
6910
Jerry Yu0b3d7c12022-02-17 14:30:51 +08006911#define SSL_MAX_HASH_LEN 12
6912
6913int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
6914{
6915 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6916 unsigned int hash_len = 12;
6917 unsigned char buf[SSL_MAX_HASH_LEN];
6918
6919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
6920
6921 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
6922
6923 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6924 {
6925 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6926 goto exit;
6927 }
6928
6929 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6930 {
6931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6932 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6933 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6934 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
6935 goto exit;
6936 }
6937
6938 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
6939 {
6940 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6941 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6942 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
6943 goto exit;
6944 }
6945
6946 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
6947 {
6948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6949 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6950 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6951 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
6952 goto exit;
6953 }
6954
6955 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
6956 buf, hash_len ) != 0 )
6957 {
6958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6959 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6960 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
6961 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
6962 goto exit;
6963 }
6964
6965#if defined(MBEDTLS_SSL_RENEGOTIATION)
6966 ssl->verify_data_len = hash_len;
6967 memcpy( ssl->peer_verify_data, buf, hash_len );
6968#endif
6969
6970 if( ssl->handshake->resume != 0 )
6971 {
6972#if defined(MBEDTLS_SSL_CLI_C)
6973 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6974 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
6975#endif
6976#if defined(MBEDTLS_SSL_SRV_C)
6977 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6978 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
6979#endif
6980 }
6981 else
6982 ssl->state++;
6983
6984#if defined(MBEDTLS_SSL_PROTO_DTLS)
6985 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6986 mbedtls_ssl_recv_flight_completed( ssl );
6987#endif
6988
6989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
6990
6991exit:
6992 mbedtls_platform_zeroize( buf, hash_len );
6993 return( ret );
6994}
6995
Jerry Yu392112c2022-02-17 14:34:10 +08006996#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
6997/*
6998 * Helper to get TLS 1.2 PRF from ciphersuite
6999 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7000 */
7001static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7002{
7003#if defined(MBEDTLS_SHA384_C)
7004 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7005 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7006
7007 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
7008 return( tls_prf_sha384 );
7009#else
7010 (void) ciphersuite_id;
7011#endif
7012 return( tls_prf_sha256 );
7013}
7014#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007015
7016static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7017{
7018 ((void) tls_prf);
7019#if defined(MBEDTLS_SHA384_C)
7020 if( tls_prf == tls_prf_sha384 )
7021 {
7022 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7023 }
7024 else
7025#endif
7026#if defined(MBEDTLS_SHA256_C)
7027 if( tls_prf == tls_prf_sha256 )
7028 {
7029 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7030 }
7031 else
7032#endif
7033 return( MBEDTLS_SSL_TLS_PRF_NONE );
7034}
7035
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007036/*
7037 * Populate a transform structure with session keys and all the other
7038 * necessary information.
7039 *
7040 * Parameters:
7041 * - [in/out]: transform: structure to populate
7042 * [in] must be just initialised with mbedtls_ssl_transform_init()
7043 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7044 * - [in] ciphersuite
7045 * - [in] master
7046 * - [in] encrypt_then_mac
7047 * - [in] compression
7048 * - [in] tls_prf: pointer to PRF to use for key derivation
7049 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
7050 * - [in] minor_ver: SSL/TLS minor version
7051 * - [in] endpoint: client or server
7052 * - [in] ssl: used for:
7053 * - ssl->conf->{f,p}_export_keys
7054 * [in] optionally used for:
7055 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7056 */
7057static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7058 int ciphersuite,
7059 const unsigned char master[48],
7060#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
7061 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7062 int encrypt_then_mac,
7063#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
7064 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7065 ssl_tls_prf_t tls_prf,
7066 const unsigned char randbytes[64],
7067 int minor_ver,
7068 unsigned endpoint,
7069 const mbedtls_ssl_context *ssl )
7070{
7071 int ret = 0;
7072 unsigned char keyblk[256];
7073 unsigned char *key1;
7074 unsigned char *key2;
7075 unsigned char *mac_enc;
7076 unsigned char *mac_dec;
7077 size_t mac_key_len = 0;
7078 size_t iv_copy_len;
7079 size_t keylen;
7080 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
7081 const mbedtls_cipher_info_t *cipher_info;
7082 const mbedtls_md_info_t *md_info;
7083
7084#if defined(MBEDTLS_USE_PSA_CRYPTO)
7085 psa_key_type_t key_type;
7086 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7087 psa_algorithm_t alg;
7088 size_t key_bits;
7089 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7090#endif
7091
7092#if !defined(MBEDTLS_DEBUG_C) && \
7093 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7094 if( ssl->f_export_keys == NULL )
7095 {
7096 ssl = NULL; /* make sure we don't use it except for these cases */
7097 (void) ssl;
7098 }
7099#endif
7100
7101 /*
7102 * Some data just needs copying into the structure
7103 */
7104#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
7105 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7106 transform->encrypt_then_mac = encrypt_then_mac;
7107#endif
7108 transform->minor_ver = minor_ver;
7109
7110#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7111 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7112#endif
7113
7114#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
7115 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
7116 {
7117 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7118 * generation separate. This should never happen. */
7119 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7120 }
7121#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7122
7123 /*
7124 * Get various info structures
7125 */
7126 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7127 if( ciphersuite_info == NULL )
7128 {
7129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7130 ciphersuite ) );
7131 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7132 }
7133
7134 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7135 if( cipher_info == NULL )
7136 {
7137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7138 ciphersuite_info->cipher ) );
7139 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7140 }
7141
7142 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7143 if( md_info == NULL )
7144 {
7145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7146 (unsigned) ciphersuite_info->mac ) );
7147 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7148 }
7149
7150#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7151 /* Copy own and peer's CID if the use of the CID
7152 * extension has been negotiated. */
7153 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7154 {
7155 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7156
7157 transform->in_cid_len = ssl->own_cid_len;
7158 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7159 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7160 transform->in_cid_len );
7161
7162 transform->out_cid_len = ssl->handshake->peer_cid_len;
7163 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7164 ssl->handshake->peer_cid_len );
7165 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7166 transform->out_cid_len );
7167 }
7168#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7169
7170 /*
7171 * Compute key block using the PRF
7172 */
7173 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7174 if( ret != 0 )
7175 {
7176 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7177 return( ret );
7178 }
7179
7180 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7181 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7182 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7183 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7184 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7185
7186 /*
7187 * Determine the appropriate key, IV and MAC length.
7188 */
7189
7190 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
7191
7192#if defined(MBEDTLS_GCM_C) || \
7193 defined(MBEDTLS_CCM_C) || \
7194 defined(MBEDTLS_CHACHAPOLY_C)
7195 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_GCM ||
7196 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CCM ||
7197 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
7198 {
7199 size_t explicit_ivlen;
7200
7201 transform->maclen = 0;
7202 mac_key_len = 0;
7203 transform->taglen =
7204 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7205
7206 /* All modes haves 96-bit IVs, but the length of the static parts vary
7207 * with mode and version:
7208 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7209 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7210 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7211 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7212 * sequence number).
7213 */
7214 transform->ivlen = 12;
7215 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
7216 transform->fixed_ivlen = 12;
7217 else
7218 transform->fixed_ivlen = 4;
7219
7220 /* Minimum length of encrypted record */
7221 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7222 transform->minlen = explicit_ivlen + transform->taglen;
7223 }
7224 else
7225#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7226#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7227 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM ||
7228 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7229 {
Neil Armstrongcf8841a2022-02-24 11:17:45 +01007230#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007231 /* Initialize HMAC contexts */
7232 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7233 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7234 {
7235 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7236 goto end;
7237 }
Neil Armstrongcf8841a2022-02-24 11:17:45 +01007238#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007239
7240 /* Get MAC length */
7241 mac_key_len = mbedtls_md_get_size( md_info );
7242 transform->maclen = mac_key_len;
7243
7244 /* IV length */
7245 transform->ivlen = cipher_info->iv_size;
7246
7247 /* Minimum length */
7248 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM )
7249 transform->minlen = transform->maclen;
7250 else
7251 {
7252 /*
7253 * GenericBlockCipher:
7254 * 1. if EtM is in use: one block plus MAC
7255 * otherwise: * first multiple of blocklen greater than maclen
7256 * 2. IV
7257 */
7258#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7259 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
7260 {
7261 transform->minlen = transform->maclen
7262 + cipher_info->block_size;
7263 }
7264 else
7265#endif
7266 {
7267 transform->minlen = transform->maclen
7268 + cipher_info->block_size
7269 - transform->maclen % cipher_info->block_size;
7270 }
7271
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007272 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
7273 {
7274 transform->minlen += transform->ivlen;
7275 }
7276 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007277 {
7278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7279 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7280 goto end;
7281 }
7282 }
7283 }
7284 else
7285#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7286 {
7287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7288 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7289 }
7290
7291 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7292 (unsigned) keylen,
7293 (unsigned) transform->minlen,
7294 (unsigned) transform->ivlen,
7295 (unsigned) transform->maclen ) );
7296
7297 /*
7298 * Finally setup the cipher contexts, IVs and MAC secrets.
7299 */
7300#if defined(MBEDTLS_SSL_CLI_C)
7301 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7302 {
7303 key1 = keyblk + mac_key_len * 2;
7304 key2 = keyblk + mac_key_len * 2 + keylen;
7305
7306 mac_enc = keyblk;
7307 mac_dec = keyblk + mac_key_len;
7308
7309 /*
7310 * This is not used in TLS v1.1.
7311 */
7312 iv_copy_len = ( transform->fixed_ivlen ) ?
7313 transform->fixed_ivlen : transform->ivlen;
7314 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7315 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7316 iv_copy_len );
7317 }
7318 else
7319#endif /* MBEDTLS_SSL_CLI_C */
7320#if defined(MBEDTLS_SSL_SRV_C)
7321 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7322 {
7323 key1 = keyblk + mac_key_len * 2 + keylen;
7324 key2 = keyblk + mac_key_len * 2;
7325
7326 mac_enc = keyblk + mac_key_len;
7327 mac_dec = keyblk;
7328
7329 /*
7330 * This is not used in TLS v1.1.
7331 */
7332 iv_copy_len = ( transform->fixed_ivlen ) ?
7333 transform->fixed_ivlen : transform->ivlen;
7334 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7335 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7336 iv_copy_len );
7337 }
7338 else
7339#endif /* MBEDTLS_SSL_SRV_C */
7340 {
7341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7342 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7343 goto end;
7344 }
7345
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007346 if( ssl != NULL && ssl->f_export_keys != NULL )
7347 {
7348 ssl->f_export_keys( ssl->p_export_keys,
7349 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7350 master, 48,
7351 randbytes + 32,
7352 randbytes,
7353 tls_prf_get_type( tls_prf ) );
7354 }
7355
7356#if defined(MBEDTLS_USE_PSA_CRYPTO)
7357 if( ( status = mbedtls_ssl_cipher_to_psa( cipher_info->type,
7358 transform->taglen,
7359 &alg,
7360 &key_type,
7361 &key_bits ) ) != PSA_SUCCESS )
7362 {
7363 ret = psa_ssl_status_to_mbedtls( status );
7364 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7365 goto end;
7366 }
7367
7368 transform->psa_alg = alg;
7369
7370 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7371 {
7372 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7373 psa_set_key_algorithm( &attributes, alg );
7374 psa_set_key_type( &attributes, key_type );
7375
7376 if( ( status = psa_import_key( &attributes,
7377 key1,
7378 PSA_BITS_TO_BYTES( key_bits ),
7379 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7380 {
7381 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7382 ret = psa_ssl_status_to_mbedtls( status );
7383 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7384 goto end;
7385 }
7386
7387 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7388
7389 if( ( status = psa_import_key( &attributes,
7390 key2,
7391 PSA_BITS_TO_BYTES( key_bits ),
7392 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7393 {
7394 ret = psa_ssl_status_to_mbedtls( status );
7395 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7396 goto end;
7397 }
7398 }
7399#else
7400 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7401 cipher_info ) ) != 0 )
7402 {
7403 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7404 goto end;
7405 }
7406
7407 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7408 cipher_info ) ) != 0 )
7409 {
7410 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7411 goto end;
7412 }
7413
7414 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7415 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7416 MBEDTLS_ENCRYPT ) ) != 0 )
7417 {
7418 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7419 goto end;
7420 }
7421
7422 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7423 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7424 MBEDTLS_DECRYPT ) ) != 0 )
7425 {
7426 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7427 goto end;
7428 }
7429
7430#if defined(MBEDTLS_CIPHER_MODE_CBC)
7431 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7432 {
7433 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7434 MBEDTLS_PADDING_NONE ) ) != 0 )
7435 {
7436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7437 goto end;
7438 }
7439
7440 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7441 MBEDTLS_PADDING_NONE ) ) != 0 )
7442 {
7443 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7444 goto end;
7445 }
7446 }
7447#endif /* MBEDTLS_CIPHER_MODE_CBC */
7448#endif /* MBEDTLS_USE_PSA_CRYPTO */
7449
Neil Armstrong29c0c042022-03-17 17:47:28 +01007450#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7451 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7452 For AEAD-based ciphersuites, there is nothing to do here. */
7453 if( mac_key_len != 0 )
7454 {
7455#if defined(MBEDTLS_USE_PSA_CRYPTO)
7456 alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
7457 if( alg == 0 )
7458 {
7459 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7460 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_md_type_to_psa", ret );
7461 goto end;
7462 }
7463
7464 transform->psa_mac_alg = PSA_ALG_HMAC( alg );
7465
7466 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
7467 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( alg ) );
7468 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7469
7470 if( ( status = psa_import_key( &attributes,
7471 mac_enc, mac_key_len,
7472 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7473 {
7474 ret = psa_ssl_status_to_mbedtls( status );
7475 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7476 goto end;
7477 }
7478
Ronald Cronfb39f152022-03-25 14:36:28 +01007479 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
7480 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) &&
7481 ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007482 /* mbedtls_ct_hmac() requires the key to be exportable */
7483 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7484 PSA_KEY_USAGE_VERIFY_HASH );
7485 else
7486 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7487
7488 if( ( status = psa_import_key( &attributes,
7489 mac_dec, mac_key_len,
7490 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7491 {
7492 ret = psa_ssl_status_to_mbedtls( status );
7493 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7494 goto end;
7495 }
7496#else
7497 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7498 if( ret != 0 )
7499 goto end;
7500 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7501 if( ret != 0 )
7502 goto end;
7503#endif /* MBEDTLS_USE_PSA_CRYPTO */
7504 }
7505#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7506
7507 ((void) mac_dec);
7508 ((void) mac_enc);
7509
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007510end:
7511 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7512 return( ret );
7513}
7514
Jerry Yuee40f9d2022-02-17 14:55:16 +08007515#if defined(MBEDTLS_USE_PSA_CRYPTO)
7516int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7517 unsigned char *hash, size_t *hashlen,
7518 unsigned char *data, size_t data_len,
7519 mbedtls_md_type_t md_alg )
7520{
7521 psa_status_t status;
7522 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
7523 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
7524
7525 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7526
7527 if( ( status = psa_hash_setup( &hash_operation,
7528 hash_alg ) ) != PSA_SUCCESS )
7529 {
7530 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7531 goto exit;
7532 }
7533
7534 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
7535 64 ) ) != PSA_SUCCESS )
7536 {
7537 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7538 goto exit;
7539 }
7540
7541 if( ( status = psa_hash_update( &hash_operation,
7542 data, data_len ) ) != PSA_SUCCESS )
7543 {
7544 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7545 goto exit;
7546 }
7547
7548 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
7549 hashlen ) ) != PSA_SUCCESS )
7550 {
7551 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
7552 goto exit;
7553 }
7554
7555exit:
7556 if( status != PSA_SUCCESS )
7557 {
7558 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7559 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7560 switch( status )
7561 {
7562 case PSA_ERROR_NOT_SUPPORTED:
7563 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
7564 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
7565 case PSA_ERROR_BUFFER_TOO_SMALL:
7566 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
7567 case PSA_ERROR_INSUFFICIENT_MEMORY:
7568 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
7569 default:
7570 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
7571 }
7572 }
7573 return( 0 );
7574}
7575
7576#else
7577
7578int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7579 unsigned char *hash, size_t *hashlen,
7580 unsigned char *data, size_t data_len,
7581 mbedtls_md_type_t md_alg )
7582{
7583 int ret = 0;
7584 mbedtls_md_context_t ctx;
7585 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
7586 *hashlen = mbedtls_md_get_size( md_info );
7587
7588 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
7589
7590 mbedtls_md_init( &ctx );
7591
7592 /*
7593 * digitally-signed struct {
7594 * opaque client_random[32];
7595 * opaque server_random[32];
7596 * ServerDHParams params;
7597 * };
7598 */
7599 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
7600 {
7601 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7602 goto exit;
7603 }
7604 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
7605 {
7606 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
7607 goto exit;
7608 }
7609 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
7610 {
7611 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7612 goto exit;
7613 }
7614 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
7615 {
7616 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7617 goto exit;
7618 }
7619 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
7620 {
7621 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
7622 goto exit;
7623 }
7624
7625exit:
7626 mbedtls_md_free( &ctx );
7627
7628 if( ret != 0 )
7629 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7630 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7631
7632 return( ret );
7633}
7634#endif /* MBEDTLS_USE_PSA_CRYPTO */
7635
Jerry Yud9d91da2022-02-17 14:57:06 +08007636#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
7637
7638/* Find an entry in a signature-hash set matching a given hash algorithm. */
7639mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
7640 mbedtls_pk_type_t sig_alg )
7641{
7642 switch( sig_alg )
7643 {
7644 case MBEDTLS_PK_RSA:
7645 return( set->rsa );
7646 case MBEDTLS_PK_ECDSA:
7647 return( set->ecdsa );
7648 default:
7649 return( MBEDTLS_MD_NONE );
7650 }
7651}
7652
7653/* Add a signature-hash-pair to a signature-hash set */
7654void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
7655 mbedtls_pk_type_t sig_alg,
7656 mbedtls_md_type_t md_alg )
7657{
7658 switch( sig_alg )
7659 {
7660 case MBEDTLS_PK_RSA:
7661 if( set->rsa == MBEDTLS_MD_NONE )
7662 set->rsa = md_alg;
7663 break;
7664
7665 case MBEDTLS_PK_ECDSA:
7666 if( set->ecdsa == MBEDTLS_MD_NONE )
7667 set->ecdsa = md_alg;
7668 break;
7669
7670 default:
7671 break;
7672 }
7673}
7674
7675/* Allow exactly one hash algorithm for each signature. */
7676void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
7677 mbedtls_md_type_t md_alg )
7678{
7679 set->rsa = md_alg;
7680 set->ecdsa = md_alg;
7681}
7682
7683#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7684
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08007685/* Serialization of TLS 1.2 sessions:
7686 *
7687 * struct {
7688 * uint64 start_time;
7689 * uint8 ciphersuite[2]; // defined by the standard
7690 * uint8 compression; // 0 or 1
7691 * uint8 session_id_len; // at most 32
7692 * opaque session_id[32];
7693 * opaque master[48]; // fixed length in the standard
7694 * uint32 verify_result;
7695 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
7696 * opaque ticket<0..2^24-1>; // length 0 means no ticket
7697 * uint32 ticket_lifetime;
7698 * uint8 mfl_code; // up to 255 according to standard
7699 * uint8 encrypt_then_mac; // 0 or 1
7700 * } serialized_session_tls12;
7701 *
7702 */
7703static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session,
7704 unsigned char *buf,
7705 size_t buf_len )
7706{
7707 unsigned char *p = buf;
7708 size_t used = 0;
7709
7710#if defined(MBEDTLS_HAVE_TIME)
7711 uint64_t start;
7712#endif
7713#if defined(MBEDTLS_X509_CRT_PARSE_C)
7714#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7715 size_t cert_len;
7716#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7717#endif /* MBEDTLS_X509_CRT_PARSE_C */
7718
7719 /*
7720 * Time
7721 */
7722#if defined(MBEDTLS_HAVE_TIME)
7723 used += 8;
7724
7725 if( used <= buf_len )
7726 {
7727 start = (uint64_t) session->start;
7728
7729 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
7730 p += 8;
7731 }
7732#endif /* MBEDTLS_HAVE_TIME */
7733
7734 /*
7735 * Basic mandatory fields
7736 */
7737 used += 2 /* ciphersuite */
7738 + 1 /* compression */
7739 + 1 /* id_len */
7740 + sizeof( session->id )
7741 + sizeof( session->master )
7742 + 4; /* verify_result */
7743
7744 if( used <= buf_len )
7745 {
7746 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
7747 p += 2;
7748
7749 *p++ = MBEDTLS_BYTE_0( session->compression );
7750
7751 *p++ = MBEDTLS_BYTE_0( session->id_len );
7752 memcpy( p, session->id, 32 );
7753 p += 32;
7754
7755 memcpy( p, session->master, 48 );
7756 p += 48;
7757
7758 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
7759 p += 4;
7760 }
7761
7762 /*
7763 * Peer's end-entity certificate
7764 */
7765#if defined(MBEDTLS_X509_CRT_PARSE_C)
7766#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7767 if( session->peer_cert == NULL )
7768 cert_len = 0;
7769 else
7770 cert_len = session->peer_cert->raw.len;
7771
7772 used += 3 + cert_len;
7773
7774 if( used <= buf_len )
7775 {
7776 *p++ = MBEDTLS_BYTE_2( cert_len );
7777 *p++ = MBEDTLS_BYTE_1( cert_len );
7778 *p++ = MBEDTLS_BYTE_0( cert_len );
7779
7780 if( session->peer_cert != NULL )
7781 {
7782 memcpy( p, session->peer_cert->raw.p, cert_len );
7783 p += cert_len;
7784 }
7785 }
7786#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7787 if( session->peer_cert_digest != NULL )
7788 {
7789 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
7790 if( used <= buf_len )
7791 {
7792 *p++ = (unsigned char) session->peer_cert_digest_type;
7793 *p++ = (unsigned char) session->peer_cert_digest_len;
7794 memcpy( p, session->peer_cert_digest,
7795 session->peer_cert_digest_len );
7796 p += session->peer_cert_digest_len;
7797 }
7798 }
7799 else
7800 {
7801 used += 2;
7802 if( used <= buf_len )
7803 {
7804 *p++ = (unsigned char) MBEDTLS_MD_NONE;
7805 *p++ = 0;
7806 }
7807 }
7808#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7809#endif /* MBEDTLS_X509_CRT_PARSE_C */
7810
7811 /*
7812 * Session ticket if any, plus associated data
7813 */
7814#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7815 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
7816
7817 if( used <= buf_len )
7818 {
7819 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
7820 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
7821 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
7822
7823 if( session->ticket != NULL )
7824 {
7825 memcpy( p, session->ticket, session->ticket_len );
7826 p += session->ticket_len;
7827 }
7828
7829 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
7830 p += 4;
7831 }
7832#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7833
7834 /*
7835 * Misc extension-related info
7836 */
7837#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7838 used += 1;
7839
7840 if( used <= buf_len )
7841 *p++ = session->mfl_code;
7842#endif
7843
7844#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7845 used += 1;
7846
7847 if( used <= buf_len )
7848 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
7849#endif
7850
7851 return( used );
7852}
7853
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08007854static int ssl_session_load_tls12( mbedtls_ssl_session *session,
7855 const unsigned char *buf,
7856 size_t len )
7857{
7858#if defined(MBEDTLS_HAVE_TIME)
7859 uint64_t start;
7860#endif
7861#if defined(MBEDTLS_X509_CRT_PARSE_C)
7862#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7863 size_t cert_len;
7864#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7865#endif /* MBEDTLS_X509_CRT_PARSE_C */
7866
7867 const unsigned char *p = buf;
7868 const unsigned char * const end = buf + len;
7869
7870 /*
7871 * Time
7872 */
7873#if defined(MBEDTLS_HAVE_TIME)
7874 if( 8 > (size_t)( end - p ) )
7875 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7876
7877 start = ( (uint64_t) p[0] << 56 ) |
7878 ( (uint64_t) p[1] << 48 ) |
7879 ( (uint64_t) p[2] << 40 ) |
7880 ( (uint64_t) p[3] << 32 ) |
7881 ( (uint64_t) p[4] << 24 ) |
7882 ( (uint64_t) p[5] << 16 ) |
7883 ( (uint64_t) p[6] << 8 ) |
7884 ( (uint64_t) p[7] );
7885 p += 8;
7886
7887 session->start = (time_t) start;
7888#endif /* MBEDTLS_HAVE_TIME */
7889
7890 /*
7891 * Basic mandatory fields
7892 */
7893 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
7894 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7895
7896 session->ciphersuite = ( p[0] << 8 ) | p[1];
7897 p += 2;
7898
7899 session->compression = *p++;
7900
7901 session->id_len = *p++;
7902 memcpy( session->id, p, 32 );
7903 p += 32;
7904
7905 memcpy( session->master, p, 48 );
7906 p += 48;
7907
7908 session->verify_result = ( (uint32_t) p[0] << 24 ) |
7909 ( (uint32_t) p[1] << 16 ) |
7910 ( (uint32_t) p[2] << 8 ) |
7911 ( (uint32_t) p[3] );
7912 p += 4;
7913
7914 /* Immediately clear invalid pointer values that have been read, in case
7915 * we exit early before we replaced them with valid ones. */
7916#if defined(MBEDTLS_X509_CRT_PARSE_C)
7917#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7918 session->peer_cert = NULL;
7919#else
7920 session->peer_cert_digest = NULL;
7921#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7922#endif /* MBEDTLS_X509_CRT_PARSE_C */
7923#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7924 session->ticket = NULL;
7925#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7926
7927 /*
7928 * Peer certificate
7929 */
7930#if defined(MBEDTLS_X509_CRT_PARSE_C)
7931#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7932 /* Deserialize CRT from the end of the ticket. */
7933 if( 3 > (size_t)( end - p ) )
7934 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7935
7936 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
7937 p += 3;
7938
7939 if( cert_len != 0 )
7940 {
7941 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7942
7943 if( cert_len > (size_t)( end - p ) )
7944 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7945
7946 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7947
7948 if( session->peer_cert == NULL )
7949 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7950
7951 mbedtls_x509_crt_init( session->peer_cert );
7952
7953 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
7954 p, cert_len ) ) != 0 )
7955 {
7956 mbedtls_x509_crt_free( session->peer_cert );
7957 mbedtls_free( session->peer_cert );
7958 session->peer_cert = NULL;
7959 return( ret );
7960 }
7961
7962 p += cert_len;
7963 }
7964#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7965 /* Deserialize CRT digest from the end of the ticket. */
7966 if( 2 > (size_t)( end - p ) )
7967 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7968
7969 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
7970 session->peer_cert_digest_len = (size_t) *p++;
7971
7972 if( session->peer_cert_digest_len != 0 )
7973 {
7974 const mbedtls_md_info_t *md_info =
7975 mbedtls_md_info_from_type( session->peer_cert_digest_type );
7976 if( md_info == NULL )
7977 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7978 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
7979 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7980
7981 if( session->peer_cert_digest_len > (size_t)( end - p ) )
7982 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7983
7984 session->peer_cert_digest =
7985 mbedtls_calloc( 1, session->peer_cert_digest_len );
7986 if( session->peer_cert_digest == NULL )
7987 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7988
7989 memcpy( session->peer_cert_digest, p,
7990 session->peer_cert_digest_len );
7991 p += session->peer_cert_digest_len;
7992 }
7993#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7994#endif /* MBEDTLS_X509_CRT_PARSE_C */
7995
7996 /*
7997 * Session ticket and associated data
7998 */
7999#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8000 if( 3 > (size_t)( end - p ) )
8001 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8002
8003 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8004 p += 3;
8005
8006 if( session->ticket_len != 0 )
8007 {
8008 if( session->ticket_len > (size_t)( end - p ) )
8009 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8010
8011 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8012 if( session->ticket == NULL )
8013 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8014
8015 memcpy( session->ticket, p, session->ticket_len );
8016 p += session->ticket_len;
8017 }
8018
8019 if( 4 > (size_t)( end - p ) )
8020 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8021
8022 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8023 ( (uint32_t) p[1] << 16 ) |
8024 ( (uint32_t) p[2] << 8 ) |
8025 ( (uint32_t) p[3] );
8026 p += 4;
8027#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8028
8029 /*
8030 * Misc extension-related info
8031 */
8032#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8033 if( 1 > (size_t)( end - p ) )
8034 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8035
8036 session->mfl_code = *p++;
8037#endif
8038
8039#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8040 if( 1 > (size_t)( end - p ) )
8041 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8042
8043 session->encrypt_then_mac = *p++;
8044#endif
8045
8046 /* Done, should have consumed entire buffer */
8047 if( p != end )
8048 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8049
8050 return( 0 );
8051}
Jerry Yudc7bd172022-02-17 13:44:15 +08008052#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008054#endif /* MBEDTLS_SSL_TLS_C */