generate a 2048 bit key (default size in gpg)
[claws.git] / src / plugins / pgpcore / sgpgme.c
index 77c0774e6a625a98d969f67a3a455bd372fe1d9a..62fc6b3bf5c2a352ed67aaa3dfc34ca1623de761 100644 (file)
@@ -521,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)
@@ -531,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);
 
@@ -626,19 +619,23 @@ void sgpgme_init()
 #else
                ctype_locale = g_strdup(setlocale(LC_CTYPE, NULL));
 #endif
-               debug_print("setting gpgme locale to: %s\n", ctype_locale ? ctype_locale : "NULL");
-               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);
+               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
                debug_print("setting gpgme MESSAGES locale\n");
@@ -647,19 +644,23 @@ void sgpgme_init()
 #else
                messages_locale = g_strdup(setlocale(LC_MESSAGES, NULL));
 #endif
-               debug_print("setting gpgme locale to: %s\n", messages_locale ? messages_locale : "NULL");
-               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);
+               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) {
@@ -788,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"
@@ -954,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;
        }