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/>.
21 #include "claws-features.h"
28 #include <glib/gi18n.h>
39 #include <plugins/pgpcore/sgpgme.h>
40 #include <plugins/pgpcore/prefs_gpg.h>
41 #include <plugins/pgpcore/passphrase.h>
42 #include <plugins/pgpcore/pgp_utils.h>
44 #include "prefs_common.h"
46 typedef struct _PrivacyDataPGP PrivacyDataPGP;
48 struct _PrivacyDataPGP
52 gboolean done_sigtest;
54 gpgme_verify_result_t sigstatus;
58 static PrivacySystem pgpmime_system;
60 static gint pgpmime_check_signature(MimeInfo *mimeinfo);
62 static PrivacyDataPGP *pgpmime_new_privacydata()
67 data = g_new0(PrivacyDataPGP, 1);
68 data->data.system = &pgpmime_system;
69 data->done_sigtest = FALSE;
70 data->is_signed = FALSE;
71 data->sigstatus = NULL;
72 if ((err = gpgme_new(&data->ctx)) != GPG_ERR_NO_ERROR) {
73 g_warning("Couldn't initialize GPG context: %s", gpgme_strerror(err));
80 static void pgpmime_free_privacydata(PrivacyData *_data)
82 PrivacyDataPGP *data = (PrivacyDataPGP *) _data;
83 gpgme_release(data->ctx);
87 static gboolean pgpmime_is_signed(MimeInfo *mimeinfo)
91 const gchar *protocol;
92 PrivacyDataPGP *data = NULL;
94 cm_return_val_if_fail(mimeinfo != NULL, FALSE);
95 if (mimeinfo->privacy != NULL) {
96 data = (PrivacyDataPGP *) mimeinfo->privacy;
97 if (data->done_sigtest)
98 return data->is_signed;
102 parent = procmime_mimeinfo_parent(mimeinfo);
105 if ((parent->type != MIMETYPE_MULTIPART) ||
106 g_ascii_strcasecmp(parent->subtype, "signed"))
108 protocol = procmime_mimeinfo_get_parameter(parent, "protocol");
109 if ((protocol == NULL) ||
110 (g_ascii_strcasecmp(protocol, "application/pgp-signature")))
113 /* check if mimeinfo is the first child */
114 if (parent->node->children->data != mimeinfo)
117 /* check signature */
118 signature = parent->node->children->next != NULL ?
119 (MimeInfo *) parent->node->children->next->data : NULL;
120 if (signature == NULL)
122 if ((signature->type != MIMETYPE_APPLICATION) ||
123 (g_ascii_strcasecmp(signature->subtype, "pgp-signature")))
127 data = pgpmime_new_privacydata();
128 mimeinfo->privacy = (PrivacyData *) data;
131 data->done_sigtest = TRUE;
132 data->is_signed = TRUE;
138 static gchar *get_canonical_content(FILE *fp, const gchar *boundary)
145 boundary_len = strlen(boundary);
146 while (fgets(buf, sizeof(buf), fp) != NULL)
147 if (IS_BOUNDARY(buf, boundary, boundary_len))
150 textbuffer = g_string_new("");
151 while (fgets(buf, sizeof(buf), fp) != NULL) {
154 if (IS_BOUNDARY(buf, boundary, boundary_len))
157 buf2 = canonicalize_str(buf);
158 g_string_append(textbuffer, buf2);
161 g_string_truncate(textbuffer, textbuffer->len - 2);
163 ret = textbuffer->str;
164 g_string_free(textbuffer, FALSE);
169 static gint pgpmime_check_signature(MimeInfo *mimeinfo)
171 PrivacyDataPGP *data;
172 MimeInfo *parent, *signature;
176 gpgme_data_t sigdata = NULL, textdata = NULL;
178 cm_return_val_if_fail(mimeinfo != NULL, -1);
179 cm_return_val_if_fail(mimeinfo->privacy != NULL, -1);
180 data = (PrivacyDataPGP *) mimeinfo->privacy;
181 if ((err = gpgme_new(&data->ctx)) != GPG_ERR_NO_ERROR) {
182 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
183 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
188 debug_print("Checking PGP/MIME signature\n");
190 err = gpgme_set_protocol(data->ctx, GPGME_PROTOCOL_OpenPGP);
193 debug_print ("gpgme_set_protocol failed: %s\n",
194 gpgme_strerror (err));
196 parent = procmime_mimeinfo_parent(mimeinfo);
198 fp = g_fopen(parent->data.filename, "rb");
199 cm_return_val_if_fail(fp != NULL, SIGNATURE_INVALID);
201 boundary = g_hash_table_lookup(parent->typeparameters, "boundary");
203 privacy_set_error(_("Signature boundary not found."));
207 textstr = get_canonical_content(fp, boundary);
209 err = gpgme_data_new_from_mem(&textdata, textstr, (size_t)strlen(textstr), 0);
211 debug_print ("gpgme_data_new_from_mem failed: %s\n",
212 gpgme_strerror (err));
214 signature = (MimeInfo *) mimeinfo->node->next->data;
215 sigdata = sgpgme_data_from_mimeinfo(signature);
218 if (signature->encoding_type == ENC_BASE64) {
219 err = gpgme_data_set_encoding (sigdata, GPGME_DATA_ENCODING_BASE64);
223 debug_print ("gpgme_data_set_encoding failed: %s\n",
224 gpgme_strerror (err));
228 sgpgme_verify_signature (data->ctx, sigdata, textdata, NULL);
230 gpgme_data_release(sigdata);
231 gpgme_data_release(textdata);
238 static SignatureStatus pgpmime_get_sig_status(MimeInfo *mimeinfo)
240 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
242 cm_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
244 return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
247 static gchar *pgpmime_get_sig_info_short(MimeInfo *mimeinfo)
249 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
251 cm_return_val_if_fail(data != NULL, g_strdup("Error"));
253 return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
256 static gchar *pgpmime_get_sig_info_full(MimeInfo *mimeinfo)
258 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
260 cm_return_val_if_fail(data != NULL, g_strdup("Error"));
262 return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
265 static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
269 const gchar *begin_indicator = "-----BEGIN PGP MESSAGE-----";
270 const gchar *end_indicator = "-----END PGP MESSAGE-----";
273 if (mimeinfo->type != MIMETYPE_MULTIPART)
275 if (g_ascii_strcasecmp(mimeinfo->subtype, "encrypted"))
277 tmpstr = procmime_mimeinfo_get_parameter(mimeinfo, "protocol");
278 if ((tmpstr == NULL) || g_ascii_strcasecmp(tmpstr, "application/pgp-encrypted"))
280 if (g_node_n_children(mimeinfo->node) != 2)
283 tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 0)->data;
284 if (tmpinfo->type != MIMETYPE_APPLICATION)
286 if (g_ascii_strcasecmp(tmpinfo->subtype, "pgp-encrypted"))
289 tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
290 if (tmpinfo->type != MIMETYPE_APPLICATION)
292 if (g_ascii_strcasecmp(tmpinfo->subtype, "octet-stream"))
295 textdata = get_part_as_string(tmpinfo);
299 if (!pgp_locate_armor_header(textdata, begin_indicator)) {
303 if (!pgp_locate_armor_header(textdata, end_indicator)) {
313 static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
315 MimeInfo *encinfo, *decinfo, *parseinfo;
316 gpgme_data_t cipher = NULL, plain = NULL;
320 gpgme_verify_result_t sigstat = NULL;
321 PrivacyDataPGP *data = NULL;
327 if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
328 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
329 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
333 cm_return_val_if_fail(pgpmime_is_encrypted(mimeinfo), NULL);
335 encinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
337 cipher = sgpgme_data_from_mimeinfo(encinfo);
338 plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
340 gpgme_data_release(cipher);
342 debug_print("plain is null!\n");
347 fname = g_strdup_printf("%s%cplaintext.%08x",
348 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
350 if ((dstfp = g_fopen(fname, "wb")) == NULL) {
351 FILE_OP_ERROR(fname, "fopen");
352 privacy_set_error(_("Couldn't open decrypted file %s"), fname);
354 gpgme_data_release(plain);
356 debug_print("can't open!\n");
360 if (fprintf(dstfp, "MIME-Version: 1.0\n") < 0) {
361 FILE_OP_ERROR(fname, "fprintf");
363 privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
365 gpgme_data_release(plain);
367 debug_print("can't open!\n");
371 chars = sgpgme_data_release_and_get_mem(plain, &len);
373 if (fwrite(chars, 1, len, dstfp) < len) {
374 FILE_OP_ERROR(fname, "fwrite");
377 privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
379 gpgme_data_release(plain);
381 debug_print("can't open!\n");
387 if (fclose(dstfp) == EOF) {
388 FILE_OP_ERROR(fname, "fclose");
389 privacy_set_error(_("Couldn't close decrypted file %s"), fname);
391 gpgme_data_release(plain);
393 debug_print("can't open!\n");
397 parseinfo = procmime_scan_file(fname);
399 if (parseinfo == NULL) {
401 privacy_set_error(_("Couldn't parse decrypted file."));
404 decinfo = g_node_first_child(parseinfo->node) != NULL ?
405 g_node_first_child(parseinfo->node)->data : NULL;
406 if (decinfo == NULL) {
407 privacy_set_error(_("Couldn't parse decrypted file parts."));
412 g_node_unlink(decinfo->node);
413 procmime_mimeinfo_free_all(&parseinfo);
417 if (sigstat != NULL && sigstat->signatures != NULL) {
418 if (decinfo->privacy != NULL) {
419 data = (PrivacyDataPGP *) decinfo->privacy;
421 data = pgpmime_new_privacydata();
422 decinfo->privacy = (PrivacyData *) data;
425 data->done_sigtest = TRUE;
426 data->is_signed = TRUE;
427 data->sigstatus = sigstat;
429 gpgme_release(data->ctx);
438 gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *from_addr)
440 MimeInfo *msgcontent, *sigmultipart, *newinfo;
441 gchar *textstr, *micalg = NULL;
443 gchar *boundary = NULL;
446 gpgme_data_t gpgtext, gpgsig;
449 struct passphrase_cb_info_s info;
450 gpgme_sign_result_t result = NULL;
455 privacy_set_error(_("Couldn't create temporary file: %s"), g_strerror(errno));
458 procmime_write_mimeinfo(mimeinfo, fp);
461 /* read temporary file into memory */
462 test_msg = file_read_stream_to_str(fp);
465 memset (&info, 0, sizeof info);
467 /* remove content node from message */
468 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
469 g_node_unlink(msgcontent->node);
471 /* create temporary multipart for content */
472 sigmultipart = procmime_mimeinfo_new();
473 sigmultipart->type = MIMETYPE_MULTIPART;
474 sigmultipart->subtype = g_strdup("signed");
478 boundary = generate_mime_boundary("Sig");
479 } while (strstr(test_msg, boundary) != NULL);
483 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("boundary"),
485 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("protocol"),
486 g_strdup("application/pgp-signature"));
487 g_node_append(sigmultipart->node, msgcontent->node);
488 g_node_append(mimeinfo->node, sigmultipart->node);
490 /* write message content to temporary file */
493 perror("my_tmpfile");
494 privacy_set_error(_("Couldn't create temporary file: %s"), g_strerror(errno));
497 procmime_write_mimeinfo(sigmultipart, fp);
500 /* read temporary file into memory */
501 textstr = get_canonical_content(fp, boundary);
505 gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
506 gpgme_data_new(&gpgsig);
507 if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
508 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
509 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
512 gpgme_set_textmode(ctx, 1);
513 gpgme_set_armor(ctx, 1);
514 gpgme_signers_clear (ctx);
516 if (!sgpgme_setup_signers(ctx, account, from_addr)) {
521 prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
522 if (g_getenv("GPG_AGENT_INFO") && prefs_gpg_get_config()->use_gpg_agent) {
523 debug_print("GPG_AGENT_INFO environment defined, running without passphrase callback\n");
526 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
529 err = gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_DETACH);
530 if (err != GPG_ERR_NO_ERROR) {
531 if (err == GPG_ERR_CANCELED) {
532 /* ignore cancelled signing */
533 privacy_reset_error();
534 debug_print("gpgme_op_sign cancelled\n");
536 privacy_set_error(_("Data signing failed, %s"), gpgme_strerror(err));
537 debug_print("gpgme_op_sign error : %x\n", err);
542 result = gpgme_op_sign_result(ctx);
543 if (result && result->signatures) {
544 gpgme_new_signature_t sig = result->signatures;
545 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
546 gchar *down_algo = g_ascii_strdown(gpgme_hash_algo_name(
547 result->signatures->hash_algo), -1);
548 micalg = g_strdup_printf("pgp-%s", down_algo);
551 micalg = g_strdup(gpgme_hash_algo_name(
552 result->signatures->hash_algo));
555 debug_print("valid signature: %s\n", sig->fpr);
558 } else if (result && result->invalid_signers) {
559 gpgme_invalid_key_t invalid = result->invalid_signers;
561 g_warning("invalid signer: %s (%s)", invalid->fpr,
562 gpgme_strerror(invalid->reason));
563 privacy_set_error(_("Data signing failed due to invalid signer: %s"),
564 gpgme_strerror(invalid->reason));
565 invalid = invalid->next;
570 /* can't get result (maybe no signing key?) */
571 debug_print("gpgme_op_sign_result error\n");
572 privacy_set_error(_("Data signing failed, no results."));
577 sigcontent = sgpgme_data_release_and_get_mem(gpgsig, &len);
578 gpgme_data_release(gpgtext);
581 if (sigcontent == NULL || len <= 0) {
582 g_warning("sgpgme_data_release_and_get_mem failed");
583 privacy_set_error(_("Data signing failed, no contents."));
590 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("micalg"),
593 newinfo = procmime_mimeinfo_new();
594 newinfo->type = MIMETYPE_APPLICATION;
595 newinfo->subtype = g_strdup("pgp-signature");
596 newinfo->description = g_strdup(_("OpenPGP digital signature"));
597 newinfo->content = MIMECONTENT_MEM;
598 newinfo->data.mem = g_malloc(len + 1);
599 g_memmove(newinfo->data.mem, sigcontent, len);
600 newinfo->data.mem[len] = '\0';
601 g_node_append(sigmultipart->node, newinfo->node);
608 gchar *pgpmime_get_encrypt_data(GSList *recp_names)
610 return sgpgme_get_encrypt_data(recp_names, GPGME_PROTOCOL_OpenPGP);
613 static const gchar *pgpmime_get_encrypt_warning(void)
615 if (prefs_gpg_should_skip_encryption_warning(pgpmime_system.id))
618 return _("Please note that email headers, like Subject, "
619 "are not encrypted by the PGP/Mime system.");
622 static void pgpmime_inhibit_encrypt_warning(gboolean inhibit)
625 prefs_gpg_add_skip_encryption_warning(pgpmime_system.id);
627 prefs_gpg_remove_skip_encryption_warning(pgpmime_system.id);
630 gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
632 MimeInfo *msgcontent, *encmultipart, *newinfo;
634 gchar *boundary, *enccontent;
637 gpgme_data_t gpgtext = NULL, gpgenc = NULL;
638 gpgme_ctx_t ctx = NULL;
639 gpgme_key_t *kset = NULL;
640 gchar **fprs = g_strsplit(encrypt_data, " ", -1);
644 while (fprs[i] && strlen(fprs[i])) {
648 kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
649 memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
650 if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
651 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
652 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
657 while (fprs[i] && strlen(fprs[i])) {
659 err = gpgme_get_key(ctx, fprs[i], &key, 0);
661 debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
662 privacy_set_error(_("Couldn't add GPG key %s, %s"), fprs[i], gpgme_strerror(err));
666 debug_print("found %s at %d\n", fprs[i], i);
671 debug_print("Encrypting message content\n");
673 /* remove content node from message */
674 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
675 g_node_unlink(msgcontent->node);
677 /* create temporary multipart for content */
678 encmultipart = procmime_mimeinfo_new();
679 encmultipart->type = MIMETYPE_MULTIPART;
680 encmultipart->subtype = g_strdup("encrypted");
681 boundary = generate_mime_boundary("Encrypt");
682 g_hash_table_insert(encmultipart->typeparameters, g_strdup("boundary"),
684 g_hash_table_insert(encmultipart->typeparameters, g_strdup("protocol"),
685 g_strdup("application/pgp-encrypted"));
686 g_node_append(encmultipart->node, msgcontent->node);
688 /* write message content to temporary file */
691 privacy_set_error(_("Couldn't create temporary file, %s"), g_strerror(errno));
695 procmime_write_mimeinfo(encmultipart, fp);
698 /* read temporary file into memory */
699 textstr = get_canonical_content(fp, boundary);
704 gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
705 gpgme_data_new(&gpgenc);
706 gpgme_set_armor(ctx, 1);
707 cm_gpgme_data_rewind(gpgtext);
709 err = gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
711 enccontent = sgpgme_data_release_and_get_mem(gpgenc, &len);
712 gpgme_data_release(gpgtext);
716 if (enccontent == NULL || len <= 0) {
717 g_warning("sgpgme_data_release_and_get_mem failed");
718 privacy_set_error(_("Encryption failed, %s"), gpgme_strerror(err));
724 /* create encrypted multipart */
725 g_node_unlink(msgcontent->node);
726 procmime_mimeinfo_free_all(&msgcontent);
727 g_node_append(mimeinfo->node, encmultipart->node);
729 newinfo = procmime_mimeinfo_new();
730 newinfo->type = MIMETYPE_APPLICATION;
731 newinfo->subtype = g_strdup("pgp-encrypted");
732 newinfo->content = MIMECONTENT_MEM;
733 newinfo->data.mem = g_strdup("Version: 1\n");
734 g_node_append(encmultipart->node, newinfo->node);
736 newinfo = procmime_mimeinfo_new();
737 newinfo->type = MIMETYPE_APPLICATION;
738 newinfo->subtype = g_strdup("octet-stream");
739 newinfo->content = MIMECONTENT_MEM;
740 newinfo->data.mem = g_malloc(len + 1);
741 g_memmove(newinfo->data.mem, enccontent, len);
742 newinfo->data.mem[len] = '\0';
743 g_node_append(encmultipart->node, newinfo->node);
751 static PrivacySystem pgpmime_system = {
753 "PGP MIME", /* name */
755 pgpmime_free_privacydata, /* free_privacydata */
757 pgpmime_is_signed, /* is_signed(MimeInfo *) */
758 pgpmime_check_signature, /* check_signature(MimeInfo *) */
759 pgpmime_get_sig_status, /* get_sig_status(MimeInfo *) */
760 pgpmime_get_sig_info_short, /* get_sig_info_short(MimeInfo *) */
761 pgpmime_get_sig_info_full, /* get_sig_info_full(MimeInfo *) */
763 pgpmime_is_encrypted, /* is_encrypted(MimeInfo *) */
764 pgpmime_decrypt, /* decrypt(MimeInfo *) */
770 pgpmime_get_encrypt_data,
772 pgpmime_get_encrypt_warning,
773 pgpmime_inhibit_encrypt_warning,
774 prefs_gpg_auto_check_signatures,
779 privacy_register_system(&pgpmime_system);
784 privacy_unregister_system(&pgpmime_system);
787 struct PluginFeature *plugin_provides(void)
789 static struct PluginFeature features[] =
790 { {PLUGIN_PRIVACY, N_("PGP/Mime")},
791 {PLUGIN_NOTHING, NULL}};
794 #endif /* USE_GPGME */