Blame SOURCES/libcacard-2.7.0-caching-keys.patch

f52a2a
From 2c10ae315375730020108cbcae0c282d0d6eff5f Mon Sep 17 00:00:00 2001
f52a2a
From: Jakub Jelen <jjelen@redhat.com>
f52a2a
Date: Mon, 26 Aug 2019 17:42:06 +0200
f52a2a
Subject: [PATCH 1/2] vcard_emul_nss: Drop the key caching to simplify error
f52a2a
 handling
f52a2a
f52a2a
It could happen with PKCS#11 modules that (correctly) invalidate object
f52a2a
handles after logout (which was introduced in 0d3a683a), that the handles
f52a2a
are not valid when we try to use the objects again.
f52a2a
f52a2a
This is trying to address this use case, which I noticed was breaking
f52a2a
CI with SoftHSM PKCS#11 modules.
f52a2a
f52a2a
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
f52a2a
---
f52a2a
 src/vcard_emul_nss.c | 15 +--------------
f52a2a
 1 file changed, 1 insertion(+), 14 deletions(-)
f52a2a
f52a2a
diff --git a/src/vcard_emul_nss.c b/src/vcard_emul_nss.c
f52a2a
index e8f5c56..f788964 100644
f52a2a
--- a/src/vcard_emul_nss.c
f52a2a
+++ b/src/vcard_emul_nss.c
f52a2a
@@ -52,7 +52,6 @@ typedef enum {
f52a2a
 struct VCardKeyStruct {
f52a2a
     CERTCertificate *cert;
f52a2a
     PK11SlotInfo *slot;
f52a2a
-    SECKEYPrivateKey *key;
f52a2a
     VCardEmulTriState failedX509;
f52a2a
 };
f52a2a
 
f52a2a
@@ -155,10 +154,6 @@ vcard_emul_make_key(PK11SlotInfo *slot, CERTCertificate *cert)
f52a2a
     key = g_new(VCardKey, 1);
f52a2a
     key->slot = PK11_ReferenceSlot(slot);
f52a2a
     key->cert = CERT_DupCertificate(cert);
f52a2a
-    /* NOTE: if we aren't logged into the token, this could return NULL */
f52a2a
-    /* NOTE: the cert is a temp cert, not necessarily the cert in the token,
f52a2a
-     * use the DER version of this function */
f52a2a
-    key->key = PK11_FindKeyByDERCert(slot, cert, NULL);
f52a2a
     key->failedX509 = VCardEmulUnknown;
f52a2a
     return key;
f52a2a
 }
f52a2a
@@ -170,10 +165,6 @@ vcard_emul_delete_key(VCardKey *key)
f52a2a
     if (!nss_emul_init || (key == NULL)) {
f52a2a
         return;
f52a2a
     }
f52a2a
-    if (key->key) {
f52a2a
-        SECKEY_DestroyPrivateKey(key->key);
f52a2a
-        key->key = NULL;
f52a2a
-    }
f52a2a
     if (key->cert) {
f52a2a
         CERT_DestroyCertificate(key->cert);
f52a2a
     }
f52a2a
@@ -189,12 +180,8 @@ vcard_emul_delete_key(VCardKey *key)
f52a2a
 static SECKEYPrivateKey *
f52a2a
 vcard_emul_get_nss_key(VCardKey *key)
f52a2a
 {
f52a2a
-    if (key->key) {
f52a2a
-        return key->key;
f52a2a
-    }
f52a2a
     /* NOTE: if we aren't logged into the token, this could return NULL */
f52a2a
-    key->key = PK11_FindPrivateKeyFromCert(key->slot, key->cert, NULL);
f52a2a
-    return key->key;
f52a2a
+    return PK11_FindPrivateKeyFromCert(key->slot, key->cert, NULL);
f52a2a
 }
f52a2a
 
f52a2a
 /*
f52a2a
-- 
f52a2a
2.22.0
f52a2a
f52a2a
f52a2a
From 06587ef683373690f61540935b4516b4f23238ea Mon Sep 17 00:00:00 2001
f52a2a
From: Jakub Jelen <jjelen@redhat.com>
f52a2a
Date: Tue, 27 Aug 2019 12:38:45 +0200
f52a2a
Subject: [PATCH 2/2] tests: Reproducer for pkcs11 modules invalidating object
f52a2a
 handles on logout
f52a2a
f52a2a
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
f52a2a
---
f52a2a
 tests/hwtests.c | 21 +++++++++++++++++++++
f52a2a
 1 file changed, 21 insertions(+)
f52a2a
f52a2a
diff --git a/tests/hwtests.c b/tests/hwtests.c
f52a2a
index cd9a33b..39decfb 100644
f52a2a
--- a/tests/hwtests.c
f52a2a
+++ b/tests/hwtests.c
f52a2a
@@ -339,6 +339,26 @@ static void test_sign_bad_data_x509(void)
f52a2a
     vreader_free(reader); /* get by id ref */
f52a2a
 }
f52a2a
 
f52a2a
+/* This is a regression test for issues with PKCS#11 tokens
f52a2a
+ * invalidating object handles after logout (such as softhsm).
f52a2a
+ * See: https://bugzilla.mozilla.org/show_bug.cgi?id=1576642
f52a2a
+ */
f52a2a
+static void test_sign_logout_sign(void)
f52a2a
+{
f52a2a
+    VReader *reader = vreader_get_reader_by_id(0);
f52a2a
+
f52a2a
+    g_assert_nonnull(reader);
f52a2a
+
f52a2a
+    test_login();
f52a2a
+    test_sign();
f52a2a
+
f52a2a
+    /* This implicitly logs out the user */
f52a2a
+    test_login();
f52a2a
+    test_sign();
f52a2a
+
f52a2a
+    vreader_free(reader); /* get by id ref */
f52a2a
+}
f52a2a
+
f52a2a
 static void libcacard_finalize(void)
f52a2a
 {
f52a2a
     VReader *reader = vreader_get_reader_by_id(0);
f52a2a
@@ -374,6 +394,7 @@ int main(int argc, char *argv[])
f52a2a
     g_test_add_func("/hw-tests/sign-bad-data", test_sign_bad_data_x509);
f52a2a
     g_test_add_func("/hw-tests/empty-applets", test_empty_applets);
f52a2a
     g_test_add_func("/hw-tests/get-response", test_get_response);
f52a2a
+    g_test_add_func("/hw-tests/sign-logout-sign", test_sign_logout_sign);
f52a2a
 
f52a2a
     ret = g_test_run();
f52a2a
 
f52a2a
-- 
f52a2a
2.22.0
f52a2a
f52a2a