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

Check private key when modifying credential #1351

Merged
merged 4 commits into from
Nov 11, 2020
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 @@ -46,6 +46,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Do not inherit settings from deleted users [#1328](https://github.com/greenbone/gvmd/pull/1328)
- Delete TLS certificate sources when deleting users [#1334](https://github.com/greenbone/gvmd/pull/1334)
- Fix SQL errors in SCAP and CERT update [#1343](https://github.com/greenbone/gvmd/pull/1343)
- Check private key when modifying credential [#1351](https://github.com/greenbone/gvmd/pull/1351)

### Removed
- Remove DROP from vulns creation [#1281](http://github.com/greenbone/gvmd/pull/1281)
Expand Down
42 changes: 0 additions & 42 deletions src/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,48 +267,6 @@ check_certificate (const char *cert_str, const char *credential_type)
return check_certificate_x509 (cert_str);
}

/**
* @brief Check that a string represents a valid Private Key.
*
* @param[in] key_str Private Key string.
* @param[in] key_phrase Private Key passphrase.
*
* @return 0 if valid, 1 otherwise.
*/
static int
check_private_key (const char *key_str, const char *key_phrase)
{
gnutls_x509_privkey_t key;
gnutls_datum_t data;
int ret;

assert (key_str);
if (gnutls_x509_privkey_init (&key))
return 1;
data.size = strlen (key_str);
data.data = (void *) g_strdup (key_str);
ret = gnutls_x509_privkey_import2 (key, &data, GNUTLS_X509_FMT_PEM,
key_phrase, 0);
if (ret)
{
gchar *public_key;
public_key = gvm_ssh_public_from_private (key_str, key_phrase);

if (public_key == NULL)
{
gnutls_x509_privkey_deinit (key);
g_free (data.data);
g_message ("%s: import failed: %s",
__func__, gnutls_strerror (ret));
return 1;
}
g_free (public_key);
}
g_free (data.data);
gnutls_x509_privkey_deinit (key);
return 0;
}

/**
* @brief Check that a string represents a valid Public Key.
*
Expand Down
3 changes: 3 additions & 0 deletions src/manage.h
Original file line number Diff line number Diff line change
Expand Up @@ -1976,6 +1976,9 @@ typedef enum
CREDENTIAL_FORMAT_ERROR = -1 /// Error / Invalid format
} credential_format_t;

int
check_private_key (const char *, const char *);

gboolean
find_credential_with_permission (const char*, credential_t*, const char*);

Expand Down
59 changes: 58 additions & 1 deletion src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include <dirent.h>
#include <errno.h>
#include <glib/gstdio.h>
#include <gnutls/x509.h>
#include <malloc.h>
#include <pwd.h>
#include <stdlib.h>
Expand Down Expand Up @@ -33762,6 +33763,48 @@ check_db_encryption_key ()
return 0;
}

/**
* @brief Check that a string represents a valid Private Key.
*
* @param[in] key_str Private Key string.
* @param[in] key_phrase Private Key passphrase.
*
* @return 0 if valid, 1 otherwise.
*/
int
check_private_key (const char *key_str, const char *key_phrase)
{
gnutls_x509_privkey_t key;
gnutls_datum_t data;
int ret;

assert (key_str);
if (gnutls_x509_privkey_init (&key))
return 1;
data.size = strlen (key_str);
data.data = (void *) g_strdup (key_str);
ret = gnutls_x509_privkey_import2 (key, &data, GNUTLS_X509_FMT_PEM,
key_phrase, 0);
if (ret)
{
gchar *public_key;
public_key = gvm_ssh_public_from_private (key_str, key_phrase);

if (public_key == NULL)
{
gnutls_x509_privkey_deinit (key);
g_free (data.data);
g_message ("%s: import failed: %s",
__func__, gnutls_strerror (ret));
return 1;
}
g_free (public_key);
}
g_free (data.data);
gnutls_x509_privkey_deinit (key);
return 0;
}

/**
* @brief Find a credential for a specific permission, given a UUID.
*
Expand Down Expand Up @@ -34665,12 +34708,26 @@ modify_credential (const char *credential_id,
{
if (key_private_to_use || password)
{
if (check_private_key (key_private_truncated
? key_private_to_use
: credential_iterator_private_key
(&iterator),
password
? password
: credential_iterator_password
(&iterator)))
{
sql_rollback ();
cleanup_iterator (&iterator);
return 8;
}

set_credential_private_key
(credential,
key_private_truncated
? key_private_to_use
: credential_iterator_private_key (&iterator),
password
password
? password
: credential_iterator_password (&iterator));
}
Expand Down