diff --git a/src/common/ossl_helpers.c b/src/common/ossl_helpers.c index 85f6718b8..d2b2d99e9 100644 --- a/src/common/ossl_helpers.c +++ b/src/common/ossl_helpers.c @@ -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