blob: 72351c9757c77a5ec17f4878e398a6d321221a3e [file] [log] [blame]
Jens Wiklander817466c2018-05-22 13:49:31 +02001/*
2 * SSLv3/TLSv1 client-side functions
3 *
Jerome Forissier79013242021-07-28 10:24:04 +02004 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
Jens Wiklander817466c2018-05-22 13:49:31 +02006 *
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.
Jens Wiklander817466c2018-05-22 13:49:31 +020018 */
19
Jerome Forissier79013242021-07-28 10:24:04 +020020#include "common.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020021
22#if defined(MBEDTLS_SSL_CLI_C)
23
24#if defined(MBEDTLS_PLATFORM_C)
25#include "mbedtls/platform.h"
26#else
27#include <stdlib.h>
28#define mbedtls_calloc calloc
29#define mbedtls_free free
30#endif
31
Jens Wiklander817466c2018-05-22 13:49:31 +020032#include "mbedtls/ssl.h"
33#include "mbedtls/ssl_internal.h"
Jerome Forissier11fa71b2020-04-20 17:17:56 +020034#include "mbedtls/debug.h"
35#include "mbedtls/error.h"
Jerome Forissier039e02d2022-08-09 17:10:15 +020036#include "mbedtls/constant_time.h"
Jerome Forissier11fa71b2020-04-20 17:17:56 +020037
38#if defined(MBEDTLS_USE_PSA_CRYPTO)
39#include "mbedtls/psa_util.h"
Jerome Forissier039e02d2022-08-09 17:10:15 +020040#include "psa/crypto.h"
Jerome Forissier11fa71b2020-04-20 17:17:56 +020041#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jens Wiklander817466c2018-05-22 13:49:31 +020042
43#include <string.h>
44
45#include <stdint.h>
46
47#if defined(MBEDTLS_HAVE_TIME)
48#include "mbedtls/platform_time.h"
49#endif
50
51#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jens Wiklander3d3b0592019-03-20 15:30:29 +010052#include "mbedtls/platform_util.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020053#endif
54
Jerome Forissier11fa71b2020-04-20 17:17:56 +020055#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerome Forissier039e02d2022-08-09 17:10:15 +020056MBEDTLS_CHECK_RETURN_CRITICAL
Jerome Forissier11fa71b2020-04-20 17:17:56 +020057static int ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
58{
59 if( conf->psk_identity == NULL ||
60 conf->psk_identity_len == 0 )
61 {
62 return( 0 );
63 }
64
65 if( conf->psk != NULL && conf->psk_len != 0 )
66 return( 1 );
67
68#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerome Forissier79013242021-07-28 10:24:04 +020069 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Jerome Forissier11fa71b2020-04-20 17:17:56 +020070 return( 1 );
71#endif /* MBEDTLS_USE_PSA_CRYPTO */
72
73 return( 0 );
74}
75
76#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerome Forissier039e02d2022-08-09 17:10:15 +020077MBEDTLS_CHECK_RETURN_CRITICAL
Jerome Forissier11fa71b2020-04-20 17:17:56 +020078static int ssl_conf_has_static_raw_psk( mbedtls_ssl_config const *conf )
79{
80 if( conf->psk_identity == NULL ||
81 conf->psk_identity_len == 0 )
82 {
83 return( 0 );
84 }
85
86 if( conf->psk != NULL && conf->psk_len != 0 )
87 return( 1 );
88
89 return( 0 );
90}
91#endif /* MBEDTLS_USE_PSA_CRYPTO */
92
93#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
94
Jens Wiklander817466c2018-05-22 13:49:31 +020095#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Jerome Forissier039e02d2022-08-09 17:10:15 +020096MBEDTLS_CHECK_RETURN_CRITICAL
Jerome Forissier79013242021-07-28 10:24:04 +020097static int ssl_write_hostname_ext( mbedtls_ssl_context *ssl,
98 unsigned char *buf,
99 const unsigned char *end,
100 size_t *olen )
Jens Wiklander817466c2018-05-22 13:49:31 +0200101{
102 unsigned char *p = buf;
Jens Wiklander817466c2018-05-22 13:49:31 +0200103 size_t hostname_len;
104
105 *olen = 0;
106
107 if( ssl->hostname == NULL )
Jerome Forissier79013242021-07-28 10:24:04 +0200108 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200109
Jerome Forissier79013242021-07-28 10:24:04 +0200110 MBEDTLS_SSL_DEBUG_MSG( 3,
111 ( "client hello, adding server name extension: %s",
112 ssl->hostname ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200113
114 hostname_len = strlen( ssl->hostname );
115
Jerome Forissier79013242021-07-28 10:24:04 +0200116 MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200117
118 /*
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100119 * Sect. 3, RFC 6066 (TLS Extensions Definitions)
120 *
121 * In order to provide any of the server names, clients MAY include an
122 * extension of type "server_name" in the (extended) client hello. The
123 * "extension_data" field of this extension SHALL contain
124 * "ServerNameList" where:
125 *
Jens Wiklander817466c2018-05-22 13:49:31 +0200126 * struct {
127 * NameType name_type;
128 * select (name_type) {
129 * case host_name: HostName;
130 * } name;
131 * } ServerName;
132 *
133 * enum {
134 * host_name(0), (255)
135 * } NameType;
136 *
137 * opaque HostName<1..2^16-1>;
138 *
139 * struct {
140 * ServerName server_name_list<1..2^16-1>
141 * } ServerNameList;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100142 *
Jens Wiklander817466c2018-05-22 13:49:31 +0200143 */
Jerome Forissier039e02d2022-08-09 17:10:15 +0200144 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SERVERNAME, p, 0 );
145 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +0200146
Jerome Forissier039e02d2022-08-09 17:10:15 +0200147 MBEDTLS_PUT_UINT16_BE( hostname_len + 5, p, 0 );
148 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +0200149
Jerome Forissier039e02d2022-08-09 17:10:15 +0200150 MBEDTLS_PUT_UINT16_BE( hostname_len + 3, p, 0 );
151 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +0200152
Jerome Forissier039e02d2022-08-09 17:10:15 +0200153 *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME );
154
155 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
156 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +0200157
158 memcpy( p, ssl->hostname, hostname_len );
159
160 *olen = hostname_len + 9;
Jerome Forissier79013242021-07-28 10:24:04 +0200161
162 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200163}
164#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
165
166#if defined(MBEDTLS_SSL_RENEGOTIATION)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200167MBEDTLS_CHECK_RETURN_CRITICAL
Jerome Forissier79013242021-07-28 10:24:04 +0200168static int ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
169 unsigned char *buf,
170 const unsigned char *end,
171 size_t *olen )
Jens Wiklander817466c2018-05-22 13:49:31 +0200172{
173 unsigned char *p = buf;
Jens Wiklander817466c2018-05-22 13:49:31 +0200174
175 *olen = 0;
176
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100177 /* We're always including an TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
178 * initial ClientHello, in which case also adding the renegotiation
179 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Jens Wiklander817466c2018-05-22 13:49:31 +0200180 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Jerome Forissier79013242021-07-28 10:24:04 +0200181 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200182
Jerome Forissier79013242021-07-28 10:24:04 +0200183 MBEDTLS_SSL_DEBUG_MSG( 3,
184 ( "client hello, adding renegotiation extension" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200185
Jerome Forissier79013242021-07-28 10:24:04 +0200186 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + ssl->verify_data_len );
Jens Wiklander817466c2018-05-22 13:49:31 +0200187
188 /*
189 * Secure renegotiation
190 */
Jerome Forissier039e02d2022-08-09 17:10:15 +0200191 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0 );
192 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +0200193
194 *p++ = 0x00;
Jerome Forissier039e02d2022-08-09 17:10:15 +0200195 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len + 1 );
196 *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len );
Jens Wiklander817466c2018-05-22 13:49:31 +0200197
198 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
199
200 *olen = 5 + ssl->verify_data_len;
Jerome Forissier79013242021-07-28 10:24:04 +0200201
202 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200203}
204#endif /* MBEDTLS_SSL_RENEGOTIATION */
205
206/*
207 * Only if we handle at least one key exchange that needs signatures.
208 */
209#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200210 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200211MBEDTLS_CHECK_RETURN_CRITICAL
Jerome Forissier79013242021-07-28 10:24:04 +0200212static int ssl_write_signature_algorithms_ext( mbedtls_ssl_context *ssl,
213 unsigned char *buf,
214 const unsigned char *end,
215 size_t *olen )
Jens Wiklander817466c2018-05-22 13:49:31 +0200216{
217 unsigned char *p = buf;
Jens Wiklander817466c2018-05-22 13:49:31 +0200218 size_t sig_alg_len = 0;
219 const int *md;
Jerome Forissier79013242021-07-28 10:24:04 +0200220
Jens Wiklander817466c2018-05-22 13:49:31 +0200221#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C)
222 unsigned char *sig_alg_list = buf + 6;
223#endif
224
225 *olen = 0;
226
227 if( ssl->conf->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Jerome Forissier79013242021-07-28 10:24:04 +0200228 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200229
Jerome Forissier79013242021-07-28 10:24:04 +0200230 MBEDTLS_SSL_DEBUG_MSG( 3,
231 ( "client hello, adding signature_algorithms extension" ) );
232
233 if( ssl->conf->sig_hashes == NULL )
234 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jens Wiklander817466c2018-05-22 13:49:31 +0200235
236 for( md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
237 {
238#if defined(MBEDTLS_ECDSA_C)
239 sig_alg_len += 2;
240#endif
241#if defined(MBEDTLS_RSA_C)
242 sig_alg_len += 2;
243#endif
Jerome Forissier79013242021-07-28 10:24:04 +0200244 if( sig_alg_len > MBEDTLS_SSL_MAX_SIG_HASH_ALG_LIST_LEN )
245 {
246 MBEDTLS_SSL_DEBUG_MSG( 3,
247 ( "length in bytes of sig-hash-alg extension too big" ) );
248 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
249 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200250 }
251
Jerome Forissier79013242021-07-28 10:24:04 +0200252 /* Empty signature algorithms list, this is a configuration error. */
253 if( sig_alg_len == 0 )
254 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
255
256 MBEDTLS_SSL_CHK_BUF_PTR( p, end, sig_alg_len + 6 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200257
258 /*
259 * Prepare signature_algorithms extension (TLS 1.2)
260 */
261 sig_alg_len = 0;
262
263 for( md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
264 {
265#if defined(MBEDTLS_ECDSA_C)
266 sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg( *md );
267 sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_ECDSA;
268#endif
269#if defined(MBEDTLS_RSA_C)
270 sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg( *md );
271 sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_RSA;
272#endif
273 }
274
275 /*
276 * enum {
277 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
278 * sha512(6), (255)
279 * } HashAlgorithm;
280 *
281 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
282 * SignatureAlgorithm;
283 *
284 * struct {
285 * HashAlgorithm hash;
286 * SignatureAlgorithm signature;
287 * } SignatureAndHashAlgorithm;
288 *
289 * SignatureAndHashAlgorithm
290 * supported_signature_algorithms<2..2^16-2>;
291 */
Jerome Forissier039e02d2022-08-09 17:10:15 +0200292 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, p, 0 );
293 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +0200294
Jerome Forissier039e02d2022-08-09 17:10:15 +0200295 MBEDTLS_PUT_UINT16_BE( sig_alg_len + 2, p, 0 );
296 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +0200297
Jerome Forissier039e02d2022-08-09 17:10:15 +0200298 MBEDTLS_PUT_UINT16_BE( sig_alg_len, p, 0 );
299 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +0200300
301 *olen = 6 + sig_alg_len;
Jerome Forissier79013242021-07-28 10:24:04 +0200302
303 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200304}
305#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200306 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +0200307
308#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
309 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200310MBEDTLS_CHECK_RETURN_CRITICAL
Jerome Forissier79013242021-07-28 10:24:04 +0200311static int ssl_write_supported_elliptic_curves_ext( mbedtls_ssl_context *ssl,
312 unsigned char *buf,
313 const unsigned char *end,
314 size_t *olen )
Jens Wiklander817466c2018-05-22 13:49:31 +0200315{
316 unsigned char *p = buf;
Jens Wiklander817466c2018-05-22 13:49:31 +0200317 unsigned char *elliptic_curve_list = p + 6;
318 size_t elliptic_curve_len = 0;
319 const mbedtls_ecp_curve_info *info;
Jens Wiklander817466c2018-05-22 13:49:31 +0200320 const mbedtls_ecp_group_id *grp_id;
Jens Wiklander817466c2018-05-22 13:49:31 +0200321
322 *olen = 0;
323
Jerome Forissier79013242021-07-28 10:24:04 +0200324 MBEDTLS_SSL_DEBUG_MSG( 3,
325 ( "client hello, adding supported_elliptic_curves extension" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200326
Jerome Forissier79013242021-07-28 10:24:04 +0200327 if( ssl->conf->curve_list == NULL )
328 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
329
330 for( grp_id = ssl->conf->curve_list;
331 *grp_id != MBEDTLS_ECP_DP_NONE;
332 grp_id++ )
Jens Wiklander817466c2018-05-22 13:49:31 +0200333 {
Jens Wiklander817466c2018-05-22 13:49:31 +0200334 info = mbedtls_ecp_curve_info_from_grp_id( *grp_id );
Jens Wiklander817466c2018-05-22 13:49:31 +0200335 if( info == NULL )
336 {
Jerome Forissier79013242021-07-28 10:24:04 +0200337 MBEDTLS_SSL_DEBUG_MSG( 1,
338 ( "invalid curve in ssl configuration" ) );
339 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jens Wiklander817466c2018-05-22 13:49:31 +0200340 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200341 elliptic_curve_len += 2;
Jerome Forissier79013242021-07-28 10:24:04 +0200342
343 if( elliptic_curve_len > MBEDTLS_SSL_MAX_CURVE_LIST_LEN )
344 {
345 MBEDTLS_SSL_DEBUG_MSG( 3,
346 ( "malformed supported_elliptic_curves extension in config" ) );
347 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
348 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200349 }
350
Jerome Forissier79013242021-07-28 10:24:04 +0200351 /* Empty elliptic curve list, this is a configuration error. */
352 if( elliptic_curve_len == 0 )
353 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
354
355 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 + elliptic_curve_len );
Jens Wiklander817466c2018-05-22 13:49:31 +0200356
357 elliptic_curve_len = 0;
358
Jerome Forissier79013242021-07-28 10:24:04 +0200359 for( grp_id = ssl->conf->curve_list;
360 *grp_id != MBEDTLS_ECP_DP_NONE;
361 grp_id++ )
Jens Wiklander817466c2018-05-22 13:49:31 +0200362 {
Jens Wiklander817466c2018-05-22 13:49:31 +0200363 info = mbedtls_ecp_curve_info_from_grp_id( *grp_id );
Jerome Forissier039e02d2022-08-09 17:10:15 +0200364 elliptic_curve_list[elliptic_curve_len++] = MBEDTLS_BYTE_1( info->tls_id );
365 elliptic_curve_list[elliptic_curve_len++] = MBEDTLS_BYTE_0( info->tls_id );
Jens Wiklander817466c2018-05-22 13:49:31 +0200366 }
367
Jerome Forissier039e02d2022-08-09 17:10:15 +0200368 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES, p, 0 );
369 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +0200370
Jerome Forissier039e02d2022-08-09 17:10:15 +0200371 MBEDTLS_PUT_UINT16_BE( elliptic_curve_len + 2, p, 0 );
372 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +0200373
Jerome Forissier039e02d2022-08-09 17:10:15 +0200374 MBEDTLS_PUT_UINT16_BE( elliptic_curve_len, p, 0 );
375 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +0200376
377 *olen = 6 + elliptic_curve_len;
Jerome Forissier79013242021-07-28 10:24:04 +0200378
379 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200380}
381
Jerome Forissier039e02d2022-08-09 17:10:15 +0200382MBEDTLS_CHECK_RETURN_CRITICAL
Jerome Forissier79013242021-07-28 10:24:04 +0200383static int ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
384 unsigned char *buf,
385 const unsigned char *end,
386 size_t *olen )
Jens Wiklander817466c2018-05-22 13:49:31 +0200387{
388 unsigned char *p = buf;
Jerome Forissier79013242021-07-28 10:24:04 +0200389 (void) ssl; /* ssl used for debugging only */
Jens Wiklander817466c2018-05-22 13:49:31 +0200390
391 *olen = 0;
392
Jerome Forissier79013242021-07-28 10:24:04 +0200393 MBEDTLS_SSL_DEBUG_MSG( 3,
394 ( "client hello, adding supported_point_formats extension" ) );
395 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200396
Jerome Forissier039e02d2022-08-09 17:10:15 +0200397 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0 );
398 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +0200399
400 *p++ = 0x00;
401 *p++ = 2;
402
403 *p++ = 1;
404 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
405
406 *olen = 6;
Jerome Forissier79013242021-07-28 10:24:04 +0200407
408 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200409}
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100410#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Jens Wiklander817466c2018-05-22 13:49:31 +0200411 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
412
413#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200414MBEDTLS_CHECK_RETURN_CRITICAL
Jerome Forissier79013242021-07-28 10:24:04 +0200415static int ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
416 unsigned char *buf,
417 const unsigned char *end,
418 size_t *olen )
Jens Wiklander817466c2018-05-22 13:49:31 +0200419{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200420 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200421 unsigned char *p = buf;
Jens Wiklander817466c2018-05-22 13:49:31 +0200422 size_t kkpp_len;
423
424 *olen = 0;
425
426 /* Skip costly extension if we can't use EC J-PAKE anyway */
427 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
Jerome Forissier79013242021-07-28 10:24:04 +0200428 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200429
Jerome Forissier79013242021-07-28 10:24:04 +0200430 MBEDTLS_SSL_DEBUG_MSG( 3,
431 ( "client hello, adding ecjpake_kkpp extension" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200432
Jerome Forissier79013242021-07-28 10:24:04 +0200433 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200434
Jerome Forissier039e02d2022-08-09 17:10:15 +0200435 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 );
436 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +0200437
438 /*
439 * We may need to send ClientHello multiple times for Hello verification.
440 * We don't want to compute fresh values every time (both for performance
441 * and consistency reasons), so cache the extension content.
442 */
443 if( ssl->handshake->ecjpake_cache == NULL ||
444 ssl->handshake->ecjpake_cache_len == 0 )
445 {
446 MBEDTLS_SSL_DEBUG_MSG( 3, ( "generating new ecjpake parameters" ) );
447
448 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
Jerome Forissier79013242021-07-28 10:24:04 +0200449 p + 2, end - p - 2, &kkpp_len,
450 ssl->conf->f_rng, ssl->conf->p_rng );
Jens Wiklander817466c2018-05-22 13:49:31 +0200451 if( ret != 0 )
452 {
Jerome Forissier79013242021-07-28 10:24:04 +0200453 MBEDTLS_SSL_DEBUG_RET( 1 ,
454 "mbedtls_ecjpake_write_round_one", ret );
455 return( ret );
Jens Wiklander817466c2018-05-22 13:49:31 +0200456 }
457
458 ssl->handshake->ecjpake_cache = mbedtls_calloc( 1, kkpp_len );
459 if( ssl->handshake->ecjpake_cache == NULL )
460 {
461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "allocation failed" ) );
Jerome Forissier79013242021-07-28 10:24:04 +0200462 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Jens Wiklander817466c2018-05-22 13:49:31 +0200463 }
464
465 memcpy( ssl->handshake->ecjpake_cache, p + 2, kkpp_len );
466 ssl->handshake->ecjpake_cache_len = kkpp_len;
467 }
468 else
469 {
470 MBEDTLS_SSL_DEBUG_MSG( 3, ( "re-using cached ecjpake parameters" ) );
471
472 kkpp_len = ssl->handshake->ecjpake_cache_len;
Jerome Forissier79013242021-07-28 10:24:04 +0200473 MBEDTLS_SSL_CHK_BUF_PTR( p + 2, end, kkpp_len );
Jens Wiklander817466c2018-05-22 13:49:31 +0200474
475 memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len );
476 }
477
Jerome Forissier039e02d2022-08-09 17:10:15 +0200478 MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 );
479 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +0200480
481 *olen = kkpp_len + 4;
Jerome Forissier79013242021-07-28 10:24:04 +0200482
483 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200484}
485#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
486
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200487#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200488MBEDTLS_CHECK_RETURN_CRITICAL
Jerome Forissier79013242021-07-28 10:24:04 +0200489static int ssl_write_cid_ext( mbedtls_ssl_context *ssl,
490 unsigned char *buf,
491 const unsigned char *end,
492 size_t *olen )
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200493{
494 unsigned char *p = buf;
495 size_t ext_len;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200496
497 /*
498 * Quoting draft-ietf-tls-dtls-connection-id-05
499 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
500 *
501 * struct {
502 * opaque cid<0..2^8-1>;
503 * } ConnectionId;
504 */
505
506 *olen = 0;
507 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
508 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
509 {
Jerome Forissier79013242021-07-28 10:24:04 +0200510 return( 0 );
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200511 }
512 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding CID extension" ) );
513
514 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
515 * which is at most 255, so the increment cannot overflow. */
Jerome Forissier79013242021-07-28 10:24:04 +0200516 MBEDTLS_SSL_CHK_BUF_PTR( p, end, (unsigned)( ssl->own_cid_len + 5 ) );
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200517
518 /* Add extension ID + size */
Jerome Forissier039e02d2022-08-09 17:10:15 +0200519 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 );
520 p += 2;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200521 ext_len = (size_t) ssl->own_cid_len + 1;
Jerome Forissier039e02d2022-08-09 17:10:15 +0200522 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
523 p += 2;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200524
525 *p++ = (uint8_t) ssl->own_cid_len;
526 memcpy( p, ssl->own_cid, ssl->own_cid_len );
527
528 *olen = ssl->own_cid_len + 5;
Jerome Forissier79013242021-07-28 10:24:04 +0200529
530 return( 0 );
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200531}
532#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
533
Jens Wiklander817466c2018-05-22 13:49:31 +0200534#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200535MBEDTLS_CHECK_RETURN_CRITICAL
Jerome Forissier79013242021-07-28 10:24:04 +0200536static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
537 unsigned char *buf,
538 const unsigned char *end,
539 size_t *olen )
Jens Wiklander817466c2018-05-22 13:49:31 +0200540{
541 unsigned char *p = buf;
Jens Wiklander817466c2018-05-22 13:49:31 +0200542
543 *olen = 0;
544
Jerome Forissier79013242021-07-28 10:24:04 +0200545 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
546 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200547
Jerome Forissier79013242021-07-28 10:24:04 +0200548 MBEDTLS_SSL_DEBUG_MSG( 3,
549 ( "client hello, adding max_fragment_length extension" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200550
Jerome Forissier79013242021-07-28 10:24:04 +0200551 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200552
Jerome Forissier039e02d2022-08-09 17:10:15 +0200553 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 );
554 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +0200555
556 *p++ = 0x00;
557 *p++ = 1;
558
559 *p++ = ssl->conf->mfl_code;
560
561 *olen = 5;
Jerome Forissier79013242021-07-28 10:24:04 +0200562
563 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200564}
565#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
566
567#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200568MBEDTLS_CHECK_RETURN_CRITICAL
Jerome Forissier79013242021-07-28 10:24:04 +0200569static int ssl_write_truncated_hmac_ext( mbedtls_ssl_context *ssl,
570 unsigned char *buf,
571 const unsigned char *end,
572 size_t *olen )
Jens Wiklander817466c2018-05-22 13:49:31 +0200573{
574 unsigned char *p = buf;
Jens Wiklander817466c2018-05-22 13:49:31 +0200575
576 *olen = 0;
577
578 if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED )
Jerome Forissier79013242021-07-28 10:24:04 +0200579 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200580
Jerome Forissier79013242021-07-28 10:24:04 +0200581 MBEDTLS_SSL_DEBUG_MSG( 3,
582 ( "client hello, adding truncated_hmac extension" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200583
Jerome Forissier79013242021-07-28 10:24:04 +0200584 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200585
Jerome Forissier039e02d2022-08-09 17:10:15 +0200586 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_TRUNCATED_HMAC, p, 0 );
587 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +0200588
589 *p++ = 0x00;
590 *p++ = 0x00;
591
592 *olen = 4;
Jerome Forissier79013242021-07-28 10:24:04 +0200593
594 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200595}
596#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
597
598#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200599MBEDTLS_CHECK_RETURN_CRITICAL
Jerome Forissier79013242021-07-28 10:24:04 +0200600static int ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
601 unsigned char *buf,
602 const unsigned char *end,
603 size_t *olen )
Jens Wiklander817466c2018-05-22 13:49:31 +0200604{
605 unsigned char *p = buf;
Jens Wiklander817466c2018-05-22 13:49:31 +0200606
607 *olen = 0;
608
609 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
610 ssl->conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Jerome Forissier79013242021-07-28 10:24:04 +0200611 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200612
Jerome Forissier79013242021-07-28 10:24:04 +0200613 MBEDTLS_SSL_DEBUG_MSG( 3,
614 ( "client hello, adding encrypt_then_mac extension" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200615
Jerome Forissier79013242021-07-28 10:24:04 +0200616 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200617
Jerome Forissier039e02d2022-08-09 17:10:15 +0200618 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 );
619 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +0200620
621 *p++ = 0x00;
622 *p++ = 0x00;
623
624 *olen = 4;
Jerome Forissier79013242021-07-28 10:24:04 +0200625
626 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200627}
628#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
629
630#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200631MBEDTLS_CHECK_RETURN_CRITICAL
Jerome Forissier79013242021-07-28 10:24:04 +0200632static int ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
633 unsigned char *buf,
634 const unsigned char *end,
635 size_t *olen )
Jens Wiklander817466c2018-05-22 13:49:31 +0200636{
637 unsigned char *p = buf;
Jens Wiklander817466c2018-05-22 13:49:31 +0200638
639 *olen = 0;
640
641 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
642 ssl->conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Jerome Forissier79013242021-07-28 10:24:04 +0200643 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200644
Jerome Forissier79013242021-07-28 10:24:04 +0200645 MBEDTLS_SSL_DEBUG_MSG( 3,
646 ( "client hello, adding extended_master_secret extension" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200647
Jerome Forissier79013242021-07-28 10:24:04 +0200648 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200649
Jerome Forissier039e02d2022-08-09 17:10:15 +0200650 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 );
651 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +0200652
653 *p++ = 0x00;
654 *p++ = 0x00;
655
656 *olen = 4;
Jerome Forissier79013242021-07-28 10:24:04 +0200657
658 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200659}
660#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
661
662#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200663MBEDTLS_CHECK_RETURN_CRITICAL
Jerome Forissier79013242021-07-28 10:24:04 +0200664static int ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
665 unsigned char *buf,
666 const unsigned char *end,
667 size_t *olen )
Jens Wiklander817466c2018-05-22 13:49:31 +0200668{
669 unsigned char *p = buf;
Jens Wiklander817466c2018-05-22 13:49:31 +0200670 size_t tlen = ssl->session_negotiate->ticket_len;
671
672 *olen = 0;
673
674 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED )
Jerome Forissier79013242021-07-28 10:24:04 +0200675 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200676
Jerome Forissier79013242021-07-28 10:24:04 +0200677 MBEDTLS_SSL_DEBUG_MSG( 3,
678 ( "client hello, adding session ticket extension" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200679
Jerome Forissier79013242021-07-28 10:24:04 +0200680 /* The addition is safe here since the ticket length is 16 bit. */
681 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 + tlen );
Jens Wiklander817466c2018-05-22 13:49:31 +0200682
Jerome Forissier039e02d2022-08-09 17:10:15 +0200683 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 );
684 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +0200685
Jerome Forissier039e02d2022-08-09 17:10:15 +0200686 MBEDTLS_PUT_UINT16_BE( tlen, p, 0 );
687 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +0200688
689 *olen = 4;
690
691 if( ssl->session_negotiate->ticket == NULL || tlen == 0 )
Jerome Forissier79013242021-07-28 10:24:04 +0200692 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200693
Jerome Forissier79013242021-07-28 10:24:04 +0200694 MBEDTLS_SSL_DEBUG_MSG( 3,
695 ( "sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200696
697 memcpy( p, ssl->session_negotiate->ticket, tlen );
698
699 *olen += tlen;
Jerome Forissier79013242021-07-28 10:24:04 +0200700
701 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200702}
703#endif /* MBEDTLS_SSL_SESSION_TICKETS */
704
705#if defined(MBEDTLS_SSL_ALPN)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200706MBEDTLS_CHECK_RETURN_CRITICAL
Jerome Forissier79013242021-07-28 10:24:04 +0200707static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
708 unsigned char *buf,
709 const unsigned char *end,
710 size_t *olen )
Jens Wiklander817466c2018-05-22 13:49:31 +0200711{
712 unsigned char *p = buf;
Jens Wiklander817466c2018-05-22 13:49:31 +0200713 size_t alpnlen = 0;
714 const char **cur;
715
716 *olen = 0;
717
718 if( ssl->conf->alpn_list == NULL )
Jerome Forissier79013242021-07-28 10:24:04 +0200719 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200720
721 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
722
723 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
Jerome Forissier79013242021-07-28 10:24:04 +0200724 alpnlen += strlen( *cur ) + 1;
Jens Wiklander817466c2018-05-22 13:49:31 +0200725
Jerome Forissier79013242021-07-28 10:24:04 +0200726 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 + alpnlen );
Jens Wiklander817466c2018-05-22 13:49:31 +0200727
Jerome Forissier039e02d2022-08-09 17:10:15 +0200728 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
729 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +0200730
731 /*
732 * opaque ProtocolName<1..2^8-1>;
733 *
734 * struct {
735 * ProtocolName protocol_name_list<2..2^16-1>
736 * } ProtocolNameList;
737 */
738
739 /* Skip writing extension and list length for now */
740 p += 4;
741
742 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
743 {
Jerome Forissier79013242021-07-28 10:24:04 +0200744 /*
745 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
746 * protocol names is less than 255.
747 */
748 *p = (unsigned char)strlen( *cur );
Jens Wiklander817466c2018-05-22 13:49:31 +0200749 memcpy( p + 1, *cur, *p );
750 p += 1 + *p;
751 }
752
753 *olen = p - buf;
754
755 /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
Jerome Forissier039e02d2022-08-09 17:10:15 +0200756 MBEDTLS_PUT_UINT16_BE( *olen - 6, buf, 4 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200757
758 /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
Jerome Forissier039e02d2022-08-09 17:10:15 +0200759 MBEDTLS_PUT_UINT16_BE( *olen - 4, buf, 2 );
Jerome Forissier79013242021-07-28 10:24:04 +0200760
761 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200762}
763#endif /* MBEDTLS_SSL_ALPN */
764
Jerome Forissier79013242021-07-28 10:24:04 +0200765#if defined(MBEDTLS_SSL_DTLS_SRTP)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200766MBEDTLS_CHECK_RETURN_CRITICAL
Jerome Forissier79013242021-07-28 10:24:04 +0200767static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
768 unsigned char *buf,
769 const unsigned char *end,
770 size_t *olen )
771{
772 unsigned char *p = buf;
773 size_t protection_profiles_index = 0, ext_len = 0;
774 uint16_t mki_len = 0, profile_value = 0;
775
776 *olen = 0;
777
778 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
779 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
780 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
781 {
782 return( 0 );
783 }
784
785 /* RFC 5764 section 4.1.1
786 * uint8 SRTPProtectionProfile[2];
787 *
788 * struct {
789 * SRTPProtectionProfiles SRTPProtectionProfiles;
790 * opaque srtp_mki<0..255>;
791 * } UseSRTPData;
792 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
793 */
794 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
795 {
796 mki_len = ssl->dtls_srtp_info.mki_len;
797 }
798 /* Extension length = 2 bytes for profiles length,
799 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
800 * 1 byte for srtp_mki vector length and the mki_len value
801 */
802 ext_len = 2 + 2 * ( ssl->conf->dtls_srtp_profile_list_len ) + 1 + mki_len;
803
804 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding use_srtp extension" ) );
805
806 /* Check there is room in the buffer for the extension + 4 bytes
807 * - the extension tag (2 bytes)
808 * - the extension length (2 bytes)
809 */
810 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ext_len + 4 );
811
Jerome Forissier039e02d2022-08-09 17:10:15 +0200812 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_USE_SRTP, p, 0 );
813 p += 2;
Jerome Forissier79013242021-07-28 10:24:04 +0200814
Jerome Forissier039e02d2022-08-09 17:10:15 +0200815 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
816 p += 2;
Jerome Forissier79013242021-07-28 10:24:04 +0200817
818 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
819 /* micro-optimization:
820 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
821 * which is lower than 127, so the upper byte of the length is always 0
822 * For the documentation, the more generic code is left in comments
823 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
824 * >> 8 ) & 0xFF );
825 */
826 *p++ = 0;
Jerome Forissier039e02d2022-08-09 17:10:15 +0200827 *p++ = MBEDTLS_BYTE_0( 2 * ssl->conf->dtls_srtp_profile_list_len );
Jerome Forissier79013242021-07-28 10:24:04 +0200828
829 for( protection_profiles_index=0;
830 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
831 protection_profiles_index++ )
832 {
833 profile_value = mbedtls_ssl_check_srtp_profile_value
834 ( ssl->conf->dtls_srtp_profile_list[protection_profiles_index] );
835 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
836 {
837 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_write_use_srtp_ext, add profile: %04x",
838 profile_value ) );
Jerome Forissier039e02d2022-08-09 17:10:15 +0200839 MBEDTLS_PUT_UINT16_BE( profile_value, p, 0 );
840 p += 2;
Jerome Forissier79013242021-07-28 10:24:04 +0200841 }
842 else
843 {
844 /*
845 * Note: we shall never arrive here as protection profiles
846 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
847 */
848 MBEDTLS_SSL_DEBUG_MSG( 3,
849 ( "client hello, "
850 "illegal DTLS-SRTP protection profile %d",
851 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
852 ) );
853 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
854 }
855 }
856
857 *p++ = mki_len & 0xFF;
858
859 if( mki_len != 0 )
860 {
861 memcpy( p, ssl->dtls_srtp_info.mki_value, mki_len );
862 /*
863 * Increment p to point to the current position.
864 */
865 p += mki_len;
866 MBEDTLS_SSL_DEBUG_BUF( 3, "sending mki", ssl->dtls_srtp_info.mki_value,
867 ssl->dtls_srtp_info.mki_len );
868 }
869
870 /*
871 * total extension length: extension type (2 bytes)
872 * + extension length (2 bytes)
873 * + protection profile length (2 bytes)
874 * + 2 * number of protection profiles
875 * + srtp_mki vector length(1 byte)
876 * + mki value
877 */
878 *olen = p - buf;
879
880 return( 0 );
881}
882#endif /* MBEDTLS_SSL_DTLS_SRTP */
883
Jens Wiklander817466c2018-05-22 13:49:31 +0200884/*
885 * Generate random bytes for ClientHello
886 */
Jerome Forissier039e02d2022-08-09 17:10:15 +0200887MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +0200888static int ssl_generate_random( mbedtls_ssl_context *ssl )
889{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200890 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200891 unsigned char *p = ssl->handshake->randbytes;
892#if defined(MBEDTLS_HAVE_TIME)
893 mbedtls_time_t t;
894#endif
895
896 /*
897 * When responding to a verify request, MUST reuse random (RFC 6347 4.2.1)
898 */
899#if defined(MBEDTLS_SSL_PROTO_DTLS)
900 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
901 ssl->handshake->verify_cookie != NULL )
902 {
903 return( 0 );
904 }
905#endif
906
907#if defined(MBEDTLS_HAVE_TIME)
908 t = mbedtls_time( NULL );
Jerome Forissier039e02d2022-08-09 17:10:15 +0200909 MBEDTLS_PUT_UINT32_BE( t, p, 0 );
910 p += 4;
Jens Wiklander817466c2018-05-22 13:49:31 +0200911
Jerome Forissier79013242021-07-28 10:24:04 +0200912 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
913 (long long) t ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200914#else
915 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
916 return( ret );
917
918 p += 4;
919#endif /* MBEDTLS_HAVE_TIME */
920
921 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
922 return( ret );
923
924 return( 0 );
925}
926
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100927/**
928 * \brief Validate cipher suite against config in SSL context.
929 *
930 * \param suite_info cipher suite to validate
931 * \param ssl SSL context
932 * \param min_minor_ver Minimal minor version to accept a cipher suite
933 * \param max_minor_ver Maximal minor version to accept a cipher suite
934 *
935 * \return 0 if valid, else 1
936 */
Jerome Forissier039e02d2022-08-09 17:10:15 +0200937MBEDTLS_CHECK_RETURN_CRITICAL
Jerome Forissier79013242021-07-28 10:24:04 +0200938static int ssl_validate_ciphersuite(
939 const mbedtls_ssl_ciphersuite_t * suite_info,
940 const mbedtls_ssl_context * ssl,
941 int min_minor_ver, int max_minor_ver )
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100942{
943 (void) ssl;
944 if( suite_info == NULL )
945 return( 1 );
946
947 if( suite_info->min_minor_ver > max_minor_ver ||
948 suite_info->max_minor_ver < min_minor_ver )
949 return( 1 );
950
951#if defined(MBEDTLS_SSL_PROTO_DTLS)
952 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
953 ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
954 return( 1 );
955#endif
956
957#if defined(MBEDTLS_ARC4_C)
958 if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED &&
959 suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
960 return( 1 );
961#endif
962
963#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
964 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
965 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
966 return( 1 );
967#endif
968
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200969 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
970#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
971 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
972 ssl_conf_has_static_psk( ssl->conf ) == 0 )
973 {
974 return( 1 );
975 }
976#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
977
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100978 return( 0 );
979}
980
Jerome Forissier039e02d2022-08-09 17:10:15 +0200981MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +0200982static int ssl_write_client_hello( mbedtls_ssl_context *ssl )
983{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200984 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200985 size_t i, n, olen, ext_len = 0;
Jerome Forissier79013242021-07-28 10:24:04 +0200986
Jens Wiklander817466c2018-05-22 13:49:31 +0200987 unsigned char *buf;
988 unsigned char *p, *q;
Jerome Forissier79013242021-07-28 10:24:04 +0200989 const unsigned char *end;
990
Jens Wiklander817466c2018-05-22 13:49:31 +0200991 unsigned char offer_compress;
992 const int *ciphersuites;
993 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100994#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
995 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
996 int uses_ec = 0;
997#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200998
999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
1000
1001 if( ssl->conf->f_rng == NULL )
1002 {
1003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
1004 return( MBEDTLS_ERR_SSL_NO_RNG );
1005 }
1006
1007#if defined(MBEDTLS_SSL_RENEGOTIATION)
1008 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
1009#endif
1010 {
1011 ssl->major_ver = ssl->conf->min_major_ver;
1012 ssl->minor_ver = ssl->conf->min_minor_ver;
1013 }
1014
1015 if( ssl->conf->max_major_ver == 0 )
1016 {
Jerome Forissier79013242021-07-28 10:24:04 +02001017 MBEDTLS_SSL_DEBUG_MSG( 1,
1018 ( "configured max major version is invalid, consider using mbedtls_ssl_config_defaults()" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02001019 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1020 }
1021
Jerome Forissier79013242021-07-28 10:24:04 +02001022 buf = ssl->out_msg;
1023 end = buf + MBEDTLS_SSL_OUT_CONTENT_LEN;
1024
Jens Wiklander817466c2018-05-22 13:49:31 +02001025 /*
Jerome Forissier79013242021-07-28 10:24:04 +02001026 * Check if there's enough space for the first part of the ClientHello
1027 * consisting of the 38 bytes described below, the session identifier (at
1028 * most 32 bytes) and its length (1 byte).
1029 *
1030 * Use static upper bounds instead of the actual values
1031 * to allow the compiler to optimize this away.
1032 */
1033 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 38 + 1 + 32 );
1034
1035 /*
1036 * The 38 first bytes of the ClientHello:
1037 * 0 . 0 handshake type (written later)
1038 * 1 . 3 handshake length (written later)
Jens Wiklander817466c2018-05-22 13:49:31 +02001039 * 4 . 5 highest version supported
1040 * 6 . 9 current UNIX time
1041 * 10 . 37 random bytes
Jerome Forissier79013242021-07-28 10:24:04 +02001042 *
1043 * The current UNIX time (4 bytes) and following 28 random bytes are written
1044 * by ssl_generate_random() into ssl->handshake->randbytes buffer and then
1045 * copied from there into the output buffer.
Jens Wiklander817466c2018-05-22 13:49:31 +02001046 */
Jens Wiklander817466c2018-05-22 13:49:31 +02001047
Jerome Forissier79013242021-07-28 10:24:04 +02001048 p = buf + 4;
1049 mbedtls_ssl_write_version( ssl->conf->max_major_ver,
1050 ssl->conf->max_minor_ver,
1051 ssl->conf->transport, p );
Jens Wiklander817466c2018-05-22 13:49:31 +02001052 p += 2;
1053
1054 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
1055 buf[4], buf[5] ) );
1056
1057 if( ( ret = ssl_generate_random( ssl ) ) != 0 )
1058 {
1059 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
1060 return( ret );
1061 }
1062
1063 memcpy( p, ssl->handshake->randbytes, 32 );
1064 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", p, 32 );
1065 p += 32;
1066
1067 /*
1068 * 38 . 38 session id length
1069 * 39 . 39+n session id
1070 * 39+n . 39+n DTLS only: cookie length (1 byte)
Jerome Forissier79013242021-07-28 10:24:04 +02001071 * 40+n . .. DTLS only: cookie
Jens Wiklander817466c2018-05-22 13:49:31 +02001072 * .. . .. ciphersuitelist length (2 bytes)
1073 * .. . .. ciphersuitelist
1074 * .. . .. compression methods length (1 byte)
1075 * .. . .. compression methods
1076 * .. . .. extensions length (2 bytes)
1077 * .. . .. extensions
1078 */
1079 n = ssl->session_negotiate->id_len;
1080
1081 if( n < 16 || n > 32 ||
1082#if defined(MBEDTLS_SSL_RENEGOTIATION)
1083 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
1084#endif
1085 ssl->handshake->resume == 0 )
1086 {
1087 n = 0;
1088 }
1089
1090#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1091 /*
1092 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
1093 * generate and include a Session ID in the TLS ClientHello."
1094 */
1095#if defined(MBEDTLS_SSL_RENEGOTIATION)
1096 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
1097#endif
1098 {
1099 if( ssl->session_negotiate->ticket != NULL &&
1100 ssl->session_negotiate->ticket_len != 0 )
1101 {
Jerome Forissier79013242021-07-28 10:24:04 +02001102 ret = ssl->conf->f_rng( ssl->conf->p_rng,
1103 ssl->session_negotiate->id, 32 );
Jens Wiklander817466c2018-05-22 13:49:31 +02001104
1105 if( ret != 0 )
1106 return( ret );
1107
1108 ssl->session_negotiate->id_len = n = 32;
1109 }
1110 }
1111#endif /* MBEDTLS_SSL_SESSION_TICKETS */
1112
Jerome Forissier79013242021-07-28 10:24:04 +02001113 /*
1114 * The first check of the output buffer size above (
1115 * MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 38 + 1 + 32 );)
1116 * has checked that there is enough space in the output buffer for the
1117 * session identifier length byte and the session identifier (n <= 32).
1118 */
Jens Wiklander817466c2018-05-22 13:49:31 +02001119 *p++ = (unsigned char) n;
1120
1121 for( i = 0; i < n; i++ )
1122 *p++ = ssl->session_negotiate->id[i];
1123
Jerome Forissier79013242021-07-28 10:24:04 +02001124 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02001125 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
1126
1127 /*
Jerome Forissier79013242021-07-28 10:24:04 +02001128 * With 'n' being the length of the session identifier
1129 *
1130 * 39+n . 39+n DTLS only: cookie length (1 byte)
1131 * 40+n . .. DTLS only: cookie
1132 * .. . .. ciphersuitelist length (2 bytes)
1133 * .. . .. ciphersuitelist
1134 * .. . .. compression methods length (1 byte)
1135 * .. . .. compression methods
1136 * .. . .. extensions length (2 bytes)
1137 * .. . .. extensions
1138 */
1139
1140 /*
Jens Wiklander817466c2018-05-22 13:49:31 +02001141 * DTLS cookie
1142 */
1143#if defined(MBEDTLS_SSL_PROTO_DTLS)
1144 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1145 {
Jerome Forissier79013242021-07-28 10:24:04 +02001146 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
1147
Jens Wiklander817466c2018-05-22 13:49:31 +02001148 if( ssl->handshake->verify_cookie == NULL )
1149 {
1150 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no verify cookie to send" ) );
1151 *p++ = 0;
1152 }
1153 else
1154 {
1155 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
1156 ssl->handshake->verify_cookie,
1157 ssl->handshake->verify_cookie_len );
1158
1159 *p++ = ssl->handshake->verify_cookie_len;
Jerome Forissier79013242021-07-28 10:24:04 +02001160
1161 MBEDTLS_SSL_CHK_BUF_PTR( p, end,
1162 ssl->handshake->verify_cookie_len );
Jens Wiklander817466c2018-05-22 13:49:31 +02001163 memcpy( p, ssl->handshake->verify_cookie,
1164 ssl->handshake->verify_cookie_len );
1165 p += ssl->handshake->verify_cookie_len;
1166 }
1167 }
1168#endif
1169
1170 /*
1171 * Ciphersuite list
1172 */
1173 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
1174
1175 /* Skip writing ciphersuite length for now */
1176 n = 0;
1177 q = p;
Jerome Forissier79013242021-07-28 10:24:04 +02001178
1179 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jens Wiklander817466c2018-05-22 13:49:31 +02001180 p += 2;
1181
1182 for( i = 0; ciphersuites[i] != 0; i++ )
1183 {
1184 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] );
1185
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001186 if( ssl_validate_ciphersuite( ciphersuite_info, ssl,
1187 ssl->conf->min_minor_ver,
1188 ssl->conf->max_minor_ver ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02001189 continue;
1190
Jerome Forissier79013242021-07-28 10:24:04 +02001191 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %#04x (%s)",
1192 (unsigned int)ciphersuites[i], ciphersuite_info->name ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02001193
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001194#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
1195 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1196 uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info );
1197#endif
1198
Jerome Forissier79013242021-07-28 10:24:04 +02001199 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1200
Jens Wiklander817466c2018-05-22 13:49:31 +02001201 n++;
Jerome Forissier039e02d2022-08-09 17:10:15 +02001202 MBEDTLS_PUT_UINT16_BE( ciphersuites[i], p, 0 );
1203 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +02001204 }
1205
Jerome Forissier79013242021-07-28 10:24:04 +02001206 MBEDTLS_SSL_DEBUG_MSG( 3,
1207 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites (excluding SCSVs)", n ) );
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001208
Jens Wiklander817466c2018-05-22 13:49:31 +02001209 /*
1210 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1211 */
1212#if defined(MBEDTLS_SSL_RENEGOTIATION)
1213 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
1214#endif
1215 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001216 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02001217 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerome Forissier039e02d2022-08-09 17:10:15 +02001218 MBEDTLS_PUT_UINT16_BE( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0 );
1219 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +02001220 n++;
1221 }
1222
1223 /* Some versions of OpenSSL don't handle it correctly if not at end */
1224#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
1225 if( ssl->conf->fallback == MBEDTLS_SSL_IS_FALLBACK )
1226 {
1227 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02001228
1229 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jerome Forissier039e02d2022-08-09 17:10:15 +02001230 MBEDTLS_PUT_UINT16_BE( MBEDTLS_SSL_FALLBACK_SCSV_VALUE, p, 0 );
1231 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +02001232 n++;
1233 }
1234#endif
1235
1236 *q++ = (unsigned char)( n >> 7 );
1237 *q++ = (unsigned char)( n << 1 );
1238
Jens Wiklander817466c2018-05-22 13:49:31 +02001239#if defined(MBEDTLS_ZLIB_SUPPORT)
1240 offer_compress = 1;
1241#else
1242 offer_compress = 0;
1243#endif
1244
1245 /*
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001246 * We don't support compression with DTLS right now: if many records come
Jens Wiklander817466c2018-05-22 13:49:31 +02001247 * in the same datagram, uncompressing one could overwrite the next one.
1248 * We don't want to add complexity for handling that case unless there is
1249 * an actual need for it.
1250 */
1251#if defined(MBEDTLS_SSL_PROTO_DTLS)
1252 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1253 offer_compress = 0;
1254#endif
1255
1256 if( offer_compress )
1257 {
1258 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
1259 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
Jerome Forissier79013242021-07-28 10:24:04 +02001260 MBEDTLS_SSL_COMPRESS_DEFLATE,
1261 MBEDTLS_SSL_COMPRESS_NULL ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02001262
Jerome Forissier79013242021-07-28 10:24:04 +02001263 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
Jens Wiklander817466c2018-05-22 13:49:31 +02001264 *p++ = 2;
1265 *p++ = MBEDTLS_SSL_COMPRESS_DEFLATE;
1266 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
1267 }
1268 else
1269 {
1270 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
1271 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d",
1272 MBEDTLS_SSL_COMPRESS_NULL ) );
1273
Jerome Forissier79013242021-07-28 10:24:04 +02001274 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jens Wiklander817466c2018-05-22 13:49:31 +02001275 *p++ = 1;
1276 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
1277 }
1278
Jerome Forissier79013242021-07-28 10:24:04 +02001279 /* First write extensions, then the total length */
1280
1281 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1282
Jens Wiklander817466c2018-05-22 13:49:31 +02001283#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Jerome Forissier79013242021-07-28 10:24:04 +02001284 if( ( ret = ssl_write_hostname_ext( ssl, p + 2 + ext_len,
1285 end, &olen ) ) != 0 )
1286 {
1287 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_hostname_ext", ret );
1288 return( ret );
1289 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001290 ext_len += olen;
1291#endif
1292
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001293 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
1294 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
Jens Wiklander817466c2018-05-22 13:49:31 +02001295#if defined(MBEDTLS_SSL_RENEGOTIATION)
Jerome Forissier79013242021-07-28 10:24:04 +02001296 if( ( ret = ssl_write_renegotiation_ext( ssl, p + 2 + ext_len,
1297 end, &olen ) ) != 0 )
1298 {
1299 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_renegotiation_ext", ret );
1300 return( ret );
1301 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001302 ext_len += olen;
1303#endif
1304
1305#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001306 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerome Forissier79013242021-07-28 10:24:04 +02001307 if( ( ret = ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len,
1308 end, &olen ) ) != 0 )
1309 {
1310 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_signature_algorithms_ext", ret );
1311 return( ret );
1312 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001313 ext_len += olen;
1314#endif
1315
1316#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
1317 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001318 if( uses_ec )
1319 {
Jerome Forissier79013242021-07-28 10:24:04 +02001320 if( ( ret = ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len,
1321 end, &olen ) ) != 0 )
1322 {
1323 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_elliptic_curves_ext", ret );
1324 return( ret );
1325 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001326 ext_len += olen;
Jens Wiklander817466c2018-05-22 13:49:31 +02001327
Jerome Forissier79013242021-07-28 10:24:04 +02001328 if( ( ret = ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len,
1329 end, &olen ) ) != 0 )
1330 {
1331 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_point_formats_ext", ret );
1332 return( ret );
1333 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001334 ext_len += olen;
1335 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001336#endif
1337
1338#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jerome Forissier79013242021-07-28 10:24:04 +02001339 if( ( ret = ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len,
1340 end, &olen ) ) != 0 )
1341 {
1342 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_ecjpake_kkpp_ext", ret );
1343 return( ret );
1344 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001345 ext_len += olen;
1346#endif
1347
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001348#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Jerome Forissier79013242021-07-28 10:24:04 +02001349 if( ( ret = ssl_write_cid_ext( ssl, p + 2 + ext_len, end, &olen ) ) != 0 )
1350 {
1351 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_cid_ext", ret );
1352 return( ret );
1353 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001354 ext_len += olen;
1355#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1356
Jens Wiklander817466c2018-05-22 13:49:31 +02001357#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Jerome Forissier79013242021-07-28 10:24:04 +02001358 if( ( ret = ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len,
1359 end, &olen ) ) != 0 )
1360 {
1361 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret );
1362 return( ret );
1363 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001364 ext_len += olen;
1365#endif
1366
1367#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Jerome Forissier79013242021-07-28 10:24:04 +02001368 if( ( ret = ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len,
1369 end, &olen ) ) != 0 )
1370 {
1371 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_truncated_hmac_ext", ret );
1372 return( ret );
1373 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001374 ext_len += olen;
1375#endif
1376
1377#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Jerome Forissier79013242021-07-28 10:24:04 +02001378 if( ( ret = ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len,
1379 end, &olen ) ) != 0 )
1380 {
1381 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_encrypt_then_mac_ext", ret );
1382 return( ret );
1383 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001384 ext_len += olen;
1385#endif
1386
1387#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Jerome Forissier79013242021-07-28 10:24:04 +02001388 if( ( ret = ssl_write_extended_ms_ext( ssl, p + 2 + ext_len,
1389 end, &olen ) ) != 0 )
1390 {
1391 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_extended_ms_ext", ret );
1392 return( ret );
1393 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001394 ext_len += olen;
1395#endif
1396
1397#if defined(MBEDTLS_SSL_ALPN)
Jerome Forissier79013242021-07-28 10:24:04 +02001398 if( ( ret = ssl_write_alpn_ext( ssl, p + 2 + ext_len,
1399 end, &olen ) ) != 0 )
1400 {
1401 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_alpn_ext", ret );
1402 return( ret );
1403 }
1404 ext_len += olen;
1405#endif
1406
1407#if defined(MBEDTLS_SSL_DTLS_SRTP)
1408 if( ( ret = ssl_write_use_srtp_ext( ssl, p + 2 + ext_len,
1409 end, &olen ) ) != 0 )
1410 {
1411 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_use_srtp_ext", ret );
1412 return( ret );
1413 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001414 ext_len += olen;
1415#endif
1416
1417#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerome Forissier79013242021-07-28 10:24:04 +02001418 if( ( ret = ssl_write_session_ticket_ext( ssl, p + 2 + ext_len,
1419 end, &olen ) ) != 0 )
1420 {
1421 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_session_ticket_ext", ret );
1422 return( ret );
1423 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001424 ext_len += olen;
1425#endif
1426
1427 /* olen unused if all extensions are disabled */
1428 ((void) olen);
1429
Jerome Forissier79013242021-07-28 10:24:04 +02001430 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
1431 ext_len ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02001432
1433 if( ext_len > 0 )
1434 {
Jerome Forissier79013242021-07-28 10:24:04 +02001435 /* No need to check for space here, because the extension
1436 * writing functions already took care of that. */
Jerome Forissier039e02d2022-08-09 17:10:15 +02001437 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
1438 p += 2 + ext_len;
Jens Wiklander817466c2018-05-22 13:49:31 +02001439 }
1440
1441 ssl->out_msglen = p - buf;
1442 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
1443 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_HELLO;
1444
1445 ssl->state++;
1446
1447#if defined(MBEDTLS_SSL_PROTO_DTLS)
1448 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1449 mbedtls_ssl_send_flight_completed( ssl );
1450#endif
1451
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001452 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02001453 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001454 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02001455 return( ret );
1456 }
1457
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001458#if defined(MBEDTLS_SSL_PROTO_DTLS)
1459 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1460 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
1461 {
1462 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
1463 return( ret );
1464 }
1465#endif /* MBEDTLS_SSL_PROTO_DTLS */
1466
Jens Wiklander817466c2018-05-22 13:49:31 +02001467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
1468
1469 return( 0 );
1470}
1471
Jerome Forissier039e02d2022-08-09 17:10:15 +02001472MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02001473static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
1474 const unsigned char *buf,
1475 size_t len )
1476{
1477#if defined(MBEDTLS_SSL_RENEGOTIATION)
1478 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
1479 {
1480 /* Check verify-data in constant-time. The length OTOH is no secret */
1481 if( len != 1 + ssl->verify_data_len * 2 ||
1482 buf[0] != ssl->verify_data_len * 2 ||
Jerome Forissier039e02d2022-08-09 17:10:15 +02001483 mbedtls_ct_memcmp( buf + 1,
Jens Wiklander817466c2018-05-22 13:49:31 +02001484 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
Jerome Forissier039e02d2022-08-09 17:10:15 +02001485 mbedtls_ct_memcmp( buf + 1 + ssl->verify_data_len,
Jens Wiklander817466c2018-05-22 13:49:31 +02001486 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
1487 {
1488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02001489 mbedtls_ssl_send_alert_message(
1490 ssl,
1491 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1492 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Jens Wiklander817466c2018-05-22 13:49:31 +02001493 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1494 }
1495 }
1496 else
1497#endif /* MBEDTLS_SSL_RENEGOTIATION */
1498 {
1499 if( len != 1 || buf[0] != 0x00 )
1500 {
Jerome Forissier79013242021-07-28 10:24:04 +02001501 MBEDTLS_SSL_DEBUG_MSG( 1,
1502 ( "non-zero length renegotiation info" ) );
1503 mbedtls_ssl_send_alert_message(
1504 ssl,
1505 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1506 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Jens Wiklander817466c2018-05-22 13:49:31 +02001507 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1508 }
1509
1510 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
1511 }
1512
1513 return( 0 );
1514}
1515
1516#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Jerome Forissier039e02d2022-08-09 17:10:15 +02001517MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02001518static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
1519 const unsigned char *buf,
1520 size_t len )
1521{
1522 /*
1523 * server should use the extension only if we did,
1524 * and if so the server's value should match ours (and len is always 1)
1525 */
1526 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
1527 len != 1 ||
1528 buf[0] != ssl->conf->mfl_code )
1529 {
Jerome Forissier79013242021-07-28 10:24:04 +02001530 MBEDTLS_SSL_DEBUG_MSG( 1,
1531 ( "non-matching max fragment length extension" ) );
1532 mbedtls_ssl_send_alert_message(
1533 ssl,
1534 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1535 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Jens Wiklander817466c2018-05-22 13:49:31 +02001536 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1537 }
1538
1539 return( 0 );
1540}
1541#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
1542
1543#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Jerome Forissier039e02d2022-08-09 17:10:15 +02001544MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02001545static int ssl_parse_truncated_hmac_ext( mbedtls_ssl_context *ssl,
1546 const unsigned char *buf,
1547 size_t len )
1548{
1549 if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED ||
1550 len != 0 )
1551 {
Jerome Forissier79013242021-07-28 10:24:04 +02001552 MBEDTLS_SSL_DEBUG_MSG( 1,
1553 ( "non-matching truncated HMAC extension" ) );
1554 mbedtls_ssl_send_alert_message(
1555 ssl,
1556 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1557 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Jens Wiklander817466c2018-05-22 13:49:31 +02001558 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1559 }
1560
1561 ((void) buf);
1562
1563 ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED;
1564
1565 return( 0 );
1566}
1567#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1568
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001569#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Jerome Forissier039e02d2022-08-09 17:10:15 +02001570MBEDTLS_CHECK_RETURN_CRITICAL
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001571static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
1572 const unsigned char *buf,
1573 size_t len )
1574{
1575 size_t peer_cid_len;
1576
1577 if( /* CID extension only makes sense in DTLS */
1578 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
1579 /* The server must only send the CID extension if we have offered it. */
1580 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
1581 {
1582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension unexpected" ) );
1583 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Jerome Forissier79013242021-07-28 10:24:04 +02001584 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001585 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1586 }
1587
1588 if( len == 0 )
1589 {
1590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
1591 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1592 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1593 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1594 }
1595
1596 peer_cid_len = *buf++;
1597 len--;
1598
1599 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
1600 {
1601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
1602 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1603 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1604 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1605 }
1606
1607 if( len != peer_cid_len )
1608 {
1609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
1610 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1611 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
1612 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1613 }
1614
1615 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
1616 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
1617 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
1618
1619 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
1620 MBEDTLS_SSL_DEBUG_BUF( 3, "Server CID", buf, peer_cid_len );
1621
1622 return( 0 );
1623}
1624#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1625
Jens Wiklander817466c2018-05-22 13:49:31 +02001626#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Jerome Forissier039e02d2022-08-09 17:10:15 +02001627MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02001628static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
1629 const unsigned char *buf,
1630 size_t len )
1631{
1632 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
1633 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1634 len != 0 )
1635 {
Jerome Forissier79013242021-07-28 10:24:04 +02001636 MBEDTLS_SSL_DEBUG_MSG( 1,
1637 ( "non-matching encrypt-then-MAC extension" ) );
1638 mbedtls_ssl_send_alert_message(
1639 ssl,
1640 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1641 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Jens Wiklander817466c2018-05-22 13:49:31 +02001642 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1643 }
1644
1645 ((void) buf);
1646
1647 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
1648
1649 return( 0 );
1650}
1651#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
1652
1653#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Jerome Forissier039e02d2022-08-09 17:10:15 +02001654MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02001655static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
1656 const unsigned char *buf,
1657 size_t len )
1658{
1659 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
1660 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1661 len != 0 )
1662 {
Jerome Forissier79013242021-07-28 10:24:04 +02001663 MBEDTLS_SSL_DEBUG_MSG( 1,
1664 ( "non-matching extended master secret extension" ) );
1665 mbedtls_ssl_send_alert_message(
1666 ssl,
1667 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1668 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Jens Wiklander817466c2018-05-22 13:49:31 +02001669 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1670 }
1671
1672 ((void) buf);
1673
1674 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
1675
1676 return( 0 );
1677}
1678#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1679
1680#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerome Forissier039e02d2022-08-09 17:10:15 +02001681MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02001682static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
1683 const unsigned char *buf,
1684 size_t len )
1685{
1686 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
1687 len != 0 )
1688 {
Jerome Forissier79013242021-07-28 10:24:04 +02001689 MBEDTLS_SSL_DEBUG_MSG( 1,
1690 ( "non-matching session ticket extension" ) );
1691 mbedtls_ssl_send_alert_message(
1692 ssl,
1693 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1694 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Jens Wiklander817466c2018-05-22 13:49:31 +02001695 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1696 }
1697
1698 ((void) buf);
1699
1700 ssl->handshake->new_session_ticket = 1;
1701
1702 return( 0 );
1703}
1704#endif /* MBEDTLS_SSL_SESSION_TICKETS */
1705
1706#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
1707 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jerome Forissier039e02d2022-08-09 17:10:15 +02001708MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02001709static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl,
1710 const unsigned char *buf,
1711 size_t len )
1712{
1713 size_t list_size;
1714 const unsigned char *p;
1715
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001716 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Jens Wiklander817466c2018-05-22 13:49:31 +02001717 {
1718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1719 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1720 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1721 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1722 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001723 list_size = buf[0];
Jens Wiklander817466c2018-05-22 13:49:31 +02001724
1725 p = buf + 1;
1726 while( list_size > 0 )
1727 {
1728 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
1729 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
1730 {
1731#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
1732 ssl->handshake->ecdh_ctx.point_format = p[0];
1733#endif
1734#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1735 ssl->handshake->ecjpake_ctx.point_format = p[0];
1736#endif
1737 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
1738 return( 0 );
1739 }
1740
1741 list_size--;
1742 p++;
1743 }
1744
1745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
1746 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1747 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1748 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1749}
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001750#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Jens Wiklander817466c2018-05-22 13:49:31 +02001751 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1752
1753#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jerome Forissier039e02d2022-08-09 17:10:15 +02001754MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02001755static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
1756 const unsigned char *buf,
1757 size_t len )
1758{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001759 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001760
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001761 if( ssl->handshake->ciphersuite_info->key_exchange !=
Jens Wiklander817466c2018-05-22 13:49:31 +02001762 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
1763 {
1764 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
1765 return( 0 );
1766 }
1767
1768 /* If we got here, we no longer need our cached extension */
1769 mbedtls_free( ssl->handshake->ecjpake_cache );
1770 ssl->handshake->ecjpake_cache = NULL;
1771 ssl->handshake->ecjpake_cache_len = 0;
1772
1773 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
1774 buf, len ) ) != 0 )
1775 {
1776 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Jerome Forissier79013242021-07-28 10:24:04 +02001777 mbedtls_ssl_send_alert_message(
1778 ssl,
1779 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1780 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Jens Wiklander817466c2018-05-22 13:49:31 +02001781 return( ret );
1782 }
1783
1784 return( 0 );
1785}
1786#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1787
1788#if defined(MBEDTLS_SSL_ALPN)
Jerome Forissier039e02d2022-08-09 17:10:15 +02001789MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02001790static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
1791 const unsigned char *buf, size_t len )
1792{
1793 size_t list_len, name_len;
1794 const char **p;
1795
1796 /* If we didn't send it, the server shouldn't send it */
1797 if( ssl->conf->alpn_list == NULL )
1798 {
1799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02001800 mbedtls_ssl_send_alert_message(
1801 ssl,
1802 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1803 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Jens Wiklander817466c2018-05-22 13:49:31 +02001804 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1805 }
1806
1807 /*
1808 * opaque ProtocolName<1..2^8-1>;
1809 *
1810 * struct {
1811 * ProtocolName protocol_name_list<2..2^16-1>
1812 * } ProtocolNameList;
1813 *
1814 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
1815 */
1816
1817 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
1818 if( len < 4 )
1819 {
1820 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1821 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1822 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1823 }
1824
1825 list_len = ( buf[0] << 8 ) | buf[1];
1826 if( list_len != len - 2 )
1827 {
1828 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1829 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1830 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1831 }
1832
1833 name_len = buf[2];
1834 if( name_len != list_len - 1 )
1835 {
1836 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1837 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1838 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1839 }
1840
1841 /* Check that the server chosen protocol was in our list and save it */
1842 for( p = ssl->conf->alpn_list; *p != NULL; p++ )
1843 {
1844 if( name_len == strlen( *p ) &&
1845 memcmp( buf + 3, *p, name_len ) == 0 )
1846 {
1847 ssl->alpn_chosen = *p;
1848 return( 0 );
1849 }
1850 }
1851
1852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) );
1853 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1854 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1855 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1856}
1857#endif /* MBEDTLS_SSL_ALPN */
1858
Jerome Forissier79013242021-07-28 10:24:04 +02001859#if defined(MBEDTLS_SSL_DTLS_SRTP)
Jerome Forissier039e02d2022-08-09 17:10:15 +02001860MBEDTLS_CHECK_RETURN_CRITICAL
Jerome Forissier79013242021-07-28 10:24:04 +02001861static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
1862 const unsigned char *buf,
1863 size_t len )
1864{
1865 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
1866 size_t i, mki_len = 0;
1867 uint16_t server_protection_profile_value = 0;
1868
1869 /* If use_srtp is not configured, just ignore the extension */
1870 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
1871 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
1872 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
1873 return( 0 );
1874
1875 /* RFC 5764 section 4.1.1
1876 * uint8 SRTPProtectionProfile[2];
1877 *
1878 * struct {
1879 * SRTPProtectionProfiles SRTPProtectionProfiles;
1880 * opaque srtp_mki<0..255>;
1881 * } UseSRTPData;
1882
1883 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1884 *
1885 */
1886 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
1887 {
1888 mki_len = ssl->dtls_srtp_info.mki_len;
1889 }
1890
1891 /*
1892 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1893 * + protection profile (2 bytes)
1894 * + mki_len(1 byte)
1895 * and optional srtp_mki
1896 */
1897 if( ( len < 5 ) || ( len != ( buf[4] + 5u ) ) )
1898 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1899
1900 /*
1901 * get the server protection profile
1902 */
1903
1904 /*
1905 * protection profile length must be 0x0002 as we must have only
1906 * one protection profile in server Hello
1907 */
1908 if( ( buf[0] != 0 ) || ( buf[1] != 2 ) )
1909 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1910
1911 server_protection_profile_value = ( buf[2] << 8 ) | buf[3];
1912 server_protection = mbedtls_ssl_check_srtp_profile_value(
1913 server_protection_profile_value );
1914 if( server_protection != MBEDTLS_TLS_SRTP_UNSET )
1915 {
1916 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
1917 mbedtls_ssl_get_srtp_profile_as_string(
1918 server_protection ) ) );
1919 }
1920
1921 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
1922
1923 /*
1924 * Check we have the server profile in our list
1925 */
1926 for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
1927 {
1928 if( server_protection == ssl->conf->dtls_srtp_profile_list[i] )
1929 {
1930 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
1931 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
1932 mbedtls_ssl_get_srtp_profile_as_string(
1933 server_protection ) ) );
1934 break;
1935 }
1936 }
1937
1938 /* If no match was found : server problem, it shall never answer with incompatible profile */
1939 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
1940 {
1941 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1942 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1943 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1944 }
1945
1946 /* If server does not use mki in its reply, make sure the client won't keep
1947 * one as negotiated */
1948 if( len == 5 )
1949 {
1950 ssl->dtls_srtp_info.mki_len = 0;
1951 }
1952
1953 /*
1954 * RFC5764:
1955 * If the client detects a nonzero-length MKI in the server's response
1956 * that is different than the one the client offered, then the client
1957 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1958 */
1959 if( len > 5 && ( buf[4] != mki_len ||
1960 ( memcmp( ssl->dtls_srtp_info.mki_value, &buf[5], mki_len ) ) ) )
1961 {
1962 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1963 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
1964 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1965 }
1966#if defined (MBEDTLS_DEBUG_C)
1967 if( len > 5 )
1968 {
1969 MBEDTLS_SSL_DEBUG_BUF( 3, "received mki", ssl->dtls_srtp_info.mki_value,
1970 ssl->dtls_srtp_info.mki_len );
1971 }
1972#endif
1973 return( 0 );
1974}
1975#endif /* MBEDTLS_SSL_DTLS_SRTP */
1976
Jens Wiklander817466c2018-05-22 13:49:31 +02001977/*
1978 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1979 */
1980#if defined(MBEDTLS_SSL_PROTO_DTLS)
Jerome Forissier039e02d2022-08-09 17:10:15 +02001981MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02001982static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl )
1983{
1984 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
1985 int major_ver, minor_ver;
1986 unsigned char cookie_len;
1987
1988 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
1989
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001990 /* Check that there is enough room for:
1991 * - 2 bytes of version
1992 * - 1 byte of cookie_len
1993 */
1994 if( mbedtls_ssl_hs_hdr_len( ssl ) + 3 > ssl->in_msglen )
1995 {
1996 MBEDTLS_SSL_DEBUG_MSG( 1,
1997 ( "incoming HelloVerifyRequest message is too short" ) );
1998 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1999 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2000 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
2001 }
2002
Jens Wiklander817466c2018-05-22 13:49:31 +02002003 /*
2004 * struct {
2005 * ProtocolVersion server_version;
2006 * opaque cookie<0..2^8-1>;
2007 * } HelloVerifyRequest;
2008 */
2009 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
2010 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, p );
2011 p += 2;
2012
2013 /*
2014 * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1)
2015 * even is lower than our min version.
2016 */
2017 if( major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
2018 minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ||
2019 major_ver > ssl->conf->max_major_ver ||
2020 minor_ver > ssl->conf->max_minor_ver )
2021 {
2022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) );
2023
2024 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2025 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
2026
2027 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
2028 }
2029
2030 cookie_len = *p++;
Jens Wiklander817466c2018-05-22 13:49:31 +02002031 if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len )
2032 {
2033 MBEDTLS_SSL_DEBUG_MSG( 1,
2034 ( "cookie length does not match incoming message size" ) );
2035 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2036 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2037 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
2038 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002039 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len );
Jens Wiklander817466c2018-05-22 13:49:31 +02002040
2041 mbedtls_free( ssl->handshake->verify_cookie );
2042
2043 ssl->handshake->verify_cookie = mbedtls_calloc( 1, cookie_len );
2044 if( ssl->handshake->verify_cookie == NULL )
2045 {
2046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) );
2047 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2048 }
2049
2050 memcpy( ssl->handshake->verify_cookie, p, cookie_len );
2051 ssl->handshake->verify_cookie_len = cookie_len;
2052
2053 /* Start over at ClientHello */
2054 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
2055 mbedtls_ssl_reset_checksum( ssl );
2056
2057 mbedtls_ssl_recv_flight_completed( ssl );
2058
2059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
2060
2061 return( 0 );
2062}
2063#endif /* MBEDTLS_SSL_PROTO_DTLS */
2064
Jerome Forissier039e02d2022-08-09 17:10:15 +02002065MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02002066static int ssl_parse_server_hello( mbedtls_ssl_context *ssl )
2067{
2068 int ret, i;
2069 size_t n;
2070 size_t ext_len;
2071 unsigned char *buf, *ext;
2072 unsigned char comp;
2073#if defined(MBEDTLS_ZLIB_SUPPORT)
2074 int accept_comp;
2075#endif
2076#if defined(MBEDTLS_SSL_RENEGOTIATION)
2077 int renegotiation_info_seen = 0;
2078#endif
2079 int handshake_failure = 0;
2080 const mbedtls_ssl_ciphersuite_t *suite_info;
Jens Wiklander817466c2018-05-22 13:49:31 +02002081
2082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
2083
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002084 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02002085 {
2086 /* No alert on a read error. */
2087 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2088 return( ret );
2089 }
2090
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002091 buf = ssl->in_msg;
2092
Jens Wiklander817466c2018-05-22 13:49:31 +02002093 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2094 {
2095#if defined(MBEDTLS_SSL_RENEGOTIATION)
2096 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
2097 {
2098 ssl->renego_records_seen++;
2099
2100 if( ssl->conf->renego_max_records >= 0 &&
2101 ssl->renego_records_seen > ssl->conf->renego_max_records )
2102 {
Jerome Forissier79013242021-07-28 10:24:04 +02002103 MBEDTLS_SSL_DEBUG_MSG( 1,
2104 ( "renegotiation requested, but not honored by server" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002105 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2106 }
2107
Jerome Forissier79013242021-07-28 10:24:04 +02002108 MBEDTLS_SSL_DEBUG_MSG( 1,
2109 ( "non-handshake message during renegotiation" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002110
2111 ssl->keep_current_message = 1;
2112 return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
2113 }
2114#endif /* MBEDTLS_SSL_RENEGOTIATION */
2115
2116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02002117 mbedtls_ssl_send_alert_message(
2118 ssl,
2119 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2120 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Jens Wiklander817466c2018-05-22 13:49:31 +02002121 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2122 }
2123
2124#if defined(MBEDTLS_SSL_PROTO_DTLS)
2125 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2126 {
2127 if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
2128 {
2129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
2130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
2131 return( ssl_parse_hello_verify_request( ssl ) );
2132 }
2133 else
2134 {
2135 /* We made it through the verification process */
2136 mbedtls_free( ssl->handshake->verify_cookie );
2137 ssl->handshake->verify_cookie = NULL;
2138 ssl->handshake->verify_cookie_len = 0;
2139 }
2140 }
2141#endif /* MBEDTLS_SSL_PROTO_DTLS */
2142
2143 if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) ||
2144 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO )
2145 {
2146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
2147 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2148 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2149 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
2150 }
2151
2152 /*
2153 * 0 . 1 server_version
2154 * 2 . 33 random (maybe including 4 bytes of Unix time)
2155 * 34 . 34 session_id length = n
2156 * 35 . 34+n session_id
2157 * 35+n . 36+n cipher_suite
2158 * 37+n . 37+n compression_method
2159 *
2160 * 38+n . 39+n extensions length (optional)
2161 * 40+n . .. extensions
2162 */
2163 buf += mbedtls_ssl_hs_hdr_len( ssl );
2164
2165 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 );
2166 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
2167 ssl->conf->transport, buf + 0 );
2168
2169 if( ssl->major_ver < ssl->conf->min_major_ver ||
2170 ssl->minor_ver < ssl->conf->min_minor_ver ||
2171 ssl->major_ver > ssl->conf->max_major_ver ||
2172 ssl->minor_ver > ssl->conf->max_minor_ver )
2173 {
Jerome Forissier79013242021-07-28 10:24:04 +02002174 MBEDTLS_SSL_DEBUG_MSG( 1,
2175 ( "server version out of bounds - min: [%d:%d], server: [%d:%d], max: [%d:%d]",
2176 ssl->conf->min_major_ver,
2177 ssl->conf->min_minor_ver,
2178 ssl->major_ver, ssl->minor_ver,
2179 ssl->conf->max_major_ver,
2180 ssl->conf->max_minor_ver ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002181
2182 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2183 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
2184
2185 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
2186 }
2187
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002188 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu",
Jerome Forissier79013242021-07-28 10:24:04 +02002189 ( (unsigned long) buf[2] << 24 ) |
2190 ( (unsigned long) buf[3] << 16 ) |
2191 ( (unsigned long) buf[4] << 8 ) |
2192 ( (unsigned long) buf[5] ) ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002193
2194 memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
2195
2196 n = buf[34];
2197
2198 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
2199
2200 if( n > 32 )
2201 {
2202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
2203 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2204 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2205 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
2206 }
2207
2208 if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n )
2209 {
2210 ext_len = ( ( buf[38 + n] << 8 )
2211 | ( buf[39 + n] ) );
2212
2213 if( ( ext_len > 0 && ext_len < 4 ) ||
2214 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
2215 {
2216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02002217 mbedtls_ssl_send_alert_message(
2218 ssl,
2219 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2220 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Jens Wiklander817466c2018-05-22 13:49:31 +02002221 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
2222 }
2223 }
2224 else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n )
2225 {
2226 ext_len = 0;
2227 }
2228 else
2229 {
2230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
2231 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2232 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2233 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
2234 }
2235
2236 /* ciphersuite (used later) */
2237 i = ( buf[35 + n] << 8 ) | buf[36 + n];
2238
2239 /*
2240 * Read and check compression
2241 */
2242 comp = buf[37 + n];
2243
2244#if defined(MBEDTLS_ZLIB_SUPPORT)
2245 /* See comments in ssl_write_client_hello() */
2246#if defined(MBEDTLS_SSL_PROTO_DTLS)
2247 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2248 accept_comp = 0;
2249 else
2250#endif
2251 accept_comp = 1;
2252
2253 if( comp != MBEDTLS_SSL_COMPRESS_NULL &&
2254 ( comp != MBEDTLS_SSL_COMPRESS_DEFLATE || accept_comp == 0 ) )
2255#else /* MBEDTLS_ZLIB_SUPPORT */
2256 if( comp != MBEDTLS_SSL_COMPRESS_NULL )
2257#endif/* MBEDTLS_ZLIB_SUPPORT */
2258 {
Jerome Forissier79013242021-07-28 10:24:04 +02002259 MBEDTLS_SSL_DEBUG_MSG( 1,
2260 ( "server hello, bad compression: %d", comp ) );
2261 mbedtls_ssl_send_alert_message(
2262 ssl,
2263 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2264 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Jens Wiklander817466c2018-05-22 13:49:31 +02002265 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2266 }
2267
2268 /*
2269 * Initialize update checksum functions
2270 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002271 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i );
2272 if( ssl->handshake->ciphersuite_info == NULL )
Jens Wiklander817466c2018-05-22 13:49:31 +02002273 {
Jerome Forissier79013242021-07-28 10:24:04 +02002274 MBEDTLS_SSL_DEBUG_MSG( 1,
2275 ( "ciphersuite info for %04x not found", (unsigned int)i ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002276 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2277 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
2278 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2279 }
2280
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002281 mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info );
Jens Wiklander817466c2018-05-22 13:49:31 +02002282
Jerome Forissier79013242021-07-28 10:24:04 +02002283 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002284 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
2285
2286 /*
2287 * Check if the session can be resumed
2288 */
2289 if( ssl->handshake->resume == 0 || n == 0 ||
2290#if defined(MBEDTLS_SSL_RENEGOTIATION)
2291 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
2292#endif
2293 ssl->session_negotiate->ciphersuite != i ||
2294 ssl->session_negotiate->compression != comp ||
2295 ssl->session_negotiate->id_len != n ||
2296 memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
2297 {
2298 ssl->state++;
2299 ssl->handshake->resume = 0;
2300#if defined(MBEDTLS_HAVE_TIME)
2301 ssl->session_negotiate->start = mbedtls_time( NULL );
2302#endif
2303 ssl->session_negotiate->ciphersuite = i;
2304 ssl->session_negotiate->compression = comp;
2305 ssl->session_negotiate->id_len = n;
2306 memcpy( ssl->session_negotiate->id, buf + 35, n );
2307 }
2308 else
2309 {
2310 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
Jens Wiklander817466c2018-05-22 13:49:31 +02002311 }
2312
2313 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
2314 ssl->handshake->resume ? "a" : "no" ) );
2315
Jerome Forissier79013242021-07-28 10:24:04 +02002316 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", (unsigned) i ) );
2317 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d",
2318 buf[37 + n] ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002319
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002320 /*
2321 * Perform cipher suite validation in same way as in ssl_write_client_hello.
2322 */
Jens Wiklander817466c2018-05-22 13:49:31 +02002323 i = 0;
2324 while( 1 )
2325 {
2326 if( ssl->conf->ciphersuite_list[ssl->minor_ver][i] == 0 )
2327 {
2328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02002329 mbedtls_ssl_send_alert_message(
2330 ssl,
2331 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2332 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Jens Wiklander817466c2018-05-22 13:49:31 +02002333 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
2334 }
2335
2336 if( ssl->conf->ciphersuite_list[ssl->minor_ver][i++] ==
2337 ssl->session_negotiate->ciphersuite )
2338 {
2339 break;
2340 }
2341 }
2342
Jerome Forissier79013242021-07-28 10:24:04 +02002343 suite_info = mbedtls_ssl_ciphersuite_from_id(
2344 ssl->session_negotiate->ciphersuite );
2345 if( ssl_validate_ciphersuite( suite_info, ssl, ssl->minor_ver,
2346 ssl->minor_ver ) != 0 )
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002347 {
2348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02002349 mbedtls_ssl_send_alert_message(
2350 ssl,
2351 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2352 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002353 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
2354 }
2355
Jerome Forissier79013242021-07-28 10:24:04 +02002356 MBEDTLS_SSL_DEBUG_MSG( 3,
2357 ( "server hello, chosen ciphersuite: %s", suite_info->name ) );
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002358
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002359#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002360 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
2361 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
2362 {
2363 ssl->handshake->ecrs_enabled = 1;
2364 }
2365#endif
2366
Jens Wiklander817466c2018-05-22 13:49:31 +02002367 if( comp != MBEDTLS_SSL_COMPRESS_NULL
2368#if defined(MBEDTLS_ZLIB_SUPPORT)
2369 && comp != MBEDTLS_SSL_COMPRESS_DEFLATE
2370#endif
2371 )
2372 {
2373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02002374 mbedtls_ssl_send_alert_message(
2375 ssl,
2376 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2377 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Jens Wiklander817466c2018-05-22 13:49:31 +02002378 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
2379 }
2380 ssl->session_negotiate->compression = comp;
2381
2382 ext = buf + 40 + n;
2383
Jerome Forissier79013242021-07-28 10:24:04 +02002384 MBEDTLS_SSL_DEBUG_MSG( 2,
2385 ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET, ext_len ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002386
2387 while( ext_len )
2388 {
2389 unsigned int ext_id = ( ( ext[0] << 8 )
2390 | ( ext[1] ) );
2391 unsigned int ext_size = ( ( ext[2] << 8 )
2392 | ( ext[3] ) );
2393
2394 if( ext_size + 4 > ext_len )
2395 {
2396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02002397 mbedtls_ssl_send_alert_message(
2398 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2399 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Jens Wiklander817466c2018-05-22 13:49:31 +02002400 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
2401 }
2402
2403 switch( ext_id )
2404 {
2405 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
2406 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
2407#if defined(MBEDTLS_SSL_RENEGOTIATION)
2408 renegotiation_info_seen = 1;
2409#endif
2410
2411 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
2412 ext_size ) ) != 0 )
2413 return( ret );
2414
2415 break;
2416
2417#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2418 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Jerome Forissier79013242021-07-28 10:24:04 +02002419 MBEDTLS_SSL_DEBUG_MSG( 3,
2420 ( "found max_fragment_length extension" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002421
2422 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
2423 ext + 4, ext_size ) ) != 0 )
2424 {
2425 return( ret );
2426 }
2427
2428 break;
2429#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2430
2431#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
2432 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
2433 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
2434
2435 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
2436 ext + 4, ext_size ) ) != 0 )
2437 {
2438 return( ret );
2439 }
2440
2441 break;
2442#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
2443
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002444#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
2445 case MBEDTLS_TLS_EXT_CID:
2446 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
2447
2448 if( ( ret = ssl_parse_cid_ext( ssl,
2449 ext + 4,
2450 ext_size ) ) != 0 )
2451 {
2452 return( ret );
2453 }
2454
2455 break;
2456#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
2457
Jens Wiklander817466c2018-05-22 13:49:31 +02002458#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2459 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
2460 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
2461
2462 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
2463 ext + 4, ext_size ) ) != 0 )
2464 {
2465 return( ret );
2466 }
2467
2468 break;
2469#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2470
2471#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2472 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Jerome Forissier79013242021-07-28 10:24:04 +02002473 MBEDTLS_SSL_DEBUG_MSG( 3,
2474 ( "found extended_master_secret extension" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002475
2476 if( ( ret = ssl_parse_extended_ms_ext( ssl,
2477 ext + 4, ext_size ) ) != 0 )
2478 {
2479 return( ret );
2480 }
2481
2482 break;
2483#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
2484
2485#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2486 case MBEDTLS_TLS_EXT_SESSION_TICKET:
2487 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
2488
2489 if( ( ret = ssl_parse_session_ticket_ext( ssl,
2490 ext + 4, ext_size ) ) != 0 )
2491 {
2492 return( ret );
2493 }
2494
2495 break;
2496#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2497
2498#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
2499 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2500 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Jerome Forissier79013242021-07-28 10:24:04 +02002501 MBEDTLS_SSL_DEBUG_MSG( 3,
2502 ( "found supported_point_formats extension" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002503
2504 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
2505 ext + 4, ext_size ) ) != 0 )
2506 {
2507 return( ret );
2508 }
2509
2510 break;
2511#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
2512 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2513
2514#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2515 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
2516 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) );
2517
2518 if( ( ret = ssl_parse_ecjpake_kkpp( ssl,
2519 ext + 4, ext_size ) ) != 0 )
2520 {
2521 return( ret );
2522 }
2523
2524 break;
2525#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2526
2527#if defined(MBEDTLS_SSL_ALPN)
2528 case MBEDTLS_TLS_EXT_ALPN:
2529 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
2530
2531 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
2532 return( ret );
2533
2534 break;
2535#endif /* MBEDTLS_SSL_ALPN */
2536
Jerome Forissier79013242021-07-28 10:24:04 +02002537#if defined(MBEDTLS_SSL_DTLS_SRTP)
2538 case MBEDTLS_TLS_EXT_USE_SRTP:
2539 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
2540
2541 if( ( ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size ) ) != 0 )
2542 return( ret );
2543
2544 break;
2545#endif /* MBEDTLS_SSL_DTLS_SRTP */
2546
Jens Wiklander817466c2018-05-22 13:49:31 +02002547 default:
Jerome Forissier79013242021-07-28 10:24:04 +02002548 MBEDTLS_SSL_DEBUG_MSG( 3,
2549 ( "unknown extension found: %u (ignoring)", ext_id ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002550 }
2551
2552 ext_len -= 4 + ext_size;
2553 ext += 4 + ext_size;
2554
2555 if( ext_len > 0 && ext_len < 4 )
2556 {
2557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
2558 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
2559 }
2560 }
2561
2562 /*
Jerome Forissier039e02d2022-08-09 17:10:15 +02002563 * mbedtls_ssl_derive_keys() has to be called after the parsing of the
2564 * extensions. It sets the transform data for the resumed session which in
2565 * case of DTLS includes the server CID extracted from the CID extension.
2566 */
2567 if( ssl->handshake->resume )
2568 {
2569 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
2570 {
2571 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
2572 mbedtls_ssl_send_alert_message(
2573 ssl,
2574 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2575 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
2576 return( ret );
2577 }
2578 }
2579
2580 /*
Jens Wiklander817466c2018-05-22 13:49:31 +02002581 * Renegotiation security checks
2582 */
2583 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Jerome Forissier79013242021-07-28 10:24:04 +02002584 ssl->conf->allow_legacy_renegotiation ==
2585 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Jens Wiklander817466c2018-05-22 13:49:31 +02002586 {
Jerome Forissier79013242021-07-28 10:24:04 +02002587 MBEDTLS_SSL_DEBUG_MSG( 1,
2588 ( "legacy renegotiation, breaking off handshake" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002589 handshake_failure = 1;
2590 }
2591#if defined(MBEDTLS_SSL_RENEGOTIATION)
2592 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2593 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
2594 renegotiation_info_seen == 0 )
2595 {
Jerome Forissier79013242021-07-28 10:24:04 +02002596 MBEDTLS_SSL_DEBUG_MSG( 1,
2597 ( "renegotiation_info extension missing (secure)" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002598 handshake_failure = 1;
2599 }
2600 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2601 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Jerome Forissier79013242021-07-28 10:24:04 +02002602 ssl->conf->allow_legacy_renegotiation ==
2603 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Jens Wiklander817466c2018-05-22 13:49:31 +02002604 {
2605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
2606 handshake_failure = 1;
2607 }
2608 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2609 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
2610 renegotiation_info_seen == 1 )
2611 {
Jerome Forissier79013242021-07-28 10:24:04 +02002612 MBEDTLS_SSL_DEBUG_MSG( 1,
2613 ( "renegotiation_info extension present (legacy)" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002614 handshake_failure = 1;
2615 }
2616#endif /* MBEDTLS_SSL_RENEGOTIATION */
2617
2618 if( handshake_failure == 1 )
2619 {
Jerome Forissier79013242021-07-28 10:24:04 +02002620 mbedtls_ssl_send_alert_message(
2621 ssl,
2622 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2623 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Jens Wiklander817466c2018-05-22 13:49:31 +02002624 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
2625 }
2626
2627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
2628
2629 return( 0 );
2630}
2631
2632#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2633 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Jerome Forissier039e02d2022-08-09 17:10:15 +02002634MBEDTLS_CHECK_RETURN_CRITICAL
Jerome Forissier79013242021-07-28 10:24:04 +02002635static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl,
2636 unsigned char **p,
Jens Wiklander817466c2018-05-22 13:49:31 +02002637 unsigned char *end )
2638{
2639 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerome Forissier79013242021-07-28 10:24:04 +02002640 size_t dhm_actual_bitlen;
Jens Wiklander817466c2018-05-22 13:49:31 +02002641
2642 /*
2643 * Ephemeral DH parameters:
2644 *
2645 * struct {
2646 * opaque dh_p<1..2^16-1>;
2647 * opaque dh_g<1..2^16-1>;
2648 * opaque dh_Ys<1..2^16-1>;
2649 * } ServerDHParams;
2650 */
Jerome Forissier79013242021-07-28 10:24:04 +02002651 if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx,
2652 p, end ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02002653 {
2654 MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret );
2655 return( ret );
2656 }
2657
Jerome Forissier79013242021-07-28 10:24:04 +02002658 dhm_actual_bitlen = mbedtls_mpi_bitlen( &ssl->handshake->dhm_ctx.P );
2659 if( dhm_actual_bitlen < ssl->conf->dhm_min_bitlen )
Jens Wiklander817466c2018-05-22 13:49:31 +02002660 {
Jerome Forissier79013242021-07-28 10:24:04 +02002661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
2662 dhm_actual_bitlen,
Jens Wiklander817466c2018-05-22 13:49:31 +02002663 ssl->conf->dhm_min_bitlen ) );
2664 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2665 }
2666
2667 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2668 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2669 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2670
2671 return( ret );
2672}
2673#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2674 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2675
2676#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2677 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2678 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
2679 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2680 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Jerome Forissier039e02d2022-08-09 17:10:15 +02002681MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02002682static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl )
2683{
2684 const mbedtls_ecp_curve_info *curve_info;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002685 mbedtls_ecp_group_id grp_id;
2686#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
2687 grp_id = ssl->handshake->ecdh_ctx.grp.id;
2688#else
2689 grp_id = ssl->handshake->ecdh_ctx.grp_id;
2690#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02002691
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002692 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
Jens Wiklander817466c2018-05-22 13:49:31 +02002693 if( curve_info == NULL )
2694 {
2695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2696 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2697 }
2698
2699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
2700
2701#if defined(MBEDTLS_ECP_C)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002702 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02002703#else
2704 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
2705 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
2706#endif
2707 return( -1 );
2708
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002709 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2710 MBEDTLS_DEBUG_ECDH_QP );
Jens Wiklander817466c2018-05-22 13:49:31 +02002711
2712 return( 0 );
2713}
2714#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2715 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2716 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2717 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2718 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2719
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002720#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2721 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2722 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) )
Jerome Forissier039e02d2022-08-09 17:10:15 +02002723MBEDTLS_CHECK_RETURN_CRITICAL
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002724static int ssl_parse_server_ecdh_params_psa( mbedtls_ssl_context *ssl,
2725 unsigned char **p,
2726 unsigned char *end )
2727{
2728 uint16_t tls_id;
2729 size_t ecdh_bits = 0;
2730 uint8_t ecpoint_len;
2731 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2732
2733 /*
2734 * Parse ECC group
2735 */
2736
2737 if( end - *p < 4 )
2738 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2739
2740 /* First byte is curve_type; only named_curve is handled */
2741 if( *(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
2742 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2743
2744 /* Next two bytes are the namedcurve value */
2745 tls_id = *(*p)++;
2746 tls_id <<= 8;
2747 tls_id |= *(*p)++;
2748
Jerome Forissier039e02d2022-08-09 17:10:15 +02002749 /* Check it's a curve we offered */
2750 if( mbedtls_ssl_check_curve_tls_id( ssl, tls_id ) != 0 )
2751 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2752
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002753 /* Convert EC group to PSA key type. */
2754 if( ( handshake->ecdh_psa_type =
2755 mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 )
2756 {
2757 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2758 }
2759 if( ecdh_bits > 0xffff )
2760 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2761 handshake->ecdh_bits = (uint16_t) ecdh_bits;
2762
2763 /*
2764 * Put peer's ECDH public key in the format understood by PSA.
2765 */
2766
2767 ecpoint_len = *(*p)++;
2768 if( (size_t)( end - *p ) < ecpoint_len )
2769 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2770
2771 if( mbedtls_psa_tls_ecpoint_to_psa_ec(
2772 *p, ecpoint_len,
2773 handshake->ecdh_psa_peerkey,
2774 sizeof( handshake->ecdh_psa_peerkey ),
2775 &handshake->ecdh_psa_peerkey_len ) != 0 )
2776 {
2777 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2778 }
2779
2780 *p += ecpoint_len;
2781 return( 0 );
2782}
2783#endif /* MBEDTLS_USE_PSA_CRYPTO &&
2784 ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2785 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */
2786
Jens Wiklander817466c2018-05-22 13:49:31 +02002787#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2788 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2789 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerome Forissier039e02d2022-08-09 17:10:15 +02002790MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02002791static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
2792 unsigned char **p,
2793 unsigned char *end )
2794{
2795 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2796
2797 /*
2798 * Ephemeral ECDH parameters:
2799 *
2800 * struct {
2801 * ECParameters curve_params;
2802 * ECPoint public;
2803 * } ServerECDHParams;
2804 */
2805 if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
2806 (const unsigned char **) p, end ) ) != 0 )
2807 {
2808 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002809#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002810 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2811 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2812#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02002813 return( ret );
2814 }
2815
2816 if( ssl_check_server_ecdh_params( ssl ) != 0 )
2817 {
Jerome Forissier79013242021-07-28 10:24:04 +02002818 MBEDTLS_SSL_DEBUG_MSG( 1,
2819 ( "bad server key exchange message (ECDHE curve)" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002820 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2821 }
2822
2823 return( ret );
2824}
2825#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2826 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2827 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2828
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002829#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerome Forissier039e02d2022-08-09 17:10:15 +02002830MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02002831static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
2832 unsigned char **p,
2833 unsigned char *end )
2834{
2835 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002836 uint16_t len;
Jens Wiklander817466c2018-05-22 13:49:31 +02002837 ((void) ssl);
2838
2839 /*
2840 * PSK parameters:
2841 *
2842 * opaque psk_identity_hint<0..2^16-1>;
2843 */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002844 if( end - (*p) < 2 )
2845 {
Jerome Forissier79013242021-07-28 10:24:04 +02002846 MBEDTLS_SSL_DEBUG_MSG( 1,
2847 ( "bad server key exchange message (psk_identity_hint length)" ) );
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002848 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2849 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002850 len = (*p)[0] << 8 | (*p)[1];
2851 *p += 2;
2852
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002853 if( end - (*p) < len )
Jens Wiklander817466c2018-05-22 13:49:31 +02002854 {
Jerome Forissier79013242021-07-28 10:24:04 +02002855 MBEDTLS_SSL_DEBUG_MSG( 1,
2856 ( "bad server key exchange message (psk_identity_hint length)" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002857 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2858 }
2859
2860 /*
2861 * Note: we currently ignore the PKS identity hint, as we only allow one
2862 * PSK to be provisionned on the client. This could be changed later if
2863 * someone needs that feature.
2864 */
2865 *p += len;
2866 ret = 0;
2867
2868 return( ret );
2869}
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002870#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02002871
2872#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
2873 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2874/*
2875 * Generate a pre-master secret and encrypt it with the server's RSA key
2876 */
Jerome Forissier039e02d2022-08-09 17:10:15 +02002877MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02002878static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
2879 size_t offset, size_t *olen,
2880 size_t pms_offset )
2881{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002882 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02002883 size_t len_bytes = ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ? 0 : 2;
2884 unsigned char *p = ssl->handshake->premaster + pms_offset;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002885 mbedtls_pk_context * peer_pk;
Jens Wiklander817466c2018-05-22 13:49:31 +02002886
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002887 if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN )
Jens Wiklander817466c2018-05-22 13:49:31 +02002888 {
2889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
2890 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2891 }
2892
2893 /*
2894 * Generate (part of) the pre-master as
2895 * struct {
2896 * ProtocolVersion client_version;
2897 * opaque random[46];
2898 * } PreMasterSecret;
2899 */
Jerome Forissier79013242021-07-28 10:24:04 +02002900 mbedtls_ssl_write_version( ssl->conf->max_major_ver,
2901 ssl->conf->max_minor_ver,
2902 ssl->conf->transport, p );
Jens Wiklander817466c2018-05-22 13:49:31 +02002903
2904 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
2905 {
2906 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
2907 return( ret );
2908 }
2909
2910 ssl->handshake->pmslen = 48;
2911
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002912#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2913 peer_pk = &ssl->handshake->peer_pubkey;
2914#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Jens Wiklander817466c2018-05-22 13:49:31 +02002915 if( ssl->session_negotiate->peer_cert == NULL )
2916 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002917 /* Should never happen */
2918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2919 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jens Wiklander817466c2018-05-22 13:49:31 +02002920 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002921 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2922#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Jens Wiklander817466c2018-05-22 13:49:31 +02002923
2924 /*
2925 * Now write it out, encrypted
2926 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002927 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) )
Jens Wiklander817466c2018-05-22 13:49:31 +02002928 {
2929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
2930 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
2931 }
2932
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002933 if( ( ret = mbedtls_pk_encrypt( peer_pk,
Jens Wiklander817466c2018-05-22 13:49:31 +02002934 p, ssl->handshake->pmslen,
2935 ssl->out_msg + offset + len_bytes, olen,
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002936 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
Jens Wiklander817466c2018-05-22 13:49:31 +02002937 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
2938 {
2939 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
2940 return( ret );
2941 }
2942
2943#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2944 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2945 if( len_bytes == 2 )
2946 {
Jerome Forissier039e02d2022-08-09 17:10:15 +02002947 MBEDTLS_PUT_UINT16_BE( *olen, ssl->out_msg, offset );
Jens Wiklander817466c2018-05-22 13:49:31 +02002948 *olen += 2;
2949 }
2950#endif
2951
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002952#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2953 /* We don't need the peer's public key anymore. Free it. */
2954 mbedtls_pk_free( peer_pk );
2955#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Jens Wiklander817466c2018-05-22 13:49:31 +02002956 return( 0 );
2957}
2958#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
2959 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2960
2961#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2962#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2963 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2964 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Jerome Forissier039e02d2022-08-09 17:10:15 +02002965MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02002966static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl,
2967 unsigned char **p,
2968 unsigned char *end,
2969 mbedtls_md_type_t *md_alg,
2970 mbedtls_pk_type_t *pk_alg )
2971{
2972 ((void) ssl);
2973 *md_alg = MBEDTLS_MD_NONE;
2974 *pk_alg = MBEDTLS_PK_NONE;
2975
2976 /* Only in TLS 1.2 */
2977 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
2978 {
2979 return( 0 );
2980 }
2981
2982 if( (*p) + 2 > end )
2983 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2984
2985 /*
2986 * Get hash algorithm
2987 */
Jerome Forissier79013242021-07-28 10:24:04 +02002988 if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) )
2989 == MBEDTLS_MD_NONE )
Jens Wiklander817466c2018-05-22 13:49:31 +02002990 {
Jerome Forissier79013242021-07-28 10:24:04 +02002991 MBEDTLS_SSL_DEBUG_MSG( 1,
2992 ( "Server used unsupported HashAlgorithm %d", *(p)[0] ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002993 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2994 }
2995
2996 /*
2997 * Get signature algorithm
2998 */
Jerome Forissier79013242021-07-28 10:24:04 +02002999 if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) )
3000 == MBEDTLS_PK_NONE )
Jens Wiklander817466c2018-05-22 13:49:31 +02003001 {
Jerome Forissier79013242021-07-28 10:24:04 +02003002 MBEDTLS_SSL_DEBUG_MSG( 1,
3003 ( "server used unsupported SignatureAlgorithm %d", (*p)[1] ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02003004 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
3005 }
3006
3007 /*
3008 * Check if the hash is acceptable
3009 */
3010 if( mbedtls_ssl_check_sig_hash( ssl, *md_alg ) != 0 )
3011 {
Jerome Forissier79013242021-07-28 10:24:04 +02003012 MBEDTLS_SSL_DEBUG_MSG( 1,
3013 ( "server used HashAlgorithm %d that was not offered", *(p)[0] ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02003014 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
3015 }
3016
Jerome Forissier79013242021-07-28 10:24:04 +02003017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d",
3018 (*p)[1] ) );
3019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d",
3020 (*p)[0] ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02003021 *p += 2;
3022
3023 return( 0 );
3024}
3025#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3026 MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3027 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
3028#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3029
3030#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3031 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Jerome Forissier039e02d2022-08-09 17:10:15 +02003032MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02003033static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
3034{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003035 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02003036 const mbedtls_ecp_keypair *peer_key;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003037 mbedtls_pk_context * peer_pk;
Jens Wiklander817466c2018-05-22 13:49:31 +02003038
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003039#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3040 peer_pk = &ssl->handshake->peer_pubkey;
3041#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Jens Wiklander817466c2018-05-22 13:49:31 +02003042 if( ssl->session_negotiate->peer_cert == NULL )
3043 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003044 /* Should never happen */
3045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3046 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jens Wiklander817466c2018-05-22 13:49:31 +02003047 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003048 peer_pk = &ssl->session_negotiate->peer_cert->pk;
3049#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Jens Wiklander817466c2018-05-22 13:49:31 +02003050
Jerome Forissier039e02d2022-08-09 17:10:15 +02003051 /* This is a public key, so it can't be opaque, so can_do() is a good
3052 * enough check to ensure pk_ec() is safe to use below. */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003053 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) )
Jens Wiklander817466c2018-05-22 13:49:31 +02003054 {
3055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
3056 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
3057 }
3058
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003059 peer_key = mbedtls_pk_ec( *peer_pk );
Jens Wiklander817466c2018-05-22 13:49:31 +02003060
3061 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
3062 MBEDTLS_ECDH_THEIRS ) ) != 0 )
3063 {
3064 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
3065 return( ret );
3066 }
3067
3068 if( ssl_check_server_ecdh_params( ssl ) != 0 )
3069 {
3070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
3071 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
3072 }
3073
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003074#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3075 /* We don't need the peer's public key anymore. Free it,
3076 * so that more RAM is available for upcoming expensive
3077 * operations like ECDHE. */
3078 mbedtls_pk_free( peer_pk );
3079#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3080
Jens Wiklander817466c2018-05-22 13:49:31 +02003081 return( ret );
3082}
3083#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
3084 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
3085
Jerome Forissier039e02d2022-08-09 17:10:15 +02003086MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02003087static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
3088{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003089 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02003090 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003091 ssl->handshake->ciphersuite_info;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003092 unsigned char *p = NULL, *end = NULL;
Jens Wiklander817466c2018-05-22 13:49:31 +02003093
3094 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
3095
3096#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3097 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
3098 {
3099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
3100 ssl->state++;
3101 return( 0 );
3102 }
3103 ((void) p);
3104 ((void) end);
3105#endif
3106
3107#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3108 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3109 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
3110 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
3111 {
3112 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
3113 {
3114 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
Jerome Forissier79013242021-07-28 10:24:04 +02003115 mbedtls_ssl_send_alert_message(
3116 ssl,
3117 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3118 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Jens Wiklander817466c2018-05-22 13:49:31 +02003119 return( ret );
3120 }
3121
3122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
3123 ssl->state++;
3124 return( 0 );
3125 }
3126 ((void) p);
3127 ((void) end);
3128#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3129 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
3130
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003131#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003132 if( ssl->handshake->ecrs_enabled &&
3133 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing )
3134 {
3135 goto start_processing;
3136 }
3137#endif
3138
3139 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003140 {
3141 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
3142 return( ret );
3143 }
3144
3145 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
3146 {
3147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02003148 mbedtls_ssl_send_alert_message(
3149 ssl,
3150 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3151 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Jens Wiklander817466c2018-05-22 13:49:31 +02003152 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
3153 }
3154
3155 /*
3156 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
3157 * doesn't use a psk_identity_hint
3158 */
3159 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
3160 {
3161 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
3162 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
3163 {
3164 /* Current message is probably either
3165 * CertificateRequest or ServerHelloDone */
3166 ssl->keep_current_message = 1;
3167 goto exit;
3168 }
3169
Jerome Forissier79013242021-07-28 10:24:04 +02003170 MBEDTLS_SSL_DEBUG_MSG( 1,
3171 ( "server key exchange message must not be skipped" ) );
3172 mbedtls_ssl_send_alert_message(
3173 ssl,
3174 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3175 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Jens Wiklander817466c2018-05-22 13:49:31 +02003176
3177 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
3178 }
3179
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003180#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003181 if( ssl->handshake->ecrs_enabled )
3182 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
3183
3184start_processing:
3185#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02003186 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
3187 end = ssl->in_msg + ssl->in_hslen;
3188 MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
3189
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003190#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02003191 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
3192 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
3193 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
3194 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
3195 {
3196 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
3197 {
3198 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02003199 mbedtls_ssl_send_alert_message(
3200 ssl,
3201 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3202 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Jens Wiklander817466c2018-05-22 13:49:31 +02003203 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
3204 }
Jerome Forissier039e02d2022-08-09 17:10:15 +02003205 } /* FALLTHROUGH */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003206#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02003207
3208#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
3209 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3210 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
3211 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
3212 ; /* nothing more to do */
3213 else
3214#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
3215 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
3216#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3217 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3218 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
3219 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
3220 {
3221 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
3222 {
3223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02003224 mbedtls_ssl_send_alert_message(
3225 ssl,
3226 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3227 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Jens Wiklander817466c2018-05-22 13:49:31 +02003228 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
3229 }
3230 }
3231 else
3232#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3233 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003234#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
3235 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3236 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) )
3237 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
3238 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
3239 {
3240 if( ssl_parse_server_ecdh_params_psa( ssl, &p, end ) != 0 )
3241 {
3242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02003243 mbedtls_ssl_send_alert_message(
3244 ssl,
3245 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3246 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003247 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
3248 }
3249 }
3250 else
3251#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3252 ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3253 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */
Jens Wiklander817466c2018-05-22 13:49:31 +02003254#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3255 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
3256 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
3257 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
3258 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
3259 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
3260 {
3261 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
3262 {
3263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02003264 mbedtls_ssl_send_alert_message(
3265 ssl,
3266 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3267 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Jens Wiklander817466c2018-05-22 13:49:31 +02003268 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
3269 }
3270 }
3271 else
3272#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3273 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
3274 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
3275#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3276 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3277 {
3278 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
3279 p, end - p );
3280 if( ret != 0 )
3281 {
3282 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
Jerome Forissier79013242021-07-28 10:24:04 +02003283 mbedtls_ssl_send_alert_message(
3284 ssl,
3285 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3286 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Jens Wiklander817466c2018-05-22 13:49:31 +02003287 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
3288 }
3289 }
3290 else
3291#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
3292 {
3293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3294 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3295 }
3296
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003297#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02003298 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
3299 {
3300 size_t sig_len, hashlen;
Jerome Forissier039e02d2022-08-09 17:10:15 +02003301#if defined(MBEDTLS_USE_PSA_CRYPTO)
3302 unsigned char hash[PSA_HASH_MAX_SIZE];
3303#else
3304 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
3305#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02003306 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
3307 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
3308 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
3309 size_t params_len = p - params;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003310 void *rs_ctx = NULL;
Jens Wiklander817466c2018-05-22 13:49:31 +02003311
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003312 mbedtls_pk_context * peer_pk;
3313
Jens Wiklander817466c2018-05-22 13:49:31 +02003314 /*
3315 * Handle the digitally-signed structure
3316 */
3317#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3318 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
3319 {
3320 if( ssl_parse_signature_algorithm( ssl, &p, end,
3321 &md_alg, &pk_alg ) != 0 )
3322 {
Jerome Forissier79013242021-07-28 10:24:04 +02003323 MBEDTLS_SSL_DEBUG_MSG( 1,
3324 ( "bad server key exchange message" ) );
3325 mbedtls_ssl_send_alert_message(
3326 ssl,
3327 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3328 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Jens Wiklander817466c2018-05-22 13:49:31 +02003329 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
3330 }
3331
Jerome Forissier79013242021-07-28 10:24:04 +02003332 if( pk_alg !=
3333 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Jens Wiklander817466c2018-05-22 13:49:31 +02003334 {
Jerome Forissier79013242021-07-28 10:24:04 +02003335 MBEDTLS_SSL_DEBUG_MSG( 1,
3336 ( "bad server key exchange message" ) );
3337 mbedtls_ssl_send_alert_message(
3338 ssl,
3339 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3340 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Jens Wiklander817466c2018-05-22 13:49:31 +02003341 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
3342 }
3343 }
3344 else
3345#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3346#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3347 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3348 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
3349 {
3350 pk_alg = mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
3351
3352 /* Default hash for ECDSA is SHA-1 */
3353 if( pk_alg == MBEDTLS_PK_ECDSA && md_alg == MBEDTLS_MD_NONE )
3354 md_alg = MBEDTLS_MD_SHA1;
3355 }
3356 else
3357#endif
3358 {
3359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3360 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3361 }
3362
3363 /*
3364 * Read signature
3365 */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003366
3367 if( p > end - 2 )
3368 {
3369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02003370 mbedtls_ssl_send_alert_message(
3371 ssl,
3372 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3373 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003374 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
3375 }
Jens Wiklander817466c2018-05-22 13:49:31 +02003376 sig_len = ( p[0] << 8 ) | p[1];
3377 p += 2;
3378
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003379 if( p != end - sig_len )
Jens Wiklander817466c2018-05-22 13:49:31 +02003380 {
3381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02003382 mbedtls_ssl_send_alert_message(
3383 ssl,
3384 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3385 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Jens Wiklander817466c2018-05-22 13:49:31 +02003386 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
3387 }
3388
3389 MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
3390
3391 /*
3392 * Compute the hash that has been signed
3393 */
3394#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3395 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3396 if( md_alg == MBEDTLS_MD_NONE )
3397 {
Jens Wiklander817466c2018-05-22 13:49:31 +02003398 hashlen = 36;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003399 ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash, params,
3400 params_len );
3401 if( ret != 0 )
3402 return( ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02003403 }
3404 else
3405#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3406 MBEDTLS_SSL_PROTO_TLS1_1 */
3407#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3408 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3409 if( md_alg != MBEDTLS_MD_NONE )
3410 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003411 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
3412 params, params_len,
3413 md_alg );
3414 if( ret != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003415 return( ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02003416 }
3417 else
3418#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3419 MBEDTLS_SSL_PROTO_TLS1_2 */
3420 {
3421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3422 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3423 }
3424
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003425 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Jens Wiklander817466c2018-05-22 13:49:31 +02003426
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003427#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3428 peer_pk = &ssl->handshake->peer_pubkey;
3429#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Jens Wiklander817466c2018-05-22 13:49:31 +02003430 if( ssl->session_negotiate->peer_cert == NULL )
3431 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003432 /* Should never happen */
3433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3434 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jens Wiklander817466c2018-05-22 13:49:31 +02003435 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003436 peer_pk = &ssl->session_negotiate->peer_cert->pk;
3437#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Jens Wiklander817466c2018-05-22 13:49:31 +02003438
3439 /*
3440 * Verify signature
3441 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003442 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Jens Wiklander817466c2018-05-22 13:49:31 +02003443 {
3444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02003445 mbedtls_ssl_send_alert_message(
3446 ssl,
3447 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3448 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Jens Wiklander817466c2018-05-22 13:49:31 +02003449 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
3450 }
3451
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003452#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003453 if( ssl->handshake->ecrs_enabled )
3454 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
3455#endif
3456
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003457 if( ( ret = mbedtls_pk_verify_restartable( peer_pk,
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003458 md_alg, hash, hashlen, p, sig_len, rs_ctx ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003459 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003460#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003461 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3462#endif
Jerome Forissier79013242021-07-28 10:24:04 +02003463 mbedtls_ssl_send_alert_message(
3464 ssl,
3465 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3466 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
Jens Wiklander817466c2018-05-22 13:49:31 +02003467 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003468#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003469 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3470 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3471#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02003472 return( ret );
3473 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003474
3475#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3476 /* We don't need the peer's public key anymore. Free it,
3477 * so that more RAM is available for upcoming expensive
3478 * operations like ECDHE. */
3479 mbedtls_pk_free( peer_pk );
3480#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Jens Wiklander817466c2018-05-22 13:49:31 +02003481 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003482#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02003483
3484exit:
3485 ssl->state++;
3486
3487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
3488
3489 return( 0 );
3490}
3491
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003492#if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Jerome Forissier039e02d2022-08-09 17:10:15 +02003493MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02003494static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
3495{
3496 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003497 ssl->handshake->ciphersuite_info;
Jens Wiklander817466c2018-05-22 13:49:31 +02003498
3499 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
3500
3501 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
3502 {
3503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
3504 ssl->state++;
3505 return( 0 );
3506 }
3507
3508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3509 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3510}
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003511#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Jerome Forissier039e02d2022-08-09 17:10:15 +02003512MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02003513static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
3514{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003515 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02003516 unsigned char *buf;
3517 size_t n = 0;
3518 size_t cert_type_len = 0, dn_len = 0;
3519 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003520 ssl->handshake->ciphersuite_info;
Jens Wiklander817466c2018-05-22 13:49:31 +02003521
3522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
3523
3524 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
3525 {
3526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
3527 ssl->state++;
3528 return( 0 );
3529 }
3530
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003531 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003532 {
3533 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
3534 return( ret );
3535 }
3536
3537 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
3538 {
3539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02003540 mbedtls_ssl_send_alert_message(
3541 ssl,
3542 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3543 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Jens Wiklander817466c2018-05-22 13:49:31 +02003544 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
3545 }
3546
3547 ssl->state++;
3548 ssl->client_auth = ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST );
3549
3550 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
3551 ssl->client_auth ? "a" : "no" ) );
3552
3553 if( ssl->client_auth == 0 )
3554 {
3555 /* Current message is probably the ServerHelloDone */
3556 ssl->keep_current_message = 1;
3557 goto exit;
3558 }
3559
3560 /*
3561 * struct {
3562 * ClientCertificateType certificate_types<1..2^8-1>;
3563 * SignatureAndHashAlgorithm
3564 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
3565 * DistinguishedName certificate_authorities<0..2^16-1>;
3566 * } CertificateRequest;
3567 *
3568 * Since we only support a single certificate on clients, let's just
3569 * ignore all the information that's supposed to help us pick a
3570 * certificate.
3571 *
3572 * We could check that our certificate matches the request, and bail out
3573 * if it doesn't, but it's simpler to just send the certificate anyway,
3574 * and give the server the opportunity to decide if it should terminate
3575 * the connection when it doesn't like our certificate.
3576 *
3577 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
3578 * point we only have one hash available (see comments in
3579 * write_certificate_verify), so let's just use what we have.
3580 *
3581 * However, we still minimally parse the message to check it is at least
3582 * superficially sane.
3583 */
3584 buf = ssl->in_msg;
3585
3586 /* certificate_types */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003587 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) )
3588 {
3589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
3590 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3591 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
3592 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
3593 }
Jens Wiklander817466c2018-05-22 13:49:31 +02003594 cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
3595 n = cert_type_len;
3596
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003597 /*
3598 * In the subsequent code there are two paths that read from buf:
3599 * * the length of the signature algorithms field (if minor version of
3600 * SSL is 3),
3601 * * distinguished name length otherwise.
3602 * Both reach at most the index:
3603 * ...hdr_len + 2 + n,
3604 * therefore the buffer length at this point must be greater than that
3605 * regardless of the actual code path.
3606 */
3607 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
Jens Wiklander817466c2018-05-22 13:49:31 +02003608 {
3609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
3610 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3611 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
3612 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
3613 }
3614
3615 /* supported_signature_algorithms */
3616#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3617 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
3618 {
Jerome Forissier79013242021-07-28 10:24:04 +02003619 size_t sig_alg_len =
3620 ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
3621 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02003622#if defined(MBEDTLS_DEBUG_C)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003623 unsigned char* sig_alg;
Jens Wiklander817466c2018-05-22 13:49:31 +02003624 size_t i;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003625#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02003626
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003627 /*
3628 * The furthest access in buf is in the loop few lines below:
3629 * sig_alg[i + 1],
3630 * where:
3631 * sig_alg = buf + ...hdr_len + 3 + n,
3632 * max(i) = sig_alg_len - 1.
3633 * Therefore the furthest access is:
3634 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
3635 * which reduces to:
3636 * buf[...hdr_len + 3 + n + sig_alg_len],
3637 * which is one less than we need the buf to be.
3638 */
Jerome Forissier79013242021-07-28 10:24:04 +02003639 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl )
3640 + 3 + n + sig_alg_len )
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003641 {
3642 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02003643 mbedtls_ssl_send_alert_message(
3644 ssl,
3645 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3646 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003647 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
3648 }
3649
3650#if defined(MBEDTLS_DEBUG_C)
3651 sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
Jens Wiklander817466c2018-05-22 13:49:31 +02003652 for( i = 0; i < sig_alg_len; i += 2 )
3653 {
Jerome Forissier79013242021-07-28 10:24:04 +02003654 MBEDTLS_SSL_DEBUG_MSG( 3,
3655 ( "Supported Signature Algorithm found: %d,%d",
3656 sig_alg[i], sig_alg[i + 1] ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02003657 }
3658#endif
3659
3660 n += 2 + sig_alg_len;
Jens Wiklander817466c2018-05-22 13:49:31 +02003661 }
3662#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3663
3664 /* certificate_authorities */
3665 dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
3666 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
3667
3668 n += dn_len;
3669 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
3670 {
3671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
3672 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3673 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
3674 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
3675 }
3676
3677exit:
3678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
3679
3680 return( 0 );
3681}
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003682#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02003683
Jerome Forissier039e02d2022-08-09 17:10:15 +02003684MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02003685static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
3686{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003687 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02003688
3689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
3690
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003691 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003692 {
3693 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
3694 return( ret );
3695 }
3696
3697 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
3698 {
3699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
3700 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
3701 }
3702
3703 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
3704 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
3705 {
3706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
3707 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3708 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
3709 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
3710 }
3711
3712 ssl->state++;
3713
3714#if defined(MBEDTLS_SSL_PROTO_DTLS)
3715 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3716 mbedtls_ssl_recv_flight_completed( ssl );
3717#endif
3718
3719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
3720
3721 return( 0 );
3722}
3723
Jerome Forissier039e02d2022-08-09 17:10:15 +02003724MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02003725static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
3726{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003727 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3728
3729 size_t header_len;
3730 size_t content_len;
Jens Wiklander817466c2018-05-22 13:49:31 +02003731 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003732 ssl->handshake->ciphersuite_info;
Jens Wiklander817466c2018-05-22 13:49:31 +02003733
3734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
3735
3736#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
3737 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
3738 {
3739 /*
3740 * DHM key exchange -- send G^X mod P
3741 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003742 content_len = ssl->handshake->dhm_ctx.len;
Jens Wiklander817466c2018-05-22 13:49:31 +02003743
Jerome Forissier039e02d2022-08-09 17:10:15 +02003744 MBEDTLS_PUT_UINT16_BE( content_len, ssl->out_msg, 4 );
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003745 header_len = 6;
Jens Wiklander817466c2018-05-22 13:49:31 +02003746
3747 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Jerome Forissier79013242021-07-28 10:24:04 +02003748 (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
3749 &ssl->out_msg[header_len], content_len,
3750 ssl->conf->f_rng, ssl->conf->p_rng );
Jens Wiklander817466c2018-05-22 13:49:31 +02003751 if( ret != 0 )
3752 {
3753 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
3754 return( ret );
3755 }
3756
3757 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
3758 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
3759
3760 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Jerome Forissier79013242021-07-28 10:24:04 +02003761 ssl->handshake->premaster,
3762 MBEDTLS_PREMASTER_SIZE,
3763 &ssl->handshake->pmslen,
3764 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003765 {
3766 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
3767 return( ret );
3768 }
3769
3770 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
3771 }
3772 else
3773#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003774#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
3775 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3776 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) )
3777 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
3778 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
3779 {
Jerome Forissier039e02d2022-08-09 17:10:15 +02003780 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3781 psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003782 psa_key_attributes_t key_attributes;
3783
3784 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3785
3786 unsigned char own_pubkey[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
3787 size_t own_pubkey_len;
3788 unsigned char *own_pubkey_ecpoint;
3789 size_t own_pubkey_ecpoint_len;
3790
3791 header_len = 4;
3792
3793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
3794
3795 /*
3796 * Generate EC private key for ECDHE exchange.
3797 */
3798
3799 /* The master secret is obtained from the shared ECDH secret by
3800 * applying the TLS 1.2 PRF with a specific salt and label. While
3801 * the PSA Crypto API encourages combining key agreement schemes
3802 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
3803 * yet support the provisioning of salt + label to the KDF.
3804 * For the time being, we therefore need to split the computation
3805 * of the ECDH secret and the application of the TLS 1.2 PRF. */
3806 key_attributes = psa_key_attributes_init();
3807 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
3808 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
3809 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
3810 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
3811
3812 /* Generate ECDH private key. */
3813 status = psa_generate_key( &key_attributes,
3814 &handshake->ecdh_psa_privkey );
3815 if( status != PSA_SUCCESS )
3816 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
3817
3818 /* Export the public part of the ECDH private key from PSA
3819 * and convert it to ECPoint format used in ClientKeyExchange. */
3820 status = psa_export_public_key( handshake->ecdh_psa_privkey,
3821 own_pubkey, sizeof( own_pubkey ),
3822 &own_pubkey_len );
3823 if( status != PSA_SUCCESS )
Jerome Forissier039e02d2022-08-09 17:10:15 +02003824 {
3825 psa_destroy_key( handshake->ecdh_psa_privkey );
3826 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003827 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Jerome Forissier039e02d2022-08-09 17:10:15 +02003828 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003829
3830 if( mbedtls_psa_tls_psa_ec_to_ecpoint( own_pubkey,
3831 own_pubkey_len,
3832 &own_pubkey_ecpoint,
3833 &own_pubkey_ecpoint_len ) != 0 )
3834 {
Jerome Forissier039e02d2022-08-09 17:10:15 +02003835 psa_destroy_key( handshake->ecdh_psa_privkey );
3836 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003837 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
3838 }
3839
3840 /* Copy ECPoint structure to outgoing message buffer. */
3841 ssl->out_msg[header_len] = (unsigned char) own_pubkey_ecpoint_len;
3842 memcpy( ssl->out_msg + header_len + 1,
3843 own_pubkey_ecpoint, own_pubkey_ecpoint_len );
3844 content_len = own_pubkey_ecpoint_len + 1;
3845
3846 /* The ECDH secret is the premaster secret used for key derivation. */
3847
3848 /* Compute ECDH shared secret. */
3849 status = psa_raw_key_agreement( PSA_ALG_ECDH,
3850 handshake->ecdh_psa_privkey,
3851 handshake->ecdh_psa_peerkey,
3852 handshake->ecdh_psa_peerkey_len,
3853 ssl->handshake->premaster,
3854 sizeof( ssl->handshake->premaster ),
3855 &ssl->handshake->pmslen );
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003856
Jerome Forissier039e02d2022-08-09 17:10:15 +02003857 destruction_status = psa_destroy_key( handshake->ecdh_psa_privkey );
Jerome Forissier79013242021-07-28 10:24:04 +02003858 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Jerome Forissier039e02d2022-08-09 17:10:15 +02003859
3860 if( status != PSA_SUCCESS || destruction_status != PSA_SUCCESS )
3861 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003862 }
3863 else
3864#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3865 ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3866 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */
Jens Wiklander817466c2018-05-22 13:49:31 +02003867#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3868 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3869 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3870 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3871 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
3872 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
3873 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
3874 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
3875 {
3876 /*
3877 * ECDH key exchange -- send client public value
3878 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003879 header_len = 4;
Jens Wiklander817466c2018-05-22 13:49:31 +02003880
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003881#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003882 if( ssl->handshake->ecrs_enabled )
3883 {
3884 if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret )
3885 goto ecdh_calc_secret;
3886
3887 mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx );
3888 }
3889#endif
3890
Jens Wiklander817466c2018-05-22 13:49:31 +02003891 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003892 &content_len,
3893 &ssl->out_msg[header_len], 1000,
Jens Wiklander817466c2018-05-22 13:49:31 +02003894 ssl->conf->f_rng, ssl->conf->p_rng );
3895 if( ret != 0 )
3896 {
3897 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003898#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003899 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3900 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3901#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02003902 return( ret );
3903 }
3904
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003905 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3906 MBEDTLS_DEBUG_ECDH_Q );
Jens Wiklander817466c2018-05-22 13:49:31 +02003907
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003908#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003909 if( ssl->handshake->ecrs_enabled )
3910 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003911 ssl->handshake->ecrs_n = content_len;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003912 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
3913 }
3914
3915ecdh_calc_secret:
3916 if( ssl->handshake->ecrs_enabled )
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003917 content_len = ssl->handshake->ecrs_n;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003918#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02003919 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Jerome Forissier79013242021-07-28 10:24:04 +02003920 &ssl->handshake->pmslen,
3921 ssl->handshake->premaster,
3922 MBEDTLS_MPI_MAX_SIZE,
3923 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003924 {
3925 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003926#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003927 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3928 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3929#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02003930 return( ret );
3931 }
3932
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003933 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3934 MBEDTLS_DEBUG_ECDH_Z );
Jens Wiklander817466c2018-05-22 13:49:31 +02003935 }
3936 else
3937#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3938 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3939 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3940 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003941#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02003942 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
3943 {
3944 /*
3945 * opaque psk_identity<0..2^16-1>;
3946 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003947 if( ssl_conf_has_static_psk( ssl->conf ) == 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003948 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003949 /* We don't offer PSK suites if we don't have a PSK,
3950 * and we check that the server's choice is among the
3951 * ciphersuites we offered, so this should never happen. */
3952 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jens Wiklander817466c2018-05-22 13:49:31 +02003953 }
3954
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003955 header_len = 4;
3956 content_len = ssl->conf->psk_identity_len;
Jens Wiklander817466c2018-05-22 13:49:31 +02003957
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003958 if( header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Jens Wiklander817466c2018-05-22 13:49:31 +02003959 {
Jerome Forissier79013242021-07-28 10:24:04 +02003960 MBEDTLS_SSL_DEBUG_MSG( 1,
3961 ( "psk identity too long or SSL buffer too short" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02003962 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3963 }
3964
Jerome Forissier039e02d2022-08-09 17:10:15 +02003965 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
3966 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Jens Wiklander817466c2018-05-22 13:49:31 +02003967
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003968 memcpy( ssl->out_msg + header_len,
3969 ssl->conf->psk_identity,
3970 ssl->conf->psk_identity_len );
3971 header_len += ssl->conf->psk_identity_len;
Jens Wiklander817466c2018-05-22 13:49:31 +02003972
3973#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3974 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
3975 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003976 content_len = 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02003977 }
3978 else
3979#endif
3980#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3981 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
3982 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003983#if defined(MBEDTLS_USE_PSA_CRYPTO)
3984 /* Opaque PSKs are currently only supported for PSK-only suites. */
3985 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Jerome Forissier039e02d2022-08-09 17:10:15 +02003986 {
3987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "opaque PSK not supported with RSA-PSK" ) );
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003988 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerome Forissier039e02d2022-08-09 17:10:15 +02003989 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003990#endif /* MBEDTLS_USE_PSA_CRYPTO */
3991
3992 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3993 &content_len, 2 ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003994 return( ret );
3995 }
3996 else
3997#endif
3998#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3999 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
4000 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004001#if defined(MBEDTLS_USE_PSA_CRYPTO)
4002 /* Opaque PSKs are currently only supported for PSK-only suites. */
4003 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Jerome Forissier039e02d2022-08-09 17:10:15 +02004004 {
4005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "opaque PSK not supported with DHE-PSK" ) );
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004006 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerome Forissier039e02d2022-08-09 17:10:15 +02004007 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004008#endif /* MBEDTLS_USE_PSA_CRYPTO */
4009
Jens Wiklander817466c2018-05-22 13:49:31 +02004010 /*
4011 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
4012 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004013 content_len = ssl->handshake->dhm_ctx.len;
Jens Wiklander817466c2018-05-22 13:49:31 +02004014
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004015 if( header_len + 2 + content_len >
4016 MBEDTLS_SSL_OUT_CONTENT_LEN )
Jens Wiklander817466c2018-05-22 13:49:31 +02004017 {
Jerome Forissier79013242021-07-28 10:24:04 +02004018 MBEDTLS_SSL_DEBUG_MSG( 1,
4019 ( "psk identity or DHM size too long or SSL buffer too short" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02004020 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4021 }
4022
Jerome Forissier039e02d2022-08-09 17:10:15 +02004023 ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
4024 ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
Jens Wiklander817466c2018-05-22 13:49:31 +02004025
4026 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
4027 (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004028 &ssl->out_msg[header_len], content_len,
Jens Wiklander817466c2018-05-22 13:49:31 +02004029 ssl->conf->f_rng, ssl->conf->p_rng );
4030 if( ret != 0 )
4031 {
4032 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
4033 return( ret );
4034 }
4035 }
4036 else
4037#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
4038#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
4039 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
4040 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004041#if defined(MBEDTLS_USE_PSA_CRYPTO)
4042 /* Opaque PSKs are currently only supported for PSK-only suites. */
4043 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
Jerome Forissier039e02d2022-08-09 17:10:15 +02004044 {
4045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "opaque PSK not supported with ECDHE-PSK" ) );
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004046 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerome Forissier039e02d2022-08-09 17:10:15 +02004047 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004048#endif /* MBEDTLS_USE_PSA_CRYPTO */
4049
Jens Wiklander817466c2018-05-22 13:49:31 +02004050 /*
4051 * ClientECDiffieHellmanPublic public;
4052 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004053 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
4054 &content_len,
4055 &ssl->out_msg[header_len],
4056 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
Jens Wiklander817466c2018-05-22 13:49:31 +02004057 ssl->conf->f_rng, ssl->conf->p_rng );
4058 if( ret != 0 )
4059 {
4060 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
4061 return( ret );
4062 }
4063
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004064 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4065 MBEDTLS_DEBUG_ECDH_Q );
Jens Wiklander817466c2018-05-22 13:49:31 +02004066 }
4067 else
4068#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
4069 {
4070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4071 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4072 }
4073
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004074#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
4075 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
4076 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
4077 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
4078 ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
4079 {
Jerome Forissier79013242021-07-28 10:24:04 +02004080 MBEDTLS_SSL_DEBUG_MSG( 1,
4081 ( "skip PMS generation for opaque PSK" ) );
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004082 }
4083 else
4084#endif /* MBEDTLS_USE_PSA_CRYPTO &&
4085 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02004086 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
4087 ciphersuite_info->key_exchange ) ) != 0 )
4088 {
Jerome Forissier79013242021-07-28 10:24:04 +02004089 MBEDTLS_SSL_DEBUG_RET( 1,
4090 "mbedtls_ssl_psk_derive_premaster", ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02004091 return( ret );
4092 }
4093 }
4094 else
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004095#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02004096#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
4097 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
4098 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004099 header_len = 4;
4100 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
4101 &content_len, 0 ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02004102 return( ret );
4103 }
4104 else
4105#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
4106#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
4107 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
4108 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004109 header_len = 4;
Jens Wiklander817466c2018-05-22 13:49:31 +02004110
4111 ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004112 ssl->out_msg + header_len,
4113 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
4114 &content_len,
Jens Wiklander817466c2018-05-22 13:49:31 +02004115 ssl->conf->f_rng, ssl->conf->p_rng );
4116 if( ret != 0 )
4117 {
4118 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
4119 return( ret );
4120 }
4121
4122 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
4123 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
4124 ssl->conf->f_rng, ssl->conf->p_rng );
4125 if( ret != 0 )
4126 {
4127 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
4128 return( ret );
4129 }
4130 }
4131 else
4132#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
4133 {
4134 ((void) ciphersuite_info);
4135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4136 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4137 }
4138
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004139 ssl->out_msglen = header_len + content_len;
Jens Wiklander817466c2018-05-22 13:49:31 +02004140 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4141 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
4142
4143 ssl->state++;
4144
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004145 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02004146 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004147 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02004148 return( ret );
4149 }
4150
4151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
4152
4153 return( 0 );
4154}
4155
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004156#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Jerome Forissier039e02d2022-08-09 17:10:15 +02004157MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02004158static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
4159{
4160 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004161 ssl->handshake->ciphersuite_info;
4162 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02004163
4164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
4165
4166 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
4167 {
4168 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
4169 return( ret );
4170 }
4171
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004172 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Jens Wiklander817466c2018-05-22 13:49:31 +02004173 {
4174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
4175 ssl->state++;
4176 return( 0 );
4177 }
4178
4179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4180 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4181}
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004182#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Jerome Forissier039e02d2022-08-09 17:10:15 +02004183MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02004184static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
4185{
4186 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
4187 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004188 ssl->handshake->ciphersuite_info;
Jens Wiklander817466c2018-05-22 13:49:31 +02004189 size_t n = 0, offset = 0;
4190 unsigned char hash[48];
4191 unsigned char *hash_start = hash;
4192 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004193 size_t hashlen;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004194 void *rs_ctx = NULL;
Jens Wiklander817466c2018-05-22 13:49:31 +02004195
4196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
4197
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004198#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004199 if( ssl->handshake->ecrs_enabled &&
4200 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign )
4201 {
4202 goto sign;
4203 }
4204#endif
4205
Jens Wiklander817466c2018-05-22 13:49:31 +02004206 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
4207 {
4208 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
4209 return( ret );
4210 }
4211
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004212 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Jens Wiklander817466c2018-05-22 13:49:31 +02004213 {
4214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
4215 ssl->state++;
4216 return( 0 );
4217 }
4218
4219 if( ssl->client_auth == 0 || mbedtls_ssl_own_cert( ssl ) == NULL )
4220 {
4221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
4222 ssl->state++;
4223 return( 0 );
4224 }
4225
4226 if( mbedtls_ssl_own_key( ssl ) == NULL )
4227 {
4228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
4229 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
4230 }
4231
4232 /*
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004233 * Make a signature of the handshake digests
Jens Wiklander817466c2018-05-22 13:49:31 +02004234 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004235#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004236 if( ssl->handshake->ecrs_enabled )
4237 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
4238
4239sign:
4240#endif
4241
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004242 ssl->handshake->calc_verify( ssl, hash, &hashlen );
Jens Wiklander817466c2018-05-22 13:49:31 +02004243
4244#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4245 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4246 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
4247 {
4248 /*
4249 * digitally-signed struct {
4250 * opaque md5_hash[16];
4251 * opaque sha_hash[20];
4252 * };
4253 *
4254 * md5_hash
4255 * MD5(handshake_messages);
4256 *
4257 * sha_hash
4258 * SHA(handshake_messages);
4259 */
Jens Wiklander817466c2018-05-22 13:49:31 +02004260 md_alg = MBEDTLS_MD_NONE;
4261
4262 /*
4263 * For ECDSA, default hash is SHA-1 only
4264 */
4265 if( mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECDSA ) )
4266 {
4267 hash_start += 16;
4268 hashlen -= 16;
4269 md_alg = MBEDTLS_MD_SHA1;
4270 }
4271 }
4272 else
4273#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
4274 MBEDTLS_SSL_PROTO_TLS1_1 */
4275#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4276 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
4277 {
4278 /*
4279 * digitally-signed struct {
4280 * opaque handshake_messages[handshake_messages_length];
4281 * };
4282 *
4283 * Taking shortcut here. We assume that the server always allows the
4284 * PRF Hash function and has sent it in the allowed signature
4285 * algorithms list received in the Certificate Request message.
4286 *
4287 * Until we encounter a server that does not, we will take this
4288 * shortcut.
4289 *
Jerome Forissier79013242021-07-28 10:24:04 +02004290 * Reason: Otherwise we should have running hashes for SHA512 and
4291 * SHA224 in order to satisfy 'weird' needs from the server
4292 * side.
Jens Wiklander817466c2018-05-22 13:49:31 +02004293 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004294 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jens Wiklander817466c2018-05-22 13:49:31 +02004295 {
4296 md_alg = MBEDTLS_MD_SHA384;
4297 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
4298 }
4299 else
4300 {
4301 md_alg = MBEDTLS_MD_SHA256;
4302 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
4303 }
4304 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
4305
4306 /* Info from md_alg will be used instead */
4307 hashlen = 0;
4308 offset = 2;
4309 }
4310 else
4311#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4312 {
4313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4314 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4315 }
4316
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004317#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004318 if( ssl->handshake->ecrs_enabled )
4319 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
4320#endif
4321
4322 if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ),
4323 md_alg, hash_start, hashlen,
Jens Wiklander817466c2018-05-22 13:49:31 +02004324 ssl->out_msg + 6 + offset, &n,
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004325 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02004326 {
4327 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004328#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004329 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
4330 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
4331#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02004332 return( ret );
4333 }
4334
Jerome Forissier039e02d2022-08-09 17:10:15 +02004335 MBEDTLS_PUT_UINT16_BE( n, ssl->out_msg, offset + 4 );
Jens Wiklander817466c2018-05-22 13:49:31 +02004336
4337 ssl->out_msglen = 6 + n + offset;
4338 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4339 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
4340
4341 ssl->state++;
4342
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004343 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02004344 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004345 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02004346 return( ret );
4347 }
4348
4349 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
4350
4351 return( ret );
4352}
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004353#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02004354
4355#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerome Forissier039e02d2022-08-09 17:10:15 +02004356MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02004357static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
4358{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004359 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02004360 uint32_t lifetime;
4361 size_t ticket_len;
4362 unsigned char *ticket;
4363 const unsigned char *msg;
4364
4365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
4366
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004367 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02004368 {
4369 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
4370 return( ret );
4371 }
4372
4373 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
4374 {
4375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02004376 mbedtls_ssl_send_alert_message(
4377 ssl,
4378 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4379 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Jens Wiklander817466c2018-05-22 13:49:31 +02004380 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
4381 }
4382
4383 /*
4384 * struct {
4385 * uint32 ticket_lifetime_hint;
4386 * opaque ticket<0..2^16-1>;
4387 * } NewSessionTicket;
4388 *
4389 * 0 . 3 ticket_lifetime_hint
4390 * 4 . 5 ticket_len (n)
4391 * 6 . 5+n ticket content
4392 */
4393 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
4394 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
4395 {
4396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
4397 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4398 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
4399 return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
4400 }
4401
4402 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
4403
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004404 lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) |
4405 ( msg[2] << 8 ) | ( msg[3] );
Jens Wiklander817466c2018-05-22 13:49:31 +02004406
4407 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
4408
4409 if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
4410 {
4411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
4412 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4413 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
4414 return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
4415 }
4416
Jerome Forissier79013242021-07-28 10:24:04 +02004417 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02004418
4419 /* We're not waiting for a NewSessionTicket message any more */
4420 ssl->handshake->new_session_ticket = 0;
4421 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
4422
4423 /*
4424 * Zero-length ticket means the server changed his mind and doesn't want
4425 * to send a ticket after all, so just forget it
4426 */
4427 if( ticket_len == 0 )
4428 return( 0 );
4429
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004430 if( ssl->session != NULL && ssl->session->ticket != NULL )
4431 {
4432 mbedtls_platform_zeroize( ssl->session->ticket,
4433 ssl->session->ticket_len );
4434 mbedtls_free( ssl->session->ticket );
4435 ssl->session->ticket = NULL;
4436 ssl->session->ticket_len = 0;
4437 }
4438
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004439 mbedtls_platform_zeroize( ssl->session_negotiate->ticket,
4440 ssl->session_negotiate->ticket_len );
Jens Wiklander817466c2018-05-22 13:49:31 +02004441 mbedtls_free( ssl->session_negotiate->ticket );
4442 ssl->session_negotiate->ticket = NULL;
4443 ssl->session_negotiate->ticket_len = 0;
4444
4445 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
4446 {
4447 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
4448 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4449 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
4450 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
4451 }
4452
4453 memcpy( ticket, msg + 6, ticket_len );
4454
4455 ssl->session_negotiate->ticket = ticket;
4456 ssl->session_negotiate->ticket_len = ticket_len;
4457 ssl->session_negotiate->ticket_lifetime = lifetime;
4458
4459 /*
4460 * RFC 5077 section 3.4:
4461 * "If the client receives a session ticket from the server, then it
4462 * discards any Session ID that was sent in the ServerHello."
4463 */
4464 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
4465 ssl->session_negotiate->id_len = 0;
4466
4467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
4468
4469 return( 0 );
4470}
4471#endif /* MBEDTLS_SSL_SESSION_TICKETS */
4472
4473/*
4474 * SSL handshake -- client side -- single step
4475 */
4476int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
4477{
4478 int ret = 0;
4479
4480 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
4481 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4482
4483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
4484
4485 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4486 return( ret );
4487
4488#if defined(MBEDTLS_SSL_PROTO_DTLS)
4489 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4490 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
4491 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004492 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02004493 return( ret );
4494 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004495#endif /* MBEDTLS_SSL_PROTO_DTLS */
Jens Wiklander817466c2018-05-22 13:49:31 +02004496
4497 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
4498 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
4499#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4500 if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
4501 ssl->handshake->new_session_ticket != 0 )
4502 {
4503 ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET;
4504 }
4505#endif
4506
4507 switch( ssl->state )
4508 {
4509 case MBEDTLS_SSL_HELLO_REQUEST:
4510 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
4511 break;
4512
4513 /*
4514 * ==> ClientHello
4515 */
4516 case MBEDTLS_SSL_CLIENT_HELLO:
4517 ret = ssl_write_client_hello( ssl );
4518 break;
4519
4520 /*
4521 * <== ServerHello
4522 * Certificate
4523 * ( ServerKeyExchange )
4524 * ( CertificateRequest )
4525 * ServerHelloDone
4526 */
4527 case MBEDTLS_SSL_SERVER_HELLO:
4528 ret = ssl_parse_server_hello( ssl );
4529 break;
4530
4531 case MBEDTLS_SSL_SERVER_CERTIFICATE:
4532 ret = mbedtls_ssl_parse_certificate( ssl );
4533 break;
4534
4535 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
4536 ret = ssl_parse_server_key_exchange( ssl );
4537 break;
4538
4539 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
4540 ret = ssl_parse_certificate_request( ssl );
4541 break;
4542
4543 case MBEDTLS_SSL_SERVER_HELLO_DONE:
4544 ret = ssl_parse_server_hello_done( ssl );
4545 break;
4546
4547 /*
4548 * ==> ( Certificate/Alert )
4549 * ClientKeyExchange
4550 * ( CertificateVerify )
4551 * ChangeCipherSpec
4552 * Finished
4553 */
4554 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
4555 ret = mbedtls_ssl_write_certificate( ssl );
4556 break;
4557
4558 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
4559 ret = ssl_write_client_key_exchange( ssl );
4560 break;
4561
4562 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
4563 ret = ssl_write_certificate_verify( ssl );
4564 break;
4565
4566 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
4567 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
4568 break;
4569
4570 case MBEDTLS_SSL_CLIENT_FINISHED:
4571 ret = mbedtls_ssl_write_finished( ssl );
4572 break;
4573
4574 /*
4575 * <== ( NewSessionTicket )
4576 * ChangeCipherSpec
4577 * Finished
4578 */
4579#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4580 case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET:
4581 ret = ssl_parse_new_session_ticket( ssl );
4582 break;
4583#endif
4584
4585 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4586 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
4587 break;
4588
4589 case MBEDTLS_SSL_SERVER_FINISHED:
4590 ret = mbedtls_ssl_parse_finished( ssl );
4591 break;
4592
4593 case MBEDTLS_SSL_FLUSH_BUFFERS:
4594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
4595 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
4596 break;
4597
4598 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
4599 mbedtls_ssl_handshake_wrapup( ssl );
4600 break;
4601
4602 default:
4603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
4604 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4605 }
4606
4607 return( ret );
4608}
4609#endif /* MBEDTLS_SSL_CLI_C */