28 #if defined(POLARSSL_SSL_SRV_C)
32 #if defined(POLARSSL_ECP_C)
36 #if defined(POLARSSL_MEMORY_C)
39 #define polarssl_malloc malloc
40 #define polarssl_free free
46 #if defined(POLARSSL_HAVE_TIME)
50 #if defined(POLARSSL_SSL_SESSION_TICKETS)
59 static int ssl_save_session(
const ssl_session *session,
60 unsigned char *buf,
size_t buf_len,
63 unsigned char *p = buf;
64 size_t left = buf_len;
65 #if defined(POLARSSL_X509_CRT_PARSE_C)
76 #if defined(POLARSSL_X509_CRT_PARSE_C)
82 if( left < 3 + cert_len )
85 *p++ = (
unsigned char)( cert_len >> 16 & 0xFF );
86 *p++ = (
unsigned char)( cert_len >> 8 & 0xFF );
87 *p++ = (
unsigned char)( cert_len & 0xFF );
104 const unsigned char *buf,
size_t len )
106 const unsigned char *p = buf;
107 const unsigned char *
const end = buf + len;
108 #if defined(POLARSSL_X509_CRT_PARSE_C)
118 #if defined(POLARSSL_X509_CRT_PARSE_C)
122 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
133 if( p + cert_len > end )
173 static int ssl_write_ticket(
ssl_context *ssl,
size_t *tlen )
176 unsigned char *
const start = ssl->
out_msg + 10;
177 unsigned char *p = start;
178 unsigned char *state;
179 unsigned char iv[16];
180 size_t clear_len, enc_len, pad_len, i;
192 if( ( ret = ssl->
f_rng( ssl->
p_rng, p, 16 ) ) != 0 )
210 SSL_DEBUG_BUF( 3,
"session ticket cleartext", state, clear_len );
213 pad_len = 16 - clear_len % 16;
214 enc_len = clear_len + pad_len;
215 for( i = clear_len; i < enc_len; i++ )
216 state[i] = (
unsigned char) pad_len;
220 enc_len, iv, state, state ) ) != 0 )
226 *p++ = (
unsigned char)( ( enc_len >> 8 ) & 0xFF );
227 *p++ = (
unsigned char)( ( enc_len ) & 0xFF );
236 SSL_DEBUG_BUF( 3,
"session ticket structure", start, *tlen );
250 unsigned char *key_name = buf;
251 unsigned char *iv = buf + 16;
252 unsigned char *enc_len_p = iv + 16;
253 unsigned char *ticket = enc_len_p + 2;
255 unsigned char computed_mac[32];
256 size_t enc_len, clear_len, i;
257 unsigned char pad_len, diff;
261 if( len < 34 || ssl->ticket_keys == NULL )
264 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
265 mac = ticket + enc_len;
267 if( len != enc_len + 66 )
272 for( i = 0; i < 16; i++ )
280 for( i = 0; i < 32; i++ )
281 diff |= mac[i] ^ computed_mac[i];
290 enc_len, iv, ticket, ticket ) ) != 0 )
296 pad_len = ticket[enc_len - 1];
299 for( i = 2; i < pad_len; i++ )
300 if( ticket[enc_len - i] != pad_len )
305 clear_len = enc_len - pad_len;
307 SSL_DEBUG_BUF( 3,
"session ticket cleartext", ticket, clear_len );
310 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
317 #if defined(POLARSSL_HAVE_TIME)
342 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
348 const unsigned char*
name,
size_t len )
354 ret = ssl->
f_sni( ssl->
p_sni, ssl, name, len );
362 static int ssl_parse_servername_ext(
ssl_context *ssl,
363 const unsigned char *buf,
367 size_t servername_list_size, hostname_len;
368 const unsigned char *p;
370 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
371 if( servername_list_size + 2 != len )
378 while( servername_list_size > 0 )
380 hostname_len = ( ( p[1] << 8 ) | p[2] );
381 if( hostname_len + 3 > servername_list_size )
389 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
399 servername_list_size -= hostname_len + 3;
400 p += hostname_len + 3;
403 if( servername_list_size != 0 )
413 static int ssl_parse_renegotiation_info(
ssl_context *ssl,
414 const unsigned char *buf,
421 if( len != 1 || buf[0] != 0x0 )
423 SSL_DEBUG_MSG( 1, (
"non-zero length renegotiated connection field" ) );
441 SSL_DEBUG_MSG( 1, (
"non-matching renegotiated connection field" ) );
453 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
454 static int ssl_parse_signature_algorithms_ext(
ssl_context *ssl,
455 const unsigned char *buf,
458 size_t sig_alg_list_size;
459 const unsigned char *p;
461 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
462 if( sig_alg_list_size + 2 != len ||
463 sig_alg_list_size %2 != 0 )
470 while( sig_alg_list_size > 0 )
476 #if defined(POLARSSL_SHA512_C)
488 #if defined(POLARSSL_SHA256_C)
511 sig_alg_list_size -= 2;
515 SSL_DEBUG_MSG( 3, (
"client hello v3, signature_algorithm ext: %d",
522 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
523 static int ssl_parse_supported_elliptic_curves(
ssl_context *ssl,
524 const unsigned char *buf,
527 size_t list_size, our_size;
528 const unsigned char *p;
531 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
532 if( list_size + 2 != len ||
541 our_size = list_size / 2 + 1;
545 if( ( curves =
polarssl_malloc( our_size *
sizeof( *curves ) ) ) == NULL )
549 memset( (
void *) curves, 0, our_size *
sizeof( *curves ) );
553 while( list_size > 0 && our_size > 1 )
557 if( curve_info != NULL )
559 *curves++ = curve_info;
570 static int ssl_parse_supported_point_formats(
ssl_context *ssl,
571 const unsigned char *buf,
575 const unsigned char *p;
578 if( list_size + 1 != len )
585 while( list_size > 0 )
603 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
604 static int ssl_parse_max_fragment_length_ext(
ssl_context *ssl,
605 const unsigned char *buf,
620 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
621 static int ssl_parse_truncated_hmac_ext(
ssl_context *ssl,
622 const unsigned char *buf,
639 #if defined(POLARSSL_SSL_SESSION_TICKETS)
640 static int ssl_parse_session_ticket_ext(
ssl_context *ssl,
666 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
672 SSL_DEBUG_MSG( 3, (
"session successfully restored from ticket" ) );
687 #if defined(POLARSSL_X509_CRT_PARSE_C)
691 #if defined(POLARSSL_ECDSA_C)
692 static int ssl_key_matches_curves(
pk_context *pk,
698 while( *crv != NULL )
700 if( (*crv)->grp_id == grp_id )
719 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
729 for( cur = list; cur != NULL; cur = cur->
next )
734 #if defined(POLARSSL_ECDSA_C)
757 static int ssl_ciphersuite_match(
ssl_context *ssl,
int suite_id,
763 if( suite_info == NULL )
765 SSL_DEBUG_MSG( 1, (
"ciphersuite info for %04x not found", suite_id ) );
773 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
780 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
784 ssl->
f_psk == NULL &&
790 #if defined(POLARSSL_X509_CRT_PARSE_C)
798 if( ssl_pick_cert( ssl, suite_info ) != 0 )
802 *ciphersuite_info = suite_info;
806 #if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
807 static int ssl_parse_client_hello_v2(
ssl_context *ssl )
812 unsigned int ciph_len, sess_len, chal_len;
813 unsigned char *buf, *p;
814 const int *ciphersuites;
821 SSL_DEBUG_MSG( 1, (
"client hello v2 illegal for renegotiation" ) );
836 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
837 SSL_DEBUG_MSG( 3, (
"client hello v2, max. version: [%d:%d]",
857 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
859 if( n < 17 || n > 512 )
871 SSL_DEBUG_MSG( 1, (
"client only supports ssl smaller than minimum"
904 ciph_len = ( buf[0] << 8 ) | buf[1];
905 sess_len = ( buf[2] << 8 ) | buf[3];
906 chal_len = ( buf[4] << 8 ) | buf[5];
908 SSL_DEBUG_MSG( 3, (
"ciph_len: %d, sess_len: %d, chal_len: %d",
909 ciph_len, sess_len, chal_len ) );
914 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
926 if( chal_len < 8 || chal_len > 32 )
932 if( n != 6 + ciph_len + sess_len + chal_len )
941 buf + 6 + ciph_len, sess_len );
943 buf + 6 + ciph_len + sess_len, chal_len );
945 p = buf + 6 + ciph_len;
957 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
961 SSL_DEBUG_MSG( 3, (
"received TLS_EMPTY_RENEGOTIATION_INFO " ) );
964 SSL_DEBUG_MSG( 1, (
"received RENEGOTIATION SCSV during renegotiation" ) );
977 ciphersuite_info = NULL;
978 #if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
979 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
981 for( i = 0; ciphersuites[i] != 0; i++ )
983 for( i = 0; ciphersuites[i] != 0; i++ )
985 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
989 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
990 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
993 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
994 &ciphersuite_info ) ) != 0 )
997 if( ciphersuite_info != NULL )
998 goto have_ciphersuite_v2;
1006 have_ciphersuite_v2:
1017 SSL_DEBUG_MSG( 1, (
"legacy renegotiation, breaking off handshake" ) );
1034 static int ssl_parse_client_hello(
ssl_context *ssl )
1039 unsigned int ciph_len, sess_len;
1040 unsigned int comp_len;
1041 unsigned int ext_len = 0;
1042 unsigned char *buf, *p, *ext;
1043 int renegotiation_info_seen = 0;
1044 int handshake_failure = 0;
1045 const int *ciphersuites;
1059 #if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1060 if( ( buf[0] & 0x80 ) != 0 )
1061 return ssl_parse_client_hello_v2( ssl );
1069 ( buf[3] << 8 ) | buf[4] ) );
1070 SSL_DEBUG_MSG( 3, (
"client hello v3, protocol ver: [%d:%d]",
1088 n = ( buf[3] << 8 ) | buf[4];
1090 if( n < 45 || n > 2048 )
1130 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1131 SSL_DEBUG_MSG( 3, (
"client hello v3, max. version: [%d:%d]",
1150 SSL_DEBUG_MSG( 1, (
"client only supports ssl smaller than minimum"
1168 if( buf[1] != 0 || n != (
unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1194 ciph_len = ( buf[39 + sess_len] << 8 )
1195 | ( buf[40 + sess_len] );
1197 if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
1206 comp_len = buf[41 + sess_len + ciph_len];
1208 if( comp_len < 1 || comp_len > 16 )
1217 if( n > 42 + sess_len + ciph_len + comp_len )
1219 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1220 | ( buf[43 + sess_len + ciph_len + comp_len] );
1222 if( ( ext_len > 0 && ext_len < 4 ) ||
1223 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1226 SSL_DEBUG_BUF( 3,
"Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1232 #if defined(POLARSSL_ZLIB_SUPPORT)
1233 for( i = 0; i < comp_len; ++i )
1246 buf + 38, sess_len );
1248 buf + 41 + sess_len, ciph_len );
1250 buf + 42 + sess_len + ciph_len, comp_len );
1255 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1259 SSL_DEBUG_MSG( 3, (
"received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1262 SSL_DEBUG_MSG( 1, (
"received RENEGOTIATION SCSV during renegotiation" ) );
1274 ext = buf + 44 + sess_len + ciph_len + comp_len;
1278 unsigned int ext_id = ( ( ext[0] << 8 )
1280 unsigned int ext_size = ( ( ext[2] << 8 )
1283 if( ext_size + 4 > ext_len )
1290 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
1293 if( ssl->
f_sni == NULL )
1296 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1304 renegotiation_info_seen = 1;
1306 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1311 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
1313 SSL_DEBUG_MSG( 3, (
"found signature_algorithms extension" ) );
1317 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1323 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
1325 SSL_DEBUG_MSG( 3, (
"found supported elliptic curves extension" ) );
1327 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1333 SSL_DEBUG_MSG( 3, (
"found supported point formats extension" ) );
1336 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1342 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
1344 SSL_DEBUG_MSG( 3, (
"found max fragment length extension" ) );
1346 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1352 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1356 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1362 #if defined(POLARSSL_SSL_SESSION_TICKETS)
1366 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1373 SSL_DEBUG_MSG( 3, (
"unknown extension found: %d (ignoring)",
1377 ext_len -= 4 + ext_size;
1378 ext += 4 + ext_size;
1380 if( ext_len > 0 && ext_len < 4 )
1393 SSL_DEBUG_MSG( 1, (
"legacy renegotiation, breaking off handshake" ) );
1394 handshake_failure = 1;
1398 renegotiation_info_seen == 0 )
1400 SSL_DEBUG_MSG( 1, (
"renegotiation_info extension missing (secure)" ) );
1401 handshake_failure = 1;
1408 handshake_failure = 1;
1412 renegotiation_info_seen == 1 )
1414 SSL_DEBUG_MSG( 1, (
"renegotiation_info extension present (legacy)" ) );
1415 handshake_failure = 1;
1418 if( handshake_failure == 1 )
1432 ciphersuite_info = NULL;
1433 #if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1434 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1436 for( i = 0; ciphersuites[i] != 0; i++ )
1438 for( i = 0; ciphersuites[i] != 0; i++ )
1440 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1443 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1444 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1447 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1448 &ciphersuite_info ) ) != 0 )
1451 if( ciphersuite_info != NULL )
1452 goto have_ciphersuite;
1476 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1477 static void ssl_write_truncated_hmac_ext(
ssl_context *ssl,
1481 unsigned char *p = buf;
1489 SSL_DEBUG_MSG( 3, (
"server hello, adding truncated hmac extension" ) );
1501 #if defined(POLARSSL_SSL_SESSION_TICKETS)
1502 static void ssl_write_session_ticket_ext(
ssl_context *ssl,
1506 unsigned char *p = buf;
1514 SSL_DEBUG_MSG( 3, (
"server hello, adding session ticket extension" ) );
1526 static void ssl_write_renegotiation_ext(
ssl_context *ssl,
1530 unsigned char *p = buf;
1538 SSL_DEBUG_MSG( 3, (
"server hello, secure renegotiation extension" ) );
1555 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
1556 static void ssl_write_max_fragment_length_ext(
ssl_context *ssl,
1560 unsigned char *p = buf;
1568 SSL_DEBUG_MSG( 3, (
"server hello, max_fragment_length extension" ) );
1582 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
1583 static void ssl_write_supported_point_formats_ext(
ssl_context *ssl,
1587 unsigned char *p = buf;
1597 SSL_DEBUG_MSG( 3, (
"server hello, supported_point_formats extension" ) );
1612 static int ssl_write_server_hello(
ssl_context *ssl )
1614 #if defined(POLARSSL_HAVE_TIME)
1618 size_t olen, ext_len = 0, n;
1619 unsigned char *buf, *p;
1623 if( ssl->
f_rng == NULL )
1642 SSL_DEBUG_MSG( 3, (
"server hello, chosen version: [%d:%d]",
1645 #if defined(POLARSSL_HAVE_TIME)
1647 *p++ = (
unsigned char)( t >> 24 );
1648 *p++ = (
unsigned char)( t >> 16 );
1649 *p++ = (
unsigned char)( t >> 8 );
1650 *p++ = (
unsigned char)( t );
1652 SSL_DEBUG_MSG( 3, (
"server hello, current time: %lu", t ) );
1654 if( ( ret = ssl->
f_rng( ssl->
p_rng, p, 4 ) ) != 0 )
1660 if( ( ret = ssl->
f_rng( ssl->
p_rng, p, 28 ) ) != 0 )
1667 SSL_DEBUG_BUF( 3,
"server hello, random bytes", buf + 6, 32 );
1691 #if defined(POLARSSL_HAVE_TIME)
1695 #if defined(POLARSSL_SSL_SESSION_TICKETS)
1737 SSL_DEBUG_MSG( 3, (
"server hello, session id len.: %d", n ) );
1738 SSL_DEBUG_BUF( 3,
"server hello, session id", buf + 39, n );
1754 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1757 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
1758 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1762 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1763 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1767 #if defined(POLARSSL_SSL_SESSION_TICKETS)
1768 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1772 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
1773 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1777 SSL_DEBUG_MSG( 3, (
"server hello, total extension length: %d", ext_len ) );
1779 *p++ = (
unsigned char)( ( ext_len >> 8 ) & 0xFF );
1780 *p++ = (
unsigned char)( ( ext_len ) & 0xFF );
1794 #if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1795 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
1796 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1797 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1798 static int ssl_write_certificate_request(
ssl_context *ssl )
1810 SSL_DEBUG_MSG( 2, (
"<= skip write certificate request" ) );
1819 static int ssl_write_certificate_request(
ssl_context *ssl )
1823 size_t dn_size, total_dn_size;
1824 size_t ct_len, sa_len;
1825 unsigned char *buf, *p;
1838 SSL_DEBUG_MSG( 2, (
"<= skip write certificate request" ) );
1865 #if defined(POLARSSL_RSA_C)
1868 #if defined(POLARSSL_ECDSA_C)
1872 p[0] = (
unsigned char) ct_len++;
1876 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
1907 #if defined(POLARSSL_RSA_C)
1911 #if defined(POLARSSL_ECDSA_C)
1916 p[0] = (
unsigned char)( sa_len >> 8 );
1917 p[1] = (
unsigned char)( sa_len );
1931 while( crt != NULL )
1933 if( p - buf > 4096 )
1937 *p++ = (
unsigned char)( dn_size >> 8 );
1938 *p++ = (
unsigned char)( dn_size );
1944 total_dn_size += 2 + dn_size;
1951 ssl->
out_msg[4 + ct_len + sa_len] = (
unsigned char)( total_dn_size >> 8 );
1952 ssl->
out_msg[5 + ct_len + sa_len] = (
unsigned char)( total_dn_size );
1965 #if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1966 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1967 static int ssl_get_ecdh_params_from_cert(
ssl_context *ssl )
1990 static int ssl_write_server_key_exchange(
ssl_context *ssl )
1997 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1998 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1999 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2000 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
2001 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2002 unsigned char *p = ssl->
out_msg + 4;
2003 unsigned char *dig_signed = p;
2004 size_t dig_signed_len = 0, len;
2005 ((void) dig_signed);
2006 ((void) dig_signed_len);
2011 #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2012 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2013 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2018 SSL_DEBUG_MSG( 2, (
"<= skip write server key exchange" ) );
2024 #if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2025 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2029 ssl_get_ecdh_params_from_cert( ssl );
2031 SSL_DEBUG_MSG( 2, (
"<= skip parse server key exchange" ) );
2037 #if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2038 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2051 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2052 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2082 dig_signed_len = len;
2095 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2096 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2097 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2130 dig_signed_len = len;
2141 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2142 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2143 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2148 size_t signature_len = 0;
2149 unsigned int hashlen = 0;
2150 unsigned char hash[64];
2156 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2169 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2170 defined(POLARSSL_SSL_PROTO_TLS1_1)
2185 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2186 defined(POLARSSL_SSL_PROTO_TLS1_1)
2207 md5_update( &md5, dig_signed, dig_signed_len );
2220 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2221 defined(POLARSSL_SSL_PROTO_TLS1_2)
2244 md_update( &ctx, dig_signed, dig_signed_len );
2262 SSL_DEBUG_BUF( 3,
"parameters hash", hash, hashlen != 0 ? hashlen :
2274 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2285 p + 2 , &signature_len,
2292 *(p++) = (
unsigned char)( signature_len >> 8 );
2293 *(p++) = (
unsigned char)( signature_len );
2322 static int ssl_write_server_hello_done(
ssl_context *ssl )
2345 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2346 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2347 static int ssl_parse_client_dh_public(
ssl_context *ssl,
unsigned char **p,
2348 const unsigned char *end )
2362 n = ( (*p)[0] << 8 ) | (*p)[1];
2385 #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2386 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2387 static int ssl_parse_encrypted_pms(
ssl_context *ssl,
2388 const unsigned char *p,
2389 const unsigned char *end,
2405 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2406 defined(POLARSSL_SSL_PROTO_TLS1_2)
2409 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2410 *p++ != ( ( len ) & 0xFF ) )
2418 if( p + len != end )
2453 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
2454 static int ssl_parse_client_psk_identity(
ssl_context *ssl,
unsigned char **p,
2455 const unsigned char *end )
2460 if( ssl->
f_psk == NULL &&
2477 n = ( (*p)[0] << 8 ) | (*p)[1];
2480 if( n < 1 || n > 65535 || *p + n > end )
2486 if( ssl->
f_psk != NULL )
2488 if( ( ret != ssl->
f_psk( ssl->
p_psk, ssl, *p, n ) ) != 0 )
2523 static int ssl_parse_client_key_exchange(
ssl_context *ssl )
2550 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
2553 unsigned char *p = ssl->
in_msg + 4;
2556 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2577 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2578 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2579 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2580 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2586 size_t n = ssl->
in_msg[3];
2596 ssl->
in_msg + 4, n ) ) != 0 )
2625 #if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2628 unsigned char *p = ssl->
in_msg + 4;
2631 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2633 SSL_DEBUG_RET( 1, (
"ssl_parse_client_psk_identity" ), ret );
2646 #if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2649 unsigned char *p = ssl->
in_msg + 4;
2652 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2654 SSL_DEBUG_RET( 1, (
"ssl_parse_client_psk_identity" ), ret );
2658 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
2673 #if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2676 unsigned char *p = ssl->
in_msg + 4;
2679 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2681 SSL_DEBUG_RET( 1, (
"ssl_parse_client_psk_identity" ), ret );
2684 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2699 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2702 unsigned char *p = ssl->
in_msg + 4;
2705 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2707 SSL_DEBUG_RET( 1, (
"ssl_parse_client_psk_identity" ), ret );
2712 p, end - p ) ) != 0 )
2729 #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2732 if( ( ret = ssl_parse_encrypted_pms( ssl,
2737 SSL_DEBUG_RET( 1, (
"ssl_parse_parse_ecrypted_pms_secret" ), ret );
2761 #if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2762 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2763 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2764 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2765 static int ssl_parse_certificate_verify(
ssl_context *ssl )
2786 static int ssl_parse_certificate_verify(
ssl_context *ssl )
2789 size_t sa_len, sig_len;
2790 unsigned char hash[48];
2791 unsigned char *hash_start = hash;
2793 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2848 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2849 defined(POLARSSL_SSL_PROTO_TLS1_1)
2868 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2878 SSL_DEBUG_MSG( 1, (
"peer not adhering to requested sig_alg"
2879 " for verify message" ) );
2894 SSL_DEBUG_MSG( 1, (
"peer not adhering to requested sig_alg"
2895 " for verify message" ) );
2915 sig_len = ( ssl->
in_msg[4 + sa_len] << 8 ) | ssl->
in_msg[5 + sa_len];
2917 if( sa_len + sig_len + 6 != ssl->
in_hslen )
2924 md_alg, hash_start, hashlen,
2925 ssl->
in_msg + 6 + sa_len, sig_len ) ) != 0 )
2939 #if defined(POLARSSL_SSL_SESSION_TICKETS)
2940 static int ssl_write_new_session_ticket(
ssl_context *ssl )
2962 ssl->
out_msg[4] = ( lifetime >> 24 ) & 0xFF;
2963 ssl->
out_msg[5] = ( lifetime >> 16 ) & 0xFF;
2964 ssl->
out_msg[6] = ( lifetime >> 8 ) & 0xFF;
2965 ssl->
out_msg[7] = ( lifetime ) & 0xFF;
2967 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
2973 ssl->
out_msg[8] = (
unsigned char)( ( tlen >> 8 ) & 0xFF );
2974 ssl->
out_msg[9] = (
unsigned char)( ( tlen ) & 0xFF );
3008 switch( ssl->
state )
3018 ret = ssl_parse_client_hello( ssl );
3029 ret = ssl_write_server_hello( ssl );
3037 ret = ssl_write_server_key_exchange( ssl );
3041 ret = ssl_write_certificate_request( ssl );
3045 ret = ssl_write_server_hello_done( ssl );
3060 ret = ssl_parse_client_key_exchange( ssl );
3064 ret = ssl_parse_certificate_verify( ssl );
3081 #if defined(POLARSSL_SSL_SESSION_TICKETS)
3083 ret = ssl_write_new_session_ticket( ssl );
const ecp_curve_info ** curves
#define SSL_HS_CLIENT_KEY_EXCHANGE
#define SSL_CERT_TYPE_ECDSA_SIGN
int ssl_ciphersuite_uses_ec(const ssl_ciphersuite_t *info)
int ecdh_make_params(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 write the ServerKeyExhange parameters.
#define SSL_ALERT_LEVEL_FATAL
static size_t pk_get_len(const pk_context *ctx)
Get the length in bytes of the underlying key.
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 SSL_DEBUG_RET(level, text, ret)
void *(* polarssl_malloc)(size_t len)
#define POLARSSL_ERR_SSL_PK_TYPE_MISMATCH
Public key type mismatch (eg, asked for RSA key exchange and presented EC key)
#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
x509_buf raw
The raw certificate data (DER).
int(* f_sni)(void *, ssl_context *, const unsigned char *, size_t)
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
Elliptic curves over GF(p)
int ecdh_read_public(ecdh_context *ctx, const unsigned char *buf, size_t blen)
Parse and import the client's public value.
#define SSL_HS_NEW_SESSION_TICKET
ssl_session * session_negotiate
int ssl_parse_certificate(ssl_context *ssl)
ssl_key_cert * sni_key_cert
#define SSL_SESSION_TICKETS_DISABLED
const int * ciphersuite_list[4]
int ssl_parse_finished(ssl_context *ssl)
void sha256_hmac(const unsigned char *key, size_t keylen, const unsigned char *input, size_t ilen, unsigned char output[32], int is224)
Output = HMAC-SHA-256( hmac key, input buffer )
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.
int pk_decrypt(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)
Decrypt message.
#define SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY
#define SSL_RENEGOTIATION
unsigned char premaster[POLARSSL_PREMASTER_SIZE]
void ssl_session_free(ssl_session *session)
Free referenced items in an SSL session including the peer certificate and clear memory.
int x509_crt_parse(x509_crt *chain, const unsigned char *buf, size_t buflen)
Parse one or more certificates and add them to the chained list.
#define POLARSSL_ECP_PF_UNCOMPRESSED
Uncompressed point format.
int ssl_write_finished(ssl_context *ssl)
void x509_crt_free(x509_crt *crt)
Unallocate all certificate data.
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 pk_ec(pk)
Quick access to an EC context inside a PK context.
int ssl_handshake_server_step(ssl_context *ssl)
#define SSL_LEGACY_NO_RENEGOTIATION
unsigned char mac_key[16]
#define SSL_MAJOR_VERSION_3
#define POLARSSL_ERR_SSL_INVALID_MAC
Verification of the message MAC failed.
pk_type_t ssl_get_ciphersuite_sig_pk_alg(const ssl_ciphersuite_t *info)
struct _x509_crt * next
Next certificate in the CA-chain.
#define POLARSSL_ECP_DP_MAX
Number of supported curves (plus one for NONE).
#define TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT
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_HS_CERTIFICATE_REQUEST
#define SSL_CERT_TYPE_RSA_SIGN
ssl_handshake_params * handshake
#define POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE
Our own certificate(s) is/are too large to send in an SSL message.
#define SSL_MSG_HANDSHAKE
void(* update_checksum)(ssl_context *, const unsigned char *, size_t)
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
Container for an X.509 certificate.
#define SSL_ALERT_MSG_UNRECOGNIZED_NAME
#define POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS
Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Calculate Secret...
int aes_crypt_cbc(aes_context *ctx, int mode, size_t length, unsigned char iv[16], const unsigned char *input, unsigned char *output)
AES-CBC buffer encryption/decryption Length should be a multiple of the block size (16 bytes) ...
struct _ssl_session ssl_session
#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.
ssl_key_cert * key_cert
Current key/cert or key/cert list.
#define SSL_HS_SERVER_HELLO_DONE
void x509_crt_init(x509_crt *crt)
Initialize a certificate (chain)
void(* polarssl_free)(void *ptr)
unsigned char * p
ASN1 data, e.g.
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)
int ssl_ciphersuite_uses_psk(const ssl_ciphersuite_t *info)
#define POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED
Session ticket has expired.
#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.
int ssl_parse_change_cipher_spec(ssl_context *ssl)
void sha1_starts(sha1_context *ctx)
SHA-1 context setup.
#define POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
Processing of the ClientHello handshake message failed.
#define SSL_EMPTY_RENEGOTIATION_INFO
renegotiation info ext
This structure is used for storing ciphersuite information.
int ecp_use_known_dp(ecp_group *grp, ecp_group_id index)
Set a group using well-known domain parameters.
#define SSL_DEBUG_BUF(level, text, buf, len)
#define POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE
Processing of the ClientKeyExchange handshake message failed.
#define POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED
The own private key or pre-shared key is not set, but needed.
#define SSL_INITIAL_HANDSHAKE
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)
size_t len
ASN1 length, e.g.
ecp_group_id
Domain parameters (curve, subgroup and generator) identifiers.
#define SSL_MAX_FRAG_LEN_INVALID
#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.
#define POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP
Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Read Public.
#define SSL_DEBUG_MPI(level, text, X)
size_t mpi_size(const mpi *X)
Return the total size in bytes.
int mpi_copy(mpi *X, const mpi *Y)
Copy the contents of Y into X.
#define SSL_LEGACY_BREAK_HANDSHAKE
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_UNKNOWN_IDENTITY
Unkown identity received (eg, PSK identity)
#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)
int(* f_get_cache)(void *, ssl_session *)
#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.
const ecp_curve_info * ecp_curve_info_from_tls_id(uint16_t tls_id)
Get curve information from a TLS NamedCurve value.
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)
ssl_ticket_keys * ticket_keys
#define SSL_MAX_CONTENT_LEN
Size of the input / output buffer.
unsigned char * psk_identity
const char * ssl_get_ciphersuite_name(const int ciphersuite_id)
Return the name of the ciphersuite associated with the given ID.
unsigned char key_name[16]
#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.
x509_buf subject_raw
The raw subject data (DER).
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_fetch_input(ssl_context *ssl, size_t nb_want)
int dhm_make_params(dhm_context *ctx, int x_size, unsigned char *output, size_t *olen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Setup and write the ServerKeyExchange parameters.
int(* f_psk)(void *, ssl_context *, const unsigned char *, size_t)
int ssl_write_record(ssl_context *ssl)
#define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY
Processing of the CertificateVerify handshake message failed.
int dhm_read_public(dhm_context *ctx, const unsigned char *input, size_t ilen)
Import the peer's public value G^Y.
unsigned char randbytes[64]
Generic message digest context.
#define POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN
The server has no ciphersuites in common with the client.
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.