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