Protos and data structures for GPGME utilities.
More...
#include <glib.h>
#include <gpgme.h>
Go to the source code of this file.
|
| void | log_gpgme (GLogLevelFlags, gpg_error_t, const char *,...) |
| | Log function with extra gpg-error style output. More...
|
| |
| gpgme_ctx_t | gvm_init_gpgme_ctx_from_dir (const gchar *) |
| | Returns a new gpgme context. More...
|
| |
| int | gvm_gpg_import_from_string (gpgme_ctx_t, const char *, ssize_t, gpgme_data_type_t) |
| | Import a key or certificate given by a string. More...
|
| |
| int | gvm_pgp_pubkey_encrypt_stream (FILE *, FILE *, const char *, const char *, ssize_t) |
| | Encrypt a stream for a PGP public key, writing to another stream. More...
|
| |
| int | gvm_smime_encrypt_stream (FILE *, FILE *, const char *, const char *, ssize_t) |
| | Encrypt a stream for a S/MIME certificate, writing to another stream. More...
|
| |
Protos and data structures for GPGME utilities.
This file contains the protos for gpgmeutils.c
Definition in file gpgmeutils.h.
◆ 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.
Definition at line 180 of file gpgmeutils.c.
183 gpgme_data_t key_data;
185 gpgme_data_type_t given_key_type;
186 gpgme_import_result_t import_result;
188 gpgme_data_new_from_mem (
189 &key_data, key_str, (key_len >= 0 ? key_len : (ssize_t) strlen (key_str)),
192 given_key_type = gpgme_data_identify (key_data, 0);
193 if (given_key_type != key_type)
196 if (given_key_type == GPGME_DATA_TYPE_INVALID)
199 g_warning (
"%s: key_str is invalid", __FUNCTION__);
204 g_warning (
"%s: key_str is not the expected type: " 205 " expected: %d, got %d",
206 __FUNCTION__, key_type, given_key_type);
208 gpgme_data_release (key_data);
212 err = gpgme_op_import (ctx, key_data);
213 gpgme_data_release (key_data);
216 g_warning (
"%s: Import failed: %s", __FUNCTION__, gpgme_strerror (err));
220 import_result = gpgme_op_import_result (ctx);
221 g_debug (
"%s: %d imported, %d not imported", __FUNCTION__,
222 import_result->imported, import_result->not_imported);
224 gpgme_import_status_t status;
225 status = import_result->imports;
228 if (status->result != GPG_ERR_NO_ERROR)
229 g_warning (
"%s: '%s' could not be imported: %s", __FUNCTION__,
230 status->fpr, gpgme_strerror (status->result));
232 g_debug (
"%s: Imported '%s'", __FUNCTION__, status->fpr);
234 status = status->next;
237 if (import_result->not_imported)
Referenced by encrypt_stream_internal().
◆ 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.
Definition at line 88 of file gpgmeutils.c.
102 gpgme_engine_info_t info;
104 if (!gpgme_check_version (NULL))
106 g_critical (
"gpgme library could not be initialized.");
109 gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL));
111 gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL));
115 g_message (
"Setting GnuPG dir to '%s'", dir);
118 if (access (dir, F_OK))
120 err = gpg_error_from_syserror ();
124 if (mkdir (dir, 0700) == 0)
127 g_message (
"Created GnuPG dir '%s'", dir);
134 err = gpgme_set_engine_info (GPGME_PROTOCOL_OpenPGP, NULL, dir);
138 log_gpgme (G_LOG_LEVEL_WARNING, err,
"Setting GnuPG dir failed");
143 if (!gpgme_get_engine_info (&info))
145 while (info && info->protocol != GPGME_PROTOCOL_OpenPGP)
151 g_message (
"Using OpenPGP engine version '%s'",
152 info && info->version ? info->version :
"[?]");
161 err = gpgme_new (&ctx);
163 log_gpgme (G_LOG_LEVEL_WARNING, err,
"Creating GPGME context failed");
static gboolean initialized
Flag whether the config file was read.
void log_gpgme(GLogLevelFlags level, gpg_error_t err, const char *fmt,...)
Log function with extra gpg-error style output.
References initialized, and log_gpgme().
◆ 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.
Definition at line 431 of file gpgmeutils.c.
437 plain_file, encrypted_file, public_key_str, public_key_len, uid_email,
438 GPGME_PROTOCOL_OpenPGP, GPGME_DATA_TYPE_PGP_KEY);
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, gpgme_data_type_t data_type)
Encrypt a stream for a PGP public key, writing to another stream.
References encrypt_stream_internal().
◆ 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.
Definition at line 455 of file gpgmeutils.c.
460 plain_file, encrypted_file, certificate_str, certificate_len, uid_email,
461 GPGME_PROTOCOL_CMS, GPGME_DATA_TYPE_CMS_OTHER);
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, gpgme_data_type_t data_type)
Encrypt a stream for a PGP public key, writing to another stream.
References encrypt_stream_internal().
◆ 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. |
Definition at line 57 of file gpgmeutils.c.
62 va_start (arg_ptr, fmt);
63 msg = g_strdup_vprintf (fmt, arg_ptr);
65 if (err && gpg_err_source (err) != GPG_ERR_SOURCE_ANY && gpg_err_source (err))
66 g_log (
G_LOG_DOMAIN, level,
"%s: %s <%s>", msg, gpg_strerror (err),
69 g_log (
G_LOG_DOMAIN, level,
"%s: %s", msg, gpg_strerror (err));
#define G_LOG_DOMAIN
GLib log domain.
References G_LOG_DOMAIN.
Referenced by gvm_init_gpgme_ctx_from_dir().