From cfc654b2a532aa1adf3cda4bdee8b1397920f912 Mon Sep 17 00:00:00 2001 From: Daiki Ueno Date: Wed, 18 Jan 2017 10:18:23 +0100 Subject: uri: Support query attributes to specify module Accept and produce 'module-name' and 'module-path' query attributes defined in RFC 7512. --- doc/manual/p11-kit-sections.txt | 4 ++ p11-kit/test-uri.c | 115 ++++++++++++++++++++++++++++++++++++++ p11-kit/uri.c | 121 +++++++++++++++++++++++++++++++++++++--- p11-kit/uri.h | 10 ++++ 4 files changed, 241 insertions(+), 9 deletions(-) diff --git a/doc/manual/p11-kit-sections.txt b/doc/manual/p11-kit-sections.txt index 76401d5..e0f550d 100644 --- a/doc/manual/p11-kit-sections.txt +++ b/doc/manual/p11-kit-sections.txt @@ -30,6 +30,10 @@ p11_kit_uri_get_pin_source p11_kit_uri_set_pin_source p11_kit_uri_get_pinfile p11_kit_uri_set_pinfile +p11_kit_uri_get_module_name +p11_kit_uri_set_module_name +p11_kit_uri_get_module_path +p11_kit_uri_set_module_path p11_kit_uri_format p11_kit_uri_parse p11_kit_uri_free diff --git a/p11-kit/test-uri.c b/p11-kit/test-uri.c index b17001f..db694a7 100644 --- a/p11-kit/test-uri.c +++ b/p11-kit/test-uri.c @@ -1396,6 +1396,117 @@ test_uri_pin_value_bad (void) } static void +test_uri_module_name (void) +{ + P11KitUri *uri; + const char *module_name; + char *string; + int ret; + + uri = p11_kit_uri_new (); + assert_ptr_not_null (uri); + + p11_kit_uri_set_module_name (uri, "123456"); + + module_name = p11_kit_uri_get_module_name (uri); + assert_str_eq ("123456", module_name); + + p11_kit_uri_set_module_name (uri, "1*&#%&@("); + + module_name = p11_kit_uri_get_module_name (uri); + assert_str_eq ("1*&#%&@(", module_name); + + ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); + assert_num_eq (P11_KIT_URI_OK, ret); + assert (strstr (string, "pkcs11:?module-name=1%2a%26%23%25%26%40%28") != NULL); + free (string); + + ret = p11_kit_uri_parse ("pkcs11:?module-name=blah%2Fblah", P11_KIT_URI_FOR_ANY, uri); + assert_num_eq (P11_KIT_URI_OK, ret); + + module_name = p11_kit_uri_get_module_name (uri); + assert_str_eq ("blah/blah", module_name); + + p11_kit_uri_free (uri); +} + +static void +test_uri_module_name_bad (void) +{ + P11KitUri *uri; + int ret; + + uri = p11_kit_uri_new (); + assert_ptr_not_null (uri); + + ret = p11_kit_uri_parse ("pkcs11:?module-name=blahblah%2", P11_KIT_URI_FOR_ANY, uri); + assert_num_eq (P11_KIT_URI_BAD_ENCODING, ret); + + p11_kit_uri_free (uri); +} + +static void +test_uri_module_path (void) +{ + P11KitUri *uri; + const char *module_path; + char *string; + int ret; + + uri = p11_kit_uri_new (); + assert_ptr_not_null (uri); + + p11_kit_uri_set_module_path (uri, "/my-module-path"); + + module_path = p11_kit_uri_get_module_path (uri); + assert_str_eq ("/my-module-path", module_path); + + ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); + assert_num_eq (P11_KIT_URI_OK, ret); + assert (strstr (string, "module-path=%2fmy-module-path") != NULL); + free (string); + + ret = p11_kit_uri_parse ("pkcs11:?module-path=blah%2Fblah", P11_KIT_URI_FOR_ANY, uri); + assert_num_eq (P11_KIT_URI_OK, ret); + + module_path = p11_kit_uri_get_module_path (uri); + assert_str_eq ("blah/blah", module_path); + + p11_kit_uri_free (uri); +} + +static void +test_uri_module_name_and_path (void) +{ + P11KitUri *uri; + const char *module_name; + const char *module_path; + char *string; + int ret; + + uri = p11_kit_uri_new (); + assert_ptr_not_null (uri); + + p11_kit_uri_set_module_name (uri, "123456"); + p11_kit_uri_set_module_path (uri, "/my-module-path"); + + ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); + assert_num_eq (P11_KIT_URI_OK, ret); + assert (strstr (string, "pkcs11:?module-name=123456&module-path=%2fmy-module-path") != NULL); + free (string); + + ret = p11_kit_uri_parse ("pkcs11:?module-name=1%2a%26%23%25%26%40%28&module-path=blah%2Fblah", P11_KIT_URI_FOR_ANY, uri); + assert_num_eq (P11_KIT_URI_OK, ret); + + module_name = p11_kit_uri_get_module_name (uri); + assert_str_eq ("1*&#%&@(", module_name); + module_path = p11_kit_uri_get_module_path (uri); + assert_str_eq ("blah/blah", module_path); + + p11_kit_uri_free (uri); +} + +static void test_uri_slot_id (void) { P11KitUri *uri; @@ -1503,6 +1614,10 @@ main (int argc, p11_test (test_uri_pin_source, "/uri/test_uri_pin_source"); p11_test (test_uri_pin_value, "/uri/pin-value"); p11_test (test_uri_pin_value_bad, "/uri/pin-value-bad"); + p11_test (test_uri_module_name, "/uri/module-name"); + p11_test (test_uri_module_name_bad, "/uri/module-name-bad"); + p11_test (test_uri_module_path, "/uri/module-path"); + p11_test (test_uri_module_name_and_path, "/uri/module-name-and-path"); p11_test (test_uri_slot_id, "/uri/slot-id"); p11_test (test_uri_slot_id_bad, "/uri/slot-id-bad"); p11_test (test_uri_free_null, "/uri/test_uri_free_null"); diff --git a/p11-kit/uri.c b/p11-kit/uri.c index 2659fab..7641677 100644 --- a/p11-kit/uri.c +++ b/p11-kit/uri.c @@ -145,9 +145,11 @@ struct p11_kit_uri { CK_SLOT_INFO slot; CK_TOKEN_INFO token; CK_ATTRIBUTE *attrs; + CK_SLOT_ID slot_id; char *pin_source; char *pin_value; - CK_SLOT_ID slot_id; + char *module_name; + char *module_path; }; static char * @@ -727,6 +729,71 @@ p11_kit_uri_set_pinfile (P11KitUri *uri, const char *pinfile) p11_kit_uri_set_pin_source (uri, pinfile); } + +/** + * p11_kit_uri_get_module_name: + * @uri: The URI + * + * Get the 'module-name' part of the URI. This is used by some + * applications to explicitly specify the name of a PKCS\#11 module. + * + * Returns: The module-name or %NULL if not present. + */ +const char* +p11_kit_uri_get_module_name (P11KitUri *uri) +{ + return_val_if_fail (uri != NULL, NULL); + return uri->module_name; +} + +/** + * p11_kit_uri_set_module_name: + * @uri: The URI + * @name: The new module-name + * + * Set the 'module-name' part of the URI. This is used by some + * applications to explicitly specify the name of a PKCS\#11 module. + */ +void +p11_kit_uri_set_module_name (P11KitUri *uri, const char *name) +{ + return_if_fail (uri != NULL); + free (uri->module_name); + uri->module_name = name ? strdup (name) : NULL; +} + +/** + * p11_kit_uri_get_module_path: + * @uri: The URI + * + * Get the 'module-path' part of the URI. This is used by some + * applications to explicitly specify the path of a PKCS\#11 module. + * + * Returns: The module-path or %NULL if not present. + */ +const char* +p11_kit_uri_get_module_path (P11KitUri *uri) +{ + return_val_if_fail (uri != NULL, NULL); + return uri->module_path; +} + +/** + * p11_kit_uri_set_module_path: + * @uri: The URI + * @path: The new module-path + * + * Set the 'module-path' part of the URI. This is used by some + * applications to explicitly specify the path of a PKCS\#11 module. + */ +void +p11_kit_uri_set_module_path (P11KitUri *uri, const char *path) +{ + return_if_fail (uri != NULL); + free (uri->module_path); + uri->module_path = path ? strdup (path) : NULL; +} + /** * p11_kit_uri_new: * @@ -1041,6 +1108,22 @@ p11_kit_uri_format (P11KitUri *uri, P11KitUriType uri_type, char **string) } } + if (uri->module_name) { + if (!format_encode_string (&buffer, &sep, "module-name", + (const unsigned char*)uri->module_name, + strlen (uri->module_name), 0)) { + return_val_if_reached (P11_KIT_URI_UNEXPECTED); + } + } + + if (uri->module_path) { + if (!format_encode_string (&buffer, &sep, "module-path", + (const unsigned char*)uri->module_path, + strlen (uri->module_path), 0)) { + return_val_if_reached (P11_KIT_URI_UNEXPECTED); + } + } + return_val_if_fail (p11_buffer_ok (&buffer), P11_KIT_URI_UNEXPECTED); *string = p11_buffer_steal (&buffer, NULL); return P11_KIT_URI_OK; @@ -1311,25 +1394,39 @@ parse_extra_info (const char *name_start, const char *name_end, const char *start, const char *end, P11KitUri *uri) { - unsigned char *pin_source; + unsigned char *value; assert (name_start <= name_end); assert (start <= end); if (str_range_equal ("pinfile", name_start, name_end) || str_range_equal ("pin-source", name_start, name_end)) { - pin_source = p11_url_decode (start, end, P11_URL_WHITESPACE, NULL); - if (pin_source == NULL) + value = p11_url_decode (start, end, P11_URL_WHITESPACE, NULL); + if (value == NULL) return P11_KIT_URI_BAD_ENCODING; free (uri->pin_source); - uri->pin_source = (char*)pin_source; + uri->pin_source = (char*)value; return 1; } else if (str_range_equal ("pin-value", name_start, name_end)) { - pin_source = p11_url_decode (start, end, P11_URL_WHITESPACE, NULL); - if (pin_source == NULL) + value = p11_url_decode (start, end, P11_URL_WHITESPACE, NULL); + if (value == NULL) return P11_KIT_URI_BAD_ENCODING; free (uri->pin_value); - uri->pin_value = (char*)pin_source; + uri->pin_value = (char*)value; + return 1; + } else if (str_range_equal ("module-name", name_start, name_end)) { + value = p11_url_decode (start, end, P11_URL_WHITESPACE, NULL); + if (value == NULL) + return P11_KIT_URI_BAD_ENCODING; + free (uri->module_name); + uri->module_name = (char*)value; + return 1; + } else if (str_range_equal ("module-path", name_start, name_end)) { + value = p11_url_decode (start, end, P11_URL_WHITESPACE, NULL); + if (value == NULL) + return P11_KIT_URI_BAD_ENCODING; + free (uri->module_path); + uri->module_path = (char*)value; return 1; } @@ -1402,11 +1499,15 @@ p11_kit_uri_parse (const char *string, P11KitUriType uri_type, uri->module.libraryVersion.major = (CK_BYTE)-1; uri->module.libraryVersion.minor = (CK_BYTE)-1; uri->unrecognized = 0; + uri->slot_id = (CK_SLOT_ID)-1; free (uri->pin_source); uri->pin_source = NULL; free (uri->pin_value); uri->pin_value = NULL; - uri->slot_id = (CK_SLOT_ID)-1; + free (uri->module_name); + uri->module_name = NULL; + free (uri->module_path); + uri->module_path = NULL; /* Parse the path. */ for (;;) { @@ -1500,6 +1601,8 @@ p11_kit_uri_free (P11KitUri *uri) p11_attrs_free (uri->attrs); free (uri->pin_source); free (uri->pin_value); + free (uri->module_name); + free (uri->module_path); free (uri); } diff --git a/p11-kit/uri.h b/p11-kit/uri.h index 948733b..e4d3330 100644 --- a/p11-kit/uri.h +++ b/p11-kit/uri.h @@ -153,6 +153,16 @@ void p11_kit_uri_set_pinfile (P11KitUri *uri, #endif /* P11_KIT_DISABLE_DEPRECATED */ +const char* p11_kit_uri_get_module_name (P11KitUri *uri); + +void p11_kit_uri_set_module_name (P11KitUri *uri, + const char *name); + +const char* p11_kit_uri_get_module_path (P11KitUri *uri); + +void p11_kit_uri_set_module_path (P11KitUri *uri, + const char *path); + void p11_kit_uri_set_unrecognized (P11KitUri *uri, int unrecognized); -- cgit v1.1