update copyright year
[claws.git] / src / plugins / pgpmime / pgpmime.c
index 84ba17d10175efdf4179981e17a1930d5251e2ab..1841b9fb53325d4b17a7869295ff3fef5a9b7b45 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2009 the Claws Mail team
+ * Claws Mail -- a GTK based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2024 the Claws Mail 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
  *
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
- * 
  */
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #ifdef USE_GPGME
 #include <plugins/pgpcore/sgpgme.h>
 #include <plugins/pgpcore/prefs_gpg.h>
 #include <plugins/pgpcore/passphrase.h>
+#include <plugins/pgpcore/pgp_utils.h>
 
 #include "prefs_common.h"
+#include "file-utils.h"
 
 typedef struct _PrivacyDataPGP PrivacyDataPGP;
 
@@ -50,36 +52,22 @@ struct _PrivacyDataPGP
        
        gboolean        done_sigtest;
        gboolean        is_signed;
-       gpgme_verify_result_t   sigstatus;
-       gpgme_ctx_t     ctx;
 };
 
 static PrivacySystem pgpmime_system;
 
-static gint pgpmime_check_signature(MimeInfo *mimeinfo);
-
 static PrivacyDataPGP *pgpmime_new_privacydata()
 {
        PrivacyDataPGP *data;
-       gpgme_error_t err;
 
        data = g_new0(PrivacyDataPGP, 1);
        data->data.system = &pgpmime_system;
-       data->done_sigtest = FALSE;
-       data->is_signed = FALSE;
-       data->sigstatus = NULL;
-       if ((err = gpgme_new(&data->ctx)) != GPG_ERR_NO_ERROR) {
-               g_warning(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
-               return NULL;
-       }
        
        return data;
 }
 
-static void pgpmime_free_privacydata(PrivacyData *_data)
+static void pgpmime_free_privacydata(PrivacyData *data)
 {
-       PrivacyDataPGP *data = (PrivacyDataPGP *) _data;
-       gpgme_release(data->ctx);
        g_free(data);
 }
 
@@ -126,27 +114,27 @@ static gboolean pgpmime_is_signed(MimeInfo *mimeinfo)
                data = pgpmime_new_privacydata();
                mimeinfo->privacy = (PrivacyData *) data;
        }
-       
-       data->done_sigtest = TRUE;
-       data->is_signed = TRUE;
+       if (data != NULL) {
+               data->done_sigtest = TRUE;
+               data->is_signed = TRUE;
+       }
 
        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)
+       while (claws_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) {
+       while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
                gchar *buf2;
 
                if (IS_BOUNDARY(buf, boundary, boundary_len))
@@ -158,125 +146,30 @@ static gchar *get_canonical_content(FILE *fp, const gchar *boundary)
        }
        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 *boundary;
-       gchar *textstr;
-       gpgme_data_t sigdata = NULL, textdata = NULL;
-       gpgme_error_t err;
-       cm_return_val_if_fail(mimeinfo != NULL, -1);
-       cm_return_val_if_fail(mimeinfo->privacy != NULL, -1);
-       data = (PrivacyDataPGP *) mimeinfo->privacy;
-       if ((err = gpgme_new(&data->ctx)) != GPG_ERR_NO_ERROR) {
-               debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
-               privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
-               return 0;
-       }
-
-       
-       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 = g_fopen(parent->data.filename, "rb");
-       cm_return_val_if_fail(fp != NULL, SIGNATURE_INVALID);
-       
-       boundary = g_hash_table_lookup(parent->typeparameters, "boundary");
-       if (!boundary) {
-               privacy_set_error(_("Signature boundary not found."));
-               fclose(fp);
-               return 0;
-       }
-       textstr = get_canonical_content(fp, boundary);
-
-       err = gpgme_data_new_from_mem(&textdata, textstr, (size_t)strlen(textstr), 0);
-       if (err) {
-               debug_print ("gpgme_data_new_from_mem failed: %s\n",
-                   gpgme_strerror (err));
-       }
-       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);
-
-       gpgme_data_release(sigdata);
-       gpgme_data_release(textdata);
-       g_free(textstr);
-       fclose(fp);
-       
-       return 0;
-}
-
-static SignatureStatus pgpmime_get_sig_status(MimeInfo *mimeinfo)
-{
-       PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
-       
-       cm_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
-
-       if (data->sigstatus == NULL && 
-           prefs_gpg_get_config()->auto_check_signatures)
-               pgpmime_check_signature(mimeinfo);
-       
-       return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
-}
-
-static gchar *pgpmime_get_sig_info_short(MimeInfo *mimeinfo)
-{
-       PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
-       
-       cm_return_val_if_fail(data != NULL, g_strdup("Error"));
-
-       if (data->sigstatus == NULL && 
-           prefs_gpg_get_config()->auto_check_signatures)
-               pgpmime_check_signature(mimeinfo);
-       
-       return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
+       return g_string_free(textbuffer, FALSE);
 }
 
