@@ -50,7 +50,7 @@ static const char * client_finished = "client finished";
50
50
static int do_handshake (SSL * ssl , uint8_t * buf , int read_len );
51
51
static int set_key_block (SSL * ssl , int is_write );
52
52
static int verify_digest (SSL * ssl , int mode , const uint8_t * buf , int read_len );
53
- static void * crypt_new (SSL * ssl , uint8_t * key , uint8_t * iv , int is_decrypt );
53
+ static void * crypt_new (SSL * ssl , uint8_t * key , uint8_t * iv , int is_decrypt , void * cached );
54
54
static int send_raw_packet (SSL * ssl , uint8_t protocol );
55
55
56
56
/**
@@ -591,6 +591,9 @@ SSL *ssl_new(SSL_CTX *ssl_ctx, int client_fd)
591
591
ssl_ctx -> tail = ssl ;
592
592
}
593
593
594
+ ssl -> encrypt_ctx = malloc (sizeof (AES_CTX ));
595
+ ssl -> decrypt_ctx = malloc (sizeof (AES_CTX ));
596
+
594
597
SSL_CTX_UNLOCK (ssl_ctx -> mutex );
595
598
return ssl ;
596
599
}
@@ -917,14 +920,18 @@ void finished_digest(SSL *ssl, const char *label, uint8_t *digest)
917
920
/**
918
921
* Retrieve (and initialise) the context of a cipher.
919
922
*/
920
- static void * crypt_new (SSL * ssl , uint8_t * key , uint8_t * iv , int is_decrypt )
923
+ static void * crypt_new (SSL * ssl , uint8_t * key , uint8_t * iv , int is_decrypt , void * cached )
921
924
{
922
925
switch (ssl -> cipher )
923
926
{
924
927
#ifndef CONFIG_SSL_SKELETON_MODE
925
928
case SSL_AES128_SHA :
926
929
{
927
- AES_CTX * aes_ctx = (AES_CTX * )malloc (sizeof (AES_CTX ));
930
+ AES_CTX * aes_ctx ;
931
+ if (cached )
932
+ aes_ctx = (AES_CTX * ) cached ;
933
+ else
934
+ aes_ctx = (AES_CTX * ) malloc (sizeof (AES_CTX ));
928
935
AES_set_key (aes_ctx , key , iv , AES_MODE_128 );
929
936
930
937
if (is_decrypt )
@@ -937,7 +944,12 @@ static void *crypt_new(SSL *ssl, uint8_t *key, uint8_t *iv, int is_decrypt)
937
944
938
945
case SSL_AES256_SHA :
939
946
{
940
- AES_CTX * aes_ctx = (AES_CTX * )malloc (sizeof (AES_CTX ));
947
+ AES_CTX * aes_ctx ;
948
+ if (cached )
949
+ aes_ctx = (AES_CTX * ) cached ;
950
+ else
951
+ aes_ctx = (AES_CTX * ) malloc (sizeof (AES_CTX ));
952
+
941
953
AES_set_key (aes_ctx , key , iv , AES_MODE_256 );
942
954
943
955
if (is_decrypt )
@@ -952,7 +964,12 @@ static void *crypt_new(SSL *ssl, uint8_t *key, uint8_t *iv, int is_decrypt)
952
964
#endif
953
965
case SSL_RC4_128_SHA :
954
966
{
955
- RC4_CTX * rc4_ctx = (RC4_CTX * )malloc (sizeof (RC4_CTX ));
967
+ RC4_CTX * rc4_ctx ;
968
+ if (cached )
969
+ rc4_ctx = (RC4_CTX * ) cached ;
970
+ else
971
+ rc4_ctx = (RC4_CTX * ) malloc (sizeof (RC4_CTX ));
972
+
956
973
RC4_setup (rc4_ctx , key , 16 );
957
974
return (void * )rc4_ctx ;
958
975
}
@@ -1184,26 +1201,26 @@ static int set_key_block(SSL *ssl, int is_write)
1184
1201
}
1185
1202
#endif
1186
1203
1187
- free (is_write ? ssl -> encrypt_ctx : ssl -> decrypt_ctx );
1204
+ // free(is_write ? ssl->encrypt_ctx : ssl->decrypt_ctx);
1188
1205
1189
1206
/* now initialise the ciphers */
1190
1207
if (is_client )
1191
1208
{
1192
1209
finished_digest (ssl , server_finished , ssl -> dc -> final_finish_mac );
1193
1210
1194
1211
if (is_write )
1195
- ssl -> encrypt_ctx = crypt_new (ssl , client_key , client_iv , 0 );
1212
+ ssl -> encrypt_ctx = crypt_new (ssl , client_key , client_iv , 0 , ssl -> encrypt_ctx );
1196
1213
else
1197
- ssl -> decrypt_ctx = crypt_new (ssl , server_key , server_iv , 1 );
1214
+ ssl -> decrypt_ctx = crypt_new (ssl , server_key , server_iv , 1 , ssl -> decrypt_ctx );
1198
1215
}
1199
1216
else
1200
1217
{
1201
1218
finished_digest (ssl , client_finished , ssl -> dc -> final_finish_mac );
1202
1219
1203
1220
if (is_write )
1204
- ssl -> encrypt_ctx = crypt_new (ssl , server_key , server_iv , 0 );
1221
+ ssl -> encrypt_ctx = crypt_new (ssl , server_key , server_iv , 0 , ssl -> encrypt_ctx );
1205
1222
else
1206
- ssl -> decrypt_ctx = crypt_new (ssl , client_key , client_iv , 1 );
1223
+ ssl -> decrypt_ctx = crypt_new (ssl , client_key , client_iv , 1 , ssl -> decrypt_ctx );
1207
1224
}
1208
1225
1209
1226
ssl -> cipher_info = ciph_info ;
0 commit comments