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

Merge gvm-libs-11.0 into master #316

Merged
merged 18 commits into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Fixed
- Fix sigsegv when no plugin_feed_info.inc file present. [#278](https://github.com/greenbone/gvm-libs/pull/278)
- Fix missing linking to libgnutls in util/CMakeLists.txt. [#291](https://github.com/greenbone/gvm-libs/pull/291)
- Fix trust and file handling for S/MIME [#309](https://github.com/greenbone/gvm-libs/pull/309)
- Get details with get_reports in gmp_get_report_ext [#313](https://github.com/greenbone/gvm-libs/pull/313)

[11.0.1]: https://github.com/greenbone/gvm-libs/compare/v11.0.0...gvm-libs-11.0

Expand Down Expand Up @@ -55,6 +57,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Internalize struct nvtpref_t. [#260](https://github.com/greenbone/gvm-libs/pull/260)
- Extend redis connection error msg with actual path. [#264](https://github.com/greenbone/gvm-libs/pull/264)
- Disable testhosts test as it's not really a test. [#287](https://github.com/greenbone/gvm-libs/pull/287)
- Don't create an entity tree during read_string_c. [#305](https://github.com/greenbone/gvm-libs/pull/305)

### Fixed
- Prevent g_strsplit to be called with NULL. [#238](https://github.com/greenbone/gvm-libs/pull/238)
Expand Down
1 change: 1 addition & 0 deletions gmp/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,7 @@ gmp_get_report_ext (gnutls_session_t *session, gmp_get_report_opts_t opts,
if (gvm_server_sendf (
session,
"<get_reports"
" details=\"1\""
" report_id=\"%s\""
" format_id=\"%s\""
" host_first_result=\"%i\""
Expand Down
122 changes: 120 additions & 2 deletions util/gpgmeutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,104 @@ find_email_encryption_key (gpgme_ctx_t ctx, const char *uid_email)
}
}

/**
* @brief Wrapper for fread for use as a GPGME callback.
*
* @param[in] handle The file handle.
* @param[out] buffer The data buffer to read data into.
* @param[in] size The size of the buffer.
*
* @return The number of bytes read or -1 on error.
*/
static ssize_t
gvm_gpgme_fread (void *handle, void *buffer, size_t size)
{
int ret;
FILE *file = (FILE *)handle;

ret = fread (buffer, 1, size, file);
if (ferror (file))
return -1;
return ret;
}

/**
* @brief Wrapper for fread for use as a GPGME callback.
*
* @param[in] handle The file handle.
* @param[in] buffer The data buffer to read data into.
* @param[in] size The amount of buffered data.
*
* @return The number of bytes written or -1 on error.
*/
static ssize_t
gvm_gpgme_fwrite (void *handle, const void *buffer, size_t size)
{
int ret;
FILE *file = (FILE *)handle;

ret = fwrite (buffer, 1, size, file);
if (ferror (file))
return -1;
return ret;
}

/**
* @brief Adds a trust list of all current certificates to a GPG homedir.
*
* This will overwrite the existing trustlist, so it should only be used for
* temporary, automatically generated GPG home directories.
*
* TODO: This should use or be replaced by a trust model inside GVM.
*
* @param[in] ctx The GPGME context to get the keys from.
* @param[in] homedir The directory to write the trust list file to.
*
* @return 0 success, -1 error.
*/
static int
create_all_certificates_trustlist (gpgme_ctx_t ctx, const char *homedir)
{
gpgme_key_t key;
gchar *trustlist_filename;
GString *trustlist_content;
GError *g_err;

g_err = NULL;
gpgme_set_pinentry_mode (ctx, GPGME_PINENTRY_MODE_CANCEL);

trustlist_filename = g_build_filename (homedir,
"trustlist.txt",
NULL);

trustlist_content = g_string_new ("");

gpgme_op_keylist_start (ctx, NULL, 0);
gpgme_op_keylist_next (ctx, &key);
while (key)
{
g_string_append_printf (trustlist_content, "%s S\n", key->fpr);
gpgme_op_keylist_next (ctx, &key);
}

if (g_file_set_contents (trustlist_filename,
trustlist_content->str,
trustlist_content->len,
&g_err) == FALSE)
{
g_warning ("%s: Could not write trust list: %s",
__func__, g_err->message);
g_free (trustlist_filename);
g_string_free (trustlist_content, TRUE);
return -1;
}

g_free (trustlist_filename);
g_string_free (trustlist_content, TRUE);

return 0;
}

/**
* @brief Encrypt a stream for a PGP public key, writing to another stream.
*
Expand Down Expand Up @@ -387,6 +485,7 @@ encrypt_stream_internal (FILE *plain_file, FILE *encrypted_file,
gpgme_error_t err;
gpgme_encrypt_flags_t encrypt_flags;
const char *key_type_str;
struct gpgme_data_cbs callbacks;

if (uid_email == NULL || strcmp (uid_email, "") == 0)
{
Expand Down Expand Up @@ -441,10 +540,29 @@ encrypt_stream_internal (FILE *plain_file, FILE *encrypted_file,

// Set up data objects for input and output streams
gpgme_data_new_from_stream (&plain_data, plain_file);
gpgme_data_new_from_stream (&encrypted_data, encrypted_file);

/* Create a GPGME data buffer with custom read and write functions.
*
* This is neccessary as gpgme_data_new_from_stream may cause problems
* when trying to write to the stream after some operations. */
memset (&callbacks, 0, sizeof (callbacks));
callbacks.read = gvm_gpgme_fread;
callbacks.write = gvm_gpgme_fwrite;
gpgme_data_new_from_cbs (&encrypted_data, &callbacks, encrypted_file);

if (protocol == GPGME_PROTOCOL_CMS)
gpgme_data_set_encoding (encrypted_data, GPGME_DATA_ENCODING_BASE64);
{
gpgme_data_set_encoding (encrypted_data, GPGME_DATA_ENCODING_BASE64);

if (create_all_certificates_trustlist (ctx, gpg_temp_dir))
{
gpgme_data_release (plain_data);
gpgme_data_release (encrypted_data);
gpgme_release (ctx);
gvm_file_remove_recurse (gpg_temp_dir);
return -1;
}
}

// Encrypt data
err = gpgme_op_encrypt (ctx, keys, encrypt_flags, plain_data, encrypted_data);
Expand Down
Loading