26 #if !defined(POLARSSL_CONFIG_FILE)
29 #include POLARSSL_CONFIG_FILE
32 #if defined(POLARSSL_SSL_CLI_C)
37 #if defined(POLARSSL_PLATFORM_C)
40 #define polarssl_malloc malloc
41 #define polarssl_free free
47 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
49 typedef UINT32 uint32_t;
54 #if defined(POLARSSL_HAVE_TIME)
58 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
59 static void ssl_write_hostname_ext(
ssl_context *ssl,
63 unsigned char *p = buf;
70 SSL_DEBUG_MSG( 3, (
"client hello, adding server name extension: %s",
94 *p++ = (
unsigned char)( ( (ssl->
hostname_len + 5) >> 8 ) & 0xFF );
95 *p++ = (
unsigned char)( ( (ssl->
hostname_len + 5) ) & 0xFF );
97 *p++ = (
unsigned char)( ( (ssl->
hostname_len + 3) >> 8 ) & 0xFF );
98 *p++ = (
unsigned char)( ( (ssl->
hostname_len + 3) ) & 0xFF );
101 *p++ = (
unsigned char)( ( ssl->
hostname_len >> 8 ) & 0xFF );
110 static void ssl_write_renegotiation_ext(
ssl_context *ssl,
114 unsigned char *p = buf;
121 SSL_DEBUG_MSG( 3, (
"client hello, adding renegotiation extension" ) );
138 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
139 static void ssl_write_signature_algorithms_ext(
ssl_context *ssl,
143 unsigned char *p = buf;
144 unsigned char *sig_alg_list = buf + 6;
145 size_t sig_alg_len = 0;
152 SSL_DEBUG_MSG( 3, (
"client hello, adding signature_algorithms extension" ) );
157 #if defined(POLARSSL_RSA_C)
158 #if defined(POLARSSL_SHA512_C)
164 #if defined(POLARSSL_SHA256_C)
170 #if defined(POLARSSL_SHA1_C)
174 #if defined(POLARSSL_MD5_C)
179 #if defined(POLARSSL_ECDSA_C)
180 #if defined(POLARSSL_SHA512_C)
186 #if defined(POLARSSL_SHA256_C)
192 #if defined(POLARSSL_SHA1_C)
196 #if defined(POLARSSL_MD5_C)
222 *p++ = (
unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF );
223 *p++ = (
unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF );
225 *p++ = (
unsigned char)( ( sig_alg_len >> 8 ) & 0xFF );
226 *p++ = (
unsigned char)( ( sig_alg_len ) & 0xFF );
228 *olen = 6 + sig_alg_len;
232 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
233 static void ssl_write_supported_elliptic_curves_ext(
ssl_context *ssl,
237 unsigned char *p = buf;
238 unsigned char *elliptic_curve_list = p + 6;
239 size_t elliptic_curve_len = 0;
241 #if defined(POLARSSL_SSL_SET_CURVES)
249 SSL_DEBUG_MSG( 3, (
"client hello, adding supported_elliptic_curves extension" ) );
251 #if defined(POLARSSL_SSL_SET_CURVES)
260 elliptic_curve_list[elliptic_curve_len++] = info->
tls_id >> 8;
261 elliptic_curve_list[elliptic_curve_len++] = info->
tls_id & 0xFF;
264 if( elliptic_curve_len == 0 )
270 *p++ = (
unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
271 *p++ = (
unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
273 *p++ = (
unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
274 *p++ = (
unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
276 *olen = 6 + elliptic_curve_len;
279 static void ssl_write_supported_point_formats_ext(
ssl_context *ssl,
283 unsigned char *p = buf;
288 SSL_DEBUG_MSG( 3, (
"client hello, adding supported_point_formats extension" ) );
303 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
304 static void ssl_write_max_fragment_length_ext(
ssl_context *ssl,
308 unsigned char *p = buf;
315 SSL_DEBUG_MSG( 3, (
"client hello, adding max_fragment_length extension" ) );
329 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
330 static void ssl_write_truncated_hmac_ext(
ssl_context *ssl,
331 unsigned char *buf,
size_t *olen )
333 unsigned char *p = buf;
341 SSL_DEBUG_MSG( 3, (
"client hello, adding truncated_hmac extension" ) );
353 #if defined(POLARSSL_SSL_SESSION_TICKETS)
354 static void ssl_write_session_ticket_ext(
ssl_context *ssl,
355 unsigned char *buf,
size_t *olen )
357 unsigned char *p = buf;
366 SSL_DEBUG_MSG( 3, (
"client hello, adding session ticket extension" ) );
371 *p++ = (
unsigned char)( ( tlen >> 8 ) & 0xFF );
372 *p++ = (
unsigned char)( ( tlen ) & 0xFF );
382 SSL_DEBUG_MSG( 3, (
"sending session ticket of length %d", tlen ) );
390 #if defined(POLARSSL_SSL_ALPN)
392 unsigned char *buf,
size_t *olen )
394 unsigned char *p = buf;
403 SSL_DEBUG_MSG( 3, (
"client hello, adding alpn extension" ) );
419 for( cur = ssl->
alpn_list; *cur != NULL; cur++ )
421 *p = (
unsigned char)( strlen( *cur ) & 0xFF );
422 memcpy( p + 1, *cur, *p );
429 buf[4] = (
unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
430 buf[5] = (
unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
433 buf[2] = (
unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
434 buf[3] = (
unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
438 static int ssl_write_client_hello(
ssl_context *ssl )
441 size_t i, n, olen, ext_len = 0;
443 unsigned char *p, *q;
444 #if defined(POLARSSL_HAVE_TIME)
447 const int *ciphersuites;
452 if( ssl->
f_rng == NULL )
486 #if defined(POLARSSL_HAVE_TIME)
488 *p++ = (
unsigned char)( t >> 24 );
489 *p++ = (
unsigned char)( t >> 16 );
490 *p++ = (
unsigned char)( t >> 8 );
491 *p++ = (
unsigned char)( t );
493 SSL_DEBUG_MSG( 3, (
"client hello, current time: %lu", t ) );
495 if( ( ret = ssl->
f_rng( ssl->
p_rng, p, 4 ) ) != 0 )
501 if( ( ret = ssl->
f_rng( ssl->
p_rng, p, 28 ) ) != 0 )
508 SSL_DEBUG_BUF( 3,
"client hello, random bytes", buf + 6, 32 );
528 #if defined(POLARSSL_SSL_SESSION_TICKETS)
546 *p++ = (
unsigned char) n;
548 for( i = 0; i < n; i++ )
551 SSL_DEBUG_MSG( 3, (
"client hello, session id len.: %d", n ) );
571 for( i = 0; ciphersuites[i] != 0; i++ )
575 if( ciphersuite_info == NULL )
586 *p++ = (
unsigned char)( ciphersuites[i] >> 8 );
587 *p++ = (
unsigned char)( ciphersuites[i] );
590 *q++ = (
unsigned char)( n >> 7 );
591 *q++ = (
unsigned char)( n << 1 );
593 SSL_DEBUG_MSG( 3, (
"client hello, got %d ciphersuites", n ) );
596 #if defined(POLARSSL_ZLIB_SUPPORT)
597 SSL_DEBUG_MSG( 3, (
"client hello, compress len.: %d", 2 ) );
605 SSL_DEBUG_MSG( 3, (
"client hello, compress len.: %d", 1 ) );
614 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
615 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
619 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
622 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
623 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
627 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
628 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
631 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
635 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
636 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
640 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
641 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
645 #if defined(POLARSSL_SSL_SESSION_TICKETS)
646 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
650 #if defined(POLARSSL_SSL_ALPN)
651 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
655 SSL_DEBUG_MSG( 3, (
"client hello, total extension length: %d",
660 *p++ = (
unsigned char)( ( ext_len >> 8 ) & 0xFF );
661 *p++ = (
unsigned char)( ( ext_len ) & 0xFF );
682 static int ssl_parse_renegotiation_info(
ssl_context *ssl,
683 const unsigned char *buf,
690 if( len != 1 || buf[0] != 0x0 )
692 SSL_DEBUG_MSG( 1, (
"non-zero length renegotiated connection field" ) );
712 SSL_DEBUG_MSG( 1, (
"non-matching renegotiated connection field" ) );
724 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
725 static int ssl_parse_max_fragment_length_ext(
ssl_context *ssl,
726 const unsigned char *buf,
744 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
745 static int ssl_parse_truncated_hmac_ext(
ssl_context *ssl,
746 const unsigned char *buf,
763 #if defined(POLARSSL_SSL_SESSION_TICKETS)
764 static int ssl_parse_session_ticket_ext(
ssl_context *ssl,
765 const unsigned char *buf,
782 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
783 static int ssl_parse_supported_point_formats_ext(
ssl_context *ssl,
784 const unsigned char *buf,
788 const unsigned char *p;
791 if( list_size + 1 != len )
798 while( list_size > 0 )
816 #if defined(POLARSSL_SSL_ALPN)
818 const unsigned char *buf,
size_t len )
820 size_t list_len, name_len;
841 list_len = ( buf[0] << 8 ) | buf[1];
842 if( list_len != len - 2 )
846 if( name_len != list_len - 1 )
850 for( p = ssl->
alpn_list; *p != NULL; p++ )
852 if( name_len == strlen( *p ) &&
853 memcmp( buf + 3, *p, name_len ) == 0 )
864 static int ssl_parse_server_hello(
ssl_context *ssl )
869 unsigned char *buf, *ext;
870 int renegotiation_info_seen = 0;
871 int handshake_failure = 0;
872 #if defined(POLARSSL_DEBUG_C)
920 SSL_DEBUG_MSG( 1, (
"server only supports ssl smaller than minimum"
930 #if defined(POLARSSL_DEBUG_C)
931 t = ( (uint32_t) buf[6] << 24 )
932 | ( (uint32_t) buf[7] << 16 )
933 | ( (uint32_t) buf[8] << 8 )
934 | ( (uint32_t) buf[9] );
935 SSL_DEBUG_MSG( 3, (
"server hello, current time: %lu", t ) );
942 SSL_DEBUG_BUF( 3,
"server hello, random bytes", buf + 6, 32 );
960 ext_len = ( ( buf[42 + n] << 8 )
963 if( ( ext_len > 0 && ext_len < 4 ) ||
971 i = ( buf[39 + n] << 8 ) | buf[40 + n];
981 SSL_DEBUG_MSG( 1, (
"ciphersuite info for %04x not found", i ) );
987 SSL_DEBUG_MSG( 3, (
"server hello, session id len.: %d", n ) );
1002 #if defined(POLARSSL_HAVE_TIME)
1024 SSL_DEBUG_MSG( 3, (
"server hello, chosen ciphersuite: %d", i ) );
1025 SSL_DEBUG_MSG( 3, (
"server hello, compress alg.: %d", buf[41 + n] ) );
1044 #
if defined(POLARSSL_ZLIB_SUPPORT)
1056 SSL_DEBUG_MSG( 2, (
"server hello, total extension length: %d", ext_len ) );
1060 unsigned int ext_id = ( ( ext[0] << 8 )
1062 unsigned int ext_size = ( ( ext[2] << 8 )
1065 if( ext_size + 4 > ext_len )
1075 renegotiation_info_seen = 1;
1077 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1083 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
1085 SSL_DEBUG_MSG( 3, (
"found max_fragment_length extension" ) );
1087 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1088 ext + 4, ext_size ) ) != 0 )
1096 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1100 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1101 ext + 4, ext_size ) ) != 0 )
1109 #if defined(POLARSSL_SSL_SESSION_TICKETS)
1113 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1114 ext + 4, ext_size ) ) != 0 )
1122 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
1124 SSL_DEBUG_MSG( 3, (
"found supported_point_formats extension" ) );
1126 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1127 ext + 4, ext_size ) ) != 0 )
1135 #if defined(POLARSSL_SSL_ALPN)
1139 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1146 SSL_DEBUG_MSG( 3, (
"unknown extension found: %d (ignoring)",
1150 ext_len -= 4 + ext_size;
1151 ext += 4 + ext_size;
1153 if( ext_len > 0 && ext_len < 4 )
1166 SSL_DEBUG_MSG( 1, (
"legacy renegotiation, breaking off handshake" ) );
1167 handshake_failure = 1;
1171 renegotiation_info_seen == 0 )
1173 SSL_DEBUG_MSG( 1, (
"renegotiation_info extension missing (secure)" ) );
1174 handshake_failure = 1;
1181 handshake_failure = 1;
1185 renegotiation_info_seen == 1 )
1187 SSL_DEBUG_MSG( 1, (
"renegotiation_info extension present (legacy)" ) );
1188 handshake_failure = 1;
1191 if( handshake_failure == 1 )
1204 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1205 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1206 static int ssl_parse_server_dh_params(
ssl_context *ssl,
unsigned char **p,
1207 unsigned char *end )
1229 SSL_DEBUG_MSG( 1, (
"bad server key exchange message (DHM length)" ) );
1242 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1243 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1244 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1245 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1246 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1247 static int ssl_check_server_ecdh_params(
const ssl_context *ssl )
1252 if( curve_info == NULL )
1260 #if defined(POLARSSL_SSL_ECP_SET_CURVES)
1278 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1279 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1280 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1281 static int ssl_parse_server_ecdh_params(
ssl_context *ssl,
1283 unsigned char *end )
1296 (
const unsigned char **) p, end ) ) != 0 )
1302 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1304 SSL_DEBUG_MSG( 1, (
"bad server key exchange message (ECDHE curve)" ) );
1314 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1315 static int ssl_parse_server_psk_hint(
ssl_context *ssl,
1317 unsigned char *end )
1328 len = (*p)[0] << 8 | (*p)[1];
1331 if( (*p) + len > end )
1333 SSL_DEBUG_MSG( 1, (
"bad server key exchange message (psk_identity_hint length)" ) );
1346 #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1347 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1351 static int ssl_write_encrypted_pms(
ssl_context *ssl,
1352 size_t offset,
size_t *olen,
1369 if( ( ret = ssl->
f_rng( ssl->
p_rng, p + 2, 46 ) ) != 0 )
1389 ssl->
out_msg + offset + len_bytes, olen,
1397 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1398 defined(POLARSSL_SSL_PROTO_TLS1_2)
1399 if( len_bytes == 2 )
1401 ssl->
out_msg[offset+0] = (
unsigned char)( *olen >> 8 );
1402 ssl->
out_msg[offset+1] = (
unsigned char)( *olen );
1412 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
1413 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1414 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1415 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1416 static int ssl_parse_signature_algorithm(
ssl_context *ssl,
1432 if( (*p) + 2 > end )
1441 "HashAlgorithm %d", *(p)[0] ) );
1451 "SignatureAlgorithm %d", (*p)[1] ) );
1455 SSL_DEBUG_MSG( 2, (
"Server used SignatureAlgorithm %d", (*p)[1] ) );
1456 SSL_DEBUG_MSG( 2, (
"Server used HashAlgorithm %d", (*p)[0] ) );
1467 #if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1468 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1469 static int ssl_get_ecdh_params_from_cert(
ssl_context *ssl )
1490 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1492 SSL_DEBUG_MSG( 1, (
"bad server certificate (ECDH curve)" ) );
1501 static int ssl_parse_server_key_exchange(
ssl_context *ssl )
1505 unsigned char *p, *end;
1506 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1507 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1508 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1509 size_t sig_len, params_len;
1510 unsigned char hash[64];
1518 #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
1521 SSL_DEBUG_MSG( 2, (
"<= skip parse server key exchange" ) );
1529 #if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1530 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1534 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1540 SSL_DEBUG_MSG( 2, (
"<= skip parse server key exchange" ) );
1582 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1588 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1596 #if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1597 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1604 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1605 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1609 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
1618 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1619 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1620 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1625 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
1640 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1641 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1642 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1647 params_len = p - ( ssl->
in_msg + 4 );
1652 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
1655 if( ssl_parse_signature_algorithm( ssl, &p, end,
1656 &md_alg, &pk_alg ) != 0 )
1670 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1671 defined(POLARSSL_SSL_PROTO_TLS1_1)
1690 sig_len = ( p[0] << 8 ) | p[1];
1693 if( end != p + sig_len )
1704 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1705 defined(POLARSSL_SSL_PROTO_TLS1_1)
1739 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1740 defined(POLARSSL_SSL_PROTO_TLS1_2)
1776 SSL_DEBUG_BUF( 3,
"parameters hash", hash, hashlen != 0 ? hashlen :
1789 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
1807 #if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1808 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
1809 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1810 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1811 static int ssl_parse_certificate_request(
ssl_context *ssl )
1823 SSL_DEBUG_MSG( 2, (
"<= skip parse certificate request" ) );
1832 static int ssl_parse_certificate_request(
ssl_context *ssl )
1835 unsigned char *buf, *p;
1836 size_t n = 0, m = 0;
1837 size_t cert_type_len = 0, dn_len = 0;
1847 SSL_DEBUG_MSG( 2, (
"<= skip parse certificate request" ) );
1902 cert_type_len = buf[4];
1912 while( cert_type_len > 0 )
1914 #if defined(POLARSSL_RSA_C)
1923 #if defined(POLARSSL_ECDSA_C)
1940 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
1945 size_t sig_alg_len = ( ( buf[5 + n] << 8 )
1962 dn_len = ( ( buf[5 + m + n] << 8 )
1963 | ( buf[6 + m + n] ) );
1982 static int ssl_parse_server_hello_done(
ssl_context *ssl )
2018 static int ssl_write_client_key_exchange(
ssl_context *ssl )
2026 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
2034 ssl->
out_msg[4] = (
unsigned char)( n >> 8 );
2035 ssl->
out_msg[5] = (
unsigned char)( n );
2066 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2067 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2068 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2069 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2109 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
2123 ssl->
out_msg[i++] = (
unsigned char)( n >> 8 );
2124 ssl->
out_msg[i++] = (
unsigned char)( n );
2129 #if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2136 #if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2139 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2144 #if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2151 ssl->
out_msg[i++] = (
unsigned char)( n >> 8 );
2152 ssl->
out_msg[i++] = (
unsigned char)( n );
2166 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2199 #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2203 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
2209 ((void) ciphersuite_info);
2237 #if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2238 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2239 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2240 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2241 static int ssl_write_certificate_verify(
ssl_context *ssl )
2262 static int ssl_write_certificate_verify(
ssl_context *ssl )
2266 size_t n = 0, offset = 0;
2267 unsigned char hash[48];
2268 unsigned char *hash_start = hash;
2270 unsigned int hashlen;
2302 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2303 defined(POLARSSL_SSL_PROTO_TLS1_1)
2334 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2377 ssl->
out_msg + 6 + offset, &n,
2384 ssl->
out_msg[4 + offset] = (
unsigned char)( n >> 8 );
2385 ssl->
out_msg[5 + offset] = (
unsigned char)( n );
2407 #if defined(POLARSSL_SSL_SESSION_TICKETS)
2408 static int ssl_parse_new_session_ticket(
ssl_context *ssl )
2413 unsigned char *ticket;
2448 lifetime = ( ssl->
in_msg[4] << 24 ) | ( ssl->
in_msg[5] << 16 ) |
2451 ticket_len = ( ssl->
in_msg[8] << 8 ) | ( ssl->
in_msg[9] );
2453 if( ticket_len + 10 != ssl->
in_hslen )
2468 if( ticket_len == 0)
2481 memcpy( ticket, ssl->
in_msg + 10, ticket_len );
2492 SSL_DEBUG_MSG( 3, (
"ticket in use, discarding session id" ) );
2516 switch( ssl->
state )
2526 ret = ssl_write_client_hello( ssl );
2537 ret = ssl_parse_server_hello( ssl );
2545 ret = ssl_parse_server_key_exchange( ssl );
2549 ret = ssl_parse_certificate_request( ssl );
2553 ret = ssl_parse_server_hello_done( ssl );
2568 ret = ssl_write_client_key_exchange( ssl );
2572 ret = ssl_write_certificate_verify( ssl );
2589 #if defined(POLARSSL_SSL_SESSION_TICKETS)
2591 ret = ssl_parse_new_session_ticket( ssl );
#define SSL_HS_CLIENT_KEY_EXCHANGE
#define SSL_CERT_TYPE_ECDSA_SIGN
#define SSL_ALERT_LEVEL_FATAL
int ecdh_make_public(ecdh_context *ctx, size_t *olen, unsigned char *buf, size_t blen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Setup and export the client's public value.
int ssl_send_alert_message(ssl_context *ssl, unsigned char level, unsigned char message)
Send an alert message.
int(* f_rng)(void *, unsigned char *, size_t)
#define TLS_EXT_SERVERNAME_HOSTNAME
#define POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE
Processing of the ServerKeyExchange handshake message failed.
#define SSL_DEBUG_RET(level, text, ret)
#define POLARSSL_ERR_SSL_PK_TYPE_MISMATCH
Public key type mismatch (eg, asked for RSA key exchange and presented EC key)
#define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE
Processing of the ServerHelloDone handshake message failed.
#define POLARSSL_ECP_PF_COMPRESSED
Compressed point format.
pk_type_t ssl_pk_alg_from_sig(unsigned char sig)
#define POLARSSL_MPI_MAX_SIZE
Maximum number of bytes for usable MPIs.
int ecdh_calc_secret(ecdh_context *ctx, size_t *olen, unsigned char *buf, size_t blen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Derive and export the shared secret.
char peer_verify_data[36]
#define SSL_HS_CLIENT_HELLO
const ecp_curve_info * ecp_curve_list(void)
Get the list of supported curves in order of preferrence (full information)
void sha1(const unsigned char *input, size_t ilen, unsigned char output[20])
Output = SHA-1( input buffer )
void(* calc_verify)(ssl_context *, unsigned char *)
void sha1_finish(sha1_context *ctx, unsigned char output[20])
SHA-1 final digest.
int md_starts(md_context_t *ctx)
Set-up the given context for a new message digest.
#define SSL_HS_SERVER_KEY_EXCHANGE
#define TLS_EXT_TRUNCATED_HMAC
#define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO
Processing of the ServerHello handshake message failed.
#define SSL_HS_NEW_SESSION_TICKET
ssl_session * session_negotiate
int ssl_parse_certificate(ssl_context *ssl)
#define SSL_SESSION_TICKETS_DISABLED
const int * ciphersuite_list[4]
int ssl_parse_finished(ssl_context *ssl)
int md_init_ctx(md_context_t *ctx, const md_info_t *md_info)
Initialises and fills the message digest context structure with the appropriate values.
#define SSL_RENEGOTIATION
unsigned char premaster[POLARSSL_PREMASTER_SIZE]
#define POLARSSL_ECP_PF_UNCOMPRESSED
Uncompressed point format.
#define POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET
Processing of the NewSessionTicket handshake message failed.
int ssl_write_finished(ssl_context *ssl)
Configuration options (set of defines)
#define SSL_DEBUG_MSG(level, args)
#define SSL_TRUNC_HMAC_ENABLED
void ssl_handshake_wrapup(ssl_context *ssl)
int ecdh_get_params(ecdh_context *ctx, const ecp_keypair *key, ecdh_side side)
Setup an ECDH context from an EC key.
void md5_finish(md5_context *ctx, unsigned char output[16])
MD5 final digest.
#define SSL_MAX_MAJOR_VERSION
#define pk_ec(pk)
Quick access to an EC context inside a PK context.
#define SSL_LEGACY_NO_RENEGOTIATION
#define SSL_MAJOR_VERSION_3
pk_type_t ssl_get_ciphersuite_sig_pk_alg(const ssl_ciphersuite_t *info)
int dhm_read_params(dhm_context *ctx, unsigned char **p, const unsigned char *end)
Parse the ServerKeyExchange parameters.
const md_info_t * md_info_from_type(md_type_t md_type)
Returns the message digest information associated with the given digest type.
#define SSL_CERT_TYPE_RSA_SIGN
#define SSL_HS_CERTIFICATE_REQUEST
ssl_handshake_params * handshake
#define SSL_MSG_HANDSHAKE
int ssl_write_certificate(ssl_context *ssl)
#define SSL_ALERT_MSG_PROTOCOL_VERSION
#define POLARSSL_ERR_SSL_NO_RNG
No RNG was provided to the SSL module.
#define SSL_TRUNC_HMAC_DISABLED
#define SSL_MINOR_VERSION_0
int pk_verify(pk_context *ctx, md_type_t md_alg, const unsigned char *hash, size_t hash_len, const unsigned char *sig, size_t sig_len)
Verify signature.
#define POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION
Handshake protocol not within min/max boundaries.
#define SSL_HS_SERVER_HELLO_DONE
static x509_crt * ssl_own_cert(ssl_context *ssl)
int ssl_handshake_client_step(ssl_context *ssl)
#define POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE
An unexpected message was received from our peer.
const ecp_curve_info * ecp_curve_info_from_grp_id(ecp_group_id grp_id)
Get curve information from an internal group identifier.
key_exchange_type_t key_exchange
Curve information for use by other modules.
int pk_can_do(pk_context *ctx, pk_type_t type)
Tell if a context can do the operation given by type.
void md5_starts(md5_context *ctx)
MD5 context setup.
unsigned char ssl_sig_from_pk(pk_context *pk)
int ssl_flush_output(ssl_context *ssl)
#define TLS_EXT_SESSION_TICKET
#define SSL_HS_SERVER_HELLO
#define SSL_COMPRESS_DEFLATE
#define SSL_MINOR_VERSION_3
#define TLS_EXT_RENEGOTIATION_INFO
pk_type_t
Public key types.
#define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST
Processing of the CertificateRequest handshake message failed.
int ssl_parse_change_cipher_spec(ssl_context *ssl)
int ecdh_read_params(ecdh_context *ctx, const unsigned char **buf, const unsigned char *end)
Parse the ServerKeyExhange parameters.
void sha1_starts(sha1_context *ctx)
SHA-1 context setup.
#define SSL_EMPTY_RENEGOTIATION_INFO
renegotiation info ext
This structure is used for storing ciphersuite information.
#define SSL_DEBUG_BUF(level, text, buf, len)
#define POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED
The own private key or pre-shared key is not set, but needed.
#define SSL_INITIAL_HANDSHAKE
#define SSL_MAX_MINOR_VERSION
int allow_legacy_renegotiation
#define SSL_COMPRESS_NULL
const ssl_ciphersuite_t * ssl_ciphersuite_from_id(int ciphersuite_id)
int ssl_read_record(ssl_context *ssl)
ecp_group_id
Domain parameters (curve, subgroup and generator) identifiers.
#define TLS_EXT_MAX_FRAGMENT_LENGTH
#define SSL_MAX_FRAG_LEN_NONE
#define SSL_HS_CERTIFICATE_VERIFY
#define TLS_EXT_SERVERNAME
int pk_sign(pk_context *ctx, md_type_t md_alg, const unsigned char *hash, size_t hash_len, unsigned char *sig, size_t *sig_len, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Make signature.
int dhm_make_public(dhm_context *ctx, int x_size, unsigned char *output, size_t olen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Create own private value X and export G^X.
#define SSL_DEBUG_MPI(level, text, X)
size_t mpi_size(const mpi *X)
Return the total size in bytes.
#define SSL_LEGACY_BREAK_HANDSHAKE
int pk_encrypt(pk_context *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen, size_t osize, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Encrypt message.
pk_context pk
Container for the public key context.
ssl_transform * transform_negotiate
#define SSL_LEGACY_RENEGOTIATION
#define SSL_SECURE_RENEGOTIATION
#define POLARSSL_ERR_SSL_MALLOC_FAILED
Memory allocation failed.
void sha1_update(sha1_context *ctx, const unsigned char *input, size_t ilen)
SHA-1 process buffer.
#define TLS_EXT_SUPPORTED_POINT_FORMATS
void md5_update(md5_context *ctx, const unsigned char *input, size_t ilen)
MD5 process buffer.
int ssl_write_change_cipher_spec(ssl_context *ssl)
#define SSL_DEBUG_ECP(level, text, X)
int ssl_derive_keys(ssl_context *ssl)
static pk_context * ssl_own_key(ssl_context *ssl)
int md_finish(md_context_t *ctx, unsigned char *output)
Generic message digest final digest.
int ssl_psk_derive_premaster(ssl_context *ssl, key_exchange_type_t key_ex)
static int safer_memcmp(const void *a, const void *b, size_t n)
int md_free_ctx(md_context_t *ctx)
Free the message-specific context of ctx.
int ssl_send_fatal_handshake_failure(ssl_context *ssl)
#define SSL_MAX_CONTENT_LEN
Size of the input / output buffer.
unsigned char * psk_identity
#define TLS_EXT_SUPPORTED_ELLIPTIC_CURVES
void md5(const unsigned char *input, size_t ilen, unsigned char output[16])
Output = MD5( input buffer )
#define POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE
The requested feature is not available.
#define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE
Processing of the Certificate handshake message failed.
int md_update(md_context_t *ctx, const unsigned char *input, size_t ilen)
Generic message digest process buffer.
#define POLARSSL_ERR_SSL_BAD_INPUT_DATA
Bad input parameters to function.
int ssl_write_record(ssl_context *ssl)
unsigned char randbytes[64]
Generic message digest context.
void ssl_optimize_checksum(ssl_context *ssl, const ssl_ciphersuite_t *ciphersuite_info)
md_type_t ssl_md_alg_from_hash(unsigned char hash)
int dhm_calc_secret(dhm_context *ctx, unsigned char *output, size_t *olen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Derive and export the shared secret (G^Y)^X mod P.