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 #define unused(x) x __attribute__ ((unused))
25 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
27 void _notmuch_crypto_cleanup (unused(_notmuch_crypto_t *crypto))
32 _notmuch_crypto_decrypt (bool *attempted,
33 notmuch_decryption_policy_t decrypt,
34 notmuch_message_t *message,
35 GMimeMultipartEncrypted *part,
36 GMimeDecryptResult **decrypt_result,
39 GMimeObject *ret = NULL;
40 if (decrypt == NOTMUCH_DECRYPT_FALSE)
43 /* try decryption with session key if one is stashed */
45 notmuch_message_properties_t *list = NULL;
47 for (list = notmuch_message_get_properties (message, "session-key", TRUE);
48 notmuch_message_properties_valid (list); notmuch_message_properties_move_to_next (list)) {
55 ret = g_mime_multipart_encrypted_decrypt (part,
57 notmuch_message_properties_value (list),
63 notmuch_message_properties_destroy (list);
73 if (decrypt == NOTMUCH_DECRYPT_AUTO)
78 GMimeDecryptFlags flags = GMIME_DECRYPT_NONE;
79 if (decrypt == NOTMUCH_DECRYPT_TRUE && decrypt_result)
80 flags |= GMIME_DECRYPT_EXPORT_SESSION_KEY;
81 ret = g_mime_multipart_encrypted_decrypt(part, flags, NULL,
87 _notmuch_message_crypto_destructor (_notmuch_message_crypto_t *msg_crypto)
91 if (msg_crypto->sig_list)
92 g_object_unref (msg_crypto->sig_list);
96 _notmuch_message_crypto_t *
97 _notmuch_message_crypto_new (void *ctx)
99 _notmuch_message_crypto_t *ret = talloc_zero (ctx, _notmuch_message_crypto_t);
100 talloc_set_destructor (ret, _notmuch_message_crypto_destructor);
105 _notmuch_message_crypto_potential_sig_list (_notmuch_message_crypto_t *msg_crypto, GMimeSignatureList *sigs)
108 return NOTMUCH_STATUS_NULL_POINTER;
110 /* Signatures that arrive after a payload part during DFS are not
111 * part of the cryptographic envelope: */
112 if (msg_crypto->payload_encountered)
113 return NOTMUCH_STATUS_SUCCESS;
115 if (msg_crypto->sig_list)
116 g_object_unref (msg_crypto->sig_list);
118 /* This signature list needs to persist as long as the _n_m_crypto
119 * object survives. Increasing its reference counter prevents
120 * garbage-collection until after _n_m_crypto_destroy is
122 msg_crypto->sig_list = sigs;
126 if (msg_crypto->decryption_status == NOTMUCH_MESSAGE_DECRYPTED_FULL)
127 msg_crypto->signature_encrypted = true;
129 return NOTMUCH_STATUS_SUCCESS;
134 _notmuch_message_crypto_potential_payload (_notmuch_message_crypto_t *msg_crypto, GMimeObject *payload, GMimeObject *parent, int childnum)
136 if (!msg_crypto || !payload)
137 return NOTMUCH_STATUS_NULL_POINTER;
139 /* only fire on the first payload part encountered */
140 if (msg_crypto->payload_encountered)
141 return NOTMUCH_STATUS_SUCCESS;
143 /* the first child of multipart/encrypted that matches the
144 * encryption protocol should be "control information" metadata,
145 * not payload. So we skip it. (see
146 * https://tools.ietf.org/html/rfc1847#page-8) */
147 if (parent && GMIME_IS_MULTIPART_ENCRYPTED (parent) && childnum == GMIME_MULTIPART_ENCRYPTED_VERSION) {
148 const char *enc_type = g_mime_object_get_content_type_parameter (parent, "protocol");
149 GMimeContentType *ct = g_mime_object_get_content_type (payload);
150 if (ct && enc_type) {
151 const char *part_type = g_mime_content_type_get_mime_type (ct);
152 if (part_type && strcmp (part_type, enc_type) == 0)
153 return NOTMUCH_STATUS_SUCCESS;
157 msg_crypto->payload_encountered = true;
159 return NOTMUCH_STATUS_SUCCESS;
164 _notmuch_message_crypto_successful_decryption (_notmuch_message_crypto_t *msg_crypto)
167 return NOTMUCH_STATUS_NULL_POINTER;
169 /* see the rationale for different values of
170 * _notmuch_message_decryption_status_t in util/crypto.h */
171 if (!msg_crypto->payload_encountered)
172 msg_crypto->decryption_status = NOTMUCH_MESSAGE_DECRYPTED_FULL;
173 else if (msg_crypto->decryption_status == NOTMUCH_MESSAGE_DECRYPTED_NONE)
174 msg_crypto->decryption_status = NOTMUCH_MESSAGE_DECRYPTED_PARTIAL;
176 return NOTMUCH_STATUS_SUCCESS;