about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2015-08-25 23:04:21 +0100
committerJames Booth <boothj5@gmail.com>2015-08-25 23:04:21 +0100
commit55c2d1cc21974936e5b431de49ae81e6fc3845be (patch)
tree1c41a0ce6af402503a4b6bc3f06ab040a18fc89d /src
parentfc1ee79190e05ec7682d3a52c27075142c887445 (diff)
downloadprofani-tty-55c2d1cc21974936e5b431de49ae81e6fc3845be.tar.gz
PGP: Display whether contact public key was received or manually set
Diffstat (limited to 'src')
-rw-r--r--src/command/command.c4
-rw-r--r--src/command/commands.c8
-rw-r--r--src/pgp/gpg.c38
-rw-r--r--src/pgp/gpg.h5
4 files changed, 42 insertions, 13 deletions
diff --git a/src/command/command.c b/src/command/command.c
index 95fff417..58a2e295 100644
--- a/src/command/command.c
+++ b/src/command/command.c
@@ -1157,8 +1157,8 @@ static struct cmd_t command_defs[] =
         CMD_ARGS(
             { "libver",                   "Show which version of the libgpgme library is being used." },
             { "keys",                     "List all keys known to the system." },
-            { "contacts",                      "Show contacts with assigned public keys." },
-            { "setkey <contact> <keyid>", "Manually associate a key ID with a JID." },
+            { "contacts",                 "Show contacts with assigned public keys." },
+            { "setkey <contact> <keyid>", "Manually associate a contact with a public key." },
             { "start [<contact>]",        "Start PGP encrypted chat, current contact will be used if not specified." },
             { "end",                      "End PGP encrypted chat with the current recipient." },
             { "log on|off",               "Enable or disable plaintext logging of PGP encrypted messages." },
diff --git a/src/command/commands.c b/src/command/commands.c
index 5143329e..99770abd 100644
--- a/src/command/commands.c
+++ b/src/command/commands.c
@@ -4287,8 +4287,12 @@ cmd_pgp(ProfWin *window, const char * const command, gchar **args)
         GList *curr = jids;
         while (curr) {
             char *jid = curr->data;
-            char *pubkey = g_hash_table_lookup(pubkeys, jid);
-            cons_show("  %s: %s", jid, pubkey);
+            ProfPGPPubKeyId *pubkeyid = g_hash_table_lookup(pubkeys, jid);
+            if (pubkeyid->received) {
+                cons_show("  %s: %s (received)", jid, pubkeyid->id);
+            } else {
+                cons_show("  %s: %s (stored)", jid, pubkeyid->id);
+            }
             curr = g_list_next(curr);
         }
         g_list_free(jids);
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
@@ -64,13 +64,22 @@ static char* _add_header_footer(const char * const str, const char * const heade
 static void _save_pubkeys(void);
 
 void
+_p_gpg_free_pubkeyid(ProfPGPPubKeyId *pubkeyid)
+{
+    if (pubkeyid) {
+        free(pubkeyid->id);
+    }
+    free(pubkeyid);
+}
+
+void
 p_gpg_init(void)
 {
     libversion = gpgme_check_version(NULL);
     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);