blob: 6a1bfa8f5a0250832b84f1d345824f6861218e50 [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 ||
123 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
124 {
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
478void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
479{
480 ((void) ssl);
481#if defined(MBEDTLS_SHA256_C)
482#if defined(MBEDTLS_USE_PSA_CRYPTO)
483 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
484 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
485#else
486 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
487#endif
488#endif
489#if defined(MBEDTLS_SHA384_C)
490#if defined(MBEDTLS_USE_PSA_CRYPTO)
491 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
492 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
493#else
494 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
495#endif
496#endif
497}
498
499static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
500 const unsigned char *buf, size_t len )
501{
502#if defined(MBEDTLS_SHA256_C)
503#if defined(MBEDTLS_USE_PSA_CRYPTO)
504 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
505#else
506 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
507#endif
508#endif
509#if defined(MBEDTLS_SHA384_C)
510#if defined(MBEDTLS_USE_PSA_CRYPTO)
511 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
512#else
513 mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len );
514#endif
515#endif
516}
517
518#if defined(MBEDTLS_SHA256_C)
519static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
520 const unsigned char *buf, size_t len )
521{
522#if defined(MBEDTLS_USE_PSA_CRYPTO)
523 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
524#else
525 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
526#endif
527}
528#endif
529
530#if defined(MBEDTLS_SHA384_C)
531static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
532 const unsigned char *buf, size_t len )
533{
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}
540#endif
541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200543{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500547#if defined(MBEDTLS_USE_PSA_CRYPTO)
548 handshake->fin_sha256_psa = psa_hash_operation_init();
549 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
550#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200552 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200553#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500554#endif
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +0200555#if defined(MBEDTLS_SHA384_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500556#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500557 handshake->fin_sha384_psa = psa_hash_operation_init();
558 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500559#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_sha512_init( &handshake->fin_sha512 );
TRodziewicz26371e42021-06-08 16:45:41 +0200561 mbedtls_sha512_starts( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200562#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500563#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200564
565 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100566
567#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +0100568 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +0100569 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
570#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572#if defined(MBEDTLS_DHM_C)
573 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200574#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575#if defined(MBEDTLS_ECDH_C)
576 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200577#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200578#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200579 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200580#if defined(MBEDTLS_SSL_CLI_C)
581 handshake->ecjpake_cache = NULL;
582 handshake->ecjpake_cache_len = 0;
583#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200584#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200585
Gilles Peskineeccd8882020-03-10 12:19:08 +0100586#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200587 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200588#endif
589
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200590#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
591 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
592#endif
Hanno Becker75173122019-02-06 16:18:31 +0000593
594#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
595 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
596 mbedtls_pk_init( &handshake->peer_pubkey );
597#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200598}
599
Hanno Beckera18d1322018-01-03 14:27:32 +0000600void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200601{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200603
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100604#if defined(MBEDTLS_USE_PSA_CRYPTO)
605 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
606 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100607#else
608 mbedtls_cipher_init( &transform->cipher_ctx_enc );
609 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100610#endif
611
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000612#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100613#if defined(MBEDTLS_USE_PSA_CRYPTO)
614 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
615 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100616#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 mbedtls_md_init( &transform->md_ctx_enc );
618 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000619#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100620#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200621}
622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200624{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200626}
627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000629{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200630 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000631 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200633 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200635 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200636 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200637
638 /*
639 * Either the pointers are now NULL or cleared properly and can be freed.
640 * Now allocate missing structures.
641 */
642 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200643 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200644 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200645 }
Paul Bakker48916f92012-09-16 19:57:18 +0000646
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200647 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200648 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200649 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200650 }
Paul Bakker48916f92012-09-16 19:57:18 +0000651
Paul Bakker82788fb2014-10-20 13:59:19 +0200652 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200653 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200654 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200655 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500656#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
657 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400658
Andrzej Kurek4a063792020-10-21 15:08:44 +0200659 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
660 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500661#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000662
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200663 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000664 if( ssl->handshake == NULL ||
665 ssl->transform_negotiate == NULL ||
666 ssl->session_negotiate == NULL )
667 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 mbedtls_free( ssl->handshake );
671 mbedtls_free( ssl->transform_negotiate );
672 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200673
674 ssl->handshake = NULL;
675 ssl->transform_negotiate = NULL;
676 ssl->session_negotiate = NULL;
677
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200678 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000679 }
680
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200681 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000683 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200684 ssl_handshake_params_init( ssl->handshake );
685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200687 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
688 {
689 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200690
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200691 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
692 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
693 else
694 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200695
Hanno Becker0f57a652020-02-05 10:37:26 +0000696 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200697 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200698#endif
699
Brett Warrene0edc842021-08-17 09:53:13 +0100700/*
701 * curve_list is translated to IANA TLS group identifiers here because
702 * mbedtls_ssl_conf_curves returns void and so can't return
703 * any error codes.
704 */
705#if defined(MBEDTLS_ECP_C)
706#if !defined(MBEDTLS_DEPRECATED_REMOVED)
707 /* Heap allocate and translate curve_list from internal to IANA group ids */
708 if ( ssl->conf->curve_list != NULL )
709 {
710 size_t length;
711 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
712
713 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
714 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
715
716 /* Leave room for zero termination */
717 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
718 if ( group_list == NULL )
719 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
720
721 for( size_t i = 0; i < length; i++ )
722 {
723 const mbedtls_ecp_curve_info *info =
724 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
725 if ( info == NULL )
726 {
727 mbedtls_free( group_list );
728 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
729 }
730 group_list[i] = info->tls_id;
731 }
732
733 group_list[length] = 0;
734
735 ssl->handshake->group_list = group_list;
736 ssl->handshake->group_list_heap_allocated = 1;
737 }
738 else
739 {
740 ssl->handshake->group_list = ssl->conf->group_list;
741 ssl->handshake->group_list_heap_allocated = 0;
742 }
743#endif /* MBEDTLS_DEPRECATED_REMOVED */
744#endif /* MBEDTLS_ECP_C */
745
Jerry Yuf017ee42022-01-12 15:49:48 +0800746#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
747#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800748#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800749 /* Heap allocate and translate sig_hashes from internal hash identifiers to
750 signature algorithms IANA identifiers. */
751 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800752 ssl->conf->sig_hashes != NULL )
753 {
754 const int *md;
755 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800756 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800757 uint16_t *p;
758
Jerry Yub476a442022-01-21 18:14:45 +0800759#if defined(static_assert)
760 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
761 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
762 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800763#endif
764
Jerry Yuf017ee42022-01-12 15:49:48 +0800765 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
766 {
767 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
768 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800769#if defined(MBEDTLS_ECDSA_C)
770 sig_algs_len += sizeof( uint16_t );
771#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800772
Jerry Yub476a442022-01-21 18:14:45 +0800773#if defined(MBEDTLS_RSA_C)
774 sig_algs_len += sizeof( uint16_t );
775#endif
776 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
777 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800778 }
779
Jerry Yub476a442022-01-21 18:14:45 +0800780 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800781 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
782
Jerry Yub476a442022-01-21 18:14:45 +0800783 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
784 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800785 if( ssl->handshake->sig_algs == NULL )
786 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
787
788 p = (uint16_t *)ssl->handshake->sig_algs;
789 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
790 {
791 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
792 if( hash == MBEDTLS_SSL_HASH_NONE )
793 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800794#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800795 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
796 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800797#endif
798#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800799 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
800 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800801#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800802 }
803 *p = MBEDTLS_TLS1_3_SIG_NONE;
804 ssl->handshake->sig_algs_heap_allocated = 1;
805 }
806 else
Jerry Yua69269a2022-01-17 21:06:01 +0800807#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800808 {
809 ssl->handshake->sig_algs = ssl->conf->sig_algs;
810 ssl->handshake->sig_algs_heap_allocated = 0;
811 }
812#endif /* MBEDTLS_DEPRECATED_REMOVED */
813#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000814 return( 0 );
815}
816
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200817#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200818/* Dummy cookie callbacks for defaults */
819static int ssl_cookie_write_dummy( void *ctx,
820 unsigned char **p, unsigned char *end,
821 const unsigned char *cli_id, size_t cli_id_len )
822{
823 ((void) ctx);
824 ((void) p);
825 ((void) end);
826 ((void) cli_id);
827 ((void) cli_id_len);
828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200830}
831
832static int ssl_cookie_check_dummy( void *ctx,
833 const unsigned char *cookie, size_t cookie_len,
834 const unsigned char *cli_id, size_t cli_id_len )
835{
836 ((void) ctx);
837 ((void) cookie);
838 ((void) cookie_len);
839 ((void) cli_id);
840 ((void) cli_id_len);
841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200843}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200844#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200845
Paul Bakker5121ce52009-01-03 21:22:43 +0000846/*
847 * Initialize an SSL context
848 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200849void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
850{
851 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
852}
853
Jerry Yu60835a82021-08-04 10:13:52 +0800854static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
855{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100856 const mbedtls_ssl_config *conf = ssl->conf;
857
Ronald Cron6f135e12021-12-08 16:57:54 +0100858#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100859 if( mbedtls_ssl_conf_is_tls13_enabled( conf ) &&
860 ( conf->endpoint == MBEDTLS_SSL_IS_SERVER ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800861 {
Ronald Cron086ee0b2022-03-15 15:18:51 +0100862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
863 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
864 }
865
866 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
867 {
868 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jerry Yu60835a82021-08-04 10:13:52 +0800869 {
Ronald Cron086ee0b2022-03-15 15:18:51 +0100870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
Jerry Yu60835a82021-08-04 10:13:52 +0800871 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
872 }
873 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
874 return( 0 );
875 }
876#endif
877
878#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100879 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800880 {
881 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
882 return( 0 );
883 }
884#endif
885
Ronald Cron6f135e12021-12-08 16:57:54 +0100886#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100887 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800888 {
889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Hybrid TLS 1.2 + TLS 1.3 configurations are not yet supported" ) );
890 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
891 }
892#endif
893
894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
895 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
896}
897
898static int ssl_conf_check(const mbedtls_ssl_context *ssl)
899{
900 int ret;
901 ret = ssl_conf_version_check( ssl );
902 if( ret != 0 )
903 return( ret );
904
905 /* Space for further checks */
906
907 return( 0 );
908}
909
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200910/*
911 * Setup an SSL context
912 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100913
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200914int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +0200915 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +0000916{
Janos Follath865b3eb2019-12-16 11:46:15 +0000917 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +0000918 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
919 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +0000920
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200921 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +0000922
Jerry Yu60835a82021-08-04 10:13:52 +0800923 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
924 return( ret );
925
Paul Bakker62f2dee2012-09-28 07:31:51 +0000926 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +0100927 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +0000928 */
k-stachowiakc9a5f022018-07-24 13:53:31 +0200929
930 /* Set to NULL in case of an error condition */
931 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +0200932
Darryl Greenb33cc762019-11-28 14:29:44 +0000933#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
934 ssl->in_buf_len = in_buf_len;
935#endif
936 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +1000937 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000938 {
Paul Elliottd48d5c62021-01-07 14:47:05 +0000939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +0200940 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +0200941 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +1000942 }
943
Darryl Greenb33cc762019-11-28 14:29:44 +0000944#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
945 ssl->out_buf_len = out_buf_len;
946#endif
947 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +1000948 if( ssl->out_buf == NULL )
949 {
Paul Elliottd48d5c62021-01-07 14:47:05 +0000950 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +0200951 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +0200952 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +0000953 }
954
Hanno Becker3e6f8ab2020-02-05 10:40:57 +0000955 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200956
Johan Pascalb62bb512015-12-03 21:56:45 +0100957#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +0200958 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +0100959#endif
960
Paul Bakker48916f92012-09-16 19:57:18 +0000961 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +0200962 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +0000963
964 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +0200965
966error:
967 mbedtls_free( ssl->in_buf );
968 mbedtls_free( ssl->out_buf );
969
970 ssl->conf = NULL;
971
Darryl Greenb33cc762019-11-28 14:29:44 +0000972#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
973 ssl->in_buf_len = 0;
974 ssl->out_buf_len = 0;
975#endif
k-stachowiaka47911c2018-07-04 17:41:58 +0200976 ssl->in_buf = NULL;
977 ssl->out_buf = NULL;
978
979 ssl->in_hdr = NULL;
980 ssl->in_ctr = NULL;
981 ssl->in_len = NULL;
982 ssl->in_iv = NULL;
983 ssl->in_msg = NULL;
984
985 ssl->out_hdr = NULL;
986 ssl->out_ctr = NULL;
987 ssl->out_len = NULL;
988 ssl->out_iv = NULL;
989 ssl->out_msg = NULL;
990
k-stachowiak9f7798e2018-07-31 16:52:32 +0200991 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000992}
993
994/*
Paul Bakker7eb013f2011-10-06 12:37:39 +0000995 * Reset an initialized and used SSL context for re-use while retaining
996 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +0200997 *
998 * If partial is non-zero, keep data in the input buffer and client ID.
999 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001000 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001001void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1002 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001003{
Darryl Greenb33cc762019-11-28 14:29:44 +00001004#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1005 size_t in_buf_len = ssl->in_buf_len;
1006 size_t out_buf_len = ssl->out_buf_len;
1007#else
1008 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1009 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1010#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001011
Hanno Beckerb0302c42021-08-03 09:39:42 +01001012#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1013 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001014#endif
1015
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001016 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001017 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001018
Hanno Beckerb0302c42021-08-03 09:39:42 +01001019 mbedtls_ssl_reset_in_out_pointers( ssl );
1020
1021 /* Reset incoming message parsing */
1022 ssl->in_offt = NULL;
1023 ssl->nb_zero = 0;
1024 ssl->in_msgtype = 0;
1025 ssl->in_msglen = 0;
1026 ssl->in_hslen = 0;
1027 ssl->keep_current_message = 0;
1028 ssl->transform_in = NULL;
1029
1030#if defined(MBEDTLS_SSL_PROTO_DTLS)
1031 ssl->next_record_offset = 0;
1032 ssl->in_epoch = 0;
1033#endif
1034
1035 /* Keep current datagram if partial == 1 */
1036 if( partial == 0 )
1037 {
1038 ssl->in_left = 0;
1039 memset( ssl->in_buf, 0, in_buf_len );
1040 }
1041
1042 /* Reset outgoing message writing */
1043 ssl->out_msgtype = 0;
1044 ssl->out_msglen = 0;
1045 ssl->out_left = 0;
1046 memset( ssl->out_buf, 0, out_buf_len );
1047 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1048 ssl->transform_out = NULL;
1049
1050#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1051 mbedtls_ssl_dtls_replay_reset( ssl );
1052#endif
1053
XiaokangQian2b01dc32022-01-21 02:53:13 +00001054#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001055 if( ssl->transform )
1056 {
1057 mbedtls_ssl_transform_free( ssl->transform );
1058 mbedtls_free( ssl->transform );
1059 ssl->transform = NULL;
1060 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001061#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1062
XiaokangQian2b01dc32022-01-21 02:53:13 +00001063#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1064 mbedtls_ssl_transform_free( ssl->transform_application );
1065 mbedtls_free( ssl->transform_application );
1066 ssl->transform_application = NULL;
1067
1068 if( ssl->handshake != NULL )
1069 {
1070 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1071 mbedtls_free( ssl->handshake->transform_earlydata );
1072 ssl->handshake->transform_earlydata = NULL;
1073
1074 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1075 mbedtls_free( ssl->handshake->transform_handshake );
1076 ssl->handshake->transform_handshake = NULL;
1077 }
1078
XiaokangQian2b01dc32022-01-21 02:53:13 +00001079#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001080}
1081
1082int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1083{
1084 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1085
1086 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1087
XiaokangQian78b1fa72022-01-19 06:56:30 +00001088 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001089
1090 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091#if defined(MBEDTLS_SSL_RENEGOTIATION)
1092 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001093 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001094
1095 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1097 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001098#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001100
Hanno Beckerb0302c42021-08-03 09:39:42 +01001101 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001102 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001103 if( ssl->session )
1104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 mbedtls_ssl_session_free( ssl->session );
1106 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001107 ssl->session = NULL;
1108 }
1109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001111 ssl->alpn_chosen = NULL;
1112#endif
1113
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001114#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01001115#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001116 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01001117#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001118 {
1119 mbedtls_free( ssl->cli_id );
1120 ssl->cli_id = NULL;
1121 ssl->cli_id_len = 0;
1122 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001123#endif
1124
Paul Bakker48916f92012-09-16 19:57:18 +00001125 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1126 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001127
1128 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001129}
1130
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001131/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001132 * Reset an initialized and used SSL context for re-use while retaining
1133 * all application-set variables, function pointers and data.
1134 */
1135int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1136{
Hanno Becker43aefe22020-02-05 10:44:56 +00001137 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001138}
1139
1140/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001141 * SSL set accessors
1142 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001143void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001144{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001145 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001146}
1147
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001148void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001149{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001150 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001151}
1152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001154void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001155{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001156 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001157}
1158#endif
1159
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001160void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001161{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001162 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001163}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001166
Hanno Becker1841b0a2018-08-24 11:13:57 +01001167void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1168 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001169{
1170 ssl->disable_datagram_packing = !allow_packing;
1171}
1172
1173void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1174 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001175{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001176 conf->hs_timeout_min = min;
1177 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001178}
1179#endif
1180
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001181void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001182{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001183 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001184}
1185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001187void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001188 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001189 void *p_vrfy )
1190{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001191 conf->f_vrfy = f_vrfy;
1192 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001193}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001195
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001196void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001197 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001198 void *p_rng )
1199{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001200 conf->f_rng = f_rng;
1201 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001202}
1203
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001204void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001205 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 void *p_dbg )
1207{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001208 conf->f_dbg = f_dbg;
1209 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001210}
1211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001213 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001214 mbedtls_ssl_send_t *f_send,
1215 mbedtls_ssl_recv_t *f_recv,
1216 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001217{
1218 ssl->p_bio = p_bio;
1219 ssl->f_send = f_send;
1220 ssl->f_recv = f_recv;
1221 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001222}
1223
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001224#if defined(MBEDTLS_SSL_PROTO_DTLS)
1225void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1226{
1227 ssl->mtu = mtu;
1228}
1229#endif
1230
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001231void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001232{
1233 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001234}
1235
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001236void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1237 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001238 mbedtls_ssl_set_timer_t *f_set_timer,
1239 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001240{
1241 ssl->p_timer = p_timer;
1242 ssl->f_set_timer = f_set_timer;
1243 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001244
1245 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001246 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001247}
1248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249#if defined(MBEDTLS_SSL_SRV_C)
Glenn Strauss2ed95272022-01-21 18:02:17 -05001250void mbedtls_ssl_conf_cert_cb( mbedtls_ssl_config *conf,
1251 int (*f_cert_cb)(mbedtls_ssl_context *) )
1252{
1253 conf->f_cert_cb = f_cert_cb;
1254}
1255#endif /* MBEDTLS_SSL_SRV_C */
1256
1257#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001258void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001259 void *p_cache,
1260 mbedtls_ssl_cache_get_t *f_get_cache,
1261 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001262{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001263 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001264 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001265 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001266}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269#if defined(MBEDTLS_SSL_CLI_C)
1270int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001271{
Janos Follath865b3eb2019-12-16 11:46:15 +00001272 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001273
1274 if( ssl == NULL ||
1275 session == NULL ||
1276 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001277 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001280 }
1281
Hanno Beckere810bbc2021-05-14 16:01:05 +01001282 if( ssl->handshake->resume == 1 )
1283 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1284
Hanno Becker52055ae2019-02-06 14:30:46 +00001285 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1286 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001287 return( ret );
1288
Paul Bakker0a597072012-09-25 21:55:46 +00001289 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001290
1291 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001292}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001294
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001295void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1296 const int *ciphersuites )
1297{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001298 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001299}
1300
Ronald Cron6f135e12021-12-08 16:57:54 +01001301#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001302void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001303 const int kex_modes )
1304{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001305 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001306}
Ronald Cron6f135e12021-12-08 16:57:54 +01001307#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001310void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001311 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001312{
1313 conf->cert_profile = profile;
1314}
1315
Glenn Strauss36872db2022-01-22 05:06:31 -05001316static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1317{
1318 mbedtls_ssl_key_cert *cur = key_cert, *next;
1319
1320 while( cur != NULL )
1321 {
1322 next = cur->next;
1323 mbedtls_free( cur );
1324 cur = next;
1325 }
1326}
1327
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001328/* Append a new keycert entry to a (possibly empty) list */
1329static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1330 mbedtls_x509_crt *cert,
1331 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001332{
niisato8ee24222018-06-25 19:05:48 +09001333 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001334
Glenn Strauss36872db2022-01-22 05:06:31 -05001335 if( cert == NULL )
1336 {
1337 /* Free list if cert is null */
1338 ssl_key_cert_free( *head );
1339 *head = NULL;
1340 return( 0 );
1341 }
1342
niisato8ee24222018-06-25 19:05:48 +09001343 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1344 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001345 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001346
niisato8ee24222018-06-25 19:05:48 +09001347 new_cert->cert = cert;
1348 new_cert->key = key;
1349 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001350
Glenn Strauss36872db2022-01-22 05:06:31 -05001351 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001352 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001353 {
niisato8ee24222018-06-25 19:05:48 +09001354 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001355 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001356 else
1357 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001358 mbedtls_ssl_key_cert *cur = *head;
1359 while( cur->next != NULL )
1360 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001361 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001362 }
1363
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001364 return( 0 );
1365}
1366
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001367int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001368 mbedtls_x509_crt *own_cert,
1369 mbedtls_pk_context *pk_key )
1370{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001371 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001372}
1373
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001374void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001375 mbedtls_x509_crt *ca_chain,
1376 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001377{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001378 conf->ca_chain = ca_chain;
1379 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001380
1381#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1382 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1383 * cannot be used together. */
1384 conf->f_ca_cb = NULL;
1385 conf->p_ca_cb = NULL;
1386#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001387}
Hanno Becker5adaad92019-03-27 16:54:37 +00001388
1389#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1390void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001391 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001392 void *p_ca_cb )
1393{
1394 conf->f_ca_cb = f_ca_cb;
1395 conf->p_ca_cb = p_ca_cb;
1396
1397 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1398 * cannot be used together. */
1399 conf->ca_chain = NULL;
1400 conf->ca_crl = NULL;
1401}
1402#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001404
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001405#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001406const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1407 size_t *name_len )
1408{
1409 *name_len = ssl->handshake->sni_name_len;
1410 return( ssl->handshake->sni_name );
1411}
1412
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001413int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1414 mbedtls_x509_crt *own_cert,
1415 mbedtls_pk_context *pk_key )
1416{
1417 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1418 own_cert, pk_key ) );
1419}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001420
1421void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1422 mbedtls_x509_crt *ca_chain,
1423 mbedtls_x509_crl *ca_crl )
1424{
1425 ssl->handshake->sni_ca_chain = ca_chain;
1426 ssl->handshake->sni_ca_crl = ca_crl;
1427}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001428
1429void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1430 int authmode )
1431{
1432 ssl->handshake->sni_authmode = authmode;
1433}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001434#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1435
Hanno Becker8927c832019-04-03 12:52:50 +01001436#if defined(MBEDTLS_X509_CRT_PARSE_C)
1437void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1438 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1439 void *p_vrfy )
1440{
1441 ssl->f_vrfy = f_vrfy;
1442 ssl->p_vrfy = p_vrfy;
1443}
1444#endif
1445
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001446#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001447/*
1448 * Set EC J-PAKE password for current handshake
1449 */
1450int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1451 const unsigned char *pw,
1452 size_t pw_len )
1453{
1454 mbedtls_ecjpake_role role;
1455
Janos Follath8eb64132016-06-03 15:40:57 +01001456 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001457 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1458
1459 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1460 role = MBEDTLS_ECJPAKE_SERVER;
1461 else
1462 role = MBEDTLS_ECJPAKE_CLIENT;
1463
1464 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1465 role,
1466 MBEDTLS_MD_SHA256,
1467 MBEDTLS_ECP_DP_SECP256R1,
1468 pw, pw_len ) );
1469}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001470#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001471
Gilles Peskineeccd8882020-03-10 12:19:08 +01001472#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001473
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001474static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1475{
1476#if defined(MBEDTLS_USE_PSA_CRYPTO)
1477 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1478 return( 1 );
1479#endif /* MBEDTLS_USE_PSA_CRYPTO */
1480
1481 if( conf->psk != NULL )
1482 return( 1 );
1483
1484 return( 0 );
1485}
1486
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001487static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1488{
1489 /* Remove reference to existing PSK, if any. */
1490#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001491 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001492 {
1493 /* The maintenance of the PSK key slot is the
1494 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001495 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001496 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00001497 /* This and the following branch should never
1498 * be taken simultaenously as we maintain the
1499 * invariant that raw and opaque PSKs are never
1500 * configured simultaneously. As a safeguard,
1501 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001502#endif /* MBEDTLS_USE_PSA_CRYPTO */
1503 if( conf->psk != NULL )
1504 {
1505 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1506
1507 mbedtls_free( conf->psk );
1508 conf->psk = NULL;
1509 conf->psk_len = 0;
1510 }
1511
1512 /* Remove reference to PSK identity, if any. */
1513 if( conf->psk_identity != NULL )
1514 {
1515 mbedtls_free( conf->psk_identity );
1516 conf->psk_identity = NULL;
1517 conf->psk_identity_len = 0;
1518 }
1519}
1520
Hanno Becker7390c712018-11-15 13:33:04 +00001521/* This function assumes that PSK identity in the SSL config is unset.
1522 * It checks that the provided identity is well-formed and attempts
1523 * to make a copy of it in the SSL config.
1524 * On failure, the PSK identity in the config remains unset. */
1525static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1526 unsigned char const *psk_identity,
1527 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001528{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001529 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001530 if( psk_identity == NULL ||
1531 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001532 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001533 {
1534 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1535 }
1536
Hanno Becker7390c712018-11-15 13:33:04 +00001537 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1538 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001539 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001540
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001541 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001542 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001543
1544 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001545}
1546
Hanno Becker7390c712018-11-15 13:33:04 +00001547int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1548 const unsigned char *psk, size_t psk_len,
1549 const unsigned char *psk_identity, size_t psk_identity_len )
1550{
Janos Follath865b3eb2019-12-16 11:46:15 +00001551 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001552
1553 /* We currently only support one PSK, raw or opaque. */
1554 if( ssl_conf_psk_is_configured( conf ) )
1555 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001556
1557 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001558 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001559 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001560 if( psk_len == 0 )
1561 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1562 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1564
Hanno Becker7390c712018-11-15 13:33:04 +00001565 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1566 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1567 conf->psk_len = psk_len;
1568 memcpy( conf->psk, psk, conf->psk_len );
1569
1570 /* Check and set PSK Identity */
1571 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1572 if( ret != 0 )
1573 ssl_conf_remove_psk( conf );
1574
1575 return( ret );
1576}
1577
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001578static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1579{
1580#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001581 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001582 {
Ronald Croncf56a0a2020-08-04 09:51:30 +02001583 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001584 }
1585 else
1586#endif /* MBEDTLS_USE_PSA_CRYPTO */
1587 if( ssl->handshake->psk != NULL )
1588 {
1589 mbedtls_platform_zeroize( ssl->handshake->psk,
1590 ssl->handshake->psk_len );
1591 mbedtls_free( ssl->handshake->psk );
1592 ssl->handshake->psk_len = 0;
1593 }
1594}
1595
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001596int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1597 const unsigned char *psk, size_t psk_len )
1598{
1599 if( psk == NULL || ssl->handshake == NULL )
1600 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1601
1602 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1603 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1604
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001605 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001606
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001607 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001608 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001609
1610 ssl->handshake->psk_len = psk_len;
1611 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1612
1613 return( 0 );
1614}
1615
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001616#if defined(MBEDTLS_USE_PSA_CRYPTO)
1617int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001618 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001619 const unsigned char *psk_identity,
1620 size_t psk_identity_len )
1621{
Janos Follath865b3eb2019-12-16 11:46:15 +00001622 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001623
1624 /* We currently only support one PSK, raw or opaque. */
1625 if( ssl_conf_psk_is_configured( conf ) )
1626 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001627
Hanno Becker7390c712018-11-15 13:33:04 +00001628 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001629 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001630 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001631 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001632
1633 /* Check and set PSK Identity */
1634 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1635 psk_identity_len );
1636 if( ret != 0 )
1637 ssl_conf_remove_psk( conf );
1638
1639 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001640}
1641
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001642int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1643 mbedtls_svc_key_id_t psk )
1644{
1645 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1646 ( ssl->handshake == NULL ) )
1647 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1648
1649 ssl_remove_psk( ssl );
1650 ssl->handshake->psk_opaque = psk;
1651 return( 0 );
1652}
1653#endif /* MBEDTLS_USE_PSA_CRYPTO */
1654
1655void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1656 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1657 size_t),
1658 void *p_psk )
1659{
1660 conf->f_psk = f_psk;
1661 conf->p_psk = p_psk;
1662}
1663#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1664
1665#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01001666psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01001667 size_t taglen,
1668 psa_algorithm_t *alg,
1669 psa_key_type_t *key_type,
1670 size_t *key_size )
1671{
1672 switch ( mbedtls_cipher_type )
1673 {
1674 case MBEDTLS_CIPHER_AES_128_CBC:
1675 *alg = PSA_ALG_CBC_NO_PADDING;
1676 *key_type = PSA_KEY_TYPE_AES;
1677 *key_size = 128;
1678 break;
1679 case MBEDTLS_CIPHER_AES_128_CCM:
1680 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1681 *key_type = PSA_KEY_TYPE_AES;
1682 *key_size = 128;
1683 break;
1684 case MBEDTLS_CIPHER_AES_128_GCM:
1685 *alg = PSA_ALG_GCM;
1686 *key_type = PSA_KEY_TYPE_AES;
1687 *key_size = 128;
1688 break;
1689 case MBEDTLS_CIPHER_AES_192_CCM:
1690 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1691 *key_type = PSA_KEY_TYPE_AES;
1692 *key_size = 192;
1693 break;
1694 case MBEDTLS_CIPHER_AES_192_GCM:
1695 *alg = PSA_ALG_GCM;
1696 *key_type = PSA_KEY_TYPE_AES;
1697 *key_size = 192;
1698 break;
1699 case MBEDTLS_CIPHER_AES_256_CBC:
1700 *alg = PSA_ALG_CBC_NO_PADDING;
1701 *key_type = PSA_KEY_TYPE_AES;
1702 *key_size = 256;
1703 break;
1704 case MBEDTLS_CIPHER_AES_256_CCM:
1705 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1706 *key_type = PSA_KEY_TYPE_AES;
1707 *key_size = 256;
1708 break;
1709 case MBEDTLS_CIPHER_AES_256_GCM:
1710 *alg = PSA_ALG_GCM;
1711 *key_type = PSA_KEY_TYPE_AES;
1712 *key_size = 256;
1713 break;
1714 case MBEDTLS_CIPHER_ARIA_128_CBC:
1715 *alg = PSA_ALG_CBC_NO_PADDING;
1716 *key_type = PSA_KEY_TYPE_ARIA;
1717 *key_size = 128;
1718 break;
1719 case MBEDTLS_CIPHER_ARIA_128_CCM:
1720 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1721 *key_type = PSA_KEY_TYPE_ARIA;
1722 *key_size = 128;
1723 break;
1724 case MBEDTLS_CIPHER_ARIA_128_GCM:
1725 *alg = PSA_ALG_GCM;
1726 *key_type = PSA_KEY_TYPE_ARIA;
1727 *key_size = 128;
1728 break;
1729 case MBEDTLS_CIPHER_ARIA_192_CCM:
1730 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1731 *key_type = PSA_KEY_TYPE_ARIA;
1732 *key_size = 192;
1733 break;
1734 case MBEDTLS_CIPHER_ARIA_192_GCM:
1735 *alg = PSA_ALG_GCM;
1736 *key_type = PSA_KEY_TYPE_ARIA;
1737 *key_size = 192;
1738 break;
1739 case MBEDTLS_CIPHER_ARIA_256_CBC:
1740 *alg = PSA_ALG_CBC_NO_PADDING;
1741 *key_type = PSA_KEY_TYPE_ARIA;
1742 *key_size = 256;
1743 break;
1744 case MBEDTLS_CIPHER_ARIA_256_CCM:
1745 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1746 *key_type = PSA_KEY_TYPE_ARIA;
1747 *key_size = 256;
1748 break;
1749 case MBEDTLS_CIPHER_ARIA_256_GCM:
1750 *alg = PSA_ALG_GCM;
1751 *key_type = PSA_KEY_TYPE_ARIA;
1752 *key_size = 256;
1753 break;
1754 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
1755 *alg = PSA_ALG_CBC_NO_PADDING;
1756 *key_type = PSA_KEY_TYPE_CAMELLIA;
1757 *key_size = 128;
1758 break;
1759 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
1760 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1761 *key_type = PSA_KEY_TYPE_CAMELLIA;
1762 *key_size = 128;
1763 break;
1764 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
1765 *alg = PSA_ALG_GCM;
1766 *key_type = PSA_KEY_TYPE_CAMELLIA;
1767 *key_size = 128;
1768 break;
1769 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
1770 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1771 *key_type = PSA_KEY_TYPE_CAMELLIA;
1772 *key_size = 192;
1773 break;
1774 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
1775 *alg = PSA_ALG_GCM;
1776 *key_type = PSA_KEY_TYPE_CAMELLIA;
1777 *key_size = 192;
1778 break;
1779 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
1780 *alg = PSA_ALG_CBC_NO_PADDING;
1781 *key_type = PSA_KEY_TYPE_CAMELLIA;
1782 *key_size = 256;
1783 break;
1784 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
1785 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1786 *key_type = PSA_KEY_TYPE_CAMELLIA;
1787 *key_size = 256;
1788 break;
1789 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
1790 *alg = PSA_ALG_GCM;
1791 *key_type = PSA_KEY_TYPE_CAMELLIA;
1792 *key_size = 256;
1793 break;
1794 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
1795 *alg = PSA_ALG_CHACHA20_POLY1305;
1796 *key_type = PSA_KEY_TYPE_CHACHA20;
1797 *key_size = 256;
1798 break;
1799 case MBEDTLS_CIPHER_NULL:
1800 *alg = MBEDTLS_SSL_NULL_CIPHER;
1801 *key_type = 0;
1802 *key_size = 0;
1803 break;
1804 default:
1805 return PSA_ERROR_NOT_SUPPORTED;
1806 }
1807
1808 return PSA_SUCCESS;
1809}
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001810#endif /* MBEDTLS_USE_PSA_CRYPTO */
1811
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02001812#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01001813int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
1814 const unsigned char *dhm_P, size_t P_len,
1815 const unsigned char *dhm_G, size_t G_len )
1816{
Janos Follath865b3eb2019-12-16 11:46:15 +00001817 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01001818
Glenn Strausscee11292021-12-20 01:43:17 -05001819 mbedtls_mpi_free( &conf->dhm_P );
1820 mbedtls_mpi_free( &conf->dhm_G );
1821
Hanno Beckera90658f2017-10-04 15:29:08 +01001822 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
1823 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
1824 {
1825 mbedtls_mpi_free( &conf->dhm_P );
1826 mbedtls_mpi_free( &conf->dhm_G );
1827 return( ret );
1828 }
1829
1830 return( 0 );
1831}
Paul Bakker5121ce52009-01-03 21:22:43 +00001832
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001833int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00001834{
Janos Follath865b3eb2019-12-16 11:46:15 +00001835 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00001836
Glenn Strausscee11292021-12-20 01:43:17 -05001837 mbedtls_mpi_free( &conf->dhm_P );
1838 mbedtls_mpi_free( &conf->dhm_G );
1839
Gilles Peskinee5702482021-06-11 21:59:08 +02001840 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
1841 &conf->dhm_P ) ) != 0 ||
1842 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
1843 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01001844 {
1845 mbedtls_mpi_free( &conf->dhm_P );
1846 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00001847 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01001848 }
Paul Bakker1b57b062011-01-06 15:48:19 +00001849
1850 return( 0 );
1851}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02001852#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00001853
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02001854#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
1855/*
1856 * Set the minimum length for Diffie-Hellman parameters
1857 */
1858void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
1859 unsigned int bitlen )
1860{
1861 conf->dhm_min_bitlen = bitlen;
1862}
1863#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
1864
Gilles Peskineeccd8882020-03-10 12:19:08 +01001865#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08001866#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02001867/*
1868 * Set allowed/preferred hashes for handshake signatures
1869 */
1870void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
1871 const int *hashes )
1872{
1873 conf->sig_hashes = hashes;
1874}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08001875#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01001876
Jerry Yuf017ee42022-01-12 15:49:48 +08001877/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01001878void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
1879 const uint16_t* sig_algs )
1880{
Jerry Yuf017ee42022-01-12 15:49:48 +08001881#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1882 conf->sig_hashes = NULL;
1883#endif /* !MBEDTLS_DEPRECATED_REMOVED */
1884 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01001885}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001886#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02001887
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02001888#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01001889#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001890/*
1891 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01001892 *
1893 * mbedtls_ssl_setup() takes the provided list
1894 * and translates it to a list of IANA TLS group identifiers,
1895 * stored in ssl->handshake->group_list.
1896 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001897 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001898void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001899 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001900{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001901 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01001902 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001903}
Brett Warrene0edc842021-08-17 09:53:13 +01001904#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01001905#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001906
Brett Warrene0edc842021-08-17 09:53:13 +01001907/*
1908 * Set the allowed groups
1909 */
1910void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
1911 const uint16_t *group_list )
1912{
1913#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
1914 conf->curve_list = NULL;
1915#endif
1916 conf->group_list = group_list;
1917}
1918
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001919#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00001921{
Hanno Becker947194e2017-04-07 13:25:49 +01001922 /* Initialize to suppress unnecessary compiler warning */
1923 size_t hostname_len = 0;
1924
1925 /* Check if new hostname is valid before
1926 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01001927 if( hostname != NULL )
1928 {
1929 hostname_len = strlen( hostname );
1930
1931 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
1932 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1933 }
1934
1935 /* Now it's clear that we will overwrite the old hostname,
1936 * so we can free it safely */
1937
1938 if( ssl->hostname != NULL )
1939 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001940 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01001941 mbedtls_free( ssl->hostname );
1942 }
1943
1944 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01001945
Paul Bakker5121ce52009-01-03 21:22:43 +00001946 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01001947 {
1948 ssl->hostname = NULL;
1949 }
1950 else
1951 {
1952 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01001953 if( ssl->hostname == NULL )
1954 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02001955
Hanno Becker947194e2017-04-07 13:25:49 +01001956 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02001957
Hanno Becker947194e2017-04-07 13:25:49 +01001958 ssl->hostname[hostname_len] = '\0';
1959 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001960
1961 return( 0 );
1962}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01001963#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001964
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001965#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001966void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00001968 const unsigned char *, size_t),
1969 void *p_sni )
1970{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001971 conf->f_sni = f_sni;
1972 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00001973}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001977int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001978{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001979 size_t cur_len, tot_len;
1980 const char **p;
1981
1982 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08001983 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
1984 * MUST NOT be truncated."
1985 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001986 */
1987 tot_len = 0;
1988 for( p = protos; *p != NULL; p++ )
1989 {
1990 cur_len = strlen( *p );
1991 tot_len += cur_len;
1992
Ronald Cron8216dd32020-04-23 16:41:44 +02001993 if( ( cur_len == 0 ) ||
1994 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
1995 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001997 }
1998
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001999 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002000
2001 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002002}
2003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002005{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002006 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002007}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002009
Johan Pascalb62bb512015-12-03 21:56:45 +01002010#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002011void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2012 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002013{
2014 conf->dtls_srtp_mki_support = support_mki_value;
2015}
2016
Ron Eldoref72faf2018-07-12 11:54:20 +03002017int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2018 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002019 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002020{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002021 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002022 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002023 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002024 }
Ron Eldor591f1622018-01-22 12:30:04 +02002025
2026 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002027 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002028 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002029 }
Ron Eldor591f1622018-01-22 12:30:04 +02002030
2031 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2032 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002033 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002034}
2035
Ron Eldoref72faf2018-07-12 11:54:20 +03002036int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002037 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002038{
Johan Pascal253d0262020-09-22 13:04:45 +02002039 const mbedtls_ssl_srtp_profile *p;
2040 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002041
Johan Pascal253d0262020-09-22 13:04:45 +02002042 /* check the profiles list: all entry must be valid,
2043 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002044 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2045 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2046 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002047 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002048 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002049 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002050 list_size++;
2051 }
2052 else
2053 {
2054 /* unsupported value, stop parsing and set the size to an error value */
2055 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002056 }
2057 }
2058
Johan Pascal5ef72d22020-10-28 17:05:47 +01002059 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002060 {
Johan Pascal253d0262020-09-22 13:04:45 +02002061 conf->dtls_srtp_profile_list = NULL;
2062 conf->dtls_srtp_profile_list_len = 0;
2063 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2064 }
2065
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002066 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002067 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002068
2069 return( 0 );
2070}
2071
Johan Pascal5ef72d22020-10-28 17:05:47 +01002072void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2073 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002074{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002075 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2076 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002077 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002078 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002079 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002080 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002081 else
2082 {
2083 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002084 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2085 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002086 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002087}
Johan Pascalb62bb512015-12-03 21:56:45 +01002088#endif /* MBEDTLS_SSL_DTLS_SRTP */
2089
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002090void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002091{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002092 conf->max_major_ver = major;
2093 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002094}
2095
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002096void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002097{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002098 conf->min_major_ver = major;
2099 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002100}
2101
Janos Follath088ce432017-04-10 12:42:31 +01002102#if defined(MBEDTLS_SSL_SRV_C)
2103void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2104 char cert_req_ca_list )
2105{
2106 conf->cert_req_ca_list = cert_req_ca_list;
2107}
2108#endif
2109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002111void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002112{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002113 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002114}
2115#endif
2116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002118void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002119{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002120 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002121}
2122#endif
2123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002125int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002126{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002128 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002131 }
2132
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002133 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002134
2135 return( 0 );
2136}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002138
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002139void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002140{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002141 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002142}
2143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002145void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002146{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002147 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002148}
2149
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002150void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002151{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002152 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002153}
2154
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002155void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002156 const unsigned char period[8] )
2157{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002158 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002159}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002163#if defined(MBEDTLS_SSL_CLI_C)
2164void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002165{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002166 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002167}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002168#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002169
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002170#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002171void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2172 mbedtls_ssl_ticket_write_t *f_ticket_write,
2173 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2174 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002175{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002176 conf->f_ticket_write = f_ticket_write;
2177 conf->f_ticket_parse = f_ticket_parse;
2178 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002179}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002180#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002182
Hanno Becker7e6c1782021-06-08 09:24:55 +01002183void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2184 mbedtls_ssl_export_keys_t *f_export_keys,
2185 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002186{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002187 ssl->f_export_keys = f_export_keys;
2188 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002189}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002190
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002191#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002192void mbedtls_ssl_conf_async_private_cb(
2193 mbedtls_ssl_config *conf,
2194 mbedtls_ssl_async_sign_t *f_async_sign,
2195 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2196 mbedtls_ssl_async_resume_t *f_async_resume,
2197 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002198 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002199{
2200 conf->f_async_sign_start = f_async_sign;
2201 conf->f_async_decrypt_start = f_async_decrypt;
2202 conf->f_async_resume = f_async_resume;
2203 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002204 conf->p_async_config_data = async_config_data;
2205}
2206
Gilles Peskine8f97af72018-04-26 11:46:10 +02002207void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2208{
2209 return( conf->p_async_config_data );
2210}
2211
Gilles Peskine1febfef2018-04-30 11:54:39 +02002212void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002213{
2214 if( ssl->handshake == NULL )
2215 return( NULL );
2216 else
2217 return( ssl->handshake->user_async_ctx );
2218}
2219
Gilles Peskine1febfef2018-04-30 11:54:39 +02002220void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002221 void *ctx )
2222{
2223 if( ssl->handshake != NULL )
2224 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002225}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002226#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002227
Paul Bakker5121ce52009-01-03 21:22:43 +00002228/*
2229 * SSL get accessors
2230 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002231uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002232{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002233 if( ssl->session != NULL )
2234 return( ssl->session->verify_result );
2235
2236 if( ssl->session_negotiate != NULL )
2237 return( ssl->session_negotiate->verify_result );
2238
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002239 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002240}
2241
Glenn Strauss8f526902022-01-13 00:04:49 -05002242int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2243{
2244 if( ssl == NULL || ssl->session == NULL )
2245 return( 0 );
2246
2247 return( ssl->session->ciphersuite );
2248}
2249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002251{
Paul Bakker926c8e42013-03-06 10:23:34 +01002252 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002253 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002256}
2257
Gilles Peskinee1a0c252022-01-13 01:08:03 +01002258mbedtls_ssl_protocol_version mbedtls_ssl_get_version_number(
2259 const mbedtls_ssl_context *ssl )
2260{
2261 /* For major_ver, only 3 is supported, so skip checking it. */
2262 switch( ssl->minor_ver )
2263 {
2264 case MBEDTLS_SSL_MINOR_VERSION_3:
2265 return( MBEDTLS_SSL_VERSION_1_2 );
2266 case MBEDTLS_SSL_MINOR_VERSION_4:
2267 return( MBEDTLS_SSL_VERSION_1_3 );
2268 default:
2269 return( MBEDTLS_SSL_VERSION_UNKNOWN );
2270 }
2271}
2272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002274{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002276 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002277 {
2278 switch( ssl->minor_ver )
2279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002281 return( "DTLSv1.2" );
2282
2283 default:
2284 return( "unknown (DTLS)" );
2285 }
2286 }
2287#endif
2288
Paul Bakker43ca69c2011-01-15 17:35:19 +00002289 switch( ssl->minor_ver )
2290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002292 return( "TLSv1.2" );
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002293 case MBEDTLS_SSL_MINOR_VERSION_4:
2294 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002295 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002296 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002297 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002298}
2299
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002300#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002301size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2302{
David Horstmann95d516f2021-05-04 18:36:56 +01002303 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002304 size_t read_mfl;
2305
2306 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2307 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2308 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2309 {
2310 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2311 }
2312
2313 /* Check if a smaller max length was negotiated */
2314 if( ssl->session_out != NULL )
2315 {
2316 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2317 if( read_mfl < max_len )
2318 {
2319 max_len = read_mfl;
2320 }
2321 }
2322
2323 // During a handshake, use the value being negotiated
2324 if( ssl->session_negotiate != NULL )
2325 {
2326 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2327 if( read_mfl < max_len )
2328 {
2329 max_len = read_mfl;
2330 }
2331 }
2332
2333 return( max_len );
2334}
2335
2336size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002337{
2338 size_t max_len;
2339
2340 /*
2341 * Assume mfl_code is correct since it was checked when set
2342 */
Angus Grattond8213d02016-05-25 20:56:48 +10002343 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002344
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002345 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002346 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002347 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002348 {
Angus Grattond8213d02016-05-25 20:56:48 +10002349 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002350 }
2351
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002352 /* During a handshake, use the value being negotiated */
2353 if( ssl->session_negotiate != NULL &&
2354 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2355 {
2356 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2357 }
2358
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002359 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002360}
2361#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2362
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002363#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002364size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002365{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002366 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2367 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2368 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2369 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2370 return ( 0 );
2371
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002372 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2373 return( ssl->mtu );
2374
2375 if( ssl->mtu == 0 )
2376 return( ssl->handshake->mtu );
2377
2378 return( ssl->mtu < ssl->handshake->mtu ?
2379 ssl->mtu : ssl->handshake->mtu );
2380}
2381#endif /* MBEDTLS_SSL_PROTO_DTLS */
2382
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002383int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2384{
2385 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2386
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002387#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2388 !defined(MBEDTLS_SSL_PROTO_DTLS)
2389 (void) ssl;
2390#endif
2391
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002392#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002393 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002394
2395 if( max_len > mfl )
2396 max_len = mfl;
2397#endif
2398
2399#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002400 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002401 {
Hanno Becker89490712020-02-05 10:50:12 +00002402 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002403 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2404 const size_t overhead = (size_t) ret;
2405
2406 if( ret < 0 )
2407 return( ret );
2408
2409 if( mtu <= overhead )
2410 {
2411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2412 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2413 }
2414
2415 if( max_len > mtu - overhead )
2416 max_len = mtu - overhead;
2417 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002418#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002419
Hanno Becker0defedb2018-08-10 12:35:02 +01002420#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2421 !defined(MBEDTLS_SSL_PROTO_DTLS)
2422 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002423#endif
2424
2425 return( (int) max_len );
2426}
2427
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002428int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2429{
2430 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2431
2432#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2433 (void) ssl;
2434#endif
2435
2436#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2437 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2438
2439 if( max_len > mfl )
2440 max_len = mfl;
2441#endif
2442
2443 return( (int) max_len );
2444}
2445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446#if defined(MBEDTLS_X509_CRT_PARSE_C)
2447const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002448{
2449 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002450 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002451
Hanno Beckere6824572019-02-07 13:18:46 +00002452#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002453 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002454#else
2455 return( NULL );
2456#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002457}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002461int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2462 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002463{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002464 int ret;
2465
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002466 if( ssl == NULL ||
2467 dst == NULL ||
2468 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002469 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002472 }
2473
Hanno Beckere810bbc2021-05-14 16:01:05 +01002474 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2475 * idempotent: Each session can only be exported once.
2476 *
2477 * (This is in preparation for TLS 1.3 support where we will
2478 * need the ability to export multiple sessions (aka tickets),
2479 * which will be achieved by calling mbedtls_ssl_get_session()
2480 * multiple times until it fails.)
2481 *
2482 * Check whether we have already exported the current session,
2483 * and fail if so.
2484 */
2485 if( ssl->session->exported == 1 )
2486 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2487
2488 ret = mbedtls_ssl_session_copy( dst, ssl->session );
2489 if( ret != 0 )
2490 return( ret );
2491
2492 /* Remember that we've exported the session. */
2493 ssl->session->exported = 1;
2494 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002495}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002497
Paul Bakker5121ce52009-01-03 21:22:43 +00002498/*
Hanno Beckera835da52019-05-16 12:39:07 +01002499 * Define ticket header determining Mbed TLS version
2500 * and structure of the ticket.
2501 */
2502
Hanno Becker94ef3b32019-05-16 12:50:45 +01002503/*
Hanno Becker50b59662019-05-28 14:30:45 +01002504 * Define bitflag determining compile-time settings influencing
2505 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01002506 */
2507
Hanno Becker50b59662019-05-28 14:30:45 +01002508#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01002509#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01002510#else
Hanno Becker3e088662019-05-29 11:10:18 +01002511#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002512#endif /* MBEDTLS_HAVE_TIME */
2513
2514#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01002515#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002516#else
Hanno Becker3e088662019-05-29 11:10:18 +01002517#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002518#endif /* MBEDTLS_X509_CRT_PARSE_C */
2519
2520#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01002521#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002522#else
Hanno Becker3e088662019-05-29 11:10:18 +01002523#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002524#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
2525
2526#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01002527#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002528#else
Hanno Becker3e088662019-05-29 11:10:18 +01002529#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002530#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2531
Hanno Becker94ef3b32019-05-16 12:50:45 +01002532#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01002533#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002534#else
Hanno Becker3e088662019-05-29 11:10:18 +01002535#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002536#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2537
Hanno Becker94ef3b32019-05-16 12:50:45 +01002538#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2539#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
2540#else
2541#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
2542#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2543
Hanno Becker3e088662019-05-29 11:10:18 +01002544#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
2545#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
2546#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
2547#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01002548#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
2549#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01002550
Hanno Becker50b59662019-05-28 14:30:45 +01002551#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01002552 ( (uint16_t) ( \
2553 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
2554 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
2555 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
2556 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01002557 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01002558 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01002559
Hanno Beckerf8787072019-05-16 12:41:07 +01002560static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01002561 MBEDTLS_VERSION_MAJOR,
2562 MBEDTLS_VERSION_MINOR,
2563 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01002564 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
2565 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01002566};
Hanno Beckera835da52019-05-16 12:39:07 +01002567
2568/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002569 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002570 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002571 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002572 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002573 *
Hanno Beckerdce50972021-08-01 05:39:23 +01002574 * opaque mbedtls_version[3]; // library version: major, minor, patch
2575 * opaque session_format[2]; // library-version specific 16-bit field
2576 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002577 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002578 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002579 * Note: When updating the format, remember to keep
2580 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002581 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002582 * // In this version, `session_format` determines
2583 * // the setting of those compile-time
2584 * // configuration options which influence
2585 * // the structure of mbedtls_ssl_session.
2586 *
Hanno Beckerfa0d61e2021-08-02 08:56:14 +01002587 * uint8_t minor_ver; // Protocol-version. Possible values:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002588 * // - TLS 1.2 (MBEDTLS_SSL_MINOR_VERSION_3)
2589 *
2590 * select (serialized_session.minor_ver) {
2591 *
2592 * case MBEDTLS_SSL_MINOR_VERSION_3: // TLS 1.2
2593 * serialized_session_tls12 data;
2594 *
2595 * };
2596 *
2597 * } serialized_session;
2598 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002599 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002600
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002601static int ssl_session_save( const mbedtls_ssl_session *session,
2602 unsigned char omit_header,
2603 unsigned char *buf,
2604 size_t buf_len,
2605 size_t *olen )
2606{
2607 unsigned char *p = buf;
2608 size_t used = 0;
2609
2610 if( !omit_header )
2611 {
2612 /*
2613 * Add Mbed TLS version identifier
2614 */
2615
2616 used += sizeof( ssl_serialized_session_header );
2617
2618 if( used <= buf_len )
2619 {
2620 memcpy( p, ssl_serialized_session_header,
2621 sizeof( ssl_serialized_session_header ) );
2622 p += sizeof( ssl_serialized_session_header );
2623 }
2624 }
2625
2626 /*
2627 * TLS version identifier
2628 */
2629 used += 1;
2630 if( used <= buf_len )
2631 {
2632 *p++ = session->minor_ver;
2633 }
2634
2635 /* Forward to version-specific serialization routine. */
2636 switch( session->minor_ver )
2637 {
2638#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2639 case MBEDTLS_SSL_MINOR_VERSION_3:
2640 {
2641 size_t remaining_len = used <= buf_len ? buf_len - used : 0;
2642 used += ssl_session_save_tls12( session, p, remaining_len );
2643 break;
2644 }
2645#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2646
2647 default:
2648 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2649 }
2650
2651 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02002652 if( used > buf_len )
2653 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002654
2655 return( 0 );
2656}
2657
2658/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02002659 * Public wrapper for ssl_session_save()
2660 */
2661int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
2662 unsigned char *buf,
2663 size_t buf_len,
2664 size_t *olen )
2665{
2666 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
2667}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002668
Jerry Yuf1b23ca2022-02-18 11:48:47 +08002669/*
2670 * Deserialize session, see mbedtls_ssl_session_save() for format.
2671 *
2672 * This internal version is wrapped by a public function that cleans up in
2673 * case of error, and has an extra option omit_header.
2674 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002675static int ssl_session_load( mbedtls_ssl_session *session,
2676 unsigned char omit_header,
2677 const unsigned char *buf,
2678 size_t len )
2679{
2680 const unsigned char *p = buf;
2681 const unsigned char * const end = buf + len;
2682
2683 if( !omit_header )
2684 {
2685 /*
2686 * Check Mbed TLS version identifier
2687 */
2688
2689 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
2690 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2691
2692 if( memcmp( p, ssl_serialized_session_header,
2693 sizeof( ssl_serialized_session_header ) ) != 0 )
2694 {
2695 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
2696 }
2697 p += sizeof( ssl_serialized_session_header );
2698 }
2699
2700 /*
2701 * TLS version identifier
2702 */
2703 if( 1 > (size_t)( end - p ) )
2704 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2705 session->minor_ver = *p++;
2706
2707 /* Dispatch according to TLS version. */
2708 switch( session->minor_ver )
2709 {
2710#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2711 case MBEDTLS_SSL_MINOR_VERSION_3: /* TLS 1.2 */
2712 {
2713 size_t remaining_len = ( end - p );
2714 return( ssl_session_load_tls12( session, p, remaining_len ) );
2715 }
2716#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2717
2718 default:
2719 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2720 }
2721}
2722
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002723/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02002724 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02002725 */
2726int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
2727 const unsigned char *buf,
2728 size_t len )
2729{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02002730 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02002731
2732 if( ret != 0 )
2733 mbedtls_ssl_session_free( session );
2734
2735 return( ret );
2736}
2737
2738/*
Paul Bakker1961b702013-01-25 14:49:24 +01002739 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00002740 */
Hanno Becker41934dd2021-08-07 19:13:43 +01002741static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
2742{
2743 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2744
Ronald Cron66dbf912022-02-02 15:33:46 +01002745 /*
2746 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01002747 * that were written into the output buffer by the previous handshake step,
2748 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01002749 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
2750 * We proceed to the next handshake step only when all data from the
2751 * previous one have been sent to the peer, thus we make sure that this is
2752 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
2753 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
2754 * we have to wait before to go ahead.
2755 * In the case of TLS 1.3, handshake step handlers do not send data to the
2756 * peer. Data are only sent here and through
2757 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
2758 * alert occured.
2759 */
Hanno Becker41934dd2021-08-07 19:13:43 +01002760 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2761 return( ret );
2762
2763#if defined(MBEDTLS_SSL_PROTO_DTLS)
2764 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2765 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
2766 {
2767 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
2768 return( ret );
2769 }
2770#endif /* MBEDTLS_SSL_PROTO_DTLS */
2771
2772 return( ret );
2773}
2774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002776{
Hanno Becker41934dd2021-08-07 19:13:43 +01002777 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002778
Hanno Becker41934dd2021-08-07 19:13:43 +01002779 if( ssl == NULL ||
2780 ssl->conf == NULL ||
2781 ssl->handshake == NULL ||
2782 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2783 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002784 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01002785 }
2786
2787 ret = ssl_prepare_handshake_step( ssl );
2788 if( ret != 0 )
2789 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002790
Jerry Yue7047812021-09-13 19:26:39 +08002791 ret = mbedtls_ssl_handle_pending_alert( ssl );
2792 if( ret != 0 )
2793 goto cleanup;
2794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002796 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08002797 {
Ronald Cron6f135e12021-12-08 16:57:54 +01002798#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08002799 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yuf4436812021-08-26 22:59:56 +08002800 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01002801#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08002802
2803#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2804 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
2805 ret = mbedtls_ssl_handshake_client_step( ssl );
2806#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2807 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002808#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002809#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002810 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08002811 {
Ronald Cron6f135e12021-12-08 16:57:54 +01002812#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08002813 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08002814 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01002815#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08002816
2817#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2818 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
2819 ret = mbedtls_ssl_handshake_server_step( ssl );
2820#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2821 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002822#endif
2823
Jerry Yue7047812021-09-13 19:26:39 +08002824 if( ret != 0 )
2825 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08002826 /* handshake_step return error. And it is same
2827 * with alert_reason.
2828 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08002829 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08002830 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08002831 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08002832 goto cleanup;
2833 }
2834 }
2835
2836cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01002837 return( ret );
2838}
2839
2840/*
2841 * Perform the SSL handshake
2842 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01002844{
2845 int ret = 0;
2846
Hanno Beckera817ea42020-10-20 15:20:23 +01002847 /* Sanity checks */
2848
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002849 if( ssl == NULL || ssl->conf == NULL )
2850 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2851
Hanno Beckera817ea42020-10-20 15:20:23 +01002852#if defined(MBEDTLS_SSL_PROTO_DTLS)
2853 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2854 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
2855 {
2856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2857 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2858 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2859 }
2860#endif /* MBEDTLS_SSL_PROTO_DTLS */
2861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01002863
Hanno Beckera817ea42020-10-20 15:20:23 +01002864 /* Main handshake loop */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002865 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01002866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002868
2869 if( ret != 0 )
2870 break;
2871 }
2872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002874
2875 return( ret );
2876}
2877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878#if defined(MBEDTLS_SSL_RENEGOTIATION)
2879#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002880/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002881 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00002882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002884{
Janos Follath865b3eb2019-12-16 11:46:15 +00002885 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002888
2889 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2891 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002892
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002893 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002894 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002895 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002896 return( ret );
2897 }
2898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002900
2901 return( 0 );
2902}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002904
2905/*
2906 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 * - any side: calling mbedtls_ssl_renegotiate(),
2908 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
2909 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02002910 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002911 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002913 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00002914int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00002915{
Janos Follath865b3eb2019-12-16 11:46:15 +00002916 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00002917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002919
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002920 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
2921 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00002922
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02002923 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
2924 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002926 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002927 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02002928 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002929 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002930 ssl->handshake->out_msg_seq = 1;
2931 else
2932 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02002933 }
2934#endif
2935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002936 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
2937 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00002938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00002940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002941 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00002942 return( ret );
2943 }
2944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002946
2947 return( 0 );
2948}
2949
2950/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002951 * Renegotiate current connection on client,
2952 * or request renegotiation on server
2953 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002955{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002956 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002957
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002958 if( ssl == NULL || ssl->conf == NULL )
2959 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002962 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002963 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002965 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
2966 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02002969
2970 /* Did we already try/start sending HelloRequest? */
2971 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02002973
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002974 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002975 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002979 /*
2980 * On client, either start the renegotiation process or,
2981 * if already in progress, continue the handshake
2982 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
2986 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002987
Hanno Becker40cdaa12020-02-05 10:48:27 +00002988 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002989 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00002990 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002991 return( ret );
2992 }
2993 }
2994 else
2995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002998 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002999 return( ret );
3000 }
3001 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003003
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003004 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003005}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003006#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003007
Gilles Peskine9b562d52018-04-25 20:32:43 +02003008void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003009{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003010 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3011
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003012 if( handshake == NULL )
3013 return;
3014
Brett Warrene0edc842021-08-17 09:53:13 +01003015#if defined(MBEDTLS_ECP_C)
3016#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3017 if ( ssl->handshake->group_list_heap_allocated )
3018 mbedtls_free( (void*) handshake->group_list );
3019 handshake->group_list = NULL;
3020#endif /* MBEDTLS_DEPRECATED_REMOVED */
3021#endif /* MBEDTLS_ECP_C */
3022
Jerry Yuf017ee42022-01-12 15:49:48 +08003023#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3024#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3025 if ( ssl->handshake->sig_algs_heap_allocated )
3026 mbedtls_free( (void*) handshake->sig_algs );
3027 handshake->sig_algs = NULL;
3028#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003029#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3030 if( ssl->handshake->certificate_request_context )
3031 {
3032 mbedtls_free( (void*) handshake->certificate_request_context );
3033 }
3034#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003035#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3036
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003037#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3038 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3039 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003040 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003041 handshake->async_in_progress = 0;
3042 }
3043#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3044
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003045#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003046#if defined(MBEDTLS_USE_PSA_CRYPTO)
3047 psa_hash_abort( &handshake->fin_sha256_psa );
3048#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003049 mbedtls_sha256_free( &handshake->fin_sha256 );
3050#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003051#endif
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +02003052#if defined(MBEDTLS_SHA384_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003053#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003054 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003055#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003056 mbedtls_sha512_free( &handshake->fin_sha512 );
3057#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003058#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060#if defined(MBEDTLS_DHM_C)
3061 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003062#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003063#if defined(MBEDTLS_ECDH_C)
3064 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003065#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003066#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003067 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003068#if defined(MBEDTLS_SSL_CLI_C)
3069 mbedtls_free( handshake->ecjpake_cache );
3070 handshake->ecjpake_cache = NULL;
3071 handshake->ecjpake_cache_len = 0;
3072#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003073#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003074
Janos Follath4ae5c292016-02-10 11:27:43 +00003075#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3076 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003077 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003079#endif
3080
Gilles Peskineeccd8882020-03-10 12:19:08 +01003081#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003082 if( handshake->psk != NULL )
3083 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003084 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003085 mbedtls_free( handshake->psk );
3086 }
3087#endif
3088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3090 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003091 /*
3092 * Free only the linked list wrapper, not the keys themselves
3093 * since the belong to the SNI callback
3094 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003095 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003097
Gilles Peskineeccd8882020-03-10 12:19:08 +01003098#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003099 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003100 if( handshake->ecrs_peer_cert != NULL )
3101 {
3102 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3103 mbedtls_free( handshake->ecrs_peer_cert );
3104 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003105#endif
3106
Hanno Becker75173122019-02-06 16:18:31 +00003107#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3108 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3109 mbedtls_pk_free( &handshake->peer_pubkey );
3110#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3111
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003112#if defined(MBEDTLS_SSL_CLI_C) && \
3113 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3114 mbedtls_free( handshake->cookie );
3115#endif /* MBEDTLS_SSL_CLI_C &&
3116 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003117
3118#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003119 mbedtls_ssl_flight_free( handshake->flight );
3120 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003121#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003122
Ronald Cronf12b81d2022-03-15 10:42:41 +01003123#if defined(MBEDTLS_ECDH_C) && \
3124 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Hanno Becker4a63ed42019-01-08 11:39:35 +00003125 psa_destroy_key( handshake->ecdh_psa_privkey );
3126#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3127
Ronald Cron6f135e12021-12-08 16:57:54 +01003128#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003129 mbedtls_ssl_transform_free( handshake->transform_handshake );
3130 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003131 mbedtls_free( handshake->transform_earlydata );
3132 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003133#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003134
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003135
3136#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3137 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3138 * processes datagrams and the fact that a datagram is allowed to have
3139 * several records in it, it is possible that the I/O buffers are not
3140 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003141 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3142 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003143#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003144
Jerry Yuba9c7272021-10-30 11:54:10 +08003145 /* mbedtls_platform_zeroize MUST be last one in this function */
3146 mbedtls_platform_zeroize( handshake,
3147 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003148}
3149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003151{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003152 if( session == NULL )
3153 return;
3154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003156 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003157#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003158
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003159#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003160 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003161#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003162
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003163 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003164}
3165
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003166#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003167
3168#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3169#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3170#else
3171#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3172#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3173
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003174#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003175
3176#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3177#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3178#else
3179#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3180#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3181
3182#if defined(MBEDTLS_SSL_ALPN)
3183#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3184#else
3185#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3186#endif /* MBEDTLS_SSL_ALPN */
3187
3188#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3189#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3190#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3191#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3192
3193#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3194 ( (uint32_t) ( \
3195 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3196 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3197 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3198 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3199 0u ) )
3200
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003201static unsigned char ssl_serialized_context_header[] = {
3202 MBEDTLS_VERSION_MAJOR,
3203 MBEDTLS_VERSION_MINOR,
3204 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003205 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3206 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3207 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3208 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3209 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003210};
3211
Paul Bakker5121ce52009-01-03 21:22:43 +00003212/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003213 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003214 *
3215 * The format of the serialized data is:
3216 * (in the presentation language of TLS, RFC 8446 section 3)
3217 *
3218 * // header
3219 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003220 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003221 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003222 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003223 * Note: When updating the format, remember to keep these
3224 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003225 *
3226 * // session sub-structure
3227 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3228 * // transform sub-structure
3229 * uint8 random[64]; // ServerHello.random+ClientHello.random
3230 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3231 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3232 * // fields from ssl_context
3233 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3234 * uint64 in_window_top; // DTLS: last validated record seq_num
3235 * uint64 in_window; // DTLS: bitmask for replay protection
3236 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3237 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3238 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3239 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3240 *
3241 * Note that many fields of the ssl_context or sub-structures are not
3242 * serialized, as they fall in one of the following categories:
3243 *
3244 * 1. forced value (eg in_left must be 0)
3245 * 2. pointer to dynamically-allocated memory (eg session, transform)
3246 * 3. value can be re-derived from other data (eg session keys from MS)
3247 * 4. value was temporary (eg content of input buffer)
3248 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003249 */
3250int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3251 unsigned char *buf,
3252 size_t buf_len,
3253 size_t *olen )
3254{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003255 unsigned char *p = buf;
3256 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003257 size_t session_len;
3258 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003259
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003260 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003261 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3262 * this function's documentation.
3263 *
3264 * These are due to assumptions/limitations in the implementation. Some of
3265 * them are likely to stay (no handshake in progress) some might go away
3266 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003267 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003268 /* The initial handshake must be over */
3269 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003270 {
3271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003272 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003273 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003274 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003275 {
3276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003277 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003278 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003279 /* Double-check that sub-structures are indeed ready */
3280 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003281 {
3282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003283 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003284 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003285 /* There must be no pending incoming or outgoing data */
3286 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003287 {
3288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003289 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003290 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003291 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003292 {
3293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003294 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003295 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003296 /* Protocol must be DLTS, not TLS */
3297 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003298 {
3299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003300 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003301 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003302 /* Version must be 1.2 */
3303 if( ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003304 {
3305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003306 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003307 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003308 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003309 {
3310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003311 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003312 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003313 /* We must be using an AEAD ciphersuite */
3314 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003315 {
3316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003317 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003318 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003319 /* Renegotiation must not be enabled */
3320#if defined(MBEDTLS_SSL_RENEGOTIATION)
3321 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003322 {
3323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
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#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003327
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003328 /*
3329 * Version and format identifier
3330 */
3331 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003332
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003333 if( used <= buf_len )
3334 {
3335 memcpy( p, ssl_serialized_context_header,
3336 sizeof( ssl_serialized_context_header ) );
3337 p += sizeof( ssl_serialized_context_header );
3338 }
3339
3340 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003341 * Session (length + data)
3342 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003343 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003344 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3345 return( ret );
3346
3347 used += 4 + session_len;
3348 if( used <= buf_len )
3349 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003350 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3351 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003352
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003353 ret = ssl_session_save( ssl->session, 1,
3354 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003355 if( ret != 0 )
3356 return( ret );
3357
3358 p += session_len;
3359 }
3360
3361 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003362 * Transform
3363 */
3364 used += sizeof( ssl->transform->randbytes );
3365 if( used <= buf_len )
3366 {
3367 memcpy( p, ssl->transform->randbytes,
3368 sizeof( ssl->transform->randbytes ) );
3369 p += sizeof( ssl->transform->randbytes );
3370 }
3371
3372#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3373 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3374 if( used <= buf_len )
3375 {
3376 *p++ = ssl->transform->in_cid_len;
3377 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3378 p += ssl->transform->in_cid_len;
3379
3380 *p++ = ssl->transform->out_cid_len;
3381 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3382 p += ssl->transform->out_cid_len;
3383 }
3384#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3385
3386 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003387 * Saved fields from top-level ssl_context structure
3388 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003389 used += 4;
3390 if( used <= buf_len )
3391 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003392 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3393 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003394 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003395
3396#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3397 used += 16;
3398 if( used <= buf_len )
3399 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003400 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3401 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003402
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003403 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3404 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003405 }
3406#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3407
3408#if defined(MBEDTLS_SSL_PROTO_DTLS)
3409 used += 1;
3410 if( used <= buf_len )
3411 {
3412 *p++ = ssl->disable_datagram_packing;
3413 }
3414#endif /* MBEDTLS_SSL_PROTO_DTLS */
3415
Jerry Yuae0b2e22021-10-08 15:21:19 +08003416 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003417 if( used <= buf_len )
3418 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003419 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3420 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003421 }
3422
3423#if defined(MBEDTLS_SSL_PROTO_DTLS)
3424 used += 2;
3425 if( used <= buf_len )
3426 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003427 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
3428 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003429 }
3430#endif /* MBEDTLS_SSL_PROTO_DTLS */
3431
3432#if defined(MBEDTLS_SSL_ALPN)
3433 {
3434 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02003435 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003436 : 0;
3437
3438 used += 1 + alpn_len;
3439 if( used <= buf_len )
3440 {
3441 *p++ = alpn_len;
3442
3443 if( ssl->alpn_chosen != NULL )
3444 {
3445 memcpy( p, ssl->alpn_chosen, alpn_len );
3446 p += alpn_len;
3447 }
3448 }
3449 }
3450#endif /* MBEDTLS_SSL_ALPN */
3451
3452 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003453 * Done
3454 */
3455 *olen = used;
3456
3457 if( used > buf_len )
3458 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003459
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003460 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
3461
Hanno Becker43aefe22020-02-05 10:44:56 +00003462 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003463}
3464
3465/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003466 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003467 *
3468 * This internal version is wrapped by a public function that cleans up in
3469 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003470 */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003471static int ssl_context_load( mbedtls_ssl_context *ssl,
3472 const unsigned char *buf,
3473 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003474{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003475 const unsigned char *p = buf;
3476 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003477 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00003478 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003479
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003480 /*
3481 * The context should have been freshly setup or reset.
3482 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02003483 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003484 * renegotiating, or if the user mistakenly loaded a session first.)
3485 */
3486 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
3487 ssl->session != NULL )
3488 {
3489 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3490 }
3491
3492 /*
3493 * We can't check that the config matches the initial one, but we can at
3494 * least check it matches the requirements for serializing.
3495 */
3496 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
3497 ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
3498 ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 ||
3499 ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ||
3500 ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 ||
3501#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003502 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003503#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003504 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003505 {
3506 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3507 }
3508
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003509 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
3510
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003511 /*
3512 * Check version identifier
3513 */
3514 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
3515 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3516
3517 if( memcmp( p, ssl_serialized_context_header,
3518 sizeof( ssl_serialized_context_header ) ) != 0 )
3519 {
3520 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3521 }
3522 p += sizeof( ssl_serialized_context_header );
3523
3524 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003525 * Session
3526 */
3527 if( (size_t)( end - p ) < 4 )
3528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3529
3530 session_len = ( (size_t) p[0] << 24 ) |
3531 ( (size_t) p[1] << 16 ) |
3532 ( (size_t) p[2] << 8 ) |
3533 ( (size_t) p[3] );
3534 p += 4;
3535
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003536 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00003537 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003538 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003539 ssl->session_in = ssl->session;
3540 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003541 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003542
3543 if( (size_t)( end - p ) < session_len )
3544 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3545
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003546 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003547 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003548 {
3549 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003550 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003551 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003552
3553 p += session_len;
3554
3555 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003556 * Transform
3557 */
3558
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003559 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00003560 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003561 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003562 ssl->transform_in = ssl->transform;
3563 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003564 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003565
3566 /* Read random bytes and populate structure */
3567 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
3568 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08003569#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00003570 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003571 ssl->session->ciphersuite,
3572 ssl->session->master,
Hanno Beckerbd257552021-03-22 06:59:27 +00003573#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
3574 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003575 ssl->session->encrypt_then_mac,
Hanno Beckerbd257552021-03-22 06:59:27 +00003576#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
3577 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003578 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
3579 p, /* currently pointing to randbytes */
3580 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
3581 ssl->conf->endpoint,
3582 ssl );
3583 if( ret != 0 )
3584 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08003585#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003586 p += sizeof( ssl->transform->randbytes );
3587
3588#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3589 /* Read connection IDs and store them */
3590 if( (size_t)( end - p ) < 1 )
3591 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3592
3593 ssl->transform->in_cid_len = *p++;
3594
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02003595 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003596 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3597
3598 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
3599 p += ssl->transform->in_cid_len;
3600
3601 ssl->transform->out_cid_len = *p++;
3602
3603 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
3604 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3605
3606 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
3607 p += ssl->transform->out_cid_len;
3608#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3609
3610 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003611 * Saved fields from top-level ssl_context structure
3612 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003613 if( (size_t)( end - p ) < 4 )
3614 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3615
3616 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
3617 ( (uint32_t) p[1] << 16 ) |
3618 ( (uint32_t) p[2] << 8 ) |
3619 ( (uint32_t) p[3] );
3620 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003621
3622#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3623 if( (size_t)( end - p ) < 16 )
3624 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3625
3626 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
3627 ( (uint64_t) p[1] << 48 ) |
3628 ( (uint64_t) p[2] << 40 ) |
3629 ( (uint64_t) p[3] << 32 ) |
3630 ( (uint64_t) p[4] << 24 ) |
3631 ( (uint64_t) p[5] << 16 ) |
3632 ( (uint64_t) p[6] << 8 ) |
3633 ( (uint64_t) p[7] );
3634 p += 8;
3635
3636 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
3637 ( (uint64_t) p[1] << 48 ) |
3638 ( (uint64_t) p[2] << 40 ) |
3639 ( (uint64_t) p[3] << 32 ) |
3640 ( (uint64_t) p[4] << 24 ) |
3641 ( (uint64_t) p[5] << 16 ) |
3642 ( (uint64_t) p[6] << 8 ) |
3643 ( (uint64_t) p[7] );
3644 p += 8;
3645#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3646
3647#if defined(MBEDTLS_SSL_PROTO_DTLS)
3648 if( (size_t)( end - p ) < 1 )
3649 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3650
3651 ssl->disable_datagram_packing = *p++;
3652#endif /* MBEDTLS_SSL_PROTO_DTLS */
3653
Jerry Yud9a94fe2021-09-28 18:58:59 +08003654 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08003656 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
3657 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003658
3659#if defined(MBEDTLS_SSL_PROTO_DTLS)
3660 if( (size_t)( end - p ) < 2 )
3661 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3662
3663 ssl->mtu = ( p[0] << 8 ) | p[1];
3664 p += 2;
3665#endif /* MBEDTLS_SSL_PROTO_DTLS */
3666
3667#if defined(MBEDTLS_SSL_ALPN)
3668 {
3669 uint8_t alpn_len;
3670 const char **cur;
3671
3672 if( (size_t)( end - p ) < 1 )
3673 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3674
3675 alpn_len = *p++;
3676
3677 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
3678 {
3679 /* alpn_chosen should point to an item in the configured list */
3680 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
3681 {
3682 if( strlen( *cur ) == alpn_len &&
3683 memcmp( p, cur, alpn_len ) == 0 )
3684 {
3685 ssl->alpn_chosen = *cur;
3686 break;
3687 }
3688 }
3689 }
3690
3691 /* can only happen on conf mismatch */
3692 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
3693 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3694
3695 p += alpn_len;
3696 }
3697#endif /* MBEDTLS_SSL_ALPN */
3698
3699 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003700 * Forced fields from top-level ssl_context structure
3701 *
3702 * Most of them already set to the correct value by mbedtls_ssl_init() and
3703 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
3704 */
3705 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
3706
3707 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
3708 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
3709
Hanno Becker361b10d2019-08-30 10:42:49 +01003710 /* Adjust pointers for header fields of outgoing records to
3711 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00003712 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01003713
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003714#if defined(MBEDTLS_SSL_PROTO_DTLS)
3715 ssl->in_epoch = 1;
3716#endif
3717
3718 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
3719 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00003720 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
3721 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003722 if( ssl->handshake != NULL )
3723 {
3724 mbedtls_ssl_handshake_free( ssl );
3725 mbedtls_free( ssl->handshake );
3726 ssl->handshake = NULL;
3727 }
3728
3729 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003730 * Done - should have consumed entire buffer
3731 */
3732 if( p != end )
3733 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003734
3735 return( 0 );
3736}
3737
3738/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003739 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003740 */
3741int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
3742 const unsigned char *buf,
3743 size_t len )
3744{
3745 int ret = ssl_context_load( context, buf, len );
3746
3747 if( ret != 0 )
3748 mbedtls_ssl_free( context );
3749
3750 return( ret );
3751}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003752#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003753
3754/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003755 * Free an SSL context
3756 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003757void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003758{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003759 if( ssl == NULL )
3760 return;
3761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003763
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003764 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003765 {
sander-visserb8aa2072020-05-06 22:05:13 +02003766#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3767 size_t out_buf_len = ssl->out_buf_len;
3768#else
3769 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
3770#endif
3771
Darryl Greenb33cc762019-11-28 14:29:44 +00003772 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003773 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003774 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003775 }
3776
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003777 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003778 {
sander-visserb8aa2072020-05-06 22:05:13 +02003779#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3780 size_t in_buf_len = ssl->in_buf_len;
3781#else
3782 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
3783#endif
3784
Darryl Greenb33cc762019-11-28 14:29:44 +00003785 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003786 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003787 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003788 }
3789
Paul Bakker48916f92012-09-16 19:57:18 +00003790 if( ssl->transform )
3791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003792 mbedtls_ssl_transform_free( ssl->transform );
3793 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003794 }
3795
3796 if( ssl->handshake )
3797 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02003798 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003799 mbedtls_ssl_transform_free( ssl->transform_negotiate );
3800 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00003801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003802 mbedtls_free( ssl->handshake );
3803 mbedtls_free( ssl->transform_negotiate );
3804 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00003805 }
3806
Ronald Cron6f135e12021-12-08 16:57:54 +01003807#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01003808 mbedtls_ssl_transform_free( ssl->transform_application );
3809 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01003810#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01003811
Paul Bakkerc0463502013-02-14 11:19:38 +01003812 if( ssl->session )
3813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003814 mbedtls_ssl_session_free( ssl->session );
3815 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003816 }
3817
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02003818#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02003819 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003820 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003821 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003822 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00003823 }
Paul Bakker0be444a2013-08-27 21:55:01 +02003824#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003825
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003826#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003827 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02003828#endif
3829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00003831
Paul Bakker86f04f42013-02-14 11:20:09 +01003832 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003833 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003834}
3835
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02003836/*
3837 * Initialze mbedtls_ssl_config
3838 */
3839void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
3840{
3841 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
3842}
3843
Gilles Peskineeccd8882020-03-10 12:19:08 +01003844#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003845#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskineae270bf2021-06-02 00:05:29 +02003846/* The selection should be the same as mbedtls_x509_crt_profile_default in
Gilles Peskinea28f0f52021-06-02 15:29:38 +02003847 * x509_crt.c. Here, the order matters. Currently we favor stronger hashes,
3848 * for no fundamental reason.
Gilles Peskineae270bf2021-06-02 00:05:29 +02003849 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
3850 * about this list. */
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003851static int ssl_preset_default_hashes[] = {
3852#if defined(MBEDTLS_SHA512_C)
3853 MBEDTLS_MD_SHA512,
Mateusz Starzyk3352a532021-04-06 14:28:22 +02003854#endif
3855#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003856 MBEDTLS_MD_SHA384,
3857#endif
3858#if defined(MBEDTLS_SHA256_C)
3859 MBEDTLS_MD_SHA256,
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02003860#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003861 MBEDTLS_MD_NONE
3862};
Jerry Yuf017ee42022-01-12 15:49:48 +08003863#endif /* !MBEDTLS_DEPRECATED_REMOVED */
3864#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003865
Gilles Peskineae270bf2021-06-02 00:05:29 +02003866/* The selection should be the same as mbedtls_x509_crt_profile_default in
3867 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02003868 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02003869 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02003870 * about this list.
3871 */
Brett Warrene0edc842021-08-17 09:53:13 +01003872static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02003873#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003874 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003875#endif
3876#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003877 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003878#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02003879#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003880 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003881#endif
3882#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003883 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003884#endif
3885#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003886 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003887#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02003888#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003889 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003890#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02003891#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003892 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003893#endif
3894#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003895 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003896#endif
Brett Warrene0edc842021-08-17 09:53:13 +01003897 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02003898};
Gilles Peskineae270bf2021-06-02 00:05:29 +02003899
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003900static int ssl_preset_suiteb_ciphersuites[] = {
3901 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
3902 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
3903 0
3904};
3905
Gilles Peskineeccd8882020-03-10 12:19:08 +01003906#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003907#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003908static int ssl_preset_suiteb_hashes[] = {
Jerry Yuf017ee42022-01-12 15:49:48 +08003909#if defined(MBEDTLS_SHA256_C)
3910 MBEDTLS_MD_SHA256,
3911#endif
Jerry Yu713013f2022-01-17 18:16:35 +08003912#if defined(MBEDTLS_SHA384_C)
3913 MBEDTLS_MD_SHA384,
3914#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003915 MBEDTLS_MD_NONE
3916};
Jerry Yuf017ee42022-01-12 15:49:48 +08003917#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01003918
Jerry Yu909df7b2022-01-22 11:56:27 +08003919/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08003920 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08003921 * rules SHOULD be upheld.
3922 * - No duplicate entries.
3923 * - But if there is a good reason, do not change the order of the algorithms.
3924 * - ssl_tls12_present* is for TLS 1.2 use only.
3925 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08003926 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01003927static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08003928
Jerry Yued5e9f42022-01-26 11:21:34 +08003929#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \
3930 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
3931 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
3932#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C &&
3933 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08003934
Jerry Yu909df7b2022-01-22 11:56:27 +08003935#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \
3936 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
3937 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
3938#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
3939 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
3940
Jerry Yued5e9f42022-01-26 11:21:34 +08003941#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA512_C) && \
3942 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
3943 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
3944#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
3945 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08003946
Jerry Yu909df7b2022-01-22 11:56:27 +08003947#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C)
3948 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
3949#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */
3950
Ronald Cron8540cf62022-03-16 08:01:09 +01003951#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA512_C)
3952 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
3953#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
3954
3955#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA384_C)
3956 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
3957#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
3958
Jerry Yu53037892022-01-25 11:02:06 +08003959#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C)
3960 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
3961#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
3962
Jerry Yu909df7b2022-01-22 11:56:27 +08003963 MBEDTLS_TLS1_3_SIG_NONE
3964};
3965
3966/* NOTICE: see above */
3967#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3968static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Jerry Yu713013f2022-01-17 18:16:35 +08003969#if defined(MBEDTLS_SHA512_C)
3970 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA512 )
3971#endif
Jerry Yu11f0a9c2022-01-12 18:43:08 +08003972#if defined(MBEDTLS_SHA384_C)
3973 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 )
3974#endif
3975#if defined(MBEDTLS_SHA256_C)
3976 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 )
3977#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08003978 MBEDTLS_TLS1_3_SIG_NONE
3979};
3980#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3981/* NOTICE: see above */
3982static uint16_t ssl_preset_suiteb_sig_algs[] = {
3983
Jerry Yu909df7b2022-01-22 11:56:27 +08003984#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \
3985 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
3986 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
3987#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C &&
3988 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
3989
Jerry Yu53037892022-01-25 11:02:06 +08003990#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \
3991 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
3992 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
3993#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
3994 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08003995
3996#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C)
3997 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
3998#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */
Jerry Yu1a8b4812022-01-20 17:56:50 +08003999
Jerry Yu53037892022-01-25 11:02:06 +08004000#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C)
4001 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4002#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4003
Jerry Yu713013f2022-01-17 18:16:35 +08004004 MBEDTLS_TLS1_3_SIG_NONE
4005};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004006
Jerry Yu909df7b2022-01-22 11:56:27 +08004007/* NOTICE: see above */
4008#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4009static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Jerry Yu713013f2022-01-17 18:16:35 +08004010#if defined(MBEDTLS_SHA256_C)
4011 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 )
4012#endif
Jerry Yu18c833e2022-01-25 10:55:47 +08004013#if defined(MBEDTLS_SHA384_C)
4014 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 )
4015#endif
Xiaofei Bai95395012021-11-25 08:51:30 +00004016 MBEDTLS_TLS1_3_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004017};
Jerry Yu909df7b2022-01-22 11:56:27 +08004018#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4019
Jerry Yu1a8b4812022-01-20 17:56:50 +08004020#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004021
Brett Warrene0edc842021-08-17 09:53:13 +01004022static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004023#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004024 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004025#endif
4026#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004027 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004028#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004029 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004030};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004031
Jerry Yu1a8b4812022-01-20 17:56:50 +08004032#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004033/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004034 * to make sure there are no duplicated signature algorithm entries. */
Jerry Yuf377d642022-01-25 10:43:59 +08004035static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004036{
4037 size_t i, j;
4038 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004039
Jerry Yuf377d642022-01-25 10:43:59 +08004040 for( i = 0; sig_algs[i] != MBEDTLS_TLS1_3_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004041 {
Jerry Yuf377d642022-01-25 10:43:59 +08004042 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004043 {
Jerry Yuf377d642022-01-25 10:43:59 +08004044 if( sig_algs[i] != sig_algs[j] )
4045 continue;
4046 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4047 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4048 sig_algs[i], j, i );
4049 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004050 }
4051 }
4052 return( ret );
4053}
4054
4055#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4056
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004057/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004058 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004059 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004060int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004061 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004062{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004063#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004064 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004065#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004066
Jerry Yu1a8b4812022-01-20 17:56:50 +08004067#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004068 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004069 {
4070 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4071 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4072 }
4073
Jerry Yuf377d642022-01-25 10:43:59 +08004074 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004075 {
4076 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4077 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4078 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004079
4080#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004081 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004082 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004083 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004084 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4085 }
4086
Jerry Yuf377d642022-01-25 10:43:59 +08004087 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004088 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004089 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004090 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4091 }
4092#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004093#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4094
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004095 /* Use the functions here so that they are covered in tests,
4096 * but otherwise access member directly for efficiency */
4097 mbedtls_ssl_conf_endpoint( conf, endpoint );
4098 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004099
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004100 /*
4101 * Things that are common to all presets
4102 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004103#if defined(MBEDTLS_SSL_CLI_C)
4104 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4105 {
4106 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4107#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4108 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4109#endif
4110 }
4111#endif
4112
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004113#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4114 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4115#endif
4116
4117#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4118 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4119#endif
4120
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004121#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004122 conf->f_cookie_write = ssl_cookie_write_dummy;
4123 conf->f_cookie_check = ssl_cookie_check_dummy;
4124#endif
4125
4126#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4127 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4128#endif
4129
Janos Follath088ce432017-04-10 12:42:31 +01004130#if defined(MBEDTLS_SSL_SRV_C)
4131 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004132 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004133#endif
4134
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004135#if defined(MBEDTLS_SSL_PROTO_DTLS)
4136 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4137 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4138#endif
4139
4140#if defined(MBEDTLS_SSL_RENEGOTIATION)
4141 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004142 memset( conf->renego_period, 0x00, 2 );
4143 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004144#endif
4145
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004146#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004147 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4148 {
4149 const unsigned char dhm_p[] =
4150 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4151 const unsigned char dhm_g[] =
4152 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004153
Hanno Beckere2defad2021-07-24 05:59:17 +01004154 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4155 dhm_p, sizeof( dhm_p ),
4156 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4157 {
4158 return( ret );
4159 }
4160 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004161#endif
4162
Ronald Cron6f135e12021-12-08 16:57:54 +01004163#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker71f1ed62021-07-24 06:01:47 +01004164 /*
4165 * Allow all TLS 1.3 key exchange modes by default.
4166 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004167 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004168#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004169
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004170 /*
4171 * Preset-specific defaults
4172 */
4173 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004174 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004175 /*
4176 * NSA Suite B
4177 */
4178 case MBEDTLS_SSL_PRESET_SUITEB:
4179 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
4180 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
4181 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004182#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4183 /* Hybrid TLS 1.2/1.3 is not supported yet */
4184 conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
4185#else
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004186 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004187#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3 */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004188
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004189 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004190
4191#if defined(MBEDTLS_X509_CRT_PARSE_C)
4192 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004193#endif
4194
Gilles Peskineeccd8882020-03-10 12:19:08 +01004195#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004196#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004197 conf->sig_hashes = ssl_preset_suiteb_hashes;
Jerry Yuf017ee42022-01-12 15:49:48 +08004198#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004199#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4200 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4201 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4202 else
4203#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4204 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004205#endif
4206
Brett Warrene0edc842021-08-17 09:53:13 +01004207#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4208 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004209#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004210 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004211 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004212
4213 /*
4214 * Default
4215 */
4216 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03004217 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
4218 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
4219 MBEDTLS_SSL_MIN_MAJOR_VERSION :
4220 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
4221 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
4222 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
4223 MBEDTLS_SSL_MIN_MINOR_VERSION :
4224 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004225 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004226#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4227 /* Hybrid TLS 1.2/1.3 is not supported yet */
4228 conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
4229#else
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004230 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004231#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3 */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004232
4233#if defined(MBEDTLS_SSL_PROTO_DTLS)
4234 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
TRodziewiczef73f012021-05-13 14:53:36 +02004235 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004236#endif
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004237 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004238
4239#if defined(MBEDTLS_X509_CRT_PARSE_C)
4240 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4241#endif
4242
Gilles Peskineeccd8882020-03-10 12:19:08 +01004243#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004244#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01004245 conf->sig_hashes = ssl_preset_default_hashes;
Jerry Yuf017ee42022-01-12 15:49:48 +08004246#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004247#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4248 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4249 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4250 else
4251#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4252 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004253#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004254
Brett Warrene0edc842021-08-17 09:53:13 +01004255#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4256 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004257#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004258 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004259
4260#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4261 conf->dhm_min_bitlen = 1024;
4262#endif
4263 }
4264
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004265 return( 0 );
4266}
4267
4268/*
4269 * Free mbedtls_ssl_config
4270 */
4271void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4272{
4273#if defined(MBEDTLS_DHM_C)
4274 mbedtls_mpi_free( &conf->dhm_P );
4275 mbedtls_mpi_free( &conf->dhm_G );
4276#endif
4277
Gilles Peskineeccd8882020-03-10 12:19:08 +01004278#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004279 if( conf->psk != NULL )
4280 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004281 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004282 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004283 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004284 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004285 }
4286
4287 if( conf->psk_identity != NULL )
4288 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004289 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004290 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004291 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004292 conf->psk_identity_len = 0;
4293 }
4294#endif
4295
4296#if defined(MBEDTLS_X509_CRT_PARSE_C)
4297 ssl_key_cert_free( conf->key_cert );
4298#endif
4299
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004300 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004301}
4302
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004303#if defined(MBEDTLS_PK_C) && \
4304 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004305/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004306 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004308unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004309{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004310#if defined(MBEDTLS_RSA_C)
4311 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4312 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004313#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004314#if defined(MBEDTLS_ECDSA_C)
4315 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4316 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004317#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004318 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004319}
4320
Hanno Becker7e5437a2017-04-28 17:15:26 +01004321unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4322{
4323 switch( type ) {
4324 case MBEDTLS_PK_RSA:
4325 return( MBEDTLS_SSL_SIG_RSA );
4326 case MBEDTLS_PK_ECDSA:
4327 case MBEDTLS_PK_ECKEY:
4328 return( MBEDTLS_SSL_SIG_ECDSA );
4329 default:
4330 return( MBEDTLS_SSL_SIG_ANON );
4331 }
4332}
4333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004334mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004335{
4336 switch( sig )
4337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004338#if defined(MBEDTLS_RSA_C)
4339 case MBEDTLS_SSL_SIG_RSA:
4340 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004341#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004342#if defined(MBEDTLS_ECDSA_C)
4343 case MBEDTLS_SSL_SIG_ECDSA:
4344 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004345#endif
4346 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004347 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004348 }
4349}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004350#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004351
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004352/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004353 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004355mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004356{
4357 switch( hash )
4358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004359#if defined(MBEDTLS_MD5_C)
4360 case MBEDTLS_SSL_HASH_MD5:
4361 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004362#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004363#if defined(MBEDTLS_SHA1_C)
4364 case MBEDTLS_SSL_HASH_SHA1:
4365 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004366#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004367#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004368 case MBEDTLS_SSL_HASH_SHA224:
4369 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004370#endif
4371#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004372 case MBEDTLS_SSL_HASH_SHA256:
4373 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004374#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004375#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004376 case MBEDTLS_SSL_HASH_SHA384:
4377 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004378#endif
4379#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004380 case MBEDTLS_SSL_HASH_SHA512:
4381 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004382#endif
4383 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004384 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004385 }
4386}
4387
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004388/*
4389 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4390 */
4391unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4392{
4393 switch( md )
4394 {
4395#if defined(MBEDTLS_MD5_C)
4396 case MBEDTLS_MD_MD5:
4397 return( MBEDTLS_SSL_HASH_MD5 );
4398#endif
4399#if defined(MBEDTLS_SHA1_C)
4400 case MBEDTLS_MD_SHA1:
4401 return( MBEDTLS_SSL_HASH_SHA1 );
4402#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004403#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004404 case MBEDTLS_MD_SHA224:
4405 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004406#endif
4407#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004408 case MBEDTLS_MD_SHA256:
4409 return( MBEDTLS_SSL_HASH_SHA256 );
4410#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004411#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004412 case MBEDTLS_MD_SHA384:
4413 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004414#endif
4415#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004416 case MBEDTLS_MD_SHA512:
4417 return( MBEDTLS_SSL_HASH_SHA512 );
4418#endif
4419 default:
4420 return( MBEDTLS_SSL_HASH_NONE );
4421 }
4422}
4423
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004424/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004425 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004426 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004427 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004428int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004429{
Brett Warrene0edc842021-08-17 09:53:13 +01004430 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004431
Brett Warrene0edc842021-08-17 09:53:13 +01004432 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004433 return( -1 );
4434
Brett Warrene0edc842021-08-17 09:53:13 +01004435 for( ; *group_list != 0; group_list++ )
4436 {
4437 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004438 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01004439 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004440
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004441 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004442}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004443
4444#if defined(MBEDTLS_ECP_C)
4445/*
4446 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
4447 */
4448int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
4449{
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01004450 uint16_t tls_id = mbedtls_ecp_curve_info_from_grp_id( grp_id )->tls_id;
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004451 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
4452}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004453#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004455#if defined(MBEDTLS_X509_CRT_PARSE_C)
4456int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
4457 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004458 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02004459 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004460{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004461 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004462 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004463 const char *ext_oid;
4464 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004466 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004467 {
4468 /* Server part of the key exchange */
4469 switch( ciphersuite->key_exchange )
4470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004471 case MBEDTLS_KEY_EXCHANGE_RSA:
4472 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004473 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004474 break;
4475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004476 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
4477 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
4478 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
4479 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004480 break;
4481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004482 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
4483 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004484 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004485 break;
4486
4487 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004488 case MBEDTLS_KEY_EXCHANGE_NONE:
4489 case MBEDTLS_KEY_EXCHANGE_PSK:
4490 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
4491 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02004492 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004493 usage = 0;
4494 }
4495 }
4496 else
4497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004498 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
4499 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004500 }
4501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004502 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004503 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004504 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004505 ret = -1;
4506 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004508 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004510 ext_oid = MBEDTLS_OID_SERVER_AUTH;
4511 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004512 }
4513 else
4514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004515 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
4516 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004517 }
4518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004519 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004520 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004521 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004522 ret = -1;
4523 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004524
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004525 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004526}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004527#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004528
Jerry Yu148165c2021-09-24 23:20:59 +08004529#if defined(MBEDTLS_USE_PSA_CRYPTO)
4530int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
4531 const mbedtls_md_type_t md,
4532 unsigned char *dst,
4533 size_t dst_len,
4534 size_t *olen )
4535{
Ronald Cronf6893e12022-01-07 22:09:01 +01004536 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4537 psa_hash_operation_t *hash_operation_to_clone;
4538 psa_hash_operation_t hash_operation = psa_hash_operation_init();
4539
Jerry Yu148165c2021-09-24 23:20:59 +08004540 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01004541
4542 switch( md )
4543 {
4544#if defined(MBEDTLS_SHA384_C)
4545 case MBEDTLS_MD_SHA384:
4546 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
4547 break;
4548#endif
4549
4550#if defined(MBEDTLS_SHA256_C)
4551 case MBEDTLS_MD_SHA256:
4552 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
4553 break;
4554#endif
4555
4556 default:
4557 goto exit;
4558 }
4559
4560 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
4561 if( status != PSA_SUCCESS )
4562 goto exit;
4563
4564 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
4565 if( status != PSA_SUCCESS )
4566 goto exit;
4567
4568exit:
Ronald Cronb788c042022-02-15 09:14:15 +01004569 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08004570}
4571#else /* MBEDTLS_USE_PSA_CRYPTO */
4572
Jerry Yu24c0ec32021-09-09 14:21:07 +08004573#if defined(MBEDTLS_SHA384_C)
Jerry Yu000f9762021-09-14 11:12:51 +08004574static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
4575 unsigned char *dst,
4576 size_t dst_len,
4577 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004578{
Jerry Yu24c0ec32021-09-09 14:21:07 +08004579 int ret;
4580 mbedtls_sha512_context sha512;
4581
4582 if( dst_len < 48 )
4583 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4584
4585 mbedtls_sha512_init( &sha512 );
4586 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
4587
4588 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
4589 {
4590 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
4591 goto exit;
4592 }
4593
4594 *olen = 48;
4595
4596exit:
4597
4598 mbedtls_sha512_free( &sha512 );
4599 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004600}
4601#endif /* MBEDTLS_SHA384_C */
4602
4603#if defined(MBEDTLS_SHA256_C)
Jerry Yu000f9762021-09-14 11:12:51 +08004604static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
4605 unsigned char *dst,
4606 size_t dst_len,
4607 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004608{
Jerry Yu24c0ec32021-09-09 14:21:07 +08004609 int ret;
4610 mbedtls_sha256_context sha256;
4611
4612 if( dst_len < 32 )
4613 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4614
4615 mbedtls_sha256_init( &sha256 );
4616 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08004617
Jerry Yu24c0ec32021-09-09 14:21:07 +08004618 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
4619 {
4620 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
4621 goto exit;
4622 }
4623
4624 *olen = 32;
4625
4626exit:
4627
4628 mbedtls_sha256_free( &sha256 );
4629 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004630}
4631#endif /* MBEDTLS_SHA256_C */
4632
Jerry Yu000f9762021-09-14 11:12:51 +08004633int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
4634 const mbedtls_md_type_t md,
4635 unsigned char *dst,
4636 size_t dst_len,
4637 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004638{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004639 switch( md )
4640 {
4641
Jerry Yu24c0ec32021-09-09 14:21:07 +08004642#if defined(MBEDTLS_SHA384_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004643 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08004644 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08004645#endif /* MBEDTLS_SHA384_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004646
Jerry Yu24c0ec32021-09-09 14:21:07 +08004647#if defined(MBEDTLS_SHA256_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004648 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08004649 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08004650#endif /* MBEDTLS_SHA256_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004651
4652 default:
4653 break;
4654 }
Jerry Yuc5aef882021-12-23 20:15:02 +08004655 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004656}
XiaokangQian647719a2021-12-07 09:16:29 +00004657
Jerry Yu148165c2021-09-24 23:20:59 +08004658#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08004659
Jerry Yu1ea9d102021-12-21 13:41:49 +08004660#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) || \
4661 defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4662 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jerry Yuba073422021-12-20 22:22:15 +08004663/*
Jerry Yub925f212022-01-12 11:17:02 +08004664 * Function for writing a supported groups (TLS 1.3) or supported elliptic
4665 * curves (TLS 1.2) extension.
Jerry Yuba073422021-12-20 22:22:15 +08004666 *
Jerry Yub925f212022-01-12 11:17:02 +08004667 * The "extension_data" field of a supported groups extension contains a
4668 * "NamedGroupList" value (TLS 1.3 RFC8446):
Jerry Yuba073422021-12-20 22:22:15 +08004669 * enum {
4670 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
4671 * x25519(0x001D), x448(0x001E),
4672 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
4673 * ffdhe6144(0x0103), ffdhe8192(0x0104),
4674 * ffdhe_private_use(0x01FC..0x01FF),
4675 * ecdhe_private_use(0xFE00..0xFEFF),
4676 * (0xFFFF)
4677 * } NamedGroup;
4678 * struct {
4679 * NamedGroup named_group_list<2..2^16-1>;
4680 * } NamedGroupList;
Jerry Yub925f212022-01-12 11:17:02 +08004681 *
4682 * The "extension_data" field of a supported elliptic curves extension contains
4683 * a "NamedCurveList" value (TLS 1.2 RFC 8422):
Jerry Yu63282b42022-01-11 15:43:53 +08004684 * enum {
4685 * deprecated(1..22),
4686 * secp256r1 (23), secp384r1 (24), secp521r1 (25),
4687 * x25519(29), x448(30),
4688 * reserved (0xFE00..0xFEFF),
4689 * deprecated(0xFF01..0xFF02),
4690 * (0xFFFF)
4691 * } NamedCurve;
4692 * struct {
4693 * NamedCurve named_curve_list<2..2^16-1>
4694 * } NamedCurveList;
4695 *
Jerry Yub925f212022-01-12 11:17:02 +08004696 * The TLS 1.3 supported groups extension was defined to be a compatible
4697 * generalization of the TLS 1.2 supported elliptic curves extension. They both
4698 * share the same extension identifier.
Jerry Yu63282b42022-01-11 15:43:53 +08004699 *
Jerry Yub925f212022-01-12 11:17:02 +08004700 * DHE groups are not supported yet.
Jerry Yuba073422021-12-20 22:22:15 +08004701 */
Jerry Yuba073422021-12-20 22:22:15 +08004702int mbedtls_ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl,
4703 unsigned char *buf,
Jerry Yu17532612021-12-20 22:32:09 +08004704 const unsigned char *end,
Jerry Yuba073422021-12-20 22:22:15 +08004705 size_t *out_len )
4706{
4707 unsigned char *p = buf ;
4708 unsigned char *named_group_list; /* Start of named_group_list */
4709 size_t named_group_list_len; /* Length of named_group_list */
4710 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
4711
4712 *out_len = 0;
Jerry Yuf46b0162022-01-11 16:28:00 +08004713
Jerry Yuba073422021-12-20 22:22:15 +08004714 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) );
4715
4716 /* Check if we have space for header and length fields:
4717 * - extension_type (2 bytes)
4718 * - extension_data_length (2 bytes)
4719 * - named_group_list_length (2 bytes)
4720 */
4721 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
4722 p += 6;
4723
4724 named_group_list = p;
4725
4726 if( group_list == NULL )
4727 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
4728
Jerry Yu1510cea2022-01-12 10:56:49 +08004729 for( ; *group_list != 0; group_list++ )
Jerry Yuba073422021-12-20 22:22:15 +08004730 {
Jerry Yu1510cea2022-01-12 10:56:49 +08004731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got supported group(%04x)", *group_list ) );
Jerry Yu63282b42022-01-11 15:43:53 +08004732
Jerry Yu1ea9d102021-12-21 13:41:49 +08004733#if defined(MBEDTLS_ECP_C)
Jerry Yu1510cea2022-01-12 10:56:49 +08004734 if( ( mbedtls_ssl_conf_is_tls13_enabled( ssl->conf ) &&
Jerry Yu3ad14ac2022-01-11 17:13:16 +08004735 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) ) ||
Jerry Yu1510cea2022-01-12 10:56:49 +08004736 ( mbedtls_ssl_conf_is_tls12_enabled( ssl->conf ) &&
Jerry Yu3ad14ac2022-01-11 17:13:16 +08004737 mbedtls_ssl_tls12_named_group_is_ecdhe( *group_list ) ) )
Jerry Yuba073422021-12-20 22:22:15 +08004738 {
Jerry Yu3ad14ac2022-01-11 17:13:16 +08004739 const mbedtls_ecp_curve_info *curve_info;
4740 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
Jerry Yub925f212022-01-12 11:17:02 +08004741 if( curve_info == NULL )
Jerry Yu3ad14ac2022-01-11 17:13:16 +08004742 continue;
Jerry Yub925f212022-01-12 11:17:02 +08004743 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
4744 MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 );
4745 p += 2;
4746 MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )",
4747 curve_info->name, *group_list ) );
Jerry Yuba073422021-12-20 22:22:15 +08004748 }
Jerry Yu63282b42022-01-11 15:43:53 +08004749#endif /* MBEDTLS_ECP_C */
4750 /* Add DHE groups here */
Jerry Yuba073422021-12-20 22:22:15 +08004751
4752 }
4753
Jerry Yu1510cea2022-01-12 10:56:49 +08004754 /* Length of named_group_list */
Jerry Yuba073422021-12-20 22:22:15 +08004755 named_group_list_len = p - named_group_list;
4756 if( named_group_list_len == 0 )
4757 {
4758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) );
4759 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4760 }
4761
4762 /* Write extension_type */
4763 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 );
4764 /* Write extension_data_length */
4765 MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 );
4766 /* Write length of named_group_list */
4767 MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 );
4768
Jerry Yu7f029d82022-01-11 11:08:53 +08004769 MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension",
4770 buf + 4, named_group_list_len + 2 );
Jerry Yuba073422021-12-20 22:22:15 +08004771
4772 *out_len = p - buf;
4773
4774#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4775 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
4776#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4777
4778 return( 0 );
4779}
4780
Jerry Yu1ea9d102021-12-21 13:41:49 +08004781#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED ||
4782 MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
4783 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Jerry Yuba073422021-12-20 22:22:15 +08004784
Jerry Yuf017ee42022-01-12 15:49:48 +08004785#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
4786/*
Jerry Yu7ddc38c2022-01-19 11:08:05 +08004787 * Function for writing a signature algorithm extension.
Jerry Yuf017ee42022-01-12 15:49:48 +08004788 *
Jerry Yu8afd6e42022-01-20 15:54:26 +08004789 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
Jerry Yu7ddc38c2022-01-19 11:08:05 +08004790 * value (TLS 1.3 RFC8446):
4791 * enum {
4792 * ....
4793 * ecdsa_secp256r1_sha256( 0x0403 ),
4794 * ecdsa_secp384r1_sha384( 0x0503 ),
4795 * ecdsa_secp521r1_sha512( 0x0603 ),
4796 * ....
4797 * } SignatureScheme;
Jerry Yuf017ee42022-01-12 15:49:48 +08004798 *
Jerry Yu7ddc38c2022-01-19 11:08:05 +08004799 * struct {
4800 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
4801 * } SignatureSchemeList;
Jerry Yuf017ee42022-01-12 15:49:48 +08004802 *
Jerry Yu8afd6e42022-01-20 15:54:26 +08004803 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
4804 * value (TLS 1.2 RFC5246):
Jerry Yu7ddc38c2022-01-19 11:08:05 +08004805 * enum {
4806 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
4807 * sha512(6), (255)
4808 * } HashAlgorithm;
4809 *
4810 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
4811 * SignatureAlgorithm;
4812 *
4813 * struct {
4814 * HashAlgorithm hash;
4815 * SignatureAlgorithm signature;
4816 * } SignatureAndHashAlgorithm;
4817 *
4818 * SignatureAndHashAlgorithm
4819 * supported_signature_algorithms<2..2^16-2>;
4820 *
4821 * The TLS 1.3 signature algorithm extension was defined to be a compatible
4822 * generalization of the TLS 1.2 signature algorithm extension.
4823 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
4824 * `SignatureScheme` field of TLS 1.3
4825 *
Jerry Yuf017ee42022-01-12 15:49:48 +08004826 */
4827int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
4828 const unsigned char *end, size_t *out_len )
4829{
4830 unsigned char *p = buf;
4831 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
4832 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
4833
4834 *out_len = 0;
4835
4836 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
4837
4838 /* Check if we have space for header and length field:
4839 * - extension_type (2 bytes)
4840 * - extension_data_length (2 bytes)
4841 * - supported_signature_algorithms_length (2 bytes)
4842 */
4843 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
4844 p += 6;
4845
4846 /*
4847 * Write supported_signature_algorithms
4848 */
4849 supported_sig_alg = p;
Jerry Yu6106fdc2022-01-12 16:36:14 +08004850 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
4851 if( sig_alg == NULL )
4852 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
4853
4854 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
Jerry Yuf017ee42022-01-12 15:49:48 +08004855 {
Jerry Yu1bab3012022-01-19 17:43:22 +08004856 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
4857 continue;
Jerry Yuf017ee42022-01-12 15:49:48 +08004858 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
4859 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
4860 p += 2;
4861 MBEDTLS_SSL_DEBUG_MSG( 3, ( "signature scheme [%x]", *sig_alg ) );
4862 }
4863
4864 /* Length of supported_signature_algorithms */
4865 supported_sig_alg_len = p - supported_sig_alg;
4866 if( supported_sig_alg_len == 0 )
4867 {
4868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
4869 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4870 }
4871
4872 /* Write extension_type */
4873 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
4874 /* Write extension_data_length */
4875 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
4876 /* Write length of supported_signature_algorithms */
4877 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
4878
4879 /* Output the total length of signature algorithms extension. */
4880 *out_len = p - buf;
4881
4882#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4883 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
4884#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4885 return( 0 );
4886}
4887#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yuc73c6182022-02-08 20:29:25 +08004888
Jerry Yuc5aef882021-12-23 20:15:02 +08004889#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4890int mbedtls_ssl_write_hostname_ext( mbedtls_ssl_context *ssl,
4891 unsigned char *buf,
4892 const unsigned char *end,
4893 size_t *olen )
4894{
4895 unsigned char *p = buf;
4896 size_t hostname_len;
4897
4898 *olen = 0;
4899
4900 if( ssl->hostname == NULL )
4901 return( 0 );
4902
4903 MBEDTLS_SSL_DEBUG_MSG( 3,
4904 ( "client hello, adding server name extension: %s",
4905 ssl->hostname ) );
4906
4907 hostname_len = strlen( ssl->hostname );
4908
4909 MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 );
4910
4911 /*
4912 * Sect. 3, RFC 6066 (TLS Extensions Definitions)
4913 *
4914 * In order to provide any of the server names, clients MAY include an
4915 * extension of type "server_name" in the (extended) client hello. The
4916 * "extension_data" field of this extension SHALL contain
4917 * "ServerNameList" where:
4918 *
4919 * struct {
4920 * NameType name_type;
4921 * select (name_type) {
4922 * case host_name: HostName;
4923 * } name;
4924 * } ServerName;
4925 *
4926 * enum {
4927 * host_name(0), (255)
4928 * } NameType;
4929 *
4930 * opaque HostName<1..2^16-1>;
4931 *
4932 * struct {
4933 * ServerName server_name_list<1..2^16-1>
4934 * } ServerNameList;
4935 *
4936 */
4937 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SERVERNAME, p, 0 );
4938 p += 2;
4939
4940 MBEDTLS_PUT_UINT16_BE( hostname_len + 5, p, 0 );
4941 p += 2;
4942
4943 MBEDTLS_PUT_UINT16_BE( hostname_len + 3, p, 0 );
4944 p += 2;
4945
4946 *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME );
4947
4948 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
4949 p += 2;
4950
4951 memcpy( p, ssl->hostname, hostname_len );
4952
4953 *olen = hostname_len + 9;
4954
4955 return( 0 );
4956}
4957#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Jerry Yuf017ee42022-01-12 15:49:48 +08004958
Jerry Yudc7bd172022-02-17 13:44:15 +08004959#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4960
4961#if defined(MBEDTLS_USE_PSA_CRYPTO)
4962
4963static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
4964 mbedtls_svc_key_id_t key,
4965 psa_algorithm_t alg,
4966 const unsigned char* seed, size_t seed_length,
4967 const unsigned char* label, size_t label_length,
4968 size_t capacity )
4969{
4970 psa_status_t status;
4971
4972 status = psa_key_derivation_setup( derivation, alg );
4973 if( status != PSA_SUCCESS )
4974 return( status );
4975
4976 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4977 {
4978 status = psa_key_derivation_input_bytes( derivation,
4979 PSA_KEY_DERIVATION_INPUT_SEED,
4980 seed, seed_length );
4981 if( status != PSA_SUCCESS )
4982 return( status );
4983
4984 if( mbedtls_svc_key_id_is_null( key ) )
4985 {
4986 status = psa_key_derivation_input_bytes(
4987 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
4988 NULL, 0 );
4989 }
4990 else
4991 {
4992 status = psa_key_derivation_input_key(
4993 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
4994 }
4995 if( status != PSA_SUCCESS )
4996 return( status );
4997
4998 status = psa_key_derivation_input_bytes( derivation,
4999 PSA_KEY_DERIVATION_INPUT_LABEL,
5000 label, label_length );
5001 if( status != PSA_SUCCESS )
5002 return( status );
5003 }
5004 else
5005 {
5006 return( PSA_ERROR_NOT_SUPPORTED );
5007 }
5008
5009 status = psa_key_derivation_set_capacity( derivation, capacity );
5010 if( status != PSA_SUCCESS )
5011 return( status );
5012
5013 return( PSA_SUCCESS );
5014}
5015
5016static int tls_prf_generic( mbedtls_md_type_t md_type,
5017 const unsigned char *secret, size_t slen,
5018 const char *label,
5019 const unsigned char *random, size_t rlen,
5020 unsigned char *dstbuf, size_t dlen )
5021{
5022 psa_status_t status;
5023 psa_algorithm_t alg;
5024 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5025 psa_key_derivation_operation_t derivation =
5026 PSA_KEY_DERIVATION_OPERATION_INIT;
5027
5028 if( md_type == MBEDTLS_MD_SHA384 )
5029 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5030 else
5031 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5032
5033 /* Normally a "secret" should be long enough to be impossible to
5034 * find by brute force, and in particular should not be empty. But
5035 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5036 * and for this use case it makes sense to have a 0-length "secret".
5037 * Since the key API doesn't allow importing a key of length 0,
5038 * keep master_key=0, which setup_psa_key_derivation() understands
5039 * to mean a 0-length "secret" input. */
5040 if( slen != 0 )
5041 {
5042 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5043 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5044 psa_set_key_algorithm( &key_attributes, alg );
5045 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5046
5047 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5048 if( status != PSA_SUCCESS )
5049 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5050 }
5051
5052 status = setup_psa_key_derivation( &derivation,
5053 master_key, alg,
5054 random, rlen,
5055 (unsigned char const *) label,
5056 (size_t) strlen( label ),
5057 dlen );
5058 if( status != PSA_SUCCESS )
5059 {
5060 psa_key_derivation_abort( &derivation );
5061 psa_destroy_key( master_key );
5062 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5063 }
5064
5065 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5066 if( status != PSA_SUCCESS )
5067 {
5068 psa_key_derivation_abort( &derivation );
5069 psa_destroy_key( master_key );
5070 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5071 }
5072
5073 status = psa_key_derivation_abort( &derivation );
5074 if( status != PSA_SUCCESS )
5075 {
5076 psa_destroy_key( master_key );
5077 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5078 }
5079
5080 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5081 status = psa_destroy_key( master_key );
5082 if( status != PSA_SUCCESS )
5083 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5084
5085 return( 0 );
5086}
5087
5088#else /* MBEDTLS_USE_PSA_CRYPTO */
5089
5090static int tls_prf_generic( mbedtls_md_type_t md_type,
5091 const unsigned char *secret, size_t slen,
5092 const char *label,
5093 const unsigned char *random, size_t rlen,
5094 unsigned char *dstbuf, size_t dlen )
5095{
5096 size_t nb;
5097 size_t i, j, k, md_len;
5098 unsigned char *tmp;
5099 size_t tmp_len = 0;
5100 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5101 const mbedtls_md_info_t *md_info;
5102 mbedtls_md_context_t md_ctx;
5103 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5104
5105 mbedtls_md_init( &md_ctx );
5106
5107 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5108 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5109
5110 md_len = mbedtls_md_get_size( md_info );
5111
5112 tmp_len = md_len + strlen( label ) + rlen;
5113 tmp = mbedtls_calloc( 1, tmp_len );
5114 if( tmp == NULL )
5115 {
5116 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5117 goto exit;
5118 }
5119
5120 nb = strlen( label );
5121 memcpy( tmp + md_len, label, nb );
5122 memcpy( tmp + md_len + nb, random, rlen );
5123 nb += rlen;
5124
5125 /*
5126 * Compute P_<hash>(secret, label + random)[0..dlen]
5127 */
5128 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5129 goto exit;
5130
5131 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5132 if( ret != 0 )
5133 goto exit;
5134 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5135 if( ret != 0 )
5136 goto exit;
5137 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5138 if( ret != 0 )
5139 goto exit;
5140
5141 for( i = 0; i < dlen; i += md_len )
5142 {
5143 ret = mbedtls_md_hmac_reset ( &md_ctx );
5144 if( ret != 0 )
5145 goto exit;
5146 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5147 if( ret != 0 )
5148 goto exit;
5149 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5150 if( ret != 0 )
5151 goto exit;
5152
5153 ret = mbedtls_md_hmac_reset ( &md_ctx );
5154 if( ret != 0 )
5155 goto exit;
5156 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5157 if( ret != 0 )
5158 goto exit;
5159 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5160 if( ret != 0 )
5161 goto exit;
5162
5163 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5164
5165 for( j = 0; j < k; j++ )
5166 dstbuf[i + j] = h_i[j];
5167 }
5168
5169exit:
5170 mbedtls_md_free( &md_ctx );
5171
5172 mbedtls_platform_zeroize( tmp, tmp_len );
5173 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5174
5175 mbedtls_free( tmp );
5176
5177 return( ret );
5178}
5179#endif /* MBEDTLS_USE_PSA_CRYPTO */
5180
5181#if defined(MBEDTLS_SHA256_C)
5182static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5183 const char *label,
5184 const unsigned char *random, size_t rlen,
5185 unsigned char *dstbuf, size_t dlen )
5186{
5187 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5188 label, random, rlen, dstbuf, dlen ) );
5189}
5190#endif /* MBEDTLS_SHA256_C */
5191
5192#if defined(MBEDTLS_SHA384_C)
5193static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5194 const char *label,
5195 const unsigned char *random, size_t rlen,
5196 unsigned char *dstbuf, size_t dlen )
5197{
5198 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5199 label, random, rlen, dstbuf, dlen ) );
5200}
5201#endif /* MBEDTLS_SHA384_C */
5202
Jerry Yuf009d862022-02-17 14:01:37 +08005203/*
5204 * Set appropriate PRF function and other SSL / TLS1.2 functions
5205 *
5206 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005207 * - hash associated with the ciphersuite (only used by TLS 1.2)
5208 *
5209 * Outputs:
5210 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5211 */
5212static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005213 mbedtls_md_type_t hash )
5214{
Jerry Yuf009d862022-02-17 14:01:37 +08005215#if defined(MBEDTLS_SHA384_C)
Ronald Cron81591aa2022-03-07 09:05:51 +01005216 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005217 {
5218 handshake->tls_prf = tls_prf_sha384;
5219 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5220 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5221 }
5222 else
5223#endif
5224#if defined(MBEDTLS_SHA256_C)
Jerry Yuf009d862022-02-17 14:01:37 +08005225 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005226 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005227 handshake->tls_prf = tls_prf_sha256;
5228 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5229 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5230 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005231#else
Jerry Yuf009d862022-02-17 14:01:37 +08005232 {
Jerry Yuf009d862022-02-17 14:01:37 +08005233 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005234 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005235 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5236 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005237#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005238
5239 return( 0 );
5240}
Jerry Yud6ab2352022-02-17 14:03:43 +08005241
5242#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
5243 defined(MBEDTLS_USE_PSA_CRYPTO)
5244static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
5245{
5246 if( ssl->conf->f_psk != NULL )
5247 {
5248 /* If we've used a callback to select the PSK,
5249 * the static configuration is irrelevant. */
5250 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
5251 return( 1 );
5252
5253 return( 0 );
5254 }
5255
5256 if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) )
5257 return( 1 );
5258
5259 return( 0 );
5260}
5261#endif /* MBEDTLS_USE_PSA_CRYPTO &&
5262 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5263
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005264/*
5265 * Compute master secret if needed
5266 *
5267 * Parameters:
5268 * [in/out] handshake
5269 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5270 * (PSA-PSK) ciphersuite_info, psk_opaque
5271 * [out] premaster (cleared)
5272 * [out] master
5273 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5274 * debug: conf->f_dbg, conf->p_dbg
5275 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005276 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005277 */
5278static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5279 unsigned char *master,
5280 const mbedtls_ssl_context *ssl )
5281{
5282 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5283
5284 /* cf. RFC 5246, Section 8.1:
5285 * "The master secret is always exactly 48 bytes in length." */
5286 size_t const master_secret_len = 48;
5287
5288#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5289 unsigned char session_hash[48];
5290#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5291
5292 /* The label for the KDF used for key expansion.
5293 * This is either "master secret" or "extended master secret"
5294 * depending on whether the Extended Master Secret extension
5295 * is used. */
5296 char const *lbl = "master secret";
5297
5298 /* The salt for the KDF used for key expansion.
5299 * - If the Extended Master Secret extension is not used,
5300 * this is ClientHello.Random + ServerHello.Random
5301 * (see Sect. 8.1 in RFC 5246).
5302 * - If the Extended Master Secret extension is used,
5303 * this is the transcript of the handshake so far.
5304 * (see Sect. 4 in RFC 7627). */
5305 unsigned char const *salt = handshake->randbytes;
5306 size_t salt_len = 64;
5307
5308#if !defined(MBEDTLS_DEBUG_C) && \
5309 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5310 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5311 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5312 ssl = NULL; /* make sure we don't use it except for those cases */
5313 (void) ssl;
5314#endif
5315
5316 if( handshake->resume != 0 )
5317 {
5318 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5319 return( 0 );
5320 }
5321
5322#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5323 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5324 {
5325 lbl = "extended master secret";
5326 salt = session_hash;
5327 handshake->calc_verify( ssl, session_hash, &salt_len );
5328
5329 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
5330 session_hash, salt_len );
5331 }
5332#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
5333
5334#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
5335 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5336 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005337 ssl_use_opaque_psk( ssl ) == 1 )
5338 {
5339 /* Perform PSK-to-MS expansion in a single step. */
5340 psa_status_t status;
5341 psa_algorithm_t alg;
5342 mbedtls_svc_key_id_t psk;
5343 psa_key_derivation_operation_t derivation =
5344 PSA_KEY_DERIVATION_OPERATION_INIT;
5345 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5346
5347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5348
5349 psk = mbedtls_ssl_get_opaque_psk( ssl );
5350
5351 if( hash_alg == MBEDTLS_MD_SHA384 )
5352 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5353 else
5354 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5355
5356 status = setup_psa_key_derivation( &derivation, psk, alg,
5357 salt, salt_len,
5358 (unsigned char const *) lbl,
5359 (size_t) strlen( lbl ),
5360 master_secret_len );
5361 if( status != PSA_SUCCESS )
5362 {
5363 psa_key_derivation_abort( &derivation );
5364 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5365 }
5366
5367 status = psa_key_derivation_output_bytes( &derivation,
5368 master,
5369 master_secret_len );
5370 if( status != PSA_SUCCESS )
5371 {
5372 psa_key_derivation_abort( &derivation );
5373 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5374 }
5375
5376 status = psa_key_derivation_abort( &derivation );
5377 if( status != PSA_SUCCESS )
5378 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5379 }
5380 else
5381#endif
5382 {
5383 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
5384 lbl, salt, salt_len,
5385 master,
5386 master_secret_len );
5387 if( ret != 0 )
5388 {
5389 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5390 return( ret );
5391 }
5392
5393 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5394 handshake->premaster,
5395 handshake->pmslen );
5396
5397 mbedtls_platform_zeroize( handshake->premaster,
5398 sizeof(handshake->premaster) );
5399 }
5400
5401 return( 0 );
5402}
5403
Jerry Yud62f87e2022-02-17 14:09:02 +08005404int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5405{
5406 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5407 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5408 ssl->handshake->ciphersuite_info;
5409
5410 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5411
5412 /* Set PRF, calc_verify and calc_finished function pointers */
5413 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005414 ciphersuite_info->mac );
5415 if( ret != 0 )
5416 {
5417 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5418 return( ret );
5419 }
5420
5421 /* Compute master secret if needed */
5422 ret = ssl_compute_master( ssl->handshake,
5423 ssl->session_negotiate->master,
5424 ssl );
5425 if( ret != 0 )
5426 {
5427 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5428 return( ret );
5429 }
5430
5431 /* Swap the client and server random values:
5432 * - MS derivation wanted client+server (RFC 5246 8.1)
5433 * - key derivation wants server+client (RFC 5246 6.3) */
5434 {
5435 unsigned char tmp[64];
5436 memcpy( tmp, ssl->handshake->randbytes, 64 );
5437 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5438 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5439 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5440 }
5441
5442 /* Populate transform structure */
5443 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5444 ssl->session_negotiate->ciphersuite,
5445 ssl->session_negotiate->master,
5446#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
5447 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5448 ssl->session_negotiate->encrypt_then_mac,
5449#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
5450 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
5451 ssl->handshake->tls_prf,
5452 ssl->handshake->randbytes,
5453 ssl->minor_ver,
5454 ssl->conf->endpoint,
5455 ssl );
5456 if( ret != 0 )
5457 {
5458 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5459 return( ret );
5460 }
5461
5462 /* We no longer need Server/ClientHello.random values */
5463 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5464 sizeof( ssl->handshake->randbytes ) );
5465
5466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5467
5468 return( 0 );
5469}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005470
Ronald Cron4dcbca92022-03-07 10:21:40 +01005471int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5472{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005473 switch( md )
5474 {
5475#if defined(MBEDTLS_SHA384_C)
5476 case MBEDTLS_SSL_HASH_SHA384:
5477 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5478 break;
5479#endif
5480#if defined(MBEDTLS_SHA256_C)
5481 case MBEDTLS_SSL_HASH_SHA256:
5482 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5483 break;
5484#endif
5485 default:
5486 return( -1 );
5487 }
5488
Ronald Cronc2f13a02022-03-07 10:25:24 +01005489 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005490}
5491
Jerry Yu8392e0d2022-02-17 14:10:24 +08005492#if defined(MBEDTLS_SHA256_C)
5493void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5494 unsigned char *hash,
5495 size_t *hlen )
5496{
5497#if defined(MBEDTLS_USE_PSA_CRYPTO)
5498 size_t hash_size;
5499 psa_status_t status;
5500 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5501
5502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5503 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5504 if( status != PSA_SUCCESS )
5505 {
5506 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5507 return;
5508 }
5509
5510 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5511 if( status != PSA_SUCCESS )
5512 {
5513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5514 return;
5515 }
5516
5517 *hlen = 32;
5518 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5520#else
5521 mbedtls_sha256_context sha256;
5522
5523 mbedtls_sha256_init( &sha256 );
5524
5525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5526
5527 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5528 mbedtls_sha256_finish( &sha256, hash );
5529
5530 *hlen = 32;
5531
5532 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5534
5535 mbedtls_sha256_free( &sha256 );
5536#endif /* MBEDTLS_USE_PSA_CRYPTO */
5537 return;
5538}
5539#endif /* MBEDTLS_SHA256_C */
5540
Jerry Yuc1cb3842022-02-17 14:13:48 +08005541#if defined(MBEDTLS_SHA384_C)
5542void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5543 unsigned char *hash,
5544 size_t *hlen )
5545{
5546#if defined(MBEDTLS_USE_PSA_CRYPTO)
5547 size_t hash_size;
5548 psa_status_t status;
5549 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5550
5551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5552 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5553 if( status != PSA_SUCCESS )
5554 {
5555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5556 return;
5557 }
5558
5559 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5560 if( status != PSA_SUCCESS )
5561 {
5562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5563 return;
5564 }
5565
5566 *hlen = 48;
5567 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5569#else
5570 mbedtls_sha512_context sha512;
5571
5572 mbedtls_sha512_init( &sha512 );
5573
5574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
5575
5576 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
5577 mbedtls_sha512_finish( &sha512, hash );
5578
5579 *hlen = 48;
5580
5581 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5583
5584 mbedtls_sha512_free( &sha512 );
5585#endif /* MBEDTLS_USE_PSA_CRYPTO */
5586 return;
5587}
5588#endif /* MBEDTLS_SHA384_C */
5589
Jerry Yuce3dca42022-02-17 14:16:37 +08005590#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
5591int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
5592{
5593 unsigned char *p = ssl->handshake->premaster;
5594 unsigned char *end = p + sizeof( ssl->handshake->premaster );
5595 const unsigned char *psk = NULL;
5596 size_t psk_len = 0;
5597
5598 if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len )
5599 == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
5600 {
5601 /*
5602 * This should never happen because the existence of a PSK is always
5603 * checked before calling this function
5604 */
5605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5606 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5607 }
5608
5609 /*
5610 * PMS = struct {
5611 * opaque other_secret<0..2^16-1>;
5612 * opaque psk<0..2^16-1>;
5613 * };
5614 * with "other_secret" depending on the particular key exchange
5615 */
5616#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5617 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
5618 {
5619 if( end - p < 2 )
5620 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5621
5622 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5623 p += 2;
5624
5625 if( end < p || (size_t)( end - p ) < psk_len )
5626 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5627
5628 memset( p, 0, psk_len );
5629 p += psk_len;
5630 }
5631 else
5632#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5633#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
5634 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5635 {
5636 /*
5637 * other_secret already set by the ClientKeyExchange message,
5638 * and is 48 bytes long
5639 */
5640 if( end - p < 2 )
5641 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5642
5643 *p++ = 0;
5644 *p++ = 48;
5645 p += 48;
5646 }
5647 else
5648#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
5649#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
5650 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5651 {
5652 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5653 size_t len;
5654
5655 /* Write length only when we know the actual value */
5656 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
5657 p + 2, end - ( p + 2 ), &len,
5658 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5659 {
5660 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
5661 return( ret );
5662 }
5663 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
5664 p += 2 + len;
5665
5666 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
5667 }
5668 else
5669#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
5670#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
5671 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
5672 {
5673 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5674 size_t zlen;
5675
5676 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
5677 p + 2, end - ( p + 2 ),
5678 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5679 {
5680 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
5681 return( ret );
5682 }
5683
5684 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
5685 p += 2 + zlen;
5686
5687 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
5688 MBEDTLS_DEBUG_ECDH_Z );
5689 }
5690 else
5691#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
5692 {
5693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5694 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5695 }
5696
5697 /* opaque psk<0..2^16-1>; */
5698 if( end - p < 2 )
5699 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5700
5701 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5702 p += 2;
5703
5704 if( end < p || (size_t)( end - p ) < psk_len )
5705 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5706
5707 memcpy( p, psk, psk_len );
5708 p += psk_len;
5709
5710 ssl->handshake->pmslen = p - ssl->handshake->premaster;
5711
5712 return( 0 );
5713}
5714#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08005715
5716#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
5717static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
5718
5719#if defined(MBEDTLS_SSL_PROTO_DTLS)
5720int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
5721{
5722 /* If renegotiation is not enforced, retransmit until we would reach max
5723 * timeout if we were using the usual handshake doubling scheme */
5724 if( ssl->conf->renego_max_records < 0 )
5725 {
5726 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
5727 unsigned char doublings = 1;
5728
5729 while( ratio != 0 )
5730 {
5731 ++doublings;
5732 ratio >>= 1;
5733 }
5734
5735 if( ++ssl->renego_records_seen > doublings )
5736 {
5737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
5738 return( 0 );
5739 }
5740 }
5741
5742 return( ssl_write_hello_request( ssl ) );
5743}
5744#endif
5745#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08005746
Jerry Yud9526692022-02-17 14:23:47 +08005747/*
5748 * Handshake functions
5749 */
5750#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5751/* No certificate support -> dummy functions */
5752int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
5753{
5754 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5755 ssl->handshake->ciphersuite_info;
5756
5757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
5758
5759 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5760 {
5761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5762 ssl->state++;
5763 return( 0 );
5764 }
5765
5766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5767 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5768}
5769
5770int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5771{
5772 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5773 ssl->handshake->ciphersuite_info;
5774
5775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5776
5777 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5778 {
5779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5780 ssl->state++;
5781 return( 0 );
5782 }
5783
5784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5785 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5786}
5787
5788#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5789/* Some certificate support -> implement write and parse */
5790
5791int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
5792{
5793 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
5794 size_t i, n;
5795 const mbedtls_x509_crt *crt;
5796 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5797 ssl->handshake->ciphersuite_info;
5798
5799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
5800
5801 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5802 {
5803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5804 ssl->state++;
5805 return( 0 );
5806 }
5807
5808#if defined(MBEDTLS_SSL_CLI_C)
5809 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5810 {
5811 if( ssl->handshake->client_auth == 0 )
5812 {
5813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5814 ssl->state++;
5815 return( 0 );
5816 }
5817 }
5818#endif /* MBEDTLS_SSL_CLI_C */
5819#if defined(MBEDTLS_SSL_SRV_C)
5820 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
5821 {
5822 if( mbedtls_ssl_own_cert( ssl ) == NULL )
5823 {
5824 /* Should never happen because we shouldn't have picked the
5825 * ciphersuite if we don't have a certificate. */
5826 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5827 }
5828 }
5829#endif
5830
5831 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
5832
5833 /*
5834 * 0 . 0 handshake type
5835 * 1 . 3 handshake length
5836 * 4 . 6 length of all certs
5837 * 7 . 9 length of cert. 1
5838 * 10 . n-1 peer certificate
5839 * n . n+2 length of cert. 2
5840 * n+3 . ... upper level cert, etc.
5841 */
5842 i = 7;
5843 crt = mbedtls_ssl_own_cert( ssl );
5844
5845 while( crt != NULL )
5846 {
5847 n = crt->raw.len;
5848 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
5849 {
5850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
5851 " > %" MBEDTLS_PRINTF_SIZET,
5852 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
5853 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5854 }
5855
5856 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
5857 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
5858 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
5859
5860 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5861 i += n; crt = crt->next;
5862 }
5863
5864 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
5865 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
5866 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
5867
5868 ssl->out_msglen = i;
5869 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5870 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
5871
5872 ssl->state++;
5873
5874 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
5875 {
5876 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
5877 return( ret );
5878 }
5879
5880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
5881
5882 return( ret );
5883}
5884
5885#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5886
5887#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5888static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5889 unsigned char *crt_buf,
5890 size_t crt_buf_len )
5891{
5892 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5893
5894 if( peer_crt == NULL )
5895 return( -1 );
5896
5897 if( peer_crt->raw.len != crt_buf_len )
5898 return( -1 );
5899
5900 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
5901}
5902#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5903static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5904 unsigned char *crt_buf,
5905 size_t crt_buf_len )
5906{
5907 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5908 unsigned char const * const peer_cert_digest =
5909 ssl->session->peer_cert_digest;
5910 mbedtls_md_type_t const peer_cert_digest_type =
5911 ssl->session->peer_cert_digest_type;
5912 mbedtls_md_info_t const * const digest_info =
5913 mbedtls_md_info_from_type( peer_cert_digest_type );
5914 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
5915 size_t digest_len;
5916
5917 if( peer_cert_digest == NULL || digest_info == NULL )
5918 return( -1 );
5919
5920 digest_len = mbedtls_md_get_size( digest_info );
5921 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
5922 return( -1 );
5923
5924 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
5925 if( ret != 0 )
5926 return( -1 );
5927
5928 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
5929}
5930#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5931#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5932
5933/*
5934 * Once the certificate message is read, parse it into a cert chain and
5935 * perform basic checks, but leave actual verification to the caller
5936 */
5937static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5938 mbedtls_x509_crt *chain )
5939{
5940 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5941#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5942 int crt_cnt=0;
5943#endif
5944 size_t i, n;
5945 uint8_t alert;
5946
5947 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5948 {
5949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5950 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5951 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5952 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5953 }
5954
5955 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
5956 {
5957 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5958 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5959 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5960 }
5961
5962 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
5963 {
5964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5965 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5966 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5967 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5968 }
5969
5970 i = mbedtls_ssl_hs_hdr_len( ssl );
5971
5972 /*
5973 * Same message structure as in mbedtls_ssl_write_certificate()
5974 */
5975 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
5976
5977 if( ssl->in_msg[i] != 0 ||
5978 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
5979 {
5980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5981 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5982 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5983 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5984 }
5985
5986 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5987 i += 3;
5988
5989 /* Iterate through and parse the CRTs in the provided chain. */
5990 while( i < ssl->in_hslen )
5991 {
5992 /* Check that there's room for the next CRT's length fields. */
5993 if ( i + 3 > ssl->in_hslen ) {
5994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5995 mbedtls_ssl_send_alert_message( ssl,
5996 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5997 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5998 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5999 }
6000 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6001 * anything beyond 2**16 ~ 64K. */
6002 if( ssl->in_msg[i] != 0 )
6003 {
6004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6005 mbedtls_ssl_send_alert_message( ssl,
6006 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6007 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6008 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6009 }
6010
6011 /* Read length of the next CRT in the chain. */
6012 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6013 | (unsigned int) ssl->in_msg[i + 2];
6014 i += 3;
6015
6016 if( n < 128 || i + n > ssl->in_hslen )
6017 {
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
6025 /* Check if we're handling the first CRT in the chain. */
6026#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6027 if( crt_cnt++ == 0 &&
6028 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6029 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6030 {
6031 /* During client-side renegotiation, check that the server's
6032 * end-CRTs hasn't changed compared to the initial handshake,
6033 * mitigating the triple handshake attack. On success, reuse
6034 * the original end-CRT instead of parsing it again. */
6035 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6036 if( ssl_check_peer_crt_unchanged( ssl,
6037 &ssl->in_msg[i],
6038 n ) != 0 )
6039 {
6040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6041 mbedtls_ssl_send_alert_message( ssl,
6042 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6043 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6044 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6045 }
6046
6047 /* Now we can safely free the original chain. */
6048 ssl_clear_peer_cert( ssl->session );
6049 }
6050#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6051
6052 /* Parse the next certificate in the chain. */
6053#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6054 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6055#else
6056 /* If we don't need to store the CRT chain permanently, parse
6057 * it in-place from the input buffer instead of making a copy. */
6058 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6059#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6060 switch( ret )
6061 {
6062 case 0: /*ok*/
6063 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6064 /* Ignore certificate with an unknown algorithm: maybe a
6065 prior certificate was already trusted. */
6066 break;
6067
6068 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6069 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6070 goto crt_parse_der_failed;
6071
6072 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6073 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6074 goto crt_parse_der_failed;
6075
6076 default:
6077 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6078 crt_parse_der_failed:
6079 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6080 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6081 return( ret );
6082 }
6083
6084 i += n;
6085 }
6086
6087 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6088 return( 0 );
6089}
6090
6091#if defined(MBEDTLS_SSL_SRV_C)
6092static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6093{
6094 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6095 return( -1 );
6096
6097 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6098 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6099 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6100 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6101 {
6102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6103 return( 0 );
6104 }
6105 return( -1 );
6106}
6107#endif /* MBEDTLS_SSL_SRV_C */
6108
6109/* Check if a certificate message is expected.
6110 * Return either
6111 * - SSL_CERTIFICATE_EXPECTED, or
6112 * - SSL_CERTIFICATE_SKIP
6113 * indicating whether a Certificate message is expected or not.
6114 */
6115#define SSL_CERTIFICATE_EXPECTED 0
6116#define SSL_CERTIFICATE_SKIP 1
6117static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6118 int authmode )
6119{
6120 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6121 ssl->handshake->ciphersuite_info;
6122
6123 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6124 return( SSL_CERTIFICATE_SKIP );
6125
6126#if defined(MBEDTLS_SSL_SRV_C)
6127 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6128 {
6129 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6130 return( SSL_CERTIFICATE_SKIP );
6131
6132 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6133 {
6134 ssl->session_negotiate->verify_result =
6135 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6136 return( SSL_CERTIFICATE_SKIP );
6137 }
6138 }
6139#else
6140 ((void) authmode);
6141#endif /* MBEDTLS_SSL_SRV_C */
6142
6143 return( SSL_CERTIFICATE_EXPECTED );
6144}
6145
6146static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6147 int authmode,
6148 mbedtls_x509_crt *chain,
6149 void *rs_ctx )
6150{
6151 int ret = 0;
6152 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6153 ssl->handshake->ciphersuite_info;
6154 int have_ca_chain = 0;
6155
6156 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6157 void *p_vrfy;
6158
6159 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6160 return( 0 );
6161
6162 if( ssl->f_vrfy != NULL )
6163 {
6164 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6165 f_vrfy = ssl->f_vrfy;
6166 p_vrfy = ssl->p_vrfy;
6167 }
6168 else
6169 {
6170 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6171 f_vrfy = ssl->conf->f_vrfy;
6172 p_vrfy = ssl->conf->p_vrfy;
6173 }
6174
6175 /*
6176 * Main check: verify certificate
6177 */
6178#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6179 if( ssl->conf->f_ca_cb != NULL )
6180 {
6181 ((void) rs_ctx);
6182 have_ca_chain = 1;
6183
6184 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6185 ret = mbedtls_x509_crt_verify_with_ca_cb(
6186 chain,
6187 ssl->conf->f_ca_cb,
6188 ssl->conf->p_ca_cb,
6189 ssl->conf->cert_profile,
6190 ssl->hostname,
6191 &ssl->session_negotiate->verify_result,
6192 f_vrfy, p_vrfy );
6193 }
6194 else
6195#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6196 {
6197 mbedtls_x509_crt *ca_chain;
6198 mbedtls_x509_crl *ca_crl;
6199
6200#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6201 if( ssl->handshake->sni_ca_chain != NULL )
6202 {
6203 ca_chain = ssl->handshake->sni_ca_chain;
6204 ca_crl = ssl->handshake->sni_ca_crl;
6205 }
6206 else
6207#endif
6208 {
6209 ca_chain = ssl->conf->ca_chain;
6210 ca_crl = ssl->conf->ca_crl;
6211 }
6212
6213 if( ca_chain != NULL )
6214 have_ca_chain = 1;
6215
6216 ret = mbedtls_x509_crt_verify_restartable(
6217 chain,
6218 ca_chain, ca_crl,
6219 ssl->conf->cert_profile,
6220 ssl->hostname,
6221 &ssl->session_negotiate->verify_result,
6222 f_vrfy, p_vrfy, rs_ctx );
6223 }
6224
6225 if( ret != 0 )
6226 {
6227 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6228 }
6229
6230#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6231 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6232 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6233#endif
6234
6235 /*
6236 * Secondary checks: always done, but change 'ret' only if it was 0
6237 */
6238
6239#if defined(MBEDTLS_ECP_C)
6240 {
6241 const mbedtls_pk_context *pk = &chain->pk;
6242
6243 /* If certificate uses an EC key, make sure the curve is OK */
6244 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6245 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6246 {
6247 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6248
6249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6250 if( ret == 0 )
6251 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6252 }
6253 }
6254#endif /* MBEDTLS_ECP_C */
6255
6256 if( mbedtls_ssl_check_cert_usage( chain,
6257 ciphersuite_info,
6258 ! ssl->conf->endpoint,
6259 &ssl->session_negotiate->verify_result ) != 0 )
6260 {
6261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6262 if( ret == 0 )
6263 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6264 }
6265
6266 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6267 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6268 * with details encoded in the verification flags. All other kinds
6269 * of error codes, including those from the user provided f_vrfy
6270 * functions, are treated as fatal and lead to a failure of
6271 * ssl_parse_certificate even if verification was optional. */
6272 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6273 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6274 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6275 {
6276 ret = 0;
6277 }
6278
6279 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6280 {
6281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6282 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6283 }
6284
6285 if( ret != 0 )
6286 {
6287 uint8_t alert;
6288
6289 /* The certificate may have been rejected for several reasons.
6290 Pick one and send the corresponding alert. Which alert to send
6291 may be a subject of debate in some cases. */
6292 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6293 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6294 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6295 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6296 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6297 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6298 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6299 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6300 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6301 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6302 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6303 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6304 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6305 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6306 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6307 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6308 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6309 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6310 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6311 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6312 else
6313 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6314 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6315 alert );
6316 }
6317
6318#if defined(MBEDTLS_DEBUG_C)
6319 if( ssl->session_negotiate->verify_result != 0 )
6320 {
6321 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6322 (unsigned int) ssl->session_negotiate->verify_result ) );
6323 }
6324 else
6325 {
6326 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6327 }
6328#endif /* MBEDTLS_DEBUG_C */
6329
6330 return( ret );
6331}
6332
6333#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6334static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6335 unsigned char *start, size_t len )
6336{
6337 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6338 /* Remember digest of the peer's end-CRT. */
6339 ssl->session_negotiate->peer_cert_digest =
6340 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6341 if( ssl->session_negotiate->peer_cert_digest == NULL )
6342 {
6343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6344 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6345 mbedtls_ssl_send_alert_message( ssl,
6346 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6347 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6348
6349 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6350 }
6351
6352 ret = mbedtls_md( mbedtls_md_info_from_type(
6353 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6354 start, len,
6355 ssl->session_negotiate->peer_cert_digest );
6356
6357 ssl->session_negotiate->peer_cert_digest_type =
6358 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6359 ssl->session_negotiate->peer_cert_digest_len =
6360 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6361
6362 return( ret );
6363}
6364
6365static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6366 unsigned char *start, size_t len )
6367{
6368 unsigned char *end = start + len;
6369 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6370
6371 /* Make a copy of the peer's raw public key. */
6372 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6373 ret = mbedtls_pk_parse_subpubkey( &start, end,
6374 &ssl->handshake->peer_pubkey );
6375 if( ret != 0 )
6376 {
6377 /* We should have parsed the public key before. */
6378 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6379 }
6380
6381 return( 0 );
6382}
6383#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6384
6385int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6386{
6387 int ret = 0;
6388 int crt_expected;
6389#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6390 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6391 ? ssl->handshake->sni_authmode
6392 : ssl->conf->authmode;
6393#else
6394 const int authmode = ssl->conf->authmode;
6395#endif
6396 void *rs_ctx = NULL;
6397 mbedtls_x509_crt *chain = NULL;
6398
6399 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6400
6401 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6402 if( crt_expected == SSL_CERTIFICATE_SKIP )
6403 {
6404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6405 goto exit;
6406 }
6407
6408#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6409 if( ssl->handshake->ecrs_enabled &&
6410 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6411 {
6412 chain = ssl->handshake->ecrs_peer_cert;
6413 ssl->handshake->ecrs_peer_cert = NULL;
6414 goto crt_verify;
6415 }
6416#endif
6417
6418 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6419 {
6420 /* mbedtls_ssl_read_record may have sent an alert already. We
6421 let it decide whether to alert. */
6422 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6423 goto exit;
6424 }
6425
6426#if defined(MBEDTLS_SSL_SRV_C)
6427 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6428 {
6429 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6430
6431 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6432 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6433
6434 goto exit;
6435 }
6436#endif /* MBEDTLS_SSL_SRV_C */
6437
6438 /* Clear existing peer CRT structure in case we tried to
6439 * reuse a session but it failed, and allocate a new one. */
6440 ssl_clear_peer_cert( ssl->session_negotiate );
6441
6442 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6443 if( chain == NULL )
6444 {
6445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6446 sizeof( mbedtls_x509_crt ) ) );
6447 mbedtls_ssl_send_alert_message( ssl,
6448 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6449 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6450
6451 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6452 goto exit;
6453 }
6454 mbedtls_x509_crt_init( chain );
6455
6456 ret = ssl_parse_certificate_chain( ssl, chain );
6457 if( ret != 0 )
6458 goto exit;
6459
6460#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6461 if( ssl->handshake->ecrs_enabled)
6462 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6463
6464crt_verify:
6465 if( ssl->handshake->ecrs_enabled)
6466 rs_ctx = &ssl->handshake->ecrs_ctx;
6467#endif
6468
6469 ret = ssl_parse_certificate_verify( ssl, authmode,
6470 chain, rs_ctx );
6471 if( ret != 0 )
6472 goto exit;
6473
6474#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6475 {
6476 unsigned char *crt_start, *pk_start;
6477 size_t crt_len, pk_len;
6478
6479 /* We parse the CRT chain without copying, so
6480 * these pointers point into the input buffer,
6481 * and are hence still valid after freeing the
6482 * CRT chain. */
6483
6484 crt_start = chain->raw.p;
6485 crt_len = chain->raw.len;
6486
6487 pk_start = chain->pk_raw.p;
6488 pk_len = chain->pk_raw.len;
6489
6490 /* Free the CRT structures before computing
6491 * digest and copying the peer's public key. */
6492 mbedtls_x509_crt_free( chain );
6493 mbedtls_free( chain );
6494 chain = NULL;
6495
6496 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6497 if( ret != 0 )
6498 goto exit;
6499
6500 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6501 if( ret != 0 )
6502 goto exit;
6503 }
6504#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6505 /* Pass ownership to session structure. */
6506 ssl->session_negotiate->peer_cert = chain;
6507 chain = NULL;
6508#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6509
6510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6511
6512exit:
6513
6514 if( ret == 0 )
6515 ssl->state++;
6516
6517#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6518 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6519 {
6520 ssl->handshake->ecrs_peer_cert = chain;
6521 chain = NULL;
6522 }
6523#endif
6524
6525 if( chain != NULL )
6526 {
6527 mbedtls_x509_crt_free( chain );
6528 mbedtls_free( chain );
6529 }
6530
6531 return( ret );
6532}
6533#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6534
Jerry Yu615bd6f2022-02-17 14:25:15 +08006535#if defined(MBEDTLS_SHA256_C)
6536static void ssl_calc_finished_tls_sha256(
6537 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6538{
6539 int len = 12;
6540 const char *sender;
6541 unsigned char padbuf[32];
6542#if defined(MBEDTLS_USE_PSA_CRYPTO)
6543 size_t hash_size;
6544 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
6545 psa_status_t status;
6546#else
6547 mbedtls_sha256_context sha256;
6548#endif
6549
6550 mbedtls_ssl_session *session = ssl->session_negotiate;
6551 if( !session )
6552 session = ssl->session;
6553
6554 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6555 ? "client finished"
6556 : "server finished";
6557
6558#if defined(MBEDTLS_USE_PSA_CRYPTO)
6559 sha256_psa = psa_hash_operation_init();
6560
6561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6562
6563 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6564 if( status != PSA_SUCCESS )
6565 {
6566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6567 return;
6568 }
6569
6570 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6571 if( status != PSA_SUCCESS )
6572 {
6573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6574 return;
6575 }
6576 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6577#else
6578
6579 mbedtls_sha256_init( &sha256 );
6580
6581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
6582
6583 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6584
6585 /*
6586 * TLSv1.2:
6587 * hash = PRF( master, finished_label,
6588 * Hash( handshake ) )[0.11]
6589 */
6590
6591#if !defined(MBEDTLS_SHA256_ALT)
6592 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
6593 sha256.state, sizeof( sha256.state ) );
6594#endif
6595
6596 mbedtls_sha256_finish( &sha256, padbuf );
6597 mbedtls_sha256_free( &sha256 );
6598#endif /* MBEDTLS_USE_PSA_CRYPTO */
6599
6600 ssl->handshake->tls_prf( session->master, 48, sender,
6601 padbuf, 32, buf, len );
6602
6603 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6604
6605 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6606
6607 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6608}
6609#endif /* MBEDTLS_SHA256_C */
6610
Jerry Yub7ba49e2022-02-17 14:25:53 +08006611
6612#if defined(MBEDTLS_SHA384_C)
6613
6614static void ssl_calc_finished_tls_sha384(
6615 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6616{
6617 int len = 12;
6618 const char *sender;
6619 unsigned char padbuf[48];
6620#if defined(MBEDTLS_USE_PSA_CRYPTO)
6621 size_t hash_size;
6622 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
6623 psa_status_t status;
6624#else
6625 mbedtls_sha512_context sha512;
6626#endif
6627
6628 mbedtls_ssl_session *session = ssl->session_negotiate;
6629 if( !session )
6630 session = ssl->session;
6631
6632 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6633 ? "client finished"
6634 : "server finished";
6635
6636#if defined(MBEDTLS_USE_PSA_CRYPTO)
6637 sha384_psa = psa_hash_operation_init();
6638
6639 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6640
6641 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6642 if( status != PSA_SUCCESS )
6643 {
6644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6645 return;
6646 }
6647
6648 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
6649 if( status != PSA_SUCCESS )
6650 {
6651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6652 return;
6653 }
6654 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
6655#else
6656 mbedtls_sha512_init( &sha512 );
6657
6658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
6659
6660 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
6661
6662 /*
6663 * TLSv1.2:
6664 * hash = PRF( master, finished_label,
6665 * Hash( handshake ) )[0.11]
6666 */
6667
6668#if !defined(MBEDTLS_SHA512_ALT)
6669 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6670 sha512.state, sizeof( sha512.state ) );
6671#endif
6672 mbedtls_sha512_finish( &sha512, padbuf );
6673
6674 mbedtls_sha512_free( &sha512 );
6675#endif
6676
6677 ssl->handshake->tls_prf( session->master, 48, sender,
6678 padbuf, 48, buf, len );
6679
6680 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6681
6682 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6683
6684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6685}
6686#endif /* MBEDTLS_SHA384_C */
6687
Jerry Yuaef00152022-02-17 14:27:31 +08006688void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
6689{
6690 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
6691
6692 /*
6693 * Free our handshake params
6694 */
6695 mbedtls_ssl_handshake_free( ssl );
6696 mbedtls_free( ssl->handshake );
6697 ssl->handshake = NULL;
6698
6699 /*
6700 * Free the previous transform and swith in the current one
6701 */
6702 if( ssl->transform )
6703 {
6704 mbedtls_ssl_transform_free( ssl->transform );
6705 mbedtls_free( ssl->transform );
6706 }
6707 ssl->transform = ssl->transform_negotiate;
6708 ssl->transform_negotiate = NULL;
6709
6710 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
6711}
6712
Jerry Yu2a9fff52022-02-17 14:28:51 +08006713void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
6714{
6715 int resume = ssl->handshake->resume;
6716
6717 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
6718
6719#if defined(MBEDTLS_SSL_RENEGOTIATION)
6720 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6721 {
6722 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
6723 ssl->renego_records_seen = 0;
6724 }
6725#endif
6726
6727 /*
6728 * Free the previous session and switch in the current one
6729 */
6730 if( ssl->session )
6731 {
6732#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6733 /* RFC 7366 3.1: keep the EtM state */
6734 ssl->session_negotiate->encrypt_then_mac =
6735 ssl->session->encrypt_then_mac;
6736#endif
6737
6738 mbedtls_ssl_session_free( ssl->session );
6739 mbedtls_free( ssl->session );
6740 }
6741 ssl->session = ssl->session_negotiate;
6742 ssl->session_negotiate = NULL;
6743
6744 /*
6745 * Add cache entry
6746 */
6747 if( ssl->conf->f_set_cache != NULL &&
6748 ssl->session->id_len != 0 &&
6749 resume == 0 )
6750 {
6751 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
6752 ssl->session->id,
6753 ssl->session->id_len,
6754 ssl->session ) != 0 )
6755 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
6756 }
6757
6758#if defined(MBEDTLS_SSL_PROTO_DTLS)
6759 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6760 ssl->handshake->flight != NULL )
6761 {
6762 /* Cancel handshake timer */
6763 mbedtls_ssl_set_timer( ssl, 0 );
6764
6765 /* Keep last flight around in case we need to resend it:
6766 * we need the handshake and transform structures for that */
6767 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
6768 }
6769 else
6770#endif
6771 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
6772
6773 ssl->state++;
6774
6775 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
6776}
6777
Jerry Yu3c8e47b2022-02-17 14:30:01 +08006778int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
6779{
6780 int ret, hash_len;
6781
6782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
6783
6784 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
6785
6786 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
6787
6788 /*
6789 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6790 * may define some other value. Currently (early 2016), no defined
6791 * ciphersuite does this (and this is unlikely to change as activity has
6792 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6793 */
6794 hash_len = 12;
6795
6796#if defined(MBEDTLS_SSL_RENEGOTIATION)
6797 ssl->verify_data_len = hash_len;
6798 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
6799#endif
6800
6801 ssl->out_msglen = 4 + hash_len;
6802 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6803 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
6804
6805 /*
6806 * In case of session resuming, invert the client and server
6807 * ChangeCipherSpec messages order.
6808 */
6809 if( ssl->handshake->resume != 0 )
6810 {
6811#if defined(MBEDTLS_SSL_CLI_C)
6812 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6813 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
6814#endif
6815#if defined(MBEDTLS_SSL_SRV_C)
6816 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6817 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
6818#endif
6819 }
6820 else
6821 ssl->state++;
6822
6823 /*
6824 * Switch to our negotiated transform and session parameters for outbound
6825 * data.
6826 */
6827 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
6828
6829#if defined(MBEDTLS_SSL_PROTO_DTLS)
6830 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6831 {
6832 unsigned char i;
6833
6834 /* Remember current epoch settings for resending */
6835 ssl->handshake->alt_transform_out = ssl->transform_out;
6836 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
6837 sizeof( ssl->handshake->alt_out_ctr ) );
6838
6839 /* Set sequence_number to zero */
6840 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
6841
6842
6843 /* Increment epoch */
6844 for( i = 2; i > 0; i-- )
6845 if( ++ssl->cur_out_ctr[i - 1] != 0 )
6846 break;
6847
6848 /* The loop goes to its end iff the counter is wrapping */
6849 if( i == 0 )
6850 {
6851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6852 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
6853 }
6854 }
6855 else
6856#endif /* MBEDTLS_SSL_PROTO_DTLS */
6857 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6858
6859 ssl->transform_out = ssl->transform_negotiate;
6860 ssl->session_out = ssl->session_negotiate;
6861
6862#if defined(MBEDTLS_SSL_PROTO_DTLS)
6863 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6864 mbedtls_ssl_send_flight_completed( ssl );
6865#endif
6866
6867 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6868 {
6869 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6870 return( ret );
6871 }
6872
6873#if defined(MBEDTLS_SSL_PROTO_DTLS)
6874 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6875 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6876 {
6877 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6878 return( ret );
6879 }
6880#endif
6881
6882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
6883
6884 return( 0 );
6885}
6886
Jerry Yu0b3d7c12022-02-17 14:30:51 +08006887#define SSL_MAX_HASH_LEN 12
6888
6889int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
6890{
6891 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6892 unsigned int hash_len = 12;
6893 unsigned char buf[SSL_MAX_HASH_LEN];
6894
6895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
6896
6897 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
6898
6899 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6900 {
6901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6902 goto exit;
6903 }
6904
6905 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6906 {
6907 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6908 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6909 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6910 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
6911 goto exit;
6912 }
6913
6914 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
6915 {
6916 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6917 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6918 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
6919 goto exit;
6920 }
6921
6922 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
6923 {
6924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6925 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6926 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6927 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
6928 goto exit;
6929 }
6930
6931 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
6932 buf, hash_len ) != 0 )
6933 {
6934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6935 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6936 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
6937 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
6938 goto exit;
6939 }
6940
6941#if defined(MBEDTLS_SSL_RENEGOTIATION)
6942 ssl->verify_data_len = hash_len;
6943 memcpy( ssl->peer_verify_data, buf, hash_len );
6944#endif
6945
6946 if( ssl->handshake->resume != 0 )
6947 {
6948#if defined(MBEDTLS_SSL_CLI_C)
6949 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6950 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
6951#endif
6952#if defined(MBEDTLS_SSL_SRV_C)
6953 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6954 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
6955#endif
6956 }
6957 else
6958 ssl->state++;
6959
6960#if defined(MBEDTLS_SSL_PROTO_DTLS)
6961 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6962 mbedtls_ssl_recv_flight_completed( ssl );
6963#endif
6964
6965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
6966
6967exit:
6968 mbedtls_platform_zeroize( buf, hash_len );
6969 return( ret );
6970}
6971
Jerry Yu392112c2022-02-17 14:34:10 +08006972#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
6973/*
6974 * Helper to get TLS 1.2 PRF from ciphersuite
6975 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
6976 */
6977static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
6978{
6979#if defined(MBEDTLS_SHA384_C)
6980 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6981 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
6982
6983 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
6984 return( tls_prf_sha384 );
6985#else
6986 (void) ciphersuite_id;
6987#endif
6988 return( tls_prf_sha256 );
6989}
6990#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08006991
6992static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
6993{
6994 ((void) tls_prf);
6995#if defined(MBEDTLS_SHA384_C)
6996 if( tls_prf == tls_prf_sha384 )
6997 {
6998 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
6999 }
7000 else
7001#endif
7002#if defined(MBEDTLS_SHA256_C)
7003 if( tls_prf == tls_prf_sha256 )
7004 {
7005 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7006 }
7007 else
7008#endif
7009 return( MBEDTLS_SSL_TLS_PRF_NONE );
7010}
7011
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007012/*
7013 * Populate a transform structure with session keys and all the other
7014 * necessary information.
7015 *
7016 * Parameters:
7017 * - [in/out]: transform: structure to populate
7018 * [in] must be just initialised with mbedtls_ssl_transform_init()
7019 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7020 * - [in] ciphersuite
7021 * - [in] master
7022 * - [in] encrypt_then_mac
7023 * - [in] compression
7024 * - [in] tls_prf: pointer to PRF to use for key derivation
7025 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
7026 * - [in] minor_ver: SSL/TLS minor version
7027 * - [in] endpoint: client or server
7028 * - [in] ssl: used for:
7029 * - ssl->conf->{f,p}_export_keys
7030 * [in] optionally used for:
7031 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7032 */
7033static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7034 int ciphersuite,
7035 const unsigned char master[48],
7036#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
7037 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7038 int encrypt_then_mac,
7039#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
7040 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7041 ssl_tls_prf_t tls_prf,
7042 const unsigned char randbytes[64],
7043 int minor_ver,
7044 unsigned endpoint,
7045 const mbedtls_ssl_context *ssl )
7046{
7047 int ret = 0;
7048 unsigned char keyblk[256];
7049 unsigned char *key1;
7050 unsigned char *key2;
7051 unsigned char *mac_enc;
7052 unsigned char *mac_dec;
7053 size_t mac_key_len = 0;
7054 size_t iv_copy_len;
7055 size_t keylen;
7056 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
7057 const mbedtls_cipher_info_t *cipher_info;
7058 const mbedtls_md_info_t *md_info;
7059
7060#if defined(MBEDTLS_USE_PSA_CRYPTO)
7061 psa_key_type_t key_type;
7062 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7063 psa_algorithm_t alg;
7064 size_t key_bits;
7065 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7066#endif
7067
7068#if !defined(MBEDTLS_DEBUG_C) && \
7069 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7070 if( ssl->f_export_keys == NULL )
7071 {
7072 ssl = NULL; /* make sure we don't use it except for these cases */
7073 (void) ssl;
7074 }
7075#endif
7076
7077 /*
7078 * Some data just needs copying into the structure
7079 */
7080#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
7081 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7082 transform->encrypt_then_mac = encrypt_then_mac;
7083#endif
7084 transform->minor_ver = minor_ver;
7085
7086#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7087 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7088#endif
7089
7090#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
7091 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
7092 {
7093 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7094 * generation separate. This should never happen. */
7095 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7096 }
7097#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7098
7099 /*
7100 * Get various info structures
7101 */
7102 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7103 if( ciphersuite_info == NULL )
7104 {
7105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7106 ciphersuite ) );
7107 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7108 }
7109
7110 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7111 if( cipher_info == NULL )
7112 {
7113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7114 ciphersuite_info->cipher ) );
7115 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7116 }
7117
7118 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7119 if( md_info == NULL )
7120 {
7121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7122 (unsigned) ciphersuite_info->mac ) );
7123 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7124 }
7125
7126#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7127 /* Copy own and peer's CID if the use of the CID
7128 * extension has been negotiated. */
7129 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7130 {
7131 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7132
7133 transform->in_cid_len = ssl->own_cid_len;
7134 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7135 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7136 transform->in_cid_len );
7137
7138 transform->out_cid_len = ssl->handshake->peer_cid_len;
7139 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7140 ssl->handshake->peer_cid_len );
7141 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7142 transform->out_cid_len );
7143 }
7144#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7145
7146 /*
7147 * Compute key block using the PRF
7148 */
7149 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7150 if( ret != 0 )
7151 {
7152 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7153 return( ret );
7154 }
7155
7156 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7157 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7158 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7159 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7160 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7161
7162 /*
7163 * Determine the appropriate key, IV and MAC length.
7164 */
7165
7166 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
7167
7168#if defined(MBEDTLS_GCM_C) || \
7169 defined(MBEDTLS_CCM_C) || \
7170 defined(MBEDTLS_CHACHAPOLY_C)
7171 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_GCM ||
7172 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CCM ||
7173 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
7174 {
7175 size_t explicit_ivlen;
7176
7177 transform->maclen = 0;
7178 mac_key_len = 0;
7179 transform->taglen =
7180 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7181
7182 /* All modes haves 96-bit IVs, but the length of the static parts vary
7183 * with mode and version:
7184 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7185 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7186 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7187 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7188 * sequence number).
7189 */
7190 transform->ivlen = 12;
7191 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
7192 transform->fixed_ivlen = 12;
7193 else
7194 transform->fixed_ivlen = 4;
7195
7196 /* Minimum length of encrypted record */
7197 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7198 transform->minlen = explicit_ivlen + transform->taglen;
7199 }
7200 else
7201#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7202#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7203 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM ||
7204 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7205 {
Neil Armstrongcf8841a2022-02-24 11:17:45 +01007206#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007207 /* Initialize HMAC contexts */
7208 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7209 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7210 {
7211 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7212 goto end;
7213 }
Neil Armstrongcf8841a2022-02-24 11:17:45 +01007214#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007215
7216 /* Get MAC length */
7217 mac_key_len = mbedtls_md_get_size( md_info );
7218 transform->maclen = mac_key_len;
7219
7220 /* IV length */
7221 transform->ivlen = cipher_info->iv_size;
7222
7223 /* Minimum length */
7224 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM )
7225 transform->minlen = transform->maclen;
7226 else
7227 {
7228 /*
7229 * GenericBlockCipher:
7230 * 1. if EtM is in use: one block plus MAC
7231 * otherwise: * first multiple of blocklen greater than maclen
7232 * 2. IV
7233 */
7234#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7235 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
7236 {
7237 transform->minlen = transform->maclen
7238 + cipher_info->block_size;
7239 }
7240 else
7241#endif
7242 {
7243 transform->minlen = transform->maclen
7244 + cipher_info->block_size
7245 - transform->maclen % cipher_info->block_size;
7246 }
7247
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007248 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
7249 {
7250 transform->minlen += transform->ivlen;
7251 }
7252 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007253 {
7254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7255 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7256 goto end;
7257 }
7258 }
7259 }
7260 else
7261#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7262 {
7263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7264 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7265 }
7266
7267 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7268 (unsigned) keylen,
7269 (unsigned) transform->minlen,
7270 (unsigned) transform->ivlen,
7271 (unsigned) transform->maclen ) );
7272
7273 /*
7274 * Finally setup the cipher contexts, IVs and MAC secrets.
7275 */
7276#if defined(MBEDTLS_SSL_CLI_C)
7277 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7278 {
7279 key1 = keyblk + mac_key_len * 2;
7280 key2 = keyblk + mac_key_len * 2 + keylen;
7281
7282 mac_enc = keyblk;
7283 mac_dec = keyblk + mac_key_len;
7284
7285 /*
7286 * This is not used in TLS v1.1.
7287 */
7288 iv_copy_len = ( transform->fixed_ivlen ) ?
7289 transform->fixed_ivlen : transform->ivlen;
7290 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7291 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7292 iv_copy_len );
7293 }
7294 else
7295#endif /* MBEDTLS_SSL_CLI_C */
7296#if defined(MBEDTLS_SSL_SRV_C)
7297 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7298 {
7299 key1 = keyblk + mac_key_len * 2 + keylen;
7300 key2 = keyblk + mac_key_len * 2;
7301
7302 mac_enc = keyblk + mac_key_len;
7303 mac_dec = keyblk;
7304
7305 /*
7306 * This is not used in TLS v1.1.
7307 */
7308 iv_copy_len = ( transform->fixed_ivlen ) ?
7309 transform->fixed_ivlen : transform->ivlen;
7310 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7311 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7312 iv_copy_len );
7313 }
7314 else
7315#endif /* MBEDTLS_SSL_SRV_C */
7316 {
7317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7318 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7319 goto end;
7320 }
7321
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007322 if( ssl != NULL && ssl->f_export_keys != NULL )
7323 {
7324 ssl->f_export_keys( ssl->p_export_keys,
7325 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7326 master, 48,
7327 randbytes + 32,
7328 randbytes,
7329 tls_prf_get_type( tls_prf ) );
7330 }
7331
7332#if defined(MBEDTLS_USE_PSA_CRYPTO)
7333 if( ( status = mbedtls_ssl_cipher_to_psa( cipher_info->type,
7334 transform->taglen,
7335 &alg,
7336 &key_type,
7337 &key_bits ) ) != PSA_SUCCESS )
7338 {
7339 ret = psa_ssl_status_to_mbedtls( status );
7340 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7341 goto end;
7342 }
7343
7344 transform->psa_alg = alg;
7345
7346 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7347 {
7348 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7349 psa_set_key_algorithm( &attributes, alg );
7350 psa_set_key_type( &attributes, key_type );
7351
7352 if( ( status = psa_import_key( &attributes,
7353 key1,
7354 PSA_BITS_TO_BYTES( key_bits ),
7355 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7356 {
7357 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7358 ret = psa_ssl_status_to_mbedtls( status );
7359 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7360 goto end;
7361 }
7362
7363 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7364
7365 if( ( status = psa_import_key( &attributes,
7366 key2,
7367 PSA_BITS_TO_BYTES( key_bits ),
7368 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7369 {
7370 ret = psa_ssl_status_to_mbedtls( status );
7371 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7372 goto end;
7373 }
7374 }
7375#else
7376 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7377 cipher_info ) ) != 0 )
7378 {
7379 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7380 goto end;
7381 }
7382
7383 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7384 cipher_info ) ) != 0 )
7385 {
7386 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7387 goto end;
7388 }
7389
7390 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7391 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7392 MBEDTLS_ENCRYPT ) ) != 0 )
7393 {
7394 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7395 goto end;
7396 }
7397
7398 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7399 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7400 MBEDTLS_DECRYPT ) ) != 0 )
7401 {
7402 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7403 goto end;
7404 }
7405
7406#if defined(MBEDTLS_CIPHER_MODE_CBC)
7407 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7408 {
7409 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7410 MBEDTLS_PADDING_NONE ) ) != 0 )
7411 {
7412 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7413 goto end;
7414 }
7415
7416 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7417 MBEDTLS_PADDING_NONE ) ) != 0 )
7418 {
7419 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7420 goto end;
7421 }
7422 }
7423#endif /* MBEDTLS_CIPHER_MODE_CBC */
7424#endif /* MBEDTLS_USE_PSA_CRYPTO */
7425
Neil Armstrong29c0c042022-03-17 17:47:28 +01007426#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7427 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7428 For AEAD-based ciphersuites, there is nothing to do here. */
7429 if( mac_key_len != 0 )
7430 {
7431#if defined(MBEDTLS_USE_PSA_CRYPTO)
7432 alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
7433 if( alg == 0 )
7434 {
7435 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_md_type_to_psa", ret );
7437 goto end;
7438 }
7439
7440 transform->psa_mac_alg = PSA_ALG_HMAC( alg );
7441
7442 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
7443 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( alg ) );
7444 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7445
7446 if( ( status = psa_import_key( &attributes,
7447 mac_enc, mac_key_len,
7448 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7449 {
7450 ret = psa_ssl_status_to_mbedtls( status );
7451 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7452 goto end;
7453 }
7454
Ronald Cronfb39f152022-03-25 14:36:28 +01007455 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
7456 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) &&
7457 ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007458 /* mbedtls_ct_hmac() requires the key to be exportable */
7459 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7460 PSA_KEY_USAGE_VERIFY_HASH );
7461 else
7462 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7463
7464 if( ( status = psa_import_key( &attributes,
7465 mac_dec, mac_key_len,
7466 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7467 {
7468 ret = psa_ssl_status_to_mbedtls( status );
7469 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7470 goto end;
7471 }
7472#else
7473 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7474 if( ret != 0 )
7475 goto end;
7476 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7477 if( ret != 0 )
7478 goto end;
7479#endif /* MBEDTLS_USE_PSA_CRYPTO */
7480 }
7481#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7482
7483 ((void) mac_dec);
7484 ((void) mac_enc);
7485
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007486end:
7487 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7488 return( ret );
7489}
7490
Jerry Yuee40f9d2022-02-17 14:55:16 +08007491#if defined(MBEDTLS_USE_PSA_CRYPTO)
7492int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7493 unsigned char *hash, size_t *hashlen,
7494 unsigned char *data, size_t data_len,
7495 mbedtls_md_type_t md_alg )
7496{
7497 psa_status_t status;
7498 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
7499 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
7500
7501 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7502
7503 if( ( status = psa_hash_setup( &hash_operation,
7504 hash_alg ) ) != PSA_SUCCESS )
7505 {
7506 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7507 goto exit;
7508 }
7509
7510 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
7511 64 ) ) != PSA_SUCCESS )
7512 {
7513 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7514 goto exit;
7515 }
7516
7517 if( ( status = psa_hash_update( &hash_operation,
7518 data, data_len ) ) != PSA_SUCCESS )
7519 {
7520 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7521 goto exit;
7522 }
7523
7524 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
7525 hashlen ) ) != PSA_SUCCESS )
7526 {
7527 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
7528 goto exit;
7529 }
7530
7531exit:
7532 if( status != PSA_SUCCESS )
7533 {
7534 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7535 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7536 switch( status )
7537 {
7538 case PSA_ERROR_NOT_SUPPORTED:
7539 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
7540 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
7541 case PSA_ERROR_BUFFER_TOO_SMALL:
7542 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
7543 case PSA_ERROR_INSUFFICIENT_MEMORY:
7544 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
7545 default:
7546 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
7547 }
7548 }
7549 return( 0 );
7550}
7551
7552#else
7553
7554int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7555 unsigned char *hash, size_t *hashlen,
7556 unsigned char *data, size_t data_len,
7557 mbedtls_md_type_t md_alg )
7558{
7559 int ret = 0;
7560 mbedtls_md_context_t ctx;
7561 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
7562 *hashlen = mbedtls_md_get_size( md_info );
7563
7564 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
7565
7566 mbedtls_md_init( &ctx );
7567
7568 /*
7569 * digitally-signed struct {
7570 * opaque client_random[32];
7571 * opaque server_random[32];
7572 * ServerDHParams params;
7573 * };
7574 */
7575 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
7576 {
7577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7578 goto exit;
7579 }
7580 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
7581 {
7582 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
7583 goto exit;
7584 }
7585 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
7586 {
7587 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7588 goto exit;
7589 }
7590 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
7591 {
7592 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7593 goto exit;
7594 }
7595 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
7596 {
7597 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
7598 goto exit;
7599 }
7600
7601exit:
7602 mbedtls_md_free( &ctx );
7603
7604 if( ret != 0 )
7605 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7606 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7607
7608 return( ret );
7609}
7610#endif /* MBEDTLS_USE_PSA_CRYPTO */
7611
Jerry Yud9d91da2022-02-17 14:57:06 +08007612#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
7613
7614/* Find an entry in a signature-hash set matching a given hash algorithm. */
7615mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
7616 mbedtls_pk_type_t sig_alg )
7617{
7618 switch( sig_alg )
7619 {
7620 case MBEDTLS_PK_RSA:
7621 return( set->rsa );
7622 case MBEDTLS_PK_ECDSA:
7623 return( set->ecdsa );
7624 default:
7625 return( MBEDTLS_MD_NONE );
7626 }
7627}
7628
7629/* Add a signature-hash-pair to a signature-hash set */
7630void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
7631 mbedtls_pk_type_t sig_alg,
7632 mbedtls_md_type_t md_alg )
7633{
7634 switch( sig_alg )
7635 {
7636 case MBEDTLS_PK_RSA:
7637 if( set->rsa == MBEDTLS_MD_NONE )
7638 set->rsa = md_alg;
7639 break;
7640
7641 case MBEDTLS_PK_ECDSA:
7642 if( set->ecdsa == MBEDTLS_MD_NONE )
7643 set->ecdsa = md_alg;
7644 break;
7645
7646 default:
7647 break;
7648 }
7649}
7650
7651/* Allow exactly one hash algorithm for each signature. */
7652void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
7653 mbedtls_md_type_t md_alg )
7654{
7655 set->rsa = md_alg;
7656 set->ecdsa = md_alg;
7657}
7658
7659#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7660
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08007661/* Serialization of TLS 1.2 sessions:
7662 *
7663 * struct {
7664 * uint64 start_time;
7665 * uint8 ciphersuite[2]; // defined by the standard
7666 * uint8 compression; // 0 or 1
7667 * uint8 session_id_len; // at most 32
7668 * opaque session_id[32];
7669 * opaque master[48]; // fixed length in the standard
7670 * uint32 verify_result;
7671 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
7672 * opaque ticket<0..2^24-1>; // length 0 means no ticket
7673 * uint32 ticket_lifetime;
7674 * uint8 mfl_code; // up to 255 according to standard
7675 * uint8 encrypt_then_mac; // 0 or 1
7676 * } serialized_session_tls12;
7677 *
7678 */
7679static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session,
7680 unsigned char *buf,
7681 size_t buf_len )
7682{
7683 unsigned char *p = buf;
7684 size_t used = 0;
7685
7686#if defined(MBEDTLS_HAVE_TIME)
7687 uint64_t start;
7688#endif
7689#if defined(MBEDTLS_X509_CRT_PARSE_C)
7690#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7691 size_t cert_len;
7692#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7693#endif /* MBEDTLS_X509_CRT_PARSE_C */
7694
7695 /*
7696 * Time
7697 */
7698#if defined(MBEDTLS_HAVE_TIME)
7699 used += 8;
7700
7701 if( used <= buf_len )
7702 {
7703 start = (uint64_t) session->start;
7704
7705 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
7706 p += 8;
7707 }
7708#endif /* MBEDTLS_HAVE_TIME */
7709
7710 /*
7711 * Basic mandatory fields
7712 */
7713 used += 2 /* ciphersuite */
7714 + 1 /* compression */
7715 + 1 /* id_len */
7716 + sizeof( session->id )
7717 + sizeof( session->master )
7718 + 4; /* verify_result */
7719
7720 if( used <= buf_len )
7721 {
7722 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
7723 p += 2;
7724
7725 *p++ = MBEDTLS_BYTE_0( session->compression );
7726
7727 *p++ = MBEDTLS_BYTE_0( session->id_len );
7728 memcpy( p, session->id, 32 );
7729 p += 32;
7730
7731 memcpy( p, session->master, 48 );
7732 p += 48;
7733
7734 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
7735 p += 4;
7736 }
7737
7738 /*
7739 * Peer's end-entity certificate
7740 */
7741#if defined(MBEDTLS_X509_CRT_PARSE_C)
7742#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7743 if( session->peer_cert == NULL )
7744 cert_len = 0;
7745 else
7746 cert_len = session->peer_cert->raw.len;
7747
7748 used += 3 + cert_len;
7749
7750 if( used <= buf_len )
7751 {
7752 *p++ = MBEDTLS_BYTE_2( cert_len );
7753 *p++ = MBEDTLS_BYTE_1( cert_len );
7754 *p++ = MBEDTLS_BYTE_0( cert_len );
7755
7756 if( session->peer_cert != NULL )
7757 {
7758 memcpy( p, session->peer_cert->raw.p, cert_len );
7759 p += cert_len;
7760 }
7761 }
7762#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7763 if( session->peer_cert_digest != NULL )
7764 {
7765 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
7766 if( used <= buf_len )
7767 {
7768 *p++ = (unsigned char) session->peer_cert_digest_type;
7769 *p++ = (unsigned char) session->peer_cert_digest_len;
7770 memcpy( p, session->peer_cert_digest,
7771 session->peer_cert_digest_len );
7772 p += session->peer_cert_digest_len;
7773 }
7774 }
7775 else
7776 {
7777 used += 2;
7778 if( used <= buf_len )
7779 {
7780 *p++ = (unsigned char) MBEDTLS_MD_NONE;
7781 *p++ = 0;
7782 }
7783 }
7784#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7785#endif /* MBEDTLS_X509_CRT_PARSE_C */
7786
7787 /*
7788 * Session ticket if any, plus associated data
7789 */
7790#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7791 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
7792
7793 if( used <= buf_len )
7794 {
7795 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
7796 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
7797 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
7798
7799 if( session->ticket != NULL )
7800 {
7801 memcpy( p, session->ticket, session->ticket_len );
7802 p += session->ticket_len;
7803 }
7804
7805 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
7806 p += 4;
7807 }
7808#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7809
7810 /*
7811 * Misc extension-related info
7812 */
7813#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7814 used += 1;
7815
7816 if( used <= buf_len )
7817 *p++ = session->mfl_code;
7818#endif
7819
7820#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7821 used += 1;
7822
7823 if( used <= buf_len )
7824 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
7825#endif
7826
7827 return( used );
7828}
7829
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08007830static int ssl_session_load_tls12( mbedtls_ssl_session *session,
7831 const unsigned char *buf,
7832 size_t len )
7833{
7834#if defined(MBEDTLS_HAVE_TIME)
7835 uint64_t start;
7836#endif
7837#if defined(MBEDTLS_X509_CRT_PARSE_C)
7838#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7839 size_t cert_len;
7840#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7841#endif /* MBEDTLS_X509_CRT_PARSE_C */
7842
7843 const unsigned char *p = buf;
7844 const unsigned char * const end = buf + len;
7845
7846 /*
7847 * Time
7848 */
7849#if defined(MBEDTLS_HAVE_TIME)
7850 if( 8 > (size_t)( end - p ) )
7851 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7852
7853 start = ( (uint64_t) p[0] << 56 ) |
7854 ( (uint64_t) p[1] << 48 ) |
7855 ( (uint64_t) p[2] << 40 ) |
7856 ( (uint64_t) p[3] << 32 ) |
7857 ( (uint64_t) p[4] << 24 ) |
7858 ( (uint64_t) p[5] << 16 ) |
7859 ( (uint64_t) p[6] << 8 ) |
7860 ( (uint64_t) p[7] );
7861 p += 8;
7862
7863 session->start = (time_t) start;
7864#endif /* MBEDTLS_HAVE_TIME */
7865
7866 /*
7867 * Basic mandatory fields
7868 */
7869 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
7870 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7871
7872 session->ciphersuite = ( p[0] << 8 ) | p[1];
7873 p += 2;
7874
7875 session->compression = *p++;
7876
7877 session->id_len = *p++;
7878 memcpy( session->id, p, 32 );
7879 p += 32;
7880
7881 memcpy( session->master, p, 48 );
7882 p += 48;
7883
7884 session->verify_result = ( (uint32_t) p[0] << 24 ) |
7885 ( (uint32_t) p[1] << 16 ) |
7886 ( (uint32_t) p[2] << 8 ) |
7887 ( (uint32_t) p[3] );
7888 p += 4;
7889
7890 /* Immediately clear invalid pointer values that have been read, in case
7891 * we exit early before we replaced them with valid ones. */
7892#if defined(MBEDTLS_X509_CRT_PARSE_C)
7893#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7894 session->peer_cert = NULL;
7895#else
7896 session->peer_cert_digest = NULL;
7897#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7898#endif /* MBEDTLS_X509_CRT_PARSE_C */
7899#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7900 session->ticket = NULL;
7901#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7902
7903 /*
7904 * Peer certificate
7905 */
7906#if defined(MBEDTLS_X509_CRT_PARSE_C)
7907#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7908 /* Deserialize CRT from the end of the ticket. */
7909 if( 3 > (size_t)( end - p ) )
7910 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7911
7912 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
7913 p += 3;
7914
7915 if( cert_len != 0 )
7916 {
7917 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7918
7919 if( cert_len > (size_t)( end - p ) )
7920 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7921
7922 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7923
7924 if( session->peer_cert == NULL )
7925 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7926
7927 mbedtls_x509_crt_init( session->peer_cert );
7928
7929 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
7930 p, cert_len ) ) != 0 )
7931 {
7932 mbedtls_x509_crt_free( session->peer_cert );
7933 mbedtls_free( session->peer_cert );
7934 session->peer_cert = NULL;
7935 return( ret );
7936 }
7937
7938 p += cert_len;
7939 }
7940#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7941 /* Deserialize CRT digest from the end of the ticket. */
7942 if( 2 > (size_t)( end - p ) )
7943 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7944
7945 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
7946 session->peer_cert_digest_len = (size_t) *p++;
7947
7948 if( session->peer_cert_digest_len != 0 )
7949 {
7950 const mbedtls_md_info_t *md_info =
7951 mbedtls_md_info_from_type( session->peer_cert_digest_type );
7952 if( md_info == NULL )
7953 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7954 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
7955 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7956
7957 if( session->peer_cert_digest_len > (size_t)( end - p ) )
7958 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7959
7960 session->peer_cert_digest =
7961 mbedtls_calloc( 1, session->peer_cert_digest_len );
7962 if( session->peer_cert_digest == NULL )
7963 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7964
7965 memcpy( session->peer_cert_digest, p,
7966 session->peer_cert_digest_len );
7967 p += session->peer_cert_digest_len;
7968 }
7969#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7970#endif /* MBEDTLS_X509_CRT_PARSE_C */
7971
7972 /*
7973 * Session ticket and associated data
7974 */
7975#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7976 if( 3 > (size_t)( end - p ) )
7977 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7978
7979 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
7980 p += 3;
7981
7982 if( session->ticket_len != 0 )
7983 {
7984 if( session->ticket_len > (size_t)( end - p ) )
7985 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7986
7987 session->ticket = mbedtls_calloc( 1, session->ticket_len );
7988 if( session->ticket == NULL )
7989 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7990
7991 memcpy( session->ticket, p, session->ticket_len );
7992 p += session->ticket_len;
7993 }
7994
7995 if( 4 > (size_t)( end - p ) )
7996 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7997
7998 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
7999 ( (uint32_t) p[1] << 16 ) |
8000 ( (uint32_t) p[2] << 8 ) |
8001 ( (uint32_t) p[3] );
8002 p += 4;
8003#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8004
8005 /*
8006 * Misc extension-related info
8007 */
8008#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8009 if( 1 > (size_t)( end - p ) )
8010 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8011
8012 session->mfl_code = *p++;
8013#endif
8014
8015#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8016 if( 1 > (size_t)( end - p ) )
8017 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8018
8019 session->encrypt_then_mac = *p++;
8020#endif
8021
8022 /* Done, should have consumed entire buffer */
8023 if( p != end )
8024 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8025
8026 return( 0 );
8027}
Jerry Yudc7bd172022-02-17 13:44:15 +08008028#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008030#endif /* MBEDTLS_SSL_TLS_C */