Blame SOURCES/Remove-deprecated-OpenSSL-calls-from-softpkcs11.patch

a53771
From c99ecf1bb49e2fbd0bf30a7b357cf06407b9588a Mon Sep 17 00:00:00 2001
a53771
From: Robbie Harwood <rharwood@redhat.com>
a53771
Date: Sat, 15 May 2021 18:04:58 -0400
a53771
Subject: [PATCH] Remove deprecated OpenSSL calls from softpkcs11
a53771
a53771
Rewrite add_pubkey_info() in terms of the EVP_PKEY interface.  In this
a53771
process, fix its unchecked allocations and fail fast for non-RSA keys.
a53771
a53771
(cherry picked from commit d6bf42279675100e3e4fe7c6e08eef74d49624cb)
a53771
(cherry picked from commit 5072bfdfaddae762680d0f9d97afa6dbf8274760)
a53771
---
a53771
 src/configure.ac            |   1 +
a53771
 src/tests/softpkcs11/main.c | 106 ++++++++++++++++++++++++------------
a53771
 2 files changed, 72 insertions(+), 35 deletions(-)
a53771
a53771
diff --git a/src/configure.ac b/src/configure.ac
a53771
index 3e1052db7..eb6307468 100644
a53771
--- a/src/configure.ac
a53771
+++ b/src/configure.ac
a53771
@@ -1114,6 +1114,7 @@ int i = 1;
a53771
 ])], k5_cv_openssl_version_okay=yes, k5_cv_openssl_version_okay=no)])
a53771
   old_LIBS="$LIBS"
a53771
   AC_CHECK_LIB(crypto, PKCS7_get_signer_info)
a53771
+  AC_CHECK_FUNCS(EVP_PKEY_get_bn_param)
a53771
   LIBS="$old_LIBS"
a53771
 fi
a53771
 if test "$k5_cv_openssl_version_okay" = yes && (test "$enable_pkinit" = yes || test "$enable_pkinit" = try); then
a53771
diff --git a/src/tests/softpkcs11/main.c b/src/tests/softpkcs11/main.c
a53771
index caa537b68..86b4ef711 100644
a53771
--- a/src/tests/softpkcs11/main.c
a53771
+++ b/src/tests/softpkcs11/main.c
a53771
@@ -413,47 +413,83 @@ add_object_attribute(struct st_object *o,
a53771
     return CKR_OK;
a53771
 }
a53771
 
a53771
+#ifdef HAVE_EVP_PKEY_GET_BN_PARAM
a53771
+
a53771
+/* Declare owner pointers since EVP_PKEY_get_bn_param() gives us copies. */
a53771
+#define DECLARE_BIGNUM(name) BIGNUM *name = NULL
a53771
+#define RELEASE_BIGNUM(bn) BN_clear_free(bn)
a53771
 static CK_RV
a53771
-add_pubkey_info(struct st_object *o, CK_KEY_TYPE key_type, EVP_PKEY *key)
a53771
+get_bignums(EVP_PKEY *key, BIGNUM **n, BIGNUM **e)
a53771
 {
a53771
-    switch (key_type) {
a53771
-    case CKK_RSA: {
a53771
-        CK_BYTE *modulus = NULL;
a53771
-        size_t modulus_len = 0;
a53771
-        CK_ULONG modulus_bits = 0;
a53771
-        CK_BYTE *exponent = NULL;
a53771
-        size_t exponent_len = 0;
a53771
-        const RSA *rsa;
a53771
-        const BIGNUM *n, *e;
a53771
+    if (EVP_PKEY_get_bn_param(key, "n", n) == 0 ||
a53771
+        EVP_PKEY_get_bn_param(key, "e", e) == 0)
a53771
+        return CKR_DEVICE_ERROR;
a53771
 
a53771
-        rsa = EVP_PKEY_get0_RSA(key);
a53771
-        RSA_get0_key(rsa, &n, &e, NULL);
a53771
-        modulus_bits = BN_num_bits(n);
a53771
-
a53771
-        modulus_len = BN_num_bytes(n);
a53771
-        modulus = malloc(modulus_len);
a53771
-        BN_bn2bin(n, modulus);
a53771
-
a53771
-        exponent_len = BN_num_bytes(e);
a53771
-        exponent = malloc(exponent_len);
a53771
-        BN_bn2bin(e, exponent);
a53771
-
a53771
-        add_object_attribute(o, 0, CKA_MODULUS, modulus, modulus_len);
a53771
-        add_object_attribute(o, 0, CKA_MODULUS_BITS,
a53771
-                             &modulus_bits, sizeof(modulus_bits));
a53771
-        add_object_attribute(o, 0, CKA_PUBLIC_EXPONENT,
a53771
-                             exponent, exponent_len);
a53771
-
a53771
-        free(modulus);
a53771
-        free(exponent);
a53771
-    }
a53771
-    default:
a53771
-        /* XXX */
a53771
-        break;
a53771
-    }
a53771
     return CKR_OK;
a53771
 }
