Greenbone Vulnerability Management Libraries: util/gpgmeutils.c File Reference

Greenbone Vulnerability Management Libraries 23.1.1

Macros | Functions
gpgmeutils.c File Reference

GPGME utilities. More...

`#include "gpgmeutils.h"`
`#include "fileutils.h"`
`#include <errno.h>`
`#include <gpg-error.h>`
`#include <locale.h>`
`#include <stdlib.h>`
`#include <string.h>`
`#include <sys/stat.h>`
`#include <unistd.h>`

## Macros
#define | G_LOG_DOMAIN "libgvm util"
| GLib logging domain.
#define | CHECK_ERR(func)

## Functions
void | log_gpgme (GLogLevelFlags level, gpg_error_t err, const char *fmt,...)
| Log function with extra gpg-error style output.
gpgme_ctx_t | gvm_init_gpgme_ctx_from_dir (const gchar *dir)
| Returns a new gpgme context.
int | gvm_gpg_import_many_types_from_string (gpgme_ctx_t ctx, const char *key_str, ssize_t key_len, GArray *key_types)
| Import a key or certificate given by a string.
int | gvm_gpg_import_from_string (gpgme_ctx_t ctx, const char *key_str, ssize_t key_len, gpgme_data_type_t key_type)
| Import a key or certificate given by a string.
static gpgme_key_t | find_email_encryption_key (gpgme_ctx_t ctx, const char *uid_email)
| Find a key that can be used to encrypt for an email recipient.
static ssize_t | gvm_gpgme_fread (void *handle, void *buffer, size_t size)
| Wrapper for fread for use as a GPGME callback.
static ssize_t | gvm_gpgme_fwrite (void *handle, const void *buffer, size_t size)
| Wrapper for fread for use as a GPGME callback.
static int | create_all_certificates_trustlist (gpgme_ctx_t ctx, const char *homedir)
| Adds a trust list of all current certificates to a GPG homedir.
static int | encrypt_stream_internal (FILE *plain_file, FILE *encrypted_file, const char *key_str, ssize_t key_len, const char *uid_email, gpgme_protocol_t protocol, GArray *key_types)
| Encrypt a stream for a PGP public key, writing to another stream.
int | gvm_pgp_pubkey_encrypt_stream (FILE *plain_file, FILE *encrypted_file, const char *uid_email, const char *public_key_str, ssize_t public_key_len)
| Encrypt a stream for a PGP public key, writing to another stream.
int | gvm_smime_encrypt_stream (FILE *plain_file, FILE *encrypted_file, const char *uid_email, const char *certificate_str, ssize_t certificate_len)
| Encrypt a stream for a S/MIME certificate, writing to another stream.

## Detailed Description

GPGME utilities.

## Macro Definition Documentation

## ◆ CHECK_ERR [1/2]

#define CHECK_ERR | ( |  | func | ) |

Value:
if (err) \
{ \
printf ("%s: %s failed: %s\n", __func__, func, gpgme_strerror (err)); \
if (plain_data) \
gpgme_data_release (plain_data); \
if (encrypted_data) \
gpgme_data_release (encrypted_data); \
if (ctx) \
gpgme_release (ctx); \
gvm_file_remove_recurse (gpg_temp_dir); \
return -1; \
}

## ◆ CHECK_ERR [2/2]

#define CHECK_ERR | ( |  | func | ) |

Value:
if (err) \
{ \
printf ("%s: %s failed: %s\n", __func__, func, gpgme_strerror (err)); \
return -1; \
}

## ◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN "libgvm util"

GLib logging domain.

## Function Documentation

## ◆ create_all_certificates_trustlist()

int create_all_certificates_trustlist | ( | gpgme_ctx_t | ctx,
|  | const char * | homedir )
| static

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.

Parameters

  [in] | ctx | The GPGME context to get the keys from.
  [in] | homedir | The directory to write the trust list file to.

Returns
  0 success, -1 error.

## ◆ encrypt_stream_internal()

int encrypt_stream_internal | ( | FILE * | plain_file,
|  | FILE * | encrypted_file,
|  | const char * | key_str,
|  | ssize_t | key_len,
|  | const char * | uid_email,
|  | gpgme_protocol_t | protocol,
|  | GArray * | key_types )
| static

Encrypt a stream for a PGP public key, writing to another stream.

The output will use ASCII armor mode and no compression.

Parameters

  [in] | plain_file | Stream / FILE* providing the plain text.
  [in] | encrypted_file | Stream to write the encrypted text to.
  [in] | key_str | String containing the public key or certificate.
  [in] | key_len | Length of key / certificate, -1 to use strlen.
  [in] | uid_email | Email address of key / certificate to use.
  [in] | protocol | The protocol to use, e.g. OpenPGP or CMS.
  [in] | key_types | The expected GPGME buffered data types.

Returns
  0 success, -1 error.

## ◆ find_email_encryption_key()

