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[] = {
65 struct passphrase_cb_info_s {
70 static char *create_boundary (void);
72 static void dump_mimeinfo (const char *text, MimeInfo *x)
74 debug_print ("MimeInfo[%s] %p level=%d\n",
75 text, x, x? x->level:0 );
79 debug_print (" enc=`%s' enc_type=%d mime_type=%d\n",
80 x->encoding, x->encoding_type, x->mime_type );
81 debug_print (" cont_type=`%s' cs=`%s' name=`%s' bnd=`%s'\n",
82 x->content_type, x->charset, x->name, x->boundary );
83 debug_print (" cont_disp=`%s' fname=`%s' fpos=%ld size=%u, lvl=%d\n",
84 x->content_disposition, x->filename, x->fpos, x->size,
86 dump_mimeinfo (".main", x->main );
87 dump_mimeinfo (".sub", x->sub );
88 dump_mimeinfo (".next", x->next );
89 debug_print ("MimeInfo[.parent] %p\n", x );
90 dump_mimeinfo (".children", x->children );
91 dump_mimeinfo (".plaintext", x->plaintext );
94 static void dump_part ( MimeInfo *mimeinfo, FILE *fp )
96 unsigned int size = mimeinfo->size;
99 if (fseek (fp, mimeinfo->fpos, SEEK_SET)) {
100 debug_print ("dump_part: fseek error\n");
104 debug_print ("--- begin dump_part ----\n");
105 while (size-- && (c = getc (fp)) != EOF)
108 debug_print ("dump_part: read error\n");
109 debug_print ("--- end dump_part ----\n");
113 rfc2015_disable_all (void)
115 /* FIXME: set a flag, so that we don't bother the user with failed
121 rfc2015_secure_remove (const char *fname)
125 /* fixme: overwrite the file first */
131 sig_status_to_string (GpgmeSigStat status)
136 case GPGME_SIG_STAT_NONE:
137 result = _("Oops: Signature not verified");
139 case GPGME_SIG_STAT_NOSIG:
140 result = _("No signature found");
142 case GPGME_SIG_STAT_GOOD:
143 result = _("Good signature");
145 case GPGME_SIG_STAT_BAD:
146 result = _("BAD signature");
148 case GPGME_SIG_STAT_NOKEY:
149 result = _("No public key to verify the signature");
151 case GPGME_SIG_STAT_ERROR:
152 result = _("Error verifying the signature");
154 case GPGME_SIG_STAT_DIFF:
155 result = _("Different results for signatures");
158 result = _("Error: Unknown status");
166 sig_status_with_name (GpgmeSigStat status)
171 case GPGME_SIG_STAT_NONE:
172 result = _("Oops: Signature not verified");
174 case GPGME_SIG_STAT_NOSIG:
175 result = _("No signature found");
177 case GPGME_SIG_STAT_GOOD:
178 result = _("Good signature from \"%s\"");
180 case GPGME_SIG_STAT_BAD:
181 result = _("BAD signature from \"%s\"");
183 case GPGME_SIG_STAT_NOKEY:
184 result = _("No public key to verify the signature");
186 case GPGME_SIG_STAT_ERROR:
187 result = _("Error verifying the signature");
189 case GPGME_SIG_STAT_DIFF:
190 result = _("Different results for signatures");
193 result = _("Error: Unknown status");
201 sig_status_for_key(GString *str, GpgmeCtx ctx, GpgmeSigStat status,
202 GpgmeKey key, const gchar *fpr)
207 uid = gpgme_key_get_string_attr (key, GPGME_ATTR_USERID, NULL, idx);
209 g_string_sprintfa (str, "%s\n",
210 sig_status_to_string (status));
211 if ((fpr != NULL) && (*fpr != '\0'))
212 g_string_sprintfa (str, "Key fingerprint: %s\n", fpr);
213 g_string_append (str, _("Cannot find user ID for this key."));
216 g_string_sprintfa (str, sig_status_with_name (status), uid);
217 g_string_append (str, "\n");
220 uid = gpgme_key_get_string_attr (key, GPGME_ATTR_USERID,
224 g_string_sprintfa (str, _(" aka \"%s\"\n"),
230 sig_status_full (GpgmeCtx ctx)
239 struct tm *ctime_val;
243 str = g_string_new ("");
245 fpr = gpgme_get_sig_status (ctx, sig_idx, &status, &created);
246 while (fpr != NULL) {
248 ctime_val = localtime (&created);
249 strftime (ctime_str, sizeof (ctime_str), "%c",
251 g_string_sprintfa (str,
252 _("Signature made %s\n"),
255 err = gpgme_get_sig_key (ctx, sig_idx, &key);
257 g_string_sprintfa (str, "%s\n",
258 sig_status_to_string (status));
259 if ((fpr != NULL) && (*fpr != '\0'))
260 g_string_sprintfa (str,
261 _("Key fingerprint: %s\n"),
264 sig_status_for_key (str, ctx, status, key, fpr);
265 gpgme_key_unref (key);
267 g_string_append (str, "\n\n");
269 fpr = gpgme_get_sig_status (ctx, ++sig_idx, &status, &created);
273 g_string_free (str, FALSE);
277 static void check_signature (MimeInfo *mimeinfo, MimeInfo *partinfo, FILE *fp)
281 GpgmeData sig = NULL, text = NULL;
282 GpgmeSigStat status = GPGME_SIG_STAT_NONE;
283 GpgmegtkSigStatus statuswindow = NULL;
284 const char *result = NULL;
286 if (prefs_common.gpg_signature_popup)
287 statuswindow = gpgmegtk_sig_status_create ();
289 err = gpgme_new (&ctx);
291 debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
295 /* don't include the last character (LF). It does not belong to the
297 err = gpgme_data_new_from_filepart (&text, NULL, fp,
298 mimeinfo->children->fpos,
299 mimeinfo->children->size ?
300 (mimeinfo->children->size - 1) : 0 );
302 err = gpgme_data_new_from_filepart (&sig, NULL, fp,
303 partinfo->fpos, partinfo->size);
305 debug_print ("gpgme_data_new_from_filepart failed: %s\n",
306 gpgme_strerror (err));
310 err = gpgme_op_verify (ctx, sig, text, &status);
312 debug_print ("gpgme_op_verify failed: %s\n", gpgme_strerror (err));
314 /* FIXME: check what the heck this sig_status_full stuff is.
315 * it should better go into sigstatus.c */
316 g_free (partinfo->sigstatus_full);
317 partinfo->sigstatus_full = sig_status_full (ctx);
320 result = gpgmegtk_sig_status_to_string(status);
321 debug_print("verification status: %s\n", result);
322 if (prefs_common.gpg_signature_popup)
323 gpgmegtk_sig_status_update (statuswindow,ctx);
325 g_assert (!err); /* FIXME: Hey: this may indeed happen */
326 g_free (partinfo->sigstatus);
327 partinfo->sigstatus = g_strdup (result);
329 gpgme_data_release (sig);
330 gpgme_data_release (text);
332 if (prefs_common.gpg_signature_popup)
333 gpgmegtk_sig_status_destroy (statuswindow);
337 passphrase_cb (void *opaque, const char *desc, void *r_hd)
339 struct passphrase_cb_info_s *info = opaque;
340 GpgmeCtx ctx = info ? info->c : NULL;
344 /* FIXME: cleanup by looking at *r_hd */
348 gpgmegtk_set_passphrase_grab (prefs_common.passphrase_grab);
349 debug_print ("requesting passphrase for `%s': ", desc );
350 pass = gpgmegtk_passphrase_mbox (desc);
352 debug_print ("cancel passphrase entry\n");
356 debug_print ("sending passphrase\n");
362 * Copy a gpgme data object to a temporary file and
363 * return this filename
366 copy_gpgmedata_to_temp (GpgmeData data, guint *length)
375 tmp = g_strdup_printf("%s%cgpgtmp.%08x",
376 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id );
378 if ((fp = fopen(tmp, "w")) == NULL) {
379 FILE_OP_ERROR(tmp, "fopen");
384 err = gpgme_data_rewind ( data );
386 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
388 while (!(err = gpgme_data_read (data, buf, 100, &nread))) {
389 fwrite ( buf, nread, 1, fp );
392 if (err != GPGME_EOF)
393 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
402 pgp_decrypt (MimeInfo *partinfo, FILE *fp)
406 GpgmeData cipher = NULL, plain = NULL;
407 struct passphrase_cb_info_s info;
409 memset (&info, 0, sizeof info);
411 err = gpgme_new (&ctx);
413 debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
417 err = gpgme_data_new_from_filepart (&cipher, NULL, fp,
418 partinfo->fpos, partinfo->size);
420 debug_print ("gpgme_data_new_from_filepart failed: %s\n",
421 gpgme_strerror (err));
425 err = gpgme_data_new (&plain);
427 debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
431 if (!getenv("GPG_AGENT_INFO")) {
433 gpgme_set_passphrase_cb (ctx, passphrase_cb, &info);
436 err = gpgme_op_decrypt (ctx, cipher, plain);
439 gpgme_data_release (cipher);
441 debug_print ("decryption failed: %s\n", gpgme_strerror (err));
442 gpgme_data_release (plain);
446 debug_print ("decryption succeeded\n");
452 MimeInfo * rfc2015_find_signature (MimeInfo *mimeinfo)
459 if (g_strcasecmp (mimeinfo->content_type, "multipart/signed"))
462 debug_print ("multipart/signed encountered\n");
464 /* check that we have at least 2 parts of the correct type */
465 for (partinfo = mimeinfo->children;
466 partinfo != NULL; partinfo = partinfo->next) {
467 if (++n > 1 && !g_strcasecmp (partinfo->content_type,
468 "application/pgp-signature"))
475 gboolean rfc2015_has_signature (MimeInfo *mimeinfo)
477 return rfc2015_find_signature (mimeinfo) != NULL;
480 void rfc2015_check_signature (MimeInfo *mimeinfo, FILE *fp)
484 partinfo = rfc2015_find_signature (mimeinfo);
489 g_message ("** yep, it is a pgp signature");
490 dump_mimeinfo ("gpg-signature", partinfo );
491 dump_part (partinfo, fp );
492 dump_mimeinfo ("signed text", mimeinfo->children );
493 dump_part (mimeinfo->children, fp);
496 check_signature (mimeinfo, partinfo, fp);
499 int rfc2015_is_encrypted (MimeInfo *mimeinfo)
503 if (g_strcasecmp (mimeinfo->content_type, "multipart/encrypted"))
505 /* fixme: we should schek the protocol parameter */
510 name_cmp(const char *a, const char *b)
512 for( ; *a && *b; a++, b++) {
514 && toupper(*(unsigned char *)a) != toupper(*(unsigned char *)b))
522 headerp(char *p, char **names)
531 if(p2[-1] == ' ' || p2[-1] == '\t') {
540 for(i = 0 ; names[i] != NULL; i++) {
541 if(!name_cmp (names[i], p))
546 return names[i] != NULL;
550 void rfc2015_decrypt_message (MsgInfo *msginfo, MimeInfo *mimeinfo, FILE *fp)
563 g_return_if_fail (mimeinfo->mime_type == MIME_MULTIPART);
565 debug_print ("multipart/encrypted encountered\n");
568 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
570 while (fgets(buf, sizeof(buf), fp) != NULL)
571 if (buf[0] == '\r' || buf[0] == '\n') break;
573 procmime_scan_multipart_message(mimeinfo, fp);
575 /* check that we have the 2 parts */
577 for (partinfo = mimeinfo->children; partinfo; partinfo = partinfo->next) {
578 if (++n == 1 && !g_strcasecmp (partinfo->content_type,
579 "application/pgp-encrypted")) {
580 /* Fixme: check that the version is 1 */
583 else if (n == 2 && !g_strcasecmp (partinfo->content_type,
584 "application/octet-stream")) {
586 debug_print ("oops: pgp_encrypted with more than 2 parts\n");
591 if (!ver_okay || !partinfo) {
592 msginfo->decryption_failed = 1;
593 /* fixme: remove the stuff, that the above procmime_scan_multiparts()
594 * has appended to mimeino */
598 debug_print ("yep, it is pgp encrypted\n");
600 plain = pgp_decrypt (partinfo, fp);
602 msginfo->decryption_failed = 1;
606 fname = g_strdup_printf("%s%cplaintext.%08x",
607 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
609 if ((dstfp = fopen(fname, "w")) == NULL) {
610 FILE_OP_ERROR(fname, "fopen");
612 msginfo->decryption_failed = 1;
616 /* write the orginal header to the new file */
617 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
620 while (fgets(buf, sizeof(buf), fp)) {
621 if (headerp (buf, content_names))
623 if (buf[0] == '\r' || buf[0] == '\n')
628 err = gpgme_data_rewind (plain);
630 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
632 while (!(err = gpgme_data_read (plain, buf, sizeof(buf), &nread))) {
633 fwrite (buf, nread, 1, dstfp);
636 if (err != GPGME_EOF) {
637 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
642 msginfo->plaintext_file = fname;
643 msginfo->decryption_failed = 0;
648 * plain contains an entire mime object.
649 * Encrypt it and return an GpgmeData object with the encrypted version of
650 * the file or NULL in case of error.
653 pgp_encrypt ( GpgmeData plain, GpgmeRecipients rset )
657 GpgmeData cipher = NULL;
659 err = gpgme_new (&ctx);
661 err = gpgme_data_new (&cipher);
663 gpgme_set_armor (ctx, 1);
664 err = gpgme_op_encrypt (ctx, rset, plain, cipher);
668 debug_print ("encryption failed: %s\n", gpgme_strerror (err));
669 gpgme_data_release (cipher);
673 debug_print ("encryption succeeded\n");
681 * Create and return a list of keys matching a key id
684 GSList *rfc2015_create_signers_list (const char *keyid)
686 GSList *key_list = NULL;
687 GpgmeCtx list_ctx = NULL;
692 err = gpgme_new (&list_ctx);
695 err = gpgme_op_keylist_start (list_ctx, keyid, 1);
698 while ( !(err = gpgme_op_keylist_next (list_ctx, &key)) ) {
699 key_list = g_slist_append (key_list, key);
701 if (err != GPGME_EOF)
704 if (key_list == NULL) {
705 debug_print ("no keys found for keyid \"%s\"\n", keyid);
710 debug_print ("rfc2015_create_signers_list failed: %s\n", gpgme_strerror (err));
711 for (p = key_list; p != NULL; p = p->next)
712 gpgme_key_unref ((GpgmeKey) p->data);
713 g_slist_free (key_list);
716 gpgme_release (list_ctx);
717 return err ? NULL : key_list;
721 * Encrypt the file by extracting all recipients and finding the
722 * encryption keys for all of them. The file content is then replaced
723 * by the encrypted one. */
725 rfc2015_encrypt (const char *file, GSList *recp_list, gboolean ascii_armored)
729 int i, clineidx, saved_last;
730 char *clines[3] = {NULL};
732 GpgmeData header = NULL;
733 GpgmeData plain = NULL;
734 GpgmeData cipher = NULL;
735 GpgmeRecipients rset = NULL;
737 int mime_version_seen = 0;
738 char *boundary = create_boundary ();
740 /* Create the list of recipients */
741 rset = gpgmegtk_recipient_selection (recp_list);
743 debug_print ("error creating recipient list\n" );
747 /* Open the source file */
748 if ((fp = fopen(file, "rb")) == NULL) {
749 FILE_OP_ERROR(file, "fopen");
753 err = gpgme_data_new (&header);
755 err = gpgme_data_new (&plain);
757 debug_print ("gpgme_data_new failed: %s\n", gpgme_strerror (err));
761 /* get the content header lines from the source */
764 while (!err && fgets(buf, sizeof(buf), fp)) {
765 /* fixme: check for overlong lines */
766 if (headerp (buf, content_names)) {
767 if (clineidx >= DIM (clines)) {
768 debug_print ("rfc2015_encrypt: too many content lines\n");
771 clines[clineidx++] = g_strdup (buf);
777 if (*buf == ' ' || *buf == '\t') {
778 char *last = clines[clineidx-1];
779 clines[clineidx-1] = g_strconcat (last, buf, NULL);
785 if (headerp (buf, mime_version_name))
786 mime_version_seen = 1;
788 if (buf[0] == '\r' || buf[0] == '\n')
790 err = gpgme_data_write (header, buf, strlen (buf));
793 FILE_OP_ERROR (file, "fgets");
797 /* write them to the temp data and add the rest of the message */
798 for (i = 0; !err && i < clineidx; i++) {
799 debug_print ("%s:%d: cline=`%s'\n", __FILE__ ,__LINE__, clines[i]);
800 err = gpgme_data_write (plain, clines[i], strlen (clines[i]));
803 err = gpgme_data_write (plain, "\r\n", 2);
804 while (!err && fgets(buf, sizeof(buf), fp)) {
805 err = gpgme_data_write (plain, buf, strlen (buf));
808 FILE_OP_ERROR (file, "fgets");
812 debug_print ("gpgme_data_write failed: %s\n", gpgme_strerror (err));
816 cipher = pgp_encrypt (plain, rset);
817 gpgme_data_release (plain); plain = NULL;
818 gpgme_recipients_release (rset); rset = NULL;
822 /* we have the encrypted message available in cipher and now we
823 * are going to rewrite the source file. To be sure that file has
824 * been truncated we use an approach which should work everywhere:
825 * close the file and then reopen it for writing. It is important
826 * that this works, otherwise it may happen that parts of the
827 * plaintext are still in the file (The encrypted stuff is, due to
828 * compression, usually shorter than the plaintext).
830 * Yes, there is a race condition here, but everyone, who is so
831 * stupid to store the temp file with the plaintext in a public
832 * directory has to live with this anyway. */
834 FILE_OP_ERROR(file, "fclose");
837 if ((fp = fopen(file, "wb")) == NULL) {
838 FILE_OP_ERROR(file, "fopen");
842 /* Write the header, append new content lines, part 1 and part 2 header */
843 err = gpgme_data_rewind (header);
845 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
848 while (!(err = gpgme_data_read (header, buf, BUFFSIZE, &nread))) {
849 fwrite (buf, nread, 1, fp);
851 if (err != GPGME_EOF) {
852 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
856 FILE_OP_ERROR (file, "fwrite");
859 gpgme_data_release (header); header = NULL;
861 if (!mime_version_seen)
862 fputs ("MIME-Version: 1\r\n", fp);
866 "Content-Type: text/plain; charset=us-ascii\r\n"
867 "Content-Transfer-Encoding: 7bit\r\n"
871 "Content-Type: multipart/encrypted;"
872 " protocol=\"application/pgp-encrypted\";\r\n"
873 " boundary=\"%s\"\r\n"
876 "Content-Type: application/pgp-encrypted\r\n"
881 "Content-Type: application/octet-stream\r\n"
883 boundary, boundary, boundary);
886 /* append the encrypted stuff */
887 err = gpgme_data_rewind (cipher);
889 debug_print ("** gpgme_data_rewind on cipher failed: %s\n",
890 gpgme_strerror (err));
894 while (!(err = gpgme_data_read (cipher, buf, BUFFSIZE, &nread))) {
895 fwrite (buf, nread, 1, fp);
897 if (err != GPGME_EOF) {
898 debug_print ("** gpgme_data_read failed: %s\n", gpgme_strerror (err));
902 /* and the final boundary */
903 if (!ascii_armored) {
912 FILE_OP_ERROR (file, "fwrite");
916 gpgme_data_release (cipher);
922 gpgme_data_release (header);
923 gpgme_data_release (plain);
924 gpgme_data_release (cipher);
925 gpgme_recipients_release (rset);
927 return -1; /* error */
931 * plain contains an entire mime object. Sign it and return an
932 * GpgmeData object with the signature of it or NULL in case of error.
935 pgp_sign (GpgmeData plain, GSList *key_list)
940 GpgmeData sig = NULL;
941 struct passphrase_cb_info_s info;
943 memset (&info, 0, sizeof info);
945 err = gpgme_new (&ctx);
948 err = gpgme_data_new (&sig);
952 if (!getenv("GPG_AGENT_INFO")) {
954 gpgme_set_passphrase_cb (ctx, passphrase_cb, &info);
956 gpgme_set_textmode (ctx, 1);
957 gpgme_set_armor (ctx, 1);
958 gpgme_signers_clear (ctx);
959 for (p = key_list; p != NULL; p = p->next) {
960 err = gpgme_signers_add (ctx, (GpgmeKey) p->data);
964 for (p = key_list; p != NULL; p = p->next)
965 gpgme_key_unref ((GpgmeKey) p->data);
966 g_slist_free (key_list);
970 err = gpgme_op_sign (ctx, plain, sig, GPGME_SIG_MODE_DETACH);
974 debug_print ("signing failed: %s\n", gpgme_strerror (err));
975 gpgme_data_release (sig);
979 debug_print ("signing succeeded\n");
987 * Sign the file and replace its content with the signed one.
990 rfc2015_sign (const char *file, GSList *key_list)
994 int i, clineidx, saved_last;
995 char *clines[3] = {NULL};
997 GpgmeData header = NULL;
998 GpgmeData plain = NULL;
999 GpgmeData sigdata = NULL;
1001 int mime_version_seen = 0;
1002 char *boundary = create_boundary ();
1004 /* Open the source file */
1005 if ((fp = fopen(file, "rb")) == NULL) {
1006 FILE_OP_ERROR(file, "fopen");
1010 err = gpgme_data_new (&header);
1012 err = gpgme_data_new (&plain);
1014 debug_print ("gpgme_data_new failed: %s\n", gpgme_strerror (err));
1018 /* get the content header lines from the source */
1021 while (!err && fgets(buf, sizeof(buf), fp)) {
1022 /* fixme: check for overlong lines */
1023 if (headerp (buf, content_names)) {
1024 if (clineidx >= DIM (clines)) {
1025 debug_print ("rfc2015_sign: too many content lines\n");
1028 clines[clineidx++] = g_strdup (buf);
1034 if (*buf == ' ' || *buf == '\t') {
1035 char *last = clines[clineidx - 1];
1036 clines[clineidx - 1] = g_strconcat (last, buf, NULL);
1042 if (headerp (buf, mime_version_name))
1043 mime_version_seen = 1;
1045 if (buf[0] == '\r' || buf[0] == '\n')
1047 err = gpgme_data_write (header, buf, strlen (buf));
1050 FILE_OP_ERROR (file, "fgets");
1054 /* write them to the temp data and add the rest of the message */
1055 for (i = 0; !err && i < clineidx; i++) {
1056 err = gpgme_data_write (plain, clines[i], strlen (clines[i]));
1059 err = gpgme_data_write (plain, "\r\n", 2 );
1060 while (!err && fgets(buf, sizeof(buf), fp)) {
1061 err = gpgme_data_write (plain, buf, strlen (buf));
1064 FILE_OP_ERROR (file, "fgets");
1068 debug_print ("gpgme_data_write failed: %s\n", gpgme_strerror (err));
1072 sigdata = pgp_sign (plain, key_list);
1076 /* we have the signed message available in sigdata and now we are
1077 * going to rewrite the original file. To be sure that file has
1078 * been truncated we use an approach which should work everywhere:
1079 * close the file and then reopen it for writing. */
1081 FILE_OP_ERROR(file, "fclose");
1084 if ((fp = fopen(file, "wb")) == NULL) {
1085 FILE_OP_ERROR(file, "fopen");
1089 /* Write the rfc822 header and add new content lines */
1090 err = gpgme_data_rewind (header);
1092 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
1093 while (!(err = gpgme_data_read (header, buf, BUFFSIZE, &nread))) {
1094 fwrite (buf, nread, 1, fp);
1096 if (err != GPGME_EOF) {
1097 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
1101 FILE_OP_ERROR (file, "fwrite");
1104 gpgme_data_release (header);
1107 if (!mime_version_seen)
1108 fputs ("MIME-Version: 1\r\n", fp);
1109 fprintf (fp, "Content-Type: multipart/signed; "
1110 "protocol=\"application/pgp-signature\";\r\n"
1111 " boundary=\"%s\"\r\n", boundary );
1113 /* Part 1: signed material */
1114 fprintf (fp, "\r\n--%s\r\n", boundary);
1115 err = gpgme_data_rewind (plain);
1117 debug_print ("gpgme_data_rewind on plain failed: %s\n",
1118 gpgme_strerror (err));
1121 while (!(err = gpgme_data_read (plain, buf, BUFFSIZE, &nread))) {
1122 fwrite (buf, nread, 1, fp);
1124 if (err != GPGME_EOF) {
1125 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
1129 /* Part 2: signature */
1130 fprintf (fp, "\r\n--%s\r\n", boundary);
1131 fputs ("Content-Type: application/pgp-signature\r\n"
1134 err = gpgme_data_rewind (sigdata);
1136 debug_print ("gpgme_data_rewind on sigdata failed: %s\n",
1137 gpgme_strerror (err));
1141 while (!(err = gpgme_data_read (sigdata, buf, BUFFSIZE, &nread))) {
1142 fwrite (buf, nread, 1, fp);
1144 if (err != GPGME_EOF) {
1145 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
1149 /* Final boundary */
1150 fprintf (fp, "\r\n--%s--\r\n\r\n", boundary);
1153 FILE_OP_ERROR (file, "fwrite");
1157 gpgme_data_release (header);
1158 gpgme_data_release (plain);
1159 gpgme_data_release (sigdata);
1166 gpgme_data_release (header);
1167 gpgme_data_release (plain);
1168 gpgme_data_release (sigdata);
1170 return -1; /* error */
1175 * Create a new boundary in a way that it is very unlikely that this
1176 * will occur in the following text. It would be easy to ensure
1177 * uniqueness if everything is either quoted-printable or base64
1178 * encoded (note that conversion is allowed), but because MIME bodies
1179 * may be nested, it may happen that the same boundary has already
1180 * been used. We avoid scanning the message for conflicts and hope the
1183 * boundary := 0*69<bchars> bcharsnospace
1184 * bchars := bcharsnospace / " "
1185 * bcharsnospace := DIGIT / ALPHA / "'" / "(" / ")" /
1186 * "+" / "_" / "," / "-" / "." /
1187 * "/" / ":" / "=" / "?"
1191 create_boundary (void)
1193 static char tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1194 "abcdefghijklmnopqrstuvwxyz"
1195 "1234567890'()+_,./:=?";
1202 /* We make the boundary depend on the pid, so that all running
1203 * processed generate different values even when they have been
1204 * started within the same second and srand48(time(NULL)) has been
1205 * used. I can't see whether this is really an advantage but it
1206 * doesn't do any harm.
1209 for(i = 0; i < sizeof(buf) - 1; i++) {
1210 buf[i] = tbl[(lrand48() ^ pid) % (sizeof(tbl) - 1)]; /* fill with random */
1211 if(buf[i] == '=' && equal == -1)
1216 /* now make sure that we do have the sequence "=." in it which cannot
1217 * be matched by quoted-printable or base64 encoding */
1218 if(equal != -1 && (equal+1) < i)
1225 return g_strdup(buf);
1228 #endif /* USE_GPGME */