-static gchar *pgpmime_get_sig_info_full(MimeInfo *mimeinfo)
+static gint pgpmime_check_sig_async(MimeInfo *mimeinfo,
+       GCancellable *cancellable,
+       GAsyncReadyCallback callback,
+       gpointer user_data)
 {
-       PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
-       
-       cm_return_val_if_fail(data != NULL, g_strdup("Error"));
-
-       if (data->sigstatus == NULL && 
-           prefs_gpg_get_config()->auto_check_signatures)
-               pgpmime_check_signature(mimeinfo);
-       
-       return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
+       return cm_check_detached_sig_async(mimeinfo,
+               cancellable,
+               callback,
+               user_data,
+               GPGME_PROTOCOL_OpenPGP,
+               get_canonical_content);
 }
 
 static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
 {
        MimeInfo *tmpinfo;
        const gchar *tmpstr;
-       
+       const gchar *begin_indicator = "-----BEGIN PGP MESSAGE-----";
+       const gchar *end_indicator = "-----END PGP MESSAGE-----";
+       gchar *textdata;
+
        if (mimeinfo->type != MIMETYPE_MULTIPART)
                return FALSE;
        if (g_ascii_strcasecmp(mimeinfo->subtype, "encrypted"))
@@ -299,6 +192,21 @@ static gboolean pgpmime_is_encrypted(MimeInfo *mimeinfo)
        if (g_ascii_strcasecmp(tmpinfo->subtype, "octet-stream"))
                return FALSE;
        
+       textdata = procmime_get_part_as_string(tmpinfo, TRUE);
+       if (!textdata)
+               return FALSE;
+       
+       if (!pgp_locate_armor_header(textdata, begin_indicator)) {
+               g_free(textdata);
+               return FALSE;
+       }
+       if (!pgp_locate_armor_header(textdata, end_indicator)) {
+               g_free(textdata);
+               return FALSE;
+       }
+
+       g_free(textdata);
+
        return TRUE;
 }
 
@@ -315,9 +223,10 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
        gchar *chars;
        size_t len;
        gpgme_error_t err;
+       SignatureData *sig_data = NULL;
 
        if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
-               debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
+               debug_print("Couldn't initialize GPG context, %s\n", gpgme_strerror(err));
                privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
                return NULL;
        }
@@ -329,56 +238,72 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
        cipher = sgpgme_data_from_mimeinfo(encinfo);
        plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
 
+       if (sigstat != NULL && sigstat->signatures != NULL) {
+               sig_data = g_new0(SignatureData, 1);
+               sig_data->status = sgpgme_sigstat_gpgme_to_privacy(ctx, sigstat);
+               sig_data->info_short = sgpgme_sigstat_info_short(ctx, sigstat);
+               sig_data->info_full = sgpgme_sigstat_info_full(ctx, sigstat);
+       }
+
+       gpgme_release(ctx);
        gpgme_data_release(cipher);
        if (plain == NULL) {
                debug_print("plain is null!\n");
-               gpgme_release(ctx);
+               if (sig_data)
+                       privacy_free_signature_data(sig_data);
                return NULL;
        }
 
