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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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) || g_ascii_strcasecmp(protocol, "application/pgp-signature"))
104 /* check if mimeinfo is the first child */
105 if (parent->node->children->data != mimeinfo)
108 /* check signature */
109 signature = parent->node->children->next != NULL ?
110 (MimeInfo *) parent->node->children->next->data : NULL;
111 if (signature == NULL)
113 if ((signature->type != MIMETYPE_APPLICATION) ||
114 g_ascii_strcasecmp(signature->subtype, "pgp-signature"))
118 data = pgpmime_new_privacydata();
119 mimeinfo->privacy = (PrivacyData *) data;
121 data->done_sigtest = TRUE;
122 data->is_signed = TRUE;
127 static gchar *get_canonical_content(FILE *fp, const gchar *boundary)
134 boundary_len = strlen(boundary);
135 while (fgets(buf, sizeof(buf), fp) != NULL)
136 if (IS_BOUNDARY(buf, boundary, boundary_len))
139 textbuffer = g_string_new("");
140 while (fgets(buf, sizeof(buf), fp) != NULL) {
143 if (IS_BOUNDARY(buf, boundary, boundary_len))
146 buf2 = canonicalize_str(buf);
147 g_string_append(textbuffer, buf2);
150 g_string_truncate(textbuffer, textbuffer->len - 2);
152 ret = textbuffer->str;
153 g_string_free(textbuffer, FALSE);
158 static gint pgpmime_check_signature(MimeInfo *mimeinfo)
160 PrivacyDataPGP *data;
161 MimeInfo *parent, *signature;
165 gpgme_data_t sigdata = NULL, textdata = NULL;
167 g_return_val_if_fail(mimeinfo != NULL, -1);
168 g_return_val_if_fail(mimeinfo->privacy != NULL, -1);
169 data = (PrivacyDataPGP *) mimeinfo->privacy;
170 gpgme_new(&data->ctx);
172 debug_print("Checking PGP/MIME signature\n");
173 parent = procmime_mimeinfo_parent(mimeinfo);
175 fp = fopen(parent->data.filename, "rb");
176 g_return_val_if_fail(fp != NULL, SIGNATURE_INVALID);
178 boundary = g_hash_table_lookup(parent->typeparameters, "boundary");
182 textstr = get_canonical_content(fp, boundary);
184 err = gpgme_data_new_from_mem(&textdata, textstr, strlen(textstr), 0);
186 debug_print ("gpgme_data_new_from_mem failed: %s\n",
187 gpgme_strerror (err));
189 signature = (MimeInfo *) mimeinfo->node->next->data;
190 sigdata = sgpgme_data_from_mimeinfo(signature);
193 sgpgme_verify_signature (data->ctx, sigdata, textdata, NULL);
195 gpgme_data_release(sigdata);
196 gpgme_data_release(textdata);
203 static SignatureStatus pgpmime_get_sig_status(MimeInfo *mimeinfo)
205 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
207 g_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
209 if (data->sigstatus == NULL &&
210 prefs_gpg_get_config()->auto_check_signatures)
211 pgpmime_check_signature(mimeinfo);
213 return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
216 static gchar *pgpmime_get_sig_info_short(MimeInfo *mimeinfo)
218 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
220 g_return_val_if_fail(data != NULL, g_strdup("Error"));
222 if (data->sigstatus == NULL &&
223 prefs_gpg_get_config()->auto_check_signatures)
224 pgpmime_check_signature(mimeinfo);
226 return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
229 static gchar *pgpmime_get_sig_info_full(MimeInfo *mimeinfo)
231 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
233 g_return_val_if_fail(data != NULL, g_strdup("Error"));
235 if (data->sigstatus == NULL &&
236 prefs_gpg_get_config()->auto_check_signatures)
237 pgpmime_check_signature(mimeinfo);
239 return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
242 static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
247 if (mimeinfo->type != MIMETYPE_MULTIPART)
249 if (g_ascii_strcasecmp(mimeinfo->subtype, "encrypted"))
251 tmpstr = procmime_mimeinfo_get_parameter(mimeinfo, "protocol");
252 if ((tmpstr == NULL) || g_ascii_strcasecmp(tmpstr, "application/pgp-encrypted"))
254 if (g_node_n_children(mimeinfo->node) != 2)
257 tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 0)->data;
258 if (tmpinfo->type != MIMETYPE_APPLICATION)
260 if (g_ascii_strcasecmp(tmpinfo->subtype, "pgp-encrypted"))
263 tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
264 if (tmpinfo->type != MIMETYPE_APPLICATION)
266 if (g_ascii_strcasecmp(tmpinfo->subtype, "octet-stream"))
272 static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
274 MimeInfo *encinfo, *decinfo, *parseinfo;
275 gpgme_data_t cipher = NULL, plain = NULL;
279 gpgme_verify_result_t sigstat = NULL;
280 PrivacyDataPGP *data = NULL;
285 if (gpgme_new(&ctx) != GPG_ERR_NO_ERROR)
289 g_return_val_if_fail(pgpmime_is_encrypted(mimeinfo), NULL);
291 encinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
293 cipher = sgpgme_data_from_mimeinfo(encinfo);
294 plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
296 gpgme_data_release(cipher);
298 debug_print("plain is null!\n");
303 fname = g_strdup_printf("%s%cplaintext.%08x",
304 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
306 if ((dstfp = fopen(fname, "wb")) == NULL) {
307 FILE_OP_ERROR(fname, "fopen");
309 gpgme_data_release(plain);
311 debug_print("can't open!\n");
315 fprintf(dstfp, "MIME-Version: 1.0\n");
317 chars = gpgme_data_release_and_get_mem(plain, &len);
319 fwrite(chars, len, 1, dstfp);
322 parseinfo = procmime_scan_file(fname);
324 if (parseinfo == NULL) {
328 decinfo = g_node_first_child(parseinfo->node) != NULL ?
329 g_node_first_child(parseinfo->node)->data : NULL;
330 if (decinfo == NULL) {
335 g_node_unlink(decinfo->node);
336 procmime_mimeinfo_free_all(parseinfo);
340 if (sigstat != NULL && sigstat->signatures != NULL) {
341 if (decinfo->privacy != NULL) {
342 data = (PrivacyDataPGP *) decinfo->privacy;
344 data = pgpmime_new_privacydata();
345 decinfo->privacy = (PrivacyData *) data;
347 data->done_sigtest = TRUE;
348 data->is_signed = TRUE;
349 data->sigstatus = sigstat;
351 gpgme_release(data->ctx);
359 gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account)
361 MimeInfo *msgcontent, *sigmultipart, *newinfo;
362 gchar *textstr, *micalg;
364 gchar *boundary, *sigcontent;
366 gpgme_data_t gpgtext, gpgsig;
368 struct passphrase_cb_info_s info;
369 gpgme_sign_result_t result = NULL;
371 memset (&info, 0, sizeof info);
373 /* remove content node from message */
374 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
375 g_node_unlink(msgcontent->node);
377 /* create temporary multipart for content */
378 sigmultipart = procmime_mimeinfo_new();
379 sigmultipart->type = MIMETYPE_MULTIPART;
380 sigmultipart->subtype = g_strdup("signed");
381 boundary = generate_mime_boundary("Signature");
382 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("boundary"),
384 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("protocol"),
385 g_strdup("application/pgp-signature"));
386 g_node_append(sigmultipart->node, msgcontent->node);
387 g_node_append(mimeinfo->node, sigmultipart->node);
389 /* write message content to temporary file */
391 procmime_write_mimeinfo(sigmultipart, fp);
394 /* read temporary file into memory */
395 textstr = get_canonical_content(fp, boundary);
399 gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
400 gpgme_data_new(&gpgsig);
402 gpgme_set_textmode(ctx, 1);
403 gpgme_set_armor(ctx, 1);
404 gpgme_signers_clear (ctx);
406 if (!sgpgme_setup_signers(ctx, account)) {
411 if (!getenv("GPG_AGENT_INFO")) {
413 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
416 if (gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_DETACH) != GPG_ERR_NO_ERROR) {
420 result = gpgme_op_sign_result(ctx);
421 if (result && result->signatures) {
422 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
423 micalg = g_strdup_printf("PGP-%s", gpgme_hash_algo_name(
424 result->signatures->hash_algo));
426 micalg = g_strdup(gpgme_hash_algo_name(
427 result->signatures->hash_algo));
430 /* can't get result (maybe no signing key?) */
435 sigcontent = gpgme_data_release_and_get_mem(gpgsig, &len);
436 gpgme_data_release(gpgtext);
440 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("micalg"),
443 newinfo = procmime_mimeinfo_new();
444 newinfo->type = MIMETYPE_APPLICATION;
445 newinfo->subtype = g_strdup("pgp-signature");
446 newinfo->content = MIMECONTENT_MEM;
447 newinfo->data.mem = g_malloc(len + 1);
448 g_memmove(newinfo->data.mem, sigcontent, len);
449 newinfo->data.mem[len] = '\0';
450 g_node_append(sigmultipart->node, newinfo->node);
456 gchar *pgpmime_get_encrypt_data(GSList *recp_names)
458 return sgpgme_get_encrypt_data(recp_names);
461 gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
463 MimeInfo *msgcontent, *encmultipart, *newinfo;
465 gchar *boundary, *enccontent;
468 gpgme_data_t gpgtext = NULL, gpgenc = NULL;
469 gpgme_ctx_t ctx = NULL;
470 gpgme_key_t *kset = NULL;
471 gchar **fprs = g_strsplit(encrypt_data, " ", -1);
473 while (fprs[i] && strlen(fprs[i])) {
477 kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
478 memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
481 while (fprs[i] && strlen(fprs[i])) {
484 err = gpgme_get_key(ctx, fprs[i], &key, 0);
486 debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
489 debug_print("found %s at %d\n", fprs[i], i);
494 debug_print("Encrypting message content\n");
496 /* remove content node from message */
497 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
498 g_node_unlink(msgcontent->node);
500 /* create temporary multipart for content */
501 encmultipart = procmime_mimeinfo_new();
502 encmultipart->type = MIMETYPE_MULTIPART;
503 encmultipart->subtype = g_strdup("encrypted");
504 boundary = generate_mime_boundary("Encrypt");
505 g_hash_table_insert(encmultipart->typeparameters, g_strdup("boundary"),
507 g_hash_table_insert(encmultipart->typeparameters, g_strdup("protocol"),
508 g_strdup("application/pgp-encrypted"));
509 g_node_append(encmultipart->node, msgcontent->node);
511 /* write message content to temporary file */
513 procmime_write_mimeinfo(encmultipart, fp);
516 /* read temporary file into memory */
517 textstr = get_canonical_content(fp, boundary);
522 gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
523 gpgme_data_new(&gpgenc);
524 gpgme_set_armor(ctx, 1);
525 gpgme_data_rewind(gpgtext);
527 gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
530 enccontent = gpgme_data_release_and_get_mem(gpgenc, &len);
531 gpgme_data_release(gpgtext);
534 /* create encrypted multipart */
535 g_node_unlink(msgcontent->node);
536 procmime_mimeinfo_free_all(msgcontent);
537 g_node_append(mimeinfo->node, encmultipart->node);
539 newinfo = procmime_mimeinfo_new();
540 newinfo->type = MIMETYPE_APPLICATION;
541 newinfo->subtype = g_strdup("pgp-encrypted");
542 newinfo->content = MIMECONTENT_MEM;
543 newinfo->data.mem = g_strdup("Version: 1\n");
544 g_node_append(encmultipart->node, newinfo->node);
546 newinfo = procmime_mimeinfo_new();
547 newinfo->type = MIMETYPE_APPLICATION;
548 newinfo->subtype = g_strdup("octet-stream");
549 newinfo->content = MIMECONTENT_MEM;
550 newinfo->data.mem = g_malloc(len + 1);
551 g_memmove(newinfo->data.mem, enccontent, len);
552 newinfo->data.mem[len] = '\0';
553 g_node_append(encmultipart->node, newinfo->node);
560 static PrivacySystem pgpmime_system = {
562 "PGP MIME", /* name */
564 pgpmime_free_privacydata, /* free_privacydata */
566 pgpmime_is_signed, /* is_signed(MimeInfo *) */
567 pgpmime_check_signature, /* check_signature(MimeInfo *) */
568 pgpmime_get_sig_status, /* get_sig_status(MimeInfo *) */
569 pgpmime_get_sig_info_short, /* get_sig_info_short(MimeInfo *) */
570 pgpmime_get_sig_info_full, /* get_sig_info_full(MimeInfo *) */
572 pgpmime_is_encrypted, /* is_encrypted(MimeInfo *) */
573 pgpmime_decrypt, /* decrypt(MimeInfo *) */
579 pgpmime_get_encrypt_data,
585 privacy_register_system(&pgpmime_system);
590 privacy_unregister_system(&pgpmime_system);
593 #endif /* USE_GPGME */