2004-11-09 [colin] 0.9.12cvs144.1
[claws.git] / src / plugins / pgpmime / pgpmime.c
index b45b0e02718c391b529153a1e8f1dd2e672f133c..d027c75631a4e7f3972097232b2e681cb8cc34bd 100644 (file)
@@ -26,6 +26,7 @@
 #include "defs.h"
 #include <glib.h>
 #include <gpgme.h>
+#include <ctype.h>
 
 #include "utils.h"
 #include "privacy.h"
@@ -33,6 +34,8 @@
 #include "pgpmime.h"
 #include "sgpgme.h"
 #include "prefs_common.h"
+#include "prefs_gpg.h"
+#include "passphrase.h"
 
 typedef struct _PrivacyDataPGP PrivacyDataPGP;
 
@@ -90,10 +93,10 @@ static gboolean pgpmime_is_signed(MimeInfo *mimeinfo)
        if (parent == NULL)
                return FALSE;
        if ((parent->type != MIMETYPE_MULTIPART) ||
-           g_strcasecmp(parent->subtype, "signed"))
+           g_ascii_strcasecmp(parent->subtype, "signed"))
                return FALSE;
        protocol = procmime_mimeinfo_get_parameter(parent, "protocol");
-       if ((protocol == NULL) || g_strcasecmp(protocol, "application/pgp-signature"))
+       if ((protocol == NULL) || g_ascii_strcasecmp(protocol, "application/pgp-signature"))
                return FALSE;
 
        /* check if mimeinfo is the first child */
@@ -106,7 +109,7 @@ static gboolean pgpmime_is_signed(MimeInfo *mimeinfo)
        if (signature == NULL)
                return FALSE;
        if ((signature->type != MIMETYPE_APPLICATION) ||
-           g_strcasecmp(signature->subtype, "pgp-signature"))
+           g_ascii_strcasecmp(signature->subtype, "pgp-signature"))
                return FALSE;
 
        if (data == NULL) {
@@ -116,21 +119,47 @@ static gboolean pgpmime_is_signed(MimeInfo *mimeinfo)
        data->done_sigtest = TRUE;
        data->is_signed = TRUE;
 
-       if (prefs_common.auto_check_signatures)
-               pgpmime_check_signature(mimeinfo);
-       
        return TRUE;
 }
 
+static gchar *get_canonical_content(FILE *fp, const gchar *boundary)
+{
+       gchar *ret;
+       GString *textbuffer;
+       guint boundary_len;
+       gchar buf[BUFFSIZE];
+
+       boundary_len = strlen(boundary);
+       while (fgets(buf, sizeof(buf), fp) != NULL)
+               if (IS_BOUNDARY(buf, boundary, boundary_len))
+                       break;
+
+       textbuffer = g_string_new("");
+       while (fgets(buf, sizeof(buf), fp) != NULL) {
+               gchar *buf2;
+
+               if (IS_BOUNDARY(buf, boundary, boundary_len))
+                       break;
+               
+               buf2 = canonicalize_str(buf);
+               g_string_append(textbuffer, buf2);
+               g_free(buf2);
+       }
+       g_string_truncate(textbuffer, textbuffer->len - 2);
+               
+       ret = textbuffer->str;
+       g_string_free(textbuffer, FALSE);
+
+       return ret;
+}
+
 static gint pgpmime_check_signature(MimeInfo *mimeinfo)
 {
        PrivacyDataPGP *data;
        MimeInfo *parent, *signature;
        FILE *fp;
-       gchar buf[BUFFSIZE];
        gchar *boundary;
-       GString *textstr;
-       gint boundary_len;
+       gchar *textstr;
        GpgmeData sigdata, textdata;
        
        g_return_val_if_fail(mimeinfo != NULL, -1);
@@ -140,32 +169,16 @@ static gint pgpmime_check_signature(MimeInfo *mimeinfo)
        debug_print("Checking PGP/MIME signature\n");
        parent = procmime_mimeinfo_parent(mimeinfo);
 
-       fp = fopen(parent->filename, "rb");
+       fp = fopen(parent->data.filename, "rb");
        g_return_val_if_fail(fp != NULL, SIGNATURE_INVALID);
        
-       boundary = g_hash_table_lookup(parent->parameters, "boundary");
+       boundary = g_hash_table_lookup(parent->typeparameters, "boundary");
        if (!boundary)
                return 0;
 
-       boundary_len = strlen(boundary);
-       while (fgets(buf, sizeof(buf), fp) != NULL)
-               if (IS_BOUNDARY(buf, boundary, boundary_len))
-                       break;
-
-       textstr = g_string_new("");
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
-               gchar *buf2;
+       textstr = get_canonical_content(fp, boundary);
 
-               if (IS_BOUNDARY(buf, boundary, boundary_len))
-                       break;
-               
-               buf2 = canonicalize_str(buf);
-               g_string_append(textstr, buf2);
-               g_free(buf2);
-       }
-       g_string_truncate(textstr, textstr->len - 2);
-               
-       gpgme_data_new_from_mem(&textdata, textstr->str, textstr->len, 0);
+       gpgme_data_new_from_mem(&textdata, textstr, strlen(textstr), 0);
        signature = (MimeInfo *) mimeinfo->node->next->data;
        sigdata = sgpgme_data_from_mimeinfo(signature);
 
