2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto & the Sylpheed-Claws 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>
38 #include <plugins/pgpcore/sgpgme.h>
39 #include <plugins/pgpcore/prefs_gpg.h>
40 #include <plugins/pgpcore/passphrase.h>
42 #include "prefs_common.h"
44 typedef struct _PrivacyDataPGP PrivacyDataPGP;
46 struct _PrivacyDataPGP
50 gboolean done_sigtest;
52 gpgme_verify_result_t sigstatus;
56 static PrivacySystem pgpmime_system;
58 static gint pgpmime_check_signature(MimeInfo *mimeinfo);
60 static PrivacyDataPGP *pgpmime_new_privacydata()
64 data = g_new0(PrivacyDataPGP, 1);
65 data->data.system = &pgpmime_system;
66 data->done_sigtest = FALSE;
67 data->is_signed = FALSE;
68 data->sigstatus = NULL;
69 gpgme_new(&data->ctx);
74 static void pgpmime_free_privacydata(PrivacyData *_data)
76 PrivacyDataPGP *data = (PrivacyDataPGP *) _data;
77 gpgme_release(data->ctx);
81 static gboolean pgpmime_is_signed(MimeInfo *mimeinfo)
85 const gchar *protocol;
86 PrivacyDataPGP *data = NULL;
88 g_return_val_if_fail(mimeinfo != NULL, FALSE);
89 if (mimeinfo->privacy != NULL) {
90 data = (PrivacyDataPGP *) mimeinfo->privacy;
91 if (data->done_sigtest)
92 return data->is_signed;
96 parent = procmime_mimeinfo_parent(mimeinfo);
99 if ((parent->type != MIMETYPE_MULTIPART) ||
100 g_ascii_strcasecmp(parent->subtype, "signed"))
102 protocol = procmime_mimeinfo_get_parameter(parent, "protocol");
103 if ((protocol == NULL) ||
104 (g_ascii_strcasecmp(protocol, "application/pgp-signature")))
107 /* check if mimeinfo is the first child */
108 if (parent->node->children->data != mimeinfo)
111 /* check signature */
112 signature = parent->node->children->next != NULL ?
113 (MimeInfo *) parent->node->children->next->data : NULL;
114 if (signature == NULL)
116 if ((signature->type != MIMETYPE_APPLICATION) ||
117 (g_ascii_strcasecmp(signature->subtype, "pgp-signature")))
121 data = pgpmime_new_privacydata();
122 mimeinfo->privacy = (PrivacyData *) data;
125 data->done_sigtest = TRUE;
126 data->is_signed = TRUE;
131 static gchar *get_canonical_content(FILE *fp, const gchar *boundary)
138 boundary_len = strlen(boundary);
139 while (fgets(buf, sizeof(buf), fp) != NULL)
140 if (IS_BOUNDARY(buf, boundary, boundary_len))
143 textbuffer = g_string_new("");
144 while (fgets(buf, sizeof(buf), fp) != NULL) {
147 if (IS_BOUNDARY(buf, boundary, boundary_len))
150 buf2 = canonicalize_str(buf);
151 g_string_append(textbuffer, buf2);
154 g_string_truncate(textbuffer, textbuffer->len - 2);
156 ret = textbuffer->str;
157 g_string_free(textbuffer, FALSE);
162 static gint pgpmime_check_signature(MimeInfo *mimeinfo)
164 PrivacyDataPGP *data;
165 MimeInfo *parent, *signature;
169 gpgme_data_t sigdata = NULL, textdata = NULL;
171 g_return_val_if_fail(mimeinfo != NULL, -1);
172 g_return_val_if_fail(mimeinfo->privacy != NULL, -1);
173 data = (PrivacyDataPGP *) mimeinfo->privacy;
174 gpgme_new(&data->ctx);
176 debug_print("Checking PGP/MIME signature\n");
178 err = gpgme_set_protocol(data->ctx, GPGME_PROTOCOL_OpenPGP);
181 debug_print ("gpgme_set_protocol failed: %s\n",
182 gpgme_strerror (err));
184 parent = procmime_mimeinfo_parent(mimeinfo);
186 fp = g_fopen(parent->data.filename, "rb");
187 g_return_val_if_fail(fp != NULL, SIGNATURE_INVALID);
189 boundary = g_hash_table_lookup(parent->typeparameters, "boundary");
191 privacy_set_error(_("Signature boundary not found."));
194 textstr = get_canonical_content(fp, boundary);
196 err = gpgme_data_new_from_mem(&textdata, textstr, strlen(textstr), 0);
198 debug_print ("gpgme_data_new_from_mem failed: %s\n",
199 gpgme_strerror (err));
201 signature = (MimeInfo *) mimeinfo->node->next->data;
202 sigdata = sgpgme_data_from_mimeinfo(signature);
205 if (signature->encoding_type == ENC_BASE64) {
206 err = gpgme_data_set_encoding (sigdata, GPGME_DATA_ENCODING_BASE64);
210 debug_print ("gpgme_data_set_encoding failed: %s\n",
211 gpgme_strerror (err));
215 sgpgme_verify_signature (data->ctx, sigdata, textdata, NULL);
217 gpgme_data_release(sigdata);
218 gpgme_data_release(textdata);
225 static SignatureStatus pgpmime_get_sig_status(MimeInfo *mimeinfo)
227 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
229 g_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
231 if (data->sigstatus == NULL &&
232 prefs_gpg_get_config()->auto_check_signatures)
233 pgpmime_check_signature(mimeinfo);
235 return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
238 static gchar *pgpmime_get_sig_info_short(MimeInfo *mimeinfo)
240 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
242 g_return_val_if_fail(data != NULL, g_strdup("Error"));
244 if (data->sigstatus == NULL &&
245 prefs_gpg_get_config()->auto_check_signatures)
246 pgpmime_check_signature(mimeinfo);
248 return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
251 static gchar *pgpmime_get_sig_info_full(MimeInfo *mimeinfo)
253 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
255 g_return_val_if_fail(data != NULL, g_strdup("Error"));
257 if (data->sigstatus == NULL &&
258 prefs_gpg_get_config()->auto_check_signatures)
259 pgpmime_check_signature(mimeinfo);
261 return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
264 static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
269 if (mimeinfo->type != MIMETYPE_MULTIPART)
271 if (g_ascii_strcasecmp(mimeinfo->subtype, "encrypted"))
273 tmpstr = procmime_mimeinfo_get_parameter(mimeinfo, "protocol");
274 if ((tmpstr == NULL) || g_ascii_strcasecmp(tmpstr, "application/pgp-encrypted"))
276 if (g_node_n_children(mimeinfo->node) != 2)
279 tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 0)->data;
280 if (tmpinfo->type != MIMETYPE_APPLICATION)
282 if (g_ascii_strcasecmp(tmpinfo->subtype, "pgp-encrypted"))
285 tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
286 if (tmpinfo->type != MIMETYPE_APPLICATION)
288 if (g_ascii_strcasecmp(tmpinfo->subtype, "octet-stream"))
294 static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
296 MimeInfo *encinfo, *decinfo, *parseinfo;
297 gpgme_data_t cipher = NULL, plain = NULL;
301 gpgme_verify_result_t sigstat = NULL;
302 PrivacyDataPGP *data = NULL;
308 if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
309 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
313 g_return_val_if_fail(pgpmime_is_encrypted(mimeinfo), NULL);
315 encinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
317 cipher = sgpgme_data_from_mimeinfo(encinfo);
318 plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
320 gpgme_data_release(cipher);
322 debug_print("plain is null!\n");
327 fname = g_strdup_printf("%s%cplaintext.%08x",
328 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
330 if ((dstfp = g_fopen(fname, "wb")) == NULL) {
331 FILE_OP_ERROR(fname, "fopen");
332 privacy_set_error(_("Couldn't open decrypted file %s"), fname);
334 gpgme_data_release(plain);
336 debug_print("can't open!\n");
340 fprintf(dstfp, "MIME-Version: 1.0\n");
342 chars = gpgme_data_release_and_get_mem(plain, &len);
344 fwrite(chars, len, 1, dstfp);
347 parseinfo = procmime_scan_file(fname);
349 if (parseinfo == NULL) {
351 privacy_set_error(_("Couldn't parse decrypted file."));
354 decinfo = g_node_first_child(parseinfo->node) != NULL ?
355 g_node_first_child(parseinfo->node)->data : NULL;
356 if (decinfo == NULL) {
357 privacy_set_error(_("Couldn't parse decrypted file parts."));
362 g_node_unlink(decinfo->node);
363 procmime_mimeinfo_free_all(parseinfo);
367 if (sigstat != NULL && sigstat->signatures != NULL) {
368 if (decinfo->privacy != NULL) {
369 data = (PrivacyDataPGP *) decinfo->privacy;
371 data = pgpmime_new_privacydata();
372 decinfo->privacy = (PrivacyData *) data;
374 data->done_sigtest = TRUE;
375 data->is_signed = TRUE;
376 data->sigstatus = sigstat;
378 gpgme_release(data->ctx);
386 gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account)
388 MimeInfo *msgcontent, *sigmultipart, *newinfo;
389 gchar *textstr, *micalg;
391 gchar *boundary = NULL;
394 gpgme_data_t gpgtext, gpgsig;
397 struct passphrase_cb_info_s info;
398 gpgme_sign_result_t result = NULL;
403 privacy_set_error(_("Couldn't create temporary file: %s"), strerror(errno));
406 procmime_write_mimeinfo(mimeinfo, fp);
409 /* read temporary file into memory */
410 test_msg = file_read_stream_to_str(fp);
413 memset (&info, 0, sizeof info);
415 /* remove content node from message */
416 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
417 g_node_unlink(msgcontent->node);
419 /* create temporary multipart for content */
420 sigmultipart = procmime_mimeinfo_new();
421 sigmultipart->type = MIMETYPE_MULTIPART;
422 sigmultipart->subtype = g_strdup("signed");
426 boundary = generate_mime_boundary("Sig");
427 } while (strstr(test_msg, boundary) != NULL);
431 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("boundary"),
433 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("protocol"),
434 g_strdup("application/pgp-signature"));
435 g_node_append(sigmultipart->node, msgcontent->node);
436 g_node_append(mimeinfo->node, sigmultipart->node);
438 /* write message content to temporary file */
441 perror("my_tmpfile");
442 privacy_set_error(_("Couldn't create temporary file: %s"), strerror(errno));
445 procmime_write_mimeinfo(sigmultipart, fp);
448 /* read temporary file into memory */
449 textstr = get_canonical_content(fp, boundary);
453 gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
454 gpgme_data_new(&gpgsig);
456 gpgme_set_textmode(ctx, 1);
457 gpgme_set_armor(ctx, 1);
458 gpgme_signers_clear (ctx);
460 if (!sgpgme_setup_signers(ctx, account)) {
462 privacy_set_error(_("Couldn't find private key."));
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 privacy_set_error(_("Data signing failed, %s"), gpgme_strerror(err));
476 debug_print("gpgme_op_sign error : %x\n", err);
480 result = gpgme_op_sign_result(ctx);
481 if (result && result->signatures) {
482 gpgme_new_signature_t sig = result->signatures;
483 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
484 micalg = g_strdup_printf("PGP-%s", gpgme_hash_algo_name(
485 result->signatures->hash_algo));
487 micalg = g_strdup(gpgme_hash_algo_name(
488 result->signatures->hash_algo));
491 debug_print("valid signature: %s\n", sig->fpr);
494 } else if (result && result->invalid_signers) {
495 gpgme_invalid_key_t invalid = result->invalid_signers;
497 g_warning("invalid signer: %s (%s)", invalid->fpr,
498 gpgme_strerror(invalid->reason));
499 privacy_set_error(_("Data signing failed due to invalid signer: %s"),
500 gpgme_strerror(invalid->reason));
501 invalid = invalid->next;
506 /* can't get result (maybe no signing key?) */
507 debug_print("gpgme_op_sign_result error\n");
508 privacy_set_error(_("Data signing failed, no results."));
514 sigcontent = gpgme_data_release_and_get_mem(gpgsig, &len);
515 gpgme_data_release(gpgtext);
518 if (sigcontent == NULL || len <= 0) {
519 g_warning("gpgme_data_release_and_get_mem failed");
520 privacy_set_error(_("Data signing failed, no contents."));
525 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("micalg"),
528 newinfo = procmime_mimeinfo_new();
529 newinfo->type = MIMETYPE_APPLICATION;
530 newinfo->subtype = g_strdup("pgp-signature");
531 g_hash_table_insert(newinfo->typeparameters, g_strdup("name"),
532 g_strdup("signature.asc"));
533 newinfo->content = MIMECONTENT_MEM;
534 newinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
535 g_hash_table_insert(newinfo->dispositionparameters, g_strdup("filename"),
536 g_strdup("signature.asc"));
537 newinfo->data.mem = g_malloc(len + 1);
538 g_memmove(newinfo->data.mem, sigcontent, len);
539 newinfo->data.mem[len] = '\0';
540 g_node_append(sigmultipart->node, newinfo->node);
546 gchar *pgpmime_get_encrypt_data(GSList *recp_names)
548 return sgpgme_get_encrypt_data(recp_names, GPGME_PROTOCOL_OpenPGP);
551 gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
553 MimeInfo *msgcontent, *encmultipart, *newinfo;
555 gchar *boundary, *enccontent;
558 gpgme_data_t gpgtext = NULL, gpgenc = NULL;
559 gpgme_ctx_t ctx = NULL;
560 gpgme_key_t *kset = NULL;
561 gchar **fprs = g_strsplit(encrypt_data, " ", -1);
565 while (fprs[i] && strlen(fprs[i])) {
569 kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
570 memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
573 while (fprs[i] && strlen(fprs[i])) {
575 err = gpgme_get_key(ctx, fprs[i], &key, 0);
577 debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
578 privacy_set_error(_("Couldn't add GPG key %s, %s"), fprs[i], gpgme_strerror(err));
581 debug_print("found %s at %d\n", fprs[i], i);
586 debug_print("Encrypting message content\n");
588 /* remove content node from message */
589 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
590 g_node_unlink(msgcontent->node);
592 /* create temporary multipart for content */
593 encmultipart = procmime_mimeinfo_new();
594 encmultipart->type = MIMETYPE_MULTIPART;
595 encmultipart->subtype = g_strdup("encrypted");
596 boundary = generate_mime_boundary("Encrypt");
597 g_hash_table_insert(encmultipart->typeparameters, g_strdup("boundary"),
599 g_hash_table_insert(encmultipart->typeparameters, g_strdup("protocol"),
600 g_strdup("application/pgp-encrypted"));
601 g_node_append(encmultipart->node, msgcontent->node);
603 /* write message content to temporary file */
606 privacy_set_error(_("Couldn't create temporary file, %s"), strerror(errno));
609 procmime_write_mimeinfo(encmultipart, fp);
612 /* read temporary file into memory */
613 textstr = get_canonical_content(fp, boundary);
618 gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
619 gpgme_data_new(&gpgenc);
620 gpgme_set_armor(ctx, 1);
621 gpgme_data_rewind(gpgtext);
623 err = gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
626 enccontent = gpgme_data_release_and_get_mem(gpgenc, &len);
627 gpgme_data_release(gpgtext);
630 if (enccontent == NULL || len <= 0) {
631 g_warning("gpgme_data_release_and_get_mem failed");
632 privacy_set_error(_("Encryption failed, %s"), gpgme_strerror(err));
636 /* create encrypted multipart */
637 g_node_unlink(msgcontent->node);
638 procmime_mimeinfo_free_all(msgcontent);
639 g_node_append(mimeinfo->node, encmultipart->node);
641 newinfo = procmime_mimeinfo_new();
642 newinfo->type = MIMETYPE_APPLICATION;
643 newinfo->subtype = g_strdup("pgp-encrypted");
644 newinfo->content = MIMECONTENT_MEM;
645 newinfo->data.mem = g_strdup("Version: 1\n");
646 g_node_append(encmultipart->node, newinfo->node);
648 newinfo = procmime_mimeinfo_new();
649 newinfo->type = MIMETYPE_APPLICATION;
650 newinfo->subtype = g_strdup("octet-stream");
651 newinfo->content = MIMECONTENT_MEM;
652 newinfo->data.mem = g_malloc(len + 1);
653 g_memmove(newinfo->data.mem, enccontent, len);
654 newinfo->data.mem[len] = '\0';
655 g_node_append(encmultipart->node, newinfo->node);
662 static PrivacySystem pgpmime_system = {
664 "PGP MIME", /* name */
666 pgpmime_free_privacydata, /* free_privacydata */
668 pgpmime_is_signed, /* is_signed(MimeInfo *) */
669 pgpmime_check_signature, /* check_signature(MimeInfo *) */
670 pgpmime_get_sig_status, /* get_sig_status(MimeInfo *) */
671 pgpmime_get_sig_info_short, /* get_sig_info_short(MimeInfo *) */
672 pgpmime_get_sig_info_full, /* get_sig_info_full(MimeInfo *) */
674 pgpmime_is_encrypted, /* is_encrypted(MimeInfo *) */
675 pgpmime_decrypt, /* decrypt(MimeInfo *) */
681 pgpmime_get_encrypt_data,
687 privacy_register_system(&pgpmime_system);
692 privacy_unregister_system(&pgpmime_system);
695 #endif /* USE_GPGME */