2009-02-15 [colin] 3.7.0cvs66
[claws.git] / src / plugins / pgpcore / sgpgme.c
index baf276eeeca5857969f11127bd637ecf0d7a14c6..8d948381aed71d55500a0c07966a454ca6e5539b 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2007 the Claws Mail team
+ * Copyright (C) 1999-2009 the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -72,7 +72,6 @@ gpgme_verify_result_t sgpgme_verify_signature(gpgme_ctx_t ctx, gpgme_data_t sig,
                debug_print("op_verify err %s\n", gpgme_strerror(err));
                privacy_set_error("%s", gpgme_strerror(err));
                return GINT_TO_POINTER(-GPG_ERR_SYSTEM_ERROR);
-               
        }
        status = gpgme_op_verify_result(ctx);
        if (status && status->signatures == NULL) {
@@ -201,6 +200,9 @@ gchar *sgpgme_sigstat_info_short(gpgme_ctx_t ctx, gpgme_verify_result_t status)
                else
                        g_warning(_("PGP Core: Can't get key - no gpg-agent running."));
                warned = TRUE;
+       } else if (gpg_err_code(err) != GPG_ERR_NO_ERROR && gpg_err_code(err) != GPG_ERR_EOF) {
+               return g_strdup_printf(_("The signature can't be checked - %s"), 
+                       gpgme_strerror(err));
        }
        if (key)
                uname = extract_name(key->uids->uid);
@@ -468,12 +470,16 @@ gchar *sgpgme_get_encrypt_data(GSList *recp_names, gpgme_protocol_t proto)
        return ret;
 }
 
-gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account)
+gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account,
+                             const gchar *from_addr)
 {
        GPGAccountConfig *config;
+       const gchar *signer_addr = account->address;
 
        gpgme_signers_clear(ctx);
 
+       if (from_addr)
+               signer_addr = from_addr;
        config = prefs_gpg_account_get_config(account);
 
        switch(config->sign_key) {
@@ -481,7 +487,7 @@ gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account)
                debug_print("using default gnupg key\n");
                break;
        case SIGN_KEY_BY_FROM:
-               debug_print("using key for %s\n", account->address);
+               debug_print("using key for %s\n", signer_addr);
                break;
        case SIGN_KEY_CUSTOM:
                debug_print("using key for %s\n", config->sign_key_id);
@@ -489,28 +495,71 @@ gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account)
        }
 
        if (config->sign_key != SIGN_KEY_DEFAULT) {
-               gchar *keyid;
+               const gchar *keyid;
                gpgme_key_t key, key2;
                gpgme_error_t err;
 
                if (config->sign_key == SIGN_KEY_BY_FROM)
-                       keyid = account->address;
+                       keyid = signer_addr;
                else if (config->sign_key == SIGN_KEY_CUSTOM)
                        keyid = config->sign_key_id;
                else
                        goto bail;
 
                err = gpgme_op_keylist_start(ctx, keyid, 1);
-               if (!err)
-                       err = gpgme_op_keylist_next(ctx, &key);
+               if (!err) {
+                       do {
+                               err = gpgme_op_keylist_next(ctx, &key);
+                               if (!err && key && key->protocol == gpgme_get_protocol(ctx) &&
+                                   !key->expired && !key->revoked && !key->disabled)
+                                       break;
+                               if (!err && key && key->protocol != gpgme_get_protocol(ctx)) {
+                                       debug_print("skipping a key (wrong protocol %d)\n", key->protocol);
+                                       gpgme_key_release(key);
+                               }
+                               if (!err && key && (key->expired || key->revoked || key->disabled)) {
+                                       
+                                       debug_print("skipping a key");
+                                       if (key->expired) 
+                                               debug_print(" expired");
+                                       if (key->revoked) 
+                                               debug_print(" revoked");
+                                       if (key->disabled) 
+                                               debug_print(" disabled");
+                                       debug_print("\n");
+                                       gpgme_key_release(key);
+                               }
+                       } while (!err);
+               }
                if (err) {
                        g_warning("setup_signers start: %s", gpgme_strerror(err));
                        privacy_set_error(_("Secret key not found (%s)"), gpgme_strerror(err));
                        goto bail;
                }
                
-               err = gpgme_op_keylist_next(ctx, &key2);
+               do {
+                       err = gpgme_op_keylist_next(ctx, &key2);
+                       if (!err && key2 && key2->protocol == gpgme_get_protocol(ctx) &&
+                           !key2->expired && !key2->revoked && !key2->disabled)
+                               break;
+                       if (!err && key && key2->protocol != gpgme_get_protocol(ctx)) {
+                               debug_print("skipping a key (wrong protocol %d)\n", key2->protocol);
+                               gpgme_key_release(key2);
+                       }
+                       if (!err && key && (key2->expired || key2->revoked || key2->disabled)) {
+                                       debug_print("skipping a key");
+                                       if (key2->expired) 
+                                               debug_print(" expired");
+                                       if (key2->revoked) 
+                                               debug_print(" revoked");
+                                       if (key2->disabled) 
+                                               debug_print(" disabled");
+                                       debug_print("\n");
+                               gpgme_key_release(key2);
+                       }
+               } while (!err);
                if (!err) {
+                       gpgme_key_release(key2);
                        g_warning("ambiguous specification of secret key '%s'\n",
                                keyid);
                        privacy_set_error(_("Secret key specification is ambiguous"));
@@ -519,6 +568,8 @@ gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account)
                
                gpgme_op_keylist_end(ctx);
                err = gpgme_signers_add(ctx, key);
+               debug_print("got key (proto %d (pgp %d, smime %d).\n", key->protocol,
+                               GPGME_PROTOCOL_OpenPGP, GPGME_PROTOCOL_CMS);
                gpgme_key_release(key);
                
                if (err) {