diff options
author | Daiki Ueno <dueno@redhat.com> | 2018-10-15 14:57:16 +0200 |
---|---|---|
committer | Daiki Ueno <ueno@gnu.org> | 2018-11-07 17:23:53 +0100 |
commit | f758142178b4cc5c650dde75152bfb85ac992178 (patch) | |
tree | e6776c73097ae0c21444c4557983f1209f194f12 | |
parent | e81f6af7ed3b39b8df0bb7ce150619ea8178d47c (diff) |
url: Prefer upper-case letters in hex characters when encoding
This makes it more compliant with RFC 3986, where the use of
upper-case letters is recommended (as "SHOULD").
Suggested by Sumit Bose.
-rw-r--r-- | common/test-path.c | 2 | ||||
-rw-r--r-- | common/test-url.c | 28 | ||||
-rw-r--r-- | common/url.c | 24 | ||||
-rw-r--r-- | p11-kit/test-uri.c | 14 |
4 files changed, 53 insertions, 15 deletions
diff --git a/common/test-path.c b/common/test-path.c index c77acf2..2eb5444 100644 --- a/common/test-path.c +++ b/common/test-path.c @@ -206,7 +206,7 @@ test_encode (void) char *test; test = p11_path_encode ("2309haonutb;/AOE@#$O "); - assert_str_eq (test, "2309haonutb%3b/AOE%40%23%24O%20"); + assert_str_eq (test, "2309haonutb%3B/AOE%40%23%24O%20"); free (test); } diff --git a/common/test-url.c b/common/test-url.c index 892bf3c..4f10cb4 100644 --- a/common/test-url.c +++ b/common/test-url.c @@ -150,6 +150,33 @@ test_encode_verbatim (void) p11_buffer_uninit (&buf); } +static void +test_encode_lower (void) +{ + const unsigned char *input = (unsigned char *)",.:;"; + p11_buffer buf; + + if (!p11_buffer_init_null (&buf, 5)) + assert_not_reached (); + + p11_url_encode (input, input + 5, "", &buf); + assert (p11_buffer_ok (&buf)); + assert_str_eq ("%2C%2E%3A%3B%00", (char *)buf.data); + assert_num_eq (15, buf.len); + + if (!p11_buffer_reset (&buf, 5)) + assert_not_reached (); + + setenv ("P11_KIT_URI_LOWERCASE", "1", 1); + + p11_url_encode (input, input + 5, "", &buf); + assert (p11_buffer_ok (&buf)); + assert_str_eq ("%2c%2e%3a%3b%00", (char *)buf.data); + assert_num_eq (15, buf.len); + + p11_buffer_uninit (&buf); +} + int main (int argc, char *argv[]) @@ -160,5 +187,6 @@ main (int argc, p11_test (test_encode, "/url/encode"); p11_test (test_encode_verbatim, "/url/encode-verbatim"); + p11_test (test_encode_lower, "/url/encode-lower"); return p11_test_run (argc, argv); } diff --git a/common/url.c b/common/url.c index 344d971..903c58b 100644 --- a/common/url.c +++ b/common/url.c @@ -44,7 +44,8 @@ #include <stdio.h> #include <string.h> -const static char HEX_CHARS[] = "0123456789abcdef"; +const static char HEX_CHARS_UPPER[] = "0123456789ABCDEF"; +const static char HEX_CHARS_LOWER[] = "0123456789abcdef"; unsigned char * p11_url_decode (const char *value, @@ -75,14 +76,14 @@ p11_url_decode (const char *value, free (result); return NULL; } - a = strchr (HEX_CHARS, p11_ascii_tolower (value[0])); - b = strchr (HEX_CHARS, p11_ascii_tolower (value[1])); + a = strchr (HEX_CHARS_UPPER, p11_ascii_toupper (value[0])); + b = strchr (HEX_CHARS_UPPER, p11_ascii_toupper (value[1])); if (!a || !b) { free (result); return NULL; } - *p = (a - HEX_CHARS) << 4; - *(p++) |= (b - HEX_CHARS); + *p = (a - HEX_CHARS_UPPER) << 4; + *(p++) |= (b - HEX_CHARS_UPPER); value += 2; /* Ignore whitespace characters */ @@ -110,9 +111,18 @@ p11_url_encode (const unsigned char *value, p11_buffer *buf) { char hex[3]; + const char *env; + const char *hex_chars; assert (value <= end); + /* Opt to output lowercase hex-digits for compatibility */ + env = secure_getenv ("P11_KIT_URI_LOWERCASE"); + if (env && *env != '\0') + hex_chars = HEX_CHARS_LOWER; + else + hex_chars = HEX_CHARS_UPPER; + /* Now loop through looking for escapes */ while (value != end) { @@ -123,8 +133,8 @@ p11_url_encode (const unsigned char *value, /* All others get encoded */ } else { hex[0] = '%'; - hex[1] = HEX_CHARS[((unsigned char)*value) >> 4]; - hex[2] = HEX_CHARS[((unsigned char)*value) & 0x0F]; + hex[1] = hex_chars[((unsigned char)*value) >> 4]; + hex[2] = hex_chars[((unsigned char)*value) & 0x0F]; p11_buffer_add (buf, hex, 3); } diff --git a/p11-kit/test-uri.c b/p11-kit/test-uri.c index f33687e..32e8da7 100644 --- a/p11-kit/test-uri.c +++ b/p11-kit/test-uri.c @@ -657,7 +657,7 @@ test_uri_build_with_attributes (void) assert (strstr (string, "object=The%20Label") != NULL); assert (strstr (string, "type=data") != NULL); - assert (strstr (string, "id=%48%45%4c%4c%4f") != NULL); + assert (strstr (string, "id=%48%45%4C%4C%4F") != NULL); free (string); p11_kit_uri_free (uri); @@ -1352,7 +1352,7 @@ test_uri_pin_source (void) ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); assert_num_eq (P11_KIT_URI_OK, ret); - assert (strstr (string, "pin-source=%7cmy-pin-file") != NULL); + assert (strstr (string, "pin-source=%7Cmy-pin-file") != NULL); free (string); ret = p11_kit_uri_parse ("pkcs11:?pin-source=blah%2Fblah", P11_KIT_URI_FOR_ANY, uri); @@ -1388,7 +1388,7 @@ test_uri_pin_value (void) ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_ANY, &string); assert_num_eq (P11_KIT_URI_OK, ret); - assert (strstr (string, "pkcs11:?pin-value=1%2a%26%23%25%26%40%28") != NULL); + assert (strstr (string, "pkcs11:?pin-value=1%2A%26%23%25%26%40%28") != NULL); free (string); ret = p11_kit_uri_parse ("pkcs11:?pin-value=blah%2Fblah", P11_KIT_URI_FOR_ANY, uri); @@ -1438,7 +1438,7 @@ test_uri_module_name (void) 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); + 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); @@ -1483,7 +1483,7 @@ test_uri_module_path (void) 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); + 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); @@ -1512,10 +1512,10 @@ test_uri_module_name_and_path (void) 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); + 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); + 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); |