2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto & 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 2 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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()
65 data = g_new0(PrivacyDataPGP, 1);
66 data->data.system = &pgpmime_system;
67 data->done_sigtest = FALSE;
68 data->is_signed = FALSE;
69 data->sigstatus = NULL;
70 gpgme_new(&data->ctx);
75 static void pgpmime_free_privacydata(PrivacyData *_data)
77 PrivacyDataPGP *data = (PrivacyDataPGP *) _data;
78 gpgme_release(data->ctx);
82 static gboolean pgpmime_is_signed(MimeInfo *mimeinfo)
86 const gchar *protocol;
87 PrivacyDataPGP *data = NULL;
89 g_return_val_if_fail(mimeinfo != NULL, FALSE);
90 if (mimeinfo->privacy != NULL) {
91 data = (PrivacyDataPGP *) mimeinfo->privacy;
92 if (data->done_sigtest)
93 return data->is_signed;
97 parent = procmime_mimeinfo_parent(mimeinfo);
100 if ((parent->type != MIMETYPE_MULTIPART) ||
101 g_ascii_strcasecmp(parent->subtype, "signed"))
103 protocol = procmime_mimeinfo_get_parameter(parent, "protocol");
104 if ((protocol == NULL) ||
105 (g_ascii_strcasecmp(protocol, "application/pgp-signature")))
108 /* check if mimeinfo is the first child */
109 if (parent->node->children->data != mimeinfo)
112 /* check signature */
113 signature = parent->node->children->next != NULL ?
114 (MimeInfo *) parent->node->children->next->data : NULL;
115 if (signature == NULL)
117 if ((signature->type != MIMETYPE_APPLICATION) ||
118 (g_ascii_strcasecmp(signature->subtype, "pgp-signature")))
122 data = pgpmime_new_privacydata();
123 mimeinfo->privacy = (PrivacyData *) data;
126 data->done_sigtest = TRUE;
127 data->is_signed = TRUE;
132 static gchar *get_canonical_content(FILE *fp, const gchar *boundary)
139 boundary_len = strlen(boundary);
140 while (fgets(buf, sizeof(buf), fp) != NULL)
141 if (IS_BOUNDARY(buf, boundary, boundary_len))
144 textbuffer = g_string_new("");
145 while (fgets(buf, sizeof(buf), fp) != NULL) {
148 if (IS_BOUNDARY(buf, boundary, boundary_len))
151 buf2 = canonicalize_str(buf);
152 g_string_append(textbuffer, buf2);
155 g_string_truncate(textbuffer, textbuffer->len - 2);
157 ret = textbuffer->str;
158 g_string_free(textbuffer, FALSE);
163 static gint pgpmime_check_signature(MimeInfo *mimeinfo)
165 PrivacyDataPGP *data;
166 MimeInfo *parent, *signature;
170 gpgme_data_t sigdata = NULL, textdata = NULL;
172 g_return_val_if_fail(mimeinfo != NULL, -1);
173 g_return_val_if_fail(mimeinfo->privacy != NULL, -1);
174 data = (PrivacyDataPGP *) mimeinfo->privacy;
175 gpgme_new(&data->ctx);
177 debug_print("Checking PGP/MIME signature\n");
179 err = gpgme_set_protocol(data->ctx, GPGME_PROTOCOL_OpenPGP);
182 debug_print ("gpgme_set_protocol failed: %s\n",
183 gpgme_strerror (err));
185 parent = procmime_mimeinfo_parent(mimeinfo);
187 fp = g_fopen(parent->data.filename, "rb");
188 g_return_val_if_fail(fp != NULL, SIGNATURE_INVALID);
190 boundary = g_hash_table_lookup(parent->typeparameters, "boundary");
192 privacy_set_error(_("Signature boundary not found."));
195 textstr = get_canonical_content(fp, boundary);
197 err = gpgme_data_new_from_mem(&textdata, textstr, strlen(textstr), 0);
199 debug_print ("gpgme_data_new_from_mem failed: %s\n",
200 gpgme_strerror (err));
202 signature = (MimeInfo *) mimeinfo->node->next->data;
203 sigdata = sgpgme_data_from_mimeinfo(signature);
206 if (signature->encoding_type == ENC_BASE64) {
207 err = gpgme_data_set_encoding (sigdata, GPGME_DATA_ENCODING_BASE64);
211 debug_print ("gpgme_data_set_encoding failed: %s\n",
212 gpgme_strerror (err));
216 sgpgme_verify_signature (data->ctx, sigdata, textdata, NULL);
218 gpgme_data_release(sigdata);
219 gpgme_data_release(textdata);
226 static SignatureStatus pgpmime_get_sig_status(MimeInfo *mimeinfo)
228 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
230 g_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
232 if (data->sigstatus == NULL &&
233 prefs_gpg_get_config()->auto_check_signatures)
234 pgpmime_check_signature(mimeinfo);
236 return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
239 static gchar *pgpmime_get_sig_info_short(MimeInfo *mimeinfo)
241 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
243 g_return_val_if_fail(data != NULL, g_strdup("Error"));
245 if (data->sigstatus == NULL &&
246 prefs_gpg_get_config()->auto_check_signatures)
247 pgpmime_check_signature(mimeinfo);
249 return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
252 static gchar *pgpmime_get_sig_info_full(MimeInfo *mimeinfo)
254 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
256 g_return_val_if_fail(data != NULL, g_strdup("Error"));
258 if (data->sigstatus == NULL &&
259 prefs_gpg_get_config()->auto_check_signatures)
260 pgpmime_check_signature(mimeinfo);
262 return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
265 static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
270 if (mimeinfo->type != MIMETYPE_MULTIPART)
272 if (g_ascii_strcasecmp(mimeinfo->subtype, "encrypted"))
274 tmpstr = procmime_mimeinfo_get_parameter(mimeinfo, "protocol");
275 if ((tmpstr == NULL) || g_ascii_strcasecmp(tmpstr, "application/pgp-encrypted"))
277 if (g_node_n_children(mimeinfo->node) != 2)
280 tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 0)->data;
281 if (tmpinfo->type != MIMETYPE_APPLICATION)
283 if (g_ascii_strcasecmp(tmpinfo->subtype, "pgp-encrypted"))
286 tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
287 if (tmpinfo->type != MIMETYPE_APPLICATION)
289 if (g_ascii_strcasecmp(tmpinfo->subtype, "octet-stream"))
295 static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
297 MimeInfo *encinfo, *decinfo, *parseinfo;
298 gpgme_data_t cipher = NULL, plain = NULL;
302 gpgme_verify_result_t sigstat = NULL;
303 PrivacyDataPGP *data = NULL;
309 if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
310 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
314 g_return_val_if_fail(pgpmime_is_encrypted(mimeinfo), NULL);
316 encinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
318 cipher = sgpgme_data_from_mimeinfo(encinfo);
319 plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
321 gpgme_data_release(cipher);
323 debug_print("plain is null!\n");
328 fname = g_strdup_printf("%s%cplaintext.%08x",
329 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
331 if ((dstfp = g_fopen(fname, "wb")) == NULL) {
332 FILE_OP_ERROR(fname, "fopen");
333 privacy_set_error(_("Couldn't open decrypted file %s"), fname);
335 gpgme_data_release(plain);
337 debug_print("can't open!\n");
341 fprintf(dstfp, "MIME-Version: 1.0\n");
343 chars = gpgme_data_release_and_get_mem(plain, &len);
345 fwrite(chars, len, 1, dstfp);
348 parseinfo = procmime_scan_file(fname);
350 if (parseinfo == NULL) {
352 privacy_set_error(_("Couldn't parse decrypted file."));
355 decinfo = g_node_first_child(parseinfo->node) != NULL ?
356 g_node_first_child(parseinfo->node)->data : NULL;
357 if (decinfo == NULL) {
358 privacy_set_error(_("Couldn't parse decrypted file parts."));
363 g_node_unlink(decinfo->node);
364 procmime_mimeinfo_free_all(parseinfo);
368 if (sigstat != NULL && sigstat->signatures != NULL) {
369 if (decinfo->privacy != NULL) {
370 data = (PrivacyDataPGP *) decinfo->privacy;
372 data = pgpmime_new_privacydata();
373 decinfo->privacy = (PrivacyData *) data;
375 data->done_sigtest = TRUE;
376 data->is_signed = TRUE;
377 data->sigstatus = sigstat;
379 gpgme_release(data->ctx);
387 gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account)
389 MimeInfo *msgcontent, *sigmultipart, *newinfo;
390 gchar *textstr, *micalg;
392 gchar *boundary = NULL;
395 gpgme_data_t gpgtext, gpgsig;
398 struct passphrase_cb_info_s info;
399 gpgme_sign_result_t result = NULL;
404 privacy_set_error(_("Couldn't create temporary file: %s"), strerror(errno));
407 procmime_write_mimeinfo(mimeinfo, fp);
410 /* read temporary file into memory */
411 test_msg = file_read_stream_to_str(fp);
414 memset (&info, 0, sizeof info);
416 /* remove content node from message */
417 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
418 g_node_unlink(msgcontent->node);
420 /* create temporary multipart for content */
421 sigmultipart = procmime_mimeinfo_new();
422 sigmultipart->type = MIMETYPE_MULTIPART;
423 sigmultipart->subtype = g_strdup("signed");
427 boundary = generate_mime_boundary("Sig");
428 } while (strstr(test_msg, boundary) != NULL);
432 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("boundary"),
434 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("protocol"),
435 g_strdup("application/pgp-signature"));
436 g_node_append(sigmultipart->node, msgcontent->node);
437 g_node_append(mimeinfo->node, sigmultipart->node);
439 /* write message content to temporary file */
442 perror("my_tmpfile");
443 privacy_set_error(_("Couldn't create temporary file: %s"), strerror(errno));
446 procmime_write_mimeinfo(sigmultipart, fp);
449 /* read temporary file into memory */
450 textstr = get_canonical_content(fp, boundary);
454 gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
455 gpgme_data_new(&gpgsig);
457 gpgme_set_textmode(ctx, 1);
458 gpgme_set_armor(ctx, 1);
459 gpgme_signers_clear (ctx);
461 if (!sgpgme_setup_signers(ctx, account)) {
466 if (getenv("GPG_AGENT_INFO")) {
467 debug_print("GPG_AGENT_INFO environment defined, running without passphrase callback\n");
470 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
473 err = gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_DETACH);
474 if (err != GPG_ERR_NO_ERROR) {
475 if (err == GPG_ERR_CANCELED) {
476 /* ignore cancelled signing */
477 privacy_reset_error();
478 debug_print("gpgme_op_sign cancelled\n");
480 privacy_set_error(_("Data signing failed, %s"), gpgme_strerror(err));
481 debug_print("gpgme_op_sign error : %x\n", err);
486 result = gpgme_op_sign_result(ctx);
487 if (result && result->signatures) {
488 gpgme_new_signature_t sig = result->signatures;
489 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
490 micalg = g_strdup_printf("PGP-%s", gpgme_hash_algo_name(
491 result->signatures->hash_algo));
493 micalg = g_strdup(gpgme_hash_algo_name(
494 result->signatures->hash_algo));
497 debug_print("valid signature: %s\n", sig->fpr);
500 } else if (result && result->invalid_signers) {
501 gpgme_invalid_key_t invalid = result->invalid_signers;
503 g_warning("invalid signer: %s (%s)", invalid->fpr,
504 gpgme_strerror(invalid->reason));
505 privacy_set_error(_("Data signing failed due to invalid signer: %s"),
506 gpgme_strerror(invalid->reason));
507 invalid = invalid->next;
512 /* can't get result (maybe no signing key?) */
513 debug_print("gpgme_op_sign_result error\n");
514 privacy_set_error(_("Data signing failed, no results."));
520 sigcontent = gpgme_data_release_and_get_mem(gpgsig, &len);
521 gpgme_data_release(gpgtext);
524 if (sigcontent == NULL || len <= 0) {
525 g_warning("gpgme_data_release_and_get_mem failed");
526 privacy_set_error(_("Data signing failed, no contents."));
531 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("micalg"),
534 newinfo = procmime_mimeinfo_new();
535 newinfo->type = MIMETYPE_APPLICATION;
536 newinfo->subtype = g_strdup("pgp-signature");
537 g_hash_table_insert(newinfo->typeparameters, g_strdup("name"),
538 g_strdup("signature.asc"));
539 newinfo->content = MIMECONTENT_MEM;
540 newinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
541 g_hash_table_insert(newinfo->dispositionparameters, g_strdup("filename"),
542 g_strdup("signature.asc"));
543 newinfo->data.mem = g_malloc(len + 1);
544 g_memmove(newinfo->data.mem, sigcontent, len);
545 newinfo->data.mem[len] = '\0';
546 g_node_append(sigmultipart->node, newinfo->node);
552 gchar *pgpmime_get_encrypt_data(GSList *recp_names)
554 return sgpgme_get_encrypt_data(recp_names, GPGME_PROTOCOL_OpenPGP);
557 gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
559 MimeInfo *msgcontent, *encmultipart, *newinfo;
561 gchar *boundary, *enccontent;
564 gpgme_data_t gpgtext = NULL, gpgenc = NULL;
565 gpgme_ctx_t ctx = NULL;
566 gpgme_key_t *kset = NULL;
567 gchar **fprs = g_strsplit(encrypt_data, " ", -1);
571 while (fprs[i] && strlen(fprs[i])) {
575 kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
576 memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
579 while (fprs[i] && strlen(fprs[i])) {
581 err = gpgme_get_key(ctx, fprs[i], &key, 0);
583 debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
584 privacy_set_error(_("Couldn't add GPG key %s, %s"), fprs[i], gpgme_strerror(err));
587 debug_print("found %s at %d\n", fprs[i], i);
592 debug_print("Encrypting message content\n");
594 /* remove content node from message */
595 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
596 g_node_unlink(msgcontent->node);
598 /* create temporary multipart for content */
599 encmultipart = procmime_mimeinfo_new();
600 encmultipart->type = MIMETYPE_MULTIPART;
601 encmultipart->subtype = g_strdup("encrypted");
602 boundary = generate_mime_boundary("Encrypt");
603 g_hash_table_insert(encmultipart->typeparameters, g_strdup("boundary"),
605 g_hash_table_insert(encmultipart->typeparameters, g_strdup("protocol"),
606 g_strdup("application/pgp-encrypted"));
607 g_node_append(encmultipart->node, msgcontent->node);
609 /* write message content to temporary file */
612 privacy_set_error(_("Couldn't create temporary file, %s"), strerror(errno));
615 procmime_write_mimeinfo(encmultipart, fp);
618 /* read temporary file into memory */
619 textstr = get_canonical_content(fp, boundary);
624 gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
625 gpgme_data_new(&gpgenc);
626 gpgme_set_armor(ctx, 1);
627 gpgme_data_rewind(gpgtext);
629 err = gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
632 enccontent = gpgme_data_release_and_get_mem(gpgenc, &len);
633 gpgme_data_release(gpgtext);
636 if (enccontent == NULL || len <= 0) {
637 g_warning("gpgme_data_release_and_get_mem failed");
638 privacy_set_error(_("Encryption failed, %s"), gpgme_strerror(err));
642 /* create encrypted multipart */
643 g_node_unlink(msgcontent->node);
644 procmime_mimeinfo_free_all(msgcontent);
645 g_node_append(mimeinfo->node, encmultipart->node);
647 newinfo = procmime_mimeinfo_new();
648 newinfo->type = MIMETYPE_APPLICATION;
649 newinfo->subtype = g_strdup("pgp-encrypted");
650 newinfo->content = MIMECONTENT_MEM;
651 newinfo->data.mem = g_strdup("Version: 1\n");
652 g_node_append(encmultipart->node, newinfo->node);
654 newinfo = procmime_mimeinfo_new();
655 newinfo->type = MIMETYPE_APPLICATION;
656 newinfo->subtype = g_strdup("octet-stream");
657 newinfo->content = MIMECONTENT_MEM;
658 newinfo->data.mem = g_malloc(len + 1);
659 g_memmove(newinfo->data.mem, enccontent, len);
660 newinfo->data.mem[len] = '\0';
661 g_node_append(encmultipart->node, newinfo->node);
668 static PrivacySystem pgpmime_system = {
670 "PGP MIME", /* name */
672 pgpmime_free_privacydata, /* free_privacydata */
674 pgpmime_is_signed, /* is_signed(MimeInfo *) */
675 pgpmime_check_signature, /* check_signature(MimeInfo *) */
676 pgpmime_get_sig_status, /* get_sig_status(MimeInfo *) */
677 pgpmime_get_sig_info_short, /* get_sig_info_short(MimeInfo *) */
678 pgpmime_get_sig_info_full, /* get_sig_info_full(MimeInfo *) */
680 pgpmime_is_encrypted, /* is_encrypted(MimeInfo *) */
681 pgpmime_decrypt, /* decrypt(MimeInfo *) */
687 pgpmime_get_encrypt_data,
693 privacy_register_system(&pgpmime_system);
698 privacy_unregister_system(&pgpmime_system);
701 struct PluginFeature *plugin_provides(void)
703 static struct PluginFeature features[] =
704 { {PLUGIN_PRIVACY, N_("PGP/Mime")},
705 {PLUGIN_NOTHING, NULL}};
708 #endif /* USE_GPGME */