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];
2012 if( n < 1 || n > 65535 || *p + n > end )
2014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2015 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2018 if( ssl->conf->f_psk != NULL )
2020 if( ssl->conf->f_psk( ssl->conf->p_psk, ssl, *p, n ) != 0 )
2021 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
2025 /* Identity is not a big secret since clients send it in the clear,
2026 * but treat it carefully anyway, just in case */
2027 if( n != ssl->conf->psk_identity_len ||
2028 mbedtls_ssl_safer_memcmp( ssl->conf->psk_identity, *p, n ) != 0 )
2030 ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
2034 if( ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY )
2036 MBEDTLS_SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
2037 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
2038 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2039 MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2044 return( MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY );
2051 #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2053 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
2054 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2056 * Generate a pre-master secret and encrypt it with the server's RSA key
2058 static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
2059 size_t offset, size_t *olen,
2063 size_t len_bytes = ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ? 0 : 2;
2064 unsigned char *p = ssl->handshake->premaster + pms_offset;
2066 if( offset + len_bytes > MBEDTLS_SSL_MAX_CONTENT_LEN )
2068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
2069 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2073 * Generate (part of) the pre-master as
2075 * ProtocolVersion client_version;
2076 * opaque random[46];
2077 * } PreMasterSecret;
2079 mbedtls_ssl_write_version( ssl->conf->max_major_ver, ssl->conf->max_minor_ver,
2080 ssl->conf->transport, p );
2082 if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
2084 MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
2088 ssl->handshake->pmslen = 48;
2090 if( ssl->session_negotiate->peer_cert == NULL )
2092 MBEDTLS_SSL_DEBUG_MSG( 2, ( "certificate required" ) );
2093 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2097 * Now write it out, encrypted
2099 if( ! mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk,
2102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
2103 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
2106 if( ( ret = mbedtls_pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
2107 p, ssl->handshake->pmslen,
2108 ssl->out_msg + offset + len_bytes, olen,
2109 MBEDTLS_SSL_MAX_CONTENT_LEN - offset - len_bytes,
2110 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
2112 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
2116 #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2117 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2118 if( len_bytes == 2 )
2120 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
2121 ssl->out_msg[offset+1] = (unsigned char)( *olen );
2128 #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
2129 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2131 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2132 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2133 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2134 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2135 static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl,
2138 mbedtls_md_type_t *md_alg,
2139 mbedtls_pk_type_t *pk_alg )
2142 *md_alg = MBEDTLS_MD_NONE;
2143 *pk_alg = MBEDTLS_PK_NONE;
2145 /* Only in TLS 1.2 */
2146 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
2151 if( (*p) + 2 > end )
2152 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2155 * Get hash algorithm
2157 if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) ) == MBEDTLS_MD_NONE )
2159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Server used unsupported "
2160 "HashAlgorithm %d", *(p)[0] ) );
2161 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2165 * Get signature algorithm
2167 if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) ) == MBEDTLS_PK_NONE )
2169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server used unsupported "
2170 "SignatureAlgorithm %d", (*p)[1] ) );
2171 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2175 * Check if the hash is acceptable
2177 if( mbedtls_ssl_check_sig_hash( ssl, *md_alg ) != 0 )
2179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server used HashAlgorithm "
2180 "that was not offered" ) );
2181 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
2185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
2190 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2191 MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2192 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
2193 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2195 #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2196 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2197 static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
2200 const mbedtls_ecp_keypair *peer_key;
2202 if( ssl->session_negotiate->peer_cert == NULL )
2204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "certificate required" ) );
2205 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2208 if( ! mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk,
2209 MBEDTLS_PK_ECKEY ) )
2211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2212 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
2215 peer_key = mbedtls_pk_ec( ssl->session_negotiate->peer_cert->pk );
2217 if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
2218 MBEDTLS_ECDH_THEIRS ) ) != 0 )
2220 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
2224 if( ssl_check_server_ecdh_params( ssl ) != 0 )
2226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
2227 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
2232 #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2233 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2235 static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
2238 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2239 unsigned char *p, *end;
2241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
2243 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2244 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
2246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
2254 #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2255 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2256 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2257 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2259 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
2261 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
2265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
2271 #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2272 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2274 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
2276 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2280 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2283 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2287 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
2288 * doesn't use a psk_identity_hint
2290 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
2292 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2293 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
2295 ssl->record_read = 1;
2299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2300 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2303 p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
2304 end = ssl->in_msg + ssl->in_hslen;
2305 MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
2307 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2308 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2309 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2310 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2311 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
2313 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
2315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2316 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2319 #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2321 #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
2322 defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2323 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2324 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
2325 ; /* nothing more to do */
2327 #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
2328 MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2329 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2330 defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2331 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2332 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
2334 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
2336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2337 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2341 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2342 MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2343 #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2344 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
2345 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2346 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ANON_ENABLED)
2347 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2348 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
2349 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2350 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ANON )
2352 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2355 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2359 #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2360 MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
2361 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2362 MBEDTLS_KEY_EXCHANGE_ECDH_ANON_ENABLED */
2363 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2364 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2366 ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
2370 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
2371 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2375 #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2378 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2381 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2382 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2383 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2384 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
2385 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2386 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
2388 size_t sig_len, hashlen;
2389 unsigned char hash[64];
2390 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
2391 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
2392 unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
2393 size_t params_len = p - params;
2396 * Handle the digitally-signed structure
2398 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2399 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
2401 if( ssl_parse_signature_algorithm( ssl, &p, end,
2402 &md_alg, &pk_alg ) != 0 )
2404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2405 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2408 if( pk_alg != mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
2410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2411 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2415 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2416 #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
2417 defined(MBEDTLS_SSL_PROTO_TLS1_1)
2418 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
2420 pk_alg = mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
2422 /* Default hash for ECDSA is SHA-1 */
2423 if( pk_alg == MBEDTLS_PK_ECDSA && md_alg == MBEDTLS_MD_NONE )
2424 md_alg = MBEDTLS_MD_SHA1;
2429 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2430 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2433 // Anonim cipher suite without sign, ecdh param only
2434 #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_ANON_ENABLED)
2440 sig_len = ( p[0] << 8 ) | p[1];
2443 if( end != p + sig_len )
2445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2446 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2449 MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
2452 * Compute the hash that has been signed
2454 #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
2455 defined(MBEDTLS_SSL_PROTO_TLS1_1)
2456 if( md_alg == MBEDTLS_MD_NONE )
2458 mbedtls_md5_context mbedtls_md5;
2459 mbedtls_sha1_context mbedtls_sha1;
2461 mbedtls_md5_init( &mbedtls_md5 );
2462 mbedtls_sha1_init( &mbedtls_sha1 );
2467 * digitally-signed struct {
2468 * opaque md5_hash[16];
2469 * opaque sha_hash[20];
2473 * MD5(ClientHello.random + ServerHello.random
2476 * SHA(ClientHello.random + ServerHello.random
2479 mbedtls_md5_starts( &mbedtls_md5 );
2480 mbedtls_md5_update( &mbedtls_md5, ssl->handshake->randbytes, 64 );
2481 mbedtls_md5_update( &mbedtls_md5, params, params_len );
2482 mbedtls_md5_finish( &mbedtls_md5, hash );
2484 mbedtls_sha1_starts( &mbedtls_sha1 );
2485 mbedtls_sha1_update( &mbedtls_sha1, ssl->handshake->randbytes, 64 );
2486 mbedtls_sha1_update( &mbedtls_sha1, params, params_len );
2487 mbedtls_sha1_finish( &mbedtls_sha1, hash + 16 );
2489 mbedtls_md5_free( &mbedtls_md5 );
2490 mbedtls_sha1_free( &mbedtls_sha1 );
2493 #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
2494 MBEDTLS_SSL_PROTO_TLS1_1 */
2495 #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2496 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2497 if( md_alg != MBEDTLS_MD_NONE )
2499 mbedtls_md_context_t ctx;
2501 mbedtls_md_init( &ctx );
2503 /* Info from md_alg will be used instead */
2507 * digitally-signed struct {
2508 * opaque client_random[32];
2509 * opaque server_random[32];
2510 * ServerDHParams params;
2513 if( ( ret = mbedtls_md_setup( &ctx,
2514 mbedtls_md_info_from_type( md_alg ), 0 ) ) != 0 )
2516 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
2520 mbedtls_md_starts( &ctx );
2521 mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 );
2522 mbedtls_md_update( &ctx, params, params_len );
2523 mbedtls_md_finish( &ctx, hash );
2524 mbedtls_md_free( &ctx );
2527 #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2528 MBEDTLS_SSL_PROTO_TLS1_2 */
2530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2531 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2534 MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2535 (unsigned int) ( mbedtls_md_get_size( mbedtls_md_info_from_type( md_alg ) ) ) );
2537 if( ssl->session_negotiate->peer_cert == NULL )
2539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "certificate required" ) );
2540 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2546 if( ! mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
2548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2549 return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
2552 if( ( ret = mbedtls_pk_verify( &ssl->session_negotiate->peer_cert->pk,
2553 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
2555 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
2559 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2560 MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2561 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
2566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
2571 #if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
2572 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2573 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2574 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2575 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \
2576 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2577 static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
2579 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2583 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2584 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2585 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2586 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
2587 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ||
2588 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ANON )
2590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2596 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2599 static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
2604 size_t cert_type_len = 0, dn_len = 0;
2605 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2607 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2609 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2610 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2611 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2612 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
2613 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ||
2614 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ANON )
2616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2621 if( ssl->record_read == 0 )
2623 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
2625 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2629 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2632 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2635 ssl->record_read = 1;
2638 ssl->client_auth = 0;
2641 if( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST )
2644 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
2645 ssl->client_auth ? "a" : "no" ) );
2647 if( ssl->client_auth == 0 )
2650 ssl->record_read = 0;
2654 * ClientCertificateType certificate_types<1..2^8-1>;
2655 * SignatureAndHashAlgorithm
2656 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2657 * DistinguishedName certificate_authorities<0..2^16-1>;
2658 * } CertificateRequest;
2660 * Since we only support a single certificate on clients, let's just
2661 * ignore all the information that's supposed to help us pick a
2664 * We could check that our certificate matches the request, and bail out
2665 * if it doesn't, but it's simpler to just send the certificate anyway,
2666 * and give the server the opportunity to decide if it should terminate
2667 * the connection when it doesn't like our certificate.
2669 * Same goes for the hash in TLS 1.2's signature_algorithms: at this
2670 * point we only have one hash available (see comments in
2671 * write_certificate_verify), so let's just use what we have.
2673 * However, we still minimally parse the message to check it is at least
2674 * superficially sane.
2678 /* certificate_types */
2679 cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
2682 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
2684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2685 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2688 /* supported_signature_algorithms */
2689 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2690 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
2692 size_t sig_alg_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2693 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
2694 #if defined(MBEDTLS_DEBUG_C)
2695 unsigned char* sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
2698 for( i = 0; i < sig_alg_len; i += 2 )
2700 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Supported Signature Algorithm found: %d,%d", sig_alg[i], sig_alg[i + 1] ) );
2704 n += 2 + sig_alg_len;
2706 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
2708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2709 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2712 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2714 /* certificate_authorities */
2715 dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2716 | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
2719 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
2721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2722 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2730 #endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED &&
2731 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2732 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED &&
2733 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2734 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED &&
2735 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
2737 static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
2741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2743 if( ssl->record_read == 0 )
2745 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
2747 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
2751 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
2753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
2754 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
2757 ssl->record_read = 0;
2759 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
2760 ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
2762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
2763 return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
2768 #if defined(MBEDTLS_SSL_PROTO_DTLS)
2769 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2770 mbedtls_ssl_recv_flight_completed( ssl );
2773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2778 static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
2782 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2786 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2787 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
2790 * DHM key exchange -- send G^X mod P
2792 n = ssl->handshake->dhm_ctx.len;
2794 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2795 ssl->out_msg[5] = (unsigned char)( n );
2798 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
2799 (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
2800 &ssl->out_msg[i], n,
2801 ssl->conf->f_rng, ssl->conf->p_rng );
2804 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
2808 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2809 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2811 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
2812 ssl->handshake->premaster,
2813 MBEDTLS_PREMASTER_SIZE,
2814 &ssl->handshake->pmslen,
2815 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
2817 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2821 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2824 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
2825 #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2826 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2827 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2828 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) || \
2829 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ANON_ENABLED)
2830 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2831 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2832 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2833 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ||
2834 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ANON)
2837 * ECDH key exchange -- send client public value
2841 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
2843 &ssl->out_msg[i], 1000,
2844 ssl->conf->f_rng, ssl->conf->p_rng );
2847 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
2851 MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2853 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2854 &ssl->handshake->pmslen,
2855 ssl->handshake->premaster,
2856 MBEDTLS_MPI_MAX_SIZE,
2857 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
2859 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2863 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2866 #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2867 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2868 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2869 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED
2870 MBEDTLS_KEY_EXCHANGE_ECDH_ANON_ENABLED */
2871 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2872 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
2873 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
2874 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2875 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
2878 * opaque psk_identity<0..2^16-1>;
2880 if( ssl->conf->psk == NULL || ssl->conf->psk_identity == NULL )
2882 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for PSK" ) );
2883 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
2887 n = ssl->conf->psk_identity_len;
2889 if( i + 2 + n > MBEDTLS_SSL_MAX_CONTENT_LEN )
2891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "psk identity too long or "
2892 "SSL buffer too short" ) );
2893 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2896 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2897 ssl->out_msg[i++] = (unsigned char)( n );
2899 memcpy( ssl->out_msg + i, ssl->conf->psk_identity, ssl->conf->psk_identity_len );
2900 i += ssl->conf->psk_identity_len;
2902 #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2903 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
2909 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2910 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
2912 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2917 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2918 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
2921 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2923 n = ssl->handshake->dhm_ctx.len;
2925 if( i + 2 + n > MBEDTLS_SSL_MAX_CONTENT_LEN )
2927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "psk identity or DHM size too long"
2928 " or SSL buffer too short" ) );
2929 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2932 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2933 ssl->out_msg[i++] = (unsigned char)( n );
2935 ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
2936 (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
2937 &ssl->out_msg[i], n,
2938 ssl->conf->f_rng, ssl->conf->p_rng );
2941 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
2946 #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2947 #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2948 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
2951 * ClientECDiffieHellmanPublic public;
2953 ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2954 &ssl->out_msg[i], MBEDTLS_SSL_MAX_CONTENT_LEN - i,
2955 ssl->conf->f_rng, ssl->conf->p_rng );
2958 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
2962 MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2965 #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2968 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2971 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
2972 ciphersuite_info->key_exchange ) ) != 0 )
2974 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2979 #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2980 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2981 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
2984 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
2988 #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
2989 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2990 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2994 ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
2995 ssl->out_msg + i, MBEDTLS_SSL_MAX_CONTENT_LEN - i, &n,
2996 ssl->conf->f_rng, ssl->conf->p_rng );
2999 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
3003 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
3004 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3005 ssl->conf->f_rng, ssl->conf->p_rng );
3008 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
3013 #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
3015 ((void) ciphersuite_info);
3016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3017 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3020 ssl->out_msglen = i + n;
3021 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3022 ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
3026 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
3028 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
3032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
3037 #if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
3038 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3039 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3040 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3041 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \
3042 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
3043 static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
3045 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
3050 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
3052 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
3056 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
3057 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
3058 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
3059 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
3060 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ||
3061 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ANON )
3063 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
3068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3069 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3072 static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
3074 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3075 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3076 size_t n = 0, offset = 0;
3077 unsigned char hash[48];
3078 unsigned char *hash_start = hash;
3079 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
3080 unsigned int hashlen;
3082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
3084 if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
3086 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
3090 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
3091 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
3092 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
3093 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
3094 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ||
3095 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ANON )
3097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
3102 if( ssl->client_auth == 0 || mbedtls_ssl_own_cert( ssl ) == NULL )
3104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
3109 if( mbedtls_ssl_own_key( ssl ) == NULL )
3111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
3112 return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
3116 * Make an RSA signature of the handshake digests
3118 ssl->handshake->calc_verify( ssl, hash );
3120 #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3121 defined(MBEDTLS_SSL_PROTO_TLS1_1)
3122 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
3125 * digitally-signed struct {
3126 * opaque md5_hash[16];
3127 * opaque sha_hash[20];
3131 * MD5(handshake_messages);
3134 * SHA(handshake_messages);
3137 md_alg = MBEDTLS_MD_NONE;
3140 * For ECDSA, default hash is SHA-1 only
3142 if( mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECDSA ) )
3146 md_alg = MBEDTLS_MD_SHA1;
3150 #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
3151 MBEDTLS_SSL_PROTO_TLS1_1 */
3152 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3153 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
3156 * digitally-signed struct {
3157 * opaque handshake_messages[handshake_messages_length];
3160 * Taking shortcut here. We assume that the server always allows the
3161 * PRF Hash function and has sent it in the allowed signature
3162 * algorithms list received in the Certificate Request message.
3164 * Until we encounter a server that does not, we will take this
3167 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
3168 * in order to satisfy 'weird' needs from the server side.
3170 if( ssl->transform_negotiate->ciphersuite_info->mac ==
3173 md_alg = MBEDTLS_MD_SHA384;
3174 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
3178 md_alg = MBEDTLS_MD_SHA256;
3179 ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
3181 ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
3183 /* Info from md_alg will be used instead */
3188 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3191 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3194 if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ), md_alg, hash_start, hashlen,
3195 ssl->out_msg + 6 + offset, &n,
3196 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
3198 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
3202 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
3203 ssl->out_msg[5 + offset] = (unsigned char)( n );
3205 ssl->out_msglen = 6 + n + offset;
3206 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3207 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
3211 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
3213 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
3217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
3221 #endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED &&
3222 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3223 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED &&
3224 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
3225 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED &&
3226 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
3228 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
3229 static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
3234 unsigned char *ticket;
3235 const unsigned char *msg;
3237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
3239 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
3241 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
3245 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
3247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
3248 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
3253 * uint32 ticket_lifetime_hint;
3254 * opaque ticket<0..2^16-1>;
3255 * } NewSessionTicket;
3257 * 0 . 3 ticket_lifetime_hint
3258 * 4 . 5 ticket_len (n)
3259 * 6 . 5+n ticket content
3261 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
3262 ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
3264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
3265 return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
3268 msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
3270 lifetime = ( msg[0] << 24 ) | ( msg[1] << 16 ) |
3271 ( msg[2] << 8 ) | ( msg[3] );
3273 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
3275 if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
3277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
3278 return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
3281 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
3283 /* We're not waiting for a NewSessionTicket message any more */
3284 ssl->handshake->new_session_ticket = 0;
3285 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
3288 * Zero-length ticket means the server changed his mind and doesn't want
3289 * to send a ticket after all, so just forget it
3291 if( ticket_len == 0 )
3294 mbedtls_zeroize( ssl->session_negotiate->ticket,
3295 ssl->session_negotiate->ticket_len );
3296 mbedtls_free( ssl->session_negotiate->ticket );
3297 ssl->session_negotiate->ticket = NULL;
3298 ssl->session_negotiate->ticket_len = 0;
3300 if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
3302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
3303 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
3306 memcpy( ticket, msg + 6, ticket_len );
3308 ssl->session_negotiate->ticket = ticket;
3309 ssl->session_negotiate->ticket_len = ticket_len;
3310 ssl->session_negotiate->ticket_lifetime = lifetime;
3313 * RFC 5077 section 3.4:
3314 * "If the client receives a session ticket from the server, then it
3315 * discards any Session ID that was sent in the ServerHello."
3317 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
3318 ssl->session_negotiate->id_len = 0;
3320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
3324 #endif /* MBEDTLS_SSL_SESSION_TICKETS */
3327 * SSL handshake -- client side -- single step
3329 int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
3333 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
3334 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
3338 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3341 #if defined(MBEDTLS_SSL_PROTO_DTLS)
3342 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3343 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3345 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
3350 /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
3351 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
3352 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
3353 if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
3354 ssl->handshake->new_session_ticket != 0 )
3356 ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET;
3360 switch( ssl->state )
3362 case MBEDTLS_SSL_HELLO_REQUEST:
3363 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3369 case MBEDTLS_SSL_CLIENT_HELLO:
3370 ret = ssl_write_client_hello( ssl );
3376 * ( ServerKeyExchange )
3377 * ( CertificateRequest )
3380 case MBEDTLS_SSL_SERVER_HELLO:
3381 ret = ssl_parse_server_hello( ssl );
3384 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3385 ret = mbedtls_ssl_parse_certificate( ssl );
3388 case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
3389 ret = ssl_parse_server_key_exchange( ssl );
3392 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
3393 ret = ssl_parse_certificate_request( ssl );
3396 case MBEDTLS_SSL_SERVER_HELLO_DONE:
3397 ret = ssl_parse_server_hello_done( ssl );
3401 * ==> ( Certificate/Alert )
3403 * ( CertificateVerify )
3407 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3408 ret = mbedtls_ssl_write_certificate( ssl );
3411 case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
3412 ret = ssl_write_client_key_exchange( ssl );
3415 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
3416 ret = ssl_write_certificate_verify( ssl );
3419 case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
3420 ret = mbedtls_ssl_write_change_cipher_spec( ssl );
3423 case MBEDTLS_SSL_CLIENT_FINISHED:
3424 ret = mbedtls_ssl_write_finished( ssl );
3428 * <== ( NewSessionTicket )
3432 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
3433 case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET:
3434 ret = ssl_parse_new_session_ticket( ssl );
3438 case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
3439 ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
3442 case MBEDTLS_SSL_SERVER_FINISHED:
3443 ret = mbedtls_ssl_parse_finished( ssl );
3446 case MBEDTLS_SSL_FLUSH_BUFFERS:
3447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3448 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
3451 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3452 mbedtls_ssl_handshake_wrapup( ssl );
3456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3457 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3462 #endif /* MBEDTLS_SSL_CLI_C */