aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2015-08-23 22:54:41 +0100
committerJames Booth <boothj5@gmail.com>2015-08-23 22:54:41 +0100
commit57ca441f0236a86f1091a2664da227bac4e2ea59 (patch)
treeadd794e0c9c6e04ae8b56dc5e9d1adccb824bec9
parentc07638746a37c333dc15d7b8597d0e310fe5286c (diff)
downloadprofani-tty-57ca441f0236a86f1091a2664da227bac4e2ea59.tar.gz
Check for valid PGP key on start
-rw-r--r--src/command/commands.c4
-rw-r--r--src/pgp/gpg.c29
-rw-r--r--src/pgp/gpg.h1
3 files changed, 32 insertions, 2 deletions
diff --git a/src/command/commands.c b/src/command/commands.c
index 7167e26d..e2323ce6 100644
--- a/src/command/commands.c
+++ b/src/command/commands.c
@@ -4337,8 +4337,8 @@ cmd_pgp(ProfWin *window, const char * const command, gchar **args)
}
ProfAccount *account = accounts_get_account(jabber_get_account_name());
- if (!account->pgp_keyid) {
- ui_current_print_formatted_line('!', 0, "You must specify a PGP key ID for this account to start PGP encryption.");
+ if (!p_gpg_valid_key(account->pgp_keyid)) {
+ ui_current_print_formatted_line('!', 0, "You must specify a valid PGP key ID for this account to start PGP encryption.");
account_free(account);
return TRUE;
}
diff --git a/src/pgp/gpg.c b/src/pgp/gpg.c
index 3e2cfe67..be35bb3a 100644
--- a/src/pgp/gpg.c
+++ b/src/pgp/gpg.c
@@ -281,6 +281,35 @@ p_gpg_free_key(ProfPGPKey *key)
}
gboolean
+p_gpg_valid_key(const char * const keyid)
+{
+ 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;
+ 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));
+ gpgme_release(ctx);
+ return FALSE;
+ }
+
+ if (key) {
+ gpgme_release(ctx);
+ gpgme_key_unref(key);
+ return TRUE;
+ }
+
+ gpgme_release(ctx);
+ return FALSE;
+}
+
+gboolean
p_gpg_available(const char * const barejid)
{
char *fp = g_hash_table_lookup(fingerprints, barejid);
diff --git a/src/pgp/gpg.h b/src/pgp/gpg.h
index 77f14d1e..7b3a4433 100644
--- a/src/pgp/gpg.h
+++ b/src/pgp/gpg.h
@@ -48,6 +48,7 @@ void p_gpg_on_disconnect(void);
GSList* p_gpg_list_keys(void);
gboolean p_gpg_addkey(const char * const jid, const char * const keyid);
GHashTable* p_gpg_fingerprints(void);
+gboolean p_gpg_valid_key(const char * const keyid);
gboolean p_gpg_available(const char * const barejid);
const char* p_gpg_libver(void);
void p_gpg_free_key(ProfPGPKey *key);