From dd346eefc4f633db2047694ce49ef91314e5a5b4 Mon Sep 17 00:00:00 2001 From: James Booth Date: Tue, 1 Sep 2015 19:24:56 +0100 Subject: [PATCH 1/6] Added PGP passphrase callback --- src/pgp/gpg.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/pgp/gpg.c b/src/pgp/gpg.c index cda77e13..aeb88e5a 100644 --- a/src/pgp/gpg.c +++ b/src/pgp/gpg.c @@ -48,6 +48,7 @@ #include "log.h" #include "common.h" #include "tools/autocomplete.h" +#include "ui/ui.h" #define PGP_SIGNATURE_HEADER "-----BEGIN PGP SIGNATURE-----" #define PGP_SIGNATURE_FOOTER "-----END PGP SIGNATURE-----" @@ -75,6 +76,23 @@ _p_gpg_free_pubkeyid(ProfPGPPubKeyId *pubkeyid) free(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) +{ + cons_show("Passphrase callback"); + if (uid_hint) { + cons_show(" uid_hind: %s", uid_hint); + } + if (passphrase_info) { + cons_show(" passphrase_info: %s", passphrase_info); + } + if (prev_was_bad) { + cons_show(" prev_was_bad"); + } + gpgme_io_writen(fd, "password\n", strlen("password\n")); + return 0; +} + void p_gpg_init(void) { @@ -158,6 +176,8 @@ 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; @@ -217,6 +237,8 @@ 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); @@ -281,6 +303,8 @@ 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; @@ -379,6 +403,8 @@ 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); @@ -420,6 +446,8 @@ 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); @@ -470,6 +498,8 @@ p_gpg_sign(const char * const str, const char * const fp) return NULL; } + gpgme_set_passphrase_cb(ctx, (gpgme_passphrase_cb_t)_p_gpg_passphrase_cb, NULL); + gpgme_key_t key = NULL; error = gpgme_get_key(ctx, fp, &key, 1); @@ -551,6 +581,8 @@ 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); @@ -605,6 +637,8 @@ p_gpg_decrypt(const char * const cipher) return NULL; } + gpgme_set_passphrase_cb(ctx, (gpgme_passphrase_cb_t)_p_gpg_passphrase_cb, NULL); + char *cipher_with_headers = _add_header_footer(cipher, PGP_MESSAGE_HEADER, PGP_MESSAGE_FOOTER); gpgme_data_t cipher_data; gpgme_data_new_from_mem(&cipher_data, cipher_with_headers, strlen(cipher_with_headers), 1); From 1f4fd0fcb3ee7e38bfab2ca8613683a0186738d4 Mon Sep 17 00:00:00 2001 From: James Booth Date: Tue, 1 Sep 2015 20:16:04 +0100 Subject: [PATCH 2/6] Added UI function to get PGP passphrase --- src/pgp/gpg.c | 23 +++++++++++++---------- src/ui/core.c | 26 +++++++++++++++++++++++--- src/ui/ui.h | 1 + 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/pgp/gpg.c b/src/pgp/gpg.c index aeb88e5a..96821f0f 100644 --- a/src/pgp/gpg.c +++ b/src/pgp/gpg.c @@ -79,17 +79,20 @@ _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) { - cons_show("Passphrase callback"); - if (uid_hint) { - cons_show(" uid_hind: %s", uid_hint); + 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_info) { - cons_show(" passphrase_info: %s", passphrase_info); - } - if (prev_was_bad) { - cons_show(" prev_was_bad"); - } - gpgme_io_writen(fd, "password\n", strlen("password\n")); + + g_string_append(pass_term, "\n"); + + gpgme_io_writen(fd, pass_term->str, pass_term->len); + + g_string_free(pass_term, TRUE); + return 0; } diff --git a/src/ui/core.c b/src/ui/core.c index 9ee8b9a5..da18ddbf 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -2101,9 +2101,29 @@ ui_win_unread(int index) char * ui_ask_password(void) { - status_bar_get_password(); - status_bar_update_virtual(); - return inp_get_password(); + status_bar_get_password(); + status_bar_update_virtual(); + return inp_get_password(); +} + +char * +ui_ask_pgp_passphrase(const char *hint, int prev_fail) +{ + ProfWin *current = wins_get_current(); + + if (prev_fail) { + win_print(current, '!', 0, NULL, 0, 0, NULL, "Incorrect passphrase"); + } + + if (hint) { + win_vprint(current, '!', 0, NULL, 0, 0, NULL, "Enter PGP key passphrase for %s", hint); + } else { + win_print(current, '!', 0, NULL, 0, 0, NULL, "Enter PGP key passphrase"); + } + + status_bar_get_password(); + status_bar_update_virtual(); + return inp_get_password(); } void diff --git a/src/ui/ui.h b/src/ui/ui.h index e47cbddd..27395048 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -102,6 +102,7 @@ win_type_t ui_win_type(int index); void ui_close_win(int index); int ui_win_unread(int index); char * ui_ask_password(void); +char * ui_ask_pgp_passphrase(const char *hint, int prev_fail); void ui_handle_stanza(const char * const msg); From 7dc1b9d42854af67f1284da9e3d8015c4c15f5fa Mon Sep 17 00:00:00 2001 From: James Booth Date: Tue, 1 Sep 2015 20:33:23 +0100 Subject: [PATCH 3/6] Fixed passing from arg in PGP ask passphrase --- src/ui/core.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ui/core.c b/src/ui/core.c index da18ddbf..91c0f342 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -2112,13 +2112,13 @@ ui_ask_pgp_passphrase(const char *hint, int prev_fail) ProfWin *current = wins_get_current(); if (prev_fail) { - win_print(current, '!', 0, NULL, 0, 0, NULL, "Incorrect passphrase"); + win_print(current, '!', 0, NULL, 0, 0, "", "Incorrect passphrase"); } if (hint) { - win_vprint(current, '!', 0, NULL, 0, 0, NULL, "Enter PGP key passphrase for %s", hint); + win_vprint(current, '!', 0, NULL, 0, 0, "", "Enter PGP key passphrase for %s", hint); } else { - win_print(current, '!', 0, NULL, 0, 0, NULL, "Enter PGP key passphrase"); + win_print(current, '!', 0, NULL, 0, 0, "", "Enter PGP key passphrase"); } status_bar_get_password(); From 53035f5e4cdb1157478ab6602647e1feb67dbe30 Mon Sep 17 00:00:00 2001 From: James Booth Date: Tue, 1 Sep 2015 21:05:17 +0100 Subject: [PATCH 4/6] Save PGP passphrase when correct --- src/pgp/gpg.c | 71 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 23 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,20 +82,27 @@ _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(""); + if (passphrase) { + gpgme_io_writen(fd, passphrase, strlen(passphrase)); + } else { + 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); + char *password = ui_ask_pgp_passphrase(uid_hint, prev_was_bad); + if (password) { + g_string_append(pass_term, password); + free(password); + } + + g_string_append(pass_term, "\n"); + if (passphrase_attempt) { + free(passphrase_attempt); + } + passphrase_attempt = pass_term->str; + g_string_free(pass_term, FALSE); + + gpgme_io_writen(fd, passphrase_attempt, strlen(passphrase_attempt)); } - g_string_append(pass_term, "\n"); - - gpgme_io_writen(fd, pass_term->str, pass_term->len); - - g_string_free(pass_term, TRUE); - 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; } From 4d18c5ff52deb3f4abd8e2029a0be0ba0a30cfbf Mon Sep 17 00:00:00 2001 From: James Booth Date: Tue, 1 Sep 2015 21:08:23 +0100 Subject: [PATCH 5/6] PGP: Added null check for passphrase_attempt --- src/pgp/gpg.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/pgp/gpg.c b/src/pgp/gpg.c index 13a4608f..3d6bdc09 100644 --- a/src/pgp/gpg.c +++ b/src/pgp/gpg.c @@ -581,7 +581,9 @@ p_gpg_sign(const char * const str, const char * const fp) gpgme_free(signed_str); } - passphrase = strdup(passphrase_attempt); + if (passphrase_attempt) { + passphrase = strdup(passphrase_attempt); + } return result; } @@ -710,7 +712,9 @@ p_gpg_decrypt(const char * const cipher) } gpgme_free(plain_str); - passphrase = strdup(passphrase_attempt); + if (passphrase_attempt) { + passphrase = strdup(passphrase_attempt); + } return result; } From b468fd7ae440d423812aee4964975c75ae86b489 Mon Sep 17 00:00:00 2001 From: James Booth Date: Tue, 1 Sep 2015 21:25:58 +0100 Subject: [PATCH 6/6] PGP: UI tweaks for passphrase request --- src/ui/core.c | 4 ++++ src/ui/inputwin.c | 1 - 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ui/core.c b/src/ui/core.c index 91c0f342..5735f462 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -2111,6 +2111,8 @@ ui_ask_pgp_passphrase(const char *hint, int prev_fail) { ProfWin *current = wins_get_current(); + win_println(current, 0, ""); + if (prev_fail) { win_print(current, '!', 0, NULL, 0, 0, "", "Incorrect passphrase"); } @@ -2121,6 +2123,8 @@ ui_ask_pgp_passphrase(const char *hint, int prev_fail) win_print(current, '!', 0, NULL, 0, 0, "", "Enter PGP key passphrase"); } + ui_update(); + status_bar_get_password(); status_bar_update_virtual(); return inp_get_password(); diff --git a/src/ui/inputwin.c b/src/ui/inputwin.c index cb4d7a7e..9b4eddfb 100644 --- a/src/ui/inputwin.c +++ b/src/ui/inputwin.c @@ -230,7 +230,6 @@ inp_get_password(void) { werase(inp_win); wmove(inp_win, 0, 0); - pad_start = 0; _inp_win_update_virtual(); doupdate(); char *password = NULL;