@@ -174,7 +187,7 @@ static gint pgpmime_check_signature(MimeInfo *mimeinfo)
        
        gpgme_data_release(sigdata);
        gpgme_data_release(textdata);
-       g_string_free(textstr, TRUE);
+       g_free(textstr);
        fclose(fp);
        
        return 0;
@@ -186,6 +199,10 @@ static SignatureStatus pgpmime_get_sig_status(MimeInfo *mimeinfo)
        
        g_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
 
+       if (data->sigstatus == GPGME_SIG_STAT_NONE && 
+           prefs_gpg_get_config()->auto_check_signatures)
+               pgpmime_check_signature(mimeinfo);
+       
        return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
 }
 
@@ -195,6 +212,10 @@ static gchar *pgpmime_get_sig_info_short(MimeInfo *mimeinfo)
        
        g_return_val_if_fail(data != NULL, g_strdup("Error"));
 
+       if (data->sigstatus == GPGME_SIG_STAT_NONE && 
+           prefs_gpg_get_config()->auto_check_signatures)
+               pgpmime_check_signature(mimeinfo);
+       
        return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
 }
 
@@ -204,6 +225,10 @@ static gchar *pgpmime_get_sig_info_full(MimeInfo *mimeinfo)
        
        g_return_val_if_fail(data != NULL, g_strdup("Error"));
 
+       if (data->sigstatus == GPGME_SIG_STAT_NONE && 
+           prefs_gpg_get_config()->auto_check_signatures)
+               pgpmime_check_signature(mimeinfo);
+       
        return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
 }
 
@@ -214,10 +239,10 @@ static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
        
        if (mimeinfo->type != MIMETYPE_MULTIPART)
                return FALSE;
-       if (g_strcasecmp(mimeinfo->subtype, "encrypted"))
+       if (g_ascii_strcasecmp(mimeinfo->subtype, "encrypted"))
                return FALSE;
        tmpstr = procmime_mimeinfo_get_parameter(mimeinfo, "protocol");
-       if ((tmpstr == NULL) || g_strcasecmp(tmpstr, "application/pgp-encrypted"))
+       if ((tmpstr == NULL) || g_ascii_strcasecmp(tmpstr, "application/pgp-encrypted"))
                return FALSE;
        if (g_node_n_children(mimeinfo->node) != 2)
                return FALSE;
@@ -225,13 +250,13 @@ static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
        tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 0)->data;
        if (tmpinfo->type != MIMETYPE_APPLICATION)
                return FALSE;
-       if (g_strcasecmp(tmpinfo->subtype, "pgp-encrypted"))
+       if (g_ascii_strcasecmp(tmpinfo->subtype, "pgp-encrypted"))
                return FALSE;
        
        tmpinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data;
        if (tmpinfo->type != MIMETYPE_APPLICATION)
                return FALSE;
-       if (g_strcasecmp(tmpinfo->subtype, "octet-stream"))
+       if (g_ascii_strcasecmp(tmpinfo->subtype, "octet-stream"))
                return FALSE;
        
        return TRUE;
