]> granicus.if.org Git - p11-kit/commitdiff
iter: Add p11_kit_iter_get_attributes() function
authorStef Walter <stef@thewalter.net>
Mon, 13 Jan 2014 17:02:44 +0000 (18:02 +0100)
committerStef Walter <stef@thewalter.net>
Mon, 13 Jan 2014 17:11:04 +0000 (18:11 +0100)
A simple wrapper for C_GetAttributeValue()

p11-kit/iter.c
p11-kit/iter.h
p11-kit/tests/test-iter.c

index 6a3ad4abddac088ad40f9e88b804c8a8f2cf190d..b6861a5a31b8b9895482d4dc7cac1445ae4e873f 100644 (file)
@@ -748,6 +748,41 @@ p11_kit_iter_destroy_object (P11KitIter *iter)
        return (iter->module->C_DestroyObject) (iter->session, iter->object);
 }
 
+/**
+ * p11_kit_iter_get_attributes:
+ * @iter: the iterator
+ * @template: (array length=count) (inout): the attributes to get
+ * @count: the number of attributes
+ *
+ * Get attributes for the current matching object.
+ *
+ * This calls <literal>C_GetAttributeValue</literal> for the object
+ * currently iterated to. Return value and attribute memory behavior
+ * is identical to the PKCS\#11 <literal>C_GetAttributeValue</literal>
+ * function.
+ *
+ * You might choose to use p11_kit_iter_load_attributes() for a more
+ * helpful variant.
+ *
+ * This can only be called after p11_kit_iter_next() succeeds.
+ *
+ * Returns: The result from <literal>C_GetAttributeValue</literal>.
+ */
+CK_RV
+p11_kit_iter_get_attributes (P11KitIter *iter,
+                             CK_ATTRIBUTE *template,
+                             CK_ULONG count)
+{
+       return_val_if_fail (iter != NULL, CKR_GENERAL_ERROR);
+       return_val_if_fail (iter->iterating, CKR_GENERAL_ERROR);
+       return_val_if_fail (iter->module != NULL, CKR_GENERAL_ERROR);
+       return_val_if_fail (iter->session != 0, CKR_GENERAL_ERROR);
+       return_val_if_fail (iter->object != 0, CKR_GENERAL_ERROR);
+
+       return (iter->module->C_GetAttributeValue) (iter->session, iter->object,
+                                                   template, count);
+}
+
 /**
  * p11_kit_iter_load_attributes:
  * @iter: the iterator
index fda0b775182c6dce96063b61c7a13477248e8928..33a6304b314bc91a2ec6123d2fbc33969ef58989 100644 (file)
@@ -94,6 +94,10 @@ CK_SESSION_HANDLE     p11_kit_iter_get_session              (P11KitIter *iter);
 
 CK_OBJECT_HANDLE      p11_kit_iter_get_object               (P11KitIter *iter);
 
+CK_RV                 p11_kit_iter_get_attributes           (P11KitIter *iter,
+                                                             CK_ATTRIBUTE *template,
+                                                             CK_ULONG count);
+
 CK_RV                 p11_kit_iter_load_attributes          (P11KitIter *iter,
                                                              CK_ATTRIBUTE *template,
                                                              CK_ULONG count);
index b72795e0d4b647edba2057ad2b5ba9be7e9b3fe0..e8b1e8462f7f09b9f2e1cabe9e1a8af3093e93bf 100644 (file)
@@ -962,6 +962,72 @@ test_find_objects_fail (void)
        assert (rv == CKR_OK);
 }
 
+static void
+test_get_attributes (void)
+{
+       CK_FUNCTION_LIST_PTR *modules;
+       P11KitIter *iter;
+       CK_OBJECT_HANDLE object;
+       char label[128];
+       CK_ULONG klass;
+       CK_ULONG ulong;
+       CK_RV rv;
+       int at;
+
+       CK_ATTRIBUTE template[] = {
+               { CKA_CLASS, &klass, sizeof (klass) },
+               { CKA_LABEL, label, sizeof (label) },
+               { CKA_INVALID },
+       };
+
+       CK_ATTRIBUTE attrs[3];
+
+       modules = initialize_and_get_modules ();
+
+       iter = p11_kit_iter_new (NULL, 0);
+       p11_kit_iter_begin (iter, modules);
+
+       while ((rv = p11_kit_iter_next (iter)) == CKR_OK) {
+               assert (sizeof (attrs) == sizeof (template));
+               memcpy (&attrs, &template, sizeof (attrs));
+
+               rv = p11_kit_iter_get_attributes (iter, attrs, 2);
+               assert (rv == CKR_OK);
+
+               object = p11_kit_iter_get_object (iter);
+               switch (object) {
+               case MOCK_DATA_OBJECT:
+                       assert (p11_attrs_find_ulong (attrs, CKA_CLASS, &ulong) && ulong == CKO_DATA);
+                       assert (p11_attr_match_value (p11_attrs_find (attrs, CKA_LABEL), "TEST LABEL", -1));
+                       break;
+               case MOCK_PUBLIC_KEY_CAPITALIZE:
+                       assert (p11_attrs_find_ulong (attrs, CKA_CLASS, &ulong) && ulong == CKO_PUBLIC_KEY);
+                       assert (p11_attr_match_value (p11_attrs_find (attrs, CKA_LABEL), "Public Capitalize Key", -1));
+                       break;
+               case MOCK_PUBLIC_KEY_PREFIX:
+                       assert (p11_attrs_find_ulong (attrs, CKA_CLASS, &ulong) && ulong == CKO_PUBLIC_KEY);
+                       assert (p11_attr_match_value (p11_attrs_find (attrs, CKA_LABEL), "Public prefix key", -1));
+                       break;
+               default:
+                       assert_fail ("Unknown object matched", NULL);
+                       break;
+               }
+
+               at++;
+       }
+
+       assert (rv == CKR_CANCEL);
+
+       /* Three modules, each with 1 slot, and 3 public objects */
+       assert_num_eq (9, at);
+
+       p11_kit_iter_free (iter);
+
+       finalize_and_free_modules (modules);
+}
+
+
+
 static void
 test_load_attributes (void)
 {
@@ -1251,6 +1317,7 @@ main (int argc,
        p11_test (test_open_session_fail, "/iter/test_open_session_fail");
        p11_test (test_find_init_fail, "/iter/test_find_init_fail");
        p11_test (test_find_objects_fail, "/iter/test_find_objects_fail");
+       p11_test (test_get_attributes, "/iter/get-attributes");
        p11_test (test_load_attributes, "/iter/test_load_attributes");
        p11_test (test_load_attributes_none, "/iter/test_load_attributes_none");
        p11_test (test_load_attributes_fail_first, "/iter/test_load_attributes_fail_first");