summaryrefslogtreecommitdiffstats
path: root/crypto/property
diff options
context:
space:
mode:
authorPauli <pauli@openssl.org>2023-09-25 09:34:07 +1000
committerPauli <pauli@openssl.org>2023-10-05 08:10:00 +1100
commitfdfc1de2aa206ff10287933e729db892bd545567 (patch)
tree4eeaf3738ec515996cb3fd7f6db9575fc4eb5d25 /crypto/property
parent9c20f5db0feaddc4c9ea4c4b2b07e6d87d6701f1 (diff)
ossl_property_list_to_string: handle quoted strings
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) (cherry picked from commit fb20e66c6b2651067f50bab8cf098c71e2caed4b)
Diffstat (limited to 'crypto/property')
-rw-r--r--crypto/property/property_parse.c32
1 files changed, 29 insertions, 3 deletions
diff --git a/crypto/property/property_parse.c b/crypto/property/property_parse.c
index b2bf3cd631..983f07e070 100644
--- a/crypto/property/property_parse.c
+++ b/crypto/property/property_parse.c
@@ -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;