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 const char *key_id_str;
312 sig_status_for_key (str, ctx, status, key, fpr);
313 key_id_str = gpgme_key_get_string_attr(key,
314 GPGME_ATTR_KEYID, NULL, 0);
316 g_string_sprintfa (str, _("Key ID: %s\n"), key_id_str);
317 gpgme_key_unref (key);
319 g_string_append (str, "\n\n");
321 fpr = gpgme_get_sig_status (ctx, ++sig_idx, &status, &created);
325 g_string_free (str, FALSE);
329 static void check_signature (MimeInfo *mimeinfo, MimeInfo *partinfo, FILE *fp)
333 GpgmeData sig = NULL, text = NULL;
334 GpgmeSigStat status = GPGME_SIG_STAT_NONE;
335 GpgmegtkSigStatus statuswindow = NULL;
336 const char *result = NULL;
338 gint n_exclude_chars = 0;
340 if (prefs_common.gpg_signature_popup)
341 statuswindow = gpgmegtk_sig_status_create ();
343 err = gpgme_new (&ctx);
345 debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
349 /* don't include the last empty line.
350 It does not belong to the signed text */
351 if (mimeinfo->children->length > 0) {
352 if (fseek(fp, mimeinfo->children->offset + mimeinfo->children->length - 1,
357 if (fgetc(fp) == '\n') {
359 if (mimeinfo->children->length > 1) {
360 if (fseek(fp, mimeinfo->children->offset + mimeinfo->children->length - 2,
365 if (fgetc(fp) == '\r')
371 /* canonicalize the file part. */
372 tmp_file = get_tmp_file();
373 if (copy_file_part(fp, mimeinfo->children->offset,
374 mimeinfo->children->length - n_exclude_chars,
379 if (canonicalize_file_replace(tmp_file) < 0) {
385 err = gpgme_data_new_from_file(&text, tmp_file, 1);
391 err = gpgme_data_new_from_filepart (&sig, NULL, fp,
392 partinfo->offset, partinfo->length);
394 debug_print ("gpgme_data_new_from_filepart failed: %s\n",
395 gpgme_strerror (err));
399 err = gpgme_op_verify (ctx, sig, text, &status);
401 debug_print ("gpgme_op_verify failed: %s\n", gpgme_strerror (err));
405 /* FIXME: check what the heck this sig_status_full stuff is.
406 * it should better go into sigstatus.c */
407 g_free (partinfo->sigstatus_full);
408 partinfo->sigstatus_full = sig_status_full (ctx);
411 result = gpgmegtk_sig_status_to_string(status);
412 debug_print("verification status: %s\n", result);
413 if (prefs_common.gpg_signature_popup)
414 gpgmegtk_sig_status_update (statuswindow, ctx);
416 g_free (partinfo->sigstatus);
417 partinfo->sigstatus = g_strdup (result);
418 partinfo->sig_ok = (status == GPGME_SIG_STAT_GOOD);
419 partinfo->sig_unknown = (status == GPGME_SIG_STAT_NOKEY);
420 partinfo->sig_expired = (status == GPGME_SIG_STAT_GOOD_EXP);
421 partinfo->key_expired = (status == GPGME_SIG_STAT_GOOD_EXPKEY);
423 gpgme_data_release (sig);
424 gpgme_data_release (text);
426 if (prefs_common.gpg_signature_popup)
427 gpgmegtk_sig_status_destroy (statuswindow);
431 * Copy a gpgme data object to a temporary file and
432 * return this filename
436 copy_gpgmedata_to_temp (GpgmeData data, guint *length)
445 tmp = g_strdup_printf("%s%cgpgtmp.%08x",
446 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id );
448 if ((fp = fopen(tmp, "wb")) == NULL) {
449 FILE_OP_ERROR(tmp, "fopen");
454 err = gpgme_data_rewind ( data );
456 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
458 while (!(err = gpgme_data_read (data, buf, 100, &nread))) {
459 fwrite ( buf, nread, 1, fp );
462 if (err != GPGME_EOF)
463 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
473 pgp_decrypt (MimeInfo *partinfo, FILE *fp)
477 GpgmeData cipher = NULL, plain = NULL;
478 struct passphrase_cb_info_s info;
480 memset (&info, 0, sizeof info);
482 err = gpgme_new (&ctx);
484 debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
488 err = gpgme_data_new_from_filepart (&cipher, NULL, fp,
489 partinfo->offset, partinfo->length);
491 debug_print ("gpgme_data_new_from_filepart failed: %s\n",
492 gpgme_strerror (err));
496 err = gpgme_data_new (&plain);
498 debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
502 if (!getenv("GPG_AGENT_INFO")) {
504 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
507 err = gpgme_op_decrypt (ctx, cipher, plain);
510 gpgme_data_release (cipher);
512 gpgmegtk_free_passphrase();
513 debug_print ("decryption failed: %s\n", gpgme_strerror (err));
514 gpgme_data_release (plain);
518 debug_print ("** decryption succeeded\n");
524 MimeInfo * rfc2015_find_signature (MimeInfo *mimeinfo)
531 if (!((mimeinfo->type == MIMETYPE_MULTIPART) && !g_strcasecmp(mimeinfo->subtype, "signed")))
534 debug_print ("** multipart/signed encountered\n");
536 /* check that we have at least 2 parts of the correct type */
537 for (partinfo = mimeinfo->children;
538 partinfo != NULL; partinfo = partinfo->next) {
539 if (++n > 1 && (partinfo->type == MIMETYPE_APPLICATION &&
540 !g_strcasecmp (partinfo->subtype, "pgp-signature")))
547 gboolean rfc2015_has_signature (MimeInfo *mimeinfo)
549 return rfc2015_find_signature (mimeinfo) != NULL;
552 void rfc2015_check_signature (MimeInfo *mimeinfo, FILE *fp)
556 partinfo = rfc2015_find_signature (mimeinfo);
561 g_message ("** yep, it is a pgp signature");
562 dump_mimeinfo ("gpg-signature", partinfo );
563 dump_part (partinfo, fp );
564 dump_mimeinfo ("signed text", mimeinfo->children );
565 dump_part (mimeinfo->children, fp);
568 check_signature (mimeinfo, partinfo, fp);
571 int rfc2015_is_encrypted (MimeInfo *mimeinfo)
573 if (!mimeinfo || mimeinfo->type != MIMETYPE_MULTIPART)
575 if (g_strcasecmp (mimeinfo->subtype, "encrypted"))
577 /* fixme: we should check the protocol parameter */
581 gboolean rfc2015_msg_is_encrypted (const gchar *file)
588 if ((fp = fopen(file, "rb")) == NULL)
591 mimeinfo = procmime_scan_mime_header(fp);
597 ret = rfc2015_is_encrypted(mimeinfo);
598 procmime_mimeinfo_free_all(mimeinfo);
600 return ret != 0 ? TRUE : FALSE;
604 name_cmp(const char *a, const char *b)
606 for( ; *a && *b; a++, b++) {
608 && toupper(*(unsigned char *)a) != toupper(*(unsigned char *)b))
616 headerp(char *p, char **names)
625 if(p2[-1] == ' ' || p2[-1] == '\t') {
634 for(i = 0 ; names[i] != NULL; i++) {
635 if(!name_cmp (names[i], p))
640 return names[i] != NULL;
644 #define DECRYPTION_ABORT() \
646 procmime_mimeinfo_free_all(tmpinfo); \
647 msginfo->decryption_failed = 1; \
651 void rfc2015_decrypt_message (MsgInfo *msginfo, MimeInfo *mimeinfo, FILE *fp)
654 MimeInfo *tmpinfo, *partinfo;
664 g_return_if_fail (msginfo != NULL);
665 g_return_if_fail (mimeinfo != NULL);
666 g_return_if_fail (fp != NULL);
667 g_return_if_fail (mimeinfo->type == MIMETYPE_MULTIPART);
669 debug_print ("** decrypting multipart/encrypted message\n");
674 if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0)
676 tmpinfo = procmime_scan_mime_header(fp);
677 if (!tmpinfo || tmpinfo->type != MIMETYPE_MULTIPART) {
681 procmime_scan_message(tmpinfo);
683 /* check that we have the 2 parts */
684 partinfo = tmpinfo->children;
685 if (!partinfo || !partinfo->next) {
688 if ((partinfo->type == MIMETYPE_APPLICATION) && !g_strcasecmp(partinfo->subtype, "pgp-encrypted")) {
689 /* Fixme: check that the version is 1 */
692 partinfo = partinfo->next;
693 if (ver_ok && (partinfo->type == MIMETYPE_APPLICATION) &&
694 !g_strcasecmp (partinfo->subtype, "octet-stream")) {
696 g_warning ("oops: pgp_encrypted with more than 2 parts");
702 debug_print ("** yep, it is pgp encrypted\n");
704 plain = pgp_decrypt (partinfo, fp);
709 fname = g_strdup_printf("%s%cplaintext.%08x",
710 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
712 if ((dstfp = fopen(fname, "wb")) == NULL) {
713 FILE_OP_ERROR(fname, "fopen");
718 /* write the orginal header to the new file */
719 if (fseek(fp, tmpinfo->offset, SEEK_SET) < 0)
723 while (fgets(buf, sizeof(buf), fp)) {
724 if (headerp (buf, content_names)) {
729 if (buf[0] == ' ' || buf[0] == '\t')
733 if (buf[0] == '\r' || buf[0] == '\n')
738 err = gpgme_data_rewind (plain);
740 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
742 while (!(err = gpgme_data_read (plain, buf, sizeof(buf), &nread))) {
743 fwrite (buf, nread, 1, dstfp);
746 if (err != GPGME_EOF) {
747 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
751 procmime_mimeinfo_free_all(tmpinfo);
753 msginfo->plaintext_file = fname;
754 msginfo->decryption_failed = 0;
759 #undef DECRYPTION_ABORT
763 * plain contains an entire mime object.
764 * Encrypt it and return an GpgmeData object with the encrypted version of
765 * the file or NULL in case of error.
768 pgp_encrypt ( GpgmeData plain, GpgmeRecipients rset )
772 GpgmeData cipher = NULL;
774 err = gpgme_new (&ctx);
776 err = gpgme_data_new (&cipher);
778 gpgme_set_armor (ctx, 1);
779 err = gpgme_op_encrypt (ctx, rset, plain, cipher);
783 debug_print ("encryption failed: %s\n", gpgme_strerror (err));
784 gpgme_data_release (cipher);
788 debug_print ("** encryption succeeded\n");
796 * Create and return a list of keys matching a key id
799 GSList *rfc2015_create_signers_list (const char *keyid)
801 GSList *key_list = NULL;
802 GpgmeCtx list_ctx = NULL;
807 err = gpgme_new (&list_ctx);
810 err = gpgme_op_keylist_start (list_ctx, keyid, 1);
813 while ( !(err = gpgme_op_keylist_next (list_ctx, &key)) ) {
814 key_list = g_slist_append (key_list, key);
816 if (err != GPGME_EOF)
819 if (key_list == NULL) {
820 debug_print ("no keys found for keyid \"%s\"\n", keyid);
825 debug_print ("rfc2015_create_signers_list failed: %s\n", gpgme_strerror (err));
826 for (p = key_list; p != NULL; p = p->next)
827 gpgme_key_unref ((GpgmeKey) p->data);
828 g_slist_free (key_list);
831 gpgme_release (list_ctx);
832 return err ? NULL : key_list;
836 * Encrypt the file by extracting all recipients and finding the
837 * encryption keys for all of them. The file content is then replaced
838 * by the encrypted one. */
840 rfc2015_encrypt (const char *file, GSList *recp_list, gboolean ascii_armored)
844 int i, clineidx, saved_last;
845 char *clines[3] = {NULL};
847 GpgmeData header = NULL;
848 GpgmeData plain = NULL;
849 GpgmeData cipher = NULL;
850 GpgmeRecipients rset = NULL;
852 int mime_version_seen = 0;
855 boundary = generate_mime_boundary ("Encrypt");
857 /* Create the list of recipients */
858 rset = gpgmegtk_recipient_selection (recp_list);
860 debug_print ("error creating recipient list\n" );
864 /* Open the source file */
865 if ((fp = fopen(file, "rb")) == NULL) {
866 FILE_OP_ERROR(file, "fopen");
870 err = gpgme_data_new (&header);
872 err = gpgme_data_new (&plain);
874 debug_print ("gpgme_data_new failed: %s\n", gpgme_strerror (err));
878 /* get the content header lines from the source */
881 while (!err && fgets(buf, sizeof(buf), fp)) {
882 /* fixme: check for overlong lines */
883 if (headerp (buf, content_names)) {
884 if (clineidx >= DIM (clines)) {
885 debug_print ("rfc2015_encrypt: too many content lines\n");
888 clines[clineidx++] = g_strdup (buf);
893 if (*buf == ' ' || *buf == '\t') {
894 char *last = clines[clineidx - 1];
895 clines[clineidx - 1] = g_strconcat (last, buf, NULL);
902 if (headerp (buf, mime_version_name))
903 mime_version_seen = 1;
905 if (buf[0] == '\r' || buf[0] == '\n')
907 err = gpgme_data_write (header, buf, strlen (buf));
910 FILE_OP_ERROR (file, "fgets");
914 /* write them to the temp data and add the rest of the message */
915 for (i = 0; !err && i < clineidx; i++) {
916 debug_print ("%% %s:%d: cline=`%s'", __FILE__ ,__LINE__, clines[i]);
917 err = gpgme_data_write (plain, clines[i], strlen (clines[i]));
920 err = gpgme_data_write (plain, "\r\n", 2);
921 while (!err && fgets(buf, sizeof(buf), fp)) {
922 err = gpgme_data_write (plain, buf, strlen (buf));
925 FILE_OP_ERROR (file, "fgets");
929 debug_print ("gpgme_data_write failed: %s\n", gpgme_strerror (err));
933 cipher = pgp_encrypt (plain, rset);
934 gpgme_data_release (plain); plain = NULL;
935 gpgme_recipients_release (rset); rset = NULL;
939 /* we have the encrypted message available in cipher and now we
940 * are going to rewrite the source file. To be sure that file has
941 * been truncated we use an approach which should work everywhere:
942 * close the file and then reopen it for writing. It is important
943 * that this works, otherwise it may happen that parts of the
944 * plaintext are still in the file (The encrypted stuff is, due to
945 * compression, usually shorter than the plaintext).
947 * Yes, there is a race condition here, but everyone, who is so
948 * stupid to store the temp file with the plaintext in a public
949 * directory has to live with this anyway. */
951 FILE_OP_ERROR(file, "fclose");
954 if ((fp = fopen(file, "wb")) == NULL) {
955 FILE_OP_ERROR(file, "fopen");
959 /* Write the header, append new content lines, part 1 and part 2 header */
960 err = gpgme_data_rewind (header);
962 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
965 while (!(err = gpgme_data_read (header, buf, BUFFSIZE, &nread))) {
966 fwrite (buf, nread, 1, fp);
968 if (err != GPGME_EOF) {
969 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
973 FILE_OP_ERROR (file, "fwrite");
976 gpgme_data_release (header); header = NULL;
978 if (!mime_version_seen)
979 fputs ("MIME-Version: 1\r\n", fp);
983 "Content-Type: text/plain; charset=US-ASCII\r\n"
984 "Content-Transfer-Encoding: 7bit\r\n"
988 "Content-Type: multipart/encrypted;"
989 " protocol=\"application/pgp-encrypted\";\r\n"
990 " boundary=\"%s\"\r\n"
993 "Content-Type: application/pgp-encrypted\r\n"
998 "Content-Type: application/octet-stream\r\n"
1000 boundary, boundary, boundary);
1003 /* append the encrypted stuff */
1004 err = gpgme_data_rewind (cipher);
1006 debug_print ("** gpgme_data_rewind on cipher failed: %s\n",
1007 gpgme_strerror (err));
1011 while (!(err = gpgme_data_read (cipher, buf, BUFFSIZE, &nread))) {
1012 fwrite (buf, nread, 1, fp);
1014 if (err != GPGME_EOF) {
1015 debug_print ("** gpgme_data_read failed: %s\n", gpgme_strerror (err));
1019 /* and the final boundary */
1020 if (!ascii_armored) {
1028 FILE_OP_ERROR (file, "fwrite");
1032 gpgme_data_release (cipher);
1038 gpgme_data_release (header);
1039 gpgme_data_release (plain);
1040 gpgme_data_release (cipher);
1041 gpgme_recipients_release (rset);
1043 return -1; /* error */
1047 * plain contains an entire mime object. Sign it and return an
1048 * GpgmeData object with the signature of it or NULL in case of error.
1049 * r_siginfo returns an XML object with information about the signature.
1052 pgp_sign (GpgmeData plain, GSList *key_list, gboolean clearsign,
1056 GpgmeCtx ctx = NULL;
1058 GpgmeData sig = NULL;
1059 struct passphrase_cb_info_s info;
1062 memset (&info, 0, sizeof info);
1064 err = gpgme_new (&ctx);
1067 err = gpgme_data_new (&sig);
1071 if (!getenv("GPG_AGENT_INFO")) {
1073 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
1075 gpgme_set_textmode (ctx, 1);
1076 gpgme_set_armor (ctx, 1);
1077 gpgme_signers_clear (ctx);
1078 for (p = key_list; p != NULL; p = p->next) {
1079 err = gpgme_signers_add (ctx, (GpgmeKey) p->data);
1083 for (p = key_list; p != NULL; p = p->next)
1084 gpgme_key_unref ((GpgmeKey) p->data);
1085 g_slist_free (key_list);
1091 clearsign ? GPGME_SIG_MODE_CLEAR : GPGME_SIG_MODE_DETACH);
1093 *r_siginfo = gpgme_get_op_info (ctx, 0);
1097 gpgmegtk_free_passphrase();
1098 debug_print ("signing failed: %s\n", gpgme_strerror (err));
1099 gpgme_data_release (sig);
1103 debug_print ("signing succeeded\n");
1106 gpgme_release (ctx);
1111 * Find TAG in XML and return a pointer into xml set just behind the
1112 * closing angle. Return NULL if not found.
1115 find_xml_tag (const char *xml, const char *tag)
1117 int taglen = strlen (tag);
1118 const char *s = xml;
1120 while ( (s = strchr (s, '<')) ) {
1122 if (!strncmp (s, tag, taglen)) {
1123 const char *s2 = s + taglen;
1124 if (*s2 == '>' || isspace (*(const unsigned char*)s2) ) {
1126 while (*s2 && *s2 != '>') /* skip attributes */
1128 /* fixme: do need to handle angles inside attribute vallues? */
1129 return *s2? (s2+1):NULL;
1132 while (*s && *s != '>') /* skip to end of tag */
1140 * Extract the micalg from an GnupgOperationInfo XML container.
1143 extract_micalg (char *xml)
1147 s = find_xml_tag (xml, "GnupgOperationInfo");
1149 const char *s_end = find_xml_tag (s, "/GnupgOperationInfo");
1150 s = find_xml_tag (s, "signature");
1151 if (s && s_end && s < s_end) {
1152 const char *s_end2 = find_xml_tag (s, "/signature");
1153 if (s_end2 && s_end2 < s_end) {
1154 s = find_xml_tag (s, "micalg");
1155 if (s && s < s_end2) {
1156 s_end = strchr (s, '<');
1158 char *p = g_malloc (s_end - s + 1);
1159 memcpy (p, s, s_end - s);
1172 * Sign the file and replace its content with the signed one.
1175 rfc2015_sign (const char *file, GSList *key_list)
1179 int i, clineidx, saved_last;
1180 char *clines[3] = {NULL};
1182 GpgmeData header = NULL;
1183 GpgmeData plain = NULL;
1184 GpgmeData sigdata = NULL;
1186 int mime_version_seen = 0;
1188 char *micalg = NULL;
1191 boundary = generate_mime_boundary ("Signature");
1193 /* Open the source file */
1194 if ((fp = fopen(file, "rb")) == NULL) {
1195 FILE_OP_ERROR(file, "fopen");
1199 err = gpgme_data_new (&header);
1201 err = gpgme_data_new (&plain);
1203 debug_print ("gpgme_data_new failed: %s\n", gpgme_strerror (err));
1207 /* get the content header lines from the source */
1210 while (!err && fgets(buf, sizeof(buf), fp)) {
1211 /* fixme: check for overlong lines */
1212 if (headerp (buf, content_names)) {
1213 if (clineidx >= DIM (clines)) {
1214 debug_print ("rfc2015_sign: too many content lines\n");
1217 clines[clineidx++] = g_strdup (buf);
1222 if (*buf == ' ' || *buf == '\t') {
1223 char *last = clines[clineidx - 1];
1224 clines[clineidx - 1] = g_strconcat (last, buf, NULL);
1231 if (headerp (buf, mime_version_name))
1232 mime_version_seen = 1;
1234 if (buf[0] == '\r' || buf[0] == '\n')
1236 err = gpgme_data_write (header, buf, strlen (buf));
1239 FILE_OP_ERROR (file, "fgets");
1243 /* write them to the temp data and add the rest of the message */
1244 for (i = 0; !err && i < clineidx; i++) {
1245 err = gpgme_data_write (plain, clines[i], strlen (clines[i]));
1248 err = gpgme_data_write (plain, "\r\n", 2 );
1249 while (!err && fgets(buf, sizeof(buf), fp)) {
1250 err = gpgme_data_write (plain, buf, strlen (buf));
1253 FILE_OP_ERROR (file, "fgets");
1257 debug_print ("gpgme_data_write failed: %s\n", gpgme_strerror (err));
1261 sigdata = pgp_sign (plain, key_list, FALSE, &siginfo);
1263 micalg = extract_micalg (siginfo);
1269 /* we have the signed message available in sigdata and now we are
1270 * going to rewrite the original file. To be sure that file has
1271 * been truncated we use an approach which should work everywhere:
1272 * close the file and then reopen it for writing. */
1274 FILE_OP_ERROR(file, "fclose");
1277 if ((fp = fopen(file, "wb")) == NULL) {
1278 FILE_OP_ERROR(file, "fopen");
1282 /* Write the rfc822 header and add new content lines */
1283 err = gpgme_data_rewind (header);
1285 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
1286 while (!(err = gpgme_data_read (header, buf, BUFFSIZE, &nread))) {
1287 fwrite (buf, nread, 1, fp);
1289 if (err != GPGME_EOF) {
1290 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
1294 FILE_OP_ERROR (file, "fwrite");
1297 gpgme_data_release (header);
1300 if (!mime_version_seen)
1301 fputs ("MIME-Version: 1.0\r\n", fp);
1302 fprintf (fp, "Content-Type: multipart/signed; "
1303 "protocol=\"application/pgp-signature\";\r\n");
1305 fprintf (fp, " micalg=\"%s\";\r\n", micalg);
1306 fprintf (fp, " boundary=\"%s\"\r\n", boundary);
1308 /* Part 1: signed material */
1312 err = gpgme_data_rewind (plain);
1314 debug_print ("gpgme_data_rewind on plain failed: %s\n",
1315 gpgme_strerror (err));
1318 while (!(err = gpgme_data_read (plain, buf, BUFFSIZE, &nread))) {
1319 fwrite (buf, nread, 1, fp);
1321 if (err != GPGME_EOF) {
1322 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
1326 /* Part 2: signature */
1330 fputs ("Content-Type: application/pgp-signature\r\n"
1333 err = gpgme_data_rewind (sigdata);
1335 debug_print ("gpgme_data_rewind on sigdata failed: %s\n",
1336 gpgme_strerror (err));
1340 while (!(err = gpgme_data_read (sigdata, buf, BUFFSIZE, &nread))) {
1341 fwrite (buf, nread, 1, fp);
1343 if (err != GPGME_EOF) {
1344 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
1348 /* Final boundary */
1354 FILE_OP_ERROR (file, "fwrite");
1358 gpgme_data_release (header);
1359 gpgme_data_release (plain);
1360 gpgme_data_release (sigdata);
1368 gpgme_data_release (header);
1369 gpgme_data_release (plain);
1370 gpgme_data_release (sigdata);
1373 return -1; /* error */
1378 * Sign the file with clear text and replace its content with the signed one.
1381 rfc2015_clearsign (const gchar *file, GSList *key_list)
1384 gchar buf[BUFFSIZE];
1386 GpgmeData text = NULL;
1387 GpgmeData sigdata = NULL;
1391 if ((fp = fopen(file, "rb")) == NULL) {
1392 FILE_OP_ERROR(file, "fopen");
1396 err = gpgme_data_new(&text);
1398 debug_print("gpgme_data_new failed: %s\n", gpgme_strerror(err));
1402 while (!err && fgets(buf, sizeof(buf), fp)) {
1403 err = gpgme_data_write(text, buf, strlen(buf));
1406 FILE_OP_ERROR(file, "fgets");
1410 debug_print("gpgme_data_write failed: %s\n", gpgme_strerror(err));
1414 sigdata = pgp_sign(text, key_list, TRUE, &siginfo);
1421 if (fclose(fp) == EOF) {
1422 FILE_OP_ERROR(file, "fclose");
1426 if ((fp = fopen(file, "wb")) == NULL) {
1427 FILE_OP_ERROR(file, "fopen");
1431 err = gpgme_data_rewind(sigdata);
1433 debug_print("gpgme_data_rewind on sigdata failed: %s\n",
1434 gpgme_strerror(err));
1438 while (!(err = gpgme_data_read(sigdata, buf, sizeof(buf), &nread))) {
1439 fwrite(buf, nread, 1, fp);
1441 if (err != GPGME_EOF) {
1442 debug_print("gpgme_data_read failed: %s\n", gpgme_strerror(err));
1446 if (fclose(fp) == EOF) {
1447 FILE_OP_ERROR(file, "fclose");
1451 gpgme_data_release(text);
1452 gpgme_data_release(sigdata);
1458 gpgme_data_release(text);
1459 gpgme_data_release(sigdata);
1463 #endif /* USE_GPGME */