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 "prefs_common.h"
37 #include "prefs_gpg.h"
38 #include "passphrase.h"
40 typedef struct _PrivacyDataPGP PrivacyDataPGP;
42 struct _PrivacyDataPGP
46 gboolean done_sigtest;
48 gpgme_verify_result_t sigstatus;
52 static PrivacySystem pgpmime_system;
54 static gint pgpmime_check_signature(MimeInfo *mimeinfo);
56 static PrivacyDataPGP *pgpmime_new_privacydata()
60 data = g_new0(PrivacyDataPGP, 1);
61 data->data.system = &pgpmime_system;
62 data->done_sigtest = FALSE;
63 data->is_signed = FALSE;
64 data->sigstatus = NULL;
65 gpgme_new(&data->ctx);
70 static void pgpmime_free_privacydata(PrivacyData *_data)
72 PrivacyDataPGP *data = (PrivacyDataPGP *) _data;
73 gpgme_release(data->ctx);
77 static gboolean pgpmime_is_signed(MimeInfo *mimeinfo)
81 const gchar *protocol;
82 PrivacyDataPGP *data = NULL;
84 g_return_val_if_fail(mimeinfo != NULL, FALSE);
85 if (mimeinfo->privacy != NULL) {
86 data = (PrivacyDataPGP *) mimeinfo->privacy;
87 if (data->done_sigtest)
88 return data->is_signed;
92 parent = procmime_mimeinfo_parent(mimeinfo);
95 if ((parent->type != MIMETYPE_MULTIPART) ||
96 g_ascii_strcasecmp(parent->subtype, "signed"))
98 protocol = procmime_mimeinfo_get_parameter(parent, "protocol");
99 if ((protocol == NULL) || g_ascii_strcasecmp(protocol, "application/pgp-signature"))
102 /* check if mimeinfo is the first child */
103 if (parent->node->children->data != mimeinfo)
106 /* check signature */
107 signature = parent->node->children->next != NULL ?
108 (MimeInfo *) parent->node->children->next->data : NULL;
109 if (signature == NULL)
111 if ((signature->type != MIMETYPE_APPLICATION) ||
112 g_ascii_strcasecmp(signature->subtype, "pgp-signature"))
116 data = pgpmime_new_privacydata();
117 mimeinfo->privacy = (PrivacyData *) data;
119 data->done_sigtest = TRUE;
120 data->is_signed = TRUE;
125 static gchar *get_canonical_content(FILE *fp, const gchar *boundary)
132 boundary_len = strlen(boundary);
133 while (fgets(buf, sizeof(buf), fp) != NULL)
134 if (IS_BOUNDARY(buf, boundary, boundary_len))
137 textbuffer = g_string_new("");
138 while (fgets(buf, sizeof(buf), fp) != NULL) {
141 if (IS_BOUNDARY(buf, boundary, boundary_len))
144 buf2 = canonicalize_str(buf);
145 g_string_append(textbuffer, buf2);
148 g_string_truncate(textbuffer, textbuffer->len - 2);
150 ret = textbuffer->str;
151 g_string_free(textbuffer, FALSE);
156 static gint pgpmime_check_signature(MimeInfo *mimeinfo)
158 PrivacyDataPGP *data;
159 MimeInfo *parent, *signature;
163 gpgme_data_t sigdata = NULL, textdata = NULL;
165 g_return_val_if_fail(mimeinfo != NULL, -1);
166 g_return_val_if_fail(mimeinfo->privacy != NULL, -1);
167 data = (PrivacyDataPGP *) mimeinfo->privacy;
168 gpgme_new(&data->ctx);
170 debug_print("Checking PGP/MIME signature\n");
171 parent = procmime_mimeinfo_parent(mimeinfo);
173 fp = fopen(parent->data.filename, "rb");
174 g_return_val_if_fail(fp != NULL, SIGNATURE_INVALID);
176 boundary = g_hash_table_lookup(parent->typeparameters, "boundary");
180 textstr = get_canonical_content(fp, boundary);
182 err = gpgme_data_new_from_mem(&textdata, textstr, strlen(textstr), 0);
184 debug_print ("gpgme_data_new_from_mem failed: %s\n",
185 gpgme_strerror (err));
187 signature = (MimeInfo *) mimeinfo->node->next->data;
188 sigdata = sgpgme_data_from_mimeinfo(signature);
191 sgpgme_verify_signature (data->ctx, sigdata, textdata, NULL);
193 gpgme_data_release(sigdata);
194 gpgme_data_release(textdata);
201 static SignatureStatus pgpmime_get_sig_status(MimeInfo *mimeinfo)
203 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
205 g_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
207 if (data->sigstatus == NULL &&
208 prefs_gpg_get_config()->auto_check_signatures)
209 pgpmime_check_signature(mimeinfo);
211 return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
214 static gchar *pgpmime_get_sig_info_short(MimeInfo *mimeinfo)
216 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
218 g_return_val_if_fail(data != NULL, g_strdup("Error"));
220 if (data->sigstatus == NULL &&
221 prefs_gpg_get_config()->auto_check_signatures)
222 pgpmime_check_signature(mimeinfo);
224 return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
227 static gchar *pgpmime_get_sig_info_full(MimeInfo *mimeinfo)
229 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
231 g_return_val_if_fail(data != NULL, g_strdup("Error"));
233 if (data->sigstatus == NULL &&
234 prefs_gpg_get_config()->auto_check_signatures)
235 pgpmime_check_signature(mimeinfo);
237 return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
240 static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
245 if (mimeinfo->type != MIMETYPE_MULTIPART)
247 if (g_ascii_strcasecmp(mimeinfo->subtype, "encrypted"))
249 tmpstr = procmime_mimeinfo_get_parameter(mimeinfo, "protocol");
250 if ((tmpstr == NULL) || g_ascii_strcasecmp(tmpstr, "application/pgp-encrypted"))
252 if (g_node_n_children(mimeinfo->node) != 2)
255 tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 0)->data;
256 if (tmpinfo->type != MIMETYPE_APPLICATION)
258 if (g_ascii_strcasecmp(tmpinfo->subtype, "pgp-encrypted"))
261 tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
262 if (tmpinfo->type != MIMETYPE_APPLICATION)
264 if (g_ascii_strcasecmp(tmpinfo->subtype, "octet-stream"))
270 static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
272 MimeInfo *encinfo, *decinfo, *parseinfo;
273 gpgme_data_t cipher = NULL, plain = NULL;
277 gpgme_verify_result_t sigstat = NULL;
278 PrivacyDataPGP *data = NULL;
283 if (gpgme_new(&ctx) != GPG_ERR_NO_ERROR)
287 g_return_val_if_fail(pgpmime_is_encrypted(mimeinfo), NULL);
289 encinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
291 cipher = sgpgme_data_from_mimeinfo(encinfo);
292 plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
294 gpgme_data_release(cipher);
296 debug_print("plain is null!\n");
301 fname = g_strdup_printf("%s%cplaintext.%08x",
302 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
304 if ((dstfp = fopen(fname, "wb")) == NULL) {
305 FILE_OP_ERROR(fname, "fopen");
307 gpgme_data_release(plain);
309 debug_print("can't open!\n");
313 fprintf(dstfp, "MIME-Version: 1.0\n");
315 chars = gpgme_data_release_and_get_mem(plain, &len);
317 fwrite(chars, len, 1, dstfp);
320 parseinfo = procmime_scan_file(fname);
322 if (parseinfo == NULL) {
326 decinfo = g_node_first_child(parseinfo->node) != NULL ?
327 g_node_first_child(parseinfo->node)->data : NULL;
328 if (decinfo == NULL) {
333 g_node_unlink(decinfo->node);
334 procmime_mimeinfo_free_all(parseinfo);
338 if (sigstat != NULL && sigstat->signatures != NULL) {
339 if (decinfo->privacy != NULL) {
340 data = (PrivacyDataPGP *) decinfo->privacy;
342 data = pgpmime_new_privacydata();
343 decinfo->privacy = (PrivacyData *) data;
345 data->done_sigtest = TRUE;
346 data->is_signed = TRUE;
347 data->sigstatus = sigstat;
349 gpgme_release(data->ctx);
357 gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account)
359 MimeInfo *msgcontent, *sigmultipart, *newinfo;
360 gchar *textstr, *micalg;
362 gchar *boundary, *sigcontent;
364 gpgme_data_t gpgtext, gpgsig;
366 struct passphrase_cb_info_s info;
367 gpgme_sign_result_t result = NULL;
369 memset (&info, 0, sizeof info);
371 /* remove content node from message */
372 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
373 g_node_unlink(msgcontent->node);
375 /* create temporary multipart for content */
376 sigmultipart = procmime_mimeinfo_new();
377 sigmultipart->type = MIMETYPE_MULTIPART;
378 sigmultipart->subtype = g_strdup("signed");
379 boundary = generate_mime_boundary("Signature");
380 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("boundary"),
382 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("protocol"),
383 g_strdup("application/pgp-signature"));
384 g_node_append(sigmultipart->node, msgcontent->node);
385 g_node_append(mimeinfo->node, sigmultipart->node);
387 /* write message content to temporary file */
389 procmime_write_mimeinfo(sigmultipart, fp);
392 /* read temporary file into memory */
393 textstr = get_canonical_content(fp, boundary);
397 gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
398 gpgme_data_new(&gpgsig);
400 gpgme_set_textmode(ctx, 1);
401 gpgme_set_armor(ctx, 1);
402 gpgme_signers_clear (ctx);
404 if (!sgpgme_setup_signers(ctx, account)) {
409 if (!getenv("GPG_AGENT_INFO")) {
411 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
414 if (gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_DETACH) != GPG_ERR_NO_ERROR) {
418 result = gpgme_op_sign_result(ctx);
419 if (result && result->signatures) {
420 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
421 micalg = g_strdup_printf("PGP-%s", gpgme_hash_algo_name(
422 result->signatures->hash_algo));
424 micalg = g_strdup(gpgme_hash_algo_name(
425 result->signatures->hash_algo));
428 /* can't get result (maybe no signing key?) */
433 sigcontent = gpgme_data_release_and_get_mem(gpgsig, &len);
434 gpgme_data_release(gpgtext);
438 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("micalg"),
441 newinfo = procmime_mimeinfo_new();
442 newinfo->type = MIMETYPE_APPLICATION;
443 newinfo->subtype = g_strdup("pgp-signature");
444 newinfo->content = MIMECONTENT_MEM;
445 newinfo->data.mem = g_malloc(len + 1);
446 g_memmove(newinfo->data.mem, sigcontent, len);
447 newinfo->data.mem[len] = '\0';
448 g_node_append(sigmultipart->node, newinfo->node);
454 gchar *pgpmime_get_encrypt_data(GSList *recp_names)
456 return sgpgme_get_encrypt_data(recp_names);
459 gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
461 MimeInfo *msgcontent, *encmultipart, *newinfo;
463 gchar *boundary, *enccontent;
466 gpgme_data_t gpgtext = NULL, gpgenc = NULL;
467 gpgme_ctx_t ctx = NULL;
468 gpgme_key_t *kset = NULL;
469 gchar **fprs = g_strsplit(encrypt_data, " ", -1);
471 while (fprs[i] && strlen(fprs[i])) {
475 kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
476 memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
479 while (fprs[i] && strlen(fprs[i])) {
482 err = gpgme_get_key(ctx, fprs[i], &key, 0);
484 debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
487 debug_print("found %s at %d\n", fprs[i], i);
492 debug_print("Encrypting message content\n");
494 /* remove content node from message */
495 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
496 g_node_unlink(msgcontent->node);
498 /* create temporary multipart for content */
499 encmultipart = procmime_mimeinfo_new();
500 encmultipart->type = MIMETYPE_MULTIPART;
501 encmultipart->subtype = g_strdup("encrypted");
502 boundary = generate_mime_boundary("Encrypt");
503 g_hash_table_insert(encmultipart->typeparameters, g_strdup("boundary"),
505 g_hash_table_insert(encmultipart->typeparameters, g_strdup("protocol"),
506 g_strdup("application/pgp-encrypted"));
507 g_node_append(encmultipart->node, msgcontent->node);
509 /* write message content to temporary file */
511 procmime_write_mimeinfo(encmultipart, fp);
514 /* read temporary file into memory */
515 textstr = get_canonical_content(fp, boundary);
520 gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
521 gpgme_data_new(&gpgenc);
522 gpgme_set_armor(ctx, 1);
523 gpgme_data_seek(gpgtext, 0, SEEK_SET);
525 gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
528 enccontent = gpgme_data_release_and_get_mem(gpgenc, &len);
529 gpgme_data_release(gpgtext);
532 /* create encrypted multipart */
533 g_node_unlink(msgcontent->node);
534 procmime_mimeinfo_free_all(msgcontent);
535 g_node_append(mimeinfo->node, encmultipart->node);
537 newinfo = procmime_mimeinfo_new();
538 newinfo->type = MIMETYPE_APPLICATION;
539 newinfo->subtype = g_strdup("pgp-encrypted");
540 newinfo->content = MIMECONTENT_MEM;
541 newinfo->data.mem = g_strdup("Version: 1\n");
542 g_node_append(encmultipart->node, newinfo->node);
544 newinfo = procmime_mimeinfo_new();
545 newinfo->type = MIMETYPE_APPLICATION;
546 newinfo->subtype = g_strdup("octet-stream");
547 newinfo->content = MIMECONTENT_MEM;
548 newinfo->data.mem = g_malloc(len + 1);
549 g_memmove(newinfo->data.mem, enccontent, len);
550 newinfo->data.mem[len] = '\0';
551 g_node_append(encmultipart->node, newinfo->node);
558 static PrivacySystem pgpmime_system = {
560 "PGP MIME", /* name */
562 pgpmime_free_privacydata, /* free_privacydata */
564 pgpmime_is_signed, /* is_signed(MimeInfo *) */
565 pgpmime_check_signature, /* check_signature(MimeInfo *) */
566 pgpmime_get_sig_status, /* get_sig_status(MimeInfo *) */
567 pgpmime_get_sig_info_short, /* get_sig_info_short(MimeInfo *) */
568 pgpmime_get_sig_info_full, /* get_sig_info_full(MimeInfo *) */
570 pgpmime_is_encrypted, /* is_encrypted(MimeInfo *) */
571 pgpmime_decrypt, /* decrypt(MimeInfo *) */
577 pgpmime_get_encrypt_data,
583 privacy_register_system(&pgpmime_system);
588 privacy_unregister_system(&pgpmime_system);
591 #endif /* USE_GPGME */