Skip to content

Commit c422355

Browse files
authored
Fix memory allocation when session ticket is used (#2470)
1 parent 360f620 commit c422355

File tree

3 files changed

+61
-10
lines changed

3 files changed

+61
-10
lines changed

crypto/s2n_cipher.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@ int s2n_session_key_alloc(struct s2n_session_key *key)
4141

4242
int s2n_session_key_free(struct s2n_session_key *key)
4343
{
44-
notnull_check(key->evp_cipher_ctx);
45-
EVP_CIPHER_CTX_free(key->evp_cipher_ctx);
46-
key->evp_cipher_ctx = NULL;
44+
if (key->evp_cipher_ctx != NULL) {
45+
EVP_CIPHER_CTX_free(key->evp_cipher_ctx);
46+
key->evp_cipher_ctx = NULL;
47+
}
4748
#if defined(S2N_CIPHER_AEAD_API_AVAILABLE)
48-
notnull_check(key->evp_aead_ctx);
49-
EVP_AEAD_CTX_free(key->evp_aead_ctx);
50-
key->evp_aead_ctx = NULL;
49+
if (key->evp_aead_ctx != NULL) {
50+
EVP_AEAD_CTX_free(key->evp_aead_ctx);
51+
key->evp_aead_ctx = NULL;
52+
}
5153
#endif
5254

5355
return 0;

tests/unit/s2n_session_ticket_test.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,58 @@ int main(int argc, char **argv)
964964
EXPECT_SUCCESS(s2n_config_free(client_config));
965965
}
966966

967+
/* s2n_decrypt_session_ticket fails to decrypt when presented with a valid ticket_key, valid iv and invalid encrypted blob */
968+
{
969+
EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
970+
EXPECT_NOT_NULL(server_config = s2n_config_new());
971+
972+
/* Add Session Ticket key on the server config */
973+
EXPECT_SUCCESS(s2n_config_set_session_tickets_onoff(server_config, 1));
974+
EXPECT_SUCCESS(s2n_config_add_ticket_crypto_key(server_config, ticket_key_name1, strlen((char *)ticket_key_name1), ticket_key1, sizeof(ticket_key1), 0));
975+
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
976+
977+
/* Setup stuffers value containing the valid key name, valid iv and invalid encrypted blob */
978+
GUARD(s2n_stuffer_write_bytes(&server_conn->client_ticket_to_decrypt, ticket_key_name1, sizeof(ticket_key_name1)));
979+
980+
uint8_t valid_iv[S2N_TLS_GCM_IV_LEN] = {0};
981+
GUARD(s2n_stuffer_write_bytes(&server_conn->client_ticket_to_decrypt, valid_iv, sizeof(valid_iv)));
982+
983+
uint8_t invalid_en_data[S2N_STATE_SIZE_IN_BYTES + S2N_TLS_GCM_TAG_LEN] = {0};
984+
GUARD(s2n_stuffer_write_bytes(&server_conn->client_ticket_to_decrypt, invalid_en_data, sizeof(invalid_en_data)));
985+
986+
server_conn->session_ticket_status = S2N_DECRYPT_TICKET;
987+
EXPECT_FAILURE_WITH_ERRNO(s2n_decrypt_session_ticket(server_conn), S2N_ERR_DECRYPT);
988+
989+
EXPECT_SUCCESS(s2n_connection_free(server_conn));
990+
EXPECT_SUCCESS(s2n_config_free(server_config));
991+
}
992+
993+
/* s2n_decrypt_session_ticket fails with a key not found error when presented with an invalid ticket_key, valid iv and invalid encrypted blob */
994+
{
995+
EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
996+
EXPECT_NOT_NULL(server_config = s2n_config_new());
997+
998+
/* Add Session Ticket key on the server config */
999+
EXPECT_SUCCESS(s2n_config_set_session_tickets_onoff(server_config, 1));
1000+
EXPECT_SUCCESS(s2n_config_add_ticket_crypto_key(server_config, ticket_key_name1, strlen((char *)ticket_key_name1), ticket_key1, sizeof(ticket_key1), 0));
1001+
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
1002+
1003+
/* Setup stuffers value containing the invalid key name, valid iv and invalid encrypted blob */
1004+
GUARD(s2n_stuffer_write_bytes(&server_conn->client_ticket_to_decrypt, ticket_key_name2, sizeof(ticket_key_name2)));
1005+
1006+
uint8_t valid_iv[S2N_TLS_GCM_IV_LEN] = {0};
1007+
GUARD(s2n_stuffer_write_bytes(&server_conn->client_ticket_to_decrypt, valid_iv, sizeof(valid_iv)));
1008+
1009+
uint8_t invalid_en_data[S2N_STATE_SIZE_IN_BYTES + S2N_TLS_GCM_TAG_LEN] = {0};
1010+
GUARD(s2n_stuffer_write_bytes(&server_conn->client_ticket_to_decrypt, invalid_en_data, sizeof(invalid_en_data)));
1011+
1012+
server_conn->session_ticket_status = S2N_DECRYPT_TICKET;
1013+
EXPECT_FAILURE_WITH_ERRNO(s2n_decrypt_session_ticket(server_conn), S2N_ERR_KEY_USED_IN_SESSION_TICKET_NOT_FOUND);
1014+
1015+
EXPECT_SUCCESS(s2n_connection_free(server_conn));
1016+
EXPECT_SUCCESS(s2n_config_free(server_config));
1017+
}
1018+
9671019
EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
9681020
EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
9691021
free(cert_chain);

tls/s2n_resume.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ int s2n_encrypt_session_ticket(struct s2n_connection *conn, struct s2n_stuffer *
529529
int s2n_decrypt_session_ticket(struct s2n_connection *conn)
530530
{
531531
struct s2n_ticket_key *key;
532-
struct s2n_session_key aes_ticket_key = {0};
532+
DEFER_CLEANUP(struct s2n_session_key aes_ticket_key = {0}, s2n_session_key_free);
533533
struct s2n_blob aes_key_blob = {0};
534534
struct s2n_stuffer *from;
535535

@@ -581,9 +581,6 @@ int s2n_decrypt_session_ticket(struct s2n_connection *conn)
581581

582582
GUARD(s2n_deserialize_resumption_state(conn, &state));
583583

584-
GUARD(s2n_aes256_gcm.destroy_key(&aes_ticket_key));
585-
GUARD(s2n_session_key_free(&aes_ticket_key));
586-
587584
uint64_t now;
588585
GUARD(conn->config->wall_clock(conn->config->sys_clock_ctx, &now));
589586

0 commit comments

Comments
 (0)