From bc60631d3e327fd97f53c68c5b3134e4cefad7e1 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Mon, 13 Jan 2014 18:02:44 +0100 Subject: iter: Add p11_kit_iter_get_attributes() function A simple wrapper for C_GetAttributeValue() --- p11-kit/iter.c | 35 +++++++++++++++++++++++++ p11-kit/iter.h | 4 +++ p11-kit/tests/test-iter.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) diff --git a/p11-kit/iter.c b/p11-kit/iter.c index 6a3ad4a..b6861a5 100644 --- a/p11-kit/iter.c +++ b/p11-kit/iter.c @@ -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 C_GetAttributeValue for the object + * currently iterated to. Return value and attribute memory behavior + * is identical to the PKCS\#11 C_GetAttributeValue + * 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 C_GetAttributeValue. + */ +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 diff --git a/p11-kit/iter.h b/p11-kit/iter.h index fda0b77..33a6304 100644 --- a/p11-kit/iter.h +++ b/p11-kit/iter.h @@ -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); diff --git a/p11-kit/tests/test-iter.c b/p11-kit/tests/test-iter.c index b72795e..e8b1e84 100644 --- a/p11-kit/tests/test-iter.c +++ b/p11-kit/tests/test-iter.c @@ -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"); -- cgit v1.2.1