]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
ossl_property_list_to_string: handle quoted strings
authorPauli <pauli@openssl.org>
Sun, 24 Sep 2023 23:34:07 +0000 (09:34 +1000)
committerPauli <pauli@openssl.org>
Wed, 4 Oct 2023 21:09:13 +0000 (08:09 +1100)
ossl_property_list_to_string() didn't quote strings correctly which
could result in a generated property string being unparsable.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/22182)

crypto/property/property_parse.c
test/property_test.c

index b2bf3cd63180a92fa23f2db3c8b986ec2cf7262a..983f07e070cab9fcb482e5e39235e2033763119e 100644 (file)
@@ -588,15 +588,38 @@ static void put_char(char ch, char **buf, size_t *remain, size_t *needed)
 
 static void put_str(const char *str, char **buf, size_t *remain, size_t *needed)
 {
-    size_t olen, len;
+    size_t olen, len, i;
+    char quote = '\0';
+    int quotes;
 
     len = olen = strlen(str);
     *needed += len;
 
-    if (*remain == 0)
+    /*
+     * Check to see if we need quotes or not.
+     * Characters that are legal in a PropertyName don't need quoting.
+     * We simply assume all others require quotes.
+     */
+    for (i = 0; i < len; i++)
+        if (!ossl_isalnum(str[i]) && str[i] != '.' && str[i] != '_') {
+            /* Default to single quotes ... */
+            if (quote == '\0')
+                quote = '\'';
+            /* ... but use double quotes if a single is present */
+            if (str[i] == '\'')
+                quote = '"';
+        }
+
+    quotes = quote != '\0';
+    if (*remain == 0) {
+        *needed += 2 * quotes;
         return;
+    }
 
-    if (*remain < len + 1)
+    if (quotes)
+        put_char(quote, buf, remain, needed);
+
+    if (*remain < len + 1 + quotes)
         len = *remain - 1;
 
     if (len > 0) {
@@ -605,6 +628,9 @@ static void put_str(const char *str, char **buf, size_t *remain, size_t *needed)
         *remain -= len;
     }
 
+    if (quotes)
+        put_char(quote, buf, remain, needed);
+
     if (len < olen && *remain == 1) {
         **buf = '\0';
         ++*buf;
index 45b1db3e855b6a4b19d12c06c4f622c6b1d7abdd..bba96fac0a0191613a181805a6c1dfafacb0a7a6 100644 (file)
@@ -645,6 +645,9 @@ static struct {
     { "", "" },
     { "fips=3", "fips=3" },
     { "fips=-3", "fips=-3" },
+    { "provider='foo bar'", "provider='foo bar'" },
+    { "provider=\"foo bar'\"", "provider=\"foo bar'\"" },
+    { "provider=abc***", "provider='abc***'" },
     { NULL, "" }
 };