]> git.notmuchmail.org Git - notmuch/blob - util/crypto.c
emacs: Add new option notmuch-search-hide-excluded
[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             if (GMIME_IS_MULTIPART_ENCRYPTED (part)) {
59                 ret = g_mime_multipart_encrypted_decrypt (GMIME_MULTIPART_ENCRYPTED (part),
60                                                           GMIME_DECRYPT_NONE,
61                                                           notmuch_message_properties_value (list),
62                                                           decrypt_result, err);
63             } else if (GMIME_IS_APPLICATION_PKCS7_MIME (part)) {
64                 GMimeApplicationPkcs7Mime *pkcs7 = GMIME_APPLICATION_PKCS7_MIME (part);
65                 GMimeSecureMimeType type = g_mime_application_pkcs7_mime_get_smime_type (pkcs7);
66                 if (type == GMIME_SECURE_MIME_TYPE_ENVELOPED_DATA) {
67                     ret = g_mime_application_pkcs7_mime_decrypt (pkcs7,
68                                                                  GMIME_DECRYPT_NONE,
69                                                                  notmuch_message_properties_value (list),
70                                                                  decrypt_result, err);
71                 }
72             }
73             if (ret)
74                 break;
75         }
76         if (list)
77             notmuch_message_properties_destroy (list);
78         if (ret)
79             return ret;
80     }
81
82     if (err && *err) {
83         g_error_free (*err);
84         *err = NULL;
85     }
86
87     if (decrypt == NOTMUCH_DECRYPT_AUTO)
88         return ret;
89
90     if (attempted)
91         *attempted = true;
92     GMimeDecryptFlags flags = GMIME_DECRYPT_NONE;
93     if (decrypt == NOTMUCH_DECRYPT_TRUE && decrypt_result)
94         flags |= GMIME_DECRYPT_EXPORT_SESSION_KEY;
95     if (GMIME_IS_MULTIPART_ENCRYPTED (part)) {
96         ret = g_mime_multipart_encrypted_decrypt (GMIME_MULTIPART_ENCRYPTED (part), flags, NULL,
97                                                   decrypt_result, err);
98     } else if (GMIME_IS_APPLICATION_PKCS7_MIME (part)) {
99         GMimeApplicationPkcs7Mime *pkcs7 = GMIME_APPLICATION_PKCS7_MIME (part);
100         GMimeSecureMimeType p7type = g_mime_application_pkcs7_mime_get_smime_type (pkcs7);
101         if (p7type == GMIME_SECURE_MIME_TYPE_ENVELOPED_DATA) {
102             ret = g_mime_application_pkcs7_mime_decrypt (pkcs7, flags, NULL,
103                                                          decrypt_result, err);
104         }
105     }
106     return ret;
107 }
108
109 static int
110 _notmuch_message_crypto_destructor (_notmuch_message_crypto_t *msg_crypto)
111 {
112     if (! msg_crypto)
113         return 0;
114     if (msg_crypto->sig_list)
115         g_object_unref (msg_crypto->sig_list);
116     if (msg_crypto->payload_subject)
117         talloc_free (msg_crypto->payload_subject);
118     return 0;
119 }
120
121 _notmuch_message_crypto_t *
122 _notmuch_message_crypto_new (void *ctx)
123 {
124     _notmuch_message_crypto_t *ret = talloc_zero (ctx, _notmuch_message_crypto_t);
125
126     talloc_set_destructor (ret, _notmuch_message_crypto_destructor);
127     return ret;
128 }
129
130 notmuch_status_t
131 _notmuch_message_crypto_potential_sig_list (_notmuch_message_crypto_t *msg_crypto, GMimeSignatureList *sigs)
132 {
133     if (! msg_crypto)
134         return NOTMUCH_STATUS_NULL_POINTER;
135
136     /* Signatures that arrive after a payload part during DFS are not
137      * part of the cryptographic envelope: */
138     if (msg_crypto->payload_encountered)
139         return NOTMUCH_STATUS_SUCCESS;
140
141     if (msg_crypto->sig_list)
142         g_object_unref (msg_crypto->sig_list);
143
144     /* This signature list needs to persist as long as the _n_m_crypto
145      * object survives. Increasing its reference counter prevents
146      * garbage-collection until after _n_m_crypto_destroy is
147      * called. */
148     msg_crypto->sig_list = sigs;
149     if (sigs)
150         g_object_ref (sigs);
151
152     if (msg_crypto->decryption_status == NOTMUCH_MESSAGE_DECRYPTED_FULL)
153         msg_crypto->signature_encrypted = true;
154
155     return NOTMUCH_STATUS_SUCCESS;
156 }
157
158
159 bool
160 _notmuch_message_crypto_potential_payload (_notmuch_message_crypto_t *msg_crypto, GMimeObject *part, GMimeObject *parent, int childnum)
161 {
162     const char *protected_headers = NULL;
163     const char *forwarded = NULL;
164     const char *subject = NULL;
165
166     if ((! msg_crypto) || (! part))
167         INTERNAL_ERROR ("_notmuch_message_crypto_potential_payload() got NULL for %s\n",
168                         msg_crypto? "part" : "msg_crypto");
169
170     /* only fire on the first payload part encountered */
171     if (msg_crypto->payload_encountered)
172         return false;
173
174     /* the first child of multipart/encrypted that matches the
175      * encryption protocol should be "control information" metadata,
176      * not payload.  So we skip it. (see
177      * https://tools.ietf.org/html/rfc1847#page-8) */
178     if (parent && GMIME_IS_MULTIPART_ENCRYPTED (parent) && childnum == GMIME_MULTIPART_ENCRYPTED_VERSION) {
179         const char *enc_type = g_mime_object_get_content_type_parameter (parent, "protocol");
180         GMimeContentType *ct = g_mime_object_get_content_type (part);
181         if (ct && enc_type) {
182             const char *part_type = g_mime_content_type_get_mime_type (ct);
183             if (part_type && strcmp (part_type, enc_type) == 0)
184                 return false;
185         }
186     }
187
188     msg_crypto->payload_encountered = true;
189
190     /* don't bother recording anything if there is no cryptographic
191      * envelope: */
192     if ((msg_crypto->decryption_status != NOTMUCH_MESSAGE_DECRYPTED_FULL) &&
193         (msg_crypto->sig_list == NULL))
194         return false;
195
196     /* Verify that this payload has headers that are intended to be
197      * exported to the larger message: */
198
199     /* Consider a payload that uses Alexei Melinkov's forwarded="no" for
200      * message/global or message/rfc822:
201      * https://tools.ietf.org/html/draft-melnikov-smime-header-signing-05#section-4 */
202     forwarded = g_mime_object_get_content_type_parameter (part, "forwarded");
203     if (GMIME_IS_MESSAGE_PART (part) && forwarded && strcmp (forwarded, "no") == 0) {
204         GMimeMessage *message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
205         subject = g_mime_message_get_subject (message);
206         /* FIXME: handle more than just Subject: at some point */
207     } else {
208         /* Consider "memoryhole"-style protected headers as practiced by Enigmail and K-9 */
209         protected_headers = g_mime_object_get_content_type_parameter (part, "protected-headers");
210         if (protected_headers && strcasecmp ("v1", protected_headers) == 0)
211             subject = g_mime_object_get_header (part, "Subject");
212         /* FIXME: handle more than just Subject: at some point */
213     }
214
215     if (subject) {
216         if (msg_crypto->payload_subject)
217             talloc_free (msg_crypto->payload_subject);
218         msg_crypto->payload_subject = talloc_strdup (msg_crypto, subject);
219     }
220
221     return true;
222 }
223
224
225 notmuch_status_t
226 _notmuch_message_crypto_successful_decryption (_notmuch_message_crypto_t *msg_crypto)
227 {
228     if (! msg_crypto)
229         return NOTMUCH_STATUS_NULL_POINTER;
230
231     /* see the rationale for different values of
232      * _notmuch_message_decryption_status_t in util/crypto.h */
233     if (! msg_crypto->payload_encountered)
234         msg_crypto->decryption_status = NOTMUCH_MESSAGE_DECRYPTED_FULL;
235     else if (msg_crypto->decryption_status == NOTMUCH_MESSAGE_DECRYPTED_NONE)
236         msg_crypto->decryption_status = NOTMUCH_MESSAGE_DECRYPTED_PARTIAL;
237
238     return NOTMUCH_STATUS_SUCCESS;
239 }