2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 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 g_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 g_return_val_if_fail(mimeinfo != NULL, -1);
177 g_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 g_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."));
204 textstr = get_canonical_content(fp, boundary);
206 err = gpgme_data_new_from_mem(&textdata, textstr, (size_t)strlen(textstr), 0);
208 debug_print ("gpgme_data_new_from_mem failed: %s\n",
209 gpgme_strerror (err));
211 signature = (MimeInfo *) mimeinfo->node->next->data;
212 sigdata = sgpgme_data_from_mimeinfo(signature);
215 if (signature->encoding_type == ENC_BASE64) {
216 err = gpgme_data_set_encoding (sigdata, GPGME_DATA_ENCODING_BASE64);
220 debug_print ("gpgme_data_set_encoding failed: %s\n",
221 gpgme_strerror (err));
225 sgpgme_verify_signature (data->ctx, sigdata, textdata, NULL);
227 gpgme_data_release(sigdata);
228 gpgme_data_release(textdata);
235 static SignatureStatus pgpmime_get_sig_status(MimeInfo *mimeinfo)
237 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
239 g_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
241 if (data->sigstatus == NULL &&
242 prefs_gpg_get_config()->auto_check_signatures)
243 pgpmime_check_signature(mimeinfo);
245 return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
248 static gchar *pgpmime_get_sig_info_short(MimeInfo *mimeinfo)
250 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
252 g_return_val_if_fail(data != NULL, g_strdup("Error"));
254 if (data->sigstatus == NULL &&
255 prefs_gpg_get_config()->auto_check_signatures)
256 pgpmime_check_signature(mimeinfo);
258 return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
261 static gchar *pgpmime_get_sig_info_full(MimeInfo *mimeinfo)
263 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
265 g_return_val_if_fail(data != NULL, g_strdup("Error"));
267 if (data->sigstatus == NULL &&
268 prefs_gpg_get_config()->auto_check_signatures)
269 pgpmime_check_signature(mimeinfo);
271 return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
274 static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
279 if (mimeinfo->type != MIMETYPE_MULTIPART)
281 if (g_ascii_strcasecmp(mimeinfo->subtype, "encrypted"))
283 tmpstr = procmime_mimeinfo_get_parameter(mimeinfo, "protocol");
284 if ((tmpstr == NULL) || g_ascii_strcasecmp(tmpstr, "application/pgp-encrypted"))
286 if (g_node_n_children(mimeinfo->node) != 2)
289 tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 0)->data;
290 if (tmpinfo->type != MIMETYPE_APPLICATION)
292 if (g_ascii_strcasecmp(tmpinfo->subtype, "pgp-encrypted"))
295 tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
296 if (tmpinfo->type != MIMETYPE_APPLICATION)
298 if (g_ascii_strcasecmp(tmpinfo->subtype, "octet-stream"))
304 static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
306 MimeInfo *encinfo, *decinfo, *parseinfo;
307 gpgme_data_t cipher = NULL, plain = NULL;
311 gpgme_verify_result_t sigstat = NULL;
312 PrivacyDataPGP *data = NULL;
318 if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
319 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
320 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
324 g_return_val_if_fail(pgpmime_is_encrypted(mimeinfo), NULL);
326 encinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
328 cipher = sgpgme_data_from_mimeinfo(encinfo);
329 plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
331 gpgme_data_release(cipher);
333 debug_print("plain is null!\n");
338 fname = g_strdup_printf("%s%cplaintext.%08x",
339 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
341 if ((dstfp = g_fopen(fname, "wb")) == NULL) {
342 FILE_OP_ERROR(fname, "fopen");
343 privacy_set_error(_("Couldn't open decrypted file %s"), fname);
345 gpgme_data_release(plain);
347 debug_print("can't open!\n");
351 if (fprintf(dstfp, "MIME-Version: 1.0\n") < 0) {
352 FILE_OP_ERROR(fname, "fprintf");
353 privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
355 gpgme_data_release(plain);
357 debug_print("can't open!\n");
361 chars = sgpgme_data_release_and_get_mem(plain, &len);
363 if (fwrite(chars, 1, len, dstfp) < len) {
364 FILE_OP_ERROR(fname, "fwrite");
365 privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
367 gpgme_data_release(plain);
369 debug_print("can't open!\n");
373 if (fclose(dstfp) == EOF) {
374 FILE_OP_ERROR(fname, "fclose");
375 privacy_set_error(_("Couldn't close decrypted file %s"), fname);
377 gpgme_data_release(plain);
379 debug_print("can't open!\n");
383 parseinfo = procmime_scan_file(fname);
385 if (parseinfo == NULL) {
387 privacy_set_error(_("Couldn't parse decrypted file."));
390 decinfo = g_node_first_child(parseinfo->node) != NULL ?
391 g_node_first_child(parseinfo->node)->data : NULL;
392 if (decinfo == NULL) {
393 privacy_set_error(_("Couldn't parse decrypted file parts."));
398 g_node_unlink(decinfo->node);
399 procmime_mimeinfo_free_all(parseinfo);
403 if (sigstat != NULL && sigstat->signatures != NULL) {
404 if (decinfo->privacy != NULL) {
405 data = (PrivacyDataPGP *) decinfo->privacy;
407 data = pgpmime_new_privacydata();
408 decinfo->privacy = (PrivacyData *) data;
410 data->done_sigtest = TRUE;
411 data->is_signed = TRUE;
412 data->sigstatus = sigstat;
414 gpgme_release(data->ctx);
422 gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *from_addr)
424 MimeInfo *msgcontent, *sigmultipart, *newinfo;
425 gchar *textstr, *micalg;
427 gchar *boundary = NULL;
430 gpgme_data_t gpgtext, gpgsig;
433 struct passphrase_cb_info_s info;
434 gpgme_sign_result_t result = NULL;
439 privacy_set_error(_("Couldn't create temporary file: %s"), strerror(errno));
442 procmime_write_mimeinfo(mimeinfo, fp);
445 /* read temporary file into memory */
446 test_msg = file_read_stream_to_str(fp);
449 memset (&info, 0, sizeof info);
451 /* remove content node from message */
452 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
453 g_node_unlink(msgcontent->node);
455 /* create temporary multipart for content */
456 sigmultipart = procmime_mimeinfo_new();
457 sigmultipart->type = MIMETYPE_MULTIPART;
458 sigmultipart->subtype = g_strdup("signed");
462 boundary = generate_mime_boundary("Sig");
463 } while (strstr(test_msg, boundary) != NULL);
467 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("boundary"),
469 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("protocol"),
470 g_strdup("application/pgp-signature"));
471 g_node_append(sigmultipart->node, msgcontent->node);
472 g_node_append(mimeinfo->node, sigmultipart->node);
474 /* write message content to temporary file */
477 perror("my_tmpfile");
478 privacy_set_error(_("Couldn't create temporary file: %s"), strerror(errno));
481 procmime_write_mimeinfo(sigmultipart, fp);
484 /* read temporary file into memory */
485 textstr = get_canonical_content(fp, boundary);
489 gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
490 gpgme_data_new(&gpgsig);
491 if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
492 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
493 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
496 gpgme_set_textmode(ctx, 1);
497 gpgme_set_armor(ctx, 1);
498 gpgme_signers_clear (ctx);
500 if (!sgpgme_setup_signers(ctx, account, from_addr)) {
505 prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
506 if (getenv("GPG_AGENT_INFO") && prefs_gpg_get_config()->use_gpg_agent) {
507 debug_print("GPG_AGENT_INFO environment defined, running without passphrase callback\n");
510 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
513 err = gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_DETACH);
514 if (err != GPG_ERR_NO_ERROR) {
515 if (err == GPG_ERR_CANCELED) {
516 /* ignore cancelled signing */
517 privacy_reset_error();
518 debug_print("gpgme_op_sign cancelled\n");
520 privacy_set_error(_("Data signing failed, %s"), gpgme_strerror(err));
521 debug_print("gpgme_op_sign error : %x\n", err);
526 result = gpgme_op_sign_result(ctx);
527 if (result && result->signatures) {
528 gpgme_new_signature_t sig = result->signatures;
529 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
530 micalg = g_strdup_printf("PGP-%s", gpgme_hash_algo_name(
531 result->signatures->hash_algo));
533 micalg = g_strdup(gpgme_hash_algo_name(
534 result->signatures->hash_algo));
537 debug_print("valid signature: %s\n", sig->fpr);
540 } else if (result && result->invalid_signers) {
541 gpgme_invalid_key_t invalid = result->invalid_signers;
543 g_warning("invalid signer: %s (%s)", invalid->fpr,
544 gpgme_strerror(invalid->reason));
545 privacy_set_error(_("Data signing failed due to invalid signer: %s"),
546 gpgme_strerror(invalid->reason));
547 invalid = invalid->next;
552 /* can't get result (maybe no signing key?) */
553 debug_print("gpgme_op_sign_result error\n");
554 privacy_set_error(_("Data signing failed, no results."));
559 sigcontent = sgpgme_data_release_and_get_mem(gpgsig, &len);
560 gpgme_data_release(gpgtext);
563 if (sigcontent == NULL || len <= 0) {
564 g_warning("sgpgme_data_release_and_get_mem failed");
565 privacy_set_error(_("Data signing failed, no contents."));
570 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("micalg"),
573 newinfo = procmime_mimeinfo_new();
574 newinfo->type = MIMETYPE_APPLICATION;
575 newinfo->subtype = g_strdup("pgp-signature");
576 g_hash_table_insert(newinfo->typeparameters, g_strdup("name"),
577 g_strdup("signature.asc"));
578 newinfo->content = MIMECONTENT_MEM;
579 newinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
580 g_hash_table_insert(newinfo->dispositionparameters, g_strdup("filename"),
581 g_strdup("signature.asc"));
582 newinfo->data.mem = g_malloc(len + 1);
583 g_memmove(newinfo->data.mem, sigcontent, len);
584 newinfo->data.mem[len] = '\0';
585 g_node_append(sigmultipart->node, newinfo->node);
592 gchar *pgpmime_get_encrypt_data(GSList *recp_names)
594 return sgpgme_get_encrypt_data(recp_names, GPGME_PROTOCOL_OpenPGP);
597 static const gchar *pgpmime_get_encrypt_warning(void)
599 if (prefs_gpg_should_skip_encryption_warning(pgpmime_system.id))
602 return _("Please note that email headers, like Subject, "
603 "are not encrypted by the PGP/Mime system.");
606 static void pgpmime_inhibit_encrypt_warning(gboolean inhibit)
609 prefs_gpg_add_skip_encryption_warning(pgpmime_system.id);
611 prefs_gpg_remove_skip_encryption_warning(pgpmime_system.id);
614 gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
616 MimeInfo *msgcontent, *encmultipart, *newinfo;
618 gchar *boundary, *enccontent;
621 gpgme_data_t gpgtext = NULL, gpgenc = NULL;
622 gpgme_ctx_t ctx = NULL;
623 gpgme_key_t *kset = NULL;
624 gchar **fprs = g_strsplit(encrypt_data, " ", -1);
628 while (fprs[i] && strlen(fprs[i])) {
632 kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
633 memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
634 if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
635 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
636 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
640 while (fprs[i] && strlen(fprs[i])) {
642 err = gpgme_get_key(ctx, fprs[i], &key, 0);
644 debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
645 privacy_set_error(_("Couldn't add GPG key %s, %s"), fprs[i], gpgme_strerror(err));
648 debug_print("found %s at %d\n", fprs[i], i);
653 debug_print("Encrypting message content\n");
655 /* remove content node from message */
656 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
657 g_node_unlink(msgcontent->node);
659 /* create temporary multipart for content */
660 encmultipart = procmime_mimeinfo_new();
661 encmultipart->type = MIMETYPE_MULTIPART;
662 encmultipart->subtype = g_strdup("encrypted");
663 boundary = generate_mime_boundary("Encrypt");
664 g_hash_table_insert(encmultipart->typeparameters, g_strdup("boundary"),
666 g_hash_table_insert(encmultipart->typeparameters, g_strdup("protocol"),
667 g_strdup("application/pgp-encrypted"));
668 g_node_append(encmultipart->node, msgcontent->node);
670 /* write message content to temporary file */
673 privacy_set_error(_("Couldn't create temporary file, %s"), strerror(errno));
676 procmime_write_mimeinfo(encmultipart, fp);
679 /* read temporary file into memory */
680 textstr = get_canonical_content(fp, boundary);
685 gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
686 gpgme_data_new(&gpgenc);
687 gpgme_set_armor(ctx, 1);
688 gpgme_data_rewind(gpgtext);
690 err = gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
692 enccontent = sgpgme_data_release_and_get_mem(gpgenc, &len);
693 gpgme_data_release(gpgtext);
696 if (enccontent == NULL || len <= 0) {
697 g_warning("sgpgme_data_release_and_get_mem failed");
698 privacy_set_error(_("Encryption failed, %s"), gpgme_strerror(err));
703 /* create encrypted multipart */
704 g_node_unlink(msgcontent->node);
705 procmime_mimeinfo_free_all(msgcontent);
706 g_node_append(mimeinfo->node, encmultipart->node);
708 newinfo = procmime_mimeinfo_new();
709 newinfo->type = MIMETYPE_APPLICATION;
710 newinfo->subtype = g_strdup("pgp-encrypted");
711 newinfo->content = MIMECONTENT_MEM;
712 newinfo->data.mem = g_strdup("Version: 1\n");
713 g_node_append(encmultipart->node, newinfo->node);
715 newinfo = procmime_mimeinfo_new();
716 newinfo->type = MIMETYPE_APPLICATION;
717 newinfo->subtype = g_strdup("octet-stream");
718 newinfo->content = MIMECONTENT_MEM;
719 newinfo->data.mem = g_malloc(len + 1);
720 g_memmove(newinfo->data.mem, enccontent, len);
721 newinfo->data.mem[len] = '\0';
722 g_node_append(encmultipart->node, newinfo->node);
730 static PrivacySystem pgpmime_system = {
732 "PGP MIME", /* name */
734 pgpmime_free_privacydata, /* free_privacydata */
736 pgpmime_is_signed, /* is_signed(MimeInfo *) */
737 pgpmime_check_signature, /* check_signature(MimeInfo *) */
738 pgpmime_get_sig_status, /* get_sig_status(MimeInfo *) */
739 pgpmime_get_sig_info_short, /* get_sig_info_short(MimeInfo *) */
740 pgpmime_get_sig_info_full, /* get_sig_info_full(MimeInfo *) */
742 pgpmime_is_encrypted, /* is_encrypted(MimeInfo *) */
743 pgpmime_decrypt, /* decrypt(MimeInfo *) */
749 pgpmime_get_encrypt_data,
751 pgpmime_get_encrypt_warning,
752 pgpmime_inhibit_encrypt_warning,
757 privacy_register_system(&pgpmime_system);
762 privacy_unregister_system(&pgpmime_system);
765 struct PluginFeature *plugin_provides(void)
767 static struct PluginFeature features[] =
768 { {PLUGIN_PRIVACY, N_("PGP/Mime")},
769 {PLUGIN_NOTHING, NULL}};
772 #endif /* USE_GPGME */