2012-05-26 20:45:41 +02:00
|
|
|
/* notmuch - Not much of an email program, (just index and search)
|
|
|
|
*
|
|
|
|
* Copyright © 2012 Jameson Rollins
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-06-02 18:26:14 +02:00
|
|
|
* along with this program. If not, see https://www.gnu.org/licenses/ .
|
2012-05-26 20:45:41 +02:00
|
|
|
*
|
|
|
|
* Authors: Jameson Rollins <jrollins@finestructure.net>
|
|
|
|
*/
|
|
|
|
|
2017-10-17 21:09:55 +02:00
|
|
|
#include "crypto.h"
|
|
|
|
#include <strings.h>
|
2019-08-29 17:38:50 +02:00
|
|
|
#include "error_util.h"
|
2017-10-17 21:09:55 +02:00
|
|
|
#define unused(x) x __attribute__ ((unused))
|
|
|
|
|
|
|
|
#define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
|
|
|
|
|
2019-06-13 12:33:13 +02:00
|
|
|
void
|
|
|
|
_notmuch_crypto_cleanup (unused(_notmuch_crypto_t *crypto))
|
2017-07-16 01:01:45 +02:00
|
|
|
{
|
|
|
|
}
|
2017-11-30 09:59:27 +01:00
|
|
|
|
|
|
|
GMimeObject *
|
2017-12-08 07:23:58 +01:00
|
|
|
_notmuch_crypto_decrypt (bool *attempted,
|
|
|
|
notmuch_decryption_policy_t decrypt,
|
2017-12-08 07:23:53 +01:00
|
|
|
notmuch_message_t *message,
|
2020-05-13 00:29:36 +02:00
|
|
|
GMimeObject *part,
|
2017-11-30 09:59:27 +01:00
|
|
|
GMimeDecryptResult **decrypt_result,
|
|
|
|
GError **err)
|
|
|
|
{
|
|
|
|
GMimeObject *ret = NULL;
|
2019-06-13 12:33:13 +02:00
|
|
|
|
2017-12-08 07:23:53 +01:00
|
|
|
if (decrypt == NOTMUCH_DECRYPT_FALSE)
|
|
|
|
return NULL;
|
2017-11-30 09:59:27 +01:00
|
|
|
|
2019-05-06 21:45:51 +02:00
|
|
|
/* try decryption with session key if one is stashed */
|
2017-11-30 09:59:29 +01:00
|
|
|
if (message) {
|
|
|
|
notmuch_message_properties_t *list = NULL;
|
|
|
|
|
|
|
|
for (list = notmuch_message_get_properties (message, "session-key", TRUE);
|
2021-03-13 13:45:34 +01:00
|
|
|
notmuch_message_properties_valid (list); notmuch_message_properties_move_to_next (
|
|
|
|
list)) {
|
2017-11-30 09:59:29 +01:00
|
|
|
if (err && *err) {
|
|
|
|
g_error_free (*err);
|
|
|
|
*err = NULL;
|
|
|
|
}
|
2017-12-08 07:23:58 +01:00
|
|
|
if (attempted)
|
|
|
|
*attempted = true;
|
2020-05-13 00:29:37 +02:00
|
|
|
if (GMIME_IS_MULTIPART_ENCRYPTED (part)) {
|
|
|
|
ret = g_mime_multipart_encrypted_decrypt (GMIME_MULTIPART_ENCRYPTED (part),
|
|
|
|
GMIME_DECRYPT_NONE,
|
|
|
|
notmuch_message_properties_value (list),
|
|
|
|
decrypt_result, err);
|
|
|
|
} else if (GMIME_IS_APPLICATION_PKCS7_MIME (part)) {
|
2021-03-13 13:45:34 +01:00
|
|
|
GMimeApplicationPkcs7Mime *pkcs7 = GMIME_APPLICATION_PKCS7_MIME (part);
|
2020-05-13 00:29:37 +02:00
|
|
|
GMimeSecureMimeType type = g_mime_application_pkcs7_mime_get_smime_type (pkcs7);
|
|
|
|
if (type == GMIME_SECURE_MIME_TYPE_ENVELOPED_DATA) {
|
|
|
|
ret = g_mime_application_pkcs7_mime_decrypt (pkcs7,
|
|
|
|
GMIME_DECRYPT_NONE,
|
2021-03-13 13:45:34 +01:00
|
|
|
notmuch_message_properties_value (
|
|
|
|
list),
|
2020-05-13 00:29:37 +02:00
|
|
|
decrypt_result, err);
|
|
|
|
}
|
|
|
|
}
|
2017-11-30 09:59:29 +01:00
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (list)
|
|
|
|
notmuch_message_properties_destroy (list);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err && *err) {
|
|
|
|
g_error_free (*err);
|
|
|
|
*err = NULL;
|
|
|
|
}
|
2017-12-08 07:23:53 +01:00
|
|
|
|
|
|
|
if (decrypt == NOTMUCH_DECRYPT_AUTO)
|
|
|
|
return ret;
|
|
|
|
|
2017-12-08 07:23:58 +01:00
|
|
|
if (attempted)
|
|
|
|
*attempted = true;
|
2017-12-08 07:24:01 +01:00
|
|
|
GMimeDecryptFlags flags = GMIME_DECRYPT_NONE;
|
2021-03-13 13:45:34 +01:00
|
|
|
|
2017-12-08 07:24:02 +01:00
|
|
|
if (decrypt == NOTMUCH_DECRYPT_TRUE && decrypt_result)
|
2017-12-08 07:24:01 +01:00
|
|
|
flags |= GMIME_DECRYPT_EXPORT_SESSION_KEY;
|
2020-05-13 00:29:37 +02:00
|
|
|
if (GMIME_IS_MULTIPART_ENCRYPTED (part)) {
|
|
|
|
ret = g_mime_multipart_encrypted_decrypt (GMIME_MULTIPART_ENCRYPTED (part), flags, NULL,
|
|
|
|
decrypt_result, err);
|
|
|
|
} else if (GMIME_IS_APPLICATION_PKCS7_MIME (part)) {
|
|
|
|
GMimeApplicationPkcs7Mime *pkcs7 = GMIME_APPLICATION_PKCS7_MIME (part);
|
|
|
|
GMimeSecureMimeType p7type = g_mime_application_pkcs7_mime_get_smime_type (pkcs7);
|
|
|
|
if (p7type == GMIME_SECURE_MIME_TYPE_ENVELOPED_DATA) {
|
|
|
|
ret = g_mime_application_pkcs7_mime_decrypt (pkcs7, flags, NULL,
|
|
|
|
decrypt_result, err);
|
|
|
|
}
|
|
|
|
}
|
2017-11-30 09:59:27 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2019-05-25 20:04:03 +02:00
|
|
|
|
|
|
|
static int
|
|
|
|
_notmuch_message_crypto_destructor (_notmuch_message_crypto_t *msg_crypto)
|
|
|
|
{
|
2019-06-13 12:33:13 +02:00
|
|
|
if (! msg_crypto)
|
2019-05-25 20:04:03 +02:00
|
|
|
return 0;
|
|
|
|
if (msg_crypto->sig_list)
|
|
|
|
g_object_unref (msg_crypto->sig_list);
|
2019-05-27 00:15:55 +02:00
|
|
|
if (msg_crypto->payload_subject)
|
|
|
|
talloc_free (msg_crypto->payload_subject);
|
2019-05-25 20:04:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_notmuch_message_crypto_t *
|
|
|
|
_notmuch_message_crypto_new (void *ctx)
|
|
|
|
{
|
|
|
|
_notmuch_message_crypto_t *ret = talloc_zero (ctx, _notmuch_message_crypto_t);
|
2019-06-13 12:33:13 +02:00
|
|
|
|
2019-05-25 20:04:03 +02:00
|
|
|
talloc_set_destructor (ret, _notmuch_message_crypto_destructor);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
notmuch_status_t
|
2021-03-13 13:45:34 +01:00
|
|
|
_notmuch_message_crypto_potential_sig_list (_notmuch_message_crypto_t *msg_crypto,
|
|
|
|
GMimeSignatureList *sigs)
|
2019-05-25 20:04:03 +02:00
|
|
|
{
|
2019-06-13 12:33:13 +02:00
|
|
|
if (! msg_crypto)
|
2019-05-25 20:04:03 +02:00
|
|
|
return NOTMUCH_STATUS_NULL_POINTER;
|
|
|
|
|
|
|
|
/* Signatures that arrive after a payload part during DFS are not
|
|
|
|
* part of the cryptographic envelope: */
|
|
|
|
if (msg_crypto->payload_encountered)
|
|
|
|
return NOTMUCH_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
if (msg_crypto->sig_list)
|
|
|
|
g_object_unref (msg_crypto->sig_list);
|
|
|
|
|
|
|
|
/* This signature list needs to persist as long as the _n_m_crypto
|
|
|
|
* object survives. Increasing its reference counter prevents
|
|
|
|
* garbage-collection until after _n_m_crypto_destroy is
|
|
|
|
* called. */
|
|
|
|
msg_crypto->sig_list = sigs;
|
|
|
|
if (sigs)
|
|
|
|
g_object_ref (sigs);
|
|
|
|
|
|
|
|
if (msg_crypto->decryption_status == NOTMUCH_MESSAGE_DECRYPTED_FULL)
|
|
|
|
msg_crypto->signature_encrypted = true;
|
|
|
|
|
|
|
|
return NOTMUCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-29 17:38:50 +02:00
|
|
|
bool
|
2021-03-13 13:45:34 +01:00
|
|
|
_notmuch_message_crypto_potential_payload (_notmuch_message_crypto_t *msg_crypto, GMimeObject *part,
|
|
|
|
GMimeObject *parent, int childnum)
|
2019-05-25 20:04:03 +02:00
|
|
|
{
|
2019-05-27 00:15:55 +02:00
|
|
|
const char *protected_headers = NULL;
|
|
|
|
const char *forwarded = NULL;
|
|
|
|
const char *subject = NULL;
|
|
|
|
|
2019-08-29 17:38:50 +02:00
|
|
|
if ((! msg_crypto) || (! part))
|
|
|
|
INTERNAL_ERROR ("_notmuch_message_crypto_potential_payload() got NULL for %s\n",
|
|
|
|
msg_crypto? "part" : "msg_crypto");
|
2019-05-25 20:04:03 +02:00
|
|
|
|
|
|
|
/* only fire on the first payload part encountered */
|
|
|
|
if (msg_crypto->payload_encountered)
|
2019-08-29 17:38:50 +02:00
|
|
|
return false;
|
2019-05-25 20:04:03 +02:00
|
|
|
|
|
|
|
/* the first child of multipart/encrypted that matches the
|
|
|
|
* encryption protocol should be "control information" metadata,
|
|
|
|
* not payload. So we skip it. (see
|
|
|
|
* https://tools.ietf.org/html/rfc1847#page-8) */
|
2021-03-13 13:45:34 +01:00
|
|
|
if (parent && GMIME_IS_MULTIPART_ENCRYPTED (parent) && childnum ==
|
|
|
|
GMIME_MULTIPART_ENCRYPTED_VERSION) {
|
2019-05-25 20:04:03 +02:00
|
|
|
const char *enc_type = g_mime_object_get_content_type_parameter (parent, "protocol");
|
2019-08-29 17:38:49 +02:00
|
|
|
GMimeContentType *ct = g_mime_object_get_content_type (part);
|
2019-05-25 20:04:03 +02:00
|
|
|
if (ct && enc_type) {
|
|
|
|
const char *part_type = g_mime_content_type_get_mime_type (ct);
|
|
|
|
if (part_type && strcmp (part_type, enc_type) == 0)
|
2019-08-29 17:38:50 +02:00
|
|
|
return false;
|
2019-05-25 20:04:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_crypto->payload_encountered = true;
|
|
|
|
|
2019-05-27 00:15:55 +02:00
|
|
|
/* don't bother recording anything if there is no cryptographic
|
|
|
|
* envelope: */
|
|
|
|
if ((msg_crypto->decryption_status != NOTMUCH_MESSAGE_DECRYPTED_FULL) &&
|
|
|
|
(msg_crypto->sig_list == NULL))
|
2019-08-29 17:38:50 +02:00
|
|
|
return false;
|
2019-05-27 00:15:55 +02:00
|
|
|
|
|
|
|
/* Verify that this payload has headers that are intended to be
|
|
|
|
* exported to the larger message: */
|
|
|
|
|
|
|
|
/* Consider a payload that uses Alexei Melinkov's forwarded="no" for
|
|
|
|
* message/global or message/rfc822:
|
|
|
|
* https://tools.ietf.org/html/draft-melnikov-smime-header-signing-05#section-4 */
|
2019-08-29 17:38:49 +02:00
|
|
|
forwarded = g_mime_object_get_content_type_parameter (part, "forwarded");
|
|
|
|
if (GMIME_IS_MESSAGE_PART (part) && forwarded && strcmp (forwarded, "no") == 0) {
|
|
|
|
GMimeMessage *message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
|
2019-05-27 00:15:55 +02:00
|
|
|
subject = g_mime_message_get_subject (message);
|
|
|
|
/* FIXME: handle more than just Subject: at some point */
|
|
|
|
} else {
|
|
|
|
/* Consider "memoryhole"-style protected headers as practiced by Enigmail and K-9 */
|
2019-08-29 17:38:49 +02:00
|
|
|
protected_headers = g_mime_object_get_content_type_parameter (part, "protected-headers");
|
2019-06-13 12:33:13 +02:00
|
|
|
if (protected_headers && strcasecmp ("v1", protected_headers) == 0)
|
2019-08-29 17:38:49 +02:00
|
|
|
subject = g_mime_object_get_header (part, "Subject");
|
2019-05-27 00:15:55 +02:00
|
|
|
/* FIXME: handle more than just Subject: at some point */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (subject) {
|
|
|
|
if (msg_crypto->payload_subject)
|
|
|
|
talloc_free (msg_crypto->payload_subject);
|
|
|
|
msg_crypto->payload_subject = talloc_strdup (msg_crypto, subject);
|
|
|
|
}
|
|
|
|
|
2019-08-29 17:38:50 +02:00
|
|
|
return true;
|
2019-05-25 20:04:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
notmuch_status_t
|
|
|
|
_notmuch_message_crypto_successful_decryption (_notmuch_message_crypto_t *msg_crypto)
|
|
|
|
{
|
2019-06-13 12:33:13 +02:00
|
|
|
if (! msg_crypto)
|
2019-05-25 20:04:03 +02:00
|
|
|
return NOTMUCH_STATUS_NULL_POINTER;
|
|
|
|
|
|
|
|
/* see the rationale for different values of
|
|
|
|
* _notmuch_message_decryption_status_t in util/crypto.h */
|
2019-06-13 12:33:13 +02:00
|
|
|
if (! msg_crypto->payload_encountered)
|
2019-05-25 20:04:03 +02:00
|
|
|
msg_crypto->decryption_status = NOTMUCH_MESSAGE_DECRYPTED_FULL;
|
|
|
|
else if (msg_crypto->decryption_status == NOTMUCH_MESSAGE_DECRYPTED_NONE)
|
|
|
|
msg_crypto->decryption_status = NOTMUCH_MESSAGE_DECRYPTED_PARTIAL;
|
|
|
|
|
|
|
|
return NOTMUCH_STATUS_SUCCESS;
|
|
|
|
}
|