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.
36 #include <plugins/pgpcore/sgpgme.h>
37 #include <plugins/pgpcore/prefs_gpg.h>
38 #include <plugins/pgpcore/passphrase.h>
40 #include "prefs_common.h"
42 typedef struct _PrivacyDataPGP PrivacyDataPGP;
44 struct _PrivacyDataPGP
48 gboolean done_sigtest;
50 gpgme_verify_result_t sigstatus;
54 static PrivacySystem pgpmime_system;
56 static gint pgpmime_check_signature(MimeInfo *mimeinfo);
58 static PrivacyDataPGP *pgpmime_new_privacydata()
62 data = g_new0(PrivacyDataPGP, 1);
63 data->data.system = &pgpmime_system;
64 data->done_sigtest = FALSE;
65 data->is_signed = FALSE;
66 data->sigstatus = NULL;
67 gpgme_new(&data->ctx);
72 static void pgpmime_free_privacydata(PrivacyData *_data)
74 PrivacyDataPGP *data = (PrivacyDataPGP *) _data;
75 gpgme_release(data->ctx);
79 static gboolean pgpmime_is_signed(MimeInfo *mimeinfo)
83 const gchar *protocol;
84 PrivacyDataPGP *data = NULL;
86 g_return_val_if_fail(mimeinfo != NULL, FALSE);
87 if (mimeinfo->privacy != NULL) {
88 data = (PrivacyDataPGP *) mimeinfo->privacy;
89 if (data->done_sigtest)
90 return data->is_signed;
94 parent = procmime_mimeinfo_parent(mimeinfo);
97 if ((parent->type != MIMETYPE_MULTIPART) ||
98 g_ascii_strcasecmp(parent->subtype, "signed"))
100 protocol = procmime_mimeinfo_get_parameter(parent, "protocol");
101 if ((protocol == NULL) ||
102 (g_ascii_strcasecmp(protocol, "application/pgp-signature")))
105 /* check if mimeinfo is the first child */
106 if (parent->node->children->data != mimeinfo)
109 /* check signature */
110 signature = parent->node->children->next != NULL ?
111 (MimeInfo *) parent->node->children->next->data : NULL;
112 if (signature == NULL)
114 if ((signature->type != MIMETYPE_APPLICATION) ||
115 (g_ascii_strcasecmp(signature->subtype, "pgp-signature")))
119 data = pgpmime_new_privacydata();
120 mimeinfo->privacy = (PrivacyData *) data;
123 data->done_sigtest = TRUE;
124 data->is_signed = TRUE;
129 static gchar *get_canonical_content(FILE *fp, const gchar *boundary)
136 boundary_len = strlen(boundary);
137 while (fgets(buf, sizeof(buf), fp) != NULL)
138 if (IS_BOUNDARY(buf, boundary, boundary_len))
141 textbuffer = g_string_new("");
142 while (fgets(buf, sizeof(buf), fp) != NULL) {
145 if (IS_BOUNDARY(buf, boundary, boundary_len))
148 buf2 = canonicalize_str(buf);
149 g_string_append(textbuffer, buf2);
152 g_string_truncate(textbuffer, textbuffer->len - 2);
154 ret = textbuffer->str;
155 g_string_free(textbuffer, FALSE);
160 static gint pgpmime_check_signature(MimeInfo *mimeinfo)
162 PrivacyDataPGP *data;
163 MimeInfo *parent, *signature;
167 gpgme_data_t sigdata = NULL, textdata = NULL;
169 g_return_val_if_fail(mimeinfo != NULL, -1);
170 g_return_val_if_fail(mimeinfo->privacy != NULL, -1);
171 data = (PrivacyDataPGP *) mimeinfo->privacy;
172 gpgme_new(&data->ctx);
174 debug_print("Checking PGP/MIME signature\n");
176 err = gpgme_set_protocol(data->ctx, GPGME_PROTOCOL_OpenPGP);
179 debug_print ("gpgme_set_protocol failed: %s\n",
180 gpgme_strerror (err));
182 parent = procmime_mimeinfo_parent(mimeinfo);
184 fp = g_fopen(parent->data.filename, "rb");
185 g_return_val_if_fail(fp != NULL, SIGNATURE_INVALID);
187 boundary = g_hash_table_lookup(parent->typeparameters, "boundary");
191 textstr = get_canonical_content(fp, boundary);
193 err = gpgme_data_new_from_mem(&textdata, textstr, strlen(textstr), 0);
195 debug_print ("gpgme_data_new_from_mem failed: %s\n",
196 gpgme_strerror (err));
198 signature = (MimeInfo *) mimeinfo->node->next->data;
199 sigdata = sgpgme_data_from_mimeinfo(signature);
202 if (signature->encoding_type == ENC_BASE64) {
203 err = gpgme_data_set_encoding (sigdata, GPGME_DATA_ENCODING_BASE64);
207 debug_print ("gpgme_data_set_encoding failed: %s\n",
208 gpgme_strerror (err));
212 sgpgme_verify_signature (data->ctx, sigdata, textdata, NULL);
214 gpgme_data_release(sigdata);
215 gpgme_data_release(textdata);
222 static SignatureStatus pgpmime_get_sig_status(MimeInfo *mimeinfo)
224 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
226 g_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
228 if (data->sigstatus == NULL &&
229 prefs_gpg_get_config()->auto_check_signatures)
230 pgpmime_check_signature(mimeinfo);
232 return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
235 static gchar *pgpmime_get_sig_info_short(MimeInfo *mimeinfo)
237 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
239 g_return_val_if_fail(data != NULL, g_strdup("Error"));
241 if (data->sigstatus == NULL &&
242 prefs_gpg_get_config()->auto_check_signatures)
243 pgpmime_check_signature(mimeinfo);
245 return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
248 static gchar *pgpmime_get_sig_info_full(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_full(data->ctx, data->sigstatus);
261 static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
266 if (mimeinfo->type != MIMETYPE_MULTIPART)
268 if (g_ascii_strcasecmp(mimeinfo->subtype, "encrypted"))
270 tmpstr = procmime_mimeinfo_get_parameter(mimeinfo, "protocol");
271 if ((tmpstr == NULL) || g_ascii_strcasecmp(tmpstr, "application/pgp-encrypted"))
273 if (g_node_n_children(mimeinfo->node) != 2)
276 tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 0)->data;
277 if (tmpinfo->type != MIMETYPE_APPLICATION)
279 if (g_ascii_strcasecmp(tmpinfo->subtype, "pgp-encrypted"))
282 tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
283 if (tmpinfo->type != MIMETYPE_APPLICATION)
285 if (g_ascii_strcasecmp(tmpinfo->subtype, "octet-stream"))
291 static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
293 MimeInfo *encinfo, *decinfo, *parseinfo;
294 gpgme_data_t cipher = NULL, plain = NULL;
298 gpgme_verify_result_t sigstat = NULL;
299 PrivacyDataPGP *data = NULL;
304 if (gpgme_new(&ctx) != GPG_ERR_NO_ERROR)
308 g_return_val_if_fail(pgpmime_is_encrypted(mimeinfo), NULL);
310 encinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
312 cipher = sgpgme_data_from_mimeinfo(encinfo);
313 plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
315 gpgme_data_release(cipher);
317 debug_print("plain is null!\n");
322 fname = g_strdup_printf("%s%cplaintext.%08x",
323 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
325 if ((dstfp = g_fopen(fname, "wb")) == NULL) {
326 FILE_OP_ERROR(fname, "fopen");
328 gpgme_data_release(plain);
330 debug_print("can't open!\n");
334 fprintf(dstfp, "MIME-Version: 1.0\n");
336 chars = gpgme_data_release_and_get_mem(plain, &len);
338 fwrite(chars, len, 1, dstfp);
341 parseinfo = procmime_scan_file(fname);
343 if (parseinfo == NULL) {
347 decinfo = g_node_first_child(parseinfo->node) != NULL ?
348 g_node_first_child(parseinfo->node)->data : NULL;
349 if (decinfo == NULL) {
354 g_node_unlink(decinfo->node);
355 procmime_mimeinfo_free_all(parseinfo);
359 if (sigstat != NULL && sigstat->signatures != NULL) {
360 if (decinfo->privacy != NULL) {
361 data = (PrivacyDataPGP *) decinfo->privacy;
363 data = pgpmime_new_privacydata();
364 decinfo->privacy = (PrivacyData *) data;
366 data->done_sigtest = TRUE;
367 data->is_signed = TRUE;
368 data->sigstatus = sigstat;
370 gpgme_release(data->ctx);
378 gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account)
380 MimeInfo *msgcontent, *sigmultipart, *newinfo;
381 gchar *textstr, *micalg;
383 gchar *boundary = NULL;
386 gpgme_data_t gpgtext, gpgsig;
389 struct passphrase_cb_info_s info;
390 gpgme_sign_result_t result = NULL;
395 perror("my_tmpfile");
398 procmime_write_mimeinfo(mimeinfo, fp);
401 /* read temporary file into memory */
402 test_msg = file_read_stream_to_str(fp);
405 memset (&info, 0, sizeof info);
407 /* remove content node from message */
408 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
409 g_node_unlink(msgcontent->node);
411 /* create temporary multipart for content */
412 sigmultipart = procmime_mimeinfo_new();
413 sigmultipart->type = MIMETYPE_MULTIPART;
414 sigmultipart->subtype = g_strdup("signed");
419 boundary = generate_mime_boundary("Sig");
420 } while (strstr(test_msg, boundary) != NULL);
424 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("boundary"),
426 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("protocol"),
427 g_strdup("application/pgp-signature"));
428 g_node_append(sigmultipart->node, msgcontent->node);
429 g_node_append(mimeinfo->node, sigmultipart->node);
431 /* write message content to temporary file */
434 perror("my_tmpfile");
437 procmime_write_mimeinfo(sigmultipart, fp);
440 /* read temporary file into memory */
441 textstr = get_canonical_content(fp, boundary);
445 gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
446 gpgme_data_new(&gpgsig);
448 gpgme_set_textmode(ctx, 1);
449 gpgme_set_armor(ctx, 1);
450 gpgme_signers_clear (ctx);
452 if (!sgpgme_setup_signers(ctx, account)) {
457 if (getenv("GPG_AGENT_INFO")) {
458 debug_print("GPG_AGENT_INFO environment defined, running without passphrase callback\n");
461 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
464 err = gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_DETACH);
465 if (err != GPG_ERR_NO_ERROR) {
466 debug_print("gpgme_op_sign error : %x\n", err);
470 result = gpgme_op_sign_result(ctx);
471 if (result && result->signatures) {
472 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
473 micalg = g_strdup_printf("PGP-%s", gpgme_hash_algo_name(
474 result->signatures->hash_algo));
476 micalg = g_strdup(gpgme_hash_algo_name(
477 result->signatures->hash_algo));
480 /* can't get result (maybe no signing key?) */
481 debug_print("gpgme_op_sign_result error\n");
486 sigcontent = gpgme_data_release_and_get_mem(gpgsig, &len);
487 gpgme_data_release(gpgtext);
491 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("micalg"),
494 newinfo = procmime_mimeinfo_new();
495 newinfo->type = MIMETYPE_APPLICATION;
496 newinfo->subtype = g_strdup("pgp-signature");
497 g_hash_table_insert(newinfo->typeparameters, g_strdup("name"),
498 g_strdup("signature.asc"));
499 newinfo->content = MIMECONTENT_MEM;
500 newinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
501 g_hash_table_insert(newinfo->dispositionparameters, g_strdup("filename"),
502 g_strdup("signature.asc"));
503 newinfo->data.mem = g_malloc(len + 1);
504 g_memmove(newinfo->data.mem, sigcontent, len);
505 newinfo->data.mem[len] = '\0';
506 g_node_append(sigmultipart->node, newinfo->node);
512 gchar *pgpmime_get_encrypt_data(GSList *recp_names)
514 return sgpgme_get_encrypt_data(recp_names, GPGME_PROTOCOL_OpenPGP);
517 gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
519 MimeInfo *msgcontent, *encmultipart, *newinfo;
521 gchar *boundary, *enccontent;
524 gpgme_data_t gpgtext = NULL, gpgenc = NULL;
525 gpgme_ctx_t ctx = NULL;
526 gpgme_key_t *kset = NULL;
527 gchar **fprs = g_strsplit(encrypt_data, " ", -1);
529 while (fprs[i] && strlen(fprs[i])) {
533 kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
534 memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
537 while (fprs[i] && strlen(fprs[i])) {
540 err = gpgme_get_key(ctx, fprs[i], &key, 0);
542 debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
545 debug_print("found %s at %d\n", fprs[i], i);
550 debug_print("Encrypting message content\n");
552 /* remove content node from message */
553 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
554 g_node_unlink(msgcontent->node);
556 /* create temporary multipart for content */
557 encmultipart = procmime_mimeinfo_new();
558 encmultipart->type = MIMETYPE_MULTIPART;
559 encmultipart->subtype = g_strdup("encrypted");
560 boundary = generate_mime_boundary("Encrypt");
561 g_hash_table_insert(encmultipart->typeparameters, g_strdup("boundary"),
563 g_hash_table_insert(encmultipart->typeparameters, g_strdup("protocol"),
564 g_strdup("application/pgp-encrypted"));
565 g_node_append(encmultipart->node, msgcontent->node);
567 /* write message content to temporary file */
570 perror("my_tmpfile");
573 procmime_write_mimeinfo(encmultipart, fp);
576 /* read temporary file into memory */
577 textstr = get_canonical_content(fp, boundary);
582 gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
583 gpgme_data_new(&gpgenc);
584 gpgme_set_armor(ctx, 1);
585 gpgme_data_rewind(gpgtext);
587 gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
590 enccontent = gpgme_data_release_and_get_mem(gpgenc, &len);
591 gpgme_data_release(gpgtext);
594 /* create encrypted multipart */
595 g_node_unlink(msgcontent->node);
596 procmime_mimeinfo_free_all(msgcontent);
597 g_node_append(mimeinfo->node, encmultipart->node);
599 newinfo = procmime_mimeinfo_new();
600 newinfo->type = MIMETYPE_APPLICATION;
601 newinfo->subtype = g_strdup("pgp-encrypted");
602 newinfo->content = MIMECONTENT_MEM;
603 newinfo->data.mem = g_strdup("Version: 1\n");
604 g_node_append(encmultipart->node, newinfo->node);
606 newinfo = procmime_mimeinfo_new();
607 newinfo->type = MIMETYPE_APPLICATION;
608 newinfo->subtype = g_strdup("octet-stream");
609 newinfo->content = MIMECONTENT_MEM;
610 newinfo->data.mem = g_malloc(len + 1);
611 g_memmove(newinfo->data.mem, enccontent, len);
612 newinfo->data.mem[len] = '\0';
613 g_node_append(encmultipart->node, newinfo->node);
620 static PrivacySystem pgpmime_system = {
622 "PGP MIME", /* name */
624 pgpmime_free_privacydata, /* free_privacydata */
626 pgpmime_is_signed, /* is_signed(MimeInfo *) */
627 pgpmime_check_signature, /* check_signature(MimeInfo *) */
628 pgpmime_get_sig_status, /* get_sig_status(MimeInfo *) */
629 pgpmime_get_sig_info_short, /* get_sig_info_short(MimeInfo *) */
630 pgpmime_get_sig_info_full, /* get_sig_info_full(MimeInfo *) */
632 pgpmime_is_encrypted, /* is_encrypted(MimeInfo *) */
633 pgpmime_decrypt, /* decrypt(MimeInfo *) */
639 pgpmime_get_encrypt_data,
645 privacy_register_system(&pgpmime_system);
650 privacy_unregister_system(&pgpmime_system);
653 #endif /* USE_GPGME */