]> git.notmuchmail.org Git - notmuch/blob - util/crypto.c
util/crypto: improve comment
[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                          GMimeMultipartEncrypted *part,
36                          GMimeDecryptResult **decrypt_result,
37                          GError **err)
38 {
39     GMimeObject *ret = NULL;
40     if (decrypt == NOTMUCH_DECRYPT_FALSE)
41         return NULL;
42
43     /* try decryption with session key if one is stashed */
44     if (message) {
45         notmuch_message_properties_t *list = NULL;
46
47         for (list = notmuch_message_get_properties (message, "session-key", TRUE);
48              notmuch_message_properties_valid (list); notmuch_message_properties_move_to_next (list)) {
49             if (err && *err) {
50                 g_error_free (*err);
51                 *err = NULL;
52             }
53             if (attempted)
54                 *attempted = true;
55             ret = g_mime_multipart_encrypted_decrypt (part,
56                                                       GMIME_DECRYPT_NONE,
57                                                       notmuch_message_properties_value (list),
58                                                       decrypt_result, err);
59             if (ret)
60                 break;
61         }
62         if (list)
63             notmuch_message_properties_destroy (list);
64         if (ret)
65             return ret;
66     }
67
68     if (err && *err) {
69         g_error_free (*err);
70         *err = NULL;
71     }
72
73     if (decrypt == NOTMUCH_DECRYPT_AUTO)
74         return ret;
75
76     if (attempted)
77         *attempted = true;
78     GMimeDecryptFlags flags = GMIME_DECRYPT_NONE;
79     if (decrypt == NOTMUCH_DECRYPT_TRUE && decrypt_result)
80         flags |= GMIME_DECRYPT_EXPORT_SESSION_KEY;
81     ret = g_mime_multipart_encrypted_decrypt(part, flags, NULL,
82                                              decrypt_result, err);
83     return ret;
84 }