summaryrefslogtreecommitdiff
path: root/p11-kit
diff options
context:
space:
mode:
authorDaiki Ueno <dueno@redhat.com>2016-08-08 14:13:31 +0200
committerStef Walter <stefw@redhat.com>2016-09-02 17:48:36 +0200
commit31fbc32c41518b93a7b9903d7840378bab55370c (patch)
tree5668dfaaed7a6aab6bb25e8a73d773dee460a5c8 /p11-kit
parent8577e4dc23349ae8d04708190de6d1ae469ab460 (diff)
uri: Support 'slot-id' path attribute
Accept 'slot-id' path attribute defined in RFC 7512. https://bugs.freedesktop.org/show_bug.cgi?id=97245
Diffstat (limited to 'p11-kit')
-rw-r--r--p11-kit/test-uri.c47
-rw-r--r--p11-kit/uri.c78
-rw-r--r--p11-kit/uri.h4
3 files changed, 126 insertions, 3 deletions
diff --git a/p11-kit/test-uri.c b/p11-kit/test-uri.c
index 3773ddd..1fb5081 100644
--- a/p11-kit/test-uri.c
+++ b/p11-kit/test-uri.c
@@ -1396,6 +1396,51 @@ test_uri_pin_value_bad (void)
}
static void
+test_uri_slot_id (void)
+{
+ P11KitUri *uri;
+ CK_SLOT_ID slot_id;
+ char *string;
+ int ret;
+
+ uri = p11_kit_uri_new ();
+ assert_ptr_not_null (uri);
+
+ p11_kit_uri_set_slot_id (uri, 12345);
+
+ slot_id = p11_kit_uri_get_slot_id (uri);
+ assert_num_eq (12345, slot_id);
+
+ ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string);
+ assert_num_eq (P11_KIT_URI_OK, ret);
+ assert (strstr (string, "pkcs11:slot-id=12345") != NULL);
+ free (string);
+
+ ret = p11_kit_uri_parse ("pkcs11:slot-id=67890", P11_KIT_URI_FOR_ANY, uri);
+ assert_num_eq (P11_KIT_URI_OK, ret);
+
+ slot_id = p11_kit_uri_get_slot_id (uri);
+ assert_num_eq (67890, slot_id);
+
+ p11_kit_uri_free (uri);
+}
+
+static void
+test_uri_slot_id_bad (void)
+{
+ P11KitUri *uri;
+ int ret;
+
+ uri = p11_kit_uri_new ();
+ assert_ptr_not_null (uri);
+
+ ret = p11_kit_uri_parse ("pkcs11:slot-id=123^456", P11_KIT_URI_FOR_ANY, uri);
+ assert_num_eq (P11_KIT_URI_BAD_SYNTAX, ret);
+
+ p11_kit_uri_free (uri);
+}
+
+static void
test_uri_free_null (void)
{
p11_kit_uri_free (NULL);
@@ -1458,6 +1503,8 @@ 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_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");
p11_test (test_uri_message, "/uri/test_uri_message");
diff --git a/p11-kit/uri.c b/p11-kit/uri.c
index dcefec6..c64912f 100644
--- a/p11-kit/uri.c
+++ b/p11-kit/uri.c
@@ -147,6 +147,7 @@ struct p11_kit_uri {
CK_ATTRIBUTE *attrs;
char *pin_source;
char *pin_value;
+ CK_SLOT_ID slot_id;
};
static char *
@@ -325,6 +326,36 @@ p11_kit_uri_match_slot_info (P11KitUri *uri, CK_SLOT_INFO_PTR slot_info)
}
/**
+ * p11_kit_uri_get_slot_id:
+ * @uri: The URI
+ *
+ * Get the 'slot-id' part of the URI.
+ *
+ * Returns: The slot-id or <code>(CK_SLOT_ID)-1</code> if not set.
+ */
+CK_SLOT_ID
+p11_kit_uri_get_slot_id (P11KitUri *uri)
+{
+ return_val_if_fail (uri != NULL, (CK_SLOT_ID)-1);
+ return uri->slot_id;
+}
+
+/**
+ * p11_kit_uri_set_slot_id:
+ * @uri: The URI
+ * @slot_id: The new slot-id
+ *
+ * Set the 'slot-id' part of the URI.
+ */
+void
+p11_kit_uri_set_slot_id (P11KitUri *uri,
+ CK_SLOT_ID slot_id)
+{
+ return_if_fail (uri != NULL);
+ uri->slot_id = slot_id;
+}
+
+/**
* p11_kit_uri_get_token_info:
* @uri: the URI
*
@@ -717,6 +748,7 @@ p11_kit_uri_new (void)
/* So that it matches anything */
uri->module.libraryVersion.major = (CK_BYTE)-1;
uri->module.libraryVersion.minor = (CK_BYTE)-1;
+ uri->slot_id = (CK_SLOT_ID)-1;
return uri;
}
@@ -855,6 +887,22 @@ format_struct_version (p11_buffer *buffer,
return format_raw_string (buffer, is_first, name, buf);
}
+static bool
+format_ulong (p11_buffer *buffer,
+ bool *is_first,
+ const char *name,
+ CK_ULONG value)
+{
+ char buf[64];
+
+ /* Not set */
+ if (value == (CK_ULONG)-1)
+ return true;
+
+ snprintf (buf, sizeof (buf), "%lu", value);
+ return format_raw_string (buffer, is_first, name, buf);
+}
+
/**
* p11_kit_uri_format:
* @uri: The URI.
@@ -922,7 +970,9 @@ p11_kit_uri_format (P11KitUri *uri, P11KitUriType uri_type, char **string)
sizeof (uri->slot.slotDescription)) ||
!format_struct_string (&buffer, &is_first, "slot-manufacturer",
uri->slot.manufacturerID,
- sizeof (uri->slot.manufacturerID))) {
+ sizeof (uri->slot.manufacturerID)) ||
+ !format_ulong (&buffer, &is_first, "slot-id",
+ uri->slot_id)) {
return_val_if_reached (P11_KIT_URI_UNEXPECTED);
}
}
@@ -1106,10 +1156,10 @@ parse_token_info (const char *name_start, const char *name_end,
return parse_struct_info (where, length, start, end, uri);
}
-static int
+static long
atoin (const char *start, const char *end)
{
- int ret = 0;
+ long ret = 0;
while (start != end) {
if (*start < '0' || *start > '9')
return -1;
@@ -1177,6 +1227,25 @@ parse_slot_info (const char *name_start, const char *name_end,
}
static int
+parse_slot_id (const char *name_start, const char *name_end,
+ const char *start, const char *end,
+ P11KitUri *uri)
+{
+ assert (name_start <= name_end);
+ assert (start <= end);
+
+ if (memcmp ("slot-id", name_start, name_end - name_start) == 0) {
+ long val;
+ val = atoin (start, end);
+ if (val < 0)
+ return P11_KIT_URI_BAD_SYNTAX;
+ uri->slot_id = (CK_SLOT_ID)val;
+ return 1;
+ }
+ return 0;
+}
+
+static int
parse_module_version_info (const char *name_start, const char *name_end,
const char *start, const char *end,
P11KitUri *uri)
@@ -1315,6 +1384,7 @@ p11_kit_uri_parse (const char *string, P11KitUriType uri_type,
uri->pin_source = NULL;
free (uri->pin_value);
uri->pin_value = NULL;
+ uri->slot_id = (CK_SLOT_ID)-1;
for (;;) {
spos = strchr (string, ';');
@@ -1340,6 +1410,8 @@ p11_kit_uri_parse (const char *string, P11KitUriType uri_type,
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_SLOT) == P11_KIT_URI_FOR_SLOT)
+ ret = parse_slot_id (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 ca638b0..58f6fc9 100644
--- a/p11-kit/uri.h
+++ b/p11-kit/uri.h
@@ -101,6 +101,10 @@ 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_SLOT_ID p11_kit_uri_get_slot_id (P11KitUri *uri);
+void p11_kit_uri_set_slot_id (P11KitUri *uri,
+ CK_SLOT_ID slot_id);
+
CK_TOKEN_INFO_PTR p11_kit_uri_get_token_info (P11KitUri *uri);
int p11_kit_uri_match_token_info (P11KitUri *uri,