blob: 2efb13cc33c6ba3be70c0e9ba8817efea9ae50cf [file] [log] [blame]
Jens Wiklander817466c2018-05-22 13:49:31 +02001/*
2 * SSLv3/TLSv1 server-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_SRV_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"
Jens Wiklander3d3b0592019-03-20 15:30:29 +010036#include "mbedtls/platform_util.h"
Jerome Forissier039e02d2022-08-09 17:10:15 +020037#include "constant_time_internal.h"
38#include "mbedtls/constant_time.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020039
40#include <string.h>
41
42#if defined(MBEDTLS_ECP_C)
43#include "mbedtls/ecp.h"
44#endif
45
46#if defined(MBEDTLS_HAVE_TIME)
47#include "mbedtls/platform_time.h"
48#endif
49
Jens Wiklander817466c2018-05-22 13:49:31 +020050#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
51int mbedtls_ssl_set_client_transport_id( mbedtls_ssl_context *ssl,
52 const unsigned char *info,
53 size_t ilen )
54{
55 if( ssl->conf->endpoint != MBEDTLS_SSL_IS_SERVER )
56 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
57
58 mbedtls_free( ssl->cli_id );
59
60 if( ( ssl->cli_id = mbedtls_calloc( 1, ilen ) ) == NULL )
61 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
62
63 memcpy( ssl->cli_id, info, ilen );
64 ssl->cli_id_len = ilen;
65
66 return( 0 );
67}
68
69void mbedtls_ssl_conf_dtls_cookies( mbedtls_ssl_config *conf,
70 mbedtls_ssl_cookie_write_t *f_cookie_write,
71 mbedtls_ssl_cookie_check_t *f_cookie_check,
72 void *p_cookie )
73{
74 conf->f_cookie_write = f_cookie_write;
75 conf->f_cookie_check = f_cookie_check;
76 conf->p_cookie = p_cookie;
77}
78#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
79
80#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Jerome Forissier039e02d2022-08-09 17:10:15 +020081MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +020082static int ssl_parse_servername_ext( mbedtls_ssl_context *ssl,
83 const unsigned char *buf,
84 size_t len )
85{
Jerome Forissier11fa71b2020-04-20 17:17:56 +020086 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +020087 size_t servername_list_size, hostname_len;
88 const unsigned char *p;
89
90 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
91
Jens Wiklander3d3b0592019-03-20 15:30:29 +010092 if( len < 2 )
93 {
94 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
95 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
96 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
97 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
98 }
Jens Wiklander817466c2018-05-22 13:49:31 +020099 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
100 if( servername_list_size + 2 != len )
101 {
102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
103 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
104 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
105 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
106 }
107
108 p = buf + 2;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100109 while( servername_list_size > 2 )
Jens Wiklander817466c2018-05-22 13:49:31 +0200110 {
111 hostname_len = ( ( p[1] << 8 ) | p[2] );
112 if( hostname_len + 3 > servername_list_size )
113 {
114 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
115 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
116 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
117 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
118 }
119
120 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
121 {
122 ret = ssl->conf->f_sni( ssl->conf->p_sni,
123 ssl, p + 3, hostname_len );
124 if( ret != 0 )
125 {
126 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
127 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
128 MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME );
129 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
130 }
131 return( 0 );
132 }
133
134 servername_list_size -= hostname_len + 3;
135 p += hostname_len + 3;
136 }
137
138 if( servername_list_size != 0 )
139 {
140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
141 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
142 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
143 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
144 }
145
146 return( 0 );
147}
148#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
149
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200150#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200151MBEDTLS_CHECK_RETURN_CRITICAL
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200152static int ssl_conf_has_psk_or_cb( mbedtls_ssl_config const *conf )
153{
154 if( conf->f_psk != NULL )
155 return( 1 );
156
157 if( conf->psk_identity_len == 0 || conf->psk_identity == NULL )
158 return( 0 );
159
160 if( conf->psk != NULL && conf->psk_len != 0 )
161 return( 1 );
162
163#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerome Forissier79013242021-07-28 10:24:04 +0200164 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200165 return( 1 );
166#endif /* MBEDTLS_USE_PSA_CRYPTO */
167
168 return( 0 );
169}
170
171#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200172MBEDTLS_CHECK_RETURN_CRITICAL
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200173static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
174{
175 if( ssl->conf->f_psk != NULL )
176 {
177 /* If we've used a callback to select the PSK,
178 * the static configuration is irrelevant. */
179
Jerome Forissier79013242021-07-28 10:24:04 +0200180 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200181 return( 1 );
182
183 return( 0 );
184 }
185
Jerome Forissier79013242021-07-28 10:24:04 +0200186 if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) )
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200187 return( 1 );
188
189 return( 0 );
190}
191#endif /* MBEDTLS_USE_PSA_CRYPTO */
192#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
193
Jerome Forissier039e02d2022-08-09 17:10:15 +0200194MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +0200195static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
196 const unsigned char *buf,
197 size_t len )
198{
199#if defined(MBEDTLS_SSL_RENEGOTIATION)
200 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
201 {
202 /* Check verify-data in constant-time. The length OTOH is no secret */
203 if( len != 1 + ssl->verify_data_len ||
204 buf[0] != ssl->verify_data_len ||
Jerome Forissier039e02d2022-08-09 17:10:15 +0200205 mbedtls_ct_memcmp( buf + 1, ssl->peer_verify_data,
Jens Wiklander817466c2018-05-22 13:49:31 +0200206 ssl->verify_data_len ) != 0 )
207 {
208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
209 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
210 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
211 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
212 }
213 }
214 else
215#endif /* MBEDTLS_SSL_RENEGOTIATION */
216 {
217 if( len != 1 || buf[0] != 0x0 )
218 {
219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
220 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
221 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
222 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
223 }
224
225 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
226 }
227
228 return( 0 );
229}
230
231#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200232 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +0200233
234/*
235 * Status of the implementation of signature-algorithms extension:
236 *
237 * Currently, we are only considering the signature-algorithm extension
238 * to pick a ciphersuite which allows us to send the ServerKeyExchange
239 * message with a signature-hash combination that the user allows.
240 *
241 * We do *not* check whether all certificates in our certificate
242 * chain are signed with an allowed signature-hash pair.
243 * This needs to be done at a later stage.
244 *
245 */
Jerome Forissier039e02d2022-08-09 17:10:15 +0200246MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +0200247static int ssl_parse_signature_algorithms_ext( mbedtls_ssl_context *ssl,
248 const unsigned char *buf,
249 size_t len )
250{
251 size_t sig_alg_list_size;
252
253 const unsigned char *p;
254 const unsigned char *end = buf + len;
255
256 mbedtls_md_type_t md_cur;
257 mbedtls_pk_type_t sig_cur;
258
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100259 if ( len < 2 ) {
260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
261 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
262 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
263 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
264 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200265 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
266 if( sig_alg_list_size + 2 != len ||
267 sig_alg_list_size % 2 != 0 )
268 {
269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
270 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
271 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
272 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
273 }
274
275 /* Currently we only guarantee signing the ServerKeyExchange message according
276 * to the constraints specified in this extension (see above), so it suffices
277 * to remember only one suitable hash for each possible signature algorithm.
278 *
279 * This will change when we also consider certificate signatures,
280 * in which case we will need to remember the whole signature-hash
281 * pair list from the extension.
282 */
283
284 for( p = buf + 2; p < end; p += 2 )
285 {
286 /* Silently ignore unknown signature or hash algorithms. */
287
288 if( ( sig_cur = mbedtls_ssl_pk_alg_from_sig( p[1] ) ) == MBEDTLS_PK_NONE )
289 {
290 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext"
291 " unknown sig alg encoding %d", p[1] ) );
292 continue;
293 }
294
295 /* Check if we support the hash the user proposes */
296 md_cur = mbedtls_ssl_md_alg_from_hash( p[0] );
297 if( md_cur == MBEDTLS_MD_NONE )
298 {
299 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:"
300 " unknown hash alg encoding %d", p[0] ) );
301 continue;
302 }
303
304 if( mbedtls_ssl_check_sig_hash( ssl, md_cur ) == 0 )
305 {
306 mbedtls_ssl_sig_hash_set_add( &ssl->handshake->hash_algs, sig_cur, md_cur );
307 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:"
Jerome Forissier79013242021-07-28 10:24:04 +0200308 " match sig %u and hash %u",
309 (unsigned) sig_cur, (unsigned) md_cur ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200310 }
311 else
312 {
313 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: "
Jerome Forissier79013242021-07-28 10:24:04 +0200314 "hash alg %u not supported", (unsigned) md_cur ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200315 }
316 }
317
318 return( 0 );
319}
320#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200321 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +0200322
323#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
324 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200325MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +0200326static int ssl_parse_supported_elliptic_curves( mbedtls_ssl_context *ssl,
327 const unsigned char *buf,
328 size_t len )
329{
330 size_t list_size, our_size;
331 const unsigned char *p;
332 const mbedtls_ecp_curve_info *curve_info, **curves;
333
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100334 if ( len < 2 ) {
335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
336 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
337 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
338 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
339 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200340 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
341 if( list_size + 2 != len ||
342 list_size % 2 != 0 )
343 {
344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
345 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
346 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
347 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
348 }
349
350 /* Should never happen unless client duplicates the extension */
351 if( ssl->handshake->curves != NULL )
352 {
353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
354 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
355 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
356 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
357 }
358
359 /* Don't allow our peer to make us allocate too much memory,
360 * and leave room for a final 0 */
361 our_size = list_size / 2 + 1;
362 if( our_size > MBEDTLS_ECP_DP_MAX )
363 our_size = MBEDTLS_ECP_DP_MAX;
364
365 if( ( curves = mbedtls_calloc( our_size, sizeof( *curves ) ) ) == NULL )
366 {
367 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
368 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
369 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
370 }
371
372 ssl->handshake->curves = curves;
373
374 p = buf + 2;
375 while( list_size > 0 && our_size > 1 )
376 {
377 curve_info = mbedtls_ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
378
379 if( curve_info != NULL )
380 {
381 *curves++ = curve_info;
382 our_size--;
383 }
384
385 list_size -= 2;
386 p += 2;
387 }
388
389 return( 0 );
390}
391
Jerome Forissier039e02d2022-08-09 17:10:15 +0200392MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +0200393static int ssl_parse_supported_point_formats( mbedtls_ssl_context *ssl,
394 const unsigned char *buf,
395 size_t len )
396{
397 size_t list_size;
398 const unsigned char *p;
399
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100400 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Jens Wiklander817466c2018-05-22 13:49:31 +0200401 {
402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
403 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
404 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
405 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
406 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100407 list_size = buf[0];
Jens Wiklander817466c2018-05-22 13:49:31 +0200408
409 p = buf + 1;
410 while( list_size > 0 )
411 {
412 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
413 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
414 {
415#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
416 ssl->handshake->ecdh_ctx.point_format = p[0];
417#endif
418#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
419 ssl->handshake->ecjpake_ctx.point_format = p[0];
420#endif
421 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
422 return( 0 );
423 }
424
425 list_size--;
426 p++;
427 }
428
429 return( 0 );
430}
431#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
432 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
433
434#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200435MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +0200436static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
437 const unsigned char *buf,
438 size_t len )
439{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200440 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200441
442 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
443 {
444 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
445 return( 0 );
446 }
447
448 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
449 buf, len ) ) != 0 )
450 {
451 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
452 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
453 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
454 return( ret );
455 }
456
457 /* Only mark the extension as OK when we're sure it is */
458 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK;
459
460 return( 0 );
461}
462#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
463
464#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200465MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +0200466static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
467 const unsigned char *buf,
468 size_t len )
469{
470 if( len != 1 || buf[0] >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID )
471 {
472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
473 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
474 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
475 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
476 }
477
478 ssl->session_negotiate->mfl_code = buf[0];
479
480 return( 0 );
481}
482#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
483
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200484#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200485MBEDTLS_CHECK_RETURN_CRITICAL
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200486static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
487 const unsigned char *buf,
488 size_t len )
489{
490 size_t peer_cid_len;
491
492 /* CID extension only makes sense in DTLS */
493 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
494 {
495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
496 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
497 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
498 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
499 }
500
501 /*
502 * Quoting draft-ietf-tls-dtls-connection-id-05
503 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
504 *
505 * struct {
506 * opaque cid<0..2^8-1>;
507 * } ConnectionId;
508 */
509
510 if( len < 1 )
511 {
512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
513 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
514 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
515 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
516 }
517
518 peer_cid_len = *buf++;
519 len--;
520
521 if( len != peer_cid_len )
522 {
523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
524 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
525 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
526 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
527 }
528
529 /* Ignore CID if the user has disabled its use. */
530 if( ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
531 {
532 /* Leave ssl->handshake->cid_in_use in its default
533 * value of MBEDTLS_SSL_CID_DISABLED. */
534 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Client sent CID extension, but CID disabled" ) );
535 return( 0 );
536 }
537
538 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
539 {
540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
541 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
542 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
543 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
544 }
545
546 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
547 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
548 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
549
550 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
551 MBEDTLS_SSL_DEBUG_BUF( 3, "Client CID", buf, peer_cid_len );
552
553 return( 0 );
554}
555#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
556
Jens Wiklander817466c2018-05-22 13:49:31 +0200557#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200558MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +0200559static int ssl_parse_truncated_hmac_ext( mbedtls_ssl_context *ssl,
560 const unsigned char *buf,
561 size_t len )
562{
563 if( len != 0 )
564 {
565 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
566 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
567 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
568 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
569 }
570
571 ((void) buf);
572
573 if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
574 ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED;
575
576 return( 0 );
577}
578#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
579
580#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200581MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +0200582static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
583 const unsigned char *buf,
584 size_t len )
585{
586 if( len != 0 )
587 {
588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
589 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
590 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
591 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
592 }
593
594 ((void) buf);
595
596 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
597 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
598 {
599 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
600 }
601
602 return( 0 );
603}
604#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
605
606#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200607MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +0200608static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
609 const unsigned char *buf,
610 size_t len )
611{
612 if( len != 0 )
613 {
614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
615 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
616 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
617 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
618 }
619
620 ((void) buf);
621
622 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED &&
623 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
624 {
625 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
626 }
627
628 return( 0 );
629}
630#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
631
632#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200633MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +0200634static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
635 unsigned char *buf,
636 size_t len )
637{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200638 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200639 mbedtls_ssl_session session;
640
641 mbedtls_ssl_session_init( &session );
642
643 if( ssl->conf->f_ticket_parse == NULL ||
644 ssl->conf->f_ticket_write == NULL )
645 {
646 return( 0 );
647 }
648
649 /* Remember the client asked us to send a new ticket */
650 ssl->handshake->new_session_ticket = 1;
651
Jerome Forissier79013242021-07-28 10:24:04 +0200652 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, len ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200653
654 if( len == 0 )
655 return( 0 );
656
657#if defined(MBEDTLS_SSL_RENEGOTIATION)
658 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
659 {
660 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
661 return( 0 );
662 }
663#endif /* MBEDTLS_SSL_RENEGOTIATION */
664
665 /*
666 * Failures are ok: just ignore the ticket and proceed.
667 */
668 if( ( ret = ssl->conf->f_ticket_parse( ssl->conf->p_ticket, &session,
669 buf, len ) ) != 0 )
670 {
671 mbedtls_ssl_session_free( &session );
672
673 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
674 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is not authentic" ) );
675 else if( ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED )
676 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is expired" ) );
677 else
678 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_parse", ret );
679
680 return( 0 );
681 }
682
683 /*
684 * Keep the session ID sent by the client, since we MUST send it back to
685 * inform them we're accepting the ticket (RFC 5077 section 3.4)
686 */
687 session.id_len = ssl->session_negotiate->id_len;
688 memcpy( &session.id, ssl->session_negotiate->id, session.id_len );
689
690 mbedtls_ssl_session_free( ssl->session_negotiate );
691 memcpy( ssl->session_negotiate, &session, sizeof( mbedtls_ssl_session ) );
692
693 /* Zeroize instead of free as we copied the content */
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100694 mbedtls_platform_zeroize( &session, sizeof( mbedtls_ssl_session ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200695
696 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
697
698 ssl->handshake->resume = 1;
699
700 /* Don't send a new ticket after all, this one is OK */
701 ssl->handshake->new_session_ticket = 0;
702
703 return( 0 );
704}
705#endif /* MBEDTLS_SSL_SESSION_TICKETS */
706
707#if defined(MBEDTLS_SSL_ALPN)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200708MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +0200709static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
710 const unsigned char *buf, size_t len )
711{
712 size_t list_len, cur_len, ours_len;
713 const unsigned char *theirs, *start, *end;
714 const char **ours;
715
716 /* If ALPN not configured, just ignore the extension */
717 if( ssl->conf->alpn_list == NULL )
718 return( 0 );
719
720 /*
721 * opaque ProtocolName<1..2^8-1>;
722 *
723 * struct {
724 * ProtocolName protocol_name_list<2..2^16-1>
725 * } ProtocolNameList;
726 */
727
728 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
729 if( len < 4 )
730 {
731 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
732 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
733 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
734 }
735
736 list_len = ( buf[0] << 8 ) | buf[1];
737 if( list_len != len - 2 )
738 {
739 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
740 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
741 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
742 }
743
744 /*
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100745 * Validate peer's list (lengths)
Jens Wiklander817466c2018-05-22 13:49:31 +0200746 */
747 start = buf + 2;
748 end = buf + len;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100749 for( theirs = start; theirs != end; theirs += cur_len )
750 {
751 cur_len = *theirs++;
752
753 /* Current identifier must fit in list */
754 if( cur_len > (size_t)( end - theirs ) )
755 {
756 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
757 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
758 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
759 }
760
761 /* Empty strings MUST NOT be included */
762 if( cur_len == 0 )
763 {
764 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
765 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
766 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
767 }
768 }
769
770 /*
771 * Use our order of preference
772 */
Jens Wiklander817466c2018-05-22 13:49:31 +0200773 for( ours = ssl->conf->alpn_list; *ours != NULL; ours++ )
774 {
775 ours_len = strlen( *ours );
776 for( theirs = start; theirs != end; theirs += cur_len )
777 {
Jens Wiklander817466c2018-05-22 13:49:31 +0200778 cur_len = *theirs++;
779
Jens Wiklander817466c2018-05-22 13:49:31 +0200780 if( cur_len == ours_len &&
781 memcmp( theirs, *ours, cur_len ) == 0 )
782 {
783 ssl->alpn_chosen = *ours;
784 return( 0 );
785 }
786 }
787 }
788
789 /* If we get there, no match was found */
790 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
791 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
792 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
793}
794#endif /* MBEDTLS_SSL_ALPN */
795
Jerome Forissier79013242021-07-28 10:24:04 +0200796#if defined(MBEDTLS_SSL_DTLS_SRTP)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200797MBEDTLS_CHECK_RETURN_CRITICAL
Jerome Forissier79013242021-07-28 10:24:04 +0200798static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
799 const unsigned char *buf,
800 size_t len )
801{
802 mbedtls_ssl_srtp_profile client_protection = MBEDTLS_TLS_SRTP_UNSET;
803 size_t i,j;
804 size_t profile_length;
805 uint16_t mki_length;
806 /*! 2 bytes for profile length and 1 byte for mki len */
807 const size_t size_of_lengths = 3;
808
809 /* If use_srtp is not configured, just ignore the extension */
810 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
811 ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
812 ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
813 {
814 return( 0 );
815 }
816
817 /* RFC5764 section 4.1.1
818 * uint8 SRTPProtectionProfile[2];
819 *
820 * struct {
821 * SRTPProtectionProfiles SRTPProtectionProfiles;
822 * opaque srtp_mki<0..255>;
823 * } UseSRTPData;
824
825 * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
826 */
827
828 /*
829 * Min length is 5: at least one protection profile(2 bytes)
830 * and length(2 bytes) + srtp_mki length(1 byte)
831 * Check here that we have at least 2 bytes of protection profiles length
832 * and one of srtp_mki length
833 */
834 if( len < size_of_lengths )
835 {
836 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
837 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
838 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
839 }
840
841 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
842
843 /* first 2 bytes are protection profile length(in bytes) */
844 profile_length = ( buf[0] << 8 ) | buf[1];
845 buf += 2;
846
847 /* The profile length cannot be bigger than input buffer size - lengths fields */
848 if( profile_length > len - size_of_lengths ||
849 profile_length % 2 != 0 ) /* profiles are 2 bytes long, so the length must be even */
850 {
851 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
852 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
853 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
854 }
855 /*
856 * parse the extension list values are defined in
857 * http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml
858 */
859 for( j = 0; j < profile_length; j += 2 )
860 {
861 uint16_t protection_profile_value = buf[j] << 8 | buf[j + 1];
862 client_protection = mbedtls_ssl_check_srtp_profile_value( protection_profile_value );
863
864 if( client_protection != MBEDTLS_TLS_SRTP_UNSET )
865 {
866 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
867 mbedtls_ssl_get_srtp_profile_as_string(
868 client_protection ) ) );
869 }
870 else
871 {
872 continue;
873 }
874 /* check if suggested profile is in our list */
875 for( i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
876 {
877 if( client_protection == ssl->conf->dtls_srtp_profile_list[i] )
878 {
879 ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
880 MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
881 mbedtls_ssl_get_srtp_profile_as_string(
882 client_protection ) ) );
883 break;
884 }
885 }
886 if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_TLS_SRTP_UNSET )
887 break;
888 }
889 buf += profile_length; /* buf points to the mki length */
890 mki_length = *buf;
891 buf++;
892
893 if( mki_length > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ||
894 mki_length + profile_length + size_of_lengths != len )
895 {
896 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
897 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
898 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
899 }
900
901 /* Parse the mki only if present and mki is supported locally */
902 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED &&
903 mki_length > 0 )
904 {
905 ssl->dtls_srtp_info.mki_len = mki_length;
906
907 memcpy( ssl->dtls_srtp_info.mki_value, buf, mki_length );
908
909 MBEDTLS_SSL_DEBUG_BUF( 3, "using mki", ssl->dtls_srtp_info.mki_value,
910 ssl->dtls_srtp_info.mki_len );
911 }
912
913 return( 0 );
914}
915#endif /* MBEDTLS_SSL_DTLS_SRTP */
916
Jens Wiklander817466c2018-05-22 13:49:31 +0200917/*
918 * Auxiliary functions for ServerHello parsing and related actions
919 */
920
921#if defined(MBEDTLS_X509_CRT_PARSE_C)
922/*
923 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
924 */
925#if defined(MBEDTLS_ECDSA_C)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200926MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +0200927static int ssl_check_key_curve( mbedtls_pk_context *pk,
928 const mbedtls_ecp_curve_info **curves )
929{
930 const mbedtls_ecp_curve_info **crv = curves;
931 mbedtls_ecp_group_id grp_id = mbedtls_pk_ec( *pk )->grp.id;
932
933 while( *crv != NULL )
934 {
935 if( (*crv)->grp_id == grp_id )
936 return( 0 );
937 crv++;
938 }
939
940 return( -1 );
941}
942#endif /* MBEDTLS_ECDSA_C */
943
944/*
945 * Try picking a certificate for this ciphersuite,
946 * return 0 on success and -1 on failure.
947 */
Jerome Forissier039e02d2022-08-09 17:10:15 +0200948MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +0200949static int ssl_pick_cert( mbedtls_ssl_context *ssl,
950 const mbedtls_ssl_ciphersuite_t * ciphersuite_info )
951{
952 mbedtls_ssl_key_cert *cur, *list, *fallback = NULL;
953 mbedtls_pk_type_t pk_alg =
954 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
955 uint32_t flags;
956
957#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
958 if( ssl->handshake->sni_key_cert != NULL )
959 list = ssl->handshake->sni_key_cert;
960 else
961#endif
962 list = ssl->conf->key_cert;
963
964 if( pk_alg == MBEDTLS_PK_NONE )
965 return( 0 );
966
967 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
968
969 if( list == NULL )
970 {
971 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
972 return( -1 );
973 }
974
975 for( cur = list; cur != NULL; cur = cur->next )
976 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200977 flags = 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200978 MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
979 cur->cert );
980
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100981 if( ! mbedtls_pk_can_do( &cur->cert->pk, pk_alg ) )
Jens Wiklander817466c2018-05-22 13:49:31 +0200982 {
983 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
984 continue;
985 }
986
987 /*
988 * This avoids sending the client a cert it'll reject based on
989 * keyUsage or other extensions.
990 *
991 * It also allows the user to provision different certificates for
992 * different uses based on keyUsage, eg if they want to avoid signing
993 * and decrypting with the same RSA key.
994 */
995 if( mbedtls_ssl_check_cert_usage( cur->cert, ciphersuite_info,
996 MBEDTLS_SSL_IS_SERVER, &flags ) != 0 )
997 {
998 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
999 "(extended) key usage extension" ) );
1000 continue;
1001 }
1002
1003#if defined(MBEDTLS_ECDSA_C)
1004 if( pk_alg == MBEDTLS_PK_ECDSA &&
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001005 ssl_check_key_curve( &cur->cert->pk, ssl->handshake->curves ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02001006 {
1007 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
1008 continue;
1009 }
1010#endif
1011
1012 /*
1013 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
1014 * present them a SHA-higher cert rather than failing if it's the only
1015 * one we got that satisfies the other conditions.
1016 */
1017 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 &&
1018 cur->cert->sig_md != MBEDTLS_MD_SHA1 )
1019 {
1020 if( fallback == NULL )
1021 fallback = cur;
1022 {
1023 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
1024 "sha-2 with pre-TLS 1.2 client" ) );
1025 continue;
1026 }
1027 }
1028
1029 /* If we get there, we got a winner */
1030 break;
1031 }
1032
1033 if( cur == NULL )
1034 cur = fallback;
1035
1036 /* Do not update ssl->handshake->key_cert unless there is a match */
1037 if( cur != NULL )
1038 {
1039 ssl->handshake->key_cert = cur;
1040 MBEDTLS_SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
1041 ssl->handshake->key_cert->cert );
1042 return( 0 );
1043 }
1044
1045 return( -1 );
1046}
1047#endif /* MBEDTLS_X509_CRT_PARSE_C */
1048
1049/*
1050 * Check if a given ciphersuite is suitable for use with our config/keys/etc
1051 * Sets ciphersuite_info only if the suite matches.
1052 */
Jerome Forissier039e02d2022-08-09 17:10:15 +02001053MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02001054static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id,
1055 const mbedtls_ssl_ciphersuite_t **ciphersuite_info )
1056{
1057 const mbedtls_ssl_ciphersuite_t *suite_info;
1058
1059#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001060 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02001061 mbedtls_pk_type_t sig_type;
1062#endif
1063
1064 suite_info = mbedtls_ssl_ciphersuite_from_id( suite_id );
1065 if( suite_info == NULL )
1066 {
1067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1068 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1069 }
1070
Jerome Forissier79013242021-07-28 10:24:04 +02001071 MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %#04x (%s)",
1072 (unsigned int) suite_id, suite_info->name ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02001073
1074 if( suite_info->min_minor_ver > ssl->minor_ver ||
1075 suite_info->max_minor_ver < ssl->minor_ver )
1076 {
1077 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
1078 return( 0 );
1079 }
1080
1081#if defined(MBEDTLS_SSL_PROTO_DTLS)
1082 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1083 ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
1084 return( 0 );
1085#endif
1086
1087#if defined(MBEDTLS_ARC4_C)
1088 if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED &&
1089 suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
1090 {
1091 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) );
1092 return( 0 );
1093 }
1094#endif
1095
1096#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1097 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
1098 ( ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK ) == 0 )
1099 {
1100 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake "
1101 "not configured or ext missing" ) );
1102 return( 0 );
1103 }
1104#endif
1105
1106
1107#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
1108 if( mbedtls_ssl_ciphersuite_uses_ec( suite_info ) &&
1109 ( ssl->handshake->curves == NULL ||
1110 ssl->handshake->curves[0] == NULL ) )
1111 {
1112 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
1113 "no common elliptic curve" ) );
1114 return( 0 );
1115 }
1116#endif
1117
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001118#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02001119 /* If the ciphersuite requires a pre-shared key and we don't
1120 * have one, skip it now rather than failing later */
1121 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001122 ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02001123 {
1124 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
1125 return( 0 );
1126 }
1127#endif
1128
1129#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001130 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02001131 /* If the ciphersuite requires signing, check whether
1132 * a suitable hash algorithm is present. */
1133 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1134 {
1135 sig_type = mbedtls_ssl_get_ciphersuite_sig_alg( suite_info );
1136 if( sig_type != MBEDTLS_PK_NONE &&
1137 mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_type ) == MBEDTLS_MD_NONE )
1138 {
1139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm "
Jerome Forissier79013242021-07-28 10:24:04 +02001140 "for signature algorithm %u", (unsigned) sig_type ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02001141 return( 0 );
1142 }
1143 }
1144
1145#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001146 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02001147
1148#if defined(MBEDTLS_X509_CRT_PARSE_C)
1149 /*
1150 * Final check: if ciphersuite requires us to have a
1151 * certificate/key of a particular type:
1152 * - select the appropriate certificate if we have one, or
1153 * - try the next ciphersuite if we don't
1154 * This must be done last since we modify the key_cert list.
1155 */
1156 if( ssl_pick_cert( ssl, suite_info ) != 0 )
1157 {
1158 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
1159 "no suitable certificate" ) );
1160 return( 0 );
1161 }
1162#endif
1163
1164 *ciphersuite_info = suite_info;
1165 return( 0 );
1166}
1167
1168#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
Jerome Forissier039e02d2022-08-09 17:10:15 +02001169MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02001170static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl )
1171{
1172 int ret, got_common_suite;
1173 unsigned int i, j;
1174 size_t n;
1175 unsigned int ciph_len, sess_len, chal_len;
1176 unsigned char *buf, *p;
1177 const int *ciphersuites;
1178 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1179
1180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
1181
1182#if defined(MBEDTLS_SSL_RENEGOTIATION)
1183 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
1184 {
1185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
1186 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1187 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1188 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1189 }
1190#endif /* MBEDTLS_SSL_RENEGOTIATION */
1191
1192 buf = ssl->in_hdr;
1193
1194 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1195
1196 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
1197 buf[2] ) );
1198 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
1199 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
1200 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
1201 buf[3], buf[4] ) );
1202
1203 /*
1204 * SSLv2 Client Hello
1205 *
1206 * Record layer:
1207 * 0 . 1 message length
1208 *
1209 * SSL layer:
1210 * 2 . 2 message type
1211 * 3 . 4 protocol version
1212 */
1213 if( buf[2] != MBEDTLS_SSL_HS_CLIENT_HELLO ||
1214 buf[3] != MBEDTLS_SSL_MAJOR_VERSION_3 )
1215 {
1216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1217 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1218 }
1219
1220 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1221
1222 if( n < 17 || n > 512 )
1223 {
1224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1225 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1226 }
1227
1228 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1229 ssl->minor_ver = ( buf[4] <= ssl->conf->max_minor_ver )
1230 ? buf[4] : ssl->conf->max_minor_ver;
1231
1232 if( ssl->minor_ver < ssl->conf->min_minor_ver )
1233 {
1234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
1235 " [%d:%d] < [%d:%d]",
1236 ssl->major_ver, ssl->minor_ver,
1237 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
1238
1239 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1240 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
1241 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1242 }
1243
1244 ssl->handshake->max_major_ver = buf[3];
1245 ssl->handshake->max_minor_ver = buf[4];
1246
1247 if( ( ret = mbedtls_ssl_fetch_input( ssl, 2 + n ) ) != 0 )
1248 {
1249 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
1250 return( ret );
1251 }
1252
1253 ssl->handshake->update_checksum( ssl, buf + 2, n );
1254
1255 buf = ssl->in_msg;
1256 n = ssl->in_left - 5;
1257
1258 /*
1259 * 0 . 1 ciphersuitelist length
1260 * 2 . 3 session id length
1261 * 4 . 5 challenge length
1262 * 6 . .. ciphersuitelist
1263 * .. . .. session id
1264 * .. . .. challenge
1265 */
1266 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, n );
1267
1268 ciph_len = ( buf[0] << 8 ) | buf[1];
1269 sess_len = ( buf[2] << 8 ) | buf[3];
1270 chal_len = ( buf[4] << 8 ) | buf[5];
1271
Jerome Forissier79013242021-07-28 10:24:04 +02001272 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciph_len: %u, sess_len: %u, chal_len: %u",
Jens Wiklander817466c2018-05-22 13:49:31 +02001273 ciph_len, sess_len, chal_len ) );
1274
1275 /*
1276 * Make sure each parameter length is valid
1277 */
1278 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1279 {
1280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1281 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1282 }
1283
1284 if( sess_len > 32 )
1285 {
1286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1287 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1288 }
1289
1290 if( chal_len < 8 || chal_len > 32 )
1291 {
1292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1293 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1294 }
1295
1296 if( n != 6 + ciph_len + sess_len + chal_len )
1297 {
1298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1299 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1300 }
1301
1302 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1303 buf + 6, ciph_len );
1304 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
1305 buf + 6 + ciph_len, sess_len );
1306 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, challenge",
1307 buf + 6 + ciph_len + sess_len, chal_len );
1308
1309 p = buf + 6 + ciph_len;
1310 ssl->session_negotiate->id_len = sess_len;
1311 memset( ssl->session_negotiate->id, 0,
1312 sizeof( ssl->session_negotiate->id ) );
1313 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->id_len );
1314
1315 p += sess_len;
1316 memset( ssl->handshake->randbytes, 0, 64 );
1317 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1318
1319 /*
1320 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1321 */
1322 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1323 {
1324 if( p[0] == 0 && p[1] == 0 && p[2] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
1325 {
1326 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1327#if defined(MBEDTLS_SSL_RENEGOTIATION)
1328 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
1329 {
1330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
1331 "during renegotiation" ) );
1332
1333 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1334 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1335 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1336 }
1337#endif /* MBEDTLS_SSL_RENEGOTIATION */
1338 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
1339 break;
1340 }
1341 }
1342
1343#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
1344 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1345 {
1346 if( p[0] == 0 &&
Jerome Forissier039e02d2022-08-09 17:10:15 +02001347 MBEDTLS_GET_UINT16_BE(p, 1) != MBEDTLS_SSL_FALLBACK_SCSV_VALUE )
Jens Wiklander817466c2018-05-22 13:49:31 +02001348 {
1349 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
1350
1351 if( ssl->minor_ver < ssl->conf->max_minor_ver )
1352 {
1353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
1354
1355 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1356 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1357
1358 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1359 }
1360
1361 break;
1362 }
1363 }
1364#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
1365
1366 got_common_suite = 0;
1367 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
1368 ciphersuite_info = NULL;
1369#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1370 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1371 for( i = 0; ciphersuites[i] != 0; i++ )
1372#else
1373 for( i = 0; ciphersuites[i] != 0; i++ )
1374 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1375#endif
1376 {
1377 if( p[0] != 0 ||
Jerome Forissier039e02d2022-08-09 17:10:15 +02001378 MBEDTLS_GET_UINT16_BE(p, 1) != ciphersuites[i] )
Jens Wiklander817466c2018-05-22 13:49:31 +02001379 continue;
1380
1381 got_common_suite = 1;
1382
1383 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1384 &ciphersuite_info ) ) != 0 )
1385 return( ret );
1386
1387 if( ciphersuite_info != NULL )
1388 goto have_ciphersuite_v2;
1389 }
1390
1391 if( got_common_suite )
1392 {
1393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1394 "but none of them usable" ) );
1395 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
1396 }
1397 else
1398 {
1399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1400 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
1401 }
1402
1403have_ciphersuite_v2:
1404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
1405
1406 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001407 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jens Wiklander817466c2018-05-22 13:49:31 +02001408
1409 /*
1410 * SSLv2 Client Hello relevant renegotiation security checks
1411 */
1412 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1413 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
1414 {
1415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1416 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1417 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1418 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1419 }
1420
1421 ssl->in_left = 0;
1422 ssl->state++;
1423
1424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1425
1426 return( 0 );
1427}
1428#endif /* MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1429
1430/* This function doesn't alert on errors that happen early during
1431 ClientHello parsing because they might indicate that the client is
1432 not talking SSL/TLS at all and would not understand our alert. */
Jerome Forissier039e02d2022-08-09 17:10:15 +02001433MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02001434static int ssl_parse_client_hello( mbedtls_ssl_context *ssl )
1435{
1436 int ret, got_common_suite;
1437 size_t i, j;
1438 size_t ciph_offset, comp_offset, ext_offset;
1439 size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
1440#if defined(MBEDTLS_SSL_PROTO_DTLS)
1441 size_t cookie_offset, cookie_len;
1442#endif
1443 unsigned char *buf, *p, *ext;
1444#if defined(MBEDTLS_SSL_RENEGOTIATION)
1445 int renegotiation_info_seen = 0;
1446#endif
1447 int handshake_failure = 0;
1448 const int *ciphersuites;
1449 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1450 int major, minor;
1451
1452 /* If there is no signature-algorithm extension present,
1453 * we need to fall back to the default values for allowed
1454 * signature-hash pairs. */
1455#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001456 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02001457 int sig_hash_alg_ext_present = 0;
1458#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001459 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02001460
1461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1462
1463#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1464read_record_header:
1465#endif
1466 /*
1467 * If renegotiating, then the input was read with mbedtls_ssl_read_record(),
1468 * otherwise read it ourselves manually in order to support SSLv2
1469 * ClientHello, which doesn't use the same record layer format.
1470 */
1471#if defined(MBEDTLS_SSL_RENEGOTIATION)
1472 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
1473#endif
1474 {
1475 if( ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 )
1476 {
1477 /* No alert on a read error. */
1478 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
1479 return( ret );
1480 }
1481 }
1482
1483 buf = ssl->in_hdr;
1484
1485#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1486#if defined(MBEDTLS_SSL_PROTO_DTLS)
1487 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
1488#endif
1489 if( ( buf[0] & 0x80 ) != 0 )
1490 return( ssl_parse_client_hello_v2( ssl ) );
1491#endif
1492
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001493 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, mbedtls_ssl_in_hdr_len( ssl ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02001494
1495 /*
1496 * SSLv3/TLS Client Hello
1497 *
1498 * Record layer:
1499 * 0 . 0 message type
1500 * 1 . 2 protocol version
1501 * 3 . 11 DTLS: epoch + record sequence number
1502 * 3 . 4 message length
1503 */
1504 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1505 buf[0] ) );
1506
1507 if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE )
1508 {
1509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1510 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1511 }
1512
1513 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1514 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1515
1516 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
1517 buf[1], buf[2] ) );
1518
1519 mbedtls_ssl_read_version( &major, &minor, ssl->conf->transport, buf + 1 );
1520
1521 /* According to RFC 5246 Appendix E.1, the version here is typically
1522 * "{03,00}, the lowest version number supported by the client, [or] the
1523 * value of ClientHello.client_version", so the only meaningful check here
1524 * is the major version shouldn't be less than 3 */
1525 if( major < MBEDTLS_SSL_MAJOR_VERSION_3 )
1526 {
1527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1528 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1529 }
1530
1531 /* For DTLS if this is the initial handshake, remember the client sequence
1532 * number to use it in our next message (RFC 6347 4.2.1) */
1533#if defined(MBEDTLS_SSL_PROTO_DTLS)
1534 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM
1535#if defined(MBEDTLS_SSL_RENEGOTIATION)
1536 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
1537#endif
1538 )
1539 {
1540 /* Epoch should be 0 for initial handshakes */
1541 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1542 {
1543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1544 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1545 }
1546
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001547 memcpy( ssl->cur_out_ctr + 2, ssl->in_ctr + 2, 6 );
Jens Wiklander817466c2018-05-22 13:49:31 +02001548
1549#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1550 if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
1551 {
1552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
1553 ssl->next_record_offset = 0;
1554 ssl->in_left = 0;
1555 goto read_record_header;
1556 }
1557
1558 /* No MAC to check yet, so we can update right now */
1559 mbedtls_ssl_dtls_replay_update( ssl );
1560#endif
1561 }
1562#endif /* MBEDTLS_SSL_PROTO_DTLS */
1563
1564 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
1565
1566#if defined(MBEDTLS_SSL_RENEGOTIATION)
1567 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
1568 {
1569 /* Set by mbedtls_ssl_read_record() */
1570 msg_len = ssl->in_hslen;
1571 }
1572 else
1573#endif
1574 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001575 if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN )
Jens Wiklander817466c2018-05-22 13:49:31 +02001576 {
1577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1578 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1579 }
1580
1581 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001582 mbedtls_ssl_in_hdr_len( ssl ) + msg_len ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02001583 {
1584 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
1585 return( ret );
1586 }
1587
1588 /* Done reading this record, get ready for the next one */
1589#if defined(MBEDTLS_SSL_PROTO_DTLS)
1590 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001591 ssl->next_record_offset = msg_len + mbedtls_ssl_in_hdr_len( ssl );
Jens Wiklander817466c2018-05-22 13:49:31 +02001592 else
1593#endif
1594 ssl->in_left = 0;
1595 }
1596
1597 buf = ssl->in_msg;
1598
1599 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
1600
1601 ssl->handshake->update_checksum( ssl, buf, msg_len );
1602
1603 /*
1604 * Handshake layer:
1605 * 0 . 0 handshake type
1606 * 1 . 3 handshake length
Jerome Forissier039e02d2022-08-09 17:10:15 +02001607 * 4 . 5 DTLS only: message sequence number
Jens Wiklander817466c2018-05-22 13:49:31 +02001608 * 6 . 8 DTLS only: fragment offset
1609 * 9 . 11 DTLS only: fragment length
1610 */
1611 if( msg_len < mbedtls_ssl_hs_hdr_len( ssl ) )
1612 {
1613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1614 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1615 }
1616
1617 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
1618
1619 if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
1620 {
1621 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1622 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1623 }
1624
1625 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1626 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1627
Jerome Forissier039e02d2022-08-09 17:10:15 +02001628 if( buf[1] != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02001629 {
Jerome Forissier039e02d2022-08-09 17:10:15 +02001630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message: %u != 0",
1631 (unsigned) buf[1] ) );
1632 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1633 }
1634 /* We don't support fragmentation of ClientHello (yet?) */
1635 if( msg_len != mbedtls_ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
1636 {
1637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message: %u != %u + %u",
1638 (unsigned) msg_len,
1639 (unsigned) mbedtls_ssl_hs_hdr_len( ssl ),
1640 (unsigned) ( buf[2] << 8 ) | buf[3] ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02001641 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1642 }
1643
1644#if defined(MBEDTLS_SSL_PROTO_DTLS)
1645 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1646 {
1647 /*
1648 * Copy the client's handshake message_seq on initial handshakes,
1649 * check sequence number on renego.
1650 */
1651#if defined(MBEDTLS_SSL_RENEGOTIATION)
1652 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
1653 {
1654 /* This couldn't be done in ssl_prepare_handshake_record() */
1655 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1656 ssl->in_msg[5];
1657
1658 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1659 {
1660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
Jerome Forissier79013242021-07-28 10:24:04 +02001661 "%u (expected %u)", cli_msg_seq,
Jens Wiklander817466c2018-05-22 13:49:31 +02001662 ssl->handshake->in_msg_seq ) );
1663 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1664 }
1665
1666 ssl->handshake->in_msg_seq++;
1667 }
1668 else
1669#endif
1670 {
1671 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1672 ssl->in_msg[5];
1673 ssl->handshake->out_msg_seq = cli_msg_seq;
1674 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
1675 }
1676
1677 /*
1678 * For now we don't support fragmentation, so make sure
1679 * fragment_offset == 0 and fragment_length == length
1680 */
Jerome Forissier039e02d2022-08-09 17:10:15 +02001681 MBEDTLS_SSL_DEBUG_MSG(
1682 4, ( "fragment_offset=%u fragment_length=%u length=%u",
1683 (unsigned) ( ssl->in_msg[6] << 16 | ssl->in_msg[7] << 8 | ssl->in_msg[8] ),
1684 (unsigned) ( ssl->in_msg[9] << 16 | ssl->in_msg[10] << 8 | ssl->in_msg[11] ),
1685 (unsigned) ( ssl->in_msg[1] << 16 | ssl->in_msg[2] << 8 | ssl->in_msg[3] ) ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02001686 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1687 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1688 {
1689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
1690 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1691 }
1692 }
1693#endif /* MBEDTLS_SSL_PROTO_DTLS */
1694
1695 buf += mbedtls_ssl_hs_hdr_len( ssl );
1696 msg_len -= mbedtls_ssl_hs_hdr_len( ssl );
1697
1698 /*
1699 * ClientHello layer:
1700 * 0 . 1 protocol version
1701 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1702 * 34 . 35 session id length (1 byte)
1703 * 35 . 34+x session id
1704 * 35+x . 35+x DTLS only: cookie length (1 byte)
1705 * 36+x . .. DTLS only: cookie
1706 * .. . .. ciphersuite list length (2 bytes)
1707 * .. . .. ciphersuite list
1708 * .. . .. compression alg. list length (1 byte)
1709 * .. . .. compression alg. list
1710 * .. . .. extensions length (2 bytes, optional)
1711 * .. . .. extensions (optional)
1712 */
1713
1714 /*
Jerome Forissier5b25c762020-04-07 11:18:49 +02001715 * Minimal length (with everything empty and extensions omitted) is
Jens Wiklander817466c2018-05-22 13:49:31 +02001716 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1717 * read at least up to session id length without worrying.
1718 */
1719 if( msg_len < 38 )
1720 {
1721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1722 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1723 }
1724
1725 /*
1726 * Check and save the protocol version
1727 */
1728 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
1729
1730 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
1731 ssl->conf->transport, buf );
1732
1733 ssl->handshake->max_major_ver = ssl->major_ver;
1734 ssl->handshake->max_minor_ver = ssl->minor_ver;
1735
1736 if( ssl->major_ver < ssl->conf->min_major_ver ||
1737 ssl->minor_ver < ssl->conf->min_minor_ver )
1738 {
1739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
1740 " [%d:%d] < [%d:%d]",
1741 ssl->major_ver, ssl->minor_ver,
1742 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
1743 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1744 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
1745 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1746 }
1747
1748 if( ssl->major_ver > ssl->conf->max_major_ver )
1749 {
1750 ssl->major_ver = ssl->conf->max_major_ver;
1751 ssl->minor_ver = ssl->conf->max_minor_ver;
1752 }
1753 else if( ssl->minor_ver > ssl->conf->max_minor_ver )
1754 ssl->minor_ver = ssl->conf->max_minor_ver;
1755
1756 /*
1757 * Save client random (inc. Unix time)
1758 */
1759 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
1760
1761 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
1762
1763 /*
1764 * Check the session ID length and save session ID
1765 */
1766 sess_len = buf[34];
1767
1768 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
1769 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
1770 {
1771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1772 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1773 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1774 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1775 }
1776
1777 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
1778
1779 ssl->session_negotiate->id_len = sess_len;
1780 memset( ssl->session_negotiate->id, 0,
1781 sizeof( ssl->session_negotiate->id ) );
1782 memcpy( ssl->session_negotiate->id, buf + 35,
1783 ssl->session_negotiate->id_len );
1784
1785 /*
1786 * Check the cookie length and content
1787 */
1788#if defined(MBEDTLS_SSL_PROTO_DTLS)
1789 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1790 {
1791 cookie_offset = 35 + sess_len;
1792 cookie_len = buf[cookie_offset];
1793
1794 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
1795 {
1796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1797 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1798 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
1799 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1800 }
1801
1802 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
1803 buf + cookie_offset + 1, cookie_len );
1804
1805#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
1806 if( ssl->conf->f_cookie_check != NULL
1807#if defined(MBEDTLS_SSL_RENEGOTIATION)
1808 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
1809#endif
1810 )
1811 {
1812 if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
1813 buf + cookie_offset + 1, cookie_len,
1814 ssl->cli_id, ssl->cli_id_len ) != 0 )
1815 {
1816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
1817 ssl->handshake->verify_cookie_len = 1;
1818 }
1819 else
1820 {
1821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
1822 ssl->handshake->verify_cookie_len = 0;
1823 }
1824 }
1825 else
1826#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
1827 {
1828 /* We know we didn't send a cookie, so it should be empty */
1829 if( cookie_len != 0 )
1830 {
1831 /* This may be an attacker's probe, so don't send an alert */
1832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1833 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1834 }
1835
1836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
1837 }
1838
1839 /*
1840 * Check the ciphersuitelist length (will be parsed later)
1841 */
1842 ciph_offset = cookie_offset + 1 + cookie_len;
1843 }
1844 else
1845#endif /* MBEDTLS_SSL_PROTO_DTLS */
1846 ciph_offset = 35 + sess_len;
1847
1848 ciph_len = ( buf[ciph_offset + 0] << 8 )
1849 | ( buf[ciph_offset + 1] );
1850
1851 if( ciph_len < 2 ||
1852 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1853 ( ciph_len % 2 ) != 0 )
1854 {
1855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1856 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1857 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1858 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1859 }
1860
1861 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1862 buf + ciph_offset + 2, ciph_len );
1863
1864 /*
1865 * Check the compression algorithms length and pick one
1866 */
1867 comp_offset = ciph_offset + 2 + ciph_len;
1868
1869 comp_len = buf[comp_offset];
1870
1871 if( comp_len < 1 ||
1872 comp_len > 16 ||
1873 comp_len + comp_offset + 1 > msg_len )
1874 {
1875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1876 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1877 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1878 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1879 }
1880
1881 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression",
1882 buf + comp_offset + 1, comp_len );
1883
1884 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
1885#if defined(MBEDTLS_ZLIB_SUPPORT)
1886 for( i = 0; i < comp_len; ++i )
1887 {
1888 if( buf[comp_offset + 1 + i] == MBEDTLS_SSL_COMPRESS_DEFLATE )
1889 {
1890 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_DEFLATE;
1891 break;
1892 }
1893 }
1894#endif
1895
1896 /* See comments in ssl_write_client_hello() */
1897#if defined(MBEDTLS_SSL_PROTO_DTLS)
1898 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1899 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
1900#endif
1901
1902 /* Do not parse the extensions if the protocol is SSLv3 */
1903#if defined(MBEDTLS_SSL_PROTO_SSL3)
1904 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
1905 {
1906#endif
1907 /*
1908 * Check the extension length
1909 */
1910 ext_offset = comp_offset + 1 + comp_len;
1911 if( msg_len > ext_offset )
1912 {
1913 if( msg_len < ext_offset + 2 )
1914 {
1915 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1916 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1917 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1918 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1919 }
1920
1921 ext_len = ( buf[ext_offset + 0] << 8 )
1922 | ( buf[ext_offset + 1] );
1923
Jerome Forissier79013242021-07-28 10:24:04 +02001924 if( msg_len != ext_offset + 2 + ext_len )
Jens Wiklander817466c2018-05-22 13:49:31 +02001925 {
1926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1927 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1928 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1929 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1930 }
1931 }
1932 else
1933 ext_len = 0;
1934
1935 ext = buf + ext_offset + 2;
1936 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len );
1937
1938 while( ext_len != 0 )
1939 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001940 unsigned int ext_id;
1941 unsigned int ext_size;
1942 if ( ext_len < 4 ) {
1943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1944 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1945 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1946 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1947 }
1948 ext_id = ( ( ext[0] << 8 ) | ( ext[1] ) );
1949 ext_size = ( ( ext[2] << 8 ) | ( ext[3] ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02001950
1951 if( ext_size + 4 > ext_len )
1952 {
1953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1954 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1955 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1956 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1957 }
1958 switch( ext_id )
1959 {
1960#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1961 case MBEDTLS_TLS_EXT_SERVERNAME:
1962 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1963 if( ssl->conf->f_sni == NULL )
1964 break;
1965
1966 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1967 if( ret != 0 )
1968 return( ret );
1969 break;
1970#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1971
1972 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1973 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1974#if defined(MBEDTLS_SSL_RENEGOTIATION)
1975 renegotiation_info_seen = 1;
1976#endif
1977
1978 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1979 if( ret != 0 )
1980 return( ret );
1981 break;
1982
1983#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001984 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02001985 case MBEDTLS_TLS_EXT_SIG_ALG:
1986 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1987
1988 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1989 if( ret != 0 )
1990 return( ret );
1991
1992 sig_hash_alg_ext_present = 1;
1993 break;
1994#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001995 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02001996
1997#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
1998 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1999 case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
2000 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
2001
2002 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
2003 if( ret != 0 )
2004 return( ret );
2005 break;
2006
2007 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
2008 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
2009 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
2010
2011 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
2012 if( ret != 0 )
2013 return( ret );
2014 break;
2015#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
2016 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2017
2018#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2019 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
2020 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake kkpp extension" ) );
2021
2022 ret = ssl_parse_ecjpake_kkpp( ssl, ext + 4, ext_size );
2023 if( ret != 0 )
2024 return( ret );
2025 break;
2026#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2027
2028#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2029 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
2030 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
2031
2032 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
2033 if( ret != 0 )
2034 return( ret );
2035 break;
2036#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2037
2038#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
2039 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
2040 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
2041
2042 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
2043 if( ret != 0 )
2044 return( ret );
2045 break;
2046#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
2047
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002048#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
2049 case MBEDTLS_TLS_EXT_CID:
2050 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
2051
2052 ret = ssl_parse_cid_ext( ssl, ext + 4, ext_size );
2053 if( ret != 0 )
2054 return( ret );
2055 break;
2056#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
2057
Jens Wiklander817466c2018-05-22 13:49:31 +02002058#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2059 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
2060 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
2061
2062 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
2063 if( ret != 0 )
2064 return( ret );
2065 break;
2066#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2067
2068#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2069 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
2070 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
2071
2072 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
2073 if( ret != 0 )
2074 return( ret );
2075 break;
2076#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
2077
2078#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2079 case MBEDTLS_TLS_EXT_SESSION_TICKET:
2080 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
2081
2082 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
2083 if( ret != 0 )
2084 return( ret );
2085 break;
2086#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2087
2088#if defined(MBEDTLS_SSL_ALPN)
2089 case MBEDTLS_TLS_EXT_ALPN:
2090 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
2091
2092 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
2093 if( ret != 0 )
2094 return( ret );
2095 break;
2096#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2097
Jerome Forissier79013242021-07-28 10:24:04 +02002098#if defined(MBEDTLS_SSL_DTLS_SRTP)
2099 case MBEDTLS_TLS_EXT_USE_SRTP:
2100 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
2101
2102 ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size );
2103 if( ret != 0 )
2104 return( ret );
2105 break;
2106#endif /* MBEDTLS_SSL_DTLS_SRTP */
2107
Jens Wiklander817466c2018-05-22 13:49:31 +02002108 default:
Jerome Forissier79013242021-07-28 10:24:04 +02002109 MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %u (ignoring)",
Jens Wiklander817466c2018-05-22 13:49:31 +02002110 ext_id ) );
2111 }
2112
2113 ext_len -= 4 + ext_size;
2114 ext += 4 + ext_size;
Jens Wiklander817466c2018-05-22 13:49:31 +02002115 }
2116#if defined(MBEDTLS_SSL_PROTO_SSL3)
2117 }
2118#endif
2119
2120#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
2121 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
2122 {
Jerome Forissier039e02d2022-08-09 17:10:15 +02002123 if( MBEDTLS_GET_UINT16_BE( p, 0 ) == MBEDTLS_SSL_FALLBACK_SCSV_VALUE )
Jens Wiklander817466c2018-05-22 13:49:31 +02002124 {
2125 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received FALLBACK_SCSV" ) );
2126
2127 if( ssl->minor_ver < ssl->conf->max_minor_ver )
2128 {
2129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
2130
2131 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2132 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
2133
2134 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
2135 }
2136
2137 break;
2138 }
2139 }
2140#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
2141
2142#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002143 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02002144
2145 /*
2146 * Try to fall back to default hash SHA1 if the client
2147 * hasn't provided any preferred signature-hash combinations.
2148 */
2149 if( sig_hash_alg_ext_present == 0 )
2150 {
2151 mbedtls_md_type_t md_default = MBEDTLS_MD_SHA1;
2152
2153 if( mbedtls_ssl_check_sig_hash( ssl, md_default ) != 0 )
2154 md_default = MBEDTLS_MD_NONE;
2155
2156 mbedtls_ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default );
2157 }
2158
2159#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002160 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02002161
2162 /*
2163 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
2164 */
2165 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
2166 {
2167 if( p[0] == 0 && p[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
2168 {
2169 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
2170#if defined(MBEDTLS_SSL_RENEGOTIATION)
2171 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
2172 {
2173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
2174 "during renegotiation" ) );
2175 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2176 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2177 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
2178 }
2179#endif
2180 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
2181 break;
2182 }
2183 }
2184
2185 /*
2186 * Renegotiation security checks
2187 */
2188 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION &&
2189 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
2190 {
2191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
2192 handshake_failure = 1;
2193 }
2194#if defined(MBEDTLS_SSL_RENEGOTIATION)
2195 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2196 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
2197 renegotiation_info_seen == 0 )
2198 {
2199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
2200 handshake_failure = 1;
2201 }
2202 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2203 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
2204 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
2205 {
2206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
2207 handshake_failure = 1;
2208 }
2209 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2210 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
2211 renegotiation_info_seen == 1 )
2212 {
2213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
2214 handshake_failure = 1;
2215 }
2216#endif /* MBEDTLS_SSL_RENEGOTIATION */
2217
2218 if( handshake_failure == 1 )
2219 {
2220 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2221 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2222 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
2223 }
2224
2225 /*
2226 * Search for a matching ciphersuite
2227 * (At the end because we need information from the EC-based extensions
2228 * and certificate from the SNI callback triggered by the SNI extension.)
2229 */
2230 got_common_suite = 0;
2231 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
2232 ciphersuite_info = NULL;
2233#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
2234 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
2235 for( i = 0; ciphersuites[i] != 0; i++ )
2236#else
2237 for( i = 0; ciphersuites[i] != 0; i++ )
2238 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
2239#endif
2240 {
Jerome Forissier039e02d2022-08-09 17:10:15 +02002241 if( MBEDTLS_GET_UINT16_BE(p, 0) != ciphersuites[i] )
Jens Wiklander817466c2018-05-22 13:49:31 +02002242 continue;
2243
2244 got_common_suite = 1;
2245
2246 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
2247 &ciphersuite_info ) ) != 0 )
2248 return( ret );
2249
2250 if( ciphersuite_info != NULL )
2251 goto have_ciphersuite;
2252 }
2253
2254 if( got_common_suite )
2255 {
2256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
2257 "but none of them usable" ) );
2258 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2259 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2260 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
2261 }
2262 else
2263 {
2264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
2265 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2266 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2267 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
2268 }
2269
2270have_ciphersuite:
2271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
2272
2273 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002274 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jens Wiklander817466c2018-05-22 13:49:31 +02002275
2276 ssl->state++;
2277
2278#if defined(MBEDTLS_SSL_PROTO_DTLS)
2279 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2280 mbedtls_ssl_recv_flight_completed( ssl );
2281#endif
2282
2283 /* Debugging-only output for testsuite */
2284#if defined(MBEDTLS_DEBUG_C) && \
2285 defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002286 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02002287 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
2288 {
2289 mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( ciphersuite_info );
2290 if( sig_alg != MBEDTLS_PK_NONE )
2291 {
2292 mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
2293 sig_alg );
2294 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
2295 mbedtls_ssl_hash_from_md_alg( md_alg ) ) );
2296 }
2297 else
2298 {
2299 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm "
Jerome Forissier79013242021-07-28 10:24:04 +02002300 "%u - should not happen", (unsigned) sig_alg ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002301 }
2302 }
2303#endif
2304
2305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
2306
2307 return( 0 );
2308}
2309
2310#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
2311static void ssl_write_truncated_hmac_ext( mbedtls_ssl_context *ssl,
2312 unsigned char *buf,
2313 size_t *olen )
2314{
2315 unsigned char *p = buf;
2316
2317 if( ssl->session_negotiate->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED )
2318 {
2319 *olen = 0;
2320 return;
2321 }
2322
2323 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
2324
Jerome Forissier039e02d2022-08-09 17:10:15 +02002325 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_TRUNCATED_HMAC, p, 0 );
2326 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +02002327
2328 *p++ = 0x00;
2329 *p++ = 0x00;
2330
2331 *olen = 4;
2332}
2333#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
2334
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002335#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
2336static void ssl_write_cid_ext( mbedtls_ssl_context *ssl,
2337 unsigned char *buf,
2338 size_t *olen )
2339{
2340 unsigned char *p = buf;
2341 size_t ext_len;
2342 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2343
2344 *olen = 0;
2345
2346 /* Skip writing the extension if we don't want to use it or if
2347 * the client hasn't offered it. */
2348 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED )
2349 return;
2350
2351 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
2352 * which is at most 255, so the increment cannot overflow. */
2353 if( end < p || (size_t)( end - p ) < (unsigned)( ssl->own_cid_len + 5 ) )
2354 {
2355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2356 return;
2357 }
2358
2359 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding CID extension" ) );
2360
2361 /*
2362 * Quoting draft-ietf-tls-dtls-connection-id-05
2363 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
2364 *
2365 * struct {
2366 * opaque cid<0..2^8-1>;
2367 * } ConnectionId;
2368 */
Jerome Forissier039e02d2022-08-09 17:10:15 +02002369 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 );
2370 p += 2;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002371 ext_len = (size_t) ssl->own_cid_len + 1;
Jerome Forissier039e02d2022-08-09 17:10:15 +02002372 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
2373 p += 2;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002374
2375 *p++ = (uint8_t) ssl->own_cid_len;
2376 memcpy( p, ssl->own_cid, ssl->own_cid_len );
2377
2378 *olen = ssl->own_cid_len + 5;
2379}
2380#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
2381
Jens Wiklander817466c2018-05-22 13:49:31 +02002382#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2383static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
2384 unsigned char *buf,
2385 size_t *olen )
2386{
2387 unsigned char *p = buf;
2388 const mbedtls_ssl_ciphersuite_t *suite = NULL;
2389 const mbedtls_cipher_info_t *cipher = NULL;
2390
Jerome Forissier039e02d2022-08-09 17:10:15 +02002391 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
2392 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_DISABLED;
Jens Wiklander817466c2018-05-22 13:49:31 +02002393
2394 /*
2395 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2396 * from a client and then selects a stream or Authenticated Encryption
2397 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2398 * encrypt-then-MAC response extension back to the client."
2399 */
2400 if( ( suite = mbedtls_ssl_ciphersuite_from_id(
2401 ssl->session_negotiate->ciphersuite ) ) == NULL ||
2402 ( cipher = mbedtls_cipher_info_from_type( suite->cipher ) ) == NULL ||
2403 cipher->mode != MBEDTLS_MODE_CBC )
2404 {
Jerome Forissier039e02d2022-08-09 17:10:15 +02002405 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_DISABLED;
2406 }
2407
2408 if( ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
2409 {
Jens Wiklander817466c2018-05-22 13:49:31 +02002410 *olen = 0;
2411 return;
2412 }
2413
2414 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
2415
Jerome Forissier039e02d2022-08-09 17:10:15 +02002416 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 );
2417 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +02002418
2419 *p++ = 0x00;
2420 *p++ = 0x00;
2421
2422 *olen = 4;
2423}
2424#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2425
2426#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2427static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
2428 unsigned char *buf,
2429 size_t *olen )
2430{
2431 unsigned char *p = buf;
2432
2433 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
2434 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
2435 {
2436 *olen = 0;
2437 return;
2438 }
2439
2440 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
2441 "extension" ) );
2442
Jerome Forissier039e02d2022-08-09 17:10:15 +02002443 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 );
2444 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +02002445
2446 *p++ = 0x00;
2447 *p++ = 0x00;
2448
2449 *olen = 4;
2450}
2451#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
2452
2453#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2454static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
2455 unsigned char *buf,
2456 size_t *olen )
2457{
2458 unsigned char *p = buf;
2459
2460 if( ssl->handshake->new_session_ticket == 0 )
2461 {
2462 *olen = 0;
2463 return;
2464 }
2465
2466 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
2467
Jerome Forissier039e02d2022-08-09 17:10:15 +02002468 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 );
2469 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +02002470
2471 *p++ = 0x00;
2472 *p++ = 0x00;
2473
2474 *olen = 4;
2475}
2476#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2477
2478static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
2479 unsigned char *buf,
2480 size_t *olen )
2481{
2482 unsigned char *p = buf;
2483
2484 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION )
2485 {
2486 *olen = 0;
2487 return;
2488 }
2489
2490 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
2491
Jerome Forissier039e02d2022-08-09 17:10:15 +02002492 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0 );
2493 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +02002494
2495#if defined(MBEDTLS_SSL_RENEGOTIATION)
2496 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
2497 {
2498 *p++ = 0x00;
2499 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2500 *p++ = ssl->verify_data_len * 2 & 0xFF;
2501
2502 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2503 p += ssl->verify_data_len;
2504 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2505 p += ssl->verify_data_len;
2506 }
2507 else
2508#endif /* MBEDTLS_SSL_RENEGOTIATION */
2509 {
2510 *p++ = 0x00;
2511 *p++ = 0x01;
2512 *p++ = 0x00;
2513 }
2514
2515 *olen = p - buf;
2516}
2517
2518#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2519static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
2520 unsigned char *buf,
2521 size_t *olen )
2522{
2523 unsigned char *p = buf;
2524
2525 if( ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
2526 {
2527 *olen = 0;
2528 return;
2529 }
2530
2531 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
2532
Jerome Forissier039e02d2022-08-09 17:10:15 +02002533 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 );
2534 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +02002535
2536 *p++ = 0x00;
2537 *p++ = 1;
2538
2539 *p++ = ssl->session_negotiate->mfl_code;
2540
2541 *olen = 5;
2542}
2543#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2544
2545#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
2546 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2547static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
2548 unsigned char *buf,
2549 size_t *olen )
2550{
2551 unsigned char *p = buf;
2552 ((void) ssl);
2553
2554 if( ( ssl->handshake->cli_exts &
2555 MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
2556 {
2557 *olen = 0;
2558 return;
2559 }
2560
2561 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
2562
Jerome Forissier039e02d2022-08-09 17:10:15 +02002563 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0 );
2564 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +02002565
2566 *p++ = 0x00;
2567 *p++ = 2;
2568
2569 *p++ = 1;
2570 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
2571
2572 *olen = 6;
2573}
2574#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2575
2576#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2577static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
2578 unsigned char *buf,
2579 size_t *olen )
2580{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002581 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02002582 unsigned char *p = buf;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002583 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Jens Wiklander817466c2018-05-22 13:49:31 +02002584 size_t kkpp_len;
2585
2586 *olen = 0;
2587
2588 /* Skip costly computation if not needed */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002589 if( ssl->handshake->ciphersuite_info->key_exchange !=
Jens Wiklander817466c2018-05-22 13:49:31 +02002590 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2591 return;
2592
2593 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) );
2594
2595 if( end - p < 4 )
2596 {
2597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2598 return;
2599 }
2600
Jerome Forissier039e02d2022-08-09 17:10:15 +02002601 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 );
2602 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +02002603
2604 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
2605 p + 2, end - p - 2, &kkpp_len,
2606 ssl->conf->f_rng, ssl->conf->p_rng );
2607 if( ret != 0 )
2608 {
2609 MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret );
2610 return;
2611 }
2612
Jerome Forissier039e02d2022-08-09 17:10:15 +02002613 MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 );
2614 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +02002615
2616 *olen = kkpp_len + 4;
2617}
2618#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2619
2620#if defined(MBEDTLS_SSL_ALPN )
2621static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
2622 unsigned char *buf, size_t *olen )
2623{
2624 if( ssl->alpn_chosen == NULL )
2625 {
2626 *olen = 0;
2627 return;
2628 }
2629
2630 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
2631
2632 /*
2633 * 0 . 1 ext identifier
2634 * 2 . 3 ext length
2635 * 4 . 5 protocol list length
2636 * 6 . 6 protocol name length
2637 * 7 . 7+n protocol name
2638 */
Jerome Forissier039e02d2022-08-09 17:10:15 +02002639 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, buf, 0);
Jens Wiklander817466c2018-05-22 13:49:31 +02002640
2641 *olen = 7 + strlen( ssl->alpn_chosen );
2642
Jerome Forissier039e02d2022-08-09 17:10:15 +02002643 MBEDTLS_PUT_UINT16_BE( *olen - 4, buf, 2 );
Jens Wiklander817466c2018-05-22 13:49:31 +02002644
Jerome Forissier039e02d2022-08-09 17:10:15 +02002645 MBEDTLS_PUT_UINT16_BE( *olen - 6, buf, 4 );
Jens Wiklander817466c2018-05-22 13:49:31 +02002646
Jerome Forissier039e02d2022-08-09 17:10:15 +02002647 buf[6] = MBEDTLS_BYTE_0( *olen - 7 );
Jens Wiklander817466c2018-05-22 13:49:31 +02002648
2649 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2650}
2651#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
2652
Jerome Forissier79013242021-07-28 10:24:04 +02002653#if defined(MBEDTLS_SSL_DTLS_SRTP ) && defined(MBEDTLS_SSL_PROTO_DTLS)
2654static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
2655 unsigned char *buf,
2656 size_t *olen )
2657{
2658 size_t mki_len = 0, ext_len = 0;
2659 uint16_t profile_value = 0;
2660 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2661
2662 *olen = 0;
2663
2664 if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
2665 ( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) )
2666 {
2667 return;
2668 }
2669
2670 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding use_srtp extension" ) );
2671
2672 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
2673 {
2674 mki_len = ssl->dtls_srtp_info.mki_len;
2675 }
2676
2677 /* The extension total size is 9 bytes :
2678 * - 2 bytes for the extension tag
2679 * - 2 bytes for the total size
2680 * - 2 bytes for the protection profile length
2681 * - 2 bytes for the protection profile
2682 * - 1 byte for the mki length
2683 * + the actual mki length
2684 * Check we have enough room in the output buffer */
2685 if( (size_t)( end - buf ) < mki_len + 9 )
2686 {
2687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2688 return;
2689 }
2690
2691 /* extension */
Jerome Forissier039e02d2022-08-09 17:10:15 +02002692 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, buf, 0 );
Jerome Forissier79013242021-07-28 10:24:04 +02002693 /*
2694 * total length 5 and mki value: only one profile(2 bytes)
2695 * and length(2 bytes) and srtp_mki )
2696 */
2697 ext_len = 5 + mki_len;
Jerome Forissier039e02d2022-08-09 17:10:15 +02002698 MBEDTLS_PUT_UINT16_BE( ext_len, buf, 2 );
Jerome Forissier79013242021-07-28 10:24:04 +02002699
2700 /* protection profile length: 2 */
2701 buf[4] = 0x00;
2702 buf[5] = 0x02;
2703 profile_value = mbedtls_ssl_check_srtp_profile_value(
2704 ssl->dtls_srtp_info.chosen_dtls_srtp_profile );
2705 if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
2706 {
Jerome Forissier039e02d2022-08-09 17:10:15 +02002707 MBEDTLS_PUT_UINT16_BE( profile_value, buf, 6 );
Jerome Forissier79013242021-07-28 10:24:04 +02002708 }
2709 else
2710 {
2711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "use_srtp extension invalid profile" ) );
2712 return;
2713 }
2714
2715 buf[8] = mki_len & 0xFF;
2716 memcpy( &buf[9], ssl->dtls_srtp_info.mki_value, mki_len );
2717
2718 *olen = 9 + mki_len;
2719}
2720#endif /* MBEDTLS_SSL_DTLS_SRTP */
2721
Jens Wiklander817466c2018-05-22 13:49:31 +02002722#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
Jerome Forissier039e02d2022-08-09 17:10:15 +02002723MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02002724static int ssl_write_hello_verify_request( mbedtls_ssl_context *ssl )
2725{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002726 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02002727 unsigned char *p = ssl->out_msg + 4;
2728 unsigned char *cookie_len_byte;
2729
2730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
2731
2732 /*
2733 * struct {
2734 * ProtocolVersion server_version;
2735 * opaque cookie<0..2^8-1>;
2736 * } HelloVerifyRequest;
2737 */
2738
2739 /* The RFC is not clear on this point, but sending the actual negotiated
2740 * version looks like the most interoperable thing to do. */
2741 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
2742 ssl->conf->transport, p );
2743 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
2744 p += 2;
2745
2746 /* If we get here, f_cookie_check is not null */
2747 if( ssl->conf->f_cookie_write == NULL )
2748 {
2749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2750 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2751 }
2752
2753 /* Skip length byte until we know the length */
2754 cookie_len_byte = p++;
2755
2756 if( ( ret = ssl->conf->f_cookie_write( ssl->conf->p_cookie,
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002757 &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN,
Jens Wiklander817466c2018-05-22 13:49:31 +02002758 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
2759 {
2760 MBEDTLS_SSL_DEBUG_RET( 1, "f_cookie_write", ret );
2761 return( ret );
2762 }
2763
2764 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2765
2766 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
2767
2768 ssl->out_msglen = p - ssl->out_msg;
2769 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2770 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
2771
2772 ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
2773
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002774 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02002775 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002776 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02002777 return( ret );
2778 }
2779
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002780#if defined(MBEDTLS_SSL_PROTO_DTLS)
2781 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2782 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
2783 {
2784 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
2785 return( ret );
2786 }
2787#endif /* MBEDTLS_SSL_PROTO_DTLS */
2788
Jens Wiklander817466c2018-05-22 13:49:31 +02002789 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
2790
2791 return( 0 );
2792}
2793#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
2794
Jerome Forissier79013242021-07-28 10:24:04 +02002795static void ssl_handle_id_based_session_resumption( mbedtls_ssl_context *ssl )
2796{
2797 int ret;
2798 mbedtls_ssl_session session_tmp;
2799 mbedtls_ssl_session * const session = ssl->session_negotiate;
2800
2801 /* Resume is 0 by default, see ssl_handshake_init().
2802 * It may be already set to 1 by ssl_parse_session_ticket_ext(). */
2803 if( ssl->handshake->resume == 1 )
2804 return;
2805 if( session->id_len == 0 )
2806 return;
2807 if( ssl->conf->f_get_cache == NULL )
2808 return;
2809#if defined(MBEDTLS_SSL_RENEGOTIATION)
2810 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
2811 return;
2812#endif
2813
2814 mbedtls_ssl_session_init( &session_tmp );
2815
2816 session_tmp.id_len = session->id_len;
2817 memcpy( session_tmp.id, session->id, session->id_len );
2818
2819 ret = ssl->conf->f_get_cache( ssl->conf->p_cache,
2820 &session_tmp );
2821 if( ret != 0 )
2822 goto exit;
2823
2824 if( session->ciphersuite != session_tmp.ciphersuite ||
2825 session->compression != session_tmp.compression )
2826 {
2827 /* Mismatch between cached and negotiated session */
2828 goto exit;
2829 }
2830
2831 /* Move semantics */
2832 mbedtls_ssl_session_free( session );
2833 *session = session_tmp;
2834 memset( &session_tmp, 0, sizeof( session_tmp ) );
2835
2836 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
2837 ssl->handshake->resume = 1;
2838
2839exit:
2840
2841 mbedtls_ssl_session_free( &session_tmp );
2842}
2843
Jerome Forissier039e02d2022-08-09 17:10:15 +02002844MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02002845static int ssl_write_server_hello( mbedtls_ssl_context *ssl )
2846{
2847#if defined(MBEDTLS_HAVE_TIME)
2848 mbedtls_time_t t;
2849#endif
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002850 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02002851 size_t olen, ext_len = 0, n;
2852 unsigned char *buf, *p;
2853
2854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2855
2856#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2857 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2858 ssl->handshake->verify_cookie_len != 0 )
2859 {
2860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2862
2863 return( ssl_write_hello_verify_request( ssl ) );
2864 }
2865#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
2866
2867 if( ssl->conf->f_rng == NULL )
2868 {
2869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2870 return( MBEDTLS_ERR_SSL_NO_RNG );
2871 }
2872
2873 /*
2874 * 0 . 0 handshake type
2875 * 1 . 3 handshake length
2876 * 4 . 5 protocol version
2877 * 6 . 9 UNIX time()
2878 * 10 . 37 random bytes
2879 */
2880 buf = ssl->out_msg;
2881 p = buf + 4;
2882
2883 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
2884 ssl->conf->transport, p );
2885 p += 2;
2886
2887 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
2888 buf[4], buf[5] ) );
2889
2890#if defined(MBEDTLS_HAVE_TIME)
2891 t = mbedtls_time( NULL );
Jerome Forissier039e02d2022-08-09 17:10:15 +02002892 MBEDTLS_PUT_UINT32_BE( t, p, 0 );
2893 p += 4;
Jens Wiklander817466c2018-05-22 13:49:31 +02002894
Jerome Forissier79013242021-07-28 10:24:04 +02002895 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
2896 (long long) t ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002897#else
2898 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
2899 return( ret );
2900
2901 p += 4;
2902#endif /* MBEDTLS_HAVE_TIME */
2903
2904 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
2905 return( ret );
2906
2907 p += 28;
2908
2909 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
2910
2911 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2912
Jerome Forissier79013242021-07-28 10:24:04 +02002913 ssl_handle_id_based_session_resumption( ssl );
Jens Wiklander817466c2018-05-22 13:49:31 +02002914
2915 if( ssl->handshake->resume == 0 )
2916 {
2917 /*
2918 * New session, create a new session id,
2919 * unless we're about to issue a session ticket
2920 */
2921 ssl->state++;
2922
2923#if defined(MBEDTLS_HAVE_TIME)
2924 ssl->session_negotiate->start = mbedtls_time( NULL );
2925#endif
2926
2927#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2928 if( ssl->handshake->new_session_ticket != 0 )
2929 {
2930 ssl->session_negotiate->id_len = n = 0;
2931 memset( ssl->session_negotiate->id, 0, 32 );
2932 }
2933 else
2934#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2935 {
2936 ssl->session_negotiate->id_len = n = 32;
2937 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->session_negotiate->id,
2938 n ) ) != 0 )
2939 return( ret );
2940 }
2941 }
2942 else
2943 {
2944 /*
2945 * Resuming a session
2946 */
2947 n = ssl->session_negotiate->id_len;
2948 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
2949
2950 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
2951 {
2952 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
2953 return( ret );
2954 }
2955 }
2956
2957 /*
2958 * 38 . 38 session id length
2959 * 39 . 38+n session id
2960 * 39+n . 40+n chosen ciphersuite
2961 * 41+n . 41+n chosen compression alg.
2962 * 42+n . 43+n extensions length
2963 * 44+n . 43+n+m extensions
2964 */
2965 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2966 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
2967 p += ssl->session_negotiate->id_len;
2968
Jerome Forissier79013242021-07-28 10:24:04 +02002969 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002970 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2971 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
2972 ssl->handshake->resume ? "a" : "no" ) );
2973
Jerome Forissier039e02d2022-08-09 17:10:15 +02002974 MBEDTLS_PUT_UINT16_BE( ssl->session_negotiate->ciphersuite, p, 0 );
2975 p += 2;
2976 *p++ = MBEDTLS_BYTE_0( ssl->session_negotiate->compression );
Jens Wiklander817466c2018-05-22 13:49:31 +02002977
2978 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2979 mbedtls_ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
2980 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Jerome Forissier79013242021-07-28 10:24:04 +02002981 (unsigned int) ssl->session_negotiate->compression ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02002982
2983 /* Do not write the extensions if the protocol is SSLv3 */
2984#if defined(MBEDTLS_SSL_PROTO_SSL3)
2985 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
2986 {
2987#endif
2988
2989 /*
2990 * First write extensions, then the total length
2991 */
2992 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2993 ext_len += olen;
2994
2995#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2996 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2997 ext_len += olen;
2998#endif
2999
3000#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
3001 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
3002 ext_len += olen;
3003#endif
3004
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003005#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3006 ssl_write_cid_ext( ssl, p + 2 + ext_len, &olen );
3007 ext_len += olen;
3008#endif
3009
Jens Wiklander817466c2018-05-22 13:49:31 +02003010#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3011 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
3012 ext_len += olen;
3013#endif
3014
3015#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
3016 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
3017 ext_len += olen;
3018#endif
3019
3020#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3021 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
3022 ext_len += olen;
3023#endif
3024
3025#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3026 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003027 if ( mbedtls_ssl_ciphersuite_uses_ec(
3028 mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite ) ) )
3029 {
3030 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
3031 ext_len += olen;
3032 }
Jens Wiklander817466c2018-05-22 13:49:31 +02003033#endif
3034
3035#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3036 ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen );
3037 ext_len += olen;
3038#endif
3039
3040#if defined(MBEDTLS_SSL_ALPN)
3041 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
3042 ext_len += olen;
3043#endif
3044
Jerome Forissier79013242021-07-28 10:24:04 +02003045#if defined(MBEDTLS_SSL_DTLS_SRTP)
3046 ssl_write_use_srtp_ext( ssl, p + 2 + ext_len, &olen );
3047 ext_len += olen;
3048#endif
3049
3050 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
3051 ext_len ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02003052
3053 if( ext_len > 0 )
3054 {
Jerome Forissier039e02d2022-08-09 17:10:15 +02003055 MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
3056 p += 2 + ext_len;
Jens Wiklander817466c2018-05-22 13:49:31 +02003057 }
3058
3059#if defined(MBEDTLS_SSL_PROTO_SSL3)
3060 }
3061#endif
3062
3063 ssl->out_msglen = p - buf;
3064 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3065 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO;
3066
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003067 ret = mbedtls_ssl_write_handshake_msg( ssl );
Jens Wiklander817466c2018-05-22 13:49:31 +02003068
3069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
3070
3071 return( ret );
3072}
3073
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003074#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Jerome Forissier039e02d2022-08-09 17:10:15 +02003075MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02003076static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
3077{
3078 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003079 ssl->handshake->ciphersuite_info;
Jens Wiklander817466c2018-05-22 13:49:31 +02003080
3081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
3082
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003083 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Jens Wiklander817466c2018-05-22 13:49:31 +02003084 {
3085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
3086 ssl->state++;
3087 return( 0 );
3088 }
3089
3090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3091 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3092}
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003093#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Jerome Forissier039e02d2022-08-09 17:10:15 +02003094MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02003095static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
3096{
3097 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3098 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003099 ssl->handshake->ciphersuite_info;
Jerome Forissier79013242021-07-28 10:24:04 +02003100 uint16_t dn_size, total_dn_size; /* excluding length bytes */
Jens Wiklander817466c2018-05-22 13:49:31 +02003101 size_t ct_len, sa_len; /* including length bytes */
3102 unsigned char *buf, *p;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003103 const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Jens Wiklander817466c2018-05-22 13:49:31 +02003104 const mbedtls_x509_crt *crt;
3105 int authmode;
3106
3107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
3108
3109 ssl->state++;
3110
3111#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3112 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
3113 authmode = ssl->handshake->sni_authmode;
3114 else
3115#endif
3116 authmode = ssl->conf->authmode;
3117
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003118 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ||
Jens Wiklander817466c2018-05-22 13:49:31 +02003119 authmode == MBEDTLS_SSL_VERIFY_NONE )
3120 {
3121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
3122 return( 0 );
3123 }
3124
3125 /*
3126 * 0 . 0 handshake type
3127 * 1 . 3 handshake length
3128 * 4 . 4 cert type count
3129 * 5 .. m-1 cert types
3130 * m .. m+1 sig alg length (TLS 1.2 only)
3131 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
3132 * n .. n+1 length of all DNs
3133 * n+2 .. n+3 length of DN 1
3134 * n+4 .. ... Distinguished Name #1
3135 * ... .. ... length of DN 2, etc.
3136 */
3137 buf = ssl->out_msg;
3138 p = buf + 4;
3139
3140 /*
3141 * Supported certificate types
3142 *
3143 * ClientCertificateType certificate_types<1..2^8-1>;
3144 * enum { (255) } ClientCertificateType;
3145 */
3146 ct_len = 0;
3147
3148#if defined(MBEDTLS_RSA_C)
3149 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN;
3150#endif
3151#if defined(MBEDTLS_ECDSA_C)
3152 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN;
3153#endif
3154
3155 p[0] = (unsigned char) ct_len++;
3156 p += ct_len;
3157
3158 sa_len = 0;
3159#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3160 /*
3161 * Add signature_algorithms for verify (TLS 1.2)
3162 *
3163 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
3164 *
3165 * struct {
3166 * HashAlgorithm hash;
3167 * SignatureAlgorithm signature;
3168 * } SignatureAndHashAlgorithm;
3169 *
3170 * enum { (255) } HashAlgorithm;
3171 * enum { (255) } SignatureAlgorithm;
3172 */
3173 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
3174 {
3175 const int *cur;
3176
3177 /*
3178 * Supported signature algorithms
3179 */
3180 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
3181 {
3182 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *cur );
3183
3184 if( MBEDTLS_SSL_HASH_NONE == hash || mbedtls_ssl_set_calc_verify_md( ssl, hash ) )
3185 continue;
3186
3187#if defined(MBEDTLS_RSA_C)
3188 p[2 + sa_len++] = hash;
3189 p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA;
3190#endif
3191#if defined(MBEDTLS_ECDSA_C)
3192 p[2 + sa_len++] = hash;
3193 p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA;
3194#endif
3195 }
3196
Jerome Forissier039e02d2022-08-09 17:10:15 +02003197 MBEDTLS_PUT_UINT16_BE( sa_len, p, 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +02003198 sa_len += 2;
3199 p += sa_len;
3200 }
3201#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3202
3203 /*
3204 * DistinguishedName certificate_authorities<0..2^16-1>;
3205 * opaque DistinguishedName<1..2^16-1>;
3206 */
3207 p += 2;
3208
3209 total_dn_size = 0;
3210
3211 if( ssl->conf->cert_req_ca_list == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED )
3212 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003213 /* NOTE: If trusted certificates are provisioned
3214 * via a CA callback (configured through
3215 * `mbedtls_ssl_conf_ca_cb()`, then the
3216 * CertificateRequest is currently left empty. */
3217
Jens Wiklander817466c2018-05-22 13:49:31 +02003218#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3219 if( ssl->handshake->sni_ca_chain != NULL )
3220 crt = ssl->handshake->sni_ca_chain;
3221 else
3222#endif
3223 crt = ssl->conf->ca_chain;
3224
3225 while( crt != NULL && crt->version != 0 )
3226 {
Jerome Forissier79013242021-07-28 10:24:04 +02003227 /* It follows from RFC 5280 A.1 that this length
3228 * can be represented in at most 11 bits. */
3229 dn_size = (uint16_t) crt->subject_raw.len;
Jens Wiklander817466c2018-05-22 13:49:31 +02003230
Jerome Forissier79013242021-07-28 10:24:04 +02003231 if( end < p || (size_t)( end - p ) < 2 + (size_t) dn_size )
Jens Wiklander817466c2018-05-22 13:49:31 +02003232 {
3233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
3234 break;
3235 }
3236
Jerome Forissier039e02d2022-08-09 17:10:15 +02003237 MBEDTLS_PUT_UINT16_BE( dn_size, p, 0 );
3238 p += 2;
Jens Wiklander817466c2018-05-22 13:49:31 +02003239 memcpy( p, crt->subject_raw.p, dn_size );
3240 p += dn_size;
3241
3242 MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size );
3243
3244 total_dn_size += 2 + dn_size;
3245 crt = crt->next;
3246 }
3247 }
3248
3249 ssl->out_msglen = p - buf;
3250 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3251 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST;
Jerome Forissier039e02d2022-08-09 17:10:15 +02003252 MBEDTLS_PUT_UINT16_BE( total_dn_size, ssl->out_msg, 4 + ct_len + sa_len );
Jens Wiklander817466c2018-05-22 13:49:31 +02003253
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003254 ret = mbedtls_ssl_write_handshake_msg( ssl );
Jens Wiklander817466c2018-05-22 13:49:31 +02003255
3256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
3257
3258 return( ret );
3259}
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003260#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02003261
3262#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3263 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Jerome Forissier039e02d2022-08-09 17:10:15 +02003264MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02003265static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
3266{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003267 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerome Forissier039e02d2022-08-09 17:10:15 +02003268 mbedtls_pk_context *own_key = mbedtls_ssl_own_key( ssl );
Jens Wiklander817466c2018-05-22 13:49:31 +02003269
Jerome Forissier039e02d2022-08-09 17:10:15 +02003270 /* Check if the key is a transparent ECDH key.
3271 * This also ensures that it is safe to call mbedtls_pk_ec(). */
3272 if( mbedtls_pk_get_type( own_key ) != MBEDTLS_PK_ECKEY &&
3273 mbedtls_pk_get_type( own_key ) != MBEDTLS_PK_ECKEY_DH )
Jens Wiklander817466c2018-05-22 13:49:31 +02003274 {
3275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
3276 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
3277 }
3278
3279 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx,
Jerome Forissier039e02d2022-08-09 17:10:15 +02003280 mbedtls_pk_ec( *own_key ),
Jens Wiklander817466c2018-05-22 13:49:31 +02003281 MBEDTLS_ECDH_OURS ) ) != 0 )
3282 {
3283 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
3284 return( ret );
3285 }
3286
3287 return( 0 );
3288}
3289#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
3290 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
3291
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003292#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003293 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Jerome Forissier039e02d2022-08-09 17:10:15 +02003294MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003295static int ssl_resume_server_key_exchange( mbedtls_ssl_context *ssl,
3296 size_t *signature_len )
Jens Wiklander817466c2018-05-22 13:49:31 +02003297{
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003298 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3299 * signature length which will be added in ssl_write_server_key_exchange
3300 * after the call to ssl_prepare_server_key_exchange.
3301 * ssl_write_server_key_exchange also takes care of incrementing
3302 * ssl->out_msglen. */
3303 unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2;
3304 size_t sig_max_len = ( ssl->out_buf + MBEDTLS_SSL_OUT_CONTENT_LEN
3305 - sig_start );
3306 int ret = ssl->conf->f_async_resume( ssl,
3307 sig_start, signature_len, sig_max_len );
3308 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3309 {
3310 ssl->handshake->async_in_progress = 0;
3311 mbedtls_ssl_set_async_operation_data( ssl, NULL );
3312 }
3313 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_resume_server_key_exchange", ret );
3314 return( ret );
3315}
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003316#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003317 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
3318
3319/* Prepare the ServerKeyExchange message, up to and including
3320 * calculating the signature if any, but excluding formatting the
3321 * signature and sending the message. */
Jerome Forissier039e02d2022-08-09 17:10:15 +02003322MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003323static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl,
3324 size_t *signature_len )
3325{
Jens Wiklander817466c2018-05-22 13:49:31 +02003326 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003327 ssl->handshake->ciphersuite_info;
3328
3329#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED)
3330#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003331 unsigned char *dig_signed = NULL;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003332#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
3333#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02003334
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003335 (void) ciphersuite_info; /* unused in some configurations */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003336#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003337 (void) signature_len;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003338#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003339
3340 ssl->out_msglen = 4; /* header (type:1, length:3) to be written later */
Jens Wiklander817466c2018-05-22 13:49:31 +02003341
3342 /*
3343 *
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003344 * Part 1: Provide key exchange parameters for chosen ciphersuite.
Jens Wiklander817466c2018-05-22 13:49:31 +02003345 *
3346 */
3347
3348 /*
3349 * - ECJPAKE key exchanges
3350 */
3351#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3352 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3353 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003354 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003355 size_t len = 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02003356
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003357 ret = mbedtls_ecjpake_write_round_two(
3358 &ssl->handshake->ecjpake_ctx,
3359 ssl->out_msg + ssl->out_msglen,
3360 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len,
3361 ssl->conf->f_rng, ssl->conf->p_rng );
Jens Wiklander817466c2018-05-22 13:49:31 +02003362 if( ret != 0 )
3363 {
3364 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3365 return( ret );
3366 }
3367
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003368 ssl->out_msglen += len;
Jens Wiklander817466c2018-05-22 13:49:31 +02003369 }
3370#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
3371
3372 /*
3373 * For (EC)DHE key exchanges with PSK, parameters are prefixed by support
3374 * identity hint (RFC 4279, Sec. 3). Until someone needs this feature,
3375 * we use empty support identity hints here.
3376 **/
3377#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
3378 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3379 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
3380 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
3381 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003382 ssl->out_msg[ssl->out_msglen++] = 0x00;
3383 ssl->out_msg[ssl->out_msglen++] = 0x00;
Jens Wiklander817466c2018-05-22 13:49:31 +02003384 }
3385#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
3386 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
3387
3388 /*
3389 * - DHE key exchanges
3390 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003391#if defined(MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02003392 if( mbedtls_ssl_ciphersuite_uses_dhe( ciphersuite_info ) )
3393 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003394 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003395 size_t len = 0;
3396
Jens Wiklander817466c2018-05-22 13:49:31 +02003397 if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL )
3398 {
3399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) );
3400 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3401 }
3402
3403 /*
3404 * Ephemeral DH parameters:
3405 *
3406 * struct {
3407 * opaque dh_p<1..2^16-1>;
3408 * opaque dh_g<1..2^16-1>;
3409 * opaque dh_Ys<1..2^16-1>;
3410 * } ServerDHParams;
3411 */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003412 if( ( ret = mbedtls_dhm_set_group( &ssl->handshake->dhm_ctx,
3413 &ssl->conf->dhm_P,
3414 &ssl->conf->dhm_G ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003415 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003416 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_set_group", ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02003417 return( ret );
3418 }
3419
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003420 if( ( ret = mbedtls_dhm_make_params(
3421 &ssl->handshake->dhm_ctx,
3422 (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
3423 ssl->out_msg + ssl->out_msglen, &len,
3424 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003425 {
3426 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret );
3427 return( ret );
3428 }
3429
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003430#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003431 dig_signed = ssl->out_msg + ssl->out_msglen;
Jens Wiklander817466c2018-05-22 13:49:31 +02003432#endif
3433
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003434 ssl->out_msglen += len;
Jens Wiklander817466c2018-05-22 13:49:31 +02003435
3436 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
3437 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
3438 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
3439 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
3440 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003441#endif /* MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02003442
3443 /*
3444 * - ECDHE key exchanges
3445 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003446#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02003447 if( mbedtls_ssl_ciphersuite_uses_ecdhe( ciphersuite_info ) )
3448 {
3449 /*
3450 * Ephemeral ECDH parameters:
3451 *
3452 * struct {
3453 * ECParameters curve_params;
3454 * ECPoint public;
3455 * } ServerECDHParams;
3456 */
3457 const mbedtls_ecp_curve_info **curve = NULL;
3458 const mbedtls_ecp_group_id *gid;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003459 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003460 size_t len = 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02003461
3462 /* Match our preference list against the offered curves */
3463 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
3464 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
3465 if( (*curve)->grp_id == *gid )
3466 goto curve_matching_done;
3467
3468curve_matching_done:
3469 if( curve == NULL || *curve == NULL )
3470 {
3471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
3472 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
3473 }
3474
3475 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
3476
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003477 if( ( ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx,
3478 (*curve)->grp_id ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003479 {
3480 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret );
3481 return( ret );
3482 }
3483
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003484 if( ( ret = mbedtls_ecdh_make_params(
3485 &ssl->handshake->ecdh_ctx, &len,
3486 ssl->out_msg + ssl->out_msglen,
3487 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen,
3488 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003489 {
3490 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret );
3491 return( ret );
3492 }
3493
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003494#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003495 dig_signed = ssl->out_msg + ssl->out_msglen;
Jens Wiklander817466c2018-05-22 13:49:31 +02003496#endif
3497
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003498 ssl->out_msglen += len;
Jens Wiklander817466c2018-05-22 13:49:31 +02003499
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003500 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3501 MBEDTLS_DEBUG_ECDH_Q );
Jens Wiklander817466c2018-05-22 13:49:31 +02003502 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003503#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02003504
3505 /*
3506 *
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003507 * Part 2: For key exchanges involving the server signing the
Jens Wiklander817466c2018-05-22 13:49:31 +02003508 * exchange parameters, compute and add the signature here.
3509 *
3510 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003511#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02003512 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
3513 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003514 size_t dig_signed_len = ssl->out_msg + ssl->out_msglen - dig_signed;
3515 size_t hashlen = 0;
Jerome Forissier039e02d2022-08-09 17:10:15 +02003516#if defined(MBEDTLS_USE_PSA_CRYPTO)
3517 unsigned char hash[PSA_HASH_MAX_SIZE];
3518#else
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003519 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Jerome Forissier039e02d2022-08-09 17:10:15 +02003520#endif
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003521 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02003522
3523 /*
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003524 * 2.1: Choose hash algorithm:
3525 * A: For TLS 1.2, obey signature-hash-algorithm extension
Jens Wiklander817466c2018-05-22 13:49:31 +02003526 * to choose appropriate hash.
3527 * B: For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1
3528 * (RFC 4492, Sec. 5.4)
3529 * C: Otherwise, use MD5 + SHA1 (RFC 4346, Sec. 7.4.3)
3530 */
3531
3532 mbedtls_md_type_t md_alg;
3533
3534#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3535 mbedtls_pk_type_t sig_alg =
3536 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
3537 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
3538 {
3539 /* A: For TLS 1.2, obey signature-hash-algorithm extension
3540 * (RFC 5246, Sec. 7.4.1.4.1). */
3541 if( sig_alg == MBEDTLS_PK_NONE ||
3542 ( md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
3543 sig_alg ) ) == MBEDTLS_MD_NONE )
3544 {
3545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003546 /* (... because we choose a cipher suite
Jens Wiklander817466c2018-05-22 13:49:31 +02003547 * only if there is a matching hash.) */
3548 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3549 }
3550 }
3551 else
3552#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3553#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3554 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3555 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
3556 {
3557 /* B: Default hash SHA1 */
3558 md_alg = MBEDTLS_MD_SHA1;
3559 }
3560 else
3561#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3562 MBEDTLS_SSL_PROTO_TLS1_1 */
3563 {
3564 /* C: MD5 + SHA1 */
3565 md_alg = MBEDTLS_MD_NONE;
3566 }
3567
Jerome Forissier79013242021-07-28 10:24:04 +02003568 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pick hash algorithm %u for signing", (unsigned) md_alg ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02003569
3570 /*
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003571 * 2.2: Compute the hash to be signed
Jens Wiklander817466c2018-05-22 13:49:31 +02003572 */
3573#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3574 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3575 if( md_alg == MBEDTLS_MD_NONE )
3576 {
Jens Wiklander817466c2018-05-22 13:49:31 +02003577 hashlen = 36;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003578 ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash,
3579 dig_signed,
3580 dig_signed_len );
3581 if( ret != 0 )
3582 return( ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02003583 }
3584 else
3585#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3586 MBEDTLS_SSL_PROTO_TLS1_1 */
3587#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3588 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3589 if( md_alg != MBEDTLS_MD_NONE )
3590 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003591 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
3592 dig_signed,
3593 dig_signed_len,
3594 md_alg );
3595 if( ret != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003596 return( ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02003597 }
3598 else
3599#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3600 MBEDTLS_SSL_PROTO_TLS1_2 */
3601 {
3602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3603 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3604 }
3605
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003606 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Jens Wiklander817466c2018-05-22 13:49:31 +02003607
3608 /*
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003609 * 2.3: Compute and add the signature
Jens Wiklander817466c2018-05-22 13:49:31 +02003610 */
Jens Wiklander817466c2018-05-22 13:49:31 +02003611#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3612 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
3613 {
3614 /*
3615 * For TLS 1.2, we need to specify signature and hash algorithm
3616 * explicitly through a prefix to the signature.
3617 *
3618 * struct {
3619 * HashAlgorithm hash;
3620 * SignatureAlgorithm signature;
3621 * } SignatureAndHashAlgorithm;
3622 *
3623 * struct {
3624 * SignatureAndHashAlgorithm algorithm;
3625 * opaque signature<0..2^16-1>;
3626 * } DigitallySigned;
3627 *
3628 */
3629
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003630 ssl->out_msg[ssl->out_msglen++] =
3631 mbedtls_ssl_hash_from_md_alg( md_alg );
3632 ssl->out_msg[ssl->out_msglen++] =
3633 mbedtls_ssl_sig_from_pk_alg( sig_alg );
Jens Wiklander817466c2018-05-22 13:49:31 +02003634 }
3635#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3636
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003637#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3638 if( ssl->conf->f_async_sign_start != NULL )
3639 {
3640 ret = ssl->conf->f_async_sign_start( ssl,
3641 mbedtls_ssl_own_cert( ssl ),
3642 md_alg, hash, hashlen );
3643 switch( ret )
3644 {
3645 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3646 /* act as if f_async_sign was null */
3647 break;
3648 case 0:
3649 ssl->handshake->async_in_progress = 1;
3650 return( ssl_resume_server_key_exchange( ssl, signature_len ) );
3651 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
3652 ssl->handshake->async_in_progress = 1;
3653 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3654 default:
3655 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_sign_start", ret );
3656 return( ret );
3657 }
3658 }
3659#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3660
3661 if( mbedtls_ssl_own_key( ssl ) == NULL )
3662 {
3663 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3664 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3665 }
3666
3667 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3668 * signature length which will be added in ssl_write_server_key_exchange
3669 * after the call to ssl_prepare_server_key_exchange.
3670 * ssl_write_server_key_exchange also takes care of incrementing
3671 * ssl->out_msglen. */
3672 if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ),
3673 md_alg, hash, hashlen,
3674 ssl->out_msg + ssl->out_msglen + 2,
3675 signature_len,
3676 ssl->conf->f_rng,
3677 ssl->conf->p_rng ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003678 {
3679 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
3680 return( ret );
3681 }
Jens Wiklander817466c2018-05-22 13:49:31 +02003682 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003683#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02003684
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003685 return( 0 );
3686}
Jens Wiklander817466c2018-05-22 13:49:31 +02003687
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003688/* Prepare the ServerKeyExchange message and send it. For ciphersuites
3689 * that do not include a ServerKeyExchange message, do nothing. Either
3690 * way, if successful, move on to the next step in the SSL state
3691 * machine. */
Jerome Forissier039e02d2022-08-09 17:10:15 +02003692MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003693static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl )
3694{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003695 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003696 size_t signature_len = 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003697#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003698 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003699 ssl->handshake->ciphersuite_info;
3700#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003701
3702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
3703
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003704#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003705 /* Extract static ECDH parameters and abort if ServerKeyExchange
3706 * is not needed. */
3707 if( mbedtls_ssl_ciphersuite_no_pfs( ciphersuite_info ) )
3708 {
3709 /* For suites involving ECDH, extract DH parameters
3710 * from certificate at this point. */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003711#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003712 if( mbedtls_ssl_ciphersuite_uses_ecdh( ciphersuite_info ) )
3713 {
Jerome Forissier039e02d2022-08-09 17:10:15 +02003714 ret = ssl_get_ecdh_params_from_cert( ssl );
3715 if( ret != 0 )
3716 {
3717 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
3718 return( ret );
3719 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003720 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003721#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003722
3723 /* Key exchanges not involving ephemeral keys don't use
3724 * ServerKeyExchange, so end here. */
3725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
3726 ssl->state++;
3727 return( 0 );
3728 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003729#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003730
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003731#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003732 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3733 /* If we have already prepared the message and there is an ongoing
3734 * signature operation, resume signing. */
3735 if( ssl->handshake->async_in_progress != 0 )
3736 {
3737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming signature operation" ) );
3738 ret = ssl_resume_server_key_exchange( ssl, &signature_len );
3739 }
3740 else
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003741#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003742 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
3743 {
3744 /* ServerKeyExchange is needed. Prepare the message. */
3745 ret = ssl_prepare_server_key_exchange( ssl, &signature_len );
3746 }
3747
3748 if( ret != 0 )
3749 {
3750 /* If we're starting to write a new message, set ssl->out_msglen
3751 * to 0. But if we're resuming after an asynchronous message,
3752 * out_msglen is the amount of data written so far and mst be
3753 * preserved. */
3754 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange (pending)" ) );
3756 else
3757 ssl->out_msglen = 0;
3758 return( ret );
3759 }
3760
3761 /* If there is a signature, write its length.
3762 * ssl_prepare_server_key_exchange already wrote the signature
3763 * itself at its proper place in the output buffer. */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003764#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003765 if( signature_len != 0 )
3766 {
Jerome Forissier039e02d2022-08-09 17:10:15 +02003767 ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_1( signature_len );
3768 ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_0( signature_len );
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003769
3770 MBEDTLS_SSL_DEBUG_BUF( 3, "my signature",
3771 ssl->out_msg + ssl->out_msglen,
3772 signature_len );
3773
3774 /* Skip over the already-written signature */
3775 ssl->out_msglen += signature_len;
3776 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003777#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003778
3779 /* Add header and send. */
Jens Wiklander817466c2018-05-22 13:49:31 +02003780 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3781 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE;
3782
3783 ssl->state++;
3784
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003785 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003786 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003787 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02003788 return( ret );
3789 }
3790
3791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02003792 return( 0 );
3793}
3794
Jerome Forissier039e02d2022-08-09 17:10:15 +02003795MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02003796static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl )
3797{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003798 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02003799
3800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
3801
3802 ssl->out_msglen = 4;
3803 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3804 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE;
3805
3806 ssl->state++;
3807
3808#if defined(MBEDTLS_SSL_PROTO_DTLS)
3809 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3810 mbedtls_ssl_send_flight_completed( ssl );
3811#endif
3812
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003813 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003814 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003815 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02003816 return( ret );
3817 }
3818
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003819#if defined(MBEDTLS_SSL_PROTO_DTLS)
3820 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3821 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3822 {
3823 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
3824 return( ret );
3825 }
3826#endif /* MBEDTLS_SSL_PROTO_DTLS */
3827
Jens Wiklander817466c2018-05-22 13:49:31 +02003828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
3829
3830 return( 0 );
3831}
3832
3833#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3834 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Jerome Forissier039e02d2022-08-09 17:10:15 +02003835MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02003836static int ssl_parse_client_dh_public( mbedtls_ssl_context *ssl, unsigned char **p,
3837 const unsigned char *end )
3838{
3839 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3840 size_t n;
3841
3842 /*
3843 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3844 */
3845 if( *p + 2 > end )
3846 {
3847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3848 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3849 }
3850
3851 n = ( (*p)[0] << 8 ) | (*p)[1];
3852 *p += 2;
3853
3854 if( *p + n > end )
3855 {
3856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3857 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3858 }
3859
3860 if( ( ret = mbedtls_dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
3861 {
3862 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret );
3863 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3864 }
3865
3866 *p += n;
3867
3868 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
3869
3870 return( ret );
3871}
3872#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3873 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
3874
3875#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3876 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003877
3878#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Jerome Forissier039e02d2022-08-09 17:10:15 +02003879MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003880static int ssl_resume_decrypt_pms( mbedtls_ssl_context *ssl,
3881 unsigned char *peer_pms,
3882 size_t *peer_pmslen,
3883 size_t peer_pmssize )
3884{
3885 int ret = ssl->conf->f_async_resume( ssl,
3886 peer_pms, peer_pmslen, peer_pmssize );
3887 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3888 {
3889 ssl->handshake->async_in_progress = 0;
3890 mbedtls_ssl_set_async_operation_data( ssl, NULL );
3891 }
3892 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_decrypt_encrypted_pms", ret );
3893 return( ret );
3894}
3895#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3896
Jerome Forissier039e02d2022-08-09 17:10:15 +02003897MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003898static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl,
3899 const unsigned char *p,
3900 const unsigned char *end,
3901 unsigned char *peer_pms,
3902 size_t *peer_pmslen,
3903 size_t peer_pmssize )
Jens Wiklander817466c2018-05-22 13:49:31 +02003904{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003905 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003906 mbedtls_pk_context *private_key = mbedtls_ssl_own_key( ssl );
3907 mbedtls_pk_context *public_key = &mbedtls_ssl_own_cert( ssl )->pk;
3908 size_t len = mbedtls_pk_get_len( public_key );
Jens Wiklander817466c2018-05-22 13:49:31 +02003909
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003910#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3911 /* If we have already started decoding the message and there is an ongoing
3912 * decryption operation, resume signing. */
3913 if( ssl->handshake->async_in_progress != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003914 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming decryption operation" ) );
3916 return( ssl_resume_decrypt_pms( ssl,
3917 peer_pms, peer_pmslen, peer_pmssize ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02003918 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003919#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Jens Wiklander817466c2018-05-22 13:49:31 +02003920
3921 /*
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003922 * Prepare to decrypt the premaster using own private RSA key
Jens Wiklander817466c2018-05-22 13:49:31 +02003923 */
3924#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3925 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3926 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
3927 {
Jerome Forissier039e02d2022-08-09 17:10:15 +02003928 if ( p + 2 > end )
3929 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3931 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3932 }
Jerome Forissier039e02d2022-08-09 17:10:15 +02003933 if( *p++ != MBEDTLS_BYTE_1( len ) ||
3934 *p++ != MBEDTLS_BYTE_0( len ) )
Jens Wiklander817466c2018-05-22 13:49:31 +02003935 {
3936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3937 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3938 }
3939 }
3940#endif
3941
3942 if( p + len != end )
3943 {
3944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3945 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3946 }
3947
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003948 /*
3949 * Decrypt the premaster secret
3950 */
3951#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3952 if( ssl->conf->f_async_decrypt_start != NULL )
3953 {
3954 ret = ssl->conf->f_async_decrypt_start( ssl,
3955 mbedtls_ssl_own_cert( ssl ),
3956 p, len );
3957 switch( ret )
3958 {
3959 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3960 /* act as if f_async_decrypt_start was null */
3961 break;
3962 case 0:
3963 ssl->handshake->async_in_progress = 1;
3964 return( ssl_resume_decrypt_pms( ssl,
3965 peer_pms,
3966 peer_pmslen,
3967 peer_pmssize ) );
3968 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
3969 ssl->handshake->async_in_progress = 1;
3970 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3971 default:
3972 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_decrypt_start", ret );
3973 return( ret );
3974 }
3975 }
3976#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3977
3978 if( ! mbedtls_pk_can_do( private_key, MBEDTLS_PK_RSA ) )
3979 {
3980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
3981 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3982 }
3983
3984 ret = mbedtls_pk_decrypt( private_key, p, len,
3985 peer_pms, peer_pmslen, peer_pmssize,
3986 ssl->conf->f_rng, ssl->conf->p_rng );
3987 return( ret );
3988}
3989
Jerome Forissier039e02d2022-08-09 17:10:15 +02003990MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003991static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl,
3992 const unsigned char *p,
3993 const unsigned char *end,
3994 size_t pms_offset )
3995{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003996 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003997 unsigned char *pms = ssl->handshake->premaster + pms_offset;
3998 unsigned char ver[2];
3999 unsigned char fake_pms[48], peer_pms[48];
4000 unsigned char mask;
4001 size_t i, peer_pmslen;
4002 unsigned int diff;
4003
4004 /* In case of a failure in decryption, the decryption may write less than
4005 * 2 bytes of output, but we always read the first two bytes. It doesn't
4006 * matter in the end because diff will be nonzero in that case due to
Jerome Forissier79013242021-07-28 10:24:04 +02004007 * ret being nonzero, and we only care whether diff is 0.
4008 * But do initialize peer_pms and peer_pmslen for robustness anyway. This
4009 * also makes memory analyzers happy (don't access uninitialized memory,
4010 * even if it's an unsigned char). */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004011 peer_pms[0] = peer_pms[1] = ~0;
Jerome Forissier79013242021-07-28 10:24:04 +02004012 peer_pmslen = 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004013
4014 ret = ssl_decrypt_encrypted_pms( ssl, p, end,
4015 peer_pms,
4016 &peer_pmslen,
4017 sizeof( peer_pms ) );
4018
4019#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
4020 if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
4021 return( ret );
4022#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4023
Jens Wiklander817466c2018-05-22 13:49:31 +02004024 mbedtls_ssl_write_version( ssl->handshake->max_major_ver,
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004025 ssl->handshake->max_minor_ver,
4026 ssl->conf->transport, ver );
4027
4028 /* Avoid data-dependent branches while checking for invalid
4029 * padding, to protect against timing-based Bleichenbacher-type
4030 * attacks. */
4031 diff = (unsigned int) ret;
4032 diff |= peer_pmslen ^ 48;
4033 diff |= peer_pms[0] ^ ver[0];
4034 diff |= peer_pms[1] ^ ver[1];
4035
4036 /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */
Jerome Forissier039e02d2022-08-09 17:10:15 +02004037 mask = mbedtls_ct_uint_mask( diff );
Jens Wiklander817466c2018-05-22 13:49:31 +02004038
4039 /*
4040 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
4041 * must not cause the connection to end immediately; instead, send a
4042 * bad_record_mac later in the handshake.
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004043 * To protect against timing-based variants of the attack, we must
4044 * not have any branch that depends on whether the decryption was
4045 * successful. In particular, always generate the fake premaster secret,
4046 * regardless of whether it will ultimately influence the output or not.
Jens Wiklander817466c2018-05-22 13:49:31 +02004047 */
4048 ret = ssl->conf->f_rng( ssl->conf->p_rng, fake_pms, sizeof( fake_pms ) );
4049 if( ret != 0 )
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004050 {
4051 /* It's ok to abort on an RNG failure, since this does not reveal
4052 * anything about the RSA decryption. */
Jens Wiklander817466c2018-05-22 13:49:31 +02004053 return( ret );
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004054 }
Jens Wiklander817466c2018-05-22 13:49:31 +02004055
4056#if defined(MBEDTLS_SSL_DEBUG_ALL)
4057 if( diff != 0 )
4058 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4059#endif
4060
4061 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
4062 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
4063 {
4064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4065 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4066 }
4067 ssl->handshake->pmslen = 48;
4068
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004069 /* Set pms to either the true or the fake PMS, without
4070 * data-dependent branches. */
Jens Wiklander817466c2018-05-22 13:49:31 +02004071 for( i = 0; i < ssl->handshake->pmslen; i++ )
4072 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
4073
4074 return( 0 );
4075}
4076#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
4077 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
4078
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004079#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerome Forissier039e02d2022-08-09 17:10:15 +02004080MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02004081static int ssl_parse_client_psk_identity( mbedtls_ssl_context *ssl, unsigned char **p,
4082 const unsigned char *end )
4083{
4084 int ret = 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004085 uint16_t n;
Jens Wiklander817466c2018-05-22 13:49:31 +02004086
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004087 if( ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02004088 {
4089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
4090 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
4091 }
4092
4093 /*
4094 * Receive client pre-shared key identity name
4095 */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004096 if( end - *p < 2 )
Jens Wiklander817466c2018-05-22 13:49:31 +02004097 {
4098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4099 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
4100 }
4101
4102 n = ( (*p)[0] << 8 ) | (*p)[1];
4103 *p += 2;
4104
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004105 if( n == 0 || n > end - *p )
Jens Wiklander817466c2018-05-22 13:49:31 +02004106 {
4107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4108 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
4109 }
4110
4111 if( ssl->conf->f_psk != NULL )
4112 {
4113 if( ssl->conf->f_psk( ssl->conf->p_psk, ssl, *p, n ) != 0 )
4114 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
4115 }
4116 else
4117 {
4118 /* Identity is not a big secret since clients send it in the clear,
4119 * but treat it carefully anyway, just in case */
4120 if( n != ssl->conf->psk_identity_len ||
Jerome Forissier039e02d2022-08-09 17:10:15 +02004121 mbedtls_ct_memcmp( ssl->conf->psk_identity, *p, n ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02004122 {
4123 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
4124 }
4125 }
4126
4127 if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY )
4128 {
4129 MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
4130 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4131 MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY );
4132 return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
4133 }
4134
4135 *p += n;
4136
4137 return( 0 );
4138}
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004139#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02004140
Jerome Forissier039e02d2022-08-09 17:10:15 +02004141MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02004142static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl )
4143{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004144 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02004145 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
4146 unsigned char *p, *end;
4147
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004148 ciphersuite_info = ssl->handshake->ciphersuite_info;
Jens Wiklander817466c2018-05-22 13:49:31 +02004149
4150 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
4151
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004152#if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \
4153 ( defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
4154 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) )
4155 if( ( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
4156 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) &&
4157 ( ssl->handshake->async_in_progress != 0 ) )
4158 {
4159 /* We've already read a record and there is an asynchronous
4160 * operation in progress to decrypt it. So skip reading the
4161 * record. */
4162 MBEDTLS_SSL_DEBUG_MSG( 3, ( "will resume decryption of previously-read record" ) );
4163 }
4164 else
4165#endif
4166 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02004167 {
4168 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
4169 return( ret );
4170 }
4171
4172 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
4173 end = ssl->in_msg + ssl->in_hslen;
4174
4175 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
4176 {
4177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4178 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
4179 }
4180
4181 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE )
4182 {
4183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
4184 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
4185 }
4186
4187#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
4188 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
4189 {
4190 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
4191 {
4192 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
4193 return( ret );
4194 }
4195
4196 if( p != end )
4197 {
4198 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4199 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
4200 }
4201
4202 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
4203 ssl->handshake->premaster,
4204 MBEDTLS_PREMASTER_SIZE,
4205 &ssl->handshake->pmslen,
4206 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
4207 {
4208 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
4209 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
4210 }
4211
4212 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
4213 }
4214 else
4215#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
4216#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
4217 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
4218 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
4219 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
4220 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
4221 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
4222 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
4223 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
4224 {
4225 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
4226 p, end - p) ) != 0 )
4227 {
4228 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
4229 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
4230 }
4231
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004232 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4233 MBEDTLS_DEBUG_ECDH_QP );
Jens Wiklander817466c2018-05-22 13:49:31 +02004234
4235 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
4236 &ssl->handshake->pmslen,
4237 ssl->handshake->premaster,
4238 MBEDTLS_MPI_MAX_SIZE,
4239 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
4240 {
4241 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
4242 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
4243 }
4244
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004245 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4246 MBEDTLS_DEBUG_ECDH_Z );
Jens Wiklander817466c2018-05-22 13:49:31 +02004247 }
4248 else
4249#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
4250 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
4251 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
4252 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
4253#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
4254 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
4255 {
4256 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4257 {
4258 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
4259 return( ret );
4260 }
4261
4262 if( p != end )
4263 {
4264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4265 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
4266 }
4267
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004268#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerome Forissier039e02d2022-08-09 17:10:15 +02004269 /* For opaque PSKs, we perform the PSK-to-MS derivation automatically
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004270 * and skip the intermediate PMS. */
4271 if( ssl_use_opaque_psk( ssl ) == 1 )
4272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skip PMS generation for opaque PSK" ) );
4273 else
4274#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jens Wiklander817466c2018-05-22 13:49:31 +02004275 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
4276 ciphersuite_info->key_exchange ) ) != 0 )
4277 {
4278 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
4279 return( ret );
4280 }
4281 }
4282 else
4283#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
4284#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
4285 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4286 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004287#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
4288 if ( ssl->handshake->async_in_progress != 0 )
4289 {
4290 /* There is an asynchronous operation in progress to
4291 * decrypt the encrypted premaster secret, so skip
4292 * directly to resuming this operation. */
4293 MBEDTLS_SSL_DEBUG_MSG( 3, ( "PSK identity already parsed" ) );
4294 /* Update p to skip the PSK identity. ssl_parse_encrypted_pms
4295 * won't actually use it, but maintain p anyway for robustness. */
4296 p += ssl->conf->psk_identity_len + 2;
4297 }
4298 else
4299#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Jens Wiklander817466c2018-05-22 13:49:31 +02004300 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4301 {
4302 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
4303 return( ret );
4304 }
4305
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004306#if defined(MBEDTLS_USE_PSA_CRYPTO)
4307 /* Opaque PSKs are currently only supported for PSK-only. */
4308 if( ssl_use_opaque_psk( ssl ) == 1 )
Jerome Forissier039e02d2022-08-09 17:10:15 +02004309 {
4310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "opaque PSK not supported with RSA-PSK" ) );
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004311 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerome Forissier039e02d2022-08-09 17:10:15 +02004312 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004313#endif
4314
Jens Wiklander817466c2018-05-22 13:49:31 +02004315 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
4316 {
4317 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
4318 return( ret );
4319 }
4320
4321 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
4322 ciphersuite_info->key_exchange ) ) != 0 )
4323 {
4324 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
4325 return( ret );
4326 }
4327 }
4328 else
4329#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
4330#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
4331 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
4332 {
4333 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4334 {
4335 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
4336 return( ret );
4337 }
4338 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
4339 {
4340 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
4341 return( ret );
4342 }
4343
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004344#if defined(MBEDTLS_USE_PSA_CRYPTO)
4345 /* Opaque PSKs are currently only supported for PSK-only. */
4346 if( ssl_use_opaque_psk( ssl ) == 1 )
Jerome Forissier039e02d2022-08-09 17:10:15 +02004347 {
4348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "opaque PSK not supported with DHE-PSK" ) );
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004349 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerome Forissier039e02d2022-08-09 17:10:15 +02004350 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004351#endif
4352
Jens Wiklander817466c2018-05-22 13:49:31 +02004353 if( p != end )
4354 {
4355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4356 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
4357 }
4358
4359 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
4360 ciphersuite_info->key_exchange ) ) != 0 )
4361 {
4362 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
4363 return( ret );
4364 }
4365 }
4366 else
4367#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
4368#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
4369 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
4370 {
4371 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4372 {
4373 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
4374 return( ret );
4375 }
4376
4377 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
4378 p, end - p ) ) != 0 )
4379 {
4380 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
4381 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
4382 }
4383
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004384#if defined(MBEDTLS_USE_PSA_CRYPTO)
4385 /* Opaque PSKs are currently only supported for PSK-only. */
4386 if( ssl_use_opaque_psk( ssl ) == 1 )
Jerome Forissier039e02d2022-08-09 17:10:15 +02004387 {
4388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "opaque PSK not supported with ECDHE-PSK" ) );
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004389 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerome Forissier039e02d2022-08-09 17:10:15 +02004390 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004391#endif
4392
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004393 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4394 MBEDTLS_DEBUG_ECDH_QP );
Jens Wiklander817466c2018-05-22 13:49:31 +02004395
4396 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
4397 ciphersuite_info->key_exchange ) ) != 0 )
4398 {
4399 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
4400 return( ret );
4401 }
4402 }
4403 else
4404#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
4405#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
4406 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
4407 {
4408 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
4409 {
4410 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
4411 return( ret );
4412 }
4413 }
4414 else
4415#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
4416#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
4417 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
4418 {
4419 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
4420 p, end - p );
4421 if( ret != 0 )
4422 {
4423 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
4424 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
4425 }
4426
4427 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
4428 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
4429 ssl->conf->f_rng, ssl->conf->p_rng );
4430 if( ret != 0 )
4431 {
4432 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
4433 return( ret );
4434 }
4435 }
4436 else
4437#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
4438 {
4439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4440 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4441 }
4442
4443 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
4444 {
4445 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
4446 return( ret );
4447 }
4448
4449 ssl->state++;
4450
4451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
4452
4453 return( 0 );
4454}
4455
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004456#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Jerome Forissier039e02d2022-08-09 17:10:15 +02004457MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02004458static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
4459{
4460 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004461 ssl->handshake->ciphersuite_info;
Jens Wiklander817466c2018-05-22 13:49:31 +02004462
4463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
4464
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004465 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Jens Wiklander817466c2018-05-22 13:49:31 +02004466 {
4467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4468 ssl->state++;
4469 return( 0 );
4470 }
4471
4472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4473 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4474}
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004475#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Jerome Forissier039e02d2022-08-09 17:10:15 +02004476MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02004477static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
4478{
4479 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
4480 size_t i, sig_len;
4481 unsigned char hash[48];
4482 unsigned char *hash_start = hash;
4483 size_t hashlen;
4484#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4485 mbedtls_pk_type_t pk_alg;
4486#endif
4487 mbedtls_md_type_t md_alg;
4488 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004489 ssl->handshake->ciphersuite_info;
4490 mbedtls_pk_context * peer_pk;
Jens Wiklander817466c2018-05-22 13:49:31 +02004491
4492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
4493
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004494 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Jens Wiklander817466c2018-05-22 13:49:31 +02004495 {
4496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4497 ssl->state++;
4498 return( 0 );
4499 }
4500
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004501#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4502 if( ssl->session_negotiate->peer_cert == NULL )
4503 {
4504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4505 ssl->state++;
4506 return( 0 );
4507 }
4508#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4509 if( ssl->session_negotiate->peer_cert_digest == NULL )
4510 {
4511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4512 ssl->state++;
4513 return( 0 );
4514 }
4515#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4516
Jens Wiklander817466c2018-05-22 13:49:31 +02004517 /* Read the message without adding it to the checksum */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004518 ret = mbedtls_ssl_read_record( ssl, 0 /* no checksum update */ );
Jens Wiklander817466c2018-05-22 13:49:31 +02004519 if( 0 != ret )
4520 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004521 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record" ), ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02004522 return( ret );
4523 }
4524
4525 ssl->state++;
4526
4527 /* Process the message contents */
4528 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
4529 ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY )
4530 {
4531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4532 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
4533 }
4534
4535 i = mbedtls_ssl_hs_hdr_len( ssl );
4536
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004537#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4538 peer_pk = &ssl->handshake->peer_pubkey;
4539#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4540 if( ssl->session_negotiate->peer_cert == NULL )
4541 {
4542 /* Should never happen */
4543 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4544 }
4545 peer_pk = &ssl->session_negotiate->peer_cert->pk;
4546#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4547
Jens Wiklander817466c2018-05-22 13:49:31 +02004548 /*
4549 * struct {
4550 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
4551 * opaque signature<0..2^16-1>;
4552 * } DigitallySigned;
4553 */
4554#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4555 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4556 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
4557 {
4558 md_alg = MBEDTLS_MD_NONE;
4559 hashlen = 36;
4560
4561 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004562 if( mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECDSA ) )
Jens Wiklander817466c2018-05-22 13:49:31 +02004563 {
4564 hash_start += 16;
4565 hashlen -= 16;
4566 md_alg = MBEDTLS_MD_SHA1;
4567 }
4568 }
4569 else
4570#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 ||
4571 MBEDTLS_SSL_PROTO_TLS1_1 */
4572#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4573 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
4574 {
4575 if( i + 2 > ssl->in_hslen )
4576 {
4577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4578 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
4579 }
4580
4581 /*
4582 * Hash
4583 */
4584 md_alg = mbedtls_ssl_md_alg_from_hash( ssl->in_msg[i] );
4585
4586 if( md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md( ssl, ssl->in_msg[i] ) )
4587 {
4588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
4589 " for verify message" ) );
4590 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
4591 }
4592
4593#if !defined(MBEDTLS_MD_SHA1)
4594 if( MBEDTLS_MD_SHA1 == md_alg )
4595 hash_start += 16;
4596#endif
4597
4598 /* Info from md_alg will be used instead */
4599 hashlen = 0;
4600
4601 i++;
4602
4603 /*
4604 * Signature
4605 */
4606 if( ( pk_alg = mbedtls_ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
4607 == MBEDTLS_PK_NONE )
4608 {
4609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
4610 " for verify message" ) );
4611 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
4612 }
4613
4614 /*
4615 * Check the certificate's key type matches the signature alg
4616 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004617 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Jens Wiklander817466c2018-05-22 13:49:31 +02004618 {
4619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
4620 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
4621 }
4622
4623 i++;
4624 }
4625 else
4626#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4627 {
4628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4629 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4630 }
4631
4632 if( i + 2 > ssl->in_hslen )
4633 {
4634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4635 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
4636 }
4637
4638 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
4639 i += 2;
4640
4641 if( i + sig_len != ssl->in_hslen )
4642 {
4643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4644 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
4645 }
4646
4647 /* Calculate hash and verify signature */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004648 {
4649 size_t dummy_hlen;
4650 ssl->handshake->calc_verify( ssl, hash, &dummy_hlen );
4651 }
Jens Wiklander817466c2018-05-22 13:49:31 +02004652
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004653 if( ( ret = mbedtls_pk_verify( peer_pk,
Jens Wiklander817466c2018-05-22 13:49:31 +02004654 md_alg, hash_start, hashlen,
4655 ssl->in_msg + i, sig_len ) ) != 0 )
4656 {
4657 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
4658 return( ret );
4659 }
4660
4661 mbedtls_ssl_update_handshake_status( ssl );
4662
4663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
4664
4665 return( ret );
4666}
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004667#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02004668
4669#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerome Forissier039e02d2022-08-09 17:10:15 +02004670MBEDTLS_CHECK_RETURN_CRITICAL
Jens Wiklander817466c2018-05-22 13:49:31 +02004671static int ssl_write_new_session_ticket( mbedtls_ssl_context *ssl )
4672{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004673 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02004674 size_t tlen;
4675 uint32_t lifetime;
4676
4677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
4678
4679 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4680 ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET;
4681
4682 /*
4683 * struct {
4684 * uint32 ticket_lifetime_hint;
4685 * opaque ticket<0..2^16-1>;
4686 * } NewSessionTicket;
4687 *
4688 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
4689 * 8 . 9 ticket_len (n)
4690 * 10 . 9+n ticket content
4691 */
4692
4693 if( ( ret = ssl->conf->f_ticket_write( ssl->conf->p_ticket,
4694 ssl->session_negotiate,
4695 ssl->out_msg + 10,
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004696 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
Jens Wiklander817466c2018-05-22 13:49:31 +02004697 &tlen, &lifetime ) ) != 0 )
4698 {
4699 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_write", ret );
4700 tlen = 0;
4701 }
4702
Jerome Forissier039e02d2022-08-09 17:10:15 +02004703 MBEDTLS_PUT_UINT32_BE( lifetime, ssl->out_msg, 4 );
4704 MBEDTLS_PUT_UINT16_BE( tlen, ssl->out_msg, 8 );
Jens Wiklander817466c2018-05-22 13:49:31 +02004705 ssl->out_msglen = 10 + tlen;
4706
4707 /*
4708 * Morally equivalent to updating ssl->state, but NewSessionTicket and
4709 * ChangeCipherSpec share the same state.
4710 */
4711 ssl->handshake->new_session_ticket = 0;
4712
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004713 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02004714 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004715 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02004716 return( ret );
4717 }
4718
4719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
4720
4721 return( 0 );
4722}
4723#endif /* MBEDTLS_SSL_SESSION_TICKETS */
4724
4725/*
4726 * SSL handshake -- server side -- single step
4727 */
4728int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl )
4729{
4730 int ret = 0;
4731
4732 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
4733 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4734
4735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
4736
4737 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4738 return( ret );
4739
4740#if defined(MBEDTLS_SSL_PROTO_DTLS)
4741 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4742 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
4743 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004744 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02004745 return( ret );
4746 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004747#endif /* MBEDTLS_SSL_PROTO_DTLS */
Jens Wiklander817466c2018-05-22 13:49:31 +02004748
4749 switch( ssl->state )
4750 {
4751 case MBEDTLS_SSL_HELLO_REQUEST:
4752 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
4753 break;
4754
4755 /*
4756 * <== ClientHello
4757 */
4758 case MBEDTLS_SSL_CLIENT_HELLO:
4759 ret = ssl_parse_client_hello( ssl );
4760 break;
4761
4762#if defined(MBEDTLS_SSL_PROTO_DTLS)
4763 case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
4764 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4765#endif
4766
4767 /*
4768 * ==> ServerHello
4769 * Certificate
4770 * ( ServerKeyExchange )
4771 * ( CertificateRequest )
4772 * ServerHelloDone
4773 */
4774 case MBEDTLS_SSL_SERVER_HELLO:
4775 ret = ssl_write_server_hello( ssl );
4776 break;
4777
4778 case MBEDTLS_SSL_SERVER_CERTIFICATE:
4779 ret = mbedtls_ssl_write_certificate( ssl );
4780 break;
4781
4782 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
4783 ret = ssl_write_server_key_exchange( ssl );
4784 break;
4785
4786 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
4787 ret = ssl_write_certificate_request( ssl );
4788 break;
4789
4790 case MBEDTLS_SSL_SERVER_HELLO_DONE:
4791 ret = ssl_write_server_hello_done( ssl );
4792 break;
4793
4794 /*
4795 * <== ( Certificate/Alert )
4796 * ClientKeyExchange
4797 * ( CertificateVerify )
4798 * ChangeCipherSpec
4799 * Finished
4800 */
4801 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
4802 ret = mbedtls_ssl_parse_certificate( ssl );
4803 break;
4804
4805 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
4806 ret = ssl_parse_client_key_exchange( ssl );
4807 break;
4808
4809 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
4810 ret = ssl_parse_certificate_verify( ssl );
4811 break;
4812
4813 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
4814 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
4815 break;
4816
4817 case MBEDTLS_SSL_CLIENT_FINISHED:
4818 ret = mbedtls_ssl_parse_finished( ssl );
4819 break;
4820
4821 /*
4822 * ==> ( NewSessionTicket )
4823 * ChangeCipherSpec
4824 * Finished
4825 */
4826 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4827#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4828 if( ssl->handshake->new_session_ticket != 0 )
4829 ret = ssl_write_new_session_ticket( ssl );
4830 else
4831#endif
4832 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
4833 break;
4834
4835 case MBEDTLS_SSL_SERVER_FINISHED:
4836 ret = mbedtls_ssl_write_finished( ssl );
4837 break;
4838
4839 case MBEDTLS_SSL_FLUSH_BUFFERS:
4840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
4841 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
4842 break;
4843
4844 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
4845 mbedtls_ssl_handshake_wrapup( ssl );
4846 break;
4847
4848 default:
4849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
4850 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4851 }
4852
4853 return( ret );
4854}
4855#endif /* MBEDTLS_SSL_SRV_C */