2006-06-15 [colin] 2.3.0cvs16
[claws.git] / src / plugins / pgpmime / pgpmime.c
index a48a0c6cb58c7e7716b2285ffb8295d860cc6e2e..706f7925f066e06331cd69ccb24773168479ca89 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2003 Hiroyuki Yamamoto & the Sylpheed-Claws team
+ * Copyright (C) 1999-2006 Hiroyuki Yamamoto & the Sylpheed-Claws team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -14,7 +14,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
 
 #include "defs.h"
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <gpgme.h>
 #include <ctype.h>
+#include <errno.h>
 
 #include "utils.h"
 #include "privacy.h"
 #include "procmime.h"
+
 #include "pgpmime.h"
-#include "sgpgme.h"
+#include <plugins/pgpcore/sgpgme.h>
+#include <plugins/pgpcore/prefs_gpg.h>
+#include <plugins/pgpcore/passphrase.h>
+
 #include "prefs_common.h"
-#include "prefs_gpg.h"
-#include "passphrase.h"
 
 typedef struct _PrivacyDataPGP PrivacyDataPGP;
 
@@ -96,7 +100,8 @@ static gboolean pgpmime_is_signed(MimeInfo *mimeinfo)
            g_ascii_strcasecmp(parent->subtype, "signed"))
                return FALSE;
        protocol = procmime_mimeinfo_get_parameter(parent, "protocol");
-       if ((protocol == NULL) || g_ascii_strcasecmp(protocol, "application/pgp-signature"))
+       if ((protocol == NULL) || 
+           (g_ascii_strcasecmp(protocol, "application/pgp-signature")))
                return FALSE;
 
        /* check if mimeinfo is the first child */
@@ -109,13 +114,14 @@ static gboolean pgpmime_is_signed(MimeInfo *mimeinfo)
        if (signature == NULL)
                return FALSE;
        if ((signature->type != MIMETYPE_APPLICATION) ||
-           g_ascii_strcasecmp(signature->subtype, "pgp-signature"))
+           (g_ascii_strcasecmp(signature->subtype, "pgp-signature")))
                return FALSE;
 
        if (data == NULL) {
                data = pgpmime_new_privacydata();
                mimeinfo->privacy = (PrivacyData *) data;
        }
+       
        data->done_sigtest = TRUE;
        data->is_signed = TRUE;
 
@@ -168,15 +174,23 @@ static gint pgpmime_check_signature(MimeInfo *mimeinfo)
        gpgme_new(&data->ctx);
        
        debug_print("Checking PGP/MIME signature\n");
+
+       err = gpgme_set_protocol(data->ctx, GPGME_PROTOCOL_OpenPGP);
+
+       if (err) {
+               debug_print ("gpgme_set_protocol failed: %s\n",
+                   gpgme_strerror (err));
+       }
        parent = procmime_mimeinfo_parent(mimeinfo);
 
-       fp = fopen(parent->data.filename, "rb");
+       fp = g_fopen(parent->data.filename, "rb");
        g_return_val_if_fail(fp != NULL, SIGNATURE_INVALID);
        
        boundary = g_hash_table_lookup(parent->typeparameters, "boundary");
-       if (!boundary)
+       if (!boundary) {
+               privacy_set_error(_("Signature boundary not found."));
                return 0;
-
+       }
        textstr = get_canonical_content(fp, boundary);
 
        err = gpgme_data_new_from_mem(&textdata, textstr, strlen(textstr), 0);
@@ -187,6 +201,16 @@ static gint pgpmime_check_signature(MimeInfo *mimeinfo)
        signature = (MimeInfo *) mimeinfo->node->next->data;
        sigdata = sgpgme_data_from_mimeinfo(signature);
 
+       err = 0;
+       if (signature->encoding_type == ENC_BASE64) {
+               err = gpgme_data_set_encoding (sigdata, GPGME_DATA_ENCODING_BASE64);
+       }
+       
+       if (err) {
+               debug_print ("gpgme_data_set_encoding failed: %s\n",
+                       gpgme_strerror (err));
+       }
+
        data->sigstatus =
                sgpgme_verify_signature (data->ctx, sigdata, textdata, NULL);
 
@@ -278,11 +302,13 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
        PrivacyDataPGP *data = NULL;
        gpgme_ctx_t ctx;
        gchar *chars;
-       int len;
+       size_t len;
+       gpgme_error_t err;
 
-       if (gpgme_new(&ctx) != GPG_ERR_NO_ERROR)
+       if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
+       privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
                return NULL;
-
+       }
        
        g_return_val_if_fail(pgpmime_is_encrypted(mimeinfo), NULL);
        
