From 57ca441f0236a86f1091a2664da227bac4e2ea59 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 23 Aug 2015 22:54:41 +0100 Subject: [PATCH] Check for valid PGP key on start --- src/command/commands.c | 4 ++-- src/pgp/gpg.c | 29 +++++++++++++++++++++++++++++ src/pgp/gpg.h | 1 + 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 @@ -280,6 +280,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) { 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);