gpgme_key_t find_email_encryption_key | ( | gpgme_ctx_t | ctx,
|  | const char * | uid_email )
| static

Find a key that can be used to encrypt for an email recipient.

Parameters

  [in] | ctx | The GPGME context.
  [in] | uid_email | The recipient email address to look for.

Returns
  The key as a gpgme_key_t.

## ◆ gvm_gpg_import_from_string()

int gvm_gpg_import_from_string | ( | gpgme_ctx_t | ctx,
|  | const char * | key_str,
|  | ssize_t | key_len,
|  | gpgme_data_type_t | key_type )

Import a key or certificate given by a string.

Parameters

  [in] | ctx | The GPGME context to import the key / certificate into.
  [in] | key_str | Key or certificate string.
  [in] | key_len | Length of key/certificate string or -1 to use strlen.
  [in] | key_type | The expected key type.

Returns
  0 success, 1 invalid key data, 2 unexpected key data, 3 error importing key/certificate, -1 error.

## ◆ gvm_gpg_import_many_types_from_string()

int gvm_gpg_import_many_types_from_string | ( | gpgme_ctx_t | ctx,
|  | const char * | key_str,
|  | ssize_t | key_len,
|  | GArray * | key_types )

Import a key or certificate given by a string.

Parameters

  [in] | ctx | The GPGME context to import the key / certificate into.
  [in] | key_str | Key or certificate string.
  [in] | key_len | Length of key/certificate string or -1 to use strlen.
  [in] | key_types | GArray of expected key types.

Returns
  0 success, 1 invalid key data, 2 unexpected key data, 3 error importing key/certificate, -1 error.

## ◆ gvm_gpgme_fread()

ssize_t gvm_gpgme_fread | ( | void * | handle,
|  | void * | buffer,
|  | size_t | size )
| static

Wrapper for fread for use as a GPGME callback.

Parameters

  [in] | handle | The file handle.
  [out] | buffer | The data buffer to read data into.
  [in] | size | The size of the buffer.

Returns
  The number of bytes read or -1 on error.

## ◆ gvm_gpgme_fwrite()

ssize_t gvm_gpgme_fwrite | ( | void * | handle,
|  | const void * | buffer,
|  | size_t | size )
| static

Wrapper for fread for use as a GPGME callback.

Parameters

  [in] | handle | The file handle.
  [in] | buffer | The data buffer to read data into.
  [in] | size | The amount of buffered data.

Returns
  The number of bytes written or -1 on error.

## ◆ gvm_init_gpgme_ctx_from_dir()

gpgme_ctx_t gvm_init_gpgme_ctx_from_dir | ( | const gchar * | dir | ) |

Returns a new gpgme context.

Inits a gpgme context with the custom gpg directory, protocol version etc. Returns the context or NULL if an error occurred. This function also does an gpgme initialization the first time it is called.

Parameters

  dir | Directory to use for gpg

Returns
  The gpgme_ctx_t to the context or NULL if an error occurred.

## ◆ gvm_pgp_pubkey_encrypt_stream()

int gvm_pgp_pubkey_encrypt_stream | ( | FILE * | plain_file,
|  | FILE * | encrypted_file,
|  | const char * | uid_email,
|  | const char * | public_key_str,
|  | ssize_t | public_key_len )

Encrypt a stream for a PGP public key, writing to another stream.

The output will use ASCII armor mode and no compression.

Parameters

  [in] | plain_file | Stream / FILE* providing the plain text.
  [in] | encrypted_file | Stream to write the encrypted text to.
  [in] | uid_email | Email address of public key to use.
  [in] | public_key_str | String containing the public key.
  [in] | public_key_len | Length of public key or -1 to use strlen.

Returns
  0 success, -1 error.

## ◆ gvm_smime_encrypt_stream()

int gvm_smime_encrypt_stream | ( | FILE * | plain_file,
|  | FILE * | encrypted_file,
|  | const char * | uid_email,
|  | const char * | certificate_str,
|  | ssize_t | certificate_len )

Encrypt a stream for a S/MIME certificate, writing to another stream.

The output will use ASCII armor mode and no compression.

Parameters

  [in] | plain_file | Stream / FILE* providing the plain text.
  [in] | encrypted_file | Stream to write the encrypted text to.
  [in] | uid_email | Email address of certificate to use.
  [in] | certificate_str | String containing the public key.
  [in] | certificate_len | Length of public key or -1 to use strlen.

Returns
  0 success, -1 error.

## ◆ log_gpgme()

void log_gpgme | ( | GLogLevelFlags | level,
|  | gpg_error_t | err,
|  | const char * | fmt,
|  |  | ... )

Log function with extra gpg-error style output.

If `err` is not 0, the appropriate error string is appended to the output. It takes care to only add the error source string if it makes sense.

Parameters

  level | The GLib style log level
  err | An gpg-error value or 0
  fmt | The printf style format string, followed by its arguments.