Skip to content

Commit

Permalink
ossl_property_list_to_string: handle quoted strings
Browse files Browse the repository at this point in the history
ossl_property_list_to_string() didn't quote strings correctly which
could result in a generated property string being unparsable.

Reviewed-by: Tomas Mraz <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from openssl#22182)
  • Loading branch information
paulidale committed Oct 4, 2023
1 parent 456e6ca commit fb20e66
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
32 changes: 29 additions & 3 deletions crypto/property/property_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions test/property_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, "" }
};

Expand Down

0 comments on commit fb20e66

Please sign in to comment.