sync with 0.8.1cvs12
[claws.git] / src / rfc2015.c
index 3791441cc57b563fb54e91cc9f00a77b70666401..57409fe0f0b28847bde8d7473d3046a243609d4b 100644 (file)
@@ -20,7 +20,8 @@
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
 #endif
-#ifdef USE_GPGME
+
+#if USE_GPGME
 
 #include "defs.h"
 
@@ -43,6 +44,7 @@
 #include "prefs_common.h"
 #include "passphrase.h"
 #include "select-keys.h"
+#include "sigstatus.h"
 #include "rfc2015.h"
 
 #define DIM(v)     (sizeof(v)/sizeof((v)[0]))
@@ -59,32 +61,27 @@ static char *mime_version_name[] = {
     NULL
 };
 
-
-struct passphrase_cb_info_s {
-    GpgmeCtx c;
-    int did_it;
-};
-
 static char *create_boundary (void);
 
+#if 0
 static void dump_mimeinfo (const char *text, MimeInfo *x)
 {
-    g_message ("** MimeInfo[%s] %p  level=%d",
+    debug_print ("MimeInfo[%s] %p  level=%d\n",
                text, x, x? x->level:0 );
     if (!x)
         return;
 
-    g_message ("**      enc=`%s' enc_type=%d mime_type=%d",
+    debug_print ("      enc=`%s' enc_type=%d mime_type=%d\n",
                x->encoding, x->encoding_type, x->mime_type );
-    g_message ("**      cont_type=`%s' cs=`%s' name=`%s' bnd=`%s'",
+    debug_print ("      cont_type=`%s' cs=`%s' name=`%s' bnd=`%s'\n",
                x->content_type, x->charset, x->name, x->boundary );
-    g_message ("**      cont_disp=`%s' fname=`%s' fpos=%ld size=%u, lvl=%d",
+    debug_print ("      cont_disp=`%s' fname=`%s' fpos=%ld size=%u, lvl=%d\n",
                x->content_disposition, x->filename, x->fpos, x->size,
                x->level );
     dump_mimeinfo (".main", x->main );
     dump_mimeinfo (".sub", x->sub );
     dump_mimeinfo (".next", x->next );
-    g_message ("** MimeInfo[.parent] %p", x ); 
+    debug_print ("MimeInfo[.parent] %p\n", x ); 
     dump_mimeinfo (".children", x->children );
     dump_mimeinfo (".plaintext", x->plaintext );
 }
@@ -95,17 +92,18 @@ static void dump_part ( MimeInfo *mimeinfo, FILE *fp )
     int c;
 
     if (fseek (fp, mimeinfo->fpos, SEEK_SET)) {
-        g_warning ("dump_part: fseek error");
+        debug_print ("dump_part: fseek error\n");
         return;
     }
 
-    g_message ("** --- begin dump_part ----");
+    debug_print ("--- begin dump_part ----\n");
     while (size-- && (c = getc (fp)) != EOF) 
         putc (c, stderr);
     if (ferror (fp))
-        g_warning ("dump_part: read error");
-    g_message ("** --- end dump_part ----");
+        debug_print ("dump_part: read error\n");
+    debug_print ("--- end dump_part ----\n");
 }
+#endif
 
 void
 rfc2015_disable_all (void)
@@ -278,11 +276,15 @@ static void check_signature (MimeInfo *mimeinfo, MimeInfo *partinfo, FILE *fp)
     GpgmeError err;
     GpgmeData sig = NULL, text = NULL;
     GpgmeSigStat status = GPGME_SIG_STAT_NONE;
+    GpgmegtkSigStatus statuswindow = NULL;
     const char *result = NULL;
 
+    if (prefs_common.gpg_signature_popup)
+       statuswindow = gpgmegtk_sig_status_create ();
+
     err = gpgme_new (&ctx);
     if (err) {
-       g_warning ("gpgme_new failed: %s", gpgme_strerror (err));
+       debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
        goto leave;
     }
 
@@ -296,60 +298,42 @@ static void check_signature (MimeInfo *mimeinfo, MimeInfo *partinfo, FILE *fp)
        err = gpgme_data_new_from_filepart (&sig, NULL, fp,
                                            partinfo->fpos, partinfo->size);
     if (err) {
-        g_message ("gpgme_data_new_from_filepart failed: %s",
+        debug_print ("gpgme_data_new_from_filepart failed: %s\n",
                   gpgme_strerror (err));
         goto leave;
     }
 
     err = gpgme_op_verify (ctx, sig, text, &status);
     if (err) 
-        g_message ("gpgme_op_verify failed: %s", gpgme_strerror (err));
+        debug_print ("gpgme_op_verify failed: %s\n", gpgme_strerror (err));
 
+    /* FIXME: check what the heck this sig_status_full stuff is.
+     * it should better go into sigstatus.c */
     g_free (partinfo->sigstatus_full);
     partinfo->sigstatus_full = sig_status_full (ctx);
 
 leave:
-    result = sig_status_to_string(status);
+    result = gpgmegtk_sig_status_to_string(status);
     debug_print("verification status: %s\n", result);
+    if (prefs_common.gpg_signature_popup)
+       gpgmegtk_sig_status_update (statuswindow,ctx);
 
-    g_assert (!err);
+    g_assert (!err); /* FIXME: Hey: this may indeed happen */
     g_free (partinfo->sigstatus);
     partinfo->sigstatus = g_strdup (result);
 
     gpgme_data_release (sig);
     gpgme_data_release (text);
     gpgme_release (ctx);
-}
-
-static const char *
-passphrase_cb (void *opaque, const char *desc, void *r_hd)
-{
-    struct passphrase_cb_info_s *info = opaque;
-    GpgmeCtx ctx = info ? info->c : NULL;
-    const char *pass;
-
-    if (!desc) {
-        /* FIXME: cleanup by looking at *r_hd */
-        return NULL;
-    }
-
-    gpgmegtk_set_passphrase_grab (prefs_common.passphrase_grab);
-    g_message ("%% requesting passphrase for `%s': ", desc );
-    pass = gpgmegtk_passphrase_mbox (desc);
-    if (!pass) {
-        g_message ("%% cancel passphrase entry");
-        gpgme_cancel (ctx);
-    }
-    else
-        g_message ("%% sending passphrase");
-
-    return pass;
+    if (prefs_common.gpg_signature_popup)
+       gpgmegtk_sig_status_destroy (statuswindow);
 }
 
 /*
  * Copy a gpgme data object to a temporary file and
  * return this filename 
  */
+#if 0
 static char *
 copy_gpgmedata_to_temp (GpgmeData data, guint *length)
 {
@@ -363,7 +347,7 @@ copy_gpgmedata_to_temp (GpgmeData data, guint *length)
     tmp = g_strdup_printf("%s%cgpgtmp.%08x",
                           get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id );
 
-    if ((fp = fopen(tmp, "w")) == NULL) {
+    if ((fp = fopen(tmp, "wb")) == NULL) {
         FILE_OP_ERROR(tmp, "fopen");
         g_free(tmp);
         return NULL;
@@ -371,20 +355,21 @@ copy_gpgmedata_to_temp (GpgmeData data, guint *length)
 
     err = gpgme_data_rewind ( data );
     if (err)
-        g_message ("** gpgme_data_rewind failed: %s", gpgme_strerror (err));
+        debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
 
     while (!(err = gpgme_data_read (data, buf, 100, &nread))) {
         fwrite ( buf, nread, 1, fp );
     }
 
     if (err != GPGME_EOF)
-        g_warning ("** gpgme_data_read failed: %s", gpgme_strerror (err));
+        debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
 
     fclose (fp);
     *length = nread;
 
     return tmp;
 }
+#endif
 
 static GpgmeData
 pgp_decrypt (MimeInfo *partinfo, FILE *fp)
@@ -398,27 +383,27 @@ pgp_decrypt (MimeInfo *partinfo, FILE *fp)
 
     err = gpgme_new (&ctx);
     if (err) {
-        g_message ("gpgme_new failed: %s", gpgme_strerror (err));
+        debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
         goto leave;
     }
 
     err = gpgme_data_new_from_filepart (&cipher, NULL, fp,
                                        partinfo->fpos, partinfo->size);
     if (err) {
-        g_message ("gpgme_data_new_from_filepart failed: %s",
-                   gpgme_strerror (err));
+        debug_print ("gpgme_data_new_from_filepart failed: %s\n",
+                     gpgme_strerror (err));
         goto leave;
     }
 
     err = gpgme_data_new (&plain);
     if (err) {
-        g_message ("gpgme_new failed: %s", gpgme_strerror (err));
+        debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
         goto leave;
     }
 
     if (!getenv("GPG_AGENT_INFO")) {
         info.c = ctx;
-        gpgme_set_passphrase_cb (ctx, passphrase_cb, &info);
+        gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
     } 
 
     err = gpgme_op_decrypt (ctx, cipher, plain);
@@ -426,12 +411,13 @@ pgp_decrypt (MimeInfo *partinfo, FILE *fp)
 leave:
     gpgme_data_release (cipher);
     if (err) {
-        g_warning ("** decryption failed: %s", gpgme_strerror (err));
+        gpgmegtk_free_passphrase();
+        debug_print ("decryption failed: %s\n", gpgme_strerror (err));
         gpgme_data_release (plain);
         plain = NULL;
     }
     else
-        g_message ("** decryption succeeded");
+        debug_print ("** decryption succeeded\n");
 
     gpgme_release (ctx);
     return plain;
@@ -447,7 +433,7 @@ MimeInfo * rfc2015_find_signature (MimeInfo *mimeinfo)
     if (g_strcasecmp (mimeinfo->content_type, "multipart/signed"))
         return NULL;
 
-    g_message ("** multipart/signed encountered");
+    debug_print ("** multipart/signed encountered\n");
 
     /* check that we have at least 2 parts of the correct type */
     for (partinfo = mimeinfo->children;
@@ -488,12 +474,32 @@ int rfc2015_is_encrypted (MimeInfo *mimeinfo)
 {
     if (!mimeinfo)
         return 0;
-    if (strcasecmp (mimeinfo->content_type, "multipart/encrypted"))
+    if (g_strcasecmp (mimeinfo->content_type, "multipart/encrypted"))
         return 0;
-    /* fixme: we should schek the protocol parameter */
+    /* fixme: we should check the protocol parameter */
     return 1;
 }
 
+gboolean rfc2015_msg_is_encrypted (gchar *file)
+{
+       FILE *fp;
+       MimeInfo *mimeinfo;
+       int ret;
+
+       if ((fp = fopen(file, "rb")) == NULL)
+               return FALSE;
+
+       mimeinfo = procmime_scan_mime_header(fp);
+       if(!mimeinfo) {
+               fclose(fp);
+               return FALSE;
+       }
+
+       ret = rfc2015_is_encrypted(mimeinfo);
+       procmime_mimeinfo_free_all(mimeinfo);
+       return ret != 0 ? TRUE : FALSE;
+}
+
 static int
 name_cmp(const char *a, const char *b)
 {
@@ -535,12 +541,18 @@ headerp(char *p, char **names)
 }
 
 
+#define DECRYPTION_ABORT() \
+{ \
+    procmime_mimeinfo_free_all(tmpinfo); \
+    msginfo->decryption_failed = 1; \
+    return; \
+}
+
 void rfc2015_decrypt_message (MsgInfo *msginfo, MimeInfo *mimeinfo, FILE *fp)
 {
     static int id;
-    MimeInfo *partinfo;
-    int n, found;
-    int ver_okay=0;
+    MimeInfo *tmpinfo, *partinfo;
+    int ver_ok = 0;
     char *fname;
     GpgmeData plain;
     FILE *dstfp;
@@ -548,61 +560,60 @@ void rfc2015_decrypt_message (MsgInfo *msginfo, MimeInfo *mimeinfo, FILE *fp)
     char buf[BUFFSIZE];
     GpgmeError err;
 
+    g_return_if_fail (msginfo != NULL);
+    g_return_if_fail (mimeinfo != NULL);
+    g_return_if_fail (fp != NULL);
     g_return_if_fail (mimeinfo->mime_type == MIME_MULTIPART);
 
-    g_message ("** multipart/encrypted encountered");
+    debug_print ("** decrypting multipart/encrypted message\n");
 
     /* skip headers */
     if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
         perror("fseek");
-    while (fgets(buf, sizeof(buf), fp) != NULL)
-        if (buf[0] == '\r' || buf[0] == '\n') break;
-    
-    procmime_scan_multipart_message(mimeinfo, fp);
+    tmpinfo = procmime_scan_mime_header(fp);
+    if (!tmpinfo || tmpinfo->mime_type != MIME_MULTIPART) {
+        DECRYPTION_ABORT();
+    }
+
+    procmime_scan_multipart_message(tmpinfo, fp);
 
     /* check that we have the 2 parts */
-    n = found = 0;
-    for (partinfo = mimeinfo->children; partinfo; partinfo = partinfo->next) {
-        if (++n == 1 && !strcmp (partinfo->content_type,
-                                "application/pgp-encrypted")) {
-            /* Fixme: check that the version is 1 */
-            ver_okay = 1;
-        }
-        else if (n == 2 && !strcmp (partinfo->content_type,
-                                   "application/octet-stream")) {
-            if (partinfo->next)
-                g_warning ("** oops: pgp_encrypted with more than 2 parts");
-            break;
-        }
+    partinfo = tmpinfo->children;
+    if (!partinfo || !partinfo->next) {
+        DECRYPTION_ABORT();
     }
-
-    if (!ver_okay || !partinfo) {
-        msginfo->decryption_failed = 1;
-        /* fixme: remove the stuff, that the above procmime_scan_multiparts() 
-         * has appended to mimeino */
-        return;
+    if (!g_strcasecmp (partinfo->content_type, "application/pgp-encrypted")) {
+        /* Fixme: check that the version is 1 */
+        ver_ok = 1;
+    }
+    partinfo = partinfo->next;
+    if (ver_ok &&
+        !g_strcasecmp (partinfo->content_type, "application/octet-stream")) {
+        if (partinfo->next)
+            g_warning ("oops: pgp_encrypted with more than 2 parts");
+    }
+    else {
+        DECRYPTION_ABORT();
     }
 
-    g_message ("** yep, it is pgp encrypted");
+    debug_print ("** yep, it is pgp encrypted\n");
 
     plain = pgp_decrypt (partinfo, fp);
     if (!plain) {
-        msginfo->decryption_failed = 1;
-        return;
+        DECRYPTION_ABORT();
     }
-    
+
     fname = g_strdup_printf("%s%cplaintext.%08x",
                            get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
 
-    if ((dstfp = fopen(fname, "w")) == NULL) {
+    if ((dstfp = fopen(fname, "wb")) == NULL) {
         FILE_OP_ERROR(fname, "fopen");
         g_free(fname);
-        msginfo->decryption_failed = 1;
-        return;
+        DECRYPTION_ABORT();
     }
 
     /* write the orginal header to the new file */
-    if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
+    if (fseek(fp, tmpinfo->fpos, SEEK_SET) < 0)
         perror("fseek");
 
     while (fgets(buf, sizeof(buf), fp)) {
@@ -615,24 +626,27 @@ void rfc2015_decrypt_message (MsgInfo *msginfo, MimeInfo *mimeinfo, FILE *fp)
 
     err = gpgme_data_rewind (plain);
     if (err)
-        g_message ("** gpgme_data_rewind failed: %s", gpgme_strerror (err));
+        debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
 
     while (!(err = gpgme_data_read (plain, buf, sizeof(buf), &nread))) {
         fwrite (buf, nread, 1, dstfp);
     }
 
     if (err != GPGME_EOF) {
-        g_warning ("** gpgme_data_read failed: %s", gpgme_strerror (err));
+        debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
     }
 
     fclose (dstfp);
+    procmime_mimeinfo_free_all(tmpinfo);
 
     msginfo->plaintext_file = fname;
     msginfo->decryption_failed = 0;
 }
 
+#undef DECRYPTION_ABORT
 
-/* 
+
+/*
  * plain contains an entire mime object.
  * Encrypt it and return an GpgmeData object with the encrypted version of
  * the file or NULL in case of error.
@@ -653,25 +667,64 @@ pgp_encrypt ( GpgmeData plain, GpgmeRecipients rset )
     }
 
     if (err) {
-        g_warning ("** encryption failed: %s", gpgme_strerror (err));
+        debug_print ("encryption failed: %s\n", gpgme_strerror (err));
         gpgme_data_release (cipher);
         cipher = NULL;
     }
     else {
-        g_message ("** encryption succeeded");
+        debug_print ("** encryption succeeded\n");
     }
 
     gpgme_release (ctx);
     return cipher;
 }
 
+/*
+ * Create and return a list of keys matching a key id
+ */
+
+GSList *rfc2015_create_signers_list (const char *keyid)
+{
+       GSList *key_list = NULL;
+       GpgmeCtx list_ctx = NULL;
+       GSList *p;
+       GpgmeError err;
+       GpgmeKey key;
+
+       err = gpgme_new (&list_ctx);
+       if (err)
+               goto leave;
+       err = gpgme_op_keylist_start (list_ctx, keyid, 1);
+       if (err)
+               goto leave;
+       while ( !(err = gpgme_op_keylist_next (list_ctx, &key)) ) {
+               key_list = g_slist_append (key_list, key);
+       }
+       if (err != GPGME_EOF)
+               goto leave;
+       err = 0;
+       if (key_list == NULL) {
+               debug_print ("no keys found for keyid \"%s\"\n", keyid);
+       }
+
+leave:
+       if (err) {
+               debug_print ("rfc2015_create_signers_list failed: %s\n", gpgme_strerror (err));
+               for (p = key_list; p != NULL; p = p->next)
+                       gpgme_key_unref ((GpgmeKey) p->data);
+               g_slist_free (key_list);
+       }
+       if (list_ctx)
+               gpgme_release (list_ctx);
+       return err ? NULL : key_list;
+}
 
 /*
  * Encrypt the file by extracting all recipients and finding the
  * encryption keys for all of them.  The file content is then replaced
  * by the encrypted one.  */
 int
-rfc2015_encrypt (const char *file, GSList *recp_list)
+rfc2015_encrypt (const char *file, GSList *recp_list, gboolean ascii_armored)
 {
     FILE *fp = NULL;
     char buf[BUFFSIZE];
@@ -689,7 +742,7 @@ rfc2015_encrypt (const char *file, GSList *recp_list)
     /* Create the list of recipients */
     rset = gpgmegtk_recipient_selection (recp_list);
     if (!rset) {
-        g_warning ("error creating recipient list" );
+        debug_print ("error creating recipient list\n" );
         goto failure;
     }
 
@@ -703,7 +756,7 @@ rfc2015_encrypt (const char *file, GSList *recp_list)
     if (!err)
        err = gpgme_data_new (&plain);
     if (err) {
-        g_message ("gpgme_data_new failed: %s", gpgme_strerror (err));
+        debug_print ("gpgme_data_new failed: %s\n", gpgme_strerror (err));
         goto failure;
     }
 
@@ -714,7 +767,7 @@ rfc2015_encrypt (const char *file, GSList *recp_list)
         /* fixme: check for overlong lines */
         if (headerp (buf, content_names)) {
             if (clineidx >= DIM (clines)) {
-                g_message ("rfc2015_encrypt: too many content lines");
+                debug_print ("rfc2015_encrypt: too many content lines\n");
                 goto failure;
             }
             clines[clineidx++] = g_strdup (buf);
@@ -745,7 +798,7 @@ rfc2015_encrypt (const char *file, GSList *recp_list)
 
     /* write them to the temp data and add the rest of the message */
     for (i = 0; !err && i < clineidx; i++) {
-        g_message ("%% %s:%d: cline=`%s'", __FILE__ ,__LINE__, clines[i]);
+        debug_print ("%% %s:%d: cline=`%s'", __FILE__ ,__LINE__, clines[i]);
         err = gpgme_data_write (plain, clines[i], strlen (clines[i]));
     }
     if (!err)
@@ -758,7 +811,7 @@ rfc2015_encrypt (const char *file, GSList *recp_list)
         goto failure;
     }
     if (err) {
-        g_warning ("** gpgme_data_write failed: %s", gpgme_strerror (err));
+        debug_print ("gpgme_data_write failed: %s\n", gpgme_strerror (err));
         goto failure;
     }
 
@@ -791,14 +844,14 @@ rfc2015_encrypt (const char *file, GSList *recp_list)
     /* Write the header, append new content lines, part 1 and part 2 header */
     err = gpgme_data_rewind (header);
     if (err) {
-        g_message ("gpgme_data_rewind failed: %s", gpgme_strerror (err));
+        debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
         goto failure;
     }
     while (!(err = gpgme_data_read (header, buf, BUFFSIZE, &nread))) {
         fwrite (buf, nread, 1, fp);
     }
     if (err != GPGME_EOF) {
-        g_warning ("** gpgme_data_read failed: %s", gpgme_strerror (err));
+        debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
         goto failure;
     }
     if (ferror (fp)) {
@@ -810,25 +863,32 @@ rfc2015_encrypt (const char *file, GSList *recp_list)
     if (!mime_version_seen) 
         fputs ("MIME-Version: 1\r\n", fp);
 
-    fprintf (fp,
-            "Content-Type: multipart/encrypted;"
-            " protocol=\"application/pgp-encrypted\";\r\n"
-            " boundary=\"%s\"\r\n"
-            "\r\n"
-            "--%s\r\n"
-            "Content-Type: application/pgp-encrypted\r\n"
-            "\r\n"
-            "Version: 1\r\n"
-            "\r\n"
-            "--%s\r\n"
-            "Content-Type: application/octet-stream\r\n"
-            "\r\n",
-            boundary, boundary, boundary);
+    if (ascii_armored) {
+        fprintf(fp, 
+            "Content-Type: text/plain; charset=us-ascii\r\n"
+            "Content-Transfer-Encoding: 7bit\r\n"  
+            "\r\n");
+    } else {
+        fprintf (fp,
+               "Content-Type: multipart/encrypted;"
+               " protocol=\"application/pgp-encrypted\";\r\n"
+               " boundary=\"%s\"\r\n"
+               "\r\n"
+               "--%s\r\n"
+               "Content-Type: application/pgp-encrypted\r\n"
+               "\r\n"
+               "Version: 1\r\n"
+               "\r\n"
+               "--%s\r\n"
+               "Content-Type: application/octet-stream\r\n"
+               "\r\n",
+               boundary, boundary, boundary);
+    }
 
     /* append the encrypted stuff */
     err = gpgme_data_rewind (cipher);
     if (err) {
-        g_warning ("** gpgme_data_rewind on cipher failed: %s",
+        debug_print ("** gpgme_data_rewind on cipher failed: %s\n",
                    gpgme_strerror (err));
         goto failure;
     }
@@ -837,16 +897,18 @@ rfc2015_encrypt (const char *file, GSList *recp_list)
         fwrite (buf, nread, 1, fp);
     }
     if (err != GPGME_EOF) {
-        g_warning ("** gpgme_data_read failed: %s", gpgme_strerror (err));
+        debug_print ("** gpgme_data_read failed: %s\n", gpgme_strerror (err));
         goto failure;
     }
 
     /* and the final boundary */
-    fprintf (fp,
-            "\r\n"
-            "--%s--\r\n"
-            "\r\n",
-            boundary);
+    if (!ascii_armored) {
+        fprintf (fp,
+               "\r\n"
+               "--%s--\r\n"
+               "\r\n",
+            boundary);
+    }
     fflush (fp);
     if (ferror (fp)) {
         FILE_OP_ERROR (file, "fwrite");
@@ -867,79 +929,21 @@ failure:
     return -1; /* error */
 }
 
-static int
-set_signers (GpgmeCtx ctx, PrefsAccount *ac)
-{
-    GSList *key_list = NULL;
-    GpgmeCtx list_ctx = NULL;
-    const char *keyid = NULL;
-    GSList *p;
-    GpgmeError err;
-    GpgmeKey key;
-
-    if (ac == NULL)
-       return 0;
-
-    switch (ac->sign_key) {
-    case SIGN_KEY_DEFAULT:
-       return 0;               /* nothing to do */
-
-    case SIGN_KEY_BY_FROM:
-       keyid = ac->address;
-       break;
-
-    case SIGN_KEY_CUSTOM:
-       keyid = ac->sign_key_id;
-       break;
-
-    default:
-       g_assert_not_reached ();
-    }
-
-    err = gpgme_new (&list_ctx);
-    if (err)
-       goto leave;
-    err = gpgme_op_keylist_start (list_ctx, keyid, 1);
-    if (err)
-       goto leave;
-    while ( !(err = gpgme_op_keylist_next (list_ctx, &key)) ) {
-       key_list = g_slist_append (key_list, key);
-    }
-    if (err != GPGME_EOF)
-       goto leave;
-    if (key_list == NULL) {
-       g_warning ("no keys found for keyid \"%s\"", keyid);
-    }
-    gpgme_signers_clear (ctx);
-    for (p = key_list; p != NULL; p = p->next) {
-       err = gpgme_signers_add (ctx, (GpgmeKey) p->data);
-       if (err)
-           goto leave;
-    }
-
-leave:
-    if (err)
-        g_message ("** set_signers failed: %s", gpgme_strerror (err));
-    for (p = key_list; p != NULL; p = p->next)
-       gpgme_key_unref ((GpgmeKey) p->data);
-    g_slist_free (key_list);
-    if (list_ctx)
-       gpgme_release (list_ctx);
-    return err;
-}
-
 /* 
  * plain contains an entire mime object.  Sign it and return an
  * GpgmeData object with the signature of it or NULL in case of error.
+ * r_siginfo returns an XML object with information about the signature.
  */
 static GpgmeData
-pgp_sign (GpgmeData plain, PrefsAccount *ac)
+pgp_sign (GpgmeData plain, GSList *key_list, char **r_siginfo)
 {
+    GSList *p;
     GpgmeCtx ctx = NULL;
     GpgmeError err;
     GpgmeData sig = NULL;
     struct passphrase_cb_info_s info;
 
+    *r_siginfo = NULL;
     memset (&info, 0, sizeof info);
 
     err = gpgme_new (&ctx);
@@ -951,34 +955,107 @@ pgp_sign (GpgmeData plain, PrefsAccount *ac)
 
     if (!getenv("GPG_AGENT_INFO")) {
         info.c = ctx;
-        gpgme_set_passphrase_cb (ctx, passphrase_cb, &info);
+        gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
     }
     gpgme_set_textmode (ctx, 1);
     gpgme_set_armor (ctx, 1);
-    err = set_signers (ctx, ac);
+    gpgme_signers_clear (ctx);
+    for (p = key_list; p != NULL; p = p->next) {
+       err = gpgme_signers_add (ctx, (GpgmeKey) p->data);
+       if (err)
+           goto leave;
+    }
+    for (p = key_list; p != NULL; p = p->next)
+       gpgme_key_unref ((GpgmeKey) p->data);
+    g_slist_free (key_list);
+
     if (err)
        goto leave;
     err = gpgme_op_sign (ctx, plain, sig, GPGME_SIG_MODE_DETACH);
+    if (!err)
+        *r_siginfo = gpgme_get_op_info (ctx, 0);
 
 leave:
     if (err) {
-        g_message ("** signing failed: %s", gpgme_strerror (err));
+        gpgmegtk_free_passphrase();
+        debug_print ("signing failed: %s\n", gpgme_strerror (err));
         gpgme_data_release (sig);
         sig = NULL;
     }
     else {
-        g_message ("** signing succeeded");
+        debug_print ("signing succeeded\n");
     }
 
     gpgme_release (ctx);
     return sig;
 }
 
+/*
+ * Find TAG in XML and return a pointer into xml set just behind the
+ * closing angle.  Return NULL if not found. 
+ */
+static const char *
+find_xml_tag (const char *xml, const char *tag)
+{
+    int taglen = strlen (tag);
+    const char *s = xml;
+    while ( (s = strchr (s, '<')) ) {
+        s++;
+        if (!strncmp (s, tag, taglen)) {
+            const char *s2 = s + taglen;
+            if (*s2 == '>' || isspace (*(const unsigned char*)s2) ) {
+                /* found */
+                while (*s2 && *s2 != '>') /* skip attributes */
+                    s2++;
+                /* fixme: do need to handle angles inside attribute vallues? */
+                return *s2? (s2+1):NULL;
+            }
+        }
+        while (*s && *s != '>') /* skip to end of tag */
+            s++;
+    }
+    return NULL;
+}
+
+
+/*
+ * Extract the micalg from an GnupgOperationInfo XML container.
+ */
+static char *
+extract_micalg (char *xml)
+{
+    const char *s;
+
+    s = find_xml_tag (xml, "GnupgOperationInfo");
+    if (s) {
+        const char *s_end = find_xml_tag (s, "/GnupgOperationInfo");
+        s = find_xml_tag (s, "signature");
+        if (s && s_end && s < s_end) {
+            const char *s_end2 = find_xml_tag (s, "/signature");
+            if (s_end2 && s_end2 < s_end) {
+                s = find_xml_tag (s, "micalg");
+                if (s && s < s_end2) {
+                    s_end = strchr (s, '<');
+                    if (s_end) {
+                        char *p = g_malloc (s_end - s + 1);
+                        memcpy (p, s, s_end - s);
+                        p[s_end-s] = 0;
+                        return p;
+                    }
+                }
+            }
+        }
+    }
+    return NULL;
+}
+
+
 /*
  * Sign the file and replace its content with the signed one.
  */
 int
-rfc2015_sign (const char *file, PrefsAccount *ac)
+rfc2015_sign (const char *file, GSList *key_list)
 {
     FILE *fp = NULL;
     char buf[BUFFSIZE];
@@ -991,6 +1068,8 @@ rfc2015_sign (const char *file, PrefsAccount *ac)
     size_t nread;
     int mime_version_seen = 0;
     char *boundary = create_boundary ();
+    char *micalg = NULL;
+    char *siginfo;
 
     /* Open the source file */
     if ((fp = fopen(file, "rb")) == NULL) {
@@ -1002,7 +1081,7 @@ rfc2015_sign (const char *file, PrefsAccount *ac)
     if (!err)
         err = gpgme_data_new (&plain);
     if (err) {
-        g_message ("gpgme_data_new failed: %s", gpgme_strerror (err));
+        debug_print ("gpgme_data_new failed: %s\n", gpgme_strerror (err));
         goto failure;
     }
 
@@ -1013,7 +1092,7 @@ rfc2015_sign (const char *file, PrefsAccount *ac)
         /* fixme: check for overlong lines */
         if (headerp (buf, content_names)) {
             if (clineidx >= DIM (clines)) {
-                g_message ("rfc2015_sign: too many content lines");
+                debug_print ("rfc2015_sign: too many content lines\n");
                 goto failure;
             }
             clines[clineidx++] = g_strdup (buf);
@@ -1056,11 +1135,15 @@ rfc2015_sign (const char *file, PrefsAccount *ac)
         goto failure;
     }
     if (err) {
-        g_message ("** gpgme_data_write failed: %s", gpgme_strerror (err));
+        debug_print ("gpgme_data_write failed: %s\n", gpgme_strerror (err));
         goto failure;
     }
 
-    sigdata = pgp_sign (plain, ac);
+    sigdata = pgp_sign (plain, key_list, &siginfo); 
+    if (siginfo) {
+       micalg = extract_micalg (siginfo);
+       free (siginfo);
+    }
     if (!sigdata) 
         goto failure;
 
@@ -1080,12 +1163,12 @@ rfc2015_sign (const char *file, PrefsAccount *ac)
     /* Write the rfc822 header and add new content lines */
     err = gpgme_data_rewind (header);
     if (err)
-        g_message ("** gpgme_data_rewind failed: %s", gpgme_strerror (err));
+        debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
     while (!(err = gpgme_data_read (header, buf, BUFFSIZE, &nread))) {
         fwrite (buf, nread, 1, fp);
     }
     if (err != GPGME_EOF) {
-        g_message ("** gpgme_data_read failed: %s", gpgme_strerror (err));
+        debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
         goto failure;
     }
     if (ferror (fp)) {
@@ -1098,14 +1181,16 @@ rfc2015_sign (const char *file, PrefsAccount *ac)
     if (!mime_version_seen) 
         fputs ("MIME-Version: 1\r\n", fp);
     fprintf (fp, "Content-Type: multipart/signed; "
-                "protocol=\"application/pgp-signature\";\r\n"
-                " boundary=\"%s\"\r\n", boundary );
+             "protocol=\"application/pgp-signature\";\r\n");
+    if (micalg)
+        fprintf (fp, " micalg=%s;", micalg );
+    fprintf (fp, " boundary=\"%s\"\r\n", boundary );
 
     /* Part 1: signed material */
     fprintf (fp, "\r\n--%s\r\n", boundary);
     err = gpgme_data_rewind (plain);
     if (err) {
-        g_message ("** gpgme_data_rewind on plain failed: %s",
+        debug_print ("gpgme_data_rewind on plain failed: %s\n",
                    gpgme_strerror (err));
         goto failure;
     }
@@ -1113,7 +1198,7 @@ rfc2015_sign (const char *file, PrefsAccount *ac)
         fwrite (buf, nread, 1, fp);   
     }
     if (err != GPGME_EOF) {
-        g_message ("** gpgme_data_read failed: %s", gpgme_strerror (err));
+        debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
         goto failure;
     }
 
@@ -1124,7 +1209,7 @@ rfc2015_sign (const char *file, PrefsAccount *ac)
 
     err = gpgme_data_rewind (sigdata);
     if (err) {
-        g_message ("** gpgme_data_rewind on sigdata failed: %s",
+        debug_print ("gpgme_data_rewind on sigdata failed: %s\n",
                    gpgme_strerror (err));
         goto failure;
     }
@@ -1133,7 +1218,7 @@ rfc2015_sign (const char *file, PrefsAccount *ac)
         fwrite (buf, nread, 1, fp);
     }
     if (err != GPGME_EOF) {
-        g_message ("** gpgme_data_read failed: %s", gpgme_strerror (err));
+        debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
         goto failure;
     }
 
@@ -1149,6 +1234,7 @@ rfc2015_sign (const char *file, PrefsAccount *ac)
     gpgme_data_release (plain);
     gpgme_data_release (sigdata);
     g_free (boundary);
+    g_free (micalg);
     return 0;
 
 failure:
@@ -1158,6 +1244,7 @@ failure:
     gpgme_data_release (plain);
     gpgme_data_release (sigdata);
     g_free (boundary);
+    g_free (micalg);
     return -1; /* error */
 }