2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001 Werner Koch (dd9jn)
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.
38 #include "procheader.h"
44 #include "prefs_common.h"
45 #include "passphrase.h"
46 #include "select-keys.h"
47 #include "sigstatus.h"
49 #include "alertpanel.h"
51 #define DIM(v) (sizeof(v)/sizeof((v)[0]))
53 static char *content_names[] = {
55 "Content-Disposition",
56 "Content-Transfer-Encoding",
60 static char *mime_version_name[] = {
65 static void sig_expiration_check (GString *str,
70 static void sig_expired (GString *str,
73 static void sig_key_expired (GString *str,
78 static void dump_mimeinfo (const char *text, MimeInfo *x)
80 debug_print ("MimeInfo[%s] %p level=%d\n",
81 text, x, x? x->level:0 );
85 debug_print (" enc=`%s' enc_type=%d mime_type=%d\n",
86 x->encoding, x->encoding_type, x->mime_type );
87 debug_print (" cont_type=`%s' cs=`%s' name=`%s' bnd=`%s'\n",
88 x->content_type, x->charset, x->name, x->boundary );
89 debug_print (" cont_disp=`%s' fname=`%s' fpos=%ld size=%u, lvl=%d\n",
90 x->content_disposition, x->filename, x->fpos, x->size,
92 dump_mimeinfo (".main", x->main );
93 dump_mimeinfo (".sub", x->sub );
94 dump_mimeinfo (".next", x->next );
95 debug_print ("MimeInfo[.parent] %p\n", x );
96 dump_mimeinfo (".children", x->children );
97 dump_mimeinfo (".plaintext", x->plaintext );
100 static void dump_part ( MimeInfo *mimeinfo, FILE *fp )
102 unsigned int size = mimeinfo->size;
105 if (fseek (fp, mimeinfo->fpos, SEEK_SET)) {
106 debug_print ("dump_part: fseek error\n");
110 debug_print ("--- begin dump_part ----\n");
111 while (size-- && (c = getc (fp)) != EOF)
114 debug_print ("dump_part: read error\n");
115 debug_print ("--- end dump_part ----\n");
120 rfc2015_disable_all (void)
122 /* FIXME: set a flag, so that we don't bother the user with failed
128 rfc2015_secure_remove (const char *fname)
132 /* fixme: overwrite the file first */
138 sig_status_to_string (GpgmeSigStat status)
143 case GPGME_SIG_STAT_NONE:
144 result = _("Oops: Signature not verified");
146 case GPGME_SIG_STAT_NOSIG:
147 result = _("No signature found");
149 case GPGME_SIG_STAT_GOOD:
150 result = _("Good signature");
152 case GPGME_SIG_STAT_GOOD_EXP:
153 result = _("Good signature but it has expired");
155 case GPGME_SIG_STAT_GOOD_EXPKEY:
156 result = _("Good signature but the key has expired");
158 case GPGME_SIG_STAT_BAD:
159 result = _("BAD signature");
161 case GPGME_SIG_STAT_NOKEY:
162 result = _("No public key to verify the signature");
164 case GPGME_SIG_STAT_ERROR:
165 result = _("Error verifying the signature");
167 case GPGME_SIG_STAT_DIFF:
168 result = _("Different results for signatures");
171 result = _("Error: Unknown status");
179 sig_status_with_name (GpgmeSigStat status)
184 case GPGME_SIG_STAT_NONE:
185 result = _("Oops: Signature not verified");
187 case GPGME_SIG_STAT_NOSIG:
188 result = _("No signature found");
190 case GPGME_SIG_STAT_GOOD:
191 result = _("Good signature from \"%s\"");
193 case GPGME_SIG_STAT_GOOD_EXP:
194 result = _("Good signature from \"%s\" but it has expired");
196 case GPGME_SIG_STAT_GOOD_EXPKEY:
197 result = _("Good signature from \"%s\" but the key has expired");
199 case GPGME_SIG_STAT_BAD:
200 result = _("BAD signature from \"%s\"");
202 case GPGME_SIG_STAT_NOKEY:
203 result = _("No public key to verify the signature");
205 case GPGME_SIG_STAT_ERROR:
206 result = _("Error verifying the signature");
208 case GPGME_SIG_STAT_DIFF:
209 result = _("Different results for signatures");
212 result = _("Error: Unknown status");
220 sig_status_for_key(GString *str, GpgmeCtx ctx, GpgmeSigStat status,
221 GpgmeKey key, const gchar *fpr)
226 uid = gpgme_key_get_string_attr (key, GPGME_ATTR_USERID, NULL, idx);
228 g_string_sprintfa (str, "%s\n",
229 sig_status_to_string (status));
230 if ((fpr != NULL) && (*fpr != '\0'))
231 g_string_sprintfa (str, "Key fingerprint: %s\n", fpr);
232 g_string_append (str, _("Cannot find user ID for this key."));
233 sig_expiration_check(str, ctx, key, status, 0);
236 g_string_sprintfa (str, sig_status_with_name (status), uid);
237 g_string_append (str, "\n");
240 uid = gpgme_key_get_string_attr (key, GPGME_ATTR_USERID,
244 g_string_sprintfa (str, _(" aka \"%s\"\n"),
247 sig_expiration_check(str, ctx, key, status, 0);
251 sig_expiration_check(GString *str, GpgmeCtx ctx, GpgmeKey key,
252 GpgmeSigStat status, int idx)
254 if (status == GPGME_SIG_STAT_GOOD_EXP)
255 sig_expired(str, ctx, idx);
256 else if (status == GPGME_SIG_STAT_GOOD_EXPKEY)
257 sig_key_expired(str, key, idx);
261 sig_expired(GString *str, GpgmeCtx ctx, int idx)
263 unsigned long exp_time;
264 exp_time = gpgme_get_sig_ulong_attr(ctx, idx, GPGME_ATTR_EXPIRE, 0);
265 g_string_sprintfa(str, _("Signature expired %s"), ctime(&exp_time));
269 sig_key_expired(GString *str, GpgmeKey key, int idx)
271 unsigned long exp_time;
272 exp_time = gpgme_key_get_ulong_attr(key, GPGME_ATTR_EXPIRE, NULL, idx);
273 g_string_sprintfa(str, _("Key expired %s"), ctime(&exp_time));
277 sig_status_full (GpgmeCtx ctx)
286 struct tm *ctime_val;
290 str = g_string_new ("");
292 fpr = gpgme_get_sig_status (ctx, sig_idx, &status, &created);
293 while (fpr != NULL) {
295 ctime_val = localtime (&created);
296 strftime (ctime_str, sizeof (ctime_str), "%c",
298 g_string_sprintfa (str,
299 _("Signature made at %s\n"),
302 err = gpgme_get_sig_key (ctx, sig_idx, &key);
304 g_string_sprintfa (str, "%s\n",
305 sig_status_to_string (status));
306 if ((fpr != NULL) && (*fpr != '\0'))
307 g_string_sprintfa (str,
308 _("Key fingerprint: %s\n"),
311 const char *key_id_str;
313 sig_status_for_key (str, ctx, status, key, fpr);
314 key_id_str = gpgme_key_get_string_attr(key,
315 GPGME_ATTR_KEYID, NULL, 0);
317 g_string_sprintfa (str, _("Key ID: %s\n"), key_id_str);
318 gpgme_key_unref (key);
320 g_string_append (str, "\n\n");
322 fpr = gpgme_get_sig_status (ctx, ++sig_idx, &status, &created);
326 g_string_free (str, FALSE);
330 static void check_signature (MimeInfo *mimeinfo, MimeInfo *partinfo, FILE *fp)
335 GpgmeData sig = NULL, text = NULL;
336 GpgmeSigStat status = GPGME_SIG_STAT_NONE;
337 GpgmegtkSigStatus statuswindow = NULL;
338 const char *result = NULL;
340 gint n_exclude_chars = 0;
342 if (prefs_common.gpg_signature_popup)
343 statuswindow = gpgmegtk_sig_status_create ();
345 err = gpgme_new (&ctx);
347 debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
351 /* don't include the last empty line.
352 It does not belong to the signed text */
353 if (mimeinfo->children->length > 0) {
354 if (fseek(fp, mimeinfo->children->offset + mimeinfo->children->length - 1,
359 if (fgetc(fp) == '\n') {
361 if (mimeinfo->children->length > 1) {
362 if (fseek(fp, mimeinfo->children->offset + mimeinfo->children->length - 2,
367 if (fgetc(fp) == '\r')
373 /* canonicalize the file part. */
374 tmp_file = get_tmp_file();
375 if (copy_file_part(fp, mimeinfo->children->offset,
376 mimeinfo->children->length - n_exclude_chars,
381 if (canonicalize_file_replace(tmp_file) < 0) {
387 err = gpgme_data_new_from_file(&text, tmp_file, 1);
393 err = gpgme_data_new_from_filepart (&sig, NULL, fp,
394 partinfo->offset, partinfo->length);
396 debug_print ("gpgme_data_new_from_filepart failed: %s\n",
397 gpgme_strerror (err));
401 err = gpgme_op_verify (ctx, sig, text, &status);
403 debug_print ("gpgme_op_verify failed: %s\n", gpgme_strerror (err));
407 /* FIXME: check what the heck this sig_status_full stuff is.
408 * it should better go into sigstatus.c */
409 g_free (partinfo->sigstatus_full);
410 partinfo->sigstatus_full = sig_status_full (ctx);
413 result = gpgmegtk_sig_status_to_string(status);
414 debug_print("verification status: %s\n", result);
415 if (prefs_common.gpg_signature_popup)
416 gpgmegtk_sig_status_update (statuswindow, ctx);
418 g_free (partinfo->sigstatus);
419 partinfo->sigstatus = g_strdup (result);
420 partinfo->sig_ok = (status == GPGME_SIG_STAT_GOOD);
421 partinfo->sig_unknown = (status == GPGME_SIG_STAT_NOKEY);
422 partinfo->sig_expired = (status == GPGME_SIG_STAT_GOOD_EXP);
423 partinfo->key_expired = (status == GPGME_SIG_STAT_GOOD_EXPKEY);
425 gpgme_data_release (sig);
426 gpgme_data_release (text);
428 if (prefs_common.gpg_signature_popup)
429 gpgmegtk_sig_status_destroy (statuswindow);
434 * Copy a gpgme data object to a temporary file and
435 * return this filename
439 copy_gpgmedata_to_temp (GpgmeData data, guint *length)
448 tmp = g_strdup_printf("%s%cgpgtmp.%08x",
449 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id );
451 if ((fp = fopen(tmp, "wb")) == NULL) {
452 FILE_OP_ERROR(tmp, "fopen");
457 err = gpgme_data_rewind ( data );
459 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
461 while (!(err = gpgme_data_read (data, buf, 100, &nread))) {
462 fwrite ( buf, nread, 1, fp );
465 if (err != GPGME_EOF)
466 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
476 pgp_decrypt (MimeInfo *partinfo, FILE *fp)
480 GpgmeData cipher = NULL, plain = NULL;
481 struct passphrase_cb_info_s info;
483 memset (&info, 0, sizeof info);
485 err = gpgme_new (&ctx);
487 debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
491 err = gpgme_data_new_from_filepart (&cipher, NULL, fp,
492 partinfo->offset, partinfo->length);
494 debug_print ("gpgme_data_new_from_filepart failed: %s\n",
495 gpgme_strerror (err));
499 err = gpgme_data_new (&plain);
501 debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
505 if (!getenv("GPG_AGENT_INFO")) {
507 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
510 err = gpgme_op_decrypt (ctx, cipher, plain);
513 gpgme_data_release (cipher);
515 gpgmegtk_free_passphrase();
516 debug_print ("decryption failed: %s\n", gpgme_strerror (err));
517 gpgme_data_release (plain);
521 debug_print ("** decryption succeeded\n");
528 MimeInfo * rfc2015_find_signature (MimeInfo *mimeinfo)
535 if (!((mimeinfo->type == MIMETYPE_MULTIPART) && !g_strcasecmp(mimeinfo->subtype, "signed")))
538 debug_print ("** multipart/signed encountered\n");
540 /* check that we have at least 2 parts of the correct type */
541 for (partinfo = mimeinfo->children;
542 partinfo != NULL; partinfo = partinfo->next) {
543 if (++n > 1 && (partinfo->type == MIMETYPE_APPLICATION &&
544 !g_strcasecmp (partinfo->subtype, "pgp-signature")))
551 gboolean rfc2015_has_signature (MimeInfo *mimeinfo)
553 return rfc2015_find_signature (mimeinfo) != NULL;
556 void rfc2015_check_signature (MimeInfo *mimeinfo, FILE *fp)
560 partinfo = rfc2015_find_signature (mimeinfo);
565 g_message ("** yep, it is a pgp signature");
566 dump_mimeinfo ("gpg-signature", partinfo );
567 dump_part (partinfo, fp );
568 dump_mimeinfo ("signed text", mimeinfo->children );
569 dump_part (mimeinfo->children, fp);
572 check_signature (mimeinfo, partinfo, fp);
576 int rfc2015_is_encrypted (MimeInfo *mimeinfo)
578 if (!mimeinfo || mimeinfo->type != MIMETYPE_MULTIPART)
580 if (g_strcasecmp (mimeinfo->subtype, "encrypted"))
582 /* fixme: we should check the protocol parameter */
586 gboolean rfc2015_msg_is_encrypted (const gchar *file)
593 if ((fp = fopen(file, "rb")) == NULL)
596 mimeinfo = procmime_scan_mime_header(fp);
602 ret = rfc2015_is_encrypted(mimeinfo);
603 procmime_mimeinfo_free_all(mimeinfo);
605 return ret != 0 ? TRUE : FALSE;
609 name_cmp(const char *a, const char *b)
611 for( ; *a && *b; a++, b++) {
613 && toupper(*(unsigned char *)a) != toupper(*(unsigned char *)b))
621 headerp(char *p, char **names)
630 if(p2[-1] == ' ' || p2[-1] == '\t') {
639 for(i = 0 ; names[i] != NULL; i++) {
640 if(!name_cmp (names[i], p))
645 return names[i] != NULL;
649 #define DECRYPTION_ABORT() \
651 procmime_mimeinfo_free_all(tmpinfo); \
652 msginfo->decryption_failed = 1; \
656 void rfc2015_decrypt_message (MsgInfo *msginfo, MimeInfo *mimeinfo, FILE *fp)
659 MimeInfo *tmpinfo, *partinfo;
669 g_return_if_fail (msginfo != NULL);
670 g_return_if_fail (mimeinfo != NULL);
671 g_return_if_fail (fp != NULL);
672 g_return_if_fail (mimeinfo->type == MIMETYPE_MULTIPART);
674 debug_print ("** decrypting multipart/encrypted message\n");
679 if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0)
681 tmpinfo = procmime_scan_mime_header(fp);
682 if (!tmpinfo || tmpinfo->type != MIMETYPE_MULTIPART) {
686 procmime_scan_message(tmpinfo);
688 /* check that we have the 2 parts */
689 partinfo = tmpinfo->children;
690 if (!partinfo || !partinfo->next) {
693 if ((partinfo->type == MIMETYPE_APPLICATION) && !g_strcasecmp(partinfo->subtype, "pgp-encrypted")) {
694 /* Fixme: check that the version is 1 */
697 partinfo = partinfo->next;
698 if (ver_ok && (partinfo->type == MIMETYPE_APPLICATION) &&
699 !g_strcasecmp (partinfo->subtype, "octet-stream")) {
701 g_warning ("oops: pgp_encrypted with more than 2 parts");
707 debug_print ("** yep, it is pgp encrypted\n");
709 plain = pgp_decrypt (partinfo, fp);
714 fname = g_strdup_printf("%s%cplaintext.%08x",
715 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
717 if ((dstfp = fopen(fname, "wb")) == NULL) {
718 FILE_OP_ERROR(fname, "fopen");
723 /* write the orginal header to the new file */
724 if (fseek(fp, tmpinfo->offset, SEEK_SET) < 0)
728 while (fgets(buf, sizeof(buf), fp)) {
729 if (headerp (buf, content_names)) {
734 if (buf[0] == ' ' || buf[0] == '\t')
738 if (buf[0] == '\r' || buf[0] == '\n')
743 err = gpgme_data_rewind (plain);
745 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
747 while (!(err = gpgme_data_read (plain, buf, sizeof(buf), &nread))) {
748 fwrite (buf, nread, 1, dstfp);
751 if (err != GPGME_EOF) {
752 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
756 procmime_mimeinfo_free_all(tmpinfo);
758 msginfo->plaintext_file = fname;
759 msginfo->decryption_failed = 0;
764 #undef DECRYPTION_ABORT
768 * plain contains an entire mime object.
769 * Encrypt it and return an GpgmeData object with the encrypted version of
770 * the file or NULL in case of error.
773 pgp_encrypt ( GpgmeData plain, GpgmeRecipients rset )
777 GpgmeData cipher = NULL;
779 err = gpgme_new (&ctx);
781 err = gpgme_data_new (&cipher);
783 gpgme_set_armor (ctx, 1);
784 err = gpgme_op_encrypt (ctx, rset, plain, cipher);
788 debug_print ("encryption failed: %s\n", gpgme_strerror (err));
789 gpgme_data_release (cipher);
793 debug_print ("** encryption succeeded\n");
801 * Create and return a list of keys matching a key id
804 GSList *rfc2015_create_signers_list (const char *keyid)
806 GSList *key_list = NULL;
807 GpgmeCtx list_ctx = NULL;
812 err = gpgme_new (&list_ctx);
815 err = gpgme_op_keylist_start (list_ctx, keyid, 1);
818 while ( !(err = gpgme_op_keylist_next (list_ctx, &key)) ) {
819 key_list = g_slist_append (key_list, key);
821 if (err != GPGME_EOF)
824 if (key_list == NULL) {
825 debug_print ("no keys found for keyid \"%s\"\n", keyid);
830 debug_print ("rfc2015_create_signers_list failed: %s\n", gpgme_strerror (err));
831 for (p = key_list; p != NULL; p = p->next)
832 gpgme_key_unref ((GpgmeKey) p->data);
833 g_slist_free (key_list);
836 gpgme_release (list_ctx);
837 return err ? NULL : key_list;
841 * Encrypt the file by extracting all recipients and finding the
842 * encryption keys for all of them. The file content is then replaced
843 * by the encrypted one. */
845 rfc2015_encrypt (const char *file, GSList *recp_list, gboolean ascii_armored)
849 int i, clineidx, saved_last;
850 char *clines[3] = {NULL};
852 GpgmeData header = NULL;
853 GpgmeData plain = NULL;
854 GpgmeData cipher = NULL;
855 GpgmeRecipients rset = NULL;
857 int mime_version_seen = 0;
860 boundary = generate_mime_boundary ("Encrypt");
862 /* Create the list of recipients */
863 rset = gpgmegtk_recipient_selection (recp_list);
865 debug_print ("error creating recipient list\n" );
869 /* Open the source file */
870 if ((fp = fopen(file, "rb")) == NULL) {
871 FILE_OP_ERROR(file, "fopen");
875 err = gpgme_data_new (&header);
877 err = gpgme_data_new (&plain);
879 debug_print ("gpgme_data_new failed: %s\n", gpgme_strerror (err));
883 /* get the content header lines from the source */
886 while (!err && fgets(buf, sizeof(buf), fp)) {
887 /* fixme: check for overlong lines */
888 if (headerp (buf, content_names)) {
889 if (clineidx >= DIM (clines)) {
890 debug_print ("rfc2015_encrypt: too many content lines\n");
893 clines[clineidx++] = g_strdup (buf);
898 if (*buf == ' ' || *buf == '\t') {
899 char *last = clines[clineidx - 1];
900 clines[clineidx - 1] = g_strconcat (last, buf, NULL);
907 if (headerp (buf, mime_version_name))
908 mime_version_seen = 1;
910 if (buf[0] == '\r' || buf[0] == '\n')
912 err = gpgme_data_write (header, buf, strlen (buf));
915 FILE_OP_ERROR (file, "fgets");
919 /* write them to the temp data and add the rest of the message */
920 for (i = 0; !err && i < clineidx; i++) {
921 debug_print ("%% %s:%d: cline=`%s'", __FILE__ ,__LINE__, clines[i]);
922 err = gpgme_data_write (plain, clines[i], strlen (clines[i]));
925 err = gpgme_data_write (plain, "\r\n", 2);
926 while (!err && fgets(buf, sizeof(buf), fp)) {
927 err = gpgme_data_write (plain, buf, strlen (buf));
930 FILE_OP_ERROR (file, "fgets");
934 debug_print ("gpgme_data_write failed: %s\n", gpgme_strerror (err));
938 cipher = pgp_encrypt (plain, rset);
939 gpgme_data_release (plain); plain = NULL;
940 gpgme_recipients_release (rset); rset = NULL;
944 /* we have the encrypted message available in cipher and now we
945 * are going to rewrite the source file. To be sure that file has
946 * been truncated we use an approach which should work everywhere:
947 * close the file and then reopen it for writing. It is important
948 * that this works, otherwise it may happen that parts of the
949 * plaintext are still in the file (The encrypted stuff is, due to
950 * compression, usually shorter than the plaintext).
952 * Yes, there is a race condition here, but everyone, who is so
953 * stupid to store the temp file with the plaintext in a public
954 * directory has to live with this anyway. */
956 FILE_OP_ERROR(file, "fclose");
959 if ((fp = fopen(file, "wb")) == NULL) {
960 FILE_OP_ERROR(file, "fopen");
964 /* Write the header, append new content lines, part 1 and part 2 header */
965 err = gpgme_data_rewind (header);
967 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
970 while (!(err = gpgme_data_read (header, buf, BUFFSIZE, &nread))) {
971 fwrite (buf, nread, 1, fp);
973 if (err != GPGME_EOF) {
974 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
978 FILE_OP_ERROR (file, "fwrite");
981 gpgme_data_release (header); header = NULL;
983 if (!mime_version_seen)
984 fputs ("MIME-Version: 1\r\n", fp);
988 "Content-Type: text/plain; charset=US-ASCII\r\n"
989 "Content-Transfer-Encoding: 7bit\r\n"
993 "Content-Type: multipart/encrypted;"
994 " protocol=\"application/pgp-encrypted\";\r\n"
995 " boundary=\"%s\"\r\n"
998 "Content-Type: application/pgp-encrypted\r\n"
1003 "Content-Type: application/octet-stream\r\n"
1005 boundary, boundary, boundary);
1008 /* append the encrypted stuff */
1009 err = gpgme_data_rewind (cipher);
1011 debug_print ("** gpgme_data_rewind on cipher failed: %s\n",
1012 gpgme_strerror (err));
1016 while (!(err = gpgme_data_read (cipher, buf, BUFFSIZE, &nread))) {
1017 fwrite (buf, nread, 1, fp);
1019 if (err != GPGME_EOF) {
1020 debug_print ("** gpgme_data_read failed: %s\n", gpgme_strerror (err));
1024 /* and the final boundary */
1025 if (!ascii_armored) {
1033 FILE_OP_ERROR (file, "fwrite");
1037 gpgme_data_release (cipher);
1043 gpgme_data_release (header);
1044 gpgme_data_release (plain);
1045 gpgme_data_release (cipher);
1046 gpgme_recipients_release (rset);
1048 return -1; /* error */
1052 * plain contains an entire mime object. Sign it and return an
1053 * GpgmeData object with the signature of it or NULL in case of error.
1054 * r_siginfo returns an XML object with information about the signature.
1057 pgp_sign (GpgmeData plain, GSList *key_list, gboolean clearsign,
1061 GpgmeCtx ctx = NULL;
1063 GpgmeData sig = NULL;
1064 struct passphrase_cb_info_s info;
1067 memset (&info, 0, sizeof info);
1069 err = gpgme_new (&ctx);
1072 err = gpgme_data_new (&sig);
1076 if (!getenv("GPG_AGENT_INFO")) {
1078 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
1080 gpgme_set_textmode (ctx, 1);
1081 gpgme_set_armor (ctx, 1);
1082 gpgme_signers_clear (ctx);
1083 for (p = key_list; p != NULL; p = p->next) {
1084 err = gpgme_signers_add (ctx, (GpgmeKey) p->data);
1088 for (p = key_list; p != NULL; p = p->next)
1089 gpgme_key_unref ((GpgmeKey) p->data);
1090 g_slist_free (key_list);
1096 clearsign ? GPGME_SIG_MODE_CLEAR : GPGME_SIG_MODE_DETACH);
1098 *r_siginfo = gpgme_get_op_info (ctx, 0);
1102 gpgmegtk_free_passphrase();
1103 debug_print ("signing failed: %s\n", gpgme_strerror (err));
1104 gpgme_data_release (sig);
1108 debug_print ("signing succeeded\n");
1111 gpgme_release (ctx);
1116 * Find TAG in XML and return a pointer into xml set just behind the
1117 * closing angle. Return NULL if not found.
1120 find_xml_tag (const char *xml, const char *tag)
1122 int taglen = strlen (tag);
1123 const char *s = xml;
1125 while ( (s = strchr (s, '<')) ) {
1127 if (!strncmp (s, tag, taglen)) {
1128 const char *s2 = s + taglen;
1129 if (*s2 == '>' || isspace (*(const unsigned char*)s2) ) {
1131 while (*s2 && *s2 != '>') /* skip attributes */
1133 /* fixme: do need to handle angles inside attribute vallues? */
1134 return *s2? (s2+1):NULL;
1137 while (*s && *s != '>') /* skip to end of tag */
1145 * Extract the micalg from an GnupgOperationInfo XML container.
1148 extract_micalg (char *xml)
1152 s = find_xml_tag (xml, "GnupgOperationInfo");
1154 const char *s_end = find_xml_tag (s, "/GnupgOperationInfo");
1155 s = find_xml_tag (s, "signature");
1156 if (s && s_end && s < s_end) {
1157 const char *s_end2 = find_xml_tag (s, "/signature");
1158 if (s_end2 && s_end2 < s_end) {
1159 s = find_xml_tag (s, "micalg");
1160 if (s && s < s_end2) {
1161 s_end = strchr (s, '<');
1163 char *p = g_malloc (s_end - s + 1);
1164 memcpy (p, s, s_end - s);
1177 * Sign the file and replace its content with the signed one.
1180 rfc2015_sign (const char *file, GSList *key_list)
1184 int i, clineidx, saved_last;
1185 char *clines[3] = {NULL};
1187 GpgmeData header = NULL;
1188 GpgmeData plain = NULL;
1189 GpgmeData sigdata = NULL;
1191 int mime_version_seen = 0;
1193 char *micalg = NULL;
1196 boundary = generate_mime_boundary ("Signature");
1198 /* Open the source file */
1199 if ((fp = fopen(file, "rb")) == NULL) {
1200 FILE_OP_ERROR(file, "fopen");
1204 err = gpgme_data_new (&header);
1206 err = gpgme_data_new (&plain);
1208 debug_print ("gpgme_data_new failed: %s\n", gpgme_strerror (err));
1212 /* get the content header lines from the source */
1215 while (!err && fgets(buf, sizeof(buf), fp)) {
1216 /* fixme: check for overlong lines */
1217 if (headerp (buf, content_names)) {
1218 if (clineidx >= DIM (clines)) {
1219 debug_print ("rfc2015_sign: too many content lines\n");
1222 clines[clineidx++] = g_strdup (buf);
1227 if (*buf == ' ' || *buf == '\t') {
1228 char *last = clines[clineidx - 1];
1229 clines[clineidx - 1] = g_strconcat (last, buf, NULL);
1236 if (headerp (buf, mime_version_name))
1237 mime_version_seen = 1;
1239 if (buf[0] == '\r' || buf[0] == '\n')
1241 err = gpgme_data_write (header, buf, strlen (buf));
1244 FILE_OP_ERROR (file, "fgets");
1248 /* write them to the temp data and add the rest of the message */
1249 for (i = 0; !err && i < clineidx; i++) {
1250 err = gpgme_data_write (plain, clines[i], strlen (clines[i]));
1253 err = gpgme_data_write (plain, "\r\n", 2 );
1254 while (!err && fgets(buf, sizeof(buf), fp)) {
1255 err = gpgme_data_write (plain, buf, strlen (buf));
1258 FILE_OP_ERROR (file, "fgets");
1262 debug_print ("gpgme_data_write failed: %s\n", gpgme_strerror (err));
1266 sigdata = pgp_sign (plain, key_list, FALSE, &siginfo);
1268 micalg = extract_micalg (siginfo);
1274 /* we have the signed message available in sigdata and now we are
1275 * going to rewrite the original file. To be sure that file has
1276 * been truncated we use an approach which should work everywhere:
1277 * close the file and then reopen it for writing. */
1279 FILE_OP_ERROR(file, "fclose");
1282 if ((fp = fopen(file, "wb")) == NULL) {
1283 FILE_OP_ERROR(file, "fopen");
1287 /* Write the rfc822 header and add new content lines */
1288 err = gpgme_data_rewind (header);
1290 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
1291 while (!(err = gpgme_data_read (header, buf, BUFFSIZE, &nread))) {
1292 fwrite (buf, nread, 1, fp);
1294 if (err != GPGME_EOF) {
1295 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
1299 FILE_OP_ERROR (file, "fwrite");
1302 gpgme_data_release (header);
1305 if (!mime_version_seen)
1306 fputs ("MIME-Version: 1.0\r\n", fp);
1307 fprintf (fp, "Content-Type: multipart/signed; "
1308 "protocol=\"application/pgp-signature\";\r\n");
1310 fprintf (fp, " micalg=\"%s\";\r\n", micalg);
1311 fprintf (fp, " boundary=\"%s\"\r\n", boundary);
1313 /* Part 1: signed material */
1317 err = gpgme_data_rewind (plain);
1319 debug_print ("gpgme_data_rewind on plain failed: %s\n",
1320 gpgme_strerror (err));
1323 while (!(err = gpgme_data_read (plain, buf, BUFFSIZE, &nread))) {
1324 fwrite (buf, nread, 1, fp);
1326 if (err != GPGME_EOF) {
1327 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
1331 /* Part 2: signature */
1335 fputs ("Content-Type: application/pgp-signature\r\n"
1338 err = gpgme_data_rewind (sigdata);
1340 debug_print ("gpgme_data_rewind on sigdata failed: %s\n",
1341 gpgme_strerror (err));
1345 while (!(err = gpgme_data_read (sigdata, buf, BUFFSIZE, &nread))) {
1346 fwrite (buf, nread, 1, fp);
1348 if (err != GPGME_EOF) {
1349 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
1353 /* Final boundary */
1359 FILE_OP_ERROR (file, "fwrite");
1363 gpgme_data_release (header);
1364 gpgme_data_release (plain);
1365 gpgme_data_release (sigdata);
1373 gpgme_data_release (header);
1374 gpgme_data_release (plain);
1375 gpgme_data_release (sigdata);
1378 return -1; /* error */
1383 * Sign the file with clear text and replace its content with the signed one.
1386 rfc2015_clearsign (const gchar *file, GSList *key_list)
1389 gchar buf[BUFFSIZE];
1391 GpgmeData text = NULL;
1392 GpgmeData sigdata = NULL;
1396 if ((fp = fopen(file, "rb")) == NULL) {
1397 FILE_OP_ERROR(file, "fopen");
1401 err = gpgme_data_new(&text);
1403 debug_print("gpgme_data_new failed: %s\n", gpgme_strerror(err));
1407 while (!err && fgets(buf, sizeof(buf), fp)) {
1408 err = gpgme_data_write(text, buf, strlen(buf));
1411 FILE_OP_ERROR(file, "fgets");
1415 debug_print("gpgme_data_write failed: %s\n", gpgme_strerror(err));
1419 sigdata = pgp_sign(text, key_list, TRUE, &siginfo);
1426 if (fclose(fp) == EOF) {
1427 FILE_OP_ERROR(file, "fclose");
1431 if ((fp = fopen(file, "wb")) == NULL) {
1432 FILE_OP_ERROR(file, "fopen");
1436 err = gpgme_data_rewind(sigdata);
1438 debug_print("gpgme_data_rewind on sigdata failed: %s\n",
1439 gpgme_strerror(err));
1443 while (!(err = gpgme_data_read(sigdata, buf, sizeof(buf), &nread))) {
1444 fwrite(buf, nread, 1, fp);
1446 if (err != GPGME_EOF) {
1447 debug_print("gpgme_data_read failed: %s\n", gpgme_strerror(err));
1451 if (fclose(fp) == EOF) {
1452 FILE_OP_ERROR(file, "fclose");
1456 gpgme_data_release(text);
1457 gpgme_data_release(sigdata);
1463 gpgme_data_release(text);
1464 gpgme_data_release(sigdata);
1468 static gboolean rfc2015_is_signed(MimeInfo *mimeinfo)
1471 MimeInfo *signature;
1474 g_return_val_if_fail(mimeinfo != NULL, FALSE);
1477 parent = procmime_mimeinfo_parent(mimeinfo);
1480 if ((parent->type != MIMETYPE_MULTIPART) ||
1481 g_strcasecmp(parent->subtype, "signed"))
1483 protocol = g_hash_table_lookup(parent->parameters, "protocol");
1484 if ((protocol == NULL) || g_strcasecmp(protocol, "application/pgp-signature"))
1487 /* check if mimeinfo is the first child */
1488 if (parent->node->children->data != mimeinfo)
1491 /* check signature */
1492 signature = parent->node->children->next != NULL ?
1493 (MimeInfo *) parent->node->children->next->data : NULL;
1494 if (signature == NULL)
1496 if ((signature->type != MIMETYPE_APPLICATION) ||
1497 g_strcasecmp(signature->subtype, "pgp-signature"))
1503 static void idle_function_for_gpgme(void)
1505 while (gtk_events_pending())
1506 gtk_main_iteration();
1509 static PrivacySystem rfc2015_system = {
1510 "PGP/Mime", /* name */
1512 g_free, /* free_privacydata */
1514 rfc2015_is_signed, /* is_signed(MimeInfo *) */
1515 NULL, /* get_signer(MimeInfo *) */
1516 NULL, /* check_signature(MimeInfo *) */
1519 NULL, /* is_encrypted(MimeInfo *) */
1520 NULL, /* decrypt(MimeInfo *) */
1525 if (gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP) !=
1526 GPGME_No_Error) { /* Also does some gpgme init */
1527 rfc2015_disable_all();
1528 debug_print("gpgme_engine_version:\n%s\n",
1529 gpgme_get_engine_info());
1531 if (prefs_common.gpg_warning) {
1534 val = alertpanel_message_with_disable
1536 _("GnuPG is not installed properly, or needs to be upgraded.\n"
1537 "OpenPGP support disabled."));
1538 if (val & G_ALERTDISABLE)
1539 prefs_common.gpg_warning = FALSE;
1542 privacy_register_system(&rfc2015_system);
1544 gpgme_register_idle(idle_function_for_gpgme);
1549 privacy_unregister_system(&rfc2015_system);
1550 gpgmegtk_free_passphrase();
1553 #endif /* USE_GPGME */