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)
84 if( left < 3 + cert_len )
87 *p++ = (
unsigned char)( cert_len >> 16 & 0xFF );
88 *p++ = (
unsigned char)( cert_len >> 8 & 0xFF );
89 *p++ = (
unsigned char)( cert_len & 0xFF );
106 const unsigned char *buf,
size_t len )
108 const unsigned char *p = buf;
109 const unsigned char *
const end = buf + len;
110 #if defined(POLARSSL_X509_CRT_PARSE_C)
120 #if defined(POLARSSL_X509_CRT_PARSE_C)
124 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
135 if( p + cert_len > end )
175 static int ssl_write_ticket(
ssl_context *ssl,
size_t *tlen )
178 unsigned char *
const start = ssl->
out_msg + 10;
179 unsigned char *p = start;
180 unsigned char *state;
181 unsigned char iv[16];
182 size_t clear_len, enc_len, pad_len, i;
194 if( ( ret = ssl->
f_rng( ssl->
p_rng, p, 16 ) ) != 0 )
212 SSL_DEBUG_BUF( 3,
"session ticket cleartext", state, clear_len );
215 pad_len = 16 - clear_len % 16;
216 enc_len = clear_len + pad_len;
217 for( i = clear_len; i < enc_len; i++ )
218 state[i] = (
unsigned char) pad_len;
222 enc_len, iv, state, state ) ) != 0 )
228 *p++ = (
unsigned char)( ( enc_len >> 8 ) & 0xFF );
229 *p++ = (
unsigned char)( ( enc_len ) & 0xFF );
238 SSL_DEBUG_BUF( 3,
"session ticket structure", start, *tlen );
252 unsigned char *key_name = buf;
253 unsigned char *iv = buf + 16;
254 unsigned char *enc_len_p = iv + 16;
255 unsigned char *ticket = enc_len_p + 2;
257 unsigned char computed_mac[32];
258 size_t enc_len, clear_len, i;
259 unsigned char pad_len;
263 if( len < 34 || ssl->ticket_keys == NULL )
266 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
267 mac = ticket + enc_len;
269 if( len != enc_len + 66 )
280 for( i = 0; i < 32; i++ )
281 if( mac[i] != computed_mac[i] )
288 enc_len, iv, ticket, ticket ) ) != 0 )
294 pad_len = ticket[enc_len - 1];
297 for( i = 2; i < pad_len; i++ )
298 if( ticket[enc_len - i] != pad_len )
303 clear_len = enc_len - pad_len;
305 SSL_DEBUG_BUF( 3,
"session ticket cleartext", ticket, clear_len );
308 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
315 #if defined(POLARSSL_HAVE_TIME)
340 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
346 const unsigned char* name,
size_t len )
352 ret = ssl->
f_sni( ssl->
p_sni, ssl, name, len );
360 static int ssl_parse_servername_ext(
ssl_context *ssl,
361 const unsigned char *buf,
365 size_t servername_list_size, hostname_len;
366 const unsigned char *p;
368 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
369 if( servername_list_size + 2 != len )
376 while( servername_list_size > 0 )
378 hostname_len = ( ( p[1] << 8 ) | p[2] );
379 if( hostname_len + 3 > servername_list_size )
387 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
397 servername_list_size -= hostname_len + 3;
398 p += hostname_len + 3;
401 if( servername_list_size != 0 )
411 static int ssl_parse_renegotiation_info(
ssl_context *ssl,
412 const unsigned char *buf,
419 if( len != 1 || buf[0] != 0x0 )
421 SSL_DEBUG_MSG( 1, (
"non-zero length renegotiated connection field" ) );
437 SSL_DEBUG_MSG( 1, (
"non-matching renegotiated connection field" ) );
449 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
450 static int ssl_parse_signature_algorithms_ext(
ssl_context *ssl,
451 const unsigned char *buf,
454 size_t sig_alg_list_size;
455 const unsigned char *p;
457 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
458 if( sig_alg_list_size + 2 != len ||
459 sig_alg_list_size %2 != 0 )
466 while( sig_alg_list_size > 0 )
472 #if defined(POLARSSL_SHA512_C)
484 #if defined(POLARSSL_SHA256_C)
507 sig_alg_list_size -= 2;
511 SSL_DEBUG_MSG( 3, (
"client hello v3, signature_algorithm ext: %d",
518 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
519 static int ssl_parse_supported_elliptic_curves(
ssl_context *ssl,
520 const unsigned char *buf,
523 size_t list_size, our_size;
524 const unsigned char *p;
527 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
528 if( list_size + 2 != len ||
537 our_size = list_size / 2 + 1;
541 if( ( curves =
polarssl_malloc( our_size *
sizeof( *curves ) ) ) == NULL )
545 memset( (
void *) curves, 0, our_size *
sizeof( *curves ) );
549 while( list_size > 0 && our_size > 1 )
553 if( curve_info != NULL )
555 *curves++ = curve_info;
566 static int ssl_parse_supported_point_formats(
ssl_context *ssl,
567 const unsigned char *buf,
571 const unsigned char *p;
574 if( list_size + 1 != len )
581 while( list_size > 0 )
599 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
600 static int ssl_parse_max_fragment_length_ext(
ssl_context *ssl,
601 const unsigned char *buf,
616 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
617 static int ssl_parse_truncated_hmac_ext(
ssl_context *ssl,
618 const unsigned char *buf,
635 #if defined(POLARSSL_SSL_SESSION_TICKETS)
636 static int ssl_parse_session_ticket_ext(
ssl_context *ssl,
662 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
668 SSL_DEBUG_MSG( 3, (
"session successfully restored from ticket" ) );
679 #if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
680 static int ssl_parse_client_hello_v2(
ssl_context *ssl )
685 unsigned int ciph_len, sess_len, chal_len;
686 unsigned char *buf, *p;
687 const int *ciphersuites;
694 SSL_DEBUG_MSG( 1, (
"client hello v2 illegal for renegotiation" ) );
709 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
710 SSL_DEBUG_MSG( 3, (
"client hello v2, max. version: [%d:%d]",
730 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
732 if( n < 17 || n > 512 )
744 SSL_DEBUG_MSG( 1, (
"client only supports ssl smaller than minimum"
777 ciph_len = ( buf[0] << 8 ) | buf[1];
778 sess_len = ( buf[2] << 8 ) | buf[3];
779 chal_len = ( buf[4] << 8 ) | buf[5];
781 SSL_DEBUG_MSG( 3, (
"ciph_len: %d, sess_len: %d, chal_len: %d",
782 ciph_len, sess_len, chal_len ) );
787 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
799 if( chal_len < 8 || chal_len > 32 )
805 if( n != 6 + ciph_len + sess_len + chal_len )
814 buf + 6 + ciph_len, sess_len );
816 buf + 6 + ciph_len + sess_len, chal_len );
818 p = buf + 6 + ciph_len;
830 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
834 SSL_DEBUG_MSG( 3, (
"received TLS_EMPTY_RENEGOTIATION_INFO " ) );
837 SSL_DEBUG_MSG( 1, (
"received RENEGOTIATION SCSV during renegotiation" ) );
850 for( i = 0; ciphersuites[i] != 0; i++ )
852 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
856 if( p[0] == 0 && p[1] == 0 &&
857 ( ( ciphersuites[i] >> 8 ) & 0xFF ) == 0 &&
858 p[2] == ( ciphersuites[i] & 0xFF ) )
862 if( ciphersuite_info == NULL )
873 goto have_ciphersuite_v2;
893 SSL_DEBUG_MSG( 1, (
"legacy renegotiation, breaking off handshake" ) );
910 #if defined(POLARSSL_X509_CRT_PARSE_C)
911 #if defined(POLARSSL_ECDSA_C)
912 static int ssl_key_matches_curves(
pk_context *pk,
918 while( *crv != NULL )
920 if( (*crv)->grp_id == grp_id )
939 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
949 for( cur = list; cur != NULL; cur = cur->
next )
954 #if defined(POLARSSL_ECDSA_C)
973 static int ssl_parse_client_hello(
ssl_context *ssl )
978 unsigned int ciph_len, sess_len;
979 unsigned int comp_len;
980 unsigned int ext_len = 0;
981 unsigned char *buf, *p, *ext;
982 int renegotiation_info_seen = 0;
983 int handshake_failure = 0;
984 const int *ciphersuites;
998 #if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
999 if( ( buf[0] & 0x80 ) != 0 )
1000 return ssl_parse_client_hello_v2( ssl );
1008 ( buf[3] << 8 ) | buf[4] ) );
1009 SSL_DEBUG_MSG( 3, (
"client hello v3, protocol ver: [%d:%d]",
1027 n = ( buf[3] << 8 ) | buf[4];
1029 if( n < 45 || n > 2048 )
1069 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1070 SSL_DEBUG_MSG( 3, (
"client hello v3, max. version: [%d:%d]",
1089 SSL_DEBUG_MSG( 1, (
"client only supports ssl smaller than minimum"
1107 if( buf[1] != 0 || n != (
unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1133 ciph_len = ( buf[39 + sess_len] << 8 )
1134 | ( buf[40 + sess_len] );
1136 if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
1145 comp_len = buf[41 + sess_len + ciph_len];
1147 if( comp_len < 1 || comp_len > 16 )
1156 if( n > 42 + sess_len + ciph_len + comp_len )
1158 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1159 | ( buf[43 + sess_len + ciph_len + comp_len] );
1161 if( ( ext_len > 0 && ext_len < 4 ) ||
1162 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1165 SSL_DEBUG_BUF( 3,
"Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1171 #if defined(POLARSSL_ZLIB_SUPPORT)
1172 for( i = 0; i < comp_len; ++i )
1185 buf + 38, sess_len );
1187 buf + 41 + sess_len, ciph_len );
1189 buf + 42 + sess_len + ciph_len, comp_len );
1194 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1198 SSL_DEBUG_MSG( 3, (
"received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1201 SSL_DEBUG_MSG( 1, (
"received RENEGOTIATION SCSV during renegotiation" ) );
1213 ext = buf + 44 + sess_len + ciph_len + comp_len;
1217 unsigned int ext_id = ( ( ext[0] << 8 )
1219 unsigned int ext_size = ( ( ext[2] << 8 )
1222 if( ext_size + 4 > ext_len )
1229 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
1232 if( ssl->
f_sni == NULL )
1235 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1243 renegotiation_info_seen = 1;
1245 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1250 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
1252 SSL_DEBUG_MSG( 3, (
"found signature_algorithms extension" ) );
1256 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1262 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
1264 SSL_DEBUG_MSG( 3, (
"found supported elliptic curves extension" ) );
1266 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1272 SSL_DEBUG_MSG( 3, (
"found supported point formats extension" ) );
1274 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1280 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
1282 SSL_DEBUG_MSG( 3, (
"found max fragment length extension" ) );
1284 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1290 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1294 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1300 #if defined(POLARSSL_SSL_SESSION_TICKETS)
1304 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1311 SSL_DEBUG_MSG( 3, (
"unknown extension found: %d (ignoring)",
1315 ext_len -= 4 + ext_size;
1316 ext += 4 + ext_size;
1318 if( ext_len > 0 && ext_len < 4 )
1331 SSL_DEBUG_MSG( 1, (
"legacy renegotiation, breaking off handshake" ) );
1332 handshake_failure = 1;
1336 renegotiation_info_seen == 0 )
1338 SSL_DEBUG_MSG( 1, (
"renegotiation_info extension missing (secure)" ) );
1339 handshake_failure = 1;
1346 handshake_failure = 1;
1350 renegotiation_info_seen == 1 )
1352 SSL_DEBUG_MSG( 1, (
"renegotiation_info extension present (legacy)" ) );
1353 handshake_failure = 1;
1356 if( handshake_failure == 1 )
1370 for( i = 0; ciphersuites[i] != 0; i++ )
1372 for( j = 0, p = buf + 41 + sess_len; j < ciph_len;
1375 if( p[0] == ( ( ciphersuites[i] >> 8 ) & 0xFF ) &&
1376 p[1] == ( ( ciphersuites[i] ) & 0xFF ) )
1380 if( ciphersuite_info == NULL )
1383 ciphersuites[i] ) );
1391 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
1398 #if defined(POLARSSL_X509_CRT_PARSE_C)
1406 if( ssl_pick_cert( ssl, ciphersuite_info ) != 0 )
1410 goto have_ciphersuite;
1435 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1436 static void ssl_write_truncated_hmac_ext(
ssl_context *ssl,
1440 unsigned char *p = buf;
1448 SSL_DEBUG_MSG( 3, (
"server hello, adding truncated hmac extension" ) );
1460 #if defined(POLARSSL_SSL_SESSION_TICKETS)
1461 static void ssl_write_session_ticket_ext(
ssl_context *ssl,
1465 unsigned char *p = buf;
1473 SSL_DEBUG_MSG( 3, (
"server hello, adding session ticket extension" ) );
1485 static void ssl_write_renegotiation_ext(
ssl_context *ssl,
1489 unsigned char *p = buf;
1497 SSL_DEBUG_MSG( 3, (
"server hello, secure renegotiation extension" ) );
1514 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
1515 static void ssl_write_max_fragment_length_ext(
ssl_context *ssl,
1519 unsigned char *p = buf;
1527 SSL_DEBUG_MSG( 3, (
"server hello, max_fragment_length extension" ) );
1541 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
1542 static void ssl_write_supported_point_formats_ext(
ssl_context *ssl,
1546 unsigned char *p = buf;
1551 SSL_DEBUG_MSG( 3, (
"server hello, supported_point_formats extension" ) );
1566 static int ssl_write_server_hello(
ssl_context *ssl )
1568 #if defined(POLARSSL_HAVE_TIME)
1572 size_t olen, ext_len = 0, n;
1573 unsigned char *buf, *p;
1590 SSL_DEBUG_MSG( 3, (
"server hello, chosen version: [%d:%d]",
1593 #if defined(POLARSSL_HAVE_TIME)
1595 *p++ = (
unsigned char)( t >> 24 );
1596 *p++ = (
unsigned char)( t >> 16 );
1597 *p++ = (
unsigned char)( t >> 8 );
1598 *p++ = (
unsigned char)( t );
1600 SSL_DEBUG_MSG( 3, (
"server hello, current time: %lu", t ) );
1602 if( ( ret = ssl->
f_rng( ssl->
p_rng, p, 4 ) ) != 0 )
1608 if( ( ret = ssl->
f_rng( ssl->
p_rng, p, 28 ) ) != 0 )
1615 SSL_DEBUG_BUF( 3,
"server hello, random bytes", buf + 6, 32 );
1639 #if defined(POLARSSL_HAVE_TIME)
1643 #if defined(POLARSSL_SSL_SESSION_TICKETS)
1685 SSL_DEBUG_MSG( 3, (
"server hello, session id len.: %d", n ) );
1686 SSL_DEBUG_BUF( 3,
"server hello, session id", buf + 39, n );
1702 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1705 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
1706 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1710 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1711 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1715 #if defined(POLARSSL_SSL_SESSION_TICKETS)
1716 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1720 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
1721 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1725 SSL_DEBUG_MSG( 3, (
"server hello, total extension length: %d", ext_len ) );
1727 *p++ = (
unsigned char)( ( ext_len >> 8 ) & 0xFF );
1728 *p++ = (
unsigned char)( ( ext_len ) & 0xFF );
1742 #if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1743 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
1744 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1745 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1746 static int ssl_write_certificate_request(
ssl_context *ssl )
1757 SSL_DEBUG_MSG( 2, (
"<= skip write certificate request" ) );
1766 static int ssl_write_certificate_request(
ssl_context *ssl )
1770 size_t dn_size, total_dn_size;
1771 size_t ct_len, sa_len;
1772 unsigned char *buf, *p;
1784 SSL_DEBUG_MSG( 2, (
"<= skip write certificate request" ) );
1811 #if defined(POLARSSL_RSA_C)
1814 #if defined(POLARSSL_ECDSA_C)
1818 p[0] = (
unsigned char) ct_len++;
1822 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
1853 #if defined(POLARSSL_RSA_C)
1857 #if defined(POLARSSL_ECDSA_C)
1862 p[0] = (
unsigned char)( sa_len >> 8 );
1863 p[1] = (
unsigned char)( sa_len );
1877 while( crt != NULL )
1879 if( p - buf > 4096 )
1883 *p++ = (
unsigned char)( dn_size >> 8 );
1884 *p++ = (
unsigned char)( dn_size );
1890 total_dn_size += 2 + dn_size;
1897 ssl->
out_msg[4 + ct_len + sa_len] = (
unsigned char)( total_dn_size >> 8 );
1898 ssl->
out_msg[5 + ct_len + sa_len] = (
unsigned char)( total_dn_size );
1910 static int ssl_write_server_key_exchange(
ssl_context *ssl )
1917 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1918 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1919 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1920 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1921 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1922 unsigned char *p = ssl->
out_msg + 4;
1923 unsigned char *dig_signed = p;
1924 size_t dig_signed_len = 0, len;
1925 ((void) dig_signed);
1926 ((void) dig_signed_len);
1935 SSL_DEBUG_MSG( 2, (
"<= skip write server key exchange" ) );
1940 #if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1941 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1954 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1955 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1985 dig_signed_len = len;
1998 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1999 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2000 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2033 dig_signed_len = len;
2044 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2045 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2046 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2051 size_t signature_len = 0;
2052 unsigned int hashlen = 0;
2053 unsigned char hash[64];
2059 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2072 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2073 defined(POLARSSL_SSL_PROTO_TLS1_1)
2088 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2089 defined(POLARSSL_SSL_PROTO_TLS1_1)
2110 md5_update( &md5, dig_signed, dig_signed_len );
2123 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2124 defined(POLARSSL_SSL_PROTO_TLS1_2)
2147 md_update( &ctx, dig_signed, dig_signed_len );
2165 SSL_DEBUG_BUF( 3,
"parameters hash", hash, hashlen != 0 ? hashlen :
2177 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2188 p + 2 , &signature_len,
2195 *(p++) = (
unsigned char)( signature_len >> 8 );
2196 *(p++) = (
unsigned char)( signature_len );
2225 static int ssl_write_server_hello_done(
ssl_context *ssl )
2248 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2249 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2250 static int ssl_parse_client_dh_public(
ssl_context *ssl,
unsigned char **p,
2251 const unsigned char *end )
2265 n = ( (*p)[0] << 8 ) | (*p)[1];
2288 #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2289 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2290 static int ssl_parse_encrypted_pms(
ssl_context *ssl,
2291 const unsigned char *p,
2292 const unsigned char *end,
2308 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2309 defined(POLARSSL_SSL_PROTO_TLS1_2)
2312 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2313 *p++ != ( ( len ) & 0xFF ) )
2321 if( p + len != end )
2356 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
2357 static int ssl_parse_client_psk_identity(
ssl_context *ssl,
unsigned char **p,
2358 const unsigned char *end )
2363 if( ssl->
f_psk == NULL &&
2380 n = ( (*p)[0] << 8 ) | (*p)[1];
2383 if( n < 1 || n > 65535 || *p + n > end )
2389 if( ssl->
f_psk != NULL )
2391 if( ( ret != ssl->
f_psk( ssl->
p_psk, ssl, *p, n ) ) != 0 )
2424 static int ssl_parse_client_key_exchange(
ssl_context *ssl )
2451 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
2454 unsigned char *p = ssl->
in_msg + 4;
2457 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2478 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2479 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2483 size_t n = ssl->
in_msg[3];
2493 ssl->
in_msg + 4, n ) ) != 0 )
2516 #if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2519 unsigned char *p = ssl->
in_msg + 4;
2522 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2524 SSL_DEBUG_RET( 1, (
"ssl_parse_client_psk_identity" ), ret );
2537 #if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2540 unsigned char *p = ssl->
in_msg + 4;
2543 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2545 SSL_DEBUG_RET( 1, (
"ssl_parse_client_psk_identity" ), ret );
2549 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
2564 #if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2567 unsigned char *p = ssl->
in_msg + 4;
2570 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2572 SSL_DEBUG_RET( 1, (
"ssl_parse_client_psk_identity" ), ret );
2575 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2590 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2593 unsigned char *p = ssl->
in_msg + 4;
2596 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2598 SSL_DEBUG_RET( 1, (
"ssl_parse_client_psk_identity" ), ret );
2603 p, end - p ) ) != 0 )
2620 #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2623 if( ( ret = ssl_parse_encrypted_pms( ssl,
2628 SSL_DEBUG_RET( 1, (
"ssl_parse_parse_ecrypted_pms_secret" ), ret );
2652 #if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2653 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2654 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2655 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2656 static int ssl_parse_certificate_verify(
ssl_context *ssl )
2676 static int ssl_parse_certificate_verify(
ssl_context *ssl )
2679 size_t sa_len, sig_len;
2680 unsigned char hash[48];
2681 unsigned char *hash_start = hash;
2683 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2737 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2738 defined(POLARSSL_SSL_PROTO_TLS1_1)
2757 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2767 SSL_DEBUG_MSG( 1, (
"peer not adhering to requested sig_alg"
2768 " for verify message" ) );
2783 SSL_DEBUG_MSG( 1, (
"peer not adhering to requested sig_alg"
2784 " for verify message" ) );
2804 sig_len = ( ssl->
in_msg[4 + sa_len] << 8 ) | ssl->
in_msg[5 + sa_len];
2806 if( sa_len + sig_len + 6 != ssl->
in_hslen )
2813 md_alg, hash_start, hashlen,
2814 ssl->
in_msg + 6 + sa_len, sig_len ) ) != 0 )
2828 #if defined(POLARSSL_SSL_SESSION_TICKETS)
2829 static int ssl_write_new_session_ticket(
ssl_context *ssl )
2851 ssl->
out_msg[4] = ( lifetime >> 24 ) & 0xFF;
2852 ssl->
out_msg[5] = ( lifetime >> 16 ) & 0xFF;
2853 ssl->
out_msg[6] = ( lifetime >> 8 ) & 0xFF;
2854 ssl->
out_msg[7] = ( lifetime ) & 0xFF;
2856 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
2862 ssl->
out_msg[8] = (
unsigned char)( ( tlen >> 8 ) & 0xFF );
2863 ssl->
out_msg[9] = (
unsigned char)( ( tlen ) & 0xFF );
2897 switch( ssl->
state )
2907 ret = ssl_parse_client_hello( ssl );
2918 ret = ssl_write_server_hello( ssl );
2926 ret = ssl_write_server_key_exchange( ssl );
2930 ret = ssl_write_certificate_request( ssl );
2934 ret = ssl_write_server_hello_done( ssl );
2949 ret = ssl_parse_client_key_exchange( ssl );
2953 ret = ssl_parse_certificate_verify( ssl );
2970 #if defined(POLARSSL_SSL_SESSION_TICKETS)
2972 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)
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)
#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)
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.