@@ -296,7 +321,7 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
        g_node_unlink(decinfo->node);
        procmime_mimeinfo_free_all(parseinfo);
 
-       decinfo->tmpfile = TRUE;
+       decinfo->tmp = TRUE;
 
        if (sigstat != GPGME_SIG_STAT_NONE) {
                if (decinfo->privacy != NULL) {
@@ -311,15 +336,249 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
                if (data->ctx)
                        gpgme_release(data->ctx);
                data->ctx = ctx;
-       }
+       } else
+               gpgme_release(ctx);
 
-       
        return decinfo;
 }
 
+/*
+ * 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;
+}
+
+gboolean pgpmime_sign(MimeInfo *mimeinfo)
+{
+       MimeInfo *msgcontent, *sigmultipart, *newinfo;
+       gchar *textstr, *opinfo, *micalg;
+       FILE *fp;
+       gchar *boundary, *sigcontent;
+       GpgmeCtx ctx;
+       GpgmeData gpgtext, gpgsig;
+       guint len;
+       struct passphrase_cb_info_s info;
+  
+       memset (&info, 0, sizeof info);
+
+       /* remove content node from message */
+       msgcontent = (MimeInfo *) mimeinfo->node->children->data;
+       g_node_unlink(msgcontent->node);
+
+       /* create temporary multipart for content */
+       sigmultipart = procmime_mimeinfo_new();
+       sigmultipart->type = MIMETYPE_MULTIPART;
+       sigmultipart->subtype = g_strdup("signed");
+       boundary = generate_mime_boundary("Signature");
+       g_hash_table_insert(sigmultipart->typeparameters, g_strdup("boundary"),
+                            g_strdup(boundary));
+       g_hash_table_insert(sigmultipart->typeparameters, g_strdup("protocol"),
+                            g_strdup("application/pgp-signature"));
+       g_node_append(sigmultipart->node, msgcontent->node);
+       g_node_append(mimeinfo->node, sigmultipart->node);
+
+       /* write message content to temporary file */
+       fp = my_tmpfile();
+       procmime_write_mimeinfo(sigmultipart, fp);
+       rewind(fp);
+
+       /* read temporary file into memory */
+       textstr = get_canonical_content(fp, boundary);
+
+       fclose(fp);
+
+       gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
+       gpgme_data_new(&gpgsig);
+       gpgme_new(&ctx);
+       gpgme_set_textmode(ctx, 1);
+       gpgme_set_armor(ctx, 1);
+       gpgme_signers_clear(ctx);
+
+       if (!getenv("GPG_AGENT_INFO")) {
+               info.c = ctx;
+               gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
+       }
+
+       if (gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_DETACH) != GPGME_No_Error)
+               return FALSE;
+       opinfo = gpgme_get_op_info(ctx, 0);
+       micalg = extract_micalg(opinfo);
+       g_free(opinfo);
+
+       gpgme_release(ctx);
+       sigcontent = gpgme_data_release_and_get_mem(gpgsig, &len);
+       gpgme_data_release(gpgtext);
+       g_free(textstr);
+
+       /* add signature */
+       g_hash_table_insert(sigmultipart->typeparameters, g_strdup("micalg"),
+                            micalg);
+
+       newinfo = procmime_mimeinfo_new();
+       newinfo->type = MIMETYPE_APPLICATION;
+       newinfo->subtype = g_strdup("pgp-signature");
+       newinfo->content = MIMECONTENT_MEM;
+       newinfo->data.mem = g_malloc(len + 1);
+       g_memmove(newinfo->data.mem, sigcontent, len);
+       newinfo->data.mem[len] = '\0';
+       g_node_append(sigmultipart->node, newinfo->node);
+
+       g_free(sigcontent);
+
+       return TRUE;
+}
+
+gchar *pgpmime_get_encrypt_data(GSList *recp_names)
+{
+       return sgpgme_get_encrypt_data(recp_names);
+}
+
+gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
+{
+       MimeInfo *msgcontent, *encmultipart, *newinfo;
+       FILE *fp;
+       gchar *boundary, *enccontent;
+       guint len;
+       gchar *textstr;
+       GpgmeData gpgtext, gpgenc;
+       gchar **recipients, **nextrecp;
+       GpgmeRecipients recp;
+       GpgmeCtx ctx;
+
+       /* build GpgmeRecipients from encrypt_data */
+       recipients = g_strsplit(encrypt_data, " ", 0);
+       gpgme_recipients_new(&recp);
+       for (nextrecp = recipients; *nextrecp != NULL; nextrecp++) {
+               printf("%s\n", *nextrecp);
+               gpgme_recipients_add_name_with_validity(recp, *nextrecp,
+                                                       GPGME_VALIDITY_FULL);
+       }
+       g_strfreev(recipients);
+
+       debug_print("Encrypting message content\n");
+
+       /* remove content node from message */
+       msgcontent = (MimeInfo *) mimeinfo->node->children->data;
+       g_node_unlink(msgcontent->node);
+
+       /* create temporary multipart for content */
+       encmultipart = procmime_mimeinfo_new();
+       encmultipart->type = MIMETYPE_MULTIPART;
+       encmultipart->subtype = g_strdup("encrypted");
+       boundary = generate_mime_boundary("Encrypt");
+       g_hash_table_insert(encmultipart->typeparameters, g_strdup("boundary"),
+                            g_strdup(boundary));
+       g_hash_table_insert(encmultipart->typeparameters, g_strdup("protocol"),
+                            g_strdup("application/pgp-encrypted"));
+       g_node_append(encmultipart->node, msgcontent->node);
+
+       /* write message content to temporary file */
+       fp = my_tmpfile();
+       procmime_write_mimeinfo(encmultipart, fp);
+       rewind(fp);
+
+       /* read temporary file into memory */
+       textstr = get_canonical_content(fp, boundary);
+
+       fclose(fp);
+
+       /* encrypt data */
+       gpgme_data_new_from_mem(&gpgtext, textstr, strlen(textstr), 0);
+       gpgme_data_new(&gpgenc);
+       gpgme_new(&ctx);
+       gpgme_set_armor(ctx, 1);
+
+       gpgme_op_encrypt(ctx, recp, gpgtext, gpgenc);
+
+       gpgme_release(ctx);
+       enccontent = gpgme_data_release_and_get_mem(gpgenc, &len);
+       gpgme_recipients_release(recp);
+       gpgme_data_release(gpgtext);
+       g_free(textstr);
+
+       /* create encrypted multipart */
+       g_node_unlink(msgcontent->node);
+       procmime_mimeinfo_free_all(msgcontent);
+       g_node_append(mimeinfo->node, encmultipart->node);
+
+       newinfo = procmime_mimeinfo_new();
+       newinfo->type = MIMETYPE_APPLICATION;
+       newinfo->subtype = g_strdup("pgp-encrypted");
+       newinfo->content = MIMECONTENT_MEM;
+       newinfo->data.mem = g_strdup("Version: 1\n");
+       g_node_append(encmultipart->node, newinfo->node);
+
+       newinfo = procmime_mimeinfo_new();
+       newinfo->type = MIMETYPE_APPLICATION;
+       newinfo->subtype = g_strdup("octet-stream");
+       newinfo->content = MIMECONTENT_MEM;
+       newinfo->data.mem = g_malloc(len);
+       g_memmove(newinfo->data.mem, enccontent, len);
+       newinfo->data.mem[len] = '\0';
+       g_node_append(encmultipart->node, newinfo->node);
+
+       g_free(enccontent);
+
+       return TRUE;
+}
+
 static PrivacySystem pgpmime_system = {
        "pgpmime",                      /* id */
-       "PGP/Mime",                     /* name */
+       "PGP Mime",                     /* name */
 
        pgpmime_free_privacydata,       /* free_privacydata */
 
@@ -331,6 +590,13 @@ static PrivacySystem pgpmime_system = {
 
        pgpmime_is_encrypted,           /* is_encrypted(MimeInfo *) */
        pgpmime_decrypt,                /* decrypt(MimeInfo *) */
+
+       TRUE,
+       pgpmime_sign,
+
+       TRUE,
+       pgpmime_get_encrypt_data,
+       pgpmime_encrypt,
 };
 
 void pgpmime_init()