Blame src/em-format/e-mail-parser-multipart-encrypted.c

Packit 15f964
/*
Packit 15f964
 * e-mail-parser-multipart-encrypted.c
Packit 15f964
 *
Packit 15f964
 * This program is free software; you can redistribute it and/or modify it
Packit 15f964
 * under the terms of the GNU Lesser General Public License as published by
Packit 15f964
 * the Free Software Foundation.
Packit 15f964
 *
Packit 15f964
 * This program is distributed in the hope that it will be useful, but
Packit 15f964
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 15f964
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit 15f964
 * for more details.
Packit 15f964
 *
Packit 15f964
 * You should have received a copy of the GNU Lesser General Public License
Packit 15f964
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit 15f964
 *
Packit 15f964
 */
Packit 15f964
Packit 15f964
#include "evolution-config.h"
Packit 15f964
Packit 15f964
#include <glib/gi18n-lib.h>
Packit 15f964
Packit 15f964
#include <libedataserver/libedataserver.h>
Packit 15f964
Packit Service 4a41b9
#include "e-mail-formatter-utils.h"
Packit 15f964
#include "e-mail-parser-extension.h"
Packit 15f964
#include "e-mail-part-utils.h"
Packit 15f964
Packit 15f964
typedef EMailParserExtension EMailParserMultipartEncrypted;
Packit 15f964
typedef EMailParserExtensionClass EMailParserMultipartEncryptedClass;
Packit 15f964
Packit 15f964
GType e_mail_parser_multipart_encrypted_get_type (void);
Packit 15f964
Packit 15f964
G_DEFINE_TYPE (
Packit 15f964
	EMailParserMultipartEncrypted,
Packit 15f964
	e_mail_parser_multipart_encrypted,
Packit 15f964
	E_TYPE_MAIL_PARSER_EXTENSION)
Packit 15f964
Packit 15f964
static const gchar *parser_mime_types[] = {
Packit 15f964
	"multipart/encrypted",
Packit 15f964
	NULL
Packit 15f964
};
Packit 15f964
Packit 15f964
static gboolean
Packit 15f964
empe_mp_encrypted_parse (EMailParserExtension *extension,
Packit 15f964
                         EMailParser *parser,
Packit 15f964
                         CamelMimePart *part,
Packit 15f964
                         GString *part_id,
Packit 15f964
                         GCancellable *cancellable,
Packit 15f964
                         GQueue *out_mail_parts)
