36 #if defined(POLARSSL_SSL_TLS_C)
41 #if defined(POLARSSL_MEMORY_C)
44 #define polarssl_malloc malloc
45 #define polarssl_free free
50 #if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
52 #define strcasecmp _stricmp
55 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
79 #if defined(POLARSSL_X509_CRT_PARSE_C)
100 #if defined(POLARSSL_SSL_SESSION_TICKETS)
114 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
116 const unsigned char *key_enc,
const unsigned char *key_dec,
118 const unsigned char *iv_enc,
const unsigned char *iv_dec,
120 const unsigned char *mac_enc,
const unsigned char *mac_dec,
121 size_t maclen) = NULL;
122 int (*ssl_hw_record_activate)(
ssl_context *ssl,
int direction) = NULL;
123 int (*ssl_hw_record_reset)(
ssl_context *ssl) = NULL;
124 int (*ssl_hw_record_write)(
ssl_context *ssl) = NULL;
125 int (*ssl_hw_record_read)(
ssl_context *ssl) = NULL;
126 int (*ssl_hw_record_finish)(
ssl_context *ssl) = NULL;
132 #if defined(POLARSSL_SSL_PROTO_SSL3)
133 static int ssl3_prf(
const unsigned char *secret,
size_t slen,
135 const unsigned char *random,
size_t rlen,
136 unsigned char *dstbuf,
size_t dlen )
141 unsigned char padding[16];
142 unsigned char sha1sum[20];
153 for( i = 0; i < dlen / 16; i++ )
155 memset( padding, (
unsigned char) (
'A' + i), 1 + i );
169 memset( &md5, 0,
sizeof( md5 ) );
170 memset( &sha1, 0,
sizeof( sha1 ) );
172 memset( padding, 0,
sizeof( padding ) );
173 memset( sha1sum, 0,
sizeof( sha1sum ) );
179 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
180 static int tls1_prf(
const unsigned char *secret,
size_t slen,
182 const unsigned char *random,
size_t rlen,
183 unsigned char *dstbuf,
size_t dlen )
187 const unsigned char *S1, *S2;
188 unsigned char tmp[128];
189 unsigned char h_i[20];
191 if(
sizeof( tmp ) < 20 + strlen( label ) + rlen )
194 hs = ( slen + 1 ) / 2;
196 S2 = secret + slen - hs;
198 nb = strlen( label );
199 memcpy( tmp + 20, label, nb );
200 memcpy( tmp + 20 + nb, random, rlen );
206 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
208 for( i = 0; i < dlen; i += 16 )
210 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
211 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
213 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
215 for( j = 0; j < k; j++ )
216 dstbuf[i + j] = h_i[j];
224 for( i = 0; i < dlen; i += 20 )
229 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
231 for( j = 0; j < k; j++ )
232 dstbuf[i + j] = (
unsigned char)( dstbuf[i + j] ^ h_i[j] );
235 memset( tmp, 0,
sizeof( tmp ) );
236 memset( h_i, 0,
sizeof( h_i ) );
242 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
243 #if defined(POLARSSL_SHA256_C)
244 static int tls_prf_sha256(
const unsigned char *secret,
size_t slen,
246 const unsigned char *random,
size_t rlen,
247 unsigned char *dstbuf,
size_t dlen )
251 unsigned char tmp[128];
252 unsigned char h_i[32];
254 if(
sizeof( tmp ) < 32 + strlen( label ) + rlen )
257 nb = strlen( label );
258 memcpy( tmp + 32, label, nb );
259 memcpy( tmp + 32 + nb, random, rlen );
267 for( i = 0; i < dlen; i += 32 )
272 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
274 for( j = 0; j < k; j++ )
275 dstbuf[i + j] = h_i[j];
278 memset( tmp, 0,
sizeof( tmp ) );
279 memset( h_i, 0,
sizeof( h_i ) );
285 #if defined(POLARSSL_SHA512_C)
286 static int tls_prf_sha384(
const unsigned char *secret,
size_t slen,
288 const unsigned char *random,
size_t rlen,
289 unsigned char *dstbuf,
size_t dlen )
293 unsigned char tmp[128];
294 unsigned char h_i[48];
296 if(
sizeof( tmp ) < 48 + strlen( label ) + rlen )
299 nb = strlen( label );
300 memcpy( tmp + 48, label, nb );
301 memcpy( tmp + 48 + nb, random, rlen );
309 for( i = 0; i < dlen; i += 48 )
314 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
316 for( j = 0; j < k; j++ )
317 dstbuf[i + j] = h_i[j];
320 memset( tmp, 0,
sizeof( tmp ) );
321 memset( h_i, 0,
sizeof( h_i ) );
328 static void ssl_update_checksum_start(
ssl_context *,
const unsigned char *,
size_t);
330 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
331 defined(POLARSSL_SSL_PROTO_TLS1_1)
332 static void ssl_update_checksum_md5sha1(
ssl_context *,
const unsigned char *,
size_t);
335 #if defined(POLARSSL_SSL_PROTO_SSL3)
336 static void ssl_calc_verify_ssl(
ssl_context *,
unsigned char *);
337 static void ssl_calc_finished_ssl(
ssl_context *,
unsigned char *,
int);
340 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
341 static void ssl_calc_verify_tls(
ssl_context *,
unsigned char *);
342 static void ssl_calc_finished_tls(
ssl_context *,
unsigned char *,
int);
345 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
346 #if defined(POLARSSL_SHA256_C)
347 static void ssl_update_checksum_sha256(
ssl_context *,
const unsigned char *,
size_t);
348 static void ssl_calc_verify_tls_sha256(
ssl_context *,
unsigned char *);
349 static void ssl_calc_finished_tls_sha256(
ssl_context *,
unsigned char *,
int);
352 #if defined(POLARSSL_SHA512_C)
353 static void ssl_update_checksum_sha384(
ssl_context *,
const unsigned char *,
size_t);
354 static void ssl_calc_verify_tls_sha384(
ssl_context *,
unsigned char *);
355 static void ssl_calc_finished_tls_sha384(
ssl_context *,
unsigned char *,
int);
362 unsigned char tmp[64];
363 unsigned char keyblk[256];
366 unsigned char *mac_enc;
367 unsigned char *mac_dec;
379 if( cipher_info == NULL )
387 if( md_info == NULL )
397 #if defined(POLARSSL_SSL_PROTO_SSL3)
406 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
415 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
416 #if defined(POLARSSL_SHA512_C)
420 handshake->
tls_prf = tls_prf_sha384;
421 handshake->
calc_verify = ssl_calc_verify_tls_sha384;
426 #if defined(POLARSSL_SHA256_C)
429 handshake->
tls_prf = tls_prf_sha256;
430 handshake->
calc_verify = ssl_calc_verify_tls_sha256;
451 if( handshake->
resume == 0 )
469 memcpy( handshake->
randbytes, tmp + 32, 32 );
470 memcpy( handshake->
randbytes + 32, tmp, 32 );
471 memset( tmp, 0,
sizeof( tmp ) );
505 transform->
ivlen = 12;
529 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
554 SSL_DEBUG_MSG( 3, (
"keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
563 key1 = keyblk + transform->
maclen * 2;
564 key2 = keyblk + transform->
maclen * 2 + transform->
keylen;
567 mac_dec = keyblk + transform->
maclen;
574 memcpy( transform->
iv_enc, key2 + transform->
keylen, iv_copy_len );
575 memcpy( transform->
iv_dec, key2 + transform->
keylen + iv_copy_len,
580 key1 = keyblk + transform->
maclen * 2 + transform->
keylen;
581 key2 = keyblk + transform->
maclen * 2;
583 mac_enc = keyblk + transform->
maclen;
591 memcpy( transform->
iv_dec, key1 + transform->
keylen, iv_copy_len );
592 memcpy( transform->
iv_enc, key1 + transform->
keylen + iv_copy_len,
596 #if defined(POLARSSL_SSL_PROTO_SSL3)
604 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
605 defined(POLARSSL_SSL_PROTO_TLS1_2)
618 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
619 if( ssl_hw_record_init != NULL)
625 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->
keylen,
629 transform->
maclen ) ) != 0 )
638 cipher_info ) ) != 0 )
645 cipher_info ) ) != 0 )
667 #if defined(POLARSSL_CIPHER_MODE_CBC)
686 memset( keyblk, 0,
sizeof( keyblk ) );
688 #if defined(POLARSSL_ZLIB_SUPPORT)
693 if( ssl->compress_buf == NULL )
697 if( ssl->compress_buf == NULL )
707 memset( &transform->ctx_deflate, 0,
sizeof( transform->ctx_deflate ) );
708 memset( &transform->ctx_inflate, 0,
sizeof( transform->ctx_inflate ) );
710 if( deflateInit( &transform->ctx_deflate, Z_DEFAULT_COMPRESSION ) != Z_OK ||
711 inflateInit( &transform->ctx_inflate ) != Z_OK )
724 #if defined(POLARSSL_SSL_PROTO_SSL3)
725 void ssl_calc_verify_ssl(
ssl_context *ssl,
unsigned char hash[36] )
729 unsigned char pad_1[48];
730 unsigned char pad_2[48];
737 memset( pad_1, 0x36, 48 );
738 memset( pad_2, 0x5C, 48 );
767 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
768 void ssl_calc_verify_tls(
ssl_context *ssl,
unsigned char hash[36] )
788 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
789 #if defined(POLARSSL_SHA256_C)
790 void ssl_calc_verify_tls_sha256(
ssl_context *ssl,
unsigned char hash[32] )
806 #if defined(POLARSSL_SHA512_C)
807 void ssl_calc_verify_tls_sha384(
ssl_context *ssl,
unsigned char hash[48] )
824 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
837 #if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
840 if( end - p < 2 + (
int) ssl->
psk_len )
843 *(p++) = (
unsigned char)( ssl->
psk_len >> 8 );
844 *(p++) = (
unsigned char)( ssl->
psk_len );
849 #if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
862 #if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
868 if( end - p < 2 + (
int) len )
871 *(p++) = (
unsigned char)( len >> 8 );
872 *(p++) = (
unsigned char)( len );
885 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
892 p + 2, end - (p + 2),
899 *(p++) = (
unsigned char)( zlen >> 8 );
900 *(p++) = (
unsigned char)( zlen );
913 *(p++) = (
unsigned char)( ssl->
psk_len >> 8 );
914 *(p++) = (
unsigned char)( ssl->
psk_len );
924 #if defined(POLARSSL_SSL_PROTO_SSL3)
928 static void ssl_mac(
md_context_t *md_ctx,
unsigned char *secret,
929 unsigned char *buf,
size_t len,
930 unsigned char *ctr,
int type )
932 unsigned char header[11];
933 unsigned char padding[48];
945 memcpy( header, ctr, 8 );
946 header[ 8] = (
unsigned char) type;
947 header[ 9] = (
unsigned char)( len >> 8 );
948 header[10] = (
unsigned char)( len );
950 memset( padding, 0x36, padlen );
958 memset( padding, 0x5C, padlen );
979 #if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
980 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
981 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
985 #if defined(POLARSSL_SSL_PROTO_SSL3)
995 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
996 defined(POLARSSL_SSL_PROTO_TLS1_2)
1024 #if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
1032 "including %d bytes of padding",
1062 SSL_DEBUG_MSG( 1, (
"total encrypted length incorrect %d %d",
1068 ssl->
out_msg + olen, &olen ) ) != 0 )
1076 SSL_DEBUG_MSG( 1, (
"total encrypted length incorrect %d %d",
1083 #if defined(POLARSSL_GCM_C)
1087 size_t enc_msglen, olen, totlen;
1088 unsigned char *enc_msg;
1089 unsigned char add_data[13];
1094 memcpy( add_data, ssl->
out_ctr, 8 );
1098 add_data[11] = ( ssl->
out_msglen >> 8 ) & 0xFF;
1129 "including %d bytes of padding",
1147 add_data, 13 ) ) != 0 )
1153 enc_msg, enc_msglen,
1154 enc_msg, &olen ) ) != 0 )
1161 enc_msg + olen, &olen ) ) != 0 )
1167 if( totlen != enc_msglen )
1179 enc_msg + enc_msglen, 16 ) ) != 0 )
1184 SSL_DEBUG_BUF( 4,
"after encrypt: tag", enc_msg + enc_msglen, 16 );
1188 #if defined(POLARSSL_CIPHER_MODE_CBC) && \
1189 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
1194 unsigned char *enc_msg;
1195 size_t enc_msglen, padlen, olen = 0;
1202 for( i = 0; i <= padlen; i++ )
1210 #if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
1238 "including %d bytes of IV and %d bytes of padding",
1259 enc_msg, enc_msglen, enc_msg,
1269 enc_msg + olen, &olen ) ) != 0 )
1275 if( enc_msglen != olen )
1277 SSL_DEBUG_MSG( 1, (
"total encrypted length incorrect %d %d",
1278 enc_msglen, olen ) );
1282 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
1302 for( i = 8; i > 0; i-- )
1303 if( ++ssl->
out_ctr[i - 1] != 0 )
1311 #define POLARSSL_SSL_MAX_MAC_SIZE 48
1315 size_t i, padlen = 0, correct = 1;
1316 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1327 #if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
1365 ssl->
in_msg + olen, &olen ) ) != 0 )
1379 #if defined(POLARSSL_GCM_C)
1383 unsigned char *dec_msg;
1384 unsigned char *dec_msg_result;
1385 size_t dec_msglen, olen, totlen;
1386 unsigned char add_data[13];
1395 dec_msg_result = ssl->
in_msg;
1398 memcpy( add_data, ssl->
in_ctr, 8 );
1402 add_data[11] = ( ssl->
in_msglen >> 8 ) & 0xFF;
1428 add_data, 13 ) ) != 0 )
1434 dec_msg, dec_msglen,
1435 dec_msg_result, &olen ) ) != 0 )
1442 dec_msg_result + olen, &olen ) ) != 0 )
1448 if( totlen != dec_msglen )
1458 dec_msg + dec_msglen, 16 ) ) != 0 )
1467 #if defined(POLARSSL_CIPHER_MODE_CBC) && \
1468 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
1476 unsigned char *dec_msg;
1477 unsigned char *dec_msg_result;
1492 #if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
1500 SSL_DEBUG_MSG( 1, (
"msglen (%d) < max( ivlen(%d), maclen (%d) + 1 ) ( + expl IV )",
1507 dec_msg_result = ssl->
in_msg;
1509 #if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
1538 dec_msg, dec_msglen, dec_msg_result,
1547 dec_msg_result + olen, &olen ) ) != 0 )
1553 if( dec_msglen != olen )
1559 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
1575 #if defined(POLARSSL_SSL_DEBUG_ALL)
1576 SSL_DEBUG_MSG( 1, (
"msglen (%d) < maclen (%d) + padlen (%d)",
1583 #if defined(POLARSSL_SSL_PROTO_SSL3)
1588 #if defined(POLARSSL_SSL_DEBUG_ALL)
1590 "should be no more than %d",
1598 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1599 defined(POLARSSL_SSL_PROTO_TLS1_2)
1606 size_t pad_count = 0, real_count = 1;
1607 size_t padding_idx = ssl->
in_msglen - padlen - 1;
1609 for( i = 1; i <= 256; i++ )
1611 real_count &= ( i <= padlen );
1612 pad_count += real_count *
1613 ( ssl->
in_msg[padding_idx + i] == padlen - 1 );
1616 correct &= ( pad_count == padlen );
1618 #if defined(POLARSSL_SSL_DEBUG_ALL)
1619 if( padlen > 0 && correct == 0)
1622 padlen &= correct * 0x1FF;
1646 #if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1647 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1648 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1659 #if defined(POLARSSL_SSL_PROTO_SSL3)
1669 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1670 defined(POLARSSL_SSL_PROTO_TLS1_2)
1686 size_t j, extra_run = 0;
1687 extra_run = ( 13 + ssl->
in_msglen + padlen + 8 ) / 64 -
1690 extra_run &= correct * 0xFF;
1697 for( j = 0; j < extra_run; j++ )
1717 #if defined(POLARSSL_SSL_DEBUG_ALL)
1742 "messages, possible DoS attack" ) );
1749 for( i = 8; i > 0; i-- )
1750 if( ++ssl->
in_ctr[i - 1] != 0 )
1758 #if defined(POLARSSL_ZLIB_SUPPORT)
1765 unsigned char *msg_post = ssl->
out_msg;
1767 unsigned char *msg_pre = ssl->compress_buf;
1774 memcpy( msg_pre, ssl->
out_msg, len_pre );
1787 ret = deflate( &ssl->
transform_out->ctx_deflate, Z_SYNC_FLUSH );
1790 SSL_DEBUG_MSG( 1, (
"failed to perform compression (%d)", ret ) );
1810 unsigned char *msg_post = ssl->
in_msg;
1812 unsigned char *msg_pre = ssl->compress_buf;
1819 memcpy( msg_pre, ssl->
in_msg, len_pre );
1832 ret = inflate( &ssl->
transform_in->ctx_inflate, Z_SYNC_FLUSH );
1835 SSL_DEBUG_MSG( 1, (
"failed to perform decompression (%d)", ret ) );
1863 while( ssl->
in_left < nb_want )
1929 ssl->
out_msg[1] = (
unsigned char)( ( len - 4 ) >> 16 );
1930 ssl->
out_msg[2] = (
unsigned char)( ( len - 4 ) >> 8 );
1931 ssl->
out_msg[3] = (
unsigned char)( ( len - 4 ) );
1937 #if defined(POLARSSL_ZLIB_SUPPORT)
1941 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1951 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1952 if( ssl_hw_record_write != NULL)
1956 ret = ssl_hw_record_write( ssl );
1972 ssl->
out_hdr[3] = (
unsigned char)( len >> 8 );
1973 ssl->
out_hdr[4] = (
unsigned char)( len );
1977 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1984 ssl->
out_hdr[3] = (
unsigned char)( len >> 8 );
1985 ssl->
out_hdr[4] = (
unsigned char)( len );
1991 "version = [%d:%d], msglen = %d",
2034 " %d, type = %d, hslen = %d",
2069 "version = [%d:%d], msglen = %d",
2105 #if defined(POLARSSL_SSL_PROTO_SSL3)
2114 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2115 defined(POLARSSL_SSL_PROTO_TLS1_2)
2140 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2141 if( ssl_hw_record_read != NULL)
2145 ret = ssl_hw_record_read( ssl );
2158 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2160 #if defined(POLARSSL_SSL_ALERT_MESSAGES)
2182 #if defined(POLARSSL_ZLIB_SUPPORT)
2186 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2220 " %d, type = %d, hslen = %d",
2291 unsigned char level,
2292 unsigned char message )
2317 #if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2318 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2319 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2320 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2388 #if defined(POLARSSL_SSL_PROTO_SSL3)
2429 while( crt != NULL )
2439 ssl->
out_msg[i ] = (
unsigned char)( n >> 16 );
2440 ssl->
out_msg[i + 1] = (
unsigned char)( n >> 8 );
2441 ssl->
out_msg[i + 2] = (
unsigned char)( n );
2443 i += 3; memcpy( ssl->
out_msg + i, crt->
raw.
p, n );
2444 i += n; crt = crt->
next;
2447 ssl->
out_msg[4] = (
unsigned char)( ( i - 7 ) >> 16 );
2448 ssl->
out_msg[5] = (
unsigned char)( ( i - 7 ) >> 8 );
2449 ssl->
out_msg[6] = (
unsigned char)( ( i - 7 ) );
2455 #if defined(POLARSSL_SSL_PROTO_SSL3)
2506 #if defined(POLARSSL_SSL_PROTO_SSL3)
2529 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2530 defined(POLARSSL_SSL_PROTO_TLS1_2)
2537 memcmp( ssl->
in_msg + 4,
"\0\0\0", 3 ) == 0 )
2593 while( i < ssl->in_hslen )
2595 if( ssl->
in_msg[i] != 0 )
2601 n = ( (
unsigned int) ssl->
in_msg[i + 1] << 8 )
2602 | (
unsigned int) ssl->
in_msg[i + 2];
2605 if( n < 128 || i + n > ssl->
in_hslen )
2709 ((void) ciphersuite_info);
2711 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2712 defined(POLARSSL_SSL_PROTO_TLS1_1)
2717 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2718 #if defined(POLARSSL_SHA512_C)
2723 #if defined(POLARSSL_SHA256_C)
2733 static void ssl_update_checksum_start(
ssl_context *ssl,
2734 const unsigned char *buf,
size_t len )
2736 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2737 defined(POLARSSL_SSL_PROTO_TLS1_1)
2741 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2742 #if defined(POLARSSL_SHA256_C)
2745 #if defined(POLARSSL_SHA512_C)
2751 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2752 defined(POLARSSL_SSL_PROTO_TLS1_1)
2753 static void ssl_update_checksum_md5sha1(
ssl_context *ssl,
2754 const unsigned char *buf,
size_t len )
2761 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2762 #if defined(POLARSSL_SHA256_C)
2763 static void ssl_update_checksum_sha256(
ssl_context *ssl,
2764 const unsigned char *buf,
size_t len )
2770 #if defined(POLARSSL_SHA512_C)
2771 static void ssl_update_checksum_sha384(
ssl_context *ssl,
2772 const unsigned char *buf,
size_t len )
2779 #if defined(POLARSSL_SSL_PROTO_SSL3)
2780 static void ssl_calc_finished_ssl(
2787 unsigned char padbuf[48];
2788 unsigned char md5sum[16];
2789 unsigned char sha1sum[20];
2809 #if !defined(POLARSSL_MD5_ALT)
2814 #if !defined(POLARSSL_SHA1_ALT)
2822 memset( padbuf, 0x36, 48 );
2824 md5_update( &md5, (
const unsigned char *) sender, 4 );
2829 sha1_update( &sha1, (
const unsigned char *) sender, 4 );
2834 memset( padbuf, 0x5C, 48 );
2853 memset( padbuf, 0,
sizeof( padbuf ) );
2854 memset( md5sum, 0,
sizeof( md5sum ) );
2855 memset( sha1sum, 0,
sizeof( sha1sum ) );
2861 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
2862 static void ssl_calc_finished_tls(
2869 unsigned char padbuf[36];
2886 #if !defined(POLARSSL_MD5_ALT)
2891 #if !defined(POLARSSL_SHA1_ALT)
2898 :
"server finished";
2904 padbuf, 36, buf, len );
2911 memset( padbuf, 0,
sizeof( padbuf ) );
2917 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2918 #if defined(POLARSSL_SHA256_C)
2919 static void ssl_calc_finished_tls_sha256(
2925 unsigned char padbuf[32];
2941 #if !defined(POLARSSL_SHA256_ALT)
2948 :
"server finished";
2953 padbuf, 32, buf, len );
2959 memset( padbuf, 0,
sizeof( padbuf ) );
2965 #if defined(POLARSSL_SHA512_C)
2966 static void ssl_calc_finished_tls_sha384(
2972 unsigned char padbuf[48];
2988 #if !defined(POLARSSL_SHA512_ALT)
2989 SSL_DEBUG_BUF( 4,
"finished sha512 state", (
unsigned char *)
2995 :
"server finished";
3000 padbuf, 48, buf, len );
3006 memset( padbuf, 0,
sizeof( padbuf ) );
3110 SSL_DEBUG_MSG( 3, (
"switching to new transform spec for outbound data" ) );
3115 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3116 if( ssl_hw_record_activate != NULL)
3118 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3140 unsigned int hash_len;
3141 unsigned char buf[36];
3150 SSL_DEBUG_MSG( 3, (
"switching to new transform spec for inbound data" ) );
3153 memset( ssl->
in_ctr, 0, 8 );
3166 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3167 if( ssl_hw_record_activate != NULL)
3169 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3254 SSL_DEBUG_MSG( 1, (
"malloc() of ssl sub-contexts failed" ) );
3262 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3263 defined(POLARSSL_SSL_PROTO_TLS1_1)
3267 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
3268 #if defined(POLARSSL_SHA256_C)
3271 #if defined(POLARSSL_SHA512_C)
3279 #if defined(POLARSSL_ECDH_C)
3283 #if defined(POLARSSL_X509_CRT_PARSE_C)
3310 #if defined(POLARSSL_DHM_C)
3329 if( ssl->
in_ctr == NULL )
3350 #if defined(POLARSSL_SSL_SESSION_TICKETS)
3354 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3398 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3399 if( ssl_hw_record_reset != NULL)
3402 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
3424 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3430 #if defined(POLARSSL_SSL_SESSION_TICKETS)
3434 static int ssl_ticket_keys_init(
ssl_context *ssl )
3438 unsigned char buf[16];
3450 if( ( ret = ssl->
f_rng( ssl->
p_rng, buf, 16 ) ) != 0 ||
3473 #if defined(POLARSSL_SSL_SESSION_TICKETS)
3484 #if defined(POLARSSL_X509_CRT_PARSE_C)
3486 int (*f_vrfy)(
void *,
x509_crt *,
int,
int *),
3495 int (*f_rng)(
void *,
unsigned char *,
size_t),
3503 void (*f_dbg)(
void *,
int,
const char *),
3511 int (*f_recv)(
void *,
unsigned char *,
size_t),
void *p_recv,
3512 int (*f_send)(
void *,
const unsigned char *,
size_t),
void *p_send )
3521 int (*f_get_cache)(
void *,
ssl_session *),
void *p_get_cache,
3522 int (*f_set_cache)(
void *,
const ssl_session *),
void *p_set_cache )
3559 int major,
int minor )
3570 #if defined(POLARSSL_X509_CRT_PARSE_C)
3577 if( key_cert == NULL )
3591 while( last->
next != NULL )
3593 last->
next = key_cert;
3600 x509_crl *ca_crl,
const char *peer_cn )
3612 if( key_cert == NULL )
3615 key_cert->
cert = own_cert;
3616 key_cert->
key = pk_key;
3621 #if defined(POLARSSL_RSA_C)
3628 if( key_cert == NULL )
3632 if( key_cert->
key == NULL )
3644 key_cert->
cert = own_cert;
3660 if( key_cert == NULL )
3664 if( key_cert->
key == NULL )
3670 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3673 key_cert->
cert = own_cert;
3680 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
3682 const unsigned char *psk_identity,
size_t psk_identity_len )
3684 if( psk == NULL || psk_identity == NULL )
3687 if( ssl->
psk != NULL )
3709 int (*f_psk)(
void *,
ssl_context *,
const unsigned char *,
3718 #if defined(POLARSSL_DHM_C)
3758 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
3761 if( hostname == NULL )
3774 memcpy( ssl->
hostname, (
const unsigned char *) hostname,
3784 const unsigned char *,
size_t),
3812 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
3815 if( mfl_code >=
sizeof( mfl_code_to_length ) ||
3827 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
3849 #if defined(POLARSSL_SSL_SESSION_TICKETS)
3857 if( ssl->
f_rng == NULL )
3860 return( ssl_ticket_keys_init( ssl ) );
3884 if( ssl == NULL || ssl->
session == NULL )
3895 return(
"SSLv3.0" );
3898 return(
"TLSv1.0" );
3901 return(
"TLSv1.1" );
3904 return(
"TLSv1.2" );
3909 return(
"unknown" );
3912 #if defined(POLARSSL_X509_CRT_PARSE_C)
3915 if( ssl == NULL || ssl->
session == NULL )
3932 return( ssl_session_copy( dst, ssl->
session ) );
3942 #if defined(POLARSSL_SSL_CLI_C)
3947 #if defined(POLARSSL_SSL_SRV_C)
3977 #if defined(POLARSSL_SSL_SRV_C)
3981 static int ssl_write_hello_request(
ssl_context *ssl )
4014 static int ssl_start_renegotiation(
ssl_context *ssl )
4020 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4045 #if defined(POLARSSL_SSL_SRV_C)
4052 return( ssl_write_hello_request( ssl ) );
4056 #if defined(POLARSSL_SSL_CLI_C)
4066 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4139 SSL_DEBUG_MSG( 1, (
"handshake received (not HelloRequest)" ) );
4147 SSL_DEBUG_MSG( 3, (
"ignoring renegotiation, sending alert" ) );
4149 #if defined(POLARSSL_SSL_PROTO_SSL3)
4160 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4161 defined(POLARSSL_SSL_PROTO_TLS1_2)
4180 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4192 "but not honored by client" ) );
4207 memcpy( buf, ssl->
in_offt, n );
4242 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
4246 max_len = mfl_code_to_length[ssl->
mfl_code];
4258 n = ( len < max_len) ? len : max_len;
4272 memcpy( ssl->
out_msg, buf, n );
4318 #if defined(POLARSSL_ZLIB_SUPPORT)
4319 deflateEnd( &transform->ctx_deflate );
4320 inflateEnd( &transform->ctx_inflate );
4332 #if defined(POLARSSL_X509_CRT_PARSE_C)
4333 static void ssl_key_cert_free(
ssl_key_cert *key_cert )
4337 while( cur != NULL )
4355 #if defined(POLARSSL_DHM_C)
4358 #if defined(POLARSSL_ECDH_C)
4362 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
4367 #if defined(POLARSSL_X509_CRT_PARSE_C) && \
4368 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4377 while( cur != NULL )
4391 #if defined(POLARSSL_X509_CRT_PARSE_C)
4399 #if defined(POLARSSL_SSL_SESSION_TICKETS)
4419 if( ssl->
in_ctr != NULL )
4425 #if defined(POLARSSL_ZLIB_SUPPORT)
4426 if( ssl->compress_buf != NULL )
4433 #if defined(POLARSSL_DHM_C)
4461 #if defined(POLARSSL_SSL_SESSION_TICKETS)
4465 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4474 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
4475 if( ssl->
psk != NULL )
4486 #if defined(POLARSSL_X509_CRT_PARSE_C)
4487 ssl_key_cert_free( ssl->
key_cert );
4490 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4491 if( ssl_hw_record_finish != NULL )
4494 ssl_hw_record_finish( ssl );
4504 #if defined(POLARSSL_PK_C)
4510 #if defined(POLARSSL_RSA_C)
4514 #if defined(POLARSSL_ECDSA_C)
4525 #if defined(POLARSSL_RSA_C)
4529 #if defined(POLARSSL_ECDSA_C)
4546 #if defined(POLARSSL_MD5_C)
4550 #if defined(POLARSSL_SHA1_C)
4554 #if defined(POLARSSL_SHA256_C)
4560 #if defined(POLARSSL_SHA512_C)
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.
void(* f_dbg)(void *, int, const char *)
int(* f_rng)(void *, unsigned char *, size_t)
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
int cipher_finish(cipher_context_t *ctx, unsigned char *output, size_t *olen)
Generic cipher finalisation function.
#define SSL_DEBUG_RET(level, text, ret)
int rsa_copy(rsa_context *dst, const rsa_context *src)
Copy the components of an RSA context.
void *(* polarssl_malloc)(size_t len)
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 ssl_set_truncated_hmac(ssl_context *ssl, int truncate)
Activate negotiation of truncated HMAC (Client only) (Default: SSL_TRUNC_HMAC_ENABLED) ...
ssl_transform * transform_out
x509_buf raw
The raw certificate data (DER).
#define POLARSSL_ERR_SSL_CONN_EOF
The connection indicated an EOF.
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.
int cipher_write_tag(cipher_context_t *ctx, unsigned char *tag, size_t tag_len)
Write tag for AEAD ciphers.
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.
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
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.
#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 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.
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
#define SSL_DEBUG_MSG(level, args)
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_send)(void *, 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(* tls_prf)(const unsigned char *, size_t, const char *, const unsigned char *, size_t, unsigned char *, size_t)
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_ERR_SSL_NO_CLIENT_CERTIFICATE
No client certification received from the client, but required by the authentication mode...
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_ciphersuites_for_version(ssl_context *ssl, const int *ciphersuites, int major, int minor)
Set the list of allowed ciphersuites 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)
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
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.
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 POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH
Hardware acceleration function skipped / left alone data.
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...
unsigned char iv[POLARSSL_MAX_IV_LENGTH]
Current IV or NONCE_COUNTER for CTR-mode.
const cipher_info_t * cipher_info
Information about the associated cipher.
#define POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED
The own certificate is not set, but needed by the server.
#define SSL_RENEGOTIATION_PENDING
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
void(* update_checksum)(ssl_context *, const unsigned char *, size_t)
#define SSL_MINOR_VERSION_2
int ssl_write_certificate(ssl_context *ssl)
size_t(* rsa_key_len_func)(void *ctx)
#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.
#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.
int cipher_free_ctx(cipher_context_t *ctx)
Free the cipher-specific context of ctx.
int cipher_update_ad(cipher_context_t *ctx, const unsigned char *ad, size_t ad_len)
Add additional data (for AEAD ciphers).
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...
int cipher_set_iv(cipher_context_t *ctx, const unsigned char *iv, size_t iv_len)
Set the initialization vector (IV) or nonce.
#define SSL_MINOR_VERSION_0
#define SSL_MSG_CHANGE_CIPHER_SPEC
void sha256_starts(sha256_context *ctx, int is224)
SHA-256 context setup.
int cipher_update(cipher_context_t *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen)
Generic cipher update function.
ssl_key_cert * key_cert
Current key/cert or key/cert list.
void x509_crt_init(x509_crt *crt)
Initialize a certificate (chain)
static x509_crt * ssl_own_cert(ssl_context *ssl)
void(* polarssl_free)(void *ptr)
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 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 (Overrides all version specific lists)
void sha512_starts(sha512_context *ctx, int is384)
SHA-512 context setup.
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
int(* rsa_sign_func)(void *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, int hash_id, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
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 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
pk_type_t
Public key types.
#define POLARSSL_ERR_NET_WANT_READ
Connection requires a read call.
#define SSL_HS_CERTIFICATE
int ssl_parse_change_cipher_spec(ssl_context *ssl)
#define SSL_DEBUG_CRT(level, text, crt)
int cipher_reset(cipher_context_t *ctx)
Finish preparation of the given context.
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...
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.
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.
#define SSL_DEBUG_BUF(level, text, buf, len)
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 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
#define SSL_TRUNCATED_HMAC_LEN
void(* calc_finished)(ssl_context *, unsigned char *, int)
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.
#define pk_rsa(pk)
Quick access to an RSA context inside a PK context.
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...
int(* f_vrfy)(void *, x509_crt *, int, int *)
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.
#define POLARSSL_ERR_SSL_BAD_HS_FINISHED
Processing of the Finished handshake message failed.
#define SSL_DEBUG_MPI(level, text, X)
int mpi_copy(mpi *X, const mpi *Y)
Copy the contents of Y into X.
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)
Certificate revocation list structure.
const int * ssl_list_ciphersuites(void)
Returns the list of ciphersuites supported by the SSL/TLS module.
ssl_transform * transform_negotiate
#define SSL_LEGACY_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.
void dhm_free(dhm_context *ctx)
Free 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.
int ssl_write_change_cipher_spec(ssl_context *ssl)
int(* f_get_cache)(void *, ssl_session *)
int ssl_derive_keys(ssl_context *ssl)
void ssl_set_authmode(ssl_context *ssl, int authmode)
Set the certificate verification mode.
md_type_t type
Digest identifier.
int(* f_set_cache)(void *, const ssl_session *)
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)
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
int ssl_read(ssl_context *ssl, unsigned char *buf, size_t len)
Read at most 'len' application data bytes.
void ssl_transform_free(ssl_transform *transform)
Free referenced items in an SSL transform context and clear memory.
#define SSL_MAX_CONTENT_LEN
Size of the input / output buffer.
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.
int ssl_renegotiate(ssl_context *ssl)
Initiate an SSL renegotiation on the running connection.
int(* f_recv)(void *, unsigned char *, size_t)
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.
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 cipher_check_tag(cipher_context_t *ctx, const unsigned char *tag, size_t tag_len)
Check tag for AEAD ciphers.
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.
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).
unsigned int iv_size
IV/NONCE size, in bytes.
int ssl_fetch_input(ssl_context *ssl, size_t nb_want)
int(* f_psk)(void *, ssl_context *, const unsigned char *, size_t)
int ssl_write_record(ssl_context *ssl)
void ecdh_free(ecdh_context *ctx)
Free context.
unsigned char randbytes[64]
int md_hmac_finish(md_context_t *ctx, unsigned char *output)
Generic HMAC final digest.
int(* rsa_decrypt_func)(void *ctx, int mode, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
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