34 #if !defined(POLARSSL_CONFIG_FILE) 37 #include POLARSSL_CONFIG_FILE 40 #if defined(POLARSSL_SSL_TLS_C) 45 #if defined(POLARSSL_X509_CRT_PARSE_C) && \ 46 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE) 50 #if defined(POLARSSL_PLATFORM_C) 53 #define polarssl_malloc malloc 54 #define polarssl_free free 59 #if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \ 61 #define strcasecmp _stricmp 65 static void polarssl_zeroize(
void *v,
size_t n ) {
66 volatile unsigned char *p = v;
while( n-- ) *p++ = 0;
69 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH) 93 #if defined(POLARSSL_X509_CRT_PARSE_C) 114 #if defined(POLARSSL_SSL_SESSION_TICKETS) 128 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL) 130 const unsigned char *key_enc,
const unsigned char *key_dec,
132 const unsigned char *iv_enc,
const unsigned char *iv_dec,
134 const unsigned char *mac_enc,
const unsigned char *mac_dec,
135 size_t maclen ) = NULL;
136 int (*ssl_hw_record_activate)(
ssl_context *ssl,
int direction) = NULL;
137 int (*ssl_hw_record_reset)(
ssl_context *ssl ) = NULL;
138 int (*ssl_hw_record_write)(
ssl_context *ssl ) = NULL;
139 int (*ssl_hw_record_read)(
ssl_context *ssl ) = NULL;
140 int (*ssl_hw_record_finish)(
ssl_context *ssl ) = NULL;
146 #if defined(POLARSSL_SSL_PROTO_SSL3) 147 static int ssl3_prf(
const unsigned char *secret,
size_t slen,
149 const unsigned char *random,
size_t rlen,
150 unsigned char *dstbuf,
size_t dlen )
155 unsigned char padding[16];
156 unsigned char sha1sum[20];
170 for( i = 0; i < dlen / 16; i++ )
172 memset( padding, (
unsigned char) (
'A' + i), 1 + i );
189 polarssl_zeroize( padding,
sizeof( padding ) );
190 polarssl_zeroize( sha1sum,
sizeof( sha1sum ) );
196 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) 197 static int tls1_prf(
const unsigned char *secret,
size_t slen,
199 const unsigned char *random,
size_t rlen,
200 unsigned char *dstbuf,
size_t dlen )
204 const unsigned char *S1, *S2;
205 unsigned char tmp[128];
206 unsigned char h_i[20];
208 if(
sizeof( tmp ) < 20 + strlen( label ) + rlen )
211 hs = ( slen + 1 ) / 2;
213 S2 = secret + slen - hs;
215 nb = strlen( label );
216 memcpy( tmp + 20, label, nb );
217 memcpy( tmp + 20 + nb, random, rlen );
223 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
225 for( i = 0; i < dlen; i += 16 )
227 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
228 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
230 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
232 for( j = 0; j < k; j++ )
233 dstbuf[i + j] = h_i[j];
241 for( i = 0; i < dlen; i += 20 )
246 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
248 for( j = 0; j < k; j++ )
249 dstbuf[i + j] = (
unsigned char)( dstbuf[i + j] ^ h_i[j] );
252 polarssl_zeroize( tmp,
sizeof( tmp ) );
253 polarssl_zeroize( h_i,
sizeof( h_i ) );
259 #if defined(POLARSSL_SSL_PROTO_TLS1_2) 260 #if defined(POLARSSL_SHA256_C) 261 static int tls_prf_sha256(
const unsigned char *secret,
size_t slen,
263 const unsigned char *random,
size_t rlen,
264 unsigned char *dstbuf,
size_t dlen )
268 unsigned char tmp[128];
269 unsigned char h_i[32];
271 if(
sizeof( tmp ) < 32 + strlen( label ) + rlen )
274 nb = strlen( label );
275 memcpy( tmp + 32, label, nb );
276 memcpy( tmp + 32 + nb, random, rlen );
284 for( i = 0; i < dlen; i += 32 )
289 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
291 for( j = 0; j < k; j++ )
292 dstbuf[i + j] = h_i[j];
295 polarssl_zeroize( tmp,
sizeof( tmp ) );
296 polarssl_zeroize( h_i,
sizeof( h_i ) );
302 #if defined(POLARSSL_SHA512_C) 303 static int tls_prf_sha384(
const unsigned char *secret,
size_t slen,
305 const unsigned char *random,
size_t rlen,
306 unsigned char *dstbuf,
size_t dlen )
310 unsigned char tmp[128];
311 unsigned char h_i[48];
313 if(
sizeof( tmp ) < 48 + strlen( label ) + rlen )
316 nb = strlen( label );
317 memcpy( tmp + 48, label, nb );
318 memcpy( tmp + 48 + nb, random, rlen );
326 for( i = 0; i < dlen; i += 48 )
331 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
333 for( j = 0; j < k; j++ )
334 dstbuf[i + j] = h_i[j];
337 polarssl_zeroize( tmp,
sizeof( tmp ) );
338 polarssl_zeroize( h_i,
sizeof( h_i ) );
345 static void ssl_update_checksum_start(
ssl_context *,
const unsigned char *,
size_t );
347 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \ 348 defined(POLARSSL_SSL_PROTO_TLS1_1) 349 static void ssl_update_checksum_md5sha1(
ssl_context *,
const unsigned char *,
size_t );
352 #if defined(POLARSSL_SSL_PROTO_SSL3) 353 static void ssl_calc_verify_ssl(
ssl_context *,
unsigned char * );
354 static void ssl_calc_finished_ssl(
ssl_context *,
unsigned char *,
int );
357 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) 358 static void ssl_calc_verify_tls(
ssl_context *,
unsigned char * );
359 static void ssl_calc_finished_tls(
ssl_context *,
unsigned char *,
int );
362 #if defined(POLARSSL_SSL_PROTO_TLS1_2) 363 #if defined(POLARSSL_SHA256_C) 364 static void ssl_update_checksum_sha256(
ssl_context *,
const unsigned char *,
size_t );
365 static void ssl_calc_verify_tls_sha256(
ssl_context *,
unsigned char * );
366 static void ssl_calc_finished_tls_sha256(
ssl_context *,
unsigned char *,
int );
369 #if defined(POLARSSL_SHA512_C) 370 static void ssl_update_checksum_sha384(
ssl_context *,
const unsigned char *,
size_t );
371 static void ssl_calc_verify_tls_sha384(
ssl_context *,
unsigned char * );
372 static void ssl_calc_finished_tls_sha384(
ssl_context *,
unsigned char *,
int );
379 unsigned char tmp[64];
380 unsigned char keyblk[256];
383 unsigned char *mac_enc;
384 unsigned char *mac_dec;
396 if( cipher_info == NULL )
404 if( md_info == NULL )
414 #if defined(POLARSSL_SSL_PROTO_SSL3) 423 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) 432 #if defined(POLARSSL_SSL_PROTO_TLS1_2) 433 #if defined(POLARSSL_SHA512_C) 437 handshake->
tls_prf = tls_prf_sha384;
438 handshake->
calc_verify = ssl_calc_verify_tls_sha384;
443 #if defined(POLARSSL_SHA256_C) 446 handshake->
tls_prf = tls_prf_sha256;
447 handshake->
calc_verify = ssl_calc_verify_tls_sha256;
468 if( handshake->
resume == 0 )
486 memcpy( handshake->
randbytes, tmp + 32, 32 );
487 memcpy( handshake->
randbytes + 32, tmp, 32 );
488 polarssl_zeroize( tmp,
sizeof( tmp ) );
524 transform->
ivlen = 12;
547 #if defined(POLARSSL_SSL_TRUNCATED_HMAC) 574 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) 580 #if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2) 595 SSL_DEBUG_MSG( 3, (
"keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
604 key1 = keyblk + transform->
maclen * 2;
605 key2 = keyblk + transform->
maclen * 2 + transform->
keylen;
608 mac_dec = keyblk + transform->
maclen;
615 memcpy( transform->
iv_enc, key2 + transform->
keylen, iv_copy_len );
616 memcpy( transform->
iv_dec, key2 + transform->
keylen + iv_copy_len,
621 key1 = keyblk + transform->
maclen * 2 + transform->
keylen;
622 key2 = keyblk + transform->
maclen * 2;
624 mac_enc = keyblk + transform->
maclen;
632 memcpy( transform->
iv_dec, key1 + transform->
keylen, iv_copy_len );
633 memcpy( transform->
iv_enc, key1 + transform->
keylen + iv_copy_len,
637 #if defined(POLARSSL_SSL_PROTO_SSL3) 651 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \ 652 defined(POLARSSL_SSL_PROTO_TLS1_2) 665 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL) 666 if( ssl_hw_record_init != NULL )
672 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->
keylen,
676 transform->
maclen ) ) != 0 )
685 cipher_info ) ) != 0 )
692 cipher_info ) ) != 0 )
714 #if defined(POLARSSL_CIPHER_MODE_CBC) 733 polarssl_zeroize( keyblk,
sizeof( keyblk ) );
735 #if defined(POLARSSL_ZLIB_SUPPORT) 740 if( ssl->compress_buf == NULL )
744 if( ssl->compress_buf == NULL )
754 memset( &transform->ctx_deflate, 0,
sizeof( transform->ctx_deflate ) );
755 memset( &transform->ctx_inflate, 0,
sizeof( transform->ctx_inflate ) );
757 if( deflateInit( &transform->ctx_deflate,
758 Z_DEFAULT_COMPRESSION ) != Z_OK ||
759 inflateInit( &transform->ctx_inflate ) != Z_OK )
772 #if defined(POLARSSL_SSL_PROTO_SSL3) 773 void ssl_calc_verify_ssl(
ssl_context *ssl,
unsigned char hash[36] )
777 unsigned char pad_1[48];
778 unsigned char pad_2[48];
785 memset( pad_1, 0x36, 48 );
786 memset( pad_2, 0x5C, 48 );
818 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) 819 void ssl_calc_verify_tls(
ssl_context *ssl,
unsigned char hash[36] )
842 #if defined(POLARSSL_SSL_PROTO_TLS1_2) 843 #if defined(POLARSSL_SHA256_C) 844 void ssl_calc_verify_tls_sha256(
ssl_context *ssl,
unsigned char hash[32] )
862 #if defined(POLARSSL_SHA512_C) 863 void ssl_calc_verify_tls_sha384(
ssl_context *ssl,
unsigned char hash[48] )
882 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED) 895 #if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) 898 if( end - p < 2 + (
int) ssl->
psk_len )
901 *(p++) = (
unsigned char)( ssl->
psk_len >> 8 );
902 *(p++) = (
unsigned char)( ssl->
psk_len );
907 #if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) 920 #if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) 924 size_t len = end - ( p + 2 );
934 *(p++) = (
unsigned char)( len >> 8 );
935 *(p++) = (
unsigned char)( len );
942 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) 949 p + 2, end - ( p + 2 ),
956 *(p++) = (
unsigned char)( zlen >> 8 );
957 *(p++) = (
unsigned char)( zlen );
970 if( end - p < 2 + (
int) ssl->
psk_len )
973 *(p++) = (
unsigned char)( ssl->
psk_len >> 8 );
974 *(p++) = (
unsigned char)( ssl->
psk_len );
984 #if defined(POLARSSL_SSL_PROTO_SSL3) 988 static void ssl_mac(
md_context_t *md_ctx,
unsigned char *secret,
989 unsigned char *buf,
size_t len,
990 unsigned char *ctr,
int type )
992 unsigned char header[11];
993 unsigned char padding[48];
1004 memcpy( header, ctr, 8 );
1005 header[ 8] = (
unsigned char) type;
1006 header[ 9] = (
unsigned char)( len >> 8 );
1007 header[10] = (
unsigned char)( len );
1009 memset( padding, 0x36, padlen );
1017 memset( padding, 0x5C, padlen );
1021 md_update( md_ctx, buf + len, md_size );
1040 #if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \ 1041 ( defined(POLARSSL_CIPHER_MODE_CBC) && \ 1042 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) ) 1046 #if defined(POLARSSL_SSL_PROTO_SSL3) 1056 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \ 1057 defined(POLARSSL_SSL_PROTO_TLS1_2) 1085 #if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) 1092 "including %d bytes of padding",
1102 ssl->
out_msg, &olen ) ) != 0 )
1116 #if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C) 1121 size_t enc_msglen, olen;
1122 unsigned char *enc_msg;
1123 unsigned char add_data[13];
1127 memcpy( add_data, ssl->
out_ctr, 8 );
1131 add_data[11] = ( ssl->
out_msglen >> 8 ) & 0xFF;
1162 "including %d bytes of padding",
1175 enc_msg, enc_msglen,
1177 enc_msg + enc_msglen, taglen ) ) != 0 )
1183 if( olen != enc_msglen )
1191 SSL_DEBUG_BUF( 4,
"after encrypt: tag", enc_msg + enc_msglen, taglen );
1195 #if defined(POLARSSL_CIPHER_MODE_CBC) && \ 1196 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) 1200 unsigned char *enc_msg;
1201 size_t enc_msglen, padlen, olen = 0;
1208 for( i = 0; i <= padlen; i++ )
1216 #if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2) 1244 "including %d bytes of IV and %d bytes of padding",
1254 enc_msg, enc_msglen,
1255 enc_msg, &olen ) ) != 0 )
1261 if( enc_msglen != olen )
1267 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) 1287 for( i = 8; i > 0; i-- )
1288 if( ++ssl->
out_ctr[i - 1] != 0 )
1294 SSL_DEBUG_MSG( 1, (
"outgoing message counter would wrap" ) );
1303 #define POLARSSL_SSL_MAX_MAC_SIZE 48 1310 #if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \ 1311 ( defined(POLARSSL_CIPHER_MODE_CBC) && \ 1312 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) ) 1313 size_t padlen = 0, correct = 1;
1325 #if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) 1337 ssl->
in_msg, &olen ) ) != 0 )
1351 #if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C) 1356 size_t dec_msglen, olen;
1357 unsigned char *dec_msg;
1358 unsigned char *dec_msg_result;
1359 unsigned char add_data[13];
1365 if( ssl->
in_msglen < explicit_iv_len + taglen )
1369 explicit_iv_len, taglen ) );
1372 dec_msglen = ssl->
in_msglen - explicit_iv_len - taglen;
1375 dec_msg_result = ssl->
in_msg;
1378 memcpy( add_data, ssl->
in_ctr, 8 );
1382 add_data[11] = ( ssl->
in_msglen >> 8 ) & 0xFF;
1394 SSL_DEBUG_BUF( 4,
"TAG used", dec_msg + dec_msglen, taglen );
1403 dec_msg, dec_msglen,
1404 dec_msg_result, &olen,
1405 dec_msg + dec_msglen, taglen ) ) != 0 )
1415 if( olen != dec_msglen )
1423 #if defined(POLARSSL_CIPHER_MODE_CBC) && \ 1424 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) 1431 unsigned char *dec_msg;
1432 unsigned char *dec_msg_result;
1447 #if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2) 1455 SSL_DEBUG_MSG( 1, (
"msglen (%d) < max( ivlen(%d), maclen (%d) " 1464 dec_msg_result = ssl->
in_msg;
1466 #if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2) 1483 dec_msg, dec_msglen,
1484 dec_msg_result, &olen ) ) != 0 )
1490 if( dec_msglen != olen )
1496 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) 1512 #if defined(POLARSSL_SSL_DEBUG_ALL) 1513 SSL_DEBUG_MSG( 1, (
"msglen (%d) < maclen (%d) + padlen (%d)",
1520 #if defined(POLARSSL_SSL_PROTO_SSL3) 1525 #if defined(POLARSSL_SSL_DEBUG_ALL) 1527 "should be no more than %d",
1535 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \ 1536 defined(POLARSSL_SSL_PROTO_TLS1_2) 1543 size_t pad_count = 0, real_count = 1;
1544 size_t padding_idx = ssl->
in_msglen - padlen - 1;
1556 correct &= ( ssl->
in_msglen >= padlen + 1 );
1560 padding_idx *= correct;
1562 for( i = 1; i <= 256; i++ )
1564 real_count &= ( i <= padlen );
1565 pad_count += real_count *
1566 ( ssl->
in_msg[padding_idx + i] == padlen - 1 );
1569 correct &= ( pad_count == padlen );
1571 #if defined(POLARSSL_SSL_DEBUG_ALL) 1572 if( padlen > 0 && correct == 0 )
1575 padlen &= correct * 0x1FF;
1599 #if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \ 1600 ( defined(POLARSSL_CIPHER_MODE_CBC) && \ 1601 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) ) 1605 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1614 #if defined(POLARSSL_SSL_PROTO_SSL3) 1624 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \ 1625 defined(POLARSSL_SSL_PROTO_TLS1_2) 1641 size_t j, extra_run = 0;
1642 extra_run = ( 13 + ssl->
in_msglen + padlen + 8 ) / 64 -
1645 extra_run &= correct * 0xFF;
1652 for( j = 0; j < extra_run; j++ )
1672 #if defined(POLARSSL_SSL_DEBUG_ALL) 1697 "messages, possible DoS attack" ) );
1704 for( i = 8; i > 0; i-- )
1705 if( ++ssl->
in_ctr[i - 1] != 0 )
1711 SSL_DEBUG_MSG( 1, (
"incoming message counter would wrap" ) );
1720 #if defined(POLARSSL_ZLIB_SUPPORT) 1727 unsigned char *msg_post = ssl->
out_msg;
1729 unsigned char *msg_pre = ssl->compress_buf;
1736 memcpy( msg_pre, ssl->
out_msg, len_pre );
1749 ret = deflate( &ssl->
transform_out->ctx_deflate, Z_SYNC_FLUSH );
1752 SSL_DEBUG_MSG( 1, (
"failed to perform compression (%d)", ret ) );
1773 unsigned char *msg_post = ssl->
in_msg;
1775 unsigned char *msg_pre = ssl->compress_buf;
1782 memcpy( msg_pre, ssl->
in_msg, len_pre );
1795 ret = inflate( &ssl->
transform_in->ctx_inflate, Z_SYNC_FLUSH );
1798 SSL_DEBUG_MSG( 1, (
"failed to perform decompression (%d)", ret ) );
1833 while( ssl->
in_left < nb_want )
1899 ssl->
out_msg[1] = (
unsigned char)( ( len - 4 ) >> 16 );
1900 ssl->
out_msg[2] = (
unsigned char)( ( len - 4 ) >> 8 );
1901 ssl->
out_msg[3] = (
unsigned char)( ( len - 4 ) );
1907 #if defined(POLARSSL_ZLIB_SUPPORT) 1911 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1921 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL) 1922 if( ssl_hw_record_write != NULL )
1926 ret = ssl_hw_record_write( ssl );
1942 ssl->
out_hdr[3] = (
unsigned char)( len >> 8 );
1943 ssl->
out_hdr[4] = (
unsigned char)( len );
1947 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1954 ssl->
out_hdr[3] = (
unsigned char)( len >> 8 );
1955 ssl->
out_hdr[4] = (
unsigned char)( len );
1961 "version = [%d:%d], msglen = %d",
2001 " %d, type = %d, hslen = %d",
2037 "version = [%d:%d], msglen = %d",
2080 #if defined(POLARSSL_SSL_PROTO_SSL3) 2089 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \ 2090 defined(POLARSSL_SSL_PROTO_TLS1_2) 2116 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL) 2117 if( ssl_hw_record_read != NULL )
2121 ret = ssl_hw_record_read( ssl );
2134 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2136 #if defined(POLARSSL_SSL_ALERT_MESSAGES) 2158 #if defined(POLARSSL_ZLIB_SUPPORT) 2162 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2196 " %d, type = %d, hslen = %d",
2263 unsigned char level,
2264 unsigned char message )
2289 #if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \ 2290 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \ 2291 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \ 2292 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \ 2293 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \ 2294 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \ 2295 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) 2361 #if defined(POLARSSL_SSL_PROTO_SSL3) 2402 while( crt != NULL )
2412 ssl->
out_msg[i ] = (
unsigned char)( n >> 16 );
2413 ssl->
out_msg[i + 1] = (
unsigned char)( n >> 8 );
2414 ssl->
out_msg[i + 2] = (
unsigned char)( n );
2416 i += 3; memcpy( ssl->
out_msg + i, crt->
raw.
p, n );
2417 i += n; crt = crt->
next;
2420 ssl->
out_msg[4] = (
unsigned char)( ( i - 7 ) >> 16 );
2421 ssl->
out_msg[5] = (
unsigned char)( ( i - 7 ) >> 8 );
2422 ssl->
out_msg[6] = (
unsigned char)( ( i - 7 ) );
2428 #if defined(POLARSSL_SSL_PROTO_SSL3) 2480 #if defined(POLARSSL_SSL_PROTO_SSL3) 2503 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \ 2504 defined(POLARSSL_SSL_PROTO_TLS1_2) 2511 memcmp( ssl->
in_msg + 4,
"\0\0\0", 3 ) == 0 )
2567 while( i < ssl->in_hslen )
2569 if( ssl->
in_msg[i] != 0 )
2575 n = ( (
unsigned int) ssl->
in_msg[i + 1] << 8 )
2576 | (
unsigned int) ssl->
in_msg[i + 2];
2579 if( n < 128 || i + n > ssl->
in_hslen )
2607 SSL_DEBUG_MSG( 1, (
"new server cert during renegotiation" ) );
2617 SSL_DEBUG_MSG( 1, (
"server cert changed during renegotiation" ) );
2647 #if defined(POLARSSL_SSL_SET_CURVES) 2653 ! ssl_curve_is_acceptable( ssl,
pk_ec( *pk )->grp.id ) )
2666 SSL_DEBUG_MSG( 1, (
"bad certificate (usage extensions)" ) );
2744 ((void) ciphersuite_info);
2746 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \ 2747 defined(POLARSSL_SSL_PROTO_TLS1_1) 2752 #if defined(POLARSSL_SSL_PROTO_TLS1_2) 2753 #if defined(POLARSSL_SHA512_C) 2758 #if defined(POLARSSL_SHA256_C) 2770 static void ssl_update_checksum_start(
ssl_context *ssl,
2771 const unsigned char *buf,
size_t len )
2773 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \ 2774 defined(POLARSSL_SSL_PROTO_TLS1_1) 2778 #if defined(POLARSSL_SSL_PROTO_TLS1_2) 2779 #if defined(POLARSSL_SHA256_C) 2782 #if defined(POLARSSL_SHA512_C) 2788 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \ 2789 defined(POLARSSL_SSL_PROTO_TLS1_1) 2790 static void ssl_update_checksum_md5sha1(
ssl_context *ssl,
2791 const unsigned char *buf,
size_t len )
2798 #if defined(POLARSSL_SSL_PROTO_TLS1_2) 2799 #if defined(POLARSSL_SHA256_C) 2800 static void ssl_update_checksum_sha256(
ssl_context *ssl,
2801 const unsigned char *buf,
size_t len )
2807 #if defined(POLARSSL_SHA512_C) 2808 static void ssl_update_checksum_sha384(
ssl_context *ssl,
2809 const unsigned char *buf,
size_t len )
2816 #if defined(POLARSSL_SSL_PROTO_SSL3) 2817 static void ssl_calc_finished_ssl(
2824 unsigned char padbuf[48];
2825 unsigned char md5sum[16];
2826 unsigned char sha1sum[20];
2846 #if !defined(POLARSSL_MD5_ALT) 2851 #if !defined(POLARSSL_SHA1_ALT) 2859 memset( padbuf, 0x36, 48 );
2861 md5_update( &md5, (
const unsigned char *) sender, 4 );
2866 sha1_update( &sha1, (
const unsigned char *) sender, 4 );
2871 memset( padbuf, 0x5C, 48 );
2890 polarssl_zeroize( padbuf,
sizeof( padbuf ) );
2891 polarssl_zeroize( md5sum,
sizeof( md5sum ) );
2892 polarssl_zeroize( sha1sum,
sizeof( sha1sum ) );
2898 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) 2899 static void ssl_calc_finished_tls(
2906 unsigned char padbuf[36];
2923 #if !defined(POLARSSL_MD5_ALT) 2928 #if !defined(POLARSSL_SHA1_ALT) 2935 :
"server finished";
2941 padbuf, 36, buf, len );
2948 polarssl_zeroize( padbuf,
sizeof( padbuf ) );
2954 #if defined(POLARSSL_SSL_PROTO_TLS1_2) 2955 #if defined(POLARSSL_SHA256_C) 2956 static void ssl_calc_finished_tls_sha256(
2962 unsigned char padbuf[32];
2978 #if !defined(POLARSSL_SHA256_ALT) 2985 :
"server finished";
2990 padbuf, 32, buf, len );
2996 polarssl_zeroize( padbuf,
sizeof( padbuf ) );
3002 #if defined(POLARSSL_SHA512_C) 3003 static void ssl_calc_finished_tls_sha384(
3009 unsigned char padbuf[48];
3025 #if !defined(POLARSSL_SHA512_ALT) 3026 SSL_DEBUG_BUF( 4,
"finished sha512 state", (
unsigned char *)
3032 :
"server finished";
3037 padbuf, 48, buf, len );
3043 polarssl_zeroize( padbuf,
sizeof( padbuf ) );
3151 SSL_DEBUG_MSG( 3, (
"switching to new transform spec for outbound data" ) );
3156 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL) 3157 if( ssl_hw_record_activate != NULL )
3159 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3181 unsigned int hash_len;
3182 unsigned char buf[36];
3192 SSL_DEBUG_MSG( 3, (
"switching to new transform spec for inbound data" ) );
3195 memset( ssl->
in_ctr, 0, 8 );
3208 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL) 3209 if( ssl_hw_record_activate != NULL )
3211 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3270 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \ 3271 defined(POLARSSL_SSL_PROTO_TLS1_1) 3277 #if defined(POLARSSL_SSL_PROTO_TLS1_2) 3278 #if defined(POLARSSL_SHA256_C) 3282 #if defined(POLARSSL_SHA512_C) 3291 #if defined(POLARSSL_DHM_C) 3294 #if defined(POLARSSL_ECDH_C) 3352 SSL_DEBUG_MSG( 1, (
"malloc() of ssl sub-contexts failed" ) );
3368 ssl_handshake_params_init( ssl->
handshake );
3370 #if defined(POLARSSL_X509_CRT_PARSE_C) 3399 #if defined(POLARSSL_DHM_C) 3418 if( ssl->
in_ctr == NULL )
3440 #if defined(POLARSSL_SSL_SESSION_TICKETS) 3444 #if defined(POLARSSL_SSL_SET_CURVES) 3448 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3494 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL) 3495 if( ssl_hw_record_reset != NULL )
3498 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
3520 #if defined(POLARSSL_SSL_ALPN) 3524 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3530 #if defined(POLARSSL_SSL_SESSION_TICKETS) 3542 static int ssl_ticket_keys_init(
ssl_context *ssl )
3546 unsigned char buf[16];
3560 ssl_ticket_keys_free( tkeys );
3565 if( ( ret = ssl->
f_rng( ssl->
p_rng, buf, 16 ) ) != 0 ||
3569 ssl_ticket_keys_free( tkeys );
3576 ssl_ticket_keys_free( tkeys );
3594 #if defined(POLARSSL_SSL_SESSION_TICKETS) 3605 #if defined(POLARSSL_X509_CRT_PARSE_C) 3607 int (*f_vrfy)(
void *,
x509_crt *,
int,
int *),
3616 int (*f_rng)(
void *,
unsigned char *,
size_t),
3624 void (*f_dbg)(
void *,
int,
const char *),
3632 int (*f_recv)(
void *,
unsigned char *,
size_t),
void *p_recv,
3633 int (*f_send)(
void *,
const unsigned char *,
size_t),
void *p_send )
3642 int (*f_get_cache)(
void *,
ssl_session *),
void *p_get_cache,
3643 int (*f_set_cache)(
void *,
const ssl_session *),
void *p_set_cache )
3680 const int *ciphersuites,
3681 int major,
int minor )
3692 #if defined(POLARSSL_X509_CRT_PARSE_C) 3699 if( key_cert == NULL )
3714 while( last->
next != NULL )
3716 last->
next = key_cert;
3723 x509_crl *ca_crl,
const char *peer_cn )
3735 if( key_cert == NULL )
3738 key_cert->
cert = own_cert;
3739 key_cert->
key = pk_key;
3744 #if defined(POLARSSL_RSA_C) 3751 if( key_cert == NULL )
3755 if( key_cert->
key == NULL )
3767 key_cert->
cert = own_cert;
3783 if( key_cert == NULL )
3787 if( key_cert->
key == NULL )
3793 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3796 key_cert->
cert = own_cert;
3803 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED) 3805 const unsigned char *psk_identity,
size_t psk_identity_len )
3807 if( psk == NULL || psk_identity == NULL )
3813 if( ssl->
psk != NULL )
3836 int (*f_psk)(
void *,
ssl_context *,
const unsigned char *,
3845 #if defined(POLARSSL_DHM_C) 3885 #if defined(POLARSSL_SSL_SET_CURVES) 3891 ssl->curve_list = curve_list;
3895 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION) 3898 if( hostname == NULL )
3911 memcpy( ssl->
hostname, (
const unsigned char *) hostname,
3921 const unsigned char *,
size_t),
3929 #if defined(POLARSSL_SSL_ALPN) 3932 size_t cur_len, tot_len;
3940 for( p = protos; *p != NULL; p++ )
3942 cur_len = strlen( *p );
3945 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3980 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH) 3995 #if defined(POLARSSL_SSL_TRUNCATED_HMAC) 4022 #if defined(POLARSSL_SSL_SESSION_TICKETS) 4030 if( ssl->
f_rng == NULL )
4033 return( ssl_ticket_keys_init( ssl ) );
4057 if( ssl == NULL || ssl->
session == NULL )
4068 return(
"SSLv3.0" );
4071 return(
"TLSv1.0" );
4074 return(
"TLSv1.1" );
4077 return(
"TLSv1.2" );
4082 return(
"unknown" );
4085 #if defined(POLARSSL_X509_CRT_PARSE_C) 4088 if( ssl == NULL || ssl->
session == NULL )
4105 return( ssl_session_copy( dst, ssl->
session ) );
4115 #if defined(POLARSSL_SSL_CLI_C) 4120 #if defined(POLARSSL_SSL_SRV_C) 4150 #if defined(POLARSSL_SSL_SRV_C) 4154 static int ssl_write_hello_request(
ssl_context *ssl )
4185 static int ssl_start_renegotiation(
ssl_context *ssl )
4191 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4216 #if defined(POLARSSL_SSL_SRV_C) 4229 return( ssl_write_hello_request( ssl ) );
4233 #if defined(POLARSSL_SSL_CLI_C) 4243 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4267 int ret, record_read = 0;
4324 SSL_DEBUG_MSG( 1, (
"handshake received (not HelloRequest)" ) );
4333 SSL_DEBUG_MSG( 3, (
"ignoring renegotiation, sending alert" ) );
4335 #if defined(POLARSSL_SSL_PROTO_SSL3) 4346 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \ 4347 defined(POLARSSL_SSL_PROTO_TLS1_2) 4367 ret = ssl_start_renegotiation( ssl );
4392 "but not honored by client" ) );
4400 SSL_DEBUG_MSG( 2, (
"ignoring non-fatal non-closure alert" ) );
4416 memcpy( buf, ssl->
in_offt, n );
4451 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH) 4455 max_len = mfl_code_to_length[ssl->
mfl_code];
4467 n = ( len < max_len) ? len : max_len;
4481 memcpy( ssl->
out_msg, buf, n );
4525 if( transform == NULL )
4528 #if defined(POLARSSL_ZLIB_SUPPORT) 4529 deflateEnd( &transform->ctx_deflate );
4530 inflateEnd( &transform->ctx_inflate );
4542 #if defined(POLARSSL_X509_CRT_PARSE_C) 4543 static void ssl_key_cert_free(
ssl_key_cert *key_cert )
4547 while( cur != NULL )
4565 if( handshake == NULL )
4568 #if defined(POLARSSL_DHM_C) 4571 #if defined(POLARSSL_ECDH_C) 4575 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C) 4580 #if defined(POLARSSL_X509_CRT_PARSE_C) && \ 4581 defined(POLARSSL_SSL_SERVER_NAME_INDICATION) 4590 while( cur != NULL )
4604 if( session == NULL )
4607 #if defined(POLARSSL_X509_CRT_PARSE_C) 4615 #if defined(POLARSSL_SSL_SESSION_TICKETS) 4619 polarssl_zeroize( session,
sizeof(
ssl_session ) );
4638 if( ssl->
in_ctr != NULL )
4644 #if defined(POLARSSL_ZLIB_SUPPORT) 4645 if( ssl->compress_buf != NULL )
4652 #if defined(POLARSSL_DHM_C) 4680 #if defined(POLARSSL_SSL_SESSION_TICKETS) 4688 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION) 4697 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED) 4698 if( ssl->
psk != NULL )
4709 #if defined(POLARSSL_X509_CRT_PARSE_C) 4710 ssl_key_cert_free( ssl->
key_cert );
4713 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL) 4714 if( ssl_hw_record_finish != NULL )
4717 ssl_hw_record_finish( ssl );
4727 #if defined(POLARSSL_PK_C) 4733 #if defined(POLARSSL_RSA_C) 4737 #if defined(POLARSSL_ECDSA_C) 4748 #if defined(POLARSSL_RSA_C) 4752 #if defined(POLARSSL_ECDSA_C) 4769 #if defined(POLARSSL_MD5_C) 4773 #if defined(POLARSSL_SHA1_C) 4777 #if defined(POLARSSL_SHA256_C) 4783 #if defined(POLARSSL_SHA512_C) 4794 #if defined(POLARSSL_SSL_SET_CURVES) 4804 if( *gid == grp_id )
4811 #if defined(POLARSSL_X509_CRT_PARSE_C) 4816 #if defined(POLARSSL_X509_CHECK_KEY_USAGE) 4819 #if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE) 4820 const char *ext_oid;
4824 #if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \ 4825 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE) 4827 ((void) cert_endpoint);
4830 #if defined(POLARSSL_X509_CHECK_KEY_USAGE) 4869 ((void) ciphersuite);
4872 #if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE) const ecp_curve_info ** curves
#define SSL_ALERT_LEVEL_FATAL
#define SSL_ALERT_MSG_BAD_RECORD_MAC
#define POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC
Processing of the ChangeCipherSpec handshake message failed.
int ssl_send_alert_message(ssl_context *ssl, unsigned char level, unsigned char message)
Send an alert message.
const pk_info_t * pk_info_from_type(pk_type_t pk_type)
Return information associated with the given PK type.
#define POLARSSL_DHM_RFC5114_MODP_1024_P
sha256_context fin_sha256
#define POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO
Unexpected message at ServerHello in renegotiation.
int rsa_copy(rsa_context *dst, const rsa_context *src)
Copy the components of an RSA context.
pk_type_t ssl_pk_alg_from_sig(unsigned char sig)
void sha256_update(sha256_context *ctx, const unsigned char *input, size_t ilen)
SHA-256 process buffer.
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.
void sha256(const unsigned char *input, size_t ilen, unsigned char output[32], int is224)
Output = SHA-256( input buffer )
char peer_verify_data[36]
int(* f_recv)(void *, unsigned char *, size_t)
int ssl_set_truncated_hmac(ssl_context *ssl, int truncate)
Activate negotiation of truncated HMAC (Client only) (Default: SSL_TRUNC_HMAC_ENABLED) ...
#define pk_ec(pk)
Quick access to an EC context inside a PK context.
ssl_transform * transform_out
x509_buf raw
The raw certificate data (DER).
#define POLARSSL_ERR_SSL_CONN_EOF
The connection indicated an EOF.
void sha1(const unsigned char *input, size_t ilen, unsigned char output[20])
Output = SHA-1( input buffer )
void sha1_finish(sha1_context *ctx, unsigned char output[20])
SHA-1 final digest.
void cipher_init(cipher_context_t *ctx)
Initialize a cipher_context (as NONE)
int md_starts(md_context_t *ctx)
Set-up the given context for a new message digest.
void md_init(md_context_t *ctx)
Initialize a md_context (as NONE)
static cipher_mode_t cipher_get_cipher_mode(const cipher_context_t *ctx)
Returns the mode of operation for the cipher.
ssl_session * session_negotiate
const cipher_info_t * cipher_info_from_type(const cipher_type_t cipher_type)
Returns the cipher information structure associated with the given cipher type.
void ssl_legacy_renegotiation(ssl_context *ssl, int allow_legacy)
Prevent or allow legacy renegotiation.
int ssl_parse_certificate(ssl_context *ssl)
void ssl_set_dbg(ssl_context *ssl, void(*f_dbg)(void *, int, const char *), void *p_dbg)
Set the debug callback.
#define POLARSSL_ERR_SSL_INVALID_RECORD
An invalid SSL record was received.
#define BADCERT_SKIP_VERIFY
Certificate verification was skipped.
#define SSL_RENEGO_MAX_RECORDS_DEFAULT
ssl_key_cert * sni_key_cert
int ssl_set_session_tickets(ssl_context *ssl, int use_tickets)
Enable / Disable session tickets (Default: SSL_SESSION_TICKETS_ENABLED on client, SSL_SESSION_TICKETS...
void ssl_set_verify(ssl_context *ssl, int(*f_vrfy)(void *, x509_crt *, int, int *), void *p_vrfy)
Set the verification callback (Optional).
ssl_transform * transform_in
int(* f_send)(void *, const unsigned char *, size_t)
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.
void ssl_set_psk_cb(ssl_context *ssl, int(*f_psk)(void *, ssl_context *, const unsigned char *, size_t), void *p_psk)
Set the PSK callback (server-side only) (Optional).
#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.
void sha1_hmac(const unsigned char *key, size_t keylen, const unsigned char *input, size_t ilen, unsigned char output[20])
Output = HMAC-SHA-1( hmac key, input buffer )
int md_process(md_context_t *ctx, const unsigned char *data)
int ssl_write_finished(ssl_context *ssl)
void x509_crt_free(x509_crt *crt)
Unallocate all certificate data.
Configuration options (set of defines)
ssl_transform * transform
int(* f_rng)(void *, unsigned char *, size_t)
int aes_setkey_dec(aes_context *ctx, const unsigned char *key, unsigned int keysize)
AES key schedule (decryption)
#define POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY
The peer notified us that the connection is going to be closed.
#define SSL_TRUNC_HMAC_ENABLED
void ssl_handshake_wrapup(ssl_context *ssl)
static unsigned char md_get_size(const md_info_t *md_info)
Returns the size of the message digest output.
int x509_crt_parse_der(x509_crt *chain, const unsigned char *buf, size_t buflen)
Parse a single DER formatted certificate and add it to the chained list.
void md5_finish(md5_context *ctx, unsigned char output[16])
MD5 final digest.
int(* f_sni)(void *, ssl_context *, const unsigned char *, size_t)
#define SSL_MAX_MAJOR_VERSION
#define SSL_MIN_MINOR_VERSION
#define SSL_VERIFY_OPTIONAL
int ssl_set_dh_param_ctx(ssl_context *ssl, dhm_context *dhm_ctx)
Set the Diffie-Hellman public P and G values, read from existing context (server-side only) ...
sha512_context fin_sha512
#define SSL_VERIFY_REQUIRED
#define SSL_ALERT_MSG_NO_RENEGOTIATION
void md5_hmac(const unsigned char *key, size_t keylen, const unsigned char *input, size_t ilen, unsigned char output[16])
Output = HMAC-MD5( hmac key, input buffer )
int ssl_handshake_server_step(ssl_context *ssl)
#define SSL_LEGACY_NO_RENEGOTIATION
static md_type_t md_get_type(const md_info_t *md_info)
Returns the type of the message digest output.
int cipher_crypt(cipher_context_t *ctx, const unsigned char *iv, size_t iv_len, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen)
Generic all-in-one encryption/decryption (for all ciphers except AEAD constructs).
unsigned char mac_key[16]
#define SSL_MAJOR_VERSION_3
#define POLARSSL_ERR_SSL_HW_ACCEL_FAILED
Hardware acceleration function returned with error.
void ssl_set_max_version(ssl_context *ssl, int major, int minor)
Set the maximum supported version sent from the client side and/or accepted at the server side (Defau...
#define SSL_RENEGOTIATION_DONE
#define POLARSSL_ERR_SSL_INVALID_MAC
Verification of the message MAC failed.
#define POLARSSL_CIPHERSUITE_SHORT_TAG
Short authentication tag, eg for CCM_8.
int(* f_psk)(void *, ssl_context *, const unsigned char *, size_t)
#define POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE
No client certification received from the client, but required by the authentication mode...
Object Identifier (OID) database.
int pk_init_ctx_rsa_alt(pk_context *ctx, void *key, pk_rsa_alt_decrypt_func decrypt_func, pk_rsa_alt_sign_func sign_func, pk_rsa_alt_key_len_func key_len_func)
Initialize an RSA-alt context.
void ssl_set_sni(ssl_context *ssl, int(*f_sni)(void *, ssl_context *, const unsigned char *, size_t), void *p_sni)
Set server side ServerName TLS extension callback (optional, server-side only).
void ssl_set_ciphersuites_for_version(ssl_context *ssl, const int *ciphersuites, int major, int minor)
Set the list of allowed ciphersuites and the preference order for a specific version of the protocol...
int ssl_init(ssl_context *ssl)
Initialize an SSL context (An individual SSL context is not thread-safe)
#define OID_SIZE(x)
Returns the size of the binary string, without the trailing \0.
const md_info_t * md_info
Information about the associated message digest.
struct _x509_crt * next
Next certificate in the CA-chain.
#define SSL_MINOR_VERSION_1
void md5_free(md5_context *ctx)
Clear MD5 context.
int ssl_set_psk(ssl_context *ssl, const unsigned char *psk, size_t psk_len, const unsigned char *psk_identity, size_t psk_identity_len)
Set the Pre Shared Key (PSK) and the identity name connected to it.
int x509_crt_check_key_usage(const x509_crt *crt, int usage)
Check usage of certificate against keyUsage extension.
#define POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH
Hardware acceleration function skipped / left alone data.
#define OID_SERVER_AUTH
id-kp-serverAuth OBJECT IDENTIFIER ::= { id-kp 1 }
int ssl_get_session(const ssl_context *ssl, ssl_session *session)
Save session in order to resume it later (client-side only) Session data is copied to presented sessi...
const char * ssl_get_alpn_protocol(const ssl_context *ssl)
Get the name of the negotiated Application Layer Protocol.
unsigned char iv[POLARSSL_MAX_IV_LENGTH]
Current IV or NONCE_COUNTER for CTR-mode.
size_t(* rsa_key_len_func)(void *ctx)
#define POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED
The own certificate is not set, but needed by the server.
#define SSL_RENEGOTIATION_PENDING
#define SSL_DEBUG_MPI(level, text, X)
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_ALERT_MSG_UNEXPECTED_MESSAGE
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
#define SSL_MINOR_VERSION_2
int ssl_write_certificate(ssl_context *ssl)
#define POLARSSL_DHM_RFC5114_MODP_1024_G
Container for an X.509 certificate.
#define POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE
A fatal alert message was received from our peer.
void md_free(md_context_t *ctx)
Free and clear the message-specific context of ctx.
#define SSL_MIN_MAJOR_VERSION
#define SSL_DEFAULT_TICKET_LIFETIME
Lifetime of session tickets (if enabled)
const char * ssl_get_ciphersuite(const ssl_context *ssl)
Return the name of the current ciphersuite.
const char * ssl_get_version(const ssl_context *ssl)
Return the current SSL version (SSLv3/TLSv1/etc)
void ssl_set_renegotiation(ssl_context *ssl, int renegotiation)
Enable / Disable renegotiation support for connection when initiated by peer (Default: SSL_RENEGOTIAT...
unsigned int key_length
Cipher key length, in bits (default length for variable sized ciphers) (Includes parity bits for ciph...
#define SSL_MINOR_VERSION_0
int cipher_auth_encrypt(cipher_context_t *ctx, const unsigned char *iv, size_t iv_len, const unsigned char *ad, size_t ad_len, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen, unsigned char *tag, size_t tag_len)
Generic autenticated encryption (AEAD ciphers).
#define SSL_MSG_CHANGE_CIPHER_SPEC
void(* update_checksum)(ssl_context *, const unsigned char *, size_t)
void sha256_starts(sha256_context *ctx, int is224)
SHA-256 context setup.
ssl_key_cert * key_cert
Current key/cert or key/cert list.
void dhm_init(dhm_context *ctx)
Initialize DHM context.
void x509_crt_init(x509_crt *crt)
Initialize a certificate (chain)
static x509_crt * ssl_own_cert(ssl_context *ssl)
int ssl_set_max_frag_len(ssl_context *ssl, unsigned char mfl_code)
Set the maximum fragment length to emit and/or negotiate (Default: SSL_MAX_CONTENT_LEN, usually 2^14 bytes) (Server: set maximum fragment length to emit, usually negotiated by the client during handshake (Client: set maximum fragment length to emit and negotiate with the server during handshake)
SHA-512 context structure.
int ssl_handshake_client_step(ssl_context *ssl)
#define POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE
An unexpected message was received from our peer.
unsigned char * p
ASN1 data, e.g.
key_exchange_type_t key_exchange
#define SSL_DEBUG_BUF(level, text, buf, len)
int cipher_auth_decrypt(cipher_context_t *ctx, const unsigned char *iv, size_t iv_len, const unsigned char *ad, size_t ad_len, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen, const unsigned char *tag, size_t tag_len)
Generic autenticated decryption (AEAD ciphers).
#define POLARSSL_ERR_SSL_COMPRESSION_FAILED
Processing of the compression / decompression failed.
int ssl_set_own_cert(ssl_context *ssl, x509_crt *own_cert, pk_context *pk_key)
Set own certificate chain and private key.
void ssl_set_endpoint(ssl_context *ssl, int endpoint)
Set the current endpoint type.
void mpi_free(mpi *X)
Unallocate one MPI.
void ssl_set_ciphersuites(ssl_context *ssl, const int *ciphersuites)
Set the list of allowed ciphersuites and the preference order.
void sha1_free(sha1_context *ctx)
Clear SHA-1 context.
void sha512_starts(sha512_context *ctx, int is384)
SHA-512 context setup.
void(* calc_finished)(ssl_context *, unsigned char *, int)
int x509_crt_verify(x509_crt *crt, x509_crt *trust_ca, x509_crl *ca_crl, const char *cn, int *flags, int(*f_vrfy)(void *, x509_crt *, int, int *), void *p_vrfy)
Verify the certificate signature.
#define SSL_ALERT_LEVEL_WARNING
void ssl_set_rng(ssl_context *ssl, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Set the random number generator callback.
int pk_can_do(pk_context *ctx, pk_type_t type)
Tell if a context can do the operation given by type.
void ssl_set_bio(ssl_context *ssl, int(*f_recv)(void *, unsigned char *, size_t), void *p_recv, int(*f_send)(void *, const unsigned char *, size_t), void *p_send)
Set the underlying BIO read and write callbacks.
void ssl_free(ssl_context *ssl)
Free referenced items in an SSL context and clear memory.
void sha512(const unsigned char *input, size_t ilen, unsigned char output[64], int is384)
Output = SHA-512( input buffer )
void md5_starts(md5_context *ctx)
MD5 context setup.
#define SSL_RENEGOTIATION_DISABLED
unsigned char ssl_sig_from_pk(pk_context *pk)
#define POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED
No CA Chain is set, but required to operate.
void ssl_handshake_free(ssl_handshake_params *handshake)
Free referenced items in an SSL handshake context and clear memory.
int(* f_vrfy)(void *, x509_crt *, int, int *)
int ssl_flush_output(ssl_context *ssl)
int ssl_handshake(ssl_context *ssl)
Perform the SSL handshake.
void ssl_set_min_version(ssl_context *ssl, int major, int minor)
Set the minimum accepted SSL/TLS protocol version (Default: SSL_MIN_MAJOR_VERSION, SSL_MIN_MINOR_VERSION)
#define SSL_COMPRESS_DEFLATE
int ssl_set_hostname(ssl_context *ssl, const char *hostname)
Set hostname for ServerName TLS extension (client-side only)
int ssl_handshake_step(ssl_context *ssl)
Perform a single step of the SSL handshake.
#define SSL_MINOR_VERSION_3
int ssl_check_cert_usage(const x509_crt *cert, const ssl_ciphersuite_t *ciphersuite, int cert_endpoint)
pk_type_t
Public key types.
#define POLARSSL_ERR_NET_WANT_READ
Connection requires a read call.
#define SSL_HS_CERTIFICATE
#define POLARSSL_PSK_MAX_LEN
#define SSL_DEBUG_RET(level, text, ret)
int ssl_parse_change_cipher_spec(ssl_context *ssl)
void sha1_starts(sha1_context *ctx)
SHA-1 context setup.
int pk_init_ctx(pk_context *ctx, const pk_info_t *info)
Initialize a PK context with the information given and allocates the type-specific PK subcontext...
void sha512_hmac(const unsigned char *key, size_t keylen, const unsigned char *input, size_t ilen, unsigned char output[64], int is384)
Output = HMAC-SHA-512( hmac key, input buffer )
#define SSL_ALERT_MSG_HANDSHAKE_FAILURE
This structure is used for storing ciphersuite information.
int ssl_close_notify(ssl_context *ssl)
Notify the peer that the connection is being closed.
const x509_crt * ssl_get_peer_cert(const ssl_context *ssl)
Return the peer certificate from the current connection.
void ssl_set_session_cache(ssl_context *ssl, int(*f_get_cache)(void *, ssl_session *), void *p_get_cache, int(*f_set_cache)(void *, const ssl_session *), void *p_set_cache)
Set the session cache callbacks (server-side only) If not set, no session resuming is done...
int(* rsa_sign_func)(void *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
void md5_init(md5_context *ctx)
Initialize MD5 context.
size_t ssl_get_bytes_avail(const ssl_context *ssl)
Return the number of data bytes available to read.
int md_hmac_starts(md_context_t *ctx, const unsigned char *key, size_t keylen)
Generic HMAC context setup.
void cipher_free(cipher_context_t *ctx)
Free and clear the cipher-specific context of ctx.
int ssl_set_session(ssl_context *ssl, const ssl_session *session)
Request resumption of session (client-side only) Session data is copied from presented session struct...
int cipher_set_padding_mode(cipher_context_t *ctx, cipher_padding_t mode)
Set padding mode, for cipher modes that use padding.
cipher_mode_t mode
Cipher mode (e.g.
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
#define SSL_INITIAL_HANDSHAKE
#define SSL_MAX_MINOR_VERSION
void sha512_finish(sha512_context *ctx, unsigned char output[64])
SHA-512 final digest.
int(* f_set_cache)(void *, const ssl_session *)
int cipher_init_ctx(cipher_context_t *ctx, const cipher_info_t *cipher_info)
Initialises and fills the cipher context structure with the appropriate values.
int allow_legacy_renegotiation
int cipher_setkey(cipher_context_t *ctx, const unsigned char *key, int key_length, const operation_t operation)
Set the key to use with the given context.
#define POLARSSL_ERR_SSL_INTERNAL_ERROR
Internal error (eg, unexpected failure in lower-level module)
ssl_session * session_out
void sha256_init(sha256_context *ctx)
Initialize SHA-256 context.
#define SSL_TRUNCATED_HMAC_LEN
int ssl_read_record(ssl_context *ssl)
int ssl_set_own_cert_rsa(ssl_context *ssl, x509_crt *own_cert, rsa_context *rsa_key)
Set own certificate chain and private RSA key.
size_t len
ASN1 length, e.g.
int md_hmac_reset(md_context_t *ctx)
Generic HMAC context reset.
ecp_group_id
Domain parameters (curve, subgroup and generator) identifiers.
int ssl_set_dh_param(ssl_context *ssl, const char *dhm_P, const char *dhm_G)
Set the Diffie-Hellman public P and G values, read as hexadecimal strings (server-side only) (Default...
#define SSL_DEBUG_MSG(level, args)
void ssl_set_session_ticket_lifetime(ssl_context *ssl, int lifetime)
Set session ticket lifetime (server only) (Default: SSL_DEFAULT_TICKET_LIFETIME (86400 secs / 1 day))...
#define SSL_MAX_FRAG_LEN_INVALID
int md_hmac_update(md_context_t *ctx, const unsigned char *input, size_t ilen)
Generic HMAC process buffer.
#define BADCERT_MISSING
Certificate was missing.
void pk_free(pk_context *ctx)
Free a pk_context.
void sha256_free(sha256_context *ctx)
Clear SHA-256 context.
#define POLARSSL_ERR_SSL_BAD_HS_FINISHED
Processing of the Finished handshake message failed.
void(* calc_verify)(ssl_context *, unsigned char *)
int mpi_copy(mpi *X, const mpi *Y)
Copy the contents of Y into X.
#define SSL_MAX_CONTENT_LEN
int ssl_get_verify_result(const ssl_context *ssl)
Return the result of the certificate verification.
#define SSL_ALERT_MSG_NO_CERT
never pad (full blocks only)
int ssl_session_reset(ssl_context *ssl)
Reset an already initialized SSL context for re-use while retaining application-set variables...
void pk_init(pk_context *ctx)
Initialize a pk_context (as NONE)
#define KU_DIGITAL_SIGNATURE
void ssl_session_init(ssl_session *session)
Initialize SSL session structure.
void aes_free(aes_context *ctx)
Clear AES context.
Certificate revocation list structure.
const int * ssl_list_ciphersuites(void)
Returns the list of ciphersuites supported by the SSL/TLS module.
pk_context pk
Container for the public key context.
ssl_transform * transform_negotiate
int ssl_set_alpn_protocols(ssl_context *ssl, const char **protos)
Set the supported Application Layer Protocols.
#define SSL_LEGACY_RENEGOTIATION
void sha1_init(sha1_context *ctx)
Initialize SHA-1 context.
void ssl_set_renegotiation_enforced(ssl_context *ssl, int max_records)
Enforce server-requested renegotiation.
#define POLARSSL_ERR_SSL_MALLOC_FAILED
Memory allocation failed.
int disable_renegotiation
#define SSL_ALERT_MSG_CLOSE_NOTIFY
void sha256_finish(sha256_context *ctx, unsigned char output[32])
SHA-256 final digest.
void sha1_update(sha1_context *ctx, const unsigned char *input, size_t ilen)
SHA-1 process buffer.
unsigned int block_size
block size, in bytes
void dhm_free(dhm_context *ctx)
Free and clear the components of a DHM key.
void ecdh_init(ecdh_context *ctx)
Initialize context.
void md5_update(md5_context *ctx, const unsigned char *input, size_t ilen)
MD5 process buffer.
#define OID_CLIENT_AUTH
id-kp-clientAuth OBJECT IDENTIFIER ::= { id-kp 2 }
int ssl_write_change_cipher_spec(ssl_context *ssl)
int ssl_derive_keys(ssl_context *ssl)
void ssl_set_authmode(ssl_context *ssl, int authmode)
Set the certificate verification mode.
int(* rsa_decrypt_func)(void *ctx, int mode, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
SHA-256 context structure.
int md_finish(md_context_t *ctx, unsigned char *output)
Generic message digest final digest.
int ssl_psk_derive_premaster(ssl_context *ssl, key_exchange_type_t key_ex)
void sha512_free(sha512_context *ctx)
Clear SHA-512 context.
int(* tls_prf)(const unsigned char *, size_t, const char *, const unsigned char *, size_t, unsigned char *, size_t)
static int safer_memcmp(const void *a, const void *b, size_t n)
int ssl_send_fatal_handshake_failure(ssl_context *ssl)
ssl_ticket_keys * ticket_keys
int ssl_read(ssl_context *ssl, unsigned char *buf, size_t len)
Read at most 'len' application data bytes.
#define SSL_DEBUG_CRT(level, text, crt)
void ssl_transform_free(ssl_transform *transform)
Free referenced items in an SSL transform context and clear memory.
unsigned char * psk_identity
#define SSL_SESSION_TICKETS_ENABLED
#define SSL_MSG_APPLICATION_DATA
const char * ssl_get_ciphersuite_name(const int ciphersuite_id)
Return the name of the ciphersuite associated with the given ID.
void(* f_dbg)(void *, int, const char *)
int ssl_renegotiate(ssl_context *ssl)
Initiate an SSL renegotiation on the running connection.
unsigned char key_name[16]
int ssl_write(ssl_context *ssl, const unsigned char *buf, size_t len)
Write exactly 'len' application data bytes.
void md5(const unsigned char *input, size_t ilen, unsigned char output[16])
Output = MD5( input buffer )
void sha512_update(sha512_context *ctx, const unsigned char *input, size_t ilen)
SHA-512 process buffer.
#define POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE
The requested feature is not available.
#define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE
Processing of the Certificate handshake message failed.
int x509_crt_check_extended_key_usage(const x509_crt *crt, const char *usage_oid, size_t usage_len)
Check usage of certificate against extentedJeyUsage.
void ssl_set_ca_chain(ssl_context *ssl, x509_crt *ca_chain, x509_crl *ca_crl, const char *peer_cn)
Set the data required to verify peer certificate.
int aes_setkey_enc(aes_context *ctx, const unsigned char *key, unsigned int keysize)
AES key schedule (encryption)
Message digest information.
int(* f_get_cache)(void *, ssl_session *)
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_set_own_cert_alt(ssl_context *ssl, x509_crt *own_cert, void *rsa_key, rsa_decrypt_func rsa_decrypt, rsa_sign_func rsa_sign, rsa_key_len_func rsa_key_len)
Set own certificate and alternate non-PolarSSL RSA private key and handling callbacks, such as the PKCS#11 wrappers or any other external private key handler.
unsigned int iv_size
IV/NONCE size, in bytes.
#define KU_KEY_ENCIPHERMENT
#define POLARSSL_ERR_CIPHER_AUTH_FAILED
Authentication failed (for AEAD modes).
int ssl_fetch_input(ssl_context *ssl, size_t nb_want)
int ssl_write_record(ssl_context *ssl)
#define pk_rsa(pk)
Quick access to an RSA context inside a PK context.
void aes_init(aes_context *ctx)
Initialize AES context.
void ecdh_free(ecdh_context *ctx)
Free context.
unsigned char randbytes[64]
const ecp_group_id * ecp_grp_id_list(void)
Get the list of supported curves in order of preferrence (grp_id only)
int md_hmac_finish(md_context_t *ctx, unsigned char *output)
Generic HMAC final digest.
void sha512_init(sha512_context *ctx)
Initialize SHA-512 context.
#define POLARSSL_ERR_SSL_COUNTER_WRAPPING
A counter would wrap (eg, too many messages exchanged).
Generic message digest context.
void ssl_optimize_checksum(ssl_context *ssl, const ssl_ciphersuite_t *ciphersuite_info)
md_type_t ssl_md_alg_from_hash(unsigned char hash)
int dhm_calc_secret(dhm_context *ctx, unsigned char *output, size_t *olen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Derive and export the shared secret (G^Y)^X mod P.
#define SSL_HS_HELLO_REQUEST