2 * SSLv3/TLSv1 client-side functions
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
25 #include MBEDTLS_CONFIG_FILE
28 #if defined(MBEDTLS_SSL_CLI_C)
30 #if defined(MBEDTLS_PLATFORM_C)
31 #include "mbedtls/platform.h"
34 #define mbedtls_calloc calloc
35 #define mbedtls_free free
38 #include "mbedtls/debug.h"
39 #include "mbedtls/ssl.h"
40 #include "mbedtls/ssl_internal.h"
46 #if defined(MBEDTLS_HAVE_TIME)
47 #include "mbedtls/platform_time.h"
50 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
51 /* Implementation that should never be optimized out by the compiler */
52 static void mbedtls_zeroize( void *v, size_t n ) {
53 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
57 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
58 static void ssl_write_hostname_ext( mbedtls_ssl_context *ssl,
62 unsigned char *p = buf;
63 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
68 if( ssl->hostname == NULL )
71 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding server name extension: %s",
74 hostname_len = strlen( ssl->hostname );
76 if( end < p || (size_t)( end - p ) < hostname_len + 9 )
78 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
85 * select (name_type) {
86 * case host_name: HostName;
94 * opaque HostName<1..2^16-1>;
97 * ServerName server_name_list<1..2^16-1>
100 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME >> 8 ) & 0xFF );
101 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME ) & 0xFF );
103 *p++ = (unsigned char)( ( (hostname_len + 5) >> 8 ) & 0xFF );
104 *p++ = (unsigned char)( ( (hostname_len + 5) ) & 0xFF );
106 *p++ = (unsigned char)( ( (hostname_len + 3) >> 8 ) & 0xFF );
107 *p++ = (unsigned char)( ( (hostname_len + 3) ) & 0xFF );
109 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ) & 0xFF );
110 *p++ = (unsigned char)( ( hostname_len >> 8 ) & 0xFF );
111 *p++ = (unsigned char)( ( hostname_len ) & 0xFF );
113 memcpy( p, ssl->hostname, hostname_len );
115 *olen = hostname_len + 9;
117 #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
119 #if defined(MBEDTLS_SSL_RENEGOTIATION)
120 static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
124 unsigned char *p = buf;
125 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
129 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
132 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding renegotiation extension" ) );
134 if( end < p || (size_t)( end - p ) < 5 + ssl->verify_data_len )
136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
141 * Secure renegotiation
143 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
144 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
147 *p++ = ( ssl->verify_data_len + 1 ) & 0xFF;
148 *p++ = ssl->verify_data_len & 0xFF;
150 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
152 *olen = 5 + ssl->verify_data_len;
154 #endif /* MBEDTLS_SSL_RENEGOTIATION */
157 * Only if we handle at least one key exchange that needs signatures.
159 #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
160 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
161 static void ssl_write_signature_algorithms_ext( mbedtls_ssl_context *ssl,
165 unsigned char *p = buf;
166 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
167 size_t sig_alg_len = 0;
169 #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C)
170 unsigned char *sig_alg_list = buf + 6;
175 if( ssl->conf->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
178 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding signature_algorithms extension" ) );
180 for( md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
182 #if defined(MBEDTLS_ECDSA_C)
185 #if defined(MBEDTLS_RSA_C)
190 if( end < p || (size_t)( end - p ) < sig_alg_len + 6 )
192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
197 * Prepare signature_algorithms extension (TLS 1.2)
201 for( md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
203 #if defined(MBEDTLS_ECDSA_C)
204 sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg( *md );
205 sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_ECDSA;
207 #if defined(MBEDTLS_RSA_C)
208 sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg( *md );
209 sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_RSA;
215 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
219 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
220 * SignatureAlgorithm;
223 * HashAlgorithm hash;
224 * SignatureAlgorithm signature;
225 * } SignatureAndHashAlgorithm;
227 * SignatureAndHashAlgorithm
228 * supported_signature_algorithms<2..2^16-2>;
230 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SIG_ALG >> 8 ) & 0xFF );
231 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SIG_ALG ) & 0xFF );
233 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF );
234 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF );
236 *p++ = (unsigned char)( ( sig_alg_len >> 8 ) & 0xFF );
237 *p++ = (unsigned char)( ( sig_alg_len ) & 0xFF );
239 *olen = 6 + sig_alg_len;
241 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
242 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
244 #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
245 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
246 static void ssl_write_supported_elliptic_curves_ext( mbedtls_ssl_context *ssl,
250 unsigned char *p = buf;
251 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
252 unsigned char *elliptic_curve_list = p + 6;
253 size_t elliptic_curve_len = 0;
254 const mbedtls_ecp_curve_info *info;
255 #if defined(MBEDTLS_ECP_C)
256 const mbedtls_ecp_group_id *grp_id;
263 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_elliptic_curves extension" ) );
265 #if defined(MBEDTLS_ECP_C)
266 for( grp_id = ssl->conf->curve_list; *grp_id != MBEDTLS_ECP_DP_NONE; grp_id++ )
268 info = mbedtls_ecp_curve_info_from_grp_id( *grp_id );
270 for( info = mbedtls_ecp_curve_list(); info->grp_id != MBEDTLS_ECP_DP_NONE; info++ )
275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid curve in ssl configuration" ) );
279 elliptic_curve_len += 2;
282 if( end < p || (size_t)( end - p ) < 6 + elliptic_curve_len )
284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
288 elliptic_curve_len = 0;
290 #if defined(MBEDTLS_ECP_C)
291 for( grp_id = ssl->conf->curve_list; *grp_id != MBEDTLS_ECP_DP_NONE; grp_id++ )
293 info = mbedtls_ecp_curve_info_from_grp_id( *grp_id );
295 for( info = mbedtls_ecp_curve_list(); info->grp_id != MBEDTLS_ECP_DP_NONE; info++ )
298 elliptic_curve_list[elliptic_curve_len++] = info->tls_id >> 8;
299 elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF;
302 if( elliptic_curve_len == 0 )
305 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) & 0xFF );
306 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) & 0xFF );
308 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
309 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
311 *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
312 *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
314 *olen = 6 + elliptic_curve_len;
317 static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
321 unsigned char *p = buf;
322 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
326 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_point_formats extension" ) );
328 if( end < p || (size_t)( end - p ) < 6 )
330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
334 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
335 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
341 *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
345 #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
346 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
348 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
349 static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
354 unsigned char *p = buf;
355 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
360 /* Skip costly extension if we can't use EC J-PAKE anyway */
361 if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
364 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding ecjpake_kkpp extension" ) );
368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
372 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP >> 8 ) & 0xFF );
373 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ) & 0xFF );
376 * We may need to send ClientHello multiple times for Hello verification.
377 * We don't want to compute fresh values every time (both for performance
378 * and consistency reasons), so cache the extension content.
380 if( ssl->handshake->ecjpake_cache == NULL ||
381 ssl->handshake->ecjpake_cache_len == 0 )
383 MBEDTLS_SSL_DEBUG_MSG( 3, ( "generating new ecjpake parameters" ) );
385 ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
386 p + 2, end - p - 2, &kkpp_len,
387 ssl->conf->f_rng, ssl->conf->p_rng );
390 MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret );
394 ssl->handshake->ecjpake_cache = mbedtls_calloc( 1, kkpp_len );
395 if( ssl->handshake->ecjpake_cache == NULL )
397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "allocation failed" ) );
401 memcpy( ssl->handshake->ecjpake_cache, p + 2, kkpp_len );
402 ssl->handshake->ecjpake_cache_len = kkpp_len;
406 MBEDTLS_SSL_DEBUG_MSG( 3, ( "re-using cached ecjpake parameters" ) );
408 kkpp_len = ssl->handshake->ecjpake_cache_len;
410 if( (size_t)( end - p - 2 ) < kkpp_len )
412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
416 memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len );
419 *p++ = (unsigned char)( ( kkpp_len >> 8 ) & 0xFF );
420 *p++ = (unsigned char)( ( kkpp_len ) & 0xFF );
422 *olen = kkpp_len + 4;
424 #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
426 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
427 static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
431 unsigned char *p = buf;
432 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
436 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ) {
440 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding max_fragment_length extension" ) );
442 if( end < p || (size_t)( end - p ) < 5 )
444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
448 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
449 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
454 *p++ = ssl->conf->mfl_code;
458 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
460 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
461 static void ssl_write_truncated_hmac_ext( mbedtls_ssl_context *ssl,
462 unsigned char *buf, size_t *olen )
464 unsigned char *p = buf;
465 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
469 if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED )
474 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding truncated_hmac extension" ) );
476 if( end < p || (size_t)( end - p ) < 4 )
478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
482 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
483 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
490 #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
492 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
493 static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
494 unsigned char *buf, size_t *olen )
496 unsigned char *p = buf;
497 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
501 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
502 ssl->conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
507 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding encrypt_then_mac "
510 if( end < p || (size_t)( end - p ) < 4 )
512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
516 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
517 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
524 #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
526 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
527 static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
528 unsigned char *buf, size_t *olen )
530 unsigned char *p = buf;
531 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
535 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
536 ssl->conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
541 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding extended_master_secret "
544 if( end < p || (size_t)( end - p ) < 4 )
546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
550 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
551 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
558 #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
560 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
561 static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
562 unsigned char *buf, size_t *olen )
564 unsigned char *p = buf;
565 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
566 size_t tlen = ssl->session_negotiate->ticket_len;
570 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED )
575 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) );
577 if( end < p || (size_t)( end - p ) < 4 + tlen )
579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
583 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
584 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET ) & 0xFF );
586 *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
587 *p++ = (unsigned char)( ( tlen ) & 0xFF );
591 if( ssl->session_negotiate->ticket == NULL || tlen == 0 )
596 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) );
598 memcpy( p, ssl->session_negotiate->ticket, tlen );
602 #endif /* MBEDTLS_SSL_SESSION_TICKETS */
604 #if defined(MBEDTLS_SSL_ALPN)
605 static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
606 unsigned char *buf, size_t *olen )
608 unsigned char *p = buf;
609 const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
615 if( ssl->conf->alpn_list == NULL )
620 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
622 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
623 alpnlen += (unsigned char)( strlen( *cur ) & 0xFF ) + 1;
625 if( end < p || (size_t)( end - p ) < 6 + alpnlen )
627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
631 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN >> 8 ) & 0xFF );
632 *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN ) & 0xFF );
635 * opaque ProtocolName<1..2^8-1>;
638 * ProtocolName protocol_name_list<2..2^16-1>
639 * } ProtocolNameList;
642 /* Skip writing extension and list length for now */
645 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
647 *p = (unsigned char)( strlen( *cur ) & 0xFF );
648 memcpy( p + 1, *cur, *p );
654 /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
655 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
656 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
658 /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
659 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
660 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
662 #endif /* MBEDTLS_SSL_ALPN */
665 * Generate random bytes for ClientHello
667 static int ssl_generate_random( mbedtls_ssl_context *ssl )
670 unsigned char *p = ssl->handshake->randbytes;
671 #if defined(MBEDTLS_HAVE_TIME)
676 * When responding to a verify request, MUST reuse random (RFC 6347 4.2.1)
678 #if defined(MBEDTLS_SSL_PROTO_DTLS)
679 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
680 ssl->handshake->verify_cookie != NULL )
686 #if defined(MBEDTLS_HAVE_TIME)
687 t = mbedtls_time( NULL );
688 *p++ = (unsigned char)( t >> 24 );
689 *p++ = (unsigned char)( t >> 16 );
690 *p++ = (unsigned char)( t >> 8 );
691 *p++ = (unsigned char)( t );
693 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
695 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
699 #endif /* MBEDTLS_HAVE_TIME */
701 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
707 static int ssl_write_client_hello( mbedtls_ssl_context *ssl )
710 size_t i, n, olen, ext_len = 0;
712 unsigned char *p, *q;
713 unsigned char offer_compress;
714 const int *ciphersuites;
715 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
719 if( ssl->conf->f_rng == NULL )
721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
722 return( MBEDTLS_ERR_SSL_NO_RNG );
725 #if defined(MBEDTLS_SSL_RENEGOTIATION)
726 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
729 ssl->major_ver = ssl->conf->min_major_ver;
730 ssl->minor_ver = ssl->conf->min_minor_ver;
733 if( ssl->conf->max_major_ver == 0 )
735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "configured max major version is invalid, "
736 "consider using mbedtls_ssl_config_defaults()" ) );
737 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
741 * 0 . 0 handshake type
742 * 1 . 3 handshake length
743 * 4 . 5 highest version supported
744 * 6 . 9 current UNIX time
745 * 10 . 37 random bytes
750 mbedtls_ssl_write_version( ssl->conf->max_major_ver, ssl->conf->max_minor_ver,
751 ssl->conf->transport, p );
754 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
757 if( ( ret = ssl_generate_random( ssl ) ) != 0 )
759 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
763 memcpy( p, ssl->handshake->randbytes, 32 );
764 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", p, 32 );
768 * 38 . 38 session id length
769 * 39 . 39+n session id
770 * 39+n . 39+n DTLS only: cookie length (1 byte)
771 * 40+n . .. DTSL only: cookie
772 * .. . .. ciphersuitelist length (2 bytes)
773 * .. . .. ciphersuitelist
774 * .. . .. compression methods length (1 byte)
775 * .. . .. compression methods
776 * .. . .. extensions length (2 bytes)
779 n = ssl->session_negotiate->id_len;
781 if( n < 16 || n > 32 ||
782 #if defined(MBEDTLS_SSL_RENEGOTIATION)
783 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
785 ssl->handshake->resume == 0 )
790 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
792 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
793 * generate and include a Session ID in the TLS ClientHello."
795 #if defined(MBEDTLS_SSL_RENEGOTIATION)
796 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
799 if( ssl->session_negotiate->ticket != NULL &&
800 ssl->session_negotiate->ticket_len != 0 )
802 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->session_negotiate->id, 32 );
807 ssl->session_negotiate->id_len = n = 32;
810 #endif /* MBEDTLS_SSL_SESSION_TICKETS */
812 *p++ = (unsigned char) n;
814 for( i = 0; i < n; i++ )
815 *p++ = ssl->session_negotiate->id[i];
817 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
818 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
823 #if defined(MBEDTLS_SSL_PROTO_DTLS)
824 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
826 if( ssl->handshake->verify_cookie == NULL )
828 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no verify cookie to send" ) );
833 MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
834 ssl->handshake->verify_cookie,
835 ssl->handshake->verify_cookie_len );
837 *p++ = ssl->handshake->verify_cookie_len;
838 memcpy( p, ssl->handshake->verify_cookie,
839 ssl->handshake->verify_cookie_len );
840 p += ssl->handshake->verify_cookie_len;
848 ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
850 /* Skip writing ciphersuite length for now */
855 for( i = 0; ciphersuites[i] != 0; i++ )
857 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] );
859 if( ciphersuite_info == NULL )
862 if( ciphersuite_info->min_minor_ver > ssl->conf->max_minor_ver ||
863 ciphersuite_info->max_minor_ver < ssl->conf->min_minor_ver )
866 #if defined(MBEDTLS_SSL_PROTO_DTLS)
867 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
868 ( ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
872 #if defined(MBEDTLS_ARC4_C)
873 if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED &&
874 ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
878 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
879 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
880 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
884 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x",
888 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
889 *p++ = (unsigned char)( ciphersuites[i] );
893 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
895 #if defined(MBEDTLS_SSL_RENEGOTIATION)
896 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
899 *p++ = (unsigned char)( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
900 *p++ = (unsigned char)( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO );
904 /* Some versions of OpenSSL don't handle it correctly if not at end */
905 #if defined(MBEDTLS_SSL_FALLBACK_SCSV)
906 if( ssl->conf->fallback == MBEDTLS_SSL_IS_FALLBACK )
908 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) );
909 *p++ = (unsigned char)( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 );
910 *p++ = (unsigned char)( MBEDTLS_SSL_FALLBACK_SCSV_VALUE );
915 *q++ = (unsigned char)( n >> 7 );
916 *q++ = (unsigned char)( n << 1 );
918 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
920 #if defined(MBEDTLS_ZLIB_SUPPORT)
927 * We don't support compression with DTLS right now: is many records come
928 * in the same datagram, uncompressing one could overwrite the next one.
929 * We don't want to add complexity for handling that case unless there is
930 * an actual need for it.
932 #if defined(MBEDTLS_SSL_PROTO_DTLS)
933 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
939 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
940 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
941 MBEDTLS_SSL_COMPRESS_DEFLATE, MBEDTLS_SSL_COMPRESS_NULL ) );
944 *p++ = MBEDTLS_SSL_COMPRESS_DEFLATE;
945 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
949 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
950 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d",
951 MBEDTLS_SSL_COMPRESS_NULL ) );
954 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
957 // First write extensions, then the total length
959 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
960 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
964 #if defined(MBEDTLS_SSL_RENEGOTIATION)
965 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
969 #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
970 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
971 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
975 #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
976 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
977 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
980 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
984 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
985 ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen );
989 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
990 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
994 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
995 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
999 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1000 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
1004 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1005 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
1009 #if defined(MBEDTLS_SSL_ALPN)
1010 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
1014 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
1015 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1019 /* olen unused if all extensions are disabled */
1022 MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
1027 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1028 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1032 ssl->out_msglen = p - buf;
1033 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
1034 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_HELLO;
1038 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1039 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1040 mbedtls_ssl_send_flight_completed( ssl );
1043 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
1045 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
1049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
1054 static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
1055 const unsigned char *buf,
1060 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1061 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
1063 /* Check verify-data in constant-time. The length OTOH is no secret */
1064 if( len != 1 + ssl->verify_data_len * 2 ||
1065 buf[0] != ssl->verify_data_len * 2 ||
1066 mbedtls_ssl_safer_memcmp( buf + 1,
1067 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
1068 mbedtls_ssl_safer_memcmp( buf + 1 + ssl->verify_data_len,
1069 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
1071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
1073 if( ( ret = mbedtls_ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1076 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1080 #endif /* MBEDTLS_SSL_RENEGOTIATION */
1082 if( len != 1 || buf[0] != 0x00 )
1084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
1086 if( ( ret = mbedtls_ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1089 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1092 ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
1098 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1099 static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
1100 const unsigned char *buf,
1104 * server should use the extension only if we did,
1105 * and if so the server's value should match ours (and len is always 1)
1107 if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
1109 buf[0] != ssl->conf->mfl_code )
1111 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1116 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
1118 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1119 static int ssl_parse_truncated_hmac_ext( mbedtls_ssl_context *ssl,
1120 const unsigned char *buf,
1123 if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED ||
1126 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1131 ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED;
1135 #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1137 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1138 static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
1139 const unsigned char *buf,
1142 if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
1143 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1146 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1151 ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
1155 #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
1157 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1158 static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
1159 const unsigned char *buf,
1162 if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
1163 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1166 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1171 ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
1175 #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1177 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
1178 static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
1179 const unsigned char *buf,
1182 if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
1185 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1190 ssl->handshake->new_session_ticket = 1;
1194 #endif /* MBEDTLS_SSL_SESSION_TICKETS */
1196 #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
1197 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1198 static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl,
1199 const unsigned char *buf,
1203 const unsigned char *p;
1206 if( list_size + 1 != len )
1208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1209 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1213 while( list_size > 0 )
1215 if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
1216 p[0] == MBEDTLS_ECP_PF_COMPRESSED )
1218 #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
1219 ssl->handshake->ecdh_ctx.point_format = p[0];
1221 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1222 ssl->handshake->ecjpake_ctx.point_format = p[0];
1224 MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
1232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
1233 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1235 #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1236 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1238 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1239 static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
1240 const unsigned char *buf,
1245 if( ssl->transform_negotiate->ciphersuite_info->key_exchange !=
1246 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
1248 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
1252 /* If we got here, we no longer need our cached extension */
1253 mbedtls_free( ssl->handshake->ecjpake_cache );
1254 ssl->handshake->ecjpake_cache = NULL;
1255 ssl->handshake->ecjpake_cache_len = 0;
1257 if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
1260 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
1266 #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1268 #if defined(MBEDTLS_SSL_ALPN)
1269 static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
1270 const unsigned char *buf, size_t len )
1272 size_t list_len, name_len;
1275 /* If we didn't send it, the server shouldn't send it */
1276 if( ssl->conf->alpn_list == NULL )
1277 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1280 * opaque ProtocolName<1..2^8-1>;
1283 * ProtocolName protocol_name_list<2..2^16-1>
1284 * } ProtocolNameList;
1286 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
1289 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
1291 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1293 list_len = ( buf[0] << 8 ) | buf[1];
1294 if( list_len != len - 2 )
1295 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1298 if( name_len != list_len - 1 )
1299 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1301 /* Check that the server chosen protocol was in our list and save it */
1302 for( p = ssl->conf->alpn_list; *p != NULL; p++ )
1304 if( name_len == strlen( *p ) &&
1305 memcmp( buf + 3, *p, name_len ) == 0 )
1307 ssl->alpn_chosen = *p;
1312 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1314 #endif /* MBEDTLS_SSL_ALPN */
1317 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1319 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1320 static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl )
1322 const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
1323 int major_ver, minor_ver;
1324 unsigned char cookie_len;
1326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
1330 * ProtocolVersion server_version;
1331 * opaque cookie<0..2^8-1>;
1332 * } HelloVerifyRequest;
1334 MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
1335 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, p );
1339 * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1)
1340 * even is lower than our min version.
1342 if( major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
1343 minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ||
1344 major_ver > ssl->conf->max_major_ver ||
1345 minor_ver > ssl->conf->max_minor_ver )
1347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) );
1349 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1350 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
1352 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1356 MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len );
1358 if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len )
1360 MBEDTLS_SSL_DEBUG_MSG( 1,
1361 ( "cookie length does not match incoming message size" ) );
1362 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1363 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
1364 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1367 mbedtls_free( ssl->handshake->verify_cookie );
1369 ssl->handshake->verify_cookie = mbedtls_calloc( 1, cookie_len );
1370 if( ssl->handshake->verify_cookie == NULL )
1372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) );
1373 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1376 memcpy( ssl->handshake->verify_cookie, p, cookie_len );
1377 ssl->handshake->verify_cookie_len = cookie_len;
1379 /* Start over at ClientHello */
1380 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
1381 mbedtls_ssl_reset_checksum( ssl );
1383 mbedtls_ssl_recv_flight_completed( ssl );
1385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
1389 #endif /* MBEDTLS_SSL_PROTO_DTLS */
1391 static int ssl_parse_server_hello( mbedtls_ssl_context *ssl )
1396 unsigned char *buf, *ext;
1398 #if defined(MBEDTLS_ZLIB_SUPPORT)
1401 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1402 int renegotiation_info_seen = 0;
1404 int handshake_failure = 0;
1405 const mbedtls_ssl_ciphersuite_t *suite_info;
1406 #if defined(MBEDTLS_DEBUG_C)
1410 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
1414 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
1416 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
1420 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
1422 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1423 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
1425 ssl->renego_records_seen++;
1427 if( ssl->conf->renego_max_records >= 0 &&
1428 ssl->renego_records_seen > ssl->conf->renego_max_records )
1430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
1431 "but not honored by server" ) );
1432 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
1436 return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
1438 #endif /* MBEDTLS_SSL_RENEGOTIATION */
1440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1441 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
1444 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1445 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1447 if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
1449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
1450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1451 return( ssl_parse_hello_verify_request( ssl ) );
1455 /* We made it through the verification process */
1456 mbedtls_free( ssl->handshake->verify_cookie );
1457 ssl->handshake->verify_cookie = NULL;
1458 ssl->handshake->verify_cookie_len = 0;
1461 #endif /* MBEDTLS_SSL_PROTO_DTLS */
1463 if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) ||
1464 buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO )
1466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1467 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1471 * 0 . 1 server_version
1472 * 2 . 33 random (maybe including 4 bytes of Unix time)
1473 * 34 . 34 session_id length = n
1474 * 35 . 34+n session_id
1475 * 35+n . 36+n cipher_suite
1476 * 37+n . 37+n compression_method
1478 * 38+n . 39+n extensions length (optional)
1479 * 40+n . .. extensions
1481 buf += mbedtls_ssl_hs_hdr_len( ssl );
1483 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 );
1484 mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
1485 ssl->conf->transport, buf + 0 );
1487 if( ssl->major_ver < ssl->conf->min_major_ver ||
1488 ssl->minor_ver < ssl->conf->min_minor_ver ||
1489 ssl->major_ver > ssl->conf->max_major_ver ||
1490 ssl->minor_ver > ssl->conf->max_minor_ver )
1492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server version out of bounds - "
1493 " min: [%d:%d], server: [%d:%d], max: [%d:%d]",
1494 ssl->conf->min_major_ver, ssl->conf->min_minor_ver,
1495 ssl->major_ver, ssl->minor_ver,
1496 ssl->conf->max_major_ver, ssl->conf->max_minor_ver ) );
1498 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1499 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
1501 return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1504 #if defined(MBEDTLS_DEBUG_C)
1505 t = ( (uint32_t) buf[2] << 24 )
1506 | ( (uint32_t) buf[3] << 16 )
1507 | ( (uint32_t) buf[4] << 8 )
1508 | ( (uint32_t) buf[5] );
1509 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
1512 memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
1516 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
1520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1521 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1524 if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n )
1526 ext_len = ( ( buf[38 + n] << 8 )
1527 | ( buf[39 + n] ) );
1529 if( ( ext_len > 0 && ext_len < 4 ) ||
1530 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
1532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1533 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1536 else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n )
1542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1543 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1546 /* ciphersuite (used later) */
1547 i = ( buf[35 + n] << 8 ) | buf[36 + n];
1550 * Read and check compression
1554 #if defined(MBEDTLS_ZLIB_SUPPORT)
1555 /* See comments in ssl_write_client_hello() */
1556 #if defined(MBEDTLS_SSL_PROTO_DTLS)
1557 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1563 if( comp != MBEDTLS_SSL_COMPRESS_NULL &&
1564 ( comp != MBEDTLS_SSL_COMPRESS_DEFLATE || accept_comp == 0 ) )
1565 #else /* MBEDTLS_ZLIB_SUPPORT */
1566 if( comp != MBEDTLS_SSL_COMPRESS_NULL )
1567 #endif/* MBEDTLS_ZLIB_SUPPORT */
1569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server hello, bad compression: %d", comp ) );
1570 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1574 * Initialize update checksum functions
1576 ssl->transform_negotiate->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i );
1578 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
1581 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1584 mbedtls_ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1586 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1587 MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
1590 * Check if the session can be resumed
1592 if( ssl->handshake->resume == 0 || n == 0 ||
1593 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1594 ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
1596 ssl->session_negotiate->ciphersuite != i ||
1597 ssl->session_negotiate->compression != comp ||
1598 ssl->session_negotiate->id_len != n ||
1599 memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
1602 ssl->handshake->resume = 0;
1603 #if defined(MBEDTLS_HAVE_TIME)
1604 ssl->session_negotiate->start = mbedtls_time( NULL );
1606 ssl->session_negotiate->ciphersuite = i;
1607 ssl->session_negotiate->compression = comp;
1608 ssl->session_negotiate->id_len = n;
1609 memcpy( ssl->session_negotiate->id, buf + 35, n );
1613 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
1615 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
1617 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
1622 MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
1623 ssl->handshake->resume ? "a" : "no" ) );
1625 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", i ) );
1626 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[37 + n] ) );
1628 suite_info = mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite );
1629 if( suite_info == NULL
1630 #if defined(MBEDTLS_ARC4_C)
1631 || ( ssl->conf->arc4_disabled &&
1632 suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
1636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1637 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1640 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s", suite_info->name ) );
1645 if( ssl->conf->ciphersuite_list[ssl->minor_ver][i] == 0 )
1647 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1648 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1651 if( ssl->conf->ciphersuite_list[ssl->minor_ver][i++] ==
1652 ssl->session_negotiate->ciphersuite )
1658 if( comp != MBEDTLS_SSL_COMPRESS_NULL
1659 #if defined(MBEDTLS_ZLIB_SUPPORT)
1660 && comp != MBEDTLS_SSL_COMPRESS_DEFLATE
1664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1665 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1667 ssl->session_negotiate->compression = comp;
1671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1675 unsigned int ext_id = ( ( ext[0] << 8 )
1677 unsigned int ext_size = ( ( ext[2] << 8 )
1680 if( ext_size + 4 > ext_len )
1682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1683 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1688 case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1689 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1690 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1691 renegotiation_info_seen = 1;
1694 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1700 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1701 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1702 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1704 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1705 ext + 4, ext_size ) ) != 0 )
1711 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
1713 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1714 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
1715 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1717 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1718 ext + 4, ext_size ) ) != 0 )
1724 #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1726 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1727 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1728 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
1730 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1731 ext + 4, ext_size ) ) != 0 )
1737 #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
1739 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1740 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1741 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended_master_secret extension" ) );
1743 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1744 ext + 4, ext_size ) ) != 0 )
1750 #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1752 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
1753 case MBEDTLS_TLS_EXT_SESSION_TICKET:
1754 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1756 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1757 ext + 4, ext_size ) ) != 0 )
1763 #endif /* MBEDTLS_SSL_SESSION_TICKETS */
1765 #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
1766 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1767 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1768 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1770 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1771 ext + 4, ext_size ) ) != 0 )
1777 #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
1778 MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1780 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1781 case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1782 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) );
1784 if( ( ret = ssl_parse_ecjpake_kkpp( ssl,
1785 ext + 4, ext_size ) ) != 0 )
1791 #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1793 #if defined(MBEDTLS_SSL_ALPN)
1794 case MBEDTLS_TLS_EXT_ALPN:
1795 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1797 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1801 #endif /* MBEDTLS_SSL_ALPN */
1804 MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1808 ext_len -= 4 + ext_size;
1809 ext += 4 + ext_size;
1811 if( ext_len > 0 && ext_len < 4 )
1813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1814 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1819 * Renegotiation security checks
1821 if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1822 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
1824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1825 handshake_failure = 1;
1827 #if defined(MBEDTLS_SSL_RENEGOTIATION)
1828 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1829 ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
1830 renegotiation_info_seen == 0 )
1832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1833 handshake_failure = 1;
1835 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1836 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1837 ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
1839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
1840 handshake_failure = 1;
1842 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1843 ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1844 renegotiation_info_seen == 1 )
1846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1847 handshake_failure = 1;
1849 #endif /* MBEDTLS_SSL_RENEGOTIATION */
1851 if( handshake_failure == 1 )
1853 if( ( ret = mbedtls_ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1856 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
1859 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1864 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1865 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1866 static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl, unsigned char **p,
1867 unsigned char *end )
1869 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1872 * Ephemeral DH parameters:
1875 * opaque dh_p<1..2^16-1>;
1876 * opaque dh_g<1..2^16-1>;
1877 * opaque dh_Ys<1..2^16-1>;
1880 if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1882 MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret );
1886 if( ssl->handshake->dhm_ctx.len * 8 < ssl->conf->dhm_min_bitlen )
1888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %d < %d",
1889 ssl->handshake->dhm_ctx.len * 8,
1890 ssl->conf->dhm_min_bitlen ) );
1891 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1894 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1895 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1896 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
1900 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1901 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1903 #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1904 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1905 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1906 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1907 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) || \
1908 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ANON_ENABLED)
1909 static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl )
1911 const mbedtls_ecp_curve_info *curve_info;
1913 curve_info = mbedtls_ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1914 if( curve_info == NULL )
1916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1917 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
1922 #if defined(MBEDTLS_ECP_C)
1923 if( mbedtls_ssl_check_curve( ssl, ssl->handshake->ecdh_ctx.grp.id ) != 0 )
1925 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1926 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
1930 MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1934 #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1935 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1936 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1937 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1938 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED ||
1939 MBEDTLS_KEY_EXCHANGE_ECDH_ANON_ENABLED */
1941 #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1942 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1943 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1944 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ANON_ENABLED)
1945 static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
1947 unsigned char *end )
1949 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1952 * Ephemeral ECDH parameters:
1955 * ECParameters curve_params;
1957 * } ServerECDHParams;
1959 if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
1960 (const unsigned char **) p, end ) ) != 0 )
1962 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
1966 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
1969 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1974 #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1975 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1976 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1977 MBEDTLS_KEY_EXCHANGE_ECDH_ANON_ENABLED*/
1979 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1980 static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
1982 unsigned char *end )
1987 if( ssl->conf->f_psk == NULL &&
1988 ( ssl->conf->psk == NULL || ssl->conf->psk_identity == NULL ||
1989 ssl->conf->psk_identity_len == 0 || ssl->conf->psk_len == 0 ) )
1991 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
1992 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
1996 * Receive client pre-shared key identity name
2000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2001 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2004 n = ( (*p)[0] << 8 ) | (*p)[1];
2007 if( n < 1 || n > 65535 || *p + n > end )
2009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2010 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2013 if( ssl->conf->f_psk != NULL )
2015 if( ssl->conf->f_psk( ssl->conf->p_psk, ssl, *p, n ) != 0 )
2016 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
2020 /* Identity is not a big secret since clients send it in the clear,
2021 * but treat it carefully anyway, just in case */
2022 if( n != ssl->conf->psk_identity_len ||
2023 mbedtls_ssl_safer_memcmp( ssl->conf->psk_identity, *p, n ) != 0 )
2025 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
2029 if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY )
2031 MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
2032 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
2033 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2034 MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2039 return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
2046 #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2048 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
2049 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2051 * Generate a pre-master secret and encrypt it with the server's RSA key
2053 static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
2054 size_t offset, size_t *olen,
2058 size_t len_bytes = ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ? 0 : 2;
2059 unsigned char *p = ssl->handshake->premaster + pms_offset;
2061 if( offset + len_bytes > MBEDTLS_SSL_MAX_CONTENT_LEN )
2063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
2064 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2068 * Generate (part of) the pre-master as
2070 * ProtocolVersion client_version;
2071 * opaque random[46];
2072 * } PreMasterSecret;
2074 mbedtls_ssl_write_version( ssl->conf->max_major_ver, ssl->conf->max_minor_ver,
2075 ssl->conf->transport, p );
2077 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
2079 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
2083 ssl->handshake->pmslen = 48;
2085 if( ssl->session_negotiate->peer_cert == NULL )
2087 MBEDTLS_SSL_DEBUG_MSG( 2, ( "certificate required" ) );
2088 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2092 * Now write it out, encrypted
2094 if( ! mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk,
2097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
2098 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
2101 if( ( ret = mbedtls_pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
2102 p, ssl->handshake->pmslen,
2103 ssl->out_msg + offset + len_bytes, olen,
2104 MBEDTLS_SSL_MAX_CONTENT_LEN - offset - len_bytes,
2105 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
2107 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
2111 #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2112 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2113 if( len_bytes == 2 )
2115 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
2116 ssl->out_msg[offset+1] = (unsigned char)( *olen );
2123 #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
2124 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2126 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2127 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2128 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2129 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2130 static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl,
2133 mbedtls_md_type_t *md_alg,
2134 mbedtls_pk_type_t *pk_alg )
2137 *md_alg = MBEDTLS_MD_NONE;
2138 *pk_alg = MBEDTLS_PK_NONE;
2140 /* Only in TLS 1.2 */
2141 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
2146 if( (*p) + 2 > end )
2147 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2150 * Get hash algorithm
2152 if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) ) == MBEDTLS_MD_NONE )
2154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Server used unsupported "
2155 "HashAlgorithm %d", *(p)[0] ) );
2156 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2160 * Get signature algorithm
2162 if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) ) == MBEDTLS_PK_NONE )
2164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server used unsupported "
2165 "SignatureAlgorithm %d", (*p)[1] ) );
2166 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2170 * Check if the hash is acceptable
2172 if( mbedtls_ssl_check_sig_hash( ssl, *md_alg ) != 0 )
2174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server used HashAlgorithm "
2175 "that was not offered" ) );
2176 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
2180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
2185 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2186 MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2187 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
2188 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2190 #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2191 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2192 static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
2195 const mbedtls_ecp_keypair *peer_key;
2197 if( ssl->session_negotiate->peer_cert == NULL )
2199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "certificate required" ) );
2200 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2203 if( ! mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk,
2204 MBEDTLS_PK_ECKEY ) )
2206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2207 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
2210 peer_key = mbedtls_pk_ec( ssl->session_negotiate->peer_cert->pk );
2212 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
2213 MBEDTLS_ECDH_THEIRS ) ) != 0 )
2215 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
2219 if( ssl_check_server_ecdh_params( ssl ) != 0 )
2221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
2222 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
2227 #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2228 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2230 static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
2233 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2234 unsigned char *p, *end;
2236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
2238 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2239 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
2241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
2249 #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2250 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2251 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2252 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2254 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
2256 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
2260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
2266 #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2267 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2269 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
2271 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2275 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2278 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2282 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2283 * doesn't use a psk_identity_hint
2285 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
2287 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2288 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
2290 ssl->record_read = 1;
2294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2295 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2298 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
2299 end = ssl->in_msg + ssl->in_hslen;
2300 MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
2302 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2303 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2304 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2305 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2306 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
2308 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
2310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2311 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2314 #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2316 #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2317 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2318 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2319 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
2320 ; /* nothing more to do */
2322 #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2323 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2324 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2325 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2326 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2327 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
2329 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
2331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2332 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2336 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2337 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2338 #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2339 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
2340 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2341 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ANON_ENABLED)
2342 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2343 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
2344 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2345 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ANON )
2347 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2350 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2354 #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2355 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2356 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2357 MBEDTLS_KEY_EXCHANGE_ECDH_ANON_ENABLED */
2358 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2359 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2361 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
2365 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
2366 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2370 #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2373 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2376 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2377 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2378 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2379 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2380 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2381 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
2383 size_t sig_len, hashlen;
2384 unsigned char hash[64];
2385 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2386 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
2387 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
2388 size_t params_len = p - params;
2391 * Handle the digitally-signed structure
2393 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2394 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
2396 if( ssl_parse_signature_algorithm( ssl, &p, end,
2397 &md_alg, &pk_alg ) != 0 )
2399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2400 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2403 if( pk_alg != mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
2405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2406 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2410 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2411 #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
2412 defined(MBEDTLS_SSL_PROTO_TLS1_1)
2413 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
2415 pk_alg = mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
2417 /* Default hash for ECDSA is SHA-1 */
2418 if( pk_alg == MBEDTLS_PK_ECDSA && md_alg == MBEDTLS_MD_NONE )
2419 md_alg = MBEDTLS_MD_SHA1;
2424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2425 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2428 // Anonim cipher suite without sign, ecdh param only
2429 #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_ANON_ENABLED)
2430 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ANON )
2438 sig_len = ( p[0] << 8 ) | p[1];
2441 if( end != p + sig_len )
2443 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2444 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2447 MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
2450 * Compute the hash that has been signed
2452 #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
2453 defined(MBEDTLS_SSL_PROTO_TLS1_1)
2454 if( md_alg == MBEDTLS_MD_NONE )
2456 mbedtls_md5_context mbedtls_md5;
2457 mbedtls_sha1_context mbedtls_sha1;
2459 mbedtls_md5_init( &mbedtls_md5 );
2460 mbedtls_sha1_init( &mbedtls_sha1 );
2465 * digitally-signed struct {
2466 * opaque md5_hash[16];
2467 * opaque sha_hash[20];
2471 * MD5(ClientHello.random + ServerHello.random
2474 * SHA(ClientHello.random + ServerHello.random
2477 mbedtls_md5_starts( &mbedtls_md5 );
2478 mbedtls_md5_update( &mbedtls_md5, ssl->handshake->randbytes, 64 );
2479 mbedtls_md5_update( &mbedtls_md5, params, params_len );
2480 mbedtls_md5_finish( &mbedtls_md5, hash );
2482 mbedtls_sha1_starts( &mbedtls_sha1 );
2483 mbedtls_sha1_update( &mbedtls_sha1, ssl->handshake->randbytes, 64 );
2484 mbedtls_sha1_update( &mbedtls_sha1, params, params_len );
2485 mbedtls_sha1_finish( &mbedtls_sha1, hash + 16 );
2487 mbedtls_md5_free( &mbedtls_md5 );
2488 mbedtls_sha1_free( &mbedtls_sha1 );
2491 #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
2492 MBEDTLS_SSL_PROTO_TLS1_1 */
2493 #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2494 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2495 if( md_alg != MBEDTLS_MD_NONE )
2497 mbedtls_md_context_t ctx;
2499 mbedtls_md_init( &ctx );
2501 /* Info from md_alg will be used instead */
2505 * digitally-signed struct {
2506 * opaque client_random[32];
2507 * opaque server_random[32];
2508 * ServerDHParams params;
2511 if( ( ret = mbedtls_md_setup( &ctx,
2512 mbedtls_md_info_from_type( md_alg ), 0 ) ) != 0 )
2514 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
2518 mbedtls_md_starts( &ctx );
2519 mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 );
2520 mbedtls_md_update( &ctx, params, params_len );
2521 mbedtls_md_finish( &ctx, hash );
2522 mbedtls_md_free( &ctx );
2525 #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2526 MBEDTLS_SSL_PROTO_TLS1_2 */
2528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2529 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2532 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2533 (unsigned int) ( mbedtls_md_get_size( mbedtls_md_info_from_type( md_alg ) ) ) );
2535 if( ssl->session_negotiate->peer_cert == NULL )
2537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "certificate required" ) );
2538 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2544 if( ! mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
2546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2547 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
2550 if( ( ret = mbedtls_pk_verify( &ssl->session_negotiate->peer_cert->pk,
2551 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
2553 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
2557 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2558 MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2559 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
2564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
2569 #if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
2570 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2571 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2572 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2573 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \
2574 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2575 static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
2577 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2579 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2581 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2582 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2583 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2584 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
2585 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ||
2586 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ANON )
2588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2594 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2597 static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
2602 size_t cert_type_len = 0, dn_len = 0;
2603 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2607 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2608 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2609 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2610 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
2611 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ||
2612 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ANON )
2614 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2619 if( ssl->record_read == 0 )
2621 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
2623 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2627 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2630 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2633 ssl->record_read = 1;
2636 ssl->client_auth = 0;
2639 if( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST )
2642 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
2643 ssl->client_auth ? "a" : "no" ) );
2645 if( ssl->client_auth == 0 )
2648 ssl->record_read = 0;
2652 * ClientCertificateType certificate_types<1..2^8-1>;
2653 * SignatureAndHashAlgorithm
2654 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2655 * DistinguishedName certificate_authorities<0..2^16-1>;
2656 * } CertificateRequest;
2658 * Since we only support a single certificate on clients, let's just
2659 * ignore all the information that's supposed to help us pick a
2662 * We could check that our certificate matches the request, and bail out
2663 * if it doesn't, but it's simpler to just send the certificate anyway,
2664 * and give the server the opportunity to decide if it should terminate
2665 * the connection when it doesn't like our certificate.
2667 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2668 * point we only have one hash available (see comments in
2669 * write_certificate_verify), so let's just use what we have.
2671 * However, we still minimally parse the message to check it is at least
2672 * superficially sane.
2676 /* certificate_types */
2677 cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
2680 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
2682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2683 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2686 /* supported_signature_algorithms */
2687 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2688 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
2690 size_t sig_alg_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2691 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
2692 #if defined(MBEDTLS_DEBUG_C)
2693 unsigned char* sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
2696 for( i = 0; i < sig_alg_len; i += 2 )
2698 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Supported Signature Algorithm found: %d,%d", sig_alg[i], sig_alg[i + 1] ) );
2702 n += 2 + sig_alg_len;
2704 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
2706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2707 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2710 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2712 /* certificate_authorities */
2713 dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2714 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
2717 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
2719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2720 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2728 #endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED &&
2729 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2730 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED &&
2731 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2732 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED &&
2733 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
2735 static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
2739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2741 if( ssl->record_read == 0 )
2743 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
2745 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2749 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
2752 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2755 ssl->record_read = 0;
2757 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
2758 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
2760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
2761 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
2766 #if defined(MBEDTLS_SSL_PROTO_DTLS)
2767 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2768 mbedtls_ssl_recv_flight_completed( ssl );
2771 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2776 static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
2780 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2784 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2785 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
2788 * DHM key exchange -- send G^X mod P
2790 n = ssl->handshake->dhm_ctx.len;
2792 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2793 ssl->out_msg[5] = (unsigned char)( n );
2796 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
2797 (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
2798 &ssl->out_msg[i], n,
2799 ssl->conf->f_rng, ssl->conf->p_rng );
2802 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
2806 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2807 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2809 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
2810 ssl->handshake->premaster,
2811 MBEDTLS_PREMASTER_SIZE,
2812 &ssl->handshake->pmslen,
2813 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
2815 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2819 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2822 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
2823 #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2824 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2825 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2826 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) || \
2827 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ANON_ENABLED)
2828 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2829 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2830 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2831 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ||
2832 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ANON)
2835 * ECDH key exchange -- send client public value
2839 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
2841 &ssl->out_msg[i], 1000,
2842 ssl->conf->f_rng, ssl->conf->p_rng );
2845 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
2849 MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2851 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2852 &ssl->handshake->pmslen,
2853 ssl->handshake->premaster,
2854 MBEDTLS_MPI_MAX_SIZE,
2855 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
2857 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2861 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2864 #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2865 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2866 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2867 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED
2868 MBEDTLS_KEY_EXCHANGE_ECDH_ANON_ENABLED */
2869 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2870 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2871 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2872 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2873 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
2876 * opaque psk_identity<0..2^16-1>;
2878 if( ssl->conf->psk == NULL || ssl->conf->psk_identity == NULL )
2880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for PSK" ) );
2881 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
2885 n = ssl->conf->psk_identity_len;
2887 if( i + 2 + n > MBEDTLS_SSL_MAX_CONTENT_LEN )
2889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "psk identity too long or "
2890 "SSL buffer too short" ) );
2891 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2894 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2895 ssl->out_msg[i++] = (unsigned char)( n );
2897 memcpy( ssl->out_msg + i, ssl->conf->psk_identity, ssl->conf->psk_identity_len );
2898 i += ssl->conf->psk_identity_len;
2900 #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2901 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
2907 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2908 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
2910 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2915 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2916 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
2919 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2921 n = ssl->handshake->dhm_ctx.len;
2923 if( i + 2 + n > MBEDTLS_SSL_MAX_CONTENT_LEN )
2925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "psk identity or DHM size too long"
2926 " or SSL buffer too short" ) );
2927 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2930 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2931 ssl->out_msg[i++] = (unsigned char)( n );
2933 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
2934 (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
2935 &ssl->out_msg[i], n,
2936 ssl->conf->f_rng, ssl->conf->p_rng );
2939 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
2944 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2945 #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2946 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
2949 * ClientECDiffieHellmanPublic public;
2951 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2952 &ssl->out_msg[i], MBEDTLS_SSL_MAX_CONTENT_LEN - i,
2953 ssl->conf->f_rng, ssl->conf->p_rng );
2956 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
2960 MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2963 #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2966 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2969 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
2970 ciphersuite_info->key_exchange ) ) != 0 )
2972 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2977 #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2978 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2979 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
2982 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
2986 #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
2987 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2988 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2992 ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
2993 ssl->out_msg + i, MBEDTLS_SSL_MAX_CONTENT_LEN - i, &n,
2994 ssl->conf->f_rng, ssl->conf->p_rng );
2997 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3001 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
3002 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3003 ssl->conf->f_rng, ssl->conf->p_rng );
3006 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
3011 #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
3013 ((void) ciphersuite_info);
3014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3015 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3018 ssl->out_msglen = i + n;
3019 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3020 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
3024 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
3026 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
3030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
3035 #if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
3036 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3037 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3038 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3039 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \
3040 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
3041 static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
3043 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
3048 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
3050 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
3054 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
3055 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
3056 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
3057 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
3058 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ||
3059 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ANON )
3061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
3066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3067 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3070 static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
3072 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3073 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3074 size_t n = 0, offset = 0;
3075 unsigned char hash[48];
3076 unsigned char *hash_start = hash;
3077 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
3078 unsigned int hashlen;
3080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
3082 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
3084 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
3088 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
3089 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
3090 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
3091 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
3092 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ||
3093 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ANON )
3095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
3100 if( ssl->client_auth == 0 || mbedtls_ssl_own_cert( ssl ) == NULL )
3102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
3107 if( mbedtls_ssl_own_key( ssl ) == NULL )
3109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
3110 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3114 * Make an RSA signature of the handshake digests
3116 ssl->handshake->calc_verify( ssl, hash );
3118 #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3119 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3120 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
3123 * digitally-signed struct {
3124 * opaque md5_hash[16];
3125 * opaque sha_hash[20];
3129 * MD5(handshake_messages);
3132 * SHA(handshake_messages);
3135 md_alg = MBEDTLS_MD_NONE;
3138 * For ECDSA, default hash is SHA-1 only
3140 if( mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECDSA ) )
3144 md_alg = MBEDTLS_MD_SHA1;
3148 #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3149 MBEDTLS_SSL_PROTO_TLS1_1 */
3150 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3151 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
3154 * digitally-signed struct {
3155 * opaque handshake_messages[handshake_messages_length];
3158 * Taking shortcut here. We assume that the server always allows the
3159 * PRF Hash function and has sent it in the allowed signature
3160 * algorithms list received in the Certificate Request message.
3162 * Until we encounter a server that does not, we will take this
3165 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
3166 * in order to satisfy 'weird' needs from the server side.
3168 if( ssl->transform_negotiate->ciphersuite_info->mac ==
3171 md_alg = MBEDTLS_MD_SHA384;
3172 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
3176 md_alg = MBEDTLS_MD_SHA256;
3177 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
3179 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
3181 /* Info from md_alg will be used instead */
3186 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3188 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3189 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3192 if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ), md_alg, hash_start, hashlen,
3193 ssl->out_msg + 6 + offset, &n,
3194 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
3196 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
3200 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
3201 ssl->out_msg[5 + offset] = (unsigned char)( n );
3203 ssl->out_msglen = 6 + n + offset;
3204 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3205 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
3209 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
3211 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
3215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
3219 #endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED &&
3220 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3221 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED &&
3222 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
3223 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED &&
3224 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
3226 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
3227 static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
3232 unsigned char *ticket;
3233 const unsigned char *msg;
3235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
3237 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
3239 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
3243 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
3245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
3246 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
3251 * uint32 ticket_lifetime_hint;
3252 * opaque ticket<0..2^16-1>;
3253 * } NewSessionTicket;
3255 * 0 . 3 ticket_lifetime_hint
3256 * 4 . 5 ticket_len (n)
3257 * 6 . 5+n ticket content
3259 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3260 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
3262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
3263 return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
3266 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
3268 lifetime = ( msg[0] << 24 ) | ( msg[1] << 16 ) |
3269 ( msg[2] << 8 ) | ( msg[3] );
3271 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
3273 if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
3275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
3276 return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
3279 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
3281 /* We're not waiting for a NewSessionTicket message any more */
3282 ssl->handshake->new_session_ticket = 0;
3283 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
3286 * Zero-length ticket means the server changed his mind and doesn't want
3287 * to send a ticket after all, so just forget it
3289 if( ticket_len == 0 )
3292 mbedtls_zeroize( ssl->session_negotiate->ticket,
3293 ssl->session_negotiate->ticket_len );
3294 mbedtls_free( ssl->session_negotiate->ticket );
3295 ssl->session_negotiate->ticket = NULL;
3296 ssl->session_negotiate->ticket_len = 0;
3298 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
3300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
3301 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
3304 memcpy( ticket, msg + 6, ticket_len );
3306 ssl->session_negotiate->ticket = ticket;
3307 ssl->session_negotiate->ticket_len = ticket_len;
3308 ssl->session_negotiate->ticket_lifetime = lifetime;
3311 * RFC 5077 section 3.4:
3312 * "If the client receives a session ticket from the server, then it
3313 * discards any Session ID that was sent in the ServerHello."
3315 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
3316 ssl->session_negotiate->id_len = 0;
3318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
3322 #endif /* MBEDTLS_SSL_SESSION_TICKETS */
3325 * SSL handshake -- client side -- single step
3327 int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
3331 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
3332 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
3336 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3339 #if defined(MBEDTLS_SSL_PROTO_DTLS)
3340 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3341 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3343 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
3348 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
3349 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
3350 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
3351 if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
3352 ssl->handshake->new_session_ticket != 0 )
3354 ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET;
3358 switch( ssl->state )
3360 case MBEDTLS_SSL_HELLO_REQUEST:
3361 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3367 case MBEDTLS_SSL_CLIENT_HELLO:
3368 ret = ssl_write_client_hello( ssl );
3374 * ( ServerKeyExchange )
3375 * ( CertificateRequest )
3378 case MBEDTLS_SSL_SERVER_HELLO:
3379 ret = ssl_parse_server_hello( ssl );
3382 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3383 ret = mbedtls_ssl_parse_certificate( ssl );
3386 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
3387 ret = ssl_parse_server_key_exchange( ssl );
3390 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
3391 ret = ssl_parse_certificate_request( ssl );
3394 case MBEDTLS_SSL_SERVER_HELLO_DONE:
3395 ret = ssl_parse_server_hello_done( ssl );
3399 * ==> ( Certificate/Alert )
3401 * ( CertificateVerify )
3405 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3406 ret = mbedtls_ssl_write_certificate( ssl );
3409 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
3410 ret = ssl_write_client_key_exchange( ssl );
3413 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
3414 ret = ssl_write_certificate_verify( ssl );
3417 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3418 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
3421 case MBEDTLS_SSL_CLIENT_FINISHED:
3422 ret = mbedtls_ssl_write_finished( ssl );
3426 * <== ( NewSessionTicket )
3430 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
3431 case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET:
3432 ret = ssl_parse_new_session_ticket( ssl );
3436 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3437 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
3440 case MBEDTLS_SSL_SERVER_FINISHED:
3441 ret = mbedtls_ssl_parse_finished( ssl );
3444 case MBEDTLS_SSL_FLUSH_BUFFERS:
3445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3446 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
3449 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3450 mbedtls_ssl_handshake_wrapup( ssl );
3454 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3455 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3460 #endif /* MBEDTLS_SSL_CLI_C */