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