about summary refs log tree commit diff stats
path: root/src/pgp
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2015-09-01 21:05:17 +0100
committerJames Booth <boothj5@gmail.com>2015-09-01 21:05:17 +0100
commit53035f5e4cdb1157478ab6602647e1feb67dbe30 (patch)
tree1b6331b51dd06d4e168e58fca97dfdad7478fc86 /src/pgp
parent7dc1b9d42854af67f1284da9e3d8015c4c15f5fa (diff)
downloadprofani-tty-53035f5e4cdb1157478ab6602647e1feb67dbe30.tar.gz
Save PGP passphrase when correct
Diffstat (limited to 'src/pgp')
-rw-r--r--src/pgp/gpg.c69
1 files changed, 47 insertions, 22 deletions
diff --git a/src/pgp/gpg.c b/src/pgp/gpg.c
index 96821f0f..13a4608f 100644
--- a/src/pgp/gpg.c
+++ b/src/pgp/gpg.c
@@ -61,6 +61,9 @@ static GHashTable *pubkeys;
 static gchar *pubsloc;
 static GKeyFile *pubkeyfile;
 
+static char *passphrase;
+static char *passphrase_attempt;
+
 static Autocomplete key_ac;
 
 static char* _remove_header_footer(char *str, const char * const footer);
@@ -79,19 +82,26 @@ _p_gpg_free_pubkeyid(ProfPGPPubKeyId *pubkeyid)
 static gpgme_error_t *
 _p_gpg_passphrase_cb(void *hook, const char *uid_hint, const char *passphrase_info, int prev_was_bad, int fd)
 {
-    GString *pass_term = g_string_new("");
-
-    char *password = ui_ask_pgp_passphrase(uid_hint, prev_was_bad);
-    if (password) {
-        g_string_append(pass_term, password);
-        free(password);
-    }
+    if (passphrase) {
+        gpgme_io_writen(fd, passphrase, strlen(passphrase));
+    } else {
+        GString *pass_term = g_string_new("");
 
-    g_string_append(pass_term, "\n");
+        char *password = ui_ask_pgp_passphrase(uid_hint, prev_was_bad);
+        if (password) {
+            g_string_append(pass_term, password);
+            free(password);
+        }
 
-    gpgme_io_writen(fd, pass_term->str, pass_term->len);
+        g_string_append(pass_term, "\n");
+        if (passphrase_attempt) {
+            free(passphrase_attempt);
+        }
+        passphrase_attempt = pass_term->str;
+        g_string_free(pass_term, FALSE);
 
-    g_string_free(pass_term, TRUE);
+        gpgme_io_writen(fd, passphrase_attempt, strlen(passphrase_attempt));
+    }
 
     return 0;
 }
@@ -108,6 +118,9 @@ p_gpg_init(void)
     key_ac = autocomplete_new();
     GHashTable *keys = p_gpg_list_keys();
     p_gpg_free_keys(keys);
+
+    passphrase = NULL;
+    passphrase_attempt = NULL;
 }
 
 void
@@ -128,6 +141,16 @@ p_gpg_close(void)
 
     autocomplete_free(key_ac);
     key_ac = NULL;
+
+    if (passphrase) {
+        free(passphrase);
+        passphrase = NULL;
+    }
+
+    if (passphrase_attempt) {
+        free(passphrase_attempt);
+        passphrase_attempt = NULL;
+    }
 }
 
 void
@@ -179,8 +202,6 @@ p_gpg_on_connect(const char * const barejid)
         return;
     }
 
-    gpgme_set_passphrase_cb(ctx, (gpgme_passphrase_cb_t)_p_gpg_passphrase_cb, NULL);
-
     int i = 0;
     for (i = 0; i < len; i++) {
         GError *gerr = NULL;
@@ -228,6 +249,16 @@ p_gpg_on_disconnect(void)
 
     free(pubsloc);
     pubsloc = NULL;
+
+    if (passphrase) {
+        free(passphrase);
+        passphrase = NULL;
+    }
+
+    if (passphrase_attempt) {
+        free(passphrase_attempt);
+        passphrase_attempt = NULL;
+    }
 }
 
 gboolean
@@ -240,8 +271,6 @@ p_gpg_addkey(const char * const jid, const char * const keyid)
         return FALSE;
     }
 
-    gpgme_set_passphrase_cb(ctx, (gpgme_passphrase_cb_t)_p_gpg_passphrase_cb, NULL);
-
     gpgme_key_t key = NULL;
     error = gpgme_get_key(ctx, keyid, &key, 0);
     gpgme_release(ctx);
@@ -306,8 +335,6 @@ p_gpg_list_keys(void)
         return NULL;
     }
 
-    gpgme_set_passphrase_cb(ctx, (gpgme_passphrase_cb_t)_p_gpg_passphrase_cb, NULL);
-
     error = gpgme_op_keylist_start(ctx, NULL, 0);
     if (error == GPG_ERR_NO_ERROR) {
         gpgme_key_t key;
@@ -406,8 +433,6 @@ p_gpg_valid_key(const char * const keyid)
         return FALSE;
     }
 
-    gpgme_set_passphrase_cb(ctx, (gpgme_passphrase_cb_t)_p_gpg_passphrase_cb, NULL);
-
     gpgme_key_t key = NULL;
     error = gpgme_get_key(ctx, keyid, &key, 1);
 
@@ -449,8 +474,6 @@ p_gpg_verify(const char * const barejid, const char *const sign)
         return;
     }
 
-    gpgme_set_passphrase_cb(ctx, (gpgme_passphrase_cb_t)_p_gpg_passphrase_cb, NULL);
-
     char *sign_with_header_footer = _add_header_footer(sign, PGP_SIGNATURE_HEADER, PGP_SIGNATURE_FOOTER);
     gpgme_data_t sign_data;
     gpgme_data_new_from_mem(&sign_data, sign_with_header_footer, strlen(sign_with_header_footer), 1);
@@ -558,6 +581,8 @@ p_gpg_sign(const char * const str, const char * const fp)
         gpgme_free(signed_str);
     }
 
+    passphrase = strdup(passphrase_attempt);
+
     return result;
 }
 
@@ -584,8 +609,6 @@ p_gpg_encrypt(const char * const barejid, const char * const message)
         return NULL;
     }
 
-    gpgme_set_passphrase_cb(ctx, (gpgme_passphrase_cb_t)_p_gpg_passphrase_cb, NULL);
-
     gpgme_key_t key;
     error = gpgme_get_key(ctx, pubkeyid->id, &key, 0);
 
@@ -687,6 +710,8 @@ p_gpg_decrypt(const char * const cipher)
     }
     gpgme_free(plain_str);
 
+    passphrase = strdup(passphrase_attempt);
+
     return result;
 }