a53771
 
a53771
+#else
a53771
+
a53771
+/* Declare const pointers since the old API gives us aliases. */
a53771
+#define DECLARE_BIGNUM(name) const BIGNUM *name
a53771
+#define RELEASE_BIGNUM(bn)
a53771
+static CK_RV
a53771
+get_bignums(EVP_PKEY *key, const BIGNUM **n, const BIGNUM **e)
a53771
+{
a53771
+    const RSA *rsa;
a53771
+
a53771
+    rsa = EVP_PKEY_get0_RSA(key);
a53771
+    RSA_get0_key(rsa, n, e, NULL);
a53771
+
a53771
+    return CKR_OK;
a53771
+}
a53771
+
a53771
+#endif
a53771
+
a53771
+static CK_RV
a53771
+add_pubkey_info(struct st_object *o, CK_KEY_TYPE key_type, EVP_PKEY *key)
a53771
+{
a53771
+    CK_BYTE *modulus = NULL, *exponent = 0;
a53771
+    size_t modulus_len = 0, exponent_len = 0;
a53771
+    CK_ULONG modulus_bits = 0;
a53771
+    CK_RV ret;
a53771
+    DECLARE_BIGNUM(n);
a53771
+    DECLARE_BIGNUM(e);
a53771
+
a53771
+    if (key_type != CKK_RSA)
a53771
+        abort();
a53771
+
a53771
+    ret = get_bignums(key, &n, &e);
a53771
+    if (ret != CKR_OK)
a53771
+        goto done;
a53771
+
a53771
+    modulus_bits = BN_num_bits(n);
a53771
+    modulus_len = BN_num_bytes(n);
a53771
+    exponent_len = BN_num_bytes(e);
a53771
+
a53771
+    modulus = malloc(modulus_len);
a53771
+    exponent = malloc(exponent_len);
a53771
+    if (modulus == NULL || exponent == NULL) {
a53771
+        ret = CKR_DEVICE_MEMORY;
a53771
+        goto done;
a53771
+    }
a53771
+
a53771
+    BN_bn2bin(n, modulus);
a53771
+    BN_bn2bin(e, exponent);
a53771
+
a53771
+    add_object_attribute(o, 0, CKA_MODULUS, modulus, modulus_len);
a53771
+    add_object_attribute(o, 0, CKA_MODULUS_BITS, &modulus_bits,
a53771
+                         sizeof(modulus_bits));
a53771
+    add_object_attribute(o, 0, CKA_PUBLIC_EXPONENT, exponent, exponent_len);
a53771
+
a53771
+    ret = CKR_OK;
a53771
+done:
a53771
+    free(modulus);
a53771
+    free(exponent);
a53771
+    RELEASE_BIGNUM(n);
a53771
+    RELEASE_BIGNUM(e);
a53771
+    return ret;
a53771
+}
a53771
 
a53771
 static int
a53771
 pem_callback(char *buf, int num, int w, void *key)