2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2011 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/>.
28 #include <glib/gi18n.h>
39 #include <plugins/pgpcore/sgpgme.h>
40 #include <plugins/pgpcore/prefs_gpg.h>
41 #include <plugins/pgpcore/passphrase.h>
43 #include "prefs_common.h"
45 typedef struct _PrivacyDataPGP PrivacyDataPGP;
47 struct _PrivacyDataPGP
51 gboolean done_sigtest;
53 gpgme_verify_result_t sigstatus;
57 static PrivacySystem pgpmime_system;
59 static gint pgpmime_check_signature(MimeInfo *mimeinfo);
61 static PrivacyDataPGP *pgpmime_new_privacydata()
66 data = g_new0(PrivacyDataPGP, 1);
67 data->data.system = &pgpmime_system;
68 data->done_sigtest = FALSE;
69 data->is_signed = FALSE;
70 data->sigstatus = NULL;
71 if ((err = gpgme_new(&data->ctx)) != GPG_ERR_NO_ERROR) {
72 g_warning(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
79 static void pgpmime_free_privacydata(PrivacyData *_data)
81 PrivacyDataPGP *data = (PrivacyDataPGP *) _data;
82 gpgme_release(data->ctx);
86 static gboolean pgpmime_is_signed(MimeInfo *mimeinfo)
90 const gchar *protocol;
91 PrivacyDataPGP *data = NULL;
93 cm_return_val_if_fail(mimeinfo != NULL, FALSE);
94 if (mimeinfo->privacy != NULL) {
95 data = (PrivacyDataPGP *) mimeinfo->privacy;
96 if (data->done_sigtest)
97 return data->is_signed;
101 parent = procmime_mimeinfo_parent(mimeinfo);
104 if ((parent->type != MIMETYPE_MULTIPART) ||
105 g_ascii_strcasecmp(parent->subtype, "signed"))
107 protocol = procmime_mimeinfo_get_parameter(parent, "protocol");
108 if ((protocol == NULL) ||
109 (g_ascii_strcasecmp(protocol, "application/pgp-signature")))
112 /* check if mimeinfo is the first child */
113 if (parent->node->children->data != mimeinfo)
116 /* check signature */
117 signature = parent->node->children->next != NULL ?
118 (MimeInfo *) parent->node->children->next->data : NULL;
119 if (signature == NULL)
121 if ((signature->type != MIMETYPE_APPLICATION) ||
122 (g_ascii_strcasecmp(signature->subtype, "pgp-signature")))
126 data = pgpmime_new_privacydata();
127 mimeinfo->privacy = (PrivacyData *) data;
130 data->done_sigtest = TRUE;
131 data->is_signed = TRUE;
136 static gchar *get_canonical_content(FILE *fp, const gchar *boundary)
143 boundary_len = strlen(boundary);
144 while (fgets(buf, sizeof(buf), fp) != NULL)
145 if (IS_BOUNDARY(buf, boundary, boundary_len))
148 textbuffer = g_string_new("");
149 while (fgets(buf, sizeof(buf), fp) != NULL) {
152 if (IS_BOUNDARY(buf, boundary, boundary_len))
155 buf2 = canonicalize_str(buf);
156 g_string_append(textbuffer, buf2);
159 g_string_truncate(textbuffer, textbuffer->len - 2);
161 ret = textbuffer->str;
162 g_string_free(textbuffer, FALSE);
167 static gint pgpmime_check_signature(MimeInfo *mimeinfo)
169 PrivacyDataPGP *data;
170 MimeInfo *parent, *signature;
174 gpgme_data_t sigdata = NULL, textdata = NULL;
176 cm_return_val_if_fail(mimeinfo != NULL, -1);
177 cm_return_val_if_fail(mimeinfo->privacy != NULL, -1);
178 data = (PrivacyDataPGP *) mimeinfo->privacy;
179 if ((err = gpgme_new(&data->ctx)) != GPG_ERR_NO_ERROR) {
180 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
181 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
186 debug_print("Checking PGP/MIME signature\n");
188 err = gpgme_set_protocol(data->ctx, GPGME_PROTOCOL_OpenPGP);
191 debug_print ("gpgme_set_protocol failed: %s\n",
192 gpgme_strerror (err));
194 parent = procmime_mimeinfo_parent(mimeinfo);
196 fp = g_fopen(parent->data.filename, "rb");
197 cm_return_val_if_fail(fp != NULL, SIGNATURE_INVALID);
199 boundary = g_hash_table_lookup(parent->typeparameters, "boundary");
201 privacy_set_error(_("Signature boundary not found."));
205 textstr = get_canonical_content(fp, boundary);
207 err = gpgme_data_new_from_mem(&textdata, textstr, (size_t)strlen(textstr), 0);
209 debug_print ("gpgme_data_new_from_mem failed: %s\n",
210 gpgme_strerror (err));
212 signature = (MimeInfo *) mimeinfo->node->next->data;
213 sigdata = sgpgme_data_from_mimeinfo(signature);
216 if (signature->encoding_type == ENC_BASE64) {
217 err = gpgme_data_set_encoding (sigdata, GPGME_DATA_ENCODING_BASE64);
221 debug_print ("gpgme_data_set_encoding failed: %s\n",
222 gpgme_strerror (err));
226 sgpgme_verify_signature (data->ctx, sigdata, textdata, NULL);
228 gpgme_data_release(sigdata);
229 gpgme_data_release(textdata);
236 static SignatureStatus pgpmime_get_sig_status(MimeInfo *mimeinfo)
238 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
240 cm_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
242 if (data->sigstatus == NULL &&
243 prefs_gpg_get_config()->auto_check_signatures)
244 pgpmime_check_signature(mimeinfo);
246 return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
249 static gchar *pgpmime_get_sig_info_short(MimeInfo *mimeinfo)
251 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
253 cm_return_val_if_fail(data != NULL, g_strdup("Error"));
255 if (data->sigstatus == NULL &&
256 prefs_gpg_get_config()->auto_check_signatures)
257 pgpmime_check_signature(mimeinfo);
259 return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
262 static gchar *pgpmime_get_sig_info_full(MimeInfo *mimeinfo)
264 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
266 cm_return_val_if_fail(data != NULL, g_strdup("Error"));
268 if (data->sigstatus == NULL &&
269 prefs_gpg_get_config()->auto_check_signatures)
270 pgpmime_check_signature(mimeinfo);
272 return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
275 static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
280 if (mimeinfo->type != MIMETYPE_MULTIPART)
282 if (g_ascii_strcasecmp(mimeinfo->subtype, "encrypted"))
284 tmpstr = procmime_mimeinfo_get_parameter(mimeinfo, "protocol");
285 if ((tmpstr == NULL) || g_ascii_strcasecmp(tmpstr, "application/pgp-encrypted"))
287 if (g_node_n_children(mimeinfo->node) != 2)
290 tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 0)->data;
291 if (tmpinfo->type != MIMETYPE_APPLICATION)
293 if (g_ascii_strcasecmp(tmpinfo->subtype, "pgp-encrypted"))
296 tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
297 if (tmpinfo->type != MIMETYPE_APPLICATION)
299 if (g_ascii_strcasecmp(tmpinfo->subtype, "octet-stream"))
305 static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
307 MimeInfo *encinfo, *decinfo, *parseinfo;
308 gpgme_data_t cipher = NULL, plain = NULL;
312 gpgme_verify_result_t sigstat = NULL;
313 PrivacyDataPGP *data = NULL;
319 if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
320 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
321 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
325 cm_return_val_if_fail(pgpmime_is_encrypted(mimeinfo), NULL);
327 encinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
329 cipher = sgpgme_data_from_mimeinfo(encinfo);
330 plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
332 gpgme_data_release(cipher);
334 debug_print("plain is null!\n");
339 fname = g_strdup_printf("%s%cplaintext.%08x",
340 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
342 if ((dstfp = g_fopen(fname, "wb")) == NULL) {
343 FILE_OP_ERROR(fname, "fopen");
344 privacy_set_error(_("Couldn't open decrypted file %s"), fname);
346 gpgme_data_release(plain);
348 debug_print("can't open!\n");
352 if (fprintf(dstfp, "MIME-Version: 1.0\n") < 0) {
353 FILE_OP_ERROR(fname, "fprintf");
355 privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
357 gpgme_data_release(plain);
359 debug_print("can't open!\n");
363 chars = sgpgme_data_release_and_get_mem(plain, &len);
365 if (fwrite(chars, 1, len, dstfp) < len) {
366 FILE_OP_ERROR(fname, "fwrite");
368 privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
370 gpgme_data_release(plain);
372 debug_print("can't open!\n");
376 if (fclose(dstfp) == EOF) {
377 FILE_OP_ERROR(fname, "fclose");
378 privacy_set_error(_("Couldn't close decrypted file %s"), fname);
380 gpgme_data_release(plain);
382 debug_print("can't open!\n");
386 parseinfo = procmime_scan_file(fname);
388 if (parseinfo == NULL) {
390 privacy_set_error(_("Couldn't parse decrypted file."));
393 decinfo = g_node_first_child(parseinfo->node) != NULL ?
394 g_node_first_child(parseinfo->node)->data : NULL;
395 if (decinfo == NULL) {
396 privacy_set_error(_("Couldn't parse decrypted file parts."));
401 g_node_unlink(decinfo->node);
402 procmime_mimeinfo_free_all(parseinfo);
406 if (sigstat != NULL && sigstat->signatures != NULL) {
407 if (decinfo->privacy != NULL) {
408 data = (PrivacyDataPGP *) decinfo->privacy;
410 data = pgpmime_new_privacydata();
411 decinfo->privacy = (PrivacyData *) data;
413 data->done_sigtest = TRUE;
414 data->is_signed = TRUE;
415 data->sigstatus = sigstat;
417 gpgme_release(data->ctx);
425 gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *from_addr)
427 MimeInfo *msgcontent, *sigmultipart, *newinfo;
428 gchar *textstr, *micalg = NULL;
430 gchar *boundary = NULL;
433 gpgme_data_t gpgtext, gpgsig;
436 struct passphrase_cb_info_s info;
437 gpgme_sign_result_t result = NULL;
442 privacy_set_error(_("Couldn't create temporary file: %s"), strerror(errno));
445 procmime_write_mimeinfo(mimeinfo, fp);
448 /* read temporary file into memory */
449 test_msg = file_read_stream_to_str(fp);
452 memset (&info, 0, sizeof info);
454 /* remove content node from message */
455 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
456 g_node_unlink(msgcontent->node);
458 /* create temporary multipart for content */
459 sigmultipart = procmime_mimeinfo_new();
460 sigmultipart->type = MIMETYPE_MULTIPART;
461 sigmultipart->subtype = g_strdup("signed");
465 boundary = generate_mime_boundary("Sig");
466 } while (strstr(test_msg, boundary) != NULL);
470 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("boundary"),
472 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("protocol"),
473 g_strdup("application/pgp-signature"));
474 g_node_append(sigmultipart->node, msgcontent->node);
475 g_node_append(mimeinfo->node, sigmultipart->node);
477 /* write message content to temporary file */
480 perror("my_tmpfile");
481 privacy_set_error(_("Couldn't create temporary file: %s"), strerror(errno));
484 procmime_write_mimeinfo(sigmultipart, fp);
487 /* read temporary file into memory */
488 textstr = get_canonical_content(fp, boundary);
492 gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
493 gpgme_data_new(&gpgsig);
494 if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
495 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
496 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
499 gpgme_set_textmode(ctx, 1);
500 gpgme_set_armor(ctx, 1);
501 gpgme_signers_clear (ctx);
503 if (!sgpgme_setup_signers(ctx, account, from_addr)) {
508 prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
509 if (getenv("GPG_AGENT_INFO") && prefs_gpg_get_config()->use_gpg_agent) {
510 debug_print("GPG_AGENT_INFO environment defined, running without passphrase callback\n");
513 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
516 err = gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_DETACH);
517 if (err != GPG_ERR_NO_ERROR) {
518 if (err == GPG_ERR_CANCELED) {
519 /* ignore cancelled signing */
520 privacy_reset_error();
521 debug_print("gpgme_op_sign cancelled\n");
523 privacy_set_error(_("Data signing failed, %s"), gpgme_strerror(err));
524 debug_print("gpgme_op_sign error : %x\n", err);
529 result = gpgme_op_sign_result(ctx);
530 if (result && result->signatures) {
531 gpgme_new_signature_t sig = result->signatures;
532 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
533 micalg = g_strdup_printf("PGP-%s", gpgme_hash_algo_name(
534 result->signatures->hash_algo));
536 micalg = g_strdup(gpgme_hash_algo_name(
537 result->signatures->hash_algo));
540 debug_print("valid signature: %s\n", sig->fpr);
543 } else if (result && result->invalid_signers) {
544 gpgme_invalid_key_t invalid = result->invalid_signers;
546 g_warning("invalid signer: %s (%s)", invalid->fpr,
547 gpgme_strerror(invalid->reason));
548 privacy_set_error(_("Data signing failed due to invalid signer: %s"),
549 gpgme_strerror(invalid->reason));
550 invalid = invalid->next;
555 /* can't get result (maybe no signing key?) */
556 debug_print("gpgme_op_sign_result error\n");
557 privacy_set_error(_("Data signing failed, no results."));
562 sigcontent = sgpgme_data_release_and_get_mem(gpgsig, &len);
563 gpgme_data_release(gpgtext);
566 if (sigcontent == NULL || len <= 0) {
567 g_warning("sgpgme_data_release_and_get_mem failed");
568 privacy_set_error(_("Data signing failed, no contents."));
574 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("micalg"),
577 newinfo = procmime_mimeinfo_new();
578 newinfo->type = MIMETYPE_APPLICATION;
579 newinfo->subtype = g_strdup("pgp-signature");
580 g_hash_table_insert(newinfo->typeparameters, g_strdup("name"),
581 g_strdup("signature.asc"));
582 newinfo->content = MIMECONTENT_MEM;
583 newinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
584 g_hash_table_insert(newinfo->dispositionparameters, g_strdup("filename"),
585 g_strdup("signature.asc"));
586 newinfo->data.mem = g_malloc(len + 1);
587 g_memmove(newinfo->data.mem, sigcontent, len);
588 newinfo->data.mem[len] = '\0';
589 g_node_append(sigmultipart->node, newinfo->node);
596 gchar *pgpmime_get_encrypt_data(GSList *recp_names)
598 return sgpgme_get_encrypt_data(recp_names, GPGME_PROTOCOL_OpenPGP);
601 static const gchar *pgpmime_get_encrypt_warning(void)
603 if (prefs_gpg_should_skip_encryption_warning(pgpmime_system.id))
606 return _("Please note that email headers, like Subject, "
607 "are not encrypted by the PGP/Mime system.");
610 static void pgpmime_inhibit_encrypt_warning(gboolean inhibit)
613 prefs_gpg_add_skip_encryption_warning(pgpmime_system.id);
615 prefs_gpg_remove_skip_encryption_warning(pgpmime_system.id);
618 gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
620 MimeInfo *msgcontent, *encmultipart, *newinfo;
622 gchar *boundary, *enccontent;
625 gpgme_data_t gpgtext = NULL, gpgenc = NULL;
626 gpgme_ctx_t ctx = NULL;
627 gpgme_key_t *kset = NULL;
628 gchar **fprs = g_strsplit(encrypt_data, " ", -1);
632 while (fprs[i] && strlen(fprs[i])) {
636 kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
637 memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
638 if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
639 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
640 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
645 while (fprs[i] && strlen(fprs[i])) {
647 err = gpgme_get_key(ctx, fprs[i], &key, 0);
649 debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
650 privacy_set_error(_("Couldn't add GPG key %s, %s"), fprs[i], gpgme_strerror(err));
654 debug_print("found %s at %d\n", fprs[i], i);
659 debug_print("Encrypting message content\n");
661 /* remove content node from message */
662 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
663 g_node_unlink(msgcontent->node);
665 /* create temporary multipart for content */
666 encmultipart = procmime_mimeinfo_new();
667 encmultipart->type = MIMETYPE_MULTIPART;
668 encmultipart->subtype = g_strdup("encrypted");
669 boundary = generate_mime_boundary("Encrypt");
670 g_hash_table_insert(encmultipart->typeparameters, g_strdup("boundary"),
672 g_hash_table_insert(encmultipart->typeparameters, g_strdup("protocol"),
673 g_strdup("application/pgp-encrypted"));
674 g_node_append(encmultipart->node, msgcontent->node);
676 /* write message content to temporary file */
679 privacy_set_error(_("Couldn't create temporary file, %s"), strerror(errno));
683 procmime_write_mimeinfo(encmultipart, fp);
686 /* read temporary file into memory */
687 textstr = get_canonical_content(fp, boundary);
692 gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
693 gpgme_data_new(&gpgenc);
694 gpgme_set_armor(ctx, 1);
695 cm_gpgme_data_rewind(gpgtext);
697 err = gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
699 enccontent = sgpgme_data_release_and_get_mem(gpgenc, &len);
700 gpgme_data_release(gpgtext);
704 if (enccontent == NULL || len <= 0) {
705 g_warning("sgpgme_data_release_and_get_mem failed");
706 privacy_set_error(_("Encryption failed, %s"), gpgme_strerror(err));
711 /* create encrypted multipart */
712 g_node_unlink(msgcontent->node);
713 procmime_mimeinfo_free_all(msgcontent);
714 g_node_append(mimeinfo->node, encmultipart->node);
716 newinfo = procmime_mimeinfo_new();
717 newinfo->type = MIMETYPE_APPLICATION;
718 newinfo->subtype = g_strdup("pgp-encrypted");
719 newinfo->content = MIMECONTENT_MEM;
720 newinfo->data.mem = g_strdup("Version: 1\n");
721 g_node_append(encmultipart->node, newinfo->node);
723 newinfo = procmime_mimeinfo_new();
724 newinfo->type = MIMETYPE_APPLICATION;
725 newinfo->subtype = g_strdup("octet-stream");
726 newinfo->content = MIMECONTENT_MEM;
727 newinfo->data.mem = g_malloc(len + 1);
728 g_memmove(newinfo->data.mem, enccontent, len);
729 newinfo->data.mem[len] = '\0';
730 g_node_append(encmultipart->node, newinfo->node);
738 static PrivacySystem pgpmime_system = {
740 "PGP MIME", /* name */
742 pgpmime_free_privacydata, /* free_privacydata */
744 pgpmime_is_signed, /* is_signed(MimeInfo *) */
745 pgpmime_check_signature, /* check_signature(MimeInfo *) */
746 pgpmime_get_sig_status, /* get_sig_status(MimeInfo *) */
747 pgpmime_get_sig_info_short, /* get_sig_info_short(MimeInfo *) */
748 pgpmime_get_sig_info_full, /* get_sig_info_full(MimeInfo *) */
750 pgpmime_is_encrypted, /* is_encrypted(MimeInfo *) */
751 pgpmime_decrypt, /* decrypt(MimeInfo *) */
757 pgpmime_get_encrypt_data,
759 pgpmime_get_encrypt_warning,
760 pgpmime_inhibit_encrypt_warning,
765 privacy_register_system(&pgpmime_system);
770 privacy_unregister_system(&pgpmime_system);
773 struct PluginFeature *plugin_provides(void)
775 static struct PluginFeature features[] =
776 { {PLUGIN_PRIVACY, N_("PGP/Mime")},
777 {PLUGIN_NOTHING, NULL}};
780 #endif /* USE_GPGME */