sync with 0.8.1cvs12
[claws.git] / src / rfc2015.c
index 9edc40c6c743de463af4705b04aedc76be7ffd48..57409fe0f0b28847bde8d7473d3046a243609d4b 100644 (file)
@@ -61,12 +61,6 @@ static char *mime_version_name[] = {
     NULL
 };
 
-
-struct passphrase_cb_info_s {
-    GpgmeCtx c;
-    int did_it;
-};
-
 static char *create_boundary (void);
 
 #if 0
@@ -335,31 +329,6 @@ leave:
        gpgmegtk_sig_status_destroy (statuswindow);
 }
 
-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);
-    debug_print ("%% requesting passphrase for `%s': ", desc );
-    pass = gpgmegtk_passphrase_mbox (desc);
-    if (!pass) {
-        debug_print ("%% cancel passphrase entry");
-        gpgme_cancel (ctx);
-    }
-    else
-        debug_print ("%% sending passphrase");
-
-    return pass;
-}
-
 /*
  * Copy a gpgme data object to a temporary file and
  * return this filename 
@@ -378,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;
@@ -434,7 +403,7 @@ pgp_decrypt (MimeInfo *partinfo, FILE *fp)
 
     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);
@@ -442,12 +411,13 @@ pgp_decrypt (MimeInfo *partinfo, FILE *fp)
 leave:
     gpgme_data_release (cipher);
     if (err) {
+        gpgmegtk_free_passphrase();
         debug_print ("decryption failed: %s\n", gpgme_strerror (err));
         gpgme_data_release (plain);
         plain = NULL;
     }
     else
-        debug_print ("** decryption succeeded");
+        debug_print ("** decryption succeeded\n");
 
     gpgme_release (ctx);
     return plain;
@@ -463,7 +433,7 @@ MimeInfo * rfc2015_find_signature (MimeInfo *mimeinfo)
     if (g_strcasecmp (mimeinfo->content_type, "multipart/signed"))
         return NULL;
 
-    debug_print ("** 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;
@@ -506,10 +476,30 @@ int rfc2015_is_encrypted (MimeInfo *mimeinfo)
         return 0;
     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)
 {
@@ -575,7 +565,7 @@ void rfc2015_decrypt_message (MsgInfo *msginfo, MimeInfo *mimeinfo, FILE *fp)
     g_return_if_fail (fp != NULL);
     g_return_if_fail (mimeinfo->mime_type == MIME_MULTIPART);
 
-    debug_print ("** decrypting multipart/encrypted message");
+    debug_print ("** decrypting multipart/encrypted message\n");
 
     /* skip headers */
     if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
@@ -606,7 +596,7 @@ void rfc2015_decrypt_message (MsgInfo *msginfo, MimeInfo *mimeinfo, FILE *fp)
         DECRYPTION_ABORT();
     }
 
-    debug_print ("** yep, it is pgp encrypted");
+    debug_print ("** yep, it is pgp encrypted\n");
 
     plain = pgp_decrypt (partinfo, fp);
     if (!plain) {
@@ -616,7 +606,7 @@ void rfc2015_decrypt_message (MsgInfo *msginfo, MimeInfo *mimeinfo, FILE *fp)
     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);
         DECRYPTION_ABORT();
@@ -682,7 +672,7 @@ pgp_encrypt ( GpgmeData plain, GpgmeRecipients rset )
         cipher = NULL;
     }
     else {
-        debug_print ("** encryption succeeded");
+        debug_print ("** encryption succeeded\n");
     }
 
     gpgme_release (ctx);
@@ -942,9 +932,10 @@ failure:
 /* 
  * 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, GSList *key_list)
+pgp_sign (GpgmeData plain, GSList *key_list, char **r_siginfo)
 {
     GSList *p;
     GpgmeCtx ctx = NULL;
@@ -952,6 +943,7 @@ pgp_sign (GpgmeData plain, GSList *key_list)
     GpgmeData sig = NULL;
     struct passphrase_cb_info_s info;
 
+    *r_siginfo = NULL;
     memset (&info, 0, sizeof info);
 
     err = gpgme_new (&ctx);
@@ -963,7 +955,7 @@ pgp_sign (GpgmeData plain, GSList *key_list)
 
     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);
@@ -980,9 +972,12 @@ pgp_sign (GpgmeData plain, GSList *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) {
+        gpgmegtk_free_passphrase();
         debug_print ("signing failed: %s\n", gpgme_strerror (err));
         gpgme_data_release (sig);
         sig = NULL;
@@ -995,6 +990,67 @@ leave:
     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.
  */
@@ -1012,6 +1068,8 @@ rfc2015_sign (const char *file, GSList *key_list)
     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) {
@@ -1081,7 +1139,11 @@ rfc2015_sign (const char *file, GSList *key_list)
         goto failure;
     }
 
-    sigdata = pgp_sign (plain, key_list);
+    sigdata = pgp_sign (plain, key_list, &siginfo); 
+    if (siginfo) {
+       micalg = extract_micalg (siginfo);
+       free (siginfo);
+    }
     if (!sigdata) 
         goto failure;
 
@@ -1119,8 +1181,10 @@ rfc2015_sign (const char *file, GSList *key_list)
     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);
@@ -1170,6 +1234,7 @@ rfc2015_sign (const char *file, GSList *key_list)
     gpgme_data_release (plain);
     gpgme_data_release (sigdata);
     g_free (boundary);
+    g_free (micalg);
     return 0;
 
 failure:
@@ -1179,6 +1244,7 @@ failure:
     gpgme_data_release (plain);
     gpgme_data_release (sigdata);
     g_free (boundary);
+    g_free (micalg);
     return -1; /* error */
 }