sync with 0.8.1cvs12
[claws.git] / src / rfc2015.c
index 27e6c1c8905b372c555945bfe4e6b58c28c3c60d..57409fe0f0b28847bde8d7473d3046a243609d4b 100644 (file)
@@ -61,14 +61,9 @@ static char *mime_version_name[] = {
     NULL
 };
 
     NULL
 };
 
-
-struct passphrase_cb_info_s {
-    GpgmeCtx c;
-    int did_it;
-};
-
 static char *create_boundary (void);
 
 static char *create_boundary (void);
 
+#if 0
 static void dump_mimeinfo (const char *text, MimeInfo *x)
 {
     debug_print ("MimeInfo[%s] %p  level=%d\n",
 static void dump_mimeinfo (const char *text, MimeInfo *x)
 {
     debug_print ("MimeInfo[%s] %p  level=%d\n",
@@ -108,6 +103,7 @@ static void dump_part ( MimeInfo *mimeinfo, FILE *fp )
         debug_print ("dump_part: read error\n");
     debug_print ("--- end dump_part ----\n");
 }
         debug_print ("dump_part: read error\n");
     debug_print ("--- end dump_part ----\n");
 }
+#endif
 
 void
 rfc2015_disable_all (void)
 
 void
 rfc2015_disable_all (void)
@@ -333,35 +329,11 @@ leave:
        gpgmegtk_sig_status_destroy (statuswindow);
 }
 
        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\n");
-        gpgme_cancel (ctx);
-    }
-    else
-        debug_print ("sending passphrase\n");
-
-    return pass;
-}
-
 /*
  * Copy a gpgme data object to a temporary file and
  * return this filename 
  */
 /*
  * 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)
 {
 static char *
 copy_gpgmedata_to_temp (GpgmeData data, guint *length)
 {
@@ -375,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 );
 
     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;
         FILE_OP_ERROR(tmp, "fopen");
         g_free(tmp);
         return NULL;
@@ -397,6 +369,7 @@ copy_gpgmedata_to_temp (GpgmeData data, guint *length)
 
     return tmp;
 }
 
     return tmp;
 }
+#endif
 
 static GpgmeData
 pgp_decrypt (MimeInfo *partinfo, FILE *fp)
 
 static GpgmeData
 pgp_decrypt (MimeInfo *partinfo, FILE *fp)
@@ -430,7 +403,7 @@ pgp_decrypt (MimeInfo *partinfo, FILE *fp)
 
     if (!getenv("GPG_AGENT_INFO")) {
         info.c = ctx;
 
     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);
     } 
 
     err = gpgme_op_decrypt (ctx, cipher, plain);
@@ -438,12 +411,13 @@ pgp_decrypt (MimeInfo *partinfo, FILE *fp)
 leave:
     gpgme_data_release (cipher);
     if (err) {
 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 failed: %s\n", gpgme_strerror (err));
         gpgme_data_release (plain);
         plain = NULL;
     }
     else
-        debug_print ("decryption succeeded\n");
+        debug_print ("** decryption succeeded\n");
 
     gpgme_release (ctx);
     return plain;
 
     gpgme_release (ctx);
     return plain;
@@ -459,7 +433,7 @@ MimeInfo * rfc2015_find_signature (MimeInfo *mimeinfo)
     if (g_strcasecmp (mimeinfo->content_type, "multipart/signed"))
         return NULL;
 
     if (g_strcasecmp (mimeinfo->content_type, "multipart/signed"))
         return NULL;
 
-    debug_print ("multipart/signed encountered\n");
+    debug_print ("** multipart/signed encountered\n");
 
     /* check that we have at least 2 parts of the correct type */
     for (partinfo = mimeinfo->children;
 
     /* check that we have at least 2 parts of the correct type */
     for (partinfo = mimeinfo->children;
@@ -502,10 +476,30 @@ int rfc2015_is_encrypted (MimeInfo *mimeinfo)
         return 0;
     if (g_strcasecmp (mimeinfo->content_type, "multipart/encrypted"))
         return 0;
         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;
 }
 
     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)
 {
 static int
 name_cmp(const char *a, const char *b)
 {
@@ -547,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;
 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;
     char *fname;
     GpgmeData plain;
     FILE *dstfp;
@@ -560,61 +560,60 @@ void rfc2015_decrypt_message (MsgInfo *msginfo, MimeInfo *mimeinfo, FILE *fp)
     char buf[BUFFSIZE];
     GpgmeError err;
 
     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_return_if_fail (mimeinfo->mime_type == MIME_MULTIPART);
 
-    debug_print ("multipart/encrypted encountered\n");
+    debug_print ("** decrypting multipart/encrypted message\n");
 
     /* skip headers */
     if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
         perror("fseek");
 
     /* 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 */
 
     /* check that we have the 2 parts */
-    n = found = 0;
-    for (partinfo = mimeinfo->children; partinfo; partinfo = partinfo->next) {
-        if (++n == 1 && !g_strcasecmp (partinfo->content_type,
-                                      "application/pgp-encrypted")) {
-            /* Fixme: check that the version is 1 */
-            ver_okay = 1;
-        }
-        else if (n == 2 && !g_strcasecmp (partinfo->content_type,
-                                         "application/octet-stream")) {
-            if (partinfo->next)
-                debug_print ("oops: pgp_encrypted with more than 2 parts\n");
-            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();
     }
 
     }
 
-    debug_print ("yep, it is pgp encrypted\n");
+    debug_print ("** yep, it is pgp encrypted\n");
 
     plain = pgp_decrypt (partinfo, fp);
     if (!plain) {
 
     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);
 
     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);
         FILE_OP_ERROR(fname, "fopen");
         g_free(fname);
-        msginfo->decryption_failed = 1;
-        return;
+        DECRYPTION_ABORT();
     }
 
     /* write the orginal header to the new file */
     }
 
     /* 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)) {
         perror("fseek");
 
     while (fgets(buf, sizeof(buf), fp)) {
@@ -638,13 +637,16 @@ void rfc2015_decrypt_message (MsgInfo *msginfo, MimeInfo *mimeinfo, FILE *fp)
     }
 
     fclose (dstfp);
     }
 
     fclose (dstfp);
+    procmime_mimeinfo_free_all(tmpinfo);
 
     msginfo->plaintext_file = fname;
     msginfo->decryption_failed = 0;
 }
 
 
     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.
  * 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.
@@ -670,7 +672,7 @@ pgp_encrypt ( GpgmeData plain, GpgmeRecipients rset )
         cipher = NULL;
     }
     else {
         cipher = NULL;
     }
     else {
-        debug_print ("encryption succeeded\n");
+        debug_print ("** encryption succeeded\n");
     }
 
     gpgme_release (ctx);
     }
 
     gpgme_release (ctx);
@@ -796,7 +798,7 @@ rfc2015_encrypt (const char *file, GSList *recp_list, gboolean ascii_armored)
 
     /* write them to the temp data and add the rest of the message */
     for (i = 0; !err && i < clineidx; i++) {
 
     /* write them to the temp data and add the rest of the message */
     for (i = 0; !err && i < clineidx; i++) {
-        debug_print ("%s:%d: cline=`%s'\n", __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)
         err = gpgme_data_write (plain, clines[i], strlen (clines[i]));
     }
     if (!err)
@@ -930,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.
 /* 
  * 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
  */
 static GpgmeData
-pgp_sign (GpgmeData plain, GSList *key_list)
+pgp_sign (GpgmeData plain, GSList *key_list, char **r_siginfo)
 {
     GSList *p;
     GpgmeCtx ctx = NULL;
 {
     GSList *p;
     GpgmeCtx ctx = NULL;
@@ -940,6 +943,7 @@ pgp_sign (GpgmeData plain, GSList *key_list)
     GpgmeData sig = NULL;
     struct passphrase_cb_info_s info;
 
     GpgmeData sig = NULL;
     struct passphrase_cb_info_s info;
 
+    *r_siginfo = NULL;
     memset (&info, 0, sizeof info);
 
     err = gpgme_new (&ctx);
     memset (&info, 0, sizeof info);
 
     err = gpgme_new (&ctx);
@@ -951,7 +955,7 @@ pgp_sign (GpgmeData plain, GSList *key_list)
 
     if (!getenv("GPG_AGENT_INFO")) {
         info.c = ctx;
 
     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);
     }
     gpgme_set_textmode (ctx, 1);
     gpgme_set_armor (ctx, 1);
@@ -968,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)
        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) {
 
 leave:
     if (err) {
+        gpgmegtk_free_passphrase();
         debug_print ("signing failed: %s\n", gpgme_strerror (err));
         gpgme_data_release (sig);
         sig = NULL;
         debug_print ("signing failed: %s\n", gpgme_strerror (err));
         gpgme_data_release (sig);
         sig = NULL;
@@ -983,6 +990,67 @@ leave:
     return sig;
 }
 
     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.
  */
 /*
  * Sign the file and replace its content with the signed one.
  */
@@ -1000,6 +1068,8 @@ rfc2015_sign (const char *file, GSList *key_list)
     size_t nread;
     int mime_version_seen = 0;
     char *boundary = create_boundary ();
     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) {
 
     /* Open the source file */
     if ((fp = fopen(file, "rb")) == NULL) {
@@ -1069,7 +1139,11 @@ rfc2015_sign (const char *file, GSList *key_list)
         goto failure;
     }
 
         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;
 
     if (!sigdata) 
         goto failure;
 
@@ -1107,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; "
     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);
 
     /* Part 1: signed material */
     fprintf (fp, "\r\n--%s\r\n", boundary);
@@ -1158,6 +1234,7 @@ rfc2015_sign (const char *file, GSList *key_list)
     gpgme_data_release (plain);
     gpgme_data_release (sigdata);
     g_free (boundary);
     gpgme_data_release (plain);
     gpgme_data_release (sigdata);
     g_free (boundary);
+    g_free (micalg);
     return 0;
 
 failure:
     return 0;
 
 failure:
@@ -1167,6 +1244,7 @@ failure:
     gpgme_data_release (plain);
     gpgme_data_release (sigdata);
     g_free (boundary);
     gpgme_data_release (plain);
     gpgme_data_release (sigdata);
     g_free (boundary);
+    g_free (micalg);
     return -1; /* error */
 }
 
     return -1; /* error */
 }