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/>.
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");
369 privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
371 gpgme_data_release(plain);
373 debug_print("can't open!\n");
379 if (fclose(dstfp) == EOF) {
380 FILE_OP_ERROR(fname, "fclose");
381 privacy_set_error(_("Couldn't close decrypted file %s"), fname);
383 gpgme_data_release(plain);
385 debug_print("can't open!\n");
389 parseinfo = procmime_scan_file(fname);
391 if (parseinfo == NULL) {
393 privacy_set_error(_("Couldn't parse decrypted file."));
396 decinfo = g_node_first_child(parseinfo->node) != NULL ?
397 g_node_first_child(parseinfo->node)->data : NULL;
398 if (decinfo == NULL) {
399 privacy_set_error(_("Couldn't parse decrypted file parts."));
404 g_node_unlink(decinfo->node);
405 procmime_mimeinfo_free_all(parseinfo);
409 if (sigstat != NULL && sigstat->signatures != NULL) {
410 if (decinfo->privacy != NULL) {
411 data = (PrivacyDataPGP *) decinfo->privacy;
413 data = pgpmime_new_privacydata();
414 decinfo->privacy = (PrivacyData *) data;
416 data->done_sigtest = TRUE;
417 data->is_signed = TRUE;
418 data->sigstatus = sigstat;
420 gpgme_release(data->ctx);
428 gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *from_addr)
430 MimeInfo *msgcontent, *sigmultipart, *newinfo;
431 gchar *textstr, *micalg = NULL;
433 gchar *boundary = NULL;
436 gpgme_data_t gpgtext, gpgsig;
439 struct passphrase_cb_info_s info;
440 gpgme_sign_result_t result = NULL;
445 privacy_set_error(_("Couldn't create temporary file: %s"), strerror(errno));
448 procmime_write_mimeinfo(mimeinfo, fp);
451 /* read temporary file into memory */
452 test_msg = file_read_stream_to_str(fp);
455 memset (&info, 0, sizeof info);
457 /* remove content node from message */
458 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
459 g_node_unlink(msgcontent->node);
461 /* create temporary multipart for content */
462 sigmultipart = procmime_mimeinfo_new();
463 sigmultipart->type = MIMETYPE_MULTIPART;
464 sigmultipart->subtype = g_strdup("signed");
468 boundary = generate_mime_boundary("Sig");
469 } while (strstr(test_msg, boundary) != NULL);
473 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("boundary"),
475 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("protocol"),
476 g_strdup("application/pgp-signature"));
477 g_node_append(sigmultipart->node, msgcontent->node);
478 g_node_append(mimeinfo->node, sigmultipart->node);
480 /* write message content to temporary file */
483 perror("my_tmpfile");
484 privacy_set_error(_("Couldn't create temporary file: %s"), strerror(errno));
487 procmime_write_mimeinfo(sigmultipart, fp);
490 /* read temporary file into memory */
491 textstr = get_canonical_content(fp, boundary);
495 gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
496 gpgme_data_new(&gpgsig);
497 if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
498 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
499 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
502 gpgme_set_textmode(ctx, 1);
503 gpgme_set_armor(ctx, 1);
504 gpgme_signers_clear (ctx);
506 if (!sgpgme_setup_signers(ctx, account, from_addr)) {
511 prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
512 if (getenv("GPG_AGENT_INFO") && prefs_gpg_get_config()->use_gpg_agent) {
513 debug_print("GPG_AGENT_INFO environment defined, running without passphrase callback\n");
516 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
519 err = gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_DETACH);
520 if (err != GPG_ERR_NO_ERROR) {
521 if (err == GPG_ERR_CANCELED) {
522 /* ignore cancelled signing */
523 privacy_reset_error();
524 debug_print("gpgme_op_sign cancelled\n");
526 privacy_set_error(_("Data signing failed, %s"), gpgme_strerror(err));
527 debug_print("gpgme_op_sign error : %x\n", err);
532 result = gpgme_op_sign_result(ctx);
533 if (result && result->signatures) {
534 gpgme_new_signature_t sig = result->signatures;
535 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
536 micalg = g_strdup_printf("PGP-%s", gpgme_hash_algo_name(
537 result->signatures->hash_algo));
539 micalg = g_strdup(gpgme_hash_algo_name(
540 result->signatures->hash_algo));
543 debug_print("valid signature: %s\n", sig->fpr);
546 } else if (result && result->invalid_signers) {
547 gpgme_invalid_key_t invalid = result->invalid_signers;
549 g_warning("invalid signer: %s (%s)", invalid->fpr,
550 gpgme_strerror(invalid->reason));
551 privacy_set_error(_("Data signing failed due to invalid signer: %s"),
552 gpgme_strerror(invalid->reason));
553 invalid = invalid->next;
558 /* can't get result (maybe no signing key?) */
559 debug_print("gpgme_op_sign_result error\n");
560 privacy_set_error(_("Data signing failed, no results."));
565 sigcontent = sgpgme_data_release_and_get_mem(gpgsig, &len);
566 gpgme_data_release(gpgtext);
569 if (sigcontent == NULL || len <= 0) {
570 g_warning("sgpgme_data_release_and_get_mem failed");
571 privacy_set_error(_("Data signing failed, no contents."));
577 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("micalg"),
580 newinfo = procmime_mimeinfo_new();
581 newinfo->type = MIMETYPE_APPLICATION;
582 newinfo->subtype = g_strdup("pgp-signature");
583 g_hash_table_insert(newinfo->typeparameters, g_strdup("name"),
584 g_strdup("signature.asc"));
585 newinfo->content = MIMECONTENT_MEM;
586 newinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
587 g_hash_table_insert(newinfo->dispositionparameters, g_strdup("filename"),
588 g_strdup("signature.asc"));
589 newinfo->data.mem = g_malloc(len + 1);
590 g_memmove(newinfo->data.mem, sigcontent, len);
591 newinfo->data.mem[len] = '\0';
592 g_node_append(sigmultipart->node, newinfo->node);
599 gchar *pgpmime_get_encrypt_data(GSList *recp_names)
601 return sgpgme_get_encrypt_data(recp_names, GPGME_PROTOCOL_OpenPGP);
604 static const gchar *pgpmime_get_encrypt_warning(void)
606 if (prefs_gpg_should_skip_encryption_warning(pgpmime_system.id))
609 return _("Please note that email headers, like Subject, "
610 "are not encrypted by the PGP/Mime system.");
613 static void pgpmime_inhibit_encrypt_warning(gboolean inhibit)
616 prefs_gpg_add_skip_encryption_warning(pgpmime_system.id);
618 prefs_gpg_remove_skip_encryption_warning(pgpmime_system.id);
621 gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
623 MimeInfo *msgcontent, *encmultipart, *newinfo;
625 gchar *boundary, *enccontent;
628 gpgme_data_t gpgtext = NULL, gpgenc = NULL;
629 gpgme_ctx_t ctx = NULL;
630 gpgme_key_t *kset = NULL;
631 gchar **fprs = g_strsplit(encrypt_data, " ", -1);
635 while (fprs[i] && strlen(fprs[i])) {
639 kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
640 memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
641 if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
642 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
643 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
648 while (fprs[i] && strlen(fprs[i])) {
650 err = gpgme_get_key(ctx, fprs[i], &key, 0);
652 debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
653 privacy_set_error(_("Couldn't add GPG key %s, %s"), fprs[i], gpgme_strerror(err));
657 debug_print("found %s at %d\n", fprs[i], i);
662 debug_print("Encrypting message content\n");
664 /* remove content node from message */
665 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
666 g_node_unlink(msgcontent->node);
668 /* create temporary multipart for content */
669 encmultipart = procmime_mimeinfo_new();
670 encmultipart->type = MIMETYPE_MULTIPART;
671 encmultipart->subtype = g_strdup("encrypted");
672 boundary = generate_mime_boundary("Encrypt");
673 g_hash_table_insert(encmultipart->typeparameters, g_strdup("boundary"),
675 g_hash_table_insert(encmultipart->typeparameters, g_strdup("protocol"),
676 g_strdup("application/pgp-encrypted"));
677 g_node_append(encmultipart->node, msgcontent->node);
679 /* write message content to temporary file */
682 privacy_set_error(_("Couldn't create temporary file, %s"), strerror(errno));
686 procmime_write_mimeinfo(encmultipart, fp);
689 /* read temporary file into memory */
690 textstr = get_canonical_content(fp, boundary);
695 gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
696 gpgme_data_new(&gpgenc);
697 gpgme_set_armor(ctx, 1);
698 cm_gpgme_data_rewind(gpgtext);
700 err = gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
702 enccontent = sgpgme_data_release_and_get_mem(gpgenc, &len);
703 gpgme_data_release(gpgtext);
707 if (enccontent == NULL || len <= 0) {
708 g_warning("sgpgme_data_release_and_get_mem failed");
709 privacy_set_error(_("Encryption failed, %s"), gpgme_strerror(err));
714 /* create encrypted multipart */
715 g_node_unlink(msgcontent->node);
716 procmime_mimeinfo_free_all(msgcontent);
717 g_node_append(mimeinfo->node, encmultipart->node);
719 newinfo = procmime_mimeinfo_new();
720 newinfo->type = MIMETYPE_APPLICATION;
721 newinfo->subtype = g_strdup("pgp-encrypted");
722 newinfo->content = MIMECONTENT_MEM;
723 newinfo->data.mem = g_strdup("Version: 1\n");
724 g_node_append(encmultipart->node, newinfo->node);
726 newinfo = procmime_mimeinfo_new();
727 newinfo->type = MIMETYPE_APPLICATION;
728 newinfo->subtype = g_strdup("octet-stream");
729 newinfo->content = MIMECONTENT_MEM;
730 newinfo->data.mem = g_malloc(len + 1);
731 g_memmove(newinfo->data.mem, enccontent, len);
732 newinfo->data.mem[len] = '\0';
733 g_node_append(encmultipart->node, newinfo->node);
741 static PrivacySystem pgpmime_system = {
743 "PGP MIME", /* name */
745 pgpmime_free_privacydata, /* free_privacydata */
747 pgpmime_is_signed, /* is_signed(MimeInfo *) */
748 pgpmime_check_signature, /* check_signature(MimeInfo *) */
749 pgpmime_get_sig_status, /* get_sig_status(MimeInfo *) */
750 pgpmime_get_sig_info_short, /* get_sig_info_short(MimeInfo *) */
751 pgpmime_get_sig_info_full, /* get_sig_info_full(MimeInfo *) */
753 pgpmime_is_encrypted, /* is_encrypted(MimeInfo *) */
754 pgpmime_decrypt, /* decrypt(MimeInfo *) */
760 pgpmime_get_encrypt_data,
762 pgpmime_get_encrypt_warning,
763 pgpmime_inhibit_encrypt_warning,
768 privacy_register_system(&pgpmime_system);
773 privacy_unregister_system(&pgpmime_system);
776 struct PluginFeature *plugin_provides(void)
778 static struct PluginFeature features[] =
779 { {PLUGIN_PRIVACY, N_("PGP/Mime")},
780 {PLUGIN_NOTHING, NULL}};
783 #endif /* USE_GPGME */