Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape TLS certificate DNs that are invalid UTF-8 #1486

Merged
merged 2 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Fix size calculation in `--optimize vacuum` [#1447](https://github.com/greenbone/gvmd/pull/1447)
- Fix report host end time check in CVE scans [#1462](https://github.com/greenbone/gvmd/pull/1462)
- Fix "not regexp ..." filters [#1482](https://github.com/greenbone/gvmd/pull/1482)
- Escape TLS certificate DNs that are invalid UTF-8 [#1486](https://github.com/greenbone/gvmd/pull/1486)

### Removed

Expand Down
4 changes: 2 additions & 2 deletions src/manage_migrators.c
Original file line number Diff line number Diff line change
Expand Up @@ -851,9 +851,9 @@ make_tls_certificate_214 (user_t owner,
quoted_certificate_b64
= certificate_b64 ? sql_quote (certificate_b64) : NULL;
quoted_subject_dn
= subject_dn ? sql_quote (subject_dn) : NULL;
= subject_dn ? sql_ascii_escape_and_quote (subject_dn) : NULL;
quoted_issuer_dn
= issuer_dn ? sql_quote (issuer_dn) : NULL;
= issuer_dn ? sql_ascii_escape_and_quote (issuer_dn) : NULL;
quoted_md5_fingerprint
= md5_fingerprint ? sql_quote (md5_fingerprint) : NULL;
quoted_sha256_fingerprint
Expand Down
4 changes: 2 additions & 2 deletions src/manage_sql_tls_certificates.c
Original file line number Diff line number Diff line change
Expand Up @@ -585,9 +585,9 @@ make_tls_certificate (const char *name,
quoted_sha256_fingerprint
= sql_quote (sha256_fingerprint ? sha256_fingerprint : "");
quoted_subject_dn
= sql_quote (subject_dn ? subject_dn : "");
= sql_ascii_escape_and_quote (subject_dn ? subject_dn : "");
quoted_issuer_dn
= sql_quote (issuer_dn ? issuer_dn : "");
= sql_ascii_escape_and_quote (issuer_dn ? issuer_dn : "");
quoted_serial
= sql_quote (serial ? serial : "");

Expand Down
37 changes: 37 additions & 0 deletions src/sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,43 @@ sql_quote (const char* string)
return sql_nquote (string, strlen (string));
}

/**
* @brief Quotes a string for use in SQL statements, also ASCII escaping it
* if it is not valid UTF-8.
*
* @param[in] string String to quote, has to be \\0 terminated.
*
* @return Freshly allocated, quoted string. Free with g_free.
*/
gchar*
sql_ascii_escape_and_quote (const char* string)
{
gchar *quoted_string;

assert (string);

if (string == NULL)
{
return NULL;
}
else if (g_utf8_validate (string, -1, NULL))
{
// Quote valid UTF-8 without ASCII escaping
quoted_string = sql_quote (string);
}
else
{
// Assume invalid UTF-8 uses a different, unknown encoding and
// ASCII-escape it.
gchar *escaped_string;
escaped_string = g_strescape (string, "");
quoted_string = sql_quote (escaped_string);
g_free (escaped_string);
}

return quoted_string;
}

/**
* @brief Get the SQL insert expression for a string.
*
Expand Down
3 changes: 3 additions & 0 deletions src/sql.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ sql_nquote (const char *, size_t);
gchar *
sql_quote (const char *);

gchar *
sql_ascii_escape_and_quote (const char *);

gchar *
sql_insert (const char *);

Expand Down