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");
527 MimeInfo * rfc2015_find_signature (MimeInfo *mimeinfo)
534 if (!((mimeinfo->type == MIMETYPE_MULTIPART) && !g_strcasecmp(mimeinfo->subtype, "signed")))
537 debug_print ("** multipart/signed encountered\n");
539 /* check that we have at least 2 parts of the correct type */
540 for (partinfo = mimeinfo->children;
541 partinfo != NULL; partinfo = partinfo->next) {
542 if (++n > 1 && (partinfo->type == MIMETYPE_APPLICATION &&
543 !g_strcasecmp (partinfo->subtype, "pgp-signature")))
550 gboolean rfc2015_has_signature (MimeInfo *mimeinfo)
552 return rfc2015_find_signature (mimeinfo) != NULL;
555 void rfc2015_check_signature (MimeInfo *mimeinfo, FILE *fp)
559 partinfo = rfc2015_find_signature (mimeinfo);
564 g_message ("** yep, it is a pgp signature");
565 dump_mimeinfo ("gpg-signature", partinfo );
566 dump_part (partinfo, fp );
567 dump_mimeinfo ("signed text", mimeinfo->children );
568 dump_part (mimeinfo->children, fp);
571 check_signature (mimeinfo, partinfo, fp);
574 int rfc2015_is_encrypted (MimeInfo *mimeinfo)
576 if (!mimeinfo || mimeinfo->type != MIMETYPE_MULTIPART)
578 if (g_strcasecmp (mimeinfo->subtype, "encrypted"))
580 /* fixme: we should check the protocol parameter */
584 gboolean rfc2015_msg_is_encrypted (const gchar *file)
591 if ((fp = fopen(file, "rb")) == NULL)
594 mimeinfo = procmime_scan_mime_header(fp);
600 ret = rfc2015_is_encrypted(mimeinfo);
601 procmime_mimeinfo_free_all(mimeinfo);
603 return ret != 0 ? TRUE : FALSE;
607 name_cmp(const char *a, const char *b)
609 for( ; *a && *b; a++, b++) {
611 && toupper(*(unsigned char *)a) != toupper(*(unsigned char *)b))
619 headerp(char *p, char **names)
628 if(p2[-1] == ' ' || p2[-1] == '\t') {
637 for(i = 0 ; names[i] != NULL; i++) {
638 if(!name_cmp (names[i], p))
643 return names[i] != NULL;
647 #define DECRYPTION_ABORT() \
649 procmime_mimeinfo_free_all(tmpinfo); \
650 msginfo->decryption_failed = 1; \
654 void rfc2015_decrypt_message (MsgInfo *msginfo, MimeInfo *mimeinfo, FILE *fp)
657 MimeInfo *tmpinfo, *partinfo;
667 g_return_if_fail (msginfo != NULL);
668 g_return_if_fail (mimeinfo != NULL);
669 g_return_if_fail (fp != NULL);
670 g_return_if_fail (mimeinfo->type == MIMETYPE_MULTIPART);
672 debug_print ("** decrypting multipart/encrypted message\n");
677 if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0)
679 tmpinfo = procmime_scan_mime_header(fp);
680 if (!tmpinfo || tmpinfo->type != MIMETYPE_MULTIPART) {
684 procmime_scan_message(tmpinfo);
686 /* check that we have the 2 parts */
687 partinfo = tmpinfo->children;
688 if (!partinfo || !partinfo->next) {
691 if ((partinfo->type == MIMETYPE_APPLICATION) && !g_strcasecmp(partinfo->subtype, "pgp-encrypted")) {
692 /* Fixme: check that the version is 1 */
695 partinfo = partinfo->next;
696 if (ver_ok && (partinfo->type == MIMETYPE_APPLICATION) &&
697 !g_strcasecmp (partinfo->subtype, "octet-stream")) {
699 g_warning ("oops: pgp_encrypted with more than 2 parts");
705 debug_print ("** yep, it is pgp encrypted\n");
707 plain = pgp_decrypt (partinfo, fp);
712 fname = g_strdup_printf("%s%cplaintext.%08x",
713 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
715 if ((dstfp = fopen(fname, "wb")) == NULL) {
716 FILE_OP_ERROR(fname, "fopen");
721 /* write the orginal header to the new file */
722 if (fseek(fp, tmpinfo->offset, SEEK_SET) < 0)
726 while (fgets(buf, sizeof(buf), fp)) {
727 if (headerp (buf, content_names)) {
732 if (buf[0] == ' ' || buf[0] == '\t')
736 if (buf[0] == '\r' || buf[0] == '\n')
741 err = gpgme_data_rewind (plain);
743 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
745 while (!(err = gpgme_data_read (plain, buf, sizeof(buf), &nread))) {
746 fwrite (buf, nread, 1, dstfp);
749 if (err != GPGME_EOF) {
750 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
754 procmime_mimeinfo_free_all(tmpinfo);
756 msginfo->plaintext_file = fname;
757 msginfo->decryption_failed = 0;
762 #undef DECRYPTION_ABORT
766 * plain contains an entire mime object.
767 * Encrypt it and return an GpgmeData object with the encrypted version of
768 * the file or NULL in case of error.
771 pgp_encrypt ( GpgmeData plain, GpgmeRecipients rset )
775 GpgmeData cipher = NULL;
777 err = gpgme_new (&ctx);
779 err = gpgme_data_new (&cipher);
781 gpgme_set_armor (ctx, 1);
782 err = gpgme_op_encrypt (ctx, rset, plain, cipher);
786 debug_print ("encryption failed: %s\n", gpgme_strerror (err));
787 gpgme_data_release (cipher);
791 debug_print ("** encryption succeeded\n");
799 * Create and return a list of keys matching a key id
802 GSList *rfc2015_create_signers_list (const char *keyid)
804 GSList *key_list = NULL;
805 GpgmeCtx list_ctx = NULL;
810 err = gpgme_new (&list_ctx);
813 err = gpgme_op_keylist_start (list_ctx, keyid, 1);
816 while ( !(err = gpgme_op_keylist_next (list_ctx, &key)) ) {
817 key_list = g_slist_append (key_list, key);
819 if (err != GPGME_EOF)
822 if (key_list == NULL) {
823 debug_print ("no keys found for keyid \"%s\"\n", keyid);
828 debug_print ("rfc2015_create_signers_list failed: %s\n", gpgme_strerror (err));
829 for (p = key_list; p != NULL; p = p->next)
830 gpgme_key_unref ((GpgmeKey) p->data);
831 g_slist_free (key_list);
834 gpgme_release (list_ctx);
835 return err ? NULL : key_list;
839 * Encrypt the file by extracting all recipients and finding the
840 * encryption keys for all of them. The file content is then replaced
841 * by the encrypted one. */
843 rfc2015_encrypt (const char *file, GSList *recp_list, gboolean ascii_armored)
847 int i, clineidx, saved_last;
848 char *clines[3] = {NULL};
850 GpgmeData header = NULL;
851 GpgmeData plain = NULL;
852 GpgmeData cipher = NULL;
853 GpgmeRecipients rset = NULL;
855 int mime_version_seen = 0;
858 boundary = generate_mime_boundary ("Encrypt");
860 /* Create the list of recipients */
861 rset = gpgmegtk_recipient_selection (recp_list);
863 debug_print ("error creating recipient list\n" );
867 /* Open the source file */
868 if ((fp = fopen(file, "rb")) == NULL) {
869 FILE_OP_ERROR(file, "fopen");
873 err = gpgme_data_new (&header);
875 err = gpgme_data_new (&plain);
877 debug_print ("gpgme_data_new failed: %s\n", gpgme_strerror (err));
881 /* get the content header lines from the source */
884 while (!err && fgets(buf, sizeof(buf), fp)) {
885 /* fixme: check for overlong lines */
886 if (headerp (buf, content_names)) {
887 if (clineidx >= DIM (clines)) {
888 debug_print ("rfc2015_encrypt: too many content lines\n");
891 clines[clineidx++] = g_strdup (buf);
896 if (*buf == ' ' || *buf == '\t') {
897 char *last = clines[clineidx - 1];
898 clines[clineidx - 1] = g_strconcat (last, buf, NULL);
905 if (headerp (buf, mime_version_name))
906 mime_version_seen = 1;
908 if (buf[0] == '\r' || buf[0] == '\n')
910 err = gpgme_data_write (header, buf, strlen (buf));
913 FILE_OP_ERROR (file, "fgets");
917 /* write them to the temp data and add the rest of the message */
918 for (i = 0; !err && i < clineidx; i++) {
919 debug_print ("%% %s:%d: cline=`%s'", __FILE__ ,__LINE__, clines[i]);
920 err = gpgme_data_write (plain, clines[i], strlen (clines[i]));
923 err = gpgme_data_write (plain, "\r\n", 2);
924 while (!err && fgets(buf, sizeof(buf), fp)) {
925 err = gpgme_data_write (plain, buf, strlen (buf));
928 FILE_OP_ERROR (file, "fgets");
932 debug_print ("gpgme_data_write failed: %s\n", gpgme_strerror (err));
936 cipher = pgp_encrypt (plain, rset);
937 gpgme_data_release (plain); plain = NULL;
938 gpgme_recipients_release (rset); rset = NULL;
942 /* we have the encrypted message available in cipher and now we
943 * are going to rewrite the source file. To be sure that file has
944 * been truncated we use an approach which should work everywhere:
945 * close the file and then reopen it for writing. It is important
946 * that this works, otherwise it may happen that parts of the
947 * plaintext are still in the file (The encrypted stuff is, due to
948 * compression, usually shorter than the plaintext).
950 * Yes, there is a race condition here, but everyone, who is so
951 * stupid to store the temp file with the plaintext in a public
952 * directory has to live with this anyway. */
954 FILE_OP_ERROR(file, "fclose");
957 if ((fp = fopen(file, "wb")) == NULL) {
958 FILE_OP_ERROR(file, "fopen");
962 /* Write the header, append new content lines, part 1 and part 2 header */
963 err = gpgme_data_rewind (header);
965 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
968 while (!(err = gpgme_data_read (header, buf, BUFFSIZE, &nread))) {
969 fwrite (buf, nread, 1, fp);
971 if (err != GPGME_EOF) {
972 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
976 FILE_OP_ERROR (file, "fwrite");
979 gpgme_data_release (header); header = NULL;
981 if (!mime_version_seen)
982 fputs ("MIME-Version: 1\r\n", fp);
986 "Content-Type: text/plain; charset=US-ASCII\r\n"
987 "Content-Transfer-Encoding: 7bit\r\n"
991 "Content-Type: multipart/encrypted;"
992 " protocol=\"application/pgp-encrypted\";\r\n"
993 " boundary=\"%s\"\r\n"
996 "Content-Type: application/pgp-encrypted\r\n"
1001 "Content-Type: application/octet-stream\r\n"
1003 boundary, boundary, boundary);
1006 /* append the encrypted stuff */
1007 err = gpgme_data_rewind (cipher);
1009 debug_print ("** gpgme_data_rewind on cipher failed: %s\n",
1010 gpgme_strerror (err));
1014 while (!(err = gpgme_data_read (cipher, buf, BUFFSIZE, &nread))) {
1015 fwrite (buf, nread, 1, fp);
1017 if (err != GPGME_EOF) {
1018 debug_print ("** gpgme_data_read failed: %s\n", gpgme_strerror (err));
1022 /* and the final boundary */
1023 if (!ascii_armored) {
1031 FILE_OP_ERROR (file, "fwrite");
1035 gpgme_data_release (cipher);
1041 gpgme_data_release (header);
1042 gpgme_data_release (plain);
1043 gpgme_data_release (cipher);
1044 gpgme_recipients_release (rset);
1046 return -1; /* error */
1050 * plain contains an entire mime object. Sign it and return an
1051 * GpgmeData object with the signature of it or NULL in case of error.
1052 * r_siginfo returns an XML object with information about the signature.
1055 pgp_sign (GpgmeData plain, GSList *key_list, gboolean clearsign,
1059 GpgmeCtx ctx = NULL;
1061 GpgmeData sig = NULL;
1062 struct passphrase_cb_info_s info;
1065 memset (&info, 0, sizeof info);
1067 err = gpgme_new (&ctx);
1070 err = gpgme_data_new (&sig);
1074 if (!getenv("GPG_AGENT_INFO")) {
1076 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
1078 gpgme_set_textmode (ctx, 1);
1079 gpgme_set_armor (ctx, 1);
1080 gpgme_signers_clear (ctx);
1081 for (p = key_list; p != NULL; p = p->next) {
1082 err = gpgme_signers_add (ctx, (GpgmeKey) p->data);
1086 for (p = key_list; p != NULL; p = p->next)
1087 gpgme_key_unref ((GpgmeKey) p->data);
1088 g_slist_free (key_list);
1094 clearsign ? GPGME_SIG_MODE_CLEAR : GPGME_SIG_MODE_DETACH);
1096 *r_siginfo = gpgme_get_op_info (ctx, 0);
1100 gpgmegtk_free_passphrase();
1101 debug_print ("signing failed: %s\n", gpgme_strerror (err));
1102 gpgme_data_release (sig);
1106 debug_print ("signing succeeded\n");
1109 gpgme_release (ctx);
1114 * Find TAG in XML and return a pointer into xml set just behind the
1115 * closing angle. Return NULL if not found.
1118 find_xml_tag (const char *xml, const char *tag)
1120 int taglen = strlen (tag);
1121 const char *s = xml;
1123 while ( (s = strchr (s, '<')) ) {
1125 if (!strncmp (s, tag, taglen)) {
1126 const char *s2 = s + taglen;
1127 if (*s2 == '>' || isspace (*(const unsigned char*)s2) ) {
1129 while (*s2 && *s2 != '>') /* skip attributes */
1131 /* fixme: do need to handle angles inside attribute vallues? */
1132 return *s2? (s2+1):NULL;
1135 while (*s && *s != '>') /* skip to end of tag */
1143 * Extract the micalg from an GnupgOperationInfo XML container.
1146 extract_micalg (char *xml)
1150 s = find_xml_tag (xml, "GnupgOperationInfo");
1152 const char *s_end = find_xml_tag (s, "/GnupgOperationInfo");
1153 s = find_xml_tag (s, "signature");
1154 if (s && s_end && s < s_end) {
1155 const char *s_end2 = find_xml_tag (s, "/signature");
1156 if (s_end2 && s_end2 < s_end) {
1157 s = find_xml_tag (s, "micalg");
1158 if (s && s < s_end2) {
1159 s_end = strchr (s, '<');
1161 char *p = g_malloc (s_end - s + 1);
1162 memcpy (p, s, s_end - s);
1175 * Sign the file and replace its content with the signed one.
1178 rfc2015_sign (const char *file, GSList *key_list)
1182 int i, clineidx, saved_last;
1183 char *clines[3] = {NULL};
1185 GpgmeData header = NULL;
1186 GpgmeData plain = NULL;
1187 GpgmeData sigdata = NULL;
1189 int mime_version_seen = 0;
1191 char *micalg = NULL;
1194 boundary = generate_mime_boundary ("Signature");
1196 /* Open the source file */
1197 if ((fp = fopen(file, "rb")) == NULL) {
1198 FILE_OP_ERROR(file, "fopen");
1202 err = gpgme_data_new (&header);
1204 err = gpgme_data_new (&plain);
1206 debug_print ("gpgme_data_new failed: %s\n", gpgme_strerror (err));
1210 /* get the content header lines from the source */
1213 while (!err && fgets(buf, sizeof(buf), fp)) {
1214 /* fixme: check for overlong lines */
1215 if (headerp (buf, content_names)) {
1216 if (clineidx >= DIM (clines)) {
1217 debug_print ("rfc2015_sign: too many content lines\n");
1220 clines[clineidx++] = g_strdup (buf);
1225 if (*buf == ' ' || *buf == '\t') {
1226 char *last = clines[clineidx - 1];
1227 clines[clineidx - 1] = g_strconcat (last, buf, NULL);
1234 if (headerp (buf, mime_version_name))
1235 mime_version_seen = 1;
1237 if (buf[0] == '\r' || buf[0] == '\n')
1239 err = gpgme_data_write (header, buf, strlen (buf));
1242 FILE_OP_ERROR (file, "fgets");
1246 /* write them to the temp data and add the rest of the message */
1247 for (i = 0; !err && i < clineidx; i++) {
1248 err = gpgme_data_write (plain, clines[i], strlen (clines[i]));
1251 err = gpgme_data_write (plain, "\r\n", 2 );
1252 while (!err && fgets(buf, sizeof(buf), fp)) {
1253 err = gpgme_data_write (plain, buf, strlen (buf));
1256 FILE_OP_ERROR (file, "fgets");
1260 debug_print ("gpgme_data_write failed: %s\n", gpgme_strerror (err));
1264 sigdata = pgp_sign (plain, key_list, FALSE, &siginfo);
1266 micalg = extract_micalg (siginfo);
1272 /* we have the signed message available in sigdata and now we are
1273 * going to rewrite the original file. To be sure that file has
1274 * been truncated we use an approach which should work everywhere:
1275 * close the file and then reopen it for writing. */
1277 FILE_OP_ERROR(file, "fclose");
1280 if ((fp = fopen(file, "wb")) == NULL) {
1281 FILE_OP_ERROR(file, "fopen");
1285 /* Write the rfc822 header and add new content lines */
1286 err = gpgme_data_rewind (header);
1288 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
1289 while (!(err = gpgme_data_read (header, buf, BUFFSIZE, &nread))) {
1290 fwrite (buf, nread, 1, fp);
1292 if (err != GPGME_EOF) {
1293 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
1297 FILE_OP_ERROR (file, "fwrite");
1300 gpgme_data_release (header);
1303 if (!mime_version_seen)
1304 fputs ("MIME-Version: 1.0\r\n", fp);
1305 fprintf (fp, "Content-Type: multipart/signed; "
1306 "protocol=\"application/pgp-signature\";\r\n");
1308 fprintf (fp, " micalg=\"%s\";\r\n", micalg);
1309 fprintf (fp, " boundary=\"%s\"\r\n", boundary);
1311 /* Part 1: signed material */
1315 err = gpgme_data_rewind (plain);
1317 debug_print ("gpgme_data_rewind on plain failed: %s\n",
1318 gpgme_strerror (err));
1321 while (!(err = gpgme_data_read (plain, buf, BUFFSIZE, &nread))) {
1322 fwrite (buf, nread, 1, fp);
1324 if (err != GPGME_EOF) {
1325 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
1329 /* Part 2: signature */
1333 fputs ("Content-Type: application/pgp-signature\r\n"
1336 err = gpgme_data_rewind (sigdata);
1338 debug_print ("gpgme_data_rewind on sigdata failed: %s\n",
1339 gpgme_strerror (err));
1343 while (!(err = gpgme_data_read (sigdata, buf, BUFFSIZE, &nread))) {
1344 fwrite (buf, nread, 1, fp);
1346 if (err != GPGME_EOF) {
1347 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
1351 /* Final boundary */
1357 FILE_OP_ERROR (file, "fwrite");
1361 gpgme_data_release (header);
1362 gpgme_data_release (plain);
1363 gpgme_data_release (sigdata);
1371 gpgme_data_release (header);
1372 gpgme_data_release (plain);
1373 gpgme_data_release (sigdata);
1376 return -1; /* error */
1381 * Sign the file with clear text and replace its content with the signed one.
1384 rfc2015_clearsign (const gchar *file, GSList *key_list)
1387 gchar buf[BUFFSIZE];
1389 GpgmeData text = NULL;
1390 GpgmeData sigdata = NULL;
1394 if ((fp = fopen(file, "rb")) == NULL) {
1395 FILE_OP_ERROR(file, "fopen");
1399 err = gpgme_data_new(&text);
1401 debug_print("gpgme_data_new failed: %s\n", gpgme_strerror(err));
1405 while (!err && fgets(buf, sizeof(buf), fp)) {
1406 err = gpgme_data_write(text, buf, strlen(buf));
1409 FILE_OP_ERROR(file, "fgets");
1413 debug_print("gpgme_data_write failed: %s\n", gpgme_strerror(err));
1417 sigdata = pgp_sign(text, key_list, TRUE, &siginfo);
1424 if (fclose(fp) == EOF) {
1425 FILE_OP_ERROR(file, "fclose");
1429 if ((fp = fopen(file, "wb")) == NULL) {
1430 FILE_OP_ERROR(file, "fopen");
1434 err = gpgme_data_rewind(sigdata);
1436 debug_print("gpgme_data_rewind on sigdata failed: %s\n",
1437 gpgme_strerror(err));
1441 while (!(err = gpgme_data_read(sigdata, buf, sizeof(buf), &nread))) {
1442 fwrite(buf, nread, 1, fp);
1444 if (err != GPGME_EOF) {
1445 debug_print("gpgme_data_read failed: %s\n", gpgme_strerror(err));
1449 if (fclose(fp) == EOF) {
1450 FILE_OP_ERROR(file, "fclose");
1454 gpgme_data_release(text);
1455 gpgme_data_release(sigdata);
1461 gpgme_data_release(text);
1462 gpgme_data_release(sigdata);
1466 static gboolean rfc2015_is_signed(MimeInfo *mimeinfo)
1469 MimeInfo *signature;
1472 g_return_val_if_fail(mimeinfo != NULL, FALSE);
1475 parent = mimeinfo->parent;
1478 if ((parent->type != MIMETYPE_MULTIPART) ||
1479 g_strcasecmp(parent->subtype, "signed"))
1481 protocol = g_hash_table_lookup(parent->parameters, "protocol");
1482 if ((protocol == NULL) || g_strcasecmp(protocol, "application/pgp-signature"))
1485 /* check if mimeinfo is the first child */
1486 if (parent->children != mimeinfo)
1489 /* check signature */
1490 signature = parent->children->next;
1491 if (signature == NULL)
1493 if ((signature->type != MIMETYPE_APPLICATION) ||
1494 g_strcasecmp(signature->subtype, "pgp-signature"))
1500 static void idle_function_for_gpgme(void)
1502 while (gtk_events_pending())
1503 gtk_main_iteration();
1506 static PrivacySystem rfc2015_system = {
1507 "PGP/Mime", /* name */
1509 g_free, /* free_privacydata */
1511 rfc2015_is_signed, /* is_signed(MimeInfo *) */
1512 NULL, /* get_signer(MimeInfo *) */
1513 NULL, /* check_signature(MimeInfo *) */
1516 NULL, /* is_encrypted(MimeInfo *) */
1517 NULL, /* decrypt(MimeInfo *) */
1522 if (gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP) !=
1523 GPGME_No_Error) { /* Also does some gpgme init */
1524 rfc2015_disable_all();
1525 debug_print("gpgme_engine_version:\n%s\n",
1526 gpgme_get_engine_info());
1528 if (prefs_common.gpg_warning) {
1531 val = alertpanel_message_with_disable
1533 _("GnuPG is not installed properly, or needs to be upgraded.\n"
1534 "OpenPGP support disabled."));
1535 if (val & G_ALERTDISABLE)
1536 prefs_common.gpg_warning = FALSE;
1539 privacy_register_system(&rfc2015_system);
1541 gpgme_register_idle(idle_function_for_gpgme);
1546 privacy_unregister_system(&rfc2015_system);
1547 gpgmegtk_free_passphrase();
1550 #endif /* USE_GPGME */