@@ -301,8 +327,9 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
        fname = g_strdup_printf("%s%cplaintext.%08x",
                get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
 
-       if ((dstfp = fopen(fname, "wb")) == NULL) {
+       if ((dstfp = g_fopen(fname, "wb")) == NULL) {
                FILE_OP_ERROR(fname, "fopen");
+               privacy_set_error(_("Couldn't open decrypted file %s"), fname);
                g_free(fname);
                gpgme_data_release(plain);
                gpgme_release(ctx);
@@ -321,11 +348,13 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
        g_free(fname);
        if (parseinfo == NULL) {
                gpgme_release(ctx);
+               privacy_set_error(_("Couldn't parse decrypted file."));
                return NULL;
        }
        decinfo = g_node_first_child(parseinfo->node) != NULL ?
                g_node_first_child(parseinfo->node)->data : NULL;
        if (decinfo == NULL) {
+               privacy_set_error(_("Couldn't parse decrypted file parts."));
                gpgme_release(ctx);
                return NULL;
        }
@@ -359,13 +388,28 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account)
        MimeInfo *msgcontent, *sigmultipart, *newinfo;
        gchar *textstr, *micalg;
        FILE *fp;
-       gchar *boundary, *sigcontent;
+       gchar *boundary = NULL;
+       gchar *sigcontent;
        gpgme_ctx_t ctx;
        gpgme_data_t gpgtext, gpgsig;
+       gpgme_error_t err;
        size_t len;
        struct passphrase_cb_info_s info;
        gpgme_sign_result_t result = NULL;
+       gchar *test_msg;
+       
+       fp = my_tmpfile();
+       if (fp == NULL) {
+               privacy_set_error(_("Couldn't create temporary file: %s"), strerror(errno));
+               return FALSE;
+       }
+       procmime_write_mimeinfo(mimeinfo, fp);
+       rewind(fp);
 
+       /* read temporary file into memory */
+       test_msg = file_read_stream_to_str(fp);
+       fclose(fp);
+       
        memset (&info, 0, sizeof info);
 
        /* remove content node from message */
@@ -376,7 +420,14 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account)
        sigmultipart = procmime_mimeinfo_new();
        sigmultipart->type = MIMETYPE_MULTIPART;
        sigmultipart->subtype = g_strdup("signed");
-       boundary = generate_mime_boundary("Signature");
+       
+       do {
+               g_free(boundary);
+               boundary = generate_mime_boundary("Sig");
+       } while (strstr(test_msg, boundary) != NULL);
+       
+       g_free(test_msg);
+
        g_hash_table_insert(sigmultipart->typeparameters, g_strdup("boundary"),
                             g_strdup(boundary));
        g_hash_table_insert(sigmultipart->typeparameters, g_strdup("protocol"),
@@ -386,6 +437,11 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account)
 
        /* write message content to temporary file */
        fp = my_tmpfile();
+       if (fp == NULL) {
+               perror("my_tmpfile");
+               privacy_set_error(_("Couldn't create temporary file: %s"), strerror(errno));
+               return FALSE;
+       }
        procmime_write_mimeinfo(sigmultipart, fp);
        rewind(fp);
 
@@ -403,30 +459,55 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account)
 
        if (!sgpgme_setup_signers(ctx, account)) {
                gpgme_release(ctx);
+               privacy_set_error(_("Couldn't find private key."));
                return FALSE;
        }
 
-       if (!getenv("GPG_AGENT_INFO")) {
-               info.c = ctx;
+       if (getenv("GPG_AGENT_INFO")) {
+               debug_print("GPG_AGENT_INFO environment defined, running without passphrase callback\n");
+       } else {
+               info.c = ctx;
                gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
        }
 
-       if (gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_DETACH) != GPG_ERR_NO_ERROR) {
+       err = gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_DETACH);
+       if (err != GPG_ERR_NO_ERROR) {
+               privacy_set_error(_("Data signing failed, %s"), gpgme_strerror(err));
+               debug_print("gpgme_op_sign error : %x\n", err);
                gpgme_release(ctx);
                return FALSE;
        }
        result = gpgme_op_sign_result(ctx);
        if (result && result->signatures) {
-           if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
-               micalg = g_strdup_printf("PGP-%s", gpgme_hash_algo_name(
-                           result->signatures->hash_algo));
-           } else {
-               micalg = g_strdup(gpgme_hash_algo_name(
-                           result->signatures->hash_algo));
-           }
+               gpgme_new_signature_t sig = result->signatures;
+               if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
+                       micalg = g_strdup_printf("PGP-%s", gpgme_hash_algo_name(
+                               result->signatures->hash_algo));
+               } else {
+                       micalg = g_strdup(gpgme_hash_algo_name(
+                               result->signatures->hash_algo));
+               }
+               while (sig) {
+                       debug_print("valid signature: %s\n", sig->fpr);
+                       sig = sig->next;
+               }
+       } else if (result && result->invalid_signers) {
+               gpgme_invalid_key_t invalid = result->invalid_signers;
+               while (invalid) {
+                       g_warning("invalid signer: %s (%s)", invalid->fpr, 
+                               gpgme_strerror(invalid->reason));
+                       privacy_set_error(_("Data signing failed due to invalid signer: %s"), 
+                               gpgme_strerror(invalid->reason));
+                       invalid = invalid->next;
+               }
+               gpgme_release(ctx);
+               return FALSE;
        } else {
-           /* can't get result (maybe no signing key?) */
-           return FALSE;
+               /* can't get result (maybe no signing key?) */
+               debug_print("gpgme_op_sign_result error\n");
+               privacy_set_error(_("Data signing failed, no results."));
+               gpgme_release(ctx);
+               return FALSE;
        }
 
        gpgme_release(ctx);
