2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 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 (gpg_err_code(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 gchar *extract_name(const char *uid)
160 if (!strncmp(uid, "CN=", 3)) {
161 gchar *result = g_strdup(uid+3);
162 if (strstr(result, ","))
163 *(strstr(result, ",")) = '\0';
165 } else if (strstr(uid, ",CN=")) {
166 gchar *result = g_strdup(strstr(uid, ",CN=")+4);
167 if (strstr(result, ","))
168 *(strstr(result, ",")) = '\0';
171 return g_strdup(uid);
174 gchar *sgpgme_sigstat_info_short(gpgme_ctx_t ctx, gpgme_verify_result_t status)
176 gpgme_signature_t sig = NULL;
179 gchar *result = NULL;
180 gpgme_error_t err = 0;
181 static gboolean warned = FALSE;
183 if (GPOINTER_TO_INT(status) == -GPG_ERR_SYSTEM_ERROR) {
184 return g_strdup_printf(_("The signature can't be checked - %s"), privacy_get_error());
187 if (status == NULL) {
188 return g_strdup(_("The signature has not been checked."));
190 sig = status->signatures;
192 return g_strdup(_("The signature has not been checked."));
195 err = gpgme_get_key(ctx, sig->fpr, &key, 0);
196 if (gpg_err_code(err) == GPG_ERR_NO_AGENT) {
198 alertpanel_error(_("PGP Core: Can't get key - no gpg-agent running."));
200 g_warning(_("PGP Core: Can't get key - no gpg-agent running."));
202 } else if (gpg_err_code(err) != GPG_ERR_NO_ERROR && gpg_err_code(err) != GPG_ERR_EOF) {
203 return g_strdup_printf(_("The signature can't be checked - %s"),
204 gpgme_strerror(err));
207 uname = extract_name(key->uids->uid);
209 uname = g_strdup("<?>");
210 switch (gpg_err_code(sig->status)) {
211 case GPG_ERR_NO_ERROR:
212 switch (gpg_err_code(sig->validity)) {
213 case GPGME_VALIDITY_FULL:
214 case GPGME_VALIDITY_ULTIMATE:
215 result = g_strdup_printf(_("Good signature from %s."), uname);
217 case GPGME_VALIDITY_MARGINAL:
218 case GPGME_VALIDITY_UNKNOWN:
219 case GPGME_VALIDITY_UNDEFINED:
220 case GPGME_VALIDITY_NEVER:
222 result = g_strdup_printf(_("Good signature (untrusted) from %s."), uname);
226 case GPG_ERR_SIG_EXPIRED:
227 result = g_strdup_printf(_("Expired signature from %s."), uname);
229 case GPG_ERR_KEY_EXPIRED:
230 result = g_strdup_printf(_("Expired key from %s."), uname);
232 case GPG_ERR_BAD_SIGNATURE:
233 result = g_strdup_printf(_("Bad signature from %s."), uname);
235 case GPG_ERR_NO_PUBKEY: {
236 gchar *id = g_strdup(sig->fpr + strlen(sig->fpr)-8);
237 result = g_strdup_printf(_("Key 0x%s not available to verify this signature."), id);
242 result = g_strdup(_("The signature has not been checked."));
246 result = g_strdup(_("Error"));
251 gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
256 gpgme_signature_t sig = NULL;
258 siginfo = g_string_sized_new(64);
259 if (status == NULL) {
260 g_string_append_printf(siginfo,
261 _("Error checking signature: no status\n"));
265 sig = status->signatures;
268 gpgme_user_id_t user = NULL;
271 const gchar *keytype, *keyid, *uid;
273 err = gpgme_get_key(ctx, sig->fpr, &key, 0);
275 if (err != GPG_ERR_NO_ERROR) {
277 g_string_append_printf(siginfo,
278 _("Error checking signature: %s\n"),
279 gpgme_strerror(err));
284 keytype = gpgme_pubkey_algo_name(
285 key->subkeys->pubkey_algo);
286 keyid = key->subkeys->keyid;
293 g_string_append_printf(siginfo,
294 _("Signature made using %s key ID %s\n"),
297 switch (gpg_err_code(sig->status)) {
298 case GPG_ERR_NO_ERROR:
299 case GPG_ERR_KEY_EXPIRED:
300 g_string_append_printf(siginfo,
301 _("Good signature from uid \"%s\" (Validity: %s)\n"),
302 uid, get_validity_str(user?user->validity:GPGME_VALIDITY_UNKNOWN));
304 case GPG_ERR_SIG_EXPIRED:
305 g_string_append_printf(siginfo,
306 _("Expired signature from uid \"%s\" (Validity: %s)\n"),
307 uid, get_validity_str(user?user->validity:GPGME_VALIDITY_UNKNOWN));
309 case GPG_ERR_BAD_SIGNATURE:
310 g_string_append_printf(siginfo,
311 _("BAD signature from \"%s\"\n"),
317 if (sig->status != GPG_ERR_BAD_SIGNATURE) {
319 user = user ? user->next : NULL;
320 while (user != NULL) {
321 g_string_append_printf(siginfo,
322 _(" uid \"%s\" (Validity: %s)\n"),
324 get_validity_str(user->validity));
328 g_string_append(siginfo,
329 _("Primary key fingerprint:"));
330 const char* primary_fpr = NULL;
331 if (key && key->subkeys && key->subkeys->fpr)
332 primary_fpr = key->subkeys->fpr;
334 g_string_append(siginfo, " ?");
335 int idx; /* now pretty-print the fingerprint */
336 for (idx=0; primary_fpr && *primary_fpr!='\0'; idx++, primary_fpr++) {
338 g_string_append_c(siginfo, ' ');
340 g_string_append_c(siginfo, ' ');
341 g_string_append_c(siginfo, (gchar)*primary_fpr);
343 g_string_append_c(siginfo, '\n');
344 #ifdef HAVE_GPGME_PKA_TRUST
345 if (sig->pka_trust == 1 && sig->pka_address) {
346 g_string_append_printf(siginfo,
347 _("WARNING: Signer's address \"%s\" "
348 "does not match DNS entry\n"),
351 else if (sig->pka_trust == 2 && sig->pka_address) {
352 g_string_append_printf(siginfo,
353 _("Verified signer's address is \"%s\"\n"),
355 /* FIXME: Compare the address to the
358 #endif /*HAVE_GPGME_PKA_TRUST*/
361 g_string_append(siginfo, "\n");
367 g_string_free(siginfo, FALSE);
371 gpgme_data_t sgpgme_data_from_mimeinfo(MimeInfo *mimeinfo)
373 gpgme_data_t data = NULL;
375 FILE *fp = g_fopen(mimeinfo->data.filename, "rb");
376 gchar *tmp_file = NULL;
381 tmp_file = get_tmp_file();
382 copy_file_part(fp, mimeinfo->offset, mimeinfo->length, tmp_file);
385 debug_print("tmp file %s\n", tmp_file);
387 err = gpgme_data_new_from_file(&data, tmp_file, 1);
388 claws_unlink(tmp_file);
391 debug_print("data %p (%d %d)\n", (void *)&data, mimeinfo->offset, mimeinfo->length);
393 debug_print ("gpgme_data_new_from_file failed: %s\n",
394 gpgme_strerror (err));
395 privacy_set_error(_("Couldn't get data from message, %s"), gpgme_strerror(err));
401 gpgme_data_t sgpgme_decrypt_verify(gpgme_data_t cipher, gpgme_verify_result_t *status, gpgme_ctx_t ctx)
403 struct passphrase_cb_info_s info;
407 memset (&info, 0, sizeof info);
409 if ((err = gpgme_data_new(&plain)) != GPG_ERR_NO_ERROR) {
411 privacy_set_error(_("Couldn't initialize data, %s"), gpgme_strerror(err));
415 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
416 prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
417 if (!getenv("GPG_AGENT_INFO") || !prefs_gpg_get_config()->use_gpg_agent) {
419 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
422 prefs_gpg_enable_agent(TRUE);
424 gpgme_set_passphrase_cb (ctx, NULL, &info);
428 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
429 err = gpgme_op_decrypt_verify(ctx, cipher, plain);
430 if (err != GPG_ERR_NO_ERROR) {
431 debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
432 privacy_set_error("%s", gpgme_strerror(err));
433 gpgmegtk_free_passphrase();
434 gpgme_data_release(plain);
438 err = cm_gpgme_data_rewind(plain);
440 debug_print("can't seek (%d %d %s)\n", err, errno, strerror(errno));
443 debug_print("decrypted.\n");
444 *status = gpgme_op_verify_result (ctx);
446 err = gpgme_op_decrypt(ctx, cipher, plain);
447 if (err != GPG_ERR_NO_ERROR) {
448 debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
449 privacy_set_error("%s", gpgme_strerror(err));
450 gpgmegtk_free_passphrase();
451 gpgme_data_release(plain);
455 err = cm_gpgme_data_rewind(plain);
457 debug_print("can't seek (%d %d %s)\n", err, errno, strerror(errno));
460 debug_print("decrypted.\n");
461 *status = gpgme_op_verify_result (ctx);
466 gchar *sgpgme_get_encrypt_data(GSList *recp_names, gpgme_protocol_t proto)
468 SelectionResult result = KEY_SELECTION_CANCEL;
469 gpgme_key_t *keys = gpgmegtk_recipient_selection(recp_names, &result,
475 if (result == KEY_SELECTION_DONT)
476 return g_strdup("_DONT_ENCRYPT_");
481 gpgme_subkey_t skey = keys[i]->subkeys;
482 gchar *fpr = skey->fpr;
484 debug_print("adding %s\n", fpr);
485 tmp = g_strconcat(ret?ret:"", fpr, " ", NULL);
493 gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account,
494 const gchar *from_addr)
496 GPGAccountConfig *config;
497 const gchar *signer_addr = account->address;
499 gpgme_signers_clear(ctx);
502 signer_addr = from_addr;
503 config = prefs_gpg_account_get_config(account);
505 switch(config->sign_key) {
506 case SIGN_KEY_DEFAULT:
507 debug_print("using default gnupg key\n");
509 case SIGN_KEY_BY_FROM:
510 debug_print("using key for %s\n", signer_addr);
512 case SIGN_KEY_CUSTOM:
513 debug_print("using key for %s\n", config->sign_key_id);
517 if (config->sign_key != SIGN_KEY_DEFAULT) {
519 gpgme_key_t key, key2;
522 if (config->sign_key == SIGN_KEY_BY_FROM)
524 else if (config->sign_key == SIGN_KEY_CUSTOM)
525 keyid = config->sign_key_id;
529 err = gpgme_op_keylist_start(ctx, keyid, 1);
532 err = gpgme_op_keylist_next(ctx, &key);
533 if (!err && key && key->protocol == gpgme_get_protocol(ctx) &&
534 !key->expired && !key->revoked && !key->disabled)
536 if (!err && key && key->protocol != gpgme_get_protocol(ctx)) {
537 debug_print("skipping a key (wrong protocol %d)\n", key->protocol);
538 gpgme_key_release(key);
540 if (!err && key && (key->expired || key->revoked || key->disabled)) {
542 debug_print("skipping a key");
544 debug_print(" expired");
546 debug_print(" revoked");
548 debug_print(" disabled");
550 gpgme_key_release(key);
555 g_warning("setup_signers start: %s", gpgme_strerror(err));
556 privacy_set_error(_("Secret key not found (%s)"), gpgme_strerror(err));
561 err = gpgme_op_keylist_next(ctx, &key2);
562 if (!err && key2 && key2->protocol == gpgme_get_protocol(ctx) &&
563 !key2->expired && !key2->revoked && !key2->disabled)
565 if (!err && key2 && key2->protocol != gpgme_get_protocol(ctx)) {
566 debug_print("skipping a key (wrong protocol %d)\n", key2->protocol);
567 gpgme_key_release(key2);
569 if (!err && key2 && (key2->expired || key2->revoked || key2->disabled)) {
570 debug_print("skipping a key");
572 debug_print(" expired");
574 debug_print(" revoked");
576 debug_print(" disabled");
578 gpgme_key_release(key2);
582 gpgme_key_release(key2);
583 g_warning("ambiguous specification of secret key '%s'\n",
585 privacy_set_error(_("Secret key specification is ambiguous"));
589 gpgme_op_keylist_end(ctx);
590 err = gpgme_signers_add(ctx, key);
591 debug_print("got key (proto %d (pgp %d, smime %d).\n", key->protocol,
592 GPGME_PROTOCOL_OpenPGP, GPGME_PROTOCOL_CMS);
593 gpgme_key_release(key);
596 g_warning("error adding secret key: %s\n", gpgme_strerror(err));
597 privacy_set_error(_("Error setting secret key: %s"), gpgme_strerror(err));
602 prefs_gpg_account_free_config(config);
606 prefs_gpg_account_free_config(config);
612 gchar *ctype_locale = NULL, *messages_locale = NULL;
613 gchar *ctype_utf8_locale = NULL, *messages_utf8_locale = NULL;
615 gpgme_engine_info_t engineInfo;
616 if (gpgme_check_version("1.0.0")) {
618 ctype_locale = g_strdup(setlocale(LC_CTYPE, NULL));
619 if (strchr(ctype_locale, '.'))
620 *(strchr(ctype_locale, '.')) = '\0';
621 else if (strchr(ctype_locale, '@'))
622 *(strchr(ctype_locale, '@')) = '\0';
623 ctype_utf8_locale = g_strconcat(ctype_locale, ".UTF-8", NULL);
625 gpgme_set_locale(NULL, LC_CTYPE, ctype_utf8_locale);
627 g_free(ctype_utf8_locale);
628 g_free(ctype_locale);
631 messages_locale = g_strdup(setlocale(LC_MESSAGES, NULL));
632 if (strchr(messages_locale, '.'))
633 *(strchr(messages_locale, '.')) = '\0';
634 else if (strchr(messages_locale, '@'))
635 *(strchr(messages_locale, '@')) = '\0';
636 messages_utf8_locale = g_strconcat(messages_locale, ".UTF-8", NULL);
638 gpgme_set_locale(NULL, LC_MESSAGES, messages_utf8_locale);
640 g_free(messages_utf8_locale);
641 g_free(messages_locale);
643 if (!gpgme_get_engine_info(&engineInfo)) {
646 debug_print("GpgME Protocol: %s\n"
647 "Version: %s (req %s)\n"
649 gpgme_get_protocol_name(engineInfo->protocol) ? gpgme_get_protocol_name(engineInfo->protocol):"???",
650 engineInfo->version ? engineInfo->version:"???",
651 engineInfo->req_version ? engineInfo->req_version:"???",
652 engineInfo->file_name ? engineInfo->file_name:"???");
654 if (engineInfo->protocol == GPGME_PROTOCOL_OpenPGP
655 && gpgme_engine_check_version(engineInfo->protocol) !=
657 if (engineInfo->file_name && !engineInfo->version) {
658 alertpanel_error(_("Gpgme protocol '%s' is unusable: "
659 "Engine '%s' isn't installed properly."),
660 gpgme_get_protocol_name(engineInfo->protocol),
661 engineInfo->file_name);
662 } else if (engineInfo->file_name && engineInfo->version
663 && engineInfo->req_version) {
664 alertpanel_error(_("Gpgme protocol '%s' is unusable: "
665 "Engine '%s' version %s is installed, "
666 "but version %s is required.\n"),
667 gpgme_get_protocol_name(engineInfo->protocol),
668 engineInfo->file_name,
670 engineInfo->req_version);
672 alertpanel_error(_("Gpgme protocol '%s' is unusable "
673 "(unknown problem)"),
674 gpgme_get_protocol_name(engineInfo->protocol));
677 engineInfo = engineInfo->next;
681 sgpgme_disable_all();
683 if (prefs_gpg_get_config()->gpg_warning) {
686 val = alertpanel_full
688 _("GnuPG is not installed properly, or needs "
690 "OpenPGP support disabled."),
691 GTK_STOCK_CLOSE, NULL, NULL, TRUE, NULL,
692 ALERT_WARNING, G_ALERTDEFAULT);
693 if (val & G_ALERTDISABLE)
694 prefs_gpg_get_config()->gpg_warning = FALSE;
701 gpgmegtk_free_passphrase();
704 void sgpgme_create_secret_key(PrefsAccount *account, gboolean ask_create)
706 AlertValue val = G_ALERTDEFAULT;
707 gchar *key_parms = NULL;
710 gchar *passphrase = NULL, *passphrase_second = NULL;
713 gpgme_error_t err = 0;
715 GtkWidget *window = NULL;
716 gpgme_genkey_result_t key;
719 account = account_get_default();
721 if (account->address == NULL) {
722 alertpanel_error(_("You have to save the account's information with \"OK\" "
723 "before being able to generate a key pair.\n"));
727 val = alertpanel(_("No PGP key found"),
728 _("Claws Mail did not find a secret PGP key, "
729 "which means that you won't be able to sign "
730 "emails or receive encrypted emails.\n"
731 "Do you want to create a new key pair now?"),
732 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
733 if (val == G_ALERTDEFAULT) {
734 prefs_gpg_get_config()->gpg_ask_create_key = FALSE;
735 prefs_gpg_save_config();
741 name = g_strdup(account->name);
743 name = g_strdup(account->address);
745 email = g_strdup(account->address);
746 tmp = g_strdup_printf("%s <%s>", account->name?account->name:account->address, account->address);
748 passphrase = passphrase_mbox(tmp, NULL, prev_bad, 1);
749 if (passphrase == NULL) {
755 passphrase_second = passphrase_mbox(tmp, NULL, 0, 2);
756 if (passphrase_second == NULL) {
763 if (strcmp(passphrase, passphrase_second)) {
765 g_free(passphrase_second);
770 key_parms = g_strdup_printf("<GnupgKeyParms format=\"internal\">\n"
773 "Subkey-Type: ELG-E\n"
774 "Subkey-Length: 2048\n"
779 "</GnupgKeyParms>\n",
781 strlen(passphrase)?"Passphrase: ":"",
783 strlen(passphrase)?"\n":"");
784 #ifndef G_PLATFORM_WIN32
785 if (mlock(passphrase, strlen(passphrase)) == -1)
786 debug_print("couldn't lock passphrase\n");
787 if (mlock(passphrase_second, strlen(passphrase_second)) == -1)
788 debug_print("couldn't lock passphrase2\n");
793 g_free(passphrase_second);
796 err = gpgme_new (&ctx);
798 alertpanel_error(_("Couldn't generate a new key pair: %s"),
799 gpgme_strerror(err));
805 window = label_window_create(_("Generating your new key pair... Please move the mouse "
806 "around to help generate entropy..."));
808 err = gpgme_op_genkey(ctx, key_parms, NULL, NULL);
811 label_window_destroy(window);
814 alertpanel_error(_("Couldn't generate a new key pair: %s"), gpgme_strerror(err));
818 key = gpgme_op_genkey_result(ctx);
820 alertpanel_error(_("Couldn't generate a new key pair: unknown error"));
824 gchar *buf = g_strdup_printf(_("Your new key pair has been generated. "
825 "Its fingerprint is:\n%s\n\nDo you want to export it "
827 key->fpr ? key->fpr:"null");
828 AlertValue val = alertpanel(_("Key generated"), buf,
829 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
831 if (val == G_ALERTALTERNATE) {
833 gchar *cmd = g_strdup_printf("gpg --no-tty --send-keys %s", key->fpr);
839 } else if (pid == 0) {
842 res = WEXITSTATUS(res);
846 time_t start_wait = time(NULL);
849 if (waitpid(pid, &status, WNOHANG) == 0 || !WIFEXITED(status)) {
852 res = WEXITSTATUS(status);
855 if (time(NULL) - start_wait > 5) {
856 debug_print("SIGTERM'ing gpg\n");
859 if (time(NULL) - start_wait > 6) {
860 debug_print("SIGKILL'ing gpg\n");
867 alertpanel_notice(_("Key exported."));
869 alertpanel_error(_("Couldn't export key."));
873 alertpanel_error(_("Key export isn't implemented in Windows."));
877 prefs_gpg_get_config()->gpg_ask_create_key = FALSE;
878 prefs_gpg_save_config();
882 gboolean sgpgme_has_secret_key(void)
884 gpgme_error_t err = 0;
888 err = gpgme_new (&ctx);
890 debug_print("err : %s\n", gpgme_strerror(err));
894 err = gpgme_op_keylist_start(ctx, NULL, TRUE);
896 err = gpgme_op_keylist_next(ctx, &key);
897 gpgme_op_keylist_end(ctx);
898 if (gpg_err_code(err) == GPG_ERR_EOF) {
899 if (gpgme_get_protocol(ctx) != GPGME_PROTOCOL_CMS) {
900 gpgme_set_protocol(ctx, GPGME_PROTOCOL_CMS);
911 void sgpgme_check_create_key(void)
913 if (prefs_gpg_get_config()->gpg_ask_create_key &&
914 !sgpgme_has_secret_key()) {
915 sgpgme_create_secret_key(NULL, TRUE);
917 prefs_gpg_get_config()->gpg_ask_create_key = FALSE;
918 prefs_gpg_save_config();
922 void *sgpgme_data_release_and_get_mem(gpgme_data_t data, size_t *len)
934 /* I know it's deprecated, but we don't compile with _LARGEFILE */
935 cm_gpgme_data_rewind(data);
936 while ((r = gpgme_data_read(data, buf, BUFSIZ)) > 0) {
937 result = realloc(result, r + w);
938 memcpy(result+w, buf, r);
944 gpgme_data_release(data);
953 gpgme_error_t cm_gpgme_data_rewind(gpgme_data_t dh)
955 #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
956 if (gpgme_data_seek(dh, (off_t)0, SEEK_SET) == -1)
957 return gpg_error_from_errno(errno);
961 return gpgme_data_rewind(dh);
965 #endif /* USE_GPGME */