/*
* Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2009 Colin Leroy <colin@colino.net> and
- * the Claws Mail team
+ * Copyright (C) 1999-2016 Colin Leroy and 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
* GNU General Public License for more details.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * 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 "smime.h"
#include <plugins/pgpcore/sgpgme.h>
#include <plugins/pgpcore/prefs_gpg.h>
+#include <plugins/pgpcore/pgp_utils.h>
#include <plugins/pgpcore/passphrase.h>
#include "alertpanel.h"
#include "prefs_common.h"
#include "procmime.h"
#include "plugin.h"
+#include "file-utils.h"
typedef struct _PrivacyDataPGP PrivacyDataPGP;
static PrivacyDataPGP *smime_new_privacydata()
{
PrivacyDataPGP *data;
+ gpgme_ctx_t ctx;
+
+ if (gpgme_new(&ctx) != GPG_ERR_NO_ERROR) {
+ debug_print("gpgme_new failed\n");
+ return NULL;
+ }
data = g_new0(PrivacyDataPGP, 1);
data->data.system = &smime_system;
data->done_sigtest = FALSE;
data->is_signed = FALSE;
data->sigstatus = NULL;
- gpgme_new(&data->ctx);
+ data->ctx = ctx;
return data;
}
if (tmpstr && !g_ascii_strcasecmp(tmpstr, "signed-data")) {
if (data == NULL) {
data = smime_new_privacydata();
+ if (!data)
+ return FALSE;
mimeinfo->privacy = (PrivacyData *) data;
}
if (data == NULL) {
data = smime_new_privacydata();
+ if (!data)
+ return FALSE;
mimeinfo->privacy = (PrivacyData *) data;
}
if (boundary) {
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 (boundary && IS_BOUNDARY(buf, boundary, boundary_len))
const gchar *tmpstr;
gpgme_data_t sigdata = NULL, textdata = NULL;
gpgme_error_t err;
+ EncodingType oldenc = ENC_BINARY;
+
cm_return_val_if_fail(mimeinfo != NULL, -1);
cm_return_val_if_fail(mimeinfo->privacy != NULL, -1);
+
data = (PrivacyDataPGP *) mimeinfo->privacy;
- gpgme_new(&data->ctx);
- EncodingType oldenc = ENC_BINARY;
-
+
+ if (!data->ctx) {
+ if ((err = gpgme_new(&data->ctx)) != GPG_ERR_NO_ERROR) {
+ debug_print("gpgme_new failed: %s\n",
+ gpgme_strerror(err));
+ return -1;
+ }
+ }
+
debug_print("Checking S/MIME signature\n");
err = gpgme_set_protocol(data->ctx, GPGME_PROTOCOL_CMS);
}
parent = procmime_mimeinfo_parent(mimeinfo);
- fp = g_fopen(parent->data.filename, "rb");
+ fp = claws_fopen(parent->data.filename, "rb");
cm_return_val_if_fail(fp != NULL, SIGNATURE_INVALID);
boundary = g_hash_table_lookup(parent->typeparameters, "boundary");
sgpgme_verify_signature (data->ctx, textdata, NULL, cipher);
gpgme_data_release(textdata);
g_free(textstr);
- gpgme_data_rewind(cipher);
+ cm_gpgme_data_rewind(cipher);
textstr = sgpgme_data_release_and_get_mem(cipher, &len);
- fclose(fp);
+ claws_fclose(fp);
if (textstr && len > 0)
textstr[len-1]='\0';
if (textstr && len) {
gchar *tmp_file = get_tmp_file();
MimeInfo *newinfo = NULL, *decinfo = NULL, *parentinfo = NULL;
- gint childnumber = 0;
-
- str_write_to_file(textstr, tmp_file);
+
+ str_write_to_file(textstr, tmp_file, TRUE);
newinfo = procmime_scan_file(tmp_file);
decinfo = g_node_first_child(newinfo->node) != NULL ?
g_node_first_child(newinfo->node)->data : NULL;
- if (decinfo == NULL)
+ if (decinfo == NULL) {
+ g_free(textstr);
return -1;
+ }
g_node_unlink(decinfo->node);
- procmime_mimeinfo_free_all(newinfo);
+ procmime_mimeinfo_free_all(&newinfo);
decinfo->tmp = TRUE;
parentinfo = procmime_mimeinfo_parent(mimeinfo);
- childnumber = g_node_child_index(parentinfo->node, mimeinfo);
+
if (parentinfo->type == MIMETYPE_MESSAGE &&
!strcmp(parentinfo->subtype, "rfc822")) {
procmime_decode_content(parentinfo);
}
}
g_node_prepend(parentinfo->node, decinfo->node);
+ g_free(textstr);
return 0;
} else {
+ if (textstr)
+ g_free(textstr);
return -1;
}
}
gpgme_data_release(sigdata);
gpgme_data_release(textdata);
g_free(textstr);
- fclose(fp);
+ claws_fclose(fp);
return 0;
}
cm_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
- if (data->sigstatus == NULL &&
- prefs_gpg_get_config()->auto_check_signatures)
- smime_check_signature(mimeinfo);
-
return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
}
cm_return_val_if_fail(data != NULL, g_strdup("Error"));
- if (data->sigstatus == NULL &&
- prefs_gpg_get_config()->auto_check_signatures)
- smime_check_signature(mimeinfo);
-
return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
}
cm_return_val_if_fail(data != NULL, g_strdup("Error"));
- if (data->sigstatus == NULL &&
- prefs_gpg_get_config()->auto_check_signatures)
- smime_check_signature(mimeinfo);
-
return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
}
encinfo = mimeinfo;
cipher = sgpgme_data_from_mimeinfo(encinfo);
- gpgme_data_set_encoding(cipher, GPGME_DATA_ENCODING_BASE64);
+
plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
gpgme_data_release(cipher);
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, "g_fopen");
+ if ((dstfp = claws_fopen(fname, "wb")) == NULL) {
+ FILE_OP_ERROR(fname, "claws_fopen");
g_free(fname);
gpgme_data_release(plain);
gpgme_release(ctx);
if (fprintf(dstfp, "MIME-Version: 1.0\n") < 0) {
FILE_OP_ERROR(fname, "fprintf");
g_free(fname);
- fclose(dstfp);
+ claws_fclose(dstfp);
gpgme_data_release(plain);
gpgme_release(ctx);
debug_print("can't close!\n");
chars = sgpgme_data_release_and_get_mem(plain, &len);
if (len > 0) {
- if (fwrite(chars, 1, len, dstfp) < len) {
- FILE_OP_ERROR(fname, "fwrite");
+ if (claws_fwrite(chars, 1, len, dstfp) < len) {
+ FILE_OP_ERROR(fname, "claws_fwrite");
+ claws_fclose(dstfp);
g_free(fname);
g_free(chars);
gpgme_data_release(plain);
return NULL;
}
}
- if (fclose(dstfp) == EOF) {
- FILE_OP_ERROR(fname, "fclose");
+ if (claws_safe_fclose(dstfp) == EOF) {
+ FILE_OP_ERROR(fname, "claws_fclose");
g_free(fname);
g_free(chars);
gpgme_data_release(plain);
}
g_node_unlink(decinfo->node);
- procmime_mimeinfo_free_all(parseinfo);
+ procmime_mimeinfo_free_all(&parseinfo);
decinfo->tmp = TRUE;
data = (PrivacyDataPGP *) decinfo->privacy;
} else {
data = smime_new_privacydata();
+ if (!data) {
+ gpgme_release(ctx);
+ return NULL;
+ }
decinfo->privacy = (PrivacyData *) data;
}
data->done_sigtest = TRUE;
gboolean smime_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;
/* read temporary file into memory */
test_msg = file_read_stream_to_str(fp);
- fclose(fp);
+ claws_fclose(fp);
memset (&info, 0, sizeof info);
g_free(boundary);
- fclose(fp);
+ claws_fclose(fp);
gpgme_data_new_from_mem(&gpgtext, textstr, textstr?strlen(textstr):0, 0);
gpgme_data_new(&gpgsig);
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));
+ 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));
if (!sigcontent) {
gpgme_release(ctx);
+ g_free(micalg);
return FALSE;
}
real_content = sigcontent+strlen("-----BEGIN SIGNED MESSAGE-----\n");
if (!strstr(real_content, "-----END SIGNED MESSAGE-----")) {
debug_print("missing end\n");
gpgme_release(ctx);
+ g_free(micalg);
return FALSE;
}
*strstr(real_content, "-----END SIGNED MESSAGE-----") = '\0';
g_hash_table_insert(newinfo->dispositionparameters, g_strdup("filename"),
g_strdup("smime.p7s"));
newinfo->data.mem = g_malloc(len + 1);
- g_memmove(newinfo->data.mem, real_content, len);
+ newinfo->tmp = TRUE;
+ memmove(newinfo->data.mem, real_content, len);
newinfo->data.mem[len] = '\0';
newinfo->encoding_type = ENC_BASE64;
g_node_append(sigmultipart->node, newinfo->node);
prefs_gpg_remove_skip_encryption_warning(smime_system.id);
}
-static gchar *fp_read_noconv(FILE *fp)
-{
- GByteArray *array;
- guchar buf[BUFSIZ];
- gint n_read;
- gchar *result = NULL;
-
- if (!fp)
- return NULL;
- array = g_byte_array_new();
-
- while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
- if (n_read < sizeof(buf) && ferror(fp))
- break;
- g_byte_array_append(array, buf, n_read);
- }
-
- if (ferror(fp)) {
- FILE_OP_ERROR("file stream", "fread");
- g_byte_array_free(array, TRUE);
- return NULL;
- }
-
- buf[0] = '\0';
- g_byte_array_append(array, buf, 1);
- result = (gchar *)array->data;
- g_byte_array_free(array, FALSE);
-
- return result;
-}
-
gboolean smime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
{
MimeInfo *msgcontent, *encmultipart;
while (fprs[i] && strlen(fprs[i])) {
i++;
}
-
- gpgme_new(&ctx);
+
+ if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
+ debug_print ("gpgme_new failed: %s\n", gpgme_strerror(err));
+ return FALSE;
+ }
err = gpgme_set_protocol(ctx, GPGME_PROTOCOL_CMS);
if (err) {
debug_print ("gpgme_set_protocol failed: %s\n",
gpgme_strerror (err));
- return FALSE;
+ return FALSE;
}
kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
encmultipart->subtype = g_strdup("x-pkcs7-mime");
g_hash_table_insert(encmultipart->typeparameters, g_strdup("name"),
g_strdup("smime.p7m"));
+ g_hash_table_insert(encmultipart->typeparameters,
+ g_strdup("smime-type"),
+ g_strdup("enveloped-data"));
encmultipart->disposition = DISPOSITIONTYPE_ATTACHMENT;
g_hash_table_insert(encmultipart->dispositionparameters, g_strdup("filename"),
/* write message content to temporary file */
tmpfile = get_tmp_file();
- fp = g_fopen(tmpfile, "wb");
+ fp = claws_fopen(tmpfile, "wb");
if (fp == NULL) {
- perror("get_tmp_file");
+ FILE_OP_ERROR(tmpfile, "create");
+ g_free(kset);
return FALSE;
}
procmime_decode_content(msgcontent);
procmime_write_mime_header(msgcontent, fp);
procmime_write_mimeinfo(msgcontent, fp);
- fclose(fp);
+ claws_safe_fclose(fp);
canonicalize_file_replace(tmpfile);
- fp = g_fopen(tmpfile, "rb");
+ fp = claws_fopen(tmpfile, "rb");
if (fp == NULL) {
- perror("get_tmp_file");
+ FILE_OP_ERROR(tmpfile, "open");
+ g_free(kset);
return FALSE;
}
g_free(tmpfile);
/* read temporary file into memory */
- textstr = fp_read_noconv(fp);
+ textstr = file_read_stream_to_str_no_recode(fp);
- fclose(fp);
+ claws_fclose(fp);
/* encrypt data */
gpgme_data_new_from_mem(&gpgtext, textstr, textstr?strlen(textstr):0, 0);
gpgme_data_new(&gpgenc);
- gpgme_data_rewind(gpgtext);
+ cm_gpgme_data_rewind(gpgtext);
- gpgme_data_set_encoding(gpgenc, GPGME_DATA_ENCODING_BASE64);
gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
gpgme_release(ctx);
+ g_free(kset);
enccontent = sgpgme_data_release_and_get_mem(gpgenc, &len);
if (!enccontent) {
- g_warning("no enccontent\n");
+ g_warning("no enccontent");
return FALSE;
}
tmpfile = get_tmp_file();
- fp = g_fopen(tmpfile, "wb");
+ fp = claws_fopen(tmpfile, "wb");
if (fp) {
- if (fwrite(enccontent, 1, len, fp) < len) {
- FILE_OP_ERROR(tmpfile, "fwrite");
- fclose(fp);
+ if (claws_fwrite(enccontent, 1, len, fp) < len) {
+ FILE_OP_ERROR(tmpfile, "claws_fwrite");
+ claws_fclose(fp);
claws_unlink(tmpfile);
g_free(tmpfile);
+ g_free(enccontent);
return FALSE;
}
- if (fclose(fp) == EOF) {
- FILE_OP_ERROR(tmpfile, "fclose");
+ if (claws_safe_fclose(fp) == EOF) {
+ FILE_OP_ERROR(tmpfile, "claws_fclose");
claws_unlink(tmpfile);
g_free(tmpfile);
+ g_free(enccontent);
return FALSE;
}
} else {
- perror("get_tmp_file");
+ FILE_OP_ERROR(tmpfile, "create");
g_free(tmpfile);
+ g_free(enccontent);
return FALSE;
}
gpgme_data_release(gpgtext);
g_free(textstr);
/* create encrypted multipart */
- procmime_mimeinfo_free_all(msgcontent);
+ procmime_mimeinfo_free_all(&msgcontent);
g_node_append(mimeinfo->node, encmultipart->node);
encmultipart->content = MIMECONTENT_FILE;
encmultipart->data.filename = tmpfile;
+ encmultipart->tmp = TRUE;
procmime_encode_content(encmultipart, ENC_BASE64);
g_free(enccontent);
smime_encrypt,
smime_get_encrypt_warning,
smime_inhibit_encrypt_warning,
+ prefs_gpg_auto_check_signatures,
};
void smime_init()