2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 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;
55 static PrivacySystem pgpmime_system;
57 static gint pgpmime_check_signature(MimeInfo *mimeinfo);
59 static PrivacyDataPGP *pgpmime_new_privacydata()
63 data = g_new0(PrivacyDataPGP, 1);
64 data->data.system = &pgpmime_system;
65 data->done_sigtest = FALSE;
66 data->is_signed = FALSE;
67 data->sigstatus = NULL;
68 gpgme_new(&data->ctx);
73 static void pgpmime_free_privacydata(PrivacyData *_data)
75 PrivacyDataPGP *data = (PrivacyDataPGP *) _data;
76 gpgme_release(data->ctx);
80 static gboolean pgpmime_is_signed(MimeInfo *mimeinfo)
84 const gchar *protocol;
85 PrivacyDataPGP *data = NULL;
87 g_return_val_if_fail(mimeinfo != NULL, FALSE);
88 if (mimeinfo->privacy != NULL) {
89 data = (PrivacyDataPGP *) mimeinfo->privacy;
90 if (data->done_sigtest)
91 return data->is_signed;
95 parent = procmime_mimeinfo_parent(mimeinfo);
98 if ((parent->type != MIMETYPE_MULTIPART) ||
99 g_ascii_strcasecmp(parent->subtype, "signed"))
101 protocol = procmime_mimeinfo_get_parameter(parent, "protocol");
102 if ((protocol == NULL) ||
103 (g_ascii_strcasecmp(protocol, "application/pgp-signature") &&
104 g_ascii_strcasecmp(protocol, "application/pkcs7-signature") &&
105 g_ascii_strcasecmp(protocol, "application/x-pkcs7-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") &&
119 g_ascii_strcasecmp(signature->subtype, "pkcs7-signature") &&
120 g_ascii_strcasecmp(signature->subtype, "x-pkcs7-signature")))
124 data = pgpmime_new_privacydata();
125 mimeinfo->privacy = (PrivacyData *) data;
128 if (!g_ascii_strcasecmp(signature->subtype, "pkcs7-signature") ||
129 !g_ascii_strcasecmp(signature->subtype, "x-pkcs7-signature"))
130 data->is_pkcs7 = TRUE;
132 data->is_pkcs7 = FALSE;
134 data->done_sigtest = TRUE;
135 data->is_signed = TRUE;
140 static gchar *get_canonical_content(FILE *fp, const gchar *boundary)
147 boundary_len = strlen(boundary);
148 while (fgets(buf, sizeof(buf), fp) != NULL)
149 if (IS_BOUNDARY(buf, boundary, boundary_len))
152 textbuffer = g_string_new("");
153 while (fgets(buf, sizeof(buf), fp) != NULL) {
156 if (IS_BOUNDARY(buf, boundary, boundary_len))
159 buf2 = canonicalize_str(buf);
160 g_string_append(textbuffer, buf2);
163 g_string_truncate(textbuffer, textbuffer->len - 2);
165 ret = textbuffer->str;
166 g_string_free(textbuffer, FALSE);
171 static gint pgpmime_check_signature(MimeInfo *mimeinfo)
173 PrivacyDataPGP *data;
174 MimeInfo *parent, *signature;
178 gpgme_data_t sigdata = NULL, textdata = NULL;
180 g_return_val_if_fail(mimeinfo != NULL, -1);
181 g_return_val_if_fail(mimeinfo->privacy != NULL, -1);
182 data = (PrivacyDataPGP *) mimeinfo->privacy;
183 gpgme_new(&data->ctx);
185 debug_print("Checking %s/MIME signature\n", data->is_pkcs7?"S":"PGP");
187 if (data->is_pkcs7) {
188 err = gpgme_set_protocol(data->ctx, GPGME_PROTOCOL_CMS);
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 g_return_val_if_fail(fp != NULL, SIGNATURE_INVALID);
201 boundary = g_hash_table_lookup(parent->typeparameters, "boundary");
205 textstr = get_canonical_content(fp, boundary);
207 err = gpgme_data_new_from_mem(&textdata, textstr, strlen(textstr), 0);
209 debug_print ("gpgme_data_new_from_mem failed: %s\n",
210 gpgme_strerror (err));
212 signature = (MimeInfo *) mimeinfo->node->next->data;
213 sigdata = sgpgme_data_from_mimeinfo(signature);
216 if (signature->encoding_type == ENC_BASE64) {
217 err = gpgme_data_set_encoding (sigdata, GPGME_DATA_ENCODING_BASE64);
221 debug_print ("gpgme_data_set_encoding failed: %s\n",
222 gpgme_strerror (err));
226 sgpgme_verify_signature (data->ctx, sigdata, textdata, NULL);
228 gpgme_data_release(sigdata);
229 gpgme_data_release(textdata);
236 static SignatureStatus pgpmime_get_sig_status(MimeInfo *mimeinfo)
238 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
240 g_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
242 if (data->sigstatus == NULL &&
243 prefs_gpg_get_config()->auto_check_signatures)
244 pgpmime_check_signature(mimeinfo);
246 return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
249 static gchar *pgpmime_get_sig_info_short(MimeInfo *mimeinfo)
251 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
253 g_return_val_if_fail(data != NULL, g_strdup("Error"));
255 if (data->sigstatus == NULL &&
256 prefs_gpg_get_config()->auto_check_signatures)
257 pgpmime_check_signature(mimeinfo);
259 return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
262 static gchar *pgpmime_get_sig_info_full(MimeInfo *mimeinfo)
264 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
266 g_return_val_if_fail(data != NULL, g_strdup("Error"));
268 if (data->sigstatus == NULL &&
269 prefs_gpg_get_config()->auto_check_signatures)
270 pgpmime_check_signature(mimeinfo);
272 return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
275 static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
280 if (mimeinfo->type != MIMETYPE_MULTIPART)
282 if (g_ascii_strcasecmp(mimeinfo->subtype, "encrypted"))
284 tmpstr = procmime_mimeinfo_get_parameter(mimeinfo, "protocol");
285 if ((tmpstr == NULL) || g_ascii_strcasecmp(tmpstr, "application/pgp-encrypted"))
287 if (g_node_n_children(mimeinfo->node) != 2)
290 tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 0)->data;
291 if (tmpinfo->type != MIMETYPE_APPLICATION)
293 if (g_ascii_strcasecmp(tmpinfo->subtype, "pgp-encrypted"))
296 tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
297 if (tmpinfo->type != MIMETYPE_APPLICATION)
299 if (g_ascii_strcasecmp(tmpinfo->subtype, "octet-stream"))
305 static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
307 MimeInfo *encinfo, *decinfo, *parseinfo;
308 gpgme_data_t cipher = NULL, plain = NULL;
312 gpgme_verify_result_t sigstat = NULL;
313 PrivacyDataPGP *data = NULL;
318 if (gpgme_new(&ctx) != GPG_ERR_NO_ERROR)
322 g_return_val_if_fail(pgpmime_is_encrypted(mimeinfo), NULL);
324 encinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
326 cipher = sgpgme_data_from_mimeinfo(encinfo);
327 plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
329 gpgme_data_release(cipher);
331 debug_print("plain is null!\n");
336 fname = g_strdup_printf("%s%cplaintext.%08x",
337 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
339 if ((dstfp = g_fopen(fname, "wb")) == NULL) {
340 FILE_OP_ERROR(fname, "fopen");
342 gpgme_data_release(plain);
344 debug_print("can't open!\n");
348 fprintf(dstfp, "MIME-Version: 1.0\n");
350 chars = gpgme_data_release_and_get_mem(plain, &len);
352 fwrite(chars, len, 1, dstfp);
355 parseinfo = procmime_scan_file(fname);
357 if (parseinfo == NULL) {
361 decinfo = g_node_first_child(parseinfo->node) != NULL ?
362 g_node_first_child(parseinfo->node)->data : NULL;
363 if (decinfo == NULL) {
368 g_node_unlink(decinfo->node);
369 procmime_mimeinfo_free_all(parseinfo);
373 if (sigstat != NULL && sigstat->signatures != NULL) {
374 if (decinfo->privacy != NULL) {
375 data = (PrivacyDataPGP *) decinfo->privacy;
377 data = pgpmime_new_privacydata();
378 decinfo->privacy = (PrivacyData *) data;
380 data->done_sigtest = TRUE;
381 data->is_signed = TRUE;
382 data->sigstatus = sigstat;
384 gpgme_release(data->ctx);
392 gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account)
394 MimeInfo *msgcontent, *sigmultipart, *newinfo;
395 gchar *textstr, *micalg;
397 gchar *boundary = NULL;
400 gpgme_data_t gpgtext, gpgsig;
402 struct passphrase_cb_info_s info;
403 gpgme_sign_result_t result = NULL;
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");
428 boundary = generate_mime_boundary("Sig");
429 } while (strstr(test_msg, boundary) != NULL);
433 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("boundary"),
435 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("protocol"),
436 g_strdup("application/pgp-signature"));
437 g_node_append(sigmultipart->node, msgcontent->node);
438 g_node_append(mimeinfo->node, sigmultipart->node);
440 /* write message content to temporary file */
442 procmime_write_mimeinfo(sigmultipart, fp);
445 /* read temporary file into memory */
446 textstr = get_canonical_content(fp, boundary);
450 gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
451 gpgme_data_new(&gpgsig);
453 gpgme_set_textmode(ctx, 1);
454 gpgme_set_armor(ctx, 1);
455 gpgme_signers_clear (ctx);
457 if (!sgpgme_setup_signers(ctx, account)) {
462 if (!getenv("GPG_AGENT_INFO")) {
464 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
467 if (gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_DETACH) != GPG_ERR_NO_ERROR) {
471 result = gpgme_op_sign_result(ctx);
472 if (result && result->signatures) {
473 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
474 micalg = g_strdup_printf("PGP-%s", gpgme_hash_algo_name(
475 result->signatures->hash_algo));
477 micalg = g_strdup(gpgme_hash_algo_name(
478 result->signatures->hash_algo));
481 /* can't get result (maybe no signing key?) */
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 newinfo->content = MIMECONTENT_MEM;
498 newinfo->data.mem = g_malloc(len + 1);
499 g_memmove(newinfo->data.mem, sigcontent, len);
500 newinfo->data.mem[len] = '\0';
501 g_node_append(sigmultipart->node, newinfo->node);
507 gchar *pgpmime_get_encrypt_data(GSList *recp_names)
509 return sgpgme_get_encrypt_data(recp_names);
512 gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
514 MimeInfo *msgcontent, *encmultipart, *newinfo;
516 gchar *boundary, *enccontent;
519 gpgme_data_t gpgtext = NULL, gpgenc = NULL;
520 gpgme_ctx_t ctx = NULL;
521 gpgme_key_t *kset = NULL;
522 gchar **fprs = g_strsplit(encrypt_data, " ", -1);
524 while (fprs[i] && strlen(fprs[i])) {
528 kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
529 memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
532 while (fprs[i] && strlen(fprs[i])) {
535 err = gpgme_get_key(ctx, fprs[i], &key, 0);
537 debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
540 debug_print("found %s at %d\n", fprs[i], i);
545 debug_print("Encrypting message content\n");
547 /* remove content node from message */
548 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
549 g_node_unlink(msgcontent->node);
551 /* create temporary multipart for content */
552 encmultipart = procmime_mimeinfo_new();
553 encmultipart->type = MIMETYPE_MULTIPART;
554 encmultipart->subtype = g_strdup("encrypted");
555 boundary = generate_mime_boundary("Encrypt");
556 g_hash_table_insert(encmultipart->typeparameters, g_strdup("boundary"),
558 g_hash_table_insert(encmultipart->typeparameters, g_strdup("protocol"),
559 g_strdup("application/pgp-encrypted"));
560 g_node_append(encmultipart->node, msgcontent->node);
562 /* write message content to temporary file */
564 procmime_write_mimeinfo(encmultipart, fp);
567 /* read temporary file into memory */
568 textstr = get_canonical_content(fp, boundary);
573 gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
574 gpgme_data_new(&gpgenc);
575 gpgme_set_armor(ctx, 1);
576 gpgme_data_rewind(gpgtext);
578 gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
581 enccontent = gpgme_data_release_and_get_mem(gpgenc, &len);
582 gpgme_data_release(gpgtext);
585 /* create encrypted multipart */
586 g_node_unlink(msgcontent->node);
587 procmime_mimeinfo_free_all(msgcontent);
588 g_node_append(mimeinfo->node, encmultipart->node);
590 newinfo = procmime_mimeinfo_new();
591 newinfo->type = MIMETYPE_APPLICATION;
592 newinfo->subtype = g_strdup("pgp-encrypted");
593 newinfo->content = MIMECONTENT_MEM;
594 newinfo->data.mem = g_strdup("Version: 1\n");
595 g_node_append(encmultipart->node, newinfo->node);
597 newinfo = procmime_mimeinfo_new();
598 newinfo->type = MIMETYPE_APPLICATION;
599 newinfo->subtype = g_strdup("octet-stream");
600 newinfo->content = MIMECONTENT_MEM;
601 newinfo->data.mem = g_malloc(len + 1);
602 g_memmove(newinfo->data.mem, enccontent, len);
603 newinfo->data.mem[len] = '\0';
604 g_node_append(encmultipart->node, newinfo->node);
611 static PrivacySystem pgpmime_system = {
613 "PGP MIME", /* name */
615 pgpmime_free_privacydata, /* free_privacydata */
617 pgpmime_is_signed, /* is_signed(MimeInfo *) */
618 pgpmime_check_signature, /* check_signature(MimeInfo *) */
619 pgpmime_get_sig_status, /* get_sig_status(MimeInfo *) */
620 pgpmime_get_sig_info_short, /* get_sig_info_short(MimeInfo *) */
621 pgpmime_get_sig_info_full, /* get_sig_info_full(MimeInfo *) */
623 pgpmime_is_encrypted, /* is_encrypted(MimeInfo *) */
624 pgpmime_decrypt, /* decrypt(MimeInfo *) */
630 pgpmime_get_encrypt_data,
636 privacy_register_system(&pgpmime_system);
641 privacy_unregister_system(&pgpmime_system);
644 #endif /* USE_GPGME */