blob: 5f184e849cf3996ae6d65ac621e3375c0023ef28 [file] [log] [blame]
Edison Aic6672fd2018-02-28 15:01:47 +08001// SPDX-License-Identifier: Apache-2.0
Jens Wiklander817466c2018-05-22 13:49:31 +02002/*
3 * SSLv3/TLSv1 server-side functions
4 *
5 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_SSL_SRV_C)
29
30#if defined(MBEDTLS_PLATFORM_C)
31#include "mbedtls/platform.h"
32#else
33#include <stdlib.h>
34#define mbedtls_calloc calloc
35#define mbedtls_free free
36#endif
37
Jens Wiklander817466c2018-05-22 13:49:31 +020038#include "mbedtls/ssl.h"
39#include "mbedtls/ssl_internal.h"
Jerome Forissier11fa71b2020-04-20 17:17:56 +020040#include "mbedtls/debug.h"
41#include "mbedtls/error.h"
Jens Wiklander3d3b0592019-03-20 15:30:29 +010042#include "mbedtls/platform_util.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020043
44#include <string.h>
45
46#if defined(MBEDTLS_ECP_C)
47#include "mbedtls/ecp.h"
48#endif
49
50#if defined(MBEDTLS_HAVE_TIME)
51#include "mbedtls/platform_time.h"
52#endif
53
Jens Wiklander817466c2018-05-22 13:49:31 +020054#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
55int mbedtls_ssl_set_client_transport_id( mbedtls_ssl_context *ssl,
56 const unsigned char *info,
57 size_t ilen )
58{
59 if( ssl->conf->endpoint != MBEDTLS_SSL_IS_SERVER )
60 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
61
62 mbedtls_free( ssl->cli_id );
63
64 if( ( ssl->cli_id = mbedtls_calloc( 1, ilen ) ) == NULL )
65 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
66
67 memcpy( ssl->cli_id, info, ilen );
68 ssl->cli_id_len = ilen;
69
70 return( 0 );
71}
72
73void mbedtls_ssl_conf_dtls_cookies( mbedtls_ssl_config *conf,
74 mbedtls_ssl_cookie_write_t *f_cookie_write,
75 mbedtls_ssl_cookie_check_t *f_cookie_check,
76 void *p_cookie )
77{
78 conf->f_cookie_write = f_cookie_write;
79 conf->f_cookie_check = f_cookie_check;
80 conf->p_cookie = p_cookie;
81}
82#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
83
84#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
85static int ssl_parse_servername_ext( mbedtls_ssl_context *ssl,
86 const unsigned char *buf,
87 size_t len )
88{
Jerome Forissier11fa71b2020-04-20 17:17:56 +020089 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +020090 size_t servername_list_size, hostname_len;
91 const unsigned char *p;
92
93 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
94
Jens Wiklander3d3b0592019-03-20 15:30:29 +010095 if( len < 2 )
96 {
97 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
98 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
99 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
100 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
101 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200102 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
103 if( servername_list_size + 2 != len )
104 {
105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
106 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
107 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
108 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
109 }
110
111 p = buf + 2;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100112 while( servername_list_size > 2 )
Jens Wiklander817466c2018-05-22 13:49:31 +0200113 {
114 hostname_len = ( ( p[1] << 8 ) | p[2] );
115 if( hostname_len + 3 > servername_list_size )
116 {
117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
118 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
119 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
120 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
121 }
122
123 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
124 {
125 ret = ssl->conf->f_sni( ssl->conf->p_sni,
126 ssl, p + 3, hostname_len );
127 if( ret != 0 )
128 {
129 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
130 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
131 MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME );
132 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
133 }
134 return( 0 );
135 }
136
137 servername_list_size -= hostname_len + 3;
138 p += hostname_len + 3;
139 }
140
141 if( servername_list_size != 0 )
142 {
143 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
144 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
145 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
146 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
147 }
148
149 return( 0 );
150}
151#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
152
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200153#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
154static int ssl_conf_has_psk_or_cb( mbedtls_ssl_config const *conf )
155{
156 if( conf->f_psk != NULL )
157 return( 1 );
158
159 if( conf->psk_identity_len == 0 || conf->psk_identity == NULL )
160 return( 0 );
161
162 if( conf->psk != NULL && conf->psk_len != 0 )
163 return( 1 );
164
165#if defined(MBEDTLS_USE_PSA_CRYPTO)
166 if( conf->psk_opaque != 0 )
167 return( 1 );
168#endif /* MBEDTLS_USE_PSA_CRYPTO */
169
170 return( 0 );
171}
172
173#if defined(MBEDTLS_USE_PSA_CRYPTO)
174static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
175{
176 if( ssl->conf->f_psk != NULL )
177 {
178 /* If we've used a callback to select the PSK,
179 * the static configuration is irrelevant. */
180
181 if( ssl->handshake->psk_opaque != 0 )
182 return( 1 );
183
184 return( 0 );
185 }
186
187 if( ssl->conf->psk_opaque != 0 )
188 return( 1 );
189
190 return( 0 );
191}
192#endif /* MBEDTLS_USE_PSA_CRYPTO */
193#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
194
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 ||
205 mbedtls_ssl_safer_memcmp( buf + 1, ssl->peer_verify_data,
206 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 */
246static int ssl_parse_signature_algorithms_ext( mbedtls_ssl_context *ssl,
247 const unsigned char *buf,
248 size_t len )
249{
250 size_t sig_alg_list_size;
251
252 const unsigned char *p;
253 const unsigned char *end = buf + len;
254
255 mbedtls_md_type_t md_cur;
256 mbedtls_pk_type_t sig_cur;
257
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100258 if ( len < 2 ) {
259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
260 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
261 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
262 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
263 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200264 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
265 if( sig_alg_list_size + 2 != len ||
266 sig_alg_list_size % 2 != 0 )
267 {
268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
269 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
270 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
271 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
272 }
273
274 /* Currently we only guarantee signing the ServerKeyExchange message according
275 * to the constraints specified in this extension (see above), so it suffices
276 * to remember only one suitable hash for each possible signature algorithm.
277 *
278 * This will change when we also consider certificate signatures,
279 * in which case we will need to remember the whole signature-hash
280 * pair list from the extension.
281 */
282
283 for( p = buf + 2; p < end; p += 2 )
284 {
285 /* Silently ignore unknown signature or hash algorithms. */
286
287 if( ( sig_cur = mbedtls_ssl_pk_alg_from_sig( p[1] ) ) == MBEDTLS_PK_NONE )
288 {
289 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext"
290 " unknown sig alg encoding %d", p[1] ) );
291 continue;
292 }
293
294 /* Check if we support the hash the user proposes */
295 md_cur = mbedtls_ssl_md_alg_from_hash( p[0] );
296 if( md_cur == MBEDTLS_MD_NONE )
297 {
298 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:"
299 " unknown hash alg encoding %d", p[0] ) );
300 continue;
301 }
302
303 if( mbedtls_ssl_check_sig_hash( ssl, md_cur ) == 0 )
304 {
305 mbedtls_ssl_sig_hash_set_add( &ssl->handshake->hash_algs, sig_cur, md_cur );
306 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext:"
307 " match sig %d and hash %d",
308 sig_cur, md_cur ) );
309 }
310 else
311 {
312 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: "
313 "hash alg %d not supported", md_cur ) );
314 }
315 }
316
317 return( 0 );
318}
319#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200320 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +0200321
322#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
323 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
324static int ssl_parse_supported_elliptic_curves( mbedtls_ssl_context *ssl,
325 const unsigned char *buf,
326 size_t len )
327{
328 size_t list_size, our_size;
329 const unsigned char *p;
330 const mbedtls_ecp_curve_info *curve_info, **curves;
331
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100332 if ( len < 2 ) {
333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
334 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
335 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
336 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
337 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200338 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
339 if( list_size + 2 != len ||
340 list_size % 2 != 0 )
341 {
342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
343 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
344 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
345 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
346 }
347
348 /* Should never happen unless client duplicates the extension */
349 if( ssl->handshake->curves != NULL )
350 {
351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
352 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
353 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
354 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
355 }
356
357 /* Don't allow our peer to make us allocate too much memory,
358 * and leave room for a final 0 */
359 our_size = list_size / 2 + 1;
360 if( our_size > MBEDTLS_ECP_DP_MAX )
361 our_size = MBEDTLS_ECP_DP_MAX;
362
363 if( ( curves = mbedtls_calloc( our_size, sizeof( *curves ) ) ) == NULL )
364 {
365 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
366 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
367 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
368 }
369
370 ssl->handshake->curves = curves;
371
372 p = buf + 2;
373 while( list_size > 0 && our_size > 1 )
374 {
375 curve_info = mbedtls_ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
376
377 if( curve_info != NULL )
378 {
379 *curves++ = curve_info;
380 our_size--;
381 }
382
383 list_size -= 2;
384 p += 2;
385 }
386
387 return( 0 );
388}
389
390static int ssl_parse_supported_point_formats( mbedtls_ssl_context *ssl,
391 const unsigned char *buf,
392 size_t len )
393{
394 size_t list_size;
395 const unsigned char *p;
396
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100397 if( len == 0 || (size_t)( buf[0] + 1 ) != len )
Jens Wiklander817466c2018-05-22 13:49:31 +0200398 {
399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
400 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
401 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
402 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
403 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100404 list_size = buf[0];
Jens Wiklander817466c2018-05-22 13:49:31 +0200405
406 p = buf + 1;
407 while( list_size > 0 )
408 {
409 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
410 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
411 {
412#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
413 ssl->handshake->ecdh_ctx.point_format = p[0];
414#endif
415#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
416 ssl->handshake->ecjpake_ctx.point_format = p[0];
417#endif
418 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
419 return( 0 );
420 }
421
422 list_size--;
423 p++;
424 }
425
426 return( 0 );
427}
428#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
429 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
430
431#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
432static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
433 const unsigned char *buf,
434 size_t len )
435{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200436 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200437
438 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
439 {
440 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
441 return( 0 );
442 }
443
444 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
445 buf, len ) ) != 0 )
446 {
447 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
448 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
449 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
450 return( ret );
451 }
452
453 /* Only mark the extension as OK when we're sure it is */
454 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK;
455
456 return( 0 );
457}
458#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
459
460#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
461static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
462 const unsigned char *buf,
463 size_t len )
464{
465 if( len != 1 || buf[0] >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID )
466 {
467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
468 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
469 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
470 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
471 }
472
473 ssl->session_negotiate->mfl_code = buf[0];
474
475 return( 0 );
476}
477#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
478
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200479#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
480static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
481 const unsigned char *buf,
482 size_t len )
483{
484 size_t peer_cid_len;
485
486 /* CID extension only makes sense in DTLS */
487 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
488 {
489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
490 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
491 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
492 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
493 }
494
495 /*
496 * Quoting draft-ietf-tls-dtls-connection-id-05
497 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
498 *
499 * struct {
500 * opaque cid<0..2^8-1>;
501 * } ConnectionId;
502 */
503
504 if( len < 1 )
505 {
506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
507 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
508 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
509 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
510 }
511
512 peer_cid_len = *buf++;
513 len--;
514
515 if( len != peer_cid_len )
516 {
517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
518 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
519 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
520 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
521 }
522
523 /* Ignore CID if the user has disabled its use. */
524 if( ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
525 {
526 /* Leave ssl->handshake->cid_in_use in its default
527 * value of MBEDTLS_SSL_CID_DISABLED. */
528 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Client sent CID extension, but CID disabled" ) );
529 return( 0 );
530 }
531
532 if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
533 {
534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
535 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
536 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
537 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
538 }
539
540 ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
541 ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
542 memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
543
544 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
545 MBEDTLS_SSL_DEBUG_BUF( 3, "Client CID", buf, peer_cid_len );
546
547 return( 0 );
548}
549#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
550
Jens Wiklander817466c2018-05-22 13:49:31 +0200551#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
552static int ssl_parse_truncated_hmac_ext( mbedtls_ssl_context *ssl,
553 const unsigned char *buf,
554 size_t len )
555{
556 if( len != 0 )
557 {
558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
559 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
560 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
561 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
562 }
563
564 ((void) buf);
565
566 if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
567 ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED;
568
569 return( 0 );
570}
571#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
572
573#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
574static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
575 const unsigned char *buf,
576 size_t len )
577{
578 if( len != 0 )
579 {
580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
581 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
582 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
583 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
584 }
585
586 ((void) buf);
587
588 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
589 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
590 {
591 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
592 }
593
594 return( 0 );
595}
596#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
597
598#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
599static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
600 const unsigned char *buf,
601 size_t len )
602{
603 if( len != 0 )
604 {
605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
606 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
607 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
608 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
609 }
610
611 ((void) buf);
612
613 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED &&
614 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
615 {
616 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
617 }
618
619 return( 0 );
620}
621#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
622
623#if defined(MBEDTLS_SSL_SESSION_TICKETS)
624static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
625 unsigned char *buf,
626 size_t len )
627{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200628 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200629 mbedtls_ssl_session session;
630
631 mbedtls_ssl_session_init( &session );
632
633 if( ssl->conf->f_ticket_parse == NULL ||
634 ssl->conf->f_ticket_write == NULL )
635 {
636 return( 0 );
637 }
638
639 /* Remember the client asked us to send a new ticket */
640 ssl->handshake->new_session_ticket = 1;
641
642 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
643
644 if( len == 0 )
645 return( 0 );
646
647#if defined(MBEDTLS_SSL_RENEGOTIATION)
648 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
649 {
650 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
651 return( 0 );
652 }
653#endif /* MBEDTLS_SSL_RENEGOTIATION */
654
655 /*
656 * Failures are ok: just ignore the ticket and proceed.
657 */
658 if( ( ret = ssl->conf->f_ticket_parse( ssl->conf->p_ticket, &session,
659 buf, len ) ) != 0 )
660 {
661 mbedtls_ssl_session_free( &session );
662
663 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
664 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is not authentic" ) );
665 else if( ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED )
666 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is expired" ) );
667 else
668 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_parse", ret );
669
670 return( 0 );
671 }
672
673 /*
674 * Keep the session ID sent by the client, since we MUST send it back to
675 * inform them we're accepting the ticket (RFC 5077 section 3.4)
676 */
677 session.id_len = ssl->session_negotiate->id_len;
678 memcpy( &session.id, ssl->session_negotiate->id, session.id_len );
679
680 mbedtls_ssl_session_free( ssl->session_negotiate );
681 memcpy( ssl->session_negotiate, &session, sizeof( mbedtls_ssl_session ) );
682
683 /* Zeroize instead of free as we copied the content */
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100684 mbedtls_platform_zeroize( &session, sizeof( mbedtls_ssl_session ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200685
686 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
687
688 ssl->handshake->resume = 1;
689
690 /* Don't send a new ticket after all, this one is OK */
691 ssl->handshake->new_session_ticket = 0;
692
693 return( 0 );
694}
695#endif /* MBEDTLS_SSL_SESSION_TICKETS */
696
697#if defined(MBEDTLS_SSL_ALPN)
698static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
699 const unsigned char *buf, size_t len )
700{
701 size_t list_len, cur_len, ours_len;
702 const unsigned char *theirs, *start, *end;
703 const char **ours;
704
705 /* If ALPN not configured, just ignore the extension */
706 if( ssl->conf->alpn_list == NULL )
707 return( 0 );
708
709 /*
710 * opaque ProtocolName<1..2^8-1>;
711 *
712 * struct {
713 * ProtocolName protocol_name_list<2..2^16-1>
714 * } ProtocolNameList;
715 */
716
717 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
718 if( len < 4 )
719 {
720 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
721 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
722 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
723 }
724
725 list_len = ( buf[0] << 8 ) | buf[1];
726 if( list_len != len - 2 )
727 {
728 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
729 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
730 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
731 }
732
733 /*
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100734 * Validate peer's list (lengths)
Jens Wiklander817466c2018-05-22 13:49:31 +0200735 */
736 start = buf + 2;
737 end = buf + len;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100738 for( theirs = start; theirs != end; theirs += cur_len )
739 {
740 cur_len = *theirs++;
741
742 /* Current identifier must fit in list */
743 if( cur_len > (size_t)( end - theirs ) )
744 {
745 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
746 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
747 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
748 }
749
750 /* Empty strings MUST NOT be included */
751 if( cur_len == 0 )
752 {
753 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
754 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
755 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
756 }
757 }
758
759 /*
760 * Use our order of preference
761 */
Jens Wiklander817466c2018-05-22 13:49:31 +0200762 for( ours = ssl->conf->alpn_list; *ours != NULL; ours++ )
763 {
764 ours_len = strlen( *ours );
765 for( theirs = start; theirs != end; theirs += cur_len )
766 {
Jens Wiklander817466c2018-05-22 13:49:31 +0200767 cur_len = *theirs++;
768
Jens Wiklander817466c2018-05-22 13:49:31 +0200769 if( cur_len == ours_len &&
770 memcmp( theirs, *ours, cur_len ) == 0 )
771 {
772 ssl->alpn_chosen = *ours;
773 return( 0 );
774 }
775 }
776 }
777
778 /* If we get there, no match was found */
779 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
780 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
781 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
782}
783#endif /* MBEDTLS_SSL_ALPN */
784
785/*
786 * Auxiliary functions for ServerHello parsing and related actions
787 */
788
789#if defined(MBEDTLS_X509_CRT_PARSE_C)
790/*
791 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
792 */
793#if defined(MBEDTLS_ECDSA_C)
794static int ssl_check_key_curve( mbedtls_pk_context *pk,
795 const mbedtls_ecp_curve_info **curves )
796{
797 const mbedtls_ecp_curve_info **crv = curves;
798 mbedtls_ecp_group_id grp_id = mbedtls_pk_ec( *pk )->grp.id;
799
800 while( *crv != NULL )
801 {
802 if( (*crv)->grp_id == grp_id )
803 return( 0 );
804 crv++;
805 }
806
807 return( -1 );
808}
809#endif /* MBEDTLS_ECDSA_C */
810
811/*
812 * Try picking a certificate for this ciphersuite,
813 * return 0 on success and -1 on failure.
814 */
815static int ssl_pick_cert( mbedtls_ssl_context *ssl,
816 const mbedtls_ssl_ciphersuite_t * ciphersuite_info )
817{
818 mbedtls_ssl_key_cert *cur, *list, *fallback = NULL;
819 mbedtls_pk_type_t pk_alg =
820 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
821 uint32_t flags;
822
823#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
824 if( ssl->handshake->sni_key_cert != NULL )
825 list = ssl->handshake->sni_key_cert;
826 else
827#endif
828 list = ssl->conf->key_cert;
829
830 if( pk_alg == MBEDTLS_PK_NONE )
831 return( 0 );
832
833 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
834
835 if( list == NULL )
836 {
837 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
838 return( -1 );
839 }
840
841 for( cur = list; cur != NULL; cur = cur->next )
842 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200843 flags = 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200844 MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
845 cur->cert );
846
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100847 if( ! mbedtls_pk_can_do( &cur->cert->pk, pk_alg ) )
Jens Wiklander817466c2018-05-22 13:49:31 +0200848 {
849 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
850 continue;
851 }
852
853 /*
854 * This avoids sending the client a cert it'll reject based on
855 * keyUsage or other extensions.
856 *
857 * It also allows the user to provision different certificates for
858 * different uses based on keyUsage, eg if they want to avoid signing
859 * and decrypting with the same RSA key.
860 */
861 if( mbedtls_ssl_check_cert_usage( cur->cert, ciphersuite_info,
862 MBEDTLS_SSL_IS_SERVER, &flags ) != 0 )
863 {
864 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
865 "(extended) key usage extension" ) );
866 continue;
867 }
868
869#if defined(MBEDTLS_ECDSA_C)
870 if( pk_alg == MBEDTLS_PK_ECDSA &&
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100871 ssl_check_key_curve( &cur->cert->pk, ssl->handshake->curves ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +0200872 {
873 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
874 continue;
875 }
876#endif
877
878 /*
879 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
880 * present them a SHA-higher cert rather than failing if it's the only
881 * one we got that satisfies the other conditions.
882 */
883 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 &&
884 cur->cert->sig_md != MBEDTLS_MD_SHA1 )
885 {
886 if( fallback == NULL )
887 fallback = cur;
888 {
889 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
890 "sha-2 with pre-TLS 1.2 client" ) );
891 continue;
892 }
893 }
894
895 /* If we get there, we got a winner */
896 break;
897 }
898
899 if( cur == NULL )
900 cur = fallback;
901
902 /* Do not update ssl->handshake->key_cert unless there is a match */
903 if( cur != NULL )
904 {
905 ssl->handshake->key_cert = cur;
906 MBEDTLS_SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
907 ssl->handshake->key_cert->cert );
908 return( 0 );
909 }
910
911 return( -1 );
912}
913#endif /* MBEDTLS_X509_CRT_PARSE_C */
914
915/*
916 * Check if a given ciphersuite is suitable for use with our config/keys/etc
917 * Sets ciphersuite_info only if the suite matches.
918 */
919static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id,
920 const mbedtls_ssl_ciphersuite_t **ciphersuite_info )
921{
922 const mbedtls_ssl_ciphersuite_t *suite_info;
923
924#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200925 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +0200926 mbedtls_pk_type_t sig_type;
927#endif
928
929 suite_info = mbedtls_ssl_ciphersuite_from_id( suite_id );
930 if( suite_info == NULL )
931 {
932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
933 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
934 }
935
936 MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %s", suite_info->name ) );
937
938 if( suite_info->min_minor_ver > ssl->minor_ver ||
939 suite_info->max_minor_ver < ssl->minor_ver )
940 {
941 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
942 return( 0 );
943 }
944
945#if defined(MBEDTLS_SSL_PROTO_DTLS)
946 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
947 ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
948 return( 0 );
949#endif
950
951#if defined(MBEDTLS_ARC4_C)
952 if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED &&
953 suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
954 {
955 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) );
956 return( 0 );
957 }
958#endif
959
960#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
961 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
962 ( ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK ) == 0 )
963 {
964 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake "
965 "not configured or ext missing" ) );
966 return( 0 );
967 }
968#endif
969
970
971#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
972 if( mbedtls_ssl_ciphersuite_uses_ec( suite_info ) &&
973 ( ssl->handshake->curves == NULL ||
974 ssl->handshake->curves[0] == NULL ) )
975 {
976 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
977 "no common elliptic curve" ) );
978 return( 0 );
979 }
980#endif
981
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200982#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +0200983 /* If the ciphersuite requires a pre-shared key and we don't
984 * have one, skip it now rather than failing later */
985 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200986 ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +0200987 {
988 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
989 return( 0 );
990 }
991#endif
992
993#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200994 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +0200995 /* If the ciphersuite requires signing, check whether
996 * a suitable hash algorithm is present. */
997 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
998 {
999 sig_type = mbedtls_ssl_get_ciphersuite_sig_alg( suite_info );
1000 if( sig_type != MBEDTLS_PK_NONE &&
1001 mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, sig_type ) == MBEDTLS_MD_NONE )
1002 {
1003 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no suitable hash algorithm "
1004 "for signature algorithm %d", sig_type ) );
1005 return( 0 );
1006 }
1007 }
1008
1009#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001010 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02001011
1012#if defined(MBEDTLS_X509_CRT_PARSE_C)
1013 /*
1014 * Final check: if ciphersuite requires us to have a
1015 * certificate/key of a particular type:
1016 * - select the appropriate certificate if we have one, or
1017 * - try the next ciphersuite if we don't
1018 * This must be done last since we modify the key_cert list.
1019 */
1020 if( ssl_pick_cert( ssl, suite_info ) != 0 )
1021 {
1022 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
1023 "no suitable certificate" ) );
1024 return( 0 );
1025 }
1026#endif
1027
1028 *ciphersuite_info = suite_info;
1029 return( 0 );
1030}
1031
1032#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1033static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl )
1034{
1035 int ret, got_common_suite;
1036 unsigned int i, j;
1037 size_t n;
1038 unsigned int ciph_len, sess_len, chal_len;
1039 unsigned char *buf, *p;
1040 const int *ciphersuites;
1041 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1042
1043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
1044
1045#if defined(MBEDTLS_SSL_RENEGOTIATION)
1046 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
1047 {
1048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
1049 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1050 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1051 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1052 }
1053#endif /* MBEDTLS_SSL_RENEGOTIATION */
1054
1055 buf = ssl->in_hdr;
1056
1057 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1058
1059 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
1060 buf[2] ) );
1061 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
1062 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
1063 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
1064 buf[3], buf[4] ) );
1065
1066 /*
1067 * SSLv2 Client Hello
1068 *
1069 * Record layer:
1070 * 0 . 1 message length
1071 *
1072 * SSL layer:
1073 * 2 . 2 message type
1074 * 3 . 4 protocol version
1075 */
1076 if( buf[2] != MBEDTLS_SSL_HS_CLIENT_HELLO ||
1077 buf[3] != MBEDTLS_SSL_MAJOR_VERSION_3 )
1078 {
1079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1080 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1081 }
1082
1083 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1084
1085 if( n < 17 || n > 512 )
1086 {
1087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1088 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1089 }
1090
1091 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
1092 ssl->minor_ver = ( buf[4] <= ssl->conf->max_minor_ver )
1093 ? buf[4] : ssl->conf->max_minor_ver;
1094
1095 if( ssl->minor_ver < ssl->conf->min_minor_ver )
1096 {
1097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
1098 " [%d:%d] < [%d:%d]",
1099 ssl->major_ver, ssl->minor_ver,
1100 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
1101
1102 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1103 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
1104 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1105 }
1106
1107 ssl->handshake->max_major_ver = buf[3];
1108 ssl->handshake->max_minor_ver = buf[4];
1109
1110 if( ( ret = mbedtls_ssl_fetch_input( ssl, 2 + n ) ) != 0 )
1111 {
1112 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
1113 return( ret );
1114 }
1115
1116 ssl->handshake->update_checksum( ssl, buf + 2, n );
1117
1118 buf = ssl->in_msg;
1119 n = ssl->in_left - 5;
1120
1121 /*
1122 * 0 . 1 ciphersuitelist length
1123 * 2 . 3 session id length
1124 * 4 . 5 challenge length
1125 * 6 . .. ciphersuitelist
1126 * .. . .. session id
1127 * .. . .. challenge
1128 */
1129 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, n );
1130
1131 ciph_len = ( buf[0] << 8 ) | buf[1];
1132 sess_len = ( buf[2] << 8 ) | buf[3];
1133 chal_len = ( buf[4] << 8 ) | buf[5];
1134
1135 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
1136 ciph_len, sess_len, chal_len ) );
1137
1138 /*
1139 * Make sure each parameter length is valid
1140 */
1141 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1142 {
1143 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1144 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1145 }
1146
1147 if( sess_len > 32 )
1148 {
1149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1150 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1151 }
1152
1153 if( chal_len < 8 || chal_len > 32 )
1154 {
1155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1156 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1157 }
1158
1159 if( n != 6 + ciph_len + sess_len + chal_len )
1160 {
1161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1162 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1163 }
1164
1165 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1166 buf + 6, ciph_len );
1167 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
1168 buf + 6 + ciph_len, sess_len );
1169 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, challenge",
1170 buf + 6 + ciph_len + sess_len, chal_len );
1171
1172 p = buf + 6 + ciph_len;
1173 ssl->session_negotiate->id_len = sess_len;
1174 memset( ssl->session_negotiate->id, 0,
1175 sizeof( ssl->session_negotiate->id ) );
1176 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->id_len );
1177
1178 p += sess_len;
1179 memset( ssl->handshake->randbytes, 0, 64 );
1180 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1181
1182 /*
1183 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1184 */
1185 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1186 {
1187 if( p[0] == 0 && p[1] == 0 && p[2] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
1188 {
1189 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1190#if defined(MBEDTLS_SSL_RENEGOTIATION)
1191 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
1192 {
1193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
1194 "during renegotiation" ) );
1195
1196 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1197 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1198 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1199 }
1200#endif /* MBEDTLS_SSL_RENEGOTIATION */
1201 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
1202 break;
1203 }
1204 }
1205
1206#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
1207 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1208 {
1209 if( p[0] == 0 &&
1210 p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
1211 p[2] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
1212 {
1213 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
1214
1215 if( ssl->minor_ver < ssl->conf->max_minor_ver )
1216 {
1217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
1218
1219 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1220 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1221
1222 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1223 }
1224
1225 break;
1226 }
1227 }
1228#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
1229
1230 got_common_suite = 0;
1231 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
1232 ciphersuite_info = NULL;
1233#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1234 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1235 for( i = 0; ciphersuites[i] != 0; i++ )
1236#else
1237 for( i = 0; ciphersuites[i] != 0; i++ )
1238 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1239#endif
1240 {
1241 if( p[0] != 0 ||
1242 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1243 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1244 continue;
1245
1246 got_common_suite = 1;
1247
1248 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1249 &ciphersuite_info ) ) != 0 )
1250 return( ret );
1251
1252 if( ciphersuite_info != NULL )
1253 goto have_ciphersuite_v2;
1254 }
1255
1256 if( got_common_suite )
1257 {
1258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1259 "but none of them usable" ) );
1260 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
1261 }
1262 else
1263 {
1264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1265 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
1266 }
1267
1268have_ciphersuite_v2:
1269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
1270
1271 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001272 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jens Wiklander817466c2018-05-22 13:49:31 +02001273
1274 /*
1275 * SSLv2 Client Hello relevant renegotiation security checks
1276 */
1277 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1278 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
1279 {
1280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1281 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1282 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
1283 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1284 }
1285
1286 ssl->in_left = 0;
1287 ssl->state++;
1288
1289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1290
1291 return( 0 );
1292}
1293#endif /* MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1294
1295/* This function doesn't alert on errors that happen early during
1296 ClientHello parsing because they might indicate that the client is
1297 not talking SSL/TLS at all and would not understand our alert. */
1298static int ssl_parse_client_hello( mbedtls_ssl_context *ssl )
1299{
1300 int ret, got_common_suite;
1301 size_t i, j;
1302 size_t ciph_offset, comp_offset, ext_offset;
1303 size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
1304#if defined(MBEDTLS_SSL_PROTO_DTLS)
1305 size_t cookie_offset, cookie_len;
1306#endif
1307 unsigned char *buf, *p, *ext;
1308#if defined(MBEDTLS_SSL_RENEGOTIATION)
1309 int renegotiation_info_seen = 0;
1310#endif
1311 int handshake_failure = 0;
1312 const int *ciphersuites;
1313 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1314 int major, minor;
1315
1316 /* If there is no signature-algorithm extension present,
1317 * we need to fall back to the default values for allowed
1318 * signature-hash pairs. */
1319#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001320 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02001321 int sig_hash_alg_ext_present = 0;
1322#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001323 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02001324
1325 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1326
1327#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1328read_record_header:
1329#endif
1330 /*
1331 * If renegotiating, then the input was read with mbedtls_ssl_read_record(),
1332 * otherwise read it ourselves manually in order to support SSLv2
1333 * ClientHello, which doesn't use the same record layer format.
1334 */
1335#if defined(MBEDTLS_SSL_RENEGOTIATION)
1336 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
1337#endif
1338 {
1339 if( ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 )
1340 {
1341 /* No alert on a read error. */
1342 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
1343 return( ret );
1344 }
1345 }
1346
1347 buf = ssl->in_hdr;
1348
1349#if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1350#if defined(MBEDTLS_SSL_PROTO_DTLS)
1351 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
1352#endif
1353 if( ( buf[0] & 0x80 ) != 0 )
1354 return( ssl_parse_client_hello_v2( ssl ) );
1355#endif
1356
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001357 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, mbedtls_ssl_in_hdr_len( ssl ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02001358
1359 /*
1360 * SSLv3/TLS Client Hello
1361 *
1362 * Record layer:
1363 * 0 . 0 message type
1364 * 1 . 2 protocol version
1365 * 3 . 11 DTLS: epoch + record sequence number
1366 * 3 . 4 message length
1367 */
1368 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1369 buf[0] ) );
1370
1371 if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE )
1372 {
1373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1374 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1375 }
1376
1377 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1378 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1379
1380 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
1381 buf[1], buf[2] ) );
1382
1383 mbedtls_ssl_read_version( &major, &minor, ssl->conf->transport, buf + 1 );
1384
1385 /* According to RFC 5246 Appendix E.1, the version here is typically
1386 * "{03,00}, the lowest version number supported by the client, [or] the
1387 * value of ClientHello.client_version", so the only meaningful check here
1388 * is the major version shouldn't be less than 3 */
1389 if( major < MBEDTLS_SSL_MAJOR_VERSION_3 )
1390 {
1391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1392 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1393 }
1394
1395 /* For DTLS if this is the initial handshake, remember the client sequence
1396 * number to use it in our next message (RFC 6347 4.2.1) */
1397#if defined(MBEDTLS_SSL_PROTO_DTLS)
1398 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM
1399#if defined(MBEDTLS_SSL_RENEGOTIATION)
1400 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
1401#endif
1402 )
1403 {
1404 /* Epoch should be 0 for initial handshakes */
1405 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1406 {
1407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1408 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1409 }
1410
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001411 memcpy( ssl->cur_out_ctr + 2, ssl->in_ctr + 2, 6 );
Jens Wiklander817466c2018-05-22 13:49:31 +02001412
1413#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1414 if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
1415 {
1416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
1417 ssl->next_record_offset = 0;
1418 ssl->in_left = 0;
1419 goto read_record_header;
1420 }
1421
1422 /* No MAC to check yet, so we can update right now */
1423 mbedtls_ssl_dtls_replay_update( ssl );
1424#endif
1425 }
1426#endif /* MBEDTLS_SSL_PROTO_DTLS */
1427
1428 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
1429
1430#if defined(MBEDTLS_SSL_RENEGOTIATION)
1431 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
1432 {
1433 /* Set by mbedtls_ssl_read_record() */
1434 msg_len = ssl->in_hslen;
1435 }
1436 else
1437#endif
1438 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001439 if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN )
Jens Wiklander817466c2018-05-22 13:49:31 +02001440 {
1441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1442 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1443 }
1444
1445 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001446 mbedtls_ssl_in_hdr_len( ssl ) + msg_len ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02001447 {
1448 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
1449 return( ret );
1450 }
1451
1452 /* Done reading this record, get ready for the next one */
1453#if defined(MBEDTLS_SSL_PROTO_DTLS)
1454 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001455 ssl->next_record_offset = msg_len + mbedtls_ssl_in_hdr_len( ssl );
Jens Wiklander817466c2018-05-22 13:49:31 +02001456 else
1457#endif
1458 ssl->in_left = 0;
1459 }
1460
1461 buf = ssl->in_msg;
1462
1463 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
1464
1465 ssl->handshake->update_checksum( ssl, buf, msg_len );
1466
1467 /*
1468 * Handshake layer:
1469 * 0 . 0 handshake type
1470 * 1 . 3 handshake length
1471 * 4 . 5 DTLS only: message seqence number
1472 * 6 . 8 DTLS only: fragment offset
1473 * 9 . 11 DTLS only: fragment length
1474 */
1475 if( msg_len < mbedtls_ssl_hs_hdr_len( ssl ) )
1476 {
1477 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1478 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1479 }
1480
1481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
1482
1483 if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
1484 {
1485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1486 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1487 }
1488
1489 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1490 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1491
1492 /* We don't support fragmentation of ClientHello (yet?) */
1493 if( buf[1] != 0 ||
1494 msg_len != mbedtls_ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
1495 {
1496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1497 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1498 }
1499
1500#if defined(MBEDTLS_SSL_PROTO_DTLS)
1501 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1502 {
1503 /*
1504 * Copy the client's handshake message_seq on initial handshakes,
1505 * check sequence number on renego.
1506 */
1507#if defined(MBEDTLS_SSL_RENEGOTIATION)
1508 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
1509 {
1510 /* This couldn't be done in ssl_prepare_handshake_record() */
1511 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1512 ssl->in_msg[5];
1513
1514 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1515 {
1516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
1517 "%d (expected %d)", cli_msg_seq,
1518 ssl->handshake->in_msg_seq ) );
1519 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1520 }
1521
1522 ssl->handshake->in_msg_seq++;
1523 }
1524 else
1525#endif
1526 {
1527 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1528 ssl->in_msg[5];
1529 ssl->handshake->out_msg_seq = cli_msg_seq;
1530 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
1531 }
1532
1533 /*
1534 * For now we don't support fragmentation, so make sure
1535 * fragment_offset == 0 and fragment_length == length
1536 */
1537 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1538 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1539 {
1540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
1541 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1542 }
1543 }
1544#endif /* MBEDTLS_SSL_PROTO_DTLS */
1545
1546 buf += mbedtls_ssl_hs_hdr_len( ssl );
1547 msg_len -= mbedtls_ssl_hs_hdr_len( ssl );
1548
1549 /*
1550 * ClientHello layer:
1551 * 0 . 1 protocol version
1552 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1553 * 34 . 35 session id length (1 byte)
1554 * 35 . 34+x session id
1555 * 35+x . 35+x DTLS only: cookie length (1 byte)
1556 * 36+x . .. DTLS only: cookie
1557 * .. . .. ciphersuite list length (2 bytes)
1558 * .. . .. ciphersuite list
1559 * .. . .. compression alg. list length (1 byte)
1560 * .. . .. compression alg. list
1561 * .. . .. extensions length (2 bytes, optional)
1562 * .. . .. extensions (optional)
1563 */
1564
1565 /*
Jerome Forissier5b25c762020-04-07 11:18:49 +02001566 * Minimal length (with everything empty and extensions omitted) is
Jens Wiklander817466c2018-05-22 13:49:31 +02001567 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1568 * read at least up to session id length without worrying.
1569 */
1570 if( msg_len < 38 )
1571 {
1572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1573 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1574 }
1575
1576 /*
1577 * Check and save the protocol version
1578 */
1579 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
1580
1581 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
1582 ssl->conf->transport, buf );
1583
1584 ssl->handshake->max_major_ver = ssl->major_ver;
1585 ssl->handshake->max_minor_ver = ssl->minor_ver;
1586
1587 if( ssl->major_ver < ssl->conf->min_major_ver ||
1588 ssl->minor_ver < ssl->conf->min_minor_ver )
1589 {
1590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
1591 " [%d:%d] < [%d:%d]",
1592 ssl->major_ver, ssl->minor_ver,
1593 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
1594 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1595 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
1596 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1597 }
1598
1599 if( ssl->major_ver > ssl->conf->max_major_ver )
1600 {
1601 ssl->major_ver = ssl->conf->max_major_ver;
1602 ssl->minor_ver = ssl->conf->max_minor_ver;
1603 }
1604 else if( ssl->minor_ver > ssl->conf->max_minor_ver )
1605 ssl->minor_ver = ssl->conf->max_minor_ver;
1606
1607 /*
1608 * Save client random (inc. Unix time)
1609 */
1610 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
1611
1612 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
1613
1614 /*
1615 * Check the session ID length and save session ID
1616 */
1617 sess_len = buf[34];
1618
1619 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
1620 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
1621 {
1622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1623 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1624 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1625 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1626 }
1627
1628 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
1629
1630 ssl->session_negotiate->id_len = sess_len;
1631 memset( ssl->session_negotiate->id, 0,
1632 sizeof( ssl->session_negotiate->id ) );
1633 memcpy( ssl->session_negotiate->id, buf + 35,
1634 ssl->session_negotiate->id_len );
1635
1636 /*
1637 * Check the cookie length and content
1638 */
1639#if defined(MBEDTLS_SSL_PROTO_DTLS)
1640 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1641 {
1642 cookie_offset = 35 + sess_len;
1643 cookie_len = buf[cookie_offset];
1644
1645 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
1646 {
1647 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1648 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1649 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
1650 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1651 }
1652
1653 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
1654 buf + cookie_offset + 1, cookie_len );
1655
1656#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
1657 if( ssl->conf->f_cookie_check != NULL
1658#if defined(MBEDTLS_SSL_RENEGOTIATION)
1659 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
1660#endif
1661 )
1662 {
1663 if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
1664 buf + cookie_offset + 1, cookie_len,
1665 ssl->cli_id, ssl->cli_id_len ) != 0 )
1666 {
1667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
1668 ssl->handshake->verify_cookie_len = 1;
1669 }
1670 else
1671 {
1672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
1673 ssl->handshake->verify_cookie_len = 0;
1674 }
1675 }
1676 else
1677#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
1678 {
1679 /* We know we didn't send a cookie, so it should be empty */
1680 if( cookie_len != 0 )
1681 {
1682 /* This may be an attacker's probe, so don't send an alert */
1683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1684 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1685 }
1686
1687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
1688 }
1689
1690 /*
1691 * Check the ciphersuitelist length (will be parsed later)
1692 */
1693 ciph_offset = cookie_offset + 1 + cookie_len;
1694 }
1695 else
1696#endif /* MBEDTLS_SSL_PROTO_DTLS */
1697 ciph_offset = 35 + sess_len;
1698
1699 ciph_len = ( buf[ciph_offset + 0] << 8 )
1700 | ( buf[ciph_offset + 1] );
1701
1702 if( ciph_len < 2 ||
1703 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1704 ( ciph_len % 2 ) != 0 )
1705 {
1706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1707 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1708 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1709 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1710 }
1711
1712 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1713 buf + ciph_offset + 2, ciph_len );
1714
1715 /*
1716 * Check the compression algorithms length and pick one
1717 */
1718 comp_offset = ciph_offset + 2 + ciph_len;
1719
1720 comp_len = buf[comp_offset];
1721
1722 if( comp_len < 1 ||
1723 comp_len > 16 ||
1724 comp_len + comp_offset + 1 > msg_len )
1725 {
1726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1727 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1728 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1729 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1730 }
1731
1732 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression",
1733 buf + comp_offset + 1, comp_len );
1734
1735 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
1736#if defined(MBEDTLS_ZLIB_SUPPORT)
1737 for( i = 0; i < comp_len; ++i )
1738 {
1739 if( buf[comp_offset + 1 + i] == MBEDTLS_SSL_COMPRESS_DEFLATE )
1740 {
1741 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_DEFLATE;
1742 break;
1743 }
1744 }
1745#endif
1746
1747 /* See comments in ssl_write_client_hello() */
1748#if defined(MBEDTLS_SSL_PROTO_DTLS)
1749 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1750 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
1751#endif
1752
1753 /* Do not parse the extensions if the protocol is SSLv3 */
1754#if defined(MBEDTLS_SSL_PROTO_SSL3)
1755 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
1756 {
1757#endif
1758 /*
1759 * Check the extension length
1760 */
1761 ext_offset = comp_offset + 1 + comp_len;
1762 if( msg_len > ext_offset )
1763 {
1764 if( msg_len < ext_offset + 2 )
1765 {
1766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1767 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1768 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1769 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1770 }
1771
1772 ext_len = ( buf[ext_offset + 0] << 8 )
1773 | ( buf[ext_offset + 1] );
1774
1775 if( ( ext_len > 0 && ext_len < 4 ) ||
1776 msg_len != ext_offset + 2 + ext_len )
1777 {
1778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1779 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1780 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1781 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1782 }
1783 }
1784 else
1785 ext_len = 0;
1786
1787 ext = buf + ext_offset + 2;
1788 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len );
1789
1790 while( ext_len != 0 )
1791 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001792 unsigned int ext_id;
1793 unsigned int ext_size;
1794 if ( ext_len < 4 ) {
1795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1796 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1797 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1798 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1799 }
1800 ext_id = ( ( ext[0] << 8 ) | ( ext[1] ) );
1801 ext_size = ( ( ext[2] << 8 ) | ( ext[3] ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02001802
1803 if( ext_size + 4 > ext_len )
1804 {
1805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1806 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1807 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1808 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1809 }
1810 switch( ext_id )
1811 {
1812#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1813 case MBEDTLS_TLS_EXT_SERVERNAME:
1814 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1815 if( ssl->conf->f_sni == NULL )
1816 break;
1817
1818 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1819 if( ret != 0 )
1820 return( ret );
1821 break;
1822#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1823
1824 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1825 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1826#if defined(MBEDTLS_SSL_RENEGOTIATION)
1827 renegotiation_info_seen = 1;
1828#endif
1829
1830 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1831 if( ret != 0 )
1832 return( ret );
1833 break;
1834
1835#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001836 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02001837 case MBEDTLS_TLS_EXT_SIG_ALG:
1838 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1839
1840 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1841 if( ret != 0 )
1842 return( ret );
1843
1844 sig_hash_alg_ext_present = 1;
1845 break;
1846#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001847 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02001848
1849#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
1850 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1851 case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1852 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1853
1854 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1855 if( ret != 0 )
1856 return( ret );
1857 break;
1858
1859 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1860 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1861 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
1862
1863 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1864 if( ret != 0 )
1865 return( ret );
1866 break;
1867#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1868 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1869
1870#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1871 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1872 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake kkpp extension" ) );
1873
1874 ret = ssl_parse_ecjpake_kkpp( ssl, ext + 4, ext_size );
1875 if( ret != 0 )
1876 return( ret );
1877 break;
1878#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1879
1880#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1881 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1882 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1883
1884 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1885 if( ret != 0 )
1886 return( ret );
1887 break;
1888#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
1889
1890#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1891 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
1892 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1893
1894 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1895 if( ret != 0 )
1896 return( ret );
1897 break;
1898#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1899
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001900#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1901 case MBEDTLS_TLS_EXT_CID:
1902 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
1903
1904 ret = ssl_parse_cid_ext( ssl, ext + 4, ext_size );
1905 if( ret != 0 )
1906 return( ret );
1907 break;
1908#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1909
Jens Wiklander817466c2018-05-22 13:49:31 +02001910#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1911 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1912 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
1913
1914 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1915 if( ret != 0 )
1916 return( ret );
1917 break;
1918#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
1919
1920#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1921 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1922 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
1923
1924 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1925 if( ret != 0 )
1926 return( ret );
1927 break;
1928#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1929
1930#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1931 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1932 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1933
1934 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1935 if( ret != 0 )
1936 return( ret );
1937 break;
1938#endif /* MBEDTLS_SSL_SESSION_TICKETS */
1939
1940#if defined(MBEDTLS_SSL_ALPN)
1941 case MBEDTLS_TLS_EXT_ALPN:
1942 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1943
1944 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1945 if( ret != 0 )
1946 return( ret );
1947 break;
1948#endif /* MBEDTLS_SSL_SESSION_TICKETS */
1949
1950 default:
1951 MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1952 ext_id ) );
1953 }
1954
1955 ext_len -= 4 + ext_size;
1956 ext += 4 + ext_size;
1957
1958 if( ext_len > 0 && ext_len < 4 )
1959 {
1960 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1961 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1962 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1963 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1964 }
1965 }
1966#if defined(MBEDTLS_SSL_PROTO_SSL3)
1967 }
1968#endif
1969
1970#if defined(MBEDTLS_SSL_FALLBACK_SCSV)
1971 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
1972 {
1973 if( p[0] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
1974 p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
1975 {
1976 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received FALLBACK_SCSV" ) );
1977
1978 if( ssl->minor_ver < ssl->conf->max_minor_ver )
1979 {
1980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
1981
1982 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1983 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1984
1985 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1986 }
1987
1988 break;
1989 }
1990 }
1991#endif /* MBEDTLS_SSL_FALLBACK_SCSV */
1992
1993#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001994 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02001995
1996 /*
1997 * Try to fall back to default hash SHA1 if the client
1998 * hasn't provided any preferred signature-hash combinations.
1999 */
2000 if( sig_hash_alg_ext_present == 0 )
2001 {
2002 mbedtls_md_type_t md_default = MBEDTLS_MD_SHA1;
2003
2004 if( mbedtls_ssl_check_sig_hash( ssl, md_default ) != 0 )
2005 md_default = MBEDTLS_MD_NONE;
2006
2007 mbedtls_ssl_sig_hash_set_const_hash( &ssl->handshake->hash_algs, md_default );
2008 }
2009
2010#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002011 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02002012
2013 /*
2014 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
2015 */
2016 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
2017 {
2018 if( p[0] == 0 && p[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
2019 {
2020 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
2021#if defined(MBEDTLS_SSL_RENEGOTIATION)
2022 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
2023 {
2024 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
2025 "during renegotiation" ) );
2026 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2027 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2028 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
2029 }
2030#endif
2031 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
2032 break;
2033 }
2034 }
2035
2036 /*
2037 * Renegotiation security checks
2038 */
2039 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION &&
2040 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
2041 {
2042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
2043 handshake_failure = 1;
2044 }
2045#if defined(MBEDTLS_SSL_RENEGOTIATION)
2046 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2047 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
2048 renegotiation_info_seen == 0 )
2049 {
2050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
2051 handshake_failure = 1;
2052 }
2053 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2054 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
2055 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
2056 {
2057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
2058 handshake_failure = 1;
2059 }
2060 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
2061 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
2062 renegotiation_info_seen == 1 )
2063 {
2064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
2065 handshake_failure = 1;
2066 }
2067#endif /* MBEDTLS_SSL_RENEGOTIATION */
2068
2069 if( handshake_failure == 1 )
2070 {
2071 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2072 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2073 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
2074 }
2075
2076 /*
2077 * Search for a matching ciphersuite
2078 * (At the end because we need information from the EC-based extensions
2079 * and certificate from the SNI callback triggered by the SNI extension.)
2080 */
2081 got_common_suite = 0;
2082 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
2083 ciphersuite_info = NULL;
2084#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
2085 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
2086 for( i = 0; ciphersuites[i] != 0; i++ )
2087#else
2088 for( i = 0; ciphersuites[i] != 0; i++ )
2089 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
2090#endif
2091 {
2092 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
2093 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
2094 continue;
2095
2096 got_common_suite = 1;
2097
2098 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
2099 &ciphersuite_info ) ) != 0 )
2100 return( ret );
2101
2102 if( ciphersuite_info != NULL )
2103 goto have_ciphersuite;
2104 }
2105
2106 if( got_common_suite )
2107 {
2108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
2109 "but none of them usable" ) );
2110 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2111 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2112 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
2113 }
2114 else
2115 {
2116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
2117 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2118 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
2119 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
2120 }
2121
2122have_ciphersuite:
2123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
2124
2125 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002126 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jens Wiklander817466c2018-05-22 13:49:31 +02002127
2128 ssl->state++;
2129
2130#if defined(MBEDTLS_SSL_PROTO_DTLS)
2131 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2132 mbedtls_ssl_recv_flight_completed( ssl );
2133#endif
2134
2135 /* Debugging-only output for testsuite */
2136#if defined(MBEDTLS_DEBUG_C) && \
2137 defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002138 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02002139 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
2140 {
2141 mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( ciphersuite_info );
2142 if( sig_alg != MBEDTLS_PK_NONE )
2143 {
2144 mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
2145 sig_alg );
2146 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
2147 mbedtls_ssl_hash_from_md_alg( md_alg ) ) );
2148 }
2149 else
2150 {
2151 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no hash algorithm for signature algorithm "
2152 "%d - should not happen", sig_alg ) );
2153 }
2154 }
2155#endif
2156
2157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
2158
2159 return( 0 );
2160}
2161
2162#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
2163static void ssl_write_truncated_hmac_ext( mbedtls_ssl_context *ssl,
2164 unsigned char *buf,
2165 size_t *olen )
2166{
2167 unsigned char *p = buf;
2168
2169 if( ssl->session_negotiate->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED )
2170 {
2171 *olen = 0;
2172 return;
2173 }
2174
2175 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
2176
2177 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
2178 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
2179
2180 *p++ = 0x00;
2181 *p++ = 0x00;
2182
2183 *olen = 4;
2184}
2185#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
2186
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002187#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
2188static void ssl_write_cid_ext( mbedtls_ssl_context *ssl,
2189 unsigned char *buf,
2190 size_t *olen )
2191{
2192 unsigned char *p = buf;
2193 size_t ext_len;
2194 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2195
2196 *olen = 0;
2197
2198 /* Skip writing the extension if we don't want to use it or if
2199 * the client hasn't offered it. */
2200 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED )
2201 return;
2202
2203 /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
2204 * which is at most 255, so the increment cannot overflow. */
2205 if( end < p || (size_t)( end - p ) < (unsigned)( ssl->own_cid_len + 5 ) )
2206 {
2207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2208 return;
2209 }
2210
2211 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding CID extension" ) );
2212
2213 /*
2214 * Quoting draft-ietf-tls-dtls-connection-id-05
2215 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
2216 *
2217 * struct {
2218 * opaque cid<0..2^8-1>;
2219 * } ConnectionId;
2220 */
2221
2222 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID >> 8 ) & 0xFF );
2223 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID ) & 0xFF );
2224 ext_len = (size_t) ssl->own_cid_len + 1;
2225 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2226 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2227
2228 *p++ = (uint8_t) ssl->own_cid_len;
2229 memcpy( p, ssl->own_cid, ssl->own_cid_len );
2230
2231 *olen = ssl->own_cid_len + 5;
2232}
2233#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
2234
Jens Wiklander817466c2018-05-22 13:49:31 +02002235#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2236static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
2237 unsigned char *buf,
2238 size_t *olen )
2239{
2240 unsigned char *p = buf;
2241 const mbedtls_ssl_ciphersuite_t *suite = NULL;
2242 const mbedtls_cipher_info_t *cipher = NULL;
2243
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002244 if( ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
Jens Wiklander817466c2018-05-22 13:49:31 +02002245 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
2246 {
2247 *olen = 0;
2248 return;
2249 }
2250
2251 /*
2252 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2253 * from a client and then selects a stream or Authenticated Encryption
2254 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2255 * encrypt-then-MAC response extension back to the client."
2256 */
2257 if( ( suite = mbedtls_ssl_ciphersuite_from_id(
2258 ssl->session_negotiate->ciphersuite ) ) == NULL ||
2259 ( cipher = mbedtls_cipher_info_from_type( suite->cipher ) ) == NULL ||
2260 cipher->mode != MBEDTLS_MODE_CBC )
2261 {
2262 *olen = 0;
2263 return;
2264 }
2265
2266 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
2267
2268 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
2269 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
2270
2271 *p++ = 0x00;
2272 *p++ = 0x00;
2273
2274 *olen = 4;
2275}
2276#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2277
2278#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2279static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
2280 unsigned char *buf,
2281 size_t *olen )
2282{
2283 unsigned char *p = buf;
2284
2285 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
2286 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
2287 {
2288 *olen = 0;
2289 return;
2290 }
2291
2292 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
2293 "extension" ) );
2294
2295 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2296 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
2297
2298 *p++ = 0x00;
2299 *p++ = 0x00;
2300
2301 *olen = 4;
2302}
2303#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
2304
2305#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2306static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
2307 unsigned char *buf,
2308 size_t *olen )
2309{
2310 unsigned char *p = buf;
2311
2312 if( ssl->handshake->new_session_ticket == 0 )
2313 {
2314 *olen = 0;
2315 return;
2316 }
2317
2318 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
2319
2320 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2321 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET ) & 0xFF );
2322
2323 *p++ = 0x00;
2324 *p++ = 0x00;
2325
2326 *olen = 4;
2327}
2328#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2329
2330static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
2331 unsigned char *buf,
2332 size_t *olen )
2333{
2334 unsigned char *p = buf;
2335
2336 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION )
2337 {
2338 *olen = 0;
2339 return;
2340 }
2341
2342 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
2343
2344 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2345 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
2346
2347#if defined(MBEDTLS_SSL_RENEGOTIATION)
2348 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
2349 {
2350 *p++ = 0x00;
2351 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2352 *p++ = ssl->verify_data_len * 2 & 0xFF;
2353
2354 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2355 p += ssl->verify_data_len;
2356 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2357 p += ssl->verify_data_len;
2358 }
2359 else
2360#endif /* MBEDTLS_SSL_RENEGOTIATION */
2361 {
2362 *p++ = 0x00;
2363 *p++ = 0x01;
2364 *p++ = 0x00;
2365 }
2366
2367 *olen = p - buf;
2368}
2369
2370#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2371static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
2372 unsigned char *buf,
2373 size_t *olen )
2374{
2375 unsigned char *p = buf;
2376
2377 if( ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
2378 {
2379 *olen = 0;
2380 return;
2381 }
2382
2383 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
2384
2385 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2386 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
2387
2388 *p++ = 0x00;
2389 *p++ = 1;
2390
2391 *p++ = ssl->session_negotiate->mfl_code;
2392
2393 *olen = 5;
2394}
2395#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2396
2397#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
2398 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2399static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
2400 unsigned char *buf,
2401 size_t *olen )
2402{
2403 unsigned char *p = buf;
2404 ((void) ssl);
2405
2406 if( ( ssl->handshake->cli_exts &
2407 MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
2408 {
2409 *olen = 0;
2410 return;
2411 }
2412
2413 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
2414
2415 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2416 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
2417
2418 *p++ = 0x00;
2419 *p++ = 2;
2420
2421 *p++ = 1;
2422 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
2423
2424 *olen = 6;
2425}
2426#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2427
2428#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2429static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
2430 unsigned char *buf,
2431 size_t *olen )
2432{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002433 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02002434 unsigned char *p = buf;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002435 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Jens Wiklander817466c2018-05-22 13:49:31 +02002436 size_t kkpp_len;
2437
2438 *olen = 0;
2439
2440 /* Skip costly computation if not needed */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002441 if( ssl->handshake->ciphersuite_info->key_exchange !=
Jens Wiklander817466c2018-05-22 13:49:31 +02002442 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2443 return;
2444
2445 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) );
2446
2447 if( end - p < 4 )
2448 {
2449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2450 return;
2451 }
2452
2453 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP >> 8 ) & 0xFF );
2454 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ) & 0xFF );
2455
2456 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
2457 p + 2, end - p - 2, &kkpp_len,
2458 ssl->conf->f_rng, ssl->conf->p_rng );
2459 if( ret != 0 )
2460 {
2461 MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret );
2462 return;
2463 }
2464
2465 *p++ = (unsigned char)( ( kkpp_len >> 8 ) & 0xFF );
2466 *p++ = (unsigned char)( ( kkpp_len ) & 0xFF );
2467
2468 *olen = kkpp_len + 4;
2469}
2470#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2471
2472#if defined(MBEDTLS_SSL_ALPN )
2473static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
2474 unsigned char *buf, size_t *olen )
2475{
2476 if( ssl->alpn_chosen == NULL )
2477 {
2478 *olen = 0;
2479 return;
2480 }
2481
2482 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
2483
2484 /*
2485 * 0 . 1 ext identifier
2486 * 2 . 3 ext length
2487 * 4 . 5 protocol list length
2488 * 6 . 6 protocol name length
2489 * 7 . 7+n protocol name
2490 */
2491 buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN >> 8 ) & 0xFF );
2492 buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN ) & 0xFF );
2493
2494 *olen = 7 + strlen( ssl->alpn_chosen );
2495
2496 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2497 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2498
2499 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2500 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2501
2502 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2503
2504 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2505}
2506#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
2507
2508#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2509static int ssl_write_hello_verify_request( mbedtls_ssl_context *ssl )
2510{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002511 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02002512 unsigned char *p = ssl->out_msg + 4;
2513 unsigned char *cookie_len_byte;
2514
2515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
2516
2517 /*
2518 * struct {
2519 * ProtocolVersion server_version;
2520 * opaque cookie<0..2^8-1>;
2521 * } HelloVerifyRequest;
2522 */
2523
2524 /* The RFC is not clear on this point, but sending the actual negotiated
2525 * version looks like the most interoperable thing to do. */
2526 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
2527 ssl->conf->transport, p );
2528 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
2529 p += 2;
2530
2531 /* If we get here, f_cookie_check is not null */
2532 if( ssl->conf->f_cookie_write == NULL )
2533 {
2534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2535 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2536 }
2537
2538 /* Skip length byte until we know the length */
2539 cookie_len_byte = p++;
2540
2541 if( ( ret = ssl->conf->f_cookie_write( ssl->conf->p_cookie,
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002542 &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN,
Jens Wiklander817466c2018-05-22 13:49:31 +02002543 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
2544 {
2545 MBEDTLS_SSL_DEBUG_RET( 1, "f_cookie_write", ret );
2546 return( ret );
2547 }
2548
2549 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2550
2551 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
2552
2553 ssl->out_msglen = p - ssl->out_msg;
2554 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2555 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
2556
2557 ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
2558
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002559 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02002560 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002561 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02002562 return( ret );
2563 }
2564
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002565#if defined(MBEDTLS_SSL_PROTO_DTLS)
2566 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2567 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
2568 {
2569 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
2570 return( ret );
2571 }
2572#endif /* MBEDTLS_SSL_PROTO_DTLS */
2573
Jens Wiklander817466c2018-05-22 13:49:31 +02002574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
2575
2576 return( 0 );
2577}
2578#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
2579
2580static int ssl_write_server_hello( mbedtls_ssl_context *ssl )
2581{
2582#if defined(MBEDTLS_HAVE_TIME)
2583 mbedtls_time_t t;
2584#endif
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002585 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02002586 size_t olen, ext_len = 0, n;
2587 unsigned char *buf, *p;
2588
2589 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2590
2591#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2592 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2593 ssl->handshake->verify_cookie_len != 0 )
2594 {
2595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2597
2598 return( ssl_write_hello_verify_request( ssl ) );
2599 }
2600#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
2601
2602 if( ssl->conf->f_rng == NULL )
2603 {
2604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2605 return( MBEDTLS_ERR_SSL_NO_RNG );
2606 }
2607
2608 /*
2609 * 0 . 0 handshake type
2610 * 1 . 3 handshake length
2611 * 4 . 5 protocol version
2612 * 6 . 9 UNIX time()
2613 * 10 . 37 random bytes
2614 */
2615 buf = ssl->out_msg;
2616 p = buf + 4;
2617
2618 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
2619 ssl->conf->transport, p );
2620 p += 2;
2621
2622 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
2623 buf[4], buf[5] ) );
2624
2625#if defined(MBEDTLS_HAVE_TIME)
2626 t = mbedtls_time( NULL );
2627 *p++ = (unsigned char)( t >> 24 );
2628 *p++ = (unsigned char)( t >> 16 );
2629 *p++ = (unsigned char)( t >> 8 );
2630 *p++ = (unsigned char)( t );
2631
2632 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
2633#else
2634 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
2635 return( ret );
2636
2637 p += 4;
2638#endif /* MBEDTLS_HAVE_TIME */
2639
2640 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
2641 return( ret );
2642
2643 p += 28;
2644
2645 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
2646
2647 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2648
2649 /*
2650 * Resume is 0 by default, see ssl_handshake_init().
2651 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2652 * If not, try looking up session ID in our cache.
2653 */
2654 if( ssl->handshake->resume == 0 &&
2655#if defined(MBEDTLS_SSL_RENEGOTIATION)
2656 ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE &&
2657#endif
2658 ssl->session_negotiate->id_len != 0 &&
2659 ssl->conf->f_get_cache != NULL &&
2660 ssl->conf->f_get_cache( ssl->conf->p_cache, ssl->session_negotiate ) == 0 )
2661 {
2662 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
2663 ssl->handshake->resume = 1;
2664 }
2665
2666 if( ssl->handshake->resume == 0 )
2667 {
2668 /*
2669 * New session, create a new session id,
2670 * unless we're about to issue a session ticket
2671 */
2672 ssl->state++;
2673
2674#if defined(MBEDTLS_HAVE_TIME)
2675 ssl->session_negotiate->start = mbedtls_time( NULL );
2676#endif
2677
2678#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2679 if( ssl->handshake->new_session_ticket != 0 )
2680 {
2681 ssl->session_negotiate->id_len = n = 0;
2682 memset( ssl->session_negotiate->id, 0, 32 );
2683 }
2684 else
2685#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2686 {
2687 ssl->session_negotiate->id_len = n = 32;
2688 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->session_negotiate->id,
2689 n ) ) != 0 )
2690 return( ret );
2691 }
2692 }
2693 else
2694 {
2695 /*
2696 * Resuming a session
2697 */
2698 n = ssl->session_negotiate->id_len;
2699 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
2700
2701 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
2702 {
2703 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
2704 return( ret );
2705 }
2706 }
2707
2708 /*
2709 * 38 . 38 session id length
2710 * 39 . 38+n session id
2711 * 39+n . 40+n chosen ciphersuite
2712 * 41+n . 41+n chosen compression alg.
2713 * 42+n . 43+n extensions length
2714 * 44+n . 43+n+m extensions
2715 */
2716 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2717 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
2718 p += ssl->session_negotiate->id_len;
2719
2720 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2721 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2722 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
2723 ssl->handshake->resume ? "a" : "no" ) );
2724
2725 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2726 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2727 *p++ = (unsigned char)( ssl->session_negotiate->compression );
2728
2729 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2730 mbedtls_ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
2731 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
2732 ssl->session_negotiate->compression ) );
2733
2734 /* Do not write the extensions if the protocol is SSLv3 */
2735#if defined(MBEDTLS_SSL_PROTO_SSL3)
2736 if( ( ssl->major_ver != 3 ) || ( ssl->minor_ver != 0 ) )
2737 {
2738#endif
2739
2740 /*
2741 * First write extensions, then the total length
2742 */
2743 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2744 ext_len += olen;
2745
2746#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2747 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2748 ext_len += olen;
2749#endif
2750
2751#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
2752 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2753 ext_len += olen;
2754#endif
2755
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002756#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
2757 ssl_write_cid_ext( ssl, p + 2 + ext_len, &olen );
2758 ext_len += olen;
2759#endif
2760
Jens Wiklander817466c2018-05-22 13:49:31 +02002761#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2762 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2763 ext_len += olen;
2764#endif
2765
2766#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2767 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2768 ext_len += olen;
2769#endif
2770
2771#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2772 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2773 ext_len += olen;
2774#endif
2775
2776#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
2777 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002778 if ( mbedtls_ssl_ciphersuite_uses_ec(
2779 mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite ) ) )
2780 {
2781 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2782 ext_len += olen;
2783 }
Jens Wiklander817466c2018-05-22 13:49:31 +02002784#endif
2785
2786#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2787 ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen );
2788 ext_len += olen;
2789#endif
2790
2791#if defined(MBEDTLS_SSL_ALPN)
2792 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2793 ext_len += olen;
2794#endif
2795
2796 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
2797
2798 if( ext_len > 0 )
2799 {
2800 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2801 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2802 p += ext_len;
2803 }
2804
2805#if defined(MBEDTLS_SSL_PROTO_SSL3)
2806 }
2807#endif
2808
2809 ssl->out_msglen = p - buf;
2810 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2811 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO;
2812
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002813 ret = mbedtls_ssl_write_handshake_msg( ssl );
Jens Wiklander817466c2018-05-22 13:49:31 +02002814
2815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2816
2817 return( ret );
2818}
2819
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002820#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02002821static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
2822{
2823 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002824 ssl->handshake->ciphersuite_info;
Jens Wiklander817466c2018-05-22 13:49:31 +02002825
2826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2827
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002828 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Jens Wiklander817466c2018-05-22 13:49:31 +02002829 {
2830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2831 ssl->state++;
2832 return( 0 );
2833 }
2834
2835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2836 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2837}
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002838#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02002839static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
2840{
2841 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2842 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002843 ssl->handshake->ciphersuite_info;
Jens Wiklander817466c2018-05-22 13:49:31 +02002844 size_t dn_size, total_dn_size; /* excluding length bytes */
2845 size_t ct_len, sa_len; /* including length bytes */
2846 unsigned char *buf, *p;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002847 const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
Jens Wiklander817466c2018-05-22 13:49:31 +02002848 const mbedtls_x509_crt *crt;
2849 int authmode;
2850
2851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2852
2853 ssl->state++;
2854
2855#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2856 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
2857 authmode = ssl->handshake->sni_authmode;
2858 else
2859#endif
2860 authmode = ssl->conf->authmode;
2861
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002862 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) ||
Jens Wiklander817466c2018-05-22 13:49:31 +02002863 authmode == MBEDTLS_SSL_VERIFY_NONE )
2864 {
2865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2866 return( 0 );
2867 }
2868
2869 /*
2870 * 0 . 0 handshake type
2871 * 1 . 3 handshake length
2872 * 4 . 4 cert type count
2873 * 5 .. m-1 cert types
2874 * m .. m+1 sig alg length (TLS 1.2 only)
2875 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
2876 * n .. n+1 length of all DNs
2877 * n+2 .. n+3 length of DN 1
2878 * n+4 .. ... Distinguished Name #1
2879 * ... .. ... length of DN 2, etc.
2880 */
2881 buf = ssl->out_msg;
2882 p = buf + 4;
2883
2884 /*
2885 * Supported certificate types
2886 *
2887 * ClientCertificateType certificate_types<1..2^8-1>;
2888 * enum { (255) } ClientCertificateType;
2889 */
2890 ct_len = 0;
2891
2892#if defined(MBEDTLS_RSA_C)
2893 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN;
2894#endif
2895#if defined(MBEDTLS_ECDSA_C)
2896 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN;
2897#endif
2898
2899 p[0] = (unsigned char) ct_len++;
2900 p += ct_len;
2901
2902 sa_len = 0;
2903#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2904 /*
2905 * Add signature_algorithms for verify (TLS 1.2)
2906 *
2907 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2908 *
2909 * struct {
2910 * HashAlgorithm hash;
2911 * SignatureAlgorithm signature;
2912 * } SignatureAndHashAlgorithm;
2913 *
2914 * enum { (255) } HashAlgorithm;
2915 * enum { (255) } SignatureAlgorithm;
2916 */
2917 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
2918 {
2919 const int *cur;
2920
2921 /*
2922 * Supported signature algorithms
2923 */
2924 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
2925 {
2926 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *cur );
2927
2928 if( MBEDTLS_SSL_HASH_NONE == hash || mbedtls_ssl_set_calc_verify_md( ssl, hash ) )
2929 continue;
2930
2931#if defined(MBEDTLS_RSA_C)
2932 p[2 + sa_len++] = hash;
2933 p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA;
2934#endif
2935#if defined(MBEDTLS_ECDSA_C)
2936 p[2 + sa_len++] = hash;
2937 p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA;
2938#endif
2939 }
2940
2941 p[0] = (unsigned char)( sa_len >> 8 );
2942 p[1] = (unsigned char)( sa_len );
2943 sa_len += 2;
2944 p += sa_len;
2945 }
2946#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2947
2948 /*
2949 * DistinguishedName certificate_authorities<0..2^16-1>;
2950 * opaque DistinguishedName<1..2^16-1>;
2951 */
2952 p += 2;
2953
2954 total_dn_size = 0;
2955
2956 if( ssl->conf->cert_req_ca_list == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED )
2957 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002958 /* NOTE: If trusted certificates are provisioned
2959 * via a CA callback (configured through
2960 * `mbedtls_ssl_conf_ca_cb()`, then the
2961 * CertificateRequest is currently left empty. */
2962
Jens Wiklander817466c2018-05-22 13:49:31 +02002963#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2964 if( ssl->handshake->sni_ca_chain != NULL )
2965 crt = ssl->handshake->sni_ca_chain;
2966 else
2967#endif
2968 crt = ssl->conf->ca_chain;
2969
2970 while( crt != NULL && crt->version != 0 )
2971 {
2972 dn_size = crt->subject_raw.len;
2973
2974 if( end < p ||
2975 (size_t)( end - p ) < dn_size ||
2976 (size_t)( end - p ) < 2 + dn_size )
2977 {
2978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
2979 break;
2980 }
2981
2982 *p++ = (unsigned char)( dn_size >> 8 );
2983 *p++ = (unsigned char)( dn_size );
2984 memcpy( p, crt->subject_raw.p, dn_size );
2985 p += dn_size;
2986
2987 MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size );
2988
2989 total_dn_size += 2 + dn_size;
2990 crt = crt->next;
2991 }
2992 }
2993
2994 ssl->out_msglen = p - buf;
2995 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2996 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST;
2997 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2998 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
2999
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003000 ret = mbedtls_ssl_write_handshake_msg( ssl );
Jens Wiklander817466c2018-05-22 13:49:31 +02003001
3002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
3003
3004 return( ret );
3005}
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003006#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02003007
3008#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3009 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3010static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
3011{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003012 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02003013
3014 if( ! mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECKEY ) )
3015 {
3016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
3017 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
3018 }
3019
3020 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx,
3021 mbedtls_pk_ec( *mbedtls_ssl_own_key( ssl ) ),
3022 MBEDTLS_ECDH_OURS ) ) != 0 )
3023 {
3024 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
3025 return( ret );
3026 }
3027
3028 return( 0 );
3029}
3030#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
3031 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
3032
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003033#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003034 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3035static int ssl_resume_server_key_exchange( mbedtls_ssl_context *ssl,
3036 size_t *signature_len )
Jens Wiklander817466c2018-05-22 13:49:31 +02003037{
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003038 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3039 * signature length which will be added in ssl_write_server_key_exchange
3040 * after the call to ssl_prepare_server_key_exchange.
3041 * ssl_write_server_key_exchange also takes care of incrementing
3042 * ssl->out_msglen. */
3043 unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2;
3044 size_t sig_max_len = ( ssl->out_buf + MBEDTLS_SSL_OUT_CONTENT_LEN
3045 - sig_start );
3046 int ret = ssl->conf->f_async_resume( ssl,
3047 sig_start, signature_len, sig_max_len );
3048 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3049 {
3050 ssl->handshake->async_in_progress = 0;
3051 mbedtls_ssl_set_async_operation_data( ssl, NULL );
3052 }
3053 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_resume_server_key_exchange", ret );
3054 return( ret );
3055}
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003056#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003057 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
3058
3059/* Prepare the ServerKeyExchange message, up to and including
3060 * calculating the signature if any, but excluding formatting the
3061 * signature and sending the message. */
3062static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl,
3063 size_t *signature_len )
3064{
Jens Wiklander817466c2018-05-22 13:49:31 +02003065 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003066 ssl->handshake->ciphersuite_info;
3067
3068#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED)
3069#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003070 unsigned char *dig_signed = NULL;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003071#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
3072#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02003073
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003074 (void) ciphersuite_info; /* unused in some configurations */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003075#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003076 (void) signature_len;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003077#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003078
3079 ssl->out_msglen = 4; /* header (type:1, length:3) to be written later */
Jens Wiklander817466c2018-05-22 13:49:31 +02003080
3081 /*
3082 *
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003083 * Part 1: Provide key exchange parameters for chosen ciphersuite.
Jens Wiklander817466c2018-05-22 13:49:31 +02003084 *
3085 */
3086
3087 /*
3088 * - ECJPAKE key exchanges
3089 */
3090#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3091 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3092 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003093 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003094 size_t len = 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02003095
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003096 ret = mbedtls_ecjpake_write_round_two(
3097 &ssl->handshake->ecjpake_ctx,
3098 ssl->out_msg + ssl->out_msglen,
3099 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len,
3100 ssl->conf->f_rng, ssl->conf->p_rng );
Jens Wiklander817466c2018-05-22 13:49:31 +02003101 if( ret != 0 )
3102 {
3103 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3104 return( ret );
3105 }
3106
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003107 ssl->out_msglen += len;
Jens Wiklander817466c2018-05-22 13:49:31 +02003108 }
3109#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
3110
3111 /*
3112 * For (EC)DHE key exchanges with PSK, parameters are prefixed by support
3113 * identity hint (RFC 4279, Sec. 3). Until someone needs this feature,
3114 * we use empty support identity hints here.
3115 **/
3116#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
3117 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3118 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
3119 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
3120 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003121 ssl->out_msg[ssl->out_msglen++] = 0x00;
3122 ssl->out_msg[ssl->out_msglen++] = 0x00;
Jens Wiklander817466c2018-05-22 13:49:31 +02003123 }
3124#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
3125 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
3126
3127 /*
3128 * - DHE key exchanges
3129 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003130#if defined(MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02003131 if( mbedtls_ssl_ciphersuite_uses_dhe( ciphersuite_info ) )
3132 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003133 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003134 size_t len = 0;
3135
Jens Wiklander817466c2018-05-22 13:49:31 +02003136 if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL )
3137 {
3138 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) );
3139 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3140 }
3141
3142 /*
3143 * Ephemeral DH parameters:
3144 *
3145 * struct {
3146 * opaque dh_p<1..2^16-1>;
3147 * opaque dh_g<1..2^16-1>;
3148 * opaque dh_Ys<1..2^16-1>;
3149 * } ServerDHParams;
3150 */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003151 if( ( ret = mbedtls_dhm_set_group( &ssl->handshake->dhm_ctx,
3152 &ssl->conf->dhm_P,
3153 &ssl->conf->dhm_G ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003154 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003155 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_set_group", ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02003156 return( ret );
3157 }
3158
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003159 if( ( ret = mbedtls_dhm_make_params(
3160 &ssl->handshake->dhm_ctx,
3161 (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
3162 ssl->out_msg + ssl->out_msglen, &len,
3163 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003164 {
3165 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret );
3166 return( ret );
3167 }
3168
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003169#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003170 dig_signed = ssl->out_msg + ssl->out_msglen;
Jens Wiklander817466c2018-05-22 13:49:31 +02003171#endif
3172
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003173 ssl->out_msglen += len;
Jens Wiklander817466c2018-05-22 13:49:31 +02003174
3175 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
3176 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
3177 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
3178 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
3179 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003180#endif /* MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02003181
3182 /*
3183 * - ECDHE key exchanges
3184 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003185#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02003186 if( mbedtls_ssl_ciphersuite_uses_ecdhe( ciphersuite_info ) )
3187 {
3188 /*
3189 * Ephemeral ECDH parameters:
3190 *
3191 * struct {
3192 * ECParameters curve_params;
3193 * ECPoint public;
3194 * } ServerECDHParams;
3195 */
3196 const mbedtls_ecp_curve_info **curve = NULL;
3197 const mbedtls_ecp_group_id *gid;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003198 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003199 size_t len = 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02003200
3201 /* Match our preference list against the offered curves */
3202 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
3203 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
3204 if( (*curve)->grp_id == *gid )
3205 goto curve_matching_done;
3206
3207curve_matching_done:
3208 if( curve == NULL || *curve == NULL )
3209 {
3210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
3211 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
3212 }
3213
3214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
3215
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003216 if( ( ret = mbedtls_ecdh_setup( &ssl->handshake->ecdh_ctx,
3217 (*curve)->grp_id ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003218 {
3219 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret );
3220 return( ret );
3221 }
3222
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003223 if( ( ret = mbedtls_ecdh_make_params(
3224 &ssl->handshake->ecdh_ctx, &len,
3225 ssl->out_msg + ssl->out_msglen,
3226 MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen,
3227 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003228 {
3229 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret );
3230 return( ret );
3231 }
3232
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003233#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003234 dig_signed = ssl->out_msg + ssl->out_msglen;
Jens Wiklander817466c2018-05-22 13:49:31 +02003235#endif
3236
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003237 ssl->out_msglen += len;
Jens Wiklander817466c2018-05-22 13:49:31 +02003238
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003239 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3240 MBEDTLS_DEBUG_ECDH_Q );
Jens Wiklander817466c2018-05-22 13:49:31 +02003241 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003242#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02003243
3244 /*
3245 *
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003246 * Part 2: For key exchanges involving the server signing the
Jens Wiklander817466c2018-05-22 13:49:31 +02003247 * exchange parameters, compute and add the signature here.
3248 *
3249 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003250#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02003251 if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
3252 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003253 size_t dig_signed_len = ssl->out_msg + ssl->out_msglen - dig_signed;
3254 size_t hashlen = 0;
3255 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003256 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02003257
3258 /*
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003259 * 2.1: Choose hash algorithm:
3260 * A: For TLS 1.2, obey signature-hash-algorithm extension
Jens Wiklander817466c2018-05-22 13:49:31 +02003261 * to choose appropriate hash.
3262 * B: For SSL3, TLS1.0, TLS1.1 and ECDHE_ECDSA, use SHA1
3263 * (RFC 4492, Sec. 5.4)
3264 * C: Otherwise, use MD5 + SHA1 (RFC 4346, Sec. 7.4.3)
3265 */
3266
3267 mbedtls_md_type_t md_alg;
3268
3269#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3270 mbedtls_pk_type_t sig_alg =
3271 mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
3272 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
3273 {
3274 /* A: For TLS 1.2, obey signature-hash-algorithm extension
3275 * (RFC 5246, Sec. 7.4.1.4.1). */
3276 if( sig_alg == MBEDTLS_PK_NONE ||
3277 ( md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs,
3278 sig_alg ) ) == MBEDTLS_MD_NONE )
3279 {
3280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003281 /* (... because we choose a cipher suite
Jens Wiklander817466c2018-05-22 13:49:31 +02003282 * only if there is a matching hash.) */
3283 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3284 }
3285 }
3286 else
3287#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3288#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3289 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3290 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
3291 {
3292 /* B: Default hash SHA1 */
3293 md_alg = MBEDTLS_MD_SHA1;
3294 }
3295 else
3296#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3297 MBEDTLS_SSL_PROTO_TLS1_1 */
3298 {
3299 /* C: MD5 + SHA1 */
3300 md_alg = MBEDTLS_MD_NONE;
3301 }
3302
3303 MBEDTLS_SSL_DEBUG_MSG( 3, ( "pick hash algorithm %d for signing", md_alg ) );
3304
3305 /*
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003306 * 2.2: Compute the hash to be signed
Jens Wiklander817466c2018-05-22 13:49:31 +02003307 */
3308#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3309 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3310 if( md_alg == MBEDTLS_MD_NONE )
3311 {
Jens Wiklander817466c2018-05-22 13:49:31 +02003312 hashlen = 36;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003313 ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash,
3314 dig_signed,
3315 dig_signed_len );
3316 if( ret != 0 )
3317 return( ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02003318 }
3319 else
3320#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3321 MBEDTLS_SSL_PROTO_TLS1_1 */
3322#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3323 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3324 if( md_alg != MBEDTLS_MD_NONE )
3325 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003326 ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
3327 dig_signed,
3328 dig_signed_len,
3329 md_alg );
3330 if( ret != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003331 return( ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02003332 }
3333 else
3334#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3335 MBEDTLS_SSL_PROTO_TLS1_2 */
3336 {
3337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3338 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3339 }
3340
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003341 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
Jens Wiklander817466c2018-05-22 13:49:31 +02003342
3343 /*
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003344 * 2.3: Compute and add the signature
Jens Wiklander817466c2018-05-22 13:49:31 +02003345 */
Jens Wiklander817466c2018-05-22 13:49:31 +02003346#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3347 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
3348 {
3349 /*
3350 * For TLS 1.2, we need to specify signature and hash algorithm
3351 * explicitly through a prefix to the signature.
3352 *
3353 * struct {
3354 * HashAlgorithm hash;
3355 * SignatureAlgorithm signature;
3356 * } SignatureAndHashAlgorithm;
3357 *
3358 * struct {
3359 * SignatureAndHashAlgorithm algorithm;
3360 * opaque signature<0..2^16-1>;
3361 * } DigitallySigned;
3362 *
3363 */
3364
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003365 ssl->out_msg[ssl->out_msglen++] =
3366 mbedtls_ssl_hash_from_md_alg( md_alg );
3367 ssl->out_msg[ssl->out_msglen++] =
3368 mbedtls_ssl_sig_from_pk_alg( sig_alg );
Jens Wiklander817466c2018-05-22 13:49:31 +02003369 }
3370#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3371
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003372#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3373 if( ssl->conf->f_async_sign_start != NULL )
3374 {
3375 ret = ssl->conf->f_async_sign_start( ssl,
3376 mbedtls_ssl_own_cert( ssl ),
3377 md_alg, hash, hashlen );
3378 switch( ret )
3379 {
3380 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3381 /* act as if f_async_sign was null */
3382 break;
3383 case 0:
3384 ssl->handshake->async_in_progress = 1;
3385 return( ssl_resume_server_key_exchange( ssl, signature_len ) );
3386 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
3387 ssl->handshake->async_in_progress = 1;
3388 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3389 default:
3390 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_sign_start", ret );
3391 return( ret );
3392 }
3393 }
3394#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3395
3396 if( mbedtls_ssl_own_key( ssl ) == NULL )
3397 {
3398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3399 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3400 }
3401
3402 /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3403 * signature length which will be added in ssl_write_server_key_exchange
3404 * after the call to ssl_prepare_server_key_exchange.
3405 * ssl_write_server_key_exchange also takes care of incrementing
3406 * ssl->out_msglen. */
3407 if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ),
3408 md_alg, hash, hashlen,
3409 ssl->out_msg + ssl->out_msglen + 2,
3410 signature_len,
3411 ssl->conf->f_rng,
3412 ssl->conf->p_rng ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003413 {
3414 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
3415 return( ret );
3416 }
Jens Wiklander817466c2018-05-22 13:49:31 +02003417 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003418#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02003419
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003420 return( 0 );
3421}
Jens Wiklander817466c2018-05-22 13:49:31 +02003422
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003423/* Prepare the ServerKeyExchange message and send it. For ciphersuites
3424 * that do not include a ServerKeyExchange message, do nothing. Either
3425 * way, if successful, move on to the next step in the SSL state
3426 * machine. */
3427static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl )
3428{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003429 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003430 size_t signature_len = 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003431#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003432 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003433 ssl->handshake->ciphersuite_info;
3434#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003435
3436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
3437
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003438#if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003439 /* Extract static ECDH parameters and abort if ServerKeyExchange
3440 * is not needed. */
3441 if( mbedtls_ssl_ciphersuite_no_pfs( ciphersuite_info ) )
3442 {
3443 /* For suites involving ECDH, extract DH parameters
3444 * from certificate at this point. */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003445#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003446 if( mbedtls_ssl_ciphersuite_uses_ecdh( ciphersuite_info ) )
3447 {
3448 ssl_get_ecdh_params_from_cert( ssl );
3449 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003450#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003451
3452 /* Key exchanges not involving ephemeral keys don't use
3453 * ServerKeyExchange, so end here. */
3454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
3455 ssl->state++;
3456 return( 0 );
3457 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003458#endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003459
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003460#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003461 defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3462 /* If we have already prepared the message and there is an ongoing
3463 * signature operation, resume signing. */
3464 if( ssl->handshake->async_in_progress != 0 )
3465 {
3466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming signature operation" ) );
3467 ret = ssl_resume_server_key_exchange( ssl, &signature_len );
3468 }
3469 else
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003470#endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003471 defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
3472 {
3473 /* ServerKeyExchange is needed. Prepare the message. */
3474 ret = ssl_prepare_server_key_exchange( ssl, &signature_len );
3475 }
3476
3477 if( ret != 0 )
3478 {
3479 /* If we're starting to write a new message, set ssl->out_msglen
3480 * to 0. But if we're resuming after an asynchronous message,
3481 * out_msglen is the amount of data written so far and mst be
3482 * preserved. */
3483 if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange (pending)" ) );
3485 else
3486 ssl->out_msglen = 0;
3487 return( ret );
3488 }
3489
3490 /* If there is a signature, write its length.
3491 * ssl_prepare_server_key_exchange already wrote the signature
3492 * itself at its proper place in the output buffer. */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003493#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003494 if( signature_len != 0 )
3495 {
3496 ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len >> 8 );
3497 ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len );
3498
3499 MBEDTLS_SSL_DEBUG_BUF( 3, "my signature",
3500 ssl->out_msg + ssl->out_msglen,
3501 signature_len );
3502
3503 /* Skip over the already-written signature */
3504 ssl->out_msglen += signature_len;
3505 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003506#endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003507
3508 /* Add header and send. */
Jens Wiklander817466c2018-05-22 13:49:31 +02003509 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3510 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE;
3511
3512 ssl->state++;
3513
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003514 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003515 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003516 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02003517 return( ret );
3518 }
3519
3520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02003521 return( 0 );
3522}
3523
3524static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl )
3525{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003526 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02003527
3528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
3529
3530 ssl->out_msglen = 4;
3531 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3532 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE;
3533
3534 ssl->state++;
3535
3536#if defined(MBEDTLS_SSL_PROTO_DTLS)
3537 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3538 mbedtls_ssl_send_flight_completed( ssl );
3539#endif
3540
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003541 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003542 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003543 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02003544 return( ret );
3545 }
3546
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003547#if defined(MBEDTLS_SSL_PROTO_DTLS)
3548 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3549 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3550 {
3551 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
3552 return( ret );
3553 }
3554#endif /* MBEDTLS_SSL_PROTO_DTLS */
3555
Jens Wiklander817466c2018-05-22 13:49:31 +02003556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
3557
3558 return( 0 );
3559}
3560
3561#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3562 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3563static int ssl_parse_client_dh_public( mbedtls_ssl_context *ssl, unsigned char **p,
3564 const unsigned char *end )
3565{
3566 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3567 size_t n;
3568
3569 /*
3570 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3571 */
3572 if( *p + 2 > end )
3573 {
3574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3575 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3576 }
3577
3578 n = ( (*p)[0] << 8 ) | (*p)[1];
3579 *p += 2;
3580
3581 if( *p + n > end )
3582 {
3583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3584 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3585 }
3586
3587 if( ( ret = mbedtls_dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
3588 {
3589 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret );
3590 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3591 }
3592
3593 *p += n;
3594
3595 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
3596
3597 return( ret );
3598}
3599#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3600 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
3601
3602#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3603 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003604
3605#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3606static int ssl_resume_decrypt_pms( mbedtls_ssl_context *ssl,
3607 unsigned char *peer_pms,
3608 size_t *peer_pmslen,
3609 size_t peer_pmssize )
3610{
3611 int ret = ssl->conf->f_async_resume( ssl,
3612 peer_pms, peer_pmslen, peer_pmssize );
3613 if( ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3614 {
3615 ssl->handshake->async_in_progress = 0;
3616 mbedtls_ssl_set_async_operation_data( ssl, NULL );
3617 }
3618 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_decrypt_encrypted_pms", ret );
3619 return( ret );
3620}
3621#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3622
3623static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl,
3624 const unsigned char *p,
3625 const unsigned char *end,
3626 unsigned char *peer_pms,
3627 size_t *peer_pmslen,
3628 size_t peer_pmssize )
Jens Wiklander817466c2018-05-22 13:49:31 +02003629{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003630 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003631 mbedtls_pk_context *private_key = mbedtls_ssl_own_key( ssl );
3632 mbedtls_pk_context *public_key = &mbedtls_ssl_own_cert( ssl )->pk;
3633 size_t len = mbedtls_pk_get_len( public_key );
Jens Wiklander817466c2018-05-22 13:49:31 +02003634
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003635#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3636 /* If we have already started decoding the message and there is an ongoing
3637 * decryption operation, resume signing. */
3638 if( ssl->handshake->async_in_progress != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003639 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "resuming decryption operation" ) );
3641 return( ssl_resume_decrypt_pms( ssl,
3642 peer_pms, peer_pmslen, peer_pmssize ) );
Jens Wiklander817466c2018-05-22 13:49:31 +02003643 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003644#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Jens Wiklander817466c2018-05-22 13:49:31 +02003645
3646 /*
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003647 * Prepare to decrypt the premaster using own private RSA key
Jens Wiklander817466c2018-05-22 13:49:31 +02003648 */
3649#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3650 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3651 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
3652 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003653 if ( p + 2 > end ) {
3654 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3655 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3656 }
Jens Wiklander817466c2018-05-22 13:49:31 +02003657 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3658 *p++ != ( ( len ) & 0xFF ) )
3659 {
3660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3661 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3662 }
3663 }
3664#endif
3665
3666 if( p + len != end )
3667 {
3668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3669 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3670 }
3671
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003672 /*
3673 * Decrypt the premaster secret
3674 */
3675#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3676 if( ssl->conf->f_async_decrypt_start != NULL )
3677 {
3678 ret = ssl->conf->f_async_decrypt_start( ssl,
3679 mbedtls_ssl_own_cert( ssl ),
3680 p, len );
3681 switch( ret )
3682 {
3683 case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3684 /* act as if f_async_decrypt_start was null */
3685 break;
3686 case 0:
3687 ssl->handshake->async_in_progress = 1;
3688 return( ssl_resume_decrypt_pms( ssl,
3689 peer_pms,
3690 peer_pmslen,
3691 peer_pmssize ) );
3692 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
3693 ssl->handshake->async_in_progress = 1;
3694 return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
3695 default:
3696 MBEDTLS_SSL_DEBUG_RET( 1, "f_async_decrypt_start", ret );
3697 return( ret );
3698 }
3699 }
3700#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3701
3702 if( ! mbedtls_pk_can_do( private_key, MBEDTLS_PK_RSA ) )
3703 {
3704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
3705 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3706 }
3707
3708 ret = mbedtls_pk_decrypt( private_key, p, len,
3709 peer_pms, peer_pmslen, peer_pmssize,
3710 ssl->conf->f_rng, ssl->conf->p_rng );
3711 return( ret );
3712}
3713
3714static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl,
3715 const unsigned char *p,
3716 const unsigned char *end,
3717 size_t pms_offset )
3718{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003719 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003720 unsigned char *pms = ssl->handshake->premaster + pms_offset;
3721 unsigned char ver[2];
3722 unsigned char fake_pms[48], peer_pms[48];
3723 unsigned char mask;
3724 size_t i, peer_pmslen;
3725 unsigned int diff;
3726
3727 /* In case of a failure in decryption, the decryption may write less than
3728 * 2 bytes of output, but we always read the first two bytes. It doesn't
3729 * matter in the end because diff will be nonzero in that case due to
3730 * peer_pmslen being less than 48, and we only care whether diff is 0.
3731 * But do initialize peer_pms for robustness anyway. This also makes
3732 * memory analyzers happy (don't access uninitialized memory, even
3733 * if it's an unsigned char). */
3734 peer_pms[0] = peer_pms[1] = ~0;
3735
3736 ret = ssl_decrypt_encrypted_pms( ssl, p, end,
3737 peer_pms,
3738 &peer_pmslen,
3739 sizeof( peer_pms ) );
3740
3741#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3742 if ( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS )
3743 return( ret );
3744#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3745
Jens Wiklander817466c2018-05-22 13:49:31 +02003746 mbedtls_ssl_write_version( ssl->handshake->max_major_ver,
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003747 ssl->handshake->max_minor_ver,
3748 ssl->conf->transport, ver );
3749
3750 /* Avoid data-dependent branches while checking for invalid
3751 * padding, to protect against timing-based Bleichenbacher-type
3752 * attacks. */
3753 diff = (unsigned int) ret;
3754 diff |= peer_pmslen ^ 48;
3755 diff |= peer_pms[0] ^ ver[0];
3756 diff |= peer_pms[1] ^ ver[1];
3757
3758 /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */
3759 /* MSVC has a warning about unary minus on unsigned, but this is
3760 * well-defined and precisely what we want to do here */
3761#if defined(_MSC_VER)
3762#pragma warning( push )
3763#pragma warning( disable : 4146 )
3764#endif
3765 mask = - ( ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 ) );
3766#if defined(_MSC_VER)
3767#pragma warning( pop )
3768#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02003769
3770 /*
3771 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3772 * must not cause the connection to end immediately; instead, send a
3773 * bad_record_mac later in the handshake.
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003774 * To protect against timing-based variants of the attack, we must
3775 * not have any branch that depends on whether the decryption was
3776 * successful. In particular, always generate the fake premaster secret,
3777 * regardless of whether it will ultimately influence the output or not.
Jens Wiklander817466c2018-05-22 13:49:31 +02003778 */
3779 ret = ssl->conf->f_rng( ssl->conf->p_rng, fake_pms, sizeof( fake_pms ) );
3780 if( ret != 0 )
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003781 {
3782 /* It's ok to abort on an RNG failure, since this does not reveal
3783 * anything about the RSA decryption. */
Jens Wiklander817466c2018-05-22 13:49:31 +02003784 return( ret );
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003785 }
Jens Wiklander817466c2018-05-22 13:49:31 +02003786
3787#if defined(MBEDTLS_SSL_DEBUG_ALL)
3788 if( diff != 0 )
3789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3790#endif
3791
3792 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
3793 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
3794 {
3795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3796 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3797 }
3798 ssl->handshake->pmslen = 48;
3799
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003800 /* Set pms to either the true or the fake PMS, without
3801 * data-dependent branches. */
Jens Wiklander817466c2018-05-22 13:49:31 +02003802 for( i = 0; i < ssl->handshake->pmslen; i++ )
3803 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
3804
3805 return( 0 );
3806}
3807#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
3808 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
3809
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003810#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02003811static int ssl_parse_client_psk_identity( mbedtls_ssl_context *ssl, unsigned char **p,
3812 const unsigned char *end )
3813{
3814 int ret = 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003815 uint16_t n;
Jens Wiklander817466c2018-05-22 13:49:31 +02003816
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003817 if( ssl_conf_has_psk_or_cb( ssl->conf ) == 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003818 {
3819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3820 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3821 }
3822
3823 /*
3824 * Receive client pre-shared key identity name
3825 */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003826 if( end - *p < 2 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003827 {
3828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3829 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3830 }
3831
3832 n = ( (*p)[0] << 8 ) | (*p)[1];
3833 *p += 2;
3834
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003835 if( n == 0 || n > end - *p )
Jens Wiklander817466c2018-05-22 13:49:31 +02003836 {
3837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3838 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3839 }
3840
3841 if( ssl->conf->f_psk != NULL )
3842 {
3843 if( ssl->conf->f_psk( ssl->conf->p_psk, ssl, *p, n ) != 0 )
3844 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
3845 }
3846 else
3847 {
3848 /* Identity is not a big secret since clients send it in the clear,
3849 * but treat it carefully anyway, just in case */
3850 if( n != ssl->conf->psk_identity_len ||
3851 mbedtls_ssl_safer_memcmp( ssl->conf->psk_identity, *p, n ) != 0 )
3852 {
3853 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
3854 }
3855 }
3856
3857 if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY )
3858 {
3859 MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
3860 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3861 MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY );
3862 return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
3863 }
3864
3865 *p += n;
3866
3867 return( 0 );
3868}
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003869#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02003870
3871static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl )
3872{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003873 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02003874 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3875 unsigned char *p, *end;
3876
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003877 ciphersuite_info = ssl->handshake->ciphersuite_info;
Jens Wiklander817466c2018-05-22 13:49:31 +02003878
3879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
3880
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003881#if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \
3882 ( defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3883 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) )
3884 if( ( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
3885 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) &&
3886 ( ssl->handshake->async_in_progress != 0 ) )
3887 {
3888 /* We've already read a record and there is an asynchronous
3889 * operation in progress to decrypt it. So skip reading the
3890 * record. */
3891 MBEDTLS_SSL_DEBUG_MSG( 3, ( "will resume decryption of previously-read record" ) );
3892 }
3893 else
3894#endif
3895 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02003896 {
3897 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
3898 return( ret );
3899 }
3900
3901 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
3902 end = ssl->in_msg + ssl->in_hslen;
3903
3904 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
3905 {
3906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3907 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3908 }
3909
3910 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE )
3911 {
3912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3913 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3914 }
3915
3916#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
3917 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
3918 {
3919 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3920 {
3921 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3922 return( ret );
3923 }
3924
3925 if( p != end )
3926 {
3927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3928 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3929 }
3930
3931 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
3932 ssl->handshake->premaster,
3933 MBEDTLS_PREMASTER_SIZE,
3934 &ssl->handshake->pmslen,
3935 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
3936 {
3937 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
3938 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3939 }
3940
3941 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
3942 }
3943 else
3944#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
3945#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3946 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3947 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3948 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3949 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
3950 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
3951 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
3952 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
3953 {
3954 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
3955 p, end - p) ) != 0 )
3956 {
3957 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
3958 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3959 }
3960
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003961 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3962 MBEDTLS_DEBUG_ECDH_QP );
Jens Wiklander817466c2018-05-22 13:49:31 +02003963
3964 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3965 &ssl->handshake->pmslen,
3966 ssl->handshake->premaster,
3967 MBEDTLS_MPI_MAX_SIZE,
3968 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
3969 {
3970 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
3971 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3972 }
3973
Jens Wiklander3d3b0592019-03-20 15:30:29 +01003974 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
3975 MBEDTLS_DEBUG_ECDH_Z );
Jens Wiklander817466c2018-05-22 13:49:31 +02003976 }
3977 else
3978#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3979 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3980 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3981 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
3982#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3983 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
3984 {
3985 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3986 {
3987 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3988 return( ret );
3989 }
3990
3991 if( p != end )
3992 {
3993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3994 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3995 }
3996
Jerome Forissier11fa71b2020-04-20 17:17:56 +02003997#if defined(MBEDTLS_USE_PSA_CRYPTO)
3998 /* For opaque PSKs, we perform the PSK-to-MS derivation atomatically
3999 * and skip the intermediate PMS. */
4000 if( ssl_use_opaque_psk( ssl ) == 1 )
4001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skip PMS generation for opaque PSK" ) );
4002 else
4003#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jens Wiklander817466c2018-05-22 13:49:31 +02004004 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
4005 ciphersuite_info->key_exchange ) ) != 0 )
4006 {
4007 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
4008 return( ret );
4009 }
4010 }
4011 else
4012#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
4013#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
4014 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4015 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004016#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
4017 if ( ssl->handshake->async_in_progress != 0 )
4018 {
4019 /* There is an asynchronous operation in progress to
4020 * decrypt the encrypted premaster secret, so skip
4021 * directly to resuming this operation. */
4022 MBEDTLS_SSL_DEBUG_MSG( 3, ( "PSK identity already parsed" ) );
4023 /* Update p to skip the PSK identity. ssl_parse_encrypted_pms
4024 * won't actually use it, but maintain p anyway for robustness. */
4025 p += ssl->conf->psk_identity_len + 2;
4026 }
4027 else
4028#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Jens Wiklander817466c2018-05-22 13:49:31 +02004029 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4030 {
4031 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
4032 return( ret );
4033 }
4034
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004035#if defined(MBEDTLS_USE_PSA_CRYPTO)
4036 /* Opaque PSKs are currently only supported for PSK-only. */
4037 if( ssl_use_opaque_psk( ssl ) == 1 )
4038 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4039#endif
4040
Jens Wiklander817466c2018-05-22 13:49:31 +02004041 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
4042 {
4043 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
4044 return( ret );
4045 }
4046
4047 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
4048 ciphersuite_info->key_exchange ) ) != 0 )
4049 {
4050 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
4051 return( ret );
4052 }
4053 }
4054 else
4055#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
4056#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
4057 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
4058 {
4059 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4060 {
4061 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
4062 return( ret );
4063 }
4064 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
4065 {
4066 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
4067 return( ret );
4068 }
4069
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004070#if defined(MBEDTLS_USE_PSA_CRYPTO)
4071 /* Opaque PSKs are currently only supported for PSK-only. */
4072 if( ssl_use_opaque_psk( ssl ) == 1 )
4073 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4074#endif
4075
Jens Wiklander817466c2018-05-22 13:49:31 +02004076 if( p != end )
4077 {
4078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
4079 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
4080 }
4081
4082 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
4083 ciphersuite_info->key_exchange ) ) != 0 )
4084 {
4085 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
4086 return( ret );
4087 }
4088 }
4089 else
4090#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
4091#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
4092 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
4093 {
4094 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
4095 {
4096 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
4097 return( ret );
4098 }
4099
4100 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
4101 p, end - p ) ) != 0 )
4102 {
4103 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
4104 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
4105 }
4106
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004107#if defined(MBEDTLS_USE_PSA_CRYPTO)
4108 /* Opaque PSKs are currently only supported for PSK-only. */
4109 if( ssl_use_opaque_psk( ssl ) == 1 )
4110 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4111#endif
4112
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004113 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
4114 MBEDTLS_DEBUG_ECDH_QP );
Jens Wiklander817466c2018-05-22 13:49:31 +02004115
4116 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
4117 ciphersuite_info->key_exchange ) ) != 0 )
4118 {
4119 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
4120 return( ret );
4121 }
4122 }
4123 else
4124#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
4125#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
4126 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
4127 {
4128 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
4129 {
4130 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
4131 return( ret );
4132 }
4133 }
4134 else
4135#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
4136#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
4137 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
4138 {
4139 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
4140 p, end - p );
4141 if( ret != 0 )
4142 {
4143 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
4144 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
4145 }
4146
4147 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
4148 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
4149 ssl->conf->f_rng, ssl->conf->p_rng );
4150 if( ret != 0 )
4151 {
4152 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
4153 return( ret );
4154 }
4155 }
4156 else
4157#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
4158 {
4159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4160 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4161 }
4162
4163 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
4164 {
4165 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
4166 return( ret );
4167 }
4168
4169 ssl->state++;
4170
4171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
4172
4173 return( 0 );
4174}
4175
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004176#if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Jens Wiklander817466c2018-05-22 13:49:31 +02004177static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
4178{
4179 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004180 ssl->handshake->ciphersuite_info;
Jens Wiklander817466c2018-05-22 13:49:31 +02004181
4182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
4183
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004184 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Jens Wiklander817466c2018-05-22 13:49:31 +02004185 {
4186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4187 ssl->state++;
4188 return( 0 );
4189 }
4190
4191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4192 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4193}
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004194#else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02004195static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
4196{
4197 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
4198 size_t i, sig_len;
4199 unsigned char hash[48];
4200 unsigned char *hash_start = hash;
4201 size_t hashlen;
4202#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4203 mbedtls_pk_type_t pk_alg;
4204#endif
4205 mbedtls_md_type_t md_alg;
4206 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004207 ssl->handshake->ciphersuite_info;
4208 mbedtls_pk_context * peer_pk;
Jens Wiklander817466c2018-05-22 13:49:31 +02004209
4210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
4211
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004212 if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
Jens Wiklander817466c2018-05-22 13:49:31 +02004213 {
4214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4215 ssl->state++;
4216 return( 0 );
4217 }
4218
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004219#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4220 if( ssl->session_negotiate->peer_cert == NULL )
4221 {
4222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4223 ssl->state++;
4224 return( 0 );
4225 }
4226#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4227 if( ssl->session_negotiate->peer_cert_digest == NULL )
4228 {
4229 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
4230 ssl->state++;
4231 return( 0 );
4232 }
4233#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4234
Jens Wiklander817466c2018-05-22 13:49:31 +02004235 /* Read the message without adding it to the checksum */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004236 ret = mbedtls_ssl_read_record( ssl, 0 /* no checksum update */ );
Jens Wiklander817466c2018-05-22 13:49:31 +02004237 if( 0 != ret )
4238 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004239 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record" ), ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02004240 return( ret );
4241 }
4242
4243 ssl->state++;
4244
4245 /* Process the message contents */
4246 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
4247 ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY )
4248 {
4249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4250 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
4251 }
4252
4253 i = mbedtls_ssl_hs_hdr_len( ssl );
4254
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004255#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4256 peer_pk = &ssl->handshake->peer_pubkey;
4257#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4258 if( ssl->session_negotiate->peer_cert == NULL )
4259 {
4260 /* Should never happen */
4261 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4262 }
4263 peer_pk = &ssl->session_negotiate->peer_cert->pk;
4264#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4265
Jens Wiklander817466c2018-05-22 13:49:31 +02004266 /*
4267 * struct {
4268 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
4269 * opaque signature<0..2^16-1>;
4270 * } DigitallySigned;
4271 */
4272#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4273 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4274 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
4275 {
4276 md_alg = MBEDTLS_MD_NONE;
4277 hashlen = 36;
4278
4279 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004280 if( mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECDSA ) )
Jens Wiklander817466c2018-05-22 13:49:31 +02004281 {
4282 hash_start += 16;
4283 hashlen -= 16;
4284 md_alg = MBEDTLS_MD_SHA1;
4285 }
4286 }
4287 else
4288#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 ||
4289 MBEDTLS_SSL_PROTO_TLS1_1 */
4290#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4291 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
4292 {
4293 if( i + 2 > ssl->in_hslen )
4294 {
4295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4296 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
4297 }
4298
4299 /*
4300 * Hash
4301 */
4302 md_alg = mbedtls_ssl_md_alg_from_hash( ssl->in_msg[i] );
4303
4304 if( md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md( ssl, ssl->in_msg[i] ) )
4305 {
4306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
4307 " for verify message" ) );
4308 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
4309 }
4310
4311#if !defined(MBEDTLS_MD_SHA1)
4312 if( MBEDTLS_MD_SHA1 == md_alg )
4313 hash_start += 16;
4314#endif
4315
4316 /* Info from md_alg will be used instead */
4317 hashlen = 0;
4318
4319 i++;
4320
4321 /*
4322 * Signature
4323 */
4324 if( ( pk_alg = mbedtls_ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
4325 == MBEDTLS_PK_NONE )
4326 {
4327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
4328 " for verify message" ) );
4329 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
4330 }
4331
4332 /*
4333 * Check the certificate's key type matches the signature alg
4334 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004335 if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
Jens Wiklander817466c2018-05-22 13:49:31 +02004336 {
4337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
4338 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
4339 }
4340
4341 i++;
4342 }
4343 else
4344#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4345 {
4346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4347 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4348 }
4349
4350 if( i + 2 > ssl->in_hslen )
4351 {
4352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4353 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
4354 }
4355
4356 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
4357 i += 2;
4358
4359 if( i + sig_len != ssl->in_hslen )
4360 {
4361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
4362 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
4363 }
4364
4365 /* Calculate hash and verify signature */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004366 {
4367 size_t dummy_hlen;
4368 ssl->handshake->calc_verify( ssl, hash, &dummy_hlen );
4369 }
Jens Wiklander817466c2018-05-22 13:49:31 +02004370
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004371 if( ( ret = mbedtls_pk_verify( peer_pk,
Jens Wiklander817466c2018-05-22 13:49:31 +02004372 md_alg, hash_start, hashlen,
4373 ssl->in_msg + i, sig_len ) ) != 0 )
4374 {
4375 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
4376 return( ret );
4377 }
4378
4379 mbedtls_ssl_update_handshake_status( ssl );
4380
4381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
4382
4383 return( ret );
4384}
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004385#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
Jens Wiklander817466c2018-05-22 13:49:31 +02004386
4387#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4388static int ssl_write_new_session_ticket( mbedtls_ssl_context *ssl )
4389{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02004390 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02004391 size_t tlen;
4392 uint32_t lifetime;
4393
4394 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
4395
4396 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4397 ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET;
4398
4399 /*
4400 * struct {
4401 * uint32 ticket_lifetime_hint;
4402 * opaque ticket<0..2^16-1>;
4403 * } NewSessionTicket;
4404 *
4405 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
4406 * 8 . 9 ticket_len (n)
4407 * 10 . 9+n ticket content
4408 */
4409
4410 if( ( ret = ssl->conf->f_ticket_write( ssl->conf->p_ticket,
4411 ssl->session_negotiate,
4412 ssl->out_msg + 10,
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004413 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
Jens Wiklander817466c2018-05-22 13:49:31 +02004414 &tlen, &lifetime ) ) != 0 )
4415 {
4416 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_write", ret );
4417 tlen = 0;
4418 }
4419
4420 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
4421 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
4422 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
4423 ssl->out_msg[7] = ( lifetime ) & 0xFF;
4424
4425 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
4426 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
4427
4428 ssl->out_msglen = 10 + tlen;
4429
4430 /*
4431 * Morally equivalent to updating ssl->state, but NewSessionTicket and
4432 * ChangeCipherSpec share the same state.
4433 */
4434 ssl->handshake->new_session_ticket = 0;
4435
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004436 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02004437 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004438 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Jens Wiklander817466c2018-05-22 13:49:31 +02004439 return( ret );
4440 }
4441
4442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
4443
4444 return( 0 );
4445}
4446#endif /* MBEDTLS_SSL_SESSION_TICKETS */
4447
4448/*
4449 * SSL handshake -- server side -- single step
4450 */
4451int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl )
4452{
4453 int ret = 0;
4454
4455 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
4456 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4457
4458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
4459
4460 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4461 return( ret );
4462
4463#if defined(MBEDTLS_SSL_PROTO_DTLS)
4464 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4465 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
4466 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004467 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +02004468 return( ret );
4469 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01004470#endif /* MBEDTLS_SSL_PROTO_DTLS */
Jens Wiklander817466c2018-05-22 13:49:31 +02004471
4472 switch( ssl->state )
4473 {
4474 case MBEDTLS_SSL_HELLO_REQUEST:
4475 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
4476 break;
4477
4478 /*
4479 * <== ClientHello
4480 */
4481 case MBEDTLS_SSL_CLIENT_HELLO:
4482 ret = ssl_parse_client_hello( ssl );
4483 break;
4484
4485#if defined(MBEDTLS_SSL_PROTO_DTLS)
4486 case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
4487 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4488#endif
4489
4490 /*
4491 * ==> ServerHello
4492 * Certificate
4493 * ( ServerKeyExchange )
4494 * ( CertificateRequest )
4495 * ServerHelloDone
4496 */
4497 case MBEDTLS_SSL_SERVER_HELLO:
4498 ret = ssl_write_server_hello( ssl );
4499 break;
4500
4501 case MBEDTLS_SSL_SERVER_CERTIFICATE:
4502 ret = mbedtls_ssl_write_certificate( ssl );
4503 break;
4504
4505 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
4506 ret = ssl_write_server_key_exchange( ssl );
4507 break;
4508
4509 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
4510 ret = ssl_write_certificate_request( ssl );
4511 break;
4512
4513 case MBEDTLS_SSL_SERVER_HELLO_DONE:
4514 ret = ssl_write_server_hello_done( ssl );
4515 break;
4516
4517 /*
4518 * <== ( Certificate/Alert )
4519 * ClientKeyExchange
4520 * ( CertificateVerify )
4521 * ChangeCipherSpec
4522 * Finished
4523 */
4524 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
4525 ret = mbedtls_ssl_parse_certificate( ssl );
4526 break;
4527
4528 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
4529 ret = ssl_parse_client_key_exchange( ssl );
4530 break;
4531
4532 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
4533 ret = ssl_parse_certificate_verify( ssl );
4534 break;
4535
4536 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
4537 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
4538 break;
4539
4540 case MBEDTLS_SSL_CLIENT_FINISHED:
4541 ret = mbedtls_ssl_parse_finished( ssl );
4542 break;
4543
4544 /*
4545 * ==> ( NewSessionTicket )
4546 * ChangeCipherSpec
4547 * Finished
4548 */
4549 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4550#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4551 if( ssl->handshake->new_session_ticket != 0 )
4552 ret = ssl_write_new_session_ticket( ssl );
4553 else
4554#endif
4555 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
4556 break;
4557
4558 case MBEDTLS_SSL_SERVER_FINISHED:
4559 ret = mbedtls_ssl_write_finished( ssl );
4560 break;
4561
4562 case MBEDTLS_SSL_FLUSH_BUFFERS:
4563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
4564 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
4565 break;
4566
4567 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
4568 mbedtls_ssl_handshake_wrapup( ssl );
4569 break;
4570
4571 default:
4572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
4573 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4574 }
4575
4576 return( ret );
4577}
4578#endif /* MBEDTLS_SSL_SRV_C */