2015-03-21 20:12:14 -04:00
|
|
|
/*
|
|
|
|
* gpg.c
|
2019-11-13 06:11:05 -05:00
|
|
|
* vim: expandtab:ts=4:sts=4:sw=4
|
2015-03-21 20:12:14 -04:00
|
|
|
*
|
2019-01-22 05:31:45 -05:00
|
|
|
* Copyright (C) 2012 - 2019 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>
|
2020-07-07 03:43:28 -04:00
|
|
|
#include <string.h>
|
2020-07-07 07:53:30 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
2015-06-23 18:29:10 -04:00
|
|
|
#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>
|
|
|
|
|
2020-07-07 03:43:28 -04:00
|
|
|
#include "log.h"
|
2020-07-07 07:53:30 -04:00
|
|
|
#include "common.h"
|
2020-07-07 03:43:28 -04:00
|
|
|
#include "pgp/gpg.h"
|
2020-07-07 07:53:30 -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-----"
|
2020-07-07 08:18:57 -04:00
|
|
|
#define PGP_MESSAGE_HEADER "-----BEGIN PGP MESSAGE-----"
|
|
|
|
#define PGP_MESSAGE_FOOTER "-----END PGP MESSAGE-----"
|
2015-03-23 19:38:06 -04:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
static const char* libversion = NULL;
|
|
|
|
static GHashTable* pubkeys;
|
2015-03-21 20:29:57 -04:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
static gchar* pubsloc;
|
|
|
|
static GKeyFile* pubkeyfile;
|
2015-06-23 18:29:10 -04:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
static char* passphrase;
|
|
|
|
static char* passphrase_attempt;
|
2015-09-01 16:05:17 -04:00
|
|
|
|
2015-08-25 18:44:03 -04:00
|
|
|
static Autocomplete key_ac;
|
|
|
|
|
2020-07-07 08:18:57 -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
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
static gpgme_key_t _ox_key_lookup(const char* const barejid, gboolean secret_only);
|
|
|
|
static gboolean _ox_key_is_usable(gpgme_key_t key, const char* const barejid, gboolean secret);
|
2020-06-21 03:43:42 -04:00
|
|
|
|
2015-08-25 18:04:21 -04:00
|
|
|
void
|
2020-07-07 08:18:57 -04:00
|
|
|
_p_gpg_free_pubkeyid(ProfPGPPubKeyId* pubkeyid)
|
2015-08-25 18:04:21 -04:00
|
|
|
{
|
|
|
|
if (pubkeyid) {
|
|
|
|
free(pubkeyid->id);
|
|
|
|
}
|
|
|
|
free(pubkeyid);
|
|
|
|
}
|
|
|
|
|
2015-10-25 18:43:19 -04:00
|
|
|
static gpgme_error_t*
|
2020-07-07 08:18:57 -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 14:24:56 -04:00
|
|
|
{
|
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 {
|
2020-07-07 08:18:57 -04:00
|
|
|
GString* pass_term = g_string_new("");
|
2015-09-01 15:16:04 -04:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
char* password = ui_ask_pgp_passphrase(uid_hint, prev_was_bad);
|
2015-09-01 16:05:17 -04:00
|
|
|
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();
|
2020-07-07 08:18:57 -04:00
|
|
|
GHashTable* keys = p_gpg_list_keys();
|
2015-08-25 18:44:03 -04:00
|
|
|
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
|
2020-07-07 08:18:57 -04:00
|
|
|
p_gpg_on_connect(const char* const barejid)
|
2015-06-23 18:29:10 -04:00
|
|
|
{
|
2022-03-13 06:58:56 -04:00
|
|
|
pubsloc = files_file_in_account_data_path(DIR_PGP, barejid, "pubkeys");
|
|
|
|
if (!pubsloc) {
|
|
|
|
log_error("Could not create directory for account %s.", barejid);
|
|
|
|
cons_show_error("Could not create directory for account %s.", barejid);
|
|
|
|
return;
|
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;
|
2020-07-07 08:18:57 -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;
|
|
|
|
}
|
|
|
|
|
2020-11-09 05:03:54 -05:00
|
|
|
for (int i = 0; i < len; i++) {
|
2020-07-07 08:18:57 -04:00
|
|
|
GError* gerr = NULL;
|
|
|
|
gchar* jid = jids[i];
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
ProfPGPPubKeyId* pubkeyid = malloc(sizeof(ProfPGPPubKeyId));
|
2015-08-25 18:04:21 -04:00
|
|
|
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
|
2020-07-07 08:18:57 -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
|
2020-07-07 08:18:57 -04:00
|
|
|
ProfPGPPubKeyId* pubkeyid = malloc(sizeof(ProfPGPPubKeyId));
|
2015-08-25 18:04:21 -04:00
|
|
|
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)
|
|
|
|
{
|
2020-07-07 08:18:57 -04:00
|
|
|
ProfPGPKey* p_pgpkey = malloc(sizeof(ProfPGPKey));
|
2015-08-24 19:21:49 -04:00
|
|
|
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
|
2020-07-07 08:18:57 -04:00
|
|
|
_p_gpg_free_key(ProfPGPKey* key)
|
2015-08-24 19:21:49 -04:00
|
|
|
{
|
|
|
|
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;
|
2020-07-07 08:18:57 -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
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
ProfPGPKey* p_pgpkey = _p_gpg_key_new();
|
2015-08-24 19:21:49 -04:00
|
|
|
p_pgpkey->id = strdup(sub->keyid);
|
|
|
|
p_pgpkey->name = strdup(key->uids->uid);
|
|
|
|
p_pgpkey->fp = strdup(sub->fpr);
|
2020-07-07 08:18:57 -04:00
|
|
|
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;
|
2015-08-24 19:21:49 -04:00
|
|
|
|
|
|
|
sub = sub->next;
|
|
|
|
while (sub) {
|
2020-07-07 08:18:57 -04:00
|
|
|
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;
|
2015-08-24 19:21:49 -04:00
|
|
|
|
|
|
|
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) {
|
2020-07-07 08:18:57 -04:00
|
|
|
ProfPGPKey* p_pgpkey = g_hash_table_lookup(result, key->uids->uid);
|
2015-08-24 19:21:49 -04:00
|
|
|
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);
|
2020-07-07 08:18:57 -04:00
|
|
|
GList* ids = g_hash_table_get_keys(result);
|
|
|
|
GList* curr = ids;
|
2015-08-25 18:44:03 -04:00
|
|
|
while (curr) {
|
2020-07-07 08:18:57 -04:00
|
|
|
ProfPGPKey* key = g_hash_table_lookup(result, curr->data);
|
2015-08-25 18:44:03 -04:00
|
|
|
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
|
2020-07-07 08:18:57 -04:00
|
|
|
p_gpg_free_keys(GHashTable* keys)
|
2015-08-24 19:21:49 -04:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2017-08-10 17:47:17 -04:00
|
|
|
if (libversion == NULL) {
|
|
|
|
libversion = gpgme_check_version(NULL);
|
|
|
|
}
|
2015-03-21 20:29:57 -04:00
|
|
|
return libversion;
|
|
|
|
}
|
|
|
|
|
2015-08-23 17:54:41 -04:00
|
|
|
gboolean
|
2020-07-07 08:18:57 -04: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
|
2020-07-07 08:18:57 -04:00
|
|
|
p_gpg_available(const char* const barejid)
|
2015-06-20 18:49:24 -04:00
|
|
|
{
|
2020-07-07 08:18:57 -04:00
|
|
|
char* pubkey = g_hash_table_lookup(pubkeys, barejid);
|
2015-08-25 17:45:51 -04:00
|
|
|
return (pubkey != NULL);
|
2015-06-20 18:49:24 -04:00
|
|
|
}
|
|
|
|
|
2015-03-23 20:47:13 -04:00
|
|
|
void
|
2020-07-07 08:18:57 -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;
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -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);
|
2020-07-07 08:18:57 -04:00
|
|
|
ProfPGPPubKeyId* pubkeyid = malloc(sizeof(ProfPGPPubKeyId));
|
2015-08-25 18:04:21 -04:00
|
|
|
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*
|
2020-07-07 08:18:57 -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
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
char* str_or_empty = NULL;
|
2015-03-23 19:38:06 -04:00
|
|
|
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
|
|
|
|
2020-07-07 08:18:57 -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
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
char* result = NULL;
|
2015-03-23 19:38:06 -04:00
|
|
|
|
|
|
|
size_t len = 0;
|
2020-07-07 08:18:57 -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) {
|
2020-07-07 08:18:57 -04:00
|
|
|
GString* signed_gstr = g_string_new("");
|
2015-08-10 20:00:23 -04:00
|
|
|
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*
|
2020-07-07 08:18:57 -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
|
|
|
{
|
2020-07-07 08:18:57 -04:00
|
|
|
ProfPGPPubKeyId* pubkeyid = g_hash_table_lookup(pubkeys, barejid);
|
2015-08-25 18:04:21 -04:00
|
|
|
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;
|
2020-07-07 08:18:57 -04:00
|
|
|
char* cipher_str = gpgme_data_release_and_get_mem(cipher, &len);
|
2015-08-10 20:00:23 -04:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
char* result = NULL;
|
2015-03-24 19:34:24 -04:00
|
|
|
if (cipher_str) {
|
2020-07-07 08:18:57 -04:00
|
|
|
GString* cipher_gstr = g_string_new("");
|
2015-08-10 20:00:23 -04:00
|
|
|
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*
|
2020-07-07 08:18:57 -04:00
|
|
|
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);
|
|
|
|
|
2020-07-07 08:18:57 -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) {
|
2020-07-07 08:18:57 -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) {
|
2020-07-07 08:18:57 -04:00
|
|
|
const char* addr = gpgme_key_get_string_attr(key, GPGME_ATTR_EMAIL, NULL, 0);
|
2015-08-23 17:18:54 -04:00
|
|
|
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;
|
2020-07-07 08:18:57 -04:00
|
|
|
char* plain_str = gpgme_data_release_and_get_mem(plain_data, &len);
|
|
|
|
char* result = NULL;
|
2015-03-24 20:05:32 -04:00
|
|
|
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
|
2020-07-07 08:18:57 -04:00
|
|
|
p_gpg_free_decrypted(char* decrypted)
|
2015-08-10 20:00:23 -04:00
|
|
|
{
|
|
|
|
g_free(decrypted);
|
|
|
|
}
|
|
|
|
|
2015-10-25 18:43:19 -04:00
|
|
|
char*
|
2020-07-07 08:18:57 -04:00
|
|
|
p_gpg_autocomplete_key(const char* const search_str, gboolean previous, void* context)
|
2015-08-25 18:44:03 -04:00
|
|
|
{
|
2017-03-31 19:27:11 -04:00
|
|
|
return autocomplete_complete(key_ac, search_str, TRUE, previous);
|
2015-08-25 18:44:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
p_gpg_autocomplete_key_reset(void)
|
|
|
|
{
|
|
|
|
autocomplete_reset(key_ac);
|
|
|
|
}
|
|
|
|
|
2015-10-25 18:43:19 -04:00
|
|
|
char*
|
2020-07-07 08:18:57 -04:00
|
|
|
p_gpg_format_fp_str(char* fp)
|
2015-08-29 21:12:05 -04:00
|
|
|
{
|
|
|
|
if (!fp) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
GString* format = g_string_new("");
|
2015-08-29 21:12:05 -04:00
|
|
|
int len = strlen(fp);
|
2020-11-09 05:03:54 -05:00
|
|
|
for (int i = 0; i < len; i++) {
|
2015-08-29 21:12:05 -04:00
|
|
|
g_string_append_c(format, fp[i]);
|
2020-07-07 08:18:57 -04:00
|
|
|
if (((i + 1) % 4 == 0) && (i + 1 < len)) {
|
2015-08-29 21:12:05 -04:00
|
|
|
g_string_append_c(format, ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
char* result = format->str;
|
2015-08-29 21:12:05 -04:00
|
|
|
g_string_free(format, FALSE);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-06-21 03:43:42 -04:00
|
|
|
/*!
|
|
|
|
* \brief Public keys with XMPP-URI.
|
|
|
|
*
|
|
|
|
* This function will look for all public key with a XMPP-URI as UID.
|
|
|
|
*
|
|
|
|
*/
|
2020-07-02 09:09:34 -04:00
|
|
|
GHashTable*
|
2020-07-07 08:18:57 -04:00
|
|
|
ox_gpg_public_keys(void)
|
|
|
|
{
|
2020-06-21 03:43:42 -04:00
|
|
|
gpgme_error_t error;
|
2020-07-07 08:18:57 -04:00
|
|
|
GHashTable* result = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)_p_gpg_free_key);
|
2020-06-21 03:43:42 -04:00
|
|
|
|
|
|
|
gpgme_ctx_t ctx;
|
|
|
|
error = gpgme_new(&ctx);
|
|
|
|
|
|
|
|
if (error) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: Failed to create gpgme context. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
2020-06-21 03:43:42 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = gpgme_op_keylist_start(ctx, NULL, 0); // all public keys
|
|
|
|
if (error == GPG_ERR_NO_ERROR) {
|
|
|
|
gpgme_key_t key;
|
|
|
|
error = gpgme_op_keylist_next(ctx, &key);
|
2020-07-07 08:18:57 -04:00
|
|
|
if (error != GPG_ERR_EOF && error != GPG_ERR_NO_ERROR) {
|
2020-06-21 03:43:42 -04:00
|
|
|
log_error("OX: gpgme_op_keylist_next %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
|
|
|
g_hash_table_destroy(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
while (!error) {
|
|
|
|
// Looking for XMPP URI UID
|
|
|
|
gpgme_user_id_t uid = key->uids;
|
|
|
|
gpgme_user_id_t xmppid = NULL;
|
|
|
|
while (!xmppid && uid) {
|
2020-07-07 08:18:57 -04:00
|
|
|
if (uid->name && strlen(uid->name) >= 10) {
|
|
|
|
if (strstr(uid->name, "xmpp:") == uid->name) {
|
2020-06-21 03:43:42 -04:00
|
|
|
xmppid = uid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uid = uid->next;
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
if (xmppid) {
|
2020-06-21 03:43:42 -04:00
|
|
|
// Build Key information about all subkey
|
|
|
|
gpgme_subkey_t sub = key->subkeys;
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
ProfPGPKey* p_pgpkey = _p_gpg_key_new();
|
2020-06-21 03:43:42 -04:00
|
|
|
p_pgpkey->id = strdup(sub->keyid);
|
|
|
|
p_pgpkey->name = strdup(xmppid->uid);
|
|
|
|
p_pgpkey->fp = strdup(sub->fpr);
|
2020-07-07 08:18:57 -04:00
|
|
|
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;
|
2020-06-21 03:43:42 -04:00
|
|
|
|
|
|
|
sub = sub->next;
|
|
|
|
while (sub) {
|
2020-07-07 08:18:57 -04:00
|
|
|
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;
|
2020-06-21 03:43:42 -04:00
|
|
|
|
|
|
|
sub = sub->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_hash_table_insert(result, strdup(p_pgpkey->name), p_pgpkey);
|
|
|
|
}
|
|
|
|
gpgme_key_unref(key);
|
|
|
|
error = gpgme_op_keylist_next(ctx, &key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gpgme_release(ctx);
|
|
|
|
|
2022-01-27 09:49:44 -05: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);
|
2020-06-21 03:43:42 -04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-07-02 09:09:34 -04:00
|
|
|
char*
|
2020-07-07 08:18:57 -04:00
|
|
|
p_ox_gpg_signcrypt(const char* const sender_barejid, const char* const recipient_barejid, const char* const message)
|
2020-07-02 09:09:34 -04:00
|
|
|
{
|
2020-07-07 08:18:57 -04:00
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
gpgme_check_version(NULL);
|
|
|
|
gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
|
|
|
|
gpgme_ctx_t ctx;
|
|
|
|
|
|
|
|
gpgme_error_t error = gpgme_new(&ctx);
|
|
|
|
if (GPG_ERR_NO_ERROR != error) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: Failed to create gpgme context. %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
2020-07-07 08:18:57 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = gpgme_set_protocol(ctx, GPGME_PROTOCOL_OPENPGP);
|
|
|
|
if (error != 0) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: Signcrypt error: %s", gpgme_strerror(error));
|
2020-07-07 08:18:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
gpgme_set_armor(ctx, 0);
|
|
|
|
gpgme_set_textmode(ctx, 0);
|
|
|
|
gpgme_set_offline(ctx, 1);
|
|
|
|
gpgme_set_keylist_mode(ctx, GPGME_KEYLIST_MODE_LOCAL);
|
|
|
|
if (error != 0) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: Signcrypt error: %s", gpgme_strerror(error));
|
2020-07-07 08:18:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
gpgme_key_t recp[3];
|
|
|
|
recp[0] = NULL,
|
|
|
|
recp[1] = NULL;
|
|
|
|
|
|
|
|
char* xmpp_jid_me = alloca((strlen(sender_barejid) + 6) * sizeof(char));
|
|
|
|
char* xmpp_jid_recipient = alloca((strlen(recipient_barejid) + 6) * sizeof(char));
|
2020-06-21 03:43:42 -04:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
strcpy(xmpp_jid_me, "xmpp:");
|
|
|
|
strcpy(xmpp_jid_recipient, "xmpp:");
|
|
|
|
strcat(xmpp_jid_me, sender_barejid);
|
|
|
|
strcat(xmpp_jid_recipient, recipient_barejid);
|
|
|
|
|
|
|
|
gpgme_signers_clear(ctx);
|
|
|
|
|
|
|
|
// lookup own key
|
|
|
|
recp[0] = _ox_key_lookup(sender_barejid, TRUE);
|
|
|
|
if (error != 0) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: Key not found for %s. Error: %s", xmpp_jid_me, gpgme_strerror(error));
|
2020-07-07 08:18:57 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = gpgme_signers_add(ctx, recp[0]);
|
|
|
|
if (error != 0) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: gpgme_signers_add %s. Error: %s", xmpp_jid_me, gpgme_strerror(error));
|
2020-07-07 08:18:57 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup key of recipient
|
|
|
|
recp[1] = _ox_key_lookup(recipient_barejid, FALSE);
|
|
|
|
if (error != 0) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: Key not found for %s. Error: %s", xmpp_jid_recipient, gpgme_strerror(error));
|
2020-07-07 08:18:57 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2022-02-24 05:10:23 -05:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
recp[2] = NULL;
|
|
|
|
log_debug("%s <%s>", recp[0]->uids->name, recp[0]->uids->email);
|
|
|
|
log_debug("%s <%s>", recp[1]->uids->name, recp[1]->uids->email);
|
|
|
|
|
|
|
|
gpgme_encrypt_flags_t flags = 0;
|
|
|
|
|
|
|
|
gpgme_data_t plain;
|
|
|
|
gpgme_data_t cipher;
|
|
|
|
|
|
|
|
error = gpgme_data_new(&plain);
|
|
|
|
if (error != 0) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: %s", gpgme_strerror(error));
|
2020-07-07 08:18:57 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = gpgme_data_new_from_mem(&plain, message, strlen(message), 0);
|
|
|
|
if (error != 0) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: %s", gpgme_strerror(error));
|
2020-07-07 08:18:57 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
error = gpgme_data_new(&cipher);
|
|
|
|
if (error != 0) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: %s", gpgme_strerror(error));
|
2020-07-07 08:18:57 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = gpgme_op_encrypt_sign(ctx, recp, flags, plain, cipher);
|
|
|
|
if (error != 0) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: %s", gpgme_strerror(error));
|
2020-07-07 08:18:57 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t len;
|
|
|
|
char* cipher_str = gpgme_data_release_and_get_mem(cipher, &len);
|
|
|
|
char* result = g_base64_encode((unsigned char*)cipher_str, len);
|
|
|
|
gpgme_key_release(recp[0]);
|
|
|
|
gpgme_key_release(recp[1]);
|
|
|
|
gpgme_release(ctx);
|
|
|
|
return result;
|
2020-06-21 03:43:42 -04:00
|
|
|
}
|
|
|
|
|
2020-07-02 09:09:34 -04:00
|
|
|
gboolean
|
2020-07-07 08:18:57 -04:00
|
|
|
ox_is_private_key_available(const char* const barejid)
|
2020-07-02 09:09:34 -04:00
|
|
|
{
|
2020-06-21 03:43:42 -04:00
|
|
|
g_assert(barejid);
|
|
|
|
gboolean result = FALSE;
|
|
|
|
|
|
|
|
gpgme_key_t key = _ox_key_lookup(barejid, TRUE);
|
2020-07-07 08:18:57 -04:00
|
|
|
if (key) {
|
|
|
|
if (_ox_key_is_usable(key, barejid, TRUE)) {
|
2020-06-21 03:43:42 -04:00
|
|
|
result = TRUE;
|
|
|
|
}
|
2020-07-07 08:18:57 -04:00
|
|
|
gpgme_key_unref(key);
|
2020-06-21 03:43:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-07-02 09:09:34 -04:00
|
|
|
gboolean
|
2020-07-07 08:18:57 -04:00
|
|
|
ox_is_public_key_available(const char* const barejid)
|
2020-07-02 09:09:34 -04:00
|
|
|
{
|
2020-06-21 03:43:42 -04:00
|
|
|
g_assert(barejid);
|
|
|
|
gboolean result = FALSE;
|
|
|
|
gpgme_key_t key = _ox_key_lookup(barejid, FALSE);
|
2020-07-07 08:18:57 -04:00
|
|
|
if (key) {
|
|
|
|
if (_ox_key_is_usable(key, barejid, FALSE)) {
|
2020-06-21 03:43:42 -04:00
|
|
|
result = TRUE;
|
|
|
|
}
|
2020-07-07 08:18:57 -04:00
|
|
|
gpgme_key_unref(key);
|
2020-06-21 03:43:42 -04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-03-24 19:34:24 -04:00
|
|
|
static char*
|
2020-07-07 08:18:57 -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
|
|
|
|
2018-07-24 01:21:09 -04:00
|
|
|
while (newlines < 2) {
|
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
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
char* stripped = strdup(&str[pos]);
|
|
|
|
char* footer_start = g_strrstr(stripped, footer);
|
2015-03-24 19:34:24 -04:00
|
|
|
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*
|
2020-07-07 08:18:57 -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
|
|
|
{
|
2020-07-07 08:18:57 -04:00
|
|
|
GString* result_str = g_string_new("");
|
2015-03-23 20:47:13 -04:00
|
|
|
|
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
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
char* result = result_str->str;
|
2015-03-23 20:47:13 -04:00
|
|
|
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;
|
2020-07-07 08:18:57 -04:00
|
|
|
gchar* g_pubkeys_data = g_key_file_to_data(pubkeyfile, &g_data_size, NULL);
|
2015-08-25 17:45:51 -04:00
|
|
|
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
|
|
|
}
|
2020-06-21 03:43:42 -04:00
|
|
|
|
2020-07-02 09:09:34 -04:00
|
|
|
static gpgme_key_t
|
2020-07-07 08:18:57 -04:00
|
|
|
_ox_key_lookup(const char* const barejid, gboolean secret_only)
|
2020-07-02 09:09:34 -04:00
|
|
|
{
|
2020-06-21 03:43:42 -04:00
|
|
|
g_assert(barejid);
|
2022-02-24 05:10:23 -05:00
|
|
|
log_debug("OX: Looking for %s key: %s", secret_only == TRUE ? "Private" : "Public", barejid);
|
2020-06-21 03:43:42 -04:00
|
|
|
gpgme_key_t key = NULL;
|
|
|
|
gpgme_error_t error;
|
|
|
|
|
|
|
|
gpgme_ctx_t ctx;
|
|
|
|
error = gpgme_new(&ctx);
|
|
|
|
|
|
|
|
if (error) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: gpgme_new failed: %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
2020-06-21 03:43:42 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = gpgme_op_keylist_start(ctx, NULL, secret_only);
|
|
|
|
if (error == GPG_ERR_NO_ERROR) {
|
|
|
|
error = gpgme_op_keylist_next(ctx, &key);
|
2020-07-07 08:18:57 -04:00
|
|
|
if (error != GPG_ERR_EOF && error != GPG_ERR_NO_ERROR) {
|
2020-06-21 03:43:42 -04:00
|
|
|
log_error("OX: gpgme_op_keylist_next %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
GString* xmppuri = g_string_new("xmpp:");
|
2020-07-07 08:18:57 -04:00
|
|
|
g_string_append(xmppuri, barejid);
|
2020-06-21 03:43:42 -04:00
|
|
|
|
|
|
|
while (!error) {
|
|
|
|
// Looking for XMPP URI UID
|
|
|
|
gpgme_user_id_t uid = key->uids;
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
while (uid) {
|
|
|
|
if (uid->name && strlen(uid->name) >= 10) {
|
|
|
|
if (g_strcmp0(uid->name, xmppuri->str) == 0) {
|
2020-06-21 03:43:42 -04:00
|
|
|
gpgme_release(ctx);
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uid = uid->next;
|
|
|
|
}
|
|
|
|
gpgme_key_unref(key);
|
|
|
|
error = gpgme_op_keylist_next(ctx, &key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gpgme_release(ctx);
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2020-07-02 09:09:34 -04:00
|
|
|
static gboolean
|
2020-07-07 08:18:57 -04:00
|
|
|
_ox_key_is_usable(gpgme_key_t key, const char* const barejid, gboolean secret)
|
2020-07-02 09:09:34 -04:00
|
|
|
{
|
2020-06-21 03:43:42 -04:00
|
|
|
gboolean result = TRUE;
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
if (key->revoked || key->expired || key->disabled) {
|
2020-07-02 09:11:06 -04:00
|
|
|
result = FALSE;
|
|
|
|
}
|
2020-06-21 03:43:42 -04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* @brief XMPP-OX: Decrypt OX Message.
|
|
|
|
*
|
|
|
|
* @param base64 base64_encode OpenPGP message.
|
|
|
|
*
|
|
|
|
* @result decrypt XMPP OX Message NULL terminated C-String
|
|
|
|
*/
|
2020-07-02 09:09:34 -04:00
|
|
|
char*
|
|
|
|
p_ox_gpg_decrypt(char* base64)
|
|
|
|
{
|
2021-06-29 14:25:50 -04:00
|
|
|
// if there is no private key avaibale,
|
|
|
|
// we don't try do decrypt
|
2021-10-05 04:01:27 -04:00
|
|
|
if (!ox_is_private_key_available(connection_get_barejid())) {
|
2021-06-29 14:25:50 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-07-07 08:18:57 -04:00
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
gpgme_check_version(NULL);
|
|
|
|
gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
|
|
|
|
gpgme_ctx_t ctx;
|
|
|
|
gpgme_error_t error = gpgme_new(&ctx);
|
|
|
|
|
|
|
|
if (GPG_ERR_NO_ERROR != error) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: gpgme_new failed: %s %s", gpgme_strsource(error), gpgme_strerror(error));
|
2020-07-07 08:18:57 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = gpgme_set_protocol(ctx, GPGME_PROTOCOL_OPENPGP);
|
|
|
|
if (error != 0) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: %s", gpgme_strerror(error));
|
2020-07-07 08:18:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
gpgme_set_armor(ctx, 0);
|
|
|
|
gpgme_set_textmode(ctx, 0);
|
|
|
|
gpgme_set_offline(ctx, 1);
|
|
|
|
gpgme_set_keylist_mode(ctx, GPGME_KEYLIST_MODE_LOCAL);
|
|
|
|
if (error != 0) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: %s", gpgme_strerror(error));
|
2020-07-07 08:18:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
gpgme_data_t plain = NULL;
|
|
|
|
gpgme_data_t cipher = NULL;
|
|
|
|
|
|
|
|
gsize s;
|
2020-12-10 03:24:32 -05:00
|
|
|
guchar* encrypted = g_base64_decode(base64, &s);
|
|
|
|
error = gpgme_data_new_from_mem(&cipher, (char*)encrypted, s, 0);
|
2020-07-07 08:18:57 -04:00
|
|
|
if (error != 0) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: gpgme_data_new_from_mem: %s", gpgme_strerror(error));
|
2020-07-07 08:18:57 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = gpgme_data_new(&plain);
|
|
|
|
if (error != 0) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: %s", gpgme_strerror(error));
|
2020-06-21 03:43:42 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-07-07 08:18:57 -04:00
|
|
|
|
|
|
|
error = gpgme_op_decrypt_verify(ctx, cipher, plain);
|
|
|
|
if (error != 0) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: gpgme_op_decrypt: %s", gpgme_strerror(error));
|
2020-07-07 08:18:57 -04:00
|
|
|
error = gpgme_op_decrypt(ctx, cipher, plain);
|
|
|
|
if (error != 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
size_t len;
|
|
|
|
char* plain_str = gpgme_data_release_and_get_mem(plain, &len);
|
|
|
|
char* result = malloc(len + 1);
|
|
|
|
strcpy(result, plain_str);
|
|
|
|
result[len] = '\0';
|
|
|
|
return result;
|
2020-06-21 03:43:42 -04:00
|
|
|
}
|
|
|
|
|
2020-07-05 03:28:51 -04:00
|
|
|
/*!
|
|
|
|
* \brief Read public key from file.
|
|
|
|
*
|
|
|
|
* This function is used the read a public key from a file.
|
|
|
|
*
|
|
|
|
* This function is used to read a key and push it on PEP. There are some checks
|
|
|
|
* in this function:
|
2022-01-27 09:49:44 -05:00
|
|
|
*
|
2020-07-05 03:28:51 -04:00
|
|
|
* Key is not
|
2022-01-27 09:49:44 -05:00
|
|
|
* - gkey->revoked
|
|
|
|
* - gkey->expired
|
2020-07-05 03:28:51 -04:00
|
|
|
* - gkey->disabled
|
2022-01-27 09:49:44 -05:00
|
|
|
* - gkey->invalid
|
2020-07-05 03:28:51 -04:00
|
|
|
* - gkey->secret
|
2022-01-27 09:49:44 -05:00
|
|
|
*
|
2020-07-05 03:28:51 -04:00
|
|
|
* Only one key in the file.
|
|
|
|
*
|
2020-12-10 03:24:32 -05:00
|
|
|
* \param filename filename to read the file.
|
2020-07-05 03:28:51 -04:00
|
|
|
* \param key result with base64 encode key or NULL
|
|
|
|
* \param fp result with the fingerprint or NULL
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
2020-07-07 08:18:57 -04:00
|
|
|
p_ox_gpg_readkey(const char* const filename, char** key, char** fp)
|
|
|
|
{
|
2022-02-24 05:10:23 -05:00
|
|
|
log_info("PX: Read OpenPGP Key from file %s", filename);
|
2020-07-05 03:28:51 -04:00
|
|
|
|
|
|
|
GError* error = NULL;
|
|
|
|
gchar* data = NULL;
|
|
|
|
gsize size = -1;
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
gboolean success = g_file_get_contents(filename,
|
|
|
|
&data,
|
|
|
|
&size,
|
|
|
|
&error);
|
|
|
|
if (success) {
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
gpgme_check_version(NULL);
|
|
|
|
gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
|
2020-07-05 03:28:51 -04:00
|
|
|
gpgme_ctx_t ctx;
|
2020-07-07 08:18:57 -04:00
|
|
|
gpgme_error_t error = gpgme_new(&ctx);
|
2020-07-05 03:28:51 -04:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
if (GPG_ERR_NO_ERROR != error) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: Read OpenPGP key from file: gpgme_new failed: %s", gpgme_strerror(error));
|
2020-07-05 03:28:51 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = gpgme_set_protocol(ctx, GPGME_PROTOCOL_OPENPGP);
|
2020-07-07 08:18:57 -04:00
|
|
|
if (error != GPG_ERR_NO_ERROR) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: Read OpenPGP key from file: set GPGME_PROTOCOL_OPENPGP: %s", gpgme_strerror(error));
|
2020-07-05 03:28:51 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
gpgme_set_armor(ctx, 0);
|
|
|
|
gpgme_set_textmode(ctx, 0);
|
|
|
|
gpgme_set_offline(ctx, 1);
|
2020-07-05 03:28:51 -04:00
|
|
|
gpgme_set_keylist_mode(ctx, GPGME_KEYLIST_MODE_LOCAL);
|
|
|
|
|
|
|
|
gpgme_data_t gpgme_data = NULL;
|
2020-07-07 08:18:57 -04:00
|
|
|
error = gpgme_data_new(&gpgme_data);
|
|
|
|
if (error != GPG_ERR_NO_ERROR) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: Read OpenPGP key from file: gpgme_data_new %s", gpgme_strerror(error));
|
2020-07-05 03:28:51 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
error = gpgme_data_new_from_mem(&gpgme_data, (char*)data, size, 0);
|
|
|
|
if (error != GPG_ERR_NO_ERROR) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: Read OpenPGP key from file: gpgme_data_new_from_mem %s", gpgme_strerror(error));
|
2020-07-05 03:28:51 -04:00
|
|
|
return;
|
|
|
|
}
|
2020-07-07 08:18:57 -04:00
|
|
|
error = gpgme_op_keylist_from_data_start(ctx, gpgme_data, 0);
|
|
|
|
if (error != GPG_ERR_NO_ERROR) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: Read OpenPGP key from file: gpgme_op_keylist_from_data_start %s", gpgme_strerror(error));
|
2020-07-05 03:28:51 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
gpgme_key_t gkey;
|
2020-07-07 08:18:57 -04:00
|
|
|
error = gpgme_op_keylist_next(ctx, &gkey);
|
|
|
|
if (error != GPG_ERR_NO_ERROR) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: Read OpenPGP key from file: gpgme_op_keylist_next %s", gpgme_strerror(error));
|
2020-07-05 03:28:51 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpgme_key_t end;
|
2020-07-07 08:18:57 -04:00
|
|
|
error = gpgme_op_keylist_next(ctx, &end);
|
|
|
|
if (error == GPG_ERR_NO_ERROR) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: Read OpenPGP key from file: ambiguous key");
|
2020-07-05 03:28:51 -04:00
|
|
|
return;
|
|
|
|
}
|
2020-06-21 03:43:42 -04:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
if (gkey->revoked || gkey->expired || gkey->disabled || gkey->invalid || gkey->secret) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: Read OpenPGP key from file: Key is not valid");
|
2020-07-05 03:28:51 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
gchar* keybase64 = g_base64_encode((const guchar*)data, size);
|
|
|
|
|
2020-07-05 03:28:51 -04:00
|
|
|
*key = strdup(keybase64);
|
|
|
|
*fp = strdup(gkey->fpr);
|
|
|
|
} else {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: Read OpenPGP key from file: Unable to read file: %s", error->message);
|
2020-07-05 03:28:51 -04:00
|
|
|
}
|
|
|
|
}
|
2020-07-10 07:39:34 -04:00
|
|
|
|
|
|
|
gboolean
|
|
|
|
p_ox_gpg_import(char* base64_public_key)
|
|
|
|
{
|
|
|
|
gsize size = -1;
|
|
|
|
guchar* key = g_base64_decode(base64_public_key, &size);
|
|
|
|
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
gpgme_check_version(NULL);
|
|
|
|
gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
|
|
|
|
gpgme_ctx_t ctx;
|
|
|
|
gpgme_error_t error = gpgme_new(&ctx);
|
|
|
|
|
|
|
|
if (GPG_ERR_NO_ERROR != error) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: Read OpenPGP key from file: gpgme_new failed: %s", gpgme_strerror(error));
|
2020-07-10 07:39:34 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = gpgme_set_protocol(ctx, GPGME_PROTOCOL_OPENPGP);
|
|
|
|
if (error != GPG_ERR_NO_ERROR) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: Read OpenPGP key from file: set GPGME_PROTOCOL_OPENPGP: %s", gpgme_strerror(error));
|
2020-07-10 07:39:34 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpgme_set_armor(ctx, 0);
|
|
|
|
gpgme_set_textmode(ctx, 0);
|
|
|
|
gpgme_set_offline(ctx, 1);
|
|
|
|
gpgme_set_keylist_mode(ctx, GPGME_KEYLIST_MODE_LOCAL);
|
|
|
|
|
|
|
|
gpgme_data_t gpgme_data = NULL;
|
|
|
|
error = gpgme_data_new(&gpgme_data);
|
|
|
|
if (error != GPG_ERR_NO_ERROR) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: Read OpenPGP key from file: gpgme_data_new %s", gpgme_strerror(error));
|
2020-07-10 07:39:34 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpgme_data_new_from_mem(&gpgme_data, (gchar*)key, size, 0);
|
|
|
|
error = gpgme_op_import(ctx, gpgme_data);
|
|
|
|
if (error != GPG_ERR_NO_ERROR) {
|
2022-02-24 05:10:23 -05:00
|
|
|
log_error("OX: Failed to import key");
|
2020-07-10 07:39:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|