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

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