-       fname = g_strdup_printf("%s%cplaintext.%08x",
+       fname = g_strdup_printf("%s%cplaintext.%08x",
                get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
 
-       if ((dstfp = g_fopen(fname, "wb")) == NULL) {
-               FILE_OP_ERROR(fname, "fopen");
+       if ((dstfp = claws_fopen(fname, "wb")) == NULL) {
+               FILE_OP_ERROR(fname, "claws_fopen");
                privacy_set_error(_("Couldn't open decrypted file %s"), fname);
-               g_free(fname);
-               gpgme_data_release(plain);
-               gpgme_release(ctx);
+               if (sig_data)
+                       privacy_free_signature_data(sig_data);
+               g_free(fname);
+               gpgme_data_release(plain);
                debug_print("can't open!\n");
                return NULL;
-       }
+       }
 
        if (fprintf(dstfp, "MIME-Version: 1.0\n") < 0) {
-               FILE_OP_ERROR(fname, "fprintf");
-               fclose(dstfp);
+               FILE_OP_ERROR(fname, "fprintf");
+               claws_fclose(dstfp);
                privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
-               g_free(fname);
-               gpgme_data_release(plain);
-               gpgme_release(ctx);
+               if (sig_data)
+                       privacy_free_signature_data(sig_data);
+               g_free(fname);
+               gpgme_data_release(plain);
                debug_print("can't open!\n");
                return NULL;
        }
 
        chars = sgpgme_data_release_and_get_mem(plain, &len);
        if (len > 0) {
-               if (fwrite(chars, 1, len, dstfp) < len) {
-                       FILE_OP_ERROR(fname, "fwrite");
-                       fclose(dstfp);
+               if (claws_fwrite(chars, 1, len, dstfp) < len) {
+                       FILE_OP_ERROR(fname, "claws_fwrite");
+                       g_free(chars);
+                       claws_fclose(dstfp);
                        privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
-                       g_free(fname);
-                       gpgme_data_release(plain);
-                       gpgme_release(ctx);
+                       if (sig_data)
+                               privacy_free_signature_data(sig_data);
+                       g_free(fname);
+                       gpgme_data_release(plain);
                        debug_print("can't open!\n");
                        return NULL;
                }
        }
-       if (fclose(dstfp) == EOF) {
-               FILE_OP_ERROR(fname, "fclose");
+       g_free(chars);
+
+       if (claws_safe_fclose(dstfp) == EOF) {
+               FILE_OP_ERROR(fname, "claws_fclose");
                privacy_set_error(_("Couldn't close decrypted file %s"), fname);
-               g_free(fname);
-               gpgme_data_release(plain);
-               gpgme_release(ctx);
+               if (sig_data)
+                       privacy_free_signature_data(sig_data);
+               g_free(fname);
+               gpgme_data_release(plain);
                debug_print("can't open!\n");
                return NULL;
        }
@@ -386,38 +311,39 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
        parseinfo = procmime_scan_file(fname);
        g_free(fname);
        if (parseinfo == NULL) {
-               gpgme_release(ctx);
                privacy_set_error(_("Couldn't parse decrypted file."));
+               if (sig_data)
+                       privacy_free_signature_data(sig_data);
                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);
+               if (sig_data)
+                       privacy_free_signature_data(sig_data);
                return NULL;
        }
 
        g_node_unlink(decinfo->node);
-       procmime_mimeinfo_free_all(parseinfo);
+       procmime_mimeinfo_free_all(&parseinfo);
 
        decinfo->tmp = TRUE;
 
-       if (sigstat != NULL && sigstat->signatures != NULL) {
+       if (sig_data != NULL) {
                if (decinfo->privacy != NULL) {
                        data = (PrivacyDataPGP *) decinfo->privacy;
                } else {
                        data = pgpmime_new_privacydata();
-                       decinfo->privacy = (PrivacyData *) data;        
+                       decinfo->privacy = (PrivacyData *) data;
                }
-               data->done_sigtest = TRUE;
-               data->is_signed = TRUE;
-               data->sigstatus = sigstat;
-               if (data->ctx)
-                       gpgme_release(data->ctx);
-               data->ctx = ctx;
-       } else
-               gpgme_release(ctx);
+
+               if (data != NULL) {
+                       data->done_sigtest = TRUE;
+                       data->is_signed = TRUE;
+                       decinfo->sig_data = sig_data;
+               }
+       }
 
        return decinfo;
 }
@@ -425,7 +351,7 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
 gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *from_addr)
 {
        MimeInfo *msgcontent, *sigmultipart, *newinfo;
-       gchar *textstr, *micalg;
+       gchar *textstr, *micalg = NULL;
        FILE *fp;
        gchar *boundary = NULL;
        gchar *sigcontent;
@@ -439,7 +365,8 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *fr
        
        fp = my_tmpfile();
        if (fp == NULL) {
-               privacy_set_error(_("Couldn't create temporary file: %s"), strerror(errno));
+               perror("my_tmpfile");
+               privacy_set_error(_("Couldn't create temporary file: %s"), g_strerror(errno));
                return FALSE;
        }
        procmime_write_mimeinfo(mimeinfo, fp);
@@ -447,7 +374,7 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *fr
 
        /* read temporary file into memory */
        test_msg = file_read_stream_to_str(fp);
-       fclose(fp);
+       claws_fclose(fp);
        
        memset (&info, 0, sizeof info);
 
@@ -478,7 +405,8 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *fr
        fp = my_tmpfile();
        if (fp == NULL) {
                perror("my_tmpfile");
-               privacy_set_error(_("Couldn't create temporary file: %s"), strerror(errno));
+               privacy_set_error(_("Couldn't create temporary file: %s"), g_strerror(errno));
+               g_free(boundary);
                return FALSE;
        }
        procmime_write_mimeinfo(sigmultipart, fp);
@@ -487,12 +415,13 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *fr
        /* read temporary file into memory */
        textstr = get_canonical_content(fp, boundary);
 
-       fclose(fp);
+       g_free(boundary);
+       claws_fclose(fp);
 
        gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
        gpgme_data_new(&gpgsig);
        if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
-               debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
+               debug_print("Couldn't initialize GPG context, %s\n", gpgme_strerror(err));
                privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
                return FALSE;
        }
@@ -506,7 +435,7 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *fr
        }
 
        prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
