]> git.notmuchmail.org Git - notmuch/blob - util/crypto.c
gmime-cleanup: always support session keys
[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 (message) {
46         notmuch_message_properties_t *list = NULL;
47
48         for (list = notmuch_message_get_properties (message, "session-key", TRUE);
49              notmuch_message_properties_valid (list); notmuch_message_properties_move_to_next (list)) {
50             if (err && *err) {
51                 g_error_free (*err);
52                 *err = NULL;
53             }
54             if (attempted)
55                 *attempted = true;
56             ret = g_mime_multipart_encrypted_decrypt (part,
57                                                       GMIME_DECRYPT_NONE,
58                                                       notmuch_message_properties_value (list),
59                                                       decrypt_result, err);
60             if (ret)
61                 break;
62         }
63         if (list)
64             notmuch_message_properties_destroy (list);
65         if (ret)
66             return ret;
67     }
68
69     if (err && *err) {
70         g_error_free (*err);
71         *err = NULL;
72     }
73
74     if (decrypt == NOTMUCH_DECRYPT_AUTO)
75         return ret;
76
77     if (attempted)
78         *attempted = true;
79     GMimeDecryptFlags flags = GMIME_DECRYPT_NONE;
80     if (decrypt == NOTMUCH_DECRYPT_TRUE && decrypt_result)
81         flags |= GMIME_DECRYPT_EXPORT_SESSION_KEY;
82     ret = g_mime_multipart_encrypted_decrypt(part, flags, NULL,
83                                              decrypt_result, err);
84     return ret;
85 }