]> git.notmuchmail.org Git - notmuch/blob - crypto.c
debian: start changelog for 0.23.6
[notmuch] / 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 "notmuch-client.h"
22
23 /* Create a GPG context (GMime 2.6) */
24 static notmuch_crypto_context_t *
25 create_gpg_context (notmuch_crypto_t *crypto)
26 {
27     notmuch_crypto_context_t *gpgctx;
28
29     if (crypto->gpgctx)
30         return crypto->gpgctx;
31
32     /* TODO: GMimePasswordRequestFunc */
33     gpgctx = g_mime_gpg_context_new (NULL, crypto->gpgpath ? crypto->gpgpath : "gpg");
34     if (! gpgctx) {
35         fprintf (stderr, "Failed to construct gpg context.\n");
36         return NULL;
37     }
38     crypto->gpgctx = gpgctx;
39
40     g_mime_gpg_context_set_use_agent ((GMimeGpgContext *) gpgctx, TRUE);
41     g_mime_gpg_context_set_always_trust ((GMimeGpgContext *) gpgctx, FALSE);
42
43     return gpgctx;
44 }
45
46 /* Create a PKCS7 context (GMime 2.6) */
47 static notmuch_crypto_context_t *
48 create_pkcs7_context (notmuch_crypto_t *crypto)
49 {
50     notmuch_crypto_context_t *pkcs7ctx;
51
52     if (crypto->pkcs7ctx)
53         return crypto->pkcs7ctx;
54
55     /* TODO: GMimePasswordRequestFunc */
56     pkcs7ctx = g_mime_pkcs7_context_new (NULL);
57     if (! pkcs7ctx) {
58         fprintf (stderr, "Failed to construct pkcs7 context.\n");
59         return NULL;
60     }
61     crypto->pkcs7ctx = pkcs7ctx;
62
63     g_mime_pkcs7_context_set_always_trust ((GMimePkcs7Context *) pkcs7ctx,
64                                            FALSE);
65
66     return pkcs7ctx;
67 }
68 static const struct {
69     const char *protocol;
70     notmuch_crypto_context_t *(*get_context) (notmuch_crypto_t *crypto);
71 } protocols[] = {
72     {
73         .protocol = "application/pgp-signature",
74         .get_context = create_gpg_context,
75     },
76     {
77         .protocol = "application/pgp-encrypted",
78         .get_context = create_gpg_context,
79     },
80     {
81         .protocol = "application/pkcs7-signature",
82         .get_context = create_pkcs7_context,
83     },
84     {
85         .protocol = "application/x-pkcs7-signature",
86         .get_context = create_pkcs7_context,
87     },
88 };
89
90 /* for the specified protocol return the context pointer (initializing
91  * if needed) */
92 notmuch_crypto_context_t *
93 notmuch_crypto_get_context (notmuch_crypto_t *crypto, const char *protocol)
94 {
95     notmuch_crypto_context_t *cryptoctx = NULL;
96     size_t i;
97
98     if (! protocol) {
99         fprintf (stderr, "Cryptographic protocol is empty.\n");
100         return cryptoctx;
101     }
102
103     /* As per RFC 1847 section 2.1: "the [protocol] value token is
104      * comprised of the type and sub-type tokens of the Content-Type".
105      * As per RFC 1521 section 2: "Content-Type values, subtypes, and
106      * parameter names as defined in this document are
107      * case-insensitive."  Thus, we use strcasecmp for the protocol.
108      */
109     for (i = 0; i < ARRAY_SIZE (protocols); i++) {
110         if (strcasecmp (protocol, protocols[i].protocol) == 0)
111             return protocols[i].get_context (crypto);
112     }
113
114     fprintf (stderr, "Unknown or unsupported cryptographic protocol %s.\n",
115              protocol);
116
117     return NULL;
118 }
119
120 int
121 notmuch_crypto_cleanup (notmuch_crypto_t *crypto)
122 {
123     if (crypto->gpgctx) {
124         g_object_unref (crypto->gpgctx);
125         crypto->gpgctx = NULL;
126     }
127
128     if (crypto->pkcs7ctx) {
129         g_object_unref (crypto->pkcs7ctx);
130         crypto->pkcs7ctx = NULL;
131     }
132
133     return 0;
134 }