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"
50 #define DIM(v) (sizeof(v)/sizeof((v)[0]))
52 static char *content_names[] = {
54 "Content-Disposition",
55 "Content-Transfer-Encoding",
59 static char *mime_version_name[] = {
64 static void sig_expiration_check (GString *str,
69 static void sig_expired (GString *str,
72 static void sig_key_expired (GString *str,
77 static void dump_mimeinfo (const char *text, MimeInfo *x)
79 debug_print ("MimeInfo[%s] %p level=%d\n",
80 text, x, x? x->level:0 );
84 debug_print (" enc=`%s' enc_type=%d mime_type=%d\n",
85 x->encoding, x->encoding_type, x->mime_type );
86 debug_print (" cont_type=`%s' cs=`%s' name=`%s' bnd=`%s'\n",
87 x->content_type, x->charset, x->name, x->boundary );
88 debug_print (" cont_disp=`%s' fname=`%s' fpos=%ld size=%u, lvl=%d\n",
89 x->content_disposition, x->filename, x->fpos, x->size,
91 dump_mimeinfo (".main", x->main );
92 dump_mimeinfo (".sub", x->sub );
93 dump_mimeinfo (".next", x->next );
94 debug_print ("MimeInfo[.parent] %p\n", x );
95 dump_mimeinfo (".children", x->children );
96 dump_mimeinfo (".plaintext", x->plaintext );
99 static void dump_part ( MimeInfo *mimeinfo, FILE *fp )
101 unsigned int size = mimeinfo->size;
104 if (fseek (fp, mimeinfo->fpos, SEEK_SET)) {
105 debug_print ("dump_part: fseek error\n");
109 debug_print ("--- begin dump_part ----\n");
110 while (size-- && (c = getc (fp)) != EOF)
113 debug_print ("dump_part: read error\n");
114 debug_print ("--- end dump_part ----\n");
119 rfc2015_disable_all (void)
121 /* FIXME: set a flag, so that we don't bother the user with failed
127 rfc2015_secure_remove (const char *fname)
131 /* fixme: overwrite the file first */
137 sig_status_to_string (GpgmeSigStat status)
142 case GPGME_SIG_STAT_NONE:
143 result = _("Oops: Signature not verified");
145 case GPGME_SIG_STAT_NOSIG:
146 result = _("No signature found");
148 case GPGME_SIG_STAT_GOOD:
149 result = _("Good signature");
151 case GPGME_SIG_STAT_GOOD_EXP:
152 result = _("Good signature but it has expired");
154 case GPGME_SIG_STAT_GOOD_EXPKEY:
155 result = _("Good signature but the key has expired");
157 case GPGME_SIG_STAT_BAD:
158 result = _("BAD signature");
160 case GPGME_SIG_STAT_NOKEY:
161 result = _("No public key to verify the signature");
163 case GPGME_SIG_STAT_ERROR:
164 result = _("Error verifying the signature");
166 case GPGME_SIG_STAT_DIFF:
167 result = _("Different results for signatures");
170 result = _("Error: Unknown status");
178 sig_status_with_name (GpgmeSigStat status)
183 case GPGME_SIG_STAT_NONE:
184 result = _("Oops: Signature not verified");
186 case GPGME_SIG_STAT_NOSIG:
187 result = _("No signature found");
189 case GPGME_SIG_STAT_GOOD:
190 result = _("Good signature from \"%s\"");
192 case GPGME_SIG_STAT_GOOD_EXP:
193 result = _("Good signature from \"%s\" but it has expired");
195 case GPGME_SIG_STAT_GOOD_EXPKEY:
196 result = _("Good signature from \"%s\" but the key has expired");
198 case GPGME_SIG_STAT_BAD:
199 result = _("BAD signature from \"%s\"");
201 case GPGME_SIG_STAT_NOKEY:
202 result = _("No public key to verify the signature");
204 case GPGME_SIG_STAT_ERROR:
205 result = _("Error verifying the signature");
207 case GPGME_SIG_STAT_DIFF:
208 result = _("Different results for signatures");
211 result = _("Error: Unknown status");
219 sig_status_for_key(GString *str, GpgmeCtx ctx, GpgmeSigStat status,
220 GpgmeKey key, const gchar *fpr)
225 uid = gpgme_key_get_string_attr (key, GPGME_ATTR_USERID, NULL, idx);
227 g_string_sprintfa (str, "%s\n",
228 sig_status_to_string (status));
229 if ((fpr != NULL) && (*fpr != '\0'))
230 g_string_sprintfa (str, "Key fingerprint: %s\n", fpr);
231 g_string_append (str, _("Cannot find user ID for this key."));
232 sig_expiration_check(str, ctx, key, status, 0);
235 g_string_sprintfa (str, sig_status_with_name (status), uid);
236 g_string_append (str, "\n");
239 uid = gpgme_key_get_string_attr (key, GPGME_ATTR_USERID,
243 g_string_sprintfa (str, _(" aka \"%s\"\n"),
246 sig_expiration_check(str, ctx, key, status, 0);
250 sig_expiration_check(GString *str, GpgmeCtx ctx, GpgmeKey key,
251 GpgmeSigStat status, int idx)
253 if (status == GPGME_SIG_STAT_GOOD_EXP)
254 sig_expired(str, ctx, idx);
255 else if (status == GPGME_SIG_STAT_GOOD_EXPKEY)
256 sig_key_expired(str, key, idx);
260 sig_expired(GString *str, GpgmeCtx ctx, int idx)
262 unsigned long exp_time;
263 exp_time = gpgme_get_sig_ulong_attr(ctx, idx, GPGME_ATTR_EXPIRE, 0);
264 g_string_sprintfa(str, _("Signature expired %s"), ctime(&exp_time));
268 sig_key_expired(GString *str, GpgmeKey key, int idx)
270 unsigned long exp_time;
271 exp_time = gpgme_key_get_ulong_attr(key, GPGME_ATTR_EXPIRE, NULL, idx);
272 g_string_sprintfa(str, _("Key expired %s"), ctime(&exp_time));
276 sig_status_full (GpgmeCtx ctx)
285 struct tm *ctime_val;
289 str = g_string_new ("");
291 fpr = gpgme_get_sig_status (ctx, sig_idx, &status, &created);
292 while (fpr != NULL) {
294 ctime_val = localtime (&created);
295 strftime (ctime_str, sizeof (ctime_str), "%c",
297 g_string_sprintfa (str,
298 _("Signature made at %s\n"),
301 err = gpgme_get_sig_key (ctx, sig_idx, &key);
303 g_string_sprintfa (str, "%s\n",
304 sig_status_to_string (status));
305 if ((fpr != NULL) && (*fpr != '\0'))
306 g_string_sprintfa (str,
307 _("Key fingerprint: %s\n"),
310 sig_status_for_key (str, ctx, status, key, fpr);
311 gpgme_key_unref (key);
313 g_string_append (str, "\n\n");
315 fpr = gpgme_get_sig_status (ctx, ++sig_idx, &status, &created);
319 g_string_free (str, FALSE);
323 static void check_signature (MimeInfo *mimeinfo, MimeInfo *partinfo, FILE *fp)
327 GpgmeData sig = NULL, text = NULL;
328 GpgmeSigStat status = GPGME_SIG_STAT_NONE;
329 GpgmegtkSigStatus statuswindow = NULL;
330 const char *result = NULL;
332 gint n_exclude_chars = 0;
334 if (prefs_common.gpg_signature_popup)
335 statuswindow = gpgmegtk_sig_status_create ();
337 err = gpgme_new (&ctx);
339 debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
343 /* don't include the last empty line.
344 It does not belong to the signed text */
345 if (mimeinfo->children->size > 0) {
346 if (fseek(fp, mimeinfo->children->fpos + mimeinfo->children->size - 1,
351 if (fgetc(fp) == '\n') {
353 if (mimeinfo->children->size > 1) {
354 if (fseek(fp, mimeinfo->children->fpos + mimeinfo->children->size - 2,
359 if (fgetc(fp) == '\r')
365 /* canonicalize the file part. */
366 tmp_file = get_tmp_file();
367 if (copy_file_part(fp, mimeinfo->children->fpos,
368 mimeinfo->children->size - n_exclude_chars,
373 if (canonicalize_file_replace(tmp_file) < 0) {
379 err = gpgme_data_new_from_file(&text, tmp_file, 1);
385 err = gpgme_data_new_from_filepart (&sig, NULL, fp,
386 partinfo->fpos, partinfo->size);
388 debug_print ("gpgme_data_new_from_filepart failed: %s\n",
389 gpgme_strerror (err));
393 err = gpgme_op_verify (ctx, sig, text, &status);
395 debug_print ("gpgme_op_verify failed: %s\n", gpgme_strerror (err));
399 /* FIXME: check what the heck this sig_status_full stuff is.
400 * it should better go into sigstatus.c */
401 g_free (partinfo->sigstatus_full);
402 partinfo->sigstatus_full = sig_status_full (ctx);
405 result = gpgmegtk_sig_status_to_string(status);
406 debug_print("verification status: %s\n", result);
407 if (prefs_common.gpg_signature_popup)
408 gpgmegtk_sig_status_update (statuswindow, ctx);
410 g_free (partinfo->sigstatus);
411 partinfo->sigstatus = g_strdup (result);
412 partinfo->sig_ok = (status == GPGME_SIG_STAT_GOOD);
413 partinfo->sig_unknown = (status == GPGME_SIG_STAT_NOKEY);
414 partinfo->sig_expired = (status == GPGME_SIG_STAT_GOOD_EXP);
415 partinfo->key_expired = (status == GPGME_SIG_STAT_GOOD_EXPKEY);
417 gpgme_data_release (sig);
418 gpgme_data_release (text);
420 if (prefs_common.gpg_signature_popup)
421 gpgmegtk_sig_status_destroy (statuswindow);
425 * Copy a gpgme data object to a temporary file and
426 * return this filename
430 copy_gpgmedata_to_temp (GpgmeData data, guint *length)
439 tmp = g_strdup_printf("%s%cgpgtmp.%08x",
440 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id );
442 if ((fp = fopen(tmp, "wb")) == NULL) {
443 FILE_OP_ERROR(tmp, "fopen");
448 err = gpgme_data_rewind ( data );
450 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
452 while (!(err = gpgme_data_read (data, buf, 100, &nread))) {
453 fwrite ( buf, nread, 1, fp );
456 if (err != GPGME_EOF)
457 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
467 pgp_decrypt (MimeInfo *partinfo, FILE *fp)
471 GpgmeData cipher = NULL, plain = NULL;
472 struct passphrase_cb_info_s info;
474 memset (&info, 0, sizeof info);
476 err = gpgme_new (&ctx);
478 debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
482 err = gpgme_data_new_from_filepart (&cipher, NULL, fp,
483 partinfo->fpos, partinfo->size);
485 debug_print ("gpgme_data_new_from_filepart failed: %s\n",
486 gpgme_strerror (err));
490 err = gpgme_data_new (&plain);
492 debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
496 if (!getenv("GPG_AGENT_INFO")) {
498 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
501 err = gpgme_op_decrypt (ctx, cipher, plain);
504 gpgme_data_release (cipher);
506 gpgmegtk_free_passphrase();
507 debug_print ("decryption failed: %s\n", gpgme_strerror (err));
508 gpgme_data_release (plain);
512 debug_print ("** decryption succeeded\n");
518 MimeInfo * rfc2015_find_signature (MimeInfo *mimeinfo)
525 if (g_strcasecmp (mimeinfo->content_type, "multipart/signed"))
528 debug_print ("** multipart/signed encountered\n");
530 /* check that we have at least 2 parts of the correct type */
531 for (partinfo = mimeinfo->children;
532 partinfo != NULL; partinfo = partinfo->next) {
533 if (++n > 1 && !g_strcasecmp (partinfo->content_type,
534 "application/pgp-signature"))
541 gboolean rfc2015_has_signature (MimeInfo *mimeinfo)
543 return rfc2015_find_signature (mimeinfo) != NULL;
546 void rfc2015_check_signature (MimeInfo *mimeinfo, FILE *fp)
550 partinfo = rfc2015_find_signature (mimeinfo);
555 g_message ("** yep, it is a pgp signature");
556 dump_mimeinfo ("gpg-signature", partinfo );
557 dump_part (partinfo, fp );
558 dump_mimeinfo ("signed text", mimeinfo->children );
559 dump_part (mimeinfo->children, fp);
562 check_signature (mimeinfo, partinfo, fp);
565 int rfc2015_is_encrypted (MimeInfo *mimeinfo)
567 if (!mimeinfo || mimeinfo->mime_type != MIME_MULTIPART)
569 if (g_strcasecmp (mimeinfo->content_type, "multipart/encrypted"))
571 /* fixme: we should check the protocol parameter */
575 gboolean rfc2015_msg_is_encrypted (const gchar *file)
581 if ((fp = fopen(file, "rb")) == NULL)
584 mimeinfo = procmime_scan_mime_header(fp);
590 ret = rfc2015_is_encrypted(mimeinfo);
591 procmime_mimeinfo_free_all(mimeinfo);
592 return ret != 0 ? TRUE : FALSE;
596 name_cmp(const char *a, const char *b)
598 for( ; *a && *b; a++, b++) {
600 && toupper(*(unsigned char *)a) != toupper(*(unsigned char *)b))
608 headerp(char *p, char **names)
617 if(p2[-1] == ' ' || p2[-1] == '\t') {
626 for(i = 0 ; names[i] != NULL; i++) {
627 if(!name_cmp (names[i], p))
632 return names[i] != NULL;
636 #define DECRYPTION_ABORT() \
638 procmime_mimeinfo_free_all(tmpinfo); \
639 msginfo->decryption_failed = 1; \
643 void rfc2015_decrypt_message (MsgInfo *msginfo, MimeInfo *mimeinfo, FILE *fp)
646 MimeInfo *tmpinfo, *partinfo;
655 g_return_if_fail (msginfo != NULL);
656 g_return_if_fail (mimeinfo != NULL);
657 g_return_if_fail (fp != NULL);
658 g_return_if_fail (mimeinfo->mime_type == MIME_MULTIPART);
660 debug_print ("** decrypting multipart/encrypted message\n");
663 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
665 tmpinfo = procmime_scan_mime_header(fp);
666 if (!tmpinfo || tmpinfo->mime_type != MIME_MULTIPART) {
670 procmime_scan_multipart_message(tmpinfo, fp);
672 /* check that we have the 2 parts */
673 partinfo = tmpinfo->children;
674 if (!partinfo || !partinfo->next) {
677 if (!g_strcasecmp (partinfo->content_type, "application/pgp-encrypted")) {
678 /* Fixme: check that the version is 1 */
681 partinfo = partinfo->next;
683 !g_strcasecmp (partinfo->content_type, "application/octet-stream")) {
685 g_warning ("oops: pgp_encrypted with more than 2 parts");
691 debug_print ("** yep, it is pgp encrypted\n");
693 plain = pgp_decrypt (partinfo, fp);
698 fname = g_strdup_printf("%s%cplaintext.%08x",
699 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
701 if ((dstfp = fopen(fname, "wb")) == NULL) {
702 FILE_OP_ERROR(fname, "fopen");
707 /* write the orginal header to the new file */
708 if (fseek(fp, tmpinfo->fpos, SEEK_SET) < 0)
711 while (fgets(buf, sizeof(buf), fp)) {
712 if (headerp (buf, content_names))
714 if (buf[0] == '\r' || buf[0] == '\n')
719 err = gpgme_data_rewind (plain);
721 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
723 while (!(err = gpgme_data_read (plain, buf, sizeof(buf), &nread))) {
724 fwrite (buf, nread, 1, dstfp);
727 if (err != GPGME_EOF) {
728 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
732 procmime_mimeinfo_free_all(tmpinfo);
734 msginfo->plaintext_file = fname;
735 msginfo->decryption_failed = 0;
738 #undef DECRYPTION_ABORT
742 * plain contains an entire mime object.
743 * Encrypt it and return an GpgmeData object with the encrypted version of
744 * the file or NULL in case of error.
747 pgp_encrypt ( GpgmeData plain, GpgmeRecipients rset )
751 GpgmeData cipher = NULL;
753 err = gpgme_new (&ctx);
755 err = gpgme_data_new (&cipher);
757 gpgme_set_armor (ctx, 1);
758 err = gpgme_op_encrypt (ctx, rset, plain, cipher);
762 debug_print ("encryption failed: %s\n", gpgme_strerror (err));
763 gpgme_data_release (cipher);
767 debug_print ("** encryption succeeded\n");
775 * Create and return a list of keys matching a key id
778 GSList *rfc2015_create_signers_list (const char *keyid)
780 GSList *key_list = NULL;
781 GpgmeCtx list_ctx = NULL;
786 err = gpgme_new (&list_ctx);
789 err = gpgme_op_keylist_start (list_ctx, keyid, 1);
792 while ( !(err = gpgme_op_keylist_next (list_ctx, &key)) ) {
793 key_list = g_slist_append (key_list, key);
795 if (err != GPGME_EOF)
798 if (key_list == NULL) {
799 debug_print ("no keys found for keyid \"%s\"\n", keyid);
804 debug_print ("rfc2015_create_signers_list failed: %s\n", gpgme_strerror (err));
805 for (p = key_list; p != NULL; p = p->next)
806 gpgme_key_unref ((GpgmeKey) p->data);
807 g_slist_free (key_list);
810 gpgme_release (list_ctx);
811 return err ? NULL : key_list;
815 * Encrypt the file by extracting all recipients and finding the
816 * encryption keys for all of them. The file content is then replaced
817 * by the encrypted one. */
819 rfc2015_encrypt (const char *file, GSList *recp_list, gboolean ascii_armored)
823 int i, clineidx, saved_last;
824 char *clines[3] = {NULL};
826 GpgmeData header = NULL;
827 GpgmeData plain = NULL;
828 GpgmeData cipher = NULL;
829 GpgmeRecipients rset = NULL;
831 int mime_version_seen = 0;
832 char *boundary = generate_mime_boundary ();
834 /* Create the list of recipients */
835 rset = gpgmegtk_recipient_selection (recp_list);
837 debug_print ("error creating recipient list\n" );
841 /* Open the source file */
842 if ((fp = fopen(file, "rb")) == NULL) {
843 FILE_OP_ERROR(file, "fopen");
847 err = gpgme_data_new (&header);
849 err = gpgme_data_new (&plain);
851 debug_print ("gpgme_data_new failed: %s\n", gpgme_strerror (err));
855 /* get the content header lines from the source */
858 while (!err && fgets(buf, sizeof(buf), fp)) {
859 /* fixme: check for overlong lines */
860 if (headerp (buf, content_names)) {
861 if (clineidx >= DIM (clines)) {
862 debug_print ("rfc2015_encrypt: too many content lines\n");
865 clines[clineidx++] = g_strdup (buf);
871 if (*buf == ' ' || *buf == '\t') {
872 char *last = clines[clineidx-1];
873 clines[clineidx-1] = g_strconcat (last, buf, NULL);
879 if (headerp (buf, mime_version_name))
880 mime_version_seen = 1;
882 if (buf[0] == '\r' || buf[0] == '\n')
884 err = gpgme_data_write (header, buf, strlen (buf));
887 FILE_OP_ERROR (file, "fgets");
891 /* write them to the temp data and add the rest of the message */
892 for (i = 0; !err && i < clineidx; i++) {
893 debug_print ("%% %s:%d: cline=`%s'", __FILE__ ,__LINE__, clines[i]);
894 err = gpgme_data_write (plain, clines[i], strlen (clines[i]));
897 err = gpgme_data_write (plain, "\r\n", 2);
898 while (!err && fgets(buf, sizeof(buf), fp)) {
899 err = gpgme_data_write (plain, buf, strlen (buf));
902 FILE_OP_ERROR (file, "fgets");
906 debug_print ("gpgme_data_write failed: %s\n", gpgme_strerror (err));
910 cipher = pgp_encrypt (plain, rset);
911 gpgme_data_release (plain); plain = NULL;
912 gpgme_recipients_release (rset); rset = NULL;
916 /* we have the encrypted message available in cipher and now we
917 * are going to rewrite the source file. To be sure that file has
918 * been truncated we use an approach which should work everywhere:
919 * close the file and then reopen it for writing. It is important
920 * that this works, otherwise it may happen that parts of the
921 * plaintext are still in the file (The encrypted stuff is, due to
922 * compression, usually shorter than the plaintext).
924 * Yes, there is a race condition here, but everyone, who is so
925 * stupid to store the temp file with the plaintext in a public
926 * directory has to live with this anyway. */
928 FILE_OP_ERROR(file, "fclose");
931 if ((fp = fopen(file, "wb")) == NULL) {
932 FILE_OP_ERROR(file, "fopen");
936 /* Write the header, append new content lines, part 1 and part 2 header */
937 err = gpgme_data_rewind (header);
939 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
942 while (!(err = gpgme_data_read (header, buf, BUFFSIZE, &nread))) {
943 fwrite (buf, nread, 1, fp);
945 if (err != GPGME_EOF) {
946 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
950 FILE_OP_ERROR (file, "fwrite");
953 gpgme_data_release (header); header = NULL;
955 if (!mime_version_seen)
956 fputs ("MIME-Version: 1\r\n", fp);
960 "Content-Type: text/plain; charset=US-ASCII\r\n"
961 "Content-Transfer-Encoding: 7bit\r\n"
965 "Content-Type: multipart/encrypted;"
966 " protocol=\"application/pgp-encrypted\";\r\n"
967 " boundary=\"%s\"\r\n"
970 "Content-Type: application/pgp-encrypted\r\n"
975 "Content-Type: application/octet-stream\r\n"
977 boundary, boundary, boundary);
980 /* append the encrypted stuff */
981 err = gpgme_data_rewind (cipher);
983 debug_print ("** gpgme_data_rewind on cipher failed: %s\n",
984 gpgme_strerror (err));
988 while (!(err = gpgme_data_read (cipher, buf, BUFFSIZE, &nread))) {
989 fwrite (buf, nread, 1, fp);
991 if (err != GPGME_EOF) {
992 debug_print ("** gpgme_data_read failed: %s\n", gpgme_strerror (err));
996 /* and the final boundary */
997 if (!ascii_armored) {
1005 FILE_OP_ERROR (file, "fwrite");
1009 gpgme_data_release (cipher);
1015 gpgme_data_release (header);
1016 gpgme_data_release (plain);
1017 gpgme_data_release (cipher);
1018 gpgme_recipients_release (rset);
1020 return -1; /* error */
1024 * plain contains an entire mime object. Sign it and return an
1025 * GpgmeData object with the signature of it or NULL in case of error.
1026 * r_siginfo returns an XML object with information about the signature.
1029 pgp_sign (GpgmeData plain, GSList *key_list, gboolean clearsign,
1033 GpgmeCtx ctx = NULL;
1035 GpgmeData sig = NULL;
1036 struct passphrase_cb_info_s info;
1039 memset (&info, 0, sizeof info);
1041 err = gpgme_new (&ctx);
1044 err = gpgme_data_new (&sig);
1048 if (!getenv("GPG_AGENT_INFO")) {
1050 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
1052 gpgme_set_textmode (ctx, 1);
1053 gpgme_set_armor (ctx, 1);
1054 gpgme_signers_clear (ctx);
1055 for (p = key_list; p != NULL; p = p->next) {
1056 err = gpgme_signers_add (ctx, (GpgmeKey) p->data);
1060 for (p = key_list; p != NULL; p = p->next)
1061 gpgme_key_unref ((GpgmeKey) p->data);
1062 g_slist_free (key_list);
1068 clearsign ? GPGME_SIG_MODE_CLEAR : GPGME_SIG_MODE_DETACH);
1070 *r_siginfo = gpgme_get_op_info (ctx, 0);
1074 gpgmegtk_free_passphrase();
1075 debug_print ("signing failed: %s\n", gpgme_strerror (err));
1076 gpgme_data_release (sig);
1080 debug_print ("signing succeeded\n");
1083 gpgme_release (ctx);
1088 * Find TAG in XML and return a pointer into xml set just behind the
1089 * closing angle. Return NULL if not found.
1092 find_xml_tag (const char *xml, const char *tag)
1094 int taglen = strlen (tag);
1095 const char *s = xml;
1097 while ( (s = strchr (s, '<')) ) {
1099 if (!strncmp (s, tag, taglen)) {
1100 const char *s2 = s + taglen;
1101 if (*s2 == '>' || isspace (*(const unsigned char*)s2) ) {
1103 while (*s2 && *s2 != '>') /* skip attributes */
1105 /* fixme: do need to handle angles inside attribute vallues? */
1106 return *s2? (s2+1):NULL;
1109 while (*s && *s != '>') /* skip to end of tag */
1117 * Extract the micalg from an GnupgOperationInfo XML container.
1120 extract_micalg (char *xml)
1124 s = find_xml_tag (xml, "GnupgOperationInfo");
1126 const char *s_end = find_xml_tag (s, "/GnupgOperationInfo");
1127 s = find_xml_tag (s, "signature");
1128 if (s && s_end && s < s_end) {
1129 const char *s_end2 = find_xml_tag (s, "/signature");
1130 if (s_end2 && s_end2 < s_end) {
1131 s = find_xml_tag (s, "micalg");
1132 if (s && s < s_end2) {
1133 s_end = strchr (s, '<');
1135 char *p = g_malloc (s_end - s + 1);
1136 memcpy (p, s, s_end - s);
1149 * Sign the file and replace its content with the signed one.
1152 rfc2015_sign (const char *file, GSList *key_list)
1156 int i, clineidx, saved_last;
1157 char *clines[3] = {NULL};
1159 GpgmeData header = NULL;
1160 GpgmeData plain = NULL;
1161 GpgmeData sigdata = NULL;
1163 int mime_version_seen = 0;
1164 char *boundary = generate_mime_boundary ();
1165 char *micalg = NULL;
1168 /* Open the source file */
1169 if ((fp = fopen(file, "rb")) == NULL) {
1170 FILE_OP_ERROR(file, "fopen");
1174 err = gpgme_data_new (&header);
1176 err = gpgme_data_new (&plain);
1178 debug_print ("gpgme_data_new failed: %s\n", gpgme_strerror (err));
1182 /* get the content header lines from the source */
1185 while (!err && fgets(buf, sizeof(buf), fp)) {
1186 /* fixme: check for overlong lines */
1187 if (headerp (buf, content_names)) {
1188 if (clineidx >= DIM (clines)) {
1189 debug_print ("rfc2015_sign: too many content lines\n");
1192 clines[clineidx++] = g_strdup (buf);
1198 if (*buf == ' ' || *buf == '\t') {
1199 char *last = clines[clineidx - 1];
1200 clines[clineidx - 1] = g_strconcat (last, buf, NULL);
1206 if (headerp (buf, mime_version_name))
1207 mime_version_seen = 1;
1209 if (buf[0] == '\r' || buf[0] == '\n')
1211 err = gpgme_data_write (header, buf, strlen (buf));
1214 FILE_OP_ERROR (file, "fgets");
1218 /* write them to the temp data and add the rest of the message */
1219 for (i = 0; !err && i < clineidx; i++) {
1220 err = gpgme_data_write (plain, clines[i], strlen (clines[i]));
1223 err = gpgme_data_write (plain, "\r\n", 2 );
1224 while (!err && fgets(buf, sizeof(buf), fp)) {
1225 err = gpgme_data_write (plain, buf, strlen (buf));
1228 FILE_OP_ERROR (file, "fgets");
1232 debug_print ("gpgme_data_write failed: %s\n", gpgme_strerror (err));
1236 sigdata = pgp_sign (plain, key_list, FALSE, &siginfo);
1238 micalg = extract_micalg (siginfo);
1244 /* we have the signed message available in sigdata and now we are
1245 * going to rewrite the original file. To be sure that file has
1246 * been truncated we use an approach which should work everywhere:
1247 * close the file and then reopen it for writing. */
1249 FILE_OP_ERROR(file, "fclose");
1252 if ((fp = fopen(file, "wb")) == NULL) {
1253 FILE_OP_ERROR(file, "fopen");
1257 /* Write the rfc822 header and add new content lines */
1258 err = gpgme_data_rewind (header);
1260 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
1261 while (!(err = gpgme_data_read (header, buf, BUFFSIZE, &nread))) {
1262 fwrite (buf, nread, 1, fp);
1264 if (err != GPGME_EOF) {
1265 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
1269 FILE_OP_ERROR (file, "fwrite");
1272 gpgme_data_release (header);
1275 if (!mime_version_seen)
1276 fputs ("MIME-Version: 1.0\r\n", fp);
1277 fprintf (fp, "Content-Type: multipart/signed; "
1278 "protocol=\"application/pgp-signature\";\r\n");
1280 fprintf (fp, " micalg=\"%s\";", micalg);
1281 fprintf (fp, " boundary=\"%s\"\r\n", boundary);
1283 /* Part 1: signed material */
1287 err = gpgme_data_rewind (plain);
1289 debug_print ("gpgme_data_rewind on plain failed: %s\n",
1290 gpgme_strerror (err));
1293 while (!(err = gpgme_data_read (plain, buf, BUFFSIZE, &nread))) {
1294 fwrite (buf, nread, 1, fp);
1296 if (err != GPGME_EOF) {
1297 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
1301 /* Part 2: signature */
1305 fputs ("Content-Type: application/pgp-signature\r\n"
1308 err = gpgme_data_rewind (sigdata);
1310 debug_print ("gpgme_data_rewind on sigdata failed: %s\n",
1311 gpgme_strerror (err));
1315 while (!(err = gpgme_data_read (sigdata, buf, BUFFSIZE, &nread))) {
1316 fwrite (buf, nread, 1, fp);
1318 if (err != GPGME_EOF) {
1319 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
1323 /* Final boundary */
1329 FILE_OP_ERROR (file, "fwrite");
1333 gpgme_data_release (header);
1334 gpgme_data_release (plain);
1335 gpgme_data_release (sigdata);
1343 gpgme_data_release (header);
1344 gpgme_data_release (plain);
1345 gpgme_data_release (sigdata);
1348 return -1; /* error */
1353 * Sign the file with clear text and replace its content with the signed one.
1356 rfc2015_clearsign (const gchar *file, GSList *key_list)
1359 gchar buf[BUFFSIZE];
1361 GpgmeData text = NULL;
1362 GpgmeData sigdata = NULL;
1366 if ((fp = fopen(file, "rb")) == NULL) {
1367 FILE_OP_ERROR(file, "fopen");
1371 err = gpgme_data_new(&text);
1373 debug_print("gpgme_data_new failed: %s\n", gpgme_strerror(err));
1377 while (!err && fgets(buf, sizeof(buf), fp)) {
1378 err = gpgme_data_write(text, buf, strlen(buf));
1381 FILE_OP_ERROR(file, "fgets");
1385 debug_print("gpgme_data_write failed: %s\n", gpgme_strerror(err));
1389 sigdata = pgp_sign(text, key_list, TRUE, &siginfo);
1396 if (fclose(fp) == EOF) {
1397 FILE_OP_ERROR(file, "fclose");
1401 if ((fp = fopen(file, "wb")) == NULL) {
1402 FILE_OP_ERROR(file, "fopen");
1406 err = gpgme_data_rewind(sigdata);
1408 debug_print("gpgme_data_rewind on sigdata failed: %s\n",
1409 gpgme_strerror(err));
1413 while (!(err = gpgme_data_read(sigdata, buf, sizeof(buf), &nread))) {
1414 fwrite(buf, nread, 1, fp);
1416 if (err != GPGME_EOF) {
1417 debug_print("gpgme_data_read failed: %s\n", gpgme_strerror(err));
1421 if (fclose(fp) == EOF) {
1422 FILE_OP_ERROR(file, "fclose");
1426 gpgme_data_release(text);
1427 gpgme_data_release(sigdata);
1433 gpgme_data_release(text);
1434 gpgme_data_release(sigdata);
1438 #endif /* USE_GPGME */