1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2012 Jameson Rollins
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see https://www.gnu.org/licenses/ .
18 * Authors: Jameson Rollins <jrollins@finestructure.net>
23 #include "error_util.h"
24 #define unused(x) x __attribute__ ((unused))
26 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
29 _notmuch_crypto_cleanup (unused(_notmuch_crypto_t *crypto))
34 _notmuch_crypto_decrypt (bool *attempted,
35 notmuch_decryption_policy_t decrypt,
36 notmuch_message_t *message,
38 GMimeDecryptResult **decrypt_result,
41 GMimeObject *ret = NULL;
43 if (decrypt == NOTMUCH_DECRYPT_FALSE)
46 /* try decryption with session key if one is stashed */
48 notmuch_message_properties_t *list = NULL;
50 for (list = notmuch_message_get_properties (message, "session-key", TRUE);
51 notmuch_message_properties_valid (list); notmuch_message_properties_move_to_next (
59 if (GMIME_IS_MULTIPART_ENCRYPTED (part)) {
60 ret = g_mime_multipart_encrypted_decrypt (GMIME_MULTIPART_ENCRYPTED (part),
62 notmuch_message_properties_value (list),
64 } else if (GMIME_IS_APPLICATION_PKCS7_MIME (part)) {
65 GMimeApplicationPkcs7Mime *pkcs7 = GMIME_APPLICATION_PKCS7_MIME (part);
66 GMimeSecureMimeType type = g_mime_application_pkcs7_mime_get_smime_type (pkcs7);
67 if (type == GMIME_SECURE_MIME_TYPE_ENVELOPED_DATA) {
68 ret = g_mime_application_pkcs7_mime_decrypt (pkcs7,
70 notmuch_message_properties_value (
79 notmuch_message_properties_destroy (list);
89 if (decrypt == NOTMUCH_DECRYPT_AUTO)
94 GMimeDecryptFlags flags = GMIME_DECRYPT_NONE;
96 if (decrypt == NOTMUCH_DECRYPT_TRUE && decrypt_result)
97 flags |= GMIME_DECRYPT_EXPORT_SESSION_KEY;
98 if (GMIME_IS_MULTIPART_ENCRYPTED (part)) {
99 ret = g_mime_multipart_encrypted_decrypt (GMIME_MULTIPART_ENCRYPTED (part), flags, NULL,
100 decrypt_result, err);
101 } else if (GMIME_IS_APPLICATION_PKCS7_MIME (part)) {
102 GMimeApplicationPkcs7Mime *pkcs7 = GMIME_APPLICATION_PKCS7_MIME (part);
103 GMimeSecureMimeType p7type = g_mime_application_pkcs7_mime_get_smime_type (pkcs7);
104 if (p7type == GMIME_SECURE_MIME_TYPE_ENVELOPED_DATA) {
105 ret = g_mime_application_pkcs7_mime_decrypt (pkcs7, flags, NULL,
106 decrypt_result, err);
113 _notmuch_message_crypto_destructor (_notmuch_message_crypto_t *msg_crypto)
117 if (msg_crypto->sig_list)
118 g_object_unref (msg_crypto->sig_list);
119 if (msg_crypto->payload_subject)
120 talloc_free (msg_crypto->payload_subject);
124 _notmuch_message_crypto_t *
125 _notmuch_message_crypto_new (void *ctx)
127 _notmuch_message_crypto_t *ret = talloc_zero (ctx, _notmuch_message_crypto_t);
129 talloc_set_destructor (ret, _notmuch_message_crypto_destructor);
134 _notmuch_message_crypto_potential_sig_list (_notmuch_message_crypto_t *msg_crypto,
135 GMimeSignatureList *sigs)
138 return NOTMUCH_STATUS_NULL_POINTER;
140 /* Signatures that arrive after a payload part during DFS are not
141 * part of the cryptographic envelope: */
142 if (msg_crypto->payload_encountered)
143 return NOTMUCH_STATUS_SUCCESS;
145 if (msg_crypto->sig_list)
146 g_object_unref (msg_crypto->sig_list);
148 /* This signature list needs to persist as long as the _n_m_crypto
149 * object survives. Increasing its reference counter prevents
150 * garbage-collection until after _n_m_crypto_destroy is
152 msg_crypto->sig_list = sigs;
156 if (msg_crypto->decryption_status == NOTMUCH_MESSAGE_DECRYPTED_FULL)
157 msg_crypto->signature_encrypted = true;
159 return NOTMUCH_STATUS_SUCCESS;
164 _notmuch_message_crypto_potential_payload (_notmuch_message_crypto_t *msg_crypto, GMimeObject *part,
165 GMimeObject *parent, int childnum)
167 const char *protected_headers = NULL;
168 const char *forwarded = NULL;
169 const char *subject = NULL;
171 if ((! msg_crypto) || (! part))
172 INTERNAL_ERROR ("_notmuch_message_crypto_potential_payload() got NULL for %s\n",
173 msg_crypto? "part" : "msg_crypto");
175 /* only fire on the first payload part encountered */
176 if (msg_crypto->payload_encountered)
179 /* the first child of multipart/encrypted that matches the
180 * encryption protocol should be "control information" metadata,
181 * not payload. So we skip it. (see
182 * https://tools.ietf.org/html/rfc1847#page-8) */
183 if (parent && GMIME_IS_MULTIPART_ENCRYPTED (parent) && childnum ==
184 GMIME_MULTIPART_ENCRYPTED_VERSION) {
185 const char *enc_type = g_mime_object_get_content_type_parameter (parent, "protocol");
186 GMimeContentType *ct = g_mime_object_get_content_type (part);
187 if (ct && enc_type) {
188 const char *part_type = g_mime_content_type_get_mime_type (ct);
189 if (part_type && strcmp (part_type, enc_type) == 0)
194 msg_crypto->payload_encountered = true;
196 /* don't bother recording anything if there is no cryptographic
198 if ((msg_crypto->decryption_status != NOTMUCH_MESSAGE_DECRYPTED_FULL) &&
199 (msg_crypto->sig_list == NULL))
202 /* Verify that this payload has headers that are intended to be
203 * exported to the larger message: */
205 /* Consider a payload that uses Alexei Melinkov's forwarded="no" for
206 * message/global or message/rfc822:
207 * https://tools.ietf.org/html/draft-melnikov-smime-header-signing-05#section-4 */
208 forwarded = g_mime_object_get_content_type_parameter (part, "forwarded");
209 if (GMIME_IS_MESSAGE_PART (part) && forwarded && strcmp (forwarded, "no") == 0) {
210 GMimeMessage *message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
211 subject = g_mime_message_get_subject (message);
212 /* FIXME: handle more than just Subject: at some point */
214 /* Consider "memoryhole"-style protected headers as practiced by Enigmail and K-9 */
215 protected_headers = g_mime_object_get_content_type_parameter (part, "protected-headers");
216 if (protected_headers && strcasecmp ("v1", protected_headers) == 0)
217 subject = g_mime_object_get_header (part, "Subject");
218 /* FIXME: handle more than just Subject: at some point */
222 if (msg_crypto->payload_subject)
223 talloc_free (msg_crypto->payload_subject);
224 msg_crypto->payload_subject = talloc_strdup (msg_crypto, subject);
232 _notmuch_message_crypto_successful_decryption (_notmuch_message_crypto_t *msg_crypto)
235 return NOTMUCH_STATUS_NULL_POINTER;
237 /* see the rationale for different values of
238 * _notmuch_message_decryption_status_t in util/crypto.h */
239 if (! msg_crypto->payload_encountered)
240 msg_crypto->decryption_status = NOTMUCH_MESSAGE_DECRYPTED_FULL;
241 else if (msg_crypto->decryption_status == NOTMUCH_MESSAGE_DECRYPTED_NONE)
242 msg_crypto->decryption_status = NOTMUCH_MESSAGE_DECRYPTED_PARTIAL;
244 return NOTMUCH_STATUS_SUCCESS;