-       if (getenv("GPG_AGENT_INFO") && prefs_gpg_get_config()->use_gpg_agent) {
+       if (g_getenv("GPG_AGENT_INFO") && prefs_gpg_get_config()->use_gpg_agent) {
                debug_print("GPG_AGENT_INFO environment defined, running without passphrase callback\n");
        } else {
                info.c = ctx;
@@ -530,8 +459,10 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *fr
        if (result && result->signatures) {
                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));
+                       gchar *down_algo = g_ascii_strdown(gpgme_hash_algo_name(
+                               result->signatures->hash_algo), -1);
+                       micalg = g_strdup_printf("pgp-%s", down_algo);
+                       g_free(down_algo);
                } else {
                        micalg = g_strdup(gpgme_hash_algo_name(
                                result->signatures->hash_algo));
@@ -566,6 +497,9 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *fr
        if (sigcontent == NULL || len <= 0) {
                g_warning("sgpgme_data_release_and_get_mem failed");
                privacy_set_error(_("Data signing failed, no contents."));
+               g_free(micalg);
+               g_free(sigcontent);
+               gpgme_release(ctx);
                return FALSE;
        }
 
@@ -576,15 +510,12 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *fr
        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->description = g_strdup(_("OpenPGP digital signature"));
        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);
+       memmove(newinfo->data.mem, sigcontent, len);
        newinfo->data.mem[len] = '\0';
+       newinfo->tmp = TRUE;
        g_node_append(sigmultipart->node, newinfo->node);
 
        g_free(sigcontent);
@@ -632,11 +563,12 @@ gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
                i++;
        }
        
-       kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
-       memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
+       kset = g_malloc0(sizeof(gpgme_key_t)*(i+1));
        if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
-               debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
+               debug_print("Couldn't initialize GPG context, %s\n", gpgme_strerror(err));
                privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
+               g_free(kset);
+               g_free(fprs);
                return FALSE;
        }
        i = 0;
