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 GpgmeSigStat 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 = GPGME_SIG_STAT_NONE;
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 GpgmeData sigdata, textdata;
165 g_return_val_if_fail(mimeinfo != NULL, -1);
166 g_return_val_if_fail(mimeinfo->privacy != NULL, -1);
167 data = (PrivacyDataPGP *) mimeinfo->privacy;
169 debug_print("Checking PGP/MIME signature\n");
170 parent = procmime_mimeinfo_parent(mimeinfo);
172 fp = fopen(parent->data.filename, "rb");
173 g_return_val_if_fail(fp != NULL, SIGNATURE_INVALID);
175 boundary = g_hash_table_lookup(parent->typeparameters, "boundary");
179 textstr = get_canonical_content(fp, boundary);
181 gpgme_data_new_from_mem(&textdata, textstr, strlen(textstr), 0);
182 signature = (MimeInfo *) mimeinfo->node->next->data;
183 sigdata = sgpgme_data_from_mimeinfo(signature);
186 sgpgme_verify_signature (data->ctx, sigdata, textdata);
188 gpgme_data_release(sigdata);
189 gpgme_data_release(textdata);
196 static SignatureStatus pgpmime_get_sig_status(MimeInfo *mimeinfo)
198 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
200 g_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
202 if (data->sigstatus == GPGME_SIG_STAT_NONE &&
203 prefs_gpg_get_config()->auto_check_signatures)
204 pgpmime_check_signature(mimeinfo);
206 return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
209 static gchar *pgpmime_get_sig_info_short(MimeInfo *mimeinfo)
211 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
213 g_return_val_if_fail(data != NULL, g_strdup("Error"));
215 if (data->sigstatus == GPGME_SIG_STAT_NONE &&
216 prefs_gpg_get_config()->auto_check_signatures)
217 pgpmime_check_signature(mimeinfo);
219 return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
222 static gchar *pgpmime_get_sig_info_full(MimeInfo *mimeinfo)
224 PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
226 g_return_val_if_fail(data != NULL, g_strdup("Error"));
228 if (data->sigstatus == GPGME_SIG_STAT_NONE &&
229 prefs_gpg_get_config()->auto_check_signatures)
230 pgpmime_check_signature(mimeinfo);
232 return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
235 static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
240 if (mimeinfo->type != MIMETYPE_MULTIPART)
242 if (g_ascii_strcasecmp(mimeinfo->subtype, "encrypted"))
244 tmpstr = procmime_mimeinfo_get_parameter(mimeinfo, "protocol");
245 if ((tmpstr == NULL) || g_ascii_strcasecmp(tmpstr, "application/pgp-encrypted"))
247 if (g_node_n_children(mimeinfo->node) != 2)
250 tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 0)->data;
251 if (tmpinfo->type != MIMETYPE_APPLICATION)
253 if (g_ascii_strcasecmp(tmpinfo->subtype, "pgp-encrypted"))
256 tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
257 if (tmpinfo->type != MIMETYPE_APPLICATION)
259 if (g_ascii_strcasecmp(tmpinfo->subtype, "octet-stream"))
265 static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
267 MimeInfo *encinfo, *decinfo, *parseinfo;
268 GpgmeData cipher, plain;
274 GpgmeSigStat sigstat = 0;
275 PrivacyDataPGP *data = NULL;
278 if (gpgme_new(&ctx) != GPGME_No_Error)
282 g_return_val_if_fail(pgpmime_is_encrypted(mimeinfo), NULL);
284 encinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
286 cipher = sgpgme_data_from_mimeinfo(encinfo);
287 plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
289 gpgme_data_release(cipher);
295 fname = g_strdup_printf("%s%cplaintext.%08x",
296 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
298 if ((dstfp = fopen(fname, "wb")) == NULL) {
299 FILE_OP_ERROR(fname, "fopen");
301 gpgme_data_release(plain);
306 fprintf(dstfp, "MIME-Version: 1.0\n");
307 gpgme_data_rewind (plain);
308 while (gpgme_data_read(plain, buf, sizeof(buf), &nread) == GPGME_No_Error) {
309 fwrite (buf, nread, 1, dstfp);
313 gpgme_data_release(plain);
315 parseinfo = procmime_scan_file(fname);
317 if (parseinfo == NULL) {
321 decinfo = g_node_first_child(parseinfo->node) != NULL ?
322 g_node_first_child(parseinfo->node)->data : NULL;
323 if (decinfo == NULL) {
328 g_node_unlink(decinfo->node);
329 procmime_mimeinfo_free_all(parseinfo);
333 if (sigstat != GPGME_SIG_STAT_NONE) {
334 if (decinfo->privacy != NULL) {
335 data = (PrivacyDataPGP *) decinfo->privacy;
337 data = pgpmime_new_privacydata();
338 decinfo->privacy = (PrivacyData *) data;
340 data->done_sigtest = TRUE;
341 data->is_signed = TRUE;
342 data->sigstatus = sigstat;
344 gpgme_release(data->ctx);
353 * Find TAG in XML and return a pointer into xml set just behind the
354 * closing angle. Return NULL if not found.
357 find_xml_tag (const char *xml, const char *tag)
359 int taglen = strlen (tag);
362 while ( (s = strchr (s, '<')) ) {
364 if (!strncmp (s, tag, taglen)) {
365 const char *s2 = s + taglen;
366 if (*s2 == '>' || isspace (*(const unsigned char*)s2) ) {
368 while (*s2 && *s2 != '>') /* skip attributes */
370 /* fixme: do need to handle angles inside attribute vallues? */
371 return *s2? (s2+1):NULL;
374 while (*s && *s != '>') /* skip to end of tag */
382 * Extract the micalg from an GnupgOperationInfo XML container.
385 extract_micalg (char *xml)
389 s = find_xml_tag (xml, "GnupgOperationInfo");
391 const char *s_end = find_xml_tag (s, "/GnupgOperationInfo");
392 s = find_xml_tag (s, "signature");
393 if (s && s_end && s < s_end) {
394 const char *s_end2 = find_xml_tag (s, "/signature");
395 if (s_end2 && s_end2 < s_end) {
396 s = find_xml_tag (s, "micalg");
397 if (s && s < s_end2) {
398 s_end = strchr (s, '<');
400 char *p = g_malloc (s_end - s + 1);
401 memcpy (p, s, s_end - s);
412 gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account)
414 MimeInfo *msgcontent, *sigmultipart, *newinfo;
415 gchar *textstr, *opinfo, *micalg;
417 gchar *boundary, *sigcontent;
419 GpgmeData gpgtext, gpgsig;
421 struct passphrase_cb_info_s info;
423 memset (&info, 0, sizeof info);
425 /* remove content node from message */
426 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
427 g_node_unlink(msgcontent->node);
429 /* create temporary multipart for content */
430 sigmultipart = procmime_mimeinfo_new();
431 sigmultipart->type = MIMETYPE_MULTIPART;
432 sigmultipart->subtype = g_strdup("signed");
433 boundary = generate_mime_boundary("Signature");
434 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("boundary"),
436 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("protocol"),
437 g_strdup("application/pgp-signature"));
438 g_node_append(sigmultipart->node, msgcontent->node);
439 g_node_append(mimeinfo->node, sigmultipart->node);
441 /* write message content to temporary file */
443 procmime_write_mimeinfo(sigmultipart, fp);
446 /* read temporary file into memory */
447 textstr = get_canonical_content(fp, boundary);
451 gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
452 gpgme_data_new(&gpgsig);
454 gpgme_set_textmode(ctx, 1);
455 gpgme_set_armor(ctx, 1);
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) != GPGME_No_Error) {
471 opinfo = gpgme_get_op_info(ctx, 0);
472 micalg = extract_micalg(opinfo);
476 sigcontent = gpgme_data_release_and_get_mem(gpgsig, &len);
477 gpgme_data_release(gpgtext);
481 g_hash_table_insert(sigmultipart->typeparameters, g_strdup("micalg"),
484 newinfo = procmime_mimeinfo_new();
485 newinfo->type = MIMETYPE_APPLICATION;
486 newinfo->subtype = g_strdup("pgp-signature");
487 newinfo->content = MIMECONTENT_MEM;
488 newinfo->data.mem = g_malloc(len + 1);
489 g_memmove(newinfo->data.mem, sigcontent, len);
490 newinfo->data.mem[len] = '\0';
491 g_node_append(sigmultipart->node, newinfo->node);
498 gchar *pgpmime_get_encrypt_data(GSList *recp_names)
500 return sgpgme_get_encrypt_data(recp_names);
503 gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
505 MimeInfo *msgcontent, *encmultipart, *newinfo;
507 gchar *boundary, *enccontent;
510 GpgmeData gpgtext, gpgenc;
511 gchar **recipients, **nextrecp;
512 GpgmeRecipients recp;
515 /* build GpgmeRecipients from encrypt_data */
516 recipients = g_strsplit(encrypt_data, " ", 0);
517 gpgme_recipients_new(&recp);
518 for (nextrecp = recipients; *nextrecp != NULL; nextrecp++) {
519 gpgme_recipients_add_name_with_validity(recp, *nextrecp,
520 GPGME_VALIDITY_FULL);
522 g_strfreev(recipients);
524 debug_print("Encrypting message content\n");
526 /* remove content node from message */
527 msgcontent = (MimeInfo *) mimeinfo->node->children->data;
528 g_node_unlink(msgcontent->node);
530 /* create temporary multipart for content */
531 encmultipart = procmime_mimeinfo_new();
532 encmultipart->type = MIMETYPE_MULTIPART;
533 encmultipart->subtype = g_strdup("encrypted");
534 boundary = generate_mime_boundary("Encrypt");
535 g_hash_table_insert(encmultipart->typeparameters, g_strdup("boundary"),
537 g_hash_table_insert(encmultipart->typeparameters, g_strdup("protocol"),
538 g_strdup("application/pgp-encrypted"));
539 g_node_append(encmultipart->node, msgcontent->node);
541 /* write message content to temporary file */
543 procmime_write_mimeinfo(encmultipart, fp);
546 /* read temporary file into memory */
547 textstr = get_canonical_content(fp, boundary);
552 gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
553 gpgme_data_new(&gpgenc);
555 gpgme_set_armor(ctx, 1);
557 gpgme_op_encrypt(ctx, recp, gpgtext, gpgenc);
560 enccontent = gpgme_data_release_and_get_mem(gpgenc, &len);
561 gpgme_recipients_release(recp);
562 gpgme_data_release(gpgtext);
565 /* create encrypted multipart */
566 g_node_unlink(msgcontent->node);
567 procmime_mimeinfo_free_all(msgcontent);
568 g_node_append(mimeinfo->node, encmultipart->node);
570 newinfo = procmime_mimeinfo_new();
571 newinfo->type = MIMETYPE_APPLICATION;
572 newinfo->subtype = g_strdup("pgp-encrypted");
573 newinfo->content = MIMECONTENT_MEM;
574 newinfo->data.mem = g_strdup("Version: 1\n");
575 g_node_append(encmultipart->node, newinfo->node);
577 newinfo = procmime_mimeinfo_new();
578 newinfo->type = MIMETYPE_APPLICATION;
579 newinfo->subtype = g_strdup("octet-stream");
580 newinfo->content = MIMECONTENT_MEM;
581 newinfo->data.mem = g_malloc(len + 1);
582 g_memmove(newinfo->data.mem, enccontent, len);
583 newinfo->data.mem[len] = '\0';
584 g_node_append(encmultipart->node, newinfo->node);
591 static PrivacySystem pgpmime_system = {
593 "PGP/MIME", /* name */
595 pgpmime_free_privacydata, /* free_privacydata */
597 pgpmime_is_signed, /* is_signed(MimeInfo *) */
598 pgpmime_check_signature, /* check_signature(MimeInfo *) */
599 pgpmime_get_sig_status, /* get_sig_status(MimeInfo *) */
600 pgpmime_get_sig_info_short, /* get_sig_info_short(MimeInfo *) */
601 pgpmime_get_sig_info_full, /* get_sig_info_full(MimeInfo *) */
603 pgpmime_is_encrypted, /* is_encrypted(MimeInfo *) */
604 pgpmime_decrypt, /* decrypt(MimeInfo *) */
610 pgpmime_get_encrypt_data,
616 privacy_register_system(&pgpmime_system);
621 privacy_unregister_system(&pgpmime_system);
624 #endif /* USE_GPGME */