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_SESSION_TICKETS)
60 static 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)
66 static 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 );
117 static 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)
146 static 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)
242 static 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;
288 static 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)
313 static 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)
339 static 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)
363 static 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 );
447 static 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 );
691 static 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)
734 static int ssl_parse_max_fragment_length_ext(
ssl_context *ssl,
735 const unsigned char *buf,
753 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
754 static int ssl_parse_truncated_hmac_ext(
ssl_context *ssl,
755 const unsigned char *buf,
772 #if defined(POLARSSL_SSL_SESSION_TICKETS)
773 static int ssl_parse_session_ticket_ext(
ssl_context *ssl,
774 const unsigned char *buf,
791 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
792 static 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 )
874 static 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)
930 SSL_DEBUG_MSG( 1, (
"server only supports ssl smaller than minimum"
940 #if defined(POLARSSL_DEBUG_C)
941 t = ( (uint32_t) buf[6] << 24 )
942 | ( (uint32_t) buf[7] << 16 )
943 | ( (uint32_t) buf[8] << 8 )
944 | ( (uint32_t) buf[9] );
945 SSL_DEBUG_MSG( 3, (
"server hello, current time: %lu", t ) );
952 SSL_DEBUG_BUF( 3,
"server hello, random bytes", buf + 6, 32 );
970 ext_len = ( ( buf[42 + n] << 8 )
973 if( ( ext_len > 0 && ext_len < 4 ) ||
981 i = ( buf[39 + n] << 8 ) | buf[40 + n];
991 SSL_DEBUG_MSG( 1, (
"ciphersuite info for %04x not found", i ) );
997 SSL_DEBUG_MSG( 3, (
"server hello, session id len.: %d", n ) );
1012 #if defined(POLARSSL_HAVE_TIME)
1034 SSL_DEBUG_MSG( 3, (
"server hello, chosen ciphersuite: %d", i ) );
1035 SSL_DEBUG_MSG( 3, (
"server hello, compress alg.: %d", buf[41 + n] ) );
1054 #
if defined(POLARSSL_ZLIB_SUPPORT)
1066 SSL_DEBUG_MSG( 2, (
"server hello, total extension length: %d", ext_len ) );
1070 unsigned int ext_id = ( ( ext[0] << 8 )
1072 unsigned int ext_size = ( ( ext[2] << 8 )
1075 if( ext_size + 4 > ext_len )
1085 renegotiation_info_seen = 1;
1087 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1093 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
1095 SSL_DEBUG_MSG( 3, (
"found max_fragment_length extension" ) );
1097 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1098 ext + 4, ext_size ) ) != 0 )
1106 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1110 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1111 ext + 4, ext_size ) ) != 0 )
1119 #if defined(POLARSSL_SSL_SESSION_TICKETS)
1123 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1124 ext + 4, ext_size ) ) != 0 )
1132 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
1134 SSL_DEBUG_MSG( 3, (
"found supported_point_formats extension" ) );
1136 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1137 ext + 4, ext_size ) ) != 0 )
1145 #if defined(POLARSSL_SSL_ALPN)
1149 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1156 SSL_DEBUG_MSG( 3, (
"unknown extension found: %d (ignoring)",
1160 ext_len -= 4 + ext_size;
1161 ext += 4 + ext_size;
1163 if( ext_len > 0 && ext_len < 4 )
1176 SSL_DEBUG_MSG( 1, (
"legacy renegotiation, breaking off handshake" ) );
1177 handshake_failure = 1;
1181 renegotiation_info_seen == 0 )
1183 SSL_DEBUG_MSG( 1, (
"renegotiation_info extension missing (secure)" ) );
1184 handshake_failure = 1;
1191 handshake_failure = 1;
1195 renegotiation_info_seen == 1 )
1197 SSL_DEBUG_MSG( 1, (
"renegotiation_info extension present (legacy)" ) );
1198 handshake_failure = 1;
1201 if( handshake_failure == 1 )
1214 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1215 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1216 static int ssl_parse_server_dh_params(
ssl_context *ssl,
unsigned char **p,
1217 unsigned char *end )
1239 SSL_DEBUG_MSG( 1, (
"bad server key exchange message (DHM length)" ) );
1252 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1253 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1254 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1255 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1256 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1257 static int ssl_check_server_ecdh_params(
const ssl_context *ssl )
1262 if( curve_info == NULL )
1270 #if defined(POLARSSL_SSL_ECP_SET_CURVES)
1288 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1289 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1290 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1291 static int ssl_parse_server_ecdh_params(
ssl_context *ssl,
1293 unsigned char *end )
1306 (
const unsigned char **) p, end ) ) != 0 )
1312 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1314 SSL_DEBUG_MSG( 1, (
"bad server key exchange message (ECDHE curve)" ) );
1324 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1325 static int ssl_parse_server_psk_hint(
ssl_context *ssl,
1327 unsigned char *end )
1338 len = (*p)[0] << 8 | (*p)[1];
1341 if( (*p) + len > end )
1343 SSL_DEBUG_MSG( 1, (
"bad server key exchange message (psk_identity_hint length)" ) );
1356 #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1357 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1361 static int ssl_write_encrypted_pms(
ssl_context *ssl,
1362 size_t offset,
size_t *olen,
1379 if( ( ret = ssl->
f_rng( ssl->
p_rng, p + 2, 46 ) ) != 0 )
1399 ssl->
out_msg + offset + len_bytes, olen,
1407 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1408 defined(POLARSSL_SSL_PROTO_TLS1_2)
1409 if( len_bytes == 2 )
1411 ssl->
out_msg[offset+0] = (
unsigned char)( *olen >> 8 );
1412 ssl->
out_msg[offset+1] = (
unsigned char)( *olen );
1422 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
1423 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1424 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1425 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1426 static int ssl_parse_signature_algorithm(
ssl_context *ssl,
1442 if( (*p) + 2 > end )
1451 "HashAlgorithm %d", *(p)[0] ) );
1461 "SignatureAlgorithm %d", (*p)[1] ) );
1465 SSL_DEBUG_MSG( 2, (
"Server used SignatureAlgorithm %d", (*p)[1] ) );
1466 SSL_DEBUG_MSG( 2, (
"Server used HashAlgorithm %d", (*p)[0] ) );
1477 #if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1478 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1479 static int ssl_get_ecdh_params_from_cert(
ssl_context *ssl )
1500 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1502 SSL_DEBUG_MSG( 1, (
"bad server certificate (ECDH curve)" ) );
1511 static int ssl_parse_server_key_exchange(
ssl_context *ssl )
1515 unsigned char *p, *end;
1516 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1517 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1518 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1519 size_t sig_len, params_len;
1520 unsigned char hash[64];
1528 #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
1531 SSL_DEBUG_MSG( 2, (
"<= skip parse server key exchange" ) );
1539 #if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1540 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1544 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1550 SSL_DEBUG_MSG( 2, (
"<= skip parse server key exchange" ) );
1592 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1598 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1606 #if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1607 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1614 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1615 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1619 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
1628 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1629 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1630 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1635 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
1650 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1651 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1652 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1657 params_len = p - ( ssl->
in_msg + 4 );
1662 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
1665 if( ssl_parse_signature_algorithm( ssl, &p, end,
1666 &md_alg, &pk_alg ) != 0 )
1680 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1681 defined(POLARSSL_SSL_PROTO_TLS1_1)
1700 sig_len = ( p[0] << 8 ) | p[1];
1703 if( end != p + sig_len )
1714 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1715 defined(POLARSSL_SSL_PROTO_TLS1_1)
1755 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1756 defined(POLARSSL_SSL_PROTO_TLS1_2)
1794 SSL_DEBUG_BUF( 3,
"parameters hash", hash, hashlen != 0 ? hashlen :
1807 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
1825 #if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1826 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
1827 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1828 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1829 static int ssl_parse_certificate_request(
ssl_context *ssl )
1840 SSL_DEBUG_MSG( 2, (
"<= skip parse certificate request" ) );
1849 static int ssl_parse_certificate_request(
ssl_context *ssl )
1852 unsigned char *buf, *p;
1853 size_t n = 0, m = 0;
1854 size_t cert_type_len = 0, dn_len = 0;
1864 SSL_DEBUG_MSG( 2, (
"<= skip parse certificate request" ) );
1919 cert_type_len = buf[4];
1929 while( cert_type_len > 0 )
1931 #if defined(POLARSSL_RSA_C)
1940 #if defined(POLARSSL_ECDSA_C)
1957 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
1962 size_t sig_alg_len = ( ( buf[5 + n] << 8 )
1979 dn_len = ( ( buf[5 + m + n] << 8 )
1980 | ( buf[6 + m + n] ) );
1999 static int ssl_parse_server_hello_done(
ssl_context *ssl )
2035 static int ssl_write_client_key_exchange(
ssl_context *ssl )
2043 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
2051 ssl->
out_msg[4] = (
unsigned char)( n >> 8 );
2052 ssl->
out_msg[5] = (
unsigned char)( n );
2083 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2084 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2085 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2086 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2126 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
2140 ssl->
out_msg[i++] = (
unsigned char)( n >> 8 );
2141 ssl->
out_msg[i++] = (
unsigned char)( n );
2146 #if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2153 #if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2156 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2161 #if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2168 ssl->
out_msg[i++] = (
unsigned char)( n >> 8 );
2169 ssl->
out_msg[i++] = (
unsigned char)( n );
2183 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2216 #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2220 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
2226 ((void) ciphersuite_info);
2254 #if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2255 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2256 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2257 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2258 static int ssl_write_certificate_verify(
ssl_context *ssl )
2278 static int ssl_write_certificate_verify(
ssl_context *ssl )
2282 size_t n = 0, offset = 0;
2283 unsigned char hash[48];
2284 unsigned char *hash_start = hash;
2286 unsigned int hashlen;
2318 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2319 defined(POLARSSL_SSL_PROTO_TLS1_1)
2350 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2393 ssl->
out_msg + 6 + offset, &n,
2400 ssl->
out_msg[4 + offset] = (
unsigned char)( n >> 8 );
2401 ssl->
out_msg[5 + offset] = (
unsigned char)( n );
2423 #if defined(POLARSSL_SSL_SESSION_TICKETS)
2424 static int ssl_parse_new_session_ticket(
ssl_context *ssl )
2429 unsigned char *ticket;
2464 lifetime = ( ssl->
in_msg[4] << 24 ) | ( ssl->
in_msg[5] << 16 ) |
2467 ticket_len = ( ssl->
in_msg[8] << 8 ) | ( ssl->
in_msg[9] );
2469 if( ticket_len + 10 != ssl->
in_hslen )
2484 if( ticket_len == 0 )
2499 memcpy( ticket, ssl->
in_msg + 10, ticket_len );
2510 SSL_DEBUG_MSG( 3, (
"ticket in use, discarding session id" ) );
2534 switch( ssl->
state )
2544 ret = ssl_write_client_hello( ssl );
2555 ret = ssl_parse_server_hello( ssl );
2563 ret = ssl_parse_server_key_exchange( ssl );
2567 ret = ssl_parse_certificate_request( ssl );
2571 ret = ssl_parse_server_hello_done( ssl );
2586 ret = ssl_write_client_key_exchange( ssl );
2590 ret = ssl_write_certificate_verify( ssl );
2607 #if defined(POLARSSL_SSL_SESSION_TICKETS)
2609 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)
Generate a public key and a TLS ClientKeyExchange payload.
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_PREMASTER_SIZE
#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)
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.
void md_init(md_context_t *ctx)
Initialize a md_context (as NONE)
#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)
void md5_free(md5_context *ctx)
Clear MD5 context.
#define POLARSSL_MPI_MAX_SIZE
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
void md_free(md_context_t *ctx)
Free and clear the message-specific context of ctx.
#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 (including padding if relevant).
#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
void sha1_free(sha1_context *ctx)
Clear SHA-1 context.
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 and procress a TLS ServerKeyExhange payload.
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.
void md5_init(md5_context *ctx)
Initialize MD5 context.
#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
#define POLARSSL_ERR_SSL_INTERNAL_ERROR
Internal error (eg, unexpected failure in lower-level module)
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, including padding if relevant.
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_MAX_CONTENT_LEN
#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 (including padding if relevant).
pk_context pk
Container for the public key context.
ssl_transform * transform_negotiate
#define SSL_LEGACY_RENEGOTIATION
#define SSL_SECURE_RENEGOTIATION
void sha1_init(sha1_context *ctx)
Initialize SHA-1 context.
#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 ssl_send_fatal_handshake_failure(ssl_context *ssl)
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.