blob: a1fda8cc217b0d02c4d94a0958820b945960341c [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)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 mbedtls_md_init( &transform->md_ctx_enc );
614 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000615#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200616}
617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200619{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200621}
622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000624{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200625 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000626 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200628 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200630 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200631 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200632
633 /*
634 * Either the pointers are now NULL or cleared properly and can be freed.
635 * Now allocate missing structures.
636 */
637 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200638 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200639 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200640 }
Paul Bakker48916f92012-09-16 19:57:18 +0000641
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200642 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200643 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200644 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200645 }
Paul Bakker48916f92012-09-16 19:57:18 +0000646
Paul Bakker82788fb2014-10-20 13:59:19 +0200647 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200648 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200649 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200650 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500651#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
652 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400653
Andrzej Kurek4a063792020-10-21 15:08:44 +0200654 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
655 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500656#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000657
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200658 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000659 if( ssl->handshake == NULL ||
660 ssl->transform_negotiate == NULL ||
661 ssl->session_negotiate == NULL )
662 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200663 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 mbedtls_free( ssl->handshake );
666 mbedtls_free( ssl->transform_negotiate );
667 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200668
669 ssl->handshake = NULL;
670 ssl->transform_negotiate = NULL;
671 ssl->session_negotiate = NULL;
672
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200673 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000674 }
675
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200676 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000678 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200679 ssl_handshake_params_init( ssl->handshake );
680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200682 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
683 {
684 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200685
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200686 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
687 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
688 else
689 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200690
Hanno Becker0f57a652020-02-05 10:37:26 +0000691 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200692 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200693#endif
694
Brett Warrene0edc842021-08-17 09:53:13 +0100695/*
696 * curve_list is translated to IANA TLS group identifiers here because
697 * mbedtls_ssl_conf_curves returns void and so can't return
698 * any error codes.
699 */
700#if defined(MBEDTLS_ECP_C)
701#if !defined(MBEDTLS_DEPRECATED_REMOVED)
702 /* Heap allocate and translate curve_list from internal to IANA group ids */
703 if ( ssl->conf->curve_list != NULL )
704 {
705 size_t length;
706 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
707
708 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
709 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
710
711 /* Leave room for zero termination */
712 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
713 if ( group_list == NULL )
714 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
715
716 for( size_t i = 0; i < length; i++ )
717 {
718 const mbedtls_ecp_curve_info *info =
719 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
720 if ( info == NULL )
721 {
722 mbedtls_free( group_list );
723 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
724 }
725 group_list[i] = info->tls_id;
726 }
727
728 group_list[length] = 0;
729
730 ssl->handshake->group_list = group_list;
731 ssl->handshake->group_list_heap_allocated = 1;
732 }
733 else
734 {
735 ssl->handshake->group_list = ssl->conf->group_list;
736 ssl->handshake->group_list_heap_allocated = 0;
737 }
738#endif /* MBEDTLS_DEPRECATED_REMOVED */
739#endif /* MBEDTLS_ECP_C */
740
Jerry Yuf017ee42022-01-12 15:49:48 +0800741#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
742#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800743#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800744 /* Heap allocate and translate sig_hashes from internal hash identifiers to
745 signature algorithms IANA identifiers. */
746 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800747 ssl->conf->sig_hashes != NULL )
748 {
749 const int *md;
750 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800751 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800752 uint16_t *p;
753
Jerry Yub476a442022-01-21 18:14:45 +0800754#if defined(static_assert)
755 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
756 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
757 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800758#endif
759
Jerry Yuf017ee42022-01-12 15:49:48 +0800760 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
761 {
762 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
763 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800764#if defined(MBEDTLS_ECDSA_C)
765 sig_algs_len += sizeof( uint16_t );
766#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800767
Jerry Yub476a442022-01-21 18:14:45 +0800768#if defined(MBEDTLS_RSA_C)
769 sig_algs_len += sizeof( uint16_t );
770#endif
771 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
772 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800773 }
774
Jerry Yub476a442022-01-21 18:14:45 +0800775 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800776 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
777
Jerry Yub476a442022-01-21 18:14:45 +0800778 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
779 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800780 if( ssl->handshake->sig_algs == NULL )
781 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
782
783 p = (uint16_t *)ssl->handshake->sig_algs;
784 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
785 {
786 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
787 if( hash == MBEDTLS_SSL_HASH_NONE )
788 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800789#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800790 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
791 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800792#endif
793#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800794 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
795 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800796#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800797 }
798 *p = MBEDTLS_TLS1_3_SIG_NONE;
799 ssl->handshake->sig_algs_heap_allocated = 1;
800 }
801 else
Jerry Yua69269a2022-01-17 21:06:01 +0800802#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800803 {
804 ssl->handshake->sig_algs = ssl->conf->sig_algs;
805 ssl->handshake->sig_algs_heap_allocated = 0;
806 }
807#endif /* MBEDTLS_DEPRECATED_REMOVED */
808#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000809 return( 0 );
810}
811
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200812#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200813/* Dummy cookie callbacks for defaults */
814static int ssl_cookie_write_dummy( void *ctx,
815 unsigned char **p, unsigned char *end,
816 const unsigned char *cli_id, size_t cli_id_len )
817{
818 ((void) ctx);
819 ((void) p);
820 ((void) end);
821 ((void) cli_id);
822 ((void) cli_id_len);
823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200825}
826
827static int ssl_cookie_check_dummy( void *ctx,
828 const unsigned char *cookie, size_t cookie_len,
829 const unsigned char *cli_id, size_t cli_id_len )
830{
831 ((void) ctx);
832 ((void) cookie);
833 ((void) cookie_len);
834 ((void) cli_id);
835 ((void) cli_id_len);
836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200838}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200839#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200840
Paul Bakker5121ce52009-01-03 21:22:43 +0000841/*
842 * Initialize an SSL context
843 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200844void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
845{
846 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
847}
848
Jerry Yu60835a82021-08-04 10:13:52 +0800849static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
850{
Ronald Cron6f135e12021-12-08 16:57:54 +0100851#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu60835a82021-08-04 10:13:52 +0800852 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
853 {
854 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
855 {
856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported" ) );
857 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
858 }
859 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
860 return( 0 );
861 }
862#endif
863
864#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
865 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
866 {
867 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
868 return( 0 );
869 }
870#endif
871
Ronald Cron6f135e12021-12-08 16:57:54 +0100872#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu60835a82021-08-04 10:13:52 +0800873 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( ssl->conf ) )
874 {
875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Hybrid TLS 1.2 + TLS 1.3 configurations are not yet supported" ) );
876 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
877 }
878#endif
879
880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
881 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
882}
883
884static int ssl_conf_check(const mbedtls_ssl_context *ssl)
885{
886 int ret;
887 ret = ssl_conf_version_check( ssl );
888 if( ret != 0 )
889 return( ret );
890
891 /* Space for further checks */
892
893 return( 0 );
894}
895
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200896/*
897 * Setup an SSL context
898 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100899
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200900int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +0200901 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +0000902{
Janos Follath865b3eb2019-12-16 11:46:15 +0000903 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +0000904 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
905 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +0000906
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200907 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +0000908
Jerry Yu60835a82021-08-04 10:13:52 +0800909 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
910 return( ret );
911
Paul Bakker62f2dee2012-09-28 07:31:51 +0000912 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +0100913 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +0000914 */
k-stachowiakc9a5f022018-07-24 13:53:31 +0200915
916 /* Set to NULL in case of an error condition */
917 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +0200918
Darryl Greenb33cc762019-11-28 14:29:44 +0000919#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
920 ssl->in_buf_len = in_buf_len;
921#endif
922 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +1000923 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000924 {
Paul Elliottd48d5c62021-01-07 14:47:05 +0000925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +0200926 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +0200927 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +1000928 }
929
Darryl Greenb33cc762019-11-28 14:29:44 +0000930#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
931 ssl->out_buf_len = out_buf_len;
932#endif
933 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +1000934 if( ssl->out_buf == NULL )
935 {
Paul Elliottd48d5c62021-01-07 14:47:05 +0000936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +0200937 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +0200938 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +0000939 }
940
Hanno Becker3e6f8ab2020-02-05 10:40:57 +0000941 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200942
Johan Pascalb62bb512015-12-03 21:56:45 +0100943#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +0200944 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +0100945#endif
946
Paul Bakker48916f92012-09-16 19:57:18 +0000947 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +0200948 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +0000949
950 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +0200951
952error:
953 mbedtls_free( ssl->in_buf );
954 mbedtls_free( ssl->out_buf );
955
956 ssl->conf = NULL;
957
Darryl Greenb33cc762019-11-28 14:29:44 +0000958#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
959 ssl->in_buf_len = 0;
960 ssl->out_buf_len = 0;
961#endif
k-stachowiaka47911c2018-07-04 17:41:58 +0200962 ssl->in_buf = NULL;
963 ssl->out_buf = NULL;
964
965 ssl->in_hdr = NULL;
966 ssl->in_ctr = NULL;
967 ssl->in_len = NULL;
968 ssl->in_iv = NULL;
969 ssl->in_msg = NULL;
970
971 ssl->out_hdr = NULL;
972 ssl->out_ctr = NULL;
973 ssl->out_len = NULL;
974 ssl->out_iv = NULL;
975 ssl->out_msg = NULL;
976
k-stachowiak9f7798e2018-07-31 16:52:32 +0200977 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000978}
979
980/*
Paul Bakker7eb013f2011-10-06 12:37:39 +0000981 * Reset an initialized and used SSL context for re-use while retaining
982 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +0200983 *
984 * If partial is non-zero, keep data in the input buffer and client ID.
985 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +0000986 */
XiaokangQian78b1fa72022-01-19 06:56:30 +0000987void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
988 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +0000989{
Darryl Greenb33cc762019-11-28 14:29:44 +0000990#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
991 size_t in_buf_len = ssl->in_buf_len;
992 size_t out_buf_len = ssl->out_buf_len;
993#else
994 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
995 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
996#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000997
Hanno Beckerb0302c42021-08-03 09:39:42 +0100998#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
999 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001000#endif
1001
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001002 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001003 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001004
Hanno Beckerb0302c42021-08-03 09:39:42 +01001005 mbedtls_ssl_reset_in_out_pointers( ssl );
1006
1007 /* Reset incoming message parsing */
1008 ssl->in_offt = NULL;
1009 ssl->nb_zero = 0;
1010 ssl->in_msgtype = 0;
1011 ssl->in_msglen = 0;
1012 ssl->in_hslen = 0;
1013 ssl->keep_current_message = 0;
1014 ssl->transform_in = NULL;
1015
1016#if defined(MBEDTLS_SSL_PROTO_DTLS)
1017 ssl->next_record_offset = 0;
1018 ssl->in_epoch = 0;
1019#endif
1020
1021 /* Keep current datagram if partial == 1 */
1022 if( partial == 0 )
1023 {
1024 ssl->in_left = 0;
1025 memset( ssl->in_buf, 0, in_buf_len );
1026 }
1027
1028 /* Reset outgoing message writing */
1029 ssl->out_msgtype = 0;
1030 ssl->out_msglen = 0;
1031 ssl->out_left = 0;
1032 memset( ssl->out_buf, 0, out_buf_len );
1033 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1034 ssl->transform_out = NULL;
1035
1036#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1037 mbedtls_ssl_dtls_replay_reset( ssl );
1038#endif
1039
XiaokangQian2b01dc32022-01-21 02:53:13 +00001040#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001041 if( ssl->transform )
1042 {
1043 mbedtls_ssl_transform_free( ssl->transform );
1044 mbedtls_free( ssl->transform );
1045 ssl->transform = NULL;
1046 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001047#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1048
XiaokangQian2b01dc32022-01-21 02:53:13 +00001049#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1050 mbedtls_ssl_transform_free( ssl->transform_application );
1051 mbedtls_free( ssl->transform_application );
1052 ssl->transform_application = NULL;
1053
1054 if( ssl->handshake != NULL )
1055 {
1056 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1057 mbedtls_free( ssl->handshake->transform_earlydata );
1058 ssl->handshake->transform_earlydata = NULL;
1059
1060 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1061 mbedtls_free( ssl->handshake->transform_handshake );
1062 ssl->handshake->transform_handshake = NULL;
1063 }
1064
XiaokangQian2b01dc32022-01-21 02:53:13 +00001065#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001066}
1067
1068int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1069{
1070 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1071
1072 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1073
XiaokangQian78b1fa72022-01-19 06:56:30 +00001074 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001075
1076 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077#if defined(MBEDTLS_SSL_RENEGOTIATION)
1078 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001079 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001080
1081 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1083 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001084#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001086
Hanno Beckerb0302c42021-08-03 09:39:42 +01001087 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001088 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001089 if( ssl->session )
1090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 mbedtls_ssl_session_free( ssl->session );
1092 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001093 ssl->session = NULL;
1094 }
1095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001097 ssl->alpn_chosen = NULL;
1098#endif
1099
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001100#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01001101#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001102 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01001103#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001104 {
1105 mbedtls_free( ssl->cli_id );
1106 ssl->cli_id = NULL;
1107 ssl->cli_id_len = 0;
1108 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001109#endif
1110
Paul Bakker48916f92012-09-16 19:57:18 +00001111 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1112 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001113
1114 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001115}
1116
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001117/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001118 * Reset an initialized and used SSL context for re-use while retaining
1119 * all application-set variables, function pointers and data.
1120 */
1121int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1122{
Hanno Becker43aefe22020-02-05 10:44:56 +00001123 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001124}
1125
1126/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001127 * SSL set accessors
1128 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001129void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001130{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001131 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001132}
1133
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001134void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001135{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001136 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001137}
1138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001140void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001141{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001142 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001143}
1144#endif
1145
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001146void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001147{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001148 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001149}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001152
Hanno Becker1841b0a2018-08-24 11:13:57 +01001153void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1154 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001155{
1156 ssl->disable_datagram_packing = !allow_packing;
1157}
1158
1159void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1160 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001161{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001162 conf->hs_timeout_min = min;
1163 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001164}
1165#endif
1166
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001167void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001168{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001169 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001170}
1171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001173void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001174 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001175 void *p_vrfy )
1176{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001177 conf->f_vrfy = f_vrfy;
1178 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001179}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001181
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001182void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001183 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 void *p_rng )
1185{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001186 conf->f_rng = f_rng;
1187 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001188}
1189
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001190void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001191 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001192 void *p_dbg )
1193{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001194 conf->f_dbg = f_dbg;
1195 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001196}
1197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001199 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001200 mbedtls_ssl_send_t *f_send,
1201 mbedtls_ssl_recv_t *f_recv,
1202 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001203{
1204 ssl->p_bio = p_bio;
1205 ssl->f_send = f_send;
1206 ssl->f_recv = f_recv;
1207 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001208}
1209
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001210#if defined(MBEDTLS_SSL_PROTO_DTLS)
1211void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1212{
1213 ssl->mtu = mtu;
1214}
1215#endif
1216
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001217void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001218{
1219 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001220}
1221
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001222void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1223 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001224 mbedtls_ssl_set_timer_t *f_set_timer,
1225 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001226{
1227 ssl->p_timer = p_timer;
1228 ssl->f_set_timer = f_set_timer;
1229 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001230
1231 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001232 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001233}
1234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001236void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001237 void *p_cache,
1238 mbedtls_ssl_cache_get_t *f_get_cache,
1239 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001240{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001241 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001242 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001243 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001244}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247#if defined(MBEDTLS_SSL_CLI_C)
1248int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001249{
Janos Follath865b3eb2019-12-16 11:46:15 +00001250 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001251
1252 if( ssl == NULL ||
1253 session == NULL ||
1254 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001255 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001258 }
1259
Hanno Beckere810bbc2021-05-14 16:01:05 +01001260 if( ssl->handshake->resume == 1 )
1261 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1262
Hanno Becker52055ae2019-02-06 14:30:46 +00001263 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1264 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001265 return( ret );
1266
Paul Bakker0a597072012-09-25 21:55:46 +00001267 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001268
1269 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001270}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001272
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001273void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1274 const int *ciphersuites )
1275{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001276 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001277}
1278
Ronald Cron6f135e12021-12-08 16:57:54 +01001279#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001280void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001281 const int kex_modes )
1282{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001283 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001284}
Ronald Cron6f135e12021-12-08 16:57:54 +01001285#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001288void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001289 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001290{
1291 conf->cert_profile = profile;
1292}
1293
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001294/* Append a new keycert entry to a (possibly empty) list */
1295static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1296 mbedtls_x509_crt *cert,
1297 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001298{
niisato8ee24222018-06-25 19:05:48 +09001299 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001300
niisato8ee24222018-06-25 19:05:48 +09001301 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1302 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001303 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001304
niisato8ee24222018-06-25 19:05:48 +09001305 new_cert->cert = cert;
1306 new_cert->key = key;
1307 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001308
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001309 /* Update head is the list was null, else add to the end */
1310 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001311 {
niisato8ee24222018-06-25 19:05:48 +09001312 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001313 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001314 else
1315 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001316 mbedtls_ssl_key_cert *cur = *head;
1317 while( cur->next != NULL )
1318 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001319 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001320 }
1321
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001322 return( 0 );
1323}
1324
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001325int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001326 mbedtls_x509_crt *own_cert,
1327 mbedtls_pk_context *pk_key )
1328{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001329 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001330}
1331
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001332void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001333 mbedtls_x509_crt *ca_chain,
1334 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001335{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001336 conf->ca_chain = ca_chain;
1337 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001338
1339#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1340 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1341 * cannot be used together. */
1342 conf->f_ca_cb = NULL;
1343 conf->p_ca_cb = NULL;
1344#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001345}
Hanno Becker5adaad92019-03-27 16:54:37 +00001346
1347#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1348void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001349 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001350 void *p_ca_cb )
1351{
1352 conf->f_ca_cb = f_ca_cb;
1353 conf->p_ca_cb = p_ca_cb;
1354
1355 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1356 * cannot be used together. */
1357 conf->ca_chain = NULL;
1358 conf->ca_crl = NULL;
1359}
1360#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001362
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001363#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1364int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1365 mbedtls_x509_crt *own_cert,
1366 mbedtls_pk_context *pk_key )
1367{
1368 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1369 own_cert, pk_key ) );
1370}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001371
1372void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1373 mbedtls_x509_crt *ca_chain,
1374 mbedtls_x509_crl *ca_crl )
1375{
1376 ssl->handshake->sni_ca_chain = ca_chain;
1377 ssl->handshake->sni_ca_crl = ca_crl;
1378}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001379
1380void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1381 int authmode )
1382{
1383 ssl->handshake->sni_authmode = authmode;
1384}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001385#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1386
Hanno Becker8927c832019-04-03 12:52:50 +01001387#if defined(MBEDTLS_X509_CRT_PARSE_C)
1388void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1389 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1390 void *p_vrfy )
1391{
1392 ssl->f_vrfy = f_vrfy;
1393 ssl->p_vrfy = p_vrfy;
1394}
1395#endif
1396
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001397#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001398/*
1399 * Set EC J-PAKE password for current handshake
1400 */
1401int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1402 const unsigned char *pw,
1403 size_t pw_len )
1404{
1405 mbedtls_ecjpake_role role;
1406
Janos Follath8eb64132016-06-03 15:40:57 +01001407 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001408 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1409
1410 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1411 role = MBEDTLS_ECJPAKE_SERVER;
1412 else
1413 role = MBEDTLS_ECJPAKE_CLIENT;
1414
1415 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1416 role,
1417 MBEDTLS_MD_SHA256,
1418 MBEDTLS_ECP_DP_SECP256R1,
1419 pw, pw_len ) );
1420}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001421#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001422
Gilles Peskineeccd8882020-03-10 12:19:08 +01001423#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001424
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001425static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1426{
1427#if defined(MBEDTLS_USE_PSA_CRYPTO)
1428 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1429 return( 1 );
1430#endif /* MBEDTLS_USE_PSA_CRYPTO */
1431
1432 if( conf->psk != NULL )
1433 return( 1 );
1434
1435 return( 0 );
1436}
1437
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001438static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1439{
1440 /* Remove reference to existing PSK, if any. */
1441#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001442 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001443 {
1444 /* The maintenance of the PSK key slot is the
1445 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001446 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001447 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00001448 /* This and the following branch should never
1449 * be taken simultaenously as we maintain the
1450 * invariant that raw and opaque PSKs are never
1451 * configured simultaneously. As a safeguard,
1452 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001453#endif /* MBEDTLS_USE_PSA_CRYPTO */
1454 if( conf->psk != NULL )
1455 {
1456 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1457
1458 mbedtls_free( conf->psk );
1459 conf->psk = NULL;
1460 conf->psk_len = 0;
1461 }
1462
1463 /* Remove reference to PSK identity, if any. */
1464 if( conf->psk_identity != NULL )
1465 {
1466 mbedtls_free( conf->psk_identity );
1467 conf->psk_identity = NULL;
1468 conf->psk_identity_len = 0;
1469 }
1470}
1471
Hanno Becker7390c712018-11-15 13:33:04 +00001472/* This function assumes that PSK identity in the SSL config is unset.
1473 * It checks that the provided identity is well-formed and attempts
1474 * to make a copy of it in the SSL config.
1475 * On failure, the PSK identity in the config remains unset. */
1476static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1477 unsigned char const *psk_identity,
1478 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001479{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001480 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001481 if( psk_identity == NULL ||
1482 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001483 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001484 {
1485 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1486 }
1487
Hanno Becker7390c712018-11-15 13:33:04 +00001488 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1489 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001490 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001491
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001492 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001493 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001494
1495 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001496}
1497
Hanno Becker7390c712018-11-15 13:33:04 +00001498int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1499 const unsigned char *psk, size_t psk_len,
1500 const unsigned char *psk_identity, size_t psk_identity_len )
1501{
Janos Follath865b3eb2019-12-16 11:46:15 +00001502 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001503
1504 /* We currently only support one PSK, raw or opaque. */
1505 if( ssl_conf_psk_is_configured( conf ) )
1506 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001507
1508 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001509 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001511 if( psk_len == 0 )
1512 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1513 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1514 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1515
Hanno Becker7390c712018-11-15 13:33:04 +00001516 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1517 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1518 conf->psk_len = psk_len;
1519 memcpy( conf->psk, psk, conf->psk_len );
1520
1521 /* Check and set PSK Identity */
1522 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1523 if( ret != 0 )
1524 ssl_conf_remove_psk( conf );
1525
1526 return( ret );
1527}
1528
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001529static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1530{
1531#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001532 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001533 {
Ronald Croncf56a0a2020-08-04 09:51:30 +02001534 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001535 }
1536 else
1537#endif /* MBEDTLS_USE_PSA_CRYPTO */
1538 if( ssl->handshake->psk != NULL )
1539 {
1540 mbedtls_platform_zeroize( ssl->handshake->psk,
1541 ssl->handshake->psk_len );
1542 mbedtls_free( ssl->handshake->psk );
1543 ssl->handshake->psk_len = 0;
1544 }
1545}
1546
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001547int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1548 const unsigned char *psk, size_t psk_len )
1549{
1550 if( psk == NULL || ssl->handshake == NULL )
1551 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1552
1553 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1554 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1555
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001556 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001557
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001558 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001559 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001560
1561 ssl->handshake->psk_len = psk_len;
1562 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1563
1564 return( 0 );
1565}
1566
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001567#if defined(MBEDTLS_USE_PSA_CRYPTO)
1568int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001569 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001570 const unsigned char *psk_identity,
1571 size_t psk_identity_len )
1572{
Janos Follath865b3eb2019-12-16 11:46:15 +00001573 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001574
1575 /* We currently only support one PSK, raw or opaque. */
1576 if( ssl_conf_psk_is_configured( conf ) )
1577 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001578
Hanno Becker7390c712018-11-15 13:33:04 +00001579 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001580 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001581 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001582 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001583
1584 /* Check and set PSK Identity */
1585 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1586 psk_identity_len );
1587 if( ret != 0 )
1588 ssl_conf_remove_psk( conf );
1589
1590 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001591}
1592
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001593int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1594 mbedtls_svc_key_id_t psk )
1595{
1596 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1597 ( ssl->handshake == NULL ) )
1598 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1599
1600 ssl_remove_psk( ssl );
1601 ssl->handshake->psk_opaque = psk;
1602 return( 0 );
1603}
1604#endif /* MBEDTLS_USE_PSA_CRYPTO */
1605
1606void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1607 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1608 size_t),
1609 void *p_psk )
1610{
1611 conf->f_psk = f_psk;
1612 conf->p_psk = p_psk;
1613}
1614#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1615
1616#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01001617psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01001618 size_t taglen,
1619 psa_algorithm_t *alg,
1620 psa_key_type_t *key_type,
1621 size_t *key_size )
1622{
1623 switch ( mbedtls_cipher_type )
1624 {
1625 case MBEDTLS_CIPHER_AES_128_CBC:
1626 *alg = PSA_ALG_CBC_NO_PADDING;
1627 *key_type = PSA_KEY_TYPE_AES;
1628 *key_size = 128;
1629 break;
1630 case MBEDTLS_CIPHER_AES_128_CCM:
1631 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1632 *key_type = PSA_KEY_TYPE_AES;
1633 *key_size = 128;
1634 break;
1635 case MBEDTLS_CIPHER_AES_128_GCM:
1636 *alg = PSA_ALG_GCM;
1637 *key_type = PSA_KEY_TYPE_AES;
1638 *key_size = 128;
1639 break;
1640 case MBEDTLS_CIPHER_AES_192_CCM:
1641 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1642 *key_type = PSA_KEY_TYPE_AES;
1643 *key_size = 192;
1644 break;
1645 case MBEDTLS_CIPHER_AES_192_GCM:
1646 *alg = PSA_ALG_GCM;
1647 *key_type = PSA_KEY_TYPE_AES;
1648 *key_size = 192;
1649 break;
1650 case MBEDTLS_CIPHER_AES_256_CBC:
1651 *alg = PSA_ALG_CBC_NO_PADDING;
1652 *key_type = PSA_KEY_TYPE_AES;
1653 *key_size = 256;
1654 break;
1655 case MBEDTLS_CIPHER_AES_256_CCM:
1656 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1657 *key_type = PSA_KEY_TYPE_AES;
1658 *key_size = 256;
1659 break;
1660 case MBEDTLS_CIPHER_AES_256_GCM:
1661 *alg = PSA_ALG_GCM;
1662 *key_type = PSA_KEY_TYPE_AES;
1663 *key_size = 256;
1664 break;
1665 case MBEDTLS_CIPHER_ARIA_128_CBC:
1666 *alg = PSA_ALG_CBC_NO_PADDING;
1667 *key_type = PSA_KEY_TYPE_ARIA;
1668 *key_size = 128;
1669 break;
1670 case MBEDTLS_CIPHER_ARIA_128_CCM:
1671 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1672 *key_type = PSA_KEY_TYPE_ARIA;
1673 *key_size = 128;
1674 break;
1675 case MBEDTLS_CIPHER_ARIA_128_GCM:
1676 *alg = PSA_ALG_GCM;
1677 *key_type = PSA_KEY_TYPE_ARIA;
1678 *key_size = 128;
1679 break;
1680 case MBEDTLS_CIPHER_ARIA_192_CCM:
1681 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1682 *key_type = PSA_KEY_TYPE_ARIA;
1683 *key_size = 192;
1684 break;
1685 case MBEDTLS_CIPHER_ARIA_192_GCM:
1686 *alg = PSA_ALG_GCM;
1687 *key_type = PSA_KEY_TYPE_ARIA;
1688 *key_size = 192;
1689 break;
1690 case MBEDTLS_CIPHER_ARIA_256_CBC:
1691 *alg = PSA_ALG_CBC_NO_PADDING;
1692 *key_type = PSA_KEY_TYPE_ARIA;
1693 *key_size = 256;
1694 break;
1695 case MBEDTLS_CIPHER_ARIA_256_CCM:
1696 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1697 *key_type = PSA_KEY_TYPE_ARIA;
1698 *key_size = 256;
1699 break;
1700 case MBEDTLS_CIPHER_ARIA_256_GCM:
1701 *alg = PSA_ALG_GCM;
1702 *key_type = PSA_KEY_TYPE_ARIA;
1703 *key_size = 256;
1704 break;
1705 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
1706 *alg = PSA_ALG_CBC_NO_PADDING;
1707 *key_type = PSA_KEY_TYPE_CAMELLIA;
1708 *key_size = 128;
1709 break;
1710 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
1711 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1712 *key_type = PSA_KEY_TYPE_CAMELLIA;
1713 *key_size = 128;
1714 break;
1715 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
1716 *alg = PSA_ALG_GCM;
1717 *key_type = PSA_KEY_TYPE_CAMELLIA;
1718 *key_size = 128;
1719 break;
1720 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
1721 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1722 *key_type = PSA_KEY_TYPE_CAMELLIA;
1723 *key_size = 192;
1724 break;
1725 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
1726 *alg = PSA_ALG_GCM;
1727 *key_type = PSA_KEY_TYPE_CAMELLIA;
1728 *key_size = 192;
1729 break;
1730 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
1731 *alg = PSA_ALG_CBC_NO_PADDING;
1732 *key_type = PSA_KEY_TYPE_CAMELLIA;
1733 *key_size = 256;
1734 break;
1735 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
1736 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
1737 *key_type = PSA_KEY_TYPE_CAMELLIA;
1738 *key_size = 256;
1739 break;
1740 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
1741 *alg = PSA_ALG_GCM;
1742 *key_type = PSA_KEY_TYPE_CAMELLIA;
1743 *key_size = 256;
1744 break;
1745 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
1746 *alg = PSA_ALG_CHACHA20_POLY1305;
1747 *key_type = PSA_KEY_TYPE_CHACHA20;
1748 *key_size = 256;
1749 break;
1750 case MBEDTLS_CIPHER_NULL:
1751 *alg = MBEDTLS_SSL_NULL_CIPHER;
1752 *key_type = 0;
1753 *key_size = 0;
1754 break;
1755 default:
1756 return PSA_ERROR_NOT_SUPPORTED;
1757 }
1758
1759 return PSA_SUCCESS;
1760}
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001761#endif /* MBEDTLS_USE_PSA_CRYPTO */
1762
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02001763#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01001764int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
1765 const unsigned char *dhm_P, size_t P_len,
1766 const unsigned char *dhm_G, size_t G_len )
1767{
Janos Follath865b3eb2019-12-16 11:46:15 +00001768 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01001769
Glenn Strausscee11292021-12-20 01:43:17 -05001770 mbedtls_mpi_free( &conf->dhm_P );
1771 mbedtls_mpi_free( &conf->dhm_G );
1772
Hanno Beckera90658f2017-10-04 15:29:08 +01001773 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
1774 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
1775 {
1776 mbedtls_mpi_free( &conf->dhm_P );
1777 mbedtls_mpi_free( &conf->dhm_G );
1778 return( ret );
1779 }
1780
1781 return( 0 );
1782}
Paul Bakker5121ce52009-01-03 21:22:43 +00001783
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001784int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00001785{
Janos Follath865b3eb2019-12-16 11:46:15 +00001786 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00001787
Glenn Strausscee11292021-12-20 01:43:17 -05001788 mbedtls_mpi_free( &conf->dhm_P );
1789 mbedtls_mpi_free( &conf->dhm_G );
1790
Gilles Peskinee5702482021-06-11 21:59:08 +02001791 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
1792 &conf->dhm_P ) ) != 0 ||
1793 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
1794 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01001795 {
1796 mbedtls_mpi_free( &conf->dhm_P );
1797 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00001798 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01001799 }
Paul Bakker1b57b062011-01-06 15:48:19 +00001800
1801 return( 0 );
1802}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02001803#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00001804
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02001805#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
1806/*
1807 * Set the minimum length for Diffie-Hellman parameters
1808 */
1809void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
1810 unsigned int bitlen )
1811{
1812 conf->dhm_min_bitlen = bitlen;
1813}
1814#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
1815
Gilles Peskineeccd8882020-03-10 12:19:08 +01001816#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08001817#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02001818/*
1819 * Set allowed/preferred hashes for handshake signatures
1820 */
1821void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
1822 const int *hashes )
1823{
1824 conf->sig_hashes = hashes;
1825}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08001826#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01001827
Jerry Yuf017ee42022-01-12 15:49:48 +08001828/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01001829void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
1830 const uint16_t* sig_algs )
1831{
Jerry Yuf017ee42022-01-12 15:49:48 +08001832#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1833 conf->sig_hashes = NULL;
1834#endif /* !MBEDTLS_DEPRECATED_REMOVED */
1835 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01001836}
Gilles Peskineeccd8882020-03-10 12:19:08 +01001837#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02001838
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02001839#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01001840#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001841/*
1842 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01001843 *
1844 * mbedtls_ssl_setup() takes the provided list
1845 * and translates it to a list of IANA TLS group identifiers,
1846 * stored in ssl->handshake->group_list.
1847 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001848 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001849void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001850 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001851{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001852 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01001853 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001854}
Brett Warrene0edc842021-08-17 09:53:13 +01001855#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01001856#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01001857
Brett Warrene0edc842021-08-17 09:53:13 +01001858/*
1859 * Set the allowed groups
1860 */
1861void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
1862 const uint16_t *group_list )
1863{
1864#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
1865 conf->curve_list = NULL;
1866#endif
1867 conf->group_list = group_list;
1868}
1869
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001870#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00001872{
Hanno Becker947194e2017-04-07 13:25:49 +01001873 /* Initialize to suppress unnecessary compiler warning */
1874 size_t hostname_len = 0;
1875
1876 /* Check if new hostname is valid before
1877 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01001878 if( hostname != NULL )
1879 {
1880 hostname_len = strlen( hostname );
1881
1882 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
1883 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1884 }
1885
1886 /* Now it's clear that we will overwrite the old hostname,
1887 * so we can free it safely */
1888
1889 if( ssl->hostname != NULL )
1890 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001891 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01001892 mbedtls_free( ssl->hostname );
1893 }
1894
1895 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01001896
Paul Bakker5121ce52009-01-03 21:22:43 +00001897 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01001898 {
1899 ssl->hostname = NULL;
1900 }
1901 else
1902 {
1903 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01001904 if( ssl->hostname == NULL )
1905 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02001906
Hanno Becker947194e2017-04-07 13:25:49 +01001907 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02001908
Hanno Becker947194e2017-04-07 13:25:49 +01001909 ssl->hostname[hostname_len] = '\0';
1910 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001911
1912 return( 0 );
1913}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01001914#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001915
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001916#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001917void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00001919 const unsigned char *, size_t),
1920 void *p_sni )
1921{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001922 conf->f_sni = f_sni;
1923 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00001924}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001928int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001929{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001930 size_t cur_len, tot_len;
1931 const char **p;
1932
1933 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08001934 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
1935 * MUST NOT be truncated."
1936 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001937 */
1938 tot_len = 0;
1939 for( p = protos; *p != NULL; p++ )
1940 {
1941 cur_len = strlen( *p );
1942 tot_len += cur_len;
1943
Ronald Cron8216dd32020-04-23 16:41:44 +02001944 if( ( cur_len == 0 ) ||
1945 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
1946 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001948 }
1949
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001950 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001951
1952 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001953}
1954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001956{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001957 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001958}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001960
Johan Pascalb62bb512015-12-03 21:56:45 +01001961#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03001962void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
1963 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02001964{
1965 conf->dtls_srtp_mki_support = support_mki_value;
1966}
1967
Ron Eldoref72faf2018-07-12 11:54:20 +03001968int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
1969 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02001970 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02001971{
Johan Pascal5ef72d22020-10-28 17:05:47 +01001972 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02001973 {
Johan Pascald576fdb2020-09-22 10:39:53 +02001974 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02001975 }
Ron Eldor591f1622018-01-22 12:30:04 +02001976
1977 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02001978 {
Johan Pascald576fdb2020-09-22 10:39:53 +02001979 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02001980 }
Ron Eldor591f1622018-01-22 12:30:04 +02001981
1982 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
1983 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02001984 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02001985}
1986
Ron Eldoref72faf2018-07-12 11:54:20 +03001987int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02001988 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01001989{
Johan Pascal253d0262020-09-22 13:04:45 +02001990 const mbedtls_ssl_srtp_profile *p;
1991 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01001992
Johan Pascal253d0262020-09-22 13:04:45 +02001993 /* check the profiles list: all entry must be valid,
1994 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02001995 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
1996 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
1997 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02001998 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01001999 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002000 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002001 list_size++;
2002 }
2003 else
2004 {
2005 /* unsupported value, stop parsing and set the size to an error value */
2006 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002007 }
2008 }
2009
Johan Pascal5ef72d22020-10-28 17:05:47 +01002010 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002011 {
Johan Pascal253d0262020-09-22 13:04:45 +02002012 conf->dtls_srtp_profile_list = NULL;
2013 conf->dtls_srtp_profile_list_len = 0;
2014 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2015 }
2016
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002017 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002018 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002019
2020 return( 0 );
2021}
2022
Johan Pascal5ef72d22020-10-28 17:05:47 +01002023void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2024 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002025{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002026 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2027 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002028 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002029 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002030 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002031 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002032 else
2033 {
2034 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002035 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2036 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002037 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002038}
Johan Pascalb62bb512015-12-03 21:56:45 +01002039#endif /* MBEDTLS_SSL_DTLS_SRTP */
2040
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002041void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002042{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002043 conf->max_major_ver = major;
2044 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002045}
2046
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002047void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002048{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002049 conf->min_major_ver = major;
2050 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002051}
2052
Janos Follath088ce432017-04-10 12:42:31 +01002053#if defined(MBEDTLS_SSL_SRV_C)
2054void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2055 char cert_req_ca_list )
2056{
2057 conf->cert_req_ca_list = cert_req_ca_list;
2058}
2059#endif
2060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002062void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002063{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002064 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002065}
2066#endif
2067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002068#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002069void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002070{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002071 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002072}
2073#endif
2074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002076int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002077{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002079 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002082 }
2083
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002084 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002085
2086 return( 0 );
2087}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002089
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002090void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002091{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002092 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002093}
2094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002096void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002097{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002098 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002099}
2100
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002101void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002102{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002103 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002104}
2105
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002106void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002107 const unsigned char period[8] )
2108{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002109 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002110}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002114#if defined(MBEDTLS_SSL_CLI_C)
2115void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002116{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002117 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002118}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002119#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002120
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002121#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002122void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2123 mbedtls_ssl_ticket_write_t *f_ticket_write,
2124 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2125 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002126{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002127 conf->f_ticket_write = f_ticket_write;
2128 conf->f_ticket_parse = f_ticket_parse;
2129 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002130}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002131#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002133
Hanno Becker7e6c1782021-06-08 09:24:55 +01002134void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2135 mbedtls_ssl_export_keys_t *f_export_keys,
2136 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002137{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002138 ssl->f_export_keys = f_export_keys;
2139 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002140}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002141
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002142#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002143void mbedtls_ssl_conf_async_private_cb(
2144 mbedtls_ssl_config *conf,
2145 mbedtls_ssl_async_sign_t *f_async_sign,
2146 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2147 mbedtls_ssl_async_resume_t *f_async_resume,
2148 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002149 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002150{
2151 conf->f_async_sign_start = f_async_sign;
2152 conf->f_async_decrypt_start = f_async_decrypt;
2153 conf->f_async_resume = f_async_resume;
2154 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002155 conf->p_async_config_data = async_config_data;
2156}
2157
Gilles Peskine8f97af72018-04-26 11:46:10 +02002158void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2159{
2160 return( conf->p_async_config_data );
2161}
2162
Gilles Peskine1febfef2018-04-30 11:54:39 +02002163void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002164{
2165 if( ssl->handshake == NULL )
2166 return( NULL );
2167 else
2168 return( ssl->handshake->user_async_ctx );
2169}
2170
Gilles Peskine1febfef2018-04-30 11:54:39 +02002171void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002172 void *ctx )
2173{
2174 if( ssl->handshake != NULL )
2175 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002176}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002177#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002178
Paul Bakker5121ce52009-01-03 21:22:43 +00002179/*
2180 * SSL get accessors
2181 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002182uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002183{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002184 if( ssl->session != NULL )
2185 return( ssl->session->verify_result );
2186
2187 if( ssl->session_negotiate != NULL )
2188 return( ssl->session_negotiate->verify_result );
2189
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002190 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002191}
2192
Glenn Strauss8f526902022-01-13 00:04:49 -05002193int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2194{
2195 if( ssl == NULL || ssl->session == NULL )
2196 return( 0 );
2197
2198 return( ssl->session->ciphersuite );
2199}
2200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002202{
Paul Bakker926c8e42013-03-06 10:23:34 +01002203 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002204 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002207}
2208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002210{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002212 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002213 {
2214 switch( ssl->minor_ver )
2215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002217 return( "DTLSv1.2" );
2218
2219 default:
2220 return( "unknown (DTLS)" );
2221 }
2222 }
2223#endif
2224
Paul Bakker43ca69c2011-01-15 17:35:19 +00002225 switch( ssl->minor_ver )
2226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002228 return( "TLSv1.2" );
2229
Paul Bakker43ca69c2011-01-15 17:35:19 +00002230 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002231 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002232 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002233}
2234
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002235#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002236size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2237{
David Horstmann95d516f2021-05-04 18:36:56 +01002238 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002239 size_t read_mfl;
2240
2241 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2242 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2243 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2244 {
2245 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2246 }
2247
2248 /* Check if a smaller max length was negotiated */
2249 if( ssl->session_out != NULL )
2250 {
2251 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2252 if( read_mfl < max_len )
2253 {
2254 max_len = read_mfl;
2255 }
2256 }
2257
2258 // During a handshake, use the value being negotiated
2259 if( ssl->session_negotiate != NULL )
2260 {
2261 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2262 if( read_mfl < max_len )
2263 {
2264 max_len = read_mfl;
2265 }
2266 }
2267
2268 return( max_len );
2269}
2270
2271size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002272{
2273 size_t max_len;
2274
2275 /*
2276 * Assume mfl_code is correct since it was checked when set
2277 */
Angus Grattond8213d02016-05-25 20:56:48 +10002278 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002279
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002280 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002281 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002282 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002283 {
Angus Grattond8213d02016-05-25 20:56:48 +10002284 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002285 }
2286
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002287 /* During a handshake, use the value being negotiated */
2288 if( ssl->session_negotiate != NULL &&
2289 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2290 {
2291 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2292 }
2293
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002294 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002295}
2296#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2297
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002298#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002299size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002300{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002301 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2302 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2303 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2304 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2305 return ( 0 );
2306
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002307 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2308 return( ssl->mtu );
2309
2310 if( ssl->mtu == 0 )
2311 return( ssl->handshake->mtu );
2312
2313 return( ssl->mtu < ssl->handshake->mtu ?
2314 ssl->mtu : ssl->handshake->mtu );
2315}
2316#endif /* MBEDTLS_SSL_PROTO_DTLS */
2317
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002318int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2319{
2320 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2321
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002322#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2323 !defined(MBEDTLS_SSL_PROTO_DTLS)
2324 (void) ssl;
2325#endif
2326
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002327#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002328 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002329
2330 if( max_len > mfl )
2331 max_len = mfl;
2332#endif
2333
2334#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002335 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002336 {
Hanno Becker89490712020-02-05 10:50:12 +00002337 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002338 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2339 const size_t overhead = (size_t) ret;
2340
2341 if( ret < 0 )
2342 return( ret );
2343
2344 if( mtu <= overhead )
2345 {
2346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2347 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2348 }
2349
2350 if( max_len > mtu - overhead )
2351 max_len = mtu - overhead;
2352 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002353#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002354
Hanno Becker0defedb2018-08-10 12:35:02 +01002355#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2356 !defined(MBEDTLS_SSL_PROTO_DTLS)
2357 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002358#endif
2359
2360 return( (int) max_len );
2361}
2362
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002363int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2364{
2365 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2366
2367#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2368 (void) ssl;
2369#endif
2370
2371#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2372 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2373
2374 if( max_len > mfl )
2375 max_len = mfl;
2376#endif
2377
2378 return( (int) max_len );
2379}
2380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381#if defined(MBEDTLS_X509_CRT_PARSE_C)
2382const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002383{
2384 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002385 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002386
Hanno Beckere6824572019-02-07 13:18:46 +00002387#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002388 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002389#else
2390 return( NULL );
2391#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002392}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002396int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2397 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002398{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002399 int ret;
2400
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002401 if( ssl == NULL ||
2402 dst == NULL ||
2403 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002404 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002407 }
2408
Hanno Beckere810bbc2021-05-14 16:01:05 +01002409 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2410 * idempotent: Each session can only be exported once.
2411 *
2412 * (This is in preparation for TLS 1.3 support where we will
2413 * need the ability to export multiple sessions (aka tickets),
2414 * which will be achieved by calling mbedtls_ssl_get_session()
2415 * multiple times until it fails.)
2416 *
2417 * Check whether we have already exported the current session,
2418 * and fail if so.
2419 */
2420 if( ssl->session->exported == 1 )
2421 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2422
2423 ret = mbedtls_ssl_session_copy( dst, ssl->session );
2424 if( ret != 0 )
2425 return( ret );
2426
2427 /* Remember that we've exported the session. */
2428 ssl->session->exported = 1;
2429 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002430}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002432
Paul Bakker5121ce52009-01-03 21:22:43 +00002433/*
Hanno Beckera835da52019-05-16 12:39:07 +01002434 * Define ticket header determining Mbed TLS version
2435 * and structure of the ticket.
2436 */
2437
Hanno Becker94ef3b32019-05-16 12:50:45 +01002438/*
Hanno Becker50b59662019-05-28 14:30:45 +01002439 * Define bitflag determining compile-time settings influencing
2440 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01002441 */
2442
Hanno Becker50b59662019-05-28 14:30:45 +01002443#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01002444#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01002445#else
Hanno Becker3e088662019-05-29 11:10:18 +01002446#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002447#endif /* MBEDTLS_HAVE_TIME */
2448
2449#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01002450#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002451#else
Hanno Becker3e088662019-05-29 11:10:18 +01002452#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002453#endif /* MBEDTLS_X509_CRT_PARSE_C */
2454
2455#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01002456#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002457#else
Hanno Becker3e088662019-05-29 11:10:18 +01002458#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002459#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
2460
2461#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01002462#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002463#else
Hanno Becker3e088662019-05-29 11:10:18 +01002464#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002465#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2466
Hanno Becker94ef3b32019-05-16 12:50:45 +01002467#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01002468#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002469#else
Hanno Becker3e088662019-05-29 11:10:18 +01002470#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002471#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2472
Hanno Becker94ef3b32019-05-16 12:50:45 +01002473#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2474#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
2475#else
2476#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
2477#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2478
Hanno Becker3e088662019-05-29 11:10:18 +01002479#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
2480#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
2481#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
2482#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01002483#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
2484#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01002485
Hanno Becker50b59662019-05-28 14:30:45 +01002486#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01002487 ( (uint16_t) ( \
2488 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
2489 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
2490 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
2491 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01002492 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01002493 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01002494
Hanno Beckerf8787072019-05-16 12:41:07 +01002495static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01002496 MBEDTLS_VERSION_MAJOR,
2497 MBEDTLS_VERSION_MINOR,
2498 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01002499 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
2500 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01002501};
Hanno Beckera835da52019-05-16 12:39:07 +01002502
2503/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002504 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002505 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002506 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002507 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002508 *
Hanno Beckerdce50972021-08-01 05:39:23 +01002509 * opaque mbedtls_version[3]; // library version: major, minor, patch
2510 * opaque session_format[2]; // library-version specific 16-bit field
2511 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002512 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002513 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002514 * Note: When updating the format, remember to keep
2515 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002516 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002517 * // In this version, `session_format` determines
2518 * // the setting of those compile-time
2519 * // configuration options which influence
2520 * // the structure of mbedtls_ssl_session.
2521 *
Hanno Beckerfa0d61e2021-08-02 08:56:14 +01002522 * uint8_t minor_ver; // Protocol-version. Possible values:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002523 * // - TLS 1.2 (MBEDTLS_SSL_MINOR_VERSION_3)
2524 *
2525 * select (serialized_session.minor_ver) {
2526 *
2527 * case MBEDTLS_SSL_MINOR_VERSION_3: // TLS 1.2
2528 * serialized_session_tls12 data;
2529 *
2530 * };
2531 *
2532 * } serialized_session;
2533 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002534 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002535
2536#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2537/* Serialization of TLS 1.2 sessions:
2538 *
2539 * struct {
2540 * uint64 start_time;
2541 * uint8 ciphersuite[2]; // defined by the standard
2542 * uint8 compression; // 0 or 1
2543 * uint8 session_id_len; // at most 32
2544 * opaque session_id[32];
2545 * opaque master[48]; // fixed length in the standard
2546 * uint32 verify_result;
2547 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
2548 * opaque ticket<0..2^24-1>; // length 0 means no ticket
2549 * uint32 ticket_lifetime;
2550 * uint8 mfl_code; // up to 255 according to standard
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002551 * uint8 encrypt_then_mac; // 0 or 1
2552 * } serialized_session_tls12;
2553 *
2554 */
2555static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session,
2556 unsigned char *buf,
2557 size_t buf_len )
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002558{
2559 unsigned char *p = buf;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02002560 size_t used = 0;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002561
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002562#if defined(MBEDTLS_HAVE_TIME)
2563 uint64_t start;
2564#endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002565#if defined(MBEDTLS_X509_CRT_PARSE_C)
2566#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2567 size_t cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002568#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2569#endif /* MBEDTLS_X509_CRT_PARSE_C */
2570
Hanno Beckera835da52019-05-16 12:39:07 +01002571 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002572 * Time
2573 */
2574#if defined(MBEDTLS_HAVE_TIME)
2575 used += 8;
2576
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02002577 if( used <= buf_len )
2578 {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002579 start = (uint64_t) session->start;
2580
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002581 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
2582 p += 8;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002583 }
2584#endif /* MBEDTLS_HAVE_TIME */
2585
2586 /*
2587 * Basic mandatory fields
2588 */
2589 used += 2 /* ciphersuite */
2590 + 1 /* compression */
2591 + 1 /* id_len */
2592 + sizeof( session->id )
2593 + sizeof( session->master )
2594 + 4; /* verify_result */
2595
2596 if( used <= buf_len )
2597 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002598 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
2599 p += 2;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002600
Joe Subbiani2194dc42021-07-14 12:31:31 +01002601 *p++ = MBEDTLS_BYTE_0( session->compression );
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002602
Joe Subbiani2194dc42021-07-14 12:31:31 +01002603 *p++ = MBEDTLS_BYTE_0( session->id_len );
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002604 memcpy( p, session->id, 32 );
2605 p += 32;
2606
2607 memcpy( p, session->master, 48 );
2608 p += 48;
2609
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002610 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
2611 p += 4;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02002612 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002613
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002614 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002615 * Peer's end-entity certificate
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002616 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002617#if defined(MBEDTLS_X509_CRT_PARSE_C)
2618#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2619 if( session->peer_cert == NULL )
2620 cert_len = 0;
2621 else
2622 cert_len = session->peer_cert->raw.len;
2623
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02002624 used += 3 + cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002625
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02002626 if( used <= buf_len )
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002627 {
Joe Subbiani2194dc42021-07-14 12:31:31 +01002628 *p++ = MBEDTLS_BYTE_2( cert_len );
2629 *p++ = MBEDTLS_BYTE_1( cert_len );
2630 *p++ = MBEDTLS_BYTE_0( cert_len );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002631
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02002632 if( session->peer_cert != NULL )
2633 {
2634 memcpy( p, session->peer_cert->raw.p, cert_len );
2635 p += cert_len;
2636 }
2637 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002638#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002639 if( session->peer_cert_digest != NULL )
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02002640 {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002641 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
2642 if( used <= buf_len )
2643 {
2644 *p++ = (unsigned char) session->peer_cert_digest_type;
2645 *p++ = (unsigned char) session->peer_cert_digest_len;
2646 memcpy( p, session->peer_cert_digest,
2647 session->peer_cert_digest_len );
2648 p += session->peer_cert_digest_len;
2649 }
2650 }
2651 else
2652 {
2653 used += 2;
2654 if( used <= buf_len )
2655 {
2656 *p++ = (unsigned char) MBEDTLS_MD_NONE;
2657 *p++ = 0;
2658 }
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02002659 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002660#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2661#endif /* MBEDTLS_X509_CRT_PARSE_C */
2662
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002663 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002664 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002665 */
2666#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002667 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002668
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02002669 if( used <= buf_len )
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002670 {
Joe Subbiani2194dc42021-07-14 12:31:31 +01002671 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
2672 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
2673 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02002674
2675 if( session->ticket != NULL )
2676 {
2677 memcpy( p, session->ticket, session->ticket_len );
2678 p += session->ticket_len;
2679 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002680
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01002681 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2682 p += 4;
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002683 }
2684#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
2685
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002686 /*
2687 * Misc extension-related info
2688 */
2689#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2690 used += 1;
2691
2692 if( used <= buf_len )
2693 *p++ = session->mfl_code;
2694#endif
2695
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002696#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2697 used += 1;
2698
2699 if( used <= buf_len )
Joe Subbiani2194dc42021-07-14 12:31:31 +01002700 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002701#endif
2702
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002703 return( used );
2704}
2705#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02002706
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002707static int ssl_session_save( const mbedtls_ssl_session *session,
2708 unsigned char omit_header,
2709 unsigned char *buf,
2710 size_t buf_len,
2711 size_t *olen )
2712{
2713 unsigned char *p = buf;
2714 size_t used = 0;
2715
2716 if( !omit_header )
2717 {
2718 /*
2719 * Add Mbed TLS version identifier
2720 */
2721
2722 used += sizeof( ssl_serialized_session_header );
2723
2724 if( used <= buf_len )
2725 {
2726 memcpy( p, ssl_serialized_session_header,
2727 sizeof( ssl_serialized_session_header ) );
2728 p += sizeof( ssl_serialized_session_header );
2729 }
2730 }
2731
2732 /*
2733 * TLS version identifier
2734 */
2735 used += 1;
2736 if( used <= buf_len )
2737 {
2738 *p++ = session->minor_ver;
2739 }
2740
2741 /* Forward to version-specific serialization routine. */
2742 switch( session->minor_ver )
2743 {
2744#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2745 case MBEDTLS_SSL_MINOR_VERSION_3:
2746 {
2747 size_t remaining_len = used <= buf_len ? buf_len - used : 0;
2748 used += ssl_session_save_tls12( session, p, remaining_len );
2749 break;
2750 }
2751#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2752
2753 default:
2754 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2755 }
2756
2757 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02002758 if( used > buf_len )
2759 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002760
2761 return( 0 );
2762}
2763
2764/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02002765 * Public wrapper for ssl_session_save()
2766 */
2767int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
2768 unsigned char *buf,
2769 size_t buf_len,
2770 size_t *olen )
2771{
2772 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
2773}
Jerry Yuc3091b12021-12-23 14:57:39 +08002774#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02002775/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02002776 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02002777 *
2778 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02002779 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002780 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002781static int ssl_session_load_tls12( mbedtls_ssl_session *session,
2782 const unsigned char *buf,
2783 size_t len )
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002784{
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002785#if defined(MBEDTLS_HAVE_TIME)
2786 uint64_t start;
2787#endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002788#if defined(MBEDTLS_X509_CRT_PARSE_C)
2789#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2790 size_t cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002791#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2792#endif /* MBEDTLS_X509_CRT_PARSE_C */
2793
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002794 const unsigned char *p = buf;
2795 const unsigned char * const end = buf + len;
Hanno Beckera835da52019-05-16 12:39:07 +01002796
2797 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002798 * Time
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002799 */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002800#if defined(MBEDTLS_HAVE_TIME)
2801 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2803
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002804 start = ( (uint64_t) p[0] << 56 ) |
2805 ( (uint64_t) p[1] << 48 ) |
2806 ( (uint64_t) p[2] << 40 ) |
2807 ( (uint64_t) p[3] << 32 ) |
2808 ( (uint64_t) p[4] << 24 ) |
2809 ( (uint64_t) p[5] << 16 ) |
2810 ( (uint64_t) p[6] << 8 ) |
2811 ( (uint64_t) p[7] );
2812 p += 8;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002813
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002814 session->start = (time_t) start;
2815#endif /* MBEDTLS_HAVE_TIME */
2816
2817 /*
2818 * Basic mandatory fields
2819 */
2820 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
2821 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2822
2823 session->ciphersuite = ( p[0] << 8 ) | p[1];
2824 p += 2;
2825
2826 session->compression = *p++;
2827
2828 session->id_len = *p++;
2829 memcpy( session->id, p, 32 );
2830 p += 32;
2831
2832 memcpy( session->master, p, 48 );
2833 p += 48;
2834
2835 session->verify_result = ( (uint32_t) p[0] << 24 ) |
2836 ( (uint32_t) p[1] << 16 ) |
2837 ( (uint32_t) p[2] << 8 ) |
2838 ( (uint32_t) p[3] );
2839 p += 4;
2840
2841 /* Immediately clear invalid pointer values that have been read, in case
2842 * we exit early before we replaced them with valid ones. */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002843#if defined(MBEDTLS_X509_CRT_PARSE_C)
2844#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2845 session->peer_cert = NULL;
2846#else
2847 session->peer_cert_digest = NULL;
2848#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2849#endif /* MBEDTLS_X509_CRT_PARSE_C */
2850#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
2851 session->ticket = NULL;
2852#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
2853
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002854 /*
2855 * Peer certificate
2856 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002857#if defined(MBEDTLS_X509_CRT_PARSE_C)
2858#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2859 /* Deserialize CRT from the end of the ticket. */
2860 if( 3 > (size_t)( end - p ) )
2861 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2862
2863 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
2864 p += 3;
2865
2866 if( cert_len != 0 )
2867 {
Janos Follath865b3eb2019-12-16 11:46:15 +00002868 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002869
2870 if( cert_len > (size_t)( end - p ) )
2871 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2872
2873 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
2874
2875 if( session->peer_cert == NULL )
2876 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2877
2878 mbedtls_x509_crt_init( session->peer_cert );
2879
2880 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
2881 p, cert_len ) ) != 0 )
2882 {
2883 mbedtls_x509_crt_free( session->peer_cert );
2884 mbedtls_free( session->peer_cert );
2885 session->peer_cert = NULL;
2886 return( ret );
2887 }
2888
2889 p += cert_len;
2890 }
2891#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2892 /* Deserialize CRT digest from the end of the ticket. */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002893 if( 2 > (size_t)( end - p ) )
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002894 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2895
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002896 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
2897 session->peer_cert_digest_len = (size_t) *p++;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002898
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002899 if( session->peer_cert_digest_len != 0 )
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002900 {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002901 const mbedtls_md_info_t *md_info =
2902 mbedtls_md_info_from_type( session->peer_cert_digest_type );
2903 if( md_info == NULL )
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002904 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002905 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
2906 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002907
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002908 if( session->peer_cert_digest_len > (size_t)( end - p ) )
2909 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2910
2911 session->peer_cert_digest =
2912 mbedtls_calloc( 1, session->peer_cert_digest_len );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002913 if( session->peer_cert_digest == NULL )
2914 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2915
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002916 memcpy( session->peer_cert_digest, p,
2917 session->peer_cert_digest_len );
2918 p += session->peer_cert_digest_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002919 }
2920#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2921#endif /* MBEDTLS_X509_CRT_PARSE_C */
2922
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002923 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002924 * Session ticket and associated data
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002925 */
2926#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
2927 if( 3 > (size_t)( end - p ) )
2928 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2929
2930 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
2931 p += 3;
2932
2933 if( session->ticket_len != 0 )
2934 {
2935 if( session->ticket_len > (size_t)( end - p ) )
2936 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2937
2938 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2939 if( session->ticket == NULL )
2940 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2941
2942 memcpy( session->ticket, p, session->ticket_len );
2943 p += session->ticket_len;
2944 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002945
2946 if( 4 > (size_t)( end - p ) )
2947 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2948
2949 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
2950 ( (uint32_t) p[1] << 16 ) |
2951 ( (uint32_t) p[2] << 8 ) |
2952 ( (uint32_t) p[3] );
2953 p += 4;
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002954#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
2955
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002956 /*
2957 * Misc extension-related info
2958 */
2959#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2960 if( 1 > (size_t)( end - p ) )
2961 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2962
2963 session->mfl_code = *p++;
2964#endif
2965
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02002966#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2967 if( 1 > (size_t)( end - p ) )
2968 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2969
2970 session->encrypt_then_mac = *p++;
2971#endif
2972
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002973 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002974 if( p != end )
2975 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2976
2977 return( 0 );
2978}
Jerry Yuc5aef882021-12-23 20:15:02 +08002979#endif /*MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002980static int ssl_session_load( mbedtls_ssl_session *session,
2981 unsigned char omit_header,
2982 const unsigned char *buf,
2983 size_t len )
2984{
2985 const unsigned char *p = buf;
2986 const unsigned char * const end = buf + len;
2987
2988 if( !omit_header )
2989 {
2990 /*
2991 * Check Mbed TLS version identifier
2992 */
2993
2994 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
2995 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2996
2997 if( memcmp( p, ssl_serialized_session_header,
2998 sizeof( ssl_serialized_session_header ) ) != 0 )
2999 {
3000 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3001 }
3002 p += sizeof( ssl_serialized_session_header );
3003 }
3004
3005 /*
3006 * TLS version identifier
3007 */
3008 if( 1 > (size_t)( end - p ) )
3009 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3010 session->minor_ver = *p++;
3011
3012 /* Dispatch according to TLS version. */
3013 switch( session->minor_ver )
3014 {
3015#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3016 case MBEDTLS_SSL_MINOR_VERSION_3: /* TLS 1.2 */
3017 {
3018 size_t remaining_len = ( end - p );
3019 return( ssl_session_load_tls12( session, p, remaining_len ) );
3020 }
3021#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3022
3023 default:
3024 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3025 }
3026}
3027
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003028/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003029 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003030 */
3031int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3032 const unsigned char *buf,
3033 size_t len )
3034{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003035 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003036
3037 if( ret != 0 )
3038 mbedtls_ssl_session_free( session );
3039
3040 return( ret );
3041}
3042
3043/*
Paul Bakker1961b702013-01-25 14:49:24 +01003044 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003045 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003046static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3047{
3048 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3049
3050 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3051 return( ret );
3052
3053#if defined(MBEDTLS_SSL_PROTO_DTLS)
3054 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3055 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3056 {
3057 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3058 return( ret );
3059 }
3060#endif /* MBEDTLS_SSL_PROTO_DTLS */
3061
3062 return( ret );
3063}
3064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003066{
Hanno Becker41934dd2021-08-07 19:13:43 +01003067 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003068
Hanno Becker41934dd2021-08-07 19:13:43 +01003069 if( ssl == NULL ||
3070 ssl->conf == NULL ||
3071 ssl->handshake == NULL ||
3072 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3073 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003074 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003075 }
3076
3077 ret = ssl_prepare_handshake_step( ssl );
3078 if( ret != 0 )
3079 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003080
Jerry Yue7047812021-09-13 19:26:39 +08003081 ret = mbedtls_ssl_handle_pending_alert( ssl );
3082 if( ret != 0 )
3083 goto cleanup;
3084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003085#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003086 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003087 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003088#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003089 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yuf4436812021-08-26 22:59:56 +08003090 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003091#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003092
3093#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3094 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3095 ret = mbedtls_ssl_handshake_client_step( ssl );
3096#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3097 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003098#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003100 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003101 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003102#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003103 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003104 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003105#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003106
3107#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3108 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3109 ret = mbedtls_ssl_handshake_server_step( ssl );
3110#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3111 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003112#endif
3113
Jerry Yue7047812021-09-13 19:26:39 +08003114 if( ret != 0 )
3115 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003116 /* handshake_step return error. And it is same
3117 * with alert_reason.
3118 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003119 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003120 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003121 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003122 goto cleanup;
3123 }
3124 }
3125
3126cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003127 return( ret );
3128}
3129
3130/*
3131 * Perform the SSL handshake
3132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003134{
3135 int ret = 0;
3136
Hanno Beckera817ea42020-10-20 15:20:23 +01003137 /* Sanity checks */
3138
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003139 if( ssl == NULL || ssl->conf == NULL )
3140 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3141
Hanno Beckera817ea42020-10-20 15:20:23 +01003142#if defined(MBEDTLS_SSL_PROTO_DTLS)
3143 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3144 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3145 {
3146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3147 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3148 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3149 }
3150#endif /* MBEDTLS_SSL_PROTO_DTLS */
3151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003153
Hanno Beckera817ea42020-10-20 15:20:23 +01003154 /* Main handshake loop */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01003156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003158
3159 if( ret != 0 )
3160 break;
3161 }
3162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003163 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003164
3165 return( ret );
3166}
3167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003168#if defined(MBEDTLS_SSL_RENEGOTIATION)
3169#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003170/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003171 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003174{
Janos Follath865b3eb2019-12-16 11:46:15 +00003175 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003178
3179 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3181 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003182
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003183 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003184 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003185 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003186 return( ret );
3187 }
3188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003190
3191 return( 0 );
3192}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003194
3195/*
3196 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197 * - any side: calling mbedtls_ssl_renegotiate(),
3198 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3199 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003200 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003201 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003203 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003204int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003205{
Janos Follath865b3eb2019-12-16 11:46:15 +00003206 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003209
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003210 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3211 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003212
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003213 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3214 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003216 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003218 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003219 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003220 ssl->handshake->out_msg_seq = 1;
3221 else
3222 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003223 }
3224#endif
3225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3227 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003232 return( ret );
3233 }
3234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003236
3237 return( 0 );
3238}
3239
3240/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003241 * Renegotiate current connection on client,
3242 * or request renegotiation on server
3243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003245{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003247
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003248 if( ssl == NULL || ssl->conf == NULL )
3249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003252 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003253 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
3256 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003259
3260 /* Did we already try/start sending HelloRequest? */
3261 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003262 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003263
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003264 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003265 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003268#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003269 /*
3270 * On client, either start the renegotiation process or,
3271 * if already in progress, continue the handshake
3272 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003273 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
3276 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003277
Hanno Becker40cdaa12020-02-05 10:48:27 +00003278 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003279 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003280 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003281 return( ret );
3282 }
3283 }
3284 else
3285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003286 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003289 return( ret );
3290 }
3291 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003292#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003293
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003294 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003295}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003296#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003298#if defined(MBEDTLS_X509_CRT_PARSE_C)
3299static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003300{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003302
3303 while( cur != NULL )
3304 {
3305 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003307 cur = next;
3308 }
3309}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003311
Gilles Peskine9b562d52018-04-25 20:32:43 +02003312void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003313{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003314 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3315
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003316 if( handshake == NULL )
3317 return;
3318
Brett Warrene0edc842021-08-17 09:53:13 +01003319#if defined(MBEDTLS_ECP_C)
3320#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3321 if ( ssl->handshake->group_list_heap_allocated )
3322 mbedtls_free( (void*) handshake->group_list );
3323 handshake->group_list = NULL;
3324#endif /* MBEDTLS_DEPRECATED_REMOVED */
3325#endif /* MBEDTLS_ECP_C */
3326
Jerry Yuf017ee42022-01-12 15:49:48 +08003327#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3328#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3329 if ( ssl->handshake->sig_algs_heap_allocated )
3330 mbedtls_free( (void*) handshake->sig_algs );
3331 handshake->sig_algs = NULL;
3332#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003333#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3334 if( ssl->handshake->certificate_request_context )
3335 {
3336 mbedtls_free( (void*) handshake->certificate_request_context );
3337 }
3338#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003339#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3340
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003341#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3342 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3343 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003344 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003345 handshake->async_in_progress = 0;
3346 }
3347#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3348
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003349#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003350#if defined(MBEDTLS_USE_PSA_CRYPTO)
3351 psa_hash_abort( &handshake->fin_sha256_psa );
3352#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003353 mbedtls_sha256_free( &handshake->fin_sha256 );
3354#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003355#endif
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +02003356#if defined(MBEDTLS_SHA384_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003357#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003358 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003359#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003360 mbedtls_sha512_free( &handshake->fin_sha512 );
3361#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003362#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364#if defined(MBEDTLS_DHM_C)
3365 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003366#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003367#if defined(MBEDTLS_ECDH_C)
3368 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003369#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003370#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003371 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003372#if defined(MBEDTLS_SSL_CLI_C)
3373 mbedtls_free( handshake->ecjpake_cache );
3374 handshake->ecjpake_cache = NULL;
3375 handshake->ecjpake_cache_len = 0;
3376#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003377#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003378
Janos Follath4ae5c292016-02-10 11:27:43 +00003379#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3380 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003381 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003383#endif
3384
Gilles Peskineeccd8882020-03-10 12:19:08 +01003385#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003386 if( handshake->psk != NULL )
3387 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003388 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003389 mbedtls_free( handshake->psk );
3390 }
3391#endif
3392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3394 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003395 /*
3396 * Free only the linked list wrapper, not the keys themselves
3397 * since the belong to the SNI callback
3398 */
3399 if( handshake->sni_key_cert != NULL )
3400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003401 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003402
3403 while( cur != NULL )
3404 {
3405 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003407 cur = next;
3408 }
3409 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003410#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003411
Gilles Peskineeccd8882020-03-10 12:19:08 +01003412#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003413 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003414 if( handshake->ecrs_peer_cert != NULL )
3415 {
3416 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3417 mbedtls_free( handshake->ecrs_peer_cert );
3418 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003419#endif
3420
Hanno Becker75173122019-02-06 16:18:31 +00003421#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3422 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3423 mbedtls_pk_free( &handshake->peer_pubkey );
3424#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3425
XiaokangQian34909742022-01-27 02:25:04 +00003426#if defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427 mbedtls_free( handshake->verify_cookie );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003428#endif /* MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 */
3429
3430#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003431 mbedtls_ssl_flight_free( handshake->flight );
3432 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003433#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003434
Hanno Becker4a63ed42019-01-08 11:39:35 +00003435#if defined(MBEDTLS_ECDH_C) && \
3436 defined(MBEDTLS_USE_PSA_CRYPTO)
3437 psa_destroy_key( handshake->ecdh_psa_privkey );
3438#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3439
Ronald Cron6f135e12021-12-08 16:57:54 +01003440#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003441 mbedtls_ssl_transform_free( handshake->transform_handshake );
3442 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003443 mbedtls_free( handshake->transform_earlydata );
3444 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003445#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003446
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003447
3448#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3449 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3450 * processes datagrams and the fact that a datagram is allowed to have
3451 * several records in it, it is possible that the I/O buffers are not
3452 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003453 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3454 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003455#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003456
Jerry Yuba9c7272021-10-30 11:54:10 +08003457 /* mbedtls_platform_zeroize MUST be last one in this function */
3458 mbedtls_platform_zeroize( handshake,
3459 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003460}
3461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003463{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003464 if( session == NULL )
3465 return;
3466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003468 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003469#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003470
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003471#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003473#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003474
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003475 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003476}
3477
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003478#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003479
3480#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3481#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3482#else
3483#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3484#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3485
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003486#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003487
3488#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3489#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3490#else
3491#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3492#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3493
3494#if defined(MBEDTLS_SSL_ALPN)
3495#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3496#else
3497#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3498#endif /* MBEDTLS_SSL_ALPN */
3499
3500#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3501#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3502#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3503#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3504
3505#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3506 ( (uint32_t) ( \
3507 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3508 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3509 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3510 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3511 0u ) )
3512
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003513static unsigned char ssl_serialized_context_header[] = {
3514 MBEDTLS_VERSION_MAJOR,
3515 MBEDTLS_VERSION_MINOR,
3516 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003517 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3518 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3519 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3520 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3521 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003522};
3523
Paul Bakker5121ce52009-01-03 21:22:43 +00003524/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003525 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003526 *
3527 * The format of the serialized data is:
3528 * (in the presentation language of TLS, RFC 8446 section 3)
3529 *
3530 * // header
3531 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003532 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003533 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003534 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003535 * Note: When updating the format, remember to keep these
3536 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003537 *
3538 * // session sub-structure
3539 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3540 * // transform sub-structure
3541 * uint8 random[64]; // ServerHello.random+ClientHello.random
3542 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3543 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3544 * // fields from ssl_context
3545 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3546 * uint64 in_window_top; // DTLS: last validated record seq_num
3547 * uint64 in_window; // DTLS: bitmask for replay protection
3548 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3549 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3550 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3551 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3552 *
3553 * Note that many fields of the ssl_context or sub-structures are not
3554 * serialized, as they fall in one of the following categories:
3555 *
3556 * 1. forced value (eg in_left must be 0)
3557 * 2. pointer to dynamically-allocated memory (eg session, transform)
3558 * 3. value can be re-derived from other data (eg session keys from MS)
3559 * 4. value was temporary (eg content of input buffer)
3560 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003561 */
3562int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3563 unsigned char *buf,
3564 size_t buf_len,
3565 size_t *olen )
3566{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003567 unsigned char *p = buf;
3568 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003569 size_t session_len;
3570 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003571
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003572 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003573 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3574 * this function's documentation.
3575 *
3576 * These are due to assumptions/limitations in the implementation. Some of
3577 * them are likely to stay (no handshake in progress) some might go away
3578 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003579 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003580 /* The initial handshake must be over */
3581 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003582 {
3583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003584 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003585 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003586 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003587 {
3588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003589 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003590 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003591 /* Double-check that sub-structures are indeed ready */
3592 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003593 {
3594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003595 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003596 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003597 /* There must be no pending incoming or outgoing data */
3598 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003599 {
3600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003601 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003602 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003603 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003604 {
3605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003606 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003607 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003608 /* Protocol must be DLTS, not TLS */
3609 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003610 {
3611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003612 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003613 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003614 /* Version must be 1.2 */
3615 if( ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003616 {
3617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003618 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003619 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003620 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003621 {
3622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003623 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003624 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003625 /* We must be using an AEAD ciphersuite */
3626 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003627 {
3628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003629 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003630 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003631 /* Renegotiation must not be enabled */
3632#if defined(MBEDTLS_SSL_RENEGOTIATION)
3633 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003634 {
3635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003636 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003637 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003638#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003639
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003640 /*
3641 * Version and format identifier
3642 */
3643 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003644
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003645 if( used <= buf_len )
3646 {
3647 memcpy( p, ssl_serialized_context_header,
3648 sizeof( ssl_serialized_context_header ) );
3649 p += sizeof( ssl_serialized_context_header );
3650 }
3651
3652 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003653 * Session (length + data)
3654 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003655 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003656 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3657 return( ret );
3658
3659 used += 4 + session_len;
3660 if( used <= buf_len )
3661 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003662 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3663 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003664
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003665 ret = ssl_session_save( ssl->session, 1,
3666 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003667 if( ret != 0 )
3668 return( ret );
3669
3670 p += session_len;
3671 }
3672
3673 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003674 * Transform
3675 */
3676 used += sizeof( ssl->transform->randbytes );
3677 if( used <= buf_len )
3678 {
3679 memcpy( p, ssl->transform->randbytes,
3680 sizeof( ssl->transform->randbytes ) );
3681 p += sizeof( ssl->transform->randbytes );
3682 }
3683
3684#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3685 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3686 if( used <= buf_len )
3687 {
3688 *p++ = ssl->transform->in_cid_len;
3689 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3690 p += ssl->transform->in_cid_len;
3691
3692 *p++ = ssl->transform->out_cid_len;
3693 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3694 p += ssl->transform->out_cid_len;
3695 }
3696#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3697
3698 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003699 * Saved fields from top-level ssl_context structure
3700 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003701 used += 4;
3702 if( used <= buf_len )
3703 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003704 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3705 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003706 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003707
3708#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3709 used += 16;
3710 if( used <= buf_len )
3711 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003712 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3713 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003714
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003715 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3716 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003717 }
3718#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3719
3720#if defined(MBEDTLS_SSL_PROTO_DTLS)
3721 used += 1;
3722 if( used <= buf_len )
3723 {
3724 *p++ = ssl->disable_datagram_packing;
3725 }
3726#endif /* MBEDTLS_SSL_PROTO_DTLS */
3727
Jerry Yuae0b2e22021-10-08 15:21:19 +08003728 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003729 if( used <= buf_len )
3730 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003731 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3732 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003733 }
3734
3735#if defined(MBEDTLS_SSL_PROTO_DTLS)
3736 used += 2;
3737 if( used <= buf_len )
3738 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003739 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
3740 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003741 }
3742#endif /* MBEDTLS_SSL_PROTO_DTLS */
3743
3744#if defined(MBEDTLS_SSL_ALPN)
3745 {
3746 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02003747 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003748 : 0;
3749
3750 used += 1 + alpn_len;
3751 if( used <= buf_len )
3752 {
3753 *p++ = alpn_len;
3754
3755 if( ssl->alpn_chosen != NULL )
3756 {
3757 memcpy( p, ssl->alpn_chosen, alpn_len );
3758 p += alpn_len;
3759 }
3760 }
3761 }
3762#endif /* MBEDTLS_SSL_ALPN */
3763
3764 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003765 * Done
3766 */
3767 *olen = used;
3768
3769 if( used > buf_len )
3770 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003771
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003772 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
3773
Hanno Becker43aefe22020-02-05 10:44:56 +00003774 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003775}
3776
3777/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003778 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003779 *
3780 * This internal version is wrapped by a public function that cleans up in
3781 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003782 */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003783static int ssl_context_load( mbedtls_ssl_context *ssl,
3784 const unsigned char *buf,
3785 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003786{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003787 const unsigned char *p = buf;
3788 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003789 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00003790 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003791
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003792 /*
3793 * The context should have been freshly setup or reset.
3794 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02003795 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003796 * renegotiating, or if the user mistakenly loaded a session first.)
3797 */
3798 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
3799 ssl->session != NULL )
3800 {
3801 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3802 }
3803
3804 /*
3805 * We can't check that the config matches the initial one, but we can at
3806 * least check it matches the requirements for serializing.
3807 */
3808 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
3809 ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
3810 ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 ||
3811 ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ||
3812 ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 ||
3813#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003814 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003815#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003816 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003817 {
3818 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3819 }
3820
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003821 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
3822
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003823 /*
3824 * Check version identifier
3825 */
3826 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
3827 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3828
3829 if( memcmp( p, ssl_serialized_context_header,
3830 sizeof( ssl_serialized_context_header ) ) != 0 )
3831 {
3832 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3833 }
3834 p += sizeof( ssl_serialized_context_header );
3835
3836 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003837 * Session
3838 */
3839 if( (size_t)( end - p ) < 4 )
3840 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3841
3842 session_len = ( (size_t) p[0] << 24 ) |
3843 ( (size_t) p[1] << 16 ) |
3844 ( (size_t) p[2] << 8 ) |
3845 ( (size_t) p[3] );
3846 p += 4;
3847
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003848 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00003849 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003850 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003851 ssl->session_in = ssl->session;
3852 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003853 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003854
3855 if( (size_t)( end - p ) < session_len )
3856 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3857
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003858 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003859 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003860 {
3861 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003862 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003863 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003864
3865 p += session_len;
3866
3867 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003868 * Transform
3869 */
3870
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003871 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00003872 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003873 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003874 ssl->transform_in = ssl->transform;
3875 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02003876 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003877
3878 /* Read random bytes and populate structure */
3879 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
3880 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3881
Hanno Beckerbd257552021-03-22 06:59:27 +00003882 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003883 ssl->session->ciphersuite,
3884 ssl->session->master,
Hanno Beckerbd257552021-03-22 06:59:27 +00003885#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
3886 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003887 ssl->session->encrypt_then_mac,
Hanno Beckerbd257552021-03-22 06:59:27 +00003888#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
3889 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003890 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
3891 p, /* currently pointing to randbytes */
3892 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
3893 ssl->conf->endpoint,
3894 ssl );
3895 if( ret != 0 )
3896 return( ret );
3897
3898 p += sizeof( ssl->transform->randbytes );
3899
3900#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3901 /* Read connection IDs and store them */
3902 if( (size_t)( end - p ) < 1 )
3903 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3904
3905 ssl->transform->in_cid_len = *p++;
3906
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02003907 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003908 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3909
3910 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
3911 p += ssl->transform->in_cid_len;
3912
3913 ssl->transform->out_cid_len = *p++;
3914
3915 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
3916 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3917
3918 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
3919 p += ssl->transform->out_cid_len;
3920#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3921
3922 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003923 * Saved fields from top-level ssl_context structure
3924 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003925 if( (size_t)( end - p ) < 4 )
3926 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3927
3928 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
3929 ( (uint32_t) p[1] << 16 ) |
3930 ( (uint32_t) p[2] << 8 ) |
3931 ( (uint32_t) p[3] );
3932 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003933
3934#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3935 if( (size_t)( end - p ) < 16 )
3936 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3937
3938 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
3939 ( (uint64_t) p[1] << 48 ) |
3940 ( (uint64_t) p[2] << 40 ) |
3941 ( (uint64_t) p[3] << 32 ) |
3942 ( (uint64_t) p[4] << 24 ) |
3943 ( (uint64_t) p[5] << 16 ) |
3944 ( (uint64_t) p[6] << 8 ) |
3945 ( (uint64_t) p[7] );
3946 p += 8;
3947
3948 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
3949 ( (uint64_t) p[1] << 48 ) |
3950 ( (uint64_t) p[2] << 40 ) |
3951 ( (uint64_t) p[3] << 32 ) |
3952 ( (uint64_t) p[4] << 24 ) |
3953 ( (uint64_t) p[5] << 16 ) |
3954 ( (uint64_t) p[6] << 8 ) |
3955 ( (uint64_t) p[7] );
3956 p += 8;
3957#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3958
3959#if defined(MBEDTLS_SSL_PROTO_DTLS)
3960 if( (size_t)( end - p ) < 1 )
3961 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3962
3963 ssl->disable_datagram_packing = *p++;
3964#endif /* MBEDTLS_SSL_PROTO_DTLS */
3965
Jerry Yud9a94fe2021-09-28 18:58:59 +08003966 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003967 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08003968 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
3969 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003970
3971#if defined(MBEDTLS_SSL_PROTO_DTLS)
3972 if( (size_t)( end - p ) < 2 )
3973 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3974
3975 ssl->mtu = ( p[0] << 8 ) | p[1];
3976 p += 2;
3977#endif /* MBEDTLS_SSL_PROTO_DTLS */
3978
3979#if defined(MBEDTLS_SSL_ALPN)
3980 {
3981 uint8_t alpn_len;
3982 const char **cur;
3983
3984 if( (size_t)( end - p ) < 1 )
3985 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3986
3987 alpn_len = *p++;
3988
3989 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
3990 {
3991 /* alpn_chosen should point to an item in the configured list */
3992 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
3993 {
3994 if( strlen( *cur ) == alpn_len &&
3995 memcmp( p, cur, alpn_len ) == 0 )
3996 {
3997 ssl->alpn_chosen = *cur;
3998 break;
3999 }
4000 }
4001 }
4002
4003 /* can only happen on conf mismatch */
4004 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4005 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4006
4007 p += alpn_len;
4008 }
4009#endif /* MBEDTLS_SSL_ALPN */
4010
4011 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004012 * Forced fields from top-level ssl_context structure
4013 *
4014 * Most of them already set to the correct value by mbedtls_ssl_init() and
4015 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4016 */
4017 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
4018
4019 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
4020 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
4021
Hanno Becker361b10d2019-08-30 10:42:49 +01004022 /* Adjust pointers for header fields of outgoing records to
4023 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004024 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004025
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004026#if defined(MBEDTLS_SSL_PROTO_DTLS)
4027 ssl->in_epoch = 1;
4028#endif
4029
4030 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4031 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004032 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4033 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004034 if( ssl->handshake != NULL )
4035 {
4036 mbedtls_ssl_handshake_free( ssl );
4037 mbedtls_free( ssl->handshake );
4038 ssl->handshake = NULL;
4039 }
4040
4041 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004042 * Done - should have consumed entire buffer
4043 */
4044 if( p != end )
4045 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004046
4047 return( 0 );
4048}
4049
4050/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004051 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004052 */
4053int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4054 const unsigned char *buf,
4055 size_t len )
4056{
4057 int ret = ssl_context_load( context, buf, len );
4058
4059 if( ret != 0 )
4060 mbedtls_ssl_free( context );
4061
4062 return( ret );
4063}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004064#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004065
4066/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004067 * Free an SSL context
4068 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004069void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004070{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004071 if( ssl == NULL )
4072 return;
4073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004075
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004076 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004077 {
sander-visserb8aa2072020-05-06 22:05:13 +02004078#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4079 size_t out_buf_len = ssl->out_buf_len;
4080#else
4081 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4082#endif
4083
Darryl Greenb33cc762019-11-28 14:29:44 +00004084 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004085 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004086 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004087 }
4088
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004089 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004090 {
sander-visserb8aa2072020-05-06 22:05:13 +02004091#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4092 size_t in_buf_len = ssl->in_buf_len;
4093#else
4094 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4095#endif
4096
Darryl Greenb33cc762019-11-28 14:29:44 +00004097 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004098 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004099 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004100 }
4101
Paul Bakker48916f92012-09-16 19:57:18 +00004102 if( ssl->transform )
4103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104 mbedtls_ssl_transform_free( ssl->transform );
4105 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004106 }
4107
4108 if( ssl->handshake )
4109 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004110 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004111 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4112 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004114 mbedtls_free( ssl->handshake );
4115 mbedtls_free( ssl->transform_negotiate );
4116 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004117 }
4118
Ronald Cron6f135e12021-12-08 16:57:54 +01004119#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004120 mbedtls_ssl_transform_free( ssl->transform_application );
4121 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004122#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004123
Paul Bakkerc0463502013-02-14 11:19:38 +01004124 if( ssl->session )
4125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004126 mbedtls_ssl_session_free( ssl->session );
4127 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004128 }
4129
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004130#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004131 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004132 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004133 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004134 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004135 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004136#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004137
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004138#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004139 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004140#endif
4141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004143
Paul Bakker86f04f42013-02-14 11:20:09 +01004144 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004145 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004146}
4147
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004148/*
4149 * Initialze mbedtls_ssl_config
4150 */
4151void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4152{
4153 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4154}
4155
Gilles Peskineeccd8882020-03-10 12:19:08 +01004156#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004157#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskineae270bf2021-06-02 00:05:29 +02004158/* The selection should be the same as mbedtls_x509_crt_profile_default in
Gilles Peskinea28f0f52021-06-02 15:29:38 +02004159 * x509_crt.c. Here, the order matters. Currently we favor stronger hashes,
4160 * for no fundamental reason.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004161 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
4162 * about this list. */
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01004163static int ssl_preset_default_hashes[] = {
4164#if defined(MBEDTLS_SHA512_C)
4165 MBEDTLS_MD_SHA512,
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004166#endif
4167#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01004168 MBEDTLS_MD_SHA384,
4169#endif
4170#if defined(MBEDTLS_SHA256_C)
4171 MBEDTLS_MD_SHA256,
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004172#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01004173 MBEDTLS_MD_NONE
4174};
Jerry Yuf017ee42022-01-12 15:49:48 +08004175#endif /* !MBEDTLS_DEPRECATED_REMOVED */
4176#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01004177
Gilles Peskineae270bf2021-06-02 00:05:29 +02004178/* The selection should be the same as mbedtls_x509_crt_profile_default in
4179 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004180 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004181 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004182 * about this list.
4183 */
Brett Warrene0edc842021-08-17 09:53:13 +01004184static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004185#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004186 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004187#endif
4188#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004189 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004190#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004191#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004192 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004193#endif
4194#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004195 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004196#endif
4197#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004198 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004199#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004200#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004201 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004202#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004203#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004204 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004205#endif
4206#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004207 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004208#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004209 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004210};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004211
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004212static int ssl_preset_suiteb_ciphersuites[] = {
4213 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4214 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4215 0
4216};
4217
Gilles Peskineeccd8882020-03-10 12:19:08 +01004218#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004219#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004220static int ssl_preset_suiteb_hashes[] = {
Jerry Yuf017ee42022-01-12 15:49:48 +08004221#if defined(MBEDTLS_SHA256_C)
4222 MBEDTLS_MD_SHA256,
4223#endif
Jerry Yu713013f2022-01-17 18:16:35 +08004224#if defined(MBEDTLS_SHA384_C)
4225 MBEDTLS_MD_SHA384,
4226#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004227 MBEDTLS_MD_NONE
4228};
Jerry Yuf017ee42022-01-12 15:49:48 +08004229#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004230
Jerry Yu909df7b2022-01-22 11:56:27 +08004231/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004232 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004233 * rules SHOULD be upheld.
4234 * - No duplicate entries.
4235 * - But if there is a good reason, do not change the order of the algorithms.
4236 * - ssl_tls12_present* is for TLS 1.2 use only.
4237 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004238 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004239static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004240
Jerry Yued5e9f42022-01-26 11:21:34 +08004241#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \
4242 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4243 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
4244#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C &&
4245 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004246
Jerry Yu909df7b2022-01-22 11:56:27 +08004247#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \
4248 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4249 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
4250#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
4251 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4252
Jerry Yued5e9f42022-01-26 11:21:34 +08004253#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA512_C) && \
4254 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4255 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
4256#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
4257 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004258
Jerry Yu909df7b2022-01-22 11:56:27 +08004259#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C)
4260 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4261#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */
4262
Jerry Yu53037892022-01-25 11:02:06 +08004263#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C)
4264 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4265#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4266
Jerry Yu909df7b2022-01-22 11:56:27 +08004267 MBEDTLS_TLS1_3_SIG_NONE
4268};
4269
4270/* NOTICE: see above */
4271#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4272static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Jerry Yu713013f2022-01-17 18:16:35 +08004273#if defined(MBEDTLS_SHA512_C)
4274 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA512 )
4275#endif
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004276#if defined(MBEDTLS_SHA384_C)
4277 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 )
4278#endif
4279#if defined(MBEDTLS_SHA256_C)
4280 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 )
4281#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08004282 MBEDTLS_TLS1_3_SIG_NONE
4283};
4284#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4285/* NOTICE: see above */
4286static uint16_t ssl_preset_suiteb_sig_algs[] = {
4287
Jerry Yu909df7b2022-01-22 11:56:27 +08004288#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \
4289 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4290 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
4291#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C &&
4292 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4293
Jerry Yu53037892022-01-25 11:02:06 +08004294#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \
4295 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4296 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
4297#endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C &&
4298 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004299
4300#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C)
4301 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4302#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004303
Jerry Yu53037892022-01-25 11:02:06 +08004304#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C)
4305 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4306#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4307
Jerry Yu713013f2022-01-17 18:16:35 +08004308 MBEDTLS_TLS1_3_SIG_NONE
4309};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004310
Jerry Yu909df7b2022-01-22 11:56:27 +08004311/* NOTICE: see above */
4312#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4313static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Jerry Yu713013f2022-01-17 18:16:35 +08004314#if defined(MBEDTLS_SHA256_C)
4315 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 )
4316#endif
Jerry Yu18c833e2022-01-25 10:55:47 +08004317#if defined(MBEDTLS_SHA384_C)
4318 MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 )
4319#endif
Xiaofei Bai95395012021-11-25 08:51:30 +00004320 MBEDTLS_TLS1_3_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004321};
Jerry Yu909df7b2022-01-22 11:56:27 +08004322#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4323
Jerry Yu1a8b4812022-01-20 17:56:50 +08004324#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004325
Brett Warrene0edc842021-08-17 09:53:13 +01004326static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004327#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004328 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004329#endif
4330#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004331 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004332#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004333 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004334};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004335
Jerry Yu1a8b4812022-01-20 17:56:50 +08004336#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004337/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004338 * to make sure there are no duplicated signature algorithm entries. */
Jerry Yuf377d642022-01-25 10:43:59 +08004339static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004340{
4341 size_t i, j;
4342 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004343
Jerry Yuf377d642022-01-25 10:43:59 +08004344 for( i = 0; sig_algs[i] != MBEDTLS_TLS1_3_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004345 {
Jerry Yuf377d642022-01-25 10:43:59 +08004346 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004347 {
Jerry Yuf377d642022-01-25 10:43:59 +08004348 if( sig_algs[i] != sig_algs[j] )
4349 continue;
4350 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4351 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4352 sig_algs[i], j, i );
4353 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004354 }
4355 }
4356 return( ret );
4357}
4358
4359#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4360
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004361/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004362 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004363 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004364int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004365 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004366{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004367#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004368 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004369#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004370
Jerry Yu1a8b4812022-01-20 17:56:50 +08004371#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004372 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004373 {
4374 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4375 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4376 }
4377
Jerry Yuf377d642022-01-25 10:43:59 +08004378 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004379 {
4380 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4381 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4382 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004383
4384#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004385 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004386 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004387 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004388 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4389 }
4390
Jerry Yuf377d642022-01-25 10:43:59 +08004391 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004392 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004393 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004394 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4395 }
4396#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004397#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4398
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004399 /* Use the functions here so that they are covered in tests,
4400 * but otherwise access member directly for efficiency */
4401 mbedtls_ssl_conf_endpoint( conf, endpoint );
4402 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004403
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004404 /*
4405 * Things that are common to all presets
4406 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004407#if defined(MBEDTLS_SSL_CLI_C)
4408 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4409 {
4410 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4411#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4412 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4413#endif
4414 }
4415#endif
4416
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004417#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4418 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4419#endif
4420
4421#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4422 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4423#endif
4424
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004425#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004426 conf->f_cookie_write = ssl_cookie_write_dummy;
4427 conf->f_cookie_check = ssl_cookie_check_dummy;
4428#endif
4429
4430#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4431 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4432#endif
4433
Janos Follath088ce432017-04-10 12:42:31 +01004434#if defined(MBEDTLS_SSL_SRV_C)
4435 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004436 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004437#endif
4438
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004439#if defined(MBEDTLS_SSL_PROTO_DTLS)
4440 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4441 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4442#endif
4443
4444#if defined(MBEDTLS_SSL_RENEGOTIATION)
4445 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004446 memset( conf->renego_period, 0x00, 2 );
4447 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004448#endif
4449
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004450#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004451 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4452 {
4453 const unsigned char dhm_p[] =
4454 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4455 const unsigned char dhm_g[] =
4456 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004457
Hanno Beckere2defad2021-07-24 05:59:17 +01004458 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4459 dhm_p, sizeof( dhm_p ),
4460 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4461 {
4462 return( ret );
4463 }
4464 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004465#endif
4466
Ronald Cron6f135e12021-12-08 16:57:54 +01004467#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker71f1ed62021-07-24 06:01:47 +01004468 /*
4469 * Allow all TLS 1.3 key exchange modes by default.
4470 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004471 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004472#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004473
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004474 /*
4475 * Preset-specific defaults
4476 */
4477 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004478 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004479 /*
4480 * NSA Suite B
4481 */
4482 case MBEDTLS_SSL_PRESET_SUITEB:
4483 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
4484 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
4485 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004486#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4487 /* Hybrid TLS 1.2/1.3 is not supported yet */
4488 conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
4489#else
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004490 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004491#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3 */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004492
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004493 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004494
4495#if defined(MBEDTLS_X509_CRT_PARSE_C)
4496 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004497#endif
4498
Gilles Peskineeccd8882020-03-10 12:19:08 +01004499#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004500#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004501 conf->sig_hashes = ssl_preset_suiteb_hashes;
Jerry Yuf017ee42022-01-12 15:49:48 +08004502#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004503#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4504 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4505 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4506 else
4507#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4508 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004509#endif
4510
Brett Warrene0edc842021-08-17 09:53:13 +01004511#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4512 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004513#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004514 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004515 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004516
4517 /*
4518 * Default
4519 */
4520 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03004521 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
4522 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
4523 MBEDTLS_SSL_MIN_MAJOR_VERSION :
4524 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
4525 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
4526 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
4527 MBEDTLS_SSL_MIN_MINOR_VERSION :
4528 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004529 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004530#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4531 /* Hybrid TLS 1.2/1.3 is not supported yet */
4532 conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
4533#else
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004534 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Jerry Yu7d239632022-01-27 14:16:44 +08004535#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3 */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004536
4537#if defined(MBEDTLS_SSL_PROTO_DTLS)
4538 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
TRodziewiczef73f012021-05-13 14:53:36 +02004539 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004540#endif
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004541 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004542
4543#if defined(MBEDTLS_X509_CRT_PARSE_C)
4544 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4545#endif
4546
Gilles Peskineeccd8882020-03-10 12:19:08 +01004547#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004548#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01004549 conf->sig_hashes = ssl_preset_default_hashes;
Jerry Yuf017ee42022-01-12 15:49:48 +08004550#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004551#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4552 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4553 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4554 else
4555#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4556 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004557#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004558
Brett Warrene0edc842021-08-17 09:53:13 +01004559#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4560 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004561#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004562 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004563
4564#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4565 conf->dhm_min_bitlen = 1024;
4566#endif
4567 }
4568
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004569 return( 0 );
4570}
4571
4572/*
4573 * Free mbedtls_ssl_config
4574 */
4575void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4576{
4577#if defined(MBEDTLS_DHM_C)
4578 mbedtls_mpi_free( &conf->dhm_P );
4579 mbedtls_mpi_free( &conf->dhm_G );
4580#endif
4581
Gilles Peskineeccd8882020-03-10 12:19:08 +01004582#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004583 if( conf->psk != NULL )
4584 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004585 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004586 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004587 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004588 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004589 }
4590
4591 if( conf->psk_identity != NULL )
4592 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004593 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004594 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004595 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004596 conf->psk_identity_len = 0;
4597 }
4598#endif
4599
4600#if defined(MBEDTLS_X509_CRT_PARSE_C)
4601 ssl_key_cert_free( conf->key_cert );
4602#endif
4603
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004604 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004605}
4606
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004607#if defined(MBEDTLS_PK_C) && \
4608 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004609/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004610 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004611 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004612unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004613{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004614#if defined(MBEDTLS_RSA_C)
4615 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4616 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004617#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004618#if defined(MBEDTLS_ECDSA_C)
4619 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4620 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004621#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004622 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004623}
4624
Hanno Becker7e5437a2017-04-28 17:15:26 +01004625unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4626{
4627 switch( type ) {
4628 case MBEDTLS_PK_RSA:
4629 return( MBEDTLS_SSL_SIG_RSA );
4630 case MBEDTLS_PK_ECDSA:
4631 case MBEDTLS_PK_ECKEY:
4632 return( MBEDTLS_SSL_SIG_ECDSA );
4633 default:
4634 return( MBEDTLS_SSL_SIG_ANON );
4635 }
4636}
4637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004638mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004639{
4640 switch( sig )
4641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004642#if defined(MBEDTLS_RSA_C)
4643 case MBEDTLS_SSL_SIG_RSA:
4644 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004645#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004646#if defined(MBEDTLS_ECDSA_C)
4647 case MBEDTLS_SSL_SIG_ECDSA:
4648 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004649#endif
4650 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004651 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004652 }
4653}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004654#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004655
Hanno Becker7e5437a2017-04-28 17:15:26 +01004656#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01004657 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01004658
4659/* Find an entry in a signature-hash set matching a given hash algorithm. */
4660mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
4661 mbedtls_pk_type_t sig_alg )
4662{
4663 switch( sig_alg )
4664 {
4665 case MBEDTLS_PK_RSA:
4666 return( set->rsa );
4667 case MBEDTLS_PK_ECDSA:
4668 return( set->ecdsa );
4669 default:
4670 return( MBEDTLS_MD_NONE );
4671 }
4672}
4673
4674/* Add a signature-hash-pair to a signature-hash set */
4675void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
4676 mbedtls_pk_type_t sig_alg,
4677 mbedtls_md_type_t md_alg )
4678{
4679 switch( sig_alg )
4680 {
4681 case MBEDTLS_PK_RSA:
4682 if( set->rsa == MBEDTLS_MD_NONE )
4683 set->rsa = md_alg;
4684 break;
4685
4686 case MBEDTLS_PK_ECDSA:
4687 if( set->ecdsa == MBEDTLS_MD_NONE )
4688 set->ecdsa = md_alg;
4689 break;
4690
4691 default:
4692 break;
4693 }
4694}
4695
4696/* Allow exactly one hash algorithm for each signature. */
4697void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
4698 mbedtls_md_type_t md_alg )
4699{
4700 set->rsa = md_alg;
4701 set->ecdsa = md_alg;
4702}
4703
4704#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01004705 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01004706
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004707/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004708 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004709 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004710mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004711{
4712 switch( hash )
4713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004714#if defined(MBEDTLS_MD5_C)
4715 case MBEDTLS_SSL_HASH_MD5:
4716 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004717#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004718#if defined(MBEDTLS_SHA1_C)
4719 case MBEDTLS_SSL_HASH_SHA1:
4720 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004721#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004722#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004723 case MBEDTLS_SSL_HASH_SHA224:
4724 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004725#endif
4726#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004727 case MBEDTLS_SSL_HASH_SHA256:
4728 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004729#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004730#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004731 case MBEDTLS_SSL_HASH_SHA384:
4732 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004733#endif
4734#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004735 case MBEDTLS_SSL_HASH_SHA512:
4736 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004737#endif
4738 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004739 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004740 }
4741}
4742
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004743/*
4744 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4745 */
4746unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4747{
4748 switch( md )
4749 {
4750#if defined(MBEDTLS_MD5_C)
4751 case MBEDTLS_MD_MD5:
4752 return( MBEDTLS_SSL_HASH_MD5 );
4753#endif
4754#if defined(MBEDTLS_SHA1_C)
4755 case MBEDTLS_MD_SHA1:
4756 return( MBEDTLS_SSL_HASH_SHA1 );
4757#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004758#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004759 case MBEDTLS_MD_SHA224:
4760 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004761#endif
4762#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004763 case MBEDTLS_MD_SHA256:
4764 return( MBEDTLS_SSL_HASH_SHA256 );
4765#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004766#if defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004767 case MBEDTLS_MD_SHA384:
4768 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004769#endif
4770#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004771 case MBEDTLS_MD_SHA512:
4772 return( MBEDTLS_SSL_HASH_SHA512 );
4773#endif
4774 default:
4775 return( MBEDTLS_SSL_HASH_NONE );
4776 }
4777}
4778
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004779/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004780 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004781 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004782 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004783int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004784{
Brett Warrene0edc842021-08-17 09:53:13 +01004785 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004786
Brett Warrene0edc842021-08-17 09:53:13 +01004787 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004788 return( -1 );
4789
Brett Warrene0edc842021-08-17 09:53:13 +01004790 for( ; *group_list != 0; group_list++ )
4791 {
4792 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004793 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01004794 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004795
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004796 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004797}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004798
4799#if defined(MBEDTLS_ECP_C)
4800/*
4801 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
4802 */
4803int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
4804{
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01004805 uint16_t tls_id = mbedtls_ecp_curve_info_from_grp_id( grp_id )->tls_id;
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004806 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
4807}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004808#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004810#if defined(MBEDTLS_X509_CRT_PARSE_C)
4811int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
4812 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004813 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02004814 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004815{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004816 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004817 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004818 const char *ext_oid;
4819 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004821 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004822 {
4823 /* Server part of the key exchange */
4824 switch( ciphersuite->key_exchange )
4825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004826 case MBEDTLS_KEY_EXCHANGE_RSA:
4827 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004828 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004829 break;
4830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004831 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
4832 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
4833 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
4834 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004835 break;
4836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004837 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
4838 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004839 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004840 break;
4841
4842 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004843 case MBEDTLS_KEY_EXCHANGE_NONE:
4844 case MBEDTLS_KEY_EXCHANGE_PSK:
4845 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
4846 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02004847 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004848 usage = 0;
4849 }
4850 }
4851 else
4852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004853 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
4854 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004855 }
4856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004857 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004858 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004859 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004860 ret = -1;
4861 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004863 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004865 ext_oid = MBEDTLS_OID_SERVER_AUTH;
4866 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004867 }
4868 else
4869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004870 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
4871 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004872 }
4873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004874 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004875 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004876 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004877 ret = -1;
4878 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004879
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004880 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004881}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004882#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004883
Simon Butcher99000142016-10-13 17:21:01 +01004884int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
4885{
4886#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4887 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Becker541af852021-05-14 16:49:01 +01004888 return( -1 );
Simon Butcher99000142016-10-13 17:21:01 +01004889
4890 switch( md )
4891 {
Mateusz Starzykc6d94ab2021-05-19 13:31:59 +02004892#if defined(MBEDTLS_SHA384_C)
Simon Butcher99000142016-10-13 17:21:01 +01004893 case MBEDTLS_SSL_HASH_SHA384:
4894 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
4895 break;
4896#endif
4897#if defined(MBEDTLS_SHA256_C)
4898 case MBEDTLS_SSL_HASH_SHA256:
4899 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
4900 break;
4901#endif
4902 default:
Hanno Becker541af852021-05-14 16:49:01 +01004903 return( -1 );
Simon Butcher99000142016-10-13 17:21:01 +01004904 }
4905
4906 return 0;
4907#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
4908 (void) ssl;
4909 (void) md;
4910
Hanno Becker541af852021-05-14 16:49:01 +01004911 return( -1 );
Simon Butcher99000142016-10-13 17:21:01 +01004912#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4913}
4914
TRodziewicz0f82ec62021-05-12 17:49:18 +02004915#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05004916
4917#if defined(MBEDTLS_USE_PSA_CRYPTO)
4918int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
4919 unsigned char *hash, size_t *hashlen,
4920 unsigned char *data, size_t data_len,
4921 mbedtls_md_type_t md_alg )
4922{
Andrzej Kurek814feff2019-01-14 04:35:19 -05004923 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +00004924 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05004925 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
4926
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01004927 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -05004928
4929 if( ( status = psa_hash_setup( &hash_operation,
4930 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05004931 {
Andrzej Kurek814feff2019-01-14 04:35:19 -05004932 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05004933 goto exit;
4934 }
4935
Andrzej Kurek814feff2019-01-14 04:35:19 -05004936 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
4937 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05004938 {
Andrzej Kurek814feff2019-01-14 04:35:19 -05004939 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05004940 goto exit;
4941 }
4942
Andrzej Kurek814feff2019-01-14 04:35:19 -05004943 if( ( status = psa_hash_update( &hash_operation,
4944 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05004945 {
Andrzej Kurek814feff2019-01-14 04:35:19 -05004946 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05004947 goto exit;
4948 }
4949
Ronald Cron69a63422021-10-18 09:47:58 +02004950 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
Andrzej Kurek814feff2019-01-14 04:35:19 -05004951 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05004952 {
Andrzej Kurek814feff2019-01-14 04:35:19 -05004953 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05004954 goto exit;
4955 }
4956
4957exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -05004958 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05004959 {
4960 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4961 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -05004962 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05004963 {
4964 case PSA_ERROR_NOT_SUPPORTED:
4965 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -05004966 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05004967 case PSA_ERROR_BUFFER_TOO_SMALL:
4968 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
4969 case PSA_ERROR_INSUFFICIENT_MEMORY:
4970 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
4971 default:
TRodziewiczb579ccd2021-04-13 14:28:28 +02004972 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05004973 }
4974 }
4975 return( 0 );
4976}
4977
4978#else
4979
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01004980int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02004981 unsigned char *hash, size_t *hashlen,
4982 unsigned char *data, size_t data_len,
4983 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01004984{
4985 int ret = 0;
4986 mbedtls_md_context_t ctx;
4987 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02004988 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01004989
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01004990 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -05004991
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01004992 mbedtls_md_init( &ctx );
4993
4994 /*
4995 * digitally-signed struct {
4996 * opaque client_random[32];
4997 * opaque server_random[32];
4998 * ServerDHParams params;
4999 * };
5000 */
5001 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
5002 {
5003 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
5004 goto exit;
5005 }
5006 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
5007 {
5008 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
5009 goto exit;
5010 }
5011 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
5012 {
5013 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
5014 goto exit;
5015 }
5016 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
5017 {
5018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
5019 goto exit;
5020 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02005021 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01005022 {
5023 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
5024 goto exit;
5025 }
5026
5027exit:
5028 mbedtls_md_free( &ctx );
5029
5030 if( ret != 0 )
5031 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5032 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
5033
5034 return( ret );
5035}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05005036#endif /* MBEDTLS_USE_PSA_CRYPTO */
5037
TRodziewicz0f82ec62021-05-12 17:49:18 +02005038#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01005039
Jerry Yu148165c2021-09-24 23:20:59 +08005040#if defined(MBEDTLS_USE_PSA_CRYPTO)
5041int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5042 const mbedtls_md_type_t md,
5043 unsigned char *dst,
5044 size_t dst_len,
5045 size_t *olen )
5046{
Ronald Cronf6893e12022-01-07 22:09:01 +01005047 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5048 psa_hash_operation_t *hash_operation_to_clone;
5049 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5050
Jerry Yu148165c2021-09-24 23:20:59 +08005051 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005052
5053 switch( md )
5054 {
5055#if defined(MBEDTLS_SHA384_C)
5056 case MBEDTLS_MD_SHA384:
5057 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5058 break;
5059#endif
5060
5061#if defined(MBEDTLS_SHA256_C)
5062 case MBEDTLS_MD_SHA256:
5063 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5064 break;
5065#endif
5066
5067 default:
5068 goto exit;
5069 }
5070
5071 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5072 if( status != PSA_SUCCESS )
5073 goto exit;
5074
5075 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5076 if( status != PSA_SUCCESS )
5077 goto exit;
5078
5079exit:
Ronald Cronb788c042022-02-15 09:14:15 +01005080 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005081}
5082#else /* MBEDTLS_USE_PSA_CRYPTO */
5083
Jerry Yu24c0ec32021-09-09 14:21:07 +08005084#if defined(MBEDTLS_SHA384_C)
Jerry Yu000f9762021-09-14 11:12:51 +08005085static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5086 unsigned char *dst,
5087 size_t dst_len,
5088 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005089{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005090 int ret;
5091 mbedtls_sha512_context sha512;
5092
5093 if( dst_len < 48 )
5094 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5095
5096 mbedtls_sha512_init( &sha512 );
5097 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
5098
5099 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5100 {
5101 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5102 goto exit;
5103 }
5104
5105 *olen = 48;
5106
5107exit:
5108
5109 mbedtls_sha512_free( &sha512 );
5110 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005111}
5112#endif /* MBEDTLS_SHA384_C */
5113
5114#if defined(MBEDTLS_SHA256_C)
Jerry Yu000f9762021-09-14 11:12:51 +08005115static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5116 unsigned char *dst,
5117 size_t dst_len,
5118 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005119{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005120 int ret;
5121 mbedtls_sha256_context sha256;
5122
5123 if( dst_len < 32 )
5124 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5125
5126 mbedtls_sha256_init( &sha256 );
5127 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005128
Jerry Yu24c0ec32021-09-09 14:21:07 +08005129 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5130 {
5131 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5132 goto exit;
5133 }
5134
5135 *olen = 32;
5136
5137exit:
5138
5139 mbedtls_sha256_free( &sha256 );
5140 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005141}
5142#endif /* MBEDTLS_SHA256_C */
5143
Jerry Yu000f9762021-09-14 11:12:51 +08005144int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5145 const mbedtls_md_type_t md,
5146 unsigned char *dst,
5147 size_t dst_len,
5148 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005149{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005150 switch( md )
5151 {
5152
Jerry Yu24c0ec32021-09-09 14:21:07 +08005153#if defined(MBEDTLS_SHA384_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005154 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005155 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08005156#endif /* MBEDTLS_SHA384_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005157
Jerry Yu24c0ec32021-09-09 14:21:07 +08005158#if defined(MBEDTLS_SHA256_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005159 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005160 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08005161#endif /* MBEDTLS_SHA256_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005162
5163 default:
5164 break;
5165 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005166 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005167}
XiaokangQian647719a2021-12-07 09:16:29 +00005168
Jerry Yu148165c2021-09-24 23:20:59 +08005169#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005170
Jerry Yu1ea9d102021-12-21 13:41:49 +08005171#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) || \
5172 defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
5173 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jerry Yuba073422021-12-20 22:22:15 +08005174/*
Jerry Yub925f212022-01-12 11:17:02 +08005175 * Function for writing a supported groups (TLS 1.3) or supported elliptic
5176 * curves (TLS 1.2) extension.
Jerry Yuba073422021-12-20 22:22:15 +08005177 *
Jerry Yub925f212022-01-12 11:17:02 +08005178 * The "extension_data" field of a supported groups extension contains a
5179 * "NamedGroupList" value (TLS 1.3 RFC8446):
Jerry Yuba073422021-12-20 22:22:15 +08005180 * enum {
5181 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
5182 * x25519(0x001D), x448(0x001E),
5183 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
5184 * ffdhe6144(0x0103), ffdhe8192(0x0104),
5185 * ffdhe_private_use(0x01FC..0x01FF),
5186 * ecdhe_private_use(0xFE00..0xFEFF),
5187 * (0xFFFF)
5188 * } NamedGroup;
5189 * struct {
5190 * NamedGroup named_group_list<2..2^16-1>;
5191 * } NamedGroupList;
Jerry Yub925f212022-01-12 11:17:02 +08005192 *
5193 * The "extension_data" field of a supported elliptic curves extension contains
5194 * a "NamedCurveList" value (TLS 1.2 RFC 8422):
Jerry Yu63282b42022-01-11 15:43:53 +08005195 * enum {
5196 * deprecated(1..22),
5197 * secp256r1 (23), secp384r1 (24), secp521r1 (25),
5198 * x25519(29), x448(30),
5199 * reserved (0xFE00..0xFEFF),
5200 * deprecated(0xFF01..0xFF02),
5201 * (0xFFFF)
5202 * } NamedCurve;
5203 * struct {
5204 * NamedCurve named_curve_list<2..2^16-1>
5205 * } NamedCurveList;
5206 *
Jerry Yub925f212022-01-12 11:17:02 +08005207 * The TLS 1.3 supported groups extension was defined to be a compatible
5208 * generalization of the TLS 1.2 supported elliptic curves extension. They both
5209 * share the same extension identifier.
Jerry Yu63282b42022-01-11 15:43:53 +08005210 *
Jerry Yub925f212022-01-12 11:17:02 +08005211 * DHE groups are not supported yet.
Jerry Yuba073422021-12-20 22:22:15 +08005212 */
Jerry Yuba073422021-12-20 22:22:15 +08005213int mbedtls_ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl,
5214 unsigned char *buf,
Jerry Yu17532612021-12-20 22:32:09 +08005215 const unsigned char *end,
Jerry Yuba073422021-12-20 22:22:15 +08005216 size_t *out_len )
5217{
5218 unsigned char *p = buf ;
5219 unsigned char *named_group_list; /* Start of named_group_list */
5220 size_t named_group_list_len; /* Length of named_group_list */
5221 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
5222
5223 *out_len = 0;
Jerry Yuf46b0162022-01-11 16:28:00 +08005224
Jerry Yuba073422021-12-20 22:22:15 +08005225 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) );
5226
5227 /* Check if we have space for header and length fields:
5228 * - extension_type (2 bytes)
5229 * - extension_data_length (2 bytes)
5230 * - named_group_list_length (2 bytes)
5231 */
5232 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
5233 p += 6;
5234
5235 named_group_list = p;
5236
5237 if( group_list == NULL )
5238 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
5239
Jerry Yu1510cea2022-01-12 10:56:49 +08005240 for( ; *group_list != 0; group_list++ )
Jerry Yuba073422021-12-20 22:22:15 +08005241 {
Jerry Yu1510cea2022-01-12 10:56:49 +08005242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got supported group(%04x)", *group_list ) );
Jerry Yu63282b42022-01-11 15:43:53 +08005243
Jerry Yu1ea9d102021-12-21 13:41:49 +08005244#if defined(MBEDTLS_ECP_C)
Jerry Yu1510cea2022-01-12 10:56:49 +08005245 if( ( mbedtls_ssl_conf_is_tls13_enabled( ssl->conf ) &&
Jerry Yu3ad14ac2022-01-11 17:13:16 +08005246 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) ) ||
Jerry Yu1510cea2022-01-12 10:56:49 +08005247 ( mbedtls_ssl_conf_is_tls12_enabled( ssl->conf ) &&
Jerry Yu3ad14ac2022-01-11 17:13:16 +08005248 mbedtls_ssl_tls12_named_group_is_ecdhe( *group_list ) ) )
Jerry Yuba073422021-12-20 22:22:15 +08005249 {
Jerry Yu3ad14ac2022-01-11 17:13:16 +08005250 const mbedtls_ecp_curve_info *curve_info;
5251 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
Jerry Yub925f212022-01-12 11:17:02 +08005252 if( curve_info == NULL )
Jerry Yu3ad14ac2022-01-11 17:13:16 +08005253 continue;
Jerry Yub925f212022-01-12 11:17:02 +08005254 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
5255 MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 );
5256 p += 2;
5257 MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )",
5258 curve_info->name, *group_list ) );
Jerry Yuba073422021-12-20 22:22:15 +08005259 }
Jerry Yu63282b42022-01-11 15:43:53 +08005260#endif /* MBEDTLS_ECP_C */
5261 /* Add DHE groups here */
Jerry Yuba073422021-12-20 22:22:15 +08005262
5263 }
5264
Jerry Yu1510cea2022-01-12 10:56:49 +08005265 /* Length of named_group_list */
Jerry Yuba073422021-12-20 22:22:15 +08005266 named_group_list_len = p - named_group_list;
5267 if( named_group_list_len == 0 )
5268 {
5269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) );
5270 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5271 }
5272
5273 /* Write extension_type */
5274 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 );
5275 /* Write extension_data_length */
5276 MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 );
5277 /* Write length of named_group_list */
5278 MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 );
5279
Jerry Yu7f029d82022-01-11 11:08:53 +08005280 MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension",
5281 buf + 4, named_group_list_len + 2 );
Jerry Yuba073422021-12-20 22:22:15 +08005282
5283 *out_len = p - buf;
5284
5285#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
5286 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
5287#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
5288
5289 return( 0 );
5290}
5291
Jerry Yu1ea9d102021-12-21 13:41:49 +08005292#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED ||
5293 MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
5294 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Jerry Yuba073422021-12-20 22:22:15 +08005295
Jerry Yuf017ee42022-01-12 15:49:48 +08005296#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5297/*
Jerry Yu7ddc38c2022-01-19 11:08:05 +08005298 * Function for writing a signature algorithm extension.
Jerry Yuf017ee42022-01-12 15:49:48 +08005299 *
Jerry Yu8afd6e42022-01-20 15:54:26 +08005300 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
Jerry Yu7ddc38c2022-01-19 11:08:05 +08005301 * value (TLS 1.3 RFC8446):
5302 * enum {
5303 * ....
5304 * ecdsa_secp256r1_sha256( 0x0403 ),
5305 * ecdsa_secp384r1_sha384( 0x0503 ),
5306 * ecdsa_secp521r1_sha512( 0x0603 ),
5307 * ....
5308 * } SignatureScheme;
Jerry Yuf017ee42022-01-12 15:49:48 +08005309 *
Jerry Yu7ddc38c2022-01-19 11:08:05 +08005310 * struct {
5311 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5312 * } SignatureSchemeList;
Jerry Yuf017ee42022-01-12 15:49:48 +08005313 *
Jerry Yu8afd6e42022-01-20 15:54:26 +08005314 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5315 * value (TLS 1.2 RFC5246):
Jerry Yu7ddc38c2022-01-19 11:08:05 +08005316 * enum {
5317 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5318 * sha512(6), (255)
5319 * } HashAlgorithm;
5320 *
5321 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5322 * SignatureAlgorithm;
5323 *
5324 * struct {
5325 * HashAlgorithm hash;
5326 * SignatureAlgorithm signature;
5327 * } SignatureAndHashAlgorithm;
5328 *
5329 * SignatureAndHashAlgorithm
5330 * supported_signature_algorithms<2..2^16-2>;
5331 *
5332 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5333 * generalization of the TLS 1.2 signature algorithm extension.
5334 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5335 * `SignatureScheme` field of TLS 1.3
5336 *
Jerry Yuf017ee42022-01-12 15:49:48 +08005337 */
5338int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
5339 const unsigned char *end, size_t *out_len )
5340{
5341 unsigned char *p = buf;
5342 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
5343 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
5344
5345 *out_len = 0;
5346
5347 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
5348
5349 /* Check if we have space for header and length field:
5350 * - extension_type (2 bytes)
5351 * - extension_data_length (2 bytes)
5352 * - supported_signature_algorithms_length (2 bytes)
5353 */
5354 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
5355 p += 6;
5356
5357 /*
5358 * Write supported_signature_algorithms
5359 */
5360 supported_sig_alg = p;
Jerry Yu6106fdc2022-01-12 16:36:14 +08005361 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
5362 if( sig_alg == NULL )
5363 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
5364
5365 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
Jerry Yuf017ee42022-01-12 15:49:48 +08005366 {
Jerry Yu1bab3012022-01-19 17:43:22 +08005367 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
5368 continue;
Jerry Yuf017ee42022-01-12 15:49:48 +08005369 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
5370 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
5371 p += 2;
5372 MBEDTLS_SSL_DEBUG_MSG( 3, ( "signature scheme [%x]", *sig_alg ) );
5373 }
5374
5375 /* Length of supported_signature_algorithms */
5376 supported_sig_alg_len = p - supported_sig_alg;
5377 if( supported_sig_alg_len == 0 )
5378 {
5379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
5380 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5381 }
5382
5383 /* Write extension_type */
5384 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
5385 /* Write extension_data_length */
5386 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
5387 /* Write length of supported_signature_algorithms */
5388 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
5389
5390 /* Output the total length of signature algorithms extension. */
5391 *out_len = p - buf;
5392
5393#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
5394 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
5395#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
5396 return( 0 );
5397}
5398#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yuc73c6182022-02-08 20:29:25 +08005399
Jerry Yuc5aef882021-12-23 20:15:02 +08005400#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5401int mbedtls_ssl_write_hostname_ext( mbedtls_ssl_context *ssl,
5402 unsigned char *buf,
5403 const unsigned char *end,
5404 size_t *olen )
5405{
5406 unsigned char *p = buf;
5407 size_t hostname_len;
5408
5409 *olen = 0;
5410
5411 if( ssl->hostname == NULL )
5412 return( 0 );
5413
5414 MBEDTLS_SSL_DEBUG_MSG( 3,
5415 ( "client hello, adding server name extension: %s",
5416 ssl->hostname ) );
5417
5418 hostname_len = strlen( ssl->hostname );
5419
5420 MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 );
5421
5422 /*
5423 * Sect. 3, RFC 6066 (TLS Extensions Definitions)
5424 *
5425 * In order to provide any of the server names, clients MAY include an
5426 * extension of type "server_name" in the (extended) client hello. The
5427 * "extension_data" field of this extension SHALL contain
5428 * "ServerNameList" where:
5429 *
5430 * struct {
5431 * NameType name_type;
5432 * select (name_type) {
5433 * case host_name: HostName;
5434 * } name;
5435 * } ServerName;
5436 *
5437 * enum {
5438 * host_name(0), (255)
5439 * } NameType;
5440 *
5441 * opaque HostName<1..2^16-1>;
5442 *
5443 * struct {
5444 * ServerName server_name_list<1..2^16-1>
5445 * } ServerNameList;
5446 *
5447 */
5448 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SERVERNAME, p, 0 );
5449 p += 2;
5450
5451 MBEDTLS_PUT_UINT16_BE( hostname_len + 5, p, 0 );
5452 p += 2;
5453
5454 MBEDTLS_PUT_UINT16_BE( hostname_len + 3, p, 0 );
5455 p += 2;
5456
5457 *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME );
5458
5459 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
5460 p += 2;
5461
5462 memcpy( p, ssl->hostname, hostname_len );
5463
5464 *olen = hostname_len + 9;
5465
5466 return( 0 );
5467}
5468#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Jerry Yuf017ee42022-01-12 15:49:48 +08005469
Jerry Yudc7bd172022-02-17 13:44:15 +08005470
5471#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5472
5473#if defined(MBEDTLS_USE_PSA_CRYPTO)
5474
5475static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5476 mbedtls_svc_key_id_t key,
5477 psa_algorithm_t alg,
5478 const unsigned char* seed, size_t seed_length,
5479 const unsigned char* label, size_t label_length,
5480 size_t capacity )
5481{
5482 psa_status_t status;
5483
5484 status = psa_key_derivation_setup( derivation, alg );
5485 if( status != PSA_SUCCESS )
5486 return( status );
5487
5488 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5489 {
5490 status = psa_key_derivation_input_bytes( derivation,
5491 PSA_KEY_DERIVATION_INPUT_SEED,
5492 seed, seed_length );
5493 if( status != PSA_SUCCESS )
5494 return( status );
5495
5496 if( mbedtls_svc_key_id_is_null( key ) )
5497 {
5498 status = psa_key_derivation_input_bytes(
5499 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
5500 NULL, 0 );
5501 }
5502 else
5503 {
5504 status = psa_key_derivation_input_key(
5505 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5506 }
5507 if( status != PSA_SUCCESS )
5508 return( status );
5509
5510 status = psa_key_derivation_input_bytes( derivation,
5511 PSA_KEY_DERIVATION_INPUT_LABEL,
5512 label, label_length );
5513 if( status != PSA_SUCCESS )
5514 return( status );
5515 }
5516 else
5517 {
5518 return( PSA_ERROR_NOT_SUPPORTED );
5519 }
5520
5521 status = psa_key_derivation_set_capacity( derivation, capacity );
5522 if( status != PSA_SUCCESS )
5523 return( status );
5524
5525 return( PSA_SUCCESS );
5526}
5527
5528static int tls_prf_generic( mbedtls_md_type_t md_type,
5529 const unsigned char *secret, size_t slen,
5530 const char *label,
5531 const unsigned char *random, size_t rlen,
5532 unsigned char *dstbuf, size_t dlen )
5533{
5534 psa_status_t status;
5535 psa_algorithm_t alg;
5536 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5537 psa_key_derivation_operation_t derivation =
5538 PSA_KEY_DERIVATION_OPERATION_INIT;
5539
5540 if( md_type == MBEDTLS_MD_SHA384 )
5541 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5542 else
5543 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5544
5545 /* Normally a "secret" should be long enough to be impossible to
5546 * find by brute force, and in particular should not be empty. But
5547 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5548 * and for this use case it makes sense to have a 0-length "secret".
5549 * Since the key API doesn't allow importing a key of length 0,
5550 * keep master_key=0, which setup_psa_key_derivation() understands
5551 * to mean a 0-length "secret" input. */
5552 if( slen != 0 )
5553 {
5554 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5555 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5556 psa_set_key_algorithm( &key_attributes, alg );
5557 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5558
5559 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5560 if( status != PSA_SUCCESS )
5561 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5562 }
5563
5564 status = setup_psa_key_derivation( &derivation,
5565 master_key, alg,
5566 random, rlen,
5567 (unsigned char const *) label,
5568 (size_t) strlen( label ),
5569 dlen );
5570 if( status != PSA_SUCCESS )
5571 {
5572 psa_key_derivation_abort( &derivation );
5573 psa_destroy_key( master_key );
5574 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5575 }
5576
5577 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5578 if( status != PSA_SUCCESS )
5579 {
5580 psa_key_derivation_abort( &derivation );
5581 psa_destroy_key( master_key );
5582 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5583 }
5584
5585 status = psa_key_derivation_abort( &derivation );
5586 if( status != PSA_SUCCESS )
5587 {
5588 psa_destroy_key( master_key );
5589 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5590 }
5591
5592 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5593 status = psa_destroy_key( master_key );
5594 if( status != PSA_SUCCESS )
5595 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5596
5597 return( 0 );
5598}
5599
5600#else /* MBEDTLS_USE_PSA_CRYPTO */
5601
5602static int tls_prf_generic( mbedtls_md_type_t md_type,
5603 const unsigned char *secret, size_t slen,
5604 const char *label,
5605 const unsigned char *random, size_t rlen,
5606 unsigned char *dstbuf, size_t dlen )
5607{
5608 size_t nb;
5609 size_t i, j, k, md_len;
5610 unsigned char *tmp;
5611 size_t tmp_len = 0;
5612 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5613 const mbedtls_md_info_t *md_info;
5614 mbedtls_md_context_t md_ctx;
5615 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5616
5617 mbedtls_md_init( &md_ctx );
5618
5619 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5620 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5621
5622 md_len = mbedtls_md_get_size( md_info );
5623
5624 tmp_len = md_len + strlen( label ) + rlen;
5625 tmp = mbedtls_calloc( 1, tmp_len );
5626 if( tmp == NULL )
5627 {
5628 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5629 goto exit;
5630 }
5631
5632 nb = strlen( label );
5633 memcpy( tmp + md_len, label, nb );
5634 memcpy( tmp + md_len + nb, random, rlen );
5635 nb += rlen;
5636
5637 /*
5638 * Compute P_<hash>(secret, label + random)[0..dlen]
5639 */
5640 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5641 goto exit;
5642
5643 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5644 if( ret != 0 )
5645 goto exit;
5646 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5647 if( ret != 0 )
5648 goto exit;
5649 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5650 if( ret != 0 )
5651 goto exit;
5652
5653 for( i = 0; i < dlen; i += md_len )
5654 {
5655 ret = mbedtls_md_hmac_reset ( &md_ctx );
5656 if( ret != 0 )
5657 goto exit;
5658 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5659 if( ret != 0 )
5660 goto exit;
5661 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5662 if( ret != 0 )
5663 goto exit;
5664
5665 ret = mbedtls_md_hmac_reset ( &md_ctx );
5666 if( ret != 0 )
5667 goto exit;
5668 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5669 if( ret != 0 )
5670 goto exit;
5671 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5672 if( ret != 0 )
5673 goto exit;
5674
5675 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5676
5677 for( j = 0; j < k; j++ )
5678 dstbuf[i + j] = h_i[j];
5679 }
5680
5681exit:
5682 mbedtls_md_free( &md_ctx );
5683
5684 mbedtls_platform_zeroize( tmp, tmp_len );
5685 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5686
5687 mbedtls_free( tmp );
5688
5689 return( ret );
5690}
5691#endif /* MBEDTLS_USE_PSA_CRYPTO */
5692
5693#if defined(MBEDTLS_SHA256_C)
5694static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5695 const char *label,
5696 const unsigned char *random, size_t rlen,
5697 unsigned char *dstbuf, size_t dlen )
5698{
5699 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5700 label, random, rlen, dstbuf, dlen ) );
5701}
5702#endif /* MBEDTLS_SHA256_C */
5703
5704#if defined(MBEDTLS_SHA384_C)
5705static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5706 const char *label,
5707 const unsigned char *random, size_t rlen,
5708 unsigned char *dstbuf, size_t dlen )
5709{
5710 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5711 label, random, rlen, dstbuf, dlen ) );
5712}
5713#endif /* MBEDTLS_SHA384_C */
5714
Jerry Yuf009d862022-02-17 14:01:37 +08005715/*
5716 * Set appropriate PRF function and other SSL / TLS1.2 functions
5717 *
5718 * Inputs:
5719 * - SSL/TLS minor version
5720 * - hash associated with the ciphersuite (only used by TLS 1.2)
5721 *
5722 * Outputs:
5723 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5724 */
5725static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
5726 int minor_ver,
5727 mbedtls_md_type_t hash )
5728{
5729
5730#if defined(MBEDTLS_SHA384_C)
5731 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
5732 hash == MBEDTLS_MD_SHA384 )
5733 {
5734 handshake->tls_prf = tls_prf_sha384;
5735 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5736 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5737 }
5738 else
5739#endif
5740#if defined(MBEDTLS_SHA256_C)
5741 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
5742 {
5743 handshake->tls_prf = tls_prf_sha256;
5744 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5745 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5746 }
5747 else
5748#endif
5749 {
5750 (void) hash;
5751 (void) minor_ver;
5752 (void) handshake;
5753 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5754 }
5755
5756 return( 0 );
5757}
Jerry Yud6ab2352022-02-17 14:03:43 +08005758
5759#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
5760 defined(MBEDTLS_USE_PSA_CRYPTO)
5761static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
5762{
5763 if( ssl->conf->f_psk != NULL )
5764 {
5765 /* If we've used a callback to select the PSK,
5766 * the static configuration is irrelevant. */
5767 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
5768 return( 1 );
5769
5770 return( 0 );
5771 }
5772
5773 if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) )
5774 return( 1 );
5775
5776 return( 0 );
5777}
5778#endif /* MBEDTLS_USE_PSA_CRYPTO &&
5779 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5780
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005781/*
5782 * Compute master secret if needed
5783 *
5784 * Parameters:
5785 * [in/out] handshake
5786 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5787 * (PSA-PSK) ciphersuite_info, psk_opaque
5788 * [out] premaster (cleared)
5789 * [out] master
5790 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5791 * debug: conf->f_dbg, conf->p_dbg
5792 * EMS: passed to calc_verify (debug + session_negotiate)
5793 * PSA-PSA: minor_ver, conf
5794 */
5795static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5796 unsigned char *master,
5797 const mbedtls_ssl_context *ssl )
5798{
5799 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5800
5801 /* cf. RFC 5246, Section 8.1:
5802 * "The master secret is always exactly 48 bytes in length." */
5803 size_t const master_secret_len = 48;
5804
5805#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5806 unsigned char session_hash[48];
5807#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5808
5809 /* The label for the KDF used for key expansion.
5810 * This is either "master secret" or "extended master secret"
5811 * depending on whether the Extended Master Secret extension
5812 * is used. */
5813 char const *lbl = "master secret";
5814
5815 /* The salt for the KDF used for key expansion.
5816 * - If the Extended Master Secret extension is not used,
5817 * this is ClientHello.Random + ServerHello.Random
5818 * (see Sect. 8.1 in RFC 5246).
5819 * - If the Extended Master Secret extension is used,
5820 * this is the transcript of the handshake so far.
5821 * (see Sect. 4 in RFC 7627). */
5822 unsigned char const *salt = handshake->randbytes;
5823 size_t salt_len = 64;
5824
5825#if !defined(MBEDTLS_DEBUG_C) && \
5826 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5827 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5828 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5829 ssl = NULL; /* make sure we don't use it except for those cases */
5830 (void) ssl;
5831#endif
5832
5833 if( handshake->resume != 0 )
5834 {
5835 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5836 return( 0 );
5837 }
5838
5839#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5840 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5841 {
5842 lbl = "extended master secret";
5843 salt = session_hash;
5844 handshake->calc_verify( ssl, session_hash, &salt_len );
5845
5846 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
5847 session_hash, salt_len );
5848 }
5849#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
5850
5851#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
5852 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5853 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
5854 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
5855 ssl_use_opaque_psk( ssl ) == 1 )
5856 {
5857 /* Perform PSK-to-MS expansion in a single step. */
5858 psa_status_t status;
5859 psa_algorithm_t alg;
5860 mbedtls_svc_key_id_t psk;
5861 psa_key_derivation_operation_t derivation =
5862 PSA_KEY_DERIVATION_OPERATION_INIT;
5863 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5864
5865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5866
5867 psk = mbedtls_ssl_get_opaque_psk( ssl );
5868
5869 if( hash_alg == MBEDTLS_MD_SHA384 )
5870 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5871 else
5872 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5873
5874 status = setup_psa_key_derivation( &derivation, psk, alg,
5875 salt, salt_len,
5876 (unsigned char const *) lbl,
5877 (size_t) strlen( lbl ),
5878 master_secret_len );
5879 if( status != PSA_SUCCESS )
5880 {
5881 psa_key_derivation_abort( &derivation );
5882 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5883 }
5884
5885 status = psa_key_derivation_output_bytes( &derivation,
5886 master,
5887 master_secret_len );
5888 if( status != PSA_SUCCESS )
5889 {
5890 psa_key_derivation_abort( &derivation );
5891 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5892 }
5893
5894 status = psa_key_derivation_abort( &derivation );
5895 if( status != PSA_SUCCESS )
5896 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5897 }
5898 else
5899#endif
5900 {
5901 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
5902 lbl, salt, salt_len,
5903 master,
5904 master_secret_len );
5905 if( ret != 0 )
5906 {
5907 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5908 return( ret );
5909 }
5910
5911 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5912 handshake->premaster,
5913 handshake->pmslen );
5914
5915 mbedtls_platform_zeroize( handshake->premaster,
5916 sizeof(handshake->premaster) );
5917 }
5918
5919 return( 0 );
5920}
5921
Jerry Yud62f87e2022-02-17 14:09:02 +08005922int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5923{
5924 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5925 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5926 ssl->handshake->ciphersuite_info;
5927
5928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5929
5930 /* Set PRF, calc_verify and calc_finished function pointers */
5931 ret = ssl_set_handshake_prfs( ssl->handshake,
5932 ssl->minor_ver,
5933 ciphersuite_info->mac );
5934 if( ret != 0 )
5935 {
5936 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5937 return( ret );
5938 }
5939
5940 /* Compute master secret if needed */
5941 ret = ssl_compute_master( ssl->handshake,
5942 ssl->session_negotiate->master,
5943 ssl );
5944 if( ret != 0 )
5945 {
5946 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5947 return( ret );
5948 }
5949
5950 /* Swap the client and server random values:
5951 * - MS derivation wanted client+server (RFC 5246 8.1)
5952 * - key derivation wants server+client (RFC 5246 6.3) */
5953 {
5954 unsigned char tmp[64];
5955 memcpy( tmp, ssl->handshake->randbytes, 64 );
5956 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5957 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5958 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5959 }
5960
5961 /* Populate transform structure */
5962 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5963 ssl->session_negotiate->ciphersuite,
5964 ssl->session_negotiate->master,
5965#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
5966 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5967 ssl->session_negotiate->encrypt_then_mac,
5968#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
5969 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
5970 ssl->handshake->tls_prf,
5971 ssl->handshake->randbytes,
5972 ssl->minor_ver,
5973 ssl->conf->endpoint,
5974 ssl );
5975 if( ret != 0 )
5976 {
5977 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5978 return( ret );
5979 }
5980
5981 /* We no longer need Server/ClientHello.random values */
5982 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5983 sizeof( ssl->handshake->randbytes ) );
5984
5985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5986
5987 return( 0 );
5988}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005989
5990#if defined(MBEDTLS_SHA256_C)
5991void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5992 unsigned char *hash,
5993 size_t *hlen )
5994{
5995#if defined(MBEDTLS_USE_PSA_CRYPTO)
5996 size_t hash_size;
5997 psa_status_t status;
5998 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5999
6000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
6001 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6002 if( status != PSA_SUCCESS )
6003 {
6004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6005 return;
6006 }
6007
6008 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
6009 if( status != PSA_SUCCESS )
6010 {
6011 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6012 return;
6013 }
6014
6015 *hlen = 32;
6016 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6018#else
6019 mbedtls_sha256_context sha256;
6020
6021 mbedtls_sha256_init( &sha256 );
6022
6023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
6024
6025 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6026 mbedtls_sha256_finish( &sha256, hash );
6027
6028 *hlen = 32;
6029
6030 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6032
6033 mbedtls_sha256_free( &sha256 );
6034#endif /* MBEDTLS_USE_PSA_CRYPTO */
6035 return;
6036}
6037#endif /* MBEDTLS_SHA256_C */
6038
Jerry Yuc1cb3842022-02-17 14:13:48 +08006039#if defined(MBEDTLS_SHA384_C)
6040void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
6041 unsigned char *hash,
6042 size_t *hlen )
6043{
6044#if defined(MBEDTLS_USE_PSA_CRYPTO)
6045 size_t hash_size;
6046 psa_status_t status;
6047 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6048
6049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6050 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6051 if( status != PSA_SUCCESS )
6052 {
6053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6054 return;
6055 }
6056
6057 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6058 if( status != PSA_SUCCESS )
6059 {
6060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6061 return;
6062 }
6063
6064 *hlen = 48;
6065 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6066 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6067#else
6068 mbedtls_sha512_context sha512;
6069
6070 mbedtls_sha512_init( &sha512 );
6071
6072 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6073
6074 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
6075 mbedtls_sha512_finish( &sha512, hash );
6076
6077 *hlen = 48;
6078
6079 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6081
6082 mbedtls_sha512_free( &sha512 );
6083#endif /* MBEDTLS_USE_PSA_CRYPTO */
6084 return;
6085}
6086#endif /* MBEDTLS_SHA384_C */
6087
Jerry Yuce3dca42022-02-17 14:16:37 +08006088#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
6089int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6090{
6091 unsigned char *p = ssl->handshake->premaster;
6092 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6093 const unsigned char *psk = NULL;
6094 size_t psk_len = 0;
6095
6096 if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len )
6097 == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
6098 {
6099 /*
6100 * This should never happen because the existence of a PSK is always
6101 * checked before calling this function
6102 */
6103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6104 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6105 }
6106
6107 /*
6108 * PMS = struct {
6109 * opaque other_secret<0..2^16-1>;
6110 * opaque psk<0..2^16-1>;
6111 * };
6112 * with "other_secret" depending on the particular key exchange
6113 */
6114#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6115 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6116 {
6117 if( end - p < 2 )
6118 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6119
6120 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6121 p += 2;
6122
6123 if( end < p || (size_t)( end - p ) < psk_len )
6124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6125
6126 memset( p, 0, psk_len );
6127 p += psk_len;
6128 }
6129 else
6130#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6131#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6132 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6133 {
6134 /*
6135 * other_secret already set by the ClientKeyExchange message,
6136 * and is 48 bytes long
6137 */
6138 if( end - p < 2 )
6139 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6140
6141 *p++ = 0;
6142 *p++ = 48;
6143 p += 48;
6144 }
6145 else
6146#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6147#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6148 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6149 {
6150 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6151 size_t len;
6152
6153 /* Write length only when we know the actual value */
6154 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6155 p + 2, end - ( p + 2 ), &len,
6156 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6157 {
6158 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6159 return( ret );
6160 }
6161 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6162 p += 2 + len;
6163
6164 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6165 }
6166 else
6167#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
6168#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
6169 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6170 {
6171 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6172 size_t zlen;
6173
6174 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6175 p + 2, end - ( p + 2 ),
6176 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6177 {
6178 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6179 return( ret );
6180 }
6181
6182 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6183 p += 2 + zlen;
6184
6185 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6186 MBEDTLS_DEBUG_ECDH_Z );
6187 }
6188 else
6189#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
6190 {
6191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6192 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6193 }
6194
6195 /* opaque psk<0..2^16-1>; */
6196 if( end - p < 2 )
6197 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6198
6199 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6200 p += 2;
6201
6202 if( end < p || (size_t)( end - p ) < psk_len )
6203 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6204
6205 memcpy( p, psk, psk_len );
6206 p += psk_len;
6207
6208 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6209
6210 return( 0 );
6211}
6212#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006213
6214#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
6215static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6216
6217#if defined(MBEDTLS_SSL_PROTO_DTLS)
6218int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6219{
6220 /* If renegotiation is not enforced, retransmit until we would reach max
6221 * timeout if we were using the usual handshake doubling scheme */
6222 if( ssl->conf->renego_max_records < 0 )
6223 {
6224 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6225 unsigned char doublings = 1;
6226
6227 while( ratio != 0 )
6228 {
6229 ++doublings;
6230 ratio >>= 1;
6231 }
6232
6233 if( ++ssl->renego_records_seen > doublings )
6234 {
6235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6236 return( 0 );
6237 }
6238 }
6239
6240 return( ssl_write_hello_request( ssl ) );
6241}
6242#endif
6243#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006244
6245
6246/*
6247 * Handshake functions
6248 */
6249#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6250/* No certificate support -> dummy functions */
6251int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6252{
6253 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6254 ssl->handshake->ciphersuite_info;
6255
6256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6257
6258 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6259 {
6260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6261 ssl->state++;
6262 return( 0 );
6263 }
6264
6265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6266 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6267}
6268
6269int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6270{
6271 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6272 ssl->handshake->ciphersuite_info;
6273
6274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6275
6276 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6277 {
6278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6279 ssl->state++;
6280 return( 0 );
6281 }
6282
6283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6284 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6285}
6286
6287#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6288/* Some certificate support -> implement write and parse */
6289
6290int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6291{
6292 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6293 size_t i, n;
6294 const mbedtls_x509_crt *crt;
6295 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6296 ssl->handshake->ciphersuite_info;
6297
6298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6299
6300 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6301 {
6302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6303 ssl->state++;
6304 return( 0 );
6305 }
6306
6307#if defined(MBEDTLS_SSL_CLI_C)
6308 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6309 {
6310 if( ssl->handshake->client_auth == 0 )
6311 {
6312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6313 ssl->state++;
6314 return( 0 );
6315 }
6316 }
6317#endif /* MBEDTLS_SSL_CLI_C */
6318#if defined(MBEDTLS_SSL_SRV_C)
6319 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6320 {
6321 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6322 {
6323 /* Should never happen because we shouldn't have picked the
6324 * ciphersuite if we don't have a certificate. */
6325 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6326 }
6327 }
6328#endif
6329
6330 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6331
6332 /*
6333 * 0 . 0 handshake type
6334 * 1 . 3 handshake length
6335 * 4 . 6 length of all certs
6336 * 7 . 9 length of cert. 1
6337 * 10 . n-1 peer certificate
6338 * n . n+2 length of cert. 2
6339 * n+3 . ... upper level cert, etc.
6340 */
6341 i = 7;
6342 crt = mbedtls_ssl_own_cert( ssl );
6343
6344 while( crt != NULL )
6345 {
6346 n = crt->raw.len;
6347 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6348 {
6349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6350 " > %" MBEDTLS_PRINTF_SIZET,
6351 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6352 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6353 }
6354
6355 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6356 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6357 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6358
6359 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6360 i += n; crt = crt->next;
6361 }
6362
6363 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6364 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6365 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6366
6367 ssl->out_msglen = i;
6368 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6369 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6370
6371 ssl->state++;
6372
6373 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6374 {
6375 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6376 return( ret );
6377 }
6378
6379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6380
6381 return( ret );
6382}
6383
6384#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6385
6386#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6387static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6388 unsigned char *crt_buf,
6389 size_t crt_buf_len )
6390{
6391 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6392
6393 if( peer_crt == NULL )
6394 return( -1 );
6395
6396 if( peer_crt->raw.len != crt_buf_len )
6397 return( -1 );
6398
6399 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6400}
6401#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6402static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6403 unsigned char *crt_buf,
6404 size_t crt_buf_len )
6405{
6406 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6407 unsigned char const * const peer_cert_digest =
6408 ssl->session->peer_cert_digest;
6409 mbedtls_md_type_t const peer_cert_digest_type =
6410 ssl->session->peer_cert_digest_type;
6411 mbedtls_md_info_t const * const digest_info =
6412 mbedtls_md_info_from_type( peer_cert_digest_type );
6413 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6414 size_t digest_len;
6415
6416 if( peer_cert_digest == NULL || digest_info == NULL )
6417 return( -1 );
6418
6419 digest_len = mbedtls_md_get_size( digest_info );
6420 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6421 return( -1 );
6422
6423 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6424 if( ret != 0 )
6425 return( -1 );
6426
6427 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6428}
6429#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6430#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6431
6432/*
6433 * Once the certificate message is read, parse it into a cert chain and
6434 * perform basic checks, but leave actual verification to the caller
6435 */
6436static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6437 mbedtls_x509_crt *chain )
6438{
6439 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6440#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6441 int crt_cnt=0;
6442#endif
6443 size_t i, n;
6444 uint8_t alert;
6445
6446 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6447 {
6448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6449 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6450 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6451 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6452 }
6453
6454 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6455 {
6456 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6457 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6458 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6459 }
6460
6461 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6462 {
6463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6464 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6465 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6466 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6467 }
6468
6469 i = mbedtls_ssl_hs_hdr_len( ssl );
6470
6471 /*
6472 * Same message structure as in mbedtls_ssl_write_certificate()
6473 */
6474 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6475
6476 if( ssl->in_msg[i] != 0 ||
6477 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6478 {
6479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6480 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6481 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6482 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6483 }
6484
6485 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6486 i += 3;
6487
6488 /* Iterate through and parse the CRTs in the provided chain. */
6489 while( i < ssl->in_hslen )
6490 {
6491 /* Check that there's room for the next CRT's length fields. */
6492 if ( i + 3 > ssl->in_hslen ) {
6493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6494 mbedtls_ssl_send_alert_message( ssl,
6495 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6496 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6497 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6498 }
6499 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6500 * anything beyond 2**16 ~ 64K. */
6501 if( ssl->in_msg[i] != 0 )
6502 {
6503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6504 mbedtls_ssl_send_alert_message( ssl,
6505 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6506 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6507 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6508 }
6509
6510 /* Read length of the next CRT in the chain. */
6511 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6512 | (unsigned int) ssl->in_msg[i + 2];
6513 i += 3;
6514
6515 if( n < 128 || i + n > ssl->in_hslen )
6516 {
6517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6518 mbedtls_ssl_send_alert_message( ssl,
6519 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6520 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6521 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6522 }
6523
6524 /* Check if we're handling the first CRT in the chain. */
6525#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6526 if( crt_cnt++ == 0 &&
6527 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6528 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6529 {
6530 /* During client-side renegotiation, check that the server's
6531 * end-CRTs hasn't changed compared to the initial handshake,
6532 * mitigating the triple handshake attack. On success, reuse
6533 * the original end-CRT instead of parsing it again. */
6534 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6535 if( ssl_check_peer_crt_unchanged( ssl,
6536 &ssl->in_msg[i],
6537 n ) != 0 )
6538 {
6539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6540 mbedtls_ssl_send_alert_message( ssl,
6541 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6542 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6543 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6544 }
6545
6546 /* Now we can safely free the original chain. */
6547 ssl_clear_peer_cert( ssl->session );
6548 }
6549#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6550
6551 /* Parse the next certificate in the chain. */
6552#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6553 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6554#else
6555 /* If we don't need to store the CRT chain permanently, parse
6556 * it in-place from the input buffer instead of making a copy. */
6557 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6558#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6559 switch( ret )
6560 {
6561 case 0: /*ok*/
6562 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6563 /* Ignore certificate with an unknown algorithm: maybe a
6564 prior certificate was already trusted. */
6565 break;
6566
6567 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6568 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6569 goto crt_parse_der_failed;
6570
6571 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6572 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6573 goto crt_parse_der_failed;
6574
6575 default:
6576 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6577 crt_parse_der_failed:
6578 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6579 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6580 return( ret );
6581 }
6582
6583 i += n;
6584 }
6585
6586 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6587 return( 0 );
6588}
6589
6590#if defined(MBEDTLS_SSL_SRV_C)
6591static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6592{
6593 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6594 return( -1 );
6595
6596 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6597 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6598 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6599 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6600 {
6601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6602 return( 0 );
6603 }
6604 return( -1 );
6605}
6606#endif /* MBEDTLS_SSL_SRV_C */
6607
6608/* Check if a certificate message is expected.
6609 * Return either
6610 * - SSL_CERTIFICATE_EXPECTED, or
6611 * - SSL_CERTIFICATE_SKIP
6612 * indicating whether a Certificate message is expected or not.
6613 */
6614#define SSL_CERTIFICATE_EXPECTED 0
6615#define SSL_CERTIFICATE_SKIP 1
6616static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6617 int authmode )
6618{
6619 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6620 ssl->handshake->ciphersuite_info;
6621
6622 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6623 return( SSL_CERTIFICATE_SKIP );
6624
6625#if defined(MBEDTLS_SSL_SRV_C)
6626 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6627 {
6628 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6629 return( SSL_CERTIFICATE_SKIP );
6630
6631 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6632 {
6633 ssl->session_negotiate->verify_result =
6634 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6635 return( SSL_CERTIFICATE_SKIP );
6636 }
6637 }
6638#else
6639 ((void) authmode);
6640#endif /* MBEDTLS_SSL_SRV_C */
6641
6642 return( SSL_CERTIFICATE_EXPECTED );
6643}
6644
6645static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6646 int authmode,
6647 mbedtls_x509_crt *chain,
6648 void *rs_ctx )
6649{
6650 int ret = 0;
6651 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6652 ssl->handshake->ciphersuite_info;
6653 int have_ca_chain = 0;
6654
6655 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6656 void *p_vrfy;
6657
6658 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6659 return( 0 );
6660
6661 if( ssl->f_vrfy != NULL )
6662 {
6663 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6664 f_vrfy = ssl->f_vrfy;
6665 p_vrfy = ssl->p_vrfy;
6666 }
6667 else
6668 {
6669 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6670 f_vrfy = ssl->conf->f_vrfy;
6671 p_vrfy = ssl->conf->p_vrfy;
6672 }
6673
6674 /*
6675 * Main check: verify certificate
6676 */
6677#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6678 if( ssl->conf->f_ca_cb != NULL )
6679 {
6680 ((void) rs_ctx);
6681 have_ca_chain = 1;
6682
6683 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6684 ret = mbedtls_x509_crt_verify_with_ca_cb(
6685 chain,
6686 ssl->conf->f_ca_cb,
6687 ssl->conf->p_ca_cb,
6688 ssl->conf->cert_profile,
6689 ssl->hostname,
6690 &ssl->session_negotiate->verify_result,
6691 f_vrfy, p_vrfy );
6692 }
6693 else
6694#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6695 {
6696 mbedtls_x509_crt *ca_chain;
6697 mbedtls_x509_crl *ca_crl;
6698
6699#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6700 if( ssl->handshake->sni_ca_chain != NULL )
6701 {
6702 ca_chain = ssl->handshake->sni_ca_chain;
6703 ca_crl = ssl->handshake->sni_ca_crl;
6704 }
6705 else
6706#endif
6707 {
6708 ca_chain = ssl->conf->ca_chain;
6709 ca_crl = ssl->conf->ca_crl;
6710 }
6711
6712 if( ca_chain != NULL )
6713 have_ca_chain = 1;
6714
6715 ret = mbedtls_x509_crt_verify_restartable(
6716 chain,
6717 ca_chain, ca_crl,
6718 ssl->conf->cert_profile,
6719 ssl->hostname,
6720 &ssl->session_negotiate->verify_result,
6721 f_vrfy, p_vrfy, rs_ctx );
6722 }
6723
6724 if( ret != 0 )
6725 {
6726 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6727 }
6728
6729#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6730 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6731 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6732#endif
6733
6734 /*
6735 * Secondary checks: always done, but change 'ret' only if it was 0
6736 */
6737
6738#if defined(MBEDTLS_ECP_C)
6739 {
6740 const mbedtls_pk_context *pk = &chain->pk;
6741
6742 /* If certificate uses an EC key, make sure the curve is OK */
6743 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6744 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6745 {
6746 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6747
6748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6749 if( ret == 0 )
6750 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6751 }
6752 }
6753#endif /* MBEDTLS_ECP_C */
6754
6755 if( mbedtls_ssl_check_cert_usage( chain,
6756 ciphersuite_info,
6757 ! ssl->conf->endpoint,
6758 &ssl->session_negotiate->verify_result ) != 0 )
6759 {
6760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6761 if( ret == 0 )
6762 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6763 }
6764
6765 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6766 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6767 * with details encoded in the verification flags. All other kinds
6768 * of error codes, including those from the user provided f_vrfy
6769 * functions, are treated as fatal and lead to a failure of
6770 * ssl_parse_certificate even if verification was optional. */
6771 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6772 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6773 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6774 {
6775 ret = 0;
6776 }
6777
6778 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6779 {
6780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6781 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6782 }
6783
6784 if( ret != 0 )
6785 {
6786 uint8_t alert;
6787
6788 /* The certificate may have been rejected for several reasons.
6789 Pick one and send the corresponding alert. Which alert to send
6790 may be a subject of debate in some cases. */
6791 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6792 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6793 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6794 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6795 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6796 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6797 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6798 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6799 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6800 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6801 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6802 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6803 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6804 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6805 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6806 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6807 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6808 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6809 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6810 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6811 else
6812 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6813 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6814 alert );
6815 }
6816
6817#if defined(MBEDTLS_DEBUG_C)
6818 if( ssl->session_negotiate->verify_result != 0 )
6819 {
6820 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6821 (unsigned int) ssl->session_negotiate->verify_result ) );
6822 }
6823 else
6824 {
6825 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6826 }
6827#endif /* MBEDTLS_DEBUG_C */
6828
6829 return( ret );
6830}
6831
6832#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6833static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6834 unsigned char *start, size_t len )
6835{
6836 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6837 /* Remember digest of the peer's end-CRT. */
6838 ssl->session_negotiate->peer_cert_digest =
6839 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6840 if( ssl->session_negotiate->peer_cert_digest == NULL )
6841 {
6842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6843 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6844 mbedtls_ssl_send_alert_message( ssl,
6845 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6846 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6847
6848 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6849 }
6850
6851 ret = mbedtls_md( mbedtls_md_info_from_type(
6852 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6853 start, len,
6854 ssl->session_negotiate->peer_cert_digest );
6855
6856 ssl->session_negotiate->peer_cert_digest_type =
6857 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6858 ssl->session_negotiate->peer_cert_digest_len =
6859 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6860
6861 return( ret );
6862}
6863
6864static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6865 unsigned char *start, size_t len )
6866{
6867 unsigned char *end = start + len;
6868 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6869
6870 /* Make a copy of the peer's raw public key. */
6871 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6872 ret = mbedtls_pk_parse_subpubkey( &start, end,
6873 &ssl->handshake->peer_pubkey );
6874 if( ret != 0 )
6875 {
6876 /* We should have parsed the public key before. */
6877 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6878 }
6879
6880 return( 0 );
6881}
6882#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6883
6884int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6885{
6886 int ret = 0;
6887 int crt_expected;
6888#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6889 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6890 ? ssl->handshake->sni_authmode
6891 : ssl->conf->authmode;
6892#else
6893 const int authmode = ssl->conf->authmode;
6894#endif
6895 void *rs_ctx = NULL;
6896 mbedtls_x509_crt *chain = NULL;
6897
6898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6899
6900 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6901 if( crt_expected == SSL_CERTIFICATE_SKIP )
6902 {
6903 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6904 goto exit;
6905 }
6906
6907#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6908 if( ssl->handshake->ecrs_enabled &&
6909 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6910 {
6911 chain = ssl->handshake->ecrs_peer_cert;
6912 ssl->handshake->ecrs_peer_cert = NULL;
6913 goto crt_verify;
6914 }
6915#endif
6916
6917 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6918 {
6919 /* mbedtls_ssl_read_record may have sent an alert already. We
6920 let it decide whether to alert. */
6921 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6922 goto exit;
6923 }
6924
6925#if defined(MBEDTLS_SSL_SRV_C)
6926 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6927 {
6928 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6929
6930 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6931 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6932
6933 goto exit;
6934 }
6935#endif /* MBEDTLS_SSL_SRV_C */
6936
6937 /* Clear existing peer CRT structure in case we tried to
6938 * reuse a session but it failed, and allocate a new one. */
6939 ssl_clear_peer_cert( ssl->session_negotiate );
6940
6941 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6942 if( chain == NULL )
6943 {
6944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6945 sizeof( mbedtls_x509_crt ) ) );
6946 mbedtls_ssl_send_alert_message( ssl,
6947 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6948 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6949
6950 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6951 goto exit;
6952 }
6953 mbedtls_x509_crt_init( chain );
6954
6955 ret = ssl_parse_certificate_chain( ssl, chain );
6956 if( ret != 0 )
6957 goto exit;
6958
6959#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6960 if( ssl->handshake->ecrs_enabled)
6961 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6962
6963crt_verify:
6964 if( ssl->handshake->ecrs_enabled)
6965 rs_ctx = &ssl->handshake->ecrs_ctx;
6966#endif
6967
6968 ret = ssl_parse_certificate_verify( ssl, authmode,
6969 chain, rs_ctx );
6970 if( ret != 0 )
6971 goto exit;
6972
6973#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6974 {
6975 unsigned char *crt_start, *pk_start;
6976 size_t crt_len, pk_len;
6977
6978 /* We parse the CRT chain without copying, so
6979 * these pointers point into the input buffer,
6980 * and are hence still valid after freeing the
6981 * CRT chain. */
6982
6983 crt_start = chain->raw.p;
6984 crt_len = chain->raw.len;
6985
6986 pk_start = chain->pk_raw.p;
6987 pk_len = chain->pk_raw.len;
6988
6989 /* Free the CRT structures before computing
6990 * digest and copying the peer's public key. */
6991 mbedtls_x509_crt_free( chain );
6992 mbedtls_free( chain );
6993 chain = NULL;
6994
6995 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6996 if( ret != 0 )
6997 goto exit;
6998
6999 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7000 if( ret != 0 )
7001 goto exit;
7002 }
7003#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7004 /* Pass ownership to session structure. */
7005 ssl->session_negotiate->peer_cert = chain;
7006 chain = NULL;
7007#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7008
7009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
7010
7011exit:
7012
7013 if( ret == 0 )
7014 ssl->state++;
7015
7016#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7017 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7018 {
7019 ssl->handshake->ecrs_peer_cert = chain;
7020 chain = NULL;
7021 }
7022#endif
7023
7024 if( chain != NULL )
7025 {
7026 mbedtls_x509_crt_free( chain );
7027 mbedtls_free( chain );
7028 }
7029
7030 return( ret );
7031}
7032#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7033
Jerry Yu615bd6f2022-02-17 14:25:15 +08007034
7035#if defined(MBEDTLS_SHA256_C)
7036static void ssl_calc_finished_tls_sha256(
7037 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7038{
7039 int len = 12;
7040 const char *sender;
7041 unsigned char padbuf[32];
7042#if defined(MBEDTLS_USE_PSA_CRYPTO)
7043 size_t hash_size;
7044 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7045 psa_status_t status;
7046#else
7047 mbedtls_sha256_context sha256;
7048#endif
7049
7050 mbedtls_ssl_session *session = ssl->session_negotiate;
7051 if( !session )
7052 session = ssl->session;
7053
7054 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7055 ? "client finished"
7056 : "server finished";
7057
7058#if defined(MBEDTLS_USE_PSA_CRYPTO)
7059 sha256_psa = psa_hash_operation_init();
7060
7061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7062
7063 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7064 if( status != PSA_SUCCESS )
7065 {
7066 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7067 return;
7068 }
7069
7070 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7071 if( status != PSA_SUCCESS )
7072 {
7073 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7074 return;
7075 }
7076 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7077#else
7078
7079 mbedtls_sha256_init( &sha256 );
7080
7081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7082
7083 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7084
7085 /*
7086 * TLSv1.2:
7087 * hash = PRF( master, finished_label,
7088 * Hash( handshake ) )[0.11]
7089 */
7090
7091#if !defined(MBEDTLS_SHA256_ALT)
7092 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7093 sha256.state, sizeof( sha256.state ) );
7094#endif
7095
7096 mbedtls_sha256_finish( &sha256, padbuf );
7097 mbedtls_sha256_free( &sha256 );
7098#endif /* MBEDTLS_USE_PSA_CRYPTO */
7099
7100 ssl->handshake->tls_prf( session->master, 48, sender,
7101 padbuf, 32, buf, len );
7102
7103 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7104
7105 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7106
7107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7108}
7109#endif /* MBEDTLS_SHA256_C */
7110
Jerry Yub7ba49e2022-02-17 14:25:53 +08007111
7112#if defined(MBEDTLS_SHA384_C)
7113
7114static void ssl_calc_finished_tls_sha384(
7115 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7116{
7117 int len = 12;
7118 const char *sender;
7119 unsigned char padbuf[48];
7120#if defined(MBEDTLS_USE_PSA_CRYPTO)
7121 size_t hash_size;
7122 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7123 psa_status_t status;
7124#else
7125 mbedtls_sha512_context sha512;
7126#endif
7127
7128 mbedtls_ssl_session *session = ssl->session_negotiate;
7129 if( !session )
7130 session = ssl->session;
7131
7132 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7133 ? "client finished"
7134 : "server finished";
7135
7136#if defined(MBEDTLS_USE_PSA_CRYPTO)
7137 sha384_psa = psa_hash_operation_init();
7138
7139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7140
7141 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7142 if( status != PSA_SUCCESS )
7143 {
7144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7145 return;
7146 }
7147
7148 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7149 if( status != PSA_SUCCESS )
7150 {
7151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7152 return;
7153 }
7154 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7155#else
7156 mbedtls_sha512_init( &sha512 );
7157
7158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7159
7160 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
7161
7162 /*
7163 * TLSv1.2:
7164 * hash = PRF( master, finished_label,
7165 * Hash( handshake ) )[0.11]
7166 */
7167
7168#if !defined(MBEDTLS_SHA512_ALT)
7169 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7170 sha512.state, sizeof( sha512.state ) );
7171#endif
7172 mbedtls_sha512_finish( &sha512, padbuf );
7173
7174 mbedtls_sha512_free( &sha512 );
7175#endif
7176
7177 ssl->handshake->tls_prf( session->master, 48, sender,
7178 padbuf, 48, buf, len );
7179
7180 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7181
7182 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7183
7184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7185}
7186#endif /* MBEDTLS_SHA384_C */
7187
Jerry Yuaef00152022-02-17 14:27:31 +08007188void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7189{
7190 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7191
7192 /*
7193 * Free our handshake params
7194 */
7195 mbedtls_ssl_handshake_free( ssl );
7196 mbedtls_free( ssl->handshake );
7197 ssl->handshake = NULL;
7198
7199 /*
7200 * Free the previous transform and swith in the current one
7201 */
7202 if( ssl->transform )
7203 {
7204 mbedtls_ssl_transform_free( ssl->transform );
7205 mbedtls_free( ssl->transform );
7206 }
7207 ssl->transform = ssl->transform_negotiate;
7208 ssl->transform_negotiate = NULL;
7209
7210 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7211}
7212
Jerry Yu2a9fff52022-02-17 14:28:51 +08007213
7214void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7215{
7216 int resume = ssl->handshake->resume;
7217
7218 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7219
7220#if defined(MBEDTLS_SSL_RENEGOTIATION)
7221 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7222 {
7223 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7224 ssl->renego_records_seen = 0;
7225 }
7226#endif
7227
7228 /*
7229 * Free the previous session and switch in the current one
7230 */
7231 if( ssl->session )
7232 {
7233#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7234 /* RFC 7366 3.1: keep the EtM state */
7235 ssl->session_negotiate->encrypt_then_mac =
7236 ssl->session->encrypt_then_mac;
7237#endif
7238
7239 mbedtls_ssl_session_free( ssl->session );
7240 mbedtls_free( ssl->session );
7241 }
7242 ssl->session = ssl->session_negotiate;
7243 ssl->session_negotiate = NULL;
7244
7245 /*
7246 * Add cache entry
7247 */
7248 if( ssl->conf->f_set_cache != NULL &&
7249 ssl->session->id_len != 0 &&
7250 resume == 0 )
7251 {
7252 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7253 ssl->session->id,
7254 ssl->session->id_len,
7255 ssl->session ) != 0 )
7256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7257 }
7258
7259#if defined(MBEDTLS_SSL_PROTO_DTLS)
7260 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7261 ssl->handshake->flight != NULL )
7262 {
7263 /* Cancel handshake timer */
7264 mbedtls_ssl_set_timer( ssl, 0 );
7265
7266 /* Keep last flight around in case we need to resend it:
7267 * we need the handshake and transform structures for that */
7268 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7269 }
7270 else
7271#endif
7272 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7273
7274 ssl->state++;
7275
7276 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7277}
7278
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007279int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7280{
7281 int ret, hash_len;
7282
7283 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7284
7285 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7286
7287 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7288
7289 /*
7290 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7291 * may define some other value. Currently (early 2016), no defined
7292 * ciphersuite does this (and this is unlikely to change as activity has
7293 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7294 */
7295 hash_len = 12;
7296
7297#if defined(MBEDTLS_SSL_RENEGOTIATION)
7298 ssl->verify_data_len = hash_len;
7299 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7300#endif
7301
7302 ssl->out_msglen = 4 + hash_len;
7303 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7304 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7305
7306 /*
7307 * In case of session resuming, invert the client and server
7308 * ChangeCipherSpec messages order.
7309 */
7310 if( ssl->handshake->resume != 0 )
7311 {
7312#if defined(MBEDTLS_SSL_CLI_C)
7313 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7314 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7315#endif
7316#if defined(MBEDTLS_SSL_SRV_C)
7317 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7318 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7319#endif
7320 }
7321 else
7322 ssl->state++;
7323
7324 /*
7325 * Switch to our negotiated transform and session parameters for outbound
7326 * data.
7327 */
7328 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7329
7330#if defined(MBEDTLS_SSL_PROTO_DTLS)
7331 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7332 {
7333 unsigned char i;
7334
7335 /* Remember current epoch settings for resending */
7336 ssl->handshake->alt_transform_out = ssl->transform_out;
7337 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7338 sizeof( ssl->handshake->alt_out_ctr ) );
7339
7340 /* Set sequence_number to zero */
7341 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7342
7343
7344 /* Increment epoch */
7345 for( i = 2; i > 0; i-- )
7346 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7347 break;
7348
7349 /* The loop goes to its end iff the counter is wrapping */
7350 if( i == 0 )
7351 {
7352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7353 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7354 }
7355 }
7356 else
7357#endif /* MBEDTLS_SSL_PROTO_DTLS */
7358 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7359
7360 ssl->transform_out = ssl->transform_negotiate;
7361 ssl->session_out = ssl->session_negotiate;
7362
7363#if defined(MBEDTLS_SSL_PROTO_DTLS)
7364 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7365 mbedtls_ssl_send_flight_completed( ssl );
7366#endif
7367
7368 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7369 {
7370 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7371 return( ret );
7372 }
7373
7374#if defined(MBEDTLS_SSL_PROTO_DTLS)
7375 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7376 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7377 {
7378 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7379 return( ret );
7380 }
7381#endif
7382
7383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7384
7385 return( 0 );
7386}
7387
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007388#define SSL_MAX_HASH_LEN 12
7389
7390int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7391{
7392 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7393 unsigned int hash_len = 12;
7394 unsigned char buf[SSL_MAX_HASH_LEN];
7395
7396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7397
7398 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7399
7400 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7401 {
7402 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7403 goto exit;
7404 }
7405
7406 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7407 {
7408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7409 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7410 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7411 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7412 goto exit;
7413 }
7414
7415 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7416 {
7417 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7418 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7419 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7420 goto exit;
7421 }
7422
7423 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7424 {
7425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7426 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7427 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7428 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7429 goto exit;
7430 }
7431
7432 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7433 buf, hash_len ) != 0 )
7434 {
7435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7436 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7437 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7438 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7439 goto exit;
7440 }
7441
7442#if defined(MBEDTLS_SSL_RENEGOTIATION)
7443 ssl->verify_data_len = hash_len;
7444 memcpy( ssl->peer_verify_data, buf, hash_len );
7445#endif
7446
7447 if( ssl->handshake->resume != 0 )
7448 {
7449#if defined(MBEDTLS_SSL_CLI_C)
7450 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7451 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7452#endif
7453#if defined(MBEDTLS_SSL_SRV_C)
7454 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7455 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7456#endif
7457 }
7458 else
7459 ssl->state++;
7460
7461#if defined(MBEDTLS_SSL_PROTO_DTLS)
7462 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7463 mbedtls_ssl_recv_flight_completed( ssl );
7464#endif
7465
7466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7467
7468exit:
7469 mbedtls_platform_zeroize( buf, hash_len );
7470 return( ret );
7471}
7472
Jerry Yu392112c2022-02-17 14:34:10 +08007473#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7474/*
7475 * Helper to get TLS 1.2 PRF from ciphersuite
7476 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7477 */
7478static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7479{
7480#if defined(MBEDTLS_SHA384_C)
7481 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7482 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7483
7484 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
7485 return( tls_prf_sha384 );
7486#else
7487 (void) ciphersuite_id;
7488#endif
7489 return( tls_prf_sha256 );
7490}
7491#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007492
7493static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7494{
7495 ((void) tls_prf);
7496#if defined(MBEDTLS_SHA384_C)
7497 if( tls_prf == tls_prf_sha384 )
7498 {
7499 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7500 }
7501 else
7502#endif
7503#if defined(MBEDTLS_SHA256_C)
7504 if( tls_prf == tls_prf_sha256 )
7505 {
7506 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7507 }
7508 else
7509#endif
7510 return( MBEDTLS_SSL_TLS_PRF_NONE );
7511}
7512
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007513/*
7514 * Populate a transform structure with session keys and all the other
7515 * necessary information.
7516 *
7517 * Parameters:
7518 * - [in/out]: transform: structure to populate
7519 * [in] must be just initialised with mbedtls_ssl_transform_init()
7520 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7521 * - [in] ciphersuite
7522 * - [in] master
7523 * - [in] encrypt_then_mac
7524 * - [in] compression
7525 * - [in] tls_prf: pointer to PRF to use for key derivation
7526 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
7527 * - [in] minor_ver: SSL/TLS minor version
7528 * - [in] endpoint: client or server
7529 * - [in] ssl: used for:
7530 * - ssl->conf->{f,p}_export_keys
7531 * [in] optionally used for:
7532 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7533 */
7534static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7535 int ciphersuite,
7536 const unsigned char master[48],
7537#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
7538 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7539 int encrypt_then_mac,
7540#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
7541 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7542 ssl_tls_prf_t tls_prf,
7543 const unsigned char randbytes[64],
7544 int minor_ver,
7545 unsigned endpoint,
7546 const mbedtls_ssl_context *ssl )
7547{
7548 int ret = 0;
7549 unsigned char keyblk[256];
7550 unsigned char *key1;
7551 unsigned char *key2;
7552 unsigned char *mac_enc;
7553 unsigned char *mac_dec;
7554 size_t mac_key_len = 0;
7555 size_t iv_copy_len;
7556 size_t keylen;
7557 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
7558 const mbedtls_cipher_info_t *cipher_info;
7559 const mbedtls_md_info_t *md_info;
7560
7561#if defined(MBEDTLS_USE_PSA_CRYPTO)
7562 psa_key_type_t key_type;
7563 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7564 psa_algorithm_t alg;
7565 size_t key_bits;
7566 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7567#endif
7568
7569#if !defined(MBEDTLS_DEBUG_C) && \
7570 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7571 if( ssl->f_export_keys == NULL )
7572 {
7573 ssl = NULL; /* make sure we don't use it except for these cases */
7574 (void) ssl;
7575 }
7576#endif
7577
7578 /*
7579 * Some data just needs copying into the structure
7580 */
7581#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
7582 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7583 transform->encrypt_then_mac = encrypt_then_mac;
7584#endif
7585 transform->minor_ver = minor_ver;
7586
7587#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7588 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7589#endif
7590
7591#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
7592 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
7593 {
7594 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7595 * generation separate. This should never happen. */
7596 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7597 }
7598#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7599
7600 /*
7601 * Get various info structures
7602 */
7603 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7604 if( ciphersuite_info == NULL )
7605 {
7606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7607 ciphersuite ) );
7608 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7609 }
7610
7611 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7612 if( cipher_info == NULL )
7613 {
7614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7615 ciphersuite_info->cipher ) );
7616 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7617 }
7618
7619 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7620 if( md_info == NULL )
7621 {
7622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7623 (unsigned) ciphersuite_info->mac ) );
7624 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7625 }
7626
7627#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7628 /* Copy own and peer's CID if the use of the CID
7629 * extension has been negotiated. */
7630 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7631 {
7632 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7633
7634 transform->in_cid_len = ssl->own_cid_len;
7635 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7636 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7637 transform->in_cid_len );
7638
7639 transform->out_cid_len = ssl->handshake->peer_cid_len;
7640 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7641 ssl->handshake->peer_cid_len );
7642 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7643 transform->out_cid_len );
7644 }
7645#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7646
7647 /*
7648 * Compute key block using the PRF
7649 */
7650 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7651 if( ret != 0 )
7652 {
7653 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7654 return( ret );
7655 }
7656
7657 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7658 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7659 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7660 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7661 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7662
7663 /*
7664 * Determine the appropriate key, IV and MAC length.
7665 */
7666
7667 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
7668
7669#if defined(MBEDTLS_GCM_C) || \
7670 defined(MBEDTLS_CCM_C) || \
7671 defined(MBEDTLS_CHACHAPOLY_C)
7672 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_GCM ||
7673 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CCM ||
7674 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
7675 {
7676 size_t explicit_ivlen;
7677
7678 transform->maclen = 0;
7679 mac_key_len = 0;
7680 transform->taglen =
7681 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7682
7683 /* All modes haves 96-bit IVs, but the length of the static parts vary
7684 * with mode and version:
7685 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7686 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7687 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7688 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7689 * sequence number).
7690 */
7691 transform->ivlen = 12;
7692 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
7693 transform->fixed_ivlen = 12;
7694 else
7695 transform->fixed_ivlen = 4;
7696
7697 /* Minimum length of encrypted record */
7698 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7699 transform->minlen = explicit_ivlen + transform->taglen;
7700 }
7701 else
7702#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7703#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7704 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM ||
7705 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7706 {
7707 /* Initialize HMAC contexts */
7708 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7709 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7710 {
7711 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7712 goto end;
7713 }
7714
7715 /* Get MAC length */
7716 mac_key_len = mbedtls_md_get_size( md_info );
7717 transform->maclen = mac_key_len;
7718
7719 /* IV length */
7720 transform->ivlen = cipher_info->iv_size;
7721
7722 /* Minimum length */
7723 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM )
7724 transform->minlen = transform->maclen;
7725 else
7726 {
7727 /*
7728 * GenericBlockCipher:
7729 * 1. if EtM is in use: one block plus MAC
7730 * otherwise: * first multiple of blocklen greater than maclen
7731 * 2. IV
7732 */
7733#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7734 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
7735 {
7736 transform->minlen = transform->maclen
7737 + cipher_info->block_size;
7738 }
7739 else
7740#endif
7741 {
7742 transform->minlen = transform->maclen
7743 + cipher_info->block_size
7744 - transform->maclen % cipher_info->block_size;
7745 }
7746
7747#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7748 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
7749 {
7750 transform->minlen += transform->ivlen;
7751 }
7752 else
7753#endif
7754 {
7755 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7756 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7757 goto end;
7758 }
7759 }
7760 }
7761 else
7762#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7763 {
7764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7765 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7766 }
7767
7768 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7769 (unsigned) keylen,
7770 (unsigned) transform->minlen,
7771 (unsigned) transform->ivlen,
7772 (unsigned) transform->maclen ) );
7773
7774 /*
7775 * Finally setup the cipher contexts, IVs and MAC secrets.
7776 */
7777#if defined(MBEDTLS_SSL_CLI_C)
7778 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7779 {
7780 key1 = keyblk + mac_key_len * 2;
7781 key2 = keyblk + mac_key_len * 2 + keylen;
7782
7783 mac_enc = keyblk;
7784 mac_dec = keyblk + mac_key_len;
7785
7786 /*
7787 * This is not used in TLS v1.1.
7788 */
7789 iv_copy_len = ( transform->fixed_ivlen ) ?
7790 transform->fixed_ivlen : transform->ivlen;
7791 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7792 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7793 iv_copy_len );
7794 }
7795 else
7796#endif /* MBEDTLS_SSL_CLI_C */
7797#if defined(MBEDTLS_SSL_SRV_C)
7798 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7799 {
7800 key1 = keyblk + mac_key_len * 2 + keylen;
7801 key2 = keyblk + mac_key_len * 2;
7802
7803 mac_enc = keyblk + mac_key_len;
7804 mac_dec = keyblk;
7805
7806 /*
7807 * This is not used in TLS v1.1.
7808 */
7809 iv_copy_len = ( transform->fixed_ivlen ) ?
7810 transform->fixed_ivlen : transform->ivlen;
7811 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7812 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7813 iv_copy_len );
7814 }
7815 else
7816#endif /* MBEDTLS_SSL_SRV_C */
7817 {
7818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7819 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7820 goto end;
7821 }
7822
7823#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7824#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7825 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7826 For AEAD-based ciphersuites, there is nothing to do here. */
7827 if( mac_key_len != 0 )
7828 {
7829 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7830 if( ret != 0 )
7831 goto end;
7832 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7833 if( ret != 0 )
7834 goto end;
7835 }
7836#endif
7837#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7838
7839 ((void) mac_dec);
7840 ((void) mac_enc);
7841
7842 if( ssl != NULL && ssl->f_export_keys != NULL )
7843 {
7844 ssl->f_export_keys( ssl->p_export_keys,
7845 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7846 master, 48,
7847 randbytes + 32,
7848 randbytes,
7849 tls_prf_get_type( tls_prf ) );
7850 }
7851
7852#if defined(MBEDTLS_USE_PSA_CRYPTO)
7853 if( ( status = mbedtls_ssl_cipher_to_psa( cipher_info->type,
7854 transform->taglen,
7855 &alg,
7856 &key_type,
7857 &key_bits ) ) != PSA_SUCCESS )
7858 {
7859 ret = psa_ssl_status_to_mbedtls( status );
7860 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7861 goto end;
7862 }
7863
7864 transform->psa_alg = alg;
7865
7866 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7867 {
7868 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7869 psa_set_key_algorithm( &attributes, alg );
7870 psa_set_key_type( &attributes, key_type );
7871
7872 if( ( status = psa_import_key( &attributes,
7873 key1,
7874 PSA_BITS_TO_BYTES( key_bits ),
7875 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7876 {
7877 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7878 ret = psa_ssl_status_to_mbedtls( status );
7879 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7880 goto end;
7881 }
7882
7883 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7884
7885 if( ( status = psa_import_key( &attributes,
7886 key2,
7887 PSA_BITS_TO_BYTES( key_bits ),
7888 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7889 {
7890 ret = psa_ssl_status_to_mbedtls( status );
7891 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7892 goto end;
7893 }
7894 }
7895#else
7896 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7897 cipher_info ) ) != 0 )
7898 {
7899 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7900 goto end;
7901 }
7902
7903 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7904 cipher_info ) ) != 0 )
7905 {
7906 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7907 goto end;
7908 }
7909
7910 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7911 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7912 MBEDTLS_ENCRYPT ) ) != 0 )
7913 {
7914 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7915 goto end;
7916 }
7917
7918 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7919 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7920 MBEDTLS_DECRYPT ) ) != 0 )
7921 {
7922 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7923 goto end;
7924 }
7925
7926#if defined(MBEDTLS_CIPHER_MODE_CBC)
7927 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7928 {
7929 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7930 MBEDTLS_PADDING_NONE ) ) != 0 )
7931 {
7932 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7933 goto end;
7934 }
7935
7936 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7937 MBEDTLS_PADDING_NONE ) ) != 0 )
7938 {
7939 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7940 goto end;
7941 }
7942 }
7943#endif /* MBEDTLS_CIPHER_MODE_CBC */
7944#endif /* MBEDTLS_USE_PSA_CRYPTO */
7945
7946end:
7947 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7948 return( ret );
7949}
7950
Jerry Yudc7bd172022-02-17 13:44:15 +08007951#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007953#endif /* MBEDTLS_SSL_TLS_C */