blob: 0679a70ad3dfab2c0d699b963f76ab63f28f3a32 [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 Cron6f135e12021-12-08 16:57:54 +0100856#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu60835a82021-08-04 10:13:52 +0800857 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
858 {
859 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
860 {
861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported" ) );
862 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
863 }
864 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
865 return( 0 );
866 }
867#endif
868
869#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
870 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
871 {
872 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
873 return( 0 );
874 }
875#endif
876
Ronald Cron6f135e12021-12-08 16:57:54 +0100877#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu60835a82021-08-04 10:13:52 +0800878 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( ssl->conf ) )
879 {
880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Hybrid TLS 1.2 + TLS 1.3 configurations are not yet supported" ) );
881 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
882 }
883#endif
884
885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
886 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
887}
888
889static int ssl_conf_check(const mbedtls_ssl_context *ssl)
890{
891 int ret;
892 ret = ssl_conf_version_check( ssl );
893 if( ret != 0 )
894 return( ret );
895
896 /* Space for further checks */
897
898 return( 0 );
899}
900
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200901/*
902 * Setup an SSL context
903 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100904
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200905int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +0200906 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +0000907{
Janos Follath865b3eb2019-12-16 11:46:15 +0000908 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +0000909 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
910 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +0000911
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200912 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +0000913
Jerry Yu60835a82021-08-04 10:13:52 +0800914 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
915 return( ret );
916
Paul Bakker62f2dee2012-09-28 07:31:51 +0000917 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +0100918 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +0000919 */
k-stachowiakc9a5f022018-07-24 13:53:31 +0200920
921 /* Set to NULL in case of an error condition */
922 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +0200923
Darryl Greenb33cc762019-11-28 14:29:44 +0000924#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
925 ssl->in_buf_len = in_buf_len;
926#endif
927 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +1000928 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000929 {
Paul Elliottd48d5c62021-01-07 14:47:05 +0000930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +0200931 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +0200932 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +1000933 }
934
Darryl Greenb33cc762019-11-28 14:29:44 +0000935#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
936 ssl->out_buf_len = out_buf_len;
937#endif
938 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +1000939 if( ssl->out_buf == NULL )
940 {
Paul Elliottd48d5c62021-01-07 14:47:05 +0000941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +0200942 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +0200943 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +0000944 }
945
Hanno Becker3e6f8ab2020-02-05 10:40:57 +0000946 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200947
Johan Pascalb62bb512015-12-03 21:56:45 +0100948#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +0200949 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +0100950#endif
951
Paul Bakker48916f92012-09-16 19:57:18 +0000952 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +0200953 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +0000954
955 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +0200956
957error:
958 mbedtls_free( ssl->in_buf );
959 mbedtls_free( ssl->out_buf );
960
961 ssl->conf = NULL;
962
Darryl Greenb33cc762019-11-28 14:29:44 +0000963#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
964 ssl->in_buf_len = 0;
965 ssl->out_buf_len = 0;
966#endif
k-stachowiaka47911c2018-07-04 17:41:58 +0200967 ssl->in_buf = NULL;
968 ssl->out_buf = NULL;
969
970 ssl->in_hdr = NULL;
971 ssl->in_ctr = NULL;
972 ssl->in_len = NULL;
973 ssl->in_iv = NULL;
974 ssl->in_msg = NULL;
975
976 ssl->out_hdr = NULL;
977 ssl->out_ctr = NULL;
978 ssl->out_len = NULL;
979 ssl->out_iv = NULL;
980 ssl->out_msg = NULL;
981
k-stachowiak9f7798e2018-07-31 16:52:32 +0200982 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000983}
984
985/*
Paul Bakker7eb013f2011-10-06 12:37:39 +0000986 * Reset an initialized and used SSL context for re-use while retaining
987 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +0200988 *
989 * If partial is non-zero, keep data in the input buffer and client ID.
990 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +0000991 */
XiaokangQian78b1fa72022-01-19 06:56:30 +0000992void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
993 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +0000994{
Darryl Greenb33cc762019-11-28 14:29:44 +0000995#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
996 size_t in_buf_len = ssl->in_buf_len;
997 size_t out_buf_len = ssl->out_buf_len;
998#else
999 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1000 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1001#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001002
Hanno Beckerb0302c42021-08-03 09:39:42 +01001003#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1004 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001005#endif
1006
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001007 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001008 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001009
Hanno Beckerb0302c42021-08-03 09:39:42 +01001010 mbedtls_ssl_reset_in_out_pointers( ssl );
1011
1012 /* Reset incoming message parsing */
1013 ssl->in_offt = NULL;
1014 ssl->nb_zero = 0;
1015 ssl->in_msgtype = 0;
1016 ssl->in_msglen = 0;
1017 ssl->in_hslen = 0;
1018 ssl->keep_current_message = 0;
1019 ssl->transform_in = NULL;
1020
1021#if defined(MBEDTLS_SSL_PROTO_DTLS)
1022 ssl->next_record_offset = 0;
1023 ssl->in_epoch = 0;
1024#endif
1025
1026 /* Keep current datagram if partial == 1 */
1027 if( partial == 0 )
1028 {
1029 ssl->in_left = 0;
1030 memset( ssl->in_buf, 0, in_buf_len );
1031 }
1032
1033 /* Reset outgoing message writing */
1034 ssl->out_msgtype = 0;
1035 ssl->out_msglen = 0;
1036 ssl->out_left = 0;
1037 memset( ssl->out_buf, 0, out_buf_len );
1038 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1039 ssl->transform_out = NULL;
1040
1041#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1042 mbedtls_ssl_dtls_replay_reset( ssl );
1043#endif
1044
XiaokangQian2b01dc32022-01-21 02:53:13 +00001045#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001046 if( ssl->transform )
1047 {
1048 mbedtls_ssl_transform_free( ssl->transform );
1049 mbedtls_free( ssl->transform );
1050 ssl->transform = NULL;
1051 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001052#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1053
XiaokangQian2b01dc32022-01-21 02:53:13 +00001054#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1055 mbedtls_ssl_transform_free( ssl->transform_application );
1056 mbedtls_free( ssl->transform_application );
1057 ssl->transform_application = NULL;
1058
1059 if( ssl->handshake != NULL )
1060 {
1061 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1062 mbedtls_free( ssl->handshake->transform_earlydata );
1063 ssl->handshake->transform_earlydata = NULL;
1064
1065 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1066 mbedtls_free( ssl->handshake->transform_handshake );
1067 ssl->handshake->transform_handshake = NULL;
1068 }
1069
XiaokangQian2b01dc32022-01-21 02:53:13 +00001070#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001071}
1072
1073int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1074{
1075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1076
1077 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1078
XiaokangQian78b1fa72022-01-19 06:56:30 +00001079 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001080
1081 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082#if defined(MBEDTLS_SSL_RENEGOTIATION)
1083 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001084 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001085
1086 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1088 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001089#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001091
Hanno Beckerb0302c42021-08-03 09:39:42 +01001092 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001093 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001094 if( ssl->session )
1095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 mbedtls_ssl_session_free( ssl->session );
1097 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001098 ssl->session = NULL;
1099 }
1100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001102 ssl->alpn_chosen = NULL;
1103#endif
1104
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001105#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01001106#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001107 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01001108#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001109 {
1110 mbedtls_free( ssl->cli_id );
1111 ssl->cli_id = NULL;
1112 ssl->cli_id_len = 0;
1113 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001114#endif
1115
Paul Bakker48916f92012-09-16 19:57:18 +00001116 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1117 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001118
1119 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001120}
1121
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001122/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001123 * Reset an initialized and used SSL context for re-use while retaining
1124 * all application-set variables, function pointers and data.
1125 */
1126int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1127{
Hanno Becker43aefe22020-02-05 10:44:56 +00001128 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001129}
1130
1131/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001132 * SSL set accessors
1133 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001134void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001135{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001136 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001137}
1138
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001139void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001140{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001141 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001142}
1143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001145void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001146{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001147 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001148}
1149#endif
1150
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001151void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001152{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001153 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001154}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001157
Hanno Becker1841b0a2018-08-24 11:13:57 +01001158void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1159 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001160{
1161 ssl->disable_datagram_packing = !allow_packing;
1162}
1163
1164void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1165 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001166{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001167 conf->hs_timeout_min = min;
1168 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001169}
1170#endif
1171
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001172void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001173{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001174 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001175}
1176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001178void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001179 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001180 void *p_vrfy )
1181{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001182 conf->f_vrfy = f_vrfy;
1183 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001184}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001186
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001187void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001188 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001189 void *p_rng )
1190{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001191 conf->f_rng = f_rng;
1192 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001193}
1194
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001195void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001196 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001197 void *p_dbg )
1198{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001199 conf->f_dbg = f_dbg;
1200 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001201}
1202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001204 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001205 mbedtls_ssl_send_t *f_send,
1206 mbedtls_ssl_recv_t *f_recv,
1207 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001208{
1209 ssl->p_bio = p_bio;
1210 ssl->f_send = f_send;
1211 ssl->f_recv = f_recv;
1212 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001213}
1214
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001215#if defined(MBEDTLS_SSL_PROTO_DTLS)
1216void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1217{
1218 ssl->mtu = mtu;
1219}
1220#endif
1221
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001222void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001223{
1224 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001225}
1226
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001227void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1228 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001229 mbedtls_ssl_set_timer_t *f_set_timer,
1230 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001231{
1232 ssl->p_timer = p_timer;
1233 ssl->f_set_timer = f_set_timer;
1234 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001235
1236 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001237 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001238}
1239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240#if defined(MBEDTLS_SSL_SRV_C)
Glenn Strauss2ed95272022-01-21 18:02:17 -05001241void mbedtls_ssl_conf_cert_cb( mbedtls_ssl_config *conf,
1242 int (*f_cert_cb)(mbedtls_ssl_context *) )
1243{
1244 conf->f_cert_cb = f_cert_cb;
1245}
1246#endif /* MBEDTLS_SSL_SRV_C */
1247
1248#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001249void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001250 void *p_cache,
1251 mbedtls_ssl_cache_get_t *f_get_cache,
1252 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001253{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001254 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001255 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001256 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001257}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260#if defined(MBEDTLS_SSL_CLI_C)
1261int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001262{
Janos Follath865b3eb2019-12-16 11:46:15 +00001263 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001264
1265 if( ssl == NULL ||
1266 session == NULL ||
1267 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001268 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001271 }
1272
Hanno Beckere810bbc2021-05-14 16:01:05 +01001273 if( ssl->handshake->resume == 1 )
1274 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1275
Hanno Becker52055ae2019-02-06 14:30:46 +00001276 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1277 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001278 return( ret );
1279
Paul Bakker0a597072012-09-25 21:55:46 +00001280 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001281
1282 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001283}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001285
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001286void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1287 const int *ciphersuites )
1288{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001289 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001290}
1291
Ronald Cron6f135e12021-12-08 16:57:54 +01001292#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001293void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001294 const int kex_modes )
1295{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001296 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001297}
Ronald Cron6f135e12021-12-08 16:57:54 +01001298#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001301void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001302 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001303{
1304 conf->cert_profile = profile;
1305}
1306
Glenn Strauss36872db2022-01-22 05:06:31 -05001307static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1308{
1309 mbedtls_ssl_key_cert *cur = key_cert, *next;
1310
1311 while( cur != NULL )
1312 {
1313 next = cur->next;
1314 mbedtls_free( cur );
1315 cur = next;
1316 }
1317}
1318
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001319/* Append a new keycert entry to a (possibly empty) list */
1320static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1321 mbedtls_x509_crt *cert,
1322 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001323{
niisato8ee24222018-06-25 19:05:48 +09001324 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001325
Glenn Strauss36872db2022-01-22 05:06:31 -05001326 if( cert == NULL )
1327 {
1328 /* Free list if cert is null */
1329 ssl_key_cert_free( *head );
1330 *head = NULL;
1331 return( 0 );
1332 }
1333
niisato8ee24222018-06-25 19:05:48 +09001334 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1335 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001336 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001337
niisato8ee24222018-06-25 19:05:48 +09001338 new_cert->cert = cert;
1339 new_cert->key = key;
1340 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001341
Glenn Strauss36872db2022-01-22 05:06:31 -05001342 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001343 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001344 {
niisato8ee24222018-06-25 19:05:48 +09001345 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001346 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001347 else
1348 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001349 mbedtls_ssl_key_cert *cur = *head;
1350 while( cur->next != NULL )
1351 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001352 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001353 }
1354
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001355 return( 0 );
1356}
1357
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001358int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001359 mbedtls_x509_crt *own_cert,
1360 mbedtls_pk_context *pk_key )
1361{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001362 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001363}
1364
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001365void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001366 mbedtls_x509_crt *ca_chain,
1367 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001368{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001369 conf->ca_chain = ca_chain;
1370 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001371
1372#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1373 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1374 * cannot be used together. */
1375 conf->f_ca_cb = NULL;
1376 conf->p_ca_cb = NULL;
1377#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001378}
Hanno Becker5adaad92019-03-27 16:54:37 +00001379
1380#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1381void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001382 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001383 void *p_ca_cb )
1384{
1385 conf->f_ca_cb = f_ca_cb;
1386 conf->p_ca_cb = p_ca_cb;
1387
1388 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1389 * cannot be used together. */
1390 conf->ca_chain = NULL;
1391 conf->ca_crl = NULL;
1392}
1393#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001395
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001396#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001397const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1398 size_t *name_len )
1399{
1400 *name_len = ssl->handshake->sni_name_len;
1401 return( ssl->handshake->sni_name );
1402}
1403
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001404int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1405 mbedtls_x509_crt *own_cert,
1406 mbedtls_pk_context *pk_key )
1407{
1408 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1409 own_cert, pk_key ) );
1410}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001411
1412void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1413 mbedtls_x509_crt *ca_chain,
1414 mbedtls_x509_crl *ca_crl )
1415{
1416 ssl->handshake->sni_ca_chain = ca_chain;
1417 ssl->handshake->sni_ca_crl = ca_crl;
1418}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001419
1420void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1421 int authmode )
1422{
1423 ssl->handshake->sni_authmode = authmode;
1424}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001425#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1426
Hanno Becker8927c832019-04-03 12:52:50 +01001427#if defined(MBEDTLS_X509_CRT_PARSE_C)
1428void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1429 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1430 void *p_vrfy )
1431{
1432 ssl->f_vrfy = f_vrfy;
1433 ssl->p_vrfy = p_vrfy;
1434}
1435#endif
1436
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001437#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001438/*
1439 * Set EC J-PAKE password for current handshake
1440 */
1441int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1442 const unsigned char *pw,
1443 size_t pw_len )
1444{
1445 mbedtls_ecjpake_role role;
1446
Janos Follath8eb64132016-06-03 15:40:57 +01001447 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001448 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1449
1450 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1451 role = MBEDTLS_ECJPAKE_SERVER;
1452 else
1453 role = MBEDTLS_ECJPAKE_CLIENT;
1454
1455 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1456 role,
1457 MBEDTLS_MD_SHA256,
1458 MBEDTLS_ECP_DP_SECP256R1,
1459 pw, pw_len ) );
1460}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001461#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001462
Gilles Peskineeccd8882020-03-10 12:19:08 +01001463#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001464
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001465static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1466{
1467#if defined(MBEDTLS_USE_PSA_CRYPTO)
1468 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1469 return( 1 );
1470#endif /* MBEDTLS_USE_PSA_CRYPTO */
1471
1472 if( conf->psk != NULL )
1473 return( 1 );
1474
1475 return( 0 );
1476}
1477
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001478static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1479{
1480 /* Remove reference to existing PSK, if any. */
1481#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001482 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001483 {
1484 /* The maintenance of the PSK key slot is the
1485 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001486 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001487 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00001488 /* This and the following branch should never
1489 * be taken simultaenously as we maintain the
1490 * invariant that raw and opaque PSKs are never
1491 * configured simultaneously. As a safeguard,
1492 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001493#endif /* MBEDTLS_USE_PSA_CRYPTO */
1494 if( conf->psk != NULL )
1495 {
1496 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1497
1498 mbedtls_free( conf->psk );
1499 conf->psk = NULL;
1500 conf->psk_len = 0;
1501 }
1502
1503 /* Remove reference to PSK identity, if any. */
1504 if( conf->psk_identity != NULL )
1505 {
1506 mbedtls_free( conf->psk_identity );
1507 conf->psk_identity = NULL;
1508 conf->psk_identity_len = 0;
1509 }
1510}
1511
Hanno Becker7390c712018-11-15 13:33:04 +00001512/* This function assumes that PSK identity in the SSL config is unset.
1513 * It checks that the provided identity is well-formed and attempts
1514 * to make a copy of it in the SSL config.
1515 * On failure, the PSK identity in the config remains unset. */
1516static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1517 unsigned char const *psk_identity,
1518 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001519{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001520 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001521 if( psk_identity == NULL ||
1522 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001523 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001524 {
1525 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1526 }
1527
Hanno Becker7390c712018-11-15 13:33:04 +00001528 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1529 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001530 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001531
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001532 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001533 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001534
1535 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001536}
1537
Hanno Becker7390c712018-11-15 13:33:04 +00001538int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1539 const unsigned char *psk, size_t psk_len,
1540 const unsigned char *psk_identity, size_t psk_identity_len )
1541{
Janos Follath865b3eb2019-12-16 11:46:15 +00001542 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001543
1544 /* We currently only support one PSK, raw or opaque. */
1545 if( ssl_conf_psk_is_configured( conf ) )
1546 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001547
1548 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001549 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001550 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001551 if( psk_len == 0 )
1552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1553 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1554 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1555
Hanno Becker7390c712018-11-15 13:33:04 +00001556 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1557 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1558 conf->psk_len = psk_len;
1559 memcpy( conf->psk, psk, conf->psk_len );
1560
1561 /* Check and set PSK Identity */
1562 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1563 if( ret != 0 )
1564 ssl_conf_remove_psk( conf );
1565
1566 return( ret );
1567}
1568
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001569static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1570{
1571#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001572 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001573 {
Ronald Croncf56a0a2020-08-04 09:51:30 +02001574 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001575 }
1576 else
1577#endif /* MBEDTLS_USE_PSA_CRYPTO */
1578 if( ssl->handshake->psk != NULL )
1579 {
1580 mbedtls_platform_zeroize( ssl->handshake->psk,
1581 ssl->handshake->psk_len );
1582 mbedtls_free( ssl->handshake->psk );
1583 ssl->handshake->psk_len = 0;
1584 }
1585}
1586
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001587int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1588 const unsigned char *psk, size_t psk_len )
1589{
1590 if( psk == NULL || ssl->handshake == NULL )
1591 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1592
1593 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1594 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1595
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001596 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001597
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001598 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001599 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001600
1601 ssl->handshake->psk_len = psk_len;
1602 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1603
1604 return( 0 );
1605}
1606
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001607#if defined(MBEDTLS_USE_PSA_CRYPTO)
1608int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001609 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001610 const unsigned char *psk_identity,
1611 size_t psk_identity_len )
1612{
Janos Follath865b3eb2019-12-16 11:46:15 +00001613 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001614
1615 /* We currently only support one PSK, raw or opaque. */
1616 if( ssl_conf_psk_is_configured( conf ) )
1617 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001618
Hanno Becker7390c712018-11-15 13:33:04 +00001619 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001620 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001621 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001622 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001623
1624 /* Check and set PSK Identity */
1625 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1626 psk_identity_len );
1627 if( ret != 0 )
1628 ssl_conf_remove_psk( conf );
1629
1630 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001631}
1632
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001633int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1634 mbedtls_svc_key_id_t psk )
1635{
1636 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1637 ( ssl->handshake == NULL ) )
1638 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1639
1640 ssl_remove_psk( ssl );
1641 ssl->handshake->psk_opaque = psk;
1642 return( 0 );
1643}
1644#endif /* MBEDTLS_USE_PSA_CRYPTO */
1645
1646void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1647 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1648 size_t),
1649 void *p_psk )
1650{
1651 conf->f_psk = f_psk;
1652 conf->p_psk = p_psk;
1653}
1654#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1655
1656#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01001657psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01001658 size_t taglen,
1659 psa_algorithm_t *alg,
1660 psa_key_type_t *key_type,
1661 size_t *key_size )
1662{
1663 switch ( mbedtls_cipher_type )
1664 {
1665 case MBEDTLS_CIPHER_AES_128_CBC:
1666 *alg = PSA_ALG_CBC_NO_PADDING;
1667 *key_type = PSA_KEY_TYPE_AES;
1668 *key_size = 128;
1669 break;
1670 case MBEDTLS_CIPHER_AES_128_CCM:
1671 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1672 *key_type = PSA_KEY_TYPE_AES;
1673 *key_size = 128;
1674 break;
1675 case MBEDTLS_CIPHER_AES_128_GCM:
1676 *alg = PSA_ALG_GCM;
1677 *key_type = PSA_KEY_TYPE_AES;
1678 *key_size = 128;
1679 break;
1680 case MBEDTLS_CIPHER_AES_192_CCM:
1681 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1682 *key_type = PSA_KEY_TYPE_AES;
1683 *key_size = 192;
1684 break;
1685 case MBEDTLS_CIPHER_AES_192_GCM:
1686 *alg = PSA_ALG_GCM;
1687 *key_type = PSA_KEY_TYPE_AES;
1688 *key_size = 192;
1689 break;
1690 case MBEDTLS_CIPHER_AES_256_CBC:
1691 *alg = PSA_ALG_CBC_NO_PADDING;
1692 *key_type = PSA_KEY_TYPE_AES;
1693 *key_size = 256;
1694 break;
1695 case MBEDTLS_CIPHER_AES_256_CCM:
1696 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1697 *key_type = PSA_KEY_TYPE_AES;
1698 *key_size = 256;
1699 break;
1700 case MBEDTLS_CIPHER_AES_256_GCM:
1701 *alg = PSA_ALG_GCM;
1702 *key_type = PSA_KEY_TYPE_AES;
1703 *key_size = 256;
1704 break;
1705 case MBEDTLS_CIPHER_ARIA_128_CBC:
1706 *alg = PSA_ALG_CBC_NO_PADDING;
1707 *key_type = PSA_KEY_TYPE_ARIA;
1708 *key_size = 128;
1709 break;
1710 case MBEDTLS_CIPHER_ARIA_128_CCM:
1711 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1712 *key_type = PSA_KEY_TYPE_ARIA;
1713 *key_size = 128;
1714 break;
1715 case MBEDTLS_CIPHER_ARIA_128_GCM:
1716 *alg = PSA_ALG_GCM;
1717 *key_type = PSA_KEY_TYPE_ARIA;
1718 *key_size = 128;
1719 break;
1720 case MBEDTLS_CIPHER_ARIA_192_CCM:
1721 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1722 *key_type = PSA_KEY_TYPE_ARIA;
1723 *key_size = 192;
1724 break;
1725 case MBEDTLS_CIPHER_ARIA_192_GCM:
1726 *alg = PSA_ALG_GCM;
1727 *key_type = PSA_KEY_TYPE_ARIA;
1728 *key_size = 192;
1729 break;
1730 case MBEDTLS_CIPHER_ARIA_256_CBC:
1731 *alg = PSA_ALG_CBC_NO_PADDING;
1732 *key_type = PSA_KEY_TYPE_ARIA;
1733 *key_size = 256;
1734 break;
1735 case MBEDTLS_CIPHER_ARIA_256_CCM:
1736 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1737 *key_type = PSA_KEY_TYPE_ARIA;
1738 *key_size = 256;
1739 break;
1740 case MBEDTLS_CIPHER_ARIA_256_GCM:
1741 *alg = PSA_ALG_GCM;
1742 *key_type = PSA_KEY_TYPE_ARIA;
1743 *key_size = 256;
1744 break;
1745 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
1746 *alg = PSA_ALG_CBC_NO_PADDING;
1747 *key_type = PSA_KEY_TYPE_CAMELLIA;
1748 *key_size = 128;
1749 break;
1750 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
1751 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1752 *key_type = PSA_KEY_TYPE_CAMELLIA;
1753 *key_size = 128;
1754 break;
1755 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
1756 *alg = PSA_ALG_GCM;
1757 *key_type = PSA_KEY_TYPE_CAMELLIA;
1758 *key_size = 128;
1759 break;
1760 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
1761 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1762 *key_type = PSA_KEY_TYPE_CAMELLIA;
1763 *key_size = 192;
1764 break;
1765 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
1766 *alg = PSA_ALG_GCM;
1767 *key_type = PSA_KEY_TYPE_CAMELLIA;
1768 *key_size = 192;
1769 break;
1770 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
1771 *alg = PSA_ALG_CBC_NO_PADDING;
1772 *key_type = PSA_KEY_TYPE_CAMELLIA;
1773 *key_size = 256;
1774 break;
1775 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
1776 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1777 *key_type = PSA_KEY_TYPE_CAMELLIA;
1778 *key_size = 256;
1779 break;
1780 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
1781 *alg = PSA_ALG_GCM;
1782 *key_type = PSA_KEY_TYPE_CAMELLIA;
1783 *key_size = 256;
1784 break;
1785 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
1786 *alg = PSA_ALG_CHACHA20_POLY1305;
1787 *key_type = PSA_KEY_TYPE_CHACHA20;
1788 *key_size = 256;
1789 break;
1790 case MBEDTLS_CIPHER_NULL:
1791 *alg = MBEDTLS_SSL_NULL_CIPHER;
1792 *key_type = 0;
1793 *key_size = 0;
1794 break;
1795 default:
1796 return PSA_ERROR_NOT_SUPPORTED;
1797 }
1798
1799 return PSA_SUCCESS;
1800}
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001801#endif /* MBEDTLS_USE_PSA_CRYPTO */
1802
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02001803#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01001804int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
1805 const unsigned char *dhm_P, size_t P_len,
1806 const unsigned char *dhm_G, size_t G_len )
1807{
Janos Follath865b3eb2019-12-16 11:46:15 +00001808 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01001809
Glenn Strausscee11292021-12-20 01:43:17 -05001810 mbedtls_mpi_free( &conf->dhm_P );
1811 mbedtls_mpi_free( &conf->dhm_G );
1812
Hanno Beckera90658f2017-10-04 15:29:08 +01001813 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
1814 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
1815 {
1816 mbedtls_mpi_free( &conf->dhm_P );
1817 mbedtls_mpi_free( &conf->dhm_G );
1818 return( ret );
1819 }
1820
1821 return( 0 );
1822}
Paul Bakker5121ce52009-01-03 21:22:43 +00001823
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001824int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00001825{
Janos Follath865b3eb2019-12-16 11:46:15 +00001826 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00001827
Glenn Strausscee11292021-12-20 01:43:17 -05001828 mbedtls_mpi_free( &conf->dhm_P );
1829 mbedtls_mpi_free( &conf->dhm_G );
1830
Gilles Peskinee5702482021-06-11 21:59:08 +02001831 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
1832 &conf->dhm_P ) ) != 0 ||
1833 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
1834 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01001835 {
1836 mbedtls_mpi_free( &conf->dhm_P );
1837 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00001838 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01001839 }
Paul Bakker1b57b062011-01-06 15:48:19 +00001840
1841 return( 0 );
1842}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02001843#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00001844
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02001845#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
1846/*
1847 * Set the minimum length for Diffie-Hellman parameters
1848 */
1849void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
1850 unsigned int bitlen )
1851{
1852 conf->dhm_min_bitlen = bitlen;
1853}
1854#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
1855
Gilles Peskineeccd8882020-03-10 12:19:08 +01001856#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08001857#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02001858/*
1859 * Set allowed/preferred hashes for handshake signatures
1860 */
1861void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
1862 const int *hashes )
1863{
1864 conf->sig_hashes = hashes;
1865}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08001866#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01001867
Jerry Yuf017ee42022-01-12 15:49:48 +08001868/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01001869void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
1870 const uint16_t* sig_algs )
1871{
Jerry Yuf017ee42022-01-12 15:49:48 +08001872#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1873 conf->sig_hashes = NULL;
1874#endif /* !MBEDTLS_DEPRECATED_REMOVED */
1875 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01001876}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001877#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02001878
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02001879#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01001880#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001881/*
1882 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01001883 *
1884 * mbedtls_ssl_setup() takes the provided list
1885 * and translates it to a list of IANA TLS group identifiers,
1886 * stored in ssl->handshake->group_list.
1887 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001888 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001889void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001890 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001891{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001892 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01001893 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001894}
Brett Warrene0edc842021-08-17 09:53:13 +01001895#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01001896#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001897
Brett Warrene0edc842021-08-17 09:53:13 +01001898/*
1899 * Set the allowed groups
1900 */
1901void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
1902 const uint16_t *group_list )
1903{
1904#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
1905 conf->curve_list = NULL;
1906#endif
1907 conf->group_list = group_list;
1908}
1909
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001910#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00001912{
Hanno Becker947194e2017-04-07 13:25:49 +01001913 /* Initialize to suppress unnecessary compiler warning */
1914 size_t hostname_len = 0;
1915
1916 /* Check if new hostname is valid before
1917 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01001918 if( hostname != NULL )
1919 {
1920 hostname_len = strlen( hostname );
1921
1922 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
1923 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1924 }
1925
1926 /* Now it's clear that we will overwrite the old hostname,
1927 * so we can free it safely */
1928
1929 if( ssl->hostname != NULL )
1930 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001931 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01001932 mbedtls_free( ssl->hostname );
1933 }
1934
1935 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01001936
Paul Bakker5121ce52009-01-03 21:22:43 +00001937 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01001938 {
1939 ssl->hostname = NULL;
1940 }
1941 else
1942 {
1943 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01001944 if( ssl->hostname == NULL )
1945 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02001946
Hanno Becker947194e2017-04-07 13:25:49 +01001947 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02001948
Hanno Becker947194e2017-04-07 13:25:49 +01001949 ssl->hostname[hostname_len] = '\0';
1950 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001951
1952 return( 0 );
1953}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01001954#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001955
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001956#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001957void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00001959 const unsigned char *, size_t),
1960 void *p_sni )
1961{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001962 conf->f_sni = f_sni;
1963 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00001964}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001968int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001969{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001970 size_t cur_len, tot_len;
1971 const char **p;
1972
1973 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08001974 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
1975 * MUST NOT be truncated."
1976 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001977 */
1978 tot_len = 0;
1979 for( p = protos; *p != NULL; p++ )
1980 {
1981 cur_len = strlen( *p );
1982 tot_len += cur_len;
1983
Ronald Cron8216dd32020-04-23 16:41:44 +02001984 if( ( cur_len == 0 ) ||
1985 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
1986 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001988 }
1989
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001990 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001991
1992 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001993}
1994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001996{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001997 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001998}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002000
Johan Pascalb62bb512015-12-03 21:56:45 +01002001#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002002void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2003 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002004{
2005 conf->dtls_srtp_mki_support = support_mki_value;
2006}
2007
Ron Eldoref72faf2018-07-12 11:54:20 +03002008int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2009 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002010 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002011{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002012 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002013 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002014 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002015 }
Ron Eldor591f1622018-01-22 12:30:04 +02002016
2017 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002018 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002019 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002020 }
Ron Eldor591f1622018-01-22 12:30:04 +02002021
2022 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2023 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002024 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002025}
2026
Ron Eldoref72faf2018-07-12 11:54:20 +03002027int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002028 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002029{
Johan Pascal253d0262020-09-22 13:04:45 +02002030 const mbedtls_ssl_srtp_profile *p;
2031 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002032
Johan Pascal253d0262020-09-22 13:04:45 +02002033 /* check the profiles list: all entry must be valid,
2034 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002035 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2036 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2037 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002038 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002039 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002040 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002041 list_size++;
2042 }
2043 else
2044 {
2045 /* unsupported value, stop parsing and set the size to an error value */
2046 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002047 }
2048 }
2049
Johan Pascal5ef72d22020-10-28 17:05:47 +01002050 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002051 {
Johan Pascal253d0262020-09-22 13:04:45 +02002052 conf->dtls_srtp_profile_list = NULL;
2053 conf->dtls_srtp_profile_list_len = 0;
2054 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2055 }
2056
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002057 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002058 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002059
2060 return( 0 );
2061}
2062
Johan Pascal5ef72d22020-10-28 17:05:47 +01002063void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2064 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002065{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002066 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2067 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002068 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002069 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002070 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002071 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002072 else
2073 {
2074 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002075 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2076 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002077 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002078}
Johan Pascalb62bb512015-12-03 21:56:45 +01002079#endif /* MBEDTLS_SSL_DTLS_SRTP */
2080
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002081void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002082{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002083 conf->max_major_ver = major;
2084 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002085}
2086
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002087void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002088{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002089 conf->min_major_ver = major;
2090 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002091}
2092
Janos Follath088ce432017-04-10 12:42:31 +01002093#if defined(MBEDTLS_SSL_SRV_C)
2094void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2095 char cert_req_ca_list )
2096{
2097 conf->cert_req_ca_list = cert_req_ca_list;
2098}
2099#endif
2100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002102void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002103{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002104 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002105}
2106#endif
2107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002109void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002110{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002111 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002112}
2113#endif
2114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002116int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002117{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002119 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002122 }
2123
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002124 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002125
2126 return( 0 );
2127}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002129
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002130void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002131{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002132 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002133}
2134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002136void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002137{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002138 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002139}
2140
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002141void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002142{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002143 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002144}
2145
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002146void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002147 const unsigned char period[8] )
2148{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002149 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002150}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002154#if defined(MBEDTLS_SSL_CLI_C)
2155void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002156{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002157 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002158}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002159#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002160
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002161#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002162void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2163 mbedtls_ssl_ticket_write_t *f_ticket_write,
2164 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2165 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002166{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002167 conf->f_ticket_write = f_ticket_write;
2168 conf->f_ticket_parse = f_ticket_parse;
2169 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002170}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002171#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002173
Hanno Becker7e6c1782021-06-08 09:24:55 +01002174void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2175 mbedtls_ssl_export_keys_t *f_export_keys,
2176 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002177{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002178 ssl->f_export_keys = f_export_keys;
2179 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002180}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002181
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002182#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002183void mbedtls_ssl_conf_async_private_cb(
2184 mbedtls_ssl_config *conf,
2185 mbedtls_ssl_async_sign_t *f_async_sign,
2186 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2187 mbedtls_ssl_async_resume_t *f_async_resume,
2188 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002189 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002190{
2191 conf->f_async_sign_start = f_async_sign;
2192 conf->f_async_decrypt_start = f_async_decrypt;
2193 conf->f_async_resume = f_async_resume;
2194 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002195 conf->p_async_config_data = async_config_data;
2196}
2197
Gilles Peskine8f97af72018-04-26 11:46:10 +02002198void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2199{
2200 return( conf->p_async_config_data );
2201}
2202
Gilles Peskine1febfef2018-04-30 11:54:39 +02002203void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002204{
2205 if( ssl->handshake == NULL )
2206 return( NULL );
2207 else
2208 return( ssl->handshake->user_async_ctx );
2209}
2210
Gilles Peskine1febfef2018-04-30 11:54:39 +02002211void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002212 void *ctx )
2213{
2214 if( ssl->handshake != NULL )
2215 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002216}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002217#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002218
Paul Bakker5121ce52009-01-03 21:22:43 +00002219/*
2220 * SSL get accessors
2221 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002222uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002223{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002224 if( ssl->session != NULL )
2225 return( ssl->session->verify_result );
2226
2227 if( ssl->session_negotiate != NULL )
2228 return( ssl->session_negotiate->verify_result );
2229
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002230 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002231}
2232
Glenn Strauss8f526902022-01-13 00:04:49 -05002233int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2234{
2235 if( ssl == NULL || ssl->session == NULL )
2236 return( 0 );
2237
2238 return( ssl->session->ciphersuite );
2239}
2240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002242{
Paul Bakker926c8e42013-03-06 10:23:34 +01002243 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002244 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002247}
2248
Gilles Peskinee1a0c252022-01-13 01:08:03 +01002249mbedtls_ssl_protocol_version mbedtls_ssl_get_version_number(
2250 const mbedtls_ssl_context *ssl )
2251{
2252 /* For major_ver, only 3 is supported, so skip checking it. */
2253 switch( ssl->minor_ver )
2254 {
2255 case MBEDTLS_SSL_MINOR_VERSION_3:
2256 return( MBEDTLS_SSL_VERSION_1_2 );
2257 case MBEDTLS_SSL_MINOR_VERSION_4:
2258 return( MBEDTLS_SSL_VERSION_1_3 );
2259 default:
2260 return( MBEDTLS_SSL_VERSION_UNKNOWN );
2261 }
2262}
2263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002265{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002267 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002268 {
2269 switch( ssl->minor_ver )
2270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002272 return( "DTLSv1.2" );
2273
2274 default:
2275 return( "unknown (DTLS)" );
2276 }
2277 }
2278#endif
2279
Paul Bakker43ca69c2011-01-15 17:35:19 +00002280 switch( ssl->minor_ver )
2281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002283 return( "TLSv1.2" );
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002284 case MBEDTLS_SSL_MINOR_VERSION_4:
2285 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002286 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002287 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002288 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002289}
2290
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002291#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002292size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2293{
David Horstmann95d516f2021-05-04 18:36:56 +01002294 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002295 size_t read_mfl;
2296
2297 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2298 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2299 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2300 {
2301 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2302 }
2303
2304 /* Check if a smaller max length was negotiated */
2305 if( ssl->session_out != NULL )
2306 {
2307 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2308 if( read_mfl < max_len )
2309 {
2310 max_len = read_mfl;
2311 }
2312 }
2313
2314 // During a handshake, use the value being negotiated
2315 if( ssl->session_negotiate != NULL )
2316 {
2317 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2318 if( read_mfl < max_len )
2319 {
2320 max_len = read_mfl;
2321 }
2322 }
2323
2324 return( max_len );
2325}
2326
2327size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002328{
2329 size_t max_len;
2330
2331 /*
2332 * Assume mfl_code is correct since it was checked when set
2333 */
Angus Grattond8213d02016-05-25 20:56:48 +10002334 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002335
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002336 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002337 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002338 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002339 {
Angus Grattond8213d02016-05-25 20:56:48 +10002340 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002341 }
2342
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002343 /* During a handshake, use the value being negotiated */
2344 if( ssl->session_negotiate != NULL &&
2345 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2346 {
2347 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2348 }
2349
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002350 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002351}
2352#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2353
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002354#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002355size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002356{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002357 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2358 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2359 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2360 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2361 return ( 0 );
2362
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002363 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2364 return( ssl->mtu );
2365
2366 if( ssl->mtu == 0 )
2367 return( ssl->handshake->mtu );
2368
2369 return( ssl->mtu < ssl->handshake->mtu ?
2370 ssl->mtu : ssl->handshake->mtu );
2371}
2372#endif /* MBEDTLS_SSL_PROTO_DTLS */
2373
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002374int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2375{
2376 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2377
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002378#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2379 !defined(MBEDTLS_SSL_PROTO_DTLS)
2380 (void) ssl;
2381#endif
2382
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002383#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002384 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002385
2386 if( max_len > mfl )
2387 max_len = mfl;
2388#endif
2389
2390#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002391 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002392 {
Hanno Becker89490712020-02-05 10:50:12 +00002393 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002394 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2395 const size_t overhead = (size_t) ret;
2396
2397 if( ret < 0 )
2398 return( ret );
2399
2400 if( mtu <= overhead )
2401 {
2402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2403 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2404 }
2405
2406 if( max_len > mtu - overhead )
2407 max_len = mtu - overhead;
2408 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002409#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002410
Hanno Becker0defedb2018-08-10 12:35:02 +01002411#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2412 !defined(MBEDTLS_SSL_PROTO_DTLS)
2413 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002414#endif
2415
2416 return( (int) max_len );
2417}
2418
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002419int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2420{
2421 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2422
2423#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2424 (void) ssl;
2425#endif
2426
2427#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2428 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2429
2430 if( max_len > mfl )
2431 max_len = mfl;
2432#endif
2433
2434 return( (int) max_len );
2435}
2436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437#if defined(MBEDTLS_X509_CRT_PARSE_C)
2438const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002439{
2440 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002441 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002442
Hanno Beckere6824572019-02-07 13:18:46 +00002443#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002444 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002445#else
2446 return( NULL );
2447#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002448}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002452int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2453 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002454{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002455 int ret;
2456
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002457 if( ssl == NULL ||
2458 dst == NULL ||
2459 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002460 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002463 }
2464
Hanno Beckere810bbc2021-05-14 16:01:05 +01002465 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2466 * idempotent: Each session can only be exported once.
2467 *
2468 * (This is in preparation for TLS 1.3 support where we will
2469 * need the ability to export multiple sessions (aka tickets),
2470 * which will be achieved by calling mbedtls_ssl_get_session()
2471 * multiple times until it fails.)
2472 *
2473 * Check whether we have already exported the current session,
2474 * and fail if so.
2475 */
2476 if( ssl->session->exported == 1 )
2477 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2478
2479 ret = mbedtls_ssl_session_copy( dst, ssl->session );
2480 if( ret != 0 )
2481 return( ret );
2482
2483 /* Remember that we've exported the session. */
2484 ssl->session->exported = 1;
2485 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002486}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002488
Paul Bakker5121ce52009-01-03 21:22:43 +00002489/*
Hanno Beckera835da52019-05-16 12:39:07 +01002490 * Define ticket header determining Mbed TLS version
2491 * and structure of the ticket.
2492 */
2493
Hanno Becker94ef3b32019-05-16 12:50:45 +01002494/*
Hanno Becker50b59662019-05-28 14:30:45 +01002495 * Define bitflag determining compile-time settings influencing
2496 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01002497 */
2498
Hanno Becker50b59662019-05-28 14:30:45 +01002499#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01002500#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01002501#else
Hanno Becker3e088662019-05-29 11:10:18 +01002502#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002503#endif /* MBEDTLS_HAVE_TIME */
2504
2505#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01002506#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002507#else
Hanno Becker3e088662019-05-29 11:10:18 +01002508#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002509#endif /* MBEDTLS_X509_CRT_PARSE_C */
2510
2511#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01002512#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002513#else
Hanno Becker3e088662019-05-29 11:10:18 +01002514#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002515#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
2516
2517#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01002518#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002519#else
Hanno Becker3e088662019-05-29 11:10:18 +01002520#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002521#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2522
Hanno Becker94ef3b32019-05-16 12:50:45 +01002523#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01002524#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002525#else
Hanno Becker3e088662019-05-29 11:10:18 +01002526#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002527#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2528
Hanno Becker94ef3b32019-05-16 12:50:45 +01002529#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2530#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
2531#else
2532#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
2533#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2534
Hanno Becker3e088662019-05-29 11:10:18 +01002535#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
2536#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
2537#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
2538#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01002539#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
2540#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01002541
Hanno Becker50b59662019-05-28 14:30:45 +01002542#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01002543 ( (uint16_t) ( \
2544 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
2545 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
2546 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
2547 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01002548 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01002549 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01002550
Hanno Beckerf8787072019-05-16 12:41:07 +01002551static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01002552 MBEDTLS_VERSION_MAJOR,
2553 MBEDTLS_VERSION_MINOR,
2554 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01002555 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
2556 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01002557};
Hanno Beckera835da52019-05-16 12:39:07 +01002558
2559/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002560 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002561 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002562 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002563 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002564 *
Hanno Beckerdce50972021-08-01 05:39:23 +01002565 * opaque mbedtls_version[3]; // library version: major, minor, patch
2566 * opaque session_format[2]; // library-version specific 16-bit field
2567 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002568 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002569 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002570 * Note: When updating the format, remember to keep
2571 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002572 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002573 * // In this version, `session_format` determines
2574 * // the setting of those compile-time
2575 * // configuration options which influence
2576 * // the structure of mbedtls_ssl_session.
2577 *
Hanno Beckerfa0d61e2021-08-02 08:56:14 +01002578 * uint8_t minor_ver; // Protocol-version. Possible values:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002579 * // - TLS 1.2 (MBEDTLS_SSL_MINOR_VERSION_3)
2580 *
2581 * select (serialized_session.minor_ver) {
2582 *
2583 * case MBEDTLS_SSL_MINOR_VERSION_3: // TLS 1.2
2584 * serialized_session_tls12 data;
2585 *
2586 * };
2587 *
2588 * } serialized_session;
2589 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002590 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002591
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002592static int ssl_session_save( const mbedtls_ssl_session *session,
2593 unsigned char omit_header,
2594 unsigned char *buf,
2595 size_t buf_len,
2596 size_t *olen )
2597{
2598 unsigned char *p = buf;
2599 size_t used = 0;
2600
2601 if( !omit_header )
2602 {
2603 /*
2604 * Add Mbed TLS version identifier
2605 */
2606
2607 used += sizeof( ssl_serialized_session_header );
2608
2609 if( used <= buf_len )
2610 {
2611 memcpy( p, ssl_serialized_session_header,
2612 sizeof( ssl_serialized_session_header ) );
2613 p += sizeof( ssl_serialized_session_header );
2614 }
2615 }
2616
2617 /*
2618 * TLS version identifier
2619 */
2620 used += 1;
2621 if( used <= buf_len )
2622 {
2623 *p++ = session->minor_ver;
2624 }
2625
2626 /* Forward to version-specific serialization routine. */
2627 switch( session->minor_ver )
2628 {
2629#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2630 case MBEDTLS_SSL_MINOR_VERSION_3:
2631 {
2632 size_t remaining_len = used <= buf_len ? buf_len - used : 0;
2633 used += ssl_session_save_tls12( session, p, remaining_len );
2634 break;
2635 }
2636#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2637
2638 default:
2639 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2640 }
2641
2642 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02002643 if( used > buf_len )
2644 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002645
2646 return( 0 );
2647}
2648
2649/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02002650 * Public wrapper for ssl_session_save()
2651 */
2652int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
2653 unsigned char *buf,
2654 size_t buf_len,
2655 size_t *olen )
2656{
2657 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
2658}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002659
Jerry Yuf1b23ca2022-02-18 11:48:47 +08002660/*
2661 * Deserialize session, see mbedtls_ssl_session_save() for format.
2662 *
2663 * This internal version is wrapped by a public function that cleans up in
2664 * case of error, and has an extra option omit_header.
2665 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002666static int ssl_session_load( mbedtls_ssl_session *session,
2667 unsigned char omit_header,
2668 const unsigned char *buf,
2669 size_t len )
2670{
2671 const unsigned char *p = buf;
2672 const unsigned char * const end = buf + len;
2673
2674 if( !omit_header )
2675 {
2676 /*
2677 * Check Mbed TLS version identifier
2678 */
2679
2680 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
2681 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2682
2683 if( memcmp( p, ssl_serialized_session_header,
2684 sizeof( ssl_serialized_session_header ) ) != 0 )
2685 {
2686 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
2687 }
2688 p += sizeof( ssl_serialized_session_header );
2689 }
2690
2691 /*
2692 * TLS version identifier
2693 */
2694 if( 1 > (size_t)( end - p ) )
2695 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2696 session->minor_ver = *p++;
2697
2698 /* Dispatch according to TLS version. */
2699 switch( session->minor_ver )
2700 {
2701#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2702 case MBEDTLS_SSL_MINOR_VERSION_3: /* TLS 1.2 */
2703 {
2704 size_t remaining_len = ( end - p );
2705 return( ssl_session_load_tls12( session, p, remaining_len ) );
2706 }
2707#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2708
2709 default:
2710 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2711 }
2712}
2713
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002714/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02002715 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02002716 */
2717int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
2718 const unsigned char *buf,
2719 size_t len )
2720{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02002721 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02002722
2723 if( ret != 0 )
2724 mbedtls_ssl_session_free( session );
2725
2726 return( ret );
2727}
2728
2729/*
Paul Bakker1961b702013-01-25 14:49:24 +01002730 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00002731 */
Hanno Becker41934dd2021-08-07 19:13:43 +01002732static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
2733{
2734 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2735
Ronald Cron66dbf912022-02-02 15:33:46 +01002736 /*
2737 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01002738 * that were written into the output buffer by the previous handshake step,
2739 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01002740 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
2741 * We proceed to the next handshake step only when all data from the
2742 * previous one have been sent to the peer, thus we make sure that this is
2743 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
2744 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
2745 * we have to wait before to go ahead.
2746 * In the case of TLS 1.3, handshake step handlers do not send data to the
2747 * peer. Data are only sent here and through
2748 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
2749 * alert occured.
2750 */
Hanno Becker41934dd2021-08-07 19:13:43 +01002751 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2752 return( ret );
2753
2754#if defined(MBEDTLS_SSL_PROTO_DTLS)
2755 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2756 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
2757 {
2758 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
2759 return( ret );
2760 }
2761#endif /* MBEDTLS_SSL_PROTO_DTLS */
2762
2763 return( ret );
2764}
2765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002767{
Hanno Becker41934dd2021-08-07 19:13:43 +01002768 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002769
Hanno Becker41934dd2021-08-07 19:13:43 +01002770 if( ssl == NULL ||
2771 ssl->conf == NULL ||
2772 ssl->handshake == NULL ||
2773 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2774 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002775 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01002776 }
2777
2778 ret = ssl_prepare_handshake_step( ssl );
2779 if( ret != 0 )
2780 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002781
Jerry Yue7047812021-09-13 19:26:39 +08002782 ret = mbedtls_ssl_handle_pending_alert( ssl );
2783 if( ret != 0 )
2784 goto cleanup;
2785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002787 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08002788 {
Ronald Cron6f135e12021-12-08 16:57:54 +01002789#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08002790 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yuf4436812021-08-26 22:59:56 +08002791 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01002792#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08002793
2794#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2795 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
2796 ret = mbedtls_ssl_handshake_client_step( ssl );
2797#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2798 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002799#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002801 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08002802 {
Ronald Cron6f135e12021-12-08 16:57:54 +01002803#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08002804 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08002805 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01002806#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08002807
2808#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2809 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
2810 ret = mbedtls_ssl_handshake_server_step( ssl );
2811#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2812 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002813#endif
2814
Jerry Yue7047812021-09-13 19:26:39 +08002815 if( ret != 0 )
2816 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08002817 /* handshake_step return error. And it is same
2818 * with alert_reason.
2819 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08002820 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08002821 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08002822 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08002823 goto cleanup;
2824 }
2825 }
2826
2827cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01002828 return( ret );
2829}
2830
2831/*
2832 * Perform the SSL handshake
2833 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002834int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01002835{
2836 int ret = 0;
2837
Hanno Beckera817ea42020-10-20 15:20:23 +01002838 /* Sanity checks */
2839
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002840 if( ssl == NULL || ssl->conf == NULL )
2841 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2842
Hanno Beckera817ea42020-10-20 15:20:23 +01002843#if defined(MBEDTLS_SSL_PROTO_DTLS)
2844 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2845 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
2846 {
2847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2848 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2849 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2850 }
2851#endif /* MBEDTLS_SSL_PROTO_DTLS */
2852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01002854
Hanno Beckera817ea42020-10-20 15:20:23 +01002855 /* Main handshake loop */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01002857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002859
2860 if( ret != 0 )
2861 break;
2862 }
2863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002865
2866 return( ret );
2867}
2868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002869#if defined(MBEDTLS_SSL_RENEGOTIATION)
2870#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002871/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002872 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00002873 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002875{
Janos Follath865b3eb2019-12-16 11:46:15 +00002876 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002879
2880 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002881 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2882 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002883
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002884 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002885 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002886 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002887 return( ret );
2888 }
2889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002891
2892 return( 0 );
2893}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002895
2896/*
2897 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 * - any side: calling mbedtls_ssl_renegotiate(),
2899 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
2900 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02002901 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002902 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002904 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00002905int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00002906{
Janos Follath865b3eb2019-12-16 11:46:15 +00002907 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00002908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002910
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002911 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
2912 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00002913
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02002914 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
2915 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002917 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02002919 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002920 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002921 ssl->handshake->out_msg_seq = 1;
2922 else
2923 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02002924 }
2925#endif
2926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002927 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
2928 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00002929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00002931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00002933 return( ret );
2934 }
2935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002937
2938 return( 0 );
2939}
2940
2941/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002942 * Renegotiate current connection on client,
2943 * or request renegotiation on server
2944 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002946{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002948
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02002949 if( ssl == NULL || ssl->conf == NULL )
2950 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002953 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002954 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002956 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
2957 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02002960
2961 /* Did we already try/start sending HelloRequest? */
2962 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02002964
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002965 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002966 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002969#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002970 /*
2971 * On client, either start the renegotiation process or,
2972 * if already in progress, continue the handshake
2973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
2977 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002978
Hanno Becker40cdaa12020-02-05 10:48:27 +00002979 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002980 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00002981 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002982 return( ret );
2983 }
2984 }
2985 else
2986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002990 return( ret );
2991 }
2992 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01002994
Paul Bakker37ce0ff2013-10-31 14:32:04 +01002995 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002996}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01002998
Gilles Peskine9b562d52018-04-25 20:32:43 +02002999void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003000{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003001 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3002
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003003 if( handshake == NULL )
3004 return;
3005
Brett Warrene0edc842021-08-17 09:53:13 +01003006#if defined(MBEDTLS_ECP_C)
3007#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3008 if ( ssl->handshake->group_list_heap_allocated )
3009 mbedtls_free( (void*) handshake->group_list );
3010 handshake->group_list = NULL;
3011#endif /* MBEDTLS_DEPRECATED_REMOVED */
3012#endif /* MBEDTLS_ECP_C */
3013
Jerry Yuf017ee42022-01-12 15:49:48 +08003014#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3015#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3016 if ( ssl->handshake->sig_algs_heap_allocated )
3017 mbedtls_free( (void*) handshake->sig_algs );
3018 handshake->sig_algs = NULL;
3019#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003020#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3021 if( ssl->handshake->certificate_request_context )
3022 {
3023 mbedtls_free( (void*) handshake->certificate_request_context );
3024 }
3025#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003026#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3027
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003028#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3029 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3030 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003031 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003032 handshake->async_in_progress = 0;
3033 }
3034#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3035
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003036#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003037#if defined(MBEDTLS_USE_PSA_CRYPTO)
3038 psa_hash_abort( &handshake->fin_sha256_psa );
3039#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003040 mbedtls_sha256_free( &handshake->fin_sha256 );
3041#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003042#endif
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +02003043#if defined(MBEDTLS_SHA384_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003044#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003045 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003046#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003047 mbedtls_sha512_free( &handshake->fin_sha512 );
3048#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003049#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051#if defined(MBEDTLS_DHM_C)
3052 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003053#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054#if defined(MBEDTLS_ECDH_C)
3055 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003056#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003057#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003058 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003059#if defined(MBEDTLS_SSL_CLI_C)
3060 mbedtls_free( handshake->ecjpake_cache );
3061 handshake->ecjpake_cache = NULL;
3062 handshake->ecjpake_cache_len = 0;
3063#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003064#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003065
Janos Follath4ae5c292016-02-10 11:27:43 +00003066#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3067 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003068 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003069 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003070#endif
3071
Gilles Peskineeccd8882020-03-10 12:19:08 +01003072#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003073 if( handshake->psk != NULL )
3074 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003075 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003076 mbedtls_free( handshake->psk );
3077 }
3078#endif
3079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3081 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003082 /*
3083 * Free only the linked list wrapper, not the keys themselves
3084 * since the belong to the SNI callback
3085 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003086 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003088
Gilles Peskineeccd8882020-03-10 12:19:08 +01003089#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003090 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003091 if( handshake->ecrs_peer_cert != NULL )
3092 {
3093 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3094 mbedtls_free( handshake->ecrs_peer_cert );
3095 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003096#endif
3097
Hanno Becker75173122019-02-06 16:18:31 +00003098#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3099 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3100 mbedtls_pk_free( &handshake->peer_pubkey );
3101#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3102
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003103#if defined(MBEDTLS_SSL_CLI_C) && \
3104 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3105 mbedtls_free( handshake->cookie );
3106#endif /* MBEDTLS_SSL_CLI_C &&
3107 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003108
3109#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003110 mbedtls_ssl_flight_free( handshake->flight );
3111 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003112#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003113
Ronald Cronf12b81d2022-03-15 10:42:41 +01003114#if defined(MBEDTLS_ECDH_C) && \
3115 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Hanno Becker4a63ed42019-01-08 11:39:35 +00003116 psa_destroy_key( handshake->ecdh_psa_privkey );
3117#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3118
Ronald Cron6f135e12021-12-08 16:57:54 +01003119#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003120 mbedtls_ssl_transform_free( handshake->transform_handshake );
3121 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003122 mbedtls_free( handshake->transform_earlydata );
3123 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003124#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003125
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003126
3127#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3128 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3129 * processes datagrams and the fact that a datagram is allowed to have
3130 * several records in it, it is possible that the I/O buffers are not
3131 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003132 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3133 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003134#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003135
Jerry Yuba9c7272021-10-30 11:54:10 +08003136 /* mbedtls_platform_zeroize MUST be last one in this function */
3137 mbedtls_platform_zeroize( handshake,
3138 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003139}
3140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003142{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003143 if( session == NULL )
3144 return;
3145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003147 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003148#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003149
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003150#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003152#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003153
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003154 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003155}
3156
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003157#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003158
3159#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3160#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3161#else
3162#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3163#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3164
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003165#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003166
3167#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3168#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3169#else
3170#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3171#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3172
3173#if defined(MBEDTLS_SSL_ALPN)
3174#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3175#else
3176#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3177#endif /* MBEDTLS_SSL_ALPN */
3178
3179#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3180#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3181#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3182#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3183
3184#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3185 ( (uint32_t) ( \
3186 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3187 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3188 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3189 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3190 0u ) )
3191
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003192static unsigned char ssl_serialized_context_header[] = {
3193 MBEDTLS_VERSION_MAJOR,
3194 MBEDTLS_VERSION_MINOR,
3195 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003196 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3197 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3198 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3199 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3200 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003201};
3202
Paul Bakker5121ce52009-01-03 21:22:43 +00003203/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003204 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003205 *
3206 * The format of the serialized data is:
3207 * (in the presentation language of TLS, RFC 8446 section 3)
3208 *
3209 * // header
3210 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003211 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003212 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003213 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003214 * Note: When updating the format, remember to keep these
3215 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003216 *
3217 * // session sub-structure
3218 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3219 * // transform sub-structure
3220 * uint8 random[64]; // ServerHello.random+ClientHello.random
3221 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3222 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3223 * // fields from ssl_context
3224 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3225 * uint64 in_window_top; // DTLS: last validated record seq_num
3226 * uint64 in_window; // DTLS: bitmask for replay protection
3227 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3228 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3229 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3230 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3231 *
3232 * Note that many fields of the ssl_context or sub-structures are not
3233 * serialized, as they fall in one of the following categories:
3234 *
3235 * 1. forced value (eg in_left must be 0)
3236 * 2. pointer to dynamically-allocated memory (eg session, transform)
3237 * 3. value can be re-derived from other data (eg session keys from MS)
3238 * 4. value was temporary (eg content of input buffer)
3239 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003240 */
3241int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3242 unsigned char *buf,
3243 size_t buf_len,
3244 size_t *olen )
3245{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003246 unsigned char *p = buf;
3247 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003248 size_t session_len;
3249 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003250
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003251 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003252 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3253 * this function's documentation.
3254 *
3255 * These are due to assumptions/limitations in the implementation. Some of
3256 * them are likely to stay (no handshake in progress) some might go away
3257 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003258 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003259 /* The initial handshake must be over */
3260 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003261 {
3262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003263 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003264 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003265 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003266 {
3267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003268 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003269 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003270 /* Double-check that sub-structures are indeed ready */
3271 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003272 {
3273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003274 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003275 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003276 /* There must be no pending incoming or outgoing data */
3277 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003278 {
3279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003280 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003281 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003282 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003283 {
3284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003285 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003286 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003287 /* Protocol must be DLTS, not TLS */
3288 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003289 {
3290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003291 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003292 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003293 /* Version must be 1.2 */
3294 if( ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003295 {
3296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003297 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003298 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003299 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003300 {
3301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003302 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003303 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003304 /* We must be using an AEAD ciphersuite */
3305 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003306 {
3307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003308 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003309 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003310 /* Renegotiation must not be enabled */
3311#if defined(MBEDTLS_SSL_RENEGOTIATION)
3312 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003313 {
3314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003315 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003316 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003317#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003318
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003319 /*
3320 * Version and format identifier
3321 */
3322 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003323
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003324 if( used <= buf_len )
3325 {
3326 memcpy( p, ssl_serialized_context_header,
3327 sizeof( ssl_serialized_context_header ) );
3328 p += sizeof( ssl_serialized_context_header );
3329 }
3330
3331 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003332 * Session (length + data)
3333 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003334 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003335 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3336 return( ret );
3337
3338 used += 4 + session_len;
3339 if( used <= buf_len )
3340 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003341 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3342 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003343
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003344 ret = ssl_session_save( ssl->session, 1,
3345 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003346 if( ret != 0 )
3347 return( ret );
3348
3349 p += session_len;
3350 }
3351
3352 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003353 * Transform
3354 */
3355 used += sizeof( ssl->transform->randbytes );
3356 if( used <= buf_len )
3357 {
3358 memcpy( p, ssl->transform->randbytes,
3359 sizeof( ssl->transform->randbytes ) );
3360 p += sizeof( ssl->transform->randbytes );
3361 }
3362
3363#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3364 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3365 if( used <= buf_len )
3366 {
3367 *p++ = ssl->transform->in_cid_len;
3368 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3369 p += ssl->transform->in_cid_len;
3370
3371 *p++ = ssl->transform->out_cid_len;
3372 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3373 p += ssl->transform->out_cid_len;
3374 }
3375#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3376
3377 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003378 * Saved fields from top-level ssl_context structure
3379 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003380 used += 4;
3381 if( used <= buf_len )
3382 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003383 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3384 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003385 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003386
3387#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3388 used += 16;
3389 if( used <= buf_len )
3390 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003391 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3392 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003393
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003394 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3395 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003396 }
3397#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3398
3399#if defined(MBEDTLS_SSL_PROTO_DTLS)
3400 used += 1;
3401 if( used <= buf_len )
3402 {
3403 *p++ = ssl->disable_datagram_packing;
3404 }
3405#endif /* MBEDTLS_SSL_PROTO_DTLS */
3406
Jerry Yuae0b2e22021-10-08 15:21:19 +08003407 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003408 if( used <= buf_len )
3409 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003410 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3411 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003412 }
3413
3414#if defined(MBEDTLS_SSL_PROTO_DTLS)
3415 used += 2;
3416 if( used <= buf_len )
3417 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003418 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
3419 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003420 }
3421#endif /* MBEDTLS_SSL_PROTO_DTLS */
3422
3423#if defined(MBEDTLS_SSL_ALPN)
3424 {
3425 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02003426 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003427 : 0;
3428
3429 used += 1 + alpn_len;
3430 if( used <= buf_len )
3431 {
3432 *p++ = alpn_len;
3433
3434 if( ssl->alpn_chosen != NULL )
3435 {
3436 memcpy( p, ssl->alpn_chosen, alpn_len );
3437 p += alpn_len;
3438 }
3439 }
3440 }
3441#endif /* MBEDTLS_SSL_ALPN */
3442
3443 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003444 * Done
3445 */
3446 *olen = used;
3447
3448 if( used > buf_len )
3449 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003450
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003451 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
3452
Hanno Becker43aefe22020-02-05 10:44:56 +00003453 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003454}
3455
3456/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003457 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003458 *
3459 * This internal version is wrapped by a public function that cleans up in
3460 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003461 */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003462static int ssl_context_load( mbedtls_ssl_context *ssl,
3463 const unsigned char *buf,
3464 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003465{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003466 const unsigned char *p = buf;
3467 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003468 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00003469 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003470
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003471 /*
3472 * The context should have been freshly setup or reset.
3473 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02003474 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003475 * renegotiating, or if the user mistakenly loaded a session first.)
3476 */
3477 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
3478 ssl->session != NULL )
3479 {
3480 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3481 }
3482
3483 /*
3484 * We can't check that the config matches the initial one, but we can at
3485 * least check it matches the requirements for serializing.
3486 */
3487 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
3488 ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
3489 ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 ||
3490 ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ||
3491 ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 ||
3492#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003493 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003494#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003495 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003496 {
3497 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3498 }
3499
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003500 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
3501
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003502 /*
3503 * Check version identifier
3504 */
3505 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
3506 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3507
3508 if( memcmp( p, ssl_serialized_context_header,
3509 sizeof( ssl_serialized_context_header ) ) != 0 )
3510 {
3511 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3512 }
3513 p += sizeof( ssl_serialized_context_header );
3514
3515 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003516 * Session
3517 */
3518 if( (size_t)( end - p ) < 4 )
3519 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3520
3521 session_len = ( (size_t) p[0] << 24 ) |
3522 ( (size_t) p[1] << 16 ) |
3523 ( (size_t) p[2] << 8 ) |
3524 ( (size_t) p[3] );
3525 p += 4;
3526
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003527 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00003528 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003529 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003530 ssl->session_in = ssl->session;
3531 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003532 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003533
3534 if( (size_t)( end - p ) < session_len )
3535 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3536
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003537 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003538 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003539 {
3540 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003541 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003542 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003543
3544 p += session_len;
3545
3546 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003547 * Transform
3548 */
3549
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003550 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00003551 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003552 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003553 ssl->transform_in = ssl->transform;
3554 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003555 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003556
3557 /* Read random bytes and populate structure */
3558 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
3559 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08003560#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00003561 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003562 ssl->session->ciphersuite,
3563 ssl->session->master,
Hanno Beckerbd257552021-03-22 06:59:27 +00003564#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
3565 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003566 ssl->session->encrypt_then_mac,
Hanno Beckerbd257552021-03-22 06:59:27 +00003567#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
3568 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003569 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
3570 p, /* currently pointing to randbytes */
3571 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
3572 ssl->conf->endpoint,
3573 ssl );
3574 if( ret != 0 )
3575 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08003576#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003577 p += sizeof( ssl->transform->randbytes );
3578
3579#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3580 /* Read connection IDs and store them */
3581 if( (size_t)( end - p ) < 1 )
3582 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3583
3584 ssl->transform->in_cid_len = *p++;
3585
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02003586 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003587 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3588
3589 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
3590 p += ssl->transform->in_cid_len;
3591
3592 ssl->transform->out_cid_len = *p++;
3593
3594 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
3595 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3596
3597 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
3598 p += ssl->transform->out_cid_len;
3599#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3600
3601 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003602 * Saved fields from top-level ssl_context structure
3603 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003604 if( (size_t)( end - p ) < 4 )
3605 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3606
3607 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
3608 ( (uint32_t) p[1] << 16 ) |
3609 ( (uint32_t) p[2] << 8 ) |
3610 ( (uint32_t) p[3] );
3611 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003612
3613#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3614 if( (size_t)( end - p ) < 16 )
3615 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3616
3617 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
3618 ( (uint64_t) p[1] << 48 ) |
3619 ( (uint64_t) p[2] << 40 ) |
3620 ( (uint64_t) p[3] << 32 ) |
3621 ( (uint64_t) p[4] << 24 ) |
3622 ( (uint64_t) p[5] << 16 ) |
3623 ( (uint64_t) p[6] << 8 ) |
3624 ( (uint64_t) p[7] );
3625 p += 8;
3626
3627 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
3628 ( (uint64_t) p[1] << 48 ) |
3629 ( (uint64_t) p[2] << 40 ) |
3630 ( (uint64_t) p[3] << 32 ) |
3631 ( (uint64_t) p[4] << 24 ) |
3632 ( (uint64_t) p[5] << 16 ) |
3633 ( (uint64_t) p[6] << 8 ) |
3634 ( (uint64_t) p[7] );
3635 p += 8;
3636#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3637
3638#if defined(MBEDTLS_SSL_PROTO_DTLS)
3639 if( (size_t)( end - p ) < 1 )
3640 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3641
3642 ssl->disable_datagram_packing = *p++;
3643#endif /* MBEDTLS_SSL_PROTO_DTLS */
3644
Jerry Yud9a94fe2021-09-28 18:58:59 +08003645 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003646 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08003647 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
3648 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003649
3650#if defined(MBEDTLS_SSL_PROTO_DTLS)
3651 if( (size_t)( end - p ) < 2 )
3652 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3653
3654 ssl->mtu = ( p[0] << 8 ) | p[1];
3655 p += 2;
3656#endif /* MBEDTLS_SSL_PROTO_DTLS */
3657
3658#if defined(MBEDTLS_SSL_ALPN)
3659 {
3660 uint8_t alpn_len;
3661 const char **cur;
3662
3663 if( (size_t)( end - p ) < 1 )
3664 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3665
3666 alpn_len = *p++;
3667
3668 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
3669 {
3670 /* alpn_chosen should point to an item in the configured list */
3671 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
3672 {
3673 if( strlen( *cur ) == alpn_len &&
3674 memcmp( p, cur, alpn_len ) == 0 )
3675 {
3676 ssl->alpn_chosen = *cur;
3677 break;
3678 }
3679 }
3680 }
3681
3682 /* can only happen on conf mismatch */
3683 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
3684 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3685
3686 p += alpn_len;
3687 }
3688#endif /* MBEDTLS_SSL_ALPN */
3689
3690 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003691 * Forced fields from top-level ssl_context structure
3692 *
3693 * Most of them already set to the correct value by mbedtls_ssl_init() and
3694 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
3695 */
3696 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
3697
3698 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
3699 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
3700
Hanno Becker361b10d2019-08-30 10:42:49 +01003701 /* Adjust pointers for header fields of outgoing records to
3702 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00003703 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01003704
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003705#if defined(MBEDTLS_SSL_PROTO_DTLS)
3706 ssl->in_epoch = 1;
3707#endif
3708
3709 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
3710 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00003711 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
3712 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02003713 if( ssl->handshake != NULL )
3714 {
3715 mbedtls_ssl_handshake_free( ssl );
3716 mbedtls_free( ssl->handshake );
3717 ssl->handshake = NULL;
3718 }
3719
3720 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003721 * Done - should have consumed entire buffer
3722 */
3723 if( p != end )
3724 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003725
3726 return( 0 );
3727}
3728
3729/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003730 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003731 */
3732int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
3733 const unsigned char *buf,
3734 size_t len )
3735{
3736 int ret = ssl_context_load( context, buf, len );
3737
3738 if( ret != 0 )
3739 mbedtls_ssl_free( context );
3740
3741 return( ret );
3742}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003743#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003744
3745/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003746 * Free an SSL context
3747 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003748void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003749{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003750 if( ssl == NULL )
3751 return;
3752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003754
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003755 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003756 {
sander-visserb8aa2072020-05-06 22:05:13 +02003757#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3758 size_t out_buf_len = ssl->out_buf_len;
3759#else
3760 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
3761#endif
3762
Darryl Greenb33cc762019-11-28 14:29:44 +00003763 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003764 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003765 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003766 }
3767
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003768 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003769 {
sander-visserb8aa2072020-05-06 22:05:13 +02003770#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3771 size_t in_buf_len = ssl->in_buf_len;
3772#else
3773 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
3774#endif
3775
Darryl Greenb33cc762019-11-28 14:29:44 +00003776 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003777 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003778 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003779 }
3780
Paul Bakker48916f92012-09-16 19:57:18 +00003781 if( ssl->transform )
3782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003783 mbedtls_ssl_transform_free( ssl->transform );
3784 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003785 }
3786
3787 if( ssl->handshake )
3788 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02003789 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790 mbedtls_ssl_transform_free( ssl->transform_negotiate );
3791 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00003792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003793 mbedtls_free( ssl->handshake );
3794 mbedtls_free( ssl->transform_negotiate );
3795 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00003796 }
3797
Ronald Cron6f135e12021-12-08 16:57:54 +01003798#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01003799 mbedtls_ssl_transform_free( ssl->transform_application );
3800 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01003801#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01003802
Paul Bakkerc0463502013-02-14 11:19:38 +01003803 if( ssl->session )
3804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003805 mbedtls_ssl_session_free( ssl->session );
3806 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003807 }
3808
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02003809#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02003810 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003811 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003812 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003813 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00003814 }
Paul Bakker0be444a2013-08-27 21:55:01 +02003815#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003816
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003817#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003818 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02003819#endif
3820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00003822
Paul Bakker86f04f42013-02-14 11:20:09 +01003823 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003824 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003825}
3826
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02003827/*
3828 * Initialze mbedtls_ssl_config
3829 */
3830void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
3831{
3832 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
3833}
3834
Gilles Peskineeccd8882020-03-10 12:19:08 +01003835#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003836#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskineae270bf2021-06-02 00:05:29 +02003837/* The selection should be the same as mbedtls_x509_crt_profile_default in
Gilles Peskinea28f0f52021-06-02 15:29:38 +02003838 * x509_crt.c. Here, the order matters. Currently we favor stronger hashes,
3839 * for no fundamental reason.
Gilles Peskineae270bf2021-06-02 00:05:29 +02003840 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
3841 * about this list. */
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003842static int ssl_preset_default_hashes[] = {
3843#if defined(MBEDTLS_SHA512_C)
3844 MBEDTLS_MD_SHA512,
Mateusz Starzyk3352a532021-04-06 14:28:22 +02003845#endif
3846#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003847 MBEDTLS_MD_SHA384,
3848#endif
3849#if defined(MBEDTLS_SHA256_C)
3850 MBEDTLS_MD_SHA256,
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02003851#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003852 MBEDTLS_MD_NONE
3853};
Jerry Yuf017ee42022-01-12 15:49:48 +08003854#endif /* !MBEDTLS_DEPRECATED_REMOVED */
3855#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01003856
Gilles Peskineae270bf2021-06-02 00:05:29 +02003857/* The selection should be the same as mbedtls_x509_crt_profile_default in
3858 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02003859 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02003860 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02003861 * about this list.
3862 */
Brett Warrene0edc842021-08-17 09:53:13 +01003863static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02003864#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003865 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003866#endif
3867#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003868 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003869#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02003870#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003871 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003872#endif
3873#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003874 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003875#endif
3876#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003877 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003878#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02003879#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003880 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02003881#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02003882#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003883 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003884#endif
3885#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01003886 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02003887#endif
Brett Warrene0edc842021-08-17 09:53:13 +01003888 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02003889};
Gilles Peskineae270bf2021-06-02 00:05:29 +02003890
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003891static int ssl_preset_suiteb_ciphersuites[] = {
3892 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
3893 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
3894 0
3895};
3896
Gilles Peskineeccd8882020-03-10 12:19:08 +01003897#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003898#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003899static int ssl_preset_suiteb_hashes[] = {
Jerry Yuf017ee42022-01-12 15:49:48 +08003900#if defined(MBEDTLS_SHA256_C)
3901 MBEDTLS_MD_SHA256,
3902#endif
Jerry Yu713013f2022-01-17 18:16:35 +08003903#if defined(MBEDTLS_SHA384_C)
3904 MBEDTLS_MD_SHA384,
3905#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003906 MBEDTLS_MD_NONE
3907};
Jerry Yuf017ee42022-01-12 15:49:48 +08003908#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01003909
Jerry Yu909df7b2022-01-22 11:56:27 +08003910/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08003911 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08003912 * rules SHOULD be upheld.
3913 * - No duplicate entries.
3914 * - But if there is a good reason, do not change the order of the algorithms.
3915 * - ssl_tls12_present* is for TLS 1.2 use only.
3916 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08003917 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01003918static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08003919
Jerry Yued5e9f42022-01-26 11:21:34 +08003920#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \
3921 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
3922 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
3923#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C &&
3924 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08003925
Jerry Yu909df7b2022-01-22 11:56:27 +08003926#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \
3927 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
3928 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
3929#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
3930 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
3931
Jerry Yued5e9f42022-01-26 11:21:34 +08003932#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA512_C) && \
3933 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
3934 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
3935#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
3936 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08003937
Jerry Yu909df7b2022-01-22 11:56:27 +08003938#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C)
3939 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
3940#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */
3941
Ronald Cron8540cf62022-03-16 08:01:09 +01003942#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA512_C)
3943 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
3944#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
3945
3946#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA384_C)
3947 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
3948#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
3949
Jerry Yu53037892022-01-25 11:02:06 +08003950#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C)
3951 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
3952#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
3953
Jerry Yu909df7b2022-01-22 11:56:27 +08003954 MBEDTLS_TLS1_3_SIG_NONE
3955};
3956
3957/* NOTICE: see above */
3958#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3959static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Jerry Yu713013f2022-01-17 18:16:35 +08003960#if defined(MBEDTLS_SHA512_C)
3961 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA512 )
3962#endif
Jerry Yu11f0a9c2022-01-12 18:43:08 +08003963#if defined(MBEDTLS_SHA384_C)
3964 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 )
3965#endif
3966#if defined(MBEDTLS_SHA256_C)
3967 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 )
3968#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08003969 MBEDTLS_TLS1_3_SIG_NONE
3970};
3971#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3972/* NOTICE: see above */
3973static uint16_t ssl_preset_suiteb_sig_algs[] = {
3974
Jerry Yu909df7b2022-01-22 11:56:27 +08003975#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \
3976 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
3977 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
3978#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C &&
3979 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
3980
Jerry Yu53037892022-01-25 11:02:06 +08003981#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \
3982 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
3983 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
3984#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
3985 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08003986
3987#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C)
3988 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
3989#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */
Jerry Yu1a8b4812022-01-20 17:56:50 +08003990
Jerry Yu53037892022-01-25 11:02:06 +08003991#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C)
3992 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
3993#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
3994
Jerry Yu713013f2022-01-17 18:16:35 +08003995 MBEDTLS_TLS1_3_SIG_NONE
3996};
Jerry Yu6106fdc2022-01-12 16:36:14 +08003997
Jerry Yu909df7b2022-01-22 11:56:27 +08003998/* NOTICE: see above */
3999#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4000static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Jerry Yu713013f2022-01-17 18:16:35 +08004001#if defined(MBEDTLS_SHA256_C)
4002 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 )
4003#endif
Jerry Yu18c833e2022-01-25 10:55:47 +08004004#if defined(MBEDTLS_SHA384_C)
4005 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 )
4006#endif
Xiaofei Bai95395012021-11-25 08:51:30 +00004007 MBEDTLS_TLS1_3_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004008};
Jerry Yu909df7b2022-01-22 11:56:27 +08004009#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4010
Jerry Yu1a8b4812022-01-20 17:56:50 +08004011#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004012
Brett Warrene0edc842021-08-17 09:53:13 +01004013static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004014#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004015 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004016#endif
4017#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004018 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004019#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004020 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004021};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004022
Jerry Yu1a8b4812022-01-20 17:56:50 +08004023#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004024/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004025 * to make sure there are no duplicated signature algorithm entries. */
Jerry Yuf377d642022-01-25 10:43:59 +08004026static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004027{
4028 size_t i, j;
4029 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004030
Jerry Yuf377d642022-01-25 10:43:59 +08004031 for( i = 0; sig_algs[i] != MBEDTLS_TLS1_3_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004032 {
Jerry Yuf377d642022-01-25 10:43:59 +08004033 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004034 {
Jerry Yuf377d642022-01-25 10:43:59 +08004035 if( sig_algs[i] != sig_algs[j] )
4036 continue;
4037 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4038 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4039 sig_algs[i], j, i );
4040 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004041 }
4042 }
4043 return( ret );
4044}
4045
4046#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4047
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004048/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004049 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004050 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004051int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004052 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004053{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004054#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004055 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004056#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004057
Jerry Yu1a8b4812022-01-20 17:56:50 +08004058#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004059 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004060 {
4061 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4062 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4063 }
4064
Jerry Yuf377d642022-01-25 10:43:59 +08004065 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004066 {
4067 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4068 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4069 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004070
4071#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004072 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004073 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004074 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004075 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4076 }
4077
Jerry Yuf377d642022-01-25 10:43:59 +08004078 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004079 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004080 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004081 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4082 }
4083#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004084#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4085
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004086 /* Use the functions here so that they are covered in tests,
4087 * but otherwise access member directly for efficiency */
4088 mbedtls_ssl_conf_endpoint( conf, endpoint );
4089 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004090
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004091 /*
4092 * Things that are common to all presets
4093 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004094#if defined(MBEDTLS_SSL_CLI_C)
4095 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4096 {
4097 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4098#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4099 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4100#endif
4101 }
4102#endif
4103
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004104#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4105 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4106#endif
4107
4108#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4109 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4110#endif
4111
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004112#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004113 conf->f_cookie_write = ssl_cookie_write_dummy;
4114 conf->f_cookie_check = ssl_cookie_check_dummy;
4115#endif
4116
4117#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4118 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4119#endif
4120
Janos Follath088ce432017-04-10 12:42:31 +01004121#if defined(MBEDTLS_SSL_SRV_C)
4122 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004123 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004124#endif
4125
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004126#if defined(MBEDTLS_SSL_PROTO_DTLS)
4127 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4128 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4129#endif
4130
4131#if defined(MBEDTLS_SSL_RENEGOTIATION)
4132 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004133 memset( conf->renego_period, 0x00, 2 );
4134 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004135#endif
4136
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004137#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004138 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4139 {
4140 const unsigned char dhm_p[] =
4141 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4142 const unsigned char dhm_g[] =
4143 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004144
Hanno Beckere2defad2021-07-24 05:59:17 +01004145 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4146 dhm_p, sizeof( dhm_p ),
4147 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4148 {
4149 return( ret );
4150 }
4151 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004152#endif
4153
Ronald Cron6f135e12021-12-08 16:57:54 +01004154#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker71f1ed62021-07-24 06:01:47 +01004155 /*
4156 * Allow all TLS 1.3 key exchange modes by default.
4157 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004158 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004159#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004160
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004161 /*
4162 * Preset-specific defaults
4163 */
4164 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004165 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004166 /*
4167 * NSA Suite B
4168 */
4169 case MBEDTLS_SSL_PRESET_SUITEB:
4170 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
4171 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
4172 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004173#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4174 /* Hybrid TLS 1.2/1.3 is not supported yet */
4175 conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
4176#else
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004177 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004178#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3 */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004179
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004180 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004181
4182#if defined(MBEDTLS_X509_CRT_PARSE_C)
4183 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004184#endif
4185
Gilles Peskineeccd8882020-03-10 12:19:08 +01004186#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004187#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004188 conf->sig_hashes = ssl_preset_suiteb_hashes;
Jerry Yuf017ee42022-01-12 15:49:48 +08004189#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004190#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4191 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4192 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4193 else
4194#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4195 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004196#endif
4197
Brett Warrene0edc842021-08-17 09:53:13 +01004198#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4199 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004200#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004201 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004202 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004203
4204 /*
4205 * Default
4206 */
4207 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03004208 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
4209 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
4210 MBEDTLS_SSL_MIN_MAJOR_VERSION :
4211 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
4212 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
4213 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
4214 MBEDTLS_SSL_MIN_MINOR_VERSION :
4215 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004216 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004217#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4218 /* Hybrid TLS 1.2/1.3 is not supported yet */
4219 conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
4220#else
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004221 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004222#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3 */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004223
4224#if defined(MBEDTLS_SSL_PROTO_DTLS)
4225 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
TRodziewiczef73f012021-05-13 14:53:36 +02004226 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004227#endif
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004228 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004229
4230#if defined(MBEDTLS_X509_CRT_PARSE_C)
4231 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4232#endif
4233
Gilles Peskineeccd8882020-03-10 12:19:08 +01004234#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004235#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01004236 conf->sig_hashes = ssl_preset_default_hashes;
Jerry Yuf017ee42022-01-12 15:49:48 +08004237#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004238#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4239 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4240 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4241 else
4242#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4243 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004244#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004245
Brett Warrene0edc842021-08-17 09:53:13 +01004246#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4247 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004248#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004249 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004250
4251#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4252 conf->dhm_min_bitlen = 1024;
4253#endif
4254 }
4255
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004256 return( 0 );
4257}
4258
4259/*
4260 * Free mbedtls_ssl_config
4261 */
4262void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4263{
4264#if defined(MBEDTLS_DHM_C)
4265 mbedtls_mpi_free( &conf->dhm_P );
4266 mbedtls_mpi_free( &conf->dhm_G );
4267#endif
4268
Gilles Peskineeccd8882020-03-10 12:19:08 +01004269#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004270 if( conf->psk != NULL )
4271 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004272 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004273 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004274 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004275 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004276 }
4277
4278 if( conf->psk_identity != NULL )
4279 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004280 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004281 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004282 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004283 conf->psk_identity_len = 0;
4284 }
4285#endif
4286
4287#if defined(MBEDTLS_X509_CRT_PARSE_C)
4288 ssl_key_cert_free( conf->key_cert );
4289#endif
4290
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004291 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004292}
4293
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004294#if defined(MBEDTLS_PK_C) && \
4295 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004296/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004297 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004299unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004300{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004301#if defined(MBEDTLS_RSA_C)
4302 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4303 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004304#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004305#if defined(MBEDTLS_ECDSA_C)
4306 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4307 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004308#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004309 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004310}
4311
Hanno Becker7e5437a2017-04-28 17:15:26 +01004312unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4313{
4314 switch( type ) {
4315 case MBEDTLS_PK_RSA:
4316 return( MBEDTLS_SSL_SIG_RSA );
4317 case MBEDTLS_PK_ECDSA:
4318 case MBEDTLS_PK_ECKEY:
4319 return( MBEDTLS_SSL_SIG_ECDSA );
4320 default:
4321 return( MBEDTLS_SSL_SIG_ANON );
4322 }
4323}
4324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004325mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004326{
4327 switch( sig )
4328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004329#if defined(MBEDTLS_RSA_C)
4330 case MBEDTLS_SSL_SIG_RSA:
4331 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004332#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004333#if defined(MBEDTLS_ECDSA_C)
4334 case MBEDTLS_SSL_SIG_ECDSA:
4335 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004336#endif
4337 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004338 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004339 }
4340}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004341#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004342
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004343/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004344 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004346mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004347{
4348 switch( hash )
4349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004350#if defined(MBEDTLS_MD5_C)
4351 case MBEDTLS_SSL_HASH_MD5:
4352 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004353#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004354#if defined(MBEDTLS_SHA1_C)
4355 case MBEDTLS_SSL_HASH_SHA1:
4356 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004357#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004358#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004359 case MBEDTLS_SSL_HASH_SHA224:
4360 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004361#endif
4362#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004363 case MBEDTLS_SSL_HASH_SHA256:
4364 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004365#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004366#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004367 case MBEDTLS_SSL_HASH_SHA384:
4368 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004369#endif
4370#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004371 case MBEDTLS_SSL_HASH_SHA512:
4372 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004373#endif
4374 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004375 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004376 }
4377}
4378
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004379/*
4380 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4381 */
4382unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4383{
4384 switch( md )
4385 {
4386#if defined(MBEDTLS_MD5_C)
4387 case MBEDTLS_MD_MD5:
4388 return( MBEDTLS_SSL_HASH_MD5 );
4389#endif
4390#if defined(MBEDTLS_SHA1_C)
4391 case MBEDTLS_MD_SHA1:
4392 return( MBEDTLS_SSL_HASH_SHA1 );
4393#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004394#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004395 case MBEDTLS_MD_SHA224:
4396 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004397#endif
4398#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004399 case MBEDTLS_MD_SHA256:
4400 return( MBEDTLS_SSL_HASH_SHA256 );
4401#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004402#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004403 case MBEDTLS_MD_SHA384:
4404 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004405#endif
4406#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004407 case MBEDTLS_MD_SHA512:
4408 return( MBEDTLS_SSL_HASH_SHA512 );
4409#endif
4410 default:
4411 return( MBEDTLS_SSL_HASH_NONE );
4412 }
4413}
4414
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004415/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004416 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004417 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004418 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004419int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004420{
Brett Warrene0edc842021-08-17 09:53:13 +01004421 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004422
Brett Warrene0edc842021-08-17 09:53:13 +01004423 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004424 return( -1 );
4425
Brett Warrene0edc842021-08-17 09:53:13 +01004426 for( ; *group_list != 0; group_list++ )
4427 {
4428 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004429 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01004430 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004431
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004432 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004433}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004434
4435#if defined(MBEDTLS_ECP_C)
4436/*
4437 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
4438 */
4439int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
4440{
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01004441 uint16_t tls_id = mbedtls_ecp_curve_info_from_grp_id( grp_id )->tls_id;
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004442 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
4443}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004444#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004446#if defined(MBEDTLS_X509_CRT_PARSE_C)
4447int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
4448 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004449 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02004450 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004451{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004452 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004453 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004454 const char *ext_oid;
4455 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004457 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004458 {
4459 /* Server part of the key exchange */
4460 switch( ciphersuite->key_exchange )
4461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004462 case MBEDTLS_KEY_EXCHANGE_RSA:
4463 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004464 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004465 break;
4466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004467 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
4468 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
4469 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
4470 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004471 break;
4472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004473 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
4474 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004475 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004476 break;
4477
4478 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004479 case MBEDTLS_KEY_EXCHANGE_NONE:
4480 case MBEDTLS_KEY_EXCHANGE_PSK:
4481 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
4482 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02004483 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004484 usage = 0;
4485 }
4486 }
4487 else
4488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004489 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
4490 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004491 }
4492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004493 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004494 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004495 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004496 ret = -1;
4497 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004499 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004501 ext_oid = MBEDTLS_OID_SERVER_AUTH;
4502 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004503 }
4504 else
4505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004506 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
4507 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004508 }
4509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004510 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004511 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004512 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004513 ret = -1;
4514 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004515
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004516 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004517}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004518#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004519
Simon Butcher99000142016-10-13 17:21:01 +01004520int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
4521{
4522#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4523 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Becker541af852021-05-14 16:49:01 +01004524 return( -1 );
Simon Butcher99000142016-10-13 17:21:01 +01004525
4526 switch( md )
4527 {
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +02004528#if defined(MBEDTLS_SHA384_C)
Simon Butcher99000142016-10-13 17:21:01 +01004529 case MBEDTLS_SSL_HASH_SHA384:
4530 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
4531 break;
4532#endif
4533#if defined(MBEDTLS_SHA256_C)
4534 case MBEDTLS_SSL_HASH_SHA256:
4535 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
4536 break;
4537#endif
4538 default:
Hanno Becker541af852021-05-14 16:49:01 +01004539 return( -1 );
Simon Butcher99000142016-10-13 17:21:01 +01004540 }
4541
4542 return 0;
4543#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
4544 (void) ssl;
4545 (void) md;
4546
Hanno Becker541af852021-05-14 16:49:01 +01004547 return( -1 );
Simon Butcher99000142016-10-13 17:21:01 +01004548#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4549}
4550
Jerry Yu148165c2021-09-24 23:20:59 +08004551#if defined(MBEDTLS_USE_PSA_CRYPTO)
4552int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
4553 const mbedtls_md_type_t md,
4554 unsigned char *dst,
4555 size_t dst_len,
4556 size_t *olen )
4557{
Ronald Cronf6893e12022-01-07 22:09:01 +01004558 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4559 psa_hash_operation_t *hash_operation_to_clone;
4560 psa_hash_operation_t hash_operation = psa_hash_operation_init();
4561
Jerry Yu148165c2021-09-24 23:20:59 +08004562 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01004563
4564 switch( md )
4565 {
4566#if defined(MBEDTLS_SHA384_C)
4567 case MBEDTLS_MD_SHA384:
4568 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
4569 break;
4570#endif
4571
4572#if defined(MBEDTLS_SHA256_C)
4573 case MBEDTLS_MD_SHA256:
4574 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
4575 break;
4576#endif
4577
4578 default:
4579 goto exit;
4580 }
4581
4582 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
4583 if( status != PSA_SUCCESS )
4584 goto exit;
4585
4586 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
4587 if( status != PSA_SUCCESS )
4588 goto exit;
4589
4590exit:
Ronald Cronb788c042022-02-15 09:14:15 +01004591 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08004592}
4593#else /* MBEDTLS_USE_PSA_CRYPTO */
4594
Jerry Yu24c0ec32021-09-09 14:21:07 +08004595#if defined(MBEDTLS_SHA384_C)
Jerry Yu000f9762021-09-14 11:12:51 +08004596static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
4597 unsigned char *dst,
4598 size_t dst_len,
4599 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004600{
Jerry Yu24c0ec32021-09-09 14:21:07 +08004601 int ret;
4602 mbedtls_sha512_context sha512;
4603
4604 if( dst_len < 48 )
4605 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4606
4607 mbedtls_sha512_init( &sha512 );
4608 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
4609
4610 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
4611 {
4612 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
4613 goto exit;
4614 }
4615
4616 *olen = 48;
4617
4618exit:
4619
4620 mbedtls_sha512_free( &sha512 );
4621 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004622}
4623#endif /* MBEDTLS_SHA384_C */
4624
4625#if defined(MBEDTLS_SHA256_C)
Jerry Yu000f9762021-09-14 11:12:51 +08004626static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
4627 unsigned char *dst,
4628 size_t dst_len,
4629 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004630{
Jerry Yu24c0ec32021-09-09 14:21:07 +08004631 int ret;
4632 mbedtls_sha256_context sha256;
4633
4634 if( dst_len < 32 )
4635 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4636
4637 mbedtls_sha256_init( &sha256 );
4638 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08004639
Jerry Yu24c0ec32021-09-09 14:21:07 +08004640 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
4641 {
4642 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
4643 goto exit;
4644 }
4645
4646 *olen = 32;
4647
4648exit:
4649
4650 mbedtls_sha256_free( &sha256 );
4651 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004652}
4653#endif /* MBEDTLS_SHA256_C */
4654
Jerry Yu000f9762021-09-14 11:12:51 +08004655int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
4656 const mbedtls_md_type_t md,
4657 unsigned char *dst,
4658 size_t dst_len,
4659 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004660{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004661 switch( md )
4662 {
4663
Jerry Yu24c0ec32021-09-09 14:21:07 +08004664#if defined(MBEDTLS_SHA384_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004665 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08004666 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08004667#endif /* MBEDTLS_SHA384_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004668
Jerry Yu24c0ec32021-09-09 14:21:07 +08004669#if defined(MBEDTLS_SHA256_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004670 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08004671 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08004672#endif /* MBEDTLS_SHA256_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08004673
4674 default:
4675 break;
4676 }
Jerry Yuc5aef882021-12-23 20:15:02 +08004677 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004678}
XiaokangQian647719a2021-12-07 09:16:29 +00004679
Jerry Yu148165c2021-09-24 23:20:59 +08004680#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08004681
Jerry Yu1ea9d102021-12-21 13:41:49 +08004682#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) || \
4683 defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4684 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jerry Yuba073422021-12-20 22:22:15 +08004685/*
Jerry Yub925f212022-01-12 11:17:02 +08004686 * Function for writing a supported groups (TLS 1.3) or supported elliptic
4687 * curves (TLS 1.2) extension.
Jerry Yuba073422021-12-20 22:22:15 +08004688 *
Jerry Yub925f212022-01-12 11:17:02 +08004689 * The "extension_data" field of a supported groups extension contains a
4690 * "NamedGroupList" value (TLS 1.3 RFC8446):
Jerry Yuba073422021-12-20 22:22:15 +08004691 * enum {
4692 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
4693 * x25519(0x001D), x448(0x001E),
4694 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
4695 * ffdhe6144(0x0103), ffdhe8192(0x0104),
4696 * ffdhe_private_use(0x01FC..0x01FF),
4697 * ecdhe_private_use(0xFE00..0xFEFF),
4698 * (0xFFFF)
4699 * } NamedGroup;
4700 * struct {
4701 * NamedGroup named_group_list<2..2^16-1>;
4702 * } NamedGroupList;
Jerry Yub925f212022-01-12 11:17:02 +08004703 *
4704 * The "extension_data" field of a supported elliptic curves extension contains
4705 * a "NamedCurveList" value (TLS 1.2 RFC 8422):
Jerry Yu63282b42022-01-11 15:43:53 +08004706 * enum {
4707 * deprecated(1..22),
4708 * secp256r1 (23), secp384r1 (24), secp521r1 (25),
4709 * x25519(29), x448(30),
4710 * reserved (0xFE00..0xFEFF),
4711 * deprecated(0xFF01..0xFF02),
4712 * (0xFFFF)
4713 * } NamedCurve;
4714 * struct {
4715 * NamedCurve named_curve_list<2..2^16-1>
4716 * } NamedCurveList;
4717 *
Jerry Yub925f212022-01-12 11:17:02 +08004718 * The TLS 1.3 supported groups extension was defined to be a compatible
4719 * generalization of the TLS 1.2 supported elliptic curves extension. They both
4720 * share the same extension identifier.
Jerry Yu63282b42022-01-11 15:43:53 +08004721 *
Jerry Yub925f212022-01-12 11:17:02 +08004722 * DHE groups are not supported yet.
Jerry Yuba073422021-12-20 22:22:15 +08004723 */
Jerry Yuba073422021-12-20 22:22:15 +08004724int mbedtls_ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl,
4725 unsigned char *buf,
Jerry Yu17532612021-12-20 22:32:09 +08004726 const unsigned char *end,
Jerry Yuba073422021-12-20 22:22:15 +08004727 size_t *out_len )
4728{
4729 unsigned char *p = buf ;
4730 unsigned char *named_group_list; /* Start of named_group_list */
4731 size_t named_group_list_len; /* Length of named_group_list */
4732 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
4733
4734 *out_len = 0;
Jerry Yuf46b0162022-01-11 16:28:00 +08004735
Jerry Yuba073422021-12-20 22:22:15 +08004736 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) );
4737
4738 /* Check if we have space for header and length fields:
4739 * - extension_type (2 bytes)
4740 * - extension_data_length (2 bytes)
4741 * - named_group_list_length (2 bytes)
4742 */
4743 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
4744 p += 6;
4745
4746 named_group_list = p;
4747
4748 if( group_list == NULL )
4749 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
4750
Jerry Yu1510cea2022-01-12 10:56:49 +08004751 for( ; *group_list != 0; group_list++ )
Jerry Yuba073422021-12-20 22:22:15 +08004752 {
Jerry Yu1510cea2022-01-12 10:56:49 +08004753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got supported group(%04x)", *group_list ) );
Jerry Yu63282b42022-01-11 15:43:53 +08004754
Jerry Yu1ea9d102021-12-21 13:41:49 +08004755#if defined(MBEDTLS_ECP_C)
Jerry Yu1510cea2022-01-12 10:56:49 +08004756 if( ( mbedtls_ssl_conf_is_tls13_enabled( ssl->conf ) &&
Jerry Yu3ad14ac2022-01-11 17:13:16 +08004757 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) ) ||
Jerry Yu1510cea2022-01-12 10:56:49 +08004758 ( mbedtls_ssl_conf_is_tls12_enabled( ssl->conf ) &&
Jerry Yu3ad14ac2022-01-11 17:13:16 +08004759 mbedtls_ssl_tls12_named_group_is_ecdhe( *group_list ) ) )
Jerry Yuba073422021-12-20 22:22:15 +08004760 {
Jerry Yu3ad14ac2022-01-11 17:13:16 +08004761 const mbedtls_ecp_curve_info *curve_info;
4762 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
Jerry Yub925f212022-01-12 11:17:02 +08004763 if( curve_info == NULL )
Jerry Yu3ad14ac2022-01-11 17:13:16 +08004764 continue;
Jerry Yub925f212022-01-12 11:17:02 +08004765 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
4766 MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 );
4767 p += 2;
4768 MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )",
4769 curve_info->name, *group_list ) );
Jerry Yuba073422021-12-20 22:22:15 +08004770 }
Jerry Yu63282b42022-01-11 15:43:53 +08004771#endif /* MBEDTLS_ECP_C */
4772 /* Add DHE groups here */
Jerry Yuba073422021-12-20 22:22:15 +08004773
4774 }
4775
Jerry Yu1510cea2022-01-12 10:56:49 +08004776 /* Length of named_group_list */
Jerry Yuba073422021-12-20 22:22:15 +08004777 named_group_list_len = p - named_group_list;
4778 if( named_group_list_len == 0 )
4779 {
4780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) );
4781 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4782 }
4783
4784 /* Write extension_type */
4785 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 );
4786 /* Write extension_data_length */
4787 MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 );
4788 /* Write length of named_group_list */
4789 MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 );
4790
Jerry Yu7f029d82022-01-11 11:08:53 +08004791 MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension",
4792 buf + 4, named_group_list_len + 2 );
Jerry Yuba073422021-12-20 22:22:15 +08004793
4794 *out_len = p - buf;
4795
4796#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4797 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
4798#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4799
4800 return( 0 );
4801}
4802
Jerry Yu1ea9d102021-12-21 13:41:49 +08004803#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED ||
4804 MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
4805 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Jerry Yuba073422021-12-20 22:22:15 +08004806
Jerry Yuf017ee42022-01-12 15:49:48 +08004807#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
4808/*
Jerry Yu7ddc38c2022-01-19 11:08:05 +08004809 * Function for writing a signature algorithm extension.
Jerry Yuf017ee42022-01-12 15:49:48 +08004810 *
Jerry Yu8afd6e42022-01-20 15:54:26 +08004811 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
Jerry Yu7ddc38c2022-01-19 11:08:05 +08004812 * value (TLS 1.3 RFC8446):
4813 * enum {
4814 * ....
4815 * ecdsa_secp256r1_sha256( 0x0403 ),
4816 * ecdsa_secp384r1_sha384( 0x0503 ),
4817 * ecdsa_secp521r1_sha512( 0x0603 ),
4818 * ....
4819 * } SignatureScheme;
Jerry Yuf017ee42022-01-12 15:49:48 +08004820 *
Jerry Yu7ddc38c2022-01-19 11:08:05 +08004821 * struct {
4822 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
4823 * } SignatureSchemeList;
Jerry Yuf017ee42022-01-12 15:49:48 +08004824 *
Jerry Yu8afd6e42022-01-20 15:54:26 +08004825 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
4826 * value (TLS 1.2 RFC5246):
Jerry Yu7ddc38c2022-01-19 11:08:05 +08004827 * enum {
4828 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
4829 * sha512(6), (255)
4830 * } HashAlgorithm;
4831 *
4832 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
4833 * SignatureAlgorithm;
4834 *
4835 * struct {
4836 * HashAlgorithm hash;
4837 * SignatureAlgorithm signature;
4838 * } SignatureAndHashAlgorithm;
4839 *
4840 * SignatureAndHashAlgorithm
4841 * supported_signature_algorithms<2..2^16-2>;
4842 *
4843 * The TLS 1.3 signature algorithm extension was defined to be a compatible
4844 * generalization of the TLS 1.2 signature algorithm extension.
4845 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
4846 * `SignatureScheme` field of TLS 1.3
4847 *
Jerry Yuf017ee42022-01-12 15:49:48 +08004848 */
4849int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
4850 const unsigned char *end, size_t *out_len )
4851{
4852 unsigned char *p = buf;
4853 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
4854 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
4855
4856 *out_len = 0;
4857
4858 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
4859
4860 /* Check if we have space for header and length field:
4861 * - extension_type (2 bytes)
4862 * - extension_data_length (2 bytes)
4863 * - supported_signature_algorithms_length (2 bytes)
4864 */
4865 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
4866 p += 6;
4867
4868 /*
4869 * Write supported_signature_algorithms
4870 */
4871 supported_sig_alg = p;
Jerry Yu6106fdc2022-01-12 16:36:14 +08004872 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
4873 if( sig_alg == NULL )
4874 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
4875
4876 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
Jerry Yuf017ee42022-01-12 15:49:48 +08004877 {
Jerry Yu1bab3012022-01-19 17:43:22 +08004878 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
4879 continue;
Jerry Yuf017ee42022-01-12 15:49:48 +08004880 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
4881 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
4882 p += 2;
4883 MBEDTLS_SSL_DEBUG_MSG( 3, ( "signature scheme [%x]", *sig_alg ) );
4884 }
4885
4886 /* Length of supported_signature_algorithms */
4887 supported_sig_alg_len = p - supported_sig_alg;
4888 if( supported_sig_alg_len == 0 )
4889 {
4890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
4891 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4892 }
4893
4894 /* Write extension_type */
4895 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
4896 /* Write extension_data_length */
4897 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
4898 /* Write length of supported_signature_algorithms */
4899 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
4900
4901 /* Output the total length of signature algorithms extension. */
4902 *out_len = p - buf;
4903
4904#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4905 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
4906#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4907 return( 0 );
4908}
4909#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yuc73c6182022-02-08 20:29:25 +08004910
Jerry Yuc5aef882021-12-23 20:15:02 +08004911#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4912int mbedtls_ssl_write_hostname_ext( mbedtls_ssl_context *ssl,
4913 unsigned char *buf,
4914 const unsigned char *end,
4915 size_t *olen )
4916{
4917 unsigned char *p = buf;
4918 size_t hostname_len;
4919
4920 *olen = 0;
4921
4922 if( ssl->hostname == NULL )
4923 return( 0 );
4924
4925 MBEDTLS_SSL_DEBUG_MSG( 3,
4926 ( "client hello, adding server name extension: %s",
4927 ssl->hostname ) );
4928
4929 hostname_len = strlen( ssl->hostname );
4930
4931 MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 );
4932
4933 /*
4934 * Sect. 3, RFC 6066 (TLS Extensions Definitions)
4935 *
4936 * In order to provide any of the server names, clients MAY include an
4937 * extension of type "server_name" in the (extended) client hello. The
4938 * "extension_data" field of this extension SHALL contain
4939 * "ServerNameList" where:
4940 *
4941 * struct {
4942 * NameType name_type;
4943 * select (name_type) {
4944 * case host_name: HostName;
4945 * } name;
4946 * } ServerName;
4947 *
4948 * enum {
4949 * host_name(0), (255)
4950 * } NameType;
4951 *
4952 * opaque HostName<1..2^16-1>;
4953 *
4954 * struct {
4955 * ServerName server_name_list<1..2^16-1>
4956 * } ServerNameList;
4957 *
4958 */
4959 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SERVERNAME, p, 0 );
4960 p += 2;
4961
4962 MBEDTLS_PUT_UINT16_BE( hostname_len + 5, p, 0 );
4963 p += 2;
4964
4965 MBEDTLS_PUT_UINT16_BE( hostname_len + 3, p, 0 );
4966 p += 2;
4967
4968 *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME );
4969
4970 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
4971 p += 2;
4972
4973 memcpy( p, ssl->hostname, hostname_len );
4974
4975 *olen = hostname_len + 9;
4976
4977 return( 0 );
4978}
4979#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Jerry Yuf017ee42022-01-12 15:49:48 +08004980
Jerry Yudc7bd172022-02-17 13:44:15 +08004981#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4982
4983#if defined(MBEDTLS_USE_PSA_CRYPTO)
4984
4985static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
4986 mbedtls_svc_key_id_t key,
4987 psa_algorithm_t alg,
4988 const unsigned char* seed, size_t seed_length,
4989 const unsigned char* label, size_t label_length,
4990 size_t capacity )
4991{
4992 psa_status_t status;
4993
4994 status = psa_key_derivation_setup( derivation, alg );
4995 if( status != PSA_SUCCESS )
4996 return( status );
4997
4998 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4999 {
5000 status = psa_key_derivation_input_bytes( derivation,
5001 PSA_KEY_DERIVATION_INPUT_SEED,
5002 seed, seed_length );
5003 if( status != PSA_SUCCESS )
5004 return( status );
5005
5006 if( mbedtls_svc_key_id_is_null( key ) )
5007 {
5008 status = psa_key_derivation_input_bytes(
5009 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
5010 NULL, 0 );
5011 }
5012 else
5013 {
5014 status = psa_key_derivation_input_key(
5015 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5016 }
5017 if( status != PSA_SUCCESS )
5018 return( status );
5019
5020 status = psa_key_derivation_input_bytes( derivation,
5021 PSA_KEY_DERIVATION_INPUT_LABEL,
5022 label, label_length );
5023 if( status != PSA_SUCCESS )
5024 return( status );
5025 }
5026 else
5027 {
5028 return( PSA_ERROR_NOT_SUPPORTED );
5029 }
5030
5031 status = psa_key_derivation_set_capacity( derivation, capacity );
5032 if( status != PSA_SUCCESS )
5033 return( status );
5034
5035 return( PSA_SUCCESS );
5036}
5037
5038static int tls_prf_generic( mbedtls_md_type_t md_type,
5039 const unsigned char *secret, size_t slen,
5040 const char *label,
5041 const unsigned char *random, size_t rlen,
5042 unsigned char *dstbuf, size_t dlen )
5043{
5044 psa_status_t status;
5045 psa_algorithm_t alg;
5046 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5047 psa_key_derivation_operation_t derivation =
5048 PSA_KEY_DERIVATION_OPERATION_INIT;
5049
5050 if( md_type == MBEDTLS_MD_SHA384 )
5051 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5052 else
5053 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5054
5055 /* Normally a "secret" should be long enough to be impossible to
5056 * find by brute force, and in particular should not be empty. But
5057 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5058 * and for this use case it makes sense to have a 0-length "secret".
5059 * Since the key API doesn't allow importing a key of length 0,
5060 * keep master_key=0, which setup_psa_key_derivation() understands
5061 * to mean a 0-length "secret" input. */
5062 if( slen != 0 )
5063 {
5064 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5065 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5066 psa_set_key_algorithm( &key_attributes, alg );
5067 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5068
5069 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5070 if( status != PSA_SUCCESS )
5071 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5072 }
5073
5074 status = setup_psa_key_derivation( &derivation,
5075 master_key, alg,
5076 random, rlen,
5077 (unsigned char const *) label,
5078 (size_t) strlen( label ),
5079 dlen );
5080 if( status != PSA_SUCCESS )
5081 {
5082 psa_key_derivation_abort( &derivation );
5083 psa_destroy_key( master_key );
5084 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5085 }
5086
5087 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5088 if( status != PSA_SUCCESS )
5089 {
5090 psa_key_derivation_abort( &derivation );
5091 psa_destroy_key( master_key );
5092 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5093 }
5094
5095 status = psa_key_derivation_abort( &derivation );
5096 if( status != PSA_SUCCESS )
5097 {
5098 psa_destroy_key( master_key );
5099 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5100 }
5101
5102 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5103 status = psa_destroy_key( master_key );
5104 if( status != PSA_SUCCESS )
5105 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5106
5107 return( 0 );
5108}
5109
5110#else /* MBEDTLS_USE_PSA_CRYPTO */
5111
5112static int tls_prf_generic( mbedtls_md_type_t md_type,
5113 const unsigned char *secret, size_t slen,
5114 const char *label,
5115 const unsigned char *random, size_t rlen,
5116 unsigned char *dstbuf, size_t dlen )
5117{
5118 size_t nb;
5119 size_t i, j, k, md_len;
5120 unsigned char *tmp;
5121 size_t tmp_len = 0;
5122 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5123 const mbedtls_md_info_t *md_info;
5124 mbedtls_md_context_t md_ctx;
5125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5126
5127 mbedtls_md_init( &md_ctx );
5128
5129 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5130 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5131
5132 md_len = mbedtls_md_get_size( md_info );
5133
5134 tmp_len = md_len + strlen( label ) + rlen;
5135 tmp = mbedtls_calloc( 1, tmp_len );
5136 if( tmp == NULL )
5137 {
5138 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5139 goto exit;
5140 }
5141
5142 nb = strlen( label );
5143 memcpy( tmp + md_len, label, nb );
5144 memcpy( tmp + md_len + nb, random, rlen );
5145 nb += rlen;
5146
5147 /*
5148 * Compute P_<hash>(secret, label + random)[0..dlen]
5149 */
5150 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5151 goto exit;
5152
5153 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5154 if( ret != 0 )
5155 goto exit;
5156 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
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 for( i = 0; i < dlen; i += md_len )
5164 {
5165 ret = mbedtls_md_hmac_reset ( &md_ctx );
5166 if( ret != 0 )
5167 goto exit;
5168 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5169 if( ret != 0 )
5170 goto exit;
5171 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5172 if( ret != 0 )
5173 goto exit;
5174
5175 ret = mbedtls_md_hmac_reset ( &md_ctx );
5176 if( ret != 0 )
5177 goto exit;
5178 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5179 if( ret != 0 )
5180 goto exit;
5181 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5182 if( ret != 0 )
5183 goto exit;
5184
5185 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5186
5187 for( j = 0; j < k; j++ )
5188 dstbuf[i + j] = h_i[j];
5189 }
5190
5191exit:
5192 mbedtls_md_free( &md_ctx );
5193
5194 mbedtls_platform_zeroize( tmp, tmp_len );
5195 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5196
5197 mbedtls_free( tmp );
5198
5199 return( ret );
5200}
5201#endif /* MBEDTLS_USE_PSA_CRYPTO */
5202
5203#if defined(MBEDTLS_SHA256_C)
5204static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5205 const char *label,
5206 const unsigned char *random, size_t rlen,
5207 unsigned char *dstbuf, size_t dlen )
5208{
5209 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5210 label, random, rlen, dstbuf, dlen ) );
5211}
5212#endif /* MBEDTLS_SHA256_C */
5213
5214#if defined(MBEDTLS_SHA384_C)
5215static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5216 const char *label,
5217 const unsigned char *random, size_t rlen,
5218 unsigned char *dstbuf, size_t dlen )
5219{
5220 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5221 label, random, rlen, dstbuf, dlen ) );
5222}
5223#endif /* MBEDTLS_SHA384_C */
5224
Jerry Yuf009d862022-02-17 14:01:37 +08005225/*
5226 * Set appropriate PRF function and other SSL / TLS1.2 functions
5227 *
5228 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005229 * - hash associated with the ciphersuite (only used by TLS 1.2)
5230 *
5231 * Outputs:
5232 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5233 */
5234static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005235 mbedtls_md_type_t hash )
5236{
Jerry Yuf009d862022-02-17 14:01:37 +08005237#if defined(MBEDTLS_SHA384_C)
Ronald Cron81591aa2022-03-07 09:05:51 +01005238 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005239 {
5240 handshake->tls_prf = tls_prf_sha384;
5241 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5242 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5243 }
5244 else
5245#endif
5246#if defined(MBEDTLS_SHA256_C)
Jerry Yuf009d862022-02-17 14:01:37 +08005247 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005248 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005249 handshake->tls_prf = tls_prf_sha256;
5250 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5251 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5252 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005253#else
Jerry Yuf009d862022-02-17 14:01:37 +08005254 {
Jerry Yuf009d862022-02-17 14:01:37 +08005255 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005256 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005257 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5258 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005259#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005260
5261 return( 0 );
5262}
Jerry Yud6ab2352022-02-17 14:03:43 +08005263
5264#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
5265 defined(MBEDTLS_USE_PSA_CRYPTO)
5266static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
5267{
5268 if( ssl->conf->f_psk != NULL )
5269 {
5270 /* If we've used a callback to select the PSK,
5271 * the static configuration is irrelevant. */
5272 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
5273 return( 1 );
5274
5275 return( 0 );
5276 }
5277
5278 if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) )
5279 return( 1 );
5280
5281 return( 0 );
5282}
5283#endif /* MBEDTLS_USE_PSA_CRYPTO &&
5284 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5285
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005286/*
5287 * Compute master secret if needed
5288 *
5289 * Parameters:
5290 * [in/out] handshake
5291 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5292 * (PSA-PSK) ciphersuite_info, psk_opaque
5293 * [out] premaster (cleared)
5294 * [out] master
5295 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5296 * debug: conf->f_dbg, conf->p_dbg
5297 * EMS: passed to calc_verify (debug + session_negotiate)
5298 * PSA-PSA: minor_ver, conf
5299 */
5300static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5301 unsigned char *master,
5302 const mbedtls_ssl_context *ssl )
5303{
5304 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5305
5306 /* cf. RFC 5246, Section 8.1:
5307 * "The master secret is always exactly 48 bytes in length." */
5308 size_t const master_secret_len = 48;
5309
5310#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5311 unsigned char session_hash[48];
5312#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5313
5314 /* The label for the KDF used for key expansion.
5315 * This is either "master secret" or "extended master secret"
5316 * depending on whether the Extended Master Secret extension
5317 * is used. */
5318 char const *lbl = "master secret";
5319
5320 /* The salt for the KDF used for key expansion.
5321 * - If the Extended Master Secret extension is not used,
5322 * this is ClientHello.Random + ServerHello.Random
5323 * (see Sect. 8.1 in RFC 5246).
5324 * - If the Extended Master Secret extension is used,
5325 * this is the transcript of the handshake so far.
5326 * (see Sect. 4 in RFC 7627). */
5327 unsigned char const *salt = handshake->randbytes;
5328 size_t salt_len = 64;
5329
5330#if !defined(MBEDTLS_DEBUG_C) && \
5331 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5332 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5333 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5334 ssl = NULL; /* make sure we don't use it except for those cases */
5335 (void) ssl;
5336#endif
5337
5338 if( handshake->resume != 0 )
5339 {
5340 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5341 return( 0 );
5342 }
5343
5344#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5345 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5346 {
5347 lbl = "extended master secret";
5348 salt = session_hash;
5349 handshake->calc_verify( ssl, session_hash, &salt_len );
5350
5351 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
5352 session_hash, salt_len );
5353 }
5354#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
5355
5356#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
5357 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5358 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
5359 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
5360 ssl_use_opaque_psk( ssl ) == 1 )
5361 {
5362 /* Perform PSK-to-MS expansion in a single step. */
5363 psa_status_t status;
5364 psa_algorithm_t alg;
5365 mbedtls_svc_key_id_t psk;
5366 psa_key_derivation_operation_t derivation =
5367 PSA_KEY_DERIVATION_OPERATION_INIT;
5368 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5369
5370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5371
5372 psk = mbedtls_ssl_get_opaque_psk( ssl );
5373
5374 if( hash_alg == MBEDTLS_MD_SHA384 )
5375 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5376 else
5377 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5378
5379 status = setup_psa_key_derivation( &derivation, psk, alg,
5380 salt, salt_len,
5381 (unsigned char const *) lbl,
5382 (size_t) strlen( lbl ),
5383 master_secret_len );
5384 if( status != PSA_SUCCESS )
5385 {
5386 psa_key_derivation_abort( &derivation );
5387 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5388 }
5389
5390 status = psa_key_derivation_output_bytes( &derivation,
5391 master,
5392 master_secret_len );
5393 if( status != PSA_SUCCESS )
5394 {
5395 psa_key_derivation_abort( &derivation );
5396 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5397 }
5398
5399 status = psa_key_derivation_abort( &derivation );
5400 if( status != PSA_SUCCESS )
5401 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5402 }
5403 else
5404#endif
5405 {
5406 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
5407 lbl, salt, salt_len,
5408 master,
5409 master_secret_len );
5410 if( ret != 0 )
5411 {
5412 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5413 return( ret );
5414 }
5415
5416 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5417 handshake->premaster,
5418 handshake->pmslen );
5419
5420 mbedtls_platform_zeroize( handshake->premaster,
5421 sizeof(handshake->premaster) );
5422 }
5423
5424 return( 0 );
5425}
5426
Jerry Yud62f87e2022-02-17 14:09:02 +08005427int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5428{
5429 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5430 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5431 ssl->handshake->ciphersuite_info;
5432
5433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5434
5435 /* Set PRF, calc_verify and calc_finished function pointers */
5436 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005437 ciphersuite_info->mac );
5438 if( ret != 0 )
5439 {
5440 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5441 return( ret );
5442 }
5443
5444 /* Compute master secret if needed */
5445 ret = ssl_compute_master( ssl->handshake,
5446 ssl->session_negotiate->master,
5447 ssl );
5448 if( ret != 0 )
5449 {
5450 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5451 return( ret );
5452 }
5453
5454 /* Swap the client and server random values:
5455 * - MS derivation wanted client+server (RFC 5246 8.1)
5456 * - key derivation wants server+client (RFC 5246 6.3) */
5457 {
5458 unsigned char tmp[64];
5459 memcpy( tmp, ssl->handshake->randbytes, 64 );
5460 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5461 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5462 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5463 }
5464
5465 /* Populate transform structure */
5466 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5467 ssl->session_negotiate->ciphersuite,
5468 ssl->session_negotiate->master,
5469#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
5470 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5471 ssl->session_negotiate->encrypt_then_mac,
5472#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
5473 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
5474 ssl->handshake->tls_prf,
5475 ssl->handshake->randbytes,
5476 ssl->minor_ver,
5477 ssl->conf->endpoint,
5478 ssl );
5479 if( ret != 0 )
5480 {
5481 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5482 return( ret );
5483 }
5484
5485 /* We no longer need Server/ClientHello.random values */
5486 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5487 sizeof( ssl->handshake->randbytes ) );
5488
5489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5490
5491 return( 0 );
5492}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005493
5494#if defined(MBEDTLS_SHA256_C)
5495void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5496 unsigned char *hash,
5497 size_t *hlen )
5498{
5499#if defined(MBEDTLS_USE_PSA_CRYPTO)
5500 size_t hash_size;
5501 psa_status_t status;
5502 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5503
5504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5505 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5506 if( status != PSA_SUCCESS )
5507 {
5508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5509 return;
5510 }
5511
5512 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5513 if( status != PSA_SUCCESS )
5514 {
5515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5516 return;
5517 }
5518
5519 *hlen = 32;
5520 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5522#else
5523 mbedtls_sha256_context sha256;
5524
5525 mbedtls_sha256_init( &sha256 );
5526
5527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5528
5529 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5530 mbedtls_sha256_finish( &sha256, hash );
5531
5532 *hlen = 32;
5533
5534 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5536
5537 mbedtls_sha256_free( &sha256 );
5538#endif /* MBEDTLS_USE_PSA_CRYPTO */
5539 return;
5540}
5541#endif /* MBEDTLS_SHA256_C */
5542
Jerry Yuc1cb3842022-02-17 14:13:48 +08005543#if defined(MBEDTLS_SHA384_C)
5544void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5545 unsigned char *hash,
5546 size_t *hlen )
5547{
5548#if defined(MBEDTLS_USE_PSA_CRYPTO)
5549 size_t hash_size;
5550 psa_status_t status;
5551 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5552
5553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5554 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5555 if( status != PSA_SUCCESS )
5556 {
5557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5558 return;
5559 }
5560
5561 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5562 if( status != PSA_SUCCESS )
5563 {
5564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5565 return;
5566 }
5567
5568 *hlen = 48;
5569 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5571#else
5572 mbedtls_sha512_context sha512;
5573
5574 mbedtls_sha512_init( &sha512 );
5575
5576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
5577
5578 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
5579 mbedtls_sha512_finish( &sha512, hash );
5580
5581 *hlen = 48;
5582
5583 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5584 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5585
5586 mbedtls_sha512_free( &sha512 );
5587#endif /* MBEDTLS_USE_PSA_CRYPTO */
5588 return;
5589}
5590#endif /* MBEDTLS_SHA384_C */
5591
Jerry Yuce3dca42022-02-17 14:16:37 +08005592#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
5593int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
5594{
5595 unsigned char *p = ssl->handshake->premaster;
5596 unsigned char *end = p + sizeof( ssl->handshake->premaster );
5597 const unsigned char *psk = NULL;
5598 size_t psk_len = 0;
5599
5600 if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len )
5601 == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
5602 {
5603 /*
5604 * This should never happen because the existence of a PSK is always
5605 * checked before calling this function
5606 */
5607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5608 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5609 }
5610
5611 /*
5612 * PMS = struct {
5613 * opaque other_secret<0..2^16-1>;
5614 * opaque psk<0..2^16-1>;
5615 * };
5616 * with "other_secret" depending on the particular key exchange
5617 */
5618#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5619 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
5620 {
5621 if( end - p < 2 )
5622 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5623
5624 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5625 p += 2;
5626
5627 if( end < p || (size_t)( end - p ) < psk_len )
5628 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5629
5630 memset( p, 0, psk_len );
5631 p += psk_len;
5632 }
5633 else
5634#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5635#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
5636 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5637 {
5638 /*
5639 * other_secret already set by the ClientKeyExchange message,
5640 * and is 48 bytes long
5641 */
5642 if( end - p < 2 )
5643 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5644
5645 *p++ = 0;
5646 *p++ = 48;
5647 p += 48;
5648 }
5649 else
5650#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
5651#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
5652 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5653 {
5654 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5655 size_t len;
5656
5657 /* Write length only when we know the actual value */
5658 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
5659 p + 2, end - ( p + 2 ), &len,
5660 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5661 {
5662 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
5663 return( ret );
5664 }
5665 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
5666 p += 2 + len;
5667
5668 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
5669 }
5670 else
5671#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
5672#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
5673 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
5674 {
5675 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5676 size_t zlen;
5677
5678 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
5679 p + 2, end - ( p + 2 ),
5680 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5681 {
5682 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
5683 return( ret );
5684 }
5685
5686 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
5687 p += 2 + zlen;
5688
5689 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
5690 MBEDTLS_DEBUG_ECDH_Z );
5691 }
5692 else
5693#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
5694 {
5695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5696 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5697 }
5698
5699 /* opaque psk<0..2^16-1>; */
5700 if( end - p < 2 )
5701 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5702
5703 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5704 p += 2;
5705
5706 if( end < p || (size_t)( end - p ) < psk_len )
5707 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5708
5709 memcpy( p, psk, psk_len );
5710 p += psk_len;
5711
5712 ssl->handshake->pmslen = p - ssl->handshake->premaster;
5713
5714 return( 0 );
5715}
5716#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08005717
5718#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
5719static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
5720
5721#if defined(MBEDTLS_SSL_PROTO_DTLS)
5722int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
5723{
5724 /* If renegotiation is not enforced, retransmit until we would reach max
5725 * timeout if we were using the usual handshake doubling scheme */
5726 if( ssl->conf->renego_max_records < 0 )
5727 {
5728 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
5729 unsigned char doublings = 1;
5730
5731 while( ratio != 0 )
5732 {
5733 ++doublings;
5734 ratio >>= 1;
5735 }
5736
5737 if( ++ssl->renego_records_seen > doublings )
5738 {
5739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
5740 return( 0 );
5741 }
5742 }
5743
5744 return( ssl_write_hello_request( ssl ) );
5745}
5746#endif
5747#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08005748
Jerry Yud9526692022-02-17 14:23:47 +08005749/*
5750 * Handshake functions
5751 */
5752#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5753/* No certificate support -> dummy functions */
5754int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
5755{
5756 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5757 ssl->handshake->ciphersuite_info;
5758
5759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
5760
5761 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5762 {
5763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5764 ssl->state++;
5765 return( 0 );
5766 }
5767
5768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5769 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5770}
5771
5772int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5773{
5774 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5775 ssl->handshake->ciphersuite_info;
5776
5777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5778
5779 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5780 {
5781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5782 ssl->state++;
5783 return( 0 );
5784 }
5785
5786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5787 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5788}
5789
5790#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5791/* Some certificate support -> implement write and parse */
5792
5793int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
5794{
5795 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
5796 size_t i, n;
5797 const mbedtls_x509_crt *crt;
5798 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5799 ssl->handshake->ciphersuite_info;
5800
5801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
5802
5803 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5804 {
5805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5806 ssl->state++;
5807 return( 0 );
5808 }
5809
5810#if defined(MBEDTLS_SSL_CLI_C)
5811 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5812 {
5813 if( ssl->handshake->client_auth == 0 )
5814 {
5815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
5816 ssl->state++;
5817 return( 0 );
5818 }
5819 }
5820#endif /* MBEDTLS_SSL_CLI_C */
5821#if defined(MBEDTLS_SSL_SRV_C)
5822 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
5823 {
5824 if( mbedtls_ssl_own_cert( ssl ) == NULL )
5825 {
5826 /* Should never happen because we shouldn't have picked the
5827 * ciphersuite if we don't have a certificate. */
5828 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5829 }
5830 }
5831#endif
5832
5833 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
5834
5835 /*
5836 * 0 . 0 handshake type
5837 * 1 . 3 handshake length
5838 * 4 . 6 length of all certs
5839 * 7 . 9 length of cert. 1
5840 * 10 . n-1 peer certificate
5841 * n . n+2 length of cert. 2
5842 * n+3 . ... upper level cert, etc.
5843 */
5844 i = 7;
5845 crt = mbedtls_ssl_own_cert( ssl );
5846
5847 while( crt != NULL )
5848 {
5849 n = crt->raw.len;
5850 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
5851 {
5852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
5853 " > %" MBEDTLS_PRINTF_SIZET,
5854 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
5855 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5856 }
5857
5858 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
5859 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
5860 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
5861
5862 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5863 i += n; crt = crt->next;
5864 }
5865
5866 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
5867 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
5868 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
5869
5870 ssl->out_msglen = i;
5871 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5872 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
5873
5874 ssl->state++;
5875
5876 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
5877 {
5878 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
5879 return( ret );
5880 }
5881
5882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
5883
5884 return( ret );
5885}
5886
5887#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5888
5889#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5890static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5891 unsigned char *crt_buf,
5892 size_t crt_buf_len )
5893{
5894 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5895
5896 if( peer_crt == NULL )
5897 return( -1 );
5898
5899 if( peer_crt->raw.len != crt_buf_len )
5900 return( -1 );
5901
5902 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
5903}
5904#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5905static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5906 unsigned char *crt_buf,
5907 size_t crt_buf_len )
5908{
5909 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5910 unsigned char const * const peer_cert_digest =
5911 ssl->session->peer_cert_digest;
5912 mbedtls_md_type_t const peer_cert_digest_type =
5913 ssl->session->peer_cert_digest_type;
5914 mbedtls_md_info_t const * const digest_info =
5915 mbedtls_md_info_from_type( peer_cert_digest_type );
5916 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
5917 size_t digest_len;
5918
5919 if( peer_cert_digest == NULL || digest_info == NULL )
5920 return( -1 );
5921
5922 digest_len = mbedtls_md_get_size( digest_info );
5923 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
5924 return( -1 );
5925
5926 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
5927 if( ret != 0 )
5928 return( -1 );
5929
5930 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
5931}
5932#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5933#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5934
5935/*
5936 * Once the certificate message is read, parse it into a cert chain and
5937 * perform basic checks, but leave actual verification to the caller
5938 */
5939static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5940 mbedtls_x509_crt *chain )
5941{
5942 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5943#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5944 int crt_cnt=0;
5945#endif
5946 size_t i, n;
5947 uint8_t alert;
5948
5949 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5950 {
5951 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5952 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5953 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5954 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5955 }
5956
5957 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
5958 {
5959 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5960 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5961 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5962 }
5963
5964 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
5965 {
5966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5967 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5968 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5969 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5970 }
5971
5972 i = mbedtls_ssl_hs_hdr_len( ssl );
5973
5974 /*
5975 * Same message structure as in mbedtls_ssl_write_certificate()
5976 */
5977 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
5978
5979 if( ssl->in_msg[i] != 0 ||
5980 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
5981 {
5982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5983 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5984 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5985 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5986 }
5987
5988 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5989 i += 3;
5990
5991 /* Iterate through and parse the CRTs in the provided chain. */
5992 while( i < ssl->in_hslen )
5993 {
5994 /* Check that there's room for the next CRT's length fields. */
5995 if ( i + 3 > ssl->in_hslen ) {
5996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5997 mbedtls_ssl_send_alert_message( ssl,
5998 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5999 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6000 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6001 }
6002 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6003 * anything beyond 2**16 ~ 64K. */
6004 if( ssl->in_msg[i] != 0 )
6005 {
6006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6007 mbedtls_ssl_send_alert_message( ssl,
6008 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6009 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6010 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6011 }
6012
6013 /* Read length of the next CRT in the chain. */
6014 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6015 | (unsigned int) ssl->in_msg[i + 2];
6016 i += 3;
6017
6018 if( n < 128 || i + n > ssl->in_hslen )
6019 {
6020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6021 mbedtls_ssl_send_alert_message( ssl,
6022 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6023 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6024 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6025 }
6026
6027 /* Check if we're handling the first CRT in the chain. */
6028#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6029 if( crt_cnt++ == 0 &&
6030 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6031 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6032 {
6033 /* During client-side renegotiation, check that the server's
6034 * end-CRTs hasn't changed compared to the initial handshake,
6035 * mitigating the triple handshake attack. On success, reuse
6036 * the original end-CRT instead of parsing it again. */
6037 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6038 if( ssl_check_peer_crt_unchanged( ssl,
6039 &ssl->in_msg[i],
6040 n ) != 0 )
6041 {
6042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6043 mbedtls_ssl_send_alert_message( ssl,
6044 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6045 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6046 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6047 }
6048
6049 /* Now we can safely free the original chain. */
6050 ssl_clear_peer_cert( ssl->session );
6051 }
6052#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6053
6054 /* Parse the next certificate in the chain. */
6055#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6056 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6057#else
6058 /* If we don't need to store the CRT chain permanently, parse
6059 * it in-place from the input buffer instead of making a copy. */
6060 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6061#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6062 switch( ret )
6063 {
6064 case 0: /*ok*/
6065 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6066 /* Ignore certificate with an unknown algorithm: maybe a
6067 prior certificate was already trusted. */
6068 break;
6069
6070 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6071 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6072 goto crt_parse_der_failed;
6073
6074 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6075 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6076 goto crt_parse_der_failed;
6077
6078 default:
6079 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6080 crt_parse_der_failed:
6081 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6082 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6083 return( ret );
6084 }
6085
6086 i += n;
6087 }
6088
6089 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6090 return( 0 );
6091}
6092
6093#if defined(MBEDTLS_SSL_SRV_C)
6094static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6095{
6096 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6097 return( -1 );
6098
6099 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6100 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6101 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6102 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6103 {
6104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6105 return( 0 );
6106 }
6107 return( -1 );
6108}
6109#endif /* MBEDTLS_SSL_SRV_C */
6110
6111/* Check if a certificate message is expected.
6112 * Return either
6113 * - SSL_CERTIFICATE_EXPECTED, or
6114 * - SSL_CERTIFICATE_SKIP
6115 * indicating whether a Certificate message is expected or not.
6116 */
6117#define SSL_CERTIFICATE_EXPECTED 0
6118#define SSL_CERTIFICATE_SKIP 1
6119static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6120 int authmode )
6121{
6122 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6123 ssl->handshake->ciphersuite_info;
6124
6125 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6126 return( SSL_CERTIFICATE_SKIP );
6127
6128#if defined(MBEDTLS_SSL_SRV_C)
6129 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6130 {
6131 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6132 return( SSL_CERTIFICATE_SKIP );
6133
6134 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6135 {
6136 ssl->session_negotiate->verify_result =
6137 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6138 return( SSL_CERTIFICATE_SKIP );
6139 }
6140 }
6141#else
6142 ((void) authmode);
6143#endif /* MBEDTLS_SSL_SRV_C */
6144
6145 return( SSL_CERTIFICATE_EXPECTED );
6146}
6147
6148static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6149 int authmode,
6150 mbedtls_x509_crt *chain,
6151 void *rs_ctx )
6152{
6153 int ret = 0;
6154 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6155 ssl->handshake->ciphersuite_info;
6156 int have_ca_chain = 0;
6157
6158 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6159 void *p_vrfy;
6160
6161 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6162 return( 0 );
6163
6164 if( ssl->f_vrfy != NULL )
6165 {
6166 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6167 f_vrfy = ssl->f_vrfy;
6168 p_vrfy = ssl->p_vrfy;
6169 }
6170 else
6171 {
6172 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6173 f_vrfy = ssl->conf->f_vrfy;
6174 p_vrfy = ssl->conf->p_vrfy;
6175 }
6176
6177 /*
6178 * Main check: verify certificate
6179 */
6180#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6181 if( ssl->conf->f_ca_cb != NULL )
6182 {
6183 ((void) rs_ctx);
6184 have_ca_chain = 1;
6185
6186 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6187 ret = mbedtls_x509_crt_verify_with_ca_cb(
6188 chain,
6189 ssl->conf->f_ca_cb,
6190 ssl->conf->p_ca_cb,
6191 ssl->conf->cert_profile,
6192 ssl->hostname,
6193 &ssl->session_negotiate->verify_result,
6194 f_vrfy, p_vrfy );
6195 }
6196 else
6197#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6198 {
6199 mbedtls_x509_crt *ca_chain;
6200 mbedtls_x509_crl *ca_crl;
6201
6202#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6203 if( ssl->handshake->sni_ca_chain != NULL )
6204 {
6205 ca_chain = ssl->handshake->sni_ca_chain;
6206 ca_crl = ssl->handshake->sni_ca_crl;
6207 }
6208 else
6209#endif
6210 {
6211 ca_chain = ssl->conf->ca_chain;
6212 ca_crl = ssl->conf->ca_crl;
6213 }
6214
6215 if( ca_chain != NULL )
6216 have_ca_chain = 1;
6217
6218 ret = mbedtls_x509_crt_verify_restartable(
6219 chain,
6220 ca_chain, ca_crl,
6221 ssl->conf->cert_profile,
6222 ssl->hostname,
6223 &ssl->session_negotiate->verify_result,
6224 f_vrfy, p_vrfy, rs_ctx );
6225 }
6226
6227 if( ret != 0 )
6228 {
6229 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6230 }
6231
6232#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6233 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6234 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6235#endif
6236
6237 /*
6238 * Secondary checks: always done, but change 'ret' only if it was 0
6239 */
6240
6241#if defined(MBEDTLS_ECP_C)
6242 {
6243 const mbedtls_pk_context *pk = &chain->pk;
6244
6245 /* If certificate uses an EC key, make sure the curve is OK */
6246 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6247 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6248 {
6249 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6250
6251 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6252 if( ret == 0 )
6253 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6254 }
6255 }
6256#endif /* MBEDTLS_ECP_C */
6257
6258 if( mbedtls_ssl_check_cert_usage( chain,
6259 ciphersuite_info,
6260 ! ssl->conf->endpoint,
6261 &ssl->session_negotiate->verify_result ) != 0 )
6262 {
6263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6264 if( ret == 0 )
6265 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6266 }
6267
6268 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6269 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6270 * with details encoded in the verification flags. All other kinds
6271 * of error codes, including those from the user provided f_vrfy
6272 * functions, are treated as fatal and lead to a failure of
6273 * ssl_parse_certificate even if verification was optional. */
6274 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6275 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6276 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6277 {
6278 ret = 0;
6279 }
6280
6281 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6282 {
6283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6284 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6285 }
6286
6287 if( ret != 0 )
6288 {
6289 uint8_t alert;
6290
6291 /* The certificate may have been rejected for several reasons.
6292 Pick one and send the corresponding alert. Which alert to send
6293 may be a subject of debate in some cases. */
6294 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6295 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6296 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6297 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6298 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6299 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6300 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6301 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6302 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6303 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6304 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6305 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6306 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6307 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6308 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6309 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6310 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6311 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6312 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6313 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6314 else
6315 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6316 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6317 alert );
6318 }
6319
6320#if defined(MBEDTLS_DEBUG_C)
6321 if( ssl->session_negotiate->verify_result != 0 )
6322 {
6323 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6324 (unsigned int) ssl->session_negotiate->verify_result ) );
6325 }
6326 else
6327 {
6328 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6329 }
6330#endif /* MBEDTLS_DEBUG_C */
6331
6332 return( ret );
6333}
6334
6335#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6336static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6337 unsigned char *start, size_t len )
6338{
6339 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6340 /* Remember digest of the peer's end-CRT. */
6341 ssl->session_negotiate->peer_cert_digest =
6342 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6343 if( ssl->session_negotiate->peer_cert_digest == NULL )
6344 {
6345 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6346 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6347 mbedtls_ssl_send_alert_message( ssl,
6348 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6349 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6350
6351 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6352 }
6353
6354 ret = mbedtls_md( mbedtls_md_info_from_type(
6355 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6356 start, len,
6357 ssl->session_negotiate->peer_cert_digest );
6358
6359 ssl->session_negotiate->peer_cert_digest_type =
6360 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6361 ssl->session_negotiate->peer_cert_digest_len =
6362 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6363
6364 return( ret );
6365}
6366
6367static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6368 unsigned char *start, size_t len )
6369{
6370 unsigned char *end = start + len;
6371 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6372
6373 /* Make a copy of the peer's raw public key. */
6374 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6375 ret = mbedtls_pk_parse_subpubkey( &start, end,
6376 &ssl->handshake->peer_pubkey );
6377 if( ret != 0 )
6378 {
6379 /* We should have parsed the public key before. */
6380 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6381 }
6382
6383 return( 0 );
6384}
6385#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6386
6387int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6388{
6389 int ret = 0;
6390 int crt_expected;
6391#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6392 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6393 ? ssl->handshake->sni_authmode
6394 : ssl->conf->authmode;
6395#else
6396 const int authmode = ssl->conf->authmode;
6397#endif
6398 void *rs_ctx = NULL;
6399 mbedtls_x509_crt *chain = NULL;
6400
6401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6402
6403 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6404 if( crt_expected == SSL_CERTIFICATE_SKIP )
6405 {
6406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6407 goto exit;
6408 }
6409
6410#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6411 if( ssl->handshake->ecrs_enabled &&
6412 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6413 {
6414 chain = ssl->handshake->ecrs_peer_cert;
6415 ssl->handshake->ecrs_peer_cert = NULL;
6416 goto crt_verify;
6417 }
6418#endif
6419
6420 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6421 {
6422 /* mbedtls_ssl_read_record may have sent an alert already. We
6423 let it decide whether to alert. */
6424 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6425 goto exit;
6426 }
6427
6428#if defined(MBEDTLS_SSL_SRV_C)
6429 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6430 {
6431 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6432
6433 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6434 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6435
6436 goto exit;
6437 }
6438#endif /* MBEDTLS_SSL_SRV_C */
6439
6440 /* Clear existing peer CRT structure in case we tried to
6441 * reuse a session but it failed, and allocate a new one. */
6442 ssl_clear_peer_cert( ssl->session_negotiate );
6443
6444 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6445 if( chain == NULL )
6446 {
6447 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6448 sizeof( mbedtls_x509_crt ) ) );
6449 mbedtls_ssl_send_alert_message( ssl,
6450 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6451 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6452
6453 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6454 goto exit;
6455 }
6456 mbedtls_x509_crt_init( chain );
6457
6458 ret = ssl_parse_certificate_chain( ssl, chain );
6459 if( ret != 0 )
6460 goto exit;
6461
6462#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6463 if( ssl->handshake->ecrs_enabled)
6464 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6465
6466crt_verify:
6467 if( ssl->handshake->ecrs_enabled)
6468 rs_ctx = &ssl->handshake->ecrs_ctx;
6469#endif
6470
6471 ret = ssl_parse_certificate_verify( ssl, authmode,
6472 chain, rs_ctx );
6473 if( ret != 0 )
6474 goto exit;
6475
6476#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6477 {
6478 unsigned char *crt_start, *pk_start;
6479 size_t crt_len, pk_len;
6480
6481 /* We parse the CRT chain without copying, so
6482 * these pointers point into the input buffer,
6483 * and are hence still valid after freeing the
6484 * CRT chain. */
6485
6486 crt_start = chain->raw.p;
6487 crt_len = chain->raw.len;
6488
6489 pk_start = chain->pk_raw.p;
6490 pk_len = chain->pk_raw.len;
6491
6492 /* Free the CRT structures before computing
6493 * digest and copying the peer's public key. */
6494 mbedtls_x509_crt_free( chain );
6495 mbedtls_free( chain );
6496 chain = NULL;
6497
6498 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6499 if( ret != 0 )
6500 goto exit;
6501
6502 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6503 if( ret != 0 )
6504 goto exit;
6505 }
6506#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6507 /* Pass ownership to session structure. */
6508 ssl->session_negotiate->peer_cert = chain;
6509 chain = NULL;
6510#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6511
6512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6513
6514exit:
6515
6516 if( ret == 0 )
6517 ssl->state++;
6518
6519#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6520 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6521 {
6522 ssl->handshake->ecrs_peer_cert = chain;
6523 chain = NULL;
6524 }
6525#endif
6526
6527 if( chain != NULL )
6528 {
6529 mbedtls_x509_crt_free( chain );
6530 mbedtls_free( chain );
6531 }
6532
6533 return( ret );
6534}
6535#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6536
Jerry Yu615bd6f2022-02-17 14:25:15 +08006537#if defined(MBEDTLS_SHA256_C)
6538static void ssl_calc_finished_tls_sha256(
6539 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6540{
6541 int len = 12;
6542 const char *sender;
6543 unsigned char padbuf[32];
6544#if defined(MBEDTLS_USE_PSA_CRYPTO)
6545 size_t hash_size;
6546 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
6547 psa_status_t status;
6548#else
6549 mbedtls_sha256_context sha256;
6550#endif
6551
6552 mbedtls_ssl_session *session = ssl->session_negotiate;
6553 if( !session )
6554 session = ssl->session;
6555
6556 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6557 ? "client finished"
6558 : "server finished";
6559
6560#if defined(MBEDTLS_USE_PSA_CRYPTO)
6561 sha256_psa = psa_hash_operation_init();
6562
6563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6564
6565 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6566 if( status != PSA_SUCCESS )
6567 {
6568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6569 return;
6570 }
6571
6572 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6573 if( status != PSA_SUCCESS )
6574 {
6575 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6576 return;
6577 }
6578 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6579#else
6580
6581 mbedtls_sha256_init( &sha256 );
6582
6583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
6584
6585 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6586
6587 /*
6588 * TLSv1.2:
6589 * hash = PRF( master, finished_label,
6590 * Hash( handshake ) )[0.11]
6591 */
6592
6593#if !defined(MBEDTLS_SHA256_ALT)
6594 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
6595 sha256.state, sizeof( sha256.state ) );
6596#endif
6597
6598 mbedtls_sha256_finish( &sha256, padbuf );
6599 mbedtls_sha256_free( &sha256 );
6600#endif /* MBEDTLS_USE_PSA_CRYPTO */
6601
6602 ssl->handshake->tls_prf( session->master, 48, sender,
6603 padbuf, 32, buf, len );
6604
6605 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6606
6607 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6608
6609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6610}
6611#endif /* MBEDTLS_SHA256_C */
6612
Jerry Yub7ba49e2022-02-17 14:25:53 +08006613
6614#if defined(MBEDTLS_SHA384_C)
6615
6616static void ssl_calc_finished_tls_sha384(
6617 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6618{
6619 int len = 12;
6620 const char *sender;
6621 unsigned char padbuf[48];
6622#if defined(MBEDTLS_USE_PSA_CRYPTO)
6623 size_t hash_size;
6624 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
6625 psa_status_t status;
6626#else
6627 mbedtls_sha512_context sha512;
6628#endif
6629
6630 mbedtls_ssl_session *session = ssl->session_negotiate;
6631 if( !session )
6632 session = ssl->session;
6633
6634 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6635 ? "client finished"
6636 : "server finished";
6637
6638#if defined(MBEDTLS_USE_PSA_CRYPTO)
6639 sha384_psa = psa_hash_operation_init();
6640
6641 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6642
6643 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6644 if( status != PSA_SUCCESS )
6645 {
6646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6647 return;
6648 }
6649
6650 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
6651 if( status != PSA_SUCCESS )
6652 {
6653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6654 return;
6655 }
6656 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
6657#else
6658 mbedtls_sha512_init( &sha512 );
6659
6660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
6661
6662 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
6663
6664 /*
6665 * TLSv1.2:
6666 * hash = PRF( master, finished_label,
6667 * Hash( handshake ) )[0.11]
6668 */
6669
6670#if !defined(MBEDTLS_SHA512_ALT)
6671 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6672 sha512.state, sizeof( sha512.state ) );
6673#endif
6674 mbedtls_sha512_finish( &sha512, padbuf );
6675
6676 mbedtls_sha512_free( &sha512 );
6677#endif
6678
6679 ssl->handshake->tls_prf( session->master, 48, sender,
6680 padbuf, 48, buf, len );
6681
6682 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6683
6684 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6685
6686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6687}
6688#endif /* MBEDTLS_SHA384_C */
6689
Jerry Yuaef00152022-02-17 14:27:31 +08006690void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
6691{
6692 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
6693
6694 /*
6695 * Free our handshake params
6696 */
6697 mbedtls_ssl_handshake_free( ssl );
6698 mbedtls_free( ssl->handshake );
6699 ssl->handshake = NULL;
6700
6701 /*
6702 * Free the previous transform and swith in the current one
6703 */
6704 if( ssl->transform )
6705 {
6706 mbedtls_ssl_transform_free( ssl->transform );
6707 mbedtls_free( ssl->transform );
6708 }
6709 ssl->transform = ssl->transform_negotiate;
6710 ssl->transform_negotiate = NULL;
6711
6712 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
6713}
6714
Jerry Yu2a9fff52022-02-17 14:28:51 +08006715void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
6716{
6717 int resume = ssl->handshake->resume;
6718
6719 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
6720
6721#if defined(MBEDTLS_SSL_RENEGOTIATION)
6722 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6723 {
6724 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
6725 ssl->renego_records_seen = 0;
6726 }
6727#endif
6728
6729 /*
6730 * Free the previous session and switch in the current one
6731 */
6732 if( ssl->session )
6733 {
6734#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
6735 /* RFC 7366 3.1: keep the EtM state */
6736 ssl->session_negotiate->encrypt_then_mac =
6737 ssl->session->encrypt_then_mac;
6738#endif
6739
6740 mbedtls_ssl_session_free( ssl->session );
6741 mbedtls_free( ssl->session );
6742 }
6743 ssl->session = ssl->session_negotiate;
6744 ssl->session_negotiate = NULL;
6745
6746 /*
6747 * Add cache entry
6748 */
6749 if( ssl->conf->f_set_cache != NULL &&
6750 ssl->session->id_len != 0 &&
6751 resume == 0 )
6752 {
6753 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
6754 ssl->session->id,
6755 ssl->session->id_len,
6756 ssl->session ) != 0 )
6757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
6758 }
6759
6760#if defined(MBEDTLS_SSL_PROTO_DTLS)
6761 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6762 ssl->handshake->flight != NULL )
6763 {
6764 /* Cancel handshake timer */
6765 mbedtls_ssl_set_timer( ssl, 0 );
6766
6767 /* Keep last flight around in case we need to resend it:
6768 * we need the handshake and transform structures for that */
6769 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
6770 }
6771 else
6772#endif
6773 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
6774
6775 ssl->state++;
6776
6777 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
6778}
6779
Jerry Yu3c8e47b2022-02-17 14:30:01 +08006780int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
6781{
6782 int ret, hash_len;
6783
6784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
6785
6786 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
6787
6788 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
6789
6790 /*
6791 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6792 * may define some other value. Currently (early 2016), no defined
6793 * ciphersuite does this (and this is unlikely to change as activity has
6794 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6795 */
6796 hash_len = 12;
6797
6798#if defined(MBEDTLS_SSL_RENEGOTIATION)
6799 ssl->verify_data_len = hash_len;
6800 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
6801#endif
6802
6803 ssl->out_msglen = 4 + hash_len;
6804 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6805 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
6806
6807 /*
6808 * In case of session resuming, invert the client and server
6809 * ChangeCipherSpec messages order.
6810 */
6811 if( ssl->handshake->resume != 0 )
6812 {
6813#if defined(MBEDTLS_SSL_CLI_C)
6814 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6815 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
6816#endif
6817#if defined(MBEDTLS_SSL_SRV_C)
6818 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6819 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
6820#endif
6821 }
6822 else
6823 ssl->state++;
6824
6825 /*
6826 * Switch to our negotiated transform and session parameters for outbound
6827 * data.
6828 */
6829 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
6830
6831#if defined(MBEDTLS_SSL_PROTO_DTLS)
6832 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6833 {
6834 unsigned char i;
6835
6836 /* Remember current epoch settings for resending */
6837 ssl->handshake->alt_transform_out = ssl->transform_out;
6838 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
6839 sizeof( ssl->handshake->alt_out_ctr ) );
6840
6841 /* Set sequence_number to zero */
6842 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
6843
6844
6845 /* Increment epoch */
6846 for( i = 2; i > 0; i-- )
6847 if( ++ssl->cur_out_ctr[i - 1] != 0 )
6848 break;
6849
6850 /* The loop goes to its end iff the counter is wrapping */
6851 if( i == 0 )
6852 {
6853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6854 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
6855 }
6856 }
6857 else
6858#endif /* MBEDTLS_SSL_PROTO_DTLS */
6859 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6860
6861 ssl->transform_out = ssl->transform_negotiate;
6862 ssl->session_out = ssl->session_negotiate;
6863
6864#if defined(MBEDTLS_SSL_PROTO_DTLS)
6865 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6866 mbedtls_ssl_send_flight_completed( ssl );
6867#endif
6868
6869 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6870 {
6871 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6872 return( ret );
6873 }
6874
6875#if defined(MBEDTLS_SSL_PROTO_DTLS)
6876 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6877 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6878 {
6879 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6880 return( ret );
6881 }
6882#endif
6883
6884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
6885
6886 return( 0 );
6887}
6888
Jerry Yu0b3d7c12022-02-17 14:30:51 +08006889#define SSL_MAX_HASH_LEN 12
6890
6891int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
6892{
6893 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6894 unsigned int hash_len = 12;
6895 unsigned char buf[SSL_MAX_HASH_LEN];
6896
6897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
6898
6899 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
6900
6901 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6902 {
6903 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6904 goto exit;
6905 }
6906
6907 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6908 {
6909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6910 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6911 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6912 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
6913 goto exit;
6914 }
6915
6916 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
6917 {
6918 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6919 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6920 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
6921 goto exit;
6922 }
6923
6924 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
6925 {
6926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6927 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6928 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6929 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
6930 goto exit;
6931 }
6932
6933 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
6934 buf, hash_len ) != 0 )
6935 {
6936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
6937 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6938 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
6939 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
6940 goto exit;
6941 }
6942
6943#if defined(MBEDTLS_SSL_RENEGOTIATION)
6944 ssl->verify_data_len = hash_len;
6945 memcpy( ssl->peer_verify_data, buf, hash_len );
6946#endif
6947
6948 if( ssl->handshake->resume != 0 )
6949 {
6950#if defined(MBEDTLS_SSL_CLI_C)
6951 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6952 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
6953#endif
6954#if defined(MBEDTLS_SSL_SRV_C)
6955 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6956 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
6957#endif
6958 }
6959 else
6960 ssl->state++;
6961
6962#if defined(MBEDTLS_SSL_PROTO_DTLS)
6963 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6964 mbedtls_ssl_recv_flight_completed( ssl );
6965#endif
6966
6967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
6968
6969exit:
6970 mbedtls_platform_zeroize( buf, hash_len );
6971 return( ret );
6972}
6973
Jerry Yu392112c2022-02-17 14:34:10 +08006974#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
6975/*
6976 * Helper to get TLS 1.2 PRF from ciphersuite
6977 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
6978 */
6979static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
6980{
6981#if defined(MBEDTLS_SHA384_C)
6982 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6983 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
6984
6985 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
6986 return( tls_prf_sha384 );
6987#else
6988 (void) ciphersuite_id;
6989#endif
6990 return( tls_prf_sha256 );
6991}
6992#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08006993
6994static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
6995{
6996 ((void) tls_prf);
6997#if defined(MBEDTLS_SHA384_C)
6998 if( tls_prf == tls_prf_sha384 )
6999 {
7000 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7001 }
7002 else
7003#endif
7004#if defined(MBEDTLS_SHA256_C)
7005 if( tls_prf == tls_prf_sha256 )
7006 {
7007 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7008 }
7009 else
7010#endif
7011 return( MBEDTLS_SSL_TLS_PRF_NONE );
7012}
7013
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007014/*
7015 * Populate a transform structure with session keys and all the other
7016 * necessary information.
7017 *
7018 * Parameters:
7019 * - [in/out]: transform: structure to populate
7020 * [in] must be just initialised with mbedtls_ssl_transform_init()
7021 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7022 * - [in] ciphersuite
7023 * - [in] master
7024 * - [in] encrypt_then_mac
7025 * - [in] compression
7026 * - [in] tls_prf: pointer to PRF to use for key derivation
7027 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
7028 * - [in] minor_ver: SSL/TLS minor version
7029 * - [in] endpoint: client or server
7030 * - [in] ssl: used for:
7031 * - ssl->conf->{f,p}_export_keys
7032 * [in] optionally used for:
7033 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7034 */
7035static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7036 int ciphersuite,
7037 const unsigned char master[48],
7038#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
7039 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7040 int encrypt_then_mac,
7041#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
7042 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7043 ssl_tls_prf_t tls_prf,
7044 const unsigned char randbytes[64],
7045 int minor_ver,
7046 unsigned endpoint,
7047 const mbedtls_ssl_context *ssl )
7048{
7049 int ret = 0;
7050 unsigned char keyblk[256];
7051 unsigned char *key1;
7052 unsigned char *key2;
7053 unsigned char *mac_enc;
7054 unsigned char *mac_dec;
7055 size_t mac_key_len = 0;
7056 size_t iv_copy_len;
7057 size_t keylen;
7058 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
7059 const mbedtls_cipher_info_t *cipher_info;
7060 const mbedtls_md_info_t *md_info;
7061
7062#if defined(MBEDTLS_USE_PSA_CRYPTO)
7063 psa_key_type_t key_type;
7064 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7065 psa_algorithm_t alg;
7066 size_t key_bits;
7067 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7068#endif
7069
7070#if !defined(MBEDTLS_DEBUG_C) && \
7071 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7072 if( ssl->f_export_keys == NULL )
7073 {
7074 ssl = NULL; /* make sure we don't use it except for these cases */
7075 (void) ssl;
7076 }
7077#endif
7078
7079 /*
7080 * Some data just needs copying into the structure
7081 */
7082#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
7083 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7084 transform->encrypt_then_mac = encrypt_then_mac;
7085#endif
7086 transform->minor_ver = minor_ver;
7087
7088#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7089 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7090#endif
7091
7092#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
7093 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
7094 {
7095 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7096 * generation separate. This should never happen. */
7097 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7098 }
7099#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7100
7101 /*
7102 * Get various info structures
7103 */
7104 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7105 if( ciphersuite_info == NULL )
7106 {
7107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7108 ciphersuite ) );
7109 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7110 }
7111
7112 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7113 if( cipher_info == NULL )
7114 {
7115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7116 ciphersuite_info->cipher ) );
7117 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7118 }
7119
7120 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7121 if( md_info == NULL )
7122 {
7123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7124 (unsigned) ciphersuite_info->mac ) );
7125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7126 }
7127
7128#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7129 /* Copy own and peer's CID if the use of the CID
7130 * extension has been negotiated. */
7131 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7132 {
7133 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7134
7135 transform->in_cid_len = ssl->own_cid_len;
7136 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7137 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7138 transform->in_cid_len );
7139
7140 transform->out_cid_len = ssl->handshake->peer_cid_len;
7141 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7142 ssl->handshake->peer_cid_len );
7143 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7144 transform->out_cid_len );
7145 }
7146#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7147
7148 /*
7149 * Compute key block using the PRF
7150 */
7151 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7152 if( ret != 0 )
7153 {
7154 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7155 return( ret );
7156 }
7157
7158 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7159 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7160 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7161 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7162 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7163
7164 /*
7165 * Determine the appropriate key, IV and MAC length.
7166 */
7167
7168 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
7169
7170#if defined(MBEDTLS_GCM_C) || \
7171 defined(MBEDTLS_CCM_C) || \
7172 defined(MBEDTLS_CHACHAPOLY_C)
7173 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_GCM ||
7174 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CCM ||
7175 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
7176 {
7177 size_t explicit_ivlen;
7178
7179 transform->maclen = 0;
7180 mac_key_len = 0;
7181 transform->taglen =
7182 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7183
7184 /* All modes haves 96-bit IVs, but the length of the static parts vary
7185 * with mode and version:
7186 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7187 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7188 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7189 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7190 * sequence number).
7191 */
7192 transform->ivlen = 12;
7193 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
7194 transform->fixed_ivlen = 12;
7195 else
7196 transform->fixed_ivlen = 4;
7197
7198 /* Minimum length of encrypted record */
7199 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7200 transform->minlen = explicit_ivlen + transform->taglen;
7201 }
7202 else
7203#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7204#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7205 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM ||
7206 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7207 {
Neil Armstrongcf8841a2022-02-24 11:17:45 +01007208#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007209 /* Initialize HMAC contexts */
7210 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7211 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7212 {
7213 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7214 goto end;
7215 }
Neil Armstrongcf8841a2022-02-24 11:17:45 +01007216#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007217
7218 /* Get MAC length */
7219 mac_key_len = mbedtls_md_get_size( md_info );
7220 transform->maclen = mac_key_len;
7221
7222 /* IV length */
7223 transform->ivlen = cipher_info->iv_size;
7224
7225 /* Minimum length */
7226 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM )
7227 transform->minlen = transform->maclen;
7228 else
7229 {
7230 /*
7231 * GenericBlockCipher:
7232 * 1. if EtM is in use: one block plus MAC
7233 * otherwise: * first multiple of blocklen greater than maclen
7234 * 2. IV
7235 */
7236#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7237 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
7238 {
7239 transform->minlen = transform->maclen
7240 + cipher_info->block_size;
7241 }
7242 else
7243#endif
7244 {
7245 transform->minlen = transform->maclen
7246 + cipher_info->block_size
7247 - transform->maclen % cipher_info->block_size;
7248 }
7249
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007250 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
7251 {
7252 transform->minlen += transform->ivlen;
7253 }
7254 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007255 {
7256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7257 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7258 goto end;
7259 }
7260 }
7261 }
7262 else
7263#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7264 {
7265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7266 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7267 }
7268
7269 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7270 (unsigned) keylen,
7271 (unsigned) transform->minlen,
7272 (unsigned) transform->ivlen,
7273 (unsigned) transform->maclen ) );
7274
7275 /*
7276 * Finally setup the cipher contexts, IVs and MAC secrets.
7277 */
7278#if defined(MBEDTLS_SSL_CLI_C)
7279 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7280 {
7281 key1 = keyblk + mac_key_len * 2;
7282 key2 = keyblk + mac_key_len * 2 + keylen;
7283
7284 mac_enc = keyblk;
7285 mac_dec = keyblk + mac_key_len;
7286
7287 /*
7288 * This is not used in TLS v1.1.
7289 */
7290 iv_copy_len = ( transform->fixed_ivlen ) ?
7291 transform->fixed_ivlen : transform->ivlen;
7292 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7293 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7294 iv_copy_len );
7295 }
7296 else
7297#endif /* MBEDTLS_SSL_CLI_C */
7298#if defined(MBEDTLS_SSL_SRV_C)
7299 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7300 {
7301 key1 = keyblk + mac_key_len * 2 + keylen;
7302 key2 = keyblk + mac_key_len * 2;
7303
7304 mac_enc = keyblk + mac_key_len;
7305 mac_dec = keyblk;
7306
7307 /*
7308 * This is not used in TLS v1.1.
7309 */
7310 iv_copy_len = ( transform->fixed_ivlen ) ?
7311 transform->fixed_ivlen : transform->ivlen;
7312 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7313 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7314 iv_copy_len );
7315 }
7316 else
7317#endif /* MBEDTLS_SSL_SRV_C */
7318 {
7319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7320 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7321 goto end;
7322 }
7323
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007324 if( ssl != NULL && ssl->f_export_keys != NULL )
7325 {
7326 ssl->f_export_keys( ssl->p_export_keys,
7327 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7328 master, 48,
7329 randbytes + 32,
7330 randbytes,
7331 tls_prf_get_type( tls_prf ) );
7332 }
7333
7334#if defined(MBEDTLS_USE_PSA_CRYPTO)
7335 if( ( status = mbedtls_ssl_cipher_to_psa( cipher_info->type,
7336 transform->taglen,
7337 &alg,
7338 &key_type,
7339 &key_bits ) ) != PSA_SUCCESS )
7340 {
7341 ret = psa_ssl_status_to_mbedtls( status );
7342 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7343 goto end;
7344 }
7345
7346 transform->psa_alg = alg;
7347
7348 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7349 {
7350 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7351 psa_set_key_algorithm( &attributes, alg );
7352 psa_set_key_type( &attributes, key_type );
7353
7354 if( ( status = psa_import_key( &attributes,
7355 key1,
7356 PSA_BITS_TO_BYTES( key_bits ),
7357 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7358 {
7359 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7360 ret = psa_ssl_status_to_mbedtls( status );
7361 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7362 goto end;
7363 }
7364
7365 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7366
7367 if( ( status = psa_import_key( &attributes,
7368 key2,
7369 PSA_BITS_TO_BYTES( key_bits ),
7370 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7371 {
7372 ret = psa_ssl_status_to_mbedtls( status );
7373 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7374 goto end;
7375 }
7376 }
7377#else
7378 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7379 cipher_info ) ) != 0 )
7380 {
7381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7382 goto end;
7383 }
7384
7385 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7386 cipher_info ) ) != 0 )
7387 {
7388 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7389 goto end;
7390 }
7391
7392 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7393 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7394 MBEDTLS_ENCRYPT ) ) != 0 )
7395 {
7396 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7397 goto end;
7398 }
7399
7400 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7401 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7402 MBEDTLS_DECRYPT ) ) != 0 )
7403 {
7404 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7405 goto end;
7406 }
7407
7408#if defined(MBEDTLS_CIPHER_MODE_CBC)
7409 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7410 {
7411 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7412 MBEDTLS_PADDING_NONE ) ) != 0 )
7413 {
7414 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7415 goto end;
7416 }
7417
7418 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7419 MBEDTLS_PADDING_NONE ) ) != 0 )
7420 {
7421 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7422 goto end;
7423 }
7424 }
7425#endif /* MBEDTLS_CIPHER_MODE_CBC */
7426#endif /* MBEDTLS_USE_PSA_CRYPTO */
7427
Neil Armstrong29c0c042022-03-17 17:47:28 +01007428#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7429 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7430 For AEAD-based ciphersuites, there is nothing to do here. */
7431 if( mac_key_len != 0 )
7432 {
7433#if defined(MBEDTLS_USE_PSA_CRYPTO)
7434 alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
7435 if( alg == 0 )
7436 {
7437 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7438 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_md_type_to_psa", ret );
7439 goto end;
7440 }
7441
7442 transform->psa_mac_alg = PSA_ALG_HMAC( alg );
7443
7444 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
7445 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( alg ) );
7446 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7447
7448 if( ( status = psa_import_key( &attributes,
7449 mac_enc, mac_key_len,
7450 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7451 {
7452 ret = psa_ssl_status_to_mbedtls( status );
7453 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7454 goto end;
7455 }
7456
Ronald Cronfb39f152022-03-25 14:36:28 +01007457 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
7458 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) &&
7459 ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007460 /* mbedtls_ct_hmac() requires the key to be exportable */
7461 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7462 PSA_KEY_USAGE_VERIFY_HASH );
7463 else
7464 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7465
7466 if( ( status = psa_import_key( &attributes,
7467 mac_dec, mac_key_len,
7468 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7469 {
7470 ret = psa_ssl_status_to_mbedtls( status );
7471 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7472 goto end;
7473 }
7474#else
7475 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7476 if( ret != 0 )
7477 goto end;
7478 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7479 if( ret != 0 )
7480 goto end;
7481#endif /* MBEDTLS_USE_PSA_CRYPTO */
7482 }
7483#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7484
7485 ((void) mac_dec);
7486 ((void) mac_enc);
7487
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007488end:
7489 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7490 return( ret );
7491}
7492
Jerry Yuee40f9d2022-02-17 14:55:16 +08007493#if defined(MBEDTLS_USE_PSA_CRYPTO)
7494int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7495 unsigned char *hash, size_t *hashlen,
7496 unsigned char *data, size_t data_len,
7497 mbedtls_md_type_t md_alg )
7498{
7499 psa_status_t status;
7500 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
7501 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
7502
7503 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7504
7505 if( ( status = psa_hash_setup( &hash_operation,
7506 hash_alg ) ) != PSA_SUCCESS )
7507 {
7508 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7509 goto exit;
7510 }
7511
7512 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
7513 64 ) ) != PSA_SUCCESS )
7514 {
7515 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7516 goto exit;
7517 }
7518
7519 if( ( status = psa_hash_update( &hash_operation,
7520 data, data_len ) ) != PSA_SUCCESS )
7521 {
7522 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7523 goto exit;
7524 }
7525
7526 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
7527 hashlen ) ) != PSA_SUCCESS )
7528 {
7529 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
7530 goto exit;
7531 }
7532
7533exit:
7534 if( status != PSA_SUCCESS )
7535 {
7536 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7537 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7538 switch( status )
7539 {
7540 case PSA_ERROR_NOT_SUPPORTED:
7541 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
7542 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
7543 case PSA_ERROR_BUFFER_TOO_SMALL:
7544 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
7545 case PSA_ERROR_INSUFFICIENT_MEMORY:
7546 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
7547 default:
7548 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
7549 }
7550 }
7551 return( 0 );
7552}
7553
7554#else
7555
7556int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7557 unsigned char *hash, size_t *hashlen,
7558 unsigned char *data, size_t data_len,
7559 mbedtls_md_type_t md_alg )
7560{
7561 int ret = 0;
7562 mbedtls_md_context_t ctx;
7563 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
7564 *hashlen = mbedtls_md_get_size( md_info );
7565
7566 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
7567
7568 mbedtls_md_init( &ctx );
7569
7570 /*
7571 * digitally-signed struct {
7572 * opaque client_random[32];
7573 * opaque server_random[32];
7574 * ServerDHParams params;
7575 * };
7576 */
7577 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
7578 {
7579 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7580 goto exit;
7581 }
7582 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
7583 {
7584 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
7585 goto exit;
7586 }
7587 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
7588 {
7589 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7590 goto exit;
7591 }
7592 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
7593 {
7594 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7595 goto exit;
7596 }
7597 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
7598 {
7599 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
7600 goto exit;
7601 }
7602
7603exit:
7604 mbedtls_md_free( &ctx );
7605
7606 if( ret != 0 )
7607 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7608 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7609
7610 return( ret );
7611}
7612#endif /* MBEDTLS_USE_PSA_CRYPTO */
7613
Jerry Yud9d91da2022-02-17 14:57:06 +08007614#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
7615
7616/* Find an entry in a signature-hash set matching a given hash algorithm. */
7617mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
7618 mbedtls_pk_type_t sig_alg )
7619{
7620 switch( sig_alg )
7621 {
7622 case MBEDTLS_PK_RSA:
7623 return( set->rsa );
7624 case MBEDTLS_PK_ECDSA:
7625 return( set->ecdsa );
7626 default:
7627 return( MBEDTLS_MD_NONE );
7628 }
7629}
7630
7631/* Add a signature-hash-pair to a signature-hash set */
7632void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
7633 mbedtls_pk_type_t sig_alg,
7634 mbedtls_md_type_t md_alg )
7635{
7636 switch( sig_alg )
7637 {
7638 case MBEDTLS_PK_RSA:
7639 if( set->rsa == MBEDTLS_MD_NONE )
7640 set->rsa = md_alg;
7641 break;
7642
7643 case MBEDTLS_PK_ECDSA:
7644 if( set->ecdsa == MBEDTLS_MD_NONE )
7645 set->ecdsa = md_alg;
7646 break;
7647
7648 default:
7649 break;
7650 }
7651}
7652
7653/* Allow exactly one hash algorithm for each signature. */
7654void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
7655 mbedtls_md_type_t md_alg )
7656{
7657 set->rsa = md_alg;
7658 set->ecdsa = md_alg;
7659}
7660
7661#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7662
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08007663/* Serialization of TLS 1.2 sessions:
7664 *
7665 * struct {
7666 * uint64 start_time;
7667 * uint8 ciphersuite[2]; // defined by the standard
7668 * uint8 compression; // 0 or 1
7669 * uint8 session_id_len; // at most 32
7670 * opaque session_id[32];
7671 * opaque master[48]; // fixed length in the standard
7672 * uint32 verify_result;
7673 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
7674 * opaque ticket<0..2^24-1>; // length 0 means no ticket
7675 * uint32 ticket_lifetime;
7676 * uint8 mfl_code; // up to 255 according to standard
7677 * uint8 encrypt_then_mac; // 0 or 1
7678 * } serialized_session_tls12;
7679 *
7680 */
7681static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session,
7682 unsigned char *buf,
7683 size_t buf_len )
7684{
7685 unsigned char *p = buf;
7686 size_t used = 0;
7687
7688#if defined(MBEDTLS_HAVE_TIME)
7689 uint64_t start;
7690#endif
7691#if defined(MBEDTLS_X509_CRT_PARSE_C)
7692#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7693 size_t cert_len;
7694#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7695#endif /* MBEDTLS_X509_CRT_PARSE_C */
7696
7697 /*
7698 * Time
7699 */
7700#if defined(MBEDTLS_HAVE_TIME)
7701 used += 8;
7702
7703 if( used <= buf_len )
7704 {
7705 start = (uint64_t) session->start;
7706
7707 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
7708 p += 8;
7709 }
7710#endif /* MBEDTLS_HAVE_TIME */
7711
7712 /*
7713 * Basic mandatory fields
7714 */
7715 used += 2 /* ciphersuite */
7716 + 1 /* compression */
7717 + 1 /* id_len */
7718 + sizeof( session->id )
7719 + sizeof( session->master )
7720 + 4; /* verify_result */
7721
7722 if( used <= buf_len )
7723 {
7724 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
7725 p += 2;
7726
7727 *p++ = MBEDTLS_BYTE_0( session->compression );
7728
7729 *p++ = MBEDTLS_BYTE_0( session->id_len );
7730 memcpy( p, session->id, 32 );
7731 p += 32;
7732
7733 memcpy( p, session->master, 48 );
7734 p += 48;
7735
7736 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
7737 p += 4;
7738 }
7739
7740 /*
7741 * Peer's end-entity certificate
7742 */
7743#if defined(MBEDTLS_X509_CRT_PARSE_C)
7744#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7745 if( session->peer_cert == NULL )
7746 cert_len = 0;
7747 else
7748 cert_len = session->peer_cert->raw.len;
7749
7750 used += 3 + cert_len;
7751
7752 if( used <= buf_len )
7753 {
7754 *p++ = MBEDTLS_BYTE_2( cert_len );
7755 *p++ = MBEDTLS_BYTE_1( cert_len );
7756 *p++ = MBEDTLS_BYTE_0( cert_len );
7757
7758 if( session->peer_cert != NULL )
7759 {
7760 memcpy( p, session->peer_cert->raw.p, cert_len );
7761 p += cert_len;
7762 }
7763 }
7764#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7765 if( session->peer_cert_digest != NULL )
7766 {
7767 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
7768 if( used <= buf_len )
7769 {
7770 *p++ = (unsigned char) session->peer_cert_digest_type;
7771 *p++ = (unsigned char) session->peer_cert_digest_len;
7772 memcpy( p, session->peer_cert_digest,
7773 session->peer_cert_digest_len );
7774 p += session->peer_cert_digest_len;
7775 }
7776 }
7777 else
7778 {
7779 used += 2;
7780 if( used <= buf_len )
7781 {
7782 *p++ = (unsigned char) MBEDTLS_MD_NONE;
7783 *p++ = 0;
7784 }
7785 }
7786#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7787#endif /* MBEDTLS_X509_CRT_PARSE_C */
7788
7789 /*
7790 * Session ticket if any, plus associated data
7791 */
7792#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7793 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
7794
7795 if( used <= buf_len )
7796 {
7797 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
7798 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
7799 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
7800
7801 if( session->ticket != NULL )
7802 {
7803 memcpy( p, session->ticket, session->ticket_len );
7804 p += session->ticket_len;
7805 }
7806
7807 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
7808 p += 4;
7809 }
7810#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7811
7812 /*
7813 * Misc extension-related info
7814 */
7815#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7816 used += 1;
7817
7818 if( used <= buf_len )
7819 *p++ = session->mfl_code;
7820#endif
7821
7822#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7823 used += 1;
7824
7825 if( used <= buf_len )
7826 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
7827#endif
7828
7829 return( used );
7830}
7831
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08007832static int ssl_session_load_tls12( mbedtls_ssl_session *session,
7833 const unsigned char *buf,
7834 size_t len )
7835{
7836#if defined(MBEDTLS_HAVE_TIME)
7837 uint64_t start;
7838#endif
7839#if defined(MBEDTLS_X509_CRT_PARSE_C)
7840#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7841 size_t cert_len;
7842#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7843#endif /* MBEDTLS_X509_CRT_PARSE_C */
7844
7845 const unsigned char *p = buf;
7846 const unsigned char * const end = buf + len;
7847
7848 /*
7849 * Time
7850 */
7851#if defined(MBEDTLS_HAVE_TIME)
7852 if( 8 > (size_t)( end - p ) )
7853 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7854
7855 start = ( (uint64_t) p[0] << 56 ) |
7856 ( (uint64_t) p[1] << 48 ) |
7857 ( (uint64_t) p[2] << 40 ) |
7858 ( (uint64_t) p[3] << 32 ) |
7859 ( (uint64_t) p[4] << 24 ) |
7860 ( (uint64_t) p[5] << 16 ) |
7861 ( (uint64_t) p[6] << 8 ) |
7862 ( (uint64_t) p[7] );
7863 p += 8;
7864
7865 session->start = (time_t) start;
7866#endif /* MBEDTLS_HAVE_TIME */
7867
7868 /*
7869 * Basic mandatory fields
7870 */
7871 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
7872 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7873
7874 session->ciphersuite = ( p[0] << 8 ) | p[1];
7875 p += 2;
7876
7877 session->compression = *p++;
7878
7879 session->id_len = *p++;
7880 memcpy( session->id, p, 32 );
7881 p += 32;
7882
7883 memcpy( session->master, p, 48 );
7884 p += 48;
7885
7886 session->verify_result = ( (uint32_t) p[0] << 24 ) |
7887 ( (uint32_t) p[1] << 16 ) |
7888 ( (uint32_t) p[2] << 8 ) |
7889 ( (uint32_t) p[3] );
7890 p += 4;
7891
7892 /* Immediately clear invalid pointer values that have been read, in case
7893 * we exit early before we replaced them with valid ones. */
7894#if defined(MBEDTLS_X509_CRT_PARSE_C)
7895#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7896 session->peer_cert = NULL;
7897#else
7898 session->peer_cert_digest = NULL;
7899#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7900#endif /* MBEDTLS_X509_CRT_PARSE_C */
7901#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7902 session->ticket = NULL;
7903#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
7904
7905 /*
7906 * Peer certificate
7907 */
7908#if defined(MBEDTLS_X509_CRT_PARSE_C)
7909#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7910 /* Deserialize CRT from the end of the ticket. */
7911 if( 3 > (size_t)( end - p ) )
7912 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7913
7914 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
7915 p += 3;
7916
7917 if( cert_len != 0 )
7918 {
7919 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7920
7921 if( cert_len > (size_t)( end - p ) )
7922 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7923
7924 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7925
7926 if( session->peer_cert == NULL )
7927 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7928
7929 mbedtls_x509_crt_init( session->peer_cert );
7930
7931 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
7932 p, cert_len ) ) != 0 )
7933 {
7934 mbedtls_x509_crt_free( session->peer_cert );
7935 mbedtls_free( session->peer_cert );
7936 session->peer_cert = NULL;
7937 return( ret );
7938 }
7939
7940 p += cert_len;
7941 }
7942#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7943 /* Deserialize CRT digest from the end of the ticket. */
7944 if( 2 > (size_t)( end - p ) )
7945 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7946
7947 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
7948 session->peer_cert_digest_len = (size_t) *p++;
7949
7950 if( session->peer_cert_digest_len != 0 )
7951 {
7952 const mbedtls_md_info_t *md_info =
7953 mbedtls_md_info_from_type( session->peer_cert_digest_type );
7954 if( md_info == NULL )
7955 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7956 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
7957 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7958
7959 if( session->peer_cert_digest_len > (size_t)( end - p ) )
7960 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7961
7962 session->peer_cert_digest =
7963 mbedtls_calloc( 1, session->peer_cert_digest_len );
7964 if( session->peer_cert_digest == NULL )
7965 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7966
7967 memcpy( session->peer_cert_digest, p,
7968 session->peer_cert_digest_len );
7969 p += session->peer_cert_digest_len;
7970 }
7971#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7972#endif /* MBEDTLS_X509_CRT_PARSE_C */
7973
7974 /*
7975 * Session ticket and associated data
7976 */
7977#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
7978 if( 3 > (size_t)( end - p ) )
7979 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7980
7981 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
7982 p += 3;
7983
7984 if( session->ticket_len != 0 )
7985 {
7986 if( session->ticket_len > (size_t)( end - p ) )
7987 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7988
7989 session->ticket = mbedtls_calloc( 1, session->ticket_len );
7990 if( session->ticket == NULL )
7991 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7992
7993 memcpy( session->ticket, p, session->ticket_len );
7994 p += session->ticket_len;
7995 }
7996
7997 if( 4 > (size_t)( end - p ) )
7998 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7999
8000 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8001 ( (uint32_t) p[1] << 16 ) |
8002 ( (uint32_t) p[2] << 8 ) |
8003 ( (uint32_t) p[3] );
8004 p += 4;
8005#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8006
8007 /*
8008 * Misc extension-related info
8009 */
8010#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8011 if( 1 > (size_t)( end - p ) )
8012 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8013
8014 session->mfl_code = *p++;
8015#endif
8016
8017#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8018 if( 1 > (size_t)( end - p ) )
8019 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8020
8021 session->encrypt_then_mac = *p++;
8022#endif
8023
8024 /* Done, should have consumed entire buffer */
8025 if( p != end )
8026 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8027
8028 return( 0 );
8029}
Jerry Yudc7bd172022-02-17 13:44:15 +08008030#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008032#endif /* MBEDTLS_SSL_TLS_C */