@@ -434,6 +515,12 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account)
        gpgme_data_release(gpgtext);
        g_free(textstr);
 
+       if (sigcontent == NULL || len <= 0) {
+               g_warning("gpgme_data_release_and_get_mem failed");
+               privacy_set_error(_("Data signing failed, no contents."));
+               return FALSE;
+       }
+
        /* add signature */
        g_hash_table_insert(sigmultipart->typeparameters, g_strdup("micalg"),
                             micalg);
@@ -441,7 +528,12 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account)
        newinfo = procmime_mimeinfo_new();
        newinfo->type = MIMETYPE_APPLICATION;
        newinfo->subtype = g_strdup("pgp-signature");
+       g_hash_table_insert(newinfo->typeparameters, g_strdup("name"),
+                            g_strdup("signature.asc"));
        newinfo->content = MIMECONTENT_MEM;
+       newinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
+       g_hash_table_insert(newinfo->dispositionparameters, g_strdup("filename"),
+                           g_strdup("signature.asc"));
        newinfo->data.mem = g_malloc(len + 1);
        g_memmove(newinfo->data.mem, sigcontent, len);
        newinfo->data.mem[len] = '\0';
@@ -453,7 +545,7 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account)
 }
 gchar *pgpmime_get_encrypt_data(GSList *recp_names)
 {
-       return sgpgme_get_encrypt_data(recp_names);
+       return sgpgme_get_encrypt_data(recp_names, GPGME_PROTOCOL_OpenPGP);
 }
 
 gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
@@ -468,6 +560,8 @@ gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        gpgme_key_t *kset = NULL;
        gchar **fprs = g_strsplit(encrypt_data, " ", -1);
        gint i = 0;
+       gpgme_error_t err;
+       
        while (fprs[i] && strlen(fprs[i])) {
                i++;
        }
@@ -478,11 +572,11 @@ gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        i = 0;
        while (fprs[i] && strlen(fprs[i])) {
                gpgme_key_t key;
-               gpgme_error_t err;
                err = gpgme_get_key(ctx, fprs[i], &key, 0);
                if (err) {
                        debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
-                       break;
+                       privacy_set_error(_("Couldn't add GPG key %s, %s"), fprs[i], gpgme_strerror(err));
+                       return FALSE;
                }
                debug_print("found %s at %d\n", fprs[i], i);
                kset[i] = key;
@@ -508,6 +602,10 @@ gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
 
        /* write message content to temporary file */
        fp = my_tmpfile();
+       if (fp == NULL) {
+               privacy_set_error(_("Couldn't create temporary file, %s"), strerror(errno));
+               return FALSE;
+       }
        procmime_write_mimeinfo(encmultipart, fp);
        rewind(fp);
 
@@ -522,13 +620,19 @@ gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        gpgme_set_armor(ctx, 1);
        gpgme_data_rewind(gpgtext);
        
-       gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
+       err = gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
 
        gpgme_release(ctx);
        enccontent = gpgme_data_release_and_get_mem(gpgenc, &len);
        gpgme_data_release(gpgtext);
        g_free(textstr);
 
+       if (enccontent == NULL || len <= 0) {
+               g_warning("gpgme_data_release_and_get_mem failed");
+               privacy_set_error(_("Encryption failed, %s"), gpgme_strerror(err));
+               return FALSE;
+       }
+
        /* create encrypted multipart */
        g_node_unlink(msgcontent->node);
        procmime_mimeinfo_free_all(msgcontent);