]> git.notmuchmail.org Git - notmuch/blob - util/crypto.c
util/crypto: add information about the payload part
[notmuch] / util / crypto.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2012 Jameson Rollins
4  *
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.
9  *
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.
14  *
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/ .
17  *
18  * Authors: Jameson Rollins <jrollins@finestructure.net>
19  */
20
21 #include "crypto.h"
22 #include <strings.h>
23 #define unused(x) x __attribute__ ((unused))
24
25 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
26
27 void _notmuch_crypto_cleanup (unused(_notmuch_crypto_t *crypto))
28 {
29 }
30
31 GMimeObject *
32 _notmuch_crypto_decrypt (bool *attempted,
33                          notmuch_decryption_policy_t decrypt,
34                          notmuch_message_t *message,
35                          GMimeMultipartEncrypted *part,
36                          GMimeDecryptResult **decrypt_result,
37                          GError **err)
38 {
39     GMimeObject *ret = NULL;
40     if (decrypt == NOTMUCH_DECRYPT_FALSE)
41         return NULL;
42
43     /* try decryption with session key if one is stashed */
44     if (message) {
45         notmuch_message_properties_t *list = NULL;
46
47         for (list = notmuch_message_get_properties (message, "session-key", TRUE);
48              notmuch_message_properties_valid (list); notmuch_message_properties_move_to_next (list)) {
49             if (err && *err) {
50                 g_error_free (*err);
51                 *err = NULL;
52             }
53             if (attempted)
54                 *attempted = true;
55             ret = g_mime_multipart_encrypted_decrypt (part,
56                                                       GMIME_DECRYPT_NONE,
57                                                       notmuch_message_properties_value (list),
58                                                       decrypt_result, err);
59             if (ret)
60                 break;
61         }
62         if (list)
63             notmuch_message_properties_destroy (list);
64         if (ret)
65             return ret;
66     }
67
68     if (err && *err) {
69         g_error_free (*err);
70         *err = NULL;
71     }
72
73     if (decrypt == NOTMUCH_DECRYPT_AUTO)
74         return ret;
75
76     if (attempted)
77         *attempted = true;
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,
82                                              decrypt_result, err);
83     return ret;
84 }
85
86 static int
87 _notmuch_message_crypto_destructor (_notmuch_message_crypto_t *msg_crypto)
88 {
89     if (!msg_crypto)
90         return 0;
91     if (msg_crypto->sig_list)
92         g_object_unref (msg_crypto->sig_list);
93     if (msg_crypto->payload_subject)
94         talloc_free (msg_crypto->payload_subject);
95     return 0;
96 }
97
98 _notmuch_message_crypto_t *
99 _notmuch_message_crypto_new (void *ctx)
100 {
101     _notmuch_message_crypto_t *ret = talloc_zero (ctx, _notmuch_message_crypto_t);
102     talloc_set_destructor (ret, _notmuch_message_crypto_destructor);
103     return ret;
104 }
105
106 notmuch_status_t
107 _notmuch_message_crypto_potential_sig_list (_notmuch_message_crypto_t *msg_crypto, GMimeSignatureList *sigs)
108 {
109     if (!msg_crypto)
110         return NOTMUCH_STATUS_NULL_POINTER;
111
112     /* Signatures that arrive after a payload part during DFS are not
113      * part of the cryptographic envelope: */
114     if (msg_crypto->payload_encountered)
115         return NOTMUCH_STATUS_SUCCESS;
116
117     if (msg_crypto->sig_list)
118         g_object_unref (msg_crypto->sig_list);
119
120     /* This signature list needs to persist as long as the _n_m_crypto
121      * object survives. Increasing its reference counter prevents
122      * garbage-collection until after _n_m_crypto_destroy is
123      * called. */
124     msg_crypto->sig_list = sigs;
125     if (sigs)
126         g_object_ref (sigs);
127
128     if (msg_crypto->decryption_status == NOTMUCH_MESSAGE_DECRYPTED_FULL)
129         msg_crypto->signature_encrypted = true;
130
131     return NOTMUCH_STATUS_SUCCESS;
132 }
133
134
135 notmuch_status_t
136 _notmuch_message_crypto_potential_payload (_notmuch_message_crypto_t *msg_crypto, GMimeObject *payload, GMimeObject *parent, int childnum)
137 {
138     const char *protected_headers = NULL;
139     const char *forwarded = NULL;
140     const char *subject = NULL;
141
142     if (!msg_crypto || !payload)
143         return NOTMUCH_STATUS_NULL_POINTER;
144
145     /* only fire on the first payload part encountered */
146     if (msg_crypto->payload_encountered)
147         return NOTMUCH_STATUS_SUCCESS;
148
149     /* the first child of multipart/encrypted that matches the
150      * encryption protocol should be "control information" metadata,
151      * not payload.  So we skip it. (see
152      * https://tools.ietf.org/html/rfc1847#page-8) */
153     if (parent && GMIME_IS_MULTIPART_ENCRYPTED (parent) && childnum == GMIME_MULTIPART_ENCRYPTED_VERSION) {
154         const char *enc_type = g_mime_object_get_content_type_parameter (parent, "protocol");
155         GMimeContentType *ct = g_mime_object_get_content_type (payload);
156         if (ct && enc_type) {
157             const char *part_type = g_mime_content_type_get_mime_type (ct);
158             if (part_type && strcmp (part_type, enc_type) == 0)
159                 return NOTMUCH_STATUS_SUCCESS;
160         }
161     }
162
163     msg_crypto->payload_encountered = true;
164
165     /* don't bother recording anything if there is no cryptographic
166      * envelope: */
167     if ((msg_crypto->decryption_status != NOTMUCH_MESSAGE_DECRYPTED_FULL) &&
168         (msg_crypto->sig_list == NULL))
169         return NOTMUCH_STATUS_SUCCESS;
170
171     /* Verify that this payload has headers that are intended to be
172      * exported to the larger message: */
173
174     /* Consider a payload that uses Alexei Melinkov's forwarded="no" for
175      * message/global or message/rfc822:
176      * https://tools.ietf.org/html/draft-melnikov-smime-header-signing-05#section-4 */
177     forwarded = g_mime_object_get_content_type_parameter (payload, "forwarded");
178     if (GMIME_IS_MESSAGE_PART (payload) && forwarded && strcmp (forwarded, "no") == 0) {
179         GMimeMessage *message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (payload));
180         subject = g_mime_message_get_subject (message);
181         /* FIXME: handle more than just Subject: at some point */
182     } else {
183         /* Consider "memoryhole"-style protected headers as practiced by Enigmail and K-9 */
184         protected_headers = g_mime_object_get_content_type_parameter (payload, "protected-headers");
185         if (protected_headers && strcasecmp("v1", protected_headers) == 0)
186             subject = g_mime_object_get_header (payload, "Subject");
187         /* FIXME: handle more than just Subject: at some point */
188     }
189
190     if (subject) {
191         if (msg_crypto->payload_subject)
192             talloc_free (msg_crypto->payload_subject);
193         msg_crypto->payload_subject = talloc_strdup (msg_crypto, subject);
194     }
195
196     return NOTMUCH_STATUS_SUCCESS;
197 }
198
199
200 notmuch_status_t
201 _notmuch_message_crypto_successful_decryption (_notmuch_message_crypto_t *msg_crypto)
202 {
203     if (!msg_crypto)
204         return NOTMUCH_STATUS_NULL_POINTER;
205
206     /* see the rationale for different values of
207      * _notmuch_message_decryption_status_t in util/crypto.h */
208     if (!msg_crypto->payload_encountered)
209         msg_crypto->decryption_status = NOTMUCH_MESSAGE_DECRYPTED_FULL;
210     else if (msg_crypto->decryption_status == NOTMUCH_MESSAGE_DECRYPTED_NONE)
211         msg_crypto->decryption_status = NOTMUCH_MESSAGE_DECRYPTED_PARTIAL;
212
213     return NOTMUCH_STATUS_SUCCESS;
214 }