On Fri, 2014-07-04 at 14:56 +0200, Stef Walter wrote:
> On 25.06.2014 14:25, Nikos Mavrogiannopoulos wrote:
> > Hello,
> >  By mistake I came across a PKCS #11 URL that is like that:
> > "pkcs11:model=SoftHSM;manufacturer=SoftHSM;serial=1;token=master-key;id=;object=;object-type=private"
> > 
> > Note that id and object are empty. However using
> > p11_kit_uri_get_attribute(), I cannot distinguish between a given but
> > empty ID, and an ID that doesn't exist, as they are in both cases NULL.
> > Would it make sense for p11_kit_uri_get_attribute() to distinguish
> > between these two cases and return the empty string for that particular
> > URL? My issue is that I was using the id and object fields as a way to
> > perform sanity check on the URL provided by the user, but with this
> > particular object I cannot distinguish between intentional empty ID and
> > object, and not having these fields at all.
> 
> Hmmm, indeed. That seems broken. An attribute that doesn't exist should
> cause p11_kit_uri_get_attribute() to return NULL. That's what the
> documentation says:

I was wrong on that. My test for emptiness included a test on value_len,
which was zero, and that's why I couldn't distinguish. I added a few
test cases though.

regards,
Nikos

>From e4b2074245532bb7a8a60f08d9edf2ca7424de4b Mon Sep 17 00:00:00 2001
From: Nikos Mavrogiannopoulos <n...@redhat.com>
Date: Fri, 4 Jul 2014 15:40:58 +0200
Subject: [PATCH] Added test for non-null values in empty ID and label URI
 parts

---
 p11-kit/tests/test-uri.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 66 insertions(+)

diff --git a/p11-kit/tests/test-uri.c b/p11-kit/tests/test-uri.c
index f514f7a..b6de7ad 100644
--- a/p11-kit/tests/test-uri.c
+++ b/p11-kit/tests/test-uri.c
@@ -159,6 +159,70 @@ test_uri_parse_with_label_and_klass (void)
 }
 
 static void
+test_uri_parse_with_empty_label (void)
+{
+	CK_ATTRIBUTE_PTR attr;
+	P11KitUri *uri;
+	int ret;
+
+	uri = p11_kit_uri_new ();
+	assert_ptr_not_null (uri);
+
+	ret = p11_kit_uri_parse ("pkcs11:object=;object-type=cert", P11_KIT_URI_FOR_ANY, uri);
+	assert_num_eq (P11_KIT_URI_OK, ret);
+
+	attr = p11_kit_uri_get_attribute (uri, CKA_LABEL);
+	assert_ptr_not_null (attr);
+
+	p11_kit_uri_free (uri);
+
+	/* really empty */
+
+	uri = p11_kit_uri_new ();
+	assert_ptr_not_null (uri);
+
+	ret = p11_kit_uri_parse ("pkcs11:object-type=cert", P11_KIT_URI_FOR_ANY, uri);
+	assert_num_eq (P11_KIT_URI_OK, ret);
+
+	attr = p11_kit_uri_get_attribute (uri, CKA_LABEL);
+	assert (attr == NULL);
+
+	p11_kit_uri_free (uri);
+}
+
+static void
+test_uri_parse_with_empty_id (void)
+{
+	CK_ATTRIBUTE_PTR attr;
+	P11KitUri *uri;
+	int ret;
+
+	uri = p11_kit_uri_new ();
+	assert_ptr_not_null (uri);
+
+	ret = p11_kit_uri_parse ("pkcs11:id=;object-type=cert", P11_KIT_URI_FOR_ANY, uri);
+	assert_num_eq (P11_KIT_URI_OK, ret);
+
+	attr = p11_kit_uri_get_attribute (uri, CKA_ID);
+	assert_ptr_not_null (attr);
+
+	p11_kit_uri_free (uri);
+
+	/* really empty */
+
+	uri = p11_kit_uri_new ();
+	assert_ptr_not_null (uri);
+
+	ret = p11_kit_uri_parse ("pkcs11:object-type=cert", P11_KIT_URI_FOR_ANY, uri);
+	assert_num_eq (P11_KIT_URI_OK, ret);
+
+	attr = p11_kit_uri_get_attribute (uri, CKA_ID);
+	assert (attr == NULL);
+
+	p11_kit_uri_free (uri);
+}
+
+static void
 test_uri_parse_with_id (void)
 {
 	CK_ATTRIBUTE_PTR attr;
@@ -1203,6 +1267,8 @@ main (int argc,
 	p11_test (test_uri_parse, "/uri/test_uri_parse");
 	p11_test (test_uri_parse_bad_scheme, "/uri/test_uri_parse_bad_scheme");
 	p11_test (test_uri_parse_with_label, "/uri/test_uri_parse_with_label");
+	p11_test (test_uri_parse_with_empty_label, "/uri/test_uri_parse_with_empty_label");
+	p11_test (test_uri_parse_with_empty_id, "/uri/test_uri_parse_with_empty_id");
 	p11_test (test_uri_parse_with_label_and_klass, "/uri/test_uri_parse_with_label_and_klass");
 	p11_test (test_uri_parse_with_id, "/uri/test_uri_parse_with_id");
 	p11_test (test_uri_parse_with_bad_string_encoding, "/uri/test_uri_parse_with_bad_string_encoding");
-- 
1.9.3

_______________________________________________
p11-glue mailing list
p11-glue@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/p11-glue

Reply via email to