2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2015 the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "claws-features.h"
31 #include <glib/gi18n.h>
36 #include <sys/types.h>
38 # include <sys/wait.h>
40 #if (defined(__DragonFly__) || defined(SOLARIS) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__))
41 # include <sys/signal.h>
52 #include "prefs_common.h"
54 #include "alertpanel.h"
55 #include "passphrase.h"
56 #include "prefs_gpg.h"
58 #include "select-keys.h"
60 static void sgpgme_disable_all(void)
62 /* FIXME: set a flag, so that we don't bother the user with failed
66 gpgme_verify_result_t sgpgme_verify_signature(gpgme_ctx_t ctx, gpgme_data_t sig,
67 gpgme_data_t plain, gpgme_data_t dummy)
69 gpgme_verify_result_t status = NULL;
72 if ((err = gpgme_op_verify(ctx, sig, plain, dummy)) != GPG_ERR_NO_ERROR) {
73 debug_print("op_verify err %s\n", gpgme_strerror(err));
74 privacy_set_error("%s", gpgme_strerror(err));
75 return GINT_TO_POINTER(-GPG_ERR_SYSTEM_ERROR);
77 status = gpgme_op_verify_result(ctx);
78 if (status && status->signatures == NULL) {
79 debug_print("no signature found\n");
80 privacy_set_error(_("No signature found"));
81 return GINT_TO_POINTER(-GPG_ERR_SYSTEM_ERROR);
86 SignatureStatus sgpgme_sigstat_gpgme_to_privacy(gpgme_ctx_t ctx, gpgme_verify_result_t status)
88 gpgme_signature_t sig = NULL;
90 if (GPOINTER_TO_INT(status) == -GPG_ERR_SYSTEM_ERROR) {
91 debug_print("system error\n");
92 return SIGNATURE_CHECK_FAILED;
96 debug_print("status == NULL\n");
97 return SIGNATURE_UNCHECKED;
99 sig = status->signatures;
102 debug_print("sig == NULL\n");
103 return SIGNATURE_UNCHECKED;
106 debug_print("err code %d\n", gpg_err_code(sig->status));
107 switch (gpg_err_code(sig->status)) {
108 case GPG_ERR_NO_ERROR:
109 switch (sig->validity) {
110 case GPGME_VALIDITY_NEVER:
111 return SIGNATURE_INVALID;
112 case GPGME_VALIDITY_UNKNOWN:
113 case GPGME_VALIDITY_UNDEFINED:
114 case GPGME_VALIDITY_MARGINAL:
115 case GPGME_VALIDITY_FULL:
116 case GPGME_VALIDITY_ULTIMATE:
119 return SIGNATURE_CHECK_FAILED;
121 case GPG_ERR_SIG_EXPIRED:
122 case GPG_ERR_CERT_REVOKED:
123 return SIGNATURE_WARN;
124 case GPG_ERR_KEY_EXPIRED:
125 return SIGNATURE_KEY_EXPIRED;
126 case GPG_ERR_BAD_SIGNATURE:
127 return SIGNATURE_INVALID;
128 case GPG_ERR_NO_PUBKEY:
129 return SIGNATURE_CHECK_FAILED;
131 return SIGNATURE_CHECK_FAILED;
133 return SIGNATURE_CHECK_FAILED;
136 static const gchar *get_validity_str(unsigned long validity)
138 switch (gpg_err_code(validity)) {
139 case GPGME_VALIDITY_UNKNOWN:
141 case GPGME_VALIDITY_UNDEFINED:
142 return _("Undefined");
143 case GPGME_VALIDITY_NEVER:
145 case GPGME_VALIDITY_MARGINAL:
146 return _("Marginal");
147 case GPGME_VALIDITY_FULL:
149 case GPGME_VALIDITY_ULTIMATE:
150 return _("Ultimate");
156 static const gchar *get_owner_trust_str(unsigned long owner_trust)
158 switch (gpgme_err_code(owner_trust)) {
159 case GPGME_VALIDITY_NEVER:
160 return _("Untrusted");
161 case GPGME_VALIDITY_MARGINAL:
162 return _("Marginal");
163 case GPGME_VALIDITY_FULL:
165 case GPGME_VALIDITY_ULTIMATE:
166 return _("Ultimate");
172 static gchar *extract_name(const char *uid)
176 if (!strncmp(uid, "CN=", 3)) {
177 gchar *result = g_strdup(uid+3);
178 if (strstr(result, ","))
179 *(strstr(result, ",")) = '\0';
181 } else if (strstr(uid, ",CN=")) {
182 gchar *result = g_strdup(strstr(uid, ",CN=")+4);
183 if (strstr(result, ","))
184 *(strstr(result, ",")) = '\0';
187 return g_strdup(uid);
190 gchar *sgpgme_sigstat_info_short(gpgme_ctx_t ctx, gpgme_verify_result_t status)
192 gpgme_signature_t sig = NULL;
195 gchar *result = NULL;
196 gpgme_error_t err = 0;
197 static gboolean warned = FALSE;
199 if (GPOINTER_TO_INT(status) == -GPG_ERR_SYSTEM_ERROR) {
200 return g_strdup_printf(_("The signature can't be checked - %s"), privacy_get_error());
203 if (status == NULL) {
204 return g_strdup(_("The signature has not been checked."));
206 sig = status->signatures;
208 return g_strdup(_("The signature has not been checked."));
211 err = gpgme_get_key(ctx, sig->fpr, &key, 0);
212 if (gpg_err_code(err) == GPG_ERR_NO_AGENT) {
214 alertpanel_error(_("PGP Core: Can't get key - no gpg-agent running."));
216 g_warning("PGP Core: Can't get key - no gpg-agent running.");
218 } else if (gpg_err_code(err) != GPG_ERR_NO_ERROR && gpg_err_code(err) != GPG_ERR_EOF) {
219 return g_strdup_printf(_("The signature can't be checked - %s"),
220 gpgme_strerror(err));
221 } else if (gpg_err_code(err) != GPG_ERR_NO_ERROR && gpg_err_code(err) == GPG_ERR_EOF) {
223 * When gpg is upgraded to gpg-v21 then installer tries to migrate the old
224 * gpg keyrings found in ~/.gnupg to the new version. If the keyrings contain
225 * very old keys using ciphers no more supported in gpg-v21 this transition
226 * can fail and the left-over ~/.gnupg/pubring.gpg will cause claws to crash
227 * when the above condition is meet.
229 return g_strdup_printf(_("The signature can't be checked - %s."),
230 gpgme_strerror(err));
233 uname = extract_name(key->uids->uid);
235 uname = g_strdup("<?>");
237 switch (gpg_err_code(sig->status)) {
238 case GPG_ERR_NO_ERROR:
239 switch (key->uids?key->uids->validity:GPGME_VALIDITY_UNKNOWN) {
240 case GPGME_VALIDITY_ULTIMATE:
241 result = g_strdup_printf(_("Good signature from \"%s\" [ultimate]"), uname);
243 case GPGME_VALIDITY_FULL:
244 result = g_strdup_printf(_("Good signature from \"%s\" [full]"), uname);
246 case GPGME_VALIDITY_MARGINAL:
247 result = g_strdup_printf(_("Good signature from \"%s\" [marginal]"), uname);
249 case GPGME_VALIDITY_UNKNOWN:
250 case GPGME_VALIDITY_UNDEFINED:
251 case GPGME_VALIDITY_NEVER:
253 result = g_strdup_printf(_("Good signature from \"%s\""), uname);
257 case GPG_ERR_SIG_EXPIRED:
258 result = g_strdup_printf(_("Expired signature from \"%s\""), uname);
260 case GPG_ERR_KEY_EXPIRED:
261 result = g_strdup_printf(_("Good signature from \"%s\", but the key has expired"), uname);
263 case GPG_ERR_CERT_REVOKED:
264 result = g_strdup_printf(_("Good signature from \"%s\", but the key has been revoked"), uname);
266 case GPG_ERR_BAD_SIGNATURE:
267 result = g_strdup_printf(_("Bad signature from \"%s\""), uname);
269 case GPG_ERR_NO_PUBKEY: {
270 gchar *id = g_strdup(sig->fpr + strlen(sig->fpr)-8);
271 result = g_strdup_printf(_("Key 0x%s not available to verify this signature"), id);
276 result = g_strdup(_("The signature has not been checked"));
280 result = g_strdup(_("Error"));
285 gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
290 gpgme_signature_t sig = NULL;
292 siginfo = g_string_sized_new(64);
293 if (status == NULL) {
294 g_string_append_printf(siginfo,
295 _("Error checking signature: no status\n"));
299 sig = status->signatures;
306 const gchar *keytype, *keyid, *uid;
308 err = gpgme_get_key(ctx, sig->fpr, &key, 0);
310 if (err != GPG_ERR_NO_ERROR) {
312 g_string_append_printf(siginfo,
313 _("Error checking signature: %s\n"),
314 gpgme_strerror(err));
318 keytype = gpgme_pubkey_algo_name(
319 key->subkeys->pubkey_algo);
320 keyid = key->subkeys->keyid;
321 uid = key->uids->uid;
328 memset(buf, 0, sizeof(buf));
329 fast_strftime(buf, sizeof(buf)-1, prefs_common_get_prefs()->date_format, localtime_r(&sig->timestamp, <));
330 g_string_append_printf(siginfo,
331 _("Signature made on %s using %s key ID %s\n"),
332 buf, keytype, keyid);
334 switch (gpg_err_code(sig->status)) {
335 case GPG_ERR_NO_ERROR:
336 g_string_append_printf(siginfo,
337 _("Good signature from uid \"%s\" (Validity: %s)\n"),
338 uid, get_validity_str(key->uids?key->uids->validity:GPGME_VALIDITY_UNKNOWN));
340 case GPG_ERR_KEY_EXPIRED:
341 g_string_append_printf(siginfo,
342 _("Expired key uid \"%s\"\n"),
345 case GPG_ERR_SIG_EXPIRED:
346 g_string_append_printf(siginfo,
347 _("Expired signature from uid \"%s\" (Validity: %s)\n"),
348 uid, get_validity_str(key->uids?key->uids->validity:GPGME_VALIDITY_UNKNOWN));
350 case GPG_ERR_CERT_REVOKED:
351 g_string_append_printf(siginfo,
352 _("Revoked key uid \"%s\"\n"),
355 case GPG_ERR_BAD_SIGNATURE:
356 g_string_append_printf(siginfo,
357 _("BAD signature from \"%s\"\n"),
363 if (sig->status != GPG_ERR_BAD_SIGNATURE) {
365 key->uids = key->uids ? key->uids->next : NULL;
366 while (key->uids != NULL) {
367 g_string_append_printf(siginfo,
368 _(" uid \"%s\" (Validity: %s)\n"),
370 key->uids->revoked==TRUE?_("Revoked"):get_validity_str(key->uids->validity));
372 key->uids = key->uids->next;
374 g_string_append_printf(siginfo,_("Owner Trust: %s\n"),
375 get_owner_trust_str(key->owner_trust));
376 g_string_append(siginfo,
377 _("Primary key fingerprint:"));
378 const char* primary_fpr = NULL;
379 if (key && key->subkeys && key->subkeys->fpr)
380 primary_fpr = key->subkeys->fpr;
382 g_string_append(siginfo, " ?");
383 int idx; /* now pretty-print the fingerprint */
384 for (idx=0; primary_fpr && *primary_fpr!='\0'; idx++, primary_fpr++) {
386 g_string_append_c(siginfo, ' ');
388 g_string_append_c(siginfo, ' ');
389 g_string_append_c(siginfo, (gchar)*primary_fpr);
391 g_string_append_c(siginfo, '\n');
392 #ifdef HAVE_GPGME_PKA_TRUST
393 if (sig->pka_trust == 1 && sig->pka_address) {
394 g_string_append_printf(siginfo,
395 _("WARNING: Signer's address \"%s\" "
396 "does not match DNS entry\n"),
399 else if (sig->pka_trust == 2 && sig->pka_address) {
400 g_string_append_printf(siginfo,
401 _("Verified signer's address is \"%s\"\n"),
403 /* FIXME: Compare the address to the
406 #endif /*HAVE_GPGME_PKA_TRUST*/
409 g_string_append(siginfo, "\n");
415 g_string_free(siginfo, FALSE);
419 gpgme_data_t sgpgme_data_from_mimeinfo(MimeInfo *mimeinfo)
421 gpgme_data_t data = NULL;
423 FILE *fp = g_fopen(mimeinfo->data.filename, "rb");
428 err = gpgme_data_new_from_filepart(&data, NULL, fp, mimeinfo->offset, mimeinfo->length);
431 debug_print("data %p (%d %d)\n", (void *)&data, mimeinfo->offset, mimeinfo->length);
433 debug_print ("gpgme_data_new_from_file failed: %s\n",
434 gpgme_strerror (err));
435 privacy_set_error(_("Couldn't get data from message, %s"), gpgme_strerror(err));
441 gpgme_data_t sgpgme_decrypt_verify(gpgme_data_t cipher, gpgme_verify_result_t *status, gpgme_ctx_t ctx)
443 struct passphrase_cb_info_s info;
447 memset (&info, 0, sizeof info);
449 if ((err = gpgme_data_new(&plain)) != GPG_ERR_NO_ERROR) {
451 privacy_set_error(_("Couldn't initialize data, %s"), gpgme_strerror(err));
455 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
456 prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
457 if (!getenv("GPG_AGENT_INFO") || !prefs_gpg_get_config()->use_gpg_agent) {
459 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
462 prefs_gpg_enable_agent(TRUE);
464 gpgme_set_passphrase_cb (ctx, NULL, &info);
468 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
469 err = gpgme_op_decrypt_verify(ctx, cipher, plain);
470 if (err != GPG_ERR_NO_ERROR) {
471 debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
472 privacy_set_error("%s", gpgme_strerror(err));
473 gpgmegtk_free_passphrase();
474 gpgme_data_release(plain);
478 err = cm_gpgme_data_rewind(plain);
480 debug_print("can't seek (%d %d %s)\n", err, errno, g_strerror(errno));
483 debug_print("decrypted.\n");
484 *status = gpgme_op_verify_result (ctx);
486 err = gpgme_op_decrypt(ctx, cipher, plain);
487 if (err != GPG_ERR_NO_ERROR) {
488 debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
489 privacy_set_error("%s", gpgme_strerror(err));
490 gpgmegtk_free_passphrase();
491 gpgme_data_release(plain);
495 err = cm_gpgme_data_rewind(plain);
497 debug_print("can't seek (%d %d %s)\n", err, errno, g_strerror(errno));
500 debug_print("decrypted.\n");
501 *status = gpgme_op_verify_result (ctx);
506 gchar *sgpgme_get_encrypt_data(GSList *recp_names, gpgme_protocol_t proto)
508 SelectionResult result = KEY_SELECTION_CANCEL;
509 gpgme_key_t *keys = gpgmegtk_recipient_selection(recp_names, &result,
515 if (result == KEY_SELECTION_DONT)
516 return g_strdup("_DONT_ENCRYPT_");
521 gpgme_subkey_t skey = keys[i]->subkeys;
522 gchar *fpr = skey->fpr;
524 debug_print("adding %s\n", fpr);
525 tmp = g_strconcat(ret?ret:"", fpr, " ", NULL);
533 gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account,
534 const gchar *from_addr)
536 GPGAccountConfig *config;
537 const gchar *signer_addr = account->address;
539 gpgme_signers_clear(ctx);
542 signer_addr = from_addr;
543 config = prefs_gpg_account_get_config(account);
545 switch(config->sign_key) {
546 case SIGN_KEY_DEFAULT:
547 debug_print("using default gnupg key\n");
549 case SIGN_KEY_BY_FROM:
550 debug_print("using key for %s\n", signer_addr);
552 case SIGN_KEY_CUSTOM:
553 debug_print("using key for %s\n", config->sign_key_id);
557 if (config->sign_key != SIGN_KEY_DEFAULT) {
559 gpgme_key_t key, found_key;
562 if (config->sign_key == SIGN_KEY_BY_FROM)
564 else if (config->sign_key == SIGN_KEY_CUSTOM)
565 keyid = config->sign_key_id;
570 /* Look for any key, not just private ones, or GPGMe doesn't
571 * correctly set the revoked flag. */
572 err = gpgme_op_keylist_start(ctx, keyid, 0);
573 while ((err = gpgme_op_keylist_next(ctx, &key)) == 0) {
580 if (key->protocol != gpgme_get_protocol(ctx)) {
581 debug_print("skipping a key (wrong protocol %d)\n", key->protocol);
582 gpgme_key_release(key);
587 debug_print("skipping a key, expired");
588 gpgme_key_release(key);
592 debug_print("skipping a key, revoked");
593 gpgme_key_release(key);
597 debug_print("skipping a key, disabled");
598 gpgme_key_release(key);
602 if (found_key != NULL) {
603 gpgme_key_release(key);
604 gpgme_op_keylist_end(ctx);
605 g_warning("ambiguous specification of secret key '%s'\n", keyid);
606 privacy_set_error(_("Secret key specification is ambiguous"));
612 gpgme_op_keylist_end(ctx);
614 if (found_key == NULL) {
615 g_warning("setup_signers start: %s", gpgme_strerror(err));
616 privacy_set_error(_("Secret key not found (%s)"), gpgme_strerror(err));
620 err = gpgme_signers_add(ctx, found_key);
621 debug_print("got key (proto %d (pgp %d, smime %d).\n",
622 found_key->protocol, GPGME_PROTOCOL_OpenPGP,
624 gpgme_key_release(found_key);
627 g_warning("error adding secret key: %s\n",
628 gpgme_strerror(err));
629 privacy_set_error(_("Error setting secret key: %s"),
630 gpgme_strerror(err));
635 prefs_gpg_account_free_config(config);
639 prefs_gpg_account_free_config(config);
645 gchar *ctype_locale = NULL, *messages_locale = NULL;
646 gchar *ctype_utf8_locale = NULL, *messages_utf8_locale = NULL;
648 gpgme_engine_info_t engineInfo;
650 if (strcmp(prefs_gpg_get_config()->gpg_path, "") != 0 &&
651 access(prefs_gpg_get_config()->gpg_path, X_OK) != -1)
652 gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP,prefs_gpg_get_config()->gpg_path, NULL);
654 if (gpgme_check_version("1.0.0")) {
656 debug_print("setting gpgme CTYPE locale\n");
658 ctype_locale = g_win32_getlocale();
660 ctype_locale = g_strdup(setlocale(LC_CTYPE, NULL));
663 debug_print("setting gpgme CTYPE locale to: %s\n", ctype_locale);
664 if (strchr(ctype_locale, '.'))
665 *(strchr(ctype_locale, '.')) = '\0';
666 else if (strchr(ctype_locale, '@'))
667 *(strchr(ctype_locale, '@')) = '\0';
668 ctype_utf8_locale = g_strconcat(ctype_locale, ".UTF-8", NULL);
670 debug_print("setting gpgme locale to UTF8: %s\n", ctype_utf8_locale ? ctype_utf8_locale : "NULL");
671 gpgme_set_locale(NULL, LC_CTYPE, ctype_utf8_locale);
673 debug_print("done\n");
674 g_free(ctype_utf8_locale);
675 g_free(ctype_locale);
677 debug_print("couldn't set gpgme CTYPE locale\n");
681 debug_print("setting gpgme MESSAGES locale\n");
683 messages_locale = g_win32_getlocale();
685 messages_locale = g_strdup(setlocale(LC_MESSAGES, NULL));
687 if (messages_locale) {
688 debug_print("setting gpgme MESSAGES locale to: %s\n", messages_locale);
689 if (strchr(messages_locale, '.'))
690 *(strchr(messages_locale, '.')) = '\0';
691 else if (strchr(messages_locale, '@'))
692 *(strchr(messages_locale, '@')) = '\0';
693 messages_utf8_locale = g_strconcat(messages_locale, ".UTF-8", NULL);
694 debug_print("setting gpgme locale to UTF8: %s\n", messages_utf8_locale ? messages_utf8_locale : "NULL");
696 gpgme_set_locale(NULL, LC_MESSAGES, messages_utf8_locale);
698 debug_print("done\n");
699 g_free(messages_utf8_locale);
700 g_free(messages_locale);
702 debug_print("couldn't set gpgme MESSAGES locale\n");
705 if (!gpgme_get_engine_info(&engineInfo)) {
707 debug_print("GpgME Protocol: %s\n"
708 "Version: %s (req %s)\n"
710 gpgme_get_protocol_name(engineInfo->protocol) ? gpgme_get_protocol_name(engineInfo->protocol):"???",
711 engineInfo->version ? engineInfo->version:"???",
712 engineInfo->req_version ? engineInfo->req_version:"???",
713 engineInfo->file_name ? engineInfo->file_name:"???");
714 if (engineInfo->protocol == GPGME_PROTOCOL_OpenPGP
715 && gpgme_engine_check_version(engineInfo->protocol) !=
717 if (engineInfo->file_name && !engineInfo->version) {
718 alertpanel_error(_("Gpgme protocol '%s' is unusable: "
719 "Engine '%s' isn't installed properly."),
720 gpgme_get_protocol_name(engineInfo->protocol),
721 engineInfo->file_name);
722 } else if (engineInfo->file_name && engineInfo->version
723 && engineInfo->req_version) {
724 alertpanel_error(_("Gpgme protocol '%s' is unusable: "
725 "Engine '%s' version %s is installed, "
726 "but version %s is required.\n"),
727 gpgme_get_protocol_name(engineInfo->protocol),
728 engineInfo->file_name,
730 engineInfo->req_version);
732 alertpanel_error(_("Gpgme protocol '%s' is unusable "
733 "(unknown problem)"),
734 gpgme_get_protocol_name(engineInfo->protocol));
737 engineInfo = engineInfo->next;
741 sgpgme_disable_all();
743 if (prefs_gpg_get_config()->gpg_warning) {
746 val = alertpanel_full
748 _("GnuPG is not installed properly, or needs "
750 "OpenPGP support disabled."),
751 GTK_STOCK_CLOSE, NULL, NULL, TRUE, NULL,
752 ALERT_WARNING, G_ALERTDEFAULT);
753 if (val & G_ALERTDISABLE)
754 prefs_gpg_get_config()->gpg_warning = FALSE;
761 gpgmegtk_free_passphrase();
764 void sgpgme_create_secret_key(PrefsAccount *account, gboolean ask_create)
766 AlertValue val = G_ALERTDEFAULT;
767 gchar *key_parms = NULL;
770 gchar *passphrase = NULL, *passphrase_second = NULL;
773 gpgme_error_t err = 0;
775 GtkWidget *window = NULL;
776 gpgme_genkey_result_t key;
779 account = account_get_default();
781 if (account->address == NULL) {
782 alertpanel_error(_("You have to save the account's information with \"OK\" "
783 "before being able to generate a key pair.\n"));
787 val = alertpanel(_("No PGP key found"),
788 _("Claws Mail did not find a secret PGP key, "
789 "which means that you won't be able to sign "
790 "emails or receive encrypted emails.\n"
791 "Do you want to create a new key pair now?"),
792 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
793 if (val == G_ALERTDEFAULT) {
794 prefs_gpg_get_config()->gpg_ask_create_key = FALSE;
795 prefs_gpg_save_config();
801 name = g_strdup(account->name);
803 name = g_strdup(account->address);
805 email = g_strdup(account->address);
806 tmp = g_strdup_printf("%s <%s>", account->name?account->name:account->address, account->address);
808 passphrase = passphrase_mbox(tmp, NULL, prev_bad, 1);
809 if (passphrase == NULL) {
815 passphrase_second = passphrase_mbox(tmp, NULL, 0, 2);
816 if (passphrase_second == NULL) {
823 if (strcmp(passphrase, passphrase_second)) {
825 g_free(passphrase_second);
830 key_parms = g_strdup_printf("<GnupgKeyParms format=\"internal\">\n"
834 "Subkey-Length: 2048\n"
839 "</GnupgKeyParms>\n",
841 strlen(passphrase)?"Passphrase: ":"",
843 strlen(passphrase)?"\n":"");
844 #ifndef G_PLATFORM_WIN32
845 if (mlock(passphrase, strlen(passphrase)) == -1)
846 debug_print("couldn't lock passphrase\n");
847 if (mlock(passphrase_second, strlen(passphrase_second)) == -1)
848 debug_print("couldn't lock passphrase2\n");
853 g_free(passphrase_second);
856 err = gpgme_new (&ctx);
858 alertpanel_error(_("Couldn't generate a new key pair: %s"),
859 gpgme_strerror(err));
865 window = label_window_create(_("Generating your new key pair... Please move the mouse "
866 "around to help generate entropy..."));
868 err = gpgme_op_genkey(ctx, key_parms, NULL, NULL);
871 label_window_destroy(window);
874 alertpanel_error(_("Couldn't generate a new key pair: %s"), gpgme_strerror(err));
878 key = gpgme_op_genkey_result(ctx);
880 alertpanel_error(_("Couldn't generate a new key pair: unknown error"));
884 gchar *buf = g_strdup_printf(_("Your new key pair has been generated. "
885 "Its fingerprint is:\n%s\n\nDo you want to export it "
887 key->fpr ? key->fpr:"null");
888 AlertValue val = alertpanel(_("Key generated"), buf,
889 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
891 if (val == G_ALERTALTERNATE) {
893 gchar *cmd = g_strdup_printf("gpg --no-tty --send-keys %s", key->fpr);
899 } else if (pid == 0) {
902 res = WEXITSTATUS(res);
906 time_t start_wait = time(NULL);
909 if (waitpid(pid, &status, WNOHANG) == 0 || !WIFEXITED(status)) {
912 res = WEXITSTATUS(status);
915 if (time(NULL) - start_wait > 5) {
916 debug_print("SIGTERM'ing gpg\n");
919 if (time(NULL) - start_wait > 6) {
920 debug_print("SIGKILL'ing gpg\n");
927 alertpanel_notice(_("Key exported."));
929 alertpanel_error(_("Couldn't export key."));
933 alertpanel_error(_("Key export isn't implemented in Windows."));
937 prefs_gpg_get_config()->gpg_ask_create_key = FALSE;
938 prefs_gpg_save_config();
942 gboolean sgpgme_has_secret_key(void)
944 gpgme_error_t err = 0;
948 err = gpgme_new (&ctx);
950 debug_print("err : %s\n", gpgme_strerror(err));
954 err = gpgme_op_keylist_start(ctx, NULL, TRUE);
956 err = gpgme_op_keylist_next(ctx, &key);
957 gpgme_op_keylist_end(ctx);
958 if (gpg_err_code(err) == GPG_ERR_EOF) {
959 if (gpgme_get_protocol(ctx) != GPGME_PROTOCOL_CMS) {
960 gpgme_set_protocol(ctx, GPGME_PROTOCOL_CMS);
971 void sgpgme_check_create_key(void)
973 if (prefs_gpg_get_config()->gpg_ask_create_key &&
974 !sgpgme_has_secret_key()) {
975 sgpgme_create_secret_key(NULL, TRUE);
977 prefs_gpg_get_config()->gpg_ask_create_key = FALSE;
978 prefs_gpg_save_config();
982 void *sgpgme_data_release_and_get_mem(gpgme_data_t data, size_t *len)
994 /* I know it's deprecated, but we don't compile with _LARGEFILE */
995 cm_gpgme_data_rewind(data);
996 while ((r = gpgme_data_read(data, buf, BUFSIZ)) > 0) {
997 void *rresult = realloc(result, r + w);
998 if (rresult == NULL) {
999 g_warning("can't allocate memory\n");
1005 memcpy(result+w, buf, r);
1011 gpgme_data_release(data);
1020 gpgme_error_t cm_gpgme_data_rewind(gpgme_data_t dh)
1022 #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
1023 if (gpgme_data_seek(dh, (off_t)0, SEEK_SET) == -1)
1024 return gpg_error_from_errno(errno);
1028 return gpgme_data_rewind(dh);
1032 #endif /* USE_GPGME */