Skip to content

Commit

Permalink
Avoid OpenSSL functions are unconditionally called at OQS_destroy
Browse files Browse the repository at this point in the history
When OQS_DLOPEN_OPENSSL is designated and low-level primitives are
overridden with OQS_*_set_callbacks, OQS_destroy still indirectly
calls EVP_*_free from OpenSSL. This adds a extra NULL check to
avoid that.

Signed-off-by: Daiki Ueno <dueno@redhat.com>
  • Loading branch information
ueno committed Nov 7, 2024
1 parent 8bf124f commit 13168c8
Showing 1 changed file with 52 additions and 24 deletions.
76 changes: 52 additions & 24 deletions src/common/ossl_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,58 @@ static void fetch_ossl_objects(void) {
}

static void free_ossl_objects(void) {
OSSL_FUNC(EVP_MD_free)(sha256_ptr);
sha256_ptr = NULL;
OSSL_FUNC(EVP_MD_free)(sha384_ptr);
sha384_ptr = NULL;
OSSL_FUNC(EVP_MD_free)(sha512_ptr);
sha512_ptr = NULL;
OSSL_FUNC(EVP_MD_free)(sha3_256_ptr);
sha3_256_ptr = NULL;
OSSL_FUNC(EVP_MD_free)(sha3_384_ptr);
sha3_384_ptr = NULL;
OSSL_FUNC(EVP_MD_free)(sha3_512_ptr);
sha3_512_ptr = NULL;
OSSL_FUNC(EVP_MD_free)(shake128_ptr);
shake128_ptr = NULL;
OSSL_FUNC(EVP_MD_free)(shake256_ptr);
shake256_ptr = NULL;
OSSL_FUNC(EVP_CIPHER_free)(aes128_ecb_ptr);
aes128_ecb_ptr = NULL;
OSSL_FUNC(EVP_CIPHER_free)(aes128_ctr_ptr);
aes128_ctr_ptr = NULL;
OSSL_FUNC(EVP_CIPHER_free)(aes256_ecb_ptr);
aes256_ecb_ptr = NULL;
OSSL_FUNC(EVP_CIPHER_free)(aes256_ctr_ptr);
aes256_ctr_ptr = NULL;
/* Always check argument is non-NULL before calling EVP_*_free
* to avoid OpenSSL functions being used when they are
* overridden with OQS_*_set_callbacks.
*/
if (sha256_ptr) {
OSSL_FUNC(EVP_MD_free)(sha256_ptr);
sha256_ptr = NULL;
}
if (sha384_ptr) {
OSSL_FUNC(EVP_MD_free)(sha384_ptr);
sha384_ptr = NULL;
}
if (sha512_ptr) {
OSSL_FUNC(EVP_MD_free)(sha512_ptr);
sha512_ptr = NULL;
}
if (sha3_256_ptr) {
OSSL_FUNC(EVP_MD_free)(sha3_256_ptr);
sha3_256_ptr = NULL;
}
if (sha3_384_ptr) {
OSSL_FUNC(EVP_MD_free)(sha3_384_ptr);
sha3_384_ptr = NULL;
}
if (sha3_512_ptr) {
OSSL_FUNC(EVP_MD_free)(sha3_512_ptr);
sha3_512_ptr = NULL;
}
if (shake128_ptr) {
OSSL_FUNC(EVP_MD_free)(shake128_ptr);
shake128_ptr = NULL;
}
if (shake256_ptr) {
OSSL_FUNC(EVP_MD_free)(shake256_ptr);
shake256_ptr = NULL;
}
if (aes128_ecb_ptr) {
OSSL_FUNC(EVP_CIPHER_free)(aes128_ecb_ptr);
aes128_ecb_ptr = NULL;
}
if (aes128_ctr_ptr) {
OSSL_FUNC(EVP_CIPHER_free)(aes128_ctr_ptr);
aes128_ctr_ptr = NULL;
}
if (aes256_ecb_ptr) {
OSSL_FUNC(EVP_CIPHER_free)(aes256_ecb_ptr);
aes256_ecb_ptr = NULL;
}
if (aes256_ctr_ptr) {
OSSL_FUNC(EVP_CIPHER_free)(aes256_ctr_ptr);
aes256_ctr_ptr = NULL;
}
}
#endif // OPENSSL_VERSION_NUMBER >= 0x30000000L

Expand Down

0 comments on commit 13168c8

Please sign in to comment.