]> git.notmuchmail.org Git - notmuch/blob - util/crypto.c
gmime-cleanup: remove obsolete gpg_path configuration option and crypto contexts
[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                          g_mime_3_unused(GMimeCryptoContext* crypto_ctx),
36                          GMimeMultipartEncrypted *part,
37                          GMimeDecryptResult **decrypt_result,
38                          GError **err)
39 {
40     GMimeObject *ret = NULL;
41     if (decrypt == NOTMUCH_DECRYPT_FALSE)
42         return NULL;
43
44     /* the versions of notmuch that can support session key decryption */
45 #if HAVE_GMIME_SESSION_KEYS
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 #endif
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 #if (GMIME_MAJOR_VERSION < 3)
82 #if HAVE_GMIME_SESSION_KEYS
83     gboolean oldgetsk = g_mime_crypto_context_get_retrieve_session_key (crypto_ctx);
84     gboolean newgetsk = (decrypt == NOTMUCH_DECRYPT_TRUE && decrypt_result);
85     if (newgetsk != oldgetsk)
86         /* This could return an error, but we can't do anything about it, so ignore it */
87         g_mime_crypto_context_set_retrieve_session_key (crypto_ctx, newgetsk, NULL);
88 #endif
89     ret = g_mime_multipart_encrypted_decrypt(part, crypto_ctx,
90                                              decrypt_result, err);
91 #if HAVE_GMIME_SESSION_KEYS
92     if (newgetsk != oldgetsk)
93         g_mime_crypto_context_set_retrieve_session_key (crypto_ctx, oldgetsk, NULL);
94 #endif
95 #else
96     GMimeDecryptFlags flags = GMIME_DECRYPT_NONE;
97     if (decrypt == NOTMUCH_DECRYPT_TRUE && decrypt_result)
98         flags |= GMIME_DECRYPT_EXPORT_SESSION_KEY;
99     ret = g_mime_multipart_encrypted_decrypt(part, flags, NULL,
100                                              decrypt_result, err);
101 #endif
102     return ret;
103 }