summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaiki Ueno <dueno@redhat.com>2016-08-10 17:15:25 +0200
committerStef Walter <stefw@redhat.com>2016-09-02 17:48:36 +0200
commit927c8e98f159607acf7fa8b0f5bcf9a4d0497742 (patch)
treed92740a833136fa5df416c7e284ab22aa1374003
parente0c5d429df6ebe2cb88425edf42f65bfb33f0b77 (diff)
downloadp11-kit-927c8e98f159607acf7fa8b0f5bcf9a4d0497742.tar.gz
iter: Utilize 'slot-id' URI path attribute
https://bugs.freedesktop.org/show_bug.cgi?id=97245
-rw-r--r--p11-kit/iter.c8
-rw-r--r--p11-kit/test-iter.c76
2 files changed, 83 insertions, 1 deletions
diff --git a/p11-kit/iter.c b/p11-kit/iter.c
index 6032513..4caf5d7 100644
--- a/p11-kit/iter.c
+++ b/p11-kit/iter.c
@@ -64,6 +64,7 @@ struct p11_kit_iter {
CK_SLOT_INFO match_slot;
CK_TOKEN_INFO match_token;
CK_ATTRIBUTE *match_attrs;
+ CK_SLOT_ID match_slot_id;
Callback *callbacks;
/* The input modules */
@@ -181,6 +182,8 @@ p11_kit_iter_set_uri (P11KitIter *iter,
attrs = p11_kit_uri_get_attributes (uri, &count);
iter->match_attrs = p11_attrs_buildn (NULL, attrs, count);
+ iter->match_slot_id = p11_kit_uri_get_slot_id (uri);
+
minfo = p11_kit_uri_get_module_info (uri);
if (minfo != NULL)
memcpy (&iter->match_module, minfo, sizeof (CK_INFO));
@@ -194,10 +197,11 @@ p11_kit_iter_set_uri (P11KitIter *iter,
memcpy (&iter->match_token, tinfo, sizeof (CK_TOKEN_INFO));
}
} else {
- /* Match any module version number*/
+ /* Match any module version number and slot ID */
memset (&iter->match_module, 0, sizeof (iter->match_module));
iter->match_module.libraryVersion.major = (CK_BYTE)-1;
iter->match_module.libraryVersion.minor = (CK_BYTE)-1;
+ iter->match_slot_id = (CK_SLOT_ID)-1;
}
}
@@ -513,6 +517,8 @@ move_next_session (P11KitIter *iter)
iter->slot = iter->slots[iter->saw_slots++];
assert (iter->module != NULL);
+ if (iter->match_slot_id != (CK_SLOT_ID)-1 && iter->slot != iter->match_slot_id)
+ continue;
rv = (iter->module->C_GetSlotInfo) (iter->slot, &iter->slot_info);
if (rv != CKR_OK || !p11_match_uri_slot_info (&iter->match_slot, &iter->slot_info))
continue;
diff --git a/p11-kit/test-iter.c b/p11-kit/test-iter.c
index 07b8e32..3f5a76f 100644
--- a/p11-kit/test-iter.c
+++ b/p11-kit/test-iter.c
@@ -767,6 +767,80 @@ test_slot_mismatch (void)
}
static void
+test_slot_match_by_id (void)
+{
+ CK_FUNCTION_LIST_PTR *modules;
+ P11KitIter *iter;
+ P11KitUri *uri;
+ char *string;
+ CK_RV rv;
+ int count;
+ int ret;
+
+ modules = initialize_and_get_modules ();
+
+ uri = p11_kit_uri_new ();
+ ret = asprintf (&string, "pkcs11:slot-id=%lu", MOCK_SLOT_ONE_ID);
+ assert (ret > 0);
+ ret = p11_kit_uri_parse (string, P11_KIT_URI_FOR_SLOT, uri);
+ free (string);
+ assert_num_eq (P11_KIT_URI_OK, ret);
+
+ iter = p11_kit_iter_new (uri, 0);
+ p11_kit_uri_free (uri);
+
+ p11_kit_iter_begin (iter, modules);
+
+ count = 0;
+ while ((rv = p11_kit_iter_next (iter)) == CKR_OK)
+ count++;
+
+ assert (rv == CKR_CANCEL);
+
+ /* Three modules, each with 1 slot, and 3 public objects */
+ assert_num_eq (9, count);
+
+ p11_kit_iter_free (iter);
+
+ finalize_and_free_modules (modules);
+}
+
+static void
+test_slot_mismatch_by_id (void)
+{
+ CK_FUNCTION_LIST_PTR *modules;
+ P11KitIter *iter;
+ P11KitUri *uri;
+ CK_RV rv;
+ int count;
+ int ret;
+
+ modules = initialize_and_get_modules ();
+
+ uri = p11_kit_uri_new ();
+ ret = p11_kit_uri_parse ("pkcs11:slot-id=0", P11_KIT_URI_FOR_SLOT, uri);
+ assert_num_eq (P11_KIT_URI_OK, ret);
+
+ iter = p11_kit_iter_new (uri, 0);
+ p11_kit_uri_free (uri);
+
+ p11_kit_iter_begin (iter, modules);
+
+ count = 0;
+ while ((rv = p11_kit_iter_next (iter)) == CKR_OK)
+ count++;
+
+ assert (rv == CKR_CANCEL);
+
+ /* Nothing should have matched */
+ assert_num_eq (0, count);
+
+ p11_kit_iter_free (iter);
+
+ finalize_and_free_modules (modules);
+}
+
+static void
test_slot_info (void)
{
CK_FUNCTION_LIST_PTR *modules;
@@ -1415,6 +1489,8 @@ main (int argc,
p11_test (test_token_info, "/iter/token-info");
p11_test (test_slot_match, "/iter/test_slot_match");
p11_test (test_slot_mismatch, "/iter/test_slot_mismatch");
+ p11_test (test_slot_match_by_id, "/iter/test_slot_match_by_id");
+ p11_test (test_slot_mismatch_by_id, "/iter/test_slot_mismatch_by_id");
p11_test (test_slot_info, "/iter/slot-info");
p11_test (test_module_match, "/iter/test_module_match");
p11_test (test_module_mismatch, "/iter/test_module_mismatch");