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)
49typedef UINT32 uint32_t;
54#if defined(POLARSSL_HAVE_TIME)
58#if defined(POLARSSL_SSL_SESSION_TICKETS)
60static void polarssl_zeroize(
void *v,
size_t n ) {
61 volatile unsigned char *p = v;
while( n-- ) *p++ = 0;
65#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
66static void ssl_write_hostname_ext(
ssl_context *ssl,
70 unsigned char *p = buf;
77 SSL_DEBUG_MSG( 3, (
"client hello, adding server name extension: %s",
101 *p++ = (
unsigned char)( ( (ssl->
hostname_len + 5) >> 8 ) & 0xFF );
102 *p++ = (
unsigned char)( ( (ssl->
hostname_len + 5) ) & 0xFF );
104 *p++ = (
unsigned char)( ( (ssl->
hostname_len + 3) >> 8 ) & 0xFF );
105 *p++ = (
unsigned char)( ( (ssl->
hostname_len + 3) ) & 0xFF );
108 *p++ = (
unsigned char)( ( ssl->
hostname_len >> 8 ) & 0xFF );
117static void ssl_write_renegotiation_ext(
ssl_context *ssl,
121 unsigned char *p = buf;
128 SSL_DEBUG_MSG( 3, (
"client hello, adding renegotiation extension" ) );
145#if defined(POLARSSL_SSL_PROTO_TLS1_2)
146static void ssl_write_signature_algorithms_ext(
ssl_context *ssl,
150 unsigned char *p = buf;
151 size_t sig_alg_len = 0;
152#if defined(POLARSSL_RSA_C) || defined(POLARSSL_ECDSA_C)
153 unsigned char *sig_alg_list = buf + 6;
161 SSL_DEBUG_MSG( 3, (
"client hello, adding signature_algorithms extension" ) );
166#if defined(POLARSSL_RSA_C)
167#if defined(POLARSSL_SHA512_C)
173#if defined(POLARSSL_SHA256_C)
179#if defined(POLARSSL_SHA1_C)
183#if defined(POLARSSL_MD5_C)
188#if defined(POLARSSL_ECDSA_C)
189#if defined(POLARSSL_SHA512_C)
195#if defined(POLARSSL_SHA256_C)
201#if defined(POLARSSL_SHA1_C)
205#if defined(POLARSSL_MD5_C)
231 *p++ = (
unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF );
232 *p++ = (
unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF );
234 *p++ = (
unsigned char)( ( sig_alg_len >> 8 ) & 0xFF );
235 *p++ = (
unsigned char)( ( sig_alg_len ) & 0xFF );
237 *olen = 6 + sig_alg_len;
241#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
242static void ssl_write_supported_elliptic_curves_ext(
ssl_context *ssl,
246 unsigned char *p = buf;
247 unsigned char *elliptic_curve_list = p + 6;
248 size_t elliptic_curve_len = 0;
250#if defined(POLARSSL_SSL_SET_CURVES)
258 SSL_DEBUG_MSG( 3, (
"client hello, adding supported_elliptic_curves extension" ) );
260#if defined(POLARSSL_SSL_SET_CURVES)
269 elliptic_curve_list[elliptic_curve_len++] = info->
tls_id >> 8;
270 elliptic_curve_list[elliptic_curve_len++] = info->
tls_id & 0xFF;
273 if( elliptic_curve_len == 0 )
279 *p++ = (
unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
280 *p++ = (
unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
282 *p++ = (
unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
283 *p++ = (
unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
285 *olen = 6 + elliptic_curve_len;
288static void ssl_write_supported_point_formats_ext(
ssl_context *ssl,
292 unsigned char *p = buf;
297 SSL_DEBUG_MSG( 3, (
"client hello, adding supported_point_formats extension" ) );
312#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
313static void ssl_write_max_fragment_length_ext(
ssl_context *ssl,
317 unsigned char *p = buf;
324 SSL_DEBUG_MSG( 3, (
"client hello, adding max_fragment_length extension" ) );
338#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
339static void ssl_write_truncated_hmac_ext(
ssl_context *ssl,
340 unsigned char *buf,
size_t *olen )
342 unsigned char *p = buf;
350 SSL_DEBUG_MSG( 3, (
"client hello, adding truncated_hmac extension" ) );
362#if defined(POLARSSL_SSL_SESSION_TICKETS)
363static void ssl_write_session_ticket_ext(
ssl_context *ssl,
364 unsigned char *buf,
size_t *olen )
366 unsigned char *p = buf;
375 SSL_DEBUG_MSG( 3, (
"client hello, adding session ticket extension" ) );
380 *p++ = (
unsigned char)( ( tlen >> 8 ) & 0xFF );
381 *p++ = (
unsigned char)( ( tlen ) & 0xFF );
391 SSL_DEBUG_MSG( 3, (
"sending session ticket of length %d", tlen ) );
399#if defined(POLARSSL_SSL_ALPN)
401 unsigned char *buf,
size_t *olen )
403 unsigned char *p = buf;
412 SSL_DEBUG_MSG( 3, (
"client hello, adding alpn extension" ) );
428 for( cur = ssl->
alpn_list; *cur != NULL; cur++ )
430 *p = (
unsigned char)( strlen( *cur ) & 0xFF );
431 memcpy( p + 1, *cur, *p );
438 buf[4] = (
unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
439 buf[5] = (
unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
442 buf[2] = (
unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
443 buf[3] = (
unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
447static int ssl_write_client_hello(
ssl_context *ssl )
450 size_t i, n, olen, ext_len = 0;
452 unsigned char *p, *q;
453#if defined(POLARSSL_HAVE_TIME)
456 const int *ciphersuites;
461 if( ssl->
f_rng == NULL )
495#if defined(POLARSSL_HAVE_TIME)
497 *p++ = (
unsigned char)( t >> 24 );
498 *p++ = (
unsigned char)( t >> 16 );
499 *p++ = (
unsigned char)( t >> 8 );
500 *p++ = (
unsigned char)( t );
502 SSL_DEBUG_MSG( 3, (
"client hello, current time: %lu", t ) );
504 if( ( ret = ssl->
f_rng( ssl->
p_rng, p, 4 ) ) != 0 )
510 if( ( ret = ssl->
f_rng( ssl->
p_rng, p, 28 ) ) != 0 )
517 SSL_DEBUG_BUF( 3,
"client hello, random bytes", buf + 6, 32 );
537#if defined(POLARSSL_SSL_SESSION_TICKETS)
555 *p++ = (
unsigned char) n;
557 for( i = 0; i < n; i++ )
560 SSL_DEBUG_MSG( 3, (
"client hello, session id len.: %d", n ) );
580 for( i = 0; ciphersuites[i] != 0; i++ )
584 if( ciphersuite_info == NULL )
595 *p++ = (
unsigned char)( ciphersuites[i] >> 8 );
596 *p++ = (
unsigned char)( ciphersuites[i] );
599 *q++ = (
unsigned char)( n >> 7 );
600 *q++ = (
unsigned char)( n << 1 );
602 SSL_DEBUG_MSG( 3, (
"client hello, got %d ciphersuites", n ) );
605#if defined(POLARSSL_ZLIB_SUPPORT)
606 SSL_DEBUG_MSG( 3, (
"client hello, compress len.: %d", 2 ) );
614 SSL_DEBUG_MSG( 3, (
"client hello, compress len.: %d", 1 ) );
623#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
624 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
628 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
631#if defined(POLARSSL_SSL_PROTO_TLS1_2)
632 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
636#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
637 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
640 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
644#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
645 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
649#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
650 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
654#if defined(POLARSSL_SSL_SESSION_TICKETS)
655 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
659#if defined(POLARSSL_SSL_ALPN)
660 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
664 SSL_DEBUG_MSG( 3, (
"client hello, total extension length: %d",
669 *p++ = (
unsigned char)( ( ext_len >> 8 ) & 0xFF );
670 *p++ = (
unsigned char)( ( ext_len ) & 0xFF );
691static int ssl_parse_renegotiation_info(
ssl_context *ssl,
692 const unsigned char *buf,
699 if( len != 1 || buf[0] != 0x0 )
701 SSL_DEBUG_MSG( 1, (
"non-zero length renegotiated connection field" ) );
721 SSL_DEBUG_MSG( 1, (
"non-matching renegotiated connection field" ) );
733#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
734static int ssl_parse_max_fragment_length_ext(
ssl_context *ssl,
735 const unsigned char *buf,
753#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
754static int ssl_parse_truncated_hmac_ext(
ssl_context *ssl,
755 const unsigned char *buf,
772#if defined(POLARSSL_SSL_SESSION_TICKETS)
773static int ssl_parse_session_ticket_ext(
ssl_context *ssl,
774 const unsigned char *buf,
791#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
792static int ssl_parse_supported_point_formats_ext(
ssl_context *ssl,
793 const unsigned char *buf,
797 const unsigned char *p;
800 if( list_size + 1 != len )
807 while( list_size > 0 )
826#if defined(POLARSSL_SSL_ALPN)
828 const unsigned char *buf,
size_t len )
830 size_t list_len, name_len;
851 list_len = ( buf[0] << 8 ) | buf[1];
852 if( list_len != len - 2 )
856 if( name_len != list_len - 1 )
860 for( p = ssl->
alpn_list; *p != NULL; p++ )
862 if( name_len == strlen( *p ) &&
863 memcmp( buf + 3, *p, name_len ) == 0 )
874static int ssl_parse_server_hello(
ssl_context *ssl )
879 unsigned char *buf, *ext;
880 int renegotiation_info_seen = 0;
881 int handshake_failure = 0;
882#if defined(POLARSSL_DEBUG_C)
913 "but not honored by server" ) );
917 SSL_DEBUG_MSG( 1, (
"non-handshake message during renego" ) );
946 SSL_DEBUG_MSG( 1, (
"server only supports ssl smaller than minimum"
956#if defined(POLARSSL_DEBUG_C)
957 t = ( (uint32_t) buf[6] << 24 )
958 | ( (uint32_t) buf[7] << 16 )
959 | ( (uint32_t) buf[8] << 8 )
960 | ( (uint32_t) buf[9] );
961 SSL_DEBUG_MSG( 3, (
"server hello, current time: %lu", t ) );
968 SSL_DEBUG_BUF( 3,
"server hello, random bytes", buf + 6, 32 );
986 ext_len = ( ( buf[42 + n] << 8 )
989 if( ( ext_len > 0 && ext_len < 4 ) ||
1006 i = ( buf[39 + n] << 8 ) | buf[40 + n];
1016 SSL_DEBUG_MSG( 1, (
"ciphersuite info for %04x not found", i ) );
1022 SSL_DEBUG_MSG( 3, (
"server hello, session id len.: %d", n ) );
1023 SSL_DEBUG_BUF( 3,
"server hello, session id", buf + 39, n );
1037#if defined(POLARSSL_HAVE_TIME)
1059 SSL_DEBUG_MSG( 3, (
"server hello, chosen ciphersuite: %d", i ) );
1060 SSL_DEBUG_MSG( 3, (
"server hello, compress alg.: %d", buf[41 + n] ) );
1091 SSL_DEBUG_MSG( 2, (
"server hello, total extension length: %d", ext_len ) );
1095 unsigned int ext_id = ( ( ext[0] << 8 )
1097 unsigned int ext_size = ( ( ext[2] << 8 )
1100 if( ext_size + 4 > ext_len )
1110 renegotiation_info_seen = 1;
1112 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1118#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
1120 SSL_DEBUG_MSG( 3, (
"found max_fragment_length extension" ) );
1122 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1123 ext + 4, ext_size ) ) != 0 )
1131#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1135 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1136 ext + 4, ext_size ) ) != 0 )
1144#if defined(POLARSSL_SSL_SESSION_TICKETS)
1148 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1149 ext + 4, ext_size ) ) != 0 )
1157#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
1159 SSL_DEBUG_MSG( 3, (
"found supported_point_formats extension" ) );
1161 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1162 ext + 4, ext_size ) ) != 0 )
1170#if defined(POLARSSL_SSL_ALPN)
1174 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1181 SSL_DEBUG_MSG( 3, (
"unknown extension found: %d (ignoring)",
1185 ext_len -= 4 + ext_size;
1186 ext += 4 + ext_size;
1188 if( ext_len > 0 && ext_len < 4 )
1201 SSL_DEBUG_MSG( 1, (
"legacy renegotiation, breaking off handshake" ) );
1202 handshake_failure = 1;
1206 renegotiation_info_seen == 0 )
1208 SSL_DEBUG_MSG( 1, (
"renegotiation_info extension missing (secure)" ) );
1209 handshake_failure = 1;
1216 handshake_failure = 1;
1220 renegotiation_info_seen == 1 )
1222 SSL_DEBUG_MSG( 1, (
"renegotiation_info extension present (legacy)" ) );
1223 handshake_failure = 1;
1226 if( handshake_failure == 1 )
1239#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1240 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1241static int ssl_parse_server_dh_params(
ssl_context *ssl,
unsigned char **p,
1242 unsigned char *end )
1264 SSL_DEBUG_MSG( 1, (
"bad server key exchange message (DHM length)" ) );
1277#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1278 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1279 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1280 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1281 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1282static int ssl_check_server_ecdh_params(
const ssl_context *ssl )
1287 if( curve_info == NULL )
1295#if defined(POLARSSL_SSL_ECP_SET_CURVES)
1313#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1314 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1315 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1316static int ssl_parse_server_ecdh_params(
ssl_context *ssl,
1318 unsigned char *end )
1331 (
const unsigned char **) p, end ) ) != 0 )
1337 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1339 SSL_DEBUG_MSG( 1, (
"bad server key exchange message (ECDHE curve)" ) );
1349#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1350static int ssl_parse_server_psk_hint(
ssl_context *ssl,
1352 unsigned char *end )
1363 len = (*p)[0] << 8 | (*p)[1];
1366 if( (*p) + len > end )
1368 SSL_DEBUG_MSG( 1, (
"bad server key exchange message (psk_identity_hint length)" ) );
1381#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1382 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1386static int ssl_write_encrypted_pms(
ssl_context *ssl,
1387 size_t offset,
size_t *olen,
1404 if( ( ret = ssl->
f_rng( ssl->
p_rng, p + 2, 46 ) ) != 0 )
1424 ssl->
out_msg + offset + len_bytes, olen,
1432#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1433 defined(POLARSSL_SSL_PROTO_TLS1_2)
1434 if( len_bytes == 2 )
1436 ssl->
out_msg[offset+0] = (
unsigned char)( *olen >> 8 );
1437 ssl->
out_msg[offset+1] = (
unsigned char)( *olen );
1447#if defined(POLARSSL_SSL_PROTO_TLS1_2)
1448#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1449 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1450 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1451static int ssl_parse_signature_algorithm(
ssl_context *ssl,
1467 if( (*p) + 2 > end )
1476 "HashAlgorithm %d", *(p)[0] ) );
1486 "SignatureAlgorithm %d", (*p)[1] ) );
1490 SSL_DEBUG_MSG( 2, (
"Server used SignatureAlgorithm %d", (*p)[1] ) );
1491 SSL_DEBUG_MSG( 2, (
"Server used HashAlgorithm %d", (*p)[0] ) );
1502#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1503 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1504static int ssl_get_ecdh_params_from_cert(
ssl_context *ssl )
1525 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1527 SSL_DEBUG_MSG( 1, (
"bad server certificate (ECDH curve)" ) );
1536static int ssl_parse_server_key_exchange(
ssl_context *ssl )
1540 unsigned char *p, *end;
1541#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1542 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1543 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1544 size_t sig_len, params_len;
1545 unsigned char hash[64];
1553#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
1556 SSL_DEBUG_MSG( 2, (
"<= skip parse server key exchange" ) );
1564#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1565 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1569 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1575 SSL_DEBUG_MSG( 2, (
"<= skip parse server key exchange" ) );
1617#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1623 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1631#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1632 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1639#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1640 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1644 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
1653#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1654 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1655 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1660 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
1675#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1676 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1677 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1682 params_len = p - ( ssl->
in_msg + 4 );
1687#if defined(POLARSSL_SSL_PROTO_TLS1_2)
1690 if( ssl_parse_signature_algorithm( ssl, &p, end,
1691 &md_alg, &pk_alg ) != 0 )
1705#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1706 defined(POLARSSL_SSL_PROTO_TLS1_1)
1725 sig_len = ( p[0] << 8 ) | p[1];
1728 if( end != p + sig_len )
1739#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1740 defined(POLARSSL_SSL_PROTO_TLS1_1)
1780#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1781 defined(POLARSSL_SSL_PROTO_TLS1_2)
1819 SSL_DEBUG_BUF( 3,
"parameters hash", hash, hashlen != 0 ? hashlen :
1832 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
1850#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1851 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
1852 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1853 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1854static int ssl_parse_certificate_request(
ssl_context *ssl )
1865 SSL_DEBUG_MSG( 2, (
"<= skip parse certificate request" ) );
1874static int ssl_parse_certificate_request(
ssl_context *ssl )
1877 unsigned char *buf, *p;
1878 size_t n = 0, m = 0;
1879 size_t cert_type_len = 0, dn_len = 0;
1889 SSL_DEBUG_MSG( 2, (
"<= skip parse certificate request" ) );
1944 cert_type_len = buf[4];
1954 while( cert_type_len > 0 )
1956#if defined(POLARSSL_RSA_C)
1965#if defined(POLARSSL_ECDSA_C)
1982#if defined(POLARSSL_SSL_PROTO_TLS1_2)
1987 size_t sig_alg_len = ( ( buf[5 + n] << 8 )
2004 dn_len = ( ( buf[5 + m + n] << 8 )
2005 | ( buf[6 + m + n] ) );
2024static int ssl_parse_server_hello_done(
ssl_context *ssl )
2060static int ssl_write_client_key_exchange(
ssl_context *ssl )
2068#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
2076 ssl->
out_msg[4] = (
unsigned char)( n >> 8 );
2077 ssl->
out_msg[5] = (
unsigned char)( n );
2108#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2109 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2110 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2111 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2151#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
2165 ssl->
out_msg[i++] = (
unsigned char)( n >> 8 );
2166 ssl->
out_msg[i++] = (
unsigned char)( n );
2171#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2178#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2181 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2186#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2193 ssl->
out_msg[i++] = (
unsigned char)( n >> 8 );
2194 ssl->
out_msg[i++] = (
unsigned char)( n );
2208#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2241#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2245 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
2251 ((void) ciphersuite_info);
2279#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2280 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2281 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2282 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2283static int ssl_write_certificate_verify(
ssl_context *ssl )
2303static int ssl_write_certificate_verify(
ssl_context *ssl )
2307 size_t n = 0, offset = 0;
2308 unsigned char hash[48];
2309 unsigned char *hash_start = hash;
2311 unsigned int hashlen;
2343#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2344 defined(POLARSSL_SSL_PROTO_TLS1_1)
2375#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2418 ssl->
out_msg + 6 + offset, &n,
2425 ssl->
out_msg[4 + offset] = (
unsigned char)( n >> 8 );
2426 ssl->
out_msg[5 + offset] = (
unsigned char)( n );
2448#if defined(POLARSSL_SSL_SESSION_TICKETS)
2449static int ssl_parse_new_session_ticket(
ssl_context *ssl )
2454 unsigned char *ticket;
2489 lifetime = ( ssl->
in_msg[4] << 24 ) | ( ssl->
in_msg[5] << 16 ) |
2492 ticket_len = ( ssl->
in_msg[8] << 8 ) | ( ssl->
in_msg[9] );
2494 if( ticket_len + 10 != ssl->
in_hslen )
2509 if( ticket_len == 0 )
2524 memcpy( ticket, ssl->
in_msg + 10, ticket_len );
2535 SSL_DEBUG_MSG( 3, (
"ticket in use, discarding session id" ) );
2559 switch( ssl->
state )
2569 ret = ssl_write_client_hello( ssl );
2580 ret = ssl_parse_server_hello( ssl );
2588 ret = ssl_parse_server_key_exchange( ssl );
2592 ret = ssl_parse_certificate_request( ssl );
2596 ret = ssl_parse_server_hello_done( ssl );
2611 ret = ssl_write_client_key_exchange( ssl );
2615 ret = ssl_write_certificate_verify( ssl );
2632#if defined(POLARSSL_SSL_SESSION_TICKETS)
2634 ret = ssl_parse_new_session_ticket( ssl );
size_t mpi_size(const mpi *X)
Return the total size in bytes.
#define SSL_MAX_CONTENT_LEN
#define POLARSSL_MPI_MAX_SIZE
Configuration options (set of defines)
#define POLARSSL_ZLIB_SUPPORT
If set, the SSL/TLS module uses ZLIB to support compression and decompression of packet data.
#define SSL_DEBUG_MPI(level, text, X)
#define SSL_DEBUG_ECP(level, text, X)
#define SSL_DEBUG_BUF(level, text, buf, len)
#define SSL_DEBUG_RET(level, text, ret)
#define SSL_DEBUG_MSG(level, args)
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.
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.
int dhm_read_params(dhm_context *ctx, unsigned char **p, const unsigned char *end)
Parse the ServerKeyExchange parameters.
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.
int ecdh_read_params(ecdh_context *ctx, const unsigned char **buf, const unsigned char *end)
Parse and procress a TLS ServerKeyExhange payload.
int ecdh_get_params(ecdh_context *ctx, const ecp_keypair *key, ecdh_side side)
Setup an ECDH context from an EC key.
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)
Generate a public key and a TLS ClientKeyExchange payload.
const ecp_curve_info * ecp_curve_info_from_grp_id(ecp_group_id grp_id)
Get curve information from an internal group identifier.
ecp_group_id
Domain parameters (curve, subgroup and generator) identifiers.
#define POLARSSL_ECP_PF_UNCOMPRESSED
Uncompressed point format.
#define POLARSSL_ECP_PF_COMPRESSED
Compressed point format.
const ecp_curve_info * ecp_curve_list(void)
Get the list of supported curves in order of preferrence (full information)
pk_context pk
Container for the public key context.
void md5_free(md5_context *ctx)
Clear MD5 context.
void md5_init(md5_context *ctx)
Initialize MD5 context.
void md5_update(md5_context *ctx, const unsigned char *input, size_t ilen)
MD5 process buffer.
void md5_finish(md5_context *ctx, unsigned char output[16])
MD5 final digest.
void md5(const unsigned char *input, size_t ilen, unsigned char output[16])
Output = MD5( input buffer )
void md5_starts(md5_context *ctx)
MD5 context setup.
int md_starts(md_context_t *ctx)
Set-up the given context for a new message digest.
const md_info_t * md_info_from_type(md_type_t md_type)
Returns the message digest information associated with the given digest type.
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.
void md_free(md_context_t *ctx)
Free and clear the message-specific context of ctx.
int md_finish(md_context_t *ctx, unsigned char *output)
Generic message digest final digest.
void md_init(md_context_t *ctx)
Initialize a md_context (as NONE)
int md_update(md_context_t *ctx, const unsigned char *input, size_t ilen)
Generic message digest process buffer.
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 (including padding if relevant).
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, including padding if relevant.
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 (including padding if relevant).
#define pk_ec(pk)
Quick access to an EC context inside a PK context.
pk_type_t
Public key types.
int pk_can_do(pk_context *ctx, pk_type_t type)
Tell if a context can do the operation given by type.
void sha1(const unsigned char *input, size_t ilen, unsigned char output[20])
Output = SHA-1( input buffer )
void sha1_starts(sha1_context *ctx)
SHA-1 context setup.
void sha1_update(sha1_context *ctx, const unsigned char *input, size_t ilen)
SHA-1 process buffer.
void sha1_init(sha1_context *ctx)
Initialize SHA-1 context.
void sha1_finish(sha1_context *ctx, unsigned char output[20])
SHA-1 final digest.
void sha1_free(sha1_context *ctx)
Clear SHA-1 context.
int ssl_read_record(ssl_context *ssl)
#define SSL_LEGACY_RENEGOTIATION
#define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE
Processing of the ServerHelloDone handshake message failed.
int ssl_send_alert_message(ssl_context *ssl, unsigned char level, unsigned char message)
Send an alert message.
#define TLS_EXT_TRUNCATED_HMAC
static pk_context * ssl_own_key(ssl_context *ssl)
#define POLARSSL_ERR_SSL_INTERNAL_ERROR
Internal error (eg, unexpected failure in lower-level module)
#define SSL_HS_CERTIFICATE_REQUEST
#define POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION
Handshake protocol not within min/max boundaries.
unsigned char ssl_sig_from_pk(pk_context *pk)
#define POLARSSL_ERR_SSL_NO_RNG
No RNG was provided to the SSL module.
#define SSL_MAX_FRAG_LEN_NONE
#define TLS_EXT_SERVERNAME
#define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST
Processing of the CertificateRequest handshake message failed.
@ SSL_CERTIFICATE_REQUEST
@ SSL_SERVER_CHANGE_CIPHER_SPEC
@ SSL_SERVER_KEY_EXCHANGE
@ SSL_CLIENT_CHANGE_CIPHER_SPEC
@ SSL_CLIENT_KEY_EXCHANGE
int ssl_handshake_client_step(ssl_context *ssl)
#define SSL_LEGACY_BREAK_HANDSHAKE
int ssl_write_record(ssl_context *ssl)
#define SSL_ALERT_MSG_PROTOCOL_VERSION
#define SSL_HS_CERTIFICATE_VERIFY
md_type_t ssl_md_alg_from_hash(unsigned char hash)
#define POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE
The requested feature is not available.
#define SSL_INITIAL_HANDSHAKE
#define SSL_SECURE_RENEGOTIATION
#define POLARSSL_ERR_SSL_PK_TYPE_MISMATCH
Public key type mismatch (eg, asked for RSA key exchange and presented EC key)
#define POLARSSL_PREMASTER_SIZE
int ssl_psk_derive_premaster(ssl_context *ssl, key_exchange_type_t key_ex)
#define SSL_HS_CLIENT_KEY_EXCHANGE
#define SSL_TRUNC_HMAC_DISABLED
int ssl_write_certificate(ssl_context *ssl)
#define SSL_MINOR_VERSION_0
#define TLS_EXT_SUPPORTED_ELLIPTIC_CURVES
#define POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET
Processing of the NewSessionTicket handshake message failed.
#define SSL_HS_CLIENT_HELLO
#define SSL_HS_SERVER_HELLO_DONE
int ssl_derive_keys(ssl_context *ssl)
#define SSL_SESSION_TICKETS_DISABLED
#define SSL_EMPTY_RENEGOTIATION_INFO
renegotiation info ext
#define SSL_CERT_TYPE_RSA_SIGN
#define SSL_LEGACY_NO_RENEGOTIATION
#define SSL_HS_NEW_SESSION_TICKET
int ssl_write_change_cipher_spec(ssl_context *ssl)
#define POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED
The own private key or pre-shared key is not set, but needed.
#define SSL_MINOR_VERSION_3
void ssl_optimize_checksum(ssl_context *ssl, const ssl_ciphersuite_t *ciphersuite_info)
int ssl_write_finished(ssl_context *ssl)
#define SSL_MAX_MINOR_VERSION
#define SSL_ALERT_LEVEL_FATAL
pk_type_t ssl_pk_alg_from_sig(unsigned char sig)
static x509_crt * ssl_own_cert(ssl_context *ssl)
#define SSL_MAJOR_VERSION_3
void ssl_handshake_wrapup(ssl_context *ssl)
#define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO
Processing of the ServerHello handshake message failed.
#define SSL_TRUNC_HMAC_ENABLED
#define POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO
Unexpected message at ServerHello in renegotiation.
#define TLS_EXT_SERVERNAME_HOSTNAME
#define SSL_RENEGOTIATION
#define TLS_EXT_MAX_FRAGMENT_LENGTH
int ssl_parse_certificate(ssl_context *ssl)
#define SSL_HS_SERVER_HELLO
int ssl_parse_finished(ssl_context *ssl)
int ssl_flush_output(ssl_context *ssl)
#define SSL_CERT_TYPE_ECDSA_SIGN
#define SSL_MAX_MAJOR_VERSION
#define SSL_HS_SERVER_KEY_EXCHANGE
int ssl_parse_change_cipher_spec(ssl_context *ssl)
#define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE
Processing of the Certificate handshake message failed.
#define POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE
Processing of the ServerKeyExchange handshake message failed.
#define TLS_EXT_RENEGOTIATION_INFO
#define SSL_COMPRESS_NULL
#define POLARSSL_ERR_SSL_BAD_INPUT_DATA
Bad input parameters to function.
#define SSL_MSG_HANDSHAKE
int ssl_send_fatal_handshake_failure(ssl_context *ssl)
#define POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE
An unexpected message was received from our peer.
#define SSL_COMPRESS_DEFLATE
#define TLS_EXT_SESSION_TICKET
#define POLARSSL_ERR_SSL_MALLOC_FAILED
Memory allocation failed.
#define TLS_EXT_SUPPORTED_POINT_FORMATS
static int safer_memcmp(const void *a, const void *b, size_t n)
pk_type_t ssl_get_ciphersuite_sig_pk_alg(const ssl_ciphersuite_t *info)
const ssl_ciphersuite_t * ssl_ciphersuite_from_id(int ciphersuite_id)
@ POLARSSL_KEY_EXCHANGE_DHE_RSA
@ POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA
@ POLARSSL_KEY_EXCHANGE_PSK
@ POLARSSL_KEY_EXCHANGE_DHE_PSK
@ POLARSSL_KEY_EXCHANGE_ECDHE_RSA
@ POLARSSL_KEY_EXCHANGE_ECDHE_PSK
@ POLARSSL_KEY_EXCHANGE_RSA_PSK
@ POLARSSL_KEY_EXCHANGE_RSA
@ POLARSSL_KEY_EXCHANGE_ECDH_RSA
@ POLARSSL_KEY_EXCHANGE_ECDH_ECDSA
This structure is used for storing ciphersuite information.
key_exchange_type_t key_exchange
ssl_handshake_params * handshake
const int * ciphersuite_list[4]
int allow_legacy_renegotiation
int(* f_rng)(void *, unsigned char *, size_t)
ssl_transform * transform_negotiate
unsigned char * psk_identity
ssl_session * session_negotiate
char peer_verify_data[36]
void(* calc_verify)(ssl_context *, unsigned char *)
unsigned char premaster[POLARSSL_PREMASTER_SIZE]
unsigned char randbytes[64]
Curve information for use by other modules.
Generic message digest context.