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 char *create_boundary (void);
67 static void dump_mimeinfo (const char *text, MimeInfo *x)
69 debug_print ("MimeInfo[%s] %p level=%d\n",
70 text, x, x? x->level:0 );
74 debug_print (" enc=`%s' enc_type=%d mime_type=%d\n",
75 x->encoding, x->encoding_type, x->mime_type );
76 debug_print (" cont_type=`%s' cs=`%s' name=`%s' bnd=`%s'\n",
77 x->content_type, x->charset, x->name, x->boundary );
78 debug_print (" cont_disp=`%s' fname=`%s' fpos=%ld size=%u, lvl=%d\n",
79 x->content_disposition, x->filename, x->fpos, x->size,
81 dump_mimeinfo (".main", x->main );
82 dump_mimeinfo (".sub", x->sub );
83 dump_mimeinfo (".next", x->next );
84 debug_print ("MimeInfo[.parent] %p\n", x );
85 dump_mimeinfo (".children", x->children );
86 dump_mimeinfo (".plaintext", x->plaintext );
89 static void dump_part ( MimeInfo *mimeinfo, FILE *fp )
91 unsigned int size = mimeinfo->size;
94 if (fseek (fp, mimeinfo->fpos, SEEK_SET)) {
95 debug_print ("dump_part: fseek error\n");
99 debug_print ("--- begin dump_part ----\n");
100 while (size-- && (c = getc (fp)) != EOF)
103 debug_print ("dump_part: read error\n");
104 debug_print ("--- end dump_part ----\n");
109 rfc2015_disable_all (void)
111 /* FIXME: set a flag, so that we don't bother the user with failed
117 rfc2015_secure_remove (const char *fname)
121 /* fixme: overwrite the file first */
127 sig_status_to_string (GpgmeSigStat status)
132 case GPGME_SIG_STAT_NONE:
133 result = _("Oops: Signature not verified");
135 case GPGME_SIG_STAT_NOSIG:
136 result = _("No signature found");
138 case GPGME_SIG_STAT_GOOD:
139 result = _("Good signature");
141 case GPGME_SIG_STAT_BAD:
142 result = _("BAD signature");
144 case GPGME_SIG_STAT_NOKEY:
145 result = _("No public key to verify the signature");
147 case GPGME_SIG_STAT_ERROR:
148 result = _("Error verifying the signature");
150 case GPGME_SIG_STAT_DIFF:
151 result = _("Different results for signatures");
154 result = _("Error: Unknown status");
162 sig_status_with_name (GpgmeSigStat status)
167 case GPGME_SIG_STAT_NONE:
168 result = _("Oops: Signature not verified");
170 case GPGME_SIG_STAT_NOSIG:
171 result = _("No signature found");
173 case GPGME_SIG_STAT_GOOD:
174 result = _("Good signature from \"%s\"");
176 case GPGME_SIG_STAT_BAD:
177 result = _("BAD signature from \"%s\"");
179 case GPGME_SIG_STAT_NOKEY:
180 result = _("No public key to verify the signature");
182 case GPGME_SIG_STAT_ERROR:
183 result = _("Error verifying the signature");
185 case GPGME_SIG_STAT_DIFF:
186 result = _("Different results for signatures");
189 result = _("Error: Unknown status");
197 sig_status_for_key(GString *str, GpgmeCtx ctx, GpgmeSigStat status,
198 GpgmeKey key, const gchar *fpr)
203 uid = gpgme_key_get_string_attr (key, GPGME_ATTR_USERID, NULL, idx);
205 g_string_sprintfa (str, "%s\n",
206 sig_status_to_string (status));
207 if ((fpr != NULL) && (*fpr != '\0'))
208 g_string_sprintfa (str, "Key fingerprint: %s\n", fpr);
209 g_string_append (str, _("Cannot find user ID for this key."));
212 g_string_sprintfa (str, sig_status_with_name (status), uid);
213 g_string_append (str, "\n");
216 uid = gpgme_key_get_string_attr (key, GPGME_ATTR_USERID,
220 g_string_sprintfa (str, _(" aka \"%s\"\n"),
226 sig_status_full (GpgmeCtx ctx)
235 struct tm *ctime_val;
239 str = g_string_new ("");
241 fpr = gpgme_get_sig_status (ctx, sig_idx, &status, &created);
242 while (fpr != NULL) {
244 ctime_val = localtime (&created);
245 strftime (ctime_str, sizeof (ctime_str), "%c",
247 g_string_sprintfa (str,
248 _("Signature made at %s\n"),
251 err = gpgme_get_sig_key (ctx, sig_idx, &key);
253 g_string_sprintfa (str, "%s\n",
254 sig_status_to_string (status));
255 if ((fpr != NULL) && (*fpr != '\0'))
256 g_string_sprintfa (str,
257 _("Key fingerprint: %s\n"),
260 sig_status_for_key (str, ctx, status, key, fpr);
261 gpgme_key_unref (key);
263 g_string_append (str, "\n\n");
265 fpr = gpgme_get_sig_status (ctx, ++sig_idx, &status, &created);
269 g_string_free (str, FALSE);
273 static void check_signature (MimeInfo *mimeinfo, MimeInfo *partinfo, FILE *fp)
277 GpgmeData sig = NULL, text = NULL;
278 GpgmeSigStat status = GPGME_SIG_STAT_NONE;
279 GpgmegtkSigStatus statuswindow = NULL;
280 const char *result = NULL;
282 if (prefs_common.gpg_signature_popup)
283 statuswindow = gpgmegtk_sig_status_create ();
285 err = gpgme_new (&ctx);
287 debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
291 /* don't include the last character (LF). It does not belong to the
293 err = gpgme_data_new_from_filepart (&text, NULL, fp,
294 mimeinfo->children->fpos,
295 mimeinfo->children->size ?
296 (mimeinfo->children->size - 1) : 0 );
298 err = gpgme_data_new_from_filepart (&sig, NULL, fp,
299 partinfo->fpos, partinfo->size);
301 debug_print ("gpgme_data_new_from_filepart failed: %s\n",
302 gpgme_strerror (err));
306 err = gpgme_op_verify (ctx, sig, text, &status);
308 debug_print ("gpgme_op_verify failed: %s\n", gpgme_strerror (err));
312 /* FIXME: check what the heck this sig_status_full stuff is.
313 * it should better go into sigstatus.c */
314 g_free (partinfo->sigstatus_full);
315 partinfo->sigstatus_full = sig_status_full (ctx);
318 result = gpgmegtk_sig_status_to_string(status);
319 debug_print("verification status: %s\n", result);
320 if (prefs_common.gpg_signature_popup)
321 gpgmegtk_sig_status_update (statuswindow, ctx);
323 g_free (partinfo->sigstatus);
324 partinfo->sigstatus = g_strdup (result);
326 gpgme_data_release (sig);
327 gpgme_data_release (text);
329 if (prefs_common.gpg_signature_popup)
330 gpgmegtk_sig_status_destroy (statuswindow);
334 * Copy a gpgme data object to a temporary file and
335 * return this filename
339 copy_gpgmedata_to_temp (GpgmeData data, guint *length)
348 tmp = g_strdup_printf("%s%cgpgtmp.%08x",
349 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id );
351 if ((fp = fopen(tmp, "wb")) == NULL) {
352 FILE_OP_ERROR(tmp, "fopen");
357 err = gpgme_data_rewind ( data );
359 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
361 while (!(err = gpgme_data_read (data, buf, 100, &nread))) {
362 fwrite ( buf, nread, 1, fp );
365 if (err != GPGME_EOF)
366 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
376 pgp_decrypt (MimeInfo *partinfo, FILE *fp)
380 GpgmeData cipher = NULL, plain = NULL;
381 struct passphrase_cb_info_s info;
383 memset (&info, 0, sizeof info);
385 err = gpgme_new (&ctx);
387 debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
391 err = gpgme_data_new_from_filepart (&cipher, NULL, fp,
392 partinfo->fpos, partinfo->size);
394 debug_print ("gpgme_data_new_from_filepart failed: %s\n",
395 gpgme_strerror (err));
399 err = gpgme_data_new (&plain);
401 debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
405 if (!getenv("GPG_AGENT_INFO")) {
407 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
410 err = gpgme_op_decrypt (ctx, cipher, plain);
413 gpgme_data_release (cipher);
415 gpgmegtk_free_passphrase();
416 debug_print ("decryption failed: %s\n", gpgme_strerror (err));
417 gpgme_data_release (plain);
421 debug_print ("** decryption succeeded\n");
427 MimeInfo * rfc2015_find_signature (MimeInfo *mimeinfo)
434 if (g_strcasecmp (mimeinfo->content_type, "multipart/signed"))
437 debug_print ("** multipart/signed encountered\n");
439 /* check that we have at least 2 parts of the correct type */
440 for (partinfo = mimeinfo->children;
441 partinfo != NULL; partinfo = partinfo->next) {
442 if (++n > 1 && !g_strcasecmp (partinfo->content_type,
443 "application/pgp-signature"))
450 gboolean rfc2015_has_signature (MimeInfo *mimeinfo)
452 return rfc2015_find_signature (mimeinfo) != NULL;
455 void rfc2015_check_signature (MimeInfo *mimeinfo, FILE *fp)
459 partinfo = rfc2015_find_signature (mimeinfo);
464 g_message ("** yep, it is a pgp signature");
465 dump_mimeinfo ("gpg-signature", partinfo );
466 dump_part (partinfo, fp );
467 dump_mimeinfo ("signed text", mimeinfo->children );
468 dump_part (mimeinfo->children, fp);
471 check_signature (mimeinfo, partinfo, fp);
474 int rfc2015_is_encrypted (MimeInfo *mimeinfo)
478 if (g_strcasecmp (mimeinfo->content_type, "multipart/encrypted"))
480 /* fixme: we should check the protocol parameter */
484 gboolean rfc2015_msg_is_encrypted (const gchar *file)
490 if ((fp = fopen(file, "rb")) == NULL)
493 mimeinfo = procmime_scan_mime_header(fp);
499 ret = rfc2015_is_encrypted(mimeinfo);
500 procmime_mimeinfo_free_all(mimeinfo);
501 return ret != 0 ? TRUE : FALSE;
505 name_cmp(const char *a, const char *b)
507 for( ; *a && *b; a++, b++) {
509 && toupper(*(unsigned char *)a) != toupper(*(unsigned char *)b))
517 headerp(char *p, char **names)
526 if(p2[-1] == ' ' || p2[-1] == '\t') {
535 for(i = 0 ; names[i] != NULL; i++) {
536 if(!name_cmp (names[i], p))
541 return names[i] != NULL;
545 #define DECRYPTION_ABORT() \
547 procmime_mimeinfo_free_all(tmpinfo); \
548 msginfo->decryption_failed = 1; \
552 void rfc2015_decrypt_message (MsgInfo *msginfo, MimeInfo *mimeinfo, FILE *fp)
555 MimeInfo *tmpinfo, *partinfo;
564 g_return_if_fail (msginfo != NULL);
565 g_return_if_fail (mimeinfo != NULL);
566 g_return_if_fail (fp != NULL);
567 g_return_if_fail (mimeinfo->mime_type == MIME_MULTIPART);
569 debug_print ("** decrypting multipart/encrypted message\n");
572 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
574 tmpinfo = procmime_scan_mime_header(fp);
575 if (!tmpinfo || tmpinfo->mime_type != MIME_MULTIPART) {
579 procmime_scan_multipart_message(tmpinfo, fp);
581 /* check that we have the 2 parts */
582 partinfo = tmpinfo->children;
583 if (!partinfo || !partinfo->next) {
586 if (!g_strcasecmp (partinfo->content_type, "application/pgp-encrypted")) {
587 /* Fixme: check that the version is 1 */
590 partinfo = partinfo->next;
592 !g_strcasecmp (partinfo->content_type, "application/octet-stream")) {
594 g_warning ("oops: pgp_encrypted with more than 2 parts");
600 debug_print ("** yep, it is pgp encrypted\n");
602 plain = pgp_decrypt (partinfo, fp);
607 fname = g_strdup_printf("%s%cplaintext.%08x",
608 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
610 if ((dstfp = fopen(fname, "wb")) == NULL) {
611 FILE_OP_ERROR(fname, "fopen");
616 /* write the orginal header to the new file */
617 if (fseek(fp, tmpinfo->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));
641 procmime_mimeinfo_free_all(tmpinfo);
643 msginfo->plaintext_file = fname;
644 msginfo->decryption_failed = 0;
647 #undef DECRYPTION_ABORT
651 * plain contains an entire mime object.
652 * Encrypt it and return an GpgmeData object with the encrypted version of
653 * the file or NULL in case of error.
656 pgp_encrypt ( GpgmeData plain, GpgmeRecipients rset )
660 GpgmeData cipher = NULL;
662 err = gpgme_new (&ctx);
664 err = gpgme_data_new (&cipher);
666 gpgme_set_armor (ctx, 1);
667 err = gpgme_op_encrypt (ctx, rset, plain, cipher);
671 debug_print ("encryption failed: %s\n", gpgme_strerror (err));
672 gpgme_data_release (cipher);
676 debug_print ("** encryption succeeded\n");
684 * Create and return a list of keys matching a key id
687 GSList *rfc2015_create_signers_list (const char *keyid)
689 GSList *key_list = NULL;
690 GpgmeCtx list_ctx = NULL;
695 err = gpgme_new (&list_ctx);
698 err = gpgme_op_keylist_start (list_ctx, keyid, 1);
701 while ( !(err = gpgme_op_keylist_next (list_ctx, &key)) ) {
702 key_list = g_slist_append (key_list, key);
704 if (err != GPGME_EOF)
707 if (key_list == NULL) {
708 debug_print ("no keys found for keyid \"%s\"\n", keyid);
713 debug_print ("rfc2015_create_signers_list failed: %s\n", gpgme_strerror (err));
714 for (p = key_list; p != NULL; p = p->next)
715 gpgme_key_unref ((GpgmeKey) p->data);
716 g_slist_free (key_list);
719 gpgme_release (list_ctx);
720 return err ? NULL : key_list;
724 * Encrypt the file by extracting all recipients and finding the
725 * encryption keys for all of them. The file content is then replaced
726 * by the encrypted one. */
728 rfc2015_encrypt (const char *file, GSList *recp_list, gboolean ascii_armored)
732 int i, clineidx, saved_last;
733 char *clines[3] = {NULL};
735 GpgmeData header = NULL;
736 GpgmeData plain = NULL;
737 GpgmeData cipher = NULL;
738 GpgmeRecipients rset = NULL;
740 int mime_version_seen = 0;
741 char *boundary = create_boundary ();
743 /* Create the list of recipients */
744 rset = gpgmegtk_recipient_selection (recp_list);
746 debug_print ("error creating recipient list\n" );
750 /* Open the source file */
751 if ((fp = fopen(file, "rb")) == NULL) {
752 FILE_OP_ERROR(file, "fopen");
756 err = gpgme_data_new (&header);
758 err = gpgme_data_new (&plain);
760 debug_print ("gpgme_data_new failed: %s\n", gpgme_strerror (err));
764 /* get the content header lines from the source */
767 while (!err && fgets(buf, sizeof(buf), fp)) {
768 /* fixme: check for overlong lines */
769 if (headerp (buf, content_names)) {
770 if (clineidx >= DIM (clines)) {
771 debug_print ("rfc2015_encrypt: too many content lines\n");
774 clines[clineidx++] = g_strdup (buf);
780 if (*buf == ' ' || *buf == '\t') {
781 char *last = clines[clineidx-1];
782 clines[clineidx-1] = g_strconcat (last, buf, NULL);
788 if (headerp (buf, mime_version_name))
789 mime_version_seen = 1;
791 if (buf[0] == '\r' || buf[0] == '\n')
793 err = gpgme_data_write (header, buf, strlen (buf));
796 FILE_OP_ERROR (file, "fgets");
800 /* write them to the temp data and add the rest of the message */
801 for (i = 0; !err && i < clineidx; i++) {
802 debug_print ("%% %s:%d: cline=`%s'", __FILE__ ,__LINE__, clines[i]);
803 err = gpgme_data_write (plain, clines[i], strlen (clines[i]));
806 err = gpgme_data_write (plain, "\r\n", 2);
807 while (!err && fgets(buf, sizeof(buf), fp)) {
808 err = gpgme_data_write (plain, buf, strlen (buf));
811 FILE_OP_ERROR (file, "fgets");
815 debug_print ("gpgme_data_write failed: %s\n", gpgme_strerror (err));
819 cipher = pgp_encrypt (plain, rset);
820 gpgme_data_release (plain); plain = NULL;
821 gpgme_recipients_release (rset); rset = NULL;
825 /* we have the encrypted message available in cipher and now we
826 * are going to rewrite the source file. To be sure that file has
827 * been truncated we use an approach which should work everywhere:
828 * close the file and then reopen it for writing. It is important
829 * that this works, otherwise it may happen that parts of the
830 * plaintext are still in the file (The encrypted stuff is, due to
831 * compression, usually shorter than the plaintext).
833 * Yes, there is a race condition here, but everyone, who is so
834 * stupid to store the temp file with the plaintext in a public
835 * directory has to live with this anyway. */
837 FILE_OP_ERROR(file, "fclose");
840 if ((fp = fopen(file, "wb")) == NULL) {
841 FILE_OP_ERROR(file, "fopen");
845 /* Write the header, append new content lines, part 1 and part 2 header */
846 err = gpgme_data_rewind (header);
848 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
851 while (!(err = gpgme_data_read (header, buf, BUFFSIZE, &nread))) {
852 fwrite (buf, nread, 1, fp);
854 if (err != GPGME_EOF) {
855 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
859 FILE_OP_ERROR (file, "fwrite");
862 gpgme_data_release (header); header = NULL;
864 if (!mime_version_seen)
865 fputs ("MIME-Version: 1\r\n", fp);
869 "Content-Type: text/plain; charset=US-ASCII\r\n"
870 "Content-Transfer-Encoding: 7bit\r\n"
874 "Content-Type: multipart/encrypted;"
875 " protocol=\"application/pgp-encrypted\";\r\n"
876 " boundary=\"%s\"\r\n"
879 "Content-Type: application/pgp-encrypted\r\n"
884 "Content-Type: application/octet-stream\r\n"
886 boundary, boundary, boundary);
889 /* append the encrypted stuff */
890 err = gpgme_data_rewind (cipher);
892 debug_print ("** gpgme_data_rewind on cipher failed: %s\n",
893 gpgme_strerror (err));
897 while (!(err = gpgme_data_read (cipher, buf, BUFFSIZE, &nread))) {
898 fwrite (buf, nread, 1, fp);
900 if (err != GPGME_EOF) {
901 debug_print ("** gpgme_data_read failed: %s\n", gpgme_strerror (err));
905 /* and the final boundary */
906 if (!ascii_armored) {
914 FILE_OP_ERROR (file, "fwrite");
918 gpgme_data_release (cipher);
924 gpgme_data_release (header);
925 gpgme_data_release (plain);
926 gpgme_data_release (cipher);
927 gpgme_recipients_release (rset);
929 return -1; /* error */
933 * plain contains an entire mime object. Sign it and return an
934 * GpgmeData object with the signature of it or NULL in case of error.
935 * r_siginfo returns an XML object with information about the signature.
938 pgp_sign (GpgmeData plain, GSList *key_list, gboolean clearsign,
944 GpgmeData sig = NULL;
945 struct passphrase_cb_info_s info;
948 memset (&info, 0, sizeof info);
950 err = gpgme_new (&ctx);
953 err = gpgme_data_new (&sig);
957 if (!getenv("GPG_AGENT_INFO")) {
959 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
961 gpgme_set_textmode (ctx, 1);
962 gpgme_set_armor (ctx, 1);
963 gpgme_signers_clear (ctx);
964 for (p = key_list; p != NULL; p = p->next) {
965 err = gpgme_signers_add (ctx, (GpgmeKey) p->data);
969 for (p = key_list; p != NULL; p = p->next)
970 gpgme_key_unref ((GpgmeKey) p->data);
971 g_slist_free (key_list);
977 clearsign ? GPGME_SIG_MODE_CLEAR : GPGME_SIG_MODE_DETACH);
979 *r_siginfo = gpgme_get_op_info (ctx, 0);
983 gpgmegtk_free_passphrase();
984 debug_print ("signing failed: %s\n", gpgme_strerror (err));
985 gpgme_data_release (sig);
989 debug_print ("signing succeeded\n");
997 * Find TAG in XML and return a pointer into xml set just behind the
998 * closing angle. Return NULL if not found.
1001 find_xml_tag (const char *xml, const char *tag)
1003 int taglen = strlen (tag);
1004 const char *s = xml;
1006 while ( (s = strchr (s, '<')) ) {
1008 if (!strncmp (s, tag, taglen)) {
1009 const char *s2 = s + taglen;
1010 if (*s2 == '>' || isspace (*(const unsigned char*)s2) ) {
1012 while (*s2 && *s2 != '>') /* skip attributes */
1014 /* fixme: do need to handle angles inside attribute vallues? */
1015 return *s2? (s2+1):NULL;
1018 while (*s && *s != '>') /* skip to end of tag */
1026 * Extract the micalg from an GnupgOperationInfo XML container.
1029 extract_micalg (char *xml)
1033 s = find_xml_tag (xml, "GnupgOperationInfo");
1035 const char *s_end = find_xml_tag (s, "/GnupgOperationInfo");
1036 s = find_xml_tag (s, "signature");
1037 if (s && s_end && s < s_end) {
1038 const char *s_end2 = find_xml_tag (s, "/signature");
1039 if (s_end2 && s_end2 < s_end) {
1040 s = find_xml_tag (s, "micalg");
1041 if (s && s < s_end2) {
1042 s_end = strchr (s, '<');
1044 char *p = g_malloc (s_end - s + 1);
1045 memcpy (p, s, s_end - s);
1058 * Sign the file and replace its content with the signed one.
1061 rfc2015_sign (const char *file, GSList *key_list)
1065 int i, clineidx, saved_last;
1066 char *clines[3] = {NULL};
1068 GpgmeData header = NULL;
1069 GpgmeData plain = NULL;
1070 GpgmeData sigdata = NULL;
1072 int mime_version_seen = 0;
1073 char *boundary = create_boundary ();
1074 char *micalg = NULL;
1077 /* Open the source file */
1078 if ((fp = fopen(file, "rb")) == NULL) {
1079 FILE_OP_ERROR(file, "fopen");
1083 err = gpgme_data_new (&header);
1085 err = gpgme_data_new (&plain);
1087 debug_print ("gpgme_data_new failed: %s\n", gpgme_strerror (err));
1091 /* get the content header lines from the source */
1094 while (!err && fgets(buf, sizeof(buf), fp)) {
1095 /* fixme: check for overlong lines */
1096 if (headerp (buf, content_names)) {
1097 if (clineidx >= DIM (clines)) {
1098 debug_print ("rfc2015_sign: too many content lines\n");
1101 clines[clineidx++] = g_strdup (buf);
1107 if (*buf == ' ' || *buf == '\t') {
1108 char *last = clines[clineidx - 1];
1109 clines[clineidx - 1] = g_strconcat (last, buf, NULL);
1115 if (headerp (buf, mime_version_name))
1116 mime_version_seen = 1;
1118 if (buf[0] == '\r' || buf[0] == '\n')
1120 err = gpgme_data_write (header, buf, strlen (buf));
1123 FILE_OP_ERROR (file, "fgets");
1127 /* write them to the temp data and add the rest of the message */
1128 for (i = 0; !err && i < clineidx; i++) {
1129 err = gpgme_data_write (plain, clines[i], strlen (clines[i]));
1132 err = gpgme_data_write (plain, "\r\n", 2 );
1133 while (!err && fgets(buf, sizeof(buf), fp)) {
1134 err = gpgme_data_write (plain, buf, strlen (buf));
1137 FILE_OP_ERROR (file, "fgets");
1141 debug_print ("gpgme_data_write failed: %s\n", gpgme_strerror (err));
1145 sigdata = pgp_sign (plain, key_list, FALSE, &siginfo);
1147 micalg = extract_micalg (siginfo);
1153 /* we have the signed message available in sigdata and now we are
1154 * going to rewrite the original file. To be sure that file has
1155 * been truncated we use an approach which should work everywhere:
1156 * close the file and then reopen it for writing. */
1158 FILE_OP_ERROR(file, "fclose");
1161 if ((fp = fopen(file, "wb")) == NULL) {
1162 FILE_OP_ERROR(file, "fopen");
1166 /* Write the rfc822 header and add new content lines */
1167 err = gpgme_data_rewind (header);
1169 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
1170 while (!(err = gpgme_data_read (header, buf, BUFFSIZE, &nread))) {
1171 fwrite (buf, nread, 1, fp);
1173 if (err != GPGME_EOF) {
1174 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
1178 FILE_OP_ERROR (file, "fwrite");
1181 gpgme_data_release (header);
1184 if (!mime_version_seen)
1185 fputs ("MIME-Version: 1.0\r\n", fp);
1186 fprintf (fp, "Content-Type: multipart/signed; "
1187 "protocol=\"application/pgp-signature\";\r\n");
1189 fprintf (fp, " micalg=\"%s\";", micalg);
1190 fprintf (fp, " boundary=\"%s\"\r\n", boundary);
1192 /* Part 1: signed material */
1196 err = gpgme_data_rewind (plain);
1198 debug_print ("gpgme_data_rewind on plain failed: %s\n",
1199 gpgme_strerror (err));
1202 while (!(err = gpgme_data_read (plain, buf, BUFFSIZE, &nread))) {
1203 fwrite (buf, nread, 1, fp);
1205 if (err != GPGME_EOF) {
1206 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
1210 /* Part 2: signature */
1214 fputs ("Content-Type: application/pgp-signature\r\n"
1217 err = gpgme_data_rewind (sigdata);
1219 debug_print ("gpgme_data_rewind on sigdata failed: %s\n",
1220 gpgme_strerror (err));
1224 while (!(err = gpgme_data_read (sigdata, buf, BUFFSIZE, &nread))) {
1225 fwrite (buf, nread, 1, fp);
1227 if (err != GPGME_EOF) {
1228 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
1232 /* Final boundary */
1238 FILE_OP_ERROR (file, "fwrite");
1242 gpgme_data_release (header);
1243 gpgme_data_release (plain);
1244 gpgme_data_release (sigdata);
1252 gpgme_data_release (header);
1253 gpgme_data_release (plain);
1254 gpgme_data_release (sigdata);
1257 return -1; /* error */
1262 * Sign the file with clear text and replace its content with the signed one.
1265 rfc2015_clearsign (const gchar *file, GSList *key_list)
1268 gchar buf[BUFFSIZE];
1270 GpgmeData text = NULL;
1271 GpgmeData sigdata = NULL;
1275 if ((fp = fopen(file, "rb")) == NULL) {
1276 FILE_OP_ERROR(file, "fopen");
1280 err = gpgme_data_new(&text);
1282 debug_print("gpgme_data_new failed: %s\n", gpgme_strerror(err));
1286 while (!err && fgets(buf, sizeof(buf), fp)) {
1287 err = gpgme_data_write(text, buf, strlen(buf));
1290 FILE_OP_ERROR(file, "fgets");
1294 debug_print("gpgme_data_write failed: %s\n", gpgme_strerror(err));
1298 sigdata = pgp_sign(text, key_list, TRUE, &siginfo);
1305 if (fclose(fp) == EOF) {
1306 FILE_OP_ERROR(file, "fclose");
1310 if ((fp = fopen(file, "wb")) == NULL) {
1311 FILE_OP_ERROR(file, "fopen");
1315 err = gpgme_data_rewind(sigdata);
1317 debug_print("gpgme_data_rewind on sigdata failed: %s\n",
1318 gpgme_strerror(err));
1322 while (!(err = gpgme_data_read(sigdata, buf, sizeof(buf), &nread))) {
1323 fwrite(buf, nread, 1, fp);
1325 if (err != GPGME_EOF) {
1326 debug_print("gpgme_data_read failed: %s\n", gpgme_strerror(err));
1330 if (fclose(fp) == EOF) {
1331 FILE_OP_ERROR(file, "fclose");
1335 gpgme_data_release(text);
1336 gpgme_data_release(sigdata);
1342 gpgme_data_release(text);
1343 gpgme_data_release(sigdata);
1349 * Create a new boundary in a way that it is very unlikely that this
1350 * will occur in the following text. It would be easy to ensure
1351 * uniqueness if everything is either quoted-printable or base64
1352 * encoded (note that conversion is allowed), but because MIME bodies
1353 * may be nested, it may happen that the same boundary has already
1354 * been used. We avoid scanning the message for conflicts and hope the
1357 * boundary := 0*69<bchars> bcharsnospace
1358 * bchars := bcharsnospace / " "
1359 * bcharsnospace := DIGIT / ALPHA / "'" / "(" / ")" /
1360 * "+" / "_" / "," / "-" / "." /
1361 * "/" / ":" / "=" / "?"
1365 create_boundary (void)
1367 static char tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1368 "abcdefghijklmnopqrstuvwxyz"
1369 "1234567890'()+_,./:=?";
1376 /* We make the boundary depend on the pid, so that all running
1377 * processed generate different values even when they have been
1378 * started within the same second and srand48(time(NULL)) has been
1379 * used. I can't see whether this is really an advantage but it
1380 * doesn't do any harm.
1383 for(i = 0; i < sizeof(buf) - 1; i++) {
1384 buf[i] = tbl[(lrand48() ^ pid) % (sizeof(tbl) - 1)]; /* fill with random */
1385 if(buf[i] == '=' && equal == -1)
1390 /* now make sure that we do have the sequence "=." in it which cannot
1391 * be matched by quoted-printable or base64 encoding */
1392 if(equal != -1 && (equal+1) < i)
1399 return g_strdup(buf);
1402 #endif /* USE_GPGME */