blob: ae3573597f80770ae992f250ae62d858ccdbe211 [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
Jerry Yu148165c2021-09-24 23:20:59 +08004915#if defined(MBEDTLS_USE_PSA_CRYPTO)
4916int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
4917 const mbedtls_md_type_t md,
4918 unsigned char *dst,
4919 size_t dst_len,
4920 size_t *olen )
4921{
Ronald Cronf6893e12022-01-07 22:09:01 +01004922 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4923 psa_hash_operation_t *hash_operation_to_clone;
4924 psa_hash_operation_t hash_operation = psa_hash_operation_init();
4925
Jerry Yu148165c2021-09-24 23:20:59 +08004926 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01004927
4928 switch( md )
4929 {
4930#if defined(MBEDTLS_SHA384_C)
4931 case MBEDTLS_MD_SHA384:
4932 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
4933 break;
4934#endif
4935
4936#if defined(MBEDTLS_SHA256_C)
4937 case MBEDTLS_MD_SHA256:
4938 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
4939 break;
4940#endif
4941
4942 default:
4943 goto exit;
4944 }
4945
4946 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
4947 if( status != PSA_SUCCESS )
4948 goto exit;
4949
4950 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
4951 if( status != PSA_SUCCESS )
4952 goto exit;
4953
4954exit:
Ronald Cronb788c042022-02-15 09:14:15 +01004955 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08004956}
4957#else /* MBEDTLS_USE_PSA_CRYPTO */
4958
Jerry Yu24c0ec32021-09-09 14:21:07 +08004959#if defined(MBEDTLS_SHA384_C)
Jerry Yu000f9762021-09-14 11:12:51 +08004960static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
4961 unsigned char *dst,
4962 size_t dst_len,
4963 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004964{
Jerry Yu24c0ec32021-09-09 14:21:07 +08004965 int ret;
4966 mbedtls_sha512_context sha512;
4967
4968 if( dst_len < 48 )
4969 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4970
4971 mbedtls_sha512_init( &sha512 );
4972 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
4973
4974 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
4975 {
4976 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
4977 goto exit;
4978 }
4979
4980 *olen = 48;
4981
4982exit:
4983
4984 mbedtls_sha512_free( &sha512 );
4985 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08004986}
4987#endif /* MBEDTLS_SHA384_C */
4988
4989#if defined(MBEDTLS_SHA256_C)
Jerry Yu000f9762021-09-14 11:12:51 +08004990static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
4991 unsigned char *dst,
4992 size_t dst_len,
4993 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08004994{
Jerry Yu24c0ec32021-09-09 14:21:07 +08004995 int ret;
4996 mbedtls_sha256_context sha256;
4997
4998 if( dst_len < 32 )
4999 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5000
5001 mbedtls_sha256_init( &sha256 );
5002 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005003
Jerry Yu24c0ec32021-09-09 14:21:07 +08005004 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5005 {
5006 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5007 goto exit;
5008 }
5009
5010 *olen = 32;
5011
5012exit:
5013
5014 mbedtls_sha256_free( &sha256 );
5015 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005016}
5017#endif /* MBEDTLS_SHA256_C */
5018
Jerry Yu000f9762021-09-14 11:12:51 +08005019int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5020 const mbedtls_md_type_t md,
5021 unsigned char *dst,
5022 size_t dst_len,
5023 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005024{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005025 switch( md )
5026 {
5027
Jerry Yu24c0ec32021-09-09 14:21:07 +08005028#if defined(MBEDTLS_SHA384_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005029 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005030 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08005031#endif /* MBEDTLS_SHA384_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005032
Jerry Yu24c0ec32021-09-09 14:21:07 +08005033#if defined(MBEDTLS_SHA256_C)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005034 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005035 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Jerry Yuc10f6b42021-12-23 17:16:42 +08005036#endif /* MBEDTLS_SHA256_C */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005037
5038 default:
5039 break;
5040 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005041 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005042}
XiaokangQian647719a2021-12-07 09:16:29 +00005043
Jerry Yu148165c2021-09-24 23:20:59 +08005044#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005045
Jerry Yu1ea9d102021-12-21 13:41:49 +08005046#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) || \
5047 defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
5048 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jerry Yuba073422021-12-20 22:22:15 +08005049/*
Jerry Yub925f212022-01-12 11:17:02 +08005050 * Function for writing a supported groups (TLS 1.3) or supported elliptic
5051 * curves (TLS 1.2) extension.
Jerry Yuba073422021-12-20 22:22:15 +08005052 *
Jerry Yub925f212022-01-12 11:17:02 +08005053 * The "extension_data" field of a supported groups extension contains a
5054 * "NamedGroupList" value (TLS 1.3 RFC8446):
Jerry Yuba073422021-12-20 22:22:15 +08005055 * enum {
5056 * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
5057 * x25519(0x001D), x448(0x001E),
5058 * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
5059 * ffdhe6144(0x0103), ffdhe8192(0x0104),
5060 * ffdhe_private_use(0x01FC..0x01FF),
5061 * ecdhe_private_use(0xFE00..0xFEFF),
5062 * (0xFFFF)
5063 * } NamedGroup;
5064 * struct {
5065 * NamedGroup named_group_list<2..2^16-1>;
5066 * } NamedGroupList;
Jerry Yub925f212022-01-12 11:17:02 +08005067 *
5068 * The "extension_data" field of a supported elliptic curves extension contains
5069 * a "NamedCurveList" value (TLS 1.2 RFC 8422):
Jerry Yu63282b42022-01-11 15:43:53 +08005070 * enum {
5071 * deprecated(1..22),
5072 * secp256r1 (23), secp384r1 (24), secp521r1 (25),
5073 * x25519(29), x448(30),
5074 * reserved (0xFE00..0xFEFF),
5075 * deprecated(0xFF01..0xFF02),
5076 * (0xFFFF)
5077 * } NamedCurve;
5078 * struct {
5079 * NamedCurve named_curve_list<2..2^16-1>
5080 * } NamedCurveList;
5081 *
Jerry Yub925f212022-01-12 11:17:02 +08005082 * The TLS 1.3 supported groups extension was defined to be a compatible
5083 * generalization of the TLS 1.2 supported elliptic curves extension. They both
5084 * share the same extension identifier.
Jerry Yu63282b42022-01-11 15:43:53 +08005085 *
Jerry Yub925f212022-01-12 11:17:02 +08005086 * DHE groups are not supported yet.
Jerry Yuba073422021-12-20 22:22:15 +08005087 */
Jerry Yuba073422021-12-20 22:22:15 +08005088int mbedtls_ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl,
5089 unsigned char *buf,
Jerry Yu17532612021-12-20 22:32:09 +08005090 const unsigned char *end,
Jerry Yuba073422021-12-20 22:22:15 +08005091 size_t *out_len )
5092{
5093 unsigned char *p = buf ;
5094 unsigned char *named_group_list; /* Start of named_group_list */
5095 size_t named_group_list_len; /* Length of named_group_list */
5096 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
5097
5098 *out_len = 0;
Jerry Yuf46b0162022-01-11 16:28:00 +08005099
Jerry Yuba073422021-12-20 22:22:15 +08005100 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) );
5101
5102 /* Check if we have space for header and length fields:
5103 * - extension_type (2 bytes)
5104 * - extension_data_length (2 bytes)
5105 * - named_group_list_length (2 bytes)
5106 */
5107 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
5108 p += 6;
5109
5110 named_group_list = p;
5111
5112 if( group_list == NULL )
5113 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
5114
Jerry Yu1510cea2022-01-12 10:56:49 +08005115 for( ; *group_list != 0; group_list++ )
Jerry Yuba073422021-12-20 22:22:15 +08005116 {
Jerry Yu1510cea2022-01-12 10:56:49 +08005117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got supported group(%04x)", *group_list ) );
Jerry Yu63282b42022-01-11 15:43:53 +08005118
Jerry Yu1ea9d102021-12-21 13:41:49 +08005119#if defined(MBEDTLS_ECP_C)
Jerry Yu1510cea2022-01-12 10:56:49 +08005120 if( ( mbedtls_ssl_conf_is_tls13_enabled( ssl->conf ) &&
Jerry Yu3ad14ac2022-01-11 17:13:16 +08005121 mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) ) ||
Jerry Yu1510cea2022-01-12 10:56:49 +08005122 ( mbedtls_ssl_conf_is_tls12_enabled( ssl->conf ) &&
Jerry Yu3ad14ac2022-01-11 17:13:16 +08005123 mbedtls_ssl_tls12_named_group_is_ecdhe( *group_list ) ) )
Jerry Yuba073422021-12-20 22:22:15 +08005124 {
Jerry Yu3ad14ac2022-01-11 17:13:16 +08005125 const mbedtls_ecp_curve_info *curve_info;
5126 curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
Jerry Yub925f212022-01-12 11:17:02 +08005127 if( curve_info == NULL )
Jerry Yu3ad14ac2022-01-11 17:13:16 +08005128 continue;
Jerry Yub925f212022-01-12 11:17:02 +08005129 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
5130 MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 );
5131 p += 2;
5132 MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )",
5133 curve_info->name, *group_list ) );
Jerry Yuba073422021-12-20 22:22:15 +08005134 }
Jerry Yu63282b42022-01-11 15:43:53 +08005135#endif /* MBEDTLS_ECP_C */
5136 /* Add DHE groups here */
Jerry Yuba073422021-12-20 22:22:15 +08005137
5138 }
5139
Jerry Yu1510cea2022-01-12 10:56:49 +08005140 /* Length of named_group_list */
Jerry Yuba073422021-12-20 22:22:15 +08005141 named_group_list_len = p - named_group_list;
5142 if( named_group_list_len == 0 )
5143 {
5144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) );
5145 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5146 }
5147
5148 /* Write extension_type */
5149 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 );
5150 /* Write extension_data_length */
5151 MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 );
5152 /* Write length of named_group_list */
5153 MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 );
5154
Jerry Yu7f029d82022-01-11 11:08:53 +08005155 MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension",
5156 buf + 4, named_group_list_len + 2 );
Jerry Yuba073422021-12-20 22:22:15 +08005157
5158 *out_len = p - buf;
5159
5160#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
5161 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
5162#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
5163
5164 return( 0 );
5165}
5166
Jerry Yu1ea9d102021-12-21 13:41:49 +08005167#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED ||
5168 MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
5169 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Jerry Yuba073422021-12-20 22:22:15 +08005170
Jerry Yuf017ee42022-01-12 15:49:48 +08005171#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5172/*
Jerry Yu7ddc38c2022-01-19 11:08:05 +08005173 * Function for writing a signature algorithm extension.
Jerry Yuf017ee42022-01-12 15:49:48 +08005174 *
Jerry Yu8afd6e42022-01-20 15:54:26 +08005175 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
Jerry Yu7ddc38c2022-01-19 11:08:05 +08005176 * value (TLS 1.3 RFC8446):
5177 * enum {
5178 * ....
5179 * ecdsa_secp256r1_sha256( 0x0403 ),
5180 * ecdsa_secp384r1_sha384( 0x0503 ),
5181 * ecdsa_secp521r1_sha512( 0x0603 ),
5182 * ....
5183 * } SignatureScheme;
Jerry Yuf017ee42022-01-12 15:49:48 +08005184 *
Jerry Yu7ddc38c2022-01-19 11:08:05 +08005185 * struct {
5186 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5187 * } SignatureSchemeList;
Jerry Yuf017ee42022-01-12 15:49:48 +08005188 *
Jerry Yu8afd6e42022-01-20 15:54:26 +08005189 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5190 * value (TLS 1.2 RFC5246):
Jerry Yu7ddc38c2022-01-19 11:08:05 +08005191 * enum {
5192 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5193 * sha512(6), (255)
5194 * } HashAlgorithm;
5195 *
5196 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5197 * SignatureAlgorithm;
5198 *
5199 * struct {
5200 * HashAlgorithm hash;
5201 * SignatureAlgorithm signature;
5202 * } SignatureAndHashAlgorithm;
5203 *
5204 * SignatureAndHashAlgorithm
5205 * supported_signature_algorithms<2..2^16-2>;
5206 *
5207 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5208 * generalization of the TLS 1.2 signature algorithm extension.
5209 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5210 * `SignatureScheme` field of TLS 1.3
5211 *
Jerry Yuf017ee42022-01-12 15:49:48 +08005212 */
5213int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
5214 const unsigned char *end, size_t *out_len )
5215{
5216 unsigned char *p = buf;
5217 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
5218 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
5219
5220 *out_len = 0;
5221
5222 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
5223
5224 /* Check if we have space for header and length field:
5225 * - extension_type (2 bytes)
5226 * - extension_data_length (2 bytes)
5227 * - supported_signature_algorithms_length (2 bytes)
5228 */
5229 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
5230 p += 6;
5231
5232 /*
5233 * Write supported_signature_algorithms
5234 */
5235 supported_sig_alg = p;
Jerry Yu6106fdc2022-01-12 16:36:14 +08005236 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
5237 if( sig_alg == NULL )
5238 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
5239
5240 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
Jerry Yuf017ee42022-01-12 15:49:48 +08005241 {
Jerry Yu1bab3012022-01-19 17:43:22 +08005242 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
5243 continue;
Jerry Yuf017ee42022-01-12 15:49:48 +08005244 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
5245 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
5246 p += 2;
5247 MBEDTLS_SSL_DEBUG_MSG( 3, ( "signature scheme [%x]", *sig_alg ) );
5248 }
5249
5250 /* Length of supported_signature_algorithms */
5251 supported_sig_alg_len = p - supported_sig_alg;
5252 if( supported_sig_alg_len == 0 )
5253 {
5254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
5255 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5256 }
5257
5258 /* Write extension_type */
5259 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
5260 /* Write extension_data_length */
5261 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
5262 /* Write length of supported_signature_algorithms */
5263 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
5264
5265 /* Output the total length of signature algorithms extension. */
5266 *out_len = p - buf;
5267
5268#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
5269 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
5270#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
5271 return( 0 );
5272}
5273#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yuc73c6182022-02-08 20:29:25 +08005274
Jerry Yuc5aef882021-12-23 20:15:02 +08005275#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5276int mbedtls_ssl_write_hostname_ext( mbedtls_ssl_context *ssl,
5277 unsigned char *buf,
5278 const unsigned char *end,
5279 size_t *olen )
5280{
5281 unsigned char *p = buf;
5282 size_t hostname_len;
5283
5284 *olen = 0;
5285
5286 if( ssl->hostname == NULL )
5287 return( 0 );
5288
5289 MBEDTLS_SSL_DEBUG_MSG( 3,
5290 ( "client hello, adding server name extension: %s",
5291 ssl->hostname ) );
5292
5293 hostname_len = strlen( ssl->hostname );
5294
5295 MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 );
5296
5297 /*
5298 * Sect. 3, RFC 6066 (TLS Extensions Definitions)
5299 *
5300 * In order to provide any of the server names, clients MAY include an
5301 * extension of type "server_name" in the (extended) client hello. The
5302 * "extension_data" field of this extension SHALL contain
5303 * "ServerNameList" where:
5304 *
5305 * struct {
5306 * NameType name_type;
5307 * select (name_type) {
5308 * case host_name: HostName;
5309 * } name;
5310 * } ServerName;
5311 *
5312 * enum {
5313 * host_name(0), (255)
5314 * } NameType;
5315 *
5316 * opaque HostName<1..2^16-1>;
5317 *
5318 * struct {
5319 * ServerName server_name_list<1..2^16-1>
5320 * } ServerNameList;
5321 *
5322 */
5323 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SERVERNAME, p, 0 );
5324 p += 2;
5325
5326 MBEDTLS_PUT_UINT16_BE( hostname_len + 5, p, 0 );
5327 p += 2;
5328
5329 MBEDTLS_PUT_UINT16_BE( hostname_len + 3, p, 0 );
5330 p += 2;
5331
5332 *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME );
5333
5334 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
5335 p += 2;
5336
5337 memcpy( p, ssl->hostname, hostname_len );
5338
5339 *olen = hostname_len + 9;
5340
5341 return( 0 );
5342}
5343#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Jerry Yuf017ee42022-01-12 15:49:48 +08005344
Jerry Yudc7bd172022-02-17 13:44:15 +08005345
5346#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5347
5348#if defined(MBEDTLS_USE_PSA_CRYPTO)
5349
5350static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5351 mbedtls_svc_key_id_t key,
5352 psa_algorithm_t alg,
5353 const unsigned char* seed, size_t seed_length,
5354 const unsigned char* label, size_t label_length,
5355 size_t capacity )
5356{
5357 psa_status_t status;
5358
5359 status = psa_key_derivation_setup( derivation, alg );
5360 if( status != PSA_SUCCESS )
5361 return( status );
5362
5363 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5364 {
5365 status = psa_key_derivation_input_bytes( derivation,
5366 PSA_KEY_DERIVATION_INPUT_SEED,
5367 seed, seed_length );
5368 if( status != PSA_SUCCESS )
5369 return( status );
5370
5371 if( mbedtls_svc_key_id_is_null( key ) )
5372 {
5373 status = psa_key_derivation_input_bytes(
5374 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
5375 NULL, 0 );
5376 }
5377 else
5378 {
5379 status = psa_key_derivation_input_key(
5380 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5381 }
5382 if( status != PSA_SUCCESS )
5383 return( status );
5384
5385 status = psa_key_derivation_input_bytes( derivation,
5386 PSA_KEY_DERIVATION_INPUT_LABEL,
5387 label, label_length );
5388 if( status != PSA_SUCCESS )
5389 return( status );
5390 }
5391 else
5392 {
5393 return( PSA_ERROR_NOT_SUPPORTED );
5394 }
5395
5396 status = psa_key_derivation_set_capacity( derivation, capacity );
5397 if( status != PSA_SUCCESS )
5398 return( status );
5399
5400 return( PSA_SUCCESS );
5401}
5402
5403static int tls_prf_generic( mbedtls_md_type_t md_type,
5404 const unsigned char *secret, size_t slen,
5405 const char *label,
5406 const unsigned char *random, size_t rlen,
5407 unsigned char *dstbuf, size_t dlen )
5408{
5409 psa_status_t status;
5410 psa_algorithm_t alg;
5411 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5412 psa_key_derivation_operation_t derivation =
5413 PSA_KEY_DERIVATION_OPERATION_INIT;
5414
5415 if( md_type == MBEDTLS_MD_SHA384 )
5416 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5417 else
5418 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5419
5420 /* Normally a "secret" should be long enough to be impossible to
5421 * find by brute force, and in particular should not be empty. But
5422 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5423 * and for this use case it makes sense to have a 0-length "secret".
5424 * Since the key API doesn't allow importing a key of length 0,
5425 * keep master_key=0, which setup_psa_key_derivation() understands
5426 * to mean a 0-length "secret" input. */
5427 if( slen != 0 )
5428 {
5429 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5430 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5431 psa_set_key_algorithm( &key_attributes, alg );
5432 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5433
5434 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5435 if( status != PSA_SUCCESS )
5436 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5437 }
5438
5439 status = setup_psa_key_derivation( &derivation,
5440 master_key, alg,
5441 random, rlen,
5442 (unsigned char const *) label,
5443 (size_t) strlen( label ),
5444 dlen );
5445 if( status != PSA_SUCCESS )
5446 {
5447 psa_key_derivation_abort( &derivation );
5448 psa_destroy_key( master_key );
5449 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5450 }
5451
5452 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5453 if( status != PSA_SUCCESS )
5454 {
5455 psa_key_derivation_abort( &derivation );
5456 psa_destroy_key( master_key );
5457 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5458 }
5459
5460 status = psa_key_derivation_abort( &derivation );
5461 if( status != PSA_SUCCESS )
5462 {
5463 psa_destroy_key( master_key );
5464 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5465 }
5466
5467 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5468 status = psa_destroy_key( master_key );
5469 if( status != PSA_SUCCESS )
5470 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5471
5472 return( 0 );
5473}
5474
5475#else /* MBEDTLS_USE_PSA_CRYPTO */
5476
5477static int tls_prf_generic( mbedtls_md_type_t md_type,
5478 const unsigned char *secret, size_t slen,
5479 const char *label,
5480 const unsigned char *random, size_t rlen,
5481 unsigned char *dstbuf, size_t dlen )
5482{
5483 size_t nb;
5484 size_t i, j, k, md_len;
5485 unsigned char *tmp;
5486 size_t tmp_len = 0;
5487 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5488 const mbedtls_md_info_t *md_info;
5489 mbedtls_md_context_t md_ctx;
5490 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5491
5492 mbedtls_md_init( &md_ctx );
5493
5494 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5495 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5496
5497 md_len = mbedtls_md_get_size( md_info );
5498
5499 tmp_len = md_len + strlen( label ) + rlen;
5500 tmp = mbedtls_calloc( 1, tmp_len );
5501 if( tmp == NULL )
5502 {
5503 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5504 goto exit;
5505 }
5506
5507 nb = strlen( label );
5508 memcpy( tmp + md_len, label, nb );
5509 memcpy( tmp + md_len + nb, random, rlen );
5510 nb += rlen;
5511
5512 /*
5513 * Compute P_<hash>(secret, label + random)[0..dlen]
5514 */
5515 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5516 goto exit;
5517
5518 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5519 if( ret != 0 )
5520 goto exit;
5521 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5522 if( ret != 0 )
5523 goto exit;
5524 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5525 if( ret != 0 )
5526 goto exit;
5527
5528 for( i = 0; i < dlen; i += md_len )
5529 {
5530 ret = mbedtls_md_hmac_reset ( &md_ctx );
5531 if( ret != 0 )
5532 goto exit;
5533 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5534 if( ret != 0 )
5535 goto exit;
5536 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5537 if( ret != 0 )
5538 goto exit;
5539
5540 ret = mbedtls_md_hmac_reset ( &md_ctx );
5541 if( ret != 0 )
5542 goto exit;
5543 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5544 if( ret != 0 )
5545 goto exit;
5546 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5547 if( ret != 0 )
5548 goto exit;
5549
5550 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5551
5552 for( j = 0; j < k; j++ )
5553 dstbuf[i + j] = h_i[j];
5554 }
5555
5556exit:
5557 mbedtls_md_free( &md_ctx );
5558
5559 mbedtls_platform_zeroize( tmp, tmp_len );
5560 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5561
5562 mbedtls_free( tmp );
5563
5564 return( ret );
5565}
5566#endif /* MBEDTLS_USE_PSA_CRYPTO */
5567
5568#if defined(MBEDTLS_SHA256_C)
5569static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5570 const char *label,
5571 const unsigned char *random, size_t rlen,
5572 unsigned char *dstbuf, size_t dlen )
5573{
5574 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5575 label, random, rlen, dstbuf, dlen ) );
5576}
5577#endif /* MBEDTLS_SHA256_C */
5578
5579#if defined(MBEDTLS_SHA384_C)
5580static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5581 const char *label,
5582 const unsigned char *random, size_t rlen,
5583 unsigned char *dstbuf, size_t dlen )
5584{
5585 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5586 label, random, rlen, dstbuf, dlen ) );
5587}
5588#endif /* MBEDTLS_SHA384_C */
5589
Jerry Yuf009d862022-02-17 14:01:37 +08005590/*
5591 * Set appropriate PRF function and other SSL / TLS1.2 functions
5592 *
5593 * Inputs:
5594 * - SSL/TLS minor version
5595 * - hash associated with the ciphersuite (only used by TLS 1.2)
5596 *
5597 * Outputs:
5598 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5599 */
5600static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
5601 int minor_ver,
5602 mbedtls_md_type_t hash )
5603{
5604
5605#if defined(MBEDTLS_SHA384_C)
5606 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
5607 hash == MBEDTLS_MD_SHA384 )
5608 {
5609 handshake->tls_prf = tls_prf_sha384;
5610 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5611 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5612 }
5613 else
5614#endif
5615#if defined(MBEDTLS_SHA256_C)
5616 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
5617 {
5618 handshake->tls_prf = tls_prf_sha256;
5619 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5620 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5621 }
5622 else
5623#endif
5624 {
5625 (void) hash;
5626 (void) minor_ver;
5627 (void) handshake;
5628 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5629 }
5630
5631 return( 0 );
5632}
Jerry Yud6ab2352022-02-17 14:03:43 +08005633
5634#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
5635 defined(MBEDTLS_USE_PSA_CRYPTO)
5636static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
5637{
5638 if( ssl->conf->f_psk != NULL )
5639 {
5640 /* If we've used a callback to select the PSK,
5641 * the static configuration is irrelevant. */
5642 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
5643 return( 1 );
5644
5645 return( 0 );
5646 }
5647
5648 if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) )
5649 return( 1 );
5650
5651 return( 0 );
5652}
5653#endif /* MBEDTLS_USE_PSA_CRYPTO &&
5654 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5655
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005656/*
5657 * Compute master secret if needed
5658 *
5659 * Parameters:
5660 * [in/out] handshake
5661 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5662 * (PSA-PSK) ciphersuite_info, psk_opaque
5663 * [out] premaster (cleared)
5664 * [out] master
5665 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5666 * debug: conf->f_dbg, conf->p_dbg
5667 * EMS: passed to calc_verify (debug + session_negotiate)
5668 * PSA-PSA: minor_ver, conf
5669 */
5670static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5671 unsigned char *master,
5672 const mbedtls_ssl_context *ssl )
5673{
5674 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5675
5676 /* cf. RFC 5246, Section 8.1:
5677 * "The master secret is always exactly 48 bytes in length." */
5678 size_t const master_secret_len = 48;
5679
5680#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5681 unsigned char session_hash[48];
5682#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5683
5684 /* The label for the KDF used for key expansion.
5685 * This is either "master secret" or "extended master secret"
5686 * depending on whether the Extended Master Secret extension
5687 * is used. */
5688 char const *lbl = "master secret";
5689
5690 /* The salt for the KDF used for key expansion.
5691 * - If the Extended Master Secret extension is not used,
5692 * this is ClientHello.Random + ServerHello.Random
5693 * (see Sect. 8.1 in RFC 5246).
5694 * - If the Extended Master Secret extension is used,
5695 * this is the transcript of the handshake so far.
5696 * (see Sect. 4 in RFC 7627). */
5697 unsigned char const *salt = handshake->randbytes;
5698 size_t salt_len = 64;
5699
5700#if !defined(MBEDTLS_DEBUG_C) && \
5701 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5702 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5703 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5704 ssl = NULL; /* make sure we don't use it except for those cases */
5705 (void) ssl;
5706#endif
5707
5708 if( handshake->resume != 0 )
5709 {
5710 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5711 return( 0 );
5712 }
5713
5714#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5715 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5716 {
5717 lbl = "extended master secret";
5718 salt = session_hash;
5719 handshake->calc_verify( ssl, session_hash, &salt_len );
5720
5721 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
5722 session_hash, salt_len );
5723 }
5724#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
5725
5726#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
5727 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5728 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
5729 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
5730 ssl_use_opaque_psk( ssl ) == 1 )
5731 {
5732 /* Perform PSK-to-MS expansion in a single step. */
5733 psa_status_t status;
5734 psa_algorithm_t alg;
5735 mbedtls_svc_key_id_t psk;
5736 psa_key_derivation_operation_t derivation =
5737 PSA_KEY_DERIVATION_OPERATION_INIT;
5738 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5739
5740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5741
5742 psk = mbedtls_ssl_get_opaque_psk( ssl );
5743
5744 if( hash_alg == MBEDTLS_MD_SHA384 )
5745 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5746 else
5747 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5748
5749 status = setup_psa_key_derivation( &derivation, psk, alg,
5750 salt, salt_len,
5751 (unsigned char const *) lbl,
5752 (size_t) strlen( lbl ),
5753 master_secret_len );
5754 if( status != PSA_SUCCESS )
5755 {
5756 psa_key_derivation_abort( &derivation );
5757 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5758 }
5759
5760 status = psa_key_derivation_output_bytes( &derivation,
5761 master,
5762 master_secret_len );
5763 if( status != PSA_SUCCESS )
5764 {
5765 psa_key_derivation_abort( &derivation );
5766 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5767 }
5768
5769 status = psa_key_derivation_abort( &derivation );
5770 if( status != PSA_SUCCESS )
5771 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5772 }
5773 else
5774#endif
5775 {
5776 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
5777 lbl, salt, salt_len,
5778 master,
5779 master_secret_len );
5780 if( ret != 0 )
5781 {
5782 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5783 return( ret );
5784 }
5785
5786 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5787 handshake->premaster,
5788 handshake->pmslen );
5789
5790 mbedtls_platform_zeroize( handshake->premaster,
5791 sizeof(handshake->premaster) );
5792 }
5793
5794 return( 0 );
5795}
5796
Jerry Yud62f87e2022-02-17 14:09:02 +08005797int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5798{
5799 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5800 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5801 ssl->handshake->ciphersuite_info;
5802
5803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5804
5805 /* Set PRF, calc_verify and calc_finished function pointers */
5806 ret = ssl_set_handshake_prfs( ssl->handshake,
5807 ssl->minor_ver,
5808 ciphersuite_info->mac );
5809 if( ret != 0 )
5810 {
5811 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5812 return( ret );
5813 }
5814
5815 /* Compute master secret if needed */
5816 ret = ssl_compute_master( ssl->handshake,
5817 ssl->session_negotiate->master,
5818 ssl );
5819 if( ret != 0 )
5820 {
5821 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5822 return( ret );
5823 }
5824
5825 /* Swap the client and server random values:
5826 * - MS derivation wanted client+server (RFC 5246 8.1)
5827 * - key derivation wants server+client (RFC 5246 6.3) */
5828 {
5829 unsigned char tmp[64];
5830 memcpy( tmp, ssl->handshake->randbytes, 64 );
5831 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5832 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5833 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5834 }
5835
5836 /* Populate transform structure */
5837 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5838 ssl->session_negotiate->ciphersuite,
5839 ssl->session_negotiate->master,
5840#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
5841 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5842 ssl->session_negotiate->encrypt_then_mac,
5843#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
5844 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
5845 ssl->handshake->tls_prf,
5846 ssl->handshake->randbytes,
5847 ssl->minor_ver,
5848 ssl->conf->endpoint,
5849 ssl );
5850 if( ret != 0 )
5851 {
5852 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5853 return( ret );
5854 }
5855
5856 /* We no longer need Server/ClientHello.random values */
5857 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5858 sizeof( ssl->handshake->randbytes ) );
5859
5860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5861
5862 return( 0 );
5863}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005864
5865#if defined(MBEDTLS_SHA256_C)
5866void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5867 unsigned char *hash,
5868 size_t *hlen )
5869{
5870#if defined(MBEDTLS_USE_PSA_CRYPTO)
5871 size_t hash_size;
5872 psa_status_t status;
5873 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5874
5875 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5876 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5877 if( status != PSA_SUCCESS )
5878 {
5879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5880 return;
5881 }
5882
5883 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5884 if( status != PSA_SUCCESS )
5885 {
5886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5887 return;
5888 }
5889
5890 *hlen = 32;
5891 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5893#else
5894 mbedtls_sha256_context sha256;
5895
5896 mbedtls_sha256_init( &sha256 );
5897
5898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5899
5900 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5901 mbedtls_sha256_finish( &sha256, hash );
5902
5903 *hlen = 32;
5904
5905 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5907
5908 mbedtls_sha256_free( &sha256 );
5909#endif /* MBEDTLS_USE_PSA_CRYPTO */
5910 return;
5911}
5912#endif /* MBEDTLS_SHA256_C */
5913
Jerry Yuc1cb3842022-02-17 14:13:48 +08005914#if defined(MBEDTLS_SHA384_C)
5915void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5916 unsigned char *hash,
5917 size_t *hlen )
5918{
5919#if defined(MBEDTLS_USE_PSA_CRYPTO)
5920 size_t hash_size;
5921 psa_status_t status;
5922 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5923
5924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5925 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5926 if( status != PSA_SUCCESS )
5927 {
5928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5929 return;
5930 }
5931
5932 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5933 if( status != PSA_SUCCESS )
5934 {
5935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5936 return;
5937 }
5938
5939 *hlen = 48;
5940 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5942#else
5943 mbedtls_sha512_context sha512;
5944
5945 mbedtls_sha512_init( &sha512 );
5946
5947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
5948
5949 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
5950 mbedtls_sha512_finish( &sha512, hash );
5951
5952 *hlen = 48;
5953
5954 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5956
5957 mbedtls_sha512_free( &sha512 );
5958#endif /* MBEDTLS_USE_PSA_CRYPTO */
5959 return;
5960}
5961#endif /* MBEDTLS_SHA384_C */
5962
Jerry Yuce3dca42022-02-17 14:16:37 +08005963#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
5964int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
5965{
5966 unsigned char *p = ssl->handshake->premaster;
5967 unsigned char *end = p + sizeof( ssl->handshake->premaster );
5968 const unsigned char *psk = NULL;
5969 size_t psk_len = 0;
5970
5971 if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len )
5972 == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
5973 {
5974 /*
5975 * This should never happen because the existence of a PSK is always
5976 * checked before calling this function
5977 */
5978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5979 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5980 }
5981
5982 /*
5983 * PMS = struct {
5984 * opaque other_secret<0..2^16-1>;
5985 * opaque psk<0..2^16-1>;
5986 * };
5987 * with "other_secret" depending on the particular key exchange
5988 */
5989#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5990 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
5991 {
5992 if( end - p < 2 )
5993 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5994
5995 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5996 p += 2;
5997
5998 if( end < p || (size_t)( end - p ) < psk_len )
5999 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6000
6001 memset( p, 0, psk_len );
6002 p += psk_len;
6003 }
6004 else
6005#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6006#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6007 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6008 {
6009 /*
6010 * other_secret already set by the ClientKeyExchange message,
6011 * and is 48 bytes long
6012 */
6013 if( end - p < 2 )
6014 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6015
6016 *p++ = 0;
6017 *p++ = 48;
6018 p += 48;
6019 }
6020 else
6021#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6022#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6023 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6024 {
6025 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6026 size_t len;
6027
6028 /* Write length only when we know the actual value */
6029 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6030 p + 2, end - ( p + 2 ), &len,
6031 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6032 {
6033 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6034 return( ret );
6035 }
6036 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6037 p += 2 + len;
6038
6039 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6040 }
6041 else
6042#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
6043#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
6044 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6045 {
6046 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6047 size_t zlen;
6048
6049 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6050 p + 2, end - ( p + 2 ),
6051 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6052 {
6053 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6054 return( ret );
6055 }
6056
6057 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6058 p += 2 + zlen;
6059
6060 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6061 MBEDTLS_DEBUG_ECDH_Z );
6062 }
6063 else
6064#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
6065 {
6066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6067 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6068 }
6069
6070 /* opaque psk<0..2^16-1>; */
6071 if( end - p < 2 )
6072 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6073
6074 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6075 p += 2;
6076
6077 if( end < p || (size_t)( end - p ) < psk_len )
6078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6079
6080 memcpy( p, psk, psk_len );
6081 p += psk_len;
6082
6083 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6084
6085 return( 0 );
6086}
6087#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006088
6089#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
6090static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6091
6092#if defined(MBEDTLS_SSL_PROTO_DTLS)
6093int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6094{
6095 /* If renegotiation is not enforced, retransmit until we would reach max
6096 * timeout if we were using the usual handshake doubling scheme */
6097 if( ssl->conf->renego_max_records < 0 )
6098 {
6099 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6100 unsigned char doublings = 1;
6101
6102 while( ratio != 0 )
6103 {
6104 ++doublings;
6105 ratio >>= 1;
6106 }
6107
6108 if( ++ssl->renego_records_seen > doublings )
6109 {
6110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6111 return( 0 );
6112 }
6113 }
6114
6115 return( ssl_write_hello_request( ssl ) );
6116}
6117#endif
6118#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006119
6120
6121/*
6122 * Handshake functions
6123 */
6124#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6125/* No certificate support -> dummy functions */
6126int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6127{
6128 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6129 ssl->handshake->ciphersuite_info;
6130
6131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6132
6133 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6134 {
6135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6136 ssl->state++;
6137 return( 0 );
6138 }
6139
6140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6141 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6142}
6143
6144int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6145{
6146 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6147 ssl->handshake->ciphersuite_info;
6148
6149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6150
6151 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6152 {
6153 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6154 ssl->state++;
6155 return( 0 );
6156 }
6157
6158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6159 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6160}
6161
6162#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6163/* Some certificate support -> implement write and parse */
6164
6165int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6166{
6167 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6168 size_t i, n;
6169 const mbedtls_x509_crt *crt;
6170 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6171 ssl->handshake->ciphersuite_info;
6172
6173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6174
6175 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6176 {
6177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6178 ssl->state++;
6179 return( 0 );
6180 }
6181
6182#if defined(MBEDTLS_SSL_CLI_C)
6183 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6184 {
6185 if( ssl->handshake->client_auth == 0 )
6186 {
6187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6188 ssl->state++;
6189 return( 0 );
6190 }
6191 }
6192#endif /* MBEDTLS_SSL_CLI_C */
6193#if defined(MBEDTLS_SSL_SRV_C)
6194 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6195 {
6196 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6197 {
6198 /* Should never happen because we shouldn't have picked the
6199 * ciphersuite if we don't have a certificate. */
6200 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6201 }
6202 }
6203#endif
6204
6205 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6206
6207 /*
6208 * 0 . 0 handshake type
6209 * 1 . 3 handshake length
6210 * 4 . 6 length of all certs
6211 * 7 . 9 length of cert. 1
6212 * 10 . n-1 peer certificate
6213 * n . n+2 length of cert. 2
6214 * n+3 . ... upper level cert, etc.
6215 */
6216 i = 7;
6217 crt = mbedtls_ssl_own_cert( ssl );
6218
6219 while( crt != NULL )
6220 {
6221 n = crt->raw.len;
6222 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6223 {
6224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6225 " > %" MBEDTLS_PRINTF_SIZET,
6226 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6227 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6228 }
6229
6230 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6231 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6232 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6233
6234 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6235 i += n; crt = crt->next;
6236 }
6237
6238 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6239 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6240 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6241
6242 ssl->out_msglen = i;
6243 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6244 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6245
6246 ssl->state++;
6247
6248 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6249 {
6250 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6251 return( ret );
6252 }
6253
6254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6255
6256 return( ret );
6257}
6258
6259#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6260
6261#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6262static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6263 unsigned char *crt_buf,
6264 size_t crt_buf_len )
6265{
6266 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6267
6268 if( peer_crt == NULL )
6269 return( -1 );
6270
6271 if( peer_crt->raw.len != crt_buf_len )
6272 return( -1 );
6273
6274 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6275}
6276#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6277static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6278 unsigned char *crt_buf,
6279 size_t crt_buf_len )
6280{
6281 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6282 unsigned char const * const peer_cert_digest =
6283 ssl->session->peer_cert_digest;
6284 mbedtls_md_type_t const peer_cert_digest_type =
6285 ssl->session->peer_cert_digest_type;
6286 mbedtls_md_info_t const * const digest_info =
6287 mbedtls_md_info_from_type( peer_cert_digest_type );
6288 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6289 size_t digest_len;
6290
6291 if( peer_cert_digest == NULL || digest_info == NULL )
6292 return( -1 );
6293
6294 digest_len = mbedtls_md_get_size( digest_info );
6295 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6296 return( -1 );
6297
6298 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6299 if( ret != 0 )
6300 return( -1 );
6301
6302 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6303}
6304#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6305#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6306
6307/*
6308 * Once the certificate message is read, parse it into a cert chain and
6309 * perform basic checks, but leave actual verification to the caller
6310 */
6311static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6312 mbedtls_x509_crt *chain )
6313{
6314 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6315#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6316 int crt_cnt=0;
6317#endif
6318 size_t i, n;
6319 uint8_t alert;
6320
6321 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6322 {
6323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6324 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6325 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6326 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6327 }
6328
6329 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6330 {
6331 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6332 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6333 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6334 }
6335
6336 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6337 {
6338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6339 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6340 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6341 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6342 }
6343
6344 i = mbedtls_ssl_hs_hdr_len( ssl );
6345
6346 /*
6347 * Same message structure as in mbedtls_ssl_write_certificate()
6348 */
6349 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6350
6351 if( ssl->in_msg[i] != 0 ||
6352 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6353 {
6354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6355 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6356 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6357 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6358 }
6359
6360 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6361 i += 3;
6362
6363 /* Iterate through and parse the CRTs in the provided chain. */
6364 while( i < ssl->in_hslen )
6365 {
6366 /* Check that there's room for the next CRT's length fields. */
6367 if ( i + 3 > ssl->in_hslen ) {
6368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6369 mbedtls_ssl_send_alert_message( ssl,
6370 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6371 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6372 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6373 }
6374 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6375 * anything beyond 2**16 ~ 64K. */
6376 if( ssl->in_msg[i] != 0 )
6377 {
6378 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6379 mbedtls_ssl_send_alert_message( ssl,
6380 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6381 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6382 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6383 }
6384
6385 /* Read length of the next CRT in the chain. */
6386 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6387 | (unsigned int) ssl->in_msg[i + 2];
6388 i += 3;
6389
6390 if( n < 128 || i + n > ssl->in_hslen )
6391 {
6392 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6393 mbedtls_ssl_send_alert_message( ssl,
6394 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6395 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6396 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6397 }
6398
6399 /* Check if we're handling the first CRT in the chain. */
6400#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6401 if( crt_cnt++ == 0 &&
6402 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6403 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6404 {
6405 /* During client-side renegotiation, check that the server's
6406 * end-CRTs hasn't changed compared to the initial handshake,
6407 * mitigating the triple handshake attack. On success, reuse
6408 * the original end-CRT instead of parsing it again. */
6409 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6410 if( ssl_check_peer_crt_unchanged( ssl,
6411 &ssl->in_msg[i],
6412 n ) != 0 )
6413 {
6414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6415 mbedtls_ssl_send_alert_message( ssl,
6416 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6417 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6418 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6419 }
6420
6421 /* Now we can safely free the original chain. */
6422 ssl_clear_peer_cert( ssl->session );
6423 }
6424#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6425
6426 /* Parse the next certificate in the chain. */
6427#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6428 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6429#else
6430 /* If we don't need to store the CRT chain permanently, parse
6431 * it in-place from the input buffer instead of making a copy. */
6432 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6433#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6434 switch( ret )
6435 {
6436 case 0: /*ok*/
6437 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6438 /* Ignore certificate with an unknown algorithm: maybe a
6439 prior certificate was already trusted. */
6440 break;
6441
6442 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6443 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6444 goto crt_parse_der_failed;
6445
6446 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6447 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6448 goto crt_parse_der_failed;
6449
6450 default:
6451 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6452 crt_parse_der_failed:
6453 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6454 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6455 return( ret );
6456 }
6457
6458 i += n;
6459 }
6460
6461 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6462 return( 0 );
6463}
6464
6465#if defined(MBEDTLS_SSL_SRV_C)
6466static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6467{
6468 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6469 return( -1 );
6470
6471 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6472 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6473 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6474 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6475 {
6476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6477 return( 0 );
6478 }
6479 return( -1 );
6480}
6481#endif /* MBEDTLS_SSL_SRV_C */
6482
6483/* Check if a certificate message is expected.
6484 * Return either
6485 * - SSL_CERTIFICATE_EXPECTED, or
6486 * - SSL_CERTIFICATE_SKIP
6487 * indicating whether a Certificate message is expected or not.
6488 */
6489#define SSL_CERTIFICATE_EXPECTED 0
6490#define SSL_CERTIFICATE_SKIP 1
6491static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6492 int authmode )
6493{
6494 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6495 ssl->handshake->ciphersuite_info;
6496
6497 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6498 return( SSL_CERTIFICATE_SKIP );
6499
6500#if defined(MBEDTLS_SSL_SRV_C)
6501 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6502 {
6503 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6504 return( SSL_CERTIFICATE_SKIP );
6505
6506 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6507 {
6508 ssl->session_negotiate->verify_result =
6509 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6510 return( SSL_CERTIFICATE_SKIP );
6511 }
6512 }
6513#else
6514 ((void) authmode);
6515#endif /* MBEDTLS_SSL_SRV_C */
6516
6517 return( SSL_CERTIFICATE_EXPECTED );
6518}
6519
6520static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6521 int authmode,
6522 mbedtls_x509_crt *chain,
6523 void *rs_ctx )
6524{
6525 int ret = 0;
6526 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6527 ssl->handshake->ciphersuite_info;
6528 int have_ca_chain = 0;
6529
6530 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6531 void *p_vrfy;
6532
6533 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6534 return( 0 );
6535
6536 if( ssl->f_vrfy != NULL )
6537 {
6538 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6539 f_vrfy = ssl->f_vrfy;
6540 p_vrfy = ssl->p_vrfy;
6541 }
6542 else
6543 {
6544 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6545 f_vrfy = ssl->conf->f_vrfy;
6546 p_vrfy = ssl->conf->p_vrfy;
6547 }
6548
6549 /*
6550 * Main check: verify certificate
6551 */
6552#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6553 if( ssl->conf->f_ca_cb != NULL )
6554 {
6555 ((void) rs_ctx);
6556 have_ca_chain = 1;
6557
6558 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6559 ret = mbedtls_x509_crt_verify_with_ca_cb(
6560 chain,
6561 ssl->conf->f_ca_cb,
6562 ssl->conf->p_ca_cb,
6563 ssl->conf->cert_profile,
6564 ssl->hostname,
6565 &ssl->session_negotiate->verify_result,
6566 f_vrfy, p_vrfy );
6567 }
6568 else
6569#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6570 {
6571 mbedtls_x509_crt *ca_chain;
6572 mbedtls_x509_crl *ca_crl;
6573
6574#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6575 if( ssl->handshake->sni_ca_chain != NULL )
6576 {
6577 ca_chain = ssl->handshake->sni_ca_chain;
6578 ca_crl = ssl->handshake->sni_ca_crl;
6579 }
6580 else
6581#endif
6582 {
6583 ca_chain = ssl->conf->ca_chain;
6584 ca_crl = ssl->conf->ca_crl;
6585 }
6586
6587 if( ca_chain != NULL )
6588 have_ca_chain = 1;
6589
6590 ret = mbedtls_x509_crt_verify_restartable(
6591 chain,
6592 ca_chain, ca_crl,
6593 ssl->conf->cert_profile,
6594 ssl->hostname,
6595 &ssl->session_negotiate->verify_result,
6596 f_vrfy, p_vrfy, rs_ctx );
6597 }
6598
6599 if( ret != 0 )
6600 {
6601 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6602 }
6603
6604#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6605 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6606 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6607#endif
6608
6609 /*
6610 * Secondary checks: always done, but change 'ret' only if it was 0
6611 */
6612
6613#if defined(MBEDTLS_ECP_C)
6614 {
6615 const mbedtls_pk_context *pk = &chain->pk;
6616
6617 /* If certificate uses an EC key, make sure the curve is OK */
6618 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6619 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6620 {
6621 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6622
6623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6624 if( ret == 0 )
6625 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6626 }
6627 }
6628#endif /* MBEDTLS_ECP_C */
6629
6630 if( mbedtls_ssl_check_cert_usage( chain,
6631 ciphersuite_info,
6632 ! ssl->conf->endpoint,
6633 &ssl->session_negotiate->verify_result ) != 0 )
6634 {
6635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6636 if( ret == 0 )
6637 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6638 }
6639
6640 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6641 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6642 * with details encoded in the verification flags. All other kinds
6643 * of error codes, including those from the user provided f_vrfy
6644 * functions, are treated as fatal and lead to a failure of
6645 * ssl_parse_certificate even if verification was optional. */
6646 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6647 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6648 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6649 {
6650 ret = 0;
6651 }
6652
6653 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6654 {
6655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6656 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6657 }
6658
6659 if( ret != 0 )
6660 {
6661 uint8_t alert;
6662
6663 /* The certificate may have been rejected for several reasons.
6664 Pick one and send the corresponding alert. Which alert to send
6665 may be a subject of debate in some cases. */
6666 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6667 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6668 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6669 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6670 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6671 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6672 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6673 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6674 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6675 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6676 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6677 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6678 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6679 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6680 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6681 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6682 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6683 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6684 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6685 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6686 else
6687 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6688 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6689 alert );
6690 }
6691
6692#if defined(MBEDTLS_DEBUG_C)
6693 if( ssl->session_negotiate->verify_result != 0 )
6694 {
6695 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6696 (unsigned int) ssl->session_negotiate->verify_result ) );
6697 }
6698 else
6699 {
6700 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6701 }
6702#endif /* MBEDTLS_DEBUG_C */
6703
6704 return( ret );
6705}
6706
6707#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6708static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6709 unsigned char *start, size_t len )
6710{
6711 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6712 /* Remember digest of the peer's end-CRT. */
6713 ssl->session_negotiate->peer_cert_digest =
6714 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6715 if( ssl->session_negotiate->peer_cert_digest == NULL )
6716 {
6717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6718 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6719 mbedtls_ssl_send_alert_message( ssl,
6720 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6721 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6722
6723 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6724 }
6725
6726 ret = mbedtls_md( mbedtls_md_info_from_type(
6727 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6728 start, len,
6729 ssl->session_negotiate->peer_cert_digest );
6730
6731 ssl->session_negotiate->peer_cert_digest_type =
6732 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6733 ssl->session_negotiate->peer_cert_digest_len =
6734 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6735
6736 return( ret );
6737}
6738
6739static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6740 unsigned char *start, size_t len )
6741{
6742 unsigned char *end = start + len;
6743 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6744
6745 /* Make a copy of the peer's raw public key. */
6746 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6747 ret = mbedtls_pk_parse_subpubkey( &start, end,
6748 &ssl->handshake->peer_pubkey );
6749 if( ret != 0 )
6750 {
6751 /* We should have parsed the public key before. */
6752 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6753 }
6754
6755 return( 0 );
6756}
6757#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6758
6759int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6760{
6761 int ret = 0;
6762 int crt_expected;
6763#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6764 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6765 ? ssl->handshake->sni_authmode
6766 : ssl->conf->authmode;
6767#else
6768 const int authmode = ssl->conf->authmode;
6769#endif
6770 void *rs_ctx = NULL;
6771 mbedtls_x509_crt *chain = NULL;
6772
6773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6774
6775 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6776 if( crt_expected == SSL_CERTIFICATE_SKIP )
6777 {
6778 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6779 goto exit;
6780 }
6781
6782#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6783 if( ssl->handshake->ecrs_enabled &&
6784 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6785 {
6786 chain = ssl->handshake->ecrs_peer_cert;
6787 ssl->handshake->ecrs_peer_cert = NULL;
6788 goto crt_verify;
6789 }
6790#endif
6791
6792 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6793 {
6794 /* mbedtls_ssl_read_record may have sent an alert already. We
6795 let it decide whether to alert. */
6796 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6797 goto exit;
6798 }
6799
6800#if defined(MBEDTLS_SSL_SRV_C)
6801 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6802 {
6803 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6804
6805 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6806 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6807
6808 goto exit;
6809 }
6810#endif /* MBEDTLS_SSL_SRV_C */
6811
6812 /* Clear existing peer CRT structure in case we tried to
6813 * reuse a session but it failed, and allocate a new one. */
6814 ssl_clear_peer_cert( ssl->session_negotiate );
6815
6816 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6817 if( chain == NULL )
6818 {
6819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6820 sizeof( mbedtls_x509_crt ) ) );
6821 mbedtls_ssl_send_alert_message( ssl,
6822 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6823 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6824
6825 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6826 goto exit;
6827 }
6828 mbedtls_x509_crt_init( chain );
6829
6830 ret = ssl_parse_certificate_chain( ssl, chain );
6831 if( ret != 0 )
6832 goto exit;
6833
6834#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6835 if( ssl->handshake->ecrs_enabled)
6836 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6837
6838crt_verify:
6839 if( ssl->handshake->ecrs_enabled)
6840 rs_ctx = &ssl->handshake->ecrs_ctx;
6841#endif
6842
6843 ret = ssl_parse_certificate_verify( ssl, authmode,
6844 chain, rs_ctx );
6845 if( ret != 0 )
6846 goto exit;
6847
6848#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6849 {
6850 unsigned char *crt_start, *pk_start;
6851 size_t crt_len, pk_len;
6852
6853 /* We parse the CRT chain without copying, so
6854 * these pointers point into the input buffer,
6855 * and are hence still valid after freeing the
6856 * CRT chain. */
6857
6858 crt_start = chain->raw.p;
6859 crt_len = chain->raw.len;
6860
6861 pk_start = chain->pk_raw.p;
6862 pk_len = chain->pk_raw.len;
6863
6864 /* Free the CRT structures before computing
6865 * digest and copying the peer's public key. */
6866 mbedtls_x509_crt_free( chain );
6867 mbedtls_free( chain );
6868 chain = NULL;
6869
6870 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6871 if( ret != 0 )
6872 goto exit;
6873
6874 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6875 if( ret != 0 )
6876 goto exit;
6877 }
6878#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6879 /* Pass ownership to session structure. */
6880 ssl->session_negotiate->peer_cert = chain;
6881 chain = NULL;
6882#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6883
6884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6885
6886exit:
6887
6888 if( ret == 0 )
6889 ssl->state++;
6890
6891#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6892 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6893 {
6894 ssl->handshake->ecrs_peer_cert = chain;
6895 chain = NULL;
6896 }
6897#endif
6898
6899 if( chain != NULL )
6900 {
6901 mbedtls_x509_crt_free( chain );
6902 mbedtls_free( chain );
6903 }
6904
6905 return( ret );
6906}
6907#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6908
Jerry Yu615bd6f2022-02-17 14:25:15 +08006909
6910#if defined(MBEDTLS_SHA256_C)
6911static void ssl_calc_finished_tls_sha256(
6912 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6913{
6914 int len = 12;
6915 const char *sender;
6916 unsigned char padbuf[32];
6917#if defined(MBEDTLS_USE_PSA_CRYPTO)
6918 size_t hash_size;
6919 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
6920 psa_status_t status;
6921#else
6922 mbedtls_sha256_context sha256;
6923#endif
6924
6925 mbedtls_ssl_session *session = ssl->session_negotiate;
6926 if( !session )
6927 session = ssl->session;
6928
6929 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6930 ? "client finished"
6931 : "server finished";
6932
6933#if defined(MBEDTLS_USE_PSA_CRYPTO)
6934 sha256_psa = psa_hash_operation_init();
6935
6936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6937
6938 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6939 if( status != PSA_SUCCESS )
6940 {
6941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6942 return;
6943 }
6944
6945 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6946 if( status != PSA_SUCCESS )
6947 {
6948 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6949 return;
6950 }
6951 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6952#else
6953
6954 mbedtls_sha256_init( &sha256 );
6955
6956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
6957
6958 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6959
6960 /*
6961 * TLSv1.2:
6962 * hash = PRF( master, finished_label,
6963 * Hash( handshake ) )[0.11]
6964 */
6965
6966#if !defined(MBEDTLS_SHA256_ALT)
6967 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
6968 sha256.state, sizeof( sha256.state ) );
6969#endif
6970
6971 mbedtls_sha256_finish( &sha256, padbuf );
6972 mbedtls_sha256_free( &sha256 );
6973#endif /* MBEDTLS_USE_PSA_CRYPTO */
6974
6975 ssl->handshake->tls_prf( session->master, 48, sender,
6976 padbuf, 32, buf, len );
6977
6978 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6979
6980 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6981
6982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6983}
6984#endif /* MBEDTLS_SHA256_C */
6985
Jerry Yub7ba49e2022-02-17 14:25:53 +08006986
6987#if defined(MBEDTLS_SHA384_C)
6988
6989static void ssl_calc_finished_tls_sha384(
6990 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6991{
6992 int len = 12;
6993 const char *sender;
6994 unsigned char padbuf[48];
6995#if defined(MBEDTLS_USE_PSA_CRYPTO)
6996 size_t hash_size;
6997 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
6998 psa_status_t status;
6999#else
7000 mbedtls_sha512_context sha512;
7001#endif
7002
7003 mbedtls_ssl_session *session = ssl->session_negotiate;
7004 if( !session )
7005 session = ssl->session;
7006
7007 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7008 ? "client finished"
7009 : "server finished";
7010
7011#if defined(MBEDTLS_USE_PSA_CRYPTO)
7012 sha384_psa = psa_hash_operation_init();
7013
7014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7015
7016 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7017 if( status != PSA_SUCCESS )
7018 {
7019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7020 return;
7021 }
7022
7023 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7024 if( status != PSA_SUCCESS )
7025 {
7026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7027 return;
7028 }
7029 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7030#else
7031 mbedtls_sha512_init( &sha512 );
7032
7033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7034
7035 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
7036
7037 /*
7038 * TLSv1.2:
7039 * hash = PRF( master, finished_label,
7040 * Hash( handshake ) )[0.11]
7041 */
7042
7043#if !defined(MBEDTLS_SHA512_ALT)
7044 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7045 sha512.state, sizeof( sha512.state ) );
7046#endif
7047 mbedtls_sha512_finish( &sha512, padbuf );
7048
7049 mbedtls_sha512_free( &sha512 );
7050#endif
7051
7052 ssl->handshake->tls_prf( session->master, 48, sender,
7053 padbuf, 48, buf, len );
7054
7055 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7056
7057 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7058
7059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7060}
7061#endif /* MBEDTLS_SHA384_C */
7062
Jerry Yuaef00152022-02-17 14:27:31 +08007063void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7064{
7065 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7066
7067 /*
7068 * Free our handshake params
7069 */
7070 mbedtls_ssl_handshake_free( ssl );
7071 mbedtls_free( ssl->handshake );
7072 ssl->handshake = NULL;
7073
7074 /*
7075 * Free the previous transform and swith in the current one
7076 */
7077 if( ssl->transform )
7078 {
7079 mbedtls_ssl_transform_free( ssl->transform );
7080 mbedtls_free( ssl->transform );
7081 }
7082 ssl->transform = ssl->transform_negotiate;
7083 ssl->transform_negotiate = NULL;
7084
7085 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7086}
7087
Jerry Yu2a9fff52022-02-17 14:28:51 +08007088
7089void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7090{
7091 int resume = ssl->handshake->resume;
7092
7093 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7094
7095#if defined(MBEDTLS_SSL_RENEGOTIATION)
7096 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7097 {
7098 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7099 ssl->renego_records_seen = 0;
7100 }
7101#endif
7102
7103 /*
7104 * Free the previous session and switch in the current one
7105 */
7106 if( ssl->session )
7107 {
7108#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7109 /* RFC 7366 3.1: keep the EtM state */
7110 ssl->session_negotiate->encrypt_then_mac =
7111 ssl->session->encrypt_then_mac;
7112#endif
7113
7114 mbedtls_ssl_session_free( ssl->session );
7115 mbedtls_free( ssl->session );
7116 }
7117 ssl->session = ssl->session_negotiate;
7118 ssl->session_negotiate = NULL;
7119
7120 /*
7121 * Add cache entry
7122 */
7123 if( ssl->conf->f_set_cache != NULL &&
7124 ssl->session->id_len != 0 &&
7125 resume == 0 )
7126 {
7127 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7128 ssl->session->id,
7129 ssl->session->id_len,
7130 ssl->session ) != 0 )
7131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7132 }
7133
7134#if defined(MBEDTLS_SSL_PROTO_DTLS)
7135 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7136 ssl->handshake->flight != NULL )
7137 {
7138 /* Cancel handshake timer */
7139 mbedtls_ssl_set_timer( ssl, 0 );
7140
7141 /* Keep last flight around in case we need to resend it:
7142 * we need the handshake and transform structures for that */
7143 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7144 }
7145 else
7146#endif
7147 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7148
7149 ssl->state++;
7150
7151 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7152}
7153
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007154int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7155{
7156 int ret, hash_len;
7157
7158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7159
7160 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7161
7162 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7163
7164 /*
7165 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7166 * may define some other value. Currently (early 2016), no defined
7167 * ciphersuite does this (and this is unlikely to change as activity has
7168 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7169 */
7170 hash_len = 12;
7171
7172#if defined(MBEDTLS_SSL_RENEGOTIATION)
7173 ssl->verify_data_len = hash_len;
7174 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7175#endif
7176
7177 ssl->out_msglen = 4 + hash_len;
7178 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7179 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7180
7181 /*
7182 * In case of session resuming, invert the client and server
7183 * ChangeCipherSpec messages order.
7184 */
7185 if( ssl->handshake->resume != 0 )
7186 {
7187#if defined(MBEDTLS_SSL_CLI_C)
7188 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7189 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7190#endif
7191#if defined(MBEDTLS_SSL_SRV_C)
7192 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7193 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7194#endif
7195 }
7196 else
7197 ssl->state++;
7198
7199 /*
7200 * Switch to our negotiated transform and session parameters for outbound
7201 * data.
7202 */
7203 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7204
7205#if defined(MBEDTLS_SSL_PROTO_DTLS)
7206 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7207 {
7208 unsigned char i;
7209
7210 /* Remember current epoch settings for resending */
7211 ssl->handshake->alt_transform_out = ssl->transform_out;
7212 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7213 sizeof( ssl->handshake->alt_out_ctr ) );
7214
7215 /* Set sequence_number to zero */
7216 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7217
7218
7219 /* Increment epoch */
7220 for( i = 2; i > 0; i-- )
7221 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7222 break;
7223
7224 /* The loop goes to its end iff the counter is wrapping */
7225 if( i == 0 )
7226 {
7227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7228 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7229 }
7230 }
7231 else
7232#endif /* MBEDTLS_SSL_PROTO_DTLS */
7233 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7234
7235 ssl->transform_out = ssl->transform_negotiate;
7236 ssl->session_out = ssl->session_negotiate;
7237
7238#if defined(MBEDTLS_SSL_PROTO_DTLS)
7239 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7240 mbedtls_ssl_send_flight_completed( ssl );
7241#endif
7242
7243 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7244 {
7245 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7246 return( ret );
7247 }
7248
7249#if defined(MBEDTLS_SSL_PROTO_DTLS)
7250 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7251 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7252 {
7253 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7254 return( ret );
7255 }
7256#endif
7257
7258 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7259
7260 return( 0 );
7261}
7262
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007263#define SSL_MAX_HASH_LEN 12
7264
7265int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7266{
7267 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7268 unsigned int hash_len = 12;
7269 unsigned char buf[SSL_MAX_HASH_LEN];
7270
7271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7272
7273 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7274
7275 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7276 {
7277 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7278 goto exit;
7279 }
7280
7281 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7282 {
7283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7284 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7285 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7286 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7287 goto exit;
7288 }
7289
7290 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7291 {
7292 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7293 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7294 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7295 goto exit;
7296 }
7297
7298 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7299 {
7300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7301 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7302 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7303 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7304 goto exit;
7305 }
7306
7307 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7308 buf, hash_len ) != 0 )
7309 {
7310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7311 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7312 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7313 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7314 goto exit;
7315 }
7316
7317#if defined(MBEDTLS_SSL_RENEGOTIATION)
7318 ssl->verify_data_len = hash_len;
7319 memcpy( ssl->peer_verify_data, buf, hash_len );
7320#endif
7321
7322 if( ssl->handshake->resume != 0 )
7323 {
7324#if defined(MBEDTLS_SSL_CLI_C)
7325 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7326 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7327#endif
7328#if defined(MBEDTLS_SSL_SRV_C)
7329 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7330 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7331#endif
7332 }
7333 else
7334 ssl->state++;
7335
7336#if defined(MBEDTLS_SSL_PROTO_DTLS)
7337 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7338 mbedtls_ssl_recv_flight_completed( ssl );
7339#endif
7340
7341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7342
7343exit:
7344 mbedtls_platform_zeroize( buf, hash_len );
7345 return( ret );
7346}
7347
Jerry Yu392112c2022-02-17 14:34:10 +08007348#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7349/*
7350 * Helper to get TLS 1.2 PRF from ciphersuite
7351 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7352 */
7353static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7354{
7355#if defined(MBEDTLS_SHA384_C)
7356 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7357 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7358
7359 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
7360 return( tls_prf_sha384 );
7361#else
7362 (void) ciphersuite_id;
7363#endif
7364 return( tls_prf_sha256 );
7365}
7366#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007367
7368static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7369{
7370 ((void) tls_prf);
7371#if defined(MBEDTLS_SHA384_C)
7372 if( tls_prf == tls_prf_sha384 )
7373 {
7374 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7375 }
7376 else
7377#endif
7378#if defined(MBEDTLS_SHA256_C)
7379 if( tls_prf == tls_prf_sha256 )
7380 {
7381 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7382 }
7383 else
7384#endif
7385 return( MBEDTLS_SSL_TLS_PRF_NONE );
7386}
7387
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007388/*
7389 * Populate a transform structure with session keys and all the other
7390 * necessary information.
7391 *
7392 * Parameters:
7393 * - [in/out]: transform: structure to populate
7394 * [in] must be just initialised with mbedtls_ssl_transform_init()
7395 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7396 * - [in] ciphersuite
7397 * - [in] master
7398 * - [in] encrypt_then_mac
7399 * - [in] compression
7400 * - [in] tls_prf: pointer to PRF to use for key derivation
7401 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
7402 * - [in] minor_ver: SSL/TLS minor version
7403 * - [in] endpoint: client or server
7404 * - [in] ssl: used for:
7405 * - ssl->conf->{f,p}_export_keys
7406 * [in] optionally used for:
7407 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7408 */
7409static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7410 int ciphersuite,
7411 const unsigned char master[48],
7412#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \
7413 defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7414 int encrypt_then_mac,
7415#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC &&
7416 MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7417 ssl_tls_prf_t tls_prf,
7418 const unsigned char randbytes[64],
7419 int minor_ver,
7420 unsigned endpoint,
7421 const mbedtls_ssl_context *ssl )
7422{
7423 int ret = 0;
7424 unsigned char keyblk[256];
7425 unsigned char *key1;
7426 unsigned char *key2;
7427 unsigned char *mac_enc;
7428 unsigned char *mac_dec;
7429 size_t mac_key_len = 0;
7430 size_t iv_copy_len;
7431 size_t keylen;
7432 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
7433 const mbedtls_cipher_info_t *cipher_info;
7434 const mbedtls_md_info_t *md_info;
7435
7436#if defined(MBEDTLS_USE_PSA_CRYPTO)
7437 psa_key_type_t key_type;
7438 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7439 psa_algorithm_t alg;
7440 size_t key_bits;
7441 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7442#endif
7443
7444#if !defined(MBEDTLS_DEBUG_C) && \
7445 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7446 if( ssl->f_export_keys == NULL )
7447 {
7448 ssl = NULL; /* make sure we don't use it except for these cases */
7449 (void) ssl;
7450 }
7451#endif
7452
7453 /*
7454 * Some data just needs copying into the structure
7455 */
7456#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
7457 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7458 transform->encrypt_then_mac = encrypt_then_mac;
7459#endif
7460 transform->minor_ver = minor_ver;
7461
7462#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7463 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7464#endif
7465
7466#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
7467 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
7468 {
7469 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7470 * generation separate. This should never happen. */
7471 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7472 }
7473#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7474
7475 /*
7476 * Get various info structures
7477 */
7478 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7479 if( ciphersuite_info == NULL )
7480 {
7481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7482 ciphersuite ) );
7483 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7484 }
7485
7486 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7487 if( cipher_info == NULL )
7488 {
7489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7490 ciphersuite_info->cipher ) );
7491 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7492 }
7493
7494 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7495 if( md_info == NULL )
7496 {
7497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7498 (unsigned) ciphersuite_info->mac ) );
7499 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7500 }
7501
7502#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7503 /* Copy own and peer's CID if the use of the CID
7504 * extension has been negotiated. */
7505 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7506 {
7507 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7508
7509 transform->in_cid_len = ssl->own_cid_len;
7510 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7511 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7512 transform->in_cid_len );
7513
7514 transform->out_cid_len = ssl->handshake->peer_cid_len;
7515 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7516 ssl->handshake->peer_cid_len );
7517 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7518 transform->out_cid_len );
7519 }
7520#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7521
7522 /*
7523 * Compute key block using the PRF
7524 */
7525 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7526 if( ret != 0 )
7527 {
7528 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7529 return( ret );
7530 }
7531
7532 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7533 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7534 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7535 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7536 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7537
7538 /*
7539 * Determine the appropriate key, IV and MAC length.
7540 */
7541
7542 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
7543
7544#if defined(MBEDTLS_GCM_C) || \
7545 defined(MBEDTLS_CCM_C) || \
7546 defined(MBEDTLS_CHACHAPOLY_C)
7547 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_GCM ||
7548 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CCM ||
7549 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
7550 {
7551 size_t explicit_ivlen;
7552
7553 transform->maclen = 0;
7554 mac_key_len = 0;
7555 transform->taglen =
7556 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7557
7558 /* All modes haves 96-bit IVs, but the length of the static parts vary
7559 * with mode and version:
7560 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7561 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7562 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7563 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7564 * sequence number).
7565 */
7566 transform->ivlen = 12;
7567 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
7568 transform->fixed_ivlen = 12;
7569 else
7570 transform->fixed_ivlen = 4;
7571
7572 /* Minimum length of encrypted record */
7573 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7574 transform->minlen = explicit_ivlen + transform->taglen;
7575 }
7576 else
7577#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7578#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7579 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM ||
7580 mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7581 {
7582 /* Initialize HMAC contexts */
7583 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7584 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7585 {
7586 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7587 goto end;
7588 }
7589
7590 /* Get MAC length */
7591 mac_key_len = mbedtls_md_get_size( md_info );
7592 transform->maclen = mac_key_len;
7593
7594 /* IV length */
7595 transform->ivlen = cipher_info->iv_size;
7596
7597 /* Minimum length */
7598 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM )
7599 transform->minlen = transform->maclen;
7600 else
7601 {
7602 /*
7603 * GenericBlockCipher:
7604 * 1. if EtM is in use: one block plus MAC
7605 * otherwise: * first multiple of blocklen greater than maclen
7606 * 2. IV
7607 */
7608#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7609 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
7610 {
7611 transform->minlen = transform->maclen
7612 + cipher_info->block_size;
7613 }
7614 else
7615#endif
7616 {
7617 transform->minlen = transform->maclen
7618 + cipher_info->block_size
7619 - transform->maclen % cipher_info->block_size;
7620 }
7621
7622#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7623 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
7624 {
7625 transform->minlen += transform->ivlen;
7626 }
7627 else
7628#endif
7629 {
7630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7631 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7632 goto end;
7633 }
7634 }
7635 }
7636 else
7637#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7638 {
7639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7640 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7641 }
7642
7643 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7644 (unsigned) keylen,
7645 (unsigned) transform->minlen,
7646 (unsigned) transform->ivlen,
7647 (unsigned) transform->maclen ) );
7648
7649 /*
7650 * Finally setup the cipher contexts, IVs and MAC secrets.
7651 */
7652#if defined(MBEDTLS_SSL_CLI_C)
7653 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7654 {
7655 key1 = keyblk + mac_key_len * 2;
7656 key2 = keyblk + mac_key_len * 2 + keylen;
7657
7658 mac_enc = keyblk;
7659 mac_dec = keyblk + mac_key_len;
7660
7661 /*
7662 * This is not used in TLS v1.1.
7663 */
7664 iv_copy_len = ( transform->fixed_ivlen ) ?
7665 transform->fixed_ivlen : transform->ivlen;
7666 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7667 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7668 iv_copy_len );
7669 }
7670 else
7671#endif /* MBEDTLS_SSL_CLI_C */
7672#if defined(MBEDTLS_SSL_SRV_C)
7673 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7674 {
7675 key1 = keyblk + mac_key_len * 2 + keylen;
7676 key2 = keyblk + mac_key_len * 2;
7677
7678 mac_enc = keyblk + mac_key_len;
7679 mac_dec = keyblk;
7680
7681 /*
7682 * This is not used in TLS v1.1.
7683 */
7684 iv_copy_len = ( transform->fixed_ivlen ) ?
7685 transform->fixed_ivlen : transform->ivlen;
7686 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7687 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7688 iv_copy_len );
7689 }
7690 else
7691#endif /* MBEDTLS_SSL_SRV_C */
7692 {
7693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7694 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7695 goto end;
7696 }
7697
7698#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7699#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7700 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7701 For AEAD-based ciphersuites, there is nothing to do here. */
7702 if( mac_key_len != 0 )
7703 {
7704 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7705 if( ret != 0 )
7706 goto end;
7707 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7708 if( ret != 0 )
7709 goto end;
7710 }
7711#endif
7712#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7713
7714 ((void) mac_dec);
7715 ((void) mac_enc);
7716
7717 if( ssl != NULL && ssl->f_export_keys != NULL )
7718 {
7719 ssl->f_export_keys( ssl->p_export_keys,
7720 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7721 master, 48,
7722 randbytes + 32,
7723 randbytes,
7724 tls_prf_get_type( tls_prf ) );
7725 }
7726
7727#if defined(MBEDTLS_USE_PSA_CRYPTO)
7728 if( ( status = mbedtls_ssl_cipher_to_psa( cipher_info->type,
7729 transform->taglen,
7730 &alg,
7731 &key_type,
7732 &key_bits ) ) != PSA_SUCCESS )
7733 {
7734 ret = psa_ssl_status_to_mbedtls( status );
7735 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7736 goto end;
7737 }
7738
7739 transform->psa_alg = alg;
7740
7741 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7742 {
7743 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7744 psa_set_key_algorithm( &attributes, alg );
7745 psa_set_key_type( &attributes, key_type );
7746
7747 if( ( status = psa_import_key( &attributes,
7748 key1,
7749 PSA_BITS_TO_BYTES( key_bits ),
7750 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7751 {
7752 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7753 ret = psa_ssl_status_to_mbedtls( status );
7754 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7755 goto end;
7756 }
7757
7758 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7759
7760 if( ( status = psa_import_key( &attributes,
7761 key2,
7762 PSA_BITS_TO_BYTES( key_bits ),
7763 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7764 {
7765 ret = psa_ssl_status_to_mbedtls( status );
7766 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7767 goto end;
7768 }
7769 }
7770#else
7771 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7772 cipher_info ) ) != 0 )
7773 {
7774 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7775 goto end;
7776 }
7777
7778 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7779 cipher_info ) ) != 0 )
7780 {
7781 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7782 goto end;
7783 }
7784
7785 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7786 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7787 MBEDTLS_ENCRYPT ) ) != 0 )
7788 {
7789 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7790 goto end;
7791 }
7792
7793 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7794 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7795 MBEDTLS_DECRYPT ) ) != 0 )
7796 {
7797 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7798 goto end;
7799 }
7800
7801#if defined(MBEDTLS_CIPHER_MODE_CBC)
7802 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7803 {
7804 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7805 MBEDTLS_PADDING_NONE ) ) != 0 )
7806 {
7807 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7808 goto end;
7809 }
7810
7811 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7812 MBEDTLS_PADDING_NONE ) ) != 0 )
7813 {
7814 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7815 goto end;
7816 }
7817 }
7818#endif /* MBEDTLS_CIPHER_MODE_CBC */
7819#endif /* MBEDTLS_USE_PSA_CRYPTO */
7820
7821end:
7822 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7823 return( ret );
7824}
7825
Jerry Yuee40f9d2022-02-17 14:55:16 +08007826#if defined(MBEDTLS_USE_PSA_CRYPTO)
7827int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7828 unsigned char *hash, size_t *hashlen,
7829 unsigned char *data, size_t data_len,
7830 mbedtls_md_type_t md_alg )
7831{
7832 psa_status_t status;
7833 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
7834 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
7835
7836 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7837
7838 if( ( status = psa_hash_setup( &hash_operation,
7839 hash_alg ) ) != PSA_SUCCESS )
7840 {
7841 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7842 goto exit;
7843 }
7844
7845 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
7846 64 ) ) != PSA_SUCCESS )
7847 {
7848 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7849 goto exit;
7850 }
7851
7852 if( ( status = psa_hash_update( &hash_operation,
7853 data, data_len ) ) != PSA_SUCCESS )
7854 {
7855 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7856 goto exit;
7857 }
7858
7859 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
7860 hashlen ) ) != PSA_SUCCESS )
7861 {
7862 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
7863 goto exit;
7864 }
7865
7866exit:
7867 if( status != PSA_SUCCESS )
7868 {
7869 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7870 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7871 switch( status )
7872 {
7873 case PSA_ERROR_NOT_SUPPORTED:
7874 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
7875 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
7876 case PSA_ERROR_BUFFER_TOO_SMALL:
7877 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
7878 case PSA_ERROR_INSUFFICIENT_MEMORY:
7879 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
7880 default:
7881 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
7882 }
7883 }
7884 return( 0 );
7885}
7886
7887#else
7888
7889int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7890 unsigned char *hash, size_t *hashlen,
7891 unsigned char *data, size_t data_len,
7892 mbedtls_md_type_t md_alg )
7893{
7894 int ret = 0;
7895 mbedtls_md_context_t ctx;
7896 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
7897 *hashlen = mbedtls_md_get_size( md_info );
7898
7899 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
7900
7901 mbedtls_md_init( &ctx );
7902
7903 /*
7904 * digitally-signed struct {
7905 * opaque client_random[32];
7906 * opaque server_random[32];
7907 * ServerDHParams params;
7908 * };
7909 */
7910 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
7911 {
7912 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7913 goto exit;
7914 }
7915 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
7916 {
7917 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
7918 goto exit;
7919 }
7920 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
7921 {
7922 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7923 goto exit;
7924 }
7925 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
7926 {
7927 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7928 goto exit;
7929 }
7930 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
7931 {
7932 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
7933 goto exit;
7934 }
7935
7936exit:
7937 mbedtls_md_free( &ctx );
7938
7939 if( ret != 0 )
7940 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7941 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7942
7943 return( ret );
7944}
7945#endif /* MBEDTLS_USE_PSA_CRYPTO */
7946
Jerry Yudc7bd172022-02-17 13:44:15 +08007947#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949#endif /* MBEDTLS_SSL_TLS_C */