@@ -704,39 +704,27 @@ static void add_hmac_digest(SSL *ssl, int mode, uint8_t *hmac_header,
704
704
*/
705
705
static int verify_digest (SSL * ssl , int mode , const uint8_t * buf , int read_len )
706
706
{
707
- uint8_t hmac_buf [128 ];
707
+ uint8_t hmac_buf [SHA256_SIZE ]; // size of largest digest
708
708
int hmac_offset ;
709
709
710
- if (ssl -> cipher_info -> padding_size )
711
- {
712
- int last_blk_size = buf [read_len - 1 ], i ;
713
- hmac_offset = read_len - last_blk_size - ssl -> cipher_info -> digest_size - 1 ;
710
+ int last_blk_size = buf [read_len - 1 ], i ;
711
+ hmac_offset = read_len - last_blk_size - ssl -> cipher_info -> digest_size - 1 ;
714
712
715
- /* guard against a timing attack - make sure we do the digest */
716
- if (hmac_offset < 0 )
717
- {
718
- hmac_offset = 0 ;
719
- }
720
- else
721
- {
722
- /* already looked at last byte */
723
- for (i = 1 ; i < last_blk_size ; i ++ )
724
- {
725
- if (buf [read_len - i ] != last_blk_size )
726
- {
727
- hmac_offset = 0 ;
728
- break ;
729
- }
730
- }
731
- }
713
+ /* guard against a timing attack - make sure we do the digest */
714
+ if (hmac_offset < 0 )
715
+ {
716
+ hmac_offset = 0 ;
732
717
}
733
- else /* stream cipher */
718
+ else
734
719
{
735
- hmac_offset = read_len - ssl -> cipher_info -> digest_size ;
736
-
737
- if (hmac_offset < 0 )
720
+ /* already looked at last byte */
721
+ for (i = 1 ; i < last_blk_size ; i ++ )
738
722
{
739
- hmac_offset = 0 ;
723
+ if (buf [read_len - i ] != last_blk_size )
724
+ {
725
+ hmac_offset = 0 ;
726
+ break ;
727
+ }
740
728
}
741
729
}
742
730
@@ -759,7 +747,7 @@ static int verify_digest(SSL *ssl, int mode, const uint8_t *buf, int read_len)
759
747
*/
760
748
void add_packet (SSL * ssl , const uint8_t * pkt , int len )
761
749
{
762
- // TLS1.2
750
+ // TLS1.2+
763
751
if (ssl -> version >= SSL_PROTOCOL_VERSION_TLS1_2 || ssl -> version == 0 )
764
752
{
765
753
SHA256_Update (& ssl -> dc -> sha256_ctx , pkt , len );
@@ -780,7 +768,7 @@ void add_packet(SSL *ssl, const uint8_t *pkt, int len)
780
768
static void p_hash_md5 (const uint8_t * sec , int sec_len ,
781
769
uint8_t * seed , int seed_len , uint8_t * out , int olen )
782
770
{
783
- uint8_t a1 [128 ];
771
+ uint8_t a1 [MD5_SIZE + 77 ];
784
772
785
773
/* A(1) */
786
774
hmac_md5 (seed , seed_len , sec , sec_len , a1 );
@@ -808,7 +796,7 @@ static void p_hash_md5(const uint8_t *sec, int sec_len,
808
796
static void p_hash_sha1 (const uint8_t * sec , int sec_len ,
809
797
uint8_t * seed , int seed_len , uint8_t * out , int olen )
810
798
{
811
- uint8_t a1 [128 ];
799
+ uint8_t a1 [SHA1_SIZE + 77 ];
812
800
813
801
/* A(1) */
814
802
hmac_sha1 (seed , seed_len , sec , sec_len , a1 );
@@ -836,7 +824,7 @@ static void p_hash_sha1(const uint8_t *sec, int sec_len,
836
824
static void p_hash_sha256 (const uint8_t * sec , int sec_len ,
837
825
uint8_t * seed , int seed_len , uint8_t * out , int olen )
838
826
{
839
- uint8_t a1 [128 ];
827
+ uint8_t a1 [SHA256_SIZE + 77 ];
840
828
841
829
/* A(1) */
842
830
hmac_sha256 (seed , seed_len , sec , sec_len , a1 );
@@ -865,7 +853,7 @@ static void prf(SSL *ssl, const uint8_t *sec, int sec_len,
865
853
uint8_t * seed , int seed_len ,
866
854
uint8_t * out , int olen )
867
855
{
868
- if (ssl -> version >= SSL_PROTOCOL_VERSION_TLS1_2 ) // TLS1.2
856
+ if (ssl -> version >= SSL_PROTOCOL_VERSION_TLS1_2 ) // TLS1.2+
869
857
{
870
858
p_hash_sha256 (sec , sec_len , seed , seed_len , out , olen );
871
859
}
@@ -895,7 +883,7 @@ static void prf(SSL *ssl, const uint8_t *sec, int sec_len,
895
883
*/
896
884
void generate_master_secret (SSL * ssl , const uint8_t * premaster_secret )
897
885
{
898
- uint8_t buf [128 ];
886
+ uint8_t buf [77 ];
899
887
//print_blob("premaster secret", premaster_secret, 48);
900
888
strcpy ((char * )buf , "master secret" );
901
889
memcpy (& buf [13 ], ssl -> dc -> client_random , SSL_RANDOM_SIZE );
@@ -916,7 +904,7 @@ static void generate_key_block(SSL *ssl,
916
904
uint8_t * client_random , uint8_t * server_random ,
917
905
uint8_t * master_secret , uint8_t * key_block , int key_block_size )
918
906
{
919
- uint8_t buf [128 ];
907
+ uint8_t buf [77 ];
920
908
strcpy ((char * )buf , "key expansion" );
921
909
memcpy (& buf [13 ], server_random , SSL_RANDOM_SIZE );
922
910
memcpy (& buf [45 ], client_random , SSL_RANDOM_SIZE );
@@ -930,7 +918,7 @@ static void generate_key_block(SSL *ssl,
930
918
*/
931
919
int finished_digest (SSL * ssl , const char * label , uint8_t * digest )
932
920
{
933
- uint8_t mac_buf [128 ];
921
+ uint8_t mac_buf [SHA1_SIZE + MD5_SIZE + 15 ];
934
922
uint8_t * q = mac_buf ;
935
923
int dgst_len ;
936
924
@@ -940,7 +928,7 @@ int finished_digest(SSL *ssl, const char *label, uint8_t *digest)
940
928
q += strlen (label );
941
929
}
942
930
943
- if (ssl -> version >= SSL_PROTOCOL_VERSION_TLS1_2 ) // TLS1.2
931
+ if (ssl -> version >= SSL_PROTOCOL_VERSION_TLS1_2 ) // TLS1.2+
944
932
{
945
933
SHA256_CTX sha256_ctx = ssl -> dc -> sha256_ctx ; // interim copy
946
934
SHA256_Final (q , & sha256_ctx );
@@ -1140,8 +1128,7 @@ int send_packet(SSL *ssl, uint8_t protocol, const uint8_t *in, int length)
1140
1128
& ssl -> bm_data [msg_length ]);
1141
1129
msg_length += ssl -> cipher_info -> digest_size ;
1142
1130
1143
- /* add padding? */
1144
- if (ssl -> cipher_info -> padding_size )
1131
+ /* add padding */
1145
1132
{
1146
1133
int last_blk_size = msg_length %ssl -> cipher_info -> padding_size ;
1147
1134
int pad_bytes = ssl -> cipher_info -> padding_size - last_blk_size ;
@@ -1158,8 +1145,6 @@ int send_packet(SSL *ssl, uint8_t protocol, const uint8_t *in, int length)
1158
1145
increment_write_sequence (ssl );
1159
1146
1160
1147
/* add the explicit IV for TLS1.1 */
1161
- if (ssl -> version >= SSL_PROTOCOL_VERSION_TLS1_1 &&
1162
- ssl -> cipher_info -> iv_size )
1163
1148
{
1164
1149
uint8_t iv_size = ssl -> cipher_info -> iv_size ;
1165
1150
uint8_t * t_buf = malloc (msg_length + iv_size );
@@ -1373,13 +1358,8 @@ int basic_read(SSL *ssl, uint8_t **in_data)
1373
1358
if (IS_SET_SSL_FLAG (SSL_RX_ENCRYPTED ))
1374
1359
{
1375
1360
ssl -> cipher_info -> decrypt (ssl -> decrypt_ctx , buf , buf , read_len );
1376
-
1377
- if (ssl -> version >= SSL_PROTOCOL_VERSION_TLS1_1 &&
1378
- ssl -> cipher_info -> iv_size )
1379
- {
1380
- buf += ssl -> cipher_info -> iv_size ;
1381
- read_len -= ssl -> cipher_info -> iv_size ;
1382
- }
1361
+ buf += ssl -> cipher_info -> iv_size ;
1362
+ read_len -= ssl -> cipher_info -> iv_size ;
1383
1363
1384
1364
read_len = verify_digest (ssl ,
1385
1365
is_client ? SSL_CLIENT_READ : SSL_SERVER_READ , buf , read_len );
@@ -1564,7 +1544,7 @@ int send_change_cipher_spec(SSL *ssl)
1564
1544
*/
1565
1545
int send_finished (SSL * ssl )
1566
1546
{
1567
- uint8_t buf [128 ] = {
1547
+ uint8_t buf [SHA1_SIZE + MD5_SIZE + 15 + 4 ] = {
1568
1548
HS_FINISHED , 0 , 0 , SSL_FINISHED_HASH_SIZE };
1569
1549
1570
1550
/* now add the finished digest mac (12 bytes) */
@@ -1725,9 +1705,11 @@ int send_certificate(SSL *ssl)
1725
1705
buf [1 ] = 0 ;
1726
1706
buf [4 ] = 0 ;
1727
1707
1708
+ /* spec says we must check if the hash/sig algorithm is OK */
1728
1709
if (ssl -> version >= SSL_PROTOCOL_VERSION_TLS1_2 &&
1729
1710
((ret = check_certificate_chain (ssl )) != SSL_OK ))
1730
1711
{
1712
+ ret = SSL_ERROR_INVALID_CERT_HASH_ALG ;
1731
1713
goto error ;
1732
1714
}
1733
1715
0 commit comments