Cosmetic: use always g_getenv instead of getenv
[claws.git] / src / plugins / pgpcore / sgpgme.c
index 918127ecd8f424f57bf048e2fcdd9aab0a8fc7f7..7afe4bfe82dee98a36da6fc8222472e7c4e294ea 100644 (file)
@@ -1,10 +1,10 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2007 Hiroyuki Yamamoto & the Claws Mail team
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2015 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
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
+
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
+
 #ifdef USE_GPGME
 
 #include <time.h>
 #include <glib.h>
 #include <glib/gi18n.h>
 #include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
 #include <errno.h>
 #include <sys/types.h>
-#include <sys/wait.h>
-#if (defined(__DragonFly__) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__))
+#ifndef G_OS_WIN32
+#  include <sys/wait.h>
+#endif
+#if (defined(__DragonFly__) || defined(SOLARIS) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__))
 #  include <sys/signal.h>
 #endif
 #ifndef G_OS_WIN32
@@ -66,9 +70,8 @@ gpgme_verify_result_t sgpgme_verify_signature(gpgme_ctx_t ctx, gpgme_data_t sig,
 
        if ((err = gpgme_op_verify(ctx, sig, plain, dummy)) != GPG_ERR_NO_ERROR) {
                debug_print("op_verify err %s\n", gpgme_strerror(err));
-               privacy_set_error(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) {
@@ -81,7 +84,6 @@ gpgme_verify_result_t sgpgme_verify_signature(gpgme_ctx_t ctx, gpgme_data_t sig,
 
 SignatureStatus sgpgme_sigstat_gpgme_to_privacy(gpgme_ctx_t ctx, gpgme_verify_result_t status)
 {
-       unsigned long validity = 0;
        gpgme_signature_t sig = NULL;
        
        if (GPOINTER_TO_INT(status) == -GPG_ERR_SYSTEM_ERROR) {
@@ -99,12 +101,11 @@ SignatureStatus sgpgme_sigstat_gpgme_to_privacy(gpgme_ctx_t ctx, gpgme_verify_re
                debug_print("sig == NULL\n");
                return SIGNATURE_UNCHECKED;
        }
-       validity = sig->validity;
 
        debug_print("err code %d\n", gpg_err_code(sig->status));
        switch (gpg_err_code(sig->status)) {
        case GPG_ERR_NO_ERROR:
-               switch (gpg_err_code(sig->validity)) {
+               switch (sig->validity) {
                case GPGME_VALIDITY_NEVER:
                        return SIGNATURE_INVALID;
                case GPGME_VALIDITY_UNKNOWN:
@@ -117,8 +118,10 @@ SignatureStatus sgpgme_sigstat_gpgme_to_privacy(gpgme_ctx_t ctx, gpgme_verify_re
                        return SIGNATURE_CHECK_FAILED;
                }
        case GPG_ERR_SIG_EXPIRED:
-       case GPG_ERR_KEY_EXPIRED:
+       case GPG_ERR_CERT_REVOKED:
                return SIGNATURE_WARN;
+       case GPG_ERR_KEY_EXPIRED:
+               return SIGNATURE_KEY_EXPIRED;
        case GPG_ERR_BAD_SIGNATURE:
                return SIGNATURE_INVALID;
        case GPG_ERR_NO_PUBKEY:
@@ -149,6 +152,22 @@ static const gchar *get_validity_str(unsigned long validity)
        }
 }
 
+static const gchar *get_owner_trust_str(unsigned long owner_trust)
+{
+       switch (gpgme_err_code(owner_trust)) {
+       case GPGME_VALIDITY_NEVER:
+               return _("Untrusted");
+       case GPGME_VALIDITY_MARGINAL:
+               return _("Marginal");
+       case GPGME_VALIDITY_FULL:
+               return _("Full");
+       case GPGME_VALIDITY_ULTIMATE:
+               return _("Ultimate");
+       default:
+               return _("Unknown");
+       }
+}
+
 static gchar *extract_name(const char *uid)
 {
        if (uid == NULL)
@@ -193,46 +212,68 @@ gchar *sgpgme_sigstat_info_short(gpgme_ctx_t ctx, gpgme_verify_result_t status)
                if (!warned)
                        alertpanel_error(_("PGP Core: Can't get key - no gpg-agent running."));
                else
-                       g_warning(_("PGP Core: Can't get key - no gpg-agent running."));
+                       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));
+       } else if (gpg_err_code(err) != GPG_ERR_NO_ERROR && gpg_err_code(err) == GPG_ERR_EOF &&
+                  key == NULL) {
+               /*
+                * When gpg is upgraded to gpg-v21 then installer tries to migrate the old
+                * gpg keyrings found in ~/.gnupg to the new version. If the keyrings contain
+                * very old keys using ciphers no more supported in gpg-v21 this transition
+                * can fail and the left-over ~/.gnupg/pubring.gpg will cause claws to crash
+                * when the above condition is meet.
+                */
+               return g_strdup_printf(_("The signature can't be checked - %s."),
+                        gpgme_strerror(err));
+        }
        if (key)
                uname = extract_name(key->uids->uid);
        else
                uname = g_strdup("<?>");
+
        switch (gpg_err_code(sig->status)) {
        case GPG_ERR_NO_ERROR:
-               switch (gpg_err_code(sig->validity)) {
-               case GPGME_VALIDITY_MARGINAL:
-               case GPGME_VALIDITY_FULL:
+               switch (key->uids?key->uids->validity:GPGME_VALIDITY_UNKNOWN) {
                case GPGME_VALIDITY_ULTIMATE:
-                       result = g_strdup_printf(_("Good signature from %s."), uname);
+                       result = g_strdup_printf(_("Good signature from \"%s\" [ultimate]"), uname);
+                       break;
+               case GPGME_VALIDITY_FULL:
+                       result = g_strdup_printf(_("Good signature from \"%s\" [full]"), uname);
+                       break;
+               case GPGME_VALIDITY_MARGINAL:
+                       result = g_strdup_printf(_("Good signature from \"%s\" [marginal]"), uname);
                        break;
                case GPGME_VALIDITY_UNKNOWN:
                case GPGME_VALIDITY_UNDEFINED:
                case GPGME_VALIDITY_NEVER:
                default:
-                       result = g_strdup_printf(_("Good signature (untrusted) from %s."), uname);
+                       result = g_strdup_printf(_("Good signature from \"%s\""), uname);
                        break;
-               }
+               }
                break;
        case GPG_ERR_SIG_EXPIRED:
-               result = g_strdup_printf(_("Expired signature from %s."), uname);
+               result = g_strdup_printf(_("Expired signature from \"%s\""), uname);
                break;
        case GPG_ERR_KEY_EXPIRED:
-               result = g_strdup_printf(_("Expired key from %s."), uname);
+               result = g_strdup_printf(_("Good signature from \"%s\", but the key has expired"), uname);
+               break;
+       case GPG_ERR_CERT_REVOKED:
+               result = g_strdup_printf(_("Good signature from \"%s\", but the key has been revoked"), uname);
                break;
        case GPG_ERR_BAD_SIGNATURE:
-               result = g_strdup_printf(_("Bad signature from %s."), uname);
+               result = g_strdup_printf(_("Bad signature from \"%s\""), uname);
                break;
        case GPG_ERR_NO_PUBKEY: {
                gchar *id = g_strdup(sig->fpr + strlen(sig->fpr)-8);
-               result = g_strdup_printf(_("Key 0x%s not available to verify this signature."), id);
+               result = g_strdup_printf(_("Key 0x%s not available to verify this signature"), id);
                g_free(id);
                break;
                }
        default:
-               result = g_strdup(_("The signature has not been checked."));
+               result = g_strdup(_("The signature has not been checked"));
                break;
        }
        if (result == NULL)
@@ -246,42 +287,69 @@ gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
        gint i = 0;
        gchar *ret;
        GString *siginfo;
-       gpgme_signature_t sig = status->signatures;
-       
+       gpgme_signature_t sig = NULL;
+
        siginfo = g_string_sized_new(64);
+       if (status == NULL) {
+               g_string_append_printf(siginfo,
+                       _("Error checking signature: no status\n"));
+               goto bail;
+        }
+
+       sig = status->signatures;
+       
        while (sig) {
-               gpgme_user_id_t user = NULL;
+               char buf[100];
+               struct tm lt;
                gpgme_key_t key;
-
+               gpgme_error_t err;
                const gchar *keytype, *keyid, *uid;
                
-               gpgme_get_key(ctx, sig->fpr, &key, 0);
+               err = gpgme_get_key(ctx, sig->fpr, &key, 0);
 
+               if (err != GPG_ERR_NO_ERROR) {
+                       key = NULL;
+                       g_string_append_printf(siginfo, 
+                               _("Error checking signature: %s\n"),
+                               gpgme_strerror(err));
+                       goto bail;
+               }
                if (key) {
-                       user = key->uids;
                        keytype = gpgme_pubkey_algo_name(
                                        key->subkeys->pubkey_algo);
                        keyid = key->subkeys->keyid;
-                       uid = user->uid;
+                       uid = key->uids->uid;
                } else {
                        keytype = "?";
                        keyid = "?";
                        uid = "?";
                }
+
+               memset(buf, 0, sizeof(buf));
+               fast_strftime(buf, sizeof(buf)-1, prefs_common_get_prefs()->date_format, localtime_r(&sig->timestamp, &lt));
                g_string_append_printf(siginfo,
-                       _("Signature made using %s key ID %s\n"),
-                       keytype, keyid);
+                       _("Signature made on %s using %s key ID %s\n"),
+                       buf, keytype, keyid);
                
                switch (gpg_err_code(sig->status)) {
                case GPG_ERR_NO_ERROR:
+                       g_string_append_printf(siginfo,
+                               _("Good signature from uid \"%s\" (Validity: %s)\n"),
+                               uid, get_validity_str((key && key->uids) ? key->uids->validity:GPGME_VALIDITY_UNKNOWN));
+                       break;
                case GPG_ERR_KEY_EXPIRED:
                        g_string_append_printf(siginfo,
-                               _("Good signature from \"%s\" (Trust: %s)\n"),
-                               uid, get_validity_str(sig->validity));
+                               _("Expired key uid \"%s\"\n"),
+                               uid);
                        break;
                case GPG_ERR_SIG_EXPIRED:
                        g_string_append_printf(siginfo,
-                               _("Expired signature from \"%s\"\n"),
+                               _("Expired signature from uid \"%s\" (Validity: %s)\n"),
+                               uid, get_validity_str((key && key->uids) ? key->uids->validity:GPGME_VALIDITY_UNKNOWN));
+                       break;
+               case GPG_ERR_CERT_REVOKED:
+                       g_string_append_printf(siginfo,
+                               _("Revoked key uid \"%s\"\n"),
                                uid);
                        break;
                case GPG_ERR_BAD_SIGNATURE:
@@ -294,17 +362,36 @@ gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
                }
                if (sig->status != GPG_ERR_BAD_SIGNATURE) {
                        gint j = 1;
-                       user = user ? user->next : NULL;
-                       while (user != NULL) {
-                               g_string_append_printf(siginfo,
-                                       _("                aka \"%s\"\n"),
-                                       user->uid);
-                               j++;
-                               user = user->next;
+                       if (key) {
+                               key->uids = key->uids ? key->uids->next : NULL;
+                               while (key->uids != NULL) {
+                                       g_string_append_printf(siginfo,
+                                               g_strconcat("                    ",
+                                                           _("uid \"%s\" (Validity: %s)\n"), NULL),
+                                               key->uids->uid,
+                                               key->uids->revoked==TRUE?_("Revoked"):get_validity_str(key->uids->validity));
+                                       j++;
+                                       key->uids = key->uids->next;
+                               }
                        }
-                       g_string_append_printf(siginfo,
-                               _("Primary key fingerprint: %s\n"), 
-                               sig ? sig->fpr: "?");
+                       g_string_append_printf(siginfo,_("Owner Trust: %s\n"),
+                                              key ? get_owner_trust_str(key->owner_trust) : _("No key!"));
+                       g_string_append(siginfo,
+                               _("Primary key fingerprint:"));
+                       const char* primary_fpr = NULL;
+                       if (key && key->subkeys && key->subkeys->fpr)
+                               primary_fpr = key->subkeys->fpr;
+                       else
+                               g_string_append(siginfo, " ?");
+                       int idx; /* now pretty-print the fingerprint */
+                       for (idx=0; primary_fpr && *primary_fpr!='\0'; idx++, primary_fpr++) {
+                               if (idx%4==0)
+                                       g_string_append_c(siginfo, ' ');
+                               if (idx%20==0)
+                                       g_string_append_c(siginfo, ' ');
+                               g_string_append_c(siginfo, (gchar)*primary_fpr);
+                       }
+                       g_string_append_c(siginfo, '\n');
 #ifdef HAVE_GPGME_PKA_TRUST
                         if (sig->pka_trust == 1 && sig->pka_address) {
                                 g_string_append_printf(siginfo,
@@ -326,7 +413,7 @@ gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
                i++;
                sig = sig->next;
        }
-
+bail:
        ret = siginfo->str;
        g_string_free(siginfo, FALSE);
        return ret;
@@ -337,20 +424,12 @@ gpgme_data_t sgpgme_data_from_mimeinfo(MimeInfo *mimeinfo)
        gpgme_data_t data = NULL;
        gpgme_error_t err;
        FILE *fp = g_fopen(mimeinfo->data.filename, "rb");
-       gchar *tmp_file = NULL;
 
        if (!fp) 
                return NULL;
 
-       tmp_file = get_tmp_file();
-       copy_file_part(fp, mimeinfo->offset, mimeinfo->length, tmp_file);
+       err = gpgme_data_new_from_filepart(&data, NULL, fp, mimeinfo->offset, mimeinfo->length);
        fclose(fp);
-       fp = NULL;
-       debug_print("tmp file %s\n", tmp_file);
-       
-       err = gpgme_data_new_from_file(&data, tmp_file, 1);
-       g_unlink(tmp_file);
-       g_free(tmp_file);
 
        debug_print("data %p (%d %d)\n", (void *)&data, mimeinfo->offset, mimeinfo->length);
        if (err) {
@@ -377,11 +456,13 @@ gpgme_data_t sgpgme_decrypt_verify(gpgme_data_t cipher, gpgme_verify_result_t *s
        }
        
        if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
-               if (!getenv("GPG_AGENT_INFO") || !prefs_gpg_get_config()->use_gpg_agent) {
+               prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
+               if (!g_getenv("GPG_AGENT_INFO") || !prefs_gpg_get_config()->use_gpg_agent) {
                        info.c = ctx;
                        gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
                }
        } else {
+               prefs_gpg_enable_agent(TRUE);
                info.c = ctx;
                gpgme_set_passphrase_cb (ctx, NULL, &info);
        }
@@ -397,9 +478,9 @@ gpgme_data_t sgpgme_decrypt_verify(gpgme_data_t cipher, gpgme_verify_result_t *s
                        return NULL;
                }
 
-               err = gpgme_data_rewind(plain);
+               err = cm_gpgme_data_rewind(plain);
                if (err) {
-                       debug_print("can't seek (%d %d %s)\n", err, errno, strerror(errno));
+                       debug_print("can't seek (%d %d %s)\n", err, errno, g_strerror(errno));
                }
 
                debug_print("decrypted.\n");
@@ -414,9 +495,9 @@ gpgme_data_t sgpgme_decrypt_verify(gpgme_data_t cipher, gpgme_verify_result_t *s
                        return NULL;
                }
 
-               err = gpgme_data_rewind(plain);
+               err = cm_gpgme_data_rewind(plain);
                if (err) {
-                       debug_print("can't seek (%d %d %s)\n", err, errno, strerror(errno));
+                       debug_print("can't seek (%d %d %s)\n", err, errno, g_strerror(errno));
                }
 
                debug_print("decrypted.\n");
@@ -452,12 +533,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) {
@@ -465,7 +550,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);
@@ -473,44 +558,82 @@ gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account)
        }
 
        if (config->sign_key != SIGN_KEY_DEFAULT) {
-               gchar *keyid;
-               gpgme_key_t key, key2;
+               const gchar *keyid;
+               gpgme_key_t key, found_key;
                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) {
+                found_key = NULL;
+               /* Look for any key, not just private ones, or GPGMe doesn't
+                * correctly set the revoked flag. */
+               err = gpgme_op_keylist_start(ctx, keyid, 0);
+               while ((err = gpgme_op_keylist_next(ctx, &key)) == 0) {
+                       if (key == NULL)
+                               continue;
+
+                       if (!key->can_sign)
+                               continue;
+
+                       if (key->protocol != gpgme_get_protocol(ctx)) {
+                               debug_print("skipping a key (wrong protocol %d)\n", key->protocol);
+                               gpgme_key_release(key);
+                               continue;
+                       }
+
+                       if (key->expired) {
+                               debug_print("skipping a key, expired");
+                               gpgme_key_release(key);
+                               continue;
+                       }
+                       if (key->revoked) {
+                               debug_print("skipping a key, revoked");
+                               gpgme_key_release(key);
+                               continue;
+                       }
+                       if (key->disabled) {
+                               debug_print("skipping a key, disabled");
+                               gpgme_key_release(key);
+                               continue;
+                       }
+
+                       if (found_key != NULL) {
+                               gpgme_key_release(key);
+                               gpgme_op_keylist_end(ctx);
+                               g_warning("ambiguous specification of secret key '%s'", keyid);
+                               privacy_set_error(_("Secret key specification is ambiguous"));
+                               goto bail;
+                       }
+
+                       found_key = key;
+                }
+               gpgme_op_keylist_end(ctx);
+
+               if (found_key == NULL) {
                        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);
-               if (!err) {
-                       g_warning("ambiguous specification of secret key '%s'\n",
-                               keyid);
-                       privacy_set_error(_("Secret key specification is ambiguous"));
-                       goto bail;
-               }
-               
-               gpgme_op_keylist_end(ctx);
-               err = gpgme_signers_add(ctx, key);
-               gpgme_key_release(key);
-               
+                }
+
+               err = gpgme_signers_add(ctx, found_key);
+               debug_print("got key (proto %d (pgp %d, smime %d).\n",
+                           found_key->protocol, GPGME_PROTOCOL_OpenPGP,
+                           GPGME_PROTOCOL_CMS);
+               gpgme_key_release(found_key);
+
                if (err) {
-                       g_warning("error adding secret key: %s\n", gpgme_strerror(err));
-                       privacy_set_error(_("Error setting secret key: %s"), gpgme_strerror(err));
+                       g_warning("error adding secret key: %s",
+                                 gpgme_strerror(err));
+                       privacy_set_error(_("Error setting secret key: %s"),
+                                         gpgme_strerror(err));
                        goto bail;
                }
-       }
+        }
 
        prefs_gpg_account_free_config(config);
 
@@ -522,20 +645,72 @@ bail:
 
 void sgpgme_init()
 {
+       gchar *ctype_locale = NULL, *messages_locale = NULL;
+       gchar *ctype_utf8_locale = NULL, *messages_utf8_locale = NULL;
+
        gpgme_engine_info_t engineInfo;
+
+       if (strcmp(prefs_gpg_get_config()->gpg_path, "") != 0 &&
+           access(prefs_gpg_get_config()->gpg_path, X_OK) != -1)
+               gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP,prefs_gpg_get_config()->gpg_path, NULL);
+
        if (gpgme_check_version("1.0.0")) {
 #ifdef LC_CTYPE
-               gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
+               debug_print("setting gpgme CTYPE locale\n");
+#ifdef G_OS_WIN32
+               ctype_locale = g_win32_getlocale();
+#else
+               ctype_locale = g_strdup(setlocale(LC_CTYPE, NULL));
+#endif
+               if (ctype_locale) {
+                       debug_print("setting gpgme CTYPE locale to: %s\n", ctype_locale);
+                       if (strchr(ctype_locale, '.'))
+                               *(strchr(ctype_locale, '.')) = '\0';
+                       else if (strchr(ctype_locale, '@'))
+                               *(strchr(ctype_locale, '@')) = '\0';
+                       ctype_utf8_locale = g_strconcat(ctype_locale, ".UTF-8", NULL);
+
+                       debug_print("setting gpgme locale to UTF8: %s\n", ctype_utf8_locale ? ctype_utf8_locale : "NULL");
+                       gpgme_set_locale(NULL, LC_CTYPE, ctype_utf8_locale);
+
+                       debug_print("done\n");
+                       g_free(ctype_utf8_locale);
+                       g_free(ctype_locale);
+               } else {
+                       debug_print("couldn't set gpgme CTYPE locale\n");
+               }
 #endif
 #ifdef LC_MESSAGES
-               gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
+               debug_print("setting gpgme MESSAGES locale\n");
+#ifdef G_OS_WIN32
+               messages_locale = g_win32_getlocale();
+#else
+               messages_locale = g_strdup(setlocale(LC_MESSAGES, NULL));
+#endif
+               if (messages_locale) {
+                       debug_print("setting gpgme MESSAGES locale to: %s\n", messages_locale);
+                       if (strchr(messages_locale, '.'))
+                               *(strchr(messages_locale, '.')) = '\0';
+                       else if (strchr(messages_locale, '@'))
+                               *(strchr(messages_locale, '@')) = '\0';
+                       messages_utf8_locale = g_strconcat(messages_locale, ".UTF-8", NULL);
+                       debug_print("setting gpgme locale to UTF8: %s\n", messages_utf8_locale ? messages_utf8_locale : "NULL");
+
+                       gpgme_set_locale(NULL, LC_MESSAGES, messages_utf8_locale);
+
+                       debug_print("done\n");
+                       g_free(messages_utf8_locale);
+                       g_free(messages_locale);
+               } else {
+                       debug_print("couldn't set gpgme MESSAGES locale\n");
+               }
 #endif
                if (!gpgme_get_engine_info(&engineInfo)) {
                        while (engineInfo) {
                                debug_print("GpgME Protocol: %s\n"
                                            "Version: %s (req %s)\n"
                                            "Executable: %s\n",
-                                       gpgme_get_protocol_name(engineInfo->protocol),
+                                       gpgme_get_protocol_name(engineInfo->protocol) ? gpgme_get_protocol_name(engineInfo->protocol):"???",
                                        engineInfo->version ? engineInfo->version:"???",
                                        engineInfo->req_version ? engineInfo->req_version:"???",
                                        engineInfo->file_name ? engineInfo->file_name:"???");
@@ -656,9 +831,9 @@ again:
        }
        
        key_parms = g_strdup_printf("<GnupgKeyParms format=\"internal\">\n"
-                                       "Key-Type: DSA\n"
-                                       "Key-Length: 1024\n"
-                                       "Subkey-Type: ELG-E\n"
+                                       "Key-Type: RSA\n"
+                                       "Key-Length: 2048\n"
+                                       "Subkey-Type: RSA\n"
                                        "Subkey-Length: 2048\n"
                                        "Name-Real: %s\n"
                                        "Name-Email: %s\n"
@@ -696,7 +871,7 @@ again:
        err = gpgme_op_genkey(ctx, key_parms, NULL, NULL);
        g_free(key_parms);
 
-       gtk_widget_destroy(window);
+       label_window_destroy(window);
 
        if (err) {
                alertpanel_error(_("Couldn't generate a new key pair: %s"), gpgme_strerror(err));
@@ -778,15 +953,22 @@ gboolean sgpgme_has_secret_key(void)
                debug_print("err : %s\n", gpgme_strerror(err));
                return TRUE;
        }
+check_again:
        err = gpgme_op_keylist_start(ctx, NULL, TRUE);
        if (!err)
                err = gpgme_op_keylist_next(ctx, &key);
        gpgme_op_keylist_end(ctx);
-       gpgme_release(ctx);
-       if (gpg_err_code(err) == GPG_ERR_EOF)
+       if (gpg_err_code(err) == GPG_ERR_EOF) {
+               if (gpgme_get_protocol(ctx) != GPGME_PROTOCOL_CMS) {
+                       gpgme_set_protocol(ctx, GPGME_PROTOCOL_CMS);
+                       goto check_again;
+               }
+               gpgme_release(ctx);
                return FALSE;
-       else
+       } else {
+               gpgme_release(ctx);
                return TRUE;
+       }
 }
 
 void sgpgme_check_create_key(void)
@@ -799,4 +981,55 @@ void sgpgme_check_create_key(void)
                prefs_gpg_save_config();
        }       
 }
+
+void *sgpgme_data_release_and_get_mem(gpgme_data_t data, size_t *len)
+{
+       char buf[BUFSIZ];
+       void *result = NULL;
+       ssize_t r = 0;
+       size_t w = 0;
+       
+       if (data == NULL)
+               return NULL;
+       if (len == NULL)
+               return NULL;
+
+       /* I know it's deprecated, but we don't compile with _LARGEFILE */
+       cm_gpgme_data_rewind(data);
+       while ((r = gpgme_data_read(data, buf, BUFSIZ)) > 0) {
+               void *rresult = realloc(result, r + w);
+               if (rresult == NULL) {
+                       g_warning("can't allocate memory");
+                       if (result != NULL)
+                               free(result);
+                       return NULL;
+               }
+               result = rresult;
+               memcpy(result+w, buf, r);
+               w += r;
+       }
+       
+       *len = w;
+
+       gpgme_data_release(data);
+       if (r < 0) {
+               free(result);
+               *len = 0;
+               return NULL;
+       }
+       return result;
+}
+
+gpgme_error_t cm_gpgme_data_rewind(gpgme_data_t dh)
+{
+#if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
+       if (gpgme_data_seek(dh, (off_t)0, SEEK_SET) == -1)
+               return gpg_error_from_errno(errno);
+       else
+               return 0;
+#else
+       return gpgme_data_rewind(dh);
+#endif
+}
+
 #endif /* USE_GPGME */