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