]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Erase temporary buffer in EVP_PKEY_get_bn_param()
authorAnatolii Lishchynskyi <iamnotacake@protonmail.com>
Wed, 29 Mar 2023 14:16:48 +0000 (17:16 +0300)
committerTomas Mraz <tomas@openssl.org>
Tue, 4 Apr 2023 10:06:37 +0000 (12:06 +0200)
Function EVP_PKEY_get_bn_param() uses temporary buffer (on stack or
heap allocated) to store serialized bignum, but after deserializing it
into BIGNUM*, the buffer is not erased and may contain sensitive data.

This change makes sure the buffer is erased if it was successfully
filled before. Unfortunately, it does not distinguish between public and
private key components, and will always erase the buffer.

Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/20639)

(cherry picked from commit 34e4a962bca998cc2d6eb4be721153fbde2f4c35)

crypto/evp/p_lib.c

index 56f56bcb15a1212cd4e14adb6ce965db33660afa..cd8510643e245f91bb150b74fe753a7f14fcf8d1 100644 (file)
@@ -2178,7 +2178,14 @@ int EVP_PKEY_get_bn_param(const EVP_PKEY *pkey, const char *key_name,
         goto err;
     ret = OSSL_PARAM_get_BN(params, bn);
 err:
-    OPENSSL_free(buf);
+    if (buf != NULL) {
+        if (OSSL_PARAM_modified(params))
+            OPENSSL_clear_free(buf, buf_sz);
+        else
+            OPENSSL_free(buf);
+    } else if (OSSL_PARAM_modified(params)) {
+        OPENSSL_cleanse(buffer, params[0].data_size);
+    }
     return ret;
 }