summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaiki Ueno <dueno@redhat.com>2016-08-10 16:09:31 +0200
committerStef Walter <stefw@redhat.com>2016-09-02 17:48:36 +0200
commite0c5d429df6ebe2cb88425edf42f65bfb33f0b77 (patch)
tree5e6d1fb165aee035bbc51ff28205f1bbf814c4c9
parent31fbc32c41518b93a7b9903d7840378bab55370c (diff)
downloadp11-kit-e0c5d429df6ebe2cb88425edf42f65bfb33f0b77.tar.gz
iter: Utilize slot info URI path attributes
https://bugs.freedesktop.org/show_bug.cgi?id=97245
-rw-r--r--p11-kit/iter.c27
-rw-r--r--p11-kit/iter.h2
-rw-r--r--p11-kit/test-iter.c105
3 files changed, 134 insertions, 0 deletions
diff --git a/p11-kit/iter.c b/p11-kit/iter.c
index aeee8ad..6032513 100644
--- a/p11-kit/iter.c
+++ b/p11-kit/iter.c
@@ -61,6 +61,7 @@ struct p11_kit_iter {
/* Iterator matching data */
CK_INFO match_module;
+ CK_SLOT_INFO match_slot;
CK_TOKEN_INFO match_token;
CK_ATTRIBUTE *match_attrs;
Callback *callbacks;
@@ -84,6 +85,7 @@ struct p11_kit_iter {
CK_SLOT_ID slot;
CK_SESSION_HANDLE session;
CK_OBJECT_HANDLE object;
+ CK_SLOT_INFO slot_info;
CK_TOKEN_INFO token_info;
/* And various flags */
@@ -164,6 +166,7 @@ p11_kit_iter_set_uri (P11KitIter *iter,
{
CK_ATTRIBUTE *attrs;
CK_TOKEN_INFO *tinfo;
+ CK_SLOT_INFO *sinfo;
CK_INFO *minfo;
CK_ULONG count;
@@ -182,6 +185,10 @@ p11_kit_iter_set_uri (P11KitIter *iter,
if (minfo != NULL)
memcpy (&iter->match_module, minfo, sizeof (CK_INFO));
+ sinfo = p11_kit_uri_get_slot_info (uri);
+ if (sinfo != NULL)
+ memcpy (&iter->match_slot, sinfo, sizeof (CK_SLOT_INFO));
+
tinfo = p11_kit_uri_get_token_info (uri);
if (tinfo != NULL)
memcpy (&iter->match_token, tinfo, sizeof (CK_TOKEN_INFO));
@@ -506,6 +513,9 @@ move_next_session (P11KitIter *iter)
iter->slot = iter->slots[iter->saw_slots++];
assert (iter->module != NULL);
+ 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;
rv = (iter->module->C_GetTokenInfo) (iter->slot, &iter->token_info);
if (rv != CKR_OK || !p11_match_uri_token_info (&iter->match_token, &iter->token_info))
continue;
@@ -675,6 +685,23 @@ p11_kit_iter_get_slot (P11KitIter *iter)
}
/**
+ * p11_kit_iter_get_slot_info:
+ * @iter: the iterator
+ *
+ * Get the slot info for the slot which the current matching object is on.
+ *
+ * This can only be called after p11_kit_iter_next() succeeds.
+ *
+ * Returns: the slot of the current matching object.
+ */
+CK_SLOT_INFO *
+p11_kit_iter_get_slot_info (P11KitIter *iter)
+{
+ return_val_if_fail (iter != NULL, NULL);
+ return &iter->slot_info;
+}
+
+/**
* p11_kit_iter_get_token:
* @iter: the iterator
*
diff --git a/p11-kit/iter.h b/p11-kit/iter.h
index 33a6304..3f51041 100644
--- a/p11-kit/iter.h
+++ b/p11-kit/iter.h
@@ -88,6 +88,8 @@ CK_FUNCTION_LIST_PTR p11_kit_iter_get_module (P11KitIter *iter);
CK_SLOT_ID p11_kit_iter_get_slot (P11KitIter *iter);
+CK_SLOT_INFO * p11_kit_iter_get_slot_info (P11KitIter *iter);
+
CK_TOKEN_INFO * p11_kit_iter_get_token (P11KitIter *iter);
CK_SESSION_HANDLE p11_kit_iter_get_session (P11KitIter *iter);
diff --git a/p11-kit/test-iter.c b/p11-kit/test-iter.c
index 055a4b3..07b8e32 100644
--- a/p11-kit/test-iter.c
+++ b/p11-kit/test-iter.c
@@ -697,6 +697,108 @@ test_module_mismatch (void)
}
static void
+test_slot_match (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-manufacturer=TEST%20MANUFACTURER", 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);
+
+ /* 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 (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-manufacturer=blah", 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;
+ CK_SLOT_INFO *info;
+ P11KitIter *iter;
+ char *string;
+ CK_RV rv;
+
+ modules = initialize_and_get_modules ();
+
+ iter = p11_kit_iter_new (NULL, 0);
+ p11_kit_iter_begin (iter, modules);
+
+ rv = p11_kit_iter_next (iter);
+ assert_num_eq (rv, CKR_OK);
+
+ info = p11_kit_iter_get_slot_info (iter);
+ assert_ptr_not_null (info);
+
+ string = p11_kit_space_strdup (info->slotDescription,
+ sizeof (info->slotDescription));
+ assert_ptr_not_null (string);
+
+ assert_str_eq (string, "TEST SLOT");
+
+ free (string);
+ p11_kit_iter_free (iter);
+
+ finalize_and_free_modules (modules);
+}
+
+static void
test_token_match (void)
{
CK_FUNCTION_LIST_PTR *modules;
@@ -1311,6 +1413,9 @@ main (int argc,
p11_test (test_token_match, "/iter/test_token_match");
p11_test (test_token_mismatch, "/iter/test_token_mismatch");
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_info, "/iter/slot-info");
p11_test (test_module_match, "/iter/test_module_match");
p11_test (test_module_mismatch, "/iter/test_module_mismatch");
p11_test (test_getslotlist_fail_first, "/iter/test_getslotlist_fail_first");