From 55c2d1cc21974936e5b431de49ae81e6fc3845be Mon Sep 17 00:00:00 2001 From: James Booth Date: Tue, 25 Aug 2015 23:04:21 +0100 Subject: PGP: Display whether contact public key was received or manually set --- src/pgp/gpg.c | 38 +++++++++++++++++++++++++++++--------- src/pgp/gpg.h | 5 +++++ 2 files changed, 34 insertions(+), 9 deletions(-) (limited to 'src/pgp') diff --git a/src/pgp/gpg.c b/src/pgp/gpg.c index cb1653e8..b4bcef48 100644 --- a/src/pgp/gpg.c +++ b/src/pgp/gpg.c @@ -63,6 +63,15 @@ 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); static void _save_pubkeys(void); +void +_p_gpg_free_pubkeyid(ProfPGPPubKeyId *pubkeyid) +{ + if (pubkeyid) { + free(pubkeyid->id); + } + free(pubkeyid); +} + void p_gpg_init(void) { @@ -70,7 +79,7 @@ p_gpg_init(void) log_debug("GPG: Found gpgme version: %s", libversion); gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL)); - pubkeys = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); + pubkeys = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)_p_gpg_free_pubkeyid); } void @@ -156,7 +165,10 @@ p_gpg_on_connect(const char * const barejid) continue; } - g_hash_table_replace(pubkeys, strdup(jid), strdup(keyid)); + ProfPGPPubKeyId *pubkeyid = malloc(sizeof(ProfPGPPubKeyId)); + pubkeyid->id = strdup(keyid); + pubkeyid->received = FALSE; + g_hash_table_replace(pubkeys, strdup(jid), pubkeyid); g_free(keyid); gpgme_key_unref(key); } @@ -173,7 +185,7 @@ p_gpg_on_disconnect(void) { if (pubkeys) { g_hash_table_destroy(pubkeys); - pubkeys = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); + pubkeys = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)_p_gpg_free_pubkeyid); } if (pubkeyfile) { @@ -209,7 +221,10 @@ p_gpg_addkey(const char * const jid, const char * const keyid) _save_pubkeys(); // update in memory pubkeys list - g_hash_table_replace(pubkeys, strdup(jid), strdup(keyid)); + ProfPGPPubKeyId *pubkeyid = malloc(sizeof(ProfPGPPubKeyId)); + pubkeyid->id = strdup(keyid); + pubkeyid->received = FALSE; + g_hash_table_replace(pubkeys, strdup(jid), pubkeyid); gpgme_key_unref(key); return TRUE; @@ -412,7 +427,10 @@ p_gpg_verify(const char * const barejid, const char *const sign) 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); - g_hash_table_replace(pubkeys, strdup(barejid), strdup(key->subkeys->keyid)); + ProfPGPPubKeyId *pubkeyid = malloc(sizeof(ProfPGPPubKeyId)); + pubkeyid->id = strdup(key->subkeys->keyid); + pubkeyid->received = TRUE; + g_hash_table_replace(pubkeys, strdup(barejid), pubkeyid); } gpgme_key_unref(key); @@ -493,9 +511,11 @@ p_gpg_sign(const char * const str, const char * const fp) char * p_gpg_encrypt(const char * const barejid, const char * const message) { - char *keyid = g_hash_table_lookup(pubkeys, barejid); - - if (!keyid) { + ProfPGPPubKeyId *pubkeyid = g_hash_table_lookup(pubkeys, barejid); + if (!pubkeyid) { + return NULL; + } + if (!pubkeyid->id) { return NULL; } @@ -512,7 +532,7 @@ p_gpg_encrypt(const char * const barejid, const char * const message) } gpgme_key_t key; - error = gpgme_get_key(ctx, keyid, &key, 0); + error = gpgme_get_key(ctx, pubkeyid->id, &key, 0); if (error || key == NULL) { log_error("GPG: Failed to get key. %s %s", gpgme_strsource(error), gpgme_strerror(error)); diff --git a/src/pgp/gpg.h b/src/pgp/gpg.h index 7bdd663a..2d3a78a3 100644 --- a/src/pgp/gpg.h +++ b/src/pgp/gpg.h @@ -46,6 +46,11 @@ typedef struct pgp_key_t { gboolean secret; } ProfPGPKey; +typedef struct pgp_pubkeyid_t { + char *id; + gboolean received; +} ProfPGPPubKeyId; + void p_gpg_init(void); void p_gpg_close(void); void p_gpg_on_connect(const char * const barejid); -- cgit 1.4.1-2-gfad0 7'>37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66