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