Packit 15f964
{
Packit 15f964
	CamelCipherContext *context;
Packit 15f964
	const gchar *protocol;
Packit 15f964
	CamelMimePart *opart;
Packit 15f964
	CamelCipherValidity *valid;
Packit 15f964
	CamelMultipartEncrypted *mpe;
Packit 15f964
	GQueue work_queue = G_QUEUE_INIT;
Packit 15f964
	GList *head, *link;
Packit 15f964
	GError *local_error = NULL;
Packit 15f964
	gint len;
Packit 15f964
Packit 15f964
	mpe = (CamelMultipartEncrypted *) camel_medium_get_content ((CamelMedium *) part);
Packit 15f964
	if (!CAMEL_IS_MULTIPART_ENCRYPTED (mpe)) {
Packit 15f964
		e_mail_parser_error (
Packit 15f964
			parser, out_mail_parts,
Packit 15f964
			_("Could not parse MIME message. "
Packit 15f964
			"Displaying as source."));
Packit 15f964
		e_mail_parser_parse_part_as (
Packit 15f964
			parser, part, part_id,
Packit 15f964
			"application/vnd.evolution/source",
Packit 15f964
			cancellable, out_mail_parts);
Packit 15f964
Packit 15f964
		return TRUE;
Packit 15f964
	}
Packit 15f964
Packit 15f964
	/* Currently we only handle RFC2015-style PGP encryption. */
Packit 15f964
	protocol = camel_content_type_param (camel_data_wrapper_get_mime_type_field (CAMEL_DATA_WRAPPER (mpe)), "protocol");
Packit 15f964
	if (!protocol || g_ascii_strcasecmp (protocol, "application/pgp-encrypted") != 0) {
Packit 15f964
		e_mail_parser_error (
Packit 15f964
			parser, out_mail_parts,
Packit 15f964
			_("Unsupported encryption type for multipart/encrypted"));
Packit 15f964
		e_mail_parser_parse_part_as (
Packit 15f964
			parser, part, part_id, "multipart/mixed",
Packit 15f964
			cancellable, out_mail_parts);
Packit 15f964
Packit 15f964
		return TRUE;
Packit 15f964
	}
Packit 15f964
Packit 15f964
	context = camel_gpg_context_new (e_mail_parser_get_session (parser));
Packit 15f964
Packit 15f964
	opart = camel_mime_part_new ();
Packit 15f964
	valid = camel_cipher_context_decrypt_sync (
Packit 15f964
		context, part, opart, cancellable, &local_error);
Packit 15f964
Packit 15f964
	e_mail_part_preserve_charset_in_content_type (part, opart);
Packit 15f964
Packit 15f964
	if (local_error != NULL) {
Packit 15f964
		e_mail_parser_error (
Packit 15f964
			parser, out_mail_parts,
Packit 15f964
			_("Could not parse PGP/MIME message: %s"),
Packit 15f964
			local_error->message);
Packit 15f964
		e_mail_parser_parse_part_as (
Packit 15f964
			parser, part, part_id, "multipart/mixed",
Packit 15f964
			cancellable, out_mail_parts);
Packit 15f964
Packit 15f964
		g_object_unref (opart);
Packit 15f964
		g_object_unref (context);
Packit 15f964
		g_error_free (local_error);
Packit 15f964
Packit 15f964
		return TRUE;
Packit 15f964
	}
Packit 15f964
Packit 15f964
	len = part_id->len;
Packit 15f964
	g_string_append (part_id, ".encrypted");
Packit 15f964
Packit 15f964
	g_warn_if_fail (e_mail_parser_parse_part (
Packit 15f964
		parser, opart, part_id, cancellable, &work_queue));
Packit 15f964
Packit 15f964
	g_string_truncate (part_id, len);
Packit 15f964
Packit 15f964
	head = g_queue_peek_head_link (&work_queue);
Packit 15f964
Packit 15f964
	/* Update validity of all encrypted sub-parts */
Packit 15f964
	for (link = head; link != NULL; link = g_list_next (link)) {
Packit 15f964
		EMailPart *mail_part = link->data;
Packit 15f964
Packit 15f964
		e_mail_part_update_validity (
Packit 15f964
			mail_part, valid,
Packit 15f964
			E_MAIL_PART_VALIDITY_ENCRYPTED |
Packit 15f964
			E_MAIL_PART_VALIDITY_PGP);
Packit Service 4a41b9
Packit Service 4a41b9
		/* Do not traverse sub-messages */
Packit Service 4a41b9
		if (g_str_has_suffix (e_mail_part_get_id (mail_part), ".rfc822"))
Packit Service 4a41b9
			link = e_mail_formatter_find_rfc822_end_iter (link);
Packit 15f964
	}
Packit 15f964
Packit 15f964
	e_queue_transfer (&work_queue, out_mail_parts);
Packit 15f964
Packit 15f964
	/* Add a widget with details about the encryption, but only when
Packit 15f964
	 * the decrypted part isn't itself secured, in that case it has
Packit 15f964
	 * created the button itself. */
Packit 15f964
	if (!e_mail_part_is_secured (opart)) {
Packit 15f964
		EMailPart *mail_part;
Packit 15f964
Packit 15f964
		g_string_append (part_id, ".encrypted.button");
Packit 15f964
Packit 15f964
		e_mail_parser_parse_part_as (
Packit 15f964
			parser, part, part_id,
Packit 15f964
			"application/vnd.evolution.secure-button",
Packit 15f964
			cancellable, &work_queue);
Packit 15f964
Packit 15f964
		mail_part = g_queue_peek_head (&work_queue);
Packit 15f964
Packit 15f964
		if (mail_part != NULL)
Packit 15f964
			e_mail_part_update_validity (
Packit 15f964
				mail_part, valid,
Packit 15f964
				E_MAIL_PART_VALIDITY_ENCRYPTED |
Packit 15f964
				E_MAIL_PART_VALIDITY_PGP);
Packit 15f964
Packit 15f964
		e_queue_transfer (&work_queue, out_mail_parts);
Packit 15f964
Packit 15f964
		g_string_truncate (part_id, len);
Packit 15f964
	}
Packit 15f964
Packit 15f964
	camel_cipher_validity_free (valid);
Packit 15f964
Packit 15f964
	/* TODO: Make sure when we finalize this part, it is zero'd out */
Packit 15f964
	g_object_unref (opart);
Packit 15f964
	g_object_unref (context);
Packit 15f964
Packit 15f964
	return TRUE;
Packit 15f964
}
Packit 15f964
Packit 15f964
static void
Packit 15f964
e_mail_parser_multipart_encrypted_class_init (EMailParserExtensionClass *class)
Packit 15f964
{
Packit 15f964
	class->mime_types = parser_mime_types;
Packit 15f964
	class->priority = G_PRIORITY_LOW;
Packit 15f964
	class->parse = empe_mp_encrypted_parse;
Packit 15f964
}
Packit 15f964
Packit 15f964
static void
Packit 15f964
e_mail_parser_multipart_encrypted_init (EMailParserExtension *extension)
Packit 15f964
{
Packit 15f964
}