@@ -646,6 +578,11 @@ gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
                if (err) {
                        debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
                        privacy_set_error(_("Couldn't add GPG key %s, %s"), fprs[i], gpgme_strerror(err));
+                       for (gint x = 0; x < i; x++)
+                               gpgme_key_unref(kset[x]);
+                       g_free(kset);
+                       g_free(fprs);
+                       gpgme_release(ctx);
                        return FALSE;
                }
                debug_print("found %s at %d\n", fprs[i], i);
@@ -673,7 +610,14 @@ 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));
+               perror("my_tmpfile");
+               privacy_set_error(_("Couldn't create temporary file, %s"), g_strerror(errno));
+               for (gint x = 0; x < i; x++)
+                       gpgme_key_unref(kset[x]);
+               g_free(kset);
+               g_free(boundary);
+               g_free(fprs);
+               gpgme_release(ctx);
                return FALSE;
        }
        procmime_write_mimeinfo(encmultipart, fp);
@@ -682,7 +626,8 @@ gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        /* read temporary file into memory */
        textstr = get_canonical_content(fp, boundary);
 
-       fclose(fp);
+       g_free(boundary);
+       claws_fclose(fp);
 
        /* encrypt data */
        gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
@@ -695,17 +640,22 @@ gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        enccontent = sgpgme_data_release_and_get_mem(gpgenc, &len);
        gpgme_data_release(gpgtext);
        g_free(textstr);
+       for (gint x = 0; x < i; x++)
+               gpgme_key_unref(kset[x]);
+       g_free(kset);
 
        if (enccontent == NULL || len <= 0) {
                g_warning("sgpgme_data_release_and_get_mem failed");
                privacy_set_error(_("Encryption failed, %s"), gpgme_strerror(err));
                gpgme_release(ctx);
+               g_free(enccontent);
+               g_free(fprs);
                return FALSE;
        }
 
        /* create encrypted multipart */
        g_node_unlink(msgcontent->node);
-       procmime_mimeinfo_free_all(msgcontent);
+       procmime_mimeinfo_free_all(&msgcontent);
        g_node_append(mimeinfo->node, encmultipart->node);
 
        newinfo = procmime_mimeinfo_new();
@@ -713,6 +663,7 @@ gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        newinfo->subtype = g_strdup("pgp-encrypted");
        newinfo->content = MIMECONTENT_MEM;
        newinfo->data.mem = g_strdup("Version: 1\n");
+       newinfo->tmp = TRUE;
        g_node_append(encmultipart->node, newinfo->node);
 
        newinfo = procmime_mimeinfo_new();
@@ -720,13 +671,16 @@ gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        newinfo->subtype = g_strdup("octet-stream");
        newinfo->content = MIMECONTENT_MEM;
        newinfo->data.mem = g_malloc(len + 1);
-       g_memmove(newinfo->data.mem, enccontent, len);
+       newinfo->tmp = TRUE;
+       memmove(newinfo->data.mem, enccontent, len);
        newinfo->data.mem[len] = '\0';
        g_node_append(encmultipart->node, newinfo->node);
 
        g_free(enccontent);
        gpgme_release(ctx);
 
+       g_free(fprs);
+
        return TRUE;
 }
 
@@ -737,10 +691,7 @@ static PrivacySystem pgpmime_system = {
        pgpmime_free_privacydata,       /* free_privacydata */
 
        pgpmime_is_signed,              /* is_signed(MimeInfo *) */
-       pgpmime_check_signature,        /* check_signature(MimeInfo *) */
-       pgpmime_get_sig_status,         /* get_sig_status(MimeInfo *) */
-       pgpmime_get_sig_info_short,     /* get_sig_info_short(MimeInfo *) */
-       pgpmime_get_sig_info_full,      /* get_sig_info_full(MimeInfo *) */
+       pgpmime_check_sig_async,
 
        pgpmime_is_encrypted,           /* is_encrypted(MimeInfo *) */
        pgpmime_decrypt,                /* decrypt(MimeInfo *) */
@@ -753,6 +704,7 @@ static PrivacySystem pgpmime_system = {
        pgpmime_encrypt,
        pgpmime_get_encrypt_warning,
        pgpmime_inhibit_encrypt_warning,
+       prefs_gpg_auto_check_signatures,
 };
 
 void pgpmime_init()