generate a 2048 bit key (default size in gpg)
[claws.git] / src / plugins / pgpcore / sgpgme.c
index 8410ab0e0a29c2fbc6ca8b11bebfcb8b0001eca0..62fc6b3bf5c2a352ed67aaa3dfc34ca1623de761 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2009 the Claws Mail team
+ * Copyright (C) 1999-2012 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
@@ -19,6 +19,7 @@
  
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
  
 #ifdef USE_GPGME
@@ -84,7 +85,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) {
@@ -102,12 +102,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:
@@ -211,11 +210,11 @@ gchar *sgpgme_sigstat_info_short(gpgme_ctx_t ctx, gpgme_verify_result_t status)
        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:
                case GPGME_VALIDITY_ULTIMATE:
                        result = g_strdup_printf(_("Good signature from %s."), uname);
                        break;
+               case GPGME_VALIDITY_MARGINAL:
                case GPGME_VALIDITY_UNKNOWN:
                case GPGME_VALIDITY_UNDEFINED:
                case GPGME_VALIDITY_NEVER:
@@ -266,6 +265,8 @@ gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
        sig = status->signatures;
        
        while (sig) {
+               char buf[100];
+               struct tm lt;
                gpgme_user_id_t user = NULL;
                gpgme_key_t key;
                gpgme_error_t err;
@@ -291,9 +292,12 @@ gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
                        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:
@@ -326,9 +330,22 @@ gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
                                j++;
                                user = user->next;
                        }
-                       g_string_append_printf(siginfo,
-                               _("Primary key fingerprint: %s\n"), 
-                               sig ? sig->fpr: "?");
+                       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,
@@ -504,7 +521,7 @@ gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account,
 
        if (config->sign_key != SIGN_KEY_DEFAULT) {
                const gchar *keyid;
-               gpgme_key_t key, key2;
+               gpgme_key_t key, found_key;
                gpgme_error_t err;
 
                if (config->sign_key == SIGN_KEY_BY_FROM)
@@ -514,78 +531,71 @@ gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account,
                else
                        goto bail;
 
-               err = gpgme_op_keylist_start(ctx, keyid, 1);
-               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;
-               }
-               
-               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 && key2 && key2->protocol != gpgme_get_protocol(ctx)) {
-                               debug_print("skipping a key (wrong protocol %d)\n", key2->protocol);
-                               gpgme_key_release(key2);
+                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 (!err && key2 && (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);
+
+                       if (key->expired) {
+                               debug_print("skipping a key, expired");
+                               gpgme_key_release(key);
+                               continue;
                        }
-               } 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"));
-                       goto bail;
-               }
-               
+                       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'\n", keyid);
+                               privacy_set_error(_("Secret key specification is ambiguous"));
+                               goto bail;
+                       }
+
+                       found_key = key;
+                }
                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 (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_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\n",
+                                 gpgme_strerror(err));
+                       privacy_set_error(_("Error setting secret key: %s"),
+                                         gpgme_strerror(err));
                        goto bail;
                }
-       }
+        }
 
        prefs_gpg_account_free_config(config);
 
@@ -597,17 +607,63 @@ 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 (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) {
-#ifndef G_OS_WIN32
                                debug_print("GpgME Protocol: %s\n"
                                            "Version: %s (req %s)\n"
                                            "Executable: %s\n",
@@ -615,7 +671,6 @@ void sgpgme_init()
                                        engineInfo->version ? engineInfo->version:"???",
                                        engineInfo->req_version ? engineInfo->req_version:"???",
                                        engineInfo->file_name ? engineInfo->file_name:"???");
-#endif
                                if (engineInfo->protocol == GPGME_PROTOCOL_OpenPGP
                                &&  gpgme_engine_check_version(engineInfo->protocol) != 
                                        GPG_ERR_NO_ERROR) {
@@ -734,7 +789,7 @@ again:
        
        key_parms = g_strdup_printf("<GnupgKeyParms format=\"internal\">\n"
                                        "Key-Type: DSA\n"
-                                       "Key-Length: 1024\n"
+                                       "Key-Length: 2048\n"
                                        "Subkey-Type: ELG-E\n"
                                        "Subkey-Length: 2048\n"
                                        "Name-Real: %s\n"
@@ -855,15 +910,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)
@@ -893,6 +955,10 @@ void *sgpgme_data_release_and_get_mem(gpgme_data_t data, size_t *len)
        cm_gpgme_data_rewind(data);
        while ((r = gpgme_data_read(data, buf, BUFSIZ)) > 0) {
                result = realloc(result, r + w);
+               if (result == NULL) {
+                       g_warning("can't allocate memory\n");
+                       return NULL;
+               }
                memcpy(result+w, buf, r);
                w += r;
        }