From f48aafe0f0e2d6222be635a6767bbf1a3f98c48e Mon Sep 17 00:00:00 2001 From: Daiki Ueno Date: Mon, 8 Aug 2016 13:33:14 +0200 Subject: [PATCH] uri: Support slot info path attributes Accept 'slot-description' and 'slot-manifacturer' path attributes defined in RFC 7512. https://bugs.freedesktop.org/show_bug.cgi?id=97245 --- p11-kit/private.h | 3 ++ p11-kit/test-uri.c | 70 +++++++++++++++++++++++++++++++++++++ p11-kit/uri.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++ p11-kit/uri.h | 6 ++++ 4 files changed, 180 insertions(+) diff --git a/p11-kit/private.h b/p11-kit/private.h index 1de61eb..b363b17 100644 --- a/p11-kit/private.h +++ b/p11-kit/private.h @@ -58,6 +58,9 @@ void _p11_set_progname_unlocked (const char *pro int p11_match_uri_module_info (CK_INFO_PTR one, CK_INFO_PTR two); +int p11_match_uri_slot_info (CK_SLOT_INFO_PTR one, + CK_SLOT_INFO_PTR two); + int p11_match_uri_token_info (CK_TOKEN_INFO_PTR one, CK_TOKEN_INFO_PTR two); diff --git a/p11-kit/test-uri.c b/p11-kit/test-uri.c index e3e8dfb..3773ddd 100644 --- a/p11-kit/test-uri.c +++ b/p11-kit/test-uri.c @@ -57,6 +57,14 @@ is_module_empty (P11KitUri *uri) } static int +is_slot_empty (P11KitUri *uri) +{ + CK_SLOT_INFO_PTR slot = p11_kit_uri_get_slot_info (uri); + return (slot->slotDescription[0] == 0 && + slot->manufacturerID[0] == 0); +} + +static int is_token_empty (P11KitUri *uri) { CK_TOKEN_INFO_PTR token = p11_kit_uri_get_token_info (uri); @@ -87,6 +95,7 @@ test_uri_parse (void) assert_num_eq (P11_KIT_URI_OK, ret); assert (is_module_empty (uri)); + assert (is_slot_empty (uri)); assert (is_token_empty (uri)); assert (are_attributes_empty (uri)); @@ -122,6 +131,7 @@ test_uri_parse_with_label (void) assert_num_eq (P11_KIT_URI_OK, ret); assert (is_module_empty (uri)); + assert (is_slot_empty (uri)); assert (is_token_empty (uri)); attr = p11_kit_uri_get_attribute (uri, CKA_LABEL); @@ -429,6 +439,27 @@ test_uri_parse_with_library_bad_encoding (void) } static void +test_uri_parse_with_slot (void) +{ + P11KitUri *uri = NULL; + CK_SLOT_INFO_PTR slot; + int ret; + + uri = p11_kit_uri_new (); + assert_ptr_not_null (uri); + + ret = p11_kit_uri_parse ("pkcs11:slot-description=Slot%20Description;slot-manufacturer=Me", + P11_KIT_URI_FOR_SLOT, uri); + assert_num_eq (P11_KIT_URI_OK, ret); + + slot = p11_kit_uri_get_slot_info (uri); + assert (is_space_string (slot->slotDescription, sizeof (slot->slotDescription), "Slot Description")); + assert (is_space_string (slot->manufacturerID, sizeof (slot->manufacturerID), "Me")); + + p11_kit_uri_free (uri); +} + +static void test_uri_build_empty (void) { P11KitUri *uri; @@ -613,6 +644,43 @@ test_uri_build_with_attributes (void) } static void +test_uri_build_with_slot_info (void) +{ + char *string = NULL; + P11KitUri *uri; + P11KitUri *check; + CK_SLOT_INFO_PTR slot; + int ret; + + uri = p11_kit_uri_new (); + assert_ptr_not_null (uri); + + slot = p11_kit_uri_get_slot_info (uri); + set_space_string (slot->slotDescription, sizeof (slot->slotDescription), "The Slot Description"); + set_space_string (slot->manufacturerID, sizeof (slot->manufacturerID), "Me"); + + ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); + assert_num_eq (P11_KIT_URI_OK, ret); + assert_ptr_not_null (string); + + check = p11_kit_uri_new (); + assert_ptr_not_null (check); + + ret = p11_kit_uri_parse (string, P11_KIT_URI_FOR_SLOT, check); + assert_num_eq (P11_KIT_URI_OK, ret); + + p11_kit_uri_match_slot_info (check, p11_kit_uri_get_slot_info (uri)); + + p11_kit_uri_free (uri); + p11_kit_uri_free (check); + + assert (strstr (string, "slot-description=The%20Slot%20Description") != NULL); + assert (strstr (string, "slot-manufacturer=Me") != NULL); + + free (string); +} + +static void test_uri_parse_private_key (void) { P11KitUri *uri; @@ -1361,11 +1429,13 @@ main (int argc, p11_test (test_uri_parse_with_spaces, "/uri/test_uri_parse_with_spaces"); p11_test (test_uri_parse_with_library, "/uri/test_uri_parse_with_library"); p11_test (test_uri_parse_with_library_bad_encoding, "/uri/test_uri_parse_with_library_bad_encoding"); + p11_test (test_uri_parse_with_slot, "/uri/test_uri_parse_with_slot"); p11_test (test_uri_build_empty, "/uri/test_uri_build_empty"); p11_test (test_uri_build_with_token_info, "/uri/test_uri_build_with_token_info"); p11_test (test_uri_build_with_token_null_info, "/uri/test_uri_build_with_token_null_info"); p11_test (test_uri_build_with_token_empty_info, "/uri/test_uri_build_with_token_empty_info"); p11_test (test_uri_build_with_attributes, "/uri/test_uri_build_with_attributes"); + p11_test (test_uri_build_with_slot_info, "/uri/test_uri_build_with_slot_info"); p11_test (test_uri_parse_private_key, "/uri/test_uri_parse_private_key"); p11_test (test_uri_parse_secret_key, "/uri/test_uri_parse_secret_key"); p11_test (test_uri_parse_library_version, "/uri/test_uri_parse_library_version"); diff --git a/p11-kit/uri.c b/p11-kit/uri.c index 9b5144b..dcefec6 100644 --- a/p11-kit/uri.c +++ b/p11-kit/uri.c @@ -90,6 +90,7 @@ * P11KitUriType: * @P11_KIT_URI_FOR_OBJECT: The URI represents one or more objects * @P11_KIT_URI_FOR_TOKEN: The URI represents one or more tokens + * @P11_KIT_URI_FOR_SLOT: The URI represents one or more slots * @P11_KIT_URI_FOR_MODULE: The URI represents one or more modules * @P11_KIT_URI_FOR_MODULE_WITH_VERSION: The URI represents a module with * a specific version. @@ -141,6 +142,7 @@ struct p11_kit_uri { bool unrecognized; CK_INFO module; + CK_SLOT_INFO slot; CK_TOKEN_INFO token; CK_ATTRIBUTE *attrs; char *pin_source; @@ -261,6 +263,68 @@ p11_kit_uri_match_module_info (P11KitUri *uri, CK_INFO_PTR info) } /** + * p11_kit_uri_get_slot_info: + * @uri: the URI + * + * Get the CK_SLOT_INFO structure associated with this URI. + * + * If this is a parsed URI, then the fields corresponding to slot parts of + * the URI will be filled in. Any slot URI parts that were missing will have + * their fields filled with zeros. + * + * If the caller wishes to setup information for building a URI, then relevant + * fields should be filled in. Fields that should not appear as parts in the + * resulting URI should be filled with zeros. + * + * Returns: A pointer to the CK_INFO structure. + */ +CK_SLOT_INFO_PTR +p11_kit_uri_get_slot_info (P11KitUri *uri) +{ + return_val_if_fail (uri != NULL, NULL); + return &uri->slot; +} + +int +p11_match_uri_slot_info (CK_SLOT_INFO_PTR one, + CK_SLOT_INFO_PTR two) +{ + return (match_struct_string (one->slotDescription, + two->slotDescription, + sizeof (one->slotDescription)) && + match_struct_string (one->manufacturerID, + two->manufacturerID, + sizeof (one->manufacturerID))); +} + +/** + * p11_kit_uri_match_slot_info: + * @uri: the URI + * @slot_info: the structure to match against the URI + * + * Match a CK_SLOT_INFO structure against the slot parts of this + * URI. + * + * Only the fields of the CK_SLOT_INFO structure that are valid + * for use in a URI will be matched. A URI part that was not specified in the + * URI will match any value in the structure. If during the URI parsing any + * unrecognized parts were encountered then this match will fail. + * + * Returns: 1 if the URI matches, 0 if not. + */ +int +p11_kit_uri_match_slot_info (P11KitUri *uri, CK_SLOT_INFO_PTR slot_info) +{ + return_val_if_fail (uri != NULL, 0); + return_val_if_fail (slot_info != NULL, 0); + + if (uri->unrecognized) + return 0; + + return p11_match_uri_slot_info (&uri->slot, slot_info); +} + +/** * p11_kit_uri_get_token_info: * @uri: the URI * @@ -852,6 +916,17 @@ p11_kit_uri_format (P11KitUri *uri, P11KitUriType uri_type, char **string) } } + if ((uri_type & P11_KIT_URI_FOR_SLOT) == P11_KIT_URI_FOR_SLOT) { + if (!format_struct_string (&buffer, &is_first, "slot-description", + uri->slot.slotDescription, + sizeof (uri->slot.slotDescription)) || + !format_struct_string (&buffer, &is_first, "slot-manufacturer", + uri->slot.manufacturerID, + sizeof (uri->slot.manufacturerID))) { + return_val_if_reached (P11_KIT_URI_UNEXPECTED); + } + } + if ((uri_type & P11_KIT_URI_FOR_TOKEN) == P11_KIT_URI_FOR_TOKEN) { if (!format_struct_string (&buffer, &is_first, "model", uri->token.model, @@ -1078,6 +1153,30 @@ parse_struct_version (const char *start, const char *end, CK_VERSION_PTR version } static int +parse_slot_info (const char *name_start, const char *name_end, + const char *start, const char *end, + P11KitUri *uri) +{ + unsigned char *where; + size_t length; + + assert (name_start <= name_end); + assert (start <= end); + + if (memcmp ("slot-description", name_start, name_end - name_start) == 0) { + where = uri->slot.slotDescription; + length = sizeof (uri->slot.slotDescription); + } else if (memcmp ("slot-manufacturer", name_start, name_end - name_start) == 0) { + where = uri->slot.manufacturerID; + length = sizeof (uri->slot.manufacturerID); + } else { + return 0; + } + + return parse_struct_info (where, length, start, end, uri); +} + +static int parse_module_version_info (const char *name_start, const char *name_end, const char *start, const char *end, P11KitUri *uri) @@ -1239,6 +1338,8 @@ p11_kit_uri_parse (const char *string, P11KitUriType uri_type, ret = parse_class_attribute (string, epos, epos + 1, spos, uri); if (ret == 0 && (uri_type & P11_KIT_URI_FOR_TOKEN) == P11_KIT_URI_FOR_TOKEN) ret = parse_token_info (string, epos, epos + 1, spos, uri); + if (ret == 0 && (uri_type & P11_KIT_URI_FOR_SLOT) == P11_KIT_URI_FOR_SLOT) + ret = parse_slot_info (string, epos, epos + 1, spos, uri); if (ret == 0 && (uri_type & P11_KIT_URI_FOR_MODULE) == P11_KIT_URI_FOR_MODULE) ret = parse_module_info (string, epos, epos + 1, spos, uri); if (ret == 0 && (uri_type & P11_KIT_URI_FOR_MODULE_WITH_VERSION) == P11_KIT_URI_FOR_MODULE_WITH_VERSION) diff --git a/p11-kit/uri.h b/p11-kit/uri.h index 0c20d85..ca638b0 100644 --- a/p11-kit/uri.h +++ b/p11-kit/uri.h @@ -59,6 +59,7 @@ typedef enum { typedef enum { P11_KIT_URI_FOR_OBJECT = (1 << 1), P11_KIT_URI_FOR_TOKEN = (1 << 2), + P11_KIT_URI_FOR_SLOT = (1 << 5), P11_KIT_URI_FOR_MODULE = (1 << 3), P11_KIT_URI_FOR_MODULE_WITH_VERSION = @@ -95,6 +96,11 @@ CK_INFO_PTR p11_kit_uri_get_module_info (P11KitUri *uri); int p11_kit_uri_match_module_info (P11KitUri *uri, CK_INFO_PTR info); +CK_SLOT_INFO_PTR p11_kit_uri_get_slot_info (P11KitUri *uri); + +int p11_kit_uri_match_slot_info (P11KitUri *uri, + CK_SLOT_INFO_PTR slot_info); + CK_TOKEN_INFO_PTR p11_kit_uri_get_token_info (P11KitUri *uri); int p11_kit_uri_match_token_info (P11KitUri *uri, -- 2.7.4