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
51 #define strcasecmp _stricmp
54 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
78 #if defined(POLARSSL_X509_CRT_PARSE_C)
99 #if defined(POLARSSL_SSL_SESSION_TICKETS)
113 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
115 const unsigned char *key_enc,
const unsigned char *key_dec,
117 const unsigned char *iv_enc,
const unsigned char *iv_dec,
119 const unsigned char *mac_enc,
const unsigned char *mac_dec,
120 size_t maclen) = NULL;
121 int (*ssl_hw_record_activate)(
ssl_context *ssl,
int direction) = NULL;
122 int (*ssl_hw_record_reset)(
ssl_context *ssl) = NULL;
123 int (*ssl_hw_record_write)(
ssl_context *ssl) = NULL;
124 int (*ssl_hw_record_read)(
ssl_context *ssl) = NULL;
125 int (*ssl_hw_record_finish)(
ssl_context *ssl) = NULL;
131 #if defined(POLARSSL_SSL_PROTO_SSL3)
132 static int ssl3_prf(
const unsigned char *secret,
size_t slen,
134 const unsigned char *random,
size_t rlen,
135 unsigned char *dstbuf,
size_t dlen )
140 unsigned char padding[16];
141 unsigned char sha1sum[20];
152 for( i = 0; i < dlen / 16; i++ )
154 memset( padding, (
unsigned char) (
'A' + i), 1 + i );
168 memset( &md5, 0,
sizeof( md5 ) );
169 memset( &sha1, 0,
sizeof( sha1 ) );
171 memset( padding, 0,
sizeof( padding ) );
172 memset( sha1sum, 0,
sizeof( sha1sum ) );
178 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
179 static int tls1_prf(
const unsigned char *secret,
size_t slen,
181 const unsigned char *random,
size_t rlen,
182 unsigned char *dstbuf,
size_t dlen )
186 const unsigned char *S1, *S2;
187 unsigned char tmp[128];
188 unsigned char h_i[20];
190 if(
sizeof( tmp ) < 20 + strlen( label ) + rlen )
193 hs = ( slen + 1 ) / 2;
195 S2 = secret + slen - hs;
197 nb = strlen( label );
198 memcpy( tmp + 20, label, nb );
199 memcpy( tmp + 20 + nb, random, rlen );
205 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
207 for( i = 0; i < dlen; i += 16 )
209 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
210 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
212 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
214 for( j = 0; j < k; j++ )
215 dstbuf[i + j] = h_i[j];
223 for( i = 0; i < dlen; i += 20 )
228 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
230 for( j = 0; j < k; j++ )
231 dstbuf[i + j] = (
unsigned char)( dstbuf[i + j] ^ h_i[j] );
234 memset( tmp, 0,
sizeof( tmp ) );
235 memset( h_i, 0,
sizeof( h_i ) );
241 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
242 #if defined(POLARSSL_SHA256_C)
243 static int tls_prf_sha256(
const unsigned char *secret,
size_t slen,
245 const unsigned char *random,
size_t rlen,
246 unsigned char *dstbuf,
size_t dlen )
250 unsigned char tmp[128];
251 unsigned char h_i[32];
253 if(
sizeof( tmp ) < 32 + strlen( label ) + rlen )
256 nb = strlen( label );
257 memcpy( tmp + 32, label, nb );
258 memcpy( tmp + 32 + nb, random, rlen );
266 for( i = 0; i < dlen; i += 32 )
271 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
273 for( j = 0; j < k; j++ )
274 dstbuf[i + j] = h_i[j];
277 memset( tmp, 0,
sizeof( tmp ) );
278 memset( h_i, 0,
sizeof( h_i ) );
284 #if defined(POLARSSL_SHA512_C)
285 static int tls_prf_sha384(
const unsigned char *secret,
size_t slen,
287 const unsigned char *random,
size_t rlen,
288 unsigned char *dstbuf,
size_t dlen )
292 unsigned char tmp[128];
293 unsigned char h_i[48];
295 if(
sizeof( tmp ) < 48 + strlen( label ) + rlen )
298 nb = strlen( label );
299 memcpy( tmp + 48, label, nb );
300 memcpy( tmp + 48 + nb, random, rlen );
308 for( i = 0; i < dlen; i += 48 )
313 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
315 for( j = 0; j < k; j++ )
316 dstbuf[i + j] = h_i[j];
319 memset( tmp, 0,
sizeof( tmp ) );
320 memset( h_i, 0,
sizeof( h_i ) );
327 static void ssl_update_checksum_start(
ssl_context *,
const unsigned char *,
size_t);
329 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
330 defined(POLARSSL_SSL_PROTO_TLS1_1)
331 static void ssl_update_checksum_md5sha1(
ssl_context *,
const unsigned char *,
size_t);
334 #if defined(POLARSSL_SSL_PROTO_SSL3)
335 static void ssl_calc_verify_ssl(
ssl_context *,
unsigned char *);
336 static void ssl_calc_finished_ssl(
ssl_context *,
unsigned char *,
int);
339 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
340 static void ssl_calc_verify_tls(
ssl_context *,
unsigned char *);
341 static void ssl_calc_finished_tls(
ssl_context *,
unsigned char *,
int);
344 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
345 #if defined(POLARSSL_SHA256_C)
346 static void ssl_update_checksum_sha256(
ssl_context *,
const unsigned char *,
size_t);
347 static void ssl_calc_verify_tls_sha256(
ssl_context *,
unsigned char *);
348 static void ssl_calc_finished_tls_sha256(
ssl_context *,
unsigned char *,
int);
351 #if defined(POLARSSL_SHA512_C)
352 static void ssl_update_checksum_sha384(
ssl_context *,
const unsigned char *,
size_t);
353 static void ssl_calc_verify_tls_sha384(
ssl_context *,
unsigned char *);
354 static void ssl_calc_finished_tls_sha384(
ssl_context *,
unsigned char *,
int);
361 unsigned char tmp[64];
362 unsigned char keyblk[256];
365 unsigned char *mac_enc;
366 unsigned char *mac_dec;
378 if( cipher_info == NULL )
386 if( md_info == NULL )
396 #if defined(POLARSSL_SSL_PROTO_SSL3)
405 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
414 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
415 #if defined(POLARSSL_SHA512_C)
419 handshake->
tls_prf = tls_prf_sha384;
420 handshake->
calc_verify = ssl_calc_verify_tls_sha384;
425 #if defined(POLARSSL_SHA256_C)
428 handshake->
tls_prf = tls_prf_sha256;
429 handshake->
calc_verify = ssl_calc_verify_tls_sha256;
450 if( handshake->
resume == 0 )
468 memcpy( handshake->
randbytes, tmp + 32, 32 );
469 memcpy( handshake->
randbytes + 32, tmp, 32 );
470 memset( tmp, 0,
sizeof( tmp ) );
504 transform->
ivlen = 12;
528 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
553 SSL_DEBUG_MSG( 3, (
"keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
562 key1 = keyblk + transform->
maclen * 2;
563 key2 = keyblk + transform->
maclen * 2 + transform->
keylen;
566 mac_dec = keyblk + transform->
maclen;
573 memcpy( transform->
iv_enc, key2 + transform->
keylen, iv_copy_len );
574 memcpy( transform->
iv_dec, key2 + transform->
keylen + iv_copy_len,
579 key1 = keyblk + transform->
maclen * 2 + transform->
keylen;
580 key2 = keyblk + transform->
maclen * 2;
582 mac_enc = keyblk + transform->
maclen;
590 memcpy( transform->
iv_dec, key1 + transform->
keylen, iv_copy_len );
591 memcpy( transform->
iv_enc, key1 + transform->
keylen + iv_copy_len,
595 #if defined(POLARSSL_SSL_PROTO_SSL3)
603 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
604 defined(POLARSSL_SSL_PROTO_TLS1_2)
617 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
618 if( ssl_hw_record_init != NULL)
624 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->
keylen,
628 transform->
maclen ) ) != 0 )
636 switch( cipher_info->
type )
648 cipher_info ) ) != 0 )
655 cipher_info ) ) != 0 )
677 #if defined(POLARSSL_CIPHER_MODE_CBC)
704 memset( keyblk, 0,
sizeof( keyblk ) );
706 #if defined(POLARSSL_ZLIB_SUPPORT)
711 if( ssl->compress_buf == NULL )
715 if( ssl->compress_buf == NULL )
725 memset( &transform->ctx_deflate, 0,
sizeof( transform->ctx_deflate ) );
726 memset( &transform->ctx_inflate, 0,
sizeof( transform->ctx_inflate ) );
728 if( deflateInit( &transform->ctx_deflate, Z_DEFAULT_COMPRESSION ) != Z_OK ||
729 inflateInit( &transform->ctx_inflate ) != Z_OK )
742 #if defined(POLARSSL_SSL_PROTO_SSL3)
743 void ssl_calc_verify_ssl(
ssl_context *ssl,
unsigned char hash[36] )
747 unsigned char pad_1[48];
748 unsigned char pad_2[48];
755 memset( pad_1, 0x36, 48 );
756 memset( pad_2, 0x5C, 48 );
785 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
786 void ssl_calc_verify_tls(
ssl_context *ssl,
unsigned char hash[36] )
806 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
807 #if defined(POLARSSL_SHA256_C)
808 void ssl_calc_verify_tls_sha256(
ssl_context *ssl,
unsigned char hash[32] )
824 #if defined(POLARSSL_SHA512_C)
825 void ssl_calc_verify_tls_sha384(
ssl_context *ssl,
unsigned char hash[48] )
842 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
855 #if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
858 if( end - p < 2 + (
int) ssl->
psk_len )
861 *(p++) = (
unsigned char)( ssl->
psk_len >> 8 );
862 *(p++) = (
unsigned char)( ssl->
psk_len );
867 #if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
880 #if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
886 if( end - p < 2 + (
int) len )
889 *(p++) = (
unsigned char)( len >> 8 );
890 *(p++) = (
unsigned char)( len );
903 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
910 p + 2, end - (p + 2),
917 *(p++) = (
unsigned char)( zlen >> 8 );
918 *(p++) = (
unsigned char)( zlen );
931 *(p++) = (
unsigned char)( ssl->
psk_len >> 8 );
932 *(p++) = (
unsigned char)( ssl->
psk_len );
942 #if defined(POLARSSL_SSL_PROTO_SSL3)
946 static void ssl_mac(
md_context_t *md_ctx,
unsigned char *secret,
947 unsigned char *buf,
size_t len,
948 unsigned char *ctr,
int type )
950 unsigned char header[11];
951 unsigned char padding[48];
963 memcpy( header, ctr, 8 );
964 header[ 8] = (
unsigned char) type;
965 header[ 9] = (
unsigned char)( len >> 8 );
966 header[10] = (
unsigned char)( len );
968 memset( padding, 0x36, padlen );
976 memset( padding, 0x5C, padlen );
997 #if defined(POLARSSL_SSL_PROTO_SSL3)
1007 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1008 defined(POLARSSL_SSL_PROTO_TLS1_2)
1030 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
1037 #if defined(POLARSSL_ARC4_C)
1044 "including %d bytes of padding",
1074 SSL_DEBUG_MSG( 1, (
"total encrypted length incorrect %d %d",
1081 ssl->
out_msg + olen, &olen ) ) != 0 )
1089 SSL_DEBUG_MSG( 1, (
"total encrypted length incorrect %d %d",
1097 #if defined(POLARSSL_GCM_C)
1101 size_t enc_msglen, olen, totlen;
1102 unsigned char *enc_msg;
1103 unsigned char add_data[13];
1108 memcpy( add_data, ssl->
out_ctr, 8 );
1112 add_data[11] = ( ssl->
out_msglen >> 8 ) & 0xFF;
1143 "including %d bytes of padding",
1161 add_data, 13 ) ) != 0 )
1167 enc_msg, enc_msglen,
1168 enc_msg, &olen ) ) != 0 )
1175 enc_msg + olen, &olen ) ) != 0 )
1181 if( totlen != enc_msglen )
1193 enc_msg + enc_msglen, 16 ) ) != 0 )
1198 SSL_DEBUG_BUF( 4,
"after encrypt: tag", enc_msg + enc_msglen, 16 );
1202 #if defined(POLARSSL_CIPHER_MODE_CBC)
1207 unsigned char *enc_msg;
1208 size_t enc_msglen, padlen, olen = 0;
1215 for( i = 0; i <= padlen; i++ )
1223 #if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
1251 "including %d bytes of IV and %d bytes of padding",
1272 enc_msg, enc_msglen, enc_msg,
1282 enc_msg + olen, &olen ) ) != 0 )
1288 if( enc_msglen != olen )
1290 SSL_DEBUG_MSG( 1, (
"total encrypted length incorrect %d %d",
1291 enc_msglen, olen ) );
1296 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
1315 for( i = 8; i > 0; i-- )
1316 if( ++ssl->
out_ctr[i - 1] != 0 )
1324 #define POLARSSL_SSL_MAX_MAC_SIZE 48
1328 size_t i, padlen = 0, correct = 1;
1329 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1340 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
1347 #if defined(POLARSSL_ARC4_C)
1385 ssl->
in_msg + olen, &olen ) ) != 0 )
1400 #if defined(POLARSSL_GCM_C)
1404 unsigned char *dec_msg;
1405 unsigned char *dec_msg_result;
1406 size_t dec_msglen, olen, totlen;
1407 unsigned char add_data[13];
1416 dec_msg_result = ssl->
in_msg;
1419 memcpy( add_data, ssl->
in_ctr, 8 );
1423 add_data[11] = ( ssl->
in_msglen >> 8 ) & 0xFF;
1449 add_data, 13 ) ) != 0 )
1455 dec_msg, dec_msglen,
1456 dec_msg_result, &olen ) ) != 0 )
1463 dec_msg_result + olen, &olen ) ) != 0 )
1469 if( totlen != dec_msglen )
1479 dec_msg + dec_msglen, 16 ) ) != 0 )
1488 #if defined(POLARSSL_CIPHER_MODE_CBC)
1496 unsigned char *dec_msg;
1497 unsigned char *dec_msg_result;
1512 #if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
1520 SSL_DEBUG_MSG( 1, (
"msglen (%d) < max( ivlen(%d), maclen (%d) + 1 ) ( + expl IV )",
1527 dec_msg_result = ssl->
in_msg;
1529 #if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
1558 dec_msg, dec_msglen, dec_msg_result,
1567 dec_msg_result + olen, &olen ) ) != 0 )
1573 if( dec_msglen != olen )
1580 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
1596 #if defined(POLARSSL_SSL_DEBUG_ALL)
1597 SSL_DEBUG_MSG( 1, (
"msglen (%d) < maclen (%d) + padlen (%d)",
1604 #if defined(POLARSSL_SSL_PROTO_SSL3)
1609 #if defined(POLARSSL_SSL_DEBUG_ALL)
1611 "should be no more than %d",
1619 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1620 defined(POLARSSL_SSL_PROTO_TLS1_2)
1627 size_t pad_count = 0, real_count = 1;
1628 size_t padding_idx = ssl->
in_msglen - padlen - 1;
1630 for( i = 1; i <= 256; i++ )
1632 real_count &= ( i <= padlen );
1633 pad_count += real_count *
1634 ( ssl->
in_msg[padding_idx + i] == padlen - 1 );
1637 correct &= ( pad_count == padlen );
1639 #if defined(POLARSSL_SSL_DEBUG_ALL)
1640 if( padlen > 0 && correct == 0)
1643 padlen &= correct * 0x1FF;
1673 #if defined(POLARSSL_SSL_PROTO_SSL3)
1683 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1684 defined(POLARSSL_SSL_PROTO_TLS1_2)
1700 size_t j, extra_run = 0;
1701 extra_run = ( 13 + ssl->
in_msglen + padlen + 8 ) / 64 -
1704 extra_run &= correct * 0xFF;
1711 for( j = 0; j < extra_run; j++ )
1731 #if defined(POLARSSL_SSL_DEBUG_ALL)
1754 "messages, possible DoS attack" ) );
1761 for( i = 8; i > 0; i-- )
1762 if( ++ssl->
in_ctr[i - 1] != 0 )
1770 #if defined(POLARSSL_ZLIB_SUPPORT)
1777 unsigned char *msg_post = ssl->
out_msg;
1779 unsigned char *msg_pre = ssl->compress_buf;
1786 memcpy( msg_pre, ssl->
out_msg, len_pre );
1799 ret = deflate( &ssl->
transform_out->ctx_deflate, Z_SYNC_FLUSH );
1802 SSL_DEBUG_MSG( 1, (
"failed to perform compression (%d)", ret ) );
1822 unsigned char *msg_post = ssl->
in_msg;
1824 unsigned char *msg_pre = ssl->compress_buf;
1831 memcpy( msg_pre, ssl->
in_msg, len_pre );
1844 ret = inflate( &ssl->
transform_in->ctx_inflate, Z_SYNC_FLUSH );
1847 SSL_DEBUG_MSG( 1, (
"failed to perform decompression (%d)", ret ) );
1875 while( ssl->
in_left < nb_want )
1941 ssl->
out_msg[1] = (
unsigned char)( ( len - 4 ) >> 16 );
1942 ssl->
out_msg[2] = (
unsigned char)( ( len - 4 ) >> 8 );
1943 ssl->
out_msg[3] = (
unsigned char)( ( len - 4 ) );
1948 #if defined(POLARSSL_ZLIB_SUPPORT)
1952 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1962 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1963 if( ssl_hw_record_write != NULL)
1967 ret = ssl_hw_record_write( ssl );
1983 ssl->
out_hdr[3] = (
unsigned char)( len >> 8 );
1984 ssl->
out_hdr[4] = (
unsigned char)( len );
1988 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1995 ssl->
out_hdr[3] = (
unsigned char)( len >> 8 );
1996 ssl->
out_hdr[4] = (
unsigned char)( len );
2002 "version = [%d:%d], msglen = %d",
2045 " %d, type = %d, hslen = %d",
2080 "version = [%d:%d], msglen = %d",
2116 #if defined(POLARSSL_SSL_PROTO_SSL3)
2125 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2126 defined(POLARSSL_SSL_PROTO_TLS1_2)
2151 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2152 if( ssl_hw_record_read != NULL)
2156 ret = ssl_hw_record_read( ssl );
2169 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2171 #if defined(POLARSSL_SSL_ALERT_MESSAGES)
2193 #if defined(POLARSSL_ZLIB_SUPPORT)
2197 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2231 " %d, type = %d, hslen = %d",
2302 unsigned char level,
2303 unsigned char message )
2328 #if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2329 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2330 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2331 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2399 #if defined(POLARSSL_SSL_PROTO_SSL3)
2440 while( crt != NULL )
2450 ssl->
out_msg[i ] = (
unsigned char)( n >> 16 );
2451 ssl->
out_msg[i + 1] = (
unsigned char)( n >> 8 );
2452 ssl->
out_msg[i + 2] = (
unsigned char)( n );
2454 i += 3; memcpy( ssl->
out_msg + i, crt->
raw.
p, n );
2455 i += n; crt = crt->
next;
2458 ssl->
out_msg[4] = (
unsigned char)( ( i - 7 ) >> 16 );
2459 ssl->
out_msg[5] = (
unsigned char)( ( i - 7 ) >> 8 );
2460 ssl->
out_msg[6] = (
unsigned char)( ( i - 7 ) );
2466 #if defined(POLARSSL_SSL_PROTO_SSL3)
2517 #if defined(POLARSSL_SSL_PROTO_SSL3)
2540 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2541 defined(POLARSSL_SSL_PROTO_TLS1_2)
2548 memcmp( ssl->
in_msg + 4,
"\0\0\0", 3 ) == 0 )
2604 while( i < ssl->in_hslen )
2606 if( ssl->
in_msg[i] != 0 )
2612 n = ( (
unsigned int) ssl->
in_msg[i + 1] << 8 )
2613 | (
unsigned int) ssl->
in_msg[i + 2];
2616 if( n < 128 || i + n > ssl->
in_hslen )
2720 ((void) ciphersuite_info);
2722 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2723 defined(POLARSSL_SSL_PROTO_TLS1_1)
2728 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2729 #if defined(POLARSSL_SHA512_C)
2734 #if defined(POLARSSL_SHA256_C)
2744 static void ssl_update_checksum_start(
ssl_context *ssl,
2745 const unsigned char *buf,
size_t len )
2747 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2748 defined(POLARSSL_SSL_PROTO_TLS1_1)
2752 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2753 #if defined(POLARSSL_SHA256_C)
2756 #if defined(POLARSSL_SHA512_C)
2762 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2763 defined(POLARSSL_SSL_PROTO_TLS1_1)
2764 static void ssl_update_checksum_md5sha1(
ssl_context *ssl,
2765 const unsigned char *buf,
size_t len )
2772 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2773 #if defined(POLARSSL_SHA256_C)
2774 static void ssl_update_checksum_sha256(
ssl_context *ssl,
2775 const unsigned char *buf,
size_t len )
2781 #if defined(POLARSSL_SHA512_C)
2782 static void ssl_update_checksum_sha384(
ssl_context *ssl,
2783 const unsigned char *buf,
size_t len )
2790 #if defined(POLARSSL_SSL_PROTO_SSL3)
2791 static void ssl_calc_finished_ssl(
2798 unsigned char padbuf[48];
2799 unsigned char md5sum[16];
2800 unsigned char sha1sum[20];
2820 #if !defined(POLARSSL_MD5_ALT)
2825 #if !defined(POLARSSL_SHA1_ALT)
2833 memset( padbuf, 0x36, 48 );
2835 md5_update( &md5, (
const unsigned char *) sender, 4 );
2840 sha1_update( &sha1, (
const unsigned char *) sender, 4 );
2845 memset( padbuf, 0x5C, 48 );
2864 memset( padbuf, 0,
sizeof( padbuf ) );
2865 memset( md5sum, 0,
sizeof( md5sum ) );
2866 memset( sha1sum, 0,
sizeof( sha1sum ) );
2872 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
2873 static void ssl_calc_finished_tls(
2880 unsigned char padbuf[36];
2897 #if !defined(POLARSSL_MD5_ALT)
2902 #if !defined(POLARSSL_SHA1_ALT)
2909 :
"server finished";
2915 padbuf, 36, buf, len );
2922 memset( padbuf, 0,
sizeof( padbuf ) );
2928 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2929 #if defined(POLARSSL_SHA256_C)
2930 static void ssl_calc_finished_tls_sha256(
2936 unsigned char padbuf[32];
2952 #if !defined(POLARSSL_SHA256_ALT)
2959 :
"server finished";
2964 padbuf, 32, buf, len );
2970 memset( padbuf, 0,
sizeof( padbuf ) );
2976 #if defined(POLARSSL_SHA512_C)
2977 static void ssl_calc_finished_tls_sha384(
2983 unsigned char padbuf[48];
2999 #if !defined(POLARSSL_SHA512_ALT)
3000 SSL_DEBUG_BUF( 4,
"finished sha512 state", (
unsigned char *)
3006 :
"server finished";
3011 padbuf, 48, buf, len );
3017 memset( padbuf, 0,
sizeof( padbuf ) );
3118 SSL_DEBUG_MSG( 3, (
"switching to new transform spec for outbound data" ) );
3123 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3124 if( ssl_hw_record_activate != NULL)
3126 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3148 unsigned int hash_len;
3149 unsigned char buf[36];
3158 SSL_DEBUG_MSG( 3, (
"switching to new transform spec for inbound data" ) );
3161 memset( ssl->
in_ctr, 0, 8 );
3174 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3175 if( ssl_hw_record_activate != NULL)
3177 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3207 if( memcmp( ssl->
in_msg + 4, buf, hash_len ) != 0 )
3262 SSL_DEBUG_MSG( 1, (
"malloc() of ssl sub-contexts failed" ) );
3270 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3271 defined(POLARSSL_SSL_PROTO_TLS1_1)
3275 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
3276 #if defined(POLARSSL_SHA256_C)
3279 #if defined(POLARSSL_SHA512_C)
3287 #if defined(POLARSSL_ECDH_C)
3314 #if defined(POLARSSL_DHM_C)
3333 if( ssl->
in_ctr == NULL )
3354 #if defined(POLARSSL_SSL_SESSION_TICKETS)
3358 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3402 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3403 if( ssl_hw_record_reset != NULL)
3406 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
3428 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3434 #if defined(POLARSSL_SSL_SESSION_TICKETS)
3438 static int ssl_ticket_keys_init(
ssl_context *ssl )
3442 unsigned char buf[16];
3454 if( ( ret = ssl->
f_rng( ssl->
p_rng, buf, 16 ) ) != 0 ||
3477 #if defined(POLARSSL_SSL_SESSION_TICKETS)
3488 #if defined(POLARSSL_X509_CRT_PARSE_C)
3490 int (*f_vrfy)(
void *,
x509_crt *,
int,
int *),
3499 int (*f_rng)(
void *,
unsigned char *,
size_t),
3507 void (*f_dbg)(
void *,
int,
const char *),
3515 int (*f_recv)(
void *,
unsigned char *,
size_t),
void *p_recv,
3516 int (*f_send)(
void *,
const unsigned char *,
size_t),
void *p_send )
3525 int (*f_get_cache)(
void *,
ssl_session *),
void *p_get_cache,
3526 int (*f_set_cache)(
void *,
const ssl_session *),
void *p_set_cache )
3563 int major,
int minor )
3574 #if defined(POLARSSL_X509_CRT_PARSE_C)
3581 if( key_cert == NULL )
3592 while( last->
next != NULL )
3594 last->
next = key_cert;
3601 x509_crl *ca_crl,
const char *peer_cn )
3613 if( key_cert == NULL )
3616 key_cert->
cert = own_cert;
3617 key_cert->
key = pk_key;
3622 #if defined(POLARSSL_RSA_C)
3629 if( key_cert == NULL )
3633 if( key_cert->
key == NULL )
3645 key_cert->
cert = own_cert;
3661 if( key_cert == NULL )
3665 if( key_cert->
key == NULL )
3671 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3674 key_cert->
cert = own_cert;
3681 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
3683 const unsigned char *psk_identity,
size_t psk_identity_len )
3685 if( psk == NULL || psk_identity == NULL )
3688 if( ssl->
psk != NULL )
3710 int (*f_psk)(
void *,
ssl_context *,
const unsigned char *,
3719 #if defined(POLARSSL_DHM_C)
3759 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
3762 if( hostname == NULL )
3775 memcpy( ssl->
hostname, (
const unsigned char *) hostname,
3785 const unsigned char *,
size_t),
3813 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
3816 if( mfl_code >=
sizeof( mfl_code_to_length ) ||
3828 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
3850 #if defined(POLARSSL_SSL_SESSION_TICKETS)
3858 if( ssl->
f_rng == NULL )
3861 return( ssl_ticket_keys_init( ssl ) );
3885 if( ssl == NULL || ssl->
session == NULL )
3896 return(
"SSLv3.0" );
3899 return(
"TLSv1.0" );
3902 return(
"TLSv1.1" );
3905 return(
"TLSv1.2" );
3910 return(
"unknown" );
3913 #if defined(POLARSSL_X509_CRT_PARSE_C)
3916 if( ssl == NULL || ssl->
session == NULL )
3933 return( ssl_session_copy( dst, ssl->
session ) );
3943 #if defined(POLARSSL_SSL_CLI_C)
3948 #if defined(POLARSSL_SSL_SRV_C)
3965 #if defined(POLARSSL_X509_CRT_PARSE_C)
3997 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4065 SSL_DEBUG_MSG( 1, (
"handshake received (not HelloRequest)" ) );
4073 SSL_DEBUG_MSG( 3, (
"ignoring renegotiation, sending alert" ) );
4075 #if defined(POLARSSL_SSL_PROTO_SSL3)
4086 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4087 defined(POLARSSL_SSL_PROTO_TLS1_2)
4127 memcpy( buf, ssl->
in_offt, n );
4162 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
4166 max_len = mfl_code_to_length[ssl->
mfl_code];
4178 n = ( len < max_len) ? len : max_len;
4192 memcpy( ssl->
out_msg, buf, n );
4238 #if defined(POLARSSL_ZLIB_SUPPORT)
4239 deflateEnd( &transform->ctx_deflate );
4240 inflateEnd( &transform->ctx_inflate );
4252 #if defined(POLARSSL_X509_CRT_PARSE_C)
4253 static void ssl_key_cert_free(
ssl_key_cert *key_cert )
4257 while( cur != NULL )
4275 #if defined(POLARSSL_DHM_C)
4278 #if defined(POLARSSL_ECDH_C)
4282 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
4287 #if defined(POLARSSL_X509_CRT_PARSE_C) && \
4288 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4297 while( cur != NULL )
4311 #if defined(POLARSSL_X509_CRT_PARSE_C)
4319 #if defined(POLARSSL_SSL_SESSION_TICKETS)
4339 if( ssl->
in_ctr != NULL )
4345 #if defined(POLARSSL_ZLIB_SUPPORT)
4346 if( ssl->compress_buf != NULL )
4353 #if defined(POLARSSL_DHM_C)
4381 #if defined(POLARSSL_SSL_SESSION_TICKETS)
4385 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4394 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
4395 if( ssl->
psk != NULL )
4406 #if defined(POLARSSL_X509_CRT_PARSE_C)
4407 ssl_key_cert_free( ssl->
key_cert );
4410 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4411 if( ssl_hw_record_finish != NULL )
4414 ssl_hw_record_finish( ssl );
4424 #if defined(POLARSSL_PK_C)
4430 #if defined(POLARSSL_RSA_C)
4434 #if defined(POLARSSL_ECDSA_C)
4445 #if defined(POLARSSL_RSA_C)
4449 #if defined(POLARSSL_ECDSA_C)
4466 #if defined(POLARSSL_MD5_C)
4470 #if defined(POLARSSL_SHA1_C)
4474 #if defined(POLARSSL_SHA256_C)
4480 #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) ...
cipher_type_t type
Full cipher identifier (e.g.
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 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.
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.
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)
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)
Perform 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