2015-03-21 20:12:14 -04:00
|
|
|
/*
|
|
|
|
* gpg.c
|
|
|
|
*
|
2016-02-14 17:54:46 -05:00
|
|
|
* Copyright (C) 2012 - 2016 James Booth <boothj5@gmail.com>
|
2015-03-21 20:12:14 -04:00
|
|
|
*
|
|
|
|
* This file is part of Profanity.
|
|
|
|
*
|
|
|
|
* Profanity is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Profanity is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-07-23 20:14:49 -04:00
|
|
|
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
|
2015-03-21 20:12:14 -04:00
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link the code of portions of this program with the OpenSSL library under
|
|
|
|
* certain conditions as described in each individual source file, and
|
|
|
|
* distribute linked combinations including the two.
|
|
|
|
*
|
|
|
|
* You must obey the GNU General Public License in all respects for all of the
|
|
|
|
* code used other than OpenSSL. If you modify file(s) with this exception, you
|
|
|
|
* may extend this exception to your version of the file(s), but you are not
|
|
|
|
* obligated to do so. If you do not wish to do so, delete this exception
|
|
|
|
* statement from your version. If you delete this exception statement from all
|
|
|
|
* source files in the program, then also delete it here.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-03-31 16:05:02 -04:00
|
|
|
#include "config.h"
|
2015-06-24 10:21:28 -04:00
|
|
|
|
2015-03-21 20:12:14 -04:00
|
|
|
#include <locale.h>
|
|
|
|
#include <string.h>
|
2015-03-21 21:03:06 -04:00
|
|
|
#include <stdlib.h>
|
2015-06-23 18:29:10 -04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/stat.h>
|
2015-03-21 20:12:14 -04:00
|
|
|
|
2015-03-21 21:03:06 -04:00
|
|
|
#include <glib.h>
|
2015-06-23 18:29:10 -04:00
|
|
|
#include <glib/gstdio.h>
|
2015-03-21 20:12:14 -04:00
|
|
|
#include <gpgme.h>
|
|
|
|
|
|
|
|
#include "log.h"
|
2015-06-23 18:29:10 -04:00
|
|
|
#include "common.h"
|
2016-07-24 10:43:51 -04:00
|
|
|
#include "pgp/gpg.h"
|
2016-07-24 11:22:15 -04:00
|
|
|
#include "config/files.h"
|
2015-08-25 18:44:03 -04:00
|
|
|
#include "tools/autocomplete.h"
|
2015-09-01 14:24:56 -04:00
|
|
|
#include "ui/ui.h"
|
2015-03-21 20:12:14 -04:00
|
|
|
|
2015-03-24 19:34:24 -04:00
|
|
|
#define PGP_SIGNATURE_HEADER "-----BEGIN PGP SIGNATURE-----"
|
|
|
|
#define PGP_SIGNATURE_FOOTER "-----END PGP SIGNATURE-----"
|
2015-03-24 20:05:32 -04:00
|
|
|
#define PGP_MESSAGE_HEADER "-----BEGIN PGP MESSAGE-----"
|
2015-03-24 19:34:24 -04:00
|
|
|
#define PGP_MESSAGE_FOOTER "-----END PGP MESSAGE-----"
|
2015-03-23 19:38:06 -04:00
|
|
|
|
2015-03-21 20:29:57 -04:00
|
|
|
static const char *libversion;
|
2015-08-25 17:45:51 -04:00
|
|
|
static GHashTable *pubkeys;
|
2015-03-21 20:29:57 -04:00
|
|
|
|
2015-08-25 17:45:51 -04:00
|
|
|
static gchar *pubsloc;
|
|
|
|
static GKeyFile *pubkeyfile;
|
2015-06-23 18:29:10 -04:00
|
|
|
|
2015-09-01 16:05:17 -04:00
|
|
|
static char *passphrase;
|
|
|
|
static char *passphrase_attempt;
|
|
|
|
|
2015-08-25 18:44:03 -04:00
|
|
|
static Autocomplete key_ac;
|
|
|
|
|
2015-10-25 18:43:19 -04:00
|
|
|
static char* _remove_header_footer(char *str, const char *const footer);
|
|
|
|
static char* _add_header_footer(const char *const str, const char *const header, const char *const footer);
|
2015-08-25 17:45:51 -04:00
|
|
|
static void _save_pubkeys(void);
|
2015-03-23 19:38:06 -04:00
|
|
|
|
2015-08-25 18:04:21 -04:00
|
|
|
void
|
|
|
|
_p_gpg_free_pubkeyid(ProfPGPPubKeyId *pubkeyid)
|
|
|
|
{
|
|
|
|
if (pubkeyid) {
|
|
|
|
free(pubkeyid->id);
|
|
|
|
}
|
|
|
|
free(pubkeyid);
|
|
|
|
}
|
|
|
|
|
2015-10-25 18:43:19 -04:00
|
|
|
static gpgme_error_t*
|
2015-09-01 14:24:56 -04:00
|
|
|
_p_gpg_passphrase_cb(void *hook, const char *uid_hint, const char *passphrase_info, int prev_was_bad, int fd)
|
|
|
|
{
|
2015-09-01 16:05:17 -04:00
|
|
|
if (passphrase) {
|
2015-09-01 16:52:11 -04:00
|
|
|
gpgme_io_write(fd, passphrase, strlen(passphrase));
|
2015-09-01 16:05:17 -04:00
|
|
|
} else {
|
|
|
|
GString *pass_term = g_string_new("");
|
2015-09-01 15:16:04 -04:00
|
|
|
|
2015-09-01 16:05:17 -04:00
|
|
|
char *password = ui_ask_pgp_passphrase(uid_hint, prev_was_bad);
|
|
|
|
if (password) {
|
|
|
|
g_string_append(pass_term, password);
|
|
|
|
free(password);
|
|
|
|
}
|
2015-09-01 15:16:04 -04:00
|
|
|
|
2015-09-01 16:05:17 -04:00
|
|
|
g_string_append(pass_term, "\n");
|
|
|
|
if (passphrase_attempt) {
|
|
|
|
free(passphrase_attempt);
|
|
|
|
}
|
|
|
|
passphrase_attempt = pass_term->str;
|
|
|
|
g_string_free(pass_term, FALSE);
|
2015-09-01 15:16:04 -04:00
|
|
|
|
2015-09-01 16:40:07 -04:00
|
|
|
gpgme_io_write(fd, passphrase_attempt, strlen(passphrase_attempt));
|
2015-09-01 16:05:17 -04:00
|
|
|
}
|
2015-09-01 15:16:04 -04:00
|
|
|
|
2015-09-01 14:24:56 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-21 20:12:14 -04:00
|
|
|
void
|
|
|
|
p_gpg_init(void)
|
|
|
|
{
|
2015-03-21 20:29:57 -04:00
|
|
|
libversion = gpgme_check_version(NULL);
|
|
|
|
log_debug("GPG: Found gpgme version: %s", libversion);
|
2015-03-21 20:12:14 -04:00
|
|
|
gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
|
2015-03-23 20:47:13 -04:00
|
|
|
|
2015-08-25 18:04:21 -04:00
|
|
|
pubkeys = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)_p_gpg_free_pubkeyid);
|
2015-08-25 18:44:03 -04:00
|
|
|
|
|
|
|
key_ac = autocomplete_new();
|
|
|
|
GHashTable *keys = p_gpg_list_keys();
|
|
|
|
p_gpg_free_keys(keys);
|
2015-09-01 16:05:17 -04:00
|
|
|
|
|
|
|
passphrase = NULL;
|
|
|
|
passphrase_attempt = NULL;
|
2015-03-23 20:47:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
p_gpg_close(void)
|
|
|
|
{
|
2015-08-25 17:45:51 -04:00
|
|
|
if (pubkeys) {
|
|
|
|
g_hash_table_destroy(pubkeys);
|
|
|
|
pubkeys = NULL;
|
2015-06-24 09:27:40 -04:00
|
|
|
}
|
2015-06-23 18:29:10 -04:00
|
|
|
|
2015-08-25 17:45:51 -04:00
|
|
|
if (pubkeyfile) {
|
|
|
|
g_key_file_free(pubkeyfile);
|
|
|
|
pubkeyfile = NULL;
|
2015-06-24 09:27:40 -04:00
|
|
|
}
|
2015-06-23 18:29:10 -04:00
|
|
|
|
2015-08-25 17:45:51 -04:00
|
|
|
free(pubsloc);
|
|
|
|
pubsloc = NULL;
|
2015-08-25 18:44:03 -04:00
|
|
|
|
|
|
|
autocomplete_free(key_ac);
|
|
|
|
key_ac = NULL;
|
2015-09-01 16:05:17 -04:00
|
|
|
|
|
|
|
if (passphrase) {
|
|
|
|
free(passphrase);
|
|
|
|
passphrase = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (passphrase_attempt) {
|
|
|
|
free(passphrase_attempt);
|
|
|
|
passphrase_attempt = NULL;
|
|
|
|
}
|
2015-06-23 18:29:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-10-25 18:43:19 -04:00
|
|
|
p_gpg_on_connect(const char *const barejid)
|
2015-06-23 18:29:10 -04:00
|
|
|
{
|
2016-07-24 16:35:12 -04:00
|
|
|
char *pgpdir = files_get_data_path(DIR_PGP);
|
|
|
|
GString *pubsfile = g_string_new(pgpdir);
|
2016-07-24 20:03:44 -04:00
|
|
|
free(pgpdir);
|
2015-06-23 18:29:10 -04:00
|
|
|
gchar *account_dir = str_replace(barejid, "@", "_at_");
|
2016-07-24 16:35:12 -04:00
|
|
|
g_string_append(pubsfile, "/");
|
2015-08-25 17:45:51 -04:00
|
|
|
g_string_append(pubsfile, account_dir);
|
2015-06-23 18:29:10 -04:00
|
|
|
free(account_dir);
|
|
|
|
|
|
|
|
// mkdir if doesn't exist for account
|
|
|
|
errno = 0;
|
2015-08-25 17:45:51 -04:00
|
|
|
int res = g_mkdir_with_parents(pubsfile->str, S_IRWXU);
|
2015-06-23 18:29:10 -04:00
|
|
|
if (res == -1) {
|
|
|
|
char *errmsg = strerror(errno);
|
|
|
|
if (errmsg) {
|
2015-08-25 17:45:51 -04:00
|
|
|
log_error("Error creating directory: %s, %s", pubsfile->str, errmsg);
|
2015-06-23 18:29:10 -04:00
|
|
|
} else {
|
2015-08-25 17:45:51 -04:00
|
|
|
log_error("Error creating directory: %s", pubsfile->str);
|
2015-06-23 18:29:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-25 17:45:51 -04:00
|
|
|
// create or read publickeys
|
|
|
|
g_string_append(pubsfile, "/pubkeys");
|
|
|
|
pubsloc = pubsfile->str;
|
|
|
|
g_string_free(pubsfile, FALSE);
|
2015-06-23 18:29:10 -04:00
|
|
|
|
2015-08-25 17:45:51 -04:00
|
|
|
if (g_file_test(pubsloc, G_FILE_TEST_EXISTS)) {
|
|
|
|
g_chmod(pubsloc, S_IRUSR | S_IWUSR);
|
2015-06-23 18:29:10 -04:00
|
|
|
}
|
|
|
|
|
2015-08-25 17:45:51 -04:00
|
|
|
pubkeyfile = g_key_file_new();
|
|
|
|
g_key_file_load_from_file(pubkeyfile, pubsloc, G_KEY_FILE_KEEP_COMMENTS, NULL);
|
2015-06-23 18:29:10 -04:00
|
|
|
|
|
|
|
// load each keyid
|
|
|
|
gsize len = 0;
|
2015-08-25 17:45:51 -04:00
|
|
|
gchar **jids = g_key_file_get_groups(pubkeyfile, &len);
|
2015-06-23 18:29:10 -04:00
|
|
|
|
|
|
|
gpgme_ctx_t ctx;
|
|
|
|
gpgme_error_t error = gpgme_new(&ctx);
|
2015-08-10 20:00:23 -04:00
|
|
|
|
2015-06-23 18:29:10 -04:00
|
|
|
if (error) {
|
|
|
|
log_error("GPG: Failed to create gpgme context. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
|
|
|
g_strfreev(jids);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
GError *gerr = NULL;
|
|
|
|
gchar *jid = jids[i];
|
2015-08-25 17:45:51 -04:00
|
|
|
gchar *keyid = g_key_file_get_string(pubkeyfile, jid, "keyid", &gerr);
|
2015-06-23 18:29:10 -04:00
|
|
|
if (gerr) {
|
|
|
|
log_error("Error loading PGP key id for %s", jid);
|
|
|
|
g_error_free(gerr);
|
2015-08-10 17:18:19 -04:00
|
|
|
g_free(keyid);
|
2015-06-23 18:29:10 -04:00
|
|
|
} else {
|
|
|
|
gpgme_key_t key = NULL;
|
2015-08-25 17:45:51 -04:00
|
|
|
error = gpgme_get_key(ctx, keyid, &key, 0);
|
2015-06-23 18:29:10 -04:00
|
|
|
if (error || key == NULL) {
|
2015-08-23 18:09:44 -04:00
|
|
|
log_warning("GPG: Failed to get key for %s: %s %s", jid, gpgme_strsource(error), gpgme_strerror(error));
|
2015-06-23 18:29:10 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-25 18:04:21 -04:00
|
|
|
ProfPGPPubKeyId *pubkeyid = malloc(sizeof(ProfPGPPubKeyId));
|
|
|
|
pubkeyid->id = strdup(keyid);
|
|
|
|
pubkeyid->received = FALSE;
|
|
|
|
g_hash_table_replace(pubkeys, strdup(jid), pubkeyid);
|
2015-08-25 17:45:51 -04:00
|
|
|
g_free(keyid);
|
2015-08-10 20:00:23 -04:00
|
|
|
gpgme_key_unref(key);
|
2015-06-23 18:29:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gpgme_release(ctx);
|
|
|
|
g_strfreev(jids);
|
|
|
|
|
2015-08-25 17:45:51 -04:00
|
|
|
_save_pubkeys();
|
2015-06-23 18:29:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
p_gpg_on_disconnect(void)
|
|
|
|
{
|
2015-08-25 17:45:51 -04:00
|
|
|
if (pubkeys) {
|
|
|
|
g_hash_table_destroy(pubkeys);
|
2015-08-25 18:04:21 -04:00
|
|
|
pubkeys = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)_p_gpg_free_pubkeyid);
|
2015-06-23 18:29:10 -04:00
|
|
|
}
|
|
|
|
|
2015-08-25 17:45:51 -04:00
|
|
|
if (pubkeyfile) {
|
|
|
|
g_key_file_free(pubkeyfile);
|
|
|
|
pubkeyfile = NULL;
|
2015-06-23 18:29:10 -04:00
|
|
|
}
|
|
|
|
|
2015-08-25 17:45:51 -04:00
|
|
|
free(pubsloc);
|
|
|
|
pubsloc = NULL;
|
2015-09-01 16:05:17 -04:00
|
|
|
|
|
|
|
if (passphrase) {
|
|
|
|
free(passphrase);
|
|
|
|
passphrase = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (passphrase_attempt) {
|
|
|
|
free(passphrase_attempt);
|
|
|
|
passphrase_attempt = NULL;
|
|
|
|
}
|
2015-06-23 18:29:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2015-10-25 18:43:19 -04:00
|
|
|
p_gpg_addkey(const char *const jid, const char *const keyid)
|
2015-06-23 18:29:10 -04:00
|
|
|
{
|
|
|
|
gpgme_ctx_t ctx;
|
|
|
|
gpgme_error_t error = gpgme_new(&ctx);
|
|
|
|
if (error) {
|
|
|
|
log_error("GPG: Failed to create gpgme context. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpgme_key_t key = NULL;
|
2015-08-25 16:58:24 -04:00
|
|
|
error = gpgme_get_key(ctx, keyid, &key, 0);
|
2015-08-10 20:00:23 -04:00
|
|
|
gpgme_release(ctx);
|
|
|
|
|
2015-06-23 18:29:10 -04:00
|
|
|
if (error || key == NULL) {
|
|
|
|
log_error("GPG: Failed to get key. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-08-25 17:45:51 -04:00
|
|
|
// save to public key file
|
|
|
|
g_key_file_set_string(pubkeyfile, jid, "keyid", keyid);
|
|
|
|
_save_pubkeys();
|
2015-06-23 18:29:10 -04:00
|
|
|
|
2015-08-25 17:45:51 -04:00
|
|
|
// update in memory pubkeys list
|
2015-08-25 18:04:21 -04:00
|
|
|
ProfPGPPubKeyId *pubkeyid = malloc(sizeof(ProfPGPPubKeyId));
|
|
|
|
pubkeyid->id = strdup(keyid);
|
|
|
|
pubkeyid->received = FALSE;
|
|
|
|
g_hash_table_replace(pubkeys, strdup(jid), pubkeyid);
|
2015-08-10 20:00:23 -04:00
|
|
|
gpgme_key_unref(key);
|
2015-06-23 18:29:10 -04:00
|
|
|
|
|
|
|
return TRUE;
|
2015-03-21 20:12:14 -04:00
|
|
|
}
|
|
|
|
|
2015-10-25 18:43:19 -04:00
|
|
|
static ProfPGPKey*
|
2015-08-24 19:21:49 -04:00
|
|
|
_p_gpg_key_new(void)
|
|
|
|
{
|
|
|
|
ProfPGPKey *p_pgpkey = malloc(sizeof(ProfPGPKey));
|
|
|
|
p_pgpkey->id = NULL;
|
|
|
|
p_pgpkey->name = NULL;
|
|
|
|
p_pgpkey->fp = NULL;
|
|
|
|
p_pgpkey->encrypt = FALSE;
|
|
|
|
p_pgpkey->sign = FALSE;
|
|
|
|
p_pgpkey->certify = FALSE;
|
|
|
|
p_pgpkey->authenticate = FALSE;
|
|
|
|
p_pgpkey->secret = FALSE;
|
|
|
|
|
|
|
|
return p_pgpkey;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_p_gpg_free_key(ProfPGPKey *key)
|
|
|
|
{
|
|
|
|
if (key) {
|
|
|
|
free(key->id);
|
|
|
|
free(key->name);
|
|
|
|
free(key->fp);
|
|
|
|
free(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-25 18:43:19 -04:00
|
|
|
GHashTable*
|
2015-03-21 20:12:14 -04:00
|
|
|
p_gpg_list_keys(void)
|
|
|
|
{
|
|
|
|
gpgme_error_t error;
|
2015-08-24 19:21:49 -04:00
|
|
|
GHashTable *result = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)_p_gpg_free_key);
|
2015-03-21 20:12:14 -04:00
|
|
|
|
2015-08-10 20:00:23 -04:00
|
|
|
gpgme_ctx_t ctx;
|
2015-03-21 20:12:14 -04:00
|
|
|
error = gpgme_new(&ctx);
|
2015-08-10 20:00:23 -04:00
|
|
|
|
2015-03-21 20:12:14 -04:00
|
|
|
if (error) {
|
2015-03-23 19:38:06 -04:00
|
|
|
log_error("GPG: Could not list keys. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
2015-03-21 20:12:14 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-24 17:27:15 -04:00
|
|
|
error = gpgme_op_keylist_start(ctx, NULL, 0);
|
2015-03-21 20:12:14 -04:00
|
|
|
if (error == GPG_ERR_NO_ERROR) {
|
2015-08-24 19:21:49 -04:00
|
|
|
gpgme_key_t key;
|
|
|
|
error = gpgme_op_keylist_next(ctx, &key);
|
2015-03-21 20:12:14 -04:00
|
|
|
while (!error) {
|
2015-08-24 19:21:49 -04:00
|
|
|
gpgme_subkey_t sub = key->subkeys;
|
2015-08-10 20:00:23 -04:00
|
|
|
|
2015-08-24 19:21:49 -04:00
|
|
|
ProfPGPKey *p_pgpkey = _p_gpg_key_new();
|
|
|
|
p_pgpkey->id = strdup(sub->keyid);
|
|
|
|
p_pgpkey->name = strdup(key->uids->uid);
|
|
|
|
p_pgpkey->fp = strdup(sub->fpr);
|
|
|
|
if (sub->can_encrypt) p_pgpkey->encrypt = TRUE;
|
|
|
|
if (sub->can_authenticate) p_pgpkey->authenticate = TRUE;
|
|
|
|
if (sub->can_certify) p_pgpkey->certify = TRUE;
|
|
|
|
if (sub->can_sign) p_pgpkey->sign = TRUE;
|
|
|
|
|
|
|
|
sub = sub->next;
|
|
|
|
while (sub) {
|
|
|
|
if (sub->can_encrypt) p_pgpkey->encrypt = TRUE;
|
|
|
|
if (sub->can_authenticate) p_pgpkey->authenticate = TRUE;
|
|
|
|
if (sub->can_certify) p_pgpkey->certify = TRUE;
|
|
|
|
if (sub->can_sign) p_pgpkey->sign = TRUE;
|
|
|
|
|
|
|
|
sub = sub->next;
|
2015-03-21 20:12:14 -04:00
|
|
|
}
|
2015-03-21 21:03:06 -04:00
|
|
|
|
2015-08-24 19:21:49 -04:00
|
|
|
g_hash_table_insert(result, strdup(p_pgpkey->name), p_pgpkey);
|
2015-03-21 21:03:06 -04:00
|
|
|
|
2015-08-24 19:21:49 -04:00
|
|
|
gpgme_key_unref(key);
|
|
|
|
error = gpgme_op_keylist_next(ctx, &key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
error = gpgme_op_keylist_start(ctx, NULL, 1);
|
|
|
|
if (error == GPG_ERR_NO_ERROR) {
|
|
|
|
gpgme_key_t key;
|
|
|
|
error = gpgme_op_keylist_next(ctx, &key);
|
|
|
|
while (!error) {
|
|
|
|
gpgme_subkey_t sub = key->subkeys;
|
|
|
|
while (sub) {
|
|
|
|
if (sub->secret) {
|
|
|
|
ProfPGPKey *p_pgpkey = g_hash_table_lookup(result, key->uids->uid);
|
|
|
|
if (p_pgpkey) {
|
|
|
|
p_pgpkey->secret = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sub = sub->next;
|
|
|
|
}
|
2015-03-21 21:03:06 -04:00
|
|
|
|
2015-08-10 20:00:23 -04:00
|
|
|
gpgme_key_unref(key);
|
2015-08-24 19:21:49 -04:00
|
|
|
error = gpgme_op_keylist_next(ctx, &key);
|
2015-03-21 20:12:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gpgme_release(ctx);
|
|
|
|
|
2015-08-25 18:44:03 -04:00
|
|
|
autocomplete_clear(key_ac);
|
|
|
|
GList *ids = g_hash_table_get_keys(result);
|
|
|
|
GList *curr = ids;
|
|
|
|
while (curr) {
|
|
|
|
ProfPGPKey *key = g_hash_table_lookup(result, curr->data);
|
|
|
|
autocomplete_add(key_ac, key->id);
|
|
|
|
curr = curr->next;
|
|
|
|
}
|
|
|
|
g_list_free(ids);
|
|
|
|
|
2015-03-21 20:12:14 -04:00
|
|
|
return result;
|
|
|
|
}
|
2015-03-21 20:29:57 -04:00
|
|
|
|
2015-08-24 19:21:49 -04:00
|
|
|
void
|
|
|
|
p_gpg_free_keys(GHashTable *keys)
|
|
|
|
{
|
|
|
|
g_hash_table_destroy(keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-25 18:43:19 -04:00
|
|
|
GHashTable*
|
2015-08-25 17:45:51 -04:00
|
|
|
p_gpg_pubkeys(void)
|
2015-03-23 20:47:13 -04:00
|
|
|
{
|
2015-08-25 17:45:51 -04:00
|
|
|
return pubkeys;
|
2015-03-23 20:47:13 -04:00
|
|
|
}
|
|
|
|
|
2015-03-21 20:29:57 -04:00
|
|
|
const char*
|
|
|
|
p_gpg_libver(void)
|
|
|
|
{
|
|
|
|
return libversion;
|
|
|
|
}
|
|
|
|
|
2015-08-23 17:54:41 -04:00
|
|
|
gboolean
|
2015-12-14 19:38:16 -05:00
|
|
|
p_gpg_valid_key(const char *const keyid, char **err_str)
|
2015-08-23 17:54:41 -04:00
|
|
|
{
|
|
|
|
gpgme_ctx_t ctx;
|
|
|
|
gpgme_error_t error = gpgme_new(&ctx);
|
|
|
|
if (error) {
|
|
|
|
log_error("GPG: Failed to create gpgme context. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
2015-12-14 19:38:16 -05:00
|
|
|
*err_str = strdup(gpgme_strerror(error));
|
2015-08-23 17:54:41 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpgme_key_t key = NULL;
|
|
|
|
error = gpgme_get_key(ctx, keyid, &key, 1);
|
|
|
|
|
|
|
|
if (error || key == NULL) {
|
|
|
|
log_error("GPG: Failed to get key. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
2015-12-14 19:38:16 -05:00
|
|
|
*err_str = strdup(gpgme_strerror(error));
|
2015-08-23 17:54:41 -04:00
|
|
|
gpgme_release(ctx);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-12-14 19:38:16 -05:00
|
|
|
if (key == NULL) {
|
|
|
|
*err_str = strdup("Unknown error");
|
2015-08-23 17:54:41 -04:00
|
|
|
gpgme_release(ctx);
|
2015-12-14 19:38:16 -05:00
|
|
|
return FALSE;
|
2015-08-23 17:54:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
gpgme_release(ctx);
|
2015-12-14 19:38:16 -05:00
|
|
|
gpgme_key_unref(key);
|
|
|
|
return TRUE;
|
|
|
|
|
2015-08-23 17:54:41 -04:00
|
|
|
}
|
|
|
|
|
2015-06-20 18:49:24 -04:00
|
|
|
gboolean
|
2015-10-25 18:43:19 -04:00
|
|
|
p_gpg_available(const char *const barejid)
|
2015-06-20 18:49:24 -04:00
|
|
|
{
|
2015-08-25 17:45:51 -04:00
|
|
|
char *pubkey = g_hash_table_lookup(pubkeys, barejid);
|
|
|
|
return (pubkey != NULL);
|
2015-06-20 18:49:24 -04:00
|
|
|
}
|
|
|
|
|
2015-03-23 20:47:13 -04:00
|
|
|
void
|
2015-10-25 18:43:19 -04:00
|
|
|
p_gpg_verify(const char *const barejid, const char *const sign)
|
2015-03-23 20:47:13 -04:00
|
|
|
{
|
|
|
|
if (!sign) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpgme_ctx_t ctx;
|
|
|
|
gpgme_error_t error = gpgme_new(&ctx);
|
2015-08-10 20:00:23 -04:00
|
|
|
|
2015-03-23 20:47:13 -04:00
|
|
|
if (error) {
|
|
|
|
log_error("GPG: Failed to create gpgme context. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-24 20:05:32 -04:00
|
|
|
char *sign_with_header_footer = _add_header_footer(sign, PGP_SIGNATURE_HEADER, PGP_SIGNATURE_FOOTER);
|
2015-08-10 20:00:23 -04:00
|
|
|
gpgme_data_t sign_data;
|
2015-03-23 20:47:13 -04:00
|
|
|
gpgme_data_new_from_mem(&sign_data, sign_with_header_footer, strlen(sign_with_header_footer), 1);
|
2015-08-10 20:00:23 -04:00
|
|
|
free(sign_with_header_footer);
|
|
|
|
|
|
|
|
gpgme_data_t plain_data;
|
2015-03-23 20:47:13 -04:00
|
|
|
gpgme_data_new(&plain_data);
|
|
|
|
|
|
|
|
error = gpgme_op_verify(ctx, sign_data, NULL, plain_data);
|
2015-08-10 20:00:23 -04:00
|
|
|
gpgme_data_release(sign_data);
|
|
|
|
gpgme_data_release(plain_data);
|
|
|
|
|
2015-03-23 20:47:13 -04:00
|
|
|
if (error) {
|
|
|
|
log_error("GPG: Failed to verify. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
|
|
|
gpgme_release(ctx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpgme_verify_result_t result = gpgme_op_verify_result(ctx);
|
|
|
|
if (result) {
|
|
|
|
if (result->signatures) {
|
2015-08-25 16:58:24 -04:00
|
|
|
gpgme_key_t key = NULL;
|
|
|
|
error = gpgme_get_key(ctx, result->signatures->fpr, &key, 0);
|
|
|
|
if (error) {
|
|
|
|
log_debug("Could not find PGP key with ID %s for %s", result->signatures->fpr, barejid);
|
|
|
|
} else {
|
|
|
|
log_debug("Fingerprint found for %s: %s ", barejid, key->subkeys->fpr);
|
2015-08-25 18:04:21 -04:00
|
|
|
ProfPGPPubKeyId *pubkeyid = malloc(sizeof(ProfPGPPubKeyId));
|
|
|
|
pubkeyid->id = strdup(key->subkeys->keyid);
|
|
|
|
pubkeyid->received = TRUE;
|
|
|
|
g_hash_table_replace(pubkeys, strdup(barejid), pubkeyid);
|
2015-08-25 16:58:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
gpgme_key_unref(key);
|
2015-03-23 20:47:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-08 20:20:16 -04:00
|
|
|
gpgme_release(ctx);
|
2015-03-23 20:47:13 -04:00
|
|
|
}
|
|
|
|
|
2015-03-23 19:38:06 -04:00
|
|
|
char*
|
2015-10-25 18:43:19 -04:00
|
|
|
p_gpg_sign(const char *const str, const char *const fp)
|
2015-03-23 19:38:06 -04:00
|
|
|
{
|
2015-03-23 19:41:09 -04:00
|
|
|
gpgme_ctx_t ctx;
|
|
|
|
gpgme_error_t error = gpgme_new(&ctx);
|
|
|
|
if (error) {
|
2015-03-23 19:38:06 -04:00
|
|
|
log_error("GPG: Failed to create gpgme context. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
2015-03-23 19:41:09 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-03-23 19:38:06 -04:00
|
|
|
|
2015-09-01 14:24:56 -04:00
|
|
|
gpgme_set_passphrase_cb(ctx, (gpgme_passphrase_cb_t)_p_gpg_passphrase_cb, NULL);
|
|
|
|
|
2015-03-23 19:41:09 -04:00
|
|
|
gpgme_key_t key = NULL;
|
|
|
|
error = gpgme_get_key(ctx, fp, &key, 1);
|
2015-08-10 20:00:23 -04:00
|
|
|
|
2015-03-23 19:41:09 -04:00
|
|
|
if (error || key == NULL) {
|
2015-03-23 19:38:06 -04:00
|
|
|
log_error("GPG: Failed to get key. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
2015-08-08 19:54:11 -04:00
|
|
|
gpgme_release(ctx);
|
2015-03-23 19:41:09 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-03-23 19:38:06 -04:00
|
|
|
|
2015-03-23 19:41:09 -04:00
|
|
|
gpgme_signers_clear(ctx);
|
|
|
|
error = gpgme_signers_add(ctx, key);
|
2015-08-08 19:54:11 -04:00
|
|
|
gpgme_key_unref(key);
|
2015-08-10 20:00:23 -04:00
|
|
|
|
2015-03-23 19:41:09 -04:00
|
|
|
if (error) {
|
2015-03-23 19:38:06 -04:00
|
|
|
log_error("GPG: Failed to load signer. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
2015-03-23 19:41:09 -04:00
|
|
|
gpgme_release(ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-03-23 19:38:06 -04:00
|
|
|
|
|
|
|
char *str_or_empty = NULL;
|
|
|
|
if (str) {
|
|
|
|
str_or_empty = strdup(str);
|
|
|
|
} else {
|
|
|
|
str_or_empty = strdup("");
|
|
|
|
}
|
2015-08-10 20:00:23 -04:00
|
|
|
gpgme_data_t str_data;
|
2015-03-23 19:41:09 -04:00
|
|
|
gpgme_data_new_from_mem(&str_data, str_or_empty, strlen(str_or_empty), 1);
|
2015-08-10 20:00:23 -04:00
|
|
|
free(str_or_empty);
|
|
|
|
|
|
|
|
gpgme_data_t signed_data;
|
2015-03-23 19:41:09 -04:00
|
|
|
gpgme_data_new(&signed_data);
|
2015-03-23 19:38:06 -04:00
|
|
|
|
2015-03-23 19:41:09 -04:00
|
|
|
gpgme_set_armor(ctx,1);
|
2015-08-10 20:00:23 -04:00
|
|
|
error = gpgme_op_sign(ctx, str_data, signed_data, GPGME_SIG_MODE_DETACH);
|
|
|
|
gpgme_data_release(str_data);
|
|
|
|
gpgme_release(ctx);
|
|
|
|
|
2015-03-23 19:41:09 -04:00
|
|
|
if (error) {
|
2015-03-23 19:38:06 -04:00
|
|
|
log_error("GPG: Failed to sign string. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
2015-08-10 20:00:23 -04:00
|
|
|
gpgme_data_release(signed_data);
|
2015-03-23 19:41:09 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-03-23 19:38:06 -04:00
|
|
|
|
|
|
|
char *result = NULL;
|
|
|
|
|
|
|
|
size_t len = 0;
|
2015-03-23 19:41:09 -04:00
|
|
|
char *signed_str = gpgme_data_release_and_get_mem(signed_data, &len);
|
2015-05-04 18:41:22 -04:00
|
|
|
if (signed_str) {
|
2015-08-10 20:00:23 -04:00
|
|
|
GString *signed_gstr = g_string_new("");
|
|
|
|
g_string_append_len(signed_gstr, signed_str, len);
|
|
|
|
result = _remove_header_footer(signed_gstr->str, PGP_SIGNATURE_FOOTER);
|
|
|
|
g_string_free(signed_gstr, TRUE);
|
|
|
|
gpgme_free(signed_str);
|
2015-03-23 19:41:09 -04:00
|
|
|
}
|
2015-03-23 19:38:06 -04:00
|
|
|
|
2015-09-01 16:08:23 -04:00
|
|
|
if (passphrase_attempt) {
|
|
|
|
passphrase = strdup(passphrase_attempt);
|
|
|
|
}
|
2015-09-01 16:05:17 -04:00
|
|
|
|
2015-03-23 19:41:09 -04:00
|
|
|
return result;
|
2015-03-23 19:38:06 -04:00
|
|
|
}
|
|
|
|
|
2015-10-25 18:43:19 -04:00
|
|
|
char*
|
2016-03-29 16:24:37 -04:00
|
|
|
p_gpg_encrypt(const char *const barejid, const char *const message, const char *const fp)
|
2015-03-23 19:38:06 -04:00
|
|
|
{
|
2015-08-25 18:04:21 -04:00
|
|
|
ProfPGPPubKeyId *pubkeyid = g_hash_table_lookup(pubkeys, barejid);
|
|
|
|
if (!pubkeyid) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!pubkeyid->id) {
|
2015-03-24 19:34:24 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-29 16:30:41 -04:00
|
|
|
gpgme_key_t keys[3];
|
2015-03-24 19:34:24 -04:00
|
|
|
|
|
|
|
keys[0] = NULL;
|
|
|
|
keys[1] = NULL;
|
2016-03-29 16:24:37 -04:00
|
|
|
keys[2] = NULL;
|
2015-03-24 19:34:24 -04:00
|
|
|
|
|
|
|
gpgme_ctx_t ctx;
|
|
|
|
gpgme_error_t error = gpgme_new(&ctx);
|
|
|
|
if (error) {
|
|
|
|
log_error("GPG: Failed to create gpgme context. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-29 16:24:37 -04:00
|
|
|
gpgme_key_t receiver_key;
|
|
|
|
error = gpgme_get_key(ctx, pubkeyid->id, &receiver_key, 0);
|
|
|
|
if (error || receiver_key == NULL) {
|
|
|
|
log_error("GPG: Failed to get receiver_key. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
2015-03-24 19:34:24 -04:00
|
|
|
gpgme_release(ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-03-29 16:24:37 -04:00
|
|
|
keys[0] = receiver_key;
|
2015-03-24 19:34:24 -04:00
|
|
|
|
2016-03-29 16:24:37 -04:00
|
|
|
gpgme_key_t sender_key = NULL;
|
|
|
|
error = gpgme_get_key(ctx, fp, &sender_key, 0);
|
|
|
|
if (error || sender_key == NULL) {
|
|
|
|
log_error("GPG: Failed to get sender_key. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
|
|
|
gpgme_release(ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
keys[1] = sender_key;
|
2015-03-24 19:34:24 -04:00
|
|
|
|
|
|
|
gpgme_data_t plain;
|
|
|
|
gpgme_data_new_from_mem(&plain, message, strlen(message), 1);
|
2015-08-10 20:00:23 -04:00
|
|
|
|
|
|
|
gpgme_data_t cipher;
|
2015-03-24 19:34:24 -04:00
|
|
|
gpgme_data_new(&cipher);
|
|
|
|
|
|
|
|
gpgme_set_armor(ctx, 1);
|
|
|
|
error = gpgme_op_encrypt(ctx, keys, GPGME_ENCRYPT_ALWAYS_TRUST, plain, cipher);
|
2015-08-10 20:00:23 -04:00
|
|
|
gpgme_data_release(plain);
|
|
|
|
gpgme_release(ctx);
|
2016-03-29 16:24:37 -04:00
|
|
|
gpgme_key_unref(receiver_key);
|
|
|
|
gpgme_key_unref(sender_key);
|
2015-08-10 20:00:23 -04:00
|
|
|
|
2015-03-24 19:34:24 -04:00
|
|
|
if (error) {
|
|
|
|
log_error("GPG: Failed to encrypt message. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-03-23 19:38:06 -04:00
|
|
|
|
2015-03-24 19:34:24 -04:00
|
|
|
size_t len;
|
2015-08-10 20:00:23 -04:00
|
|
|
char *cipher_str = gpgme_data_release_and_get_mem(cipher, &len);
|
|
|
|
|
|
|
|
char *result = NULL;
|
2015-03-24 19:34:24 -04:00
|
|
|
if (cipher_str) {
|
2015-08-10 20:00:23 -04:00
|
|
|
GString *cipher_gstr = g_string_new("");
|
|
|
|
g_string_append_len(cipher_gstr, cipher_str, len);
|
|
|
|
result = _remove_header_footer(cipher_gstr->str, PGP_MESSAGE_FOOTER);
|
|
|
|
g_string_free(cipher_gstr, TRUE);
|
|
|
|
gpgme_free(cipher_str);
|
2015-03-24 19:34:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-10-25 18:43:19 -04:00
|
|
|
char*
|
|
|
|
p_gpg_decrypt(const char *const cipher)
|
2015-03-24 20:05:32 -04:00
|
|
|
{
|
|
|
|
gpgme_ctx_t ctx;
|
|
|
|
gpgme_error_t error = gpgme_new(&ctx);
|
2015-08-10 20:00:23 -04:00
|
|
|
|
2015-03-24 20:05:32 -04:00
|
|
|
if (error) {
|
|
|
|
log_error("GPG: Failed to create gpgme context. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-09-01 14:24:56 -04:00
|
|
|
gpgme_set_passphrase_cb(ctx, (gpgme_passphrase_cb_t)_p_gpg_passphrase_cb, NULL);
|
|
|
|
|
2015-08-10 20:00:23 -04:00
|
|
|
char *cipher_with_headers = _add_header_footer(cipher, PGP_MESSAGE_HEADER, PGP_MESSAGE_FOOTER);
|
2015-03-24 20:05:32 -04:00
|
|
|
gpgme_data_t cipher_data;
|
2015-08-10 20:00:23 -04:00
|
|
|
gpgme_data_new_from_mem(&cipher_data, cipher_with_headers, strlen(cipher_with_headers), 1);
|
|
|
|
free(cipher_with_headers);
|
|
|
|
|
|
|
|
gpgme_data_t plain_data;
|
2015-03-24 20:05:32 -04:00
|
|
|
gpgme_data_new(&plain_data);
|
|
|
|
|
|
|
|
error = gpgme_op_decrypt(ctx, cipher_data, plain_data);
|
2015-08-10 20:00:23 -04:00
|
|
|
gpgme_data_release(cipher_data);
|
|
|
|
|
2015-03-24 20:05:32 -04:00
|
|
|
if (error) {
|
|
|
|
log_error("GPG: Failed to encrypt message. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
2015-08-10 20:00:23 -04:00
|
|
|
gpgme_data_release(plain_data);
|
2015-08-23 17:18:54 -04:00
|
|
|
gpgme_release(ctx);
|
2015-03-24 20:05:32 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-23 17:18:54 -04:00
|
|
|
gpgme_decrypt_result_t res = gpgme_op_decrypt_result(ctx);
|
|
|
|
if (res) {
|
2016-03-29 16:24:37 -04:00
|
|
|
GString *recipients_str = g_string_new("");
|
2015-08-23 17:18:54 -04:00
|
|
|
gpgme_recipient_t recipient = res->recipients;
|
2016-03-29 16:24:37 -04:00
|
|
|
while (recipient) {
|
2015-08-23 17:18:54 -04:00
|
|
|
gpgme_key_t key;
|
2015-08-25 17:45:51 -04:00
|
|
|
error = gpgme_get_key(ctx, recipient->keyid, &key, 1);
|
2015-08-23 17:18:54 -04:00
|
|
|
|
|
|
|
if (!error && key) {
|
|
|
|
const char *addr = gpgme_key_get_string_attr(key, GPGME_ATTR_EMAIL, NULL, 0);
|
|
|
|
if (addr) {
|
2016-03-29 16:24:37 -04:00
|
|
|
g_string_append(recipients_str, addr);
|
2015-08-23 17:18:54 -04:00
|
|
|
}
|
|
|
|
gpgme_key_unref(key);
|
|
|
|
}
|
2016-03-29 16:24:37 -04:00
|
|
|
|
|
|
|
if (recipient->next) {
|
|
|
|
g_string_append(recipients_str, ", ");
|
|
|
|
}
|
|
|
|
|
|
|
|
recipient = recipient->next;
|
2015-08-23 17:18:54 -04:00
|
|
|
}
|
2016-03-29 16:24:37 -04:00
|
|
|
|
|
|
|
log_debug("GPG: Decrypted message for recipients: %s", recipients_str->str);
|
|
|
|
g_string_free(recipients_str, TRUE);
|
2015-08-23 17:18:54 -04:00
|
|
|
}
|
|
|
|
gpgme_release(ctx);
|
|
|
|
|
2015-03-24 20:05:32 -04:00
|
|
|
size_t len = 0;
|
|
|
|
char *plain_str = gpgme_data_release_and_get_mem(plain_data, &len);
|
|
|
|
char *result = NULL;
|
|
|
|
if (plain_str) {
|
|
|
|
plain_str[len] = 0;
|
|
|
|
result = g_strdup(plain_str);
|
|
|
|
}
|
|
|
|
gpgme_free(plain_str);
|
|
|
|
|
2015-09-01 16:08:23 -04:00
|
|
|
if (passphrase_attempt) {
|
|
|
|
passphrase = strdup(passphrase_attempt);
|
|
|
|
}
|
2015-09-01 16:05:17 -04:00
|
|
|
|
2015-03-24 20:05:32 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-08-10 20:00:23 -04:00
|
|
|
void
|
|
|
|
p_gpg_free_decrypted(char *decrypted)
|
|
|
|
{
|
|
|
|
g_free(decrypted);
|
|
|
|
}
|
|
|
|
|
2015-10-25 18:43:19 -04:00
|
|
|
char*
|
|
|
|
p_gpg_autocomplete_key(const char *const search_str)
|
2015-08-25 18:44:03 -04:00
|
|
|
{
|
|
|
|
return autocomplete_complete(key_ac, search_str, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
p_gpg_autocomplete_key_reset(void)
|
|
|
|
{
|
|
|
|
autocomplete_reset(key_ac);
|
|
|
|
}
|
|
|
|
|
2015-10-25 18:43:19 -04:00
|
|
|
char*
|
2015-08-29 21:12:05 -04:00
|
|
|
p_gpg_format_fp_str(char *fp)
|
|
|
|
{
|
|
|
|
if (!fp) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
GString *format = g_string_new("");
|
|
|
|
int i;
|
|
|
|
int len = strlen(fp);
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
g_string_append_c(format, fp[i]);
|
|
|
|
if (((i+1) % 4 == 0) && (i+1 < len)) {
|
|
|
|
g_string_append_c(format, ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char *result = format->str;
|
|
|
|
g_string_free(format, FALSE);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-03-24 19:34:24 -04:00
|
|
|
static char*
|
2015-10-25 18:43:19 -04:00
|
|
|
_remove_header_footer(char *str, const char *const footer)
|
2015-03-24 19:34:24 -04:00
|
|
|
{
|
|
|
|
int pos = 0;
|
2015-03-23 19:42:04 -04:00
|
|
|
int newlines = 0;
|
2015-03-24 19:34:24 -04:00
|
|
|
|
2015-03-23 19:42:04 -04:00
|
|
|
while (newlines < 3) {
|
2015-03-24 19:34:24 -04:00
|
|
|
if (str[pos] == '\n') {
|
2015-03-23 19:42:04 -04:00
|
|
|
newlines++;
|
2015-03-23 19:38:06 -04:00
|
|
|
}
|
2015-03-24 19:34:24 -04:00
|
|
|
pos++;
|
2015-03-23 19:38:06 -04:00
|
|
|
|
2015-03-24 19:34:24 -04:00
|
|
|
if (str[pos] == '\0') {
|
2015-03-23 19:42:04 -04:00
|
|
|
return NULL;
|
2015-03-23 19:38:06 -04:00
|
|
|
}
|
2015-03-23 19:42:04 -04:00
|
|
|
}
|
2015-03-23 19:38:06 -04:00
|
|
|
|
2015-03-24 19:34:24 -04:00
|
|
|
char *stripped = strdup(&str[pos]);
|
|
|
|
char *footer_start = g_strrstr(stripped, footer);
|
|
|
|
footer_start[0] = '\0';
|
2015-03-23 19:38:06 -04:00
|
|
|
|
2015-03-23 19:42:04 -04:00
|
|
|
return stripped;
|
2015-03-23 20:47:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static char*
|
2015-10-25 18:43:19 -04:00
|
|
|
_add_header_footer(const char *const str, const char *const header, const char *const footer)
|
2015-03-23 20:47:13 -04:00
|
|
|
{
|
|
|
|
GString *result_str = g_string_new("");
|
|
|
|
|
2015-03-24 20:05:32 -04:00
|
|
|
g_string_append(result_str, header);
|
2015-03-23 20:47:13 -04:00
|
|
|
g_string_append(result_str, "\n\n");
|
|
|
|
g_string_append(result_str, str);
|
|
|
|
g_string_append(result_str, "\n");
|
2015-03-24 20:05:32 -04:00
|
|
|
g_string_append(result_str, footer);
|
2015-03-23 20:47:13 -04:00
|
|
|
|
|
|
|
char *result = result_str->str;
|
|
|
|
g_string_free(result_str, FALSE);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2015-06-23 18:29:10 -04:00
|
|
|
|
|
|
|
static void
|
2015-08-25 17:45:51 -04:00
|
|
|
_save_pubkeys(void)
|
2015-06-23 18:29:10 -04:00
|
|
|
{
|
|
|
|
gsize g_data_size;
|
2015-08-25 17:45:51 -04:00
|
|
|
gchar *g_pubkeys_data = g_key_file_to_data(pubkeyfile, &g_data_size, NULL);
|
|
|
|
g_file_set_contents(pubsloc, g_pubkeys_data, g_data_size, NULL);
|
|
|
|
g_chmod(pubsloc, S_IRUSR | S_IWUSR);
|
|
|
|
g_free(g_pubkeys_data);
|
2015-06-23 18:29:10 -04:00
|
|
|
}
|