]> granicus.if.org Git - p11-kit/commitdiff
uri: Support query attributes to specify module
authorDaiki Ueno <dueno@redhat.com>
Wed, 18 Jan 2017 09:18:23 +0000 (10:18 +0100)
committerDaiki Ueno <ueno@gnu.org>
Thu, 19 Jan 2017 12:51:32 +0000 (13:51 +0100)
Accept and produce 'module-name' and 'module-path' query attributes
defined in RFC 7512.

doc/manual/p11-kit-sections.txt
p11-kit/test-uri.c
p11-kit/uri.c
p11-kit/uri.h

index 76401d55995cf0067aa66313688c5cb81d24d86f..e0f550d3979f13b6ec6845388da767052c266afc 100644 (file)
@@ -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
index b17001f9226d622465429cbc4d7fefcd8feefdb6..db694a7361480cf0b4344b57c5ba6c9d023bfdfd 100644 (file)
@@ -1395,6 +1395,117 @@ test_uri_pin_value_bad (void)
        p11_kit_uri_free (uri);
 }
 
+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)
 {
@@ -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");
index 2659fab836152037c8274664fab067259244e3d0..7641677f1e1841e34bfb729810a4f63c25164c65 100644 (file)
@@ -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);
 }
 
index 948733b02802d06d00fcf5ca2d35dc66e7c4fb35..e4d333019fadd9f88b372708957c7889dd9c3cd1 100644 (file)
@@ -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);