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" ) );
683 #if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
684 static int ssl_parse_client_hello_v2(
ssl_context *ssl )
689 unsigned int ciph_len, sess_len, chal_len;
690 unsigned char *buf, *p;
691 const int *ciphersuites;
698 SSL_DEBUG_MSG( 1, (
"client hello v2 illegal for renegotiation" ) );
713 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
714 SSL_DEBUG_MSG( 3, (
"client hello v2, max. version: [%d:%d]",
734 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
736 if( n < 17 || n > 512 )
748 SSL_DEBUG_MSG( 1, (
"client only supports ssl smaller than minimum"
781 ciph_len = ( buf[0] << 8 ) | buf[1];
782 sess_len = ( buf[2] << 8 ) | buf[3];
783 chal_len = ( buf[4] << 8 ) | buf[5];
785 SSL_DEBUG_MSG( 3, (
"ciph_len: %d, sess_len: %d, chal_len: %d",
786 ciph_len, sess_len, chal_len ) );
791 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
803 if( chal_len < 8 || chal_len > 32 )
809 if( n != 6 + ciph_len + sess_len + chal_len )
818 buf + 6 + ciph_len, sess_len );
820 buf + 6 + ciph_len + sess_len, chal_len );
822 p = buf + 6 + ciph_len;
834 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
838 SSL_DEBUG_MSG( 3, (
"received TLS_EMPTY_RENEGOTIATION_INFO " ) );
841 SSL_DEBUG_MSG( 1, (
"received RENEGOTIATION SCSV during renegotiation" ) );
854 for( i = 0; ciphersuites[i] != 0; i++ )
856 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
860 if( p[0] == 0 && p[1] == 0 &&
861 ( ( ciphersuites[i] >> 8 ) & 0xFF ) == 0 &&
862 p[2] == ( ciphersuites[i] & 0xFF ) )
866 if( ciphersuite_info == NULL )
877 goto have_ciphersuite_v2;
897 SSL_DEBUG_MSG( 1, (
"legacy renegotiation, breaking off handshake" ) );
914 #if defined(POLARSSL_X509_CRT_PARSE_C)
915 #if defined(POLARSSL_ECDSA_C)
916 static int ssl_key_matches_curves(
pk_context *pk,
922 while( *crv != NULL )
924 if( (*crv)->grp_id == grp_id )
943 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
953 for( cur = list; cur != NULL; cur = cur->
next )
958 #if defined(POLARSSL_ECDSA_C)
977 static int ssl_parse_client_hello(
ssl_context *ssl )
982 unsigned int ciph_len, sess_len;
983 unsigned int comp_len;
984 unsigned int ext_len = 0;
985 unsigned char *buf, *p, *ext;
986 int renegotiation_info_seen = 0;
987 int handshake_failure = 0;
988 const int *ciphersuites;
1002 #if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1003 if( ( buf[0] & 0x80 ) != 0 )
1004 return ssl_parse_client_hello_v2( ssl );
1012 ( buf[3] << 8 ) | buf[4] ) );
1013 SSL_DEBUG_MSG( 3, (
"client hello v3, protocol ver: [%d:%d]",
1031 n = ( buf[3] << 8 ) | buf[4];
1033 if( n < 45 || n > 2048 )
1073 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1074 SSL_DEBUG_MSG( 3, (
"client hello v3, max. version: [%d:%d]",
1093 SSL_DEBUG_MSG( 1, (
"client only supports ssl smaller than minimum"
1111 if( buf[1] != 0 || n != (
unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1137 ciph_len = ( buf[39 + sess_len] << 8 )
1138 | ( buf[40 + sess_len] );
1140 if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
1149 comp_len = buf[41 + sess_len + ciph_len];
1151 if( comp_len < 1 || comp_len > 16 )
1160 if( n > 42 + sess_len + ciph_len + comp_len )
1162 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1163 | ( buf[43 + sess_len + ciph_len + comp_len] );
1165 if( ( ext_len > 0 && ext_len < 4 ) ||
1166 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1169 SSL_DEBUG_BUF( 3,
"Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1175 #if defined(POLARSSL_ZLIB_SUPPORT)
1176 for( i = 0; i < comp_len; ++i )
1189 buf + 38, sess_len );
1191 buf + 41 + sess_len, ciph_len );
1193 buf + 42 + sess_len + ciph_len, comp_len );
1198 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1202 SSL_DEBUG_MSG( 3, (
"received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1205 SSL_DEBUG_MSG( 1, (
"received RENEGOTIATION SCSV during renegotiation" ) );
1217 ext = buf + 44 + sess_len + ciph_len + comp_len;
1221 unsigned int ext_id = ( ( ext[0] << 8 )
1223 unsigned int ext_size = ( ( ext[2] << 8 )
1226 if( ext_size + 4 > ext_len )
1233 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
1236 if( ssl->
f_sni == NULL )
1239 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1247 renegotiation_info_seen = 1;
1249 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1254 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
1256 SSL_DEBUG_MSG( 3, (
"found signature_algorithms extension" ) );
1260 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1266 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
1268 SSL_DEBUG_MSG( 3, (
"found supported elliptic curves extension" ) );
1270 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1276 SSL_DEBUG_MSG( 3, (
"found supported point formats extension" ) );
1279 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1285 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
1287 SSL_DEBUG_MSG( 3, (
"found max fragment length extension" ) );
1289 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1295 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1299 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1305 #if defined(POLARSSL_SSL_SESSION_TICKETS)
1309 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1316 SSL_DEBUG_MSG( 3, (
"unknown extension found: %d (ignoring)",
1320 ext_len -= 4 + ext_size;
1321 ext += 4 + ext_size;
1323 if( ext_len > 0 && ext_len < 4 )
1336 SSL_DEBUG_MSG( 1, (
"legacy renegotiation, breaking off handshake" ) );
1337 handshake_failure = 1;
1341 renegotiation_info_seen == 0 )
1343 SSL_DEBUG_MSG( 1, (
"renegotiation_info extension missing (secure)" ) );
1344 handshake_failure = 1;
1351 handshake_failure = 1;
1355 renegotiation_info_seen == 1 )
1357 SSL_DEBUG_MSG( 1, (
"renegotiation_info extension present (legacy)" ) );
1358 handshake_failure = 1;
1361 if( handshake_failure == 1 )
1375 for( i = 0; ciphersuites[i] != 0; i++ )
1377 for( j = 0, p = buf + 41 + sess_len; j < ciph_len;
1380 if( p[0] == ( ( ciphersuites[i] >> 8 ) & 0xFF ) &&
1381 p[1] == ( ( ciphersuites[i] ) & 0xFF ) )
1385 if( ciphersuite_info == NULL )
1388 ciphersuites[i] ) );
1396 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
1403 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1407 ssl->
f_psk == NULL &&
1413 #if defined(POLARSSL_X509_CRT_PARSE_C)
1421 if( ssl_pick_cert( ssl, ciphersuite_info ) != 0 )
1425 goto have_ciphersuite;
1450 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1451 static void ssl_write_truncated_hmac_ext(
ssl_context *ssl,
1455 unsigned char *p = buf;
1463 SSL_DEBUG_MSG( 3, (
"server hello, adding truncated hmac extension" ) );
1475 #if defined(POLARSSL_SSL_SESSION_TICKETS)
1476 static void ssl_write_session_ticket_ext(
ssl_context *ssl,
1480 unsigned char *p = buf;
1488 SSL_DEBUG_MSG( 3, (
"server hello, adding session ticket extension" ) );
1500 static void ssl_write_renegotiation_ext(
ssl_context *ssl,
1504 unsigned char *p = buf;
1512 SSL_DEBUG_MSG( 3, (
"server hello, secure renegotiation extension" ) );
1529 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
1530 static void ssl_write_max_fragment_length_ext(
ssl_context *ssl,
1534 unsigned char *p = buf;
1542 SSL_DEBUG_MSG( 3, (
"server hello, max_fragment_length extension" ) );
1556 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
1557 static void ssl_write_supported_point_formats_ext(
ssl_context *ssl,
1561 unsigned char *p = buf;
1571 SSL_DEBUG_MSG( 3, (
"server hello, supported_point_formats extension" ) );
1586 static int ssl_write_server_hello(
ssl_context *ssl )
1588 #if defined(POLARSSL_HAVE_TIME)
1592 size_t olen, ext_len = 0, n;
1593 unsigned char *buf, *p;
1610 SSL_DEBUG_MSG( 3, (
"server hello, chosen version: [%d:%d]",
1613 #if defined(POLARSSL_HAVE_TIME)
1615 *p++ = (
unsigned char)( t >> 24 );
1616 *p++ = (
unsigned char)( t >> 16 );
1617 *p++ = (
unsigned char)( t >> 8 );
1618 *p++ = (
unsigned char)( t );
1620 SSL_DEBUG_MSG( 3, (
"server hello, current time: %lu", t ) );
1622 if( ( ret = ssl->
f_rng( ssl->
p_rng, p, 4 ) ) != 0 )
1628 if( ( ret = ssl->
f_rng( ssl->
p_rng, p, 28 ) ) != 0 )
1635 SSL_DEBUG_BUF( 3,
"server hello, random bytes", buf + 6, 32 );
1659 #if defined(POLARSSL_HAVE_TIME)
1663 #if defined(POLARSSL_SSL_SESSION_TICKETS)
1705 SSL_DEBUG_MSG( 3, (
"server hello, session id len.: %d", n ) );
1706 SSL_DEBUG_BUF( 3,
"server hello, session id", buf + 39, n );
1722 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1725 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
1726 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1730 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1731 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1735 #if defined(POLARSSL_SSL_SESSION_TICKETS)
1736 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1740 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
1741 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1745 SSL_DEBUG_MSG( 3, (
"server hello, total extension length: %d", ext_len ) );
1747 *p++ = (
unsigned char)( ( ext_len >> 8 ) & 0xFF );
1748 *p++ = (
unsigned char)( ( ext_len ) & 0xFF );
1762 #if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1763 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
1764 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1765 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1766 static int ssl_write_certificate_request(
ssl_context *ssl )
1777 SSL_DEBUG_MSG( 2, (
"<= skip write certificate request" ) );
1786 static int ssl_write_certificate_request(
ssl_context *ssl )
1790 size_t dn_size, total_dn_size;
1791 size_t ct_len, sa_len;
1792 unsigned char *buf, *p;
1804 SSL_DEBUG_MSG( 2, (
"<= skip write certificate request" ) );
1831 #if defined(POLARSSL_RSA_C)
1834 #if defined(POLARSSL_ECDSA_C)
1838 p[0] = (
unsigned char) ct_len++;
1842 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
1873 #if defined(POLARSSL_RSA_C)
1877 #if defined(POLARSSL_ECDSA_C)
1882 p[0] = (
unsigned char)( sa_len >> 8 );
1883 p[1] = (
unsigned char)( sa_len );
1897 while( crt != NULL )
1899 if( p - buf > 4096 )
1903 *p++ = (
unsigned char)( dn_size >> 8 );
1904 *p++ = (
unsigned char)( dn_size );
1910 total_dn_size += 2 + dn_size;
1917 ssl->
out_msg[4 + ct_len + sa_len] = (
unsigned char)( total_dn_size >> 8 );
1918 ssl->
out_msg[5 + ct_len + sa_len] = (
unsigned char)( total_dn_size );
1930 static int ssl_write_server_key_exchange(
ssl_context *ssl )
1937 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1938 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1939 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1940 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1941 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1942 unsigned char *p = ssl->
out_msg + 4;
1943 unsigned char *dig_signed = p;
1944 size_t dig_signed_len = 0, len;
1945 ((void) dig_signed);
1946 ((void) dig_signed_len);
1955 SSL_DEBUG_MSG( 2, (
"<= skip write server key exchange" ) );
1960 #if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1961 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1974 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1975 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2005 dig_signed_len = len;
2018 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2019 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2020 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2053 dig_signed_len = len;
2064 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2065 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2066 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2071 size_t signature_len = 0;
2072 unsigned int hashlen = 0;
2073 unsigned char hash[64];
2079 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2092 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2093 defined(POLARSSL_SSL_PROTO_TLS1_1)
2108 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2109 defined(POLARSSL_SSL_PROTO_TLS1_1)
2130 md5_update( &md5, dig_signed, dig_signed_len );
2143 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2144 defined(POLARSSL_SSL_PROTO_TLS1_2)
2167 md_update( &ctx, dig_signed, dig_signed_len );
2185 SSL_DEBUG_BUF( 3,
"parameters hash", hash, hashlen != 0 ? hashlen :
2197 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2208 p + 2 , &signature_len,
2215 *(p++) = (
unsigned char)( signature_len >> 8 );
2216 *(p++) = (
unsigned char)( signature_len );
2245 static int ssl_write_server_hello_done(
ssl_context *ssl )
2268 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2269 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2270 static int ssl_parse_client_dh_public(
ssl_context *ssl,
unsigned char **p,
2271 const unsigned char *end )
2285 n = ( (*p)[0] << 8 ) | (*p)[1];
2308 #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2309 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2310 static int ssl_parse_encrypted_pms(
ssl_context *ssl,
2311 const unsigned char *p,
2312 const unsigned char *end,
2328 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2329 defined(POLARSSL_SSL_PROTO_TLS1_2)
2332 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2333 *p++ != ( ( len ) & 0xFF ) )
2341 if( p + len != end )
2376 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
2377 static int ssl_parse_client_psk_identity(
ssl_context *ssl,
unsigned char **p,
2378 const unsigned char *end )
2383 if( ssl->
f_psk == NULL &&
2400 n = ( (*p)[0] << 8 ) | (*p)[1];
2403 if( n < 1 || n > 65535 || *p + n > end )
2409 if( ssl->
f_psk != NULL )
2411 if( ( ret != ssl->
f_psk( ssl->
p_psk, ssl, *p, n ) ) != 0 )
2446 static int ssl_parse_client_key_exchange(
ssl_context *ssl )
2473 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
2476 unsigned char *p = ssl->
in_msg + 4;
2479 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2500 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2501 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2505 size_t n = ssl->
in_msg[3];
2515 ssl->
in_msg + 4, n ) ) != 0 )
2538 #if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2541 unsigned char *p = ssl->
in_msg + 4;
2544 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2546 SSL_DEBUG_RET( 1, (
"ssl_parse_client_psk_identity" ), ret );
2559 #if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2562 unsigned char *p = ssl->
in_msg + 4;
2565 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2567 SSL_DEBUG_RET( 1, (
"ssl_parse_client_psk_identity" ), ret );
2571 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
2586 #if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2589 unsigned char *p = ssl->
in_msg + 4;
2592 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2594 SSL_DEBUG_RET( 1, (
"ssl_parse_client_psk_identity" ), ret );
2597 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2612 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2615 unsigned char *p = ssl->
in_msg + 4;
2618 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2620 SSL_DEBUG_RET( 1, (
"ssl_parse_client_psk_identity" ), ret );
2625 p, end - p ) ) != 0 )
2642 #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2645 if( ( ret = ssl_parse_encrypted_pms( ssl,
2650 SSL_DEBUG_RET( 1, (
"ssl_parse_parse_ecrypted_pms_secret" ), ret );
2674 #if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2675 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2676 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2677 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2678 static int ssl_parse_certificate_verify(
ssl_context *ssl )
2698 static int ssl_parse_certificate_verify(
ssl_context *ssl )
2701 size_t sa_len, sig_len;
2702 unsigned char hash[48];
2703 unsigned char *hash_start = hash;
2705 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2759 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2760 defined(POLARSSL_SSL_PROTO_TLS1_1)
2779 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2789 SSL_DEBUG_MSG( 1, (
"peer not adhering to requested sig_alg"
2790 " for verify message" ) );
2805 SSL_DEBUG_MSG( 1, (
"peer not adhering to requested sig_alg"
2806 " for verify message" ) );
2826 sig_len = ( ssl->
in_msg[4 + sa_len] << 8 ) | ssl->
in_msg[5 + sa_len];
2828 if( sa_len + sig_len + 6 != ssl->
in_hslen )
2835 md_alg, hash_start, hashlen,
2836 ssl->
in_msg + 6 + sa_len, sig_len ) ) != 0 )
2850 #if defined(POLARSSL_SSL_SESSION_TICKETS)
2851 static int ssl_write_new_session_ticket(
ssl_context *ssl )
2873 ssl->
out_msg[4] = ( lifetime >> 24 ) & 0xFF;
2874 ssl->
out_msg[5] = ( lifetime >> 16 ) & 0xFF;
2875 ssl->
out_msg[6] = ( lifetime >> 8 ) & 0xFF;
2876 ssl->
out_msg[7] = ( lifetime ) & 0xFF;
2878 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
2884 ssl->
out_msg[8] = (
unsigned char)( ( tlen >> 8 ) & 0xFF );
2885 ssl->
out_msg[9] = (
unsigned char)( ( tlen ) & 0xFF );
2919 switch( ssl->
state )
2929 ret = ssl_parse_client_hello( ssl );
2940 ret = ssl_write_server_hello( ssl );
2948 ret = ssl_write_server_key_exchange( ssl );
2952 ret = ssl_write_certificate_request( ssl );
2956 ret = ssl_write_server_hello_done( ssl );
2971 ret = ssl_parse_client_key_exchange( ssl );
2975 ret = ssl_parse_certificate_verify( ssl );
2992 #if defined(POLARSSL_SSL_SESSION_TICKETS)
2994 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_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)
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 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.