[MBEDTLS] Update to v2.2.0. By Ismael Ferreras Morezuelas. CORE-10561
[reactos.git] / reactos / dll / 3rdparty / mbedtls / ssl_srv.c
1 /*
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 #include "mbedtls/debug.h"
31 #include "mbedtls/ssl.h"
32 #include "mbedtls/ssl_internal.h"
33
34 #include <string.h>
35
36 #if defined(MBEDTLS_ECP_C)
37 #include "mbedtls/ecp.h"
38 #endif
39
40 #if defined(MBEDTLS_PLATFORM_C)
41 #include "mbedtls/platform.h"
42 #else
43 #include <stdlib.h>
44 #define mbedtls_calloc calloc
45 #define mbedtls_free free
46 #endif
47
48 #if defined(MBEDTLS_HAVE_TIME)
49 #include <time.h>
50 #endif
51
52 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
53 /* Implementation that should never be optimized out by the compiler */
54 static void mbedtls_zeroize( void *v, size_t n ) {
55 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
56 }
57 #endif
58
59 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
60 int mbedtls_ssl_set_client_transport_id( mbedtls_ssl_context *ssl,
61 const unsigned char *info,
62 size_t ilen )
63 {
64 if( ssl->conf->endpoint != MBEDTLS_SSL_IS_SERVER )
65 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
66
67 mbedtls_free( ssl->cli_id );
68
69 if( ( ssl->cli_id = mbedtls_calloc( 1, ilen ) ) == NULL )
70 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
71
72 memcpy( ssl->cli_id, info, ilen );
73 ssl->cli_id_len = ilen;
74
75 return( 0 );
76 }
77
78 void mbedtls_ssl_conf_dtls_cookies( mbedtls_ssl_config *conf,
79 mbedtls_ssl_cookie_write_t *f_cookie_write,
80 mbedtls_ssl_cookie_check_t *f_cookie_check,
81 void *p_cookie )
82 {
83 conf->f_cookie_write = f_cookie_write;
84 conf->f_cookie_check = f_cookie_check;
85 conf->p_cookie = p_cookie;
86 }
87 #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
88
89 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
90 static int ssl_parse_servername_ext( mbedtls_ssl_context *ssl,
91 const unsigned char *buf,
92 size_t len )
93 {
94 int ret;
95 size_t servername_list_size, hostname_len;
96 const unsigned char *p;
97
98 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
99
100 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
101 if( servername_list_size + 2 != len )
102 {
103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
104 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
105 }
106
107 p = buf + 2;
108 while( servername_list_size > 0 )
109 {
110 hostname_len = ( ( p[1] << 8 ) | p[2] );
111 if( hostname_len + 3 > servername_list_size )
112 {
113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
114 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
115 }
116
117 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
118 {
119 ret = ssl->conf->f_sni( ssl->conf->p_sni,
120 ssl, p + 3, hostname_len );
121 if( ret != 0 )
122 {
123 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
124 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
125 MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME );
126 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
127 }
128 return( 0 );
129 }
130
131 servername_list_size -= hostname_len + 3;
132 p += hostname_len + 3;
133 }
134
135 if( servername_list_size != 0 )
136 {
137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
138 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
139 }
140
141 return( 0 );
142 }
143 #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
144
145 static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
146 const unsigned char *buf,
147 size_t len )
148 {
149 int ret;
150
151 #if defined(MBEDTLS_SSL_RENEGOTIATION)
152 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
153 {
154 /* Check verify-data in constant-time. The length OTOH is no secret */
155 if( len != 1 + ssl->verify_data_len ||
156 buf[0] != ssl->verify_data_len ||
157 mbedtls_ssl_safer_memcmp( buf + 1, ssl->peer_verify_data,
158 ssl->verify_data_len ) != 0 )
159 {
160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
161
162 if( ( ret = mbedtls_ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
163 return( ret );
164
165 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
166 }
167 }
168 else
169 #endif /* MBEDTLS_SSL_RENEGOTIATION */
170 {
171 if( len != 1 || buf[0] != 0x0 )
172 {
173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
174
175 if( ( ret = mbedtls_ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
176 return( ret );
177
178 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
179 }
180
181 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
182 }
183
184 return( 0 );
185 }
186
187 #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
188 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
189 static int ssl_parse_signature_algorithms_ext( mbedtls_ssl_context *ssl,
190 const unsigned char *buf,
191 size_t len )
192 {
193 size_t sig_alg_list_size;
194 const unsigned char *p;
195 const unsigned char *end = buf + len;
196 const int *md_cur;
197
198
199 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
200 if( sig_alg_list_size + 2 != len ||
201 sig_alg_list_size % 2 != 0 )
202 {
203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
204 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
205 }
206
207 /*
208 * For now, ignore the SignatureAlgorithm part and rely on offered
209 * ciphersuites only for that part. To be fixed later.
210 *
211 * So, just look at the HashAlgorithm part.
212 */
213 for( md_cur = ssl->conf->sig_hashes; *md_cur != MBEDTLS_MD_NONE; md_cur++ ) {
214 for( p = buf + 2; p < end; p += 2 ) {
215 if( *md_cur == (int) mbedtls_ssl_md_alg_from_hash( p[0] ) ) {
216 ssl->handshake->sig_alg = p[0];
217 goto have_sig_alg;
218 }
219 }
220 }
221
222 /* Some key echanges do not need signatures at all */
223 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature_algorithm in common" ) );
224 return( 0 );
225
226 have_sig_alg:
227 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
228 ssl->handshake->sig_alg ) );
229
230 return( 0 );
231 }
232 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
233 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
234
235 #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
236 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
237 static int ssl_parse_supported_elliptic_curves( mbedtls_ssl_context *ssl,
238 const unsigned char *buf,
239 size_t len )
240 {
241 size_t list_size, our_size;
242 const unsigned char *p;
243 const mbedtls_ecp_curve_info *curve_info, **curves;
244
245 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
246 if( list_size + 2 != len ||
247 list_size % 2 != 0 )
248 {
249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
250 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
251 }
252
253 /* Should never happen unless client duplicates the extension */
254 if( ssl->handshake->curves != NULL )
255 {
256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
257 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
258 }
259
260 /* Don't allow our peer to make us allocate too much memory,
261 * and leave room for a final 0 */
262 our_size = list_size / 2 + 1;
263 if( our_size > MBEDTLS_ECP_DP_MAX )
264 our_size = MBEDTLS_ECP_DP_MAX;
265
266 if( ( curves = mbedtls_calloc( our_size, sizeof( *curves ) ) ) == NULL )
267 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
268
269 ssl->handshake->curves = curves;
270
271 p = buf + 2;
272 while( list_size > 0 && our_size > 1 )
273 {
274 curve_info = mbedtls_ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
275
276 if( curve_info != NULL )
277 {
278 *curves++ = curve_info;
279 our_size--;
280 }
281
282 list_size -= 2;
283 p += 2;
284 }
285
286 return( 0 );
287 }
288
289 static int ssl_parse_supported_point_formats( mbedtls_ssl_context *ssl,
290 const unsigned char *buf,
291 size_t len )
292 {
293 size_t list_size;
294 const unsigned char *p;
295
296 list_size = buf[0];
297 if( list_size + 1 != len )
298 {
299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
300 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
301 }
302
303 p = buf + 1;
304 while( list_size > 0 )
305 {
306 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
307 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
308 {
309 #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
310 ssl->handshake->ecdh_ctx.point_format = p[0];
311 #endif
312 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
313 ssl->handshake->ecjpake_ctx.point_format = p[0];
314 #endif
315 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
316 return( 0 );
317 }
318
319 list_size--;
320 p++;
321 }
322
323 return( 0 );
324 }
325 #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
326 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
327
328 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
329 static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
330 const unsigned char *buf,
331 size_t len )
332 {
333 int ret;
334
335 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
336 {
337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
338 return( 0 );
339 }
340
341 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
342 buf, len ) ) != 0 )
343 {
344 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
345 return( ret );
346 }
347
348 /* Only mark the extension as OK when we're sure it is */
349 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK;
350
351 return( 0 );
352 }
353 #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
354
355 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
356 static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
357 const unsigned char *buf,
358 size_t len )
359 {
360 if( len != 1 || buf[0] >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID )
361 {
362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
363 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
364 }
365
366 ssl->session_negotiate->mfl_code = buf[0];
367
368 return( 0 );
369 }
370 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
371
372 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
373 static int ssl_parse_truncated_hmac_ext( mbedtls_ssl_context *ssl,
374 const unsigned char *buf,
375 size_t len )
376 {
377 if( len != 0 )
378 {
379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
380 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
381 }
382
383 ((void) buf);
384
385 if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
386 ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED;
387
388 return( 0 );
389 }
390 #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
391
392 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
393 static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
394 const unsigned char *buf,
395 size_t len )
396 {
397 if( len != 0 )
398 {
399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
400 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
401 }
402
403 ((void) buf);
404
405 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
406 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
407 {
408 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
409 }
410
411 return( 0 );
412 }
413 #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
414
415 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
416 static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
417 const unsigned char *buf,
418 size_t len )
419 {
420 if( len != 0 )
421 {
422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
423 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
424 }
425
426 ((void) buf);
427
428 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED &&
429 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
430 {
431 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
432 }
433
434 return( 0 );
435 }
436 #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
437
438 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
439 static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
440 unsigned char *buf,
441 size_t len )
442 {
443 int ret;
444 mbedtls_ssl_session session;
445
446 mbedtls_ssl_session_init( &session );
447
448 if( ssl->conf->f_ticket_parse == NULL ||
449 ssl->conf->f_ticket_write == NULL )
450 {
451 return( 0 );
452 }
453
454 /* Remember the client asked us to send a new ticket */
455 ssl->handshake->new_session_ticket = 1;
456
457 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
458
459 if( len == 0 )
460 return( 0 );
461
462 #if defined(MBEDTLS_SSL_RENEGOTIATION)
463 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
464 {
465 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
466 return( 0 );
467 }
468 #endif /* MBEDTLS_SSL_RENEGOTIATION */
469
470 /*
471 * Failures are ok: just ignore the ticket and proceed.
472 */
473 if( ( ret = ssl->conf->f_ticket_parse( ssl->conf->p_ticket, &session,
474 buf, len ) ) != 0 )
475 {
476 mbedtls_ssl_session_free( &session );
477
478 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
479 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is not authentic" ) );
480 else if( ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED )
481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket is expired" ) );
482 else
483 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_parse", ret );
484
485 return( 0 );
486 }
487
488 /*
489 * Keep the session ID sent by the client, since we MUST send it back to
490 * inform them we're accepting the ticket (RFC 5077 section 3.4)
491 */
492 session.id_len = ssl->session_negotiate->id_len;
493 memcpy( &session.id, ssl->session_negotiate->id, session.id_len );
494
495 mbedtls_ssl_session_free( ssl->session_negotiate );
496 memcpy( ssl->session_negotiate, &session, sizeof( mbedtls_ssl_session ) );
497
498 /* Zeroize instead of free as we copied the content */
499 mbedtls_zeroize( &session, sizeof( mbedtls_ssl_session ) );
500
501 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
502
503 ssl->handshake->resume = 1;
504
505 /* Don't send a new ticket after all, this one is OK */
506 ssl->handshake->new_session_ticket = 0;
507
508 return( 0 );
509 }
510 #endif /* MBEDTLS_SSL_SESSION_TICKETS */
511
512 #if defined(MBEDTLS_SSL_ALPN)
513 static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
514 const unsigned char *buf, size_t len )
515 {
516 size_t list_len, cur_len, ours_len;
517 const unsigned char *theirs, *start, *end;
518 const char **ours;
519
520 /* If ALPN not configured, just ignore the extension */
521 if( ssl->conf->alpn_list == NULL )
522 return( 0 );
523
524 /*
525 * opaque ProtocolName<1..2^8-1>;
526 *
527 * struct {
528 * ProtocolName protocol_name_list<2..2^16-1>
529 * } ProtocolNameList;
530 */
531
532 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
533 if( len < 4 )
534 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
535
536 list_len = ( buf[0] << 8 ) | buf[1];
537 if( list_len != len - 2 )
538 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
539
540 /*
541 * Use our order of preference
542 */
543 start = buf + 2;
544 end = buf + len;
545 for( ours = ssl->conf->alpn_list; *ours != NULL; ours++ )
546 {
547 ours_len = strlen( *ours );
548 for( theirs = start; theirs != end; theirs += cur_len )
549 {
550 /* If the list is well formed, we should get equality first */
551 if( theirs > end )
552 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
553
554 cur_len = *theirs++;
555
556 /* Empty strings MUST NOT be included */
557 if( cur_len == 0 )
558 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
559
560 if( cur_len == ours_len &&
561 memcmp( theirs, *ours, cur_len ) == 0 )
562 {
563 ssl->alpn_chosen = *ours;
564 return( 0 );
565 }
566 }
567 }
568
569 /* If we get there, no match was found */
570 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
571 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
572 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
573 }
574 #endif /* MBEDTLS_SSL_ALPN */
575
576 /*
577 * Auxiliary functions for ServerHello parsing and related actions
578 */
579
580 #if defined(MBEDTLS_X509_CRT_PARSE_C)
581 /*
582 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
583 */
584 #if defined(MBEDTLS_ECDSA_C)
585 static int ssl_check_key_curve( mbedtls_pk_context *pk,
586 const mbedtls_ecp_curve_info **curves )
587 {
588 const mbedtls_ecp_curve_info **crv = curves;
589 mbedtls_ecp_group_id grp_id = mbedtls_pk_ec( *pk )->grp.id;
590
591 while( *crv != NULL )
592 {
593 if( (*crv)->grp_id == grp_id )
594 return( 0 );
595 crv++;
596 }
597
598 return( -1 );
599 }
600 #endif /* MBEDTLS_ECDSA_C */
601
602 /*
603 * Try picking a certificate for this ciphersuite,
604 * return 0 on success and -1 on failure.
605 */
606 static int ssl_pick_cert( mbedtls_ssl_context *ssl,
607 const mbedtls_ssl_ciphersuite_t * ciphersuite_info )
608 {
609 mbedtls_ssl_key_cert *cur, *list, *fallback = NULL;
610 mbedtls_pk_type_t pk_alg = mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
611 uint32_t flags;
612
613 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
614 if( ssl->handshake->sni_key_cert != NULL )
615 list = ssl->handshake->sni_key_cert;
616 else
617 #endif
618 list = ssl->conf->key_cert;
619
620 if( pk_alg == MBEDTLS_PK_NONE )
621 return( 0 );
622
623 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
624
625 if( list == NULL )
626 {
627 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server has no certificate" ) );
628 return( -1 );
629 }
630
631 for( cur = list; cur != NULL; cur = cur->next )
632 {
633 MBEDTLS_SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
634 cur->cert );
635
636 if( ! mbedtls_pk_can_do( cur->key, pk_alg ) )
637 {
638 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
639 continue;
640 }
641
642 /*
643 * This avoids sending the client a cert it'll reject based on
644 * keyUsage or other extensions.
645 *
646 * It also allows the user to provision different certificates for
647 * different uses based on keyUsage, eg if they want to avoid signing
648 * and decrypting with the same RSA key.
649 */
650 if( mbedtls_ssl_check_cert_usage( cur->cert, ciphersuite_info,
651 MBEDTLS_SSL_IS_SERVER, &flags ) != 0 )
652 {
653 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
654 "(extended) key usage extension" ) );
655 continue;
656 }
657
658 #if defined(MBEDTLS_ECDSA_C)
659 if( pk_alg == MBEDTLS_PK_ECDSA &&
660 ssl_check_key_curve( cur->key, ssl->handshake->curves ) != 0 )
661 {
662 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
663 continue;
664 }
665 #endif
666
667 /*
668 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
669 * present them a SHA-higher cert rather than failing if it's the only
670 * one we got that satisfies the other conditions.
671 */
672 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 &&
673 cur->cert->sig_md != MBEDTLS_MD_SHA1 )
674 {
675 if( fallback == NULL )
676 fallback = cur;
677 {
678 MBEDTLS_SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
679 "sha-2 with pre-TLS 1.2 client" ) );
680 continue;
681 }
682 }
683
684 /* If we get there, we got a winner */
685 break;
686 }
687
688 if( cur == NULL )
689 cur = fallback;
690
691 /* Do not update ssl->handshake->key_cert unless there is a match */
692 if( cur != NULL )
693 {
694 ssl->handshake->key_cert = cur;
695 MBEDTLS_SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
696 ssl->handshake->key_cert->cert );
697 return( 0 );
698 }
699
700 return( -1 );
701 }
702 #endif /* MBEDTLS_X509_CRT_PARSE_C */
703
704 /*
705 * Check if a given ciphersuite is suitable for use with our config/keys/etc
706 * Sets ciphersuite_info only if the suite matches.
707 */
708 static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id,
709 const mbedtls_ssl_ciphersuite_t **ciphersuite_info )
710 {
711 const mbedtls_ssl_ciphersuite_t *suite_info;
712
713 suite_info = mbedtls_ssl_ciphersuite_from_id( suite_id );
714 if( suite_info == NULL )
715 {
716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
717 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
718 }
719
720 MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %s", suite_info->name ) );
721
722 if( suite_info->min_minor_ver > ssl->minor_ver ||
723 suite_info->max_minor_ver < ssl->minor_ver )
724 {
725 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
726 return( 0 );
727 }
728
729 #if defined(MBEDTLS_SSL_PROTO_DTLS)
730 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
731 ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
732 return( 0 );
733 #endif
734
735 #if defined(MBEDTLS_ARC4_C)
736 if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED &&
737 suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
738 {
739 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) );
740 return( 0 );
741 }
742 #endif
743
744 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
745 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
746 ( ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK ) == 0 )
747 {
748 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake "
749 "not configured or ext missing" ) );
750 return( 0 );
751 }
752 #endif
753
754
755 #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
756 if( mbedtls_ssl_ciphersuite_uses_ec( suite_info ) &&
757 ( ssl->handshake->curves == NULL ||
758 ssl->handshake->curves[0] == NULL ) )
759 {
760 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
761 "no common elliptic curve" ) );
762 return( 0 );
763 }
764 #endif
765
766 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
767 /* If the ciphersuite requires a pre-shared key and we don't
768 * have one, skip it now rather than failing later */
769 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
770 ssl->conf->f_psk == NULL &&
771 ( ssl->conf->psk == NULL || ssl->conf->psk_identity == NULL ||
772 ssl->conf->psk_identity_len == 0 || ssl->conf->psk_len == 0 ) )
773 {
774 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
775 return( 0 );
776 }
777 #endif
778
779 #if defined(MBEDTLS_X509_CRT_PARSE_C)
780 /*
781 * Final check: if ciphersuite requires us to have a
782 * certificate/key of a particular type:
783 * - select the appropriate certificate if we have one, or
784 * - try the next ciphersuite if we don't
785 * This must be done last since we modify the key_cert list.
786 */
787 if( ssl_pick_cert( ssl, suite_info ) != 0 )
788 {
789 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
790 "no suitable certificate" ) );
791 return( 0 );
792 }
793 #endif
794
795 *ciphersuite_info = suite_info;
796 return( 0 );
797 }
798
799 #if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
800 static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl )
801 {
802 int ret, got_common_suite;
803 unsigned int i, j;
804 size_t n;
805 unsigned int ciph_len, sess_len, chal_len;
806 unsigned char *buf, *p;
807 const int *ciphersuites;
808 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
809
810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
811
812 #if defined(MBEDTLS_SSL_RENEGOTIATION)
813 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
814 {
815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
816
817 if( ( ret = mbedtls_ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
818 return( ret );
819
820 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
821 }
822 #endif /* MBEDTLS_SSL_RENEGOTIATION */
823
824 buf = ssl->in_hdr;
825
826 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, 5 );
827
828 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
829 buf[2] ) );
830 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
831 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
832 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
833 buf[3], buf[4] ) );
834
835 /*
836 * SSLv2 Client Hello
837 *
838 * Record layer:
839 * 0 . 1 message length
840 *
841 * SSL layer:
842 * 2 . 2 message type
843 * 3 . 4 protocol version
844 */
845 if( buf[2] != MBEDTLS_SSL_HS_CLIENT_HELLO ||
846 buf[3] != MBEDTLS_SSL_MAJOR_VERSION_3 )
847 {
848 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
849 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
850 }
851
852 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
853
854 if( n < 17 || n > 512 )
855 {
856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
857 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
858 }
859
860 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
861 ssl->minor_ver = ( buf[4] <= ssl->conf->max_minor_ver )
862 ? buf[4] : ssl->conf->max_minor_ver;
863
864 if( ssl->minor_ver < ssl->conf->min_minor_ver )
865 {
866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
867 " [%d:%d] < [%d:%d]",
868 ssl->major_ver, ssl->minor_ver,
869 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
870
871 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
872 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
873 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
874 }
875
876 ssl->handshake->max_major_ver = buf[3];
877 ssl->handshake->max_minor_ver = buf[4];
878
879 if( ( ret = mbedtls_ssl_fetch_input( ssl, 2 + n ) ) != 0 )
880 {
881 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
882 return( ret );
883 }
884
885 ssl->handshake->update_checksum( ssl, buf + 2, n );
886
887 buf = ssl->in_msg;
888 n = ssl->in_left - 5;
889
890 /*
891 * 0 . 1 ciphersuitelist length
892 * 2 . 3 session id length
893 * 4 . 5 challenge length
894 * 6 . .. ciphersuitelist
895 * .. . .. session id
896 * .. . .. challenge
897 */
898 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, n );
899
900 ciph_len = ( buf[0] << 8 ) | buf[1];
901 sess_len = ( buf[2] << 8 ) | buf[3];
902 chal_len = ( buf[4] << 8 ) | buf[5];
903
904 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
905 ciph_len, sess_len, chal_len ) );
906
907 /*
908 * Make sure each parameter length is valid
909 */
910 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
911 {
912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
913 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
914 }
915
916 if( sess_len > 32 )
917 {
918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
919 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
920 }
921
922 if( chal_len < 8 || chal_len > 32 )
923 {
924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
925 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
926 }
927
928 if( n != 6 + ciph_len + sess_len + chal_len )
929 {
930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
931 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
932 }
933
934 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
935 buf + 6, ciph_len );
936 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id",
937 buf + 6 + ciph_len, sess_len );
938 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, challenge",
939 buf + 6 + ciph_len + sess_len, chal_len );
940
941 p = buf + 6 + ciph_len;
942 ssl->session_negotiate->id_len = sess_len;
943 memset( ssl->session_negotiate->id, 0,
944 sizeof( ssl->session_negotiate->id ) );
945 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->id_len );
946
947 p += sess_len;
948 memset( ssl->handshake->randbytes, 0, 64 );
949 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
950
951 /*
952 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
953 */
954 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
955 {
956 if( p[0] == 0 && p[1] == 0 && p[2] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
957 {
958 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
959 #if defined(MBEDTLS_SSL_RENEGOTIATION)
960 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
961 {
962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
963 "during renegotiation" ) );
964
965 if( ( ret = mbedtls_ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
966 return( ret );
967
968 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
969 }
970 #endif /* MBEDTLS_SSL_RENEGOTIATION */
971 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
972 break;
973 }
974 }
975
976 #if defined(MBEDTLS_SSL_FALLBACK_SCSV)
977 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
978 {
979 if( p[0] == 0 &&
980 p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
981 p[2] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
982 {
983 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
984
985 if( ssl->minor_ver < ssl->conf->max_minor_ver )
986 {
987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
988
989 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
990 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
991
992 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
993 }
994
995 break;
996 }
997 }
998 #endif /* MBEDTLS_SSL_FALLBACK_SCSV */
999
1000 got_common_suite = 0;
1001 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
1002 ciphersuite_info = NULL;
1003 #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1004 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1005 {
1006 for( i = 0; ciphersuites[i] != 0; i++ )
1007 #else
1008 for( i = 0; ciphersuites[i] != 0; i++ )
1009 {
1010 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1011 #endif
1012 {
1013 if( p[0] != 0 ||
1014 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1015 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1016 continue;
1017
1018 got_common_suite = 1;
1019
1020 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1021 &ciphersuite_info ) ) != 0 )
1022 return( ret );
1023
1024 if( ciphersuite_info != NULL )
1025 goto have_ciphersuite_v2;
1026 }
1027 }
1028
1029 if( got_common_suite )
1030 {
1031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1032 "but none of them usable" ) );
1033 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
1034 }
1035 else
1036 {
1037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1038 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
1039 }
1040
1041 have_ciphersuite_v2:
1042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
1043
1044 ssl->session_negotiate->ciphersuite = ciphersuites[i];
1045 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1046 mbedtls_ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1047
1048 /*
1049 * SSLv2 Client Hello relevant renegotiation security checks
1050 */
1051 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1052 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
1053 {
1054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1055
1056 if( ( ret = mbedtls_ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1057 return( ret );
1058
1059 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1060 }
1061
1062 ssl->in_left = 0;
1063 ssl->state++;
1064
1065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1066
1067 return( 0 );
1068 }
1069 #endif /* MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1070
1071 static int ssl_parse_client_hello( mbedtls_ssl_context *ssl )
1072 {
1073 int ret, got_common_suite;
1074 size_t i, j;
1075 size_t ciph_offset, comp_offset, ext_offset;
1076 size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
1077 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1078 size_t cookie_offset, cookie_len;
1079 #endif
1080 unsigned char *buf, *p, *ext;
1081 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1082 int renegotiation_info_seen = 0;
1083 #endif
1084 int handshake_failure = 0;
1085 const int *ciphersuites;
1086 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1087 int major, minor;
1088
1089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1090
1091 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1092 read_record_header:
1093 #endif
1094 /*
1095 * If renegotiating, then the input was read with mbedtls_ssl_read_record(),
1096 * otherwise read it ourselves manually in order to support SSLv2
1097 * ClientHello, which doesn't use the same record layer format.
1098 */
1099 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1100 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
1101 #endif
1102 {
1103 if( ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 )
1104 {
1105 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
1106 return( ret );
1107 }
1108 }
1109
1110 buf = ssl->in_hdr;
1111
1112 #if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1113 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1114 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
1115 #endif
1116 if( ( buf[0] & 0x80 ) != 0 )
1117 return ssl_parse_client_hello_v2( ssl );
1118 #endif
1119
1120 MBEDTLS_SSL_DEBUG_BUF( 4, "record header", buf, mbedtls_ssl_hdr_len( ssl ) );
1121
1122 /*
1123 * SSLv3/TLS Client Hello
1124 *
1125 * Record layer:
1126 * 0 . 0 message type
1127 * 1 . 2 protocol version
1128 * 3 . 11 DTLS: epoch + record sequence number
1129 * 3 . 4 message length
1130 */
1131 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1132 buf[0] ) );
1133
1134 if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE )
1135 {
1136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1137 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1138 }
1139
1140 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1141 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1142
1143 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
1144 buf[1], buf[2] ) );
1145
1146 mbedtls_ssl_read_version( &major, &minor, ssl->conf->transport, buf + 1 );
1147
1148 /* According to RFC 5246 Appendix E.1, the version here is typically
1149 * "{03,00}, the lowest version number supported by the client, [or] the
1150 * value of ClientHello.client_version", so the only meaningful check here
1151 * is the major version shouldn't be less than 3 */
1152 if( major < MBEDTLS_SSL_MAJOR_VERSION_3 )
1153 {
1154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1155 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1156 }
1157
1158 /* For DTLS if this is the initial handshake, remember the client sequence
1159 * number to use it in our next message (RFC 6347 4.2.1) */
1160 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1161 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM
1162 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1163 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
1164 #endif
1165 )
1166 {
1167 /* Epoch should be 0 for initial handshakes */
1168 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1169 {
1170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1171 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1172 }
1173
1174 memcpy( ssl->out_ctr + 2, ssl->in_ctr + 2, 6 );
1175
1176 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1177 if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
1178 {
1179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
1180 ssl->next_record_offset = 0;
1181 ssl->in_left = 0;
1182 goto read_record_header;
1183 }
1184
1185 /* No MAC to check yet, so we can update right now */
1186 mbedtls_ssl_dtls_replay_update( ssl );
1187 #endif
1188 }
1189 #endif /* MBEDTLS_SSL_PROTO_DTLS */
1190
1191 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
1192
1193 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1194 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
1195 {
1196 /* Set by mbedtls_ssl_read_record() */
1197 msg_len = ssl->in_hslen;
1198 }
1199 else
1200 #endif
1201 {
1202 if( msg_len > MBEDTLS_SSL_MAX_CONTENT_LEN )
1203 {
1204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1205 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1206 }
1207
1208 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) + msg_len ) ) != 0 )
1209 {
1210 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
1211 return( ret );
1212 }
1213
1214 /* Done reading this record, get ready for the next one */
1215 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1216 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1217 ssl->next_record_offset = msg_len + mbedtls_ssl_hdr_len( ssl );
1218 else
1219 #endif
1220 ssl->in_left = 0;
1221 }
1222
1223 buf = ssl->in_msg;
1224
1225 MBEDTLS_SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
1226
1227 ssl->handshake->update_checksum( ssl, buf, msg_len );
1228
1229 /*
1230 * Handshake layer:
1231 * 0 . 0 handshake type
1232 * 1 . 3 handshake length
1233 * 4 . 5 DTLS only: message seqence number
1234 * 6 . 8 DTLS only: fragment offset
1235 * 9 . 11 DTLS only: fragment length
1236 */
1237 if( msg_len < mbedtls_ssl_hs_hdr_len( ssl ) )
1238 {
1239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1240 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1241 }
1242
1243 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
1244
1245 if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
1246 {
1247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1248 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1249 }
1250
1251 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1252 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1253
1254 /* We don't support fragmentation of ClientHello (yet?) */
1255 if( buf[1] != 0 ||
1256 msg_len != mbedtls_ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
1257 {
1258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1259 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1260 }
1261
1262 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1263 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1264 {
1265 /*
1266 * Copy the client's handshake message_seq on initial handshakes,
1267 * check sequence number on renego.
1268 */
1269 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1270 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
1271 {
1272 /* This couldn't be done in ssl_prepare_handshake_record() */
1273 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1274 ssl->in_msg[5];
1275
1276 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1277 {
1278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
1279 "%d (expected %d)", cli_msg_seq,
1280 ssl->handshake->in_msg_seq ) );
1281 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1282 }
1283
1284 ssl->handshake->in_msg_seq++;
1285 }
1286 else
1287 #endif
1288 {
1289 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1290 ssl->in_msg[5];
1291 ssl->handshake->out_msg_seq = cli_msg_seq;
1292 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
1293 }
1294
1295 /*
1296 * For now we don't support fragmentation, so make sure
1297 * fragment_offset == 0 and fragment_length == length
1298 */
1299 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1300 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1301 {
1302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
1303 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1304 }
1305 }
1306 #endif /* MBEDTLS_SSL_PROTO_DTLS */
1307
1308 buf += mbedtls_ssl_hs_hdr_len( ssl );
1309 msg_len -= mbedtls_ssl_hs_hdr_len( ssl );
1310
1311 /*
1312 * ClientHello layer:
1313 * 0 . 1 protocol version
1314 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1315 * 34 . 35 session id length (1 byte)
1316 * 35 . 34+x session id
1317 * 35+x . 35+x DTLS only: cookie length (1 byte)
1318 * 36+x . .. DTLS only: cookie
1319 * .. . .. ciphersuite list length (2 bytes)
1320 * .. . .. ciphersuite list
1321 * .. . .. compression alg. list length (1 byte)
1322 * .. . .. compression alg. list
1323 * .. . .. extensions length (2 bytes, optional)
1324 * .. . .. extensions (optional)
1325 */
1326
1327 /*
1328 * Minimal length (with everything empty and extensions ommitted) is
1329 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1330 * read at least up to session id length without worrying.
1331 */
1332 if( msg_len < 38 )
1333 {
1334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1335 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1336 }
1337
1338 /*
1339 * Check and save the protocol version
1340 */
1341 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
1342
1343 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
1344 ssl->conf->transport, buf );
1345
1346 ssl->handshake->max_major_ver = ssl->major_ver;
1347 ssl->handshake->max_minor_ver = ssl->minor_ver;
1348
1349 if( ssl->major_ver < ssl->conf->min_major_ver ||
1350 ssl->minor_ver < ssl->conf->min_minor_ver )
1351 {
1352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
1353 " [%d:%d] < [%d:%d]",
1354 ssl->major_ver, ssl->minor_ver,
1355 ssl->conf->min_major_ver, ssl->conf->min_minor_ver ) );
1356
1357 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1358 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
1359
1360 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1361 }
1362
1363 if( ssl->major_ver > ssl->conf->max_major_ver )
1364 {
1365 ssl->major_ver = ssl->conf->max_major_ver;
1366 ssl->minor_ver = ssl->conf->max_minor_ver;
1367 }
1368 else if( ssl->minor_ver > ssl->conf->max_minor_ver )
1369 ssl->minor_ver = ssl->conf->max_minor_ver;
1370
1371 /*
1372 * Save client random (inc. Unix time)
1373 */
1374 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
1375
1376 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
1377
1378 /*
1379 * Check the session ID length and save session ID
1380 */
1381 sess_len = buf[34];
1382
1383 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
1384 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
1385 {
1386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1387 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1388 }
1389
1390 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
1391
1392 ssl->session_negotiate->id_len = sess_len;
1393 memset( ssl->session_negotiate->id, 0,
1394 sizeof( ssl->session_negotiate->id ) );
1395 memcpy( ssl->session_negotiate->id, buf + 35,
1396 ssl->session_negotiate->id_len );
1397
1398 /*
1399 * Check the cookie length and content
1400 */
1401 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1402 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1403 {
1404 cookie_offset = 35 + sess_len;
1405 cookie_len = buf[cookie_offset];
1406
1407 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
1408 {
1409 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1410 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1411 }
1412
1413 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
1414 buf + cookie_offset + 1, cookie_len );
1415
1416 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
1417 if( ssl->conf->f_cookie_check != NULL
1418 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1419 && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
1420 #endif
1421 )
1422 {
1423 if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
1424 buf + cookie_offset + 1, cookie_len,
1425 ssl->cli_id, ssl->cli_id_len ) != 0 )
1426 {
1427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
1428 ssl->handshake->verify_cookie_len = 1;
1429 }
1430 else
1431 {
1432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
1433 ssl->handshake->verify_cookie_len = 0;
1434 }
1435 }
1436 else
1437 #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
1438 {
1439 /* We know we didn't send a cookie, so it should be empty */
1440 if( cookie_len != 0 )
1441 {
1442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1443 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1444 }
1445
1446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
1447 }
1448
1449 /*
1450 * Check the ciphersuitelist length (will be parsed later)
1451 */
1452 ciph_offset = cookie_offset + 1 + cookie_len;
1453 }
1454 else
1455 #endif /* MBEDTLS_SSL_PROTO_DTLS */
1456 ciph_offset = 35 + sess_len;
1457
1458 ciph_len = ( buf[ciph_offset + 0] << 8 )
1459 | ( buf[ciph_offset + 1] );
1460
1461 if( ciph_len < 2 ||
1462 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1463 ( ciph_len % 2 ) != 0 )
1464 {
1465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1466 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1467 }
1468
1469 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1470 buf + ciph_offset + 2, ciph_len );
1471
1472 /*
1473 * Check the compression algorithms length and pick one
1474 */
1475 comp_offset = ciph_offset + 2 + ciph_len;
1476
1477 comp_len = buf[comp_offset];
1478
1479 if( comp_len < 1 ||
1480 comp_len > 16 ||
1481 comp_len + comp_offset + 1 > msg_len )
1482 {
1483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1484 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1485 }
1486
1487 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, compression",
1488 buf + comp_offset + 1, comp_len );
1489
1490 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
1491 #if defined(MBEDTLS_ZLIB_SUPPORT)
1492 for( i = 0; i < comp_len; ++i )
1493 {
1494 if( buf[comp_offset + 1 + i] == MBEDTLS_SSL_COMPRESS_DEFLATE )
1495 {
1496 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_DEFLATE;
1497 break;
1498 }
1499 }
1500 #endif
1501
1502 /* See comments in ssl_write_client_hello() */
1503 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1504 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1505 ssl->session_negotiate->compression = MBEDTLS_SSL_COMPRESS_NULL;
1506 #endif
1507
1508 /*
1509 * Check the extension length
1510 */
1511 ext_offset = comp_offset + 1 + comp_len;
1512 if( msg_len > ext_offset )
1513 {
1514 if( msg_len < ext_offset + 2 )
1515 {
1516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1517 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1518 }
1519
1520 ext_len = ( buf[ext_offset + 0] << 8 )
1521 | ( buf[ext_offset + 1] );
1522
1523 if( ( ext_len > 0 && ext_len < 4 ) ||
1524 msg_len != ext_offset + 2 + ext_len )
1525 {
1526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1527 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1528 }
1529 }
1530 else
1531 ext_len = 0;
1532
1533 ext = buf + ext_offset + 2;
1534 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", ext, ext_len );
1535
1536 while( ext_len != 0 )
1537 {
1538 unsigned int ext_id = ( ( ext[0] << 8 )
1539 | ( ext[1] ) );
1540 unsigned int ext_size = ( ( ext[2] << 8 )
1541 | ( ext[3] ) );
1542
1543 if( ext_size + 4 > ext_len )
1544 {
1545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1546 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1547 }
1548 switch( ext_id )
1549 {
1550 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1551 case MBEDTLS_TLS_EXT_SERVERNAME:
1552 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1553 if( ssl->conf->f_sni == NULL )
1554 break;
1555
1556 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1557 if( ret != 0 )
1558 return( ret );
1559 break;
1560 #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1561
1562 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1563 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1564 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1565 renegotiation_info_seen = 1;
1566 #endif
1567
1568 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1569 if( ret != 0 )
1570 return( ret );
1571 break;
1572
1573 #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
1574 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
1575 case MBEDTLS_TLS_EXT_SIG_ALG:
1576 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1577 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1578 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
1579 break;
1580 #endif
1581
1582 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1583 if( ret != 0 )
1584 return( ret );
1585 break;
1586 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
1587 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
1588
1589 #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
1590 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1591 case MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1592 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1593
1594 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1595 if( ret != 0 )
1596 return( ret );
1597 break;
1598
1599 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1600 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1601 ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
1602
1603 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1604 if( ret != 0 )
1605 return( ret );
1606 break;
1607 #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1608 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1609
1610 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1611 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1612 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake kkpp extension" ) );
1613
1614 ret = ssl_parse_ecjpake_kkpp( ssl, ext + 4, ext_size );
1615 if( ret != 0 )
1616 return( ret );
1617 break;
1618 #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1619
1620 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1621 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1622 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1623
1624 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1625 if( ret != 0 )
1626 return( ret );
1627 break;
1628 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
1629
1630 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1631 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
1632 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1633
1634 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1635 if( ret != 0 )
1636 return( ret );
1637 break;
1638 #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1639
1640 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1641 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1642 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
1643
1644 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1645 if( ret != 0 )
1646 return( ret );
1647 break;
1648 #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
1649
1650 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1651 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1652 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
1653
1654 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1655 if( ret != 0 )
1656 return( ret );
1657 break;
1658 #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1659
1660 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
1661 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1662 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1663
1664 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1665 if( ret != 0 )
1666 return( ret );
1667 break;
1668 #endif /* MBEDTLS_SSL_SESSION_TICKETS */
1669
1670 #if defined(MBEDTLS_SSL_ALPN)
1671 case MBEDTLS_TLS_EXT_ALPN:
1672 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1673
1674 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1675 if( ret != 0 )
1676 return( ret );
1677 break;
1678 #endif /* MBEDTLS_SSL_SESSION_TICKETS */
1679
1680 default:
1681 MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1682 ext_id ) );
1683 }
1684
1685 ext_len -= 4 + ext_size;
1686 ext += 4 + ext_size;
1687
1688 if( ext_len > 0 && ext_len < 4 )
1689 {
1690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1691 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1692 }
1693 }
1694
1695 #if defined(MBEDTLS_SSL_FALLBACK_SCSV)
1696 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1697 {
1698 if( p[0] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
1699 p[1] == (unsigned char)( ( MBEDTLS_SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
1700 {
1701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received FALLBACK_SCSV" ) );
1702
1703 if( ssl->minor_ver < ssl->conf->max_minor_ver )
1704 {
1705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
1706
1707 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1708 MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1709
1710 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1711 }
1712
1713 break;
1714 }
1715 }
1716 #endif /* MBEDTLS_SSL_FALLBACK_SCSV */
1717
1718 /*
1719 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1720 */
1721 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
1722 {
1723 if( p[0] == 0 && p[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO )
1724 {
1725 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1726 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1727 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
1728 {
1729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1730
1731 if( ( ret = mbedtls_ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1732 return( ret );
1733
1734 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1735 }
1736 #endif
1737 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
1738 break;
1739 }
1740 }
1741
1742 /*
1743 * Renegotiation security checks
1744 */
1745 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION &&
1746 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
1747 {
1748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1749 handshake_failure = 1;
1750 }
1751 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1752 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1753 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
1754 renegotiation_info_seen == 0 )
1755 {
1756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1757 handshake_failure = 1;
1758 }
1759 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1760 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1761 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
1762 {
1763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
1764 handshake_failure = 1;
1765 }
1766 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1767 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1768 renegotiation_info_seen == 1 )
1769 {
1770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1771 handshake_failure = 1;
1772 }
1773 #endif /* MBEDTLS_SSL_RENEGOTIATION */
1774
1775 if( handshake_failure == 1 )
1776 {
1777 if( ( ret = mbedtls_ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1778 return( ret );
1779
1780 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
1781 }
1782
1783 /*
1784 * Search for a matching ciphersuite
1785 * (At the end because we need information from the EC-based extensions
1786 * and certificate from the SNI callback triggered by the SNI extension.)
1787 */
1788 got_common_suite = 0;
1789 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
1790 ciphersuite_info = NULL;
1791 #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1792 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
1793 {
1794 for( i = 0; ciphersuites[i] != 0; i++ )
1795 #else
1796 for( i = 0; ciphersuites[i] != 0; i++ )
1797 {
1798 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
1799 #endif
1800 {
1801 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1802 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1803 continue;
1804
1805 got_common_suite = 1;
1806
1807 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1808 &ciphersuite_info ) ) != 0 )
1809 return( ret );
1810
1811 if( ciphersuite_info != NULL )
1812 goto have_ciphersuite;
1813 }
1814 }
1815
1816 if( got_common_suite )
1817 {
1818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1819 "but none of them usable" ) );
1820 mbedtls_ssl_send_fatal_handshake_failure( ssl );
1821 return( MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE );
1822 }
1823 else
1824 {
1825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1826 mbedtls_ssl_send_fatal_handshake_failure( ssl );
1827 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
1828 }
1829
1830 have_ciphersuite:
1831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
1832
1833 ssl->session_negotiate->ciphersuite = ciphersuites[i];
1834 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1835 mbedtls_ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1836
1837 ssl->state++;
1838
1839 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1840 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1841 mbedtls_ssl_recv_flight_completed( ssl );
1842 #endif
1843
1844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1845
1846 return( 0 );
1847 }
1848
1849 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1850 static void ssl_write_truncated_hmac_ext( mbedtls_ssl_context *ssl,
1851 unsigned char *buf,
1852 size_t *olen )
1853 {
1854 unsigned char *p = buf;
1855
1856 if( ssl->session_negotiate->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED )
1857 {
1858 *olen = 0;
1859 return;
1860 }
1861
1862 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1863
1864 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1865 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1866
1867 *p++ = 0x00;
1868 *p++ = 0x00;
1869
1870 *olen = 4;
1871 }
1872 #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1873
1874 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1875 static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
1876 unsigned char *buf,
1877 size_t *olen )
1878 {
1879 unsigned char *p = buf;
1880 const mbedtls_ssl_ciphersuite_t *suite = NULL;
1881 const mbedtls_cipher_info_t *cipher = NULL;
1882
1883 if( ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
1884 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
1885 {
1886 *olen = 0;
1887 return;
1888 }
1889
1890 /*
1891 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
1892 * from a client and then selects a stream or Authenticated Encryption
1893 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
1894 * encrypt-then-MAC response extension back to the client."
1895 */
1896 if( ( suite = mbedtls_ssl_ciphersuite_from_id(
1897 ssl->session_negotiate->ciphersuite ) ) == NULL ||
1898 ( cipher = mbedtls_cipher_info_from_type( suite->cipher ) ) == NULL ||
1899 cipher->mode != MBEDTLS_MODE_CBC )
1900 {
1901 *olen = 0;
1902 return;
1903 }
1904
1905 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
1906
1907 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
1908 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
1909
1910 *p++ = 0x00;
1911 *p++ = 0x00;
1912
1913 *olen = 4;
1914 }
1915 #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
1916
1917 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1918 static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
1919 unsigned char *buf,
1920 size_t *olen )
1921 {
1922 unsigned char *p = buf;
1923
1924 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
1925 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
1926 {
1927 *olen = 0;
1928 return;
1929 }
1930
1931 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
1932 "extension" ) );
1933
1934 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
1935 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
1936
1937 *p++ = 0x00;
1938 *p++ = 0x00;
1939
1940 *olen = 4;
1941 }
1942 #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1943
1944 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
1945 static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
1946 unsigned char *buf,
1947 size_t *olen )
1948 {
1949 unsigned char *p = buf;
1950
1951 if( ssl->handshake->new_session_ticket == 0 )
1952 {
1953 *olen = 0;
1954 return;
1955 }
1956
1957 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1958
1959 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1960 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET ) & 0xFF );
1961
1962 *p++ = 0x00;
1963 *p++ = 0x00;
1964
1965 *olen = 4;
1966 }
1967 #endif /* MBEDTLS_SSL_SESSION_TICKETS */
1968
1969 static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
1970 unsigned char *buf,
1971 size_t *olen )
1972 {
1973 unsigned char *p = buf;
1974
1975 if( ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION )
1976 {
1977 *olen = 0;
1978 return;
1979 }
1980
1981 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1982
1983 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1984 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1985
1986 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1987 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
1988 {
1989 *p++ = 0x00;
1990 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1991 *p++ = ssl->verify_data_len * 2 & 0xFF;
1992
1993 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1994 p += ssl->verify_data_len;
1995 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1996 p += ssl->verify_data_len;
1997 }
1998 else
1999 #endif /* MBEDTLS_SSL_RENEGOTIATION */
2000 {
2001 *p++ = 0x00;
2002 *p++ = 0x01;
2003 *p++ = 0x00;
2004 }
2005
2006 *olen = p - buf;
2007 }
2008
2009 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2010 static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
2011 unsigned char *buf,
2012 size_t *olen )
2013 {
2014 unsigned char *p = buf;
2015
2016 if( ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
2017 {
2018 *olen = 0;
2019 return;
2020 }
2021
2022 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
2023
2024 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2025 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
2026
2027 *p++ = 0x00;
2028 *p++ = 1;
2029
2030 *p++ = ssl->session_negotiate->mfl_code;
2031
2032 *olen = 5;
2033 }
2034 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2035
2036 #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
2037 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2038 static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
2039 unsigned char *buf,
2040 size_t *olen )
2041 {
2042 unsigned char *p = buf;
2043 ((void) ssl);
2044
2045 if( ( ssl->handshake->cli_exts &
2046 MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
2047 {
2048 *olen = 0;
2049 return;
2050 }
2051
2052 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
2053
2054 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2055 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
2056
2057 *p++ = 0x00;
2058 *p++ = 2;
2059
2060 *p++ = 1;
2061 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
2062
2063 *olen = 6;
2064 }
2065 #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2066
2067 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2068 static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
2069 unsigned char *buf,
2070 size_t *olen )
2071 {
2072 int ret;
2073 unsigned char *p = buf;
2074 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
2075 size_t kkpp_len;
2076
2077 *olen = 0;
2078
2079 /* Skip costly computation if not needed */
2080 if( ssl->transform_negotiate->ciphersuite_info->key_exchange !=
2081 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2082 return;
2083
2084 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) );
2085
2086 if( end - p < 4 )
2087 {
2088 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
2089 return;
2090 }
2091
2092 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP >> 8 ) & 0xFF );
2093 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ) & 0xFF );
2094
2095 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
2096 p + 2, end - p - 2, &kkpp_len,
2097 ssl->conf->f_rng, ssl->conf->p_rng );
2098 if( ret != 0 )
2099 {
2100 MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret );
2101 return;
2102 }
2103
2104 *p++ = (unsigned char)( ( kkpp_len >> 8 ) & 0xFF );
2105 *p++ = (unsigned char)( ( kkpp_len ) & 0xFF );
2106
2107 *olen = kkpp_len + 4;
2108 }
2109 #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2110
2111 #if defined(MBEDTLS_SSL_ALPN )
2112 static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
2113 unsigned char *buf, size_t *olen )
2114 {
2115 if( ssl->alpn_chosen == NULL )
2116 {
2117 *olen = 0;
2118 return;
2119 }
2120
2121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
2122
2123 /*
2124 * 0 . 1 ext identifier
2125 * 2 . 3 ext length
2126 * 4 . 5 protocol list length
2127 * 6 . 6 protocol name length
2128 * 7 . 7+n protocol name
2129 */
2130 buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN >> 8 ) & 0xFF );
2131 buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN ) & 0xFF );
2132
2133 *olen = 7 + strlen( ssl->alpn_chosen );
2134
2135 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2136 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2137
2138 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2139 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2140
2141 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2142
2143 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2144 }
2145 #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
2146
2147 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2148 static int ssl_write_hello_verify_request( mbedtls_ssl_context *ssl )
2149 {
2150 int ret;
2151 unsigned char *p = ssl->out_msg + 4;
2152 unsigned char *cookie_len_byte;
2153
2154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
2155
2156 /*
2157 * struct {
2158 * ProtocolVersion server_version;
2159 * opaque cookie<0..2^8-1>;
2160 * } HelloVerifyRequest;
2161 */
2162
2163 /* The RFC is not clear on this point, but sending the actual negotiated
2164 * version looks like the most interoperable thing to do. */
2165 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
2166 ssl->conf->transport, p );
2167 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
2168 p += 2;
2169
2170 /* If we get here, f_cookie_check is not null */
2171 if( ssl->conf->f_cookie_write == NULL )
2172 {
2173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2174 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2175 }
2176
2177 /* Skip length byte until we know the length */
2178 cookie_len_byte = p++;
2179
2180 if( ( ret = ssl->conf->f_cookie_write( ssl->conf->p_cookie,
2181 &p, ssl->out_buf + MBEDTLS_SSL_BUFFER_LEN,
2182 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
2183 {
2184 MBEDTLS_SSL_DEBUG_RET( 1, "f_cookie_write", ret );
2185 return( ret );
2186 }
2187
2188 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2189
2190 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
2191
2192 ssl->out_msglen = p - ssl->out_msg;
2193 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2194 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
2195
2196 ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
2197
2198 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
2199 {
2200 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
2201 return( ret );
2202 }
2203
2204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
2205
2206 return( 0 );
2207 }
2208 #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
2209
2210 static int ssl_write_server_hello( mbedtls_ssl_context *ssl )
2211 {
2212 #if defined(MBEDTLS_HAVE_TIME)
2213 time_t t;
2214 #endif
2215 int ret;
2216 size_t olen, ext_len = 0, n;
2217 unsigned char *buf, *p;
2218
2219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2220
2221 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2222 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2223 ssl->handshake->verify_cookie_len != 0 )
2224 {
2225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2227
2228 return( ssl_write_hello_verify_request( ssl ) );
2229 }
2230 #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
2231
2232 if( ssl->conf->f_rng == NULL )
2233 {
2234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2235 return( MBEDTLS_ERR_SSL_NO_RNG );
2236 }
2237
2238 /*
2239 * 0 . 0 handshake type
2240 * 1 . 3 handshake length
2241 * 4 . 5 protocol version
2242 * 6 . 9 UNIX time()
2243 * 10 . 37 random bytes
2244 */
2245 buf = ssl->out_msg;
2246 p = buf + 4;
2247
2248 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
2249 ssl->conf->transport, p );
2250 p += 2;
2251
2252 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
2253 buf[4], buf[5] ) );
2254
2255 #if defined(MBEDTLS_HAVE_TIME)
2256 t = time( NULL );
2257 *p++ = (unsigned char)( t >> 24 );
2258 *p++ = (unsigned char)( t >> 16 );
2259 *p++ = (unsigned char)( t >> 8 );
2260 *p++ = (unsigned char)( t );
2261
2262 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
2263 #else
2264 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
2265 return( ret );
2266
2267 p += 4;
2268 #endif /* MBEDTLS_HAVE_TIME */
2269
2270 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
2271 return( ret );
2272
2273 p += 28;
2274
2275 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
2276
2277 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2278
2279 /*
2280 * Resume is 0 by default, see ssl_handshake_init().
2281 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2282 * If not, try looking up session ID in our cache.
2283 */
2284 if( ssl->handshake->resume == 0 &&
2285 #if defined(MBEDTLS_SSL_RENEGOTIATION)
2286 ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE &&
2287 #endif
2288 ssl->session_negotiate->id_len != 0 &&
2289 ssl->conf->f_get_cache != NULL &&
2290 ssl->conf->f_get_cache( ssl->conf->p_cache, ssl->session_negotiate ) == 0 )
2291 {
2292 MBEDTLS_SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
2293 ssl->handshake->resume = 1;
2294 }
2295
2296 if( ssl->handshake->resume == 0 )
2297 {
2298 /*
2299 * New session, create a new session id,
2300 * unless we're about to issue a session ticket
2301 */
2302 ssl->state++;
2303
2304 #if defined(MBEDTLS_HAVE_TIME)
2305 ssl->session_negotiate->start = time( NULL );
2306 #endif
2307
2308 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
2309 if( ssl->handshake->new_session_ticket != 0 )
2310 {
2311 ssl->session_negotiate->id_len = n = 0;
2312 memset( ssl->session_negotiate->id, 0, 32 );
2313 }
2314 else
2315 #endif /* MBEDTLS_SSL_SESSION_TICKETS */
2316 {
2317 ssl->session_negotiate->id_len = n = 32;
2318 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->session_negotiate->id,
2319 n ) ) != 0 )
2320 return( ret );
2321 }
2322 }
2323 else
2324 {
2325 /*
2326 * Resuming a session
2327 */
2328 n = ssl->session_negotiate->id_len;
2329 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
2330
2331 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
2332 {
2333 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
2334 return( ret );
2335 }
2336 }
2337
2338 /*
2339 * 38 . 38 session id length
2340 * 39 . 38+n session id
2341 * 39+n . 40+n chosen ciphersuite
2342 * 41+n . 41+n chosen compression alg.
2343 * 42+n . 43+n extensions length
2344 * 44+n . 43+n+m extensions
2345 */
2346 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2347 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->id_len );
2348 p += ssl->session_negotiate->id_len;
2349
2350 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2351 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2352 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
2353 ssl->handshake->resume ? "a" : "no" ) );
2354
2355 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2356 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2357 *p++ = (unsigned char)( ssl->session_negotiate->compression );
2358
2359 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2360 mbedtls_ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
2361 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
2362 ssl->session_negotiate->compression ) );
2363
2364 /*
2365 * First write extensions, then the total length
2366 */
2367 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2368 ext_len += olen;
2369
2370 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2371 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2372 ext_len += olen;
2373 #endif
2374
2375 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
2376 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2377 ext_len += olen;
2378 #endif
2379
2380 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2381 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2382 ext_len += olen;
2383 #endif
2384
2385 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2386 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2387 ext_len += olen;
2388 #endif
2389
2390 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
2391 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2392 ext_len += olen;
2393 #endif
2394
2395 #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
2396 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2397 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2398 ext_len += olen;
2399 #endif
2400
2401 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2402 ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen );
2403 ext_len += olen;
2404 #endif
2405
2406 #if defined(MBEDTLS_SSL_ALPN)
2407 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2408 ext_len += olen;
2409 #endif
2410
2411 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
2412
2413 if( ext_len > 0 )
2414 {
2415 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2416 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2417 p += ext_len;
2418 }
2419
2420 ssl->out_msglen = p - buf;
2421 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2422 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO;
2423
2424 ret = mbedtls_ssl_write_record( ssl );
2425
2426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2427
2428 return( ret );
2429 }
2430
2431 #if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
2432 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2433 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2434 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2435 static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
2436 {
2437 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2438
2439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2440
2441 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2442 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2443 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2444 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
2445 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2446 {
2447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2448 ssl->state++;
2449 return( 0 );
2450 }
2451
2452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2453 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2454 }
2455 #else
2456 static int ssl_write_certificate_request( mbedtls_ssl_context *ssl )
2457 {
2458 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2459 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2460 size_t dn_size, total_dn_size; /* excluding length bytes */
2461 size_t ct_len, sa_len; /* including length bytes */
2462 unsigned char *buf, *p;
2463 const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
2464 const mbedtls_x509_crt *crt;
2465 int authmode;
2466
2467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2468
2469 ssl->state++;
2470
2471 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2472 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
2473 authmode = ssl->handshake->sni_authmode;
2474 else
2475 #endif
2476 authmode = ssl->conf->authmode;
2477
2478 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2479 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2480 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2481 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
2482 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ||
2483 authmode == MBEDTLS_SSL_VERIFY_NONE )
2484 {
2485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2486 return( 0 );
2487 }
2488
2489 /*
2490 * 0 . 0 handshake type
2491 * 1 . 3 handshake length
2492 * 4 . 4 cert type count
2493 * 5 .. m-1 cert types
2494 * m .. m+1 sig alg length (TLS 1.2 only)
2495 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
2496 * n .. n+1 length of all DNs
2497 * n+2 .. n+3 length of DN 1
2498 * n+4 .. ... Distinguished Name #1
2499 * ... .. ... length of DN 2, etc.
2500 */
2501 buf = ssl->out_msg;
2502 p = buf + 4;
2503
2504 /*
2505 * Supported certificate types
2506 *
2507 * ClientCertificateType certificate_types<1..2^8-1>;
2508 * enum { (255) } ClientCertificateType;
2509 */
2510 ct_len = 0;
2511
2512 #if defined(MBEDTLS_RSA_C)
2513 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN;
2514 #endif
2515 #if defined(MBEDTLS_ECDSA_C)
2516 p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN;
2517 #endif
2518
2519 p[0] = (unsigned char) ct_len++;
2520 p += ct_len;
2521
2522 sa_len = 0;
2523 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2524 /*
2525 * Add signature_algorithms for verify (TLS 1.2)
2526 *
2527 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2528 *
2529 * struct {
2530 * HashAlgorithm hash;
2531 * SignatureAlgorithm signature;
2532 * } SignatureAndHashAlgorithm;
2533 *
2534 * enum { (255) } HashAlgorithm;
2535 * enum { (255) } SignatureAlgorithm;
2536 */
2537 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
2538 {
2539 /*
2540 * Only use current running hash algorithm that is already required
2541 * for requested ciphersuite.
2542 */
2543 ssl->handshake->verify_sig_alg = MBEDTLS_SSL_HASH_SHA256;
2544
2545 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2546 MBEDTLS_MD_SHA384 )
2547 {
2548 ssl->handshake->verify_sig_alg = MBEDTLS_SSL_HASH_SHA384;
2549 }
2550
2551 /*
2552 * Supported signature algorithms
2553 */
2554 #if defined(MBEDTLS_RSA_C)
2555 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2556 p[2 + sa_len++] = MBEDTLS_SSL_SIG_RSA;
2557 #endif
2558 #if defined(MBEDTLS_ECDSA_C)
2559 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2560 p[2 + sa_len++] = MBEDTLS_SSL_SIG_ECDSA;
2561 #endif
2562
2563 p[0] = (unsigned char)( sa_len >> 8 );
2564 p[1] = (unsigned char)( sa_len );
2565 sa_len += 2;
2566 p += sa_len;
2567 }
2568 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2569
2570 /*
2571 * DistinguishedName certificate_authorities<0..2^16-1>;
2572 * opaque DistinguishedName<1..2^16-1>;
2573 */
2574 p += 2;
2575 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2576 if( ssl->handshake->sni_ca_chain != NULL )
2577 crt = ssl->handshake->sni_ca_chain;
2578 else
2579 #endif
2580 crt = ssl->conf->ca_chain;
2581
2582 total_dn_size = 0;
2583 while( crt != NULL && crt->version != 0 )
2584 {
2585 dn_size = crt->subject_raw.len;
2586
2587 if( end < p || (size_t)( end - p ) < 2 + dn_size )
2588 {
2589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
2590 break;
2591 }
2592
2593 *p++ = (unsigned char)( dn_size >> 8 );
2594 *p++ = (unsigned char)( dn_size );
2595 memcpy( p, crt->subject_raw.p, dn_size );
2596 p += dn_size;
2597
2598 MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size );
2599
2600 total_dn_size += 2 + dn_size;
2601 crt = crt->next;
2602 }
2603
2604 ssl->out_msglen = p - buf;
2605 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2606 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST;
2607 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2608 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
2609
2610 ret = mbedtls_ssl_write_record( ssl );
2611
2612 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2613
2614 return( ret );
2615 }
2616 #endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED &&
2617 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2618 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2619 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
2620
2621 #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2622 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2623 static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
2624 {
2625 int ret;
2626
2627 if( ! mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECKEY ) )
2628 {
2629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2630 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
2631 }
2632
2633 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx,
2634 mbedtls_pk_ec( *mbedtls_ssl_own_key( ssl ) ),
2635 MBEDTLS_ECDH_OURS ) ) != 0 )
2636 {
2637 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
2638 return( ret );
2639 }
2640
2641 return( 0 );
2642 }
2643 #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2644 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2645
2646 static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl )
2647 {
2648 int ret;
2649 size_t n = 0;
2650 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2651 ssl->transform_negotiate->ciphersuite_info;
2652
2653 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2654 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2655 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2656 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
2657 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2658 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2659 unsigned char *p = ssl->out_msg + 4;
2660 unsigned char *dig_signed = p;
2661 size_t dig_signed_len = 0, len;
2662 ((void) dig_signed);
2663 ((void) dig_signed_len);
2664 ((void) len);
2665 #endif
2666
2667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2668
2669 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
2670 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2671 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2672 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ||
2673 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2674 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
2675 {
2676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2677 ssl->state++;
2678 return( 0 );
2679 }
2680 #endif
2681
2682 #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2683 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2684 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2685 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2686 {
2687 ssl_get_ecdh_params_from_cert( ssl );
2688
2689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2690 ssl->state++;
2691 return( 0 );
2692 }
2693 #endif
2694
2695 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2696 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2697 {
2698 size_t jlen;
2699 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
2700
2701 ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
2702 p, end - p, &jlen, ssl->conf->f_rng, ssl->conf->p_rng );
2703 if( ret != 0 )
2704 {
2705 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
2706 return( ret );
2707 }
2708
2709 p += jlen;
2710 n += jlen;
2711 }
2712 #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2713
2714 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2715 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2716 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2717 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
2718 {
2719 /* TODO: Support identity hints */
2720 *(p++) = 0x00;
2721 *(p++) = 0x00;
2722
2723 n += 2;
2724 }
2725 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2726 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2727
2728 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2729 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2730 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2731 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
2732 {
2733 if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL )
2734 {
2735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) );
2736 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2737 }
2738
2739 /*
2740 * Ephemeral DH parameters:
2741 *
2742 * struct {
2743 * opaque dh_p<1..2^16-1>;
2744 * opaque dh_g<1..2^16-1>;
2745 * opaque dh_Ys<1..2^16-1>;
2746 * } ServerDHParams;
2747 */
2748 if( ( ret = mbedtls_mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->conf->dhm_P ) ) != 0 ||
2749 ( ret = mbedtls_mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->conf->dhm_G ) ) != 0 )
2750 {
2751 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_mpi_copy", ret );
2752 return( ret );
2753 }
2754
2755 if( ( ret = mbedtls_dhm_make_params( &ssl->handshake->dhm_ctx,
2756 (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
2757 p, &len, ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
2758 {
2759 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret );
2760 return( ret );
2761 }
2762
2763 dig_signed = p;
2764 dig_signed_len = len;
2765
2766 p += len;
2767 n += len;
2768
2769 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2770 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2771 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2772 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2773 }
2774 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2775 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2776
2777 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
2778 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2779 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2780 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
2781 {
2782 /*
2783 * Ephemeral ECDH parameters:
2784 *
2785 * struct {
2786 * ECParameters curve_params;
2787 * ECPoint public;
2788 * } ServerECDHParams;
2789 */
2790 const mbedtls_ecp_curve_info **curve = NULL;
2791 const mbedtls_ecp_group_id *gid;
2792
2793 /* Match our preference list against the offered curves */
2794 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
2795 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2796 if( (*curve)->grp_id == *gid )
2797 goto curve_matching_done;
2798
2799 curve_matching_done:
2800 if( curve == NULL || *curve == NULL )
2801 {
2802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2803 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
2804 }
2805
2806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
2807
2808 if( ( ret = mbedtls_ecp_group_load( &ssl->handshake->ecdh_ctx.grp,
2809 (*curve)->grp_id ) ) != 0 )
2810 {
2811 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret );
2812 return( ret );
2813 }
2814
2815 if( ( ret = mbedtls_ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2816 p, MBEDTLS_SSL_MAX_CONTENT_LEN - n,
2817 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
2818 {
2819 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret );
2820 return( ret );
2821 }
2822
2823 dig_signed = p;
2824 dig_signed_len = len;
2825
2826 p += len;
2827 n += len;
2828
2829 MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2830 }
2831 #endif /* MBEDTLS_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
2832
2833 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2834 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2835 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2836 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2837 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2838 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
2839 {
2840 size_t signature_len = 0;
2841 unsigned int hashlen = 0;
2842 unsigned char hash[64];
2843 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2844
2845 /*
2846 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2847 */
2848 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2849 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
2850 {
2851 md_alg = mbedtls_ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2852
2853 if( md_alg == MBEDTLS_MD_NONE )
2854 {
2855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2856 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2857 }
2858 }
2859 else
2860 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2861 #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
2862 defined(MBEDTLS_SSL_PROTO_TLS1_1)
2863 if( ciphersuite_info->key_exchange ==
2864 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
2865 {
2866 md_alg = MBEDTLS_MD_SHA1;
2867 }
2868 else
2869 #endif
2870 {
2871 md_alg = MBEDTLS_MD_NONE;
2872 }
2873
2874 /*
2875 * Compute the hash to be signed
2876 */
2877 #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
2878 defined(MBEDTLS_SSL_PROTO_TLS1_1)
2879 if( md_alg == MBEDTLS_MD_NONE )
2880 {
2881 mbedtls_md5_context mbedtls_md5;
2882 mbedtls_sha1_context mbedtls_sha1;
2883
2884 mbedtls_md5_init( &mbedtls_md5 );
2885 mbedtls_sha1_init( &mbedtls_sha1 );
2886
2887 /*
2888 * digitally-signed struct {
2889 * opaque md5_hash[16];
2890 * opaque sha_hash[20];
2891 * };
2892 *
2893 * md5_hash
2894 * MD5(ClientHello.random + ServerHello.random
2895 * + ServerParams);
2896 * sha_hash
2897 * SHA(ClientHello.random + ServerHello.random
2898 * + ServerParams);
2899 */
2900 mbedtls_md5_starts( &mbedtls_md5 );
2901 mbedtls_md5_update( &mbedtls_md5, ssl->handshake->randbytes, 64 );
2902 mbedtls_md5_update( &mbedtls_md5, dig_signed, dig_signed_len );
2903 mbedtls_md5_finish( &mbedtls_md5, hash );
2904
2905 mbedtls_sha1_starts( &mbedtls_sha1 );
2906 mbedtls_sha1_update( &mbedtls_sha1, ssl->handshake->randbytes, 64 );
2907 mbedtls_sha1_update( &mbedtls_sha1, dig_signed, dig_signed_len );
2908 mbedtls_sha1_finish( &mbedtls_sha1, hash + 16 );
2909
2910 hashlen = 36;
2911
2912 mbedtls_md5_free( &mbedtls_md5 );
2913 mbedtls_sha1_free( &mbedtls_sha1 );
2914 }
2915 else
2916 #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
2917 MBEDTLS_SSL_PROTO_TLS1_1 */
2918 #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2919 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2920 if( md_alg != MBEDTLS_MD_NONE )
2921 {
2922 mbedtls_md_context_t ctx;
2923 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
2924
2925 mbedtls_md_init( &ctx );
2926
2927 /* Info from md_alg will be used instead */
2928 hashlen = 0;
2929
2930 /*
2931 * digitally-signed struct {
2932 * opaque client_random[32];
2933 * opaque server_random[32];
2934 * ServerDHParams params;
2935 * };
2936 */
2937 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
2938 {
2939 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
2940 return( ret );
2941 }
2942
2943 mbedtls_md_starts( &ctx );
2944 mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 );
2945 mbedtls_md_update( &ctx, dig_signed, dig_signed_len );
2946 mbedtls_md_finish( &ctx, hash );
2947 mbedtls_md_free( &ctx );
2948 }
2949 else
2950 #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2951 MBEDTLS_SSL_PROTO_TLS1_2 */
2952 {
2953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2954 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2955 }
2956
2957 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2958 (unsigned int) ( mbedtls_md_get_size( mbedtls_md_info_from_type( md_alg ) ) ) );
2959
2960 /*
2961 * Make the signature
2962 */
2963 if( mbedtls_ssl_own_key( ssl ) == NULL )
2964 {
2965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2966 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
2967 }
2968
2969 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2970 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
2971 {
2972 *(p++) = ssl->handshake->sig_alg;
2973 *(p++) = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
2974
2975 n += 2;
2976 }
2977 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2978
2979 if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ), md_alg, hash, hashlen,
2980 p + 2 , &signature_len,
2981 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
2982 {
2983 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
2984 return( ret );
2985 }
2986
2987 *(p++) = (unsigned char)( signature_len >> 8 );
2988 *(p++) = (unsigned char)( signature_len );
2989 n += 2;
2990
2991 MBEDTLS_SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
2992
2993 n += signature_len;
2994 }
2995 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
2996 MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2997 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
2998
2999 ssl->out_msglen = 4 + n;
3000 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3001 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE;
3002
3003 ssl->state++;
3004
3005 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
3006 {
3007 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
3008 return( ret );
3009 }
3010
3011 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
3012
3013 return( 0 );
3014 }
3015
3016 static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl )
3017 {
3018 int ret;
3019
3020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
3021
3022 ssl->out_msglen = 4;
3023 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3024 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE;
3025
3026 ssl->state++;
3027
3028 #if defined(MBEDTLS_SSL_PROTO_DTLS)
3029 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3030 mbedtls_ssl_send_flight_completed( ssl );
3031 #endif
3032
3033 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
3034 {
3035 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
3036 return( ret );
3037 }
3038
3039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
3040
3041 return( 0 );
3042 }
3043
3044 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3045 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3046 static int ssl_parse_client_dh_public( mbedtls_ssl_context *ssl, unsigned char **p,
3047 const unsigned char *end )
3048 {
3049 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3050 size_t n;
3051
3052 /*
3053 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3054 */
3055 if( *p + 2 > end )
3056 {
3057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3058 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3059 }
3060
3061 n = ( (*p)[0] << 8 ) | (*p)[1];
3062 *p += 2;
3063
3064 if( *p + n > end )
3065 {
3066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3067 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3068 }
3069
3070 if( ( ret = mbedtls_dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
3071 {
3072 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret );
3073 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3074 }
3075
3076 *p += n;
3077
3078 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
3079
3080 return( ret );
3081 }
3082 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3083 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
3084
3085 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3086 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3087 static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl,
3088 const unsigned char *p,
3089 const unsigned char *end,
3090 size_t pms_offset )
3091 {
3092 int ret;
3093 size_t len = mbedtls_pk_get_len( mbedtls_ssl_own_key( ssl ) );
3094 unsigned char *pms = ssl->handshake->premaster + pms_offset;
3095 unsigned char ver[2];
3096 unsigned char fake_pms[48], peer_pms[48];
3097 unsigned char mask;
3098 size_t i, peer_pmslen;
3099 unsigned int diff;
3100
3101 if( ! mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_RSA ) )
3102 {
3103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
3104 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUI