[MBEDTLS] Sync to v2.2.1. By Ismael Ferreras Morezuelas. CORE-10817
[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 ||
2588 (size_t)( end - p ) < dn_size ||
2589 (size_t)( end - p ) < 2 + dn_size )
2590 {
2591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "skipping CAs: buffer too short" ) );
2592 break;
2593 }
2594
2595 *p++ = (unsigned char)( dn_size >> 8 );
2596 *p++ = (unsigned char)( dn_size );
2597 memcpy( p, crt->subject_raw.p, dn_size );
2598 p += dn_size;
2599
2600 MBEDTLS_SSL_DEBUG_BUF( 3, "requested DN", p - dn_size, dn_size );
2601
2602 total_dn_size += 2 + dn_size;
2603 crt = crt->next;
2604 }
2605
2606 ssl->out_msglen = p - buf;
2607 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2608 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST;
2609 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2610 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
2611
2612 ret = mbedtls_ssl_write_record( ssl );
2613
2614 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2615
2616 return( ret );
2617 }
2618 #endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED &&
2619 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2620 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2621 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
2622
2623 #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2624 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2625 static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
2626 {
2627 int ret;
2628
2629 if( ! mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECKEY ) )
2630 {
2631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2632 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
2633 }
2634
2635 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx,
2636 mbedtls_pk_ec( *mbedtls_ssl_own_key( ssl ) ),
2637 MBEDTLS_ECDH_OURS ) ) != 0 )
2638 {
2639 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
2640 return( ret );
2641 }
2642
2643 return( 0 );
2644 }
2645 #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2646 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2647
2648 static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl )
2649 {
2650 int ret;
2651 size_t n = 0;
2652 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2653 ssl->transform_negotiate->ciphersuite_info;
2654
2655 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2656 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2657 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2658 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
2659 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2660 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2661 unsigned char *p = ssl->out_msg + 4;
2662 unsigned char *dig_signed = p;
2663 size_t dig_signed_len = 0, len;
2664 ((void) dig_signed);
2665 ((void) dig_signed_len);
2666 ((void) len);
2667 #endif
2668
2669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2670
2671 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
2672 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2673 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2674 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ||
2675 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2676 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
2677 {
2678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2679 ssl->state++;
2680 return( 0 );
2681 }
2682 #endif
2683
2684 #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2685 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2686 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2687 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2688 {
2689 ssl_get_ecdh_params_from_cert( ssl );
2690
2691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2692 ssl->state++;
2693 return( 0 );
2694 }
2695 #endif
2696
2697 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2698 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2699 {
2700 size_t jlen;
2701 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
2702
2703 ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
2704 p, end - p, &jlen, ssl->conf->f_rng, ssl->conf->p_rng );
2705 if( ret != 0 )
2706 {
2707 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
2708 return( ret );
2709 }
2710
2711 p += jlen;
2712 n += jlen;
2713 }
2714 #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2715
2716 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2717 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2718 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2719 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
2720 {
2721 /* TODO: Support identity hints */
2722 *(p++) = 0x00;
2723 *(p++) = 0x00;
2724
2725 n += 2;
2726 }
2727 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2728 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2729
2730 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2731 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2732 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2733 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
2734 {
2735 if( ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL )
2736 {
2737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no DH parameters set" ) );
2738 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2739 }
2740
2741 /*
2742 * Ephemeral DH parameters:
2743 *
2744 * struct {
2745 * opaque dh_p<1..2^16-1>;
2746 * opaque dh_g<1..2^16-1>;
2747 * opaque dh_Ys<1..2^16-1>;
2748 * } ServerDHParams;
2749 */
2750 if( ( ret = mbedtls_mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->conf->dhm_P ) ) != 0 ||
2751 ( ret = mbedtls_mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->conf->dhm_G ) ) != 0 )
2752 {
2753 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_mpi_copy", ret );
2754 return( ret );
2755 }
2756
2757 if( ( ret = mbedtls_dhm_make_params( &ssl->handshake->dhm_ctx,
2758 (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
2759 p, &len, ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
2760 {
2761 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_params", ret );
2762 return( ret );
2763 }
2764
2765 dig_signed = p;
2766 dig_signed_len = len;
2767
2768 p += len;
2769 n += len;
2770
2771 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2772 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2773 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2774 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2775 }
2776 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2777 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2778
2779 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
2780 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2781 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2782 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
2783 {
2784 /*
2785 * Ephemeral ECDH parameters:
2786 *
2787 * struct {
2788 * ECParameters curve_params;
2789 * ECPoint public;
2790 * } ServerECDHParams;
2791 */
2792 const mbedtls_ecp_curve_info **curve = NULL;
2793 const mbedtls_ecp_group_id *gid;
2794
2795 /* Match our preference list against the offered curves */
2796 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
2797 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2798 if( (*curve)->grp_id == *gid )
2799 goto curve_matching_done;
2800
2801 curve_matching_done:
2802 if( curve == NULL || *curve == NULL )
2803 {
2804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2805 return( MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN );
2806 }
2807
2808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
2809
2810 if( ( ret = mbedtls_ecp_group_load( &ssl->handshake->ecdh_ctx.grp,
2811 (*curve)->grp_id ) ) != 0 )
2812 {
2813 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecp_group_load", ret );
2814 return( ret );
2815 }
2816
2817 if( ( ret = mbedtls_ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2818 p, MBEDTLS_SSL_MAX_CONTENT_LEN - n,
2819 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
2820 {
2821 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_params", ret );
2822 return( ret );
2823 }
2824
2825 dig_signed = p;
2826 dig_signed_len = len;
2827
2828 p += len;
2829 n += len;
2830
2831 MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2832 }
2833 #endif /* MBEDTLS_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
2834
2835 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2836 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2837 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2838 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2839 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2840 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
2841 {
2842 size_t signature_len = 0;
2843 unsigned int hashlen = 0;
2844 unsigned char hash[64];
2845 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2846
2847 /*
2848 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2849 */
2850 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2851 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
2852 {
2853 md_alg = mbedtls_ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2854
2855 if( md_alg == MBEDTLS_MD_NONE )
2856 {
2857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2858 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2859 }
2860 }
2861 else
2862 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2863 #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
2864 defined(MBEDTLS_SSL_PROTO_TLS1_1)
2865 if( ciphersuite_info->key_exchange ==
2866 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
2867 {
2868 md_alg = MBEDTLS_MD_SHA1;
2869 }
2870 else
2871 #endif
2872 {
2873 md_alg = MBEDTLS_MD_NONE;
2874 }
2875
2876 /*
2877 * Compute the hash to be signed
2878 */
2879 #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
2880 defined(MBEDTLS_SSL_PROTO_TLS1_1)
2881 if( md_alg == MBEDTLS_MD_NONE )
2882 {
2883 mbedtls_md5_context mbedtls_md5;
2884 mbedtls_sha1_context mbedtls_sha1;
2885
2886 mbedtls_md5_init( &mbedtls_md5 );
2887 mbedtls_sha1_init( &mbedtls_sha1 );
2888
2889 /*
2890 * digitally-signed struct {
2891 * opaque md5_hash[16];
2892 * opaque sha_hash[20];
2893 * };
2894 *
2895 * md5_hash
2896 * MD5(ClientHello.random + ServerHello.random
2897 * + ServerParams);
2898 * sha_hash
2899 * SHA(ClientHello.random + ServerHello.random
2900 * + ServerParams);
2901 */
2902 mbedtls_md5_starts( &mbedtls_md5 );
2903 mbedtls_md5_update( &mbedtls_md5, ssl->handshake->randbytes, 64 );
2904 mbedtls_md5_update( &mbedtls_md5, dig_signed, dig_signed_len );
2905 mbedtls_md5_finish( &mbedtls_md5, hash );
2906
2907 mbedtls_sha1_starts( &mbedtls_sha1 );
2908 mbedtls_sha1_update( &mbedtls_sha1, ssl->handshake->randbytes, 64 );
2909 mbedtls_sha1_update( &mbedtls_sha1, dig_signed, dig_signed_len );
2910 mbedtls_sha1_finish( &mbedtls_sha1, hash + 16 );
2911
2912 hashlen = 36;
2913
2914 mbedtls_md5_free( &mbedtls_md5 );
2915 mbedtls_sha1_free( &mbedtls_sha1 );
2916 }
2917 else
2918 #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
2919 MBEDTLS_SSL_PROTO_TLS1_1 */
2920 #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2921 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2922 if( md_alg != MBEDTLS_MD_NONE )
2923 {
2924 mbedtls_md_context_t ctx;
2925 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
2926
2927 mbedtls_md_init( &ctx );
2928
2929 /* Info from md_alg will be used instead */
2930 hashlen = 0;
2931
2932 /*
2933 * digitally-signed struct {
2934 * opaque client_random[32];
2935 * opaque server_random[32];
2936 * ServerDHParams params;
2937 * };
2938 */
2939 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
2940 {
2941 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
2942 return( ret );
2943 }
2944
2945 mbedtls_md_starts( &ctx );
2946 mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 );
2947 mbedtls_md_update( &ctx, dig_signed, dig_signed_len );
2948 mbedtls_md_finish( &ctx, hash );
2949 mbedtls_md_free( &ctx );
2950 }
2951 else
2952 #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2953 MBEDTLS_SSL_PROTO_TLS1_2 */
2954 {
2955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2956 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2957 }
2958
2959 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2960 (unsigned int) ( mbedtls_md_get_size( mbedtls_md_info_from_type( md_alg ) ) ) );
2961
2962 /*
2963 * Make the signature
2964 */
2965 if( mbedtls_ssl_own_key( ssl ) == NULL )
2966 {
2967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2968 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
2969 }
2970
2971 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2972 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
2973 {
2974 *(p++) = ssl->handshake->sig_alg;
2975 *(p++) = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
2976
2977 n += 2;
2978 }
2979 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2980
2981 if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ), md_alg, hash, hashlen,
2982 p + 2 , &signature_len,
2983 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
2984 {
2985 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
2986 return( ret );
2987 }
2988
2989 *(p++) = (unsigned char)( signature_len >> 8 );
2990 *(p++) = (unsigned char)( signature_len );
2991 n += 2;
2992
2993 MBEDTLS_SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
2994
2995 n += signature_len;
2996 }
2997 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
2998 MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2999 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
3000
3001 ssl->out_msglen = 4 + n;
3002 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3003 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE;
3004
3005 ssl->state++;
3006
3007 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
3008 {
3009 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
3010 return( ret );
3011 }
3012
3013 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
3014
3015 return( 0 );
3016 }
3017
3018 static int ssl_write_server_hello_done( mbedtls_ssl_context *ssl )
3019 {
3020 int ret;
3021
3022 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
3023
3024 ssl->out_msglen = 4;
3025 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3026 ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE;
3027
3028 ssl->state++;
3029
3030 #if defined(MBEDTLS_SSL_PROTO_DTLS)
3031 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3032 mbedtls_ssl_send_flight_completed( ssl );
3033 #endif
3034
3035 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
3036 {
3037 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
3038 return( ret );
3039 }
3040
3041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
3042
3043 return( 0 );
3044 }
3045
3046 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3047 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3048 static int ssl_parse_client_dh_public( mbedtls_ssl_context *ssl, unsigned char **p,
3049 const unsigned char *end )
3050 {
3051 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3052 size_t n;
3053
3054 /*
3055 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3056 */
3057 if( *p + 2 > end )
3058 {
3059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3060 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3061 }
3062
3063 n = ( (*p)[0] << 8 ) | (*p)[1];
3064 *p += 2;
3065
3066 if( *p + n > end )
3067 {
3068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3069 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3070 }
3071
3072 if( ( ret = mbedtls_dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
3073 {
3074 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_read_public", ret );
3075 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3076 }
3077
3078 *p += n;
3079
3080 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
3081
3082 return( ret );
3083 }
3084 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3085 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
3086
3087 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3088 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3089 static int ssl_parse_encrypted_pms( mbedtls_ssl_context *ssl,
3090 const unsigned char *p,
3091 const unsigned char *end,
3092 size_t pms_offset )
3093 {
3094 int ret;
3095 size_t len = mbedtls_pk_get_len( mbedtls_ssl_own_key( ssl ) );
3096 unsigned char *pms = ssl->handshake->premaster + pms_offset;
3097 unsigned char ver[2];
3098 unsigned char fake_pms[48], peer_pms[48];
3099 unsigned char mask;
3100 size_t i, peer_pmslen;
3101 unsigned int diff;
3102
3103 if( ! mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_RSA ) )
3104 {
3105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
3106 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3107 }
3108
3109 /*
3110 * Decrypt the premaster using own private RSA key
3111 */
3112 #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3113 defined(MBEDTLS_SSL_PROTO_TLS1_2)
3114 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
3115 {
3116 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3117 *p++ != ( ( len ) & 0xFF ) )
3118 {
3119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3120 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3121 }
3122 }
3123 #endif
3124
3125 if( p + len != end )
3126 {
3127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3128 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3129 }
3130
3131 mbedtls_ssl_write_version( ssl->handshake->max_major_ver,
3132 ssl->handshake->max_minor_ver,
3133 ssl->conf->transport, ver );
3134
3135 /*
3136 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3137 * must not cause the connection to end immediately; instead, send a
3138 * bad_record_mac later in the handshake.
3139 * Also, avoid data-dependant branches here to protect against
3140 * timing-based variants.
3141 */
3142 ret = ssl->conf->f_rng( ssl->conf->p_rng, fake_pms, sizeof( fake_pms ) );
3143 if( ret != 0 )
3144 return( ret );
3145
3146 ret = mbedtls_pk_decrypt( mbedtls_ssl_own_key( ssl ), p, len,
3147 peer_pms, &peer_pmslen,
3148 sizeof( peer_pms ),
3149 ssl->conf->f_rng, ssl->conf->p_rng );
3150
3151 diff = (unsigned int) ret;
3152 diff |= peer_pmslen ^ 48;
3153 diff |= peer_pms[0] ^ ver[0];
3154 diff |= peer_pms[1] ^ ver[1];
3155
3156 #if defined(MBEDTLS_SSL_DEBUG_ALL)
3157 if( diff != 0 )
3158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3159 #endif
3160
3161 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
3162 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
3163 {
3164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3165 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3166 }
3167 ssl->handshake->pmslen = 48;
3168
3169 /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */
3170 /* MSVC has a warning about unary minus on unsigned, but this is
3171 * well-defined and precisely what we want to do here */
3172 #if defined(_MSC_VER)
3173 #pragma warning( push )
3174 #pragma warning( disable : 4146 )
3175 #endif
3176 mask = - ( ( diff | - diff ) >> ( sizeof( unsigned int ) * 8 - 1 ) );
3177 #if defined(_MSC_VER)
3178 #pragma warning( pop )
3179 #endif
3180
3181 for( i = 0; i < ssl->handshake->pmslen; i++ )
3182 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
3183
3184 return( 0 );
3185 }
3186 #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
3187 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
3188
3189 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
3190 static int ssl_parse_client_psk_identity( mbedtls_ssl_context *ssl, unsigned char **p,
3191 const unsigned char *end )
3192 {
3193 int ret = 0;
3194 size_t n;
3195
3196 if( ssl->conf->f_psk == NULL &&
3197 ( ssl->conf->psk == NULL || ssl->conf->psk_identity == NULL ||
3198 ssl->conf->psk_identity_len == 0 || ssl->conf->psk_len == 0 ) )
3199 {
3200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3201 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3202 }
3203
3204 /*
3205 * Receive client pre-shared key identity name
3206 */
3207 if( *p + 2 > end )
3208 {
3209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3210 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3211 }
3212
3213 n = ( (*p)[0] << 8 ) | (*p)[1];
3214 *p += 2;
3215
3216 if( n < 1 || n > 65535 || *p + n > end )
3217 {
3218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3219 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3220 }
3221
3222 if( ssl->conf->f_psk != NULL )
3223 {
3224 if( ssl->conf->f_psk( ssl->conf->p_psk, ssl, *p, n ) != 0 )
3225 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
3226 }
3227 else
3228 {
3229 /* Identity is not a big secret since clients send it in the clear,
3230 * but treat it carefully anyway, just in case */
3231 if( n != ssl->conf->psk_identity_len ||
3232 mbedtls_ssl_safer_memcmp( ssl->conf->psk_identity, *p, n ) != 0 )
3233 {
3234 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
3235 }
3236 }
3237
3238 if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY )
3239 {
3240 MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
3241 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
3242 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3243 MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
3244 {
3245 return( ret );
3246 }
3247
3248 return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
3249 }
3250
3251 *p += n;
3252
3253 return( 0 );
3254 }
3255 #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
3256
3257 static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl )
3258 {
3259 int ret;
3260 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3261 unsigned char *p, *end;
3262
3263 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3264
3265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
3266
3267 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
3268 {
3269 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
3270 return( ret );
3271 }
3272
3273 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
3274 end = ssl->in_msg + ssl->in_hslen;
3275
3276 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
3277 {
3278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3279 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3280 }
3281
3282 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE )
3283 {
3284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3285 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3286 }
3287
3288 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
3289 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
3290 {
3291 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3292 {
3293 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3294 return( ret );
3295 }
3296
3297 if( p != end )
3298 {
3299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3300 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3301 }
3302
3303 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
3304 ssl->handshake->premaster,
3305 MBEDTLS_PREMASTER_SIZE,
3306 &ssl->handshake->pmslen,
3307 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
3308 {
3309 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
3310 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3311 }
3312
3313 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
3314 }
3315 else
3316 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
3317 #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3318 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3319 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3320 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3321 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
3322 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
3323 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
3324 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
3325 {
3326 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
3327 p, end - p) ) != 0 )
3328 {
3329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
3330 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3331 }
3332
3333 MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3334
3335 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3336 &ssl->handshake->pmslen,
3337 ssl->handshake->premaster,
3338 MBEDTLS_MPI_MAX_SIZE,
3339 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
3340 {
3341 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
3342 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3343 }
3344
3345 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
3346 }
3347 else
3348 #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3349 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3350 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3351 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
3352 #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3353 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
3354 {
3355 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3356 {
3357 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3358 return( ret );
3359 }
3360
3361 if( p != end )
3362 {
3363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3364 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3365 }
3366
3367 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
3368 ciphersuite_info->key_exchange ) ) != 0 )
3369 {
3370 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
3371 return( ret );
3372 }
3373 }
3374 else
3375 #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
3376 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3377 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
3378 {
3379 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3380 {
3381 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3382 return( ret );
3383 }
3384
3385 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3386 {
3387 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3388 return( ret );
3389 }
3390
3391 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
3392 ciphersuite_info->key_exchange ) ) != 0 )
3393 {
3394 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
3395 return( ret );
3396 }
3397 }
3398 else
3399 #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
3400 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3401 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
3402 {
3403 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3404 {
3405 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3406 return( ret );
3407 }
3408 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3409 {
3410 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3411 return( ret );
3412 }
3413
3414 if( p != end )
3415 {
3416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3417 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3418 }
3419
3420 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
3421 ciphersuite_info->key_exchange ) ) != 0 )
3422 {
3423 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
3424 return( ret );
3425 }
3426 }
3427 else
3428 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
3429 #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3430 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
3431 {
3432 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3433 {
3434 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3435 return( ret );
3436 }
3437
3438 if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx,
3439 p, end - p ) ) != 0 )
3440 {
3441 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_read_public", ret );
3442 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3443 }
3444
3445 MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3446
3447 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
3448 ciphersuite_info->key_exchange ) ) != 0 )
3449 {
3450 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
3451 return( ret );
3452 }
3453 }
3454 else
3455 #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
3456 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3457 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
3458 {
3459 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
3460 {
3461 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
3462 return( ret );
3463 }
3464 }
3465 else
3466 #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
3467 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3468 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3469 {
3470 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
3471 p, end - p );
3472 if( ret != 0 )
3473 {
3474 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
3475 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
3476 }
3477
3478 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
3479 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3480 ssl->conf->f_rng, ssl->conf->p_rng );
3481 if( ret != 0 )
3482 {
3483 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
3484 return( ret );
3485 }
3486 }
3487 else
3488 #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
3489 {
3490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3491 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3492 }
3493
3494 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
3495 {
3496 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
3497 return( ret );
3498 }
3499
3500 ssl->state++;
3501
3502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3503
3504 return( 0 );
3505 }
3506
3507 #if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
3508 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3509 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3510 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
3511 static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
3512 {
3513 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3514
3515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3516
3517 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
3518 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
3519 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
3520 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
3521 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
3522 {
3523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3524 ssl->state++;
3525 return( 0 );
3526 }
3527
3528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3529 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3530 }
3531 #else
3532 static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl )
3533 {
3534 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3535 size_t i, sig_len;
3536 unsigned char hash[48];
3537 unsigned char *hash_start = hash;
3538 size_t hashlen;
3539 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3540 mbedtls_pk_type_t pk_alg;
3541 #endif
3542 mbedtls_md_type_t md_alg;
3543 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3544
3545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3546
3547 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
3548 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
3549 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
3550 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
3551 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ||
3552 ssl->session_negotiate->peer_cert == NULL )
3553 {
3554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3555 ssl->state++;
3556 return( 0 );
3557 }
3558
3559 /* Needs to be done before read_record() to exclude current message */
3560 ssl->handshake->calc_verify( ssl, hash );
3561
3562 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
3563 {
3564 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
3565 return( ret );
3566 }
3567
3568 ssl->state++;
3569
3570 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
3571 ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY )
3572 {
3573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3574 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3575 }
3576
3577 i = mbedtls_ssl_hs_hdr_len( ssl );
3578
3579 /*
3580 * struct {
3581 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
3582 * opaque signature<0..2^16-1>;
3583 * } DigitallySigned;
3584 */
3585 #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3586 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3587 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
3588 {
3589 md_alg = MBEDTLS_MD_NONE;
3590 hashlen = 36;
3591
3592 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3593 if( mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3594 MBEDTLS_PK_ECDSA ) )
3595 {
3596 hash_start += 16;
3597 hashlen -= 16;
3598 md_alg = MBEDTLS_MD_SHA1;
3599 }
3600 }
3601 else
3602 #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 ||
3603 MBEDTLS_SSL_PROTO_TLS1_1 */
3604 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3605 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
3606 {
3607 if( i + 2 > ssl->in_hslen )
3608 {
3609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3610 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3611 }
3612
3613 /*
3614 * Hash
3615 */
3616 if( ssl->in_msg[i] != ssl->handshake->verify_sig_alg )
3617 {
3618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3619 " for verify message" ) );
3620 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3621 }
3622
3623 md_alg = mbedtls_ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
3624
3625 /* Info from md_alg will be used instead */
3626 hashlen = 0;
3627
3628 i++;
3629
3630 /*
3631 * Signature
3632 */
3633 if( ( pk_alg = mbedtls_ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
3634 == MBEDTLS_PK_NONE )
3635 {
3636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3637 " for verify message" ) );
3638 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3639 }
3640
3641 /*
3642 * Check the certificate's key type matches the signature alg
3643 */
3644 if( ! mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3645 {
3646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3647 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3648 }
3649
3650 i++;
3651 }
3652 else
3653 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3654 {
3655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3656 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3657 }
3658
3659 if( i + 2 > ssl->in_hslen )
3660 {
3661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3662 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3663 }
3664
3665 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
3666 i += 2;
3667
3668 if( i + sig_len != ssl->in_hslen )
3669 {
3670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3671 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3672 }
3673
3674 if( ( ret = mbedtls_pk_verify( &ssl->session_negotiate->peer_cert->pk,
3675 md_alg, hash_start, hashlen,
3676 ssl->in_msg + i, sig_len ) ) != 0 )
3677 {
3678 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
3679 return( ret );
3680 }
3681
3682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3683
3684 return( ret );
3685 }
3686 #endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED &&
3687 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3688 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
3689
3690 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
3691 static int ssl_write_new_session_ticket( mbedtls_ssl_context *ssl )
3692 {
3693 int ret;
3694 size_t tlen;
3695 uint32_t lifetime;
3696
3697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3698
3699 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3700 ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET;
3701
3702 /*
3703 * struct {
3704 * uint32 ticket_lifetime_hint;
3705 * opaque ticket<0..2^16-1>;
3706 * } NewSessionTicket;
3707 *
3708 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3709 * 8 . 9 ticket_len (n)
3710 * 10 . 9+n ticket content
3711 */
3712
3713 if( ( ret = ssl->conf->f_ticket_write( ssl->conf->p_ticket,
3714 ssl->session_negotiate,
3715 ssl->out_msg + 10,
3716 ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN,
3717 &tlen, &lifetime ) ) != 0 )
3718 {
3719 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_ticket_write", ret );
3720 tlen = 0;
3721 }
3722
3723 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3724 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3725 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3726 ssl->out_msg[7] = ( lifetime ) & 0xFF;
3727
3728 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3729 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
3730
3731 ssl->out_msglen = 10 + tlen;
3732
3733 /*
3734 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3735 * ChangeCipherSpec share the same state.
3736 */
3737 ssl->handshake->new_session_ticket = 0;
3738
3739 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
3740 {
3741 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
3742 return( ret );
3743 }
3744
3745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3746
3747 return( 0 );
3748 }
3749 #endif /* MBEDTLS_SSL_SESSION_TICKETS */
3750
3751 /*
3752 * SSL handshake -- server side -- single step
3753 */
3754 int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl )
3755 {
3756 int ret = 0;
3757
3758 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
3759 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3760
3761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3762
3763 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3764 return( ret );
3765
3766 #if defined(MBEDTLS_SSL_PROTO_DTLS)
3767 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3768 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3769 {
3770 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
3771 return( ret );
3772 }
3773 #endif
3774
3775 switch( ssl->state )
3776 {
3777 case MBEDTLS_SSL_HELLO_REQUEST:
3778 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3779 break;
3780
3781 /*
3782 * <== ClientHello
3783 */
3784 case MBEDTLS_SSL_CLIENT_HELLO:
3785 ret = ssl_parse_client_hello( ssl );
3786 break;
3787
3788 #if defined(MBEDTLS_SSL_PROTO_DTLS)
3789 case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
3790 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3791 #endif
3792
3793 /*
3794 * ==> ServerHello
3795 * Certificate
3796 * ( ServerKeyExchange )
3797 * ( CertificateRequest )
3798 * ServerHelloDone
3799 */
3800 case MBEDTLS_SSL_SERVER_HELLO:
3801 ret = ssl_write_server_hello( ssl );
3802 break;
3803
3804 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3805 ret = mbedtls_ssl_write_certificate( ssl );
3806 break;
3807
3808 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
3809 ret = ssl_write_server_key_exchange( ssl );
3810 break;
3811
3812 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
3813 ret = ssl_write_certificate_request( ssl );
3814 break;
3815
3816 case MBEDTLS_SSL_SERVER_HELLO_DONE:
3817 ret = ssl_write_server_hello_done( ssl );
3818 break;
3819
3820 /*
3821 * <== ( Certificate/Alert )
3822 * ClientKeyExchange
3823 * ( CertificateVerify )
3824 * ChangeCipherSpec
3825 * Finished
3826 */
3827 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3828 ret = mbedtls_ssl_parse_certificate( ssl );
3829 break;
3830
3831 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
3832 ret = ssl_parse_client_key_exchange( ssl );
3833 break;
3834
3835 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
3836 ret = ssl_parse_certificate_verify( ssl );
3837 break;
3838
3839 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3840 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
3841 break;
3842
3843 case MBEDTLS_SSL_CLIENT_FINISHED:
3844 ret = mbedtls_ssl_parse_finished( ssl );
3845 break;
3846
3847 /*
3848 * ==> ( NewSessionTicket )
3849 * ChangeCipherSpec
3850 * Finished
3851 */
3852 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3853 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
3854 if( ssl->handshake->new_session_ticket != 0 )
3855 ret = ssl_write_new_session_ticket( ssl );
3856 else
3857 #endif
3858 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
3859 break;
3860
3861 case MBEDTLS_SSL_SERVER_FINISHED:
3862 ret = mbedtls_ssl_write_finished( ssl );
3863 break;
3864
3865 case MBEDTLS_SSL_FLUSH_BUFFERS:
3866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3867 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
3868 break;
3869
3870 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3871 mbedtls_ssl_handshake_wrapup( ssl );
3872 break;
3873
3874 default:
3875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3876 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3877 }
3878
3879 return( ret );
3880 }
3881 #endif /* MBEDTLS_SSL_SRV_C */