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_secure_remove (const char *fname)
124 /* fixme: overwrite the file first */
130 sig_status_to_string (GpgmeSigStat status)
135 case GPGME_SIG_STAT_NONE:
136 result = _("Oops: Signature not verified");
138 case GPGME_SIG_STAT_NOSIG:
139 result = _("No signature found");
141 case GPGME_SIG_STAT_GOOD:
142 result = _("Good signature");
144 case GPGME_SIG_STAT_GOOD_EXP:
145 result = _("Good signature but it has expired");
147 case GPGME_SIG_STAT_GOOD_EXPKEY:
148 result = _("Good signature but the key has expired");
150 case GPGME_SIG_STAT_BAD:
151 result = _("BAD signature");
153 case GPGME_SIG_STAT_NOKEY:
154 result = _("No public key to verify the signature");
156 case GPGME_SIG_STAT_ERROR:
157 result = _("Error verifying the signature");
159 case GPGME_SIG_STAT_DIFF:
160 result = _("Different results for signatures");
163 result = _("Error: Unknown status");
171 sig_status_with_name (GpgmeSigStat status)
176 case GPGME_SIG_STAT_NONE:
177 result = _("Oops: Signature not verified");
179 case GPGME_SIG_STAT_NOSIG:
180 result = _("No signature found");
182 case GPGME_SIG_STAT_GOOD:
183 result = _("Good signature from \"%s\"");
185 case GPGME_SIG_STAT_GOOD_EXP:
186 result = _("Good signature from \"%s\" but it has expired");
188 case GPGME_SIG_STAT_GOOD_EXPKEY:
189 result = _("Good signature from \"%s\" but the key has expired");
191 case GPGME_SIG_STAT_BAD:
192 result = _("BAD signature from \"%s\"");
194 case GPGME_SIG_STAT_NOKEY:
195 result = _("No public key to verify the signature");
197 case GPGME_SIG_STAT_ERROR:
198 result = _("Error verifying the signature");
200 case GPGME_SIG_STAT_DIFF:
201 result = _("Different results for signatures");
204 result = _("Error: Unknown status");
212 sig_status_for_key(GString *str, GpgmeCtx ctx, GpgmeSigStat status,
213 GpgmeKey key, const gchar *fpr)
218 uid = gpgme_key_get_string_attr (key, GPGME_ATTR_USERID, NULL, idx);
220 g_string_sprintfa (str, "%s\n",
221 sig_status_to_string (status));
222 if ((fpr != NULL) && (*fpr != '\0'))
223 g_string_sprintfa (str, "Key fingerprint: %s\n", fpr);
224 g_string_append (str, _("Cannot find user ID for this key."));
225 sig_expiration_check(str, ctx, key, status, 0);
228 g_string_sprintfa (str, sig_status_with_name (status), uid);
229 g_string_append (str, "\n");
232 uid = gpgme_key_get_string_attr (key, GPGME_ATTR_USERID,
236 g_string_sprintfa (str, _(" aka \"%s\"\n"),
239 sig_expiration_check(str, ctx, key, status, 0);
243 sig_expiration_check(GString *str, GpgmeCtx ctx, GpgmeKey key,
244 GpgmeSigStat status, int idx)
246 if (status == GPGME_SIG_STAT_GOOD_EXP)
247 sig_expired(str, ctx, idx);
248 else if (status == GPGME_SIG_STAT_GOOD_EXPKEY)
249 sig_key_expired(str, key, idx);
253 sig_expired(GString *str, GpgmeCtx ctx, int idx)
255 unsigned long exp_time;
256 exp_time = gpgme_get_sig_ulong_attr(ctx, idx, GPGME_ATTR_EXPIRE, 0);
257 g_string_sprintfa(str, _("Signature expired %s"), ctime(&exp_time));
261 sig_key_expired(GString *str, GpgmeKey key, int idx)
263 unsigned long exp_time;
264 exp_time = gpgme_key_get_ulong_attr(key, GPGME_ATTR_EXPIRE, NULL, idx);
265 g_string_sprintfa(str, _("Key expired %s"), ctime(&exp_time));
269 sig_status_full (GpgmeCtx ctx)
278 struct tm *ctime_val;
282 str = g_string_new ("");
284 fpr = gpgme_get_sig_status (ctx, sig_idx, &status, &created);
285 while (fpr != NULL) {
287 ctime_val = localtime (&created);
288 strftime (ctime_str, sizeof (ctime_str), "%c",
290 g_string_sprintfa (str,
291 _("Signature made at %s\n"),
294 err = gpgme_get_sig_key (ctx, sig_idx, &key);
296 g_string_sprintfa (str, "%s\n",
297 sig_status_to_string (status));
298 if ((fpr != NULL) && (*fpr != '\0'))
299 g_string_sprintfa (str,
300 _("Key fingerprint: %s\n"),
303 const char *key_id_str;
305 sig_status_for_key (str, ctx, status, key, fpr);
306 key_id_str = gpgme_key_get_string_attr(key,
307 GPGME_ATTR_KEYID, NULL, 0);
309 g_string_sprintfa (str, _("Key ID: %s\n"), key_id_str);
310 gpgme_key_unref (key);
312 g_string_append (str, "\n\n");
314 fpr = gpgme_get_sig_status (ctx, ++sig_idx, &status, &created);
318 g_string_free (str, FALSE);
322 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->length > 0) {
346 if (fseek(fp, mimeinfo->children->offset + mimeinfo->children->length - 1,
351 if (fgetc(fp) == '\n') {
353 if (mimeinfo->children->length > 1) {
354 if (fseek(fp, mimeinfo->children->offset + mimeinfo->children->length - 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->offset,
368 mimeinfo->children->length - 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->offset, partinfo->length);
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);
426 * Copy a gpgme data object to a temporary file and
427 * return this filename
431 copy_gpgmedata_to_temp (GpgmeData data, guint *length)
440 tmp = g_strdup_printf("%s%cgpgtmp.%08x",
441 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id );
443 if ((fp = fopen(tmp, "wb")) == NULL) {
444 FILE_OP_ERROR(tmp, "fopen");
449 err = gpgme_data_rewind ( data );
451 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
453 while (!(err = gpgme_data_read (data, buf, 100, &nread))) {
454 fwrite ( buf, nread, 1, fp );
457 if (err != GPGME_EOF)
458 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
468 pgp_decrypt (MimeInfo *partinfo, FILE *fp)
472 GpgmeData cipher = NULL, plain = NULL;
473 struct passphrase_cb_info_s info;
475 memset (&info, 0, sizeof info);
477 err = gpgme_new (&ctx);
479 debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
483 err = gpgme_data_new_from_filepart (&cipher, NULL, fp,
484 partinfo->offset, partinfo->length);
486 debug_print ("gpgme_data_new_from_filepart failed: %s\n",
487 gpgme_strerror (err));
491 err = gpgme_data_new (&plain);
493 debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
497 if (!getenv("GPG_AGENT_INFO")) {
499 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
502 err = gpgme_op_decrypt (ctx, cipher, plain);
505 gpgme_data_release (cipher);
507 gpgmegtk_free_passphrase();
508 debug_print ("decryption failed: %s\n", gpgme_strerror (err));
509 gpgme_data_release (plain);
513 debug_print ("** decryption succeeded\n");
519 int rfc2015_is_encrypted (MimeInfo *mimeinfo)
521 if (!mimeinfo || mimeinfo->type != MIMETYPE_MULTIPART)
523 if (g_strcasecmp (mimeinfo->subtype, "encrypted"))
525 /* fixme: we should check the protocol parameter */
529 gboolean rfc2015_msg_is_encrypted (const gchar *file)
536 if ((fp = fopen(file, "rb")) == NULL)
539 mimeinfo = procmime_scan_mime_header(fp);
545 ret = rfc2015_is_encrypted(mimeinfo);
546 procmime_mimeinfo_free_all(mimeinfo);
548 return ret != 0 ? TRUE : FALSE;
552 name_cmp(const char *a, const char *b)
554 for( ; *a && *b; a++, b++) {
556 && toupper(*(unsigned char *)a) != toupper(*(unsigned char *)b))
564 headerp(char *p, char **names)
573 if(p2[-1] == ' ' || p2[-1] == '\t') {
582 for(i = 0 ; names[i] != NULL; i++) {
583 if(!name_cmp (names[i], p))
588 return names[i] != NULL;
592 #define DECRYPTION_ABORT() \
594 procmime_mimeinfo_free_all(tmpinfo); \
595 msginfo->decryption_failed = 1; \
599 void rfc2015_decrypt_message (MsgInfo *msginfo, MimeInfo *mimeinfo, FILE *fp)
602 MimeInfo *tmpinfo, *partinfo;
612 g_return_if_fail (msginfo != NULL);
613 g_return_if_fail (mimeinfo != NULL);
614 g_return_if_fail (fp != NULL);
615 g_return_if_fail (mimeinfo->type == MIMETYPE_MULTIPART);
617 debug_print ("** decrypting multipart/encrypted message\n");
622 if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0)
624 tmpinfo = procmime_scan_mime_header(fp);
625 if (!tmpinfo || tmpinfo->type != MIMETYPE_MULTIPART) {
629 procmime_scan_message(tmpinfo);
631 /* check that we have the 2 parts */
632 partinfo = tmpinfo->children;
633 if (!partinfo || !partinfo->next) {
636 if ((partinfo->type == MIMETYPE_APPLICATION) && !g_strcasecmp(partinfo->subtype, "pgp-encrypted")) {
637 /* Fixme: check that the version is 1 */
640 partinfo = partinfo->next;
641 if (ver_ok && (partinfo->type == MIMETYPE_APPLICATION) &&
642 !g_strcasecmp (partinfo->subtype, "octet-stream")) {
644 g_warning ("oops: pgp_encrypted with more than 2 parts");
650 debug_print ("** yep, it is pgp encrypted\n");
652 plain = pgp_decrypt (partinfo, fp);
657 fname = g_strdup_printf("%s%cplaintext.%08x",
658 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
660 if ((dstfp = fopen(fname, "wb")) == NULL) {
661 FILE_OP_ERROR(fname, "fopen");
666 /* write the orginal header to the new file */
667 if (fseek(fp, tmpinfo->offset, SEEK_SET) < 0)
671 while (fgets(buf, sizeof(buf), fp)) {
672 if (headerp (buf, content_names)) {
677 if (buf[0] == ' ' || buf[0] == '\t')
681 if (buf[0] == '\r' || buf[0] == '\n')
686 err = gpgme_data_rewind (plain);
688 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
690 while (!(err = gpgme_data_read (plain, buf, sizeof(buf), &nread))) {
691 fwrite (buf, nread, 1, dstfp);
694 if (err != GPGME_EOF) {
695 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
699 procmime_mimeinfo_free_all(tmpinfo);
701 msginfo->plaintext_file = fname;
702 msginfo->decryption_failed = 0;
707 #undef DECRYPTION_ABORT
711 * plain contains an entire mime object.
712 * Encrypt it and return an GpgmeData object with the encrypted version of
713 * the file or NULL in case of error.
716 pgp_encrypt ( GpgmeData plain, GpgmeRecipients rset )
720 GpgmeData cipher = NULL;
722 err = gpgme_new (&ctx);
724 err = gpgme_data_new (&cipher);
726 gpgme_set_armor (ctx, 1);
727 err = gpgme_op_encrypt (ctx, rset, plain, cipher);
731 debug_print ("encryption failed: %s\n", gpgme_strerror (err));
732 gpgme_data_release (cipher);
736 debug_print ("** encryption succeeded\n");
744 * Create and return a list of keys matching a key id
747 GSList *rfc2015_create_signers_list (const char *keyid)
749 GSList *key_list = NULL;
750 GpgmeCtx list_ctx = NULL;
755 err = gpgme_new (&list_ctx);
758 err = gpgme_op_keylist_start (list_ctx, keyid, 1);
761 while ( !(err = gpgme_op_keylist_next (list_ctx, &key)) ) {
762 key_list = g_slist_append (key_list, key);
764 if (err != GPGME_EOF)
767 if (key_list == NULL) {
768 debug_print ("no keys found for keyid \"%s\"\n", keyid);
773 debug_print ("rfc2015_create_signers_list failed: %s\n", gpgme_strerror (err));
774 for (p = key_list; p != NULL; p = p->next)
775 gpgme_key_unref ((GpgmeKey) p->data);
776 g_slist_free (key_list);
779 gpgme_release (list_ctx);
780 return err ? NULL : key_list;
784 * Encrypt the file by extracting all recipients and finding the
785 * encryption keys for all of them. The file content is then replaced
786 * by the encrypted one. */
788 rfc2015_encrypt (const char *file, GSList *recp_list, gboolean ascii_armored)
792 int i, clineidx, saved_last;
793 char *clines[3] = {NULL};
795 GpgmeData header = NULL;
796 GpgmeData plain = NULL;
797 GpgmeData cipher = NULL;
798 GpgmeRecipients rset = NULL;
800 int mime_version_seen = 0;
803 boundary = generate_mime_boundary ("Encrypt");
805 /* Create the list of recipients */
806 rset = gpgmegtk_recipient_selection (recp_list);
808 debug_print ("error creating recipient list\n" );
812 /* Open the source file */
813 if ((fp = fopen(file, "rb")) == NULL) {
814 FILE_OP_ERROR(file, "fopen");
818 err = gpgme_data_new (&header);
820 err = gpgme_data_new (&plain);
822 debug_print ("gpgme_data_new failed: %s\n", gpgme_strerror (err));
826 /* get the content header lines from the source */
829 while (!err && fgets(buf, sizeof(buf), fp)) {
830 /* fixme: check for overlong lines */
831 if (headerp (buf, content_names)) {
832 if (clineidx >= DIM (clines)) {
833 debug_print ("rfc2015_encrypt: too many content lines\n");
836 clines[clineidx++] = g_strdup (buf);
841 if (*buf == ' ' || *buf == '\t') {
842 char *last = clines[clineidx - 1];
843 clines[clineidx - 1] = g_strconcat (last, buf, NULL);
850 if (headerp (buf, mime_version_name))
851 mime_version_seen = 1;
853 if (buf[0] == '\r' || buf[0] == '\n')
855 err = gpgme_data_write (header, buf, strlen (buf));
858 FILE_OP_ERROR (file, "fgets");
862 /* write them to the temp data and add the rest of the message */
863 for (i = 0; !err && i < clineidx; i++) {
864 debug_print ("%% %s:%d: cline=`%s'", __FILE__ ,__LINE__, clines[i]);
865 err = gpgme_data_write (plain, clines[i], strlen (clines[i]));
868 err = gpgme_data_write (plain, "\r\n", 2);
869 while (!err && fgets(buf, sizeof(buf), fp)) {
870 err = gpgme_data_write (plain, buf, strlen (buf));
873 FILE_OP_ERROR (file, "fgets");
877 debug_print ("gpgme_data_write failed: %s\n", gpgme_strerror (err));
881 cipher = pgp_encrypt (plain, rset);
882 gpgme_data_release (plain); plain = NULL;
883 gpgme_recipients_release (rset); rset = NULL;
887 /* we have the encrypted message available in cipher and now we
888 * are going to rewrite the source file. To be sure that file has
889 * been truncated we use an approach which should work everywhere:
890 * close the file and then reopen it for writing. It is important
891 * that this works, otherwise it may happen that parts of the
892 * plaintext are still in the file (The encrypted stuff is, due to
893 * compression, usually shorter than the plaintext).
895 * Yes, there is a race condition here, but everyone, who is so
896 * stupid to store the temp file with the plaintext in a public
897 * directory has to live with this anyway. */
899 FILE_OP_ERROR(file, "fclose");
902 if ((fp = fopen(file, "wb")) == NULL) {
903 FILE_OP_ERROR(file, "fopen");
907 /* Write the header, append new content lines, part 1 and part 2 header */
908 err = gpgme_data_rewind (header);
910 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
913 while (!(err = gpgme_data_read (header, buf, BUFFSIZE, &nread))) {
914 fwrite (buf, nread, 1, fp);
916 if (err != GPGME_EOF) {
917 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
921 FILE_OP_ERROR (file, "fwrite");
924 gpgme_data_release (header); header = NULL;
926 if (!mime_version_seen)
927 fputs ("MIME-Version: 1\r\n", fp);
931 "Content-Type: text/plain; charset=US-ASCII\r\n"
932 "Content-Transfer-Encoding: 7bit\r\n"
936 "Content-Type: multipart/encrypted;"
937 " protocol=\"application/pgp-encrypted\";\r\n"
938 " boundary=\"%s\"\r\n"
941 "Content-Type: application/pgp-encrypted\r\n"
946 "Content-Type: application/octet-stream\r\n"
948 boundary, boundary, boundary);
951 /* append the encrypted stuff */
952 err = gpgme_data_rewind (cipher);
954 debug_print ("** gpgme_data_rewind on cipher failed: %s\n",
955 gpgme_strerror (err));
959 while (!(err = gpgme_data_read (cipher, buf, BUFFSIZE, &nread))) {
960 fwrite (buf, nread, 1, fp);
962 if (err != GPGME_EOF) {
963 debug_print ("** gpgme_data_read failed: %s\n", gpgme_strerror (err));
967 /* and the final boundary */
968 if (!ascii_armored) {
976 FILE_OP_ERROR (file, "fwrite");
980 gpgme_data_release (cipher);
986 gpgme_data_release (header);
987 gpgme_data_release (plain);
988 gpgme_data_release (cipher);
989 gpgme_recipients_release (rset);
991 return -1; /* error */
995 * plain contains an entire mime object. Sign it and return an
996 * GpgmeData object with the signature of it or NULL in case of error.
997 * r_siginfo returns an XML object with information about the signature.
1000 pgp_sign (GpgmeData plain, GSList *key_list, gboolean clearsign,
1004 GpgmeCtx ctx = NULL;
1006 GpgmeData sig = NULL;
1007 struct passphrase_cb_info_s info;
1010 memset (&info, 0, sizeof info);
1012 err = gpgme_new (&ctx);
1015 err = gpgme_data_new (&sig);
1019 if (!getenv("GPG_AGENT_INFO")) {
1021 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
1023 gpgme_set_textmode (ctx, 1);
1024 gpgme_set_armor (ctx, 1);
1025 gpgme_signers_clear (ctx);
1026 for (p = key_list; p != NULL; p = p->next) {
1027 err = gpgme_signers_add (ctx, (GpgmeKey) p->data);
1031 for (p = key_list; p != NULL; p = p->next)
1032 gpgme_key_unref ((GpgmeKey) p->data);
1033 g_slist_free (key_list);
1039 clearsign ? GPGME_SIG_MODE_CLEAR : GPGME_SIG_MODE_DETACH);
1041 *r_siginfo = gpgme_get_op_info (ctx, 0);
1045 gpgmegtk_free_passphrase();
1046 debug_print ("signing failed: %s\n", gpgme_strerror (err));
1047 gpgme_data_release (sig);
1051 debug_print ("signing succeeded\n");
1054 gpgme_release (ctx);
1059 * Find TAG in XML and return a pointer into xml set just behind the
1060 * closing angle. Return NULL if not found.
1063 find_xml_tag (const char *xml, const char *tag)
1065 int taglen = strlen (tag);
1066 const char *s = xml;
1068 while ( (s = strchr (s, '<')) ) {
1070 if (!strncmp (s, tag, taglen)) {
1071 const char *s2 = s + taglen;
1072 if (*s2 == '>' || isspace (*(const unsigned char*)s2) ) {
1074 while (*s2 && *s2 != '>') /* skip attributes */
1076 /* fixme: do need to handle angles inside attribute vallues? */
1077 return *s2? (s2+1):NULL;
1080 while (*s && *s != '>') /* skip to end of tag */
1088 * Extract the micalg from an GnupgOperationInfo XML container.
1091 extract_micalg (char *xml)
1095 s = find_xml_tag (xml, "GnupgOperationInfo");
1097 const char *s_end = find_xml_tag (s, "/GnupgOperationInfo");
1098 s = find_xml_tag (s, "signature");
1099 if (s && s_end && s < s_end) {
1100 const char *s_end2 = find_xml_tag (s, "/signature");
1101 if (s_end2 && s_end2 < s_end) {
1102 s = find_xml_tag (s, "micalg");
1103 if (s && s < s_end2) {
1104 s_end = strchr (s, '<');
1106 char *p = g_malloc (s_end - s + 1);
1107 memcpy (p, s, s_end - s);
1120 * Sign the file and replace its content with the signed one.
1123 rfc2015_sign (const char *file, GSList *key_list)
1127 int i, clineidx, saved_last;
1128 char *clines[3] = {NULL};
1130 GpgmeData header = NULL;
1131 GpgmeData plain = NULL;
1132 GpgmeData sigdata = NULL;
1134 int mime_version_seen = 0;
1136 char *micalg = NULL;
1139 boundary = generate_mime_boundary ("Signature");
1141 /* Open the source file */
1142 if ((fp = fopen(file, "rb")) == NULL) {
1143 FILE_OP_ERROR(file, "fopen");
1147 err = gpgme_data_new (&header);
1149 err = gpgme_data_new (&plain);
1151 debug_print ("gpgme_data_new failed: %s\n", gpgme_strerror (err));
1155 /* get the content header lines from the source */
1158 while (!err && fgets(buf, sizeof(buf), fp)) {
1159 /* fixme: check for overlong lines */
1160 if (headerp (buf, content_names)) {
1161 if (clineidx >= DIM (clines)) {
1162 debug_print ("rfc2015_sign: too many content lines\n");
1165 clines[clineidx++] = g_strdup (buf);
1170 if (*buf == ' ' || *buf == '\t') {
1171 char *last = clines[clineidx - 1];
1172 clines[clineidx - 1] = g_strconcat (last, buf, NULL);
1179 if (headerp (buf, mime_version_name))
1180 mime_version_seen = 1;
1182 if (buf[0] == '\r' || buf[0] == '\n')
1184 err = gpgme_data_write (header, buf, strlen (buf));
1187 FILE_OP_ERROR (file, "fgets");
1191 /* write them to the temp data and add the rest of the message */
1192 for (i = 0; !err && i < clineidx; i++) {
1193 err = gpgme_data_write (plain, clines[i], strlen (clines[i]));
1196 err = gpgme_data_write (plain, "\r\n", 2 );
1197 while (!err && fgets(buf, sizeof(buf), fp)) {
1198 err = gpgme_data_write (plain, buf, strlen (buf));
1201 FILE_OP_ERROR (file, "fgets");
1205 debug_print ("gpgme_data_write failed: %s\n", gpgme_strerror (err));
1209 sigdata = pgp_sign (plain, key_list, FALSE, &siginfo);
1211 micalg = extract_micalg (siginfo);
1217 /* we have the signed message available in sigdata and now we are
1218 * going to rewrite the original file. To be sure that file has
1219 * been truncated we use an approach which should work everywhere:
1220 * close the file and then reopen it for writing. */
1222 FILE_OP_ERROR(file, "fclose");
1225 if ((fp = fopen(file, "wb")) == NULL) {
1226 FILE_OP_ERROR(file, "fopen");
1230 /* Write the rfc822 header and add new content lines */
1231 err = gpgme_data_rewind (header);
1233 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
1234 while (!(err = gpgme_data_read (header, buf, BUFFSIZE, &nread))) {
1235 fwrite (buf, nread, 1, fp);
1237 if (err != GPGME_EOF) {
1238 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
1242 FILE_OP_ERROR (file, "fwrite");
1245 gpgme_data_release (header);
1248 if (!mime_version_seen)
1249 fputs ("MIME-Version: 1.0\r\n", fp);
1250 fprintf (fp, "Content-Type: multipart/signed; "
1251 "protocol=\"application/pgp-signature\";\r\n");
1253 fprintf (fp, " micalg=\"%s\";\r\n", micalg);
1254 fprintf (fp, " boundary=\"%s\"\r\n", boundary);
1256 /* Part 1: signed material */
1260 err = gpgme_data_rewind (plain);
1262 debug_print ("gpgme_data_rewind on plain failed: %s\n",
1263 gpgme_strerror (err));
1266 while (!(err = gpgme_data_read (plain, buf, BUFFSIZE, &nread))) {
1267 fwrite (buf, nread, 1, fp);
1269 if (err != GPGME_EOF) {
1270 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
1274 /* Part 2: signature */
1278 fputs ("Content-Type: application/pgp-signature\r\n"
1281 err = gpgme_data_rewind (sigdata);
1283 debug_print ("gpgme_data_rewind on sigdata failed: %s\n",
1284 gpgme_strerror (err));
1288 while (!(err = gpgme_data_read (sigdata, buf, BUFFSIZE, &nread))) {
1289 fwrite (buf, nread, 1, fp);
1291 if (err != GPGME_EOF) {
1292 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
1296 /* Final boundary */
1302 FILE_OP_ERROR (file, "fwrite");
1306 gpgme_data_release (header);
1307 gpgme_data_release (plain);
1308 gpgme_data_release (sigdata);
1316 gpgme_data_release (header);
1317 gpgme_data_release (plain);
1318 gpgme_data_release (sigdata);
1321 return -1; /* error */
1326 * Sign the file with clear text and replace its content with the signed one.
1329 rfc2015_clearsign (const gchar *file, GSList *key_list)
1332 gchar buf[BUFFSIZE];
1334 GpgmeData text = NULL;
1335 GpgmeData sigdata = NULL;
1339 if ((fp = fopen(file, "rb")) == NULL) {
1340 FILE_OP_ERROR(file, "fopen");
1344 err = gpgme_data_new(&text);
1346 debug_print("gpgme_data_new failed: %s\n", gpgme_strerror(err));
1350 while (!err && fgets(buf, sizeof(buf), fp)) {
1351 err = gpgme_data_write(text, buf, strlen(buf));
1354 FILE_OP_ERROR(file, "fgets");
1358 debug_print("gpgme_data_write failed: %s\n", gpgme_strerror(err));
1362 sigdata = pgp_sign(text, key_list, TRUE, &siginfo);
1369 if (fclose(fp) == EOF) {
1370 FILE_OP_ERROR(file, "fclose");
1374 if ((fp = fopen(file, "wb")) == NULL) {
1375 FILE_OP_ERROR(file, "fopen");
1379 err = gpgme_data_rewind(sigdata);
1381 debug_print("gpgme_data_rewind on sigdata failed: %s\n",
1382 gpgme_strerror(err));
1386 while (!(err = gpgme_data_read(sigdata, buf, sizeof(buf), &nread))) {
1387 fwrite(buf, nread, 1, fp);
1389 if (err != GPGME_EOF) {
1390 debug_print("gpgme_data_read failed: %s\n", gpgme_strerror(err));
1394 if (fclose(fp) == EOF) {
1395 FILE_OP_ERROR(file, "fclose");
1399 gpgme_data_release(text);
1400 gpgme_data_release(sigdata);
1406 gpgme_data_release(text);
1407 gpgme_data_release(sigdata);
1411 #endif /* USE_GPGME */