blob: f49178cf41eeebb1c6dc297f3b9ef86117904b9c [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"
36
37#if defined(MBEDTLS_USE_PSA_CRYPTO)
38#include "mbedtls/psa_util.h"
39#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jens Wiklander817466c2018-05-22 13:49:31 +020040
41#include <string.h>
42
43#include <stdint.h>
44
45#if defined(MBEDTLS_HAVE_TIME)
46#include "mbedtls/platform_time.h"
47#endif
48
49#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jens Wiklander3d3b0592019-03-20 15:30:29 +010050#include "mbedtls/platform_util.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020051#endif
52
Jerome Forissier11fa71b2020-04-20 17:17:56 +020053#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
54static int ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
55{
56 if( conf->psk_identity == NULL ||
57 conf->psk_identity_len == 0 )
58 {
59 return( 0 );
60 }
61
62 if( conf->psk != NULL && conf->psk_len != 0 )
63 return( 1 );
64
65#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerome Forissier79013242021-07-28 10:24:04 +020066 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Jerome Forissier11fa71b2020-04-20 17:17:56 +020067 return( 1 );
68#endif /* MBEDTLS_USE_PSA_CRYPTO */
69
70 return( 0 );
71}
72
73#if defined(MBEDTLS_USE_PSA_CRYPTO)
74static int ssl_conf_has_static_raw_psk( mbedtls_ssl_config const *conf )
75{
76 if( conf->psk_identity == NULL ||
77 conf->psk_identity_len == 0 )
78 {
79 return( 0 );
80 }
81
82 if( conf->psk != NULL && conf->psk_len != 0 )
83 return( 1 );
84
85 return( 0 );
86}
87#endif /* MBEDTLS_USE_PSA_CRYPTO */
88
89#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
90
Jens Wiklander817466c2018-05-22 13:49:31 +020091#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Jerome Forissier79013242021-07-28 10:24:04 +020092static int ssl_write_hostname_ext( mbedtls_ssl_context *ssl,
93 unsigned char *buf,
94 const unsigned char *end,
95 size_t *olen )
Jens Wiklander817466c2018-05-22 13:49:31 +020096{
97 unsigned char *p = buf;
Jens Wiklander817466c2018-05-22 13:49:31 +020098 size_t hostname_len;
99
100 *olen = 0;
101
102 if( ssl->hostname == NULL )
Jerome Forissier79013242021-07-28 10:24:04 +0200103 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200104
Jerome Forissier79013242021-07-28 10:24:04 +0200105 MBEDTLS_SSL_DEBUG_MSG( 3,
106 ( "client hello, adding server name extension: %s",
107 ssl->hostname ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200108
109 hostname_len = strlen( ssl->hostname );
110
Jerome Forissier79013242021-07-28 10:24:04 +0200111 MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200112
113 /*
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100114 * Sect. 3, RFC 6066 (TLS Extensions Definitions)
115 *
116 * In order to provide any of the server names, clients MAY include an
117 * extension of type "server_name" in the (extended) client hello. The
118 * "extension_data" field of this extension SHALL contain
119 * "ServerNameList" where:
120 *
Jens Wiklander817466c2018-05-22 13:49:31 +0200121 * struct {
122 * NameType name_type;
123 * select (name_type) {
124 * case host_name: HostName;
125 * } name;
126 * } ServerName;
127 *
128 * enum {
129 * host_name(0), (255)
130 * } NameType;
131 *
132 * opaque HostName<1..2^16-1>;
133 *
134 * struct {
135 * ServerName server_name_list<1..2^16-1>
136 * } ServerNameList;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100137 *
Jens Wiklander817466c2018-05-22 13:49:31 +0200138 */
139 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME >> 8 ) & 0xFF );
140 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME ) & 0xFF );
141
142 *p++ = (unsigned char)( ( (hostname_len + 5) >> 8 ) & 0xFF );
143 *p++ = (unsigned char)( ( (hostname_len + 5) ) & 0xFF );
144
145 *p++ = (unsigned char)( ( (hostname_len + 3) >> 8 ) & 0xFF );
146 *p++ = (unsigned char)( ( (hostname_len + 3) ) & 0xFF );
147
148 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ) & 0xFF );
149 *p++ = (unsigned char)( ( hostname_len >> 8 ) & 0xFF );
150 *p++ = (unsigned char)( ( hostname_len ) & 0xFF );
151
152 memcpy( p, ssl->hostname, hostname_len );
153
154 *olen = hostname_len + 9;
Jerome Forissier79013242021-07-28 10:24:04 +0200155
156 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200157}
158#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
159
160#if defined(MBEDTLS_SSL_RENEGOTIATION)
Jerome Forissier79013242021-07-28 10:24:04 +0200161static int ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
162 unsigned char *buf,
163 const unsigned char *end,
164 size_t *olen )
Jens Wiklander817466c2018-05-22 13:49:31 +0200165{
166 unsigned char *p = buf;
Jens Wiklander817466c2018-05-22 13:49:31 +0200167
168 *olen = 0;
169
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100170 /* We're always including an TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
171 * initial ClientHello, in which case also adding the renegotiation
172 * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
Jens Wiklander817466c2018-05-22 13:49:31 +0200173 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Jerome Forissier79013242021-07-28 10:24:04 +0200174 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200175
Jerome Forissier79013242021-07-28 10:24:04 +0200176 MBEDTLS_SSL_DEBUG_MSG( 3,
177 ( "client hello, adding renegotiation extension" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200178
Jerome Forissier79013242021-07-28 10:24:04 +0200179 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + ssl->verify_data_len );
Jens Wiklander817466c2018-05-22 13:49:31 +0200180
181 /*
182 * Secure renegotiation
183 */
Jerome Forissier79013242021-07-28 10:24:04 +0200184 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 )
185 & 0xFF );
186 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO )
187 & 0xFF );
Jens Wiklander817466c2018-05-22 13:49:31 +0200188
189 *p++ = 0x00;
190 *p++ = ( ssl->verify_data_len + 1 ) & 0xFF;
191 *p++ = ssl->verify_data_len & 0xFF;
192
193 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
194
195 *olen = 5 + ssl->verify_data_len;
Jerome Forissier79013242021-07-28 10:24:04 +0200196
197 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200198}
199#endif /* MBEDTLS_SSL_RENEGOTIATION */
200
201/*
202 * Only if we handle at least one key exchange that needs signatures.
203 */
204#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200205 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerome Forissier79013242021-07-28 10:24:04 +0200206static int ssl_write_signature_algorithms_ext( mbedtls_ssl_context *ssl,
207 unsigned char *buf,
208 const unsigned char *end,
209 size_t *olen )
Jens Wiklander817466c2018-05-22 13:49:31 +0200210{
211 unsigned char *p = buf;
Jens Wiklander817466c2018-05-22 13:49:31 +0200212 size_t sig_alg_len = 0;
213 const int *md;
Jerome Forissier79013242021-07-28 10:24:04 +0200214
Jens Wiklander817466c2018-05-22 13:49:31 +0200215#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C)
216 unsigned char *sig_alg_list = buf + 6;
217#endif
218
219 *olen = 0;
220
221 if( ssl->conf->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
Jerome Forissier79013242021-07-28 10:24:04 +0200222 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200223
Jerome Forissier79013242021-07-28 10:24:04 +0200224 MBEDTLS_SSL_DEBUG_MSG( 3,
225 ( "client hello, adding signature_algorithms extension" ) );
226
227 if( ssl->conf->sig_hashes == NULL )
228 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jens Wiklander817466c2018-05-22 13:49:31 +0200229
230 for( md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
231 {
232#if defined(MBEDTLS_ECDSA_C)
233 sig_alg_len += 2;
234#endif
235#if defined(MBEDTLS_RSA_C)
236 sig_alg_len += 2;
237#endif
Jerome Forissier79013242021-07-28 10:24:04 +0200238 if( sig_alg_len > MBEDTLS_SSL_MAX_SIG_HASH_ALG_LIST_LEN )
239 {
240 MBEDTLS_SSL_DEBUG_MSG( 3,
241 ( "length in bytes of sig-hash-alg extension too big" ) );
242 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
243 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200244 }
245
Jerome Forissier79013242021-07-28 10:24:04 +0200246 /* Empty signature algorithms list, this is a configuration error. */
247 if( sig_alg_len == 0 )
248 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
249
250 MBEDTLS_SSL_CHK_BUF_PTR( p, end, sig_alg_len + 6 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200251
252 /*
253 * Prepare signature_algorithms extension (TLS 1.2)
254 */
255 sig_alg_len = 0;
256
257 for( md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
258 {
259#if defined(MBEDTLS_ECDSA_C)
260 sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg( *md );
261 sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_ECDSA;
262#endif
263#if defined(MBEDTLS_RSA_C)
264 sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg( *md );
265 sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_RSA;
266#endif
267 }
268
269 /*
270 * enum {
271 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
272 * sha512(6), (255)
273 * } HashAlgorithm;
274 *
275 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
276 * SignatureAlgorithm;
277 *
278 * struct {
279 * HashAlgorithm hash;
280 * SignatureAlgorithm signature;
281 * } SignatureAndHashAlgorithm;
282 *
283 * SignatureAndHashAlgorithm
284 * supported_signature_algorithms<2..2^16-2>;
285 */
286 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SIG_ALG >> 8 ) & 0xFF );
287 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SIG_ALG ) & 0xFF );
288
289 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF );
290 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF );
291
292 *p++ = (unsigned char)( ( sig_alg_len >> 8 ) & 0xFF );
293 *p++ = (unsigned char)( ( sig_alg_len ) & 0xFF );
294
295 *olen = 6 + sig_alg_len;
Jerome Forissier79013242021-07-28 10:24:04 +0200296
297 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200298}
299#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200300 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +0200301
302#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
303 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jerome Forissier79013242021-07-28 10:24:04 +0200304static int ssl_write_supported_elliptic_curves_ext( mbedtls_ssl_context *ssl,
305 unsigned char *buf,
306 const unsigned char *end,
307 size_t *olen )
Jens Wiklander817466c2018-05-22 13:49:31 +0200308{
309 unsigned char *p = buf;
Jens Wiklander817466c2018-05-22 13:49:31 +0200310 unsigned char *elliptic_curve_list = p + 6;
311 size_t elliptic_curve_len = 0;
312 const mbedtls_ecp_curve_info *info;
Jens Wiklander817466c2018-05-22 13:49:31 +0200313 const mbedtls_ecp_group_id *grp_id;
Jens Wiklander817466c2018-05-22 13:49:31 +0200314
315 *olen = 0;
316
Jerome Forissier79013242021-07-28 10:24:04 +0200317 MBEDTLS_SSL_DEBUG_MSG( 3,
318 ( "client hello, adding supported_elliptic_curves extension" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200319
Jerome Forissier79013242021-07-28 10:24:04 +0200320 if( ssl->conf->curve_list == NULL )
321 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
322
323 for( grp_id = ssl->conf->curve_list;
324 *grp_id != MBEDTLS_ECP_DP_NONE;
325 grp_id++ )
Jens Wiklander817466c2018-05-22 13:49:31 +0200326 {
Jens Wiklander817466c2018-05-22 13:49:31 +0200327 info = mbedtls_ecp_curve_info_from_grp_id( *grp_id );
Jens Wiklander817466c2018-05-22 13:49:31 +0200328 if( info == NULL )
329 {
Jerome Forissier79013242021-07-28 10:24:04 +0200330 MBEDTLS_SSL_DEBUG_MSG( 1,
331 ( "invalid curve in ssl configuration" ) );
332 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jens Wiklander817466c2018-05-22 13:49:31 +0200333 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200334 elliptic_curve_len += 2;
Jerome Forissier79013242021-07-28 10:24:04 +0200335
336 if( elliptic_curve_len > MBEDTLS_SSL_MAX_CURVE_LIST_LEN )
337 {
338 MBEDTLS_SSL_DEBUG_MSG( 3,
339 ( "malformed supported_elliptic_curves extension in config" ) );
340 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
341 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200342 }
343
Jerome Forissier79013242021-07-28 10:24:04 +0200344 /* Empty elliptic curve list, this is a configuration error. */
345 if( elliptic_curve_len == 0 )
346 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
347
348 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 + elliptic_curve_len );
Jens Wiklander817466c2018-05-22 13:49:31 +0200349
350 elliptic_curve_len = 0;
351
Jerome Forissier79013242021-07-28 10:24:04 +0200352 for( grp_id = ssl->conf->curve_list;
353 *grp_id != MBEDTLS_ECP_DP_NONE;
354 grp_id++ )
Jens Wiklander817466c2018-05-22 13:49:31 +0200355 {
Jens Wiklander817466c2018-05-22 13:49:31 +0200356 info = mbedtls_ecp_curve_info_from_grp_id( *grp_id );
Jens Wiklander817466c2018-05-22 13:49:31 +0200357 elliptic_curve_list[elliptic_curve_len++] = info->tls_id >> 8;
358 elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF;
359 }
360
Jerome Forissier79013242021-07-28 10:24:04 +0200361 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 )
362 & 0xFF );
363 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES )
364 & 0xFF );
Jens Wiklander817466c2018-05-22 13:49:31 +0200365
366 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
367 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
368
369 *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
370 *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
371
372 *olen = 6 + elliptic_curve_len;
Jerome Forissier79013242021-07-28 10:24:04 +0200373
374 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200375}
376
Jerome Forissier79013242021-07-28 10:24:04 +0200377static int ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
378 unsigned char *buf,
379 const unsigned char *end,
380 size_t *olen )
Jens Wiklander817466c2018-05-22 13:49:31 +0200381{
382 unsigned char *p = buf;
Jerome Forissier79013242021-07-28 10:24:04 +0200383 (void) ssl; /* ssl used for debugging only */
Jens Wiklander817466c2018-05-22 13:49:31 +0200384
385 *olen = 0;
386
Jerome Forissier79013242021-07-28 10:24:04 +0200387 MBEDTLS_SSL_DEBUG_MSG( 3,
388 ( "client hello, adding supported_point_formats extension" ) );
389 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200390
Jerome Forissier79013242021-07-28 10:24:04 +0200391 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 )
392 & 0xFF );
393 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS )
394 & 0xFF );
Jens Wiklander817466c2018-05-22 13:49:31 +0200395
396 *p++ = 0x00;
397 *p++ = 2;
398
399 *p++ = 1;
400 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
401
402 *olen = 6;
Jerome Forissier79013242021-07-28 10:24:04 +0200403
404 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200405}
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100406#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Jens Wiklander817466c2018-05-22 13:49:31 +0200407 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
408
409#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jerome Forissier79013242021-07-28 10:24:04 +0200410static int ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
411 unsigned char *buf,
412 const unsigned char *end,
413 size_t *olen )
Jens Wiklander817466c2018-05-22 13:49:31 +0200414{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200415 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200416 unsigned char *p = buf;
Jens Wiklander817466c2018-05-22 13:49:31 +0200417 size_t kkpp_len;
418
419 *olen = 0;
420
421 /* Skip costly extension if we can't use EC J-PAKE anyway */
422 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
Jerome Forissier79013242021-07-28 10:24:04 +0200423 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200424
Jerome Forissier79013242021-07-28 10:24:04 +0200425 MBEDTLS_SSL_DEBUG_MSG( 3,
426 ( "client hello, adding ecjpake_kkpp extension" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200427
Jerome Forissier79013242021-07-28 10:24:04 +0200428 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200429
430 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP >> 8 ) & 0xFF );
431 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ) & 0xFF );
432
433 /*
434 * We may need to send ClientHello multiple times for Hello verification.
435 * We don't want to compute fresh values every time (both for performance
436 * and consistency reasons), so cache the extension content.
437 */
438 if( ssl->handshake->ecjpake_cache == NULL ||
439 ssl->handshake->ecjpake_cache_len == 0 )
440 {
441 MBEDTLS_SSL_DEBUG_MSG( 3, ( "generating new ecjpake parameters" ) );
442
443 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
Jerome Forissier79013242021-07-28 10:24:04 +0200444 p + 2, end - p - 2, &kkpp_len,
445 ssl->conf->f_rng, ssl->conf->p_rng );
Jens Wiklander817466c2018-05-22 13:49:31 +0200446 if( ret != 0 )
447 {
Jerome Forissier79013242021-07-28 10:24:04 +0200448 MBEDTLS_SSL_DEBUG_RET( 1 ,
449 "mbedtls_ecjpake_write_round_one", ret );
450 return( ret );
Jens Wiklander817466c2018-05-22 13:49:31 +0200451 }
452
453 ssl->handshake->ecjpake_cache = mbedtls_calloc( 1, kkpp_len );
454 if( ssl->handshake->ecjpake_cache == NULL )
455 {
456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "allocation failed" ) );
Jerome Forissier79013242021-07-28 10:24:04 +0200457 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Jens Wiklander817466c2018-05-22 13:49:31 +0200458 }
459
460 memcpy( ssl->handshake->ecjpake_cache, p + 2, kkpp_len );
461 ssl->handshake->ecjpake_cache_len = kkpp_len;
462 }
463 else
464 {
465 MBEDTLS_SSL_DEBUG_MSG( 3, ( "re-using cached ecjpake parameters" ) );
466
467 kkpp_len = ssl->handshake->ecjpake_cache_len;
Jerome Forissier79013242021-07-28 10:24:04 +0200468 MBEDTLS_SSL_CHK_BUF_PTR( p + 2, end, kkpp_len );
Jens Wiklander817466c2018-05-22 13:49:31 +0200469
470 memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len );
471 }
472
473 *p++ = (unsigned char)( ( kkpp_len >> 8 ) & 0xFF );
474 *p++ = (unsigned char)( ( kkpp_len ) & 0xFF );
475
476 *olen = kkpp_len + 4;
Jerome Forissier79013242021-07-28 10:24:04 +0200477
478 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200479}
480#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
481
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200482#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Jerome Forissier79013242021-07-28 10:24:04 +0200483static int ssl_write_cid_ext( mbedtls_ssl_context *ssl,
484 unsigned char *buf,
485 const unsigned char *end,
486 size_t *olen )
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200487{
488 unsigned char *p = buf;
489 size_t ext_len;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200490
491 /*
492 * Quoting draft-ietf-tls-dtls-connection-id-05
493 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
494 *
495 * struct {
496 * opaque cid<0..2^8-1>;
497 * } ConnectionId;
498 */
499
500 *olen = 0;
501 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
502 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
503 {
Jerome Forissier79013242021-07-28 10:24:04 +0200504 return( 0 );
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200505 }
506 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding CID extension" ) );
507
508 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
509 * which is at most 255, so the increment cannot overflow. */
Jerome Forissier79013242021-07-28 10:24:04 +0200510 MBEDTLS_SSL_CHK_BUF_PTR( p, end, (unsigned)( ssl->own_cid_len + 5 ) );
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200511
512 /* Add extension ID + size */
513 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID >> 8 ) & 0xFF );
514 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID ) & 0xFF );
515 ext_len = (size_t) ssl->own_cid_len + 1;
516 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
517 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
518
519 *p++ = (uint8_t) ssl->own_cid_len;
520 memcpy( p, ssl->own_cid, ssl->own_cid_len );
521
522 *olen = ssl->own_cid_len + 5;
Jerome Forissier79013242021-07-28 10:24:04 +0200523
524 return( 0 );
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200525}
526#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
527
Jens Wiklander817466c2018-05-22 13:49:31 +0200528#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Jerome Forissier79013242021-07-28 10:24:04 +0200529static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
530 unsigned char *buf,
531 const unsigned char *end,
532 size_t *olen )
Jens Wiklander817466c2018-05-22 13:49:31 +0200533{
534 unsigned char *p = buf;
Jens Wiklander817466c2018-05-22 13:49:31 +0200535
536 *olen = 0;
537
Jerome Forissier79013242021-07-28 10:24:04 +0200538 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
539 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200540
Jerome Forissier79013242021-07-28 10:24:04 +0200541 MBEDTLS_SSL_DEBUG_MSG( 3,
542 ( "client hello, adding max_fragment_length extension" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200543
Jerome Forissier79013242021-07-28 10:24:04 +0200544 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200545
Jerome Forissier79013242021-07-28 10:24:04 +0200546 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 )
547 & 0xFF );
548 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH )
549 & 0xFF );
Jens Wiklander817466c2018-05-22 13:49:31 +0200550
551 *p++ = 0x00;
552 *p++ = 1;
553
554 *p++ = ssl->conf->mfl_code;
555
556 *olen = 5;
Jerome Forissier79013242021-07-28 10:24:04 +0200557
558 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200559}
560#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
561
562#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Jerome Forissier79013242021-07-28 10:24:04 +0200563static int ssl_write_truncated_hmac_ext( mbedtls_ssl_context *ssl,
564 unsigned char *buf,
565 const unsigned char *end,
566 size_t *olen )
Jens Wiklander817466c2018-05-22 13:49:31 +0200567{
568 unsigned char *p = buf;
Jens Wiklander817466c2018-05-22 13:49:31 +0200569
570 *olen = 0;
571
572 if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED )
Jerome Forissier79013242021-07-28 10:24:04 +0200573 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200574
Jerome Forissier79013242021-07-28 10:24:04 +0200575 MBEDTLS_SSL_DEBUG_MSG( 3,
576 ( "client hello, adding truncated_hmac extension" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200577
Jerome Forissier79013242021-07-28 10:24:04 +0200578 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200579
580 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
581 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
582
583 *p++ = 0x00;
584 *p++ = 0x00;
585
586 *olen = 4;
Jerome Forissier79013242021-07-28 10:24:04 +0200587
588 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200589}
590#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
591
592#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Jerome Forissier79013242021-07-28 10:24:04 +0200593static int ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
594 unsigned char *buf,
595 const unsigned char *end,
596 size_t *olen )
Jens Wiklander817466c2018-05-22 13:49:31 +0200597{
598 unsigned char *p = buf;
Jens Wiklander817466c2018-05-22 13:49:31 +0200599
600 *olen = 0;
601
602 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
603 ssl->conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Jerome Forissier79013242021-07-28 10:24:04 +0200604 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200605
Jerome Forissier79013242021-07-28 10:24:04 +0200606 MBEDTLS_SSL_DEBUG_MSG( 3,
607 ( "client hello, adding encrypt_then_mac extension" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200608
Jerome Forissier79013242021-07-28 10:24:04 +0200609 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200610
611 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
612 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
613
614 *p++ = 0x00;
615 *p++ = 0x00;
616
617 *olen = 4;
Jerome Forissier79013242021-07-28 10:24:04 +0200618
619 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200620}
621#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
622
623#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Jerome Forissier79013242021-07-28 10:24:04 +0200624static int ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
625 unsigned char *buf,
626 const unsigned char *end,
627 size_t *olen )
Jens Wiklander817466c2018-05-22 13:49:31 +0200628{
629 unsigned char *p = buf;
Jens Wiklander817466c2018-05-22 13:49:31 +0200630
631 *olen = 0;
632
633 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
634 ssl->conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Jerome Forissier79013242021-07-28 10:24:04 +0200635 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200636
Jerome Forissier79013242021-07-28 10:24:04 +0200637 MBEDTLS_SSL_DEBUG_MSG( 3,
638 ( "client hello, adding extended_master_secret extension" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200639
Jerome Forissier79013242021-07-28 10:24:04 +0200640 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200641
Jerome Forissier79013242021-07-28 10:24:04 +0200642 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET >> 8 )
643 & 0xFF );
644 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET )
645 & 0xFF );
Jens Wiklander817466c2018-05-22 13:49:31 +0200646
647 *p++ = 0x00;
648 *p++ = 0x00;
649
650 *olen = 4;
Jerome Forissier79013242021-07-28 10:24:04 +0200651
652 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200653}
654#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
655
656#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerome Forissier79013242021-07-28 10:24:04 +0200657static int ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
658 unsigned char *buf,
659 const unsigned char *end,
660 size_t *olen )
Jens Wiklander817466c2018-05-22 13:49:31 +0200661{
662 unsigned char *p = buf;
Jens Wiklander817466c2018-05-22 13:49:31 +0200663 size_t tlen = ssl->session_negotiate->ticket_len;
664
665 *olen = 0;
666
667 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED )
Jerome Forissier79013242021-07-28 10:24:04 +0200668 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200669
Jerome Forissier79013242021-07-28 10:24:04 +0200670 MBEDTLS_SSL_DEBUG_MSG( 3,
671 ( "client hello, adding session ticket extension" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200672
Jerome Forissier79013242021-07-28 10:24:04 +0200673 /* The addition is safe here since the ticket length is 16 bit. */
674 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 + tlen );
Jens Wiklander817466c2018-05-22 13:49:31 +0200675
676 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
677 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET ) & 0xFF );
678
679 *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
680 *p++ = (unsigned char)( ( tlen ) & 0xFF );
681
682 *olen = 4;
683
684 if( ssl->session_negotiate->ticket == NULL || tlen == 0 )
Jerome Forissier79013242021-07-28 10:24:04 +0200685 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200686
Jerome Forissier79013242021-07-28 10:24:04 +0200687 MBEDTLS_SSL_DEBUG_MSG( 3,
688 ( "sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200689
690 memcpy( p, ssl->session_negotiate->ticket, tlen );
691
692 *olen += tlen;
Jerome Forissier79013242021-07-28 10:24:04 +0200693
694 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200695}
696#endif /* MBEDTLS_SSL_SESSION_TICKETS */
697
698#if defined(MBEDTLS_SSL_ALPN)
Jerome Forissier79013242021-07-28 10:24:04 +0200699static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
700 unsigned char *buf,
701 const unsigned char *end,
702 size_t *olen )
Jens Wiklander817466c2018-05-22 13:49:31 +0200703{
704 unsigned char *p = buf;
Jens Wiklander817466c2018-05-22 13:49:31 +0200705 size_t alpnlen = 0;
706 const char **cur;
707
708 *olen = 0;
709
710 if( ssl->conf->alpn_list == NULL )
Jerome Forissier79013242021-07-28 10:24:04 +0200711 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200712
713 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
714
715 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
Jerome Forissier79013242021-07-28 10:24:04 +0200716 alpnlen += strlen( *cur ) + 1;
Jens Wiklander817466c2018-05-22 13:49:31 +0200717
Jerome Forissier79013242021-07-28 10:24:04 +0200718 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 + alpnlen );
Jens Wiklander817466c2018-05-22 13:49:31 +0200719
720 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN >> 8 ) & 0xFF );
721 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN ) & 0xFF );
722
723 /*
724 * opaque ProtocolName<1..2^8-1>;
725 *
726 * struct {
727 * ProtocolName protocol_name_list<2..2^16-1>
728 * } ProtocolNameList;
729 */
730
731 /* Skip writing extension and list length for now */
732 p += 4;
733
734 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
735 {
Jerome Forissier79013242021-07-28 10:24:04 +0200736 /*
737 * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
738 * protocol names is less than 255.
739 */
740 *p = (unsigned char)strlen( *cur );
Jens Wiklander817466c2018-05-22 13:49:31 +0200741 memcpy( p + 1, *cur, *p );
742 p += 1 + *p;
743 }
744
745 *olen = p - buf;
746
747 /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
748 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
749 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
750
751 /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
752 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
753 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
Jerome Forissier79013242021-07-28 10:24:04 +0200754
755 return( 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200756}
757#endif /* MBEDTLS_SSL_ALPN */
758
Jerome Forissier79013242021-07-28 10:24:04 +0200759#if defined(MBEDTLS_SSL_DTLS_SRTP)
760static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
761 unsigned char *buf,
762 const unsigned char *end,
763 size_t *olen )
764{
765 unsigned char *p = buf;
766 size_t protection_profiles_index = 0, ext_len = 0;
767 uint16_t mki_len = 0, profile_value = 0;
768
769 *olen = 0;
770
771 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
772 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
773 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
774 {
775 return( 0 );
776 }
777
778 /* RFC 5764 section 4.1.1
779 * uint8 SRTPProtectionProfile[2];
780 *
781 * struct {
782 * SRTPProtectionProfiles SRTPProtectionProfiles;
783 * opaque srtp_mki<0..255>;
784 * } UseSRTPData;
785 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
786 */
787 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
788 {
789 mki_len = ssl->dtls_srtp_info.mki_len;
790 }
791 /* Extension length = 2 bytes for profiles length,
792 * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
793 * 1 byte for srtp_mki vector length and the mki_len value
794 */
795 ext_len = 2 + 2 * ( ssl->conf->dtls_srtp_profile_list_len ) + 1 + mki_len;
796
797 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding use_srtp extension" ) );
798
799 /* Check there is room in the buffer for the extension + 4 bytes
800 * - the extension tag (2 bytes)
801 * - the extension length (2 bytes)
802 */
803 MBEDTLS_SSL_CHK_BUF_PTR( p, end, ext_len + 4 );
804
805 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP >> 8 ) & 0xFF );
806 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP ) & 0xFF );
807
808
809 *p++ = (unsigned char)( ( ( ext_len & 0xFF00 ) >> 8 ) & 0xFF );
810 *p++ = (unsigned char)( ext_len & 0xFF );
811
812 /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
813 /* micro-optimization:
814 * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
815 * which is lower than 127, so the upper byte of the length is always 0
816 * For the documentation, the more generic code is left in comments
817 * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
818 * >> 8 ) & 0xFF );
819 */
820 *p++ = 0;
821 *p++ = (unsigned char)( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
822 & 0xFF );
823
824 for( protection_profiles_index=0;
825 protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
826 protection_profiles_index++ )
827 {
828 profile_value = mbedtls_ssl_check_srtp_profile_value
829 ( ssl->conf->dtls_srtp_profile_list[protection_profiles_index] );
830 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
831 {
832 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_write_use_srtp_ext, add profile: %04x",
833 profile_value ) );
834 *p++ = ( ( profile_value >> 8 ) & 0xFF );
835 *p++ = ( profile_value & 0xFF );
836 }
837 else
838 {
839 /*
840 * Note: we shall never arrive here as protection profiles
841 * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
842 */
843 MBEDTLS_SSL_DEBUG_MSG( 3,
844 ( "client hello, "
845 "illegal DTLS-SRTP protection profile %d",
846 ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
847 ) );
848 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
849 }
850 }
851
852 *p++ = mki_len & 0xFF;
853
854 if( mki_len != 0 )
855 {
856 memcpy( p, ssl->dtls_srtp_info.mki_value, mki_len );
857 /*
858 * Increment p to point to the current position.
859 */
860 p += mki_len;
861 MBEDTLS_SSL_DEBUG_BUF( 3, "sending mki", ssl->dtls_srtp_info.mki_value,
862 ssl->dtls_srtp_info.mki_len );
863 }
864
865 /*
866 * total extension length: extension type (2 bytes)
867 * + extension length (2 bytes)
868 * + protection profile length (2 bytes)
869 * + 2 * number of protection profiles
870 * + srtp_mki vector length(1 byte)
871 * + mki value
872 */
873 *olen = p - buf;
874
875 return( 0 );
876}
877#endif /* MBEDTLS_SSL_DTLS_SRTP */
878
Jens Wiklander817466c2018-05-22 13:49:31 +0200879/*
880 * Generate random bytes for ClientHello
881 */
882static int ssl_generate_random( mbedtls_ssl_context *ssl )
883{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200884 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200885 unsigned char *p = ssl->handshake->randbytes;
886#if defined(MBEDTLS_HAVE_TIME)
887 mbedtls_time_t t;
888#endif
889
890 /*
891 * When responding to a verify request, MUST reuse random (RFC 6347 4.2.1)
892 */
893#if defined(MBEDTLS_SSL_PROTO_DTLS)
894 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
895 ssl->handshake->verify_cookie != NULL )
896 {
897 return( 0 );
898 }
899#endif
900
901#if defined(MBEDTLS_HAVE_TIME)
902 t = mbedtls_time( NULL );
903 *p++ = (unsigned char)( t >> 24 );
904 *p++ = (unsigned char)( t >> 16 );
905 *p++ = (unsigned char)( t >> 8 );
906 *p++ = (unsigned char)( t );
907
Jerome Forissier79013242021-07-28 10:24:04 +0200908 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
909 (long long) t ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200910#else
911 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
912 return( ret );
913
914 p += 4;
915#endif /* MBEDTLS_HAVE_TIME */
916
917 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
918 return( ret );
919
920 return( 0 );
921}
922
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100923/**
924 * \brief Validate cipher suite against config in SSL context.
925 *
926 * \param suite_info cipher suite to validate
927 * \param ssl SSL context
928 * \param min_minor_ver Minimal minor version to accept a cipher suite
929 * \param max_minor_ver Maximal minor version to accept a cipher suite
930 *
931 * \return 0 if valid, else 1
932 */
Jerome Forissier79013242021-07-28 10:24:04 +0200933static int ssl_validate_ciphersuite(
934 const mbedtls_ssl_ciphersuite_t * suite_info,
935 const mbedtls_ssl_context * ssl,
936 int min_minor_ver, int max_minor_ver )
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100937{
938 (void) ssl;
939 if( suite_info == NULL )
940 return( 1 );
941
942 if( suite_info->min_minor_ver > max_minor_ver ||
943 suite_info->max_minor_ver < min_minor_ver )
944 return( 1 );
945
946#if defined(MBEDTLS_SSL_PROTO_DTLS)
947 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
948 ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
949 return( 1 );
950#endif
951
952#if defined(MBEDTLS_ARC4_C)
953 if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED &&
954 suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
955 return( 1 );
956#endif
957
958#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
959 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
960 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
961 return( 1 );
962#endif
963
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200964 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
965#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
966 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
967 ssl_conf_has_static_psk( ssl->conf ) == 0 )
968 {
969 return( 1 );
970 }
971#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
972
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100973 return( 0 );
974}
975
Jens Wiklander817466c2018-05-22 13:49:31 +0200976static int ssl_write_client_hello( mbedtls_ssl_context *ssl )
977{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200978 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200979 size_t i, n, olen, ext_len = 0;
Jerome Forissier79013242021-07-28 10:24:04 +0200980
Jens Wiklander817466c2018-05-22 13:49:31 +0200981 unsigned char *buf;
982 unsigned char *p, *q;
Jerome Forissier79013242021-07-28 10:24:04 +0200983 const unsigned char *end;
984
Jens Wiklander817466c2018-05-22 13:49:31 +0200985 unsigned char offer_compress;
986 const int *ciphersuites;
987 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100988#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
989 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
990 int uses_ec = 0;
991#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200992
993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
994
995 if( ssl->conf->f_rng == NULL )
996 {
997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
998 return( MBEDTLS_ERR_SSL_NO_RNG );
999 }
1000
1001#if defined(MBEDTLS_SSL_RENEGOTIATION)
1002 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
1003#endif
1004 {
1005 ssl->major_ver = ssl->conf->min_major_ver;
1006 ssl->minor_ver = ssl->conf->min_minor_ver;
1007 }
1008
1009 if( ssl->conf->max_major_ver == 0 )
1010 {
Jerome Forissier79013242021-07-28 10:24:04 +02001011 MBEDTLS_SSL_DEBUG_MSG( 1,
1012 ( "configured max major version is invalid, consider using mbedtls_ssl_config_defaults()" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02001013 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1014 }
1015
Jerome Forissier79013242021-07-28 10:24:04 +02001016 buf = ssl->out_msg;
1017 end = buf + MBEDTLS_SSL_OUT_CONTENT_LEN;
1018
Jens Wiklander817466c2018-05-22 13:49:31 +02001019 /*
Jerome Forissier79013242021-07-28 10:24:04 +02001020 * Check if there's enough space for the first part of the ClientHello
1021 * consisting of the 38 bytes described below, the session identifier (at
1022 * most 32 bytes) and its length (1 byte).
1023 *
1024 * Use static upper bounds instead of the actual values
1025 * to allow the compiler to optimize this away.
1026 */
1027 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 38 + 1 + 32 );
1028
1029 /*
1030 * The 38 first bytes of the ClientHello:
1031 * 0 . 0 handshake type (written later)
1032 * 1 . 3 handshake length (written later)
Jens Wiklander817466c2018-05-22 13:49:31 +02001033 * 4 . 5 highest version supported
1034 * 6 . 9 current UNIX time
1035 * 10 . 37 random bytes
Jerome Forissier79013242021-07-28 10:24:04 +02001036 *
1037 * The current UNIX time (4 bytes) and following 28 random bytes are written
1038 * by ssl_generate_random() into ssl->handshake->randbytes buffer and then
1039 * copied from there into the output buffer.
Jens Wiklander817466c2018-05-22 13:49:31 +02001040 */
Jens Wiklander817466c2018-05-22 13:49:31 +02001041
Jerome Forissier79013242021-07-28 10:24:04 +02001042 p = buf + 4;
1043 mbedtls_ssl_write_version( ssl->conf->max_major_ver,
1044 ssl->conf->max_minor_ver,
1045 ssl->conf->transport, p );
Jens Wiklander817466c2018-05-22 13:49:31 +02001046 p += 2;
1047
1048 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
1049 buf[4], buf[5] ) );
1050
1051 if( ( ret = ssl_generate_random( ssl ) ) != 0 )
1052 {
1053 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
1054 return( ret );
1055 }
1056
1057 memcpy( p, ssl->handshake->randbytes, 32 );
1058 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", p, 32 );
1059 p += 32;
1060
1061 /*
1062 * 38 . 38 session id length
1063 * 39 . 39+n session id
1064 * 39+n . 39+n DTLS only: cookie length (1 byte)
Jerome Forissier79013242021-07-28 10:24:04 +02001065 * 40+n . .. DTLS only: cookie
Jens Wiklander817466c2018-05-22 13:49:31 +02001066 * .. . .. ciphersuitelist length (2 bytes)
1067 * .. . .. ciphersuitelist
1068 * .. . .. compression methods length (1 byte)
1069 * .. . .. compression methods
1070 * .. . .. extensions length (2 bytes)
1071 * .. . .. extensions
1072 */
1073 n = ssl->session_negotiate->id_len;
1074
1075 if( n < 16 || n > 32 ||
1076#if defined(MBEDTLS_SSL_RENEGOTIATION)
1077 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
1078#endif
1079 ssl->handshake->resume == 0 )
1080 {
1081 n = 0;
1082 }
1083
1084#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1085 /*
1086 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
1087 * generate and include a Session ID in the TLS ClientHello."
1088 */
1089#if defined(MBEDTLS_SSL_RENEGOTIATION)
1090 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
1091#endif
1092 {
1093 if( ssl->session_negotiate->ticket != NULL &&
1094 ssl->session_negotiate->ticket_len != 0 )
1095 {
Jerome Forissier79013242021-07-28 10:24:04 +02001096 ret = ssl->conf->f_rng( ssl->conf->p_rng,
1097 ssl->session_negotiate->id, 32 );
Jens Wiklander817466c2018-05-22 13:49:31 +02001098
1099 if( ret != 0 )
1100 return( ret );
1101
1102 ssl->session_negotiate->id_len = n = 32;
1103 }
1104 }
1105#endif /* MBEDTLS_SSL_SESSION_TICKETS */
1106
Jerome Forissier79013242021-07-28 10:24:04 +02001107 /*
1108 * The first check of the output buffer size above (
1109 * MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 38 + 1 + 32 );)
1110 * has checked that there is enough space in the output buffer for the
1111 * session identifier length byte and the session identifier (n <= 32).
1112 */
Jens Wiklander817466c2018-05-22 13:49:31 +02001113 *p++ = (unsigned char) n;
1114
1115 for( i = 0; i < n; i++ )
1116 *p++ = ssl->session_negotiate->id[i];
1117
Jerome Forissier79013242021-07-28 10:24:04 +02001118 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02001119 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
1120
1121 /*
Jerome Forissier79013242021-07-28 10:24:04 +02001122 * With 'n' being the length of the session identifier
1123 *
1124 * 39+n . 39+n DTLS only: cookie length (1 byte)
1125 * 40+n . .. DTLS only: cookie
1126 * .. . .. ciphersuitelist length (2 bytes)
1127 * .. . .. ciphersuitelist
1128 * .. . .. compression methods length (1 byte)
1129 * .. . .. compression methods
1130 * .. . .. extensions length (2 bytes)
1131 * .. . .. extensions
1132 */
1133
1134 /*
Jens Wiklander817466c2018-05-22 13:49:31 +02001135 * DTLS cookie
1136 */
1137#if defined(MBEDTLS_SSL_PROTO_DTLS)
1138 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1139 {
Jerome Forissier79013242021-07-28 10:24:04 +02001140 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
1141
Jens Wiklander817466c2018-05-22 13:49:31 +02001142 if( ssl->handshake->verify_cookie == NULL )
1143 {
1144 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no verify cookie to send" ) );
1145 *p++ = 0;
1146 }
1147 else
1148 {
1149 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
1150 ssl->handshake->verify_cookie,
1151 ssl->handshake->verify_cookie_len );
1152
1153 *p++ = ssl->handshake->verify_cookie_len;
Jerome Forissier79013242021-07-28 10:24:04 +02001154
1155 MBEDTLS_SSL_CHK_BUF_PTR( p, end,
1156 ssl->handshake->verify_cookie_len );
Jens Wiklander817466c2018-05-22 13:49:31 +02001157 memcpy( p, ssl->handshake->verify_cookie,
1158 ssl->handshake->verify_cookie_len );
1159 p += ssl->handshake->verify_cookie_len;
1160 }
1161 }
1162#endif
1163
1164 /*
1165 * Ciphersuite list
1166 */
1167 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
1168
1169 /* Skip writing ciphersuite length for now */
1170 n = 0;
1171 q = p;
Jerome Forissier79013242021-07-28 10:24:04 +02001172
1173 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jens Wiklander817466c2018-05-22 13:49:31 +02001174 p += 2;
1175
1176 for( i = 0; ciphersuites[i] != 0; i++ )
1177 {
1178 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] );
1179
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001180 if( ssl_validate_ciphersuite( ciphersuite_info, ssl,
1181 ssl->conf->min_minor_ver,
1182 ssl->conf->max_minor_ver ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02001183 continue;
1184
Jerome Forissier79013242021-07-28 10:24:04 +02001185 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %#04x (%s)",
1186 (unsigned int)ciphersuites[i], ciphersuite_info->name ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02001187
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001188#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
1189 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1190 uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info );
1191#endif
1192
Jerome Forissier79013242021-07-28 10:24:04 +02001193 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1194
Jens Wiklander817466c2018-05-22 13:49:31 +02001195 n++;
1196 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
1197 *p++ = (unsigned char)( ciphersuites[i] );
1198 }
1199
Jerome Forissier79013242021-07-28 10:24:04 +02001200 MBEDTLS_SSL_DEBUG_MSG( 3,
1201 ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites (excluding SCSVs)", n ) );
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001202
Jens Wiklander817466c2018-05-22 13:49:31 +02001203 /*
1204 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1205 */
1206#if defined(MBEDTLS_SSL_RENEGOTIATION)
1207 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
1208#endif
1209 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001210 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02001211 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jens Wiklander817466c2018-05-22 13:49:31 +02001212 *p++ = (unsigned char)( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
1213 *p++ = (unsigned char)( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO );
1214 n++;
1215 }
1216
1217 /* Some versions of OpenSSL don't handle it correctly if not at end */
1218#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
1219 if( ssl->conf->fallback == MBEDTLS_SSL_IS_FALLBACK )
1220 {
1221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02001222
1223 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jens Wiklander817466c2018-05-22 13:49:31 +02001224 *p++ = (unsigned char)( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 );
1225 *p++ = (unsigned char)( MBEDTLS_SSL_FALLBACK_SCSV_VALUE );
1226 n++;
1227 }
1228#endif
1229
1230 *q++ = (unsigned char)( n >> 7 );
1231 *q++ = (unsigned char)( n << 1 );
1232
Jens Wiklander817466c2018-05-22 13:49:31 +02001233#if defined(MBEDTLS_ZLIB_SUPPORT)
1234 offer_compress = 1;
1235#else
1236 offer_compress = 0;
1237#endif
1238
1239 /*
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001240 * We don't support compression with DTLS right now: if many records come
Jens Wiklander817466c2018-05-22 13:49:31 +02001241 * in the same datagram, uncompressing one could overwrite the next one.
1242 * We don't want to add complexity for handling that case unless there is
1243 * an actual need for it.
1244 */
1245#if defined(MBEDTLS_SSL_PROTO_DTLS)
1246 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1247 offer_compress = 0;
1248#endif
1249
1250 if( offer_compress )
1251 {
1252 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
1253 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
Jerome Forissier79013242021-07-28 10:24:04 +02001254 MBEDTLS_SSL_COMPRESS_DEFLATE,
1255 MBEDTLS_SSL_COMPRESS_NULL ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02001256
Jerome Forissier79013242021-07-28 10:24:04 +02001257 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
Jens Wiklander817466c2018-05-22 13:49:31 +02001258 *p++ = 2;
1259 *p++ = MBEDTLS_SSL_COMPRESS_DEFLATE;
1260 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
1261 }
1262 else
1263 {
1264 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
1265 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d",
1266 MBEDTLS_SSL_COMPRESS_NULL ) );
1267
Jerome Forissier79013242021-07-28 10:24:04 +02001268 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
Jens Wiklander817466c2018-05-22 13:49:31 +02001269 *p++ = 1;
1270 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
1271 }
1272
Jerome Forissier79013242021-07-28 10:24:04 +02001273 /* First write extensions, then the total length */
1274
1275 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
1276
Jens Wiklander817466c2018-05-22 13:49:31 +02001277#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Jerome Forissier79013242021-07-28 10:24:04 +02001278 if( ( ret = ssl_write_hostname_ext( ssl, p + 2 + ext_len,
1279 end, &olen ) ) != 0 )
1280 {
1281 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_hostname_ext", ret );
1282 return( ret );
1283 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001284 ext_len += olen;
1285#endif
1286
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001287 /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
1288 * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
Jens Wiklander817466c2018-05-22 13:49:31 +02001289#if defined(MBEDTLS_SSL_RENEGOTIATION)
Jerome Forissier79013242021-07-28 10:24:04 +02001290 if( ( ret = ssl_write_renegotiation_ext( ssl, p + 2 + ext_len,
1291 end, &olen ) ) != 0 )
1292 {
1293 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_renegotiation_ext", ret );
1294 return( ret );
1295 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001296 ext_len += olen;
1297#endif
1298
1299#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001300 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerome Forissier79013242021-07-28 10:24:04 +02001301 if( ( ret = ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len,
1302 end, &olen ) ) != 0 )
1303 {
1304 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_signature_algorithms_ext", ret );
1305 return( ret );
1306 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001307 ext_len += olen;
1308#endif
1309
1310#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
1311 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001312 if( uses_ec )
1313 {
Jerome Forissier79013242021-07-28 10:24:04 +02001314 if( ( ret = ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len,
1315 end, &olen ) ) != 0 )
1316 {
1317 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_elliptic_curves_ext", ret );
1318 return( ret );
1319 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001320 ext_len += olen;
Jens Wiklander817466c2018-05-22 13:49:31 +02001321
Jerome Forissier79013242021-07-28 10:24:04 +02001322 if( ( ret = ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len,
1323 end, &olen ) ) != 0 )
1324 {
1325 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_point_formats_ext", ret );
1326 return( ret );
1327 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001328 ext_len += olen;
1329 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001330#endif
1331
1332#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jerome Forissier79013242021-07-28 10:24:04 +02001333 if( ( ret = ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len,
1334 end, &olen ) ) != 0 )
1335 {
1336 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_ecjpake_kkpp_ext", ret );
1337 return( ret );
1338 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001339 ext_len += olen;
1340#endif
1341
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001342#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Jerome Forissier79013242021-07-28 10:24:04 +02001343 if( ( ret = ssl_write_cid_ext( ssl, p + 2 + ext_len, end, &olen ) ) != 0 )
1344 {
1345 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_cid_ext", ret );
1346 return( ret );
1347 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001348 ext_len += olen;
1349#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1350
Jens Wiklander817466c2018-05-22 13:49:31 +02001351#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Jerome Forissier79013242021-07-28 10:24:04 +02001352 if( ( ret = ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len,
1353 end, &olen ) ) != 0 )
1354 {
1355 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret );
1356 return( ret );
1357 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001358 ext_len += olen;
1359#endif
1360
1361#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Jerome Forissier79013242021-07-28 10:24:04 +02001362 if( ( ret = ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len,
1363 end, &olen ) ) != 0 )
1364 {
1365 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_truncated_hmac_ext", ret );
1366 return( ret );
1367 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001368 ext_len += olen;
1369#endif
1370
1371#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Jerome Forissier79013242021-07-28 10:24:04 +02001372 if( ( ret = ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len,
1373 end, &olen ) ) != 0 )
1374 {
1375 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_encrypt_then_mac_ext", ret );
1376 return( ret );
1377 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001378 ext_len += olen;
1379#endif
1380
1381#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Jerome Forissier79013242021-07-28 10:24:04 +02001382 if( ( ret = ssl_write_extended_ms_ext( ssl, p + 2 + ext_len,
1383 end, &olen ) ) != 0 )
1384 {
1385 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_extended_ms_ext", ret );
1386 return( ret );
1387 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001388 ext_len += olen;
1389#endif
1390
1391#if defined(MBEDTLS_SSL_ALPN)
Jerome Forissier79013242021-07-28 10:24:04 +02001392 if( ( ret = ssl_write_alpn_ext( ssl, p + 2 + ext_len,
1393 end, &olen ) ) != 0 )
1394 {
1395 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_alpn_ext", ret );
1396 return( ret );
1397 }
1398 ext_len += olen;
1399#endif
1400
1401#if defined(MBEDTLS_SSL_DTLS_SRTP)
1402 if( ( ret = ssl_write_use_srtp_ext( ssl, p + 2 + ext_len,
1403 end, &olen ) ) != 0 )
1404 {
1405 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_use_srtp_ext", ret );
1406 return( ret );
1407 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001408 ext_len += olen;
1409#endif
1410
1411#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerome Forissier79013242021-07-28 10:24:04 +02001412 if( ( ret = ssl_write_session_ticket_ext( ssl, p + 2 + ext_len,
1413 end, &olen ) ) != 0 )
1414 {
1415 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_session_ticket_ext", ret );
1416 return( ret );
1417 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001418 ext_len += olen;
1419#endif
1420
1421 /* olen unused if all extensions are disabled */
1422 ((void) olen);
1423
Jerome Forissier79013242021-07-28 10:24:04 +02001424 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
1425 ext_len ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02001426
1427 if( ext_len > 0 )
1428 {
Jerome Forissier79013242021-07-28 10:24:04 +02001429 /* No need to check for space here, because the extension
1430 * writing functions already took care of that. */
Jens Wiklander817466c2018-05-22 13:49:31 +02001431 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1432 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1433 p += ext_len;
1434 }
1435
1436 ssl->out_msglen = p - buf;
1437 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
1438 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_HELLO;
1439
1440 ssl->state++;
1441
1442#if defined(MBEDTLS_SSL_PROTO_DTLS)
1443 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1444 mbedtls_ssl_send_flight_completed( ssl );
1445#endif
1446
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001447 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02001448 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001449 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02001450 return( ret );
1451 }
1452
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001453#if defined(MBEDTLS_SSL_PROTO_DTLS)
1454 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1455 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
1456 {
1457 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
1458 return( ret );
1459 }
1460#endif /* MBEDTLS_SSL_PROTO_DTLS */
1461
Jens Wiklander817466c2018-05-22 13:49:31 +02001462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
1463
1464 return( 0 );
1465}
1466
1467static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
1468 const unsigned char *buf,
1469 size_t len )
1470{
1471#if defined(MBEDTLS_SSL_RENEGOTIATION)
1472 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
1473 {
1474 /* Check verify-data in constant-time. The length OTOH is no secret */
1475 if( len != 1 + ssl->verify_data_len * 2 ||
1476 buf[0] != ssl->verify_data_len * 2 ||
1477 mbedtls_ssl_safer_memcmp( buf + 1,
1478 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
1479 mbedtls_ssl_safer_memcmp( buf + 1 + ssl->verify_data_len,
1480 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
1481 {
1482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02001483 mbedtls_ssl_send_alert_message(
1484 ssl,
1485 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1486 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Jens Wiklander817466c2018-05-22 13:49:31 +02001487 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1488 }
1489 }
1490 else
1491#endif /* MBEDTLS_SSL_RENEGOTIATION */
1492 {
1493 if( len != 1 || buf[0] != 0x00 )
1494 {
Jerome Forissier79013242021-07-28 10:24:04 +02001495 MBEDTLS_SSL_DEBUG_MSG( 1,
1496 ( "non-zero length renegotiation info" ) );
1497 mbedtls_ssl_send_alert_message(
1498 ssl,
1499 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1500 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Jens Wiklander817466c2018-05-22 13:49:31 +02001501 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1502 }
1503
1504 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
1505 }
1506
1507 return( 0 );
1508}
1509
1510#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1511static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
1512 const unsigned char *buf,
1513 size_t len )
1514{
1515 /*
1516 * server should use the extension only if we did,
1517 * and if so the server's value should match ours (and len is always 1)
1518 */
1519 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
1520 len != 1 ||
1521 buf[0] != ssl->conf->mfl_code )
1522 {
Jerome Forissier79013242021-07-28 10:24:04 +02001523 MBEDTLS_SSL_DEBUG_MSG( 1,
1524 ( "non-matching max fragment length extension" ) );
1525 mbedtls_ssl_send_alert_message(
1526 ssl,
1527 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1528 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Jens Wiklander817466c2018-05-22 13:49:31 +02001529 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1530 }
1531
1532 return( 0 );
1533}
1534#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
1535
1536#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1537static int ssl_parse_truncated_hmac_ext( mbedtls_ssl_context *ssl,
1538 const unsigned char *buf,
1539 size_t len )
1540{
1541 if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED ||
1542 len != 0 )
1543 {
Jerome Forissier79013242021-07-28 10:24:04 +02001544 MBEDTLS_SSL_DEBUG_MSG( 1,
1545 ( "non-matching truncated HMAC extension" ) );
1546 mbedtls_ssl_send_alert_message(
1547 ssl,
1548 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1549 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Jens Wiklander817466c2018-05-22 13:49:31 +02001550 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1551 }
1552
1553 ((void) buf);
1554
1555 ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED;
1556
1557 return( 0 );
1558}
1559#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1560
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001561#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1562static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
1563 const unsigned char *buf,
1564 size_t len )
1565{
1566 size_t peer_cid_len;
1567
1568 if( /* CID extension only makes sense in DTLS */
1569 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
1570 /* The server must only send the CID extension if we have offered it. */
1571 ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
1572 {
1573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension unexpected" ) );
1574 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Jerome Forissier79013242021-07-28 10:24:04 +02001575 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001576 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1577 }
1578
1579 if( len == 0 )
1580 {
1581 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
1582 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1583 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1584 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1585 }
1586
1587 peer_cid_len = *buf++;
1588 len--;
1589
1590 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
1591 {
1592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
1593 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1594 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1595 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1596 }
1597
1598 if( len != peer_cid_len )
1599 {
1600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
1601 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1602 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
1603 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1604 }
1605
1606 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
1607 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
1608 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
1609
1610 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
1611 MBEDTLS_SSL_DEBUG_BUF( 3, "Server CID", buf, peer_cid_len );
1612
1613 return( 0 );
1614}
1615#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1616
Jens Wiklander817466c2018-05-22 13:49:31 +02001617#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1618static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
1619 const unsigned char *buf,
1620 size_t len )
1621{
1622 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
1623 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1624 len != 0 )
1625 {
Jerome Forissier79013242021-07-28 10:24:04 +02001626 MBEDTLS_SSL_DEBUG_MSG( 1,
1627 ( "non-matching encrypt-then-MAC extension" ) );
1628 mbedtls_ssl_send_alert_message(
1629 ssl,
1630 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1631 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Jens Wiklander817466c2018-05-22 13:49:31 +02001632 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1633 }
1634
1635 ((void) buf);
1636
1637 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
1638
1639 return( 0 );
1640}
1641#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
1642
1643#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1644static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
1645 const unsigned char *buf,
1646 size_t len )
1647{
1648 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
1649 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1650 len != 0 )
1651 {
Jerome Forissier79013242021-07-28 10:24:04 +02001652 MBEDTLS_SSL_DEBUG_MSG( 1,
1653 ( "non-matching extended master secret extension" ) );
1654 mbedtls_ssl_send_alert_message(
1655 ssl,
1656 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1657 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Jens Wiklander817466c2018-05-22 13:49:31 +02001658 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1659 }
1660
1661 ((void) buf);
1662
1663 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
1664
1665 return( 0 );
1666}
1667#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1668
1669#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1670static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
1671 const unsigned char *buf,
1672 size_t len )
1673{
1674 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
1675 len != 0 )
1676 {
Jerome Forissier79013242021-07-28 10:24:04 +02001677 MBEDTLS_SSL_DEBUG_MSG( 1,
1678 ( "non-matching session ticket extension" ) );
1679 mbedtls_ssl_send_alert_message(
1680 ssl,
1681 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1682 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Jens Wiklander817466c2018-05-22 13:49:31 +02001683 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1684 }
1685
1686 ((void) buf);
1687
1688 ssl->handshake->new_session_ticket = 1;
1689
1690 return( 0 );
1691}
1692#endif /* MBEDTLS_SSL_SESSION_TICKETS */
1693
1694#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
1695 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1696static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl,
1697 const unsigned char *buf,
1698 size_t len )
1699{
1700 size_t list_size;
1701 const unsigned char *p;
1702
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001703 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Jens Wiklander817466c2018-05-22 13:49:31 +02001704 {
1705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1706 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1707 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1708 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1709 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001710 list_size = buf[0];
Jens Wiklander817466c2018-05-22 13:49:31 +02001711
1712 p = buf + 1;
1713 while( list_size > 0 )
1714 {
1715 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
1716 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
1717 {
1718#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
1719 ssl->handshake->ecdh_ctx.point_format = p[0];
1720#endif
1721#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1722 ssl->handshake->ecjpake_ctx.point_format = p[0];
1723#endif
1724 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
1725 return( 0 );
1726 }
1727
1728 list_size--;
1729 p++;
1730 }
1731
1732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
1733 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1734 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1735 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1736}
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001737#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
Jens Wiklander817466c2018-05-22 13:49:31 +02001738 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1739
1740#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1741static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
1742 const unsigned char *buf,
1743 size_t len )
1744{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001745 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001746
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001747 if( ssl->handshake->ciphersuite_info->key_exchange !=
Jens Wiklander817466c2018-05-22 13:49:31 +02001748 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
1749 {
1750 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
1751 return( 0 );
1752 }
1753
1754 /* If we got here, we no longer need our cached extension */
1755 mbedtls_free( ssl->handshake->ecjpake_cache );
1756 ssl->handshake->ecjpake_cache = NULL;
1757 ssl->handshake->ecjpake_cache_len = 0;
1758
1759 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
1760 buf, len ) ) != 0 )
1761 {
1762 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
Jerome Forissier79013242021-07-28 10:24:04 +02001763 mbedtls_ssl_send_alert_message(
1764 ssl,
1765 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1766 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Jens Wiklander817466c2018-05-22 13:49:31 +02001767 return( ret );
1768 }
1769
1770 return( 0 );
1771}
1772#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1773
1774#if defined(MBEDTLS_SSL_ALPN)
1775static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
1776 const unsigned char *buf, size_t len )
1777{
1778 size_t list_len, name_len;
1779 const char **p;
1780
1781 /* If we didn't send it, the server shouldn't send it */
1782 if( ssl->conf->alpn_list == NULL )
1783 {
1784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02001785 mbedtls_ssl_send_alert_message(
1786 ssl,
1787 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1788 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
Jens Wiklander817466c2018-05-22 13:49:31 +02001789 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1790 }
1791
1792 /*
1793 * opaque ProtocolName<1..2^8-1>;
1794 *
1795 * struct {
1796 * ProtocolName protocol_name_list<2..2^16-1>
1797 * } ProtocolNameList;
1798 *
1799 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
1800 */
1801
1802 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
1803 if( len < 4 )
1804 {
1805 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1806 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1807 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1808 }
1809
1810 list_len = ( buf[0] << 8 ) | buf[1];
1811 if( list_len != len - 2 )
1812 {
1813 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1814 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1815 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1816 }
1817
1818 name_len = buf[2];
1819 if( name_len != list_len - 1 )
1820 {
1821 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1822 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1823 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1824 }
1825
1826 /* Check that the server chosen protocol was in our list and save it */
1827 for( p = ssl->conf->alpn_list; *p != NULL; p++ )
1828 {
1829 if( name_len == strlen( *p ) &&
1830 memcmp( buf + 3, *p, name_len ) == 0 )
1831 {
1832 ssl->alpn_chosen = *p;
1833 return( 0 );
1834 }
1835 }
1836
1837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) );
1838 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1839 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1840 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1841}
1842#endif /* MBEDTLS_SSL_ALPN */
1843
Jerome Forissier79013242021-07-28 10:24:04 +02001844#if defined(MBEDTLS_SSL_DTLS_SRTP)
1845static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
1846 const unsigned char *buf,
1847 size_t len )
1848{
1849 mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
1850 size_t i, mki_len = 0;
1851 uint16_t server_protection_profile_value = 0;
1852
1853 /* If use_srtp is not configured, just ignore the extension */
1854 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
1855 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
1856 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
1857 return( 0 );
1858
1859 /* RFC 5764 section 4.1.1
1860 * uint8 SRTPProtectionProfile[2];
1861 *
1862 * struct {
1863 * SRTPProtectionProfiles SRTPProtectionProfiles;
1864 * opaque srtp_mki<0..255>;
1865 * } UseSRTPData;
1866
1867 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
1868 *
1869 */
1870 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
1871 {
1872 mki_len = ssl->dtls_srtp_info.mki_len;
1873 }
1874
1875 /*
1876 * Length is 5 + optional mki_value : one protection profile length (2 bytes)
1877 * + protection profile (2 bytes)
1878 * + mki_len(1 byte)
1879 * and optional srtp_mki
1880 */
1881 if( ( len < 5 ) || ( len != ( buf[4] + 5u ) ) )
1882 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1883
1884 /*
1885 * get the server protection profile
1886 */
1887
1888 /*
1889 * protection profile length must be 0x0002 as we must have only
1890 * one protection profile in server Hello
1891 */
1892 if( ( buf[0] != 0 ) || ( buf[1] != 2 ) )
1893 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1894
1895 server_protection_profile_value = ( buf[2] << 8 ) | buf[3];
1896 server_protection = mbedtls_ssl_check_srtp_profile_value(
1897 server_protection_profile_value );
1898 if( server_protection != MBEDTLS_TLS_SRTP_UNSET )
1899 {
1900 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
1901 mbedtls_ssl_get_srtp_profile_as_string(
1902 server_protection ) ) );
1903 }
1904
1905 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
1906
1907 /*
1908 * Check we have the server profile in our list
1909 */
1910 for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
1911 {
1912 if( server_protection == ssl->conf->dtls_srtp_profile_list[i] )
1913 {
1914 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
1915 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
1916 mbedtls_ssl_get_srtp_profile_as_string(
1917 server_protection ) ) );
1918 break;
1919 }
1920 }
1921
1922 /* If no match was found : server problem, it shall never answer with incompatible profile */
1923 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
1924 {
1925 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1926 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1927 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1928 }
1929
1930 /* If server does not use mki in its reply, make sure the client won't keep
1931 * one as negotiated */
1932 if( len == 5 )
1933 {
1934 ssl->dtls_srtp_info.mki_len = 0;
1935 }
1936
1937 /*
1938 * RFC5764:
1939 * If the client detects a nonzero-length MKI in the server's response
1940 * that is different than the one the client offered, then the client
1941 * MUST abort the handshake and SHOULD send an invalid_parameter alert.
1942 */
1943 if( len > 5 && ( buf[4] != mki_len ||
1944 ( memcmp( ssl->dtls_srtp_info.mki_value, &buf[5], mki_len ) ) ) )
1945 {
1946 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1947 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
1948 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1949 }
1950#if defined (MBEDTLS_DEBUG_C)
1951 if( len > 5 )
1952 {
1953 MBEDTLS_SSL_DEBUG_BUF( 3, "received mki", ssl->dtls_srtp_info.mki_value,
1954 ssl->dtls_srtp_info.mki_len );
1955 }
1956#endif
1957 return( 0 );
1958}
1959#endif /* MBEDTLS_SSL_DTLS_SRTP */
1960
Jens Wiklander817466c2018-05-22 13:49:31 +02001961/*
1962 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1963 */
1964#if defined(MBEDTLS_SSL_PROTO_DTLS)
1965static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl )
1966{
1967 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
1968 int major_ver, minor_ver;
1969 unsigned char cookie_len;
1970
1971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
1972
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001973 /* Check that there is enough room for:
1974 * - 2 bytes of version
1975 * - 1 byte of cookie_len
1976 */
1977 if( mbedtls_ssl_hs_hdr_len( ssl ) + 3 > ssl->in_msglen )
1978 {
1979 MBEDTLS_SSL_DEBUG_MSG( 1,
1980 ( "incoming HelloVerifyRequest message is too short" ) );
1981 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1982 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1983 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1984 }
1985
Jens Wiklander817466c2018-05-22 13:49:31 +02001986 /*
1987 * struct {
1988 * ProtocolVersion server_version;
1989 * opaque cookie<0..2^8-1>;
1990 * } HelloVerifyRequest;
1991 */
1992 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
1993 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, p );
1994 p += 2;
1995
1996 /*
1997 * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1)
1998 * even is lower than our min version.
1999 */
2000 if( major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
2001 minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ||
2002 major_ver > ssl->conf->max_major_ver ||
2003 minor_ver > ssl->conf->max_minor_ver )
2004 {
2005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) );
2006
2007 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2008 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
2009
2010 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
2011 }
2012
2013 cookie_len = *p++;
Jens Wiklander817466c2018-05-22 13:49:31 +02002014 if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len )
2015 {
2016 MBEDTLS_SSL_DEBUG_MSG( 1,
2017 ( "cookie length does not match incoming message size" ) );
2018 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2019 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2020 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
2021 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002022 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len );
Jens Wiklander817466c2018-05-22 13:49:31 +02002023
2024 mbedtls_free( ssl->handshake->verify_cookie );
2025
2026 ssl->handshake->verify_cookie = mbedtls_calloc( 1, cookie_len );
2027 if( ssl->handshake->verify_cookie == NULL )
2028 {
2029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) );
2030 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2031 }
2032
2033 memcpy( ssl->handshake->verify_cookie, p, cookie_len );
2034 ssl->handshake->verify_cookie_len = cookie_len;
2035
2036 /* Start over at ClientHello */
2037 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
2038 mbedtls_ssl_reset_checksum( ssl );
2039
2040 mbedtls_ssl_recv_flight_completed( ssl );
2041
2042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
2043
2044 return( 0 );
2045}
2046#endif /* MBEDTLS_SSL_PROTO_DTLS */
2047
2048static int ssl_parse_server_hello( mbedtls_ssl_context *ssl )
2049{
2050 int ret, i;
2051 size_t n;
2052 size_t ext_len;
2053 unsigned char *buf, *ext;
2054 unsigned char comp;
2055#if defined(MBEDTLS_ZLIB_SUPPORT)
2056 int accept_comp;
2057#endif
2058#if defined(MBEDTLS_SSL_RENEGOTIATION)
2059 int renegotiation_info_seen = 0;
2060#endif
2061 int handshake_failure = 0;
2062 const mbedtls_ssl_ciphersuite_t *suite_info;
Jens Wiklander817466c2018-05-22 13:49:31 +02002063
2064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
2065
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002066 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02002067 {
2068 /* No alert on a read error. */
2069 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2070 return( ret );
2071 }
2072
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002073 buf = ssl->in_msg;
2074
Jens Wiklander817466c2018-05-22 13:49:31 +02002075 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2076 {
2077#if defined(MBEDTLS_SSL_RENEGOTIATION)
2078 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
2079 {
2080 ssl->renego_records_seen++;
2081
2082 if( ssl->conf->renego_max_records >= 0 &&
2083 ssl->renego_records_seen > ssl->conf->renego_max_records )
2084 {
Jerome Forissier79013242021-07-28 10:24:04 +02002085 MBEDTLS_SSL_DEBUG_MSG( 1,
2086 ( "renegotiation requested, but not honored by server" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002087 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2088 }
2089
Jerome Forissier79013242021-07-28 10:24:04 +02002090 MBEDTLS_SSL_DEBUG_MSG( 1,
2091 ( "non-handshake message during renegotiation" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002092
2093 ssl->keep_current_message = 1;
2094 return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
2095 }
2096#endif /* MBEDTLS_SSL_RENEGOTIATION */
2097
2098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02002099 mbedtls_ssl_send_alert_message(
2100 ssl,
2101 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2102 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Jens Wiklander817466c2018-05-22 13:49:31 +02002103 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2104 }
2105
2106#if defined(MBEDTLS_SSL_PROTO_DTLS)
2107 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2108 {
2109 if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
2110 {
2111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
2112 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
2113 return( ssl_parse_hello_verify_request( ssl ) );
2114 }
2115 else
2116 {
2117 /* We made it through the verification process */
2118 mbedtls_free( ssl->handshake->verify_cookie );
2119 ssl->handshake->verify_cookie = NULL;
2120 ssl->handshake->verify_cookie_len = 0;
2121 }
2122 }
2123#endif /* MBEDTLS_SSL_PROTO_DTLS */
2124
2125 if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) ||
2126 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO )
2127 {
2128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
2129 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2130 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2131 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
2132 }
2133
2134 /*
2135 * 0 . 1 server_version
2136 * 2 . 33 random (maybe including 4 bytes of Unix time)
2137 * 34 . 34 session_id length = n
2138 * 35 . 34+n session_id
2139 * 35+n . 36+n cipher_suite
2140 * 37+n . 37+n compression_method
2141 *
2142 * 38+n . 39+n extensions length (optional)
2143 * 40+n . .. extensions
2144 */
2145 buf += mbedtls_ssl_hs_hdr_len( ssl );
2146
2147 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 );
2148 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
2149 ssl->conf->transport, buf + 0 );
2150
2151 if( ssl->major_ver < ssl->conf->min_major_ver ||
2152 ssl->minor_ver < ssl->conf->min_minor_ver ||
2153 ssl->major_ver > ssl->conf->max_major_ver ||
2154 ssl->minor_ver > ssl->conf->max_minor_ver )
2155 {
Jerome Forissier79013242021-07-28 10:24:04 +02002156 MBEDTLS_SSL_DEBUG_MSG( 1,
2157 ( "server version out of bounds - min: [%d:%d], server: [%d:%d], max: [%d:%d]",
2158 ssl->conf->min_major_ver,
2159 ssl->conf->min_minor_ver,
2160 ssl->major_ver, ssl->minor_ver,
2161 ssl->conf->max_major_ver,
2162 ssl->conf->max_minor_ver ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002163
2164 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2165 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
2166
2167 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
2168 }
2169
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002170 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu",
Jerome Forissier79013242021-07-28 10:24:04 +02002171 ( (unsigned long) buf[2] << 24 ) |
2172 ( (unsigned long) buf[3] << 16 ) |
2173 ( (unsigned long) buf[4] << 8 ) |
2174 ( (unsigned long) buf[5] ) ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002175
2176 memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
2177
2178 n = buf[34];
2179
2180 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
2181
2182 if( n > 32 )
2183 {
2184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
2185 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2186 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2187 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
2188 }
2189
2190 if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n )
2191 {
2192 ext_len = ( ( buf[38 + n] << 8 )
2193 | ( buf[39 + n] ) );
2194
2195 if( ( ext_len > 0 && ext_len < 4 ) ||
2196 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
2197 {
2198 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02002199 mbedtls_ssl_send_alert_message(
2200 ssl,
2201 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2202 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Jens Wiklander817466c2018-05-22 13:49:31 +02002203 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
2204 }
2205 }
2206 else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n )
2207 {
2208 ext_len = 0;
2209 }
2210 else
2211 {
2212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
2213 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2214 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
2215 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
2216 }
2217
2218 /* ciphersuite (used later) */
2219 i = ( buf[35 + n] << 8 ) | buf[36 + n];
2220
2221 /*
2222 * Read and check compression
2223 */
2224 comp = buf[37 + n];
2225
2226#if defined(MBEDTLS_ZLIB_SUPPORT)
2227 /* See comments in ssl_write_client_hello() */
2228#if defined(MBEDTLS_SSL_PROTO_DTLS)
2229 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2230 accept_comp = 0;
2231 else
2232#endif
2233 accept_comp = 1;
2234
2235 if( comp != MBEDTLS_SSL_COMPRESS_NULL &&
2236 ( comp != MBEDTLS_SSL_COMPRESS_DEFLATE || accept_comp == 0 ) )
2237#else /* MBEDTLS_ZLIB_SUPPORT */
2238 if( comp != MBEDTLS_SSL_COMPRESS_NULL )
2239#endif/* MBEDTLS_ZLIB_SUPPORT */
2240 {
Jerome Forissier79013242021-07-28 10:24:04 +02002241 MBEDTLS_SSL_DEBUG_MSG( 1,
2242 ( "server hello, bad compression: %d", comp ) );
2243 mbedtls_ssl_send_alert_message(
2244 ssl,
2245 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2246 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Jens Wiklander817466c2018-05-22 13:49:31 +02002247 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2248 }
2249
2250 /*
2251 * Initialize update checksum functions
2252 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002253 ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i );
2254 if( ssl->handshake->ciphersuite_info == NULL )
Jens Wiklander817466c2018-05-22 13:49:31 +02002255 {
Jerome Forissier79013242021-07-28 10:24:04 +02002256 MBEDTLS_SSL_DEBUG_MSG( 1,
2257 ( "ciphersuite info for %04x not found", (unsigned int)i ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002258 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2259 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
2260 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2261 }
2262
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002263 mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info );
Jens Wiklander817466c2018-05-22 13:49:31 +02002264
Jerome Forissier79013242021-07-28 10:24:04 +02002265 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002266 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
2267
2268 /*
2269 * Check if the session can be resumed
2270 */
2271 if( ssl->handshake->resume == 0 || n == 0 ||
2272#if defined(MBEDTLS_SSL_RENEGOTIATION)
2273 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
2274#endif
2275 ssl->session_negotiate->ciphersuite != i ||
2276 ssl->session_negotiate->compression != comp ||
2277 ssl->session_negotiate->id_len != n ||
2278 memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
2279 {
2280 ssl->state++;
2281 ssl->handshake->resume = 0;
2282#if defined(MBEDTLS_HAVE_TIME)
2283 ssl->session_negotiate->start = mbedtls_time( NULL );
2284#endif
2285 ssl->session_negotiate->ciphersuite = i;
2286 ssl->session_negotiate->compression = comp;
2287 ssl->session_negotiate->id_len = n;
2288 memcpy( ssl->session_negotiate->id, buf + 35, n );
2289 }
2290 else
2291 {
2292 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
2293
2294 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
2295 {
2296 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
Jerome Forissier79013242021-07-28 10:24:04 +02002297 mbedtls_ssl_send_alert_message(
2298 ssl,
2299 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2300 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Jens Wiklander817466c2018-05-22 13:49:31 +02002301 return( ret );
2302 }
2303 }
2304
2305 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
2306 ssl->handshake->resume ? "a" : "no" ) );
2307
Jerome Forissier79013242021-07-28 10:24:04 +02002308 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", (unsigned) i ) );
2309 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d",
2310 buf[37 + n] ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002311
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002312 /*
2313 * Perform cipher suite validation in same way as in ssl_write_client_hello.
2314 */
Jens Wiklander817466c2018-05-22 13:49:31 +02002315 i = 0;
2316 while( 1 )
2317 {
2318 if( ssl->conf->ciphersuite_list[ssl->minor_ver][i] == 0 )
2319 {
2320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02002321 mbedtls_ssl_send_alert_message(
2322 ssl,
2323 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2324 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Jens Wiklander817466c2018-05-22 13:49:31 +02002325 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
2326 }
2327
2328 if( ssl->conf->ciphersuite_list[ssl->minor_ver][i++] ==
2329 ssl->session_negotiate->ciphersuite )
2330 {
2331 break;
2332 }
2333 }
2334
Jerome Forissier79013242021-07-28 10:24:04 +02002335 suite_info = mbedtls_ssl_ciphersuite_from_id(
2336 ssl->session_negotiate->ciphersuite );
2337 if( ssl_validate_ciphersuite( suite_info, ssl, ssl->minor_ver,
2338 ssl->minor_ver ) != 0 )
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002339 {
2340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02002341 mbedtls_ssl_send_alert_message(
2342 ssl,
2343 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2344 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002345 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
2346 }
2347
Jerome Forissier79013242021-07-28 10:24:04 +02002348 MBEDTLS_SSL_DEBUG_MSG( 3,
2349 ( "server hello, chosen ciphersuite: %s", suite_info->name ) );
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002350
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002351#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002352 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
2353 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
2354 {
2355 ssl->handshake->ecrs_enabled = 1;
2356 }
2357#endif
2358
Jens Wiklander817466c2018-05-22 13:49:31 +02002359 if( comp != MBEDTLS_SSL_COMPRESS_NULL
2360#if defined(MBEDTLS_ZLIB_SUPPORT)
2361 && comp != MBEDTLS_SSL_COMPRESS_DEFLATE
2362#endif
2363 )
2364 {
2365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02002366 mbedtls_ssl_send_alert_message(
2367 ssl,
2368 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2369 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Jens Wiklander817466c2018-05-22 13:49:31 +02002370 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
2371 }
2372 ssl->session_negotiate->compression = comp;
2373
2374 ext = buf + 40 + n;
2375
Jerome Forissier79013242021-07-28 10:24:04 +02002376 MBEDTLS_SSL_DEBUG_MSG( 2,
2377 ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET, ext_len ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002378
2379 while( ext_len )
2380 {
2381 unsigned int ext_id = ( ( ext[0] << 8 )
2382 | ( ext[1] ) );
2383 unsigned int ext_size = ( ( ext[2] << 8 )
2384 | ( ext[3] ) );
2385
2386 if( ext_size + 4 > ext_len )
2387 {
2388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02002389 mbedtls_ssl_send_alert_message(
2390 ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2391 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Jens Wiklander817466c2018-05-22 13:49:31 +02002392 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
2393 }
2394
2395 switch( ext_id )
2396 {
2397 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
2398 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
2399#if defined(MBEDTLS_SSL_RENEGOTIATION)
2400 renegotiation_info_seen = 1;
2401#endif
2402
2403 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
2404 ext_size ) ) != 0 )
2405 return( ret );
2406
2407 break;
2408
2409#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2410 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Jerome Forissier79013242021-07-28 10:24:04 +02002411 MBEDTLS_SSL_DEBUG_MSG( 3,
2412 ( "found max_fragment_length extension" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002413
2414 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
2415 ext + 4, ext_size ) ) != 0 )
2416 {
2417 return( ret );
2418 }
2419
2420 break;
2421#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2422
2423#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
2424 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
2425 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
2426
2427 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
2428 ext + 4, ext_size ) ) != 0 )
2429 {
2430 return( ret );
2431 }
2432
2433 break;
2434#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
2435
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002436#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
2437 case MBEDTLS_TLS_EXT_CID:
2438 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
2439
2440 if( ( ret = ssl_parse_cid_ext( ssl,
2441 ext + 4,
2442 ext_size ) ) != 0 )
2443 {
2444 return( ret );
2445 }
2446
2447 break;
2448#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
2449
Jens Wiklander817466c2018-05-22 13:49:31 +02002450#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2451 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
2452 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
2453
2454 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
2455 ext + 4, ext_size ) ) != 0 )
2456 {
2457 return( ret );
2458 }
2459
2460 break;
2461#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2462
2463#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2464 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Jerome Forissier79013242021-07-28 10:24:04 +02002465 MBEDTLS_SSL_DEBUG_MSG( 3,
2466 ( "found extended_master_secret extension" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002467
2468 if( ( ret = ssl_parse_extended_ms_ext( ssl,
2469 ext + 4, ext_size ) ) != 0 )
2470 {
2471 return( ret );
2472 }
2473
2474 break;
2475#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
2476
2477#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2478 case MBEDTLS_TLS_EXT_SESSION_TICKET:
2479 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
2480
2481 if( ( ret = ssl_parse_session_ticket_ext( ssl,
2482 ext + 4, ext_size ) ) != 0 )
2483 {
2484 return( ret );
2485 }
2486
2487 break;
2488#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2489
2490#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
2491 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2492 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Jerome Forissier79013242021-07-28 10:24:04 +02002493 MBEDTLS_SSL_DEBUG_MSG( 3,
2494 ( "found supported_point_formats extension" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002495
2496 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
2497 ext + 4, ext_size ) ) != 0 )
2498 {
2499 return( ret );
2500 }
2501
2502 break;
2503#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
2504 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2505
2506#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2507 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
2508 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) );
2509
2510 if( ( ret = ssl_parse_ecjpake_kkpp( ssl,
2511 ext + 4, ext_size ) ) != 0 )
2512 {
2513 return( ret );
2514 }
2515
2516 break;
2517#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2518
2519#if defined(MBEDTLS_SSL_ALPN)
2520 case MBEDTLS_TLS_EXT_ALPN:
2521 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
2522
2523 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
2524 return( ret );
2525
2526 break;
2527#endif /* MBEDTLS_SSL_ALPN */
2528
Jerome Forissier79013242021-07-28 10:24:04 +02002529#if defined(MBEDTLS_SSL_DTLS_SRTP)
2530 case MBEDTLS_TLS_EXT_USE_SRTP:
2531 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
2532
2533 if( ( ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size ) ) != 0 )
2534 return( ret );
2535
2536 break;
2537#endif /* MBEDTLS_SSL_DTLS_SRTP */
2538
Jens Wiklander817466c2018-05-22 13:49:31 +02002539 default:
Jerome Forissier79013242021-07-28 10:24:04 +02002540 MBEDTLS_SSL_DEBUG_MSG( 3,
2541 ( "unknown extension found: %u (ignoring)", ext_id ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002542 }
2543
2544 ext_len -= 4 + ext_size;
2545 ext += 4 + ext_size;
2546
2547 if( ext_len > 0 && ext_len < 4 )
2548 {
2549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
2550 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
2551 }
2552 }
2553
2554 /*
2555 * Renegotiation security checks
2556 */
2557 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Jerome Forissier79013242021-07-28 10:24:04 +02002558 ssl->conf->allow_legacy_renegotiation ==
2559 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
Jens Wiklander817466c2018-05-22 13:49:31 +02002560 {
Jerome Forissier79013242021-07-28 10:24:04 +02002561 MBEDTLS_SSL_DEBUG_MSG( 1,
2562 ( "legacy renegotiation, breaking off handshake" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002563 handshake_failure = 1;
2564 }
2565#if defined(MBEDTLS_SSL_RENEGOTIATION)
2566 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2567 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
2568 renegotiation_info_seen == 0 )
2569 {
Jerome Forissier79013242021-07-28 10:24:04 +02002570 MBEDTLS_SSL_DEBUG_MSG( 1,
2571 ( "renegotiation_info extension missing (secure)" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002572 handshake_failure = 1;
2573 }
2574 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2575 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Jerome Forissier79013242021-07-28 10:24:04 +02002576 ssl->conf->allow_legacy_renegotiation ==
2577 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
Jens Wiklander817466c2018-05-22 13:49:31 +02002578 {
2579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
2580 handshake_failure = 1;
2581 }
2582 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2583 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
2584 renegotiation_info_seen == 1 )
2585 {
Jerome Forissier79013242021-07-28 10:24:04 +02002586 MBEDTLS_SSL_DEBUG_MSG( 1,
2587 ( "renegotiation_info extension present (legacy)" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002588 handshake_failure = 1;
2589 }
2590#endif /* MBEDTLS_SSL_RENEGOTIATION */
2591
2592 if( handshake_failure == 1 )
2593 {
Jerome Forissier79013242021-07-28 10:24:04 +02002594 mbedtls_ssl_send_alert_message(
2595 ssl,
2596 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2597 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Jens Wiklander817466c2018-05-22 13:49:31 +02002598 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
2599 }
2600
2601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
2602
2603 return( 0 );
2604}
2605
2606#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2607 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Jerome Forissier79013242021-07-28 10:24:04 +02002608static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl,
2609 unsigned char **p,
Jens Wiklander817466c2018-05-22 13:49:31 +02002610 unsigned char *end )
2611{
2612 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerome Forissier79013242021-07-28 10:24:04 +02002613 size_t dhm_actual_bitlen;
Jens Wiklander817466c2018-05-22 13:49:31 +02002614
2615 /*
2616 * Ephemeral DH parameters:
2617 *
2618 * struct {
2619 * opaque dh_p<1..2^16-1>;
2620 * opaque dh_g<1..2^16-1>;
2621 * opaque dh_Ys<1..2^16-1>;
2622 * } ServerDHParams;
2623 */
Jerome Forissier79013242021-07-28 10:24:04 +02002624 if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx,
2625 p, end ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02002626 {
2627 MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret );
2628 return( ret );
2629 }
2630
Jerome Forissier79013242021-07-28 10:24:04 +02002631 dhm_actual_bitlen = mbedtls_mpi_bitlen( &ssl->handshake->dhm_ctx.P );
2632 if( dhm_actual_bitlen < ssl->conf->dhm_min_bitlen )
Jens Wiklander817466c2018-05-22 13:49:31 +02002633 {
Jerome Forissier79013242021-07-28 10:24:04 +02002634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
2635 dhm_actual_bitlen,
Jens Wiklander817466c2018-05-22 13:49:31 +02002636 ssl->conf->dhm_min_bitlen ) );
2637 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2638 }
2639
2640 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2641 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2642 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2643
2644 return( ret );
2645}
2646#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2647 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2648
2649#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2650 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2651 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
2652 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2653 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2654static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl )
2655{
2656 const mbedtls_ecp_curve_info *curve_info;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002657 mbedtls_ecp_group_id grp_id;
2658#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
2659 grp_id = ssl->handshake->ecdh_ctx.grp.id;
2660#else
2661 grp_id = ssl->handshake->ecdh_ctx.grp_id;
2662#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02002663
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002664 curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
Jens Wiklander817466c2018-05-22 13:49:31 +02002665 if( curve_info == NULL )
2666 {
2667 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2668 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2669 }
2670
2671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
2672
2673#if defined(MBEDTLS_ECP_C)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002674 if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02002675#else
2676 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
2677 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
2678#endif
2679 return( -1 );
2680
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002681 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2682 MBEDTLS_DEBUG_ECDH_QP );
Jens Wiklander817466c2018-05-22 13:49:31 +02002683
2684 return( 0 );
2685}
2686#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2687 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2688 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2689 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2690 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2691
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002692#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
2693 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2694 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) )
2695static int ssl_parse_server_ecdh_params_psa( mbedtls_ssl_context *ssl,
2696 unsigned char **p,
2697 unsigned char *end )
2698{
2699 uint16_t tls_id;
2700 size_t ecdh_bits = 0;
2701 uint8_t ecpoint_len;
2702 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2703
2704 /*
2705 * Parse ECC group
2706 */
2707
2708 if( end - *p < 4 )
2709 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2710
2711 /* First byte is curve_type; only named_curve is handled */
2712 if( *(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
2713 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2714
2715 /* Next two bytes are the namedcurve value */
2716 tls_id = *(*p)++;
2717 tls_id <<= 8;
2718 tls_id |= *(*p)++;
2719
2720 /* Convert EC group to PSA key type. */
2721 if( ( handshake->ecdh_psa_type =
2722 mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 )
2723 {
2724 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2725 }
2726 if( ecdh_bits > 0xffff )
2727 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2728 handshake->ecdh_bits = (uint16_t) ecdh_bits;
2729
2730 /*
2731 * Put peer's ECDH public key in the format understood by PSA.
2732 */
2733
2734 ecpoint_len = *(*p)++;
2735 if( (size_t)( end - *p ) < ecpoint_len )
2736 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2737
2738 if( mbedtls_psa_tls_ecpoint_to_psa_ec(
2739 *p, ecpoint_len,
2740 handshake->ecdh_psa_peerkey,
2741 sizeof( handshake->ecdh_psa_peerkey ),
2742 &handshake->ecdh_psa_peerkey_len ) != 0 )
2743 {
2744 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2745 }
2746
2747 *p += ecpoint_len;
2748 return( 0 );
2749}
2750#endif /* MBEDTLS_USE_PSA_CRYPTO &&
2751 ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2752 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */
2753
Jens Wiklander817466c2018-05-22 13:49:31 +02002754#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2755 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2756 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2757static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
2758 unsigned char **p,
2759 unsigned char *end )
2760{
2761 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2762
2763 /*
2764 * Ephemeral ECDH parameters:
2765 *
2766 * struct {
2767 * ECParameters curve_params;
2768 * ECPoint public;
2769 * } ServerECDHParams;
2770 */
2771 if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
2772 (const unsigned char **) p, end ) ) != 0 )
2773 {
2774 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002775#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002776 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2777 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2778#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02002779 return( ret );
2780 }
2781
2782 if( ssl_check_server_ecdh_params( ssl ) != 0 )
2783 {
Jerome Forissier79013242021-07-28 10:24:04 +02002784 MBEDTLS_SSL_DEBUG_MSG( 1,
2785 ( "bad server key exchange message (ECDHE curve)" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002786 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2787 }
2788
2789 return( ret );
2790}
2791#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2792 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2793 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2794
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002795#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02002796static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
2797 unsigned char **p,
2798 unsigned char *end )
2799{
2800 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002801 uint16_t len;
Jens Wiklander817466c2018-05-22 13:49:31 +02002802 ((void) ssl);
2803
2804 /*
2805 * PSK parameters:
2806 *
2807 * opaque psk_identity_hint<0..2^16-1>;
2808 */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002809 if( end - (*p) < 2 )
2810 {
Jerome Forissier79013242021-07-28 10:24:04 +02002811 MBEDTLS_SSL_DEBUG_MSG( 1,
2812 ( "bad server key exchange message (psk_identity_hint length)" ) );
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002813 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2814 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002815 len = (*p)[0] << 8 | (*p)[1];
2816 *p += 2;
2817
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002818 if( end - (*p) < len )
Jens Wiklander817466c2018-05-22 13:49:31 +02002819 {
Jerome Forissier79013242021-07-28 10:24:04 +02002820 MBEDTLS_SSL_DEBUG_MSG( 1,
2821 ( "bad server key exchange message (psk_identity_hint length)" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002822 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2823 }
2824
2825 /*
2826 * Note: we currently ignore the PKS identity hint, as we only allow one
2827 * PSK to be provisionned on the client. This could be changed later if
2828 * someone needs that feature.
2829 */
2830 *p += len;
2831 ret = 0;
2832
2833 return( ret );
2834}
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002835#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02002836
2837#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
2838 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2839/*
2840 * Generate a pre-master secret and encrypt it with the server's RSA key
2841 */
2842static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
2843 size_t offset, size_t *olen,
2844 size_t pms_offset )
2845{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002846 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02002847 size_t len_bytes = ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ? 0 : 2;
2848 unsigned char *p = ssl->handshake->premaster + pms_offset;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002849 mbedtls_pk_context * peer_pk;
Jens Wiklander817466c2018-05-22 13:49:31 +02002850
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002851 if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN )
Jens Wiklander817466c2018-05-22 13:49:31 +02002852 {
2853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
2854 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2855 }
2856
2857 /*
2858 * Generate (part of) the pre-master as
2859 * struct {
2860 * ProtocolVersion client_version;
2861 * opaque random[46];
2862 * } PreMasterSecret;
2863 */
Jerome Forissier79013242021-07-28 10:24:04 +02002864 mbedtls_ssl_write_version( ssl->conf->max_major_ver,
2865 ssl->conf->max_minor_ver,
2866 ssl->conf->transport, p );
Jens Wiklander817466c2018-05-22 13:49:31 +02002867
2868 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
2869 {
2870 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
2871 return( ret );
2872 }
2873
2874 ssl->handshake->pmslen = 48;
2875
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002876#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2877 peer_pk = &ssl->handshake->peer_pubkey;
2878#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Jens Wiklander817466c2018-05-22 13:49:31 +02002879 if( ssl->session_negotiate->peer_cert == NULL )
2880 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002881 /* Should never happen */
2882 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2883 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jens Wiklander817466c2018-05-22 13:49:31 +02002884 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002885 peer_pk = &ssl->session_negotiate->peer_cert->pk;
2886#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Jens Wiklander817466c2018-05-22 13:49:31 +02002887
2888 /*
2889 * Now write it out, encrypted
2890 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002891 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) )
Jens Wiklander817466c2018-05-22 13:49:31 +02002892 {
2893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
2894 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
2895 }
2896
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002897 if( ( ret = mbedtls_pk_encrypt( peer_pk,
Jens Wiklander817466c2018-05-22 13:49:31 +02002898 p, ssl->handshake->pmslen,
2899 ssl->out_msg + offset + len_bytes, olen,
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002900 MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
Jens Wiklander817466c2018-05-22 13:49:31 +02002901 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
2902 {
2903 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
2904 return( ret );
2905 }
2906
2907#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2908 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2909 if( len_bytes == 2 )
2910 {
2911 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
2912 ssl->out_msg[offset+1] = (unsigned char)( *olen );
2913 *olen += 2;
2914 }
2915#endif
2916
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002917#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
2918 /* We don't need the peer's public key anymore. Free it. */
2919 mbedtls_pk_free( peer_pk );
2920#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Jens Wiklander817466c2018-05-22 13:49:31 +02002921 return( 0 );
2922}
2923#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
2924 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2925
2926#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2927#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2928 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2929 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2930static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl,
2931 unsigned char **p,
2932 unsigned char *end,
2933 mbedtls_md_type_t *md_alg,
2934 mbedtls_pk_type_t *pk_alg )
2935{
2936 ((void) ssl);
2937 *md_alg = MBEDTLS_MD_NONE;
2938 *pk_alg = MBEDTLS_PK_NONE;
2939
2940 /* Only in TLS 1.2 */
2941 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
2942 {
2943 return( 0 );
2944 }
2945
2946 if( (*p) + 2 > end )
2947 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2948
2949 /*
2950 * Get hash algorithm
2951 */
Jerome Forissier79013242021-07-28 10:24:04 +02002952 if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) )
2953 == MBEDTLS_MD_NONE )
Jens Wiklander817466c2018-05-22 13:49:31 +02002954 {
Jerome Forissier79013242021-07-28 10:24:04 +02002955 MBEDTLS_SSL_DEBUG_MSG( 1,
2956 ( "Server used unsupported HashAlgorithm %d", *(p)[0] ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002957 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2958 }
2959
2960 /*
2961 * Get signature algorithm
2962 */
Jerome Forissier79013242021-07-28 10:24:04 +02002963 if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) )
2964 == MBEDTLS_PK_NONE )
Jens Wiklander817466c2018-05-22 13:49:31 +02002965 {
Jerome Forissier79013242021-07-28 10:24:04 +02002966 MBEDTLS_SSL_DEBUG_MSG( 1,
2967 ( "server used unsupported SignatureAlgorithm %d", (*p)[1] ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002968 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2969 }
2970
2971 /*
2972 * Check if the hash is acceptable
2973 */
2974 if( mbedtls_ssl_check_sig_hash( ssl, *md_alg ) != 0 )
2975 {
Jerome Forissier79013242021-07-28 10:24:04 +02002976 MBEDTLS_SSL_DEBUG_MSG( 1,
2977 ( "server used HashAlgorithm %d that was not offered", *(p)[0] ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002978 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2979 }
2980
Jerome Forissier79013242021-07-28 10:24:04 +02002981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d",
2982 (*p)[1] ) );
2983 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d",
2984 (*p)[0] ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002985 *p += 2;
2986
2987 return( 0 );
2988}
2989#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2990 MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2991 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
2992#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2993
2994#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2995 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2996static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
2997{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002998 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02002999 const mbedtls_ecp_keypair *peer_key;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003000 mbedtls_pk_context * peer_pk;
Jens Wiklander817466c2018-05-22 13:49:31 +02003001
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003002#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3003 peer_pk = &ssl->handshake->peer_pubkey;
3004#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Jens Wiklander817466c2018-05-22 13:49:31 +02003005 if( ssl->session_negotiate->peer_cert == NULL )
3006 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003007 /* Should never happen */
3008 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3009 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jens Wiklander817466c2018-05-22 13:49:31 +02003010 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003011 peer_pk = &ssl->session_negotiate->peer_cert->pk;
3012#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Jens Wiklander817466c2018-05-22 13:49:31 +02003013
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003014 if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) )
Jens Wiklander817466c2018-05-22 13:49:31 +02003015 {
3016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
3017 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
3018 }
3019
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003020 peer_key = mbedtls_pk_ec( *peer_pk );
Jens Wiklander817466c2018-05-22 13:49:31 +02003021
3022 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
3023 MBEDTLS_ECDH_THEIRS ) ) != 0 )
3024 {
3025 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
3026 return( ret );
3027 }
3028
3029 if( ssl_check_server_ecdh_params( ssl ) != 0 )
3030 {
3031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
3032 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
3033 }
3034
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003035#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3036 /* We don't need the peer's public key anymore. Free it,
3037 * so that more RAM is available for upcoming expensive
3038 * operations like ECDHE. */
3039 mbedtls_pk_free( peer_pk );
3040#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3041
Jens Wiklander817466c2018-05-22 13:49:31 +02003042 return( ret );
3043}
3044#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
3045 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
3046
3047static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
3048{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003049 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02003050 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003051 ssl->handshake->ciphersuite_info;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003052 unsigned char *p = NULL, *end = NULL;
Jens Wiklander817466c2018-05-22 13:49:31 +02003053
3054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
3055
3056#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3057 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
3058 {
3059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
3060 ssl->state++;
3061 return( 0 );
3062 }
3063 ((void) p);
3064 ((void) end);
3065#endif
3066
3067#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3068 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3069 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
3070 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
3071 {
3072 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
3073 {
3074 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
Jerome Forissier79013242021-07-28 10:24:04 +02003075 mbedtls_ssl_send_alert_message(
3076 ssl,
3077 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3078 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Jens Wiklander817466c2018-05-22 13:49:31 +02003079 return( ret );
3080 }
3081
3082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
3083 ssl->state++;
3084 return( 0 );
3085 }
3086 ((void) p);
3087 ((void) end);
3088#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3089 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
3090
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003091#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003092 if( ssl->handshake->ecrs_enabled &&
3093 ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing )
3094 {
3095 goto start_processing;
3096 }
3097#endif
3098
3099 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003100 {
3101 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
3102 return( ret );
3103 }
3104
3105 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
3106 {
3107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02003108 mbedtls_ssl_send_alert_message(
3109 ssl,
3110 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3111 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Jens Wiklander817466c2018-05-22 13:49:31 +02003112 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
3113 }
3114
3115 /*
3116 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
3117 * doesn't use a psk_identity_hint
3118 */
3119 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
3120 {
3121 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
3122 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
3123 {
3124 /* Current message is probably either
3125 * CertificateRequest or ServerHelloDone */
3126 ssl->keep_current_message = 1;
3127 goto exit;
3128 }
3129
Jerome Forissier79013242021-07-28 10:24:04 +02003130 MBEDTLS_SSL_DEBUG_MSG( 1,
3131 ( "server key exchange message must not be skipped" ) );
3132 mbedtls_ssl_send_alert_message(
3133 ssl,
3134 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3135 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Jens Wiklander817466c2018-05-22 13:49:31 +02003136
3137 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
3138 }
3139
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003140#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003141 if( ssl->handshake->ecrs_enabled )
3142 ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
3143
3144start_processing:
3145#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02003146 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
3147 end = ssl->in_msg + ssl->in_hslen;
3148 MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
3149
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003150#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02003151 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
3152 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
3153 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
3154 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
3155 {
3156 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
3157 {
3158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02003159 mbedtls_ssl_send_alert_message(
3160 ssl,
3161 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3162 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Jens Wiklander817466c2018-05-22 13:49:31 +02003163 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
3164 }
3165 } /* FALLTROUGH */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003166#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02003167
3168#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
3169 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3170 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
3171 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
3172 ; /* nothing more to do */
3173 else
3174#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
3175 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
3176#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3177 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3178 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
3179 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
3180 {
3181 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
3182 {
3183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02003184 mbedtls_ssl_send_alert_message(
3185 ssl,
3186 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3187 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Jens Wiklander817466c2018-05-22 13:49:31 +02003188 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
3189 }
3190 }
3191 else
3192#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3193 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003194#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
3195 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3196 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) )
3197 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
3198 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
3199 {
3200 if( ssl_parse_server_ecdh_params_psa( ssl, &p, end ) != 0 )
3201 {
3202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02003203 mbedtls_ssl_send_alert_message(
3204 ssl,
3205 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3206 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003207 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
3208 }
3209 }
3210 else
3211#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3212 ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3213 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */
Jens Wiklander817466c2018-05-22 13:49:31 +02003214#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3215 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
3216 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
3217 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
3218 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
3219 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
3220 {
3221 if( ssl_parse_server_ecdh_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_ECDHE_RSA_ENABLED ||
3233 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
3234 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
3235#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3236 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3237 {
3238 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
3239 p, end - p );
3240 if( ret != 0 )
3241 {
3242 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
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 );
Jens Wiklander817466c2018-05-22 13:49:31 +02003247 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
3248 }
3249 }
3250 else
3251#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
3252 {
3253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3254 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3255 }
3256
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003257#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02003258 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
3259 {
3260 size_t sig_len, hashlen;
3261 unsigned char hash[64];
3262 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
3263 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
3264 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
3265 size_t params_len = p - params;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003266 void *rs_ctx = NULL;
Jens Wiklander817466c2018-05-22 13:49:31 +02003267
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003268 mbedtls_pk_context * peer_pk;
3269
Jens Wiklander817466c2018-05-22 13:49:31 +02003270 /*
3271 * Handle the digitally-signed structure
3272 */
3273#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3274 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
3275 {
3276 if( ssl_parse_signature_algorithm( ssl, &p, end,
3277 &md_alg, &pk_alg ) != 0 )
3278 {
Jerome Forissier79013242021-07-28 10:24:04 +02003279 MBEDTLS_SSL_DEBUG_MSG( 1,
3280 ( "bad server key exchange message" ) );
3281 mbedtls_ssl_send_alert_message(
3282 ssl,
3283 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3284 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Jens Wiklander817466c2018-05-22 13:49:31 +02003285 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
3286 }
3287
Jerome Forissier79013242021-07-28 10:24:04 +02003288 if( pk_alg !=
3289 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Jens Wiklander817466c2018-05-22 13:49:31 +02003290 {
Jerome Forissier79013242021-07-28 10:24:04 +02003291 MBEDTLS_SSL_DEBUG_MSG( 1,
3292 ( "bad server key exchange message" ) );
3293 mbedtls_ssl_send_alert_message(
3294 ssl,
3295 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3296 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
Jens Wiklander817466c2018-05-22 13:49:31 +02003297 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
3298 }
3299 }
3300 else
3301#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3302#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3303 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3304 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
3305 {
3306 pk_alg = mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
3307
3308 /* Default hash for ECDSA is SHA-1 */
3309 if( pk_alg == MBEDTLS_PK_ECDSA && md_alg == MBEDTLS_MD_NONE )
3310 md_alg = MBEDTLS_MD_SHA1;
3311 }
3312 else
3313#endif
3314 {
3315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3316 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3317 }
3318
3319 /*
3320 * Read signature
3321 */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003322
3323 if( p > end - 2 )
3324 {
3325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02003326 mbedtls_ssl_send_alert_message(
3327 ssl,
3328 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3329 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003330 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
3331 }
Jens Wiklander817466c2018-05-22 13:49:31 +02003332 sig_len = ( p[0] << 8 ) | p[1];
3333 p += 2;
3334
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003335 if( p != end - sig_len )
Jens Wiklander817466c2018-05-22 13:49:31 +02003336 {
3337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02003338 mbedtls_ssl_send_alert_message(
3339 ssl,
3340 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3341 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Jens Wiklander817466c2018-05-22 13:49:31 +02003342 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
3343 }
3344
3345 MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
3346
3347 /*
3348 * Compute the hash that has been signed
3349 */
3350#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3351 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3352 if( md_alg == MBEDTLS_MD_NONE )
3353 {
Jens Wiklander817466c2018-05-22 13:49:31 +02003354 hashlen = 36;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003355 ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash, params,
3356 params_len );
3357 if( ret != 0 )
3358 return( ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02003359 }
3360 else
3361#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3362 MBEDTLS_SSL_PROTO_TLS1_1 */
3363#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3364 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3365 if( md_alg != MBEDTLS_MD_NONE )
3366 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003367 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
3368 params, params_len,
3369 md_alg );
3370 if( ret != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003371 return( ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02003372 }
3373 else
3374#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3375 MBEDTLS_SSL_PROTO_TLS1_2 */
3376 {
3377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3378 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3379 }
3380
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003381 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Jens Wiklander817466c2018-05-22 13:49:31 +02003382
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003383#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3384 peer_pk = &ssl->handshake->peer_pubkey;
3385#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Jens Wiklander817466c2018-05-22 13:49:31 +02003386 if( ssl->session_negotiate->peer_cert == NULL )
3387 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003388 /* Should never happen */
3389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3390 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jens Wiklander817466c2018-05-22 13:49:31 +02003391 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003392 peer_pk = &ssl->session_negotiate->peer_cert->pk;
3393#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Jens Wiklander817466c2018-05-22 13:49:31 +02003394
3395 /*
3396 * Verify signature
3397 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003398 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Jens Wiklander817466c2018-05-22 13:49:31 +02003399 {
3400 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02003401 mbedtls_ssl_send_alert_message(
3402 ssl,
3403 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3404 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
Jens Wiklander817466c2018-05-22 13:49:31 +02003405 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
3406 }
3407
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003408#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003409 if( ssl->handshake->ecrs_enabled )
3410 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
3411#endif
3412
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003413 if( ( ret = mbedtls_pk_verify_restartable( peer_pk,
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003414 md_alg, hash, hashlen, p, sig_len, rs_ctx ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003415 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003416#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003417 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3418#endif
Jerome Forissier79013242021-07-28 10:24:04 +02003419 mbedtls_ssl_send_alert_message(
3420 ssl,
3421 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3422 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
Jens Wiklander817466c2018-05-22 13:49:31 +02003423 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003424#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003425 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3426 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3427#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02003428 return( ret );
3429 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003430
3431#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3432 /* We don't need the peer's public key anymore. Free it,
3433 * so that more RAM is available for upcoming expensive
3434 * operations like ECDHE. */
3435 mbedtls_pk_free( peer_pk );
3436#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Jens Wiklander817466c2018-05-22 13:49:31 +02003437 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003438#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02003439
3440exit:
3441 ssl->state++;
3442
3443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
3444
3445 return( 0 );
3446}
3447
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003448#if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02003449static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
3450{
3451 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003452 ssl->handshake->ciphersuite_info;
Jens Wiklander817466c2018-05-22 13:49:31 +02003453
3454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
3455
3456 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
3457 {
3458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
3459 ssl->state++;
3460 return( 0 );
3461 }
3462
3463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3464 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3465}
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003466#else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02003467static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
3468{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003469 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02003470 unsigned char *buf;
3471 size_t n = 0;
3472 size_t cert_type_len = 0, dn_len = 0;
3473 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003474 ssl->handshake->ciphersuite_info;
Jens Wiklander817466c2018-05-22 13:49:31 +02003475
3476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
3477
3478 if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
3479 {
3480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
3481 ssl->state++;
3482 return( 0 );
3483 }
3484
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003485 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003486 {
3487 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
3488 return( ret );
3489 }
3490
3491 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
3492 {
3493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02003494 mbedtls_ssl_send_alert_message(
3495 ssl,
3496 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3497 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Jens Wiklander817466c2018-05-22 13:49:31 +02003498 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
3499 }
3500
3501 ssl->state++;
3502 ssl->client_auth = ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST );
3503
3504 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
3505 ssl->client_auth ? "a" : "no" ) );
3506
3507 if( ssl->client_auth == 0 )
3508 {
3509 /* Current message is probably the ServerHelloDone */
3510 ssl->keep_current_message = 1;
3511 goto exit;
3512 }
3513
3514 /*
3515 * struct {
3516 * ClientCertificateType certificate_types<1..2^8-1>;
3517 * SignatureAndHashAlgorithm
3518 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
3519 * DistinguishedName certificate_authorities<0..2^16-1>;
3520 * } CertificateRequest;
3521 *
3522 * Since we only support a single certificate on clients, let's just
3523 * ignore all the information that's supposed to help us pick a
3524 * certificate.
3525 *
3526 * We could check that our certificate matches the request, and bail out
3527 * if it doesn't, but it's simpler to just send the certificate anyway,
3528 * and give the server the opportunity to decide if it should terminate
3529 * the connection when it doesn't like our certificate.
3530 *
3531 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
3532 * point we only have one hash available (see comments in
3533 * write_certificate_verify), so let's just use what we have.
3534 *
3535 * However, we still minimally parse the message to check it is at least
3536 * superficially sane.
3537 */
3538 buf = ssl->in_msg;
3539
3540 /* certificate_types */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003541 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) )
3542 {
3543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
3544 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3545 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
3546 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
3547 }
Jens Wiklander817466c2018-05-22 13:49:31 +02003548 cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
3549 n = cert_type_len;
3550
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003551 /*
3552 * In the subsequent code there are two paths that read from buf:
3553 * * the length of the signature algorithms field (if minor version of
3554 * SSL is 3),
3555 * * distinguished name length otherwise.
3556 * Both reach at most the index:
3557 * ...hdr_len + 2 + n,
3558 * therefore the buffer length at this point must be greater than that
3559 * regardless of the actual code path.
3560 */
3561 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
Jens Wiklander817466c2018-05-22 13:49:31 +02003562 {
3563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
3564 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3565 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
3566 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
3567 }
3568
3569 /* supported_signature_algorithms */
3570#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3571 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
3572 {
Jerome Forissier79013242021-07-28 10:24:04 +02003573 size_t sig_alg_len =
3574 ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
3575 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02003576#if defined(MBEDTLS_DEBUG_C)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003577 unsigned char* sig_alg;
Jens Wiklander817466c2018-05-22 13:49:31 +02003578 size_t i;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003579#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02003580
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003581 /*
3582 * The furthest access in buf is in the loop few lines below:
3583 * sig_alg[i + 1],
3584 * where:
3585 * sig_alg = buf + ...hdr_len + 3 + n,
3586 * max(i) = sig_alg_len - 1.
3587 * Therefore the furthest access is:
3588 * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
3589 * which reduces to:
3590 * buf[...hdr_len + 3 + n + sig_alg_len],
3591 * which is one less than we need the buf to be.
3592 */
Jerome Forissier79013242021-07-28 10:24:04 +02003593 if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl )
3594 + 3 + n + sig_alg_len )
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003595 {
3596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02003597 mbedtls_ssl_send_alert_message(
3598 ssl,
3599 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3600 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003601 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
3602 }
3603
3604#if defined(MBEDTLS_DEBUG_C)
3605 sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
Jens Wiklander817466c2018-05-22 13:49:31 +02003606 for( i = 0; i < sig_alg_len; i += 2 )
3607 {
Jerome Forissier79013242021-07-28 10:24:04 +02003608 MBEDTLS_SSL_DEBUG_MSG( 3,
3609 ( "Supported Signature Algorithm found: %d,%d",
3610 sig_alg[i], sig_alg[i + 1] ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02003611 }
3612#endif
3613
3614 n += 2 + sig_alg_len;
Jens Wiklander817466c2018-05-22 13:49:31 +02003615 }
3616#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3617
3618 /* certificate_authorities */
3619 dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
3620 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
3621
3622 n += dn_len;
3623 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
3624 {
3625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
3626 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3627 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
3628 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
3629 }
3630
3631exit:
3632 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
3633
3634 return( 0 );
3635}
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003636#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02003637
3638static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
3639{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003640 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02003641
3642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
3643
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003644 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003645 {
3646 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
3647 return( ret );
3648 }
3649
3650 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
3651 {
3652 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
3653 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
3654 }
3655
3656 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
3657 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
3658 {
3659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
3660 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3661 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
3662 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
3663 }
3664
3665 ssl->state++;
3666
3667#if defined(MBEDTLS_SSL_PROTO_DTLS)
3668 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3669 mbedtls_ssl_recv_flight_completed( ssl );
3670#endif
3671
3672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
3673
3674 return( 0 );
3675}
3676
3677static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
3678{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003679 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3680
3681 size_t header_len;
3682 size_t content_len;
Jens Wiklander817466c2018-05-22 13:49:31 +02003683 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003684 ssl->handshake->ciphersuite_info;
Jens Wiklander817466c2018-05-22 13:49:31 +02003685
3686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
3687
3688#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
3689 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
3690 {
3691 /*
3692 * DHM key exchange -- send G^X mod P
3693 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003694 content_len = ssl->handshake->dhm_ctx.len;
Jens Wiklander817466c2018-05-22 13:49:31 +02003695
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003696 ssl->out_msg[4] = (unsigned char)( content_len >> 8 );
3697 ssl->out_msg[5] = (unsigned char)( content_len );
3698 header_len = 6;
Jens Wiklander817466c2018-05-22 13:49:31 +02003699
3700 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
Jerome Forissier79013242021-07-28 10:24:04 +02003701 (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
3702 &ssl->out_msg[header_len], content_len,
3703 ssl->conf->f_rng, ssl->conf->p_rng );
Jens Wiklander817466c2018-05-22 13:49:31 +02003704 if( ret != 0 )
3705 {
3706 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
3707 return( ret );
3708 }
3709
3710 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
3711 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
3712
3713 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Jerome Forissier79013242021-07-28 10:24:04 +02003714 ssl->handshake->premaster,
3715 MBEDTLS_PREMASTER_SIZE,
3716 &ssl->handshake->pmslen,
3717 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003718 {
3719 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
3720 return( ret );
3721 }
3722
3723 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
3724 }
3725 else
3726#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003727#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
3728 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3729 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) )
3730 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
3731 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
3732 {
3733 psa_status_t status;
3734 psa_key_attributes_t key_attributes;
3735
3736 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3737
3738 unsigned char own_pubkey[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
3739 size_t own_pubkey_len;
3740 unsigned char *own_pubkey_ecpoint;
3741 size_t own_pubkey_ecpoint_len;
3742
3743 header_len = 4;
3744
3745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
3746
3747 /*
3748 * Generate EC private key for ECDHE exchange.
3749 */
3750
3751 /* The master secret is obtained from the shared ECDH secret by
3752 * applying the TLS 1.2 PRF with a specific salt and label. While
3753 * the PSA Crypto API encourages combining key agreement schemes
3754 * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
3755 * yet support the provisioning of salt + label to the KDF.
3756 * For the time being, we therefore need to split the computation
3757 * of the ECDH secret and the application of the TLS 1.2 PRF. */
3758 key_attributes = psa_key_attributes_init();
3759 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
3760 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
3761 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
3762 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
3763
3764 /* Generate ECDH private key. */
3765 status = psa_generate_key( &key_attributes,
3766 &handshake->ecdh_psa_privkey );
3767 if( status != PSA_SUCCESS )
3768 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
3769
3770 /* Export the public part of the ECDH private key from PSA
3771 * and convert it to ECPoint format used in ClientKeyExchange. */
3772 status = psa_export_public_key( handshake->ecdh_psa_privkey,
3773 own_pubkey, sizeof( own_pubkey ),
3774 &own_pubkey_len );
3775 if( status != PSA_SUCCESS )
3776 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
3777
3778 if( mbedtls_psa_tls_psa_ec_to_ecpoint( own_pubkey,
3779 own_pubkey_len,
3780 &own_pubkey_ecpoint,
3781 &own_pubkey_ecpoint_len ) != 0 )
3782 {
3783 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
3784 }
3785
3786 /* Copy ECPoint structure to outgoing message buffer. */
3787 ssl->out_msg[header_len] = (unsigned char) own_pubkey_ecpoint_len;
3788 memcpy( ssl->out_msg + header_len + 1,
3789 own_pubkey_ecpoint, own_pubkey_ecpoint_len );
3790 content_len = own_pubkey_ecpoint_len + 1;
3791
3792 /* The ECDH secret is the premaster secret used for key derivation. */
3793
3794 /* Compute ECDH shared secret. */
3795 status = psa_raw_key_agreement( PSA_ALG_ECDH,
3796 handshake->ecdh_psa_privkey,
3797 handshake->ecdh_psa_peerkey,
3798 handshake->ecdh_psa_peerkey_len,
3799 ssl->handshake->premaster,
3800 sizeof( ssl->handshake->premaster ),
3801 &ssl->handshake->pmslen );
3802 if( status != PSA_SUCCESS )
3803 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
3804
3805 status = psa_destroy_key( handshake->ecdh_psa_privkey );
3806 if( status != PSA_SUCCESS )
3807 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Jerome Forissier79013242021-07-28 10:24:04 +02003808 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003809 }
3810 else
3811#endif /* MBEDTLS_USE_PSA_CRYPTO &&
3812 ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3813 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */
Jens Wiklander817466c2018-05-22 13:49:31 +02003814#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3815 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3816 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3817 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3818 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
3819 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
3820 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
3821 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
3822 {
3823 /*
3824 * ECDH key exchange -- send client public value
3825 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003826 header_len = 4;
Jens Wiklander817466c2018-05-22 13:49:31 +02003827
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003828#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003829 if( ssl->handshake->ecrs_enabled )
3830 {
3831 if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret )
3832 goto ecdh_calc_secret;
3833
3834 mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx );
3835 }
3836#endif
3837
Jens Wiklander817466c2018-05-22 13:49:31 +02003838 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003839 &content_len,
3840 &ssl->out_msg[header_len], 1000,
Jens Wiklander817466c2018-05-22 13:49:31 +02003841 ssl->conf->f_rng, ssl->conf->p_rng );
3842 if( ret != 0 )
3843 {
3844 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003845#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003846 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3847 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3848#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02003849 return( ret );
3850 }
3851
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003852 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3853 MBEDTLS_DEBUG_ECDH_Q );
Jens Wiklander817466c2018-05-22 13:49:31 +02003854
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003855#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003856 if( ssl->handshake->ecrs_enabled )
3857 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003858 ssl->handshake->ecrs_n = content_len;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003859 ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
3860 }
3861
3862ecdh_calc_secret:
3863 if( ssl->handshake->ecrs_enabled )
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003864 content_len = ssl->handshake->ecrs_n;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003865#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02003866 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Jerome Forissier79013242021-07-28 10:24:04 +02003867 &ssl->handshake->pmslen,
3868 ssl->handshake->premaster,
3869 MBEDTLS_MPI_MAX_SIZE,
3870 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003871 {
3872 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003873#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003874 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
3875 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
3876#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02003877 return( ret );
3878 }
3879
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003880 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3881 MBEDTLS_DEBUG_ECDH_Z );
Jens Wiklander817466c2018-05-22 13:49:31 +02003882 }
3883 else
3884#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3885 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3886 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3887 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003888#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02003889 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
3890 {
3891 /*
3892 * opaque psk_identity<0..2^16-1>;
3893 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003894 if( ssl_conf_has_static_psk( ssl->conf ) == 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003895 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003896 /* We don't offer PSK suites if we don't have a PSK,
3897 * and we check that the server's choice is among the
3898 * ciphersuites we offered, so this should never happen. */
3899 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jens Wiklander817466c2018-05-22 13:49:31 +02003900 }
3901
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003902 header_len = 4;
3903 content_len = ssl->conf->psk_identity_len;
Jens Wiklander817466c2018-05-22 13:49:31 +02003904
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003905 if( header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Jens Wiklander817466c2018-05-22 13:49:31 +02003906 {
Jerome Forissier79013242021-07-28 10:24:04 +02003907 MBEDTLS_SSL_DEBUG_MSG( 1,
3908 ( "psk identity too long or SSL buffer too short" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02003909 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3910 }
3911
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003912 ssl->out_msg[header_len++] = (unsigned char)( content_len >> 8 );
3913 ssl->out_msg[header_len++] = (unsigned char)( content_len );
Jens Wiklander817466c2018-05-22 13:49:31 +02003914
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003915 memcpy( ssl->out_msg + header_len,
3916 ssl->conf->psk_identity,
3917 ssl->conf->psk_identity_len );
3918 header_len += ssl->conf->psk_identity_len;
Jens Wiklander817466c2018-05-22 13:49:31 +02003919
3920#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3921 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
3922 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003923 content_len = 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02003924 }
3925 else
3926#endif
3927#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3928 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
3929 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003930#if defined(MBEDTLS_USE_PSA_CRYPTO)
3931 /* Opaque PSKs are currently only supported for PSK-only suites. */
3932 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
3933 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3934#endif /* MBEDTLS_USE_PSA_CRYPTO */
3935
3936 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
3937 &content_len, 2 ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003938 return( ret );
3939 }
3940 else
3941#endif
3942#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3943 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
3944 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003945#if defined(MBEDTLS_USE_PSA_CRYPTO)
3946 /* Opaque PSKs are currently only supported for PSK-only suites. */
3947 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
3948 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3949#endif /* MBEDTLS_USE_PSA_CRYPTO */
3950
Jens Wiklander817466c2018-05-22 13:49:31 +02003951 /*
3952 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
3953 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003954 content_len = ssl->handshake->dhm_ctx.len;
Jens Wiklander817466c2018-05-22 13:49:31 +02003955
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003956 if( header_len + 2 + content_len >
3957 MBEDTLS_SSL_OUT_CONTENT_LEN )
Jens Wiklander817466c2018-05-22 13:49:31 +02003958 {
Jerome Forissier79013242021-07-28 10:24:04 +02003959 MBEDTLS_SSL_DEBUG_MSG( 1,
3960 ( "psk identity or DHM size too long or SSL buffer too short" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02003961 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3962 }
3963
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003964 ssl->out_msg[header_len++] = (unsigned char)( content_len >> 8 );
3965 ssl->out_msg[header_len++] = (unsigned char)( content_len );
Jens Wiklander817466c2018-05-22 13:49:31 +02003966
3967 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
3968 (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003969 &ssl->out_msg[header_len], content_len,
Jens Wiklander817466c2018-05-22 13:49:31 +02003970 ssl->conf->f_rng, ssl->conf->p_rng );
3971 if( ret != 0 )
3972 {
3973 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
3974 return( ret );
3975 }
3976 }
3977 else
3978#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
3979#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3980 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
3981 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003982#if defined(MBEDTLS_USE_PSA_CRYPTO)
3983 /* Opaque PSKs are currently only supported for PSK-only suites. */
3984 if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
3985 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3986#endif /* MBEDTLS_USE_PSA_CRYPTO */
3987
Jens Wiklander817466c2018-05-22 13:49:31 +02003988 /*
3989 * ClientECDiffieHellmanPublic public;
3990 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003991 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
3992 &content_len,
3993 &ssl->out_msg[header_len],
3994 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
Jens Wiklander817466c2018-05-22 13:49:31 +02003995 ssl->conf->f_rng, ssl->conf->p_rng );
3996 if( ret != 0 )
3997 {
3998 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
3999 return( ret );
4000 }
4001
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004002 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4003 MBEDTLS_DEBUG_ECDH_Q );
Jens Wiklander817466c2018-05-22 13:49:31 +02004004 }
4005 else
4006#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
4007 {
4008 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4009 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4010 }
4011
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004012#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
4013 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
4014 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
4015 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
4016 ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
4017 {
Jerome Forissier79013242021-07-28 10:24:04 +02004018 MBEDTLS_SSL_DEBUG_MSG( 1,
4019 ( "skip PMS generation for opaque PSK" ) );
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004020 }
4021 else
4022#endif /* MBEDTLS_USE_PSA_CRYPTO &&
4023 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02004024 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
4025 ciphersuite_info->key_exchange ) ) != 0 )
4026 {
Jerome Forissier79013242021-07-28 10:24:04 +02004027 MBEDTLS_SSL_DEBUG_RET( 1,
4028 "mbedtls_ssl_psk_derive_premaster", ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02004029 return( ret );
4030 }
4031 }
4032 else
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004033#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02004034#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
4035 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
4036 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004037 header_len = 4;
4038 if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
4039 &content_len, 0 ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02004040 return( ret );
4041 }
4042 else
4043#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
4044#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
4045 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
4046 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004047 header_len = 4;
Jens Wiklander817466c2018-05-22 13:49:31 +02004048
4049 ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004050 ssl->out_msg + header_len,
4051 MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
4052 &content_len,
Jens Wiklander817466c2018-05-22 13:49:31 +02004053 ssl->conf->f_rng, ssl->conf->p_rng );
4054 if( ret != 0 )
4055 {
4056 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
4057 return( ret );
4058 }
4059
4060 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
4061 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
4062 ssl->conf->f_rng, ssl->conf->p_rng );
4063 if( ret != 0 )
4064 {
4065 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
4066 return( ret );
4067 }
4068 }
4069 else
4070#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
4071 {
4072 ((void) ciphersuite_info);
4073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4074 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4075 }
4076
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004077 ssl->out_msglen = header_len + content_len;
Jens Wiklander817466c2018-05-22 13:49:31 +02004078 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4079 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
4080
4081 ssl->state++;
4082
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004083 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02004084 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004085 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02004086 return( ret );
4087 }
4088
4089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
4090
4091 return( 0 );
4092}
4093
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004094#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02004095static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
4096{
4097 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004098 ssl->handshake->ciphersuite_info;
4099 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02004100
4101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
4102
4103 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
4104 {
4105 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
4106 return( ret );
4107 }
4108
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004109 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Jens Wiklander817466c2018-05-22 13:49:31 +02004110 {
4111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
4112 ssl->state++;
4113 return( 0 );
4114 }
4115
4116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4117 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4118}
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004119#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02004120static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
4121{
4122 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
4123 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004124 ssl->handshake->ciphersuite_info;
Jens Wiklander817466c2018-05-22 13:49:31 +02004125 size_t n = 0, offset = 0;
4126 unsigned char hash[48];
4127 unsigned char *hash_start = hash;
4128 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004129 size_t hashlen;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004130 void *rs_ctx = NULL;
Jens Wiklander817466c2018-05-22 13:49:31 +02004131
4132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
4133
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004134#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004135 if( ssl->handshake->ecrs_enabled &&
4136 ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign )
4137 {
4138 goto sign;
4139 }
4140#endif
4141
Jens Wiklander817466c2018-05-22 13:49:31 +02004142 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
4143 {
4144 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
4145 return( ret );
4146 }
4147
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004148 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Jens Wiklander817466c2018-05-22 13:49:31 +02004149 {
4150 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
4151 ssl->state++;
4152 return( 0 );
4153 }
4154
4155 if( ssl->client_auth == 0 || mbedtls_ssl_own_cert( ssl ) == NULL )
4156 {
4157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
4158 ssl->state++;
4159 return( 0 );
4160 }
4161
4162 if( mbedtls_ssl_own_key( ssl ) == NULL )
4163 {
4164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
4165 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
4166 }
4167
4168 /*
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004169 * Make a signature of the handshake digests
Jens Wiklander817466c2018-05-22 13:49:31 +02004170 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004171#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004172 if( ssl->handshake->ecrs_enabled )
4173 ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
4174
4175sign:
4176#endif
4177
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004178 ssl->handshake->calc_verify( ssl, hash, &hashlen );
Jens Wiklander817466c2018-05-22 13:49:31 +02004179
4180#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4181 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4182 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
4183 {
4184 /*
4185 * digitally-signed struct {
4186 * opaque md5_hash[16];
4187 * opaque sha_hash[20];
4188 * };
4189 *
4190 * md5_hash
4191 * MD5(handshake_messages);
4192 *
4193 * sha_hash
4194 * SHA(handshake_messages);
4195 */
Jens Wiklander817466c2018-05-22 13:49:31 +02004196 md_alg = MBEDTLS_MD_NONE;
4197
4198 /*
4199 * For ECDSA, default hash is SHA-1 only
4200 */
4201 if( mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECDSA ) )
4202 {
4203 hash_start += 16;
4204 hashlen -= 16;
4205 md_alg = MBEDTLS_MD_SHA1;
4206 }
4207 }
4208 else
4209#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
4210 MBEDTLS_SSL_PROTO_TLS1_1 */
4211#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4212 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
4213 {
4214 /*
4215 * digitally-signed struct {
4216 * opaque handshake_messages[handshake_messages_length];
4217 * };
4218 *
4219 * Taking shortcut here. We assume that the server always allows the
4220 * PRF Hash function and has sent it in the allowed signature
4221 * algorithms list received in the Certificate Request message.
4222 *
4223 * Until we encounter a server that does not, we will take this
4224 * shortcut.
4225 *
Jerome Forissier79013242021-07-28 10:24:04 +02004226 * Reason: Otherwise we should have running hashes for SHA512 and
4227 * SHA224 in order to satisfy 'weird' needs from the server
4228 * side.
Jens Wiklander817466c2018-05-22 13:49:31 +02004229 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004230 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jens Wiklander817466c2018-05-22 13:49:31 +02004231 {
4232 md_alg = MBEDTLS_MD_SHA384;
4233 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
4234 }
4235 else
4236 {
4237 md_alg = MBEDTLS_MD_SHA256;
4238 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
4239 }
4240 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
4241
4242 /* Info from md_alg will be used instead */
4243 hashlen = 0;
4244 offset = 2;
4245 }
4246 else
4247#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4248 {
4249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4250 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4251 }
4252
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004253#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004254 if( ssl->handshake->ecrs_enabled )
4255 rs_ctx = &ssl->handshake->ecrs_ctx.pk;
4256#endif
4257
4258 if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ),
4259 md_alg, hash_start, hashlen,
Jens Wiklander817466c2018-05-22 13:49:31 +02004260 ssl->out_msg + 6 + offset, &n,
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004261 ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02004262 {
4263 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004264#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004265 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
4266 ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
4267#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02004268 return( ret );
4269 }
4270
4271 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
4272 ssl->out_msg[5 + offset] = (unsigned char)( n );
4273
4274 ssl->out_msglen = 6 + n + offset;
4275 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4276 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
4277
4278 ssl->state++;
4279
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004280 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02004281 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004282 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02004283 return( ret );
4284 }
4285
4286 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
4287
4288 return( ret );
4289}
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004290#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02004291
4292#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4293static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
4294{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02004296 uint32_t lifetime;
4297 size_t ticket_len;
4298 unsigned char *ticket;
4299 const unsigned char *msg;
4300
4301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
4302
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004303 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02004304 {
4305 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
4306 return( ret );
4307 }
4308
4309 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
4310 {
4311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
Jerome Forissier79013242021-07-28 10:24:04 +02004312 mbedtls_ssl_send_alert_message(
4313 ssl,
4314 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4315 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Jens Wiklander817466c2018-05-22 13:49:31 +02004316 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
4317 }
4318
4319 /*
4320 * struct {
4321 * uint32 ticket_lifetime_hint;
4322 * opaque ticket<0..2^16-1>;
4323 * } NewSessionTicket;
4324 *
4325 * 0 . 3 ticket_lifetime_hint
4326 * 4 . 5 ticket_len (n)
4327 * 6 . 5+n ticket content
4328 */
4329 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
4330 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
4331 {
4332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
4333 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4334 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
4335 return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
4336 }
4337
4338 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
4339
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004340 lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) |
4341 ( msg[2] << 8 ) | ( msg[3] );
Jens Wiklander817466c2018-05-22 13:49:31 +02004342
4343 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
4344
4345 if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
4346 {
4347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
4348 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4349 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
4350 return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
4351 }
4352
Jerome Forissier79013242021-07-28 10:24:04 +02004353 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02004354
4355 /* We're not waiting for a NewSessionTicket message any more */
4356 ssl->handshake->new_session_ticket = 0;
4357 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
4358
4359 /*
4360 * Zero-length ticket means the server changed his mind and doesn't want
4361 * to send a ticket after all, so just forget it
4362 */
4363 if( ticket_len == 0 )
4364 return( 0 );
4365
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004366 if( ssl->session != NULL && ssl->session->ticket != NULL )
4367 {
4368 mbedtls_platform_zeroize( ssl->session->ticket,
4369 ssl->session->ticket_len );
4370 mbedtls_free( ssl->session->ticket );
4371 ssl->session->ticket = NULL;
4372 ssl->session->ticket_len = 0;
4373 }
4374
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004375 mbedtls_platform_zeroize( ssl->session_negotiate->ticket,
4376 ssl->session_negotiate->ticket_len );
Jens Wiklander817466c2018-05-22 13:49:31 +02004377 mbedtls_free( ssl->session_negotiate->ticket );
4378 ssl->session_negotiate->ticket = NULL;
4379 ssl->session_negotiate->ticket_len = 0;
4380
4381 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
4382 {
4383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
4384 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4385 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
4386 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
4387 }
4388
4389 memcpy( ticket, msg + 6, ticket_len );
4390
4391 ssl->session_negotiate->ticket = ticket;
4392 ssl->session_negotiate->ticket_len = ticket_len;
4393 ssl->session_negotiate->ticket_lifetime = lifetime;
4394
4395 /*
4396 * RFC 5077 section 3.4:
4397 * "If the client receives a session ticket from the server, then it
4398 * discards any Session ID that was sent in the ServerHello."
4399 */
4400 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
4401 ssl->session_negotiate->id_len = 0;
4402
4403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
4404
4405 return( 0 );
4406}
4407#endif /* MBEDTLS_SSL_SESSION_TICKETS */
4408
4409/*
4410 * SSL handshake -- client side -- single step
4411 */
4412int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
4413{
4414 int ret = 0;
4415
4416 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
4417 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4418
4419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
4420
4421 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4422 return( ret );
4423
4424#if defined(MBEDTLS_SSL_PROTO_DTLS)
4425 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4426 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
4427 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004428 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02004429 return( ret );
4430 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004431#endif /* MBEDTLS_SSL_PROTO_DTLS */
Jens Wiklander817466c2018-05-22 13:49:31 +02004432
4433 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
4434 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
4435#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4436 if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
4437 ssl->handshake->new_session_ticket != 0 )
4438 {
4439 ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET;
4440 }
4441#endif
4442
4443 switch( ssl->state )
4444 {
4445 case MBEDTLS_SSL_HELLO_REQUEST:
4446 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
4447 break;
4448
4449 /*
4450 * ==> ClientHello
4451 */
4452 case MBEDTLS_SSL_CLIENT_HELLO:
4453 ret = ssl_write_client_hello( ssl );
4454 break;
4455
4456 /*
4457 * <== ServerHello
4458 * Certificate
4459 * ( ServerKeyExchange )
4460 * ( CertificateRequest )
4461 * ServerHelloDone
4462 */
4463 case MBEDTLS_SSL_SERVER_HELLO:
4464 ret = ssl_parse_server_hello( ssl );
4465 break;
4466
4467 case MBEDTLS_SSL_SERVER_CERTIFICATE:
4468 ret = mbedtls_ssl_parse_certificate( ssl );
4469 break;
4470
4471 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
4472 ret = ssl_parse_server_key_exchange( ssl );
4473 break;
4474
4475 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
4476 ret = ssl_parse_certificate_request( ssl );
4477 break;
4478
4479 case MBEDTLS_SSL_SERVER_HELLO_DONE:
4480 ret = ssl_parse_server_hello_done( ssl );
4481 break;
4482
4483 /*
4484 * ==> ( Certificate/Alert )
4485 * ClientKeyExchange
4486 * ( CertificateVerify )
4487 * ChangeCipherSpec
4488 * Finished
4489 */
4490 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
4491 ret = mbedtls_ssl_write_certificate( ssl );
4492 break;
4493
4494 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
4495 ret = ssl_write_client_key_exchange( ssl );
4496 break;
4497
4498 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
4499 ret = ssl_write_certificate_verify( ssl );
4500 break;
4501
4502 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
4503 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
4504 break;
4505
4506 case MBEDTLS_SSL_CLIENT_FINISHED:
4507 ret = mbedtls_ssl_write_finished( ssl );
4508 break;
4509
4510 /*
4511 * <== ( NewSessionTicket )
4512 * ChangeCipherSpec
4513 * Finished
4514 */
4515#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4516 case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET:
4517 ret = ssl_parse_new_session_ticket( ssl );
4518 break;
4519#endif
4520
4521 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4522 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
4523 break;
4524
4525 case MBEDTLS_SSL_SERVER_FINISHED:
4526 ret = mbedtls_ssl_parse_finished( ssl );
4527 break;
4528
4529 case MBEDTLS_SSL_FLUSH_BUFFERS:
4530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
4531 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
4532 break;
4533
4534 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
4535 mbedtls_ssl_handshake_wrapup( ssl );
4536 break;
4537
4538 default:
4539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
4540 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4541 }
4542
4543 return( ret );
4544}
4545#endif /* MBEDTLS_SSL_CLI_C */