1 Return-Path: <dkg@fifthhorseman.net>
\r
2 X-Original-To: notmuch@notmuchmail.org
\r
3 Delivered-To: notmuch@notmuchmail.org
\r
4 Received: from localhost (localhost [127.0.0.1])
\r
5 by arlo.cworth.org (Postfix) with ESMTP id 1A7966DE1B1C
\r
6 for <notmuch@notmuchmail.org>; Sun, 31 Jan 2016 12:40:15 -0800 (PST)
\r
7 X-Virus-Scanned: Debian amavisd-new at cworth.org
\r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]
\r
13 Received: from arlo.cworth.org ([127.0.0.1])
\r
14 by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)
\r
15 with ESMTP id vkPz5K4UqHMl for <notmuch@notmuchmail.org>;
\r
16 Sun, 31 Jan 2016 12:40:12 -0800 (PST)
\r
17 Received: from che.mayfirst.org (che.mayfirst.org [209.234.253.108])
\r
18 by arlo.cworth.org (Postfix) with ESMTP id E51046DE0C5F
\r
19 for <notmuch@notmuchmail.org>; Sun, 31 Jan 2016 12:40:08 -0800 (PST)
\r
20 Received: from fifthhorseman.net (ip-64-134-185-108.public.wayport.net
\r
22 by che.mayfirst.org (Postfix) with ESMTPSA id 8B562F993
\r
23 for <notmuch@notmuchmail.org>; Sun, 31 Jan 2016 15:40:05 -0500 (EST)
\r
24 Received: by fifthhorseman.net (Postfix, from userid 1000)
\r
25 id 30F5920388; Sun, 31 Jan 2016 15:40:06 -0500 (EST)
\r
26 From: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
\r
27 To: Notmuch Mail <notmuch@notmuchmail.org>
\r
28 Subject: [PATCH v3 02/16] Move crypto.c into libutil
\r
29 Date: Sun, 31 Jan 2016 15:39:47 -0500
\r
30 Message-Id: <1454272801-23623-3-git-send-email-dkg@fifthhorseman.net>
\r
31 X-Mailer: git-send-email 2.7.0.rc3
\r
32 In-Reply-To: <1454272801-23623-1-git-send-email-dkg@fifthhorseman.net>
\r
33 References: <1454272801-23623-1-git-send-email-dkg@fifthhorseman.net>
\r
35 Content-Type: text/plain; charset=UTF-8
\r
36 Content-Transfer-Encoding: 8bit
\r
37 X-BeenThere: notmuch@notmuchmail.org
\r
38 X-Mailman-Version: 2.1.20
\r
40 List-Id: "Use and development of the notmuch mail system."
\r
41 <notmuch.notmuchmail.org>
\r
42 List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,
\r
43 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
44 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>
\r
45 List-Post: <mailto:notmuch@notmuchmail.org>
\r
46 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
47 List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,
\r
48 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
49 X-List-Received-Date: Sun, 31 Jan 2016 20:40:15 -0000
\r
51 This prepares us for using the crypto object in both the library and
\r
54 i've prefixed notmuch_crypto with _ to indicate that while this can be
\r
55 built into the library when needed, it's not something to be exported
\r
58 Makefile.local | 1 -
\r
59 crypto.c | 134 --------------------------------------------------
\r
60 mime-node.c | 12 ++---
\r
61 notmuch-client.h | 23 ++-------
\r
62 notmuch-reply.c | 2 +-
\r
63 notmuch-show.c | 2 +-
\r
64 util/Makefile.local | 2 +-
\r
65 util/crypto.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++
\r
66 util/crypto.h | 25 ++++++++++
\r
67 9 files changed, 175 insertions(+), 164 deletions(-)
\r
68 delete mode 100644 crypto.c
\r
69 create mode 100644 util/crypto.c
\r
70 create mode 100644 util/crypto.h
\r
72 diff --git a/Makefile.local b/Makefile.local
\r
73 index 066ecf2..6206771 100644
\r
74 --- a/Makefile.local
\r
75 +++ b/Makefile.local
\r
76 @@ -293,7 +293,6 @@ notmuch_client_srcs = \
\r
83 notmuch_client_modules = $(notmuch_client_srcs:.c=.o)
\r
84 diff --git a/crypto.c b/crypto.c
\r
85 deleted file mode 100644
\r
86 index 3dabc97..0000000
\r
90 -/* notmuch - Not much of an email program, (just index and search)
\r
92 - * Copyright © 2012 Jameson Rollins
\r
94 - * This program is free software: you can redistribute it and/or modify
\r
95 - * it under the terms of the GNU General Public License as published by
\r
96 - * the Free Software Foundation, either version 3 of the License, or
\r
97 - * (at your option) any later version.
\r
99 - * This program is distributed in the hope that it will be useful,
\r
100 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
101 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
102 - * GNU General Public License for more details.
\r
104 - * You should have received a copy of the GNU General Public License
\r
105 - * along with this program. If not, see http://www.gnu.org/licenses/ .
\r
107 - * Authors: Jameson Rollins <jrollins@finestructure.net>
\r
110 -#include "notmuch-client.h"
\r
112 -/* Create a GPG context (GMime 2.6) */
\r
113 -static notmuch_crypto_context_t *
\r
114 -create_gpg_context (notmuch_crypto_t *crypto)
\r
116 - notmuch_crypto_context_t *gpgctx;
\r
118 - if (crypto->gpgctx)
\r
119 - return crypto->gpgctx;
\r
121 - /* TODO: GMimePasswordRequestFunc */
\r
122 - gpgctx = g_mime_gpg_context_new (NULL, crypto->gpgpath ? crypto->gpgpath : "gpg");
\r
124 - fprintf (stderr, "Failed to construct gpg context.\n");
\r
127 - crypto->gpgctx = gpgctx;
\r
129 - g_mime_gpg_context_set_use_agent ((GMimeGpgContext *) gpgctx, TRUE);
\r
130 - g_mime_gpg_context_set_always_trust ((GMimeGpgContext *) gpgctx, FALSE);
\r
135 -/* Create a PKCS7 context (GMime 2.6) */
\r
136 -static notmuch_crypto_context_t *
\r
137 -create_pkcs7_context (notmuch_crypto_t *crypto)
\r
139 - notmuch_crypto_context_t *pkcs7ctx;
\r
141 - if (crypto->pkcs7ctx)
\r
142 - return crypto->pkcs7ctx;
\r
144 - /* TODO: GMimePasswordRequestFunc */
\r
145 - pkcs7ctx = g_mime_pkcs7_context_new (NULL);
\r
146 - if (! pkcs7ctx) {
\r
147 - fprintf (stderr, "Failed to construct pkcs7 context.\n");
\r
150 - crypto->pkcs7ctx = pkcs7ctx;
\r
152 - g_mime_pkcs7_context_set_always_trust ((GMimePkcs7Context *) pkcs7ctx,
\r
157 -static const struct {
\r
158 - const char *protocol;
\r
159 - notmuch_crypto_context_t *(*get_context) (notmuch_crypto_t *crypto);
\r
162 - .protocol = "application/pgp-signature",
\r
163 - .get_context = create_gpg_context,
\r
166 - .protocol = "application/pgp-encrypted",
\r
167 - .get_context = create_gpg_context,
\r
170 - .protocol = "application/pkcs7-signature",
\r
171 - .get_context = create_pkcs7_context,
\r
174 - .protocol = "application/x-pkcs7-signature",
\r
175 - .get_context = create_pkcs7_context,
\r
179 -/* for the specified protocol return the context pointer (initializing
\r
181 -notmuch_crypto_context_t *
\r
182 -notmuch_crypto_get_context (notmuch_crypto_t *crypto, const char *protocol)
\r
184 - notmuch_crypto_context_t *cryptoctx = NULL;
\r
187 - if (! protocol) {
\r
188 - fprintf (stderr, "Cryptographic protocol is empty.\n");
\r
189 - return cryptoctx;
\r
192 - /* As per RFC 1847 section 2.1: "the [protocol] value token is
\r
193 - * comprised of the type and sub-type tokens of the Content-Type".
\r
194 - * As per RFC 1521 section 2: "Content-Type values, subtypes, and
\r
195 - * parameter names as defined in this document are
\r
196 - * case-insensitive." Thus, we use strcasecmp for the protocol.
\r
198 - for (i = 0; i < ARRAY_SIZE (protocols); i++) {
\r
199 - if (strcasecmp (protocol, protocols[i].protocol) == 0)
\r
200 - return protocols[i].get_context (crypto);
\r
203 - fprintf (stderr, "Unknown or unsupported cryptographic protocol %s.\n",
\r
210 -notmuch_crypto_cleanup (notmuch_crypto_t *crypto)
\r
212 - if (crypto->gpgctx) {
\r
213 - g_object_unref (crypto->gpgctx);
\r
214 - crypto->gpgctx = NULL;
\r
217 - if (crypto->pkcs7ctx) {
\r
218 - g_object_unref (crypto->pkcs7ctx);
\r
219 - crypto->pkcs7ctx = NULL;
\r
224 diff --git a/mime-node.c b/mime-node.c
\r
225 index e96e663..a8f5670 100644
\r
228 @@ -33,7 +33,7 @@ typedef struct mime_node_context {
\r
229 GMimeMessage *mime_message;
\r
231 /* Context provided by the caller. */
\r
232 - notmuch_crypto_t *crypto;
\r
233 + _notmuch_crypto_t *crypto;
\r
234 } mime_node_context_t;
\r
237 @@ -56,7 +56,7 @@ _mime_node_context_free (mime_node_context_t *res)
\r
240 mime_node_open (const void *ctx, notmuch_message_t *message,
\r
241 - notmuch_crypto_t *crypto, mime_node_t **root_out)
\r
242 + _notmuch_crypto_t *crypto, mime_node_t **root_out)
\r
244 const char *filename = notmuch_message_get_filename (message);
\r
245 mime_node_context_t *mctx;
\r
246 @@ -151,7 +151,7 @@ set_signature_list_destructor (mime_node_t *node)
\r
247 /* Verify a signed mime node (GMime 2.6) */
\r
249 node_verify (mime_node_t *node, GMimeObject *part,
\r
250 - notmuch_crypto_context_t *cryptoctx)
\r
251 + GMimeCryptoContext *cryptoctx)
\r
253 GError *err = NULL;
\r
255 @@ -172,7 +172,7 @@ node_verify (mime_node_t *node, GMimeObject *part,
\r
256 /* Decrypt and optionally verify an encrypted mime node (GMime 2.6) */
\r
258 node_decrypt_and_verify (mime_node_t *node, GMimeObject *part,
\r
259 - notmuch_crypto_context_t *cryptoctx)
\r
260 + GMimeCryptoContext *cryptoctx)
\r
262 GError *err = NULL;
\r
263 GMimeDecryptResult *decrypt_result = NULL;
\r
264 @@ -207,7 +207,7 @@ static mime_node_t *
\r
265 _mime_node_create (mime_node_t *parent, GMimeObject *part)
\r
267 mime_node_t *node = talloc_zero (parent, mime_node_t);
\r
268 - notmuch_crypto_context_t *cryptoctx = NULL;
\r
269 + GMimeCryptoContext *cryptoctx = NULL;
\r
271 /* Set basic node properties */
\r
273 @@ -244,7 +244,7 @@ _mime_node_create (mime_node_t *parent, GMimeObject *part)
\r
274 || (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->crypto->verify)) {
\r
275 GMimeContentType *content_type = g_mime_object_get_content_type (part);
\r
276 const char *protocol = g_mime_content_type_get_parameter (content_type, "protocol");
\r
277 - cryptoctx = notmuch_crypto_get_context (node->ctx->crypto, protocol);
\r
278 + cryptoctx = _notmuch_crypto_get_gmime_context (node->ctx->crypto, protocol);
\r
281 /* Handle PGP/MIME parts */
\r
282 diff --git a/notmuch-client.h b/notmuch-client.h
\r
283 index 18e6c60..a41e90a 100644
\r
284 --- a/notmuch-client.h
\r
285 +++ b/notmuch-client.h
\r
288 #include <gmime/gmime.h>
\r
290 -typedef GMimeCryptoContext notmuch_crypto_context_t;
\r
291 -/* This is automatically included only since gmime 2.6.10 */
\r
292 -#include <gmime/gmime-pkcs7-context.h>
\r
294 #include "notmuch.h"
\r
296 /* This is separate from notmuch-private.h because we're trying to
\r
297 @@ -53,6 +49,7 @@ typedef GMimeCryptoContext notmuch_crypto_context_t;
\r
300 #include "talloc-extra.h"
\r
301 +#include "crypto.h"
\r
303 #define unused(x) x __attribute__ ((unused))
\r
305 @@ -70,21 +67,13 @@ typedef struct notmuch_show_format {
\r
306 const struct notmuch_show_params *params);
\r
307 } notmuch_show_format_t;
\r
309 -typedef struct notmuch_crypto {
\r
310 - notmuch_crypto_context_t* gpgctx;
\r
311 - notmuch_crypto_context_t* pkcs7ctx;
\r
312 - notmuch_bool_t verify;
\r
313 - notmuch_bool_t decrypt;
\r
314 - const char *gpgpath;
\r
315 -} notmuch_crypto_t;
\r
317 typedef struct notmuch_show_params {
\r
318 notmuch_bool_t entire_thread;
\r
319 notmuch_bool_t omit_excluded;
\r
320 notmuch_bool_t output_body;
\r
321 notmuch_bool_t raw;
\r
323 - notmuch_crypto_t crypto;
\r
324 + _notmuch_crypto_t crypto;
\r
325 notmuch_bool_t include_html;
\r
326 } notmuch_show_params_t;
\r
328 @@ -167,12 +156,6 @@ typedef struct _notmuch_config notmuch_config_t;
\r
330 notmuch_exit_if_unsupported_format (void);
\r
332 -notmuch_crypto_context_t *
\r
333 -notmuch_crypto_get_context (notmuch_crypto_t *crypto, const char *protocol);
\r
336 -notmuch_crypto_cleanup (notmuch_crypto_t *crypto);
\r
339 notmuch_count_command (notmuch_config_t *config, int argc, char *argv[]);
\r
341 @@ -423,7 +406,7 @@ struct mime_node {
\r
344 mime_node_open (const void *ctx, notmuch_message_t *message,
\r
345 - notmuch_crypto_t *crypto, mime_node_t **node_out);
\r
346 + _notmuch_crypto_t *crypto, mime_node_t **node_out);
\r
348 /* Return a new MIME node for the requested child part of parent.
\r
349 * parent will be used as the talloc context for the returned child
\r
350 diff --git a/notmuch-reply.c b/notmuch-reply.c
\r
351 index 6df54fc..eccfb32 100644
\r
352 --- a/notmuch-reply.c
\r
353 +++ b/notmuch-reply.c
\r
354 @@ -862,7 +862,7 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])
\r
355 if (reply_format_func (config, config, query, ¶ms, reply_all, sp) != 0)
\r
356 return EXIT_FAILURE;
\r
358 - notmuch_crypto_cleanup (¶ms.crypto);
\r
359 + _notmuch_crypto_cleanup (¶ms.crypto);
\r
360 notmuch_query_destroy (query);
\r
361 notmuch_database_destroy (notmuch);
\r
363 diff --git a/notmuch-show.c b/notmuch-show.c
\r
364 index 87e52bb..3c91ece 100644
\r
365 --- a/notmuch-show.c
\r
366 +++ b/notmuch-show.c
\r
367 @@ -1171,7 +1171,7 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])
\r
368 ret = do_show (config, query, format, sprinter, ¶ms);
\r
371 - notmuch_crypto_cleanup (¶ms.crypto);
\r
372 + _notmuch_crypto_cleanup (¶ms.crypto);
\r
373 notmuch_query_destroy (query);
\r
374 notmuch_database_destroy (notmuch);
\r
376 diff --git a/util/Makefile.local b/util/Makefile.local
\r
377 index 8b2b91b..7590618 100644
\r
378 --- a/util/Makefile.local
\r
379 +++ b/util/Makefile.local
\r
380 @@ -5,7 +5,7 @@ extra_cflags += -I$(srcdir)/$(dir)
\r
382 libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c \
\r
383 $(dir)/string-util.c $(dir)/talloc-extra.c $(dir)/zlib-extra.c \
\r
384 - $(dir)/util.c $(dir)/search-path.c
\r
385 + $(dir)/util.c $(dir)/search-path.c $(dir)/crypto.c
\r
387 libutil_modules := $(libutil_c_srcs:.c=.o)
\r
389 diff --git a/util/crypto.c b/util/crypto.c
\r
390 new file mode 100644
\r
391 index 0000000..eab12a3
\r
393 +++ b/util/crypto.c
\r
395 +/* notmuch - Not much of an email program, (just index and search)
\r
397 + * Copyright © 2012 Jameson Rollins
\r
399 + * This program is free software: you can redistribute it and/or modify
\r
400 + * it under the terms of the GNU General Public License as published by
\r
401 + * the Free Software Foundation, either version 3 of the License, or
\r
402 + * (at your option) any later version.
\r
404 + * This program is distributed in the hope that it will be useful,
\r
405 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
406 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
407 + * GNU General Public License for more details.
\r
409 + * You should have received a copy of the GNU General Public License
\r
410 + * along with this program. If not, see http://www.gnu.org/licenses/ .
\r
412 + * Authors: Jameson Rollins <jrollins@finestructure.net>
\r
413 + * Daniel Kahn Gillmor <dkg@fifthhorseman.net>
\r
416 +#include "notmuch.h"
\r
417 +#include "crypto.h"
\r
418 +#include <string.h>
\r
420 +#define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
\r
422 +/* Create a GPG context (GMime 2.6) */
\r
423 +static GMimeCryptoContext*
\r
424 +create_gpg_context (_notmuch_crypto_t *crypto)
\r
426 + GMimeCryptoContext *gpgctx;
\r
428 + if (crypto->gpgctx) {
\r
429 + return crypto->gpgctx;
\r
432 + /* TODO: GMimePasswordRequestFunc */
\r
433 + gpgctx = g_mime_gpg_context_new (NULL, crypto->gpgpath ? crypto->gpgpath : "gpg");
\r
435 + fprintf (stderr, "Failed to construct gpg context.\n");
\r
438 + crypto->gpgctx = gpgctx;
\r
440 + g_mime_gpg_context_set_use_agent ((GMimeGpgContext *) gpgctx, TRUE);
\r
441 + g_mime_gpg_context_set_always_trust ((GMimeGpgContext *) gpgctx, FALSE);
\r
443 + return crypto->gpgctx;
\r
446 +/* Create a PKCS7 context (GMime 2.6) */
\r
447 +static notmuch_crypto_context_t *
\r
448 +create_pkcs7_context (notmuch_crypto_t *crypto)
\r
450 + notmuch_crypto_context_t *pkcs7ctx;
\r
452 + if (crypto->pkcs7ctx)
\r
453 + return crypto->pkcs7ctx;
\r
455 + /* TODO: GMimePasswordRequestFunc */
\r
456 + pkcs7ctx = g_mime_pkcs7_context_new (NULL);
\r
457 + if (! pkcs7ctx) {
\r
458 + fprintf (stderr, "Failed to construct pkcs7 context.\n");
\r
461 + crypto->pkcs7ctx = pkcs7ctx;
\r
463 + g_mime_pkcs7_context_set_always_trust ((GMimePkcs7Context *) pkcs7ctx,
\r
466 + return crypto->pkcs7ctx;
\r
468 +static const struct {
\r
469 + const char *protocol;
\r
470 + GMimeCryptoContext *(*get_context) (_notmuch_crypto_t *crypto);
\r
473 + .protocol = "application/pgp-signature",
\r
474 + .get_context = create_gpg_context,
\r
477 + .protocol = "application/pgp-encrypted",
\r
478 + .get_context = create_gpg_context,
\r
481 + .protocol = "application/pkcs7-signature",
\r
482 + .get_context = create_pkcs7_context,
\r
485 + .protocol = "application/x-pkcs7-signature",
\r
486 + .get_context = create_pkcs7_context,
\r
490 +/* for the specified protocol return the context pointer (initializing
\r
492 +GMimeCryptoContext *
\r
493 +_notmuch_crypto_get_gmime_context (_notmuch_crypto_t *crypto, const char *protocol)
\r
495 + GMimeCryptoContext *cryptoctx = NULL;
\r
498 + if (! protocol) {
\r
499 + fprintf (stderr, "Cryptographic protocol is empty.\n");
\r
500 + return cryptoctx;
\r
503 + /* As per RFC 1847 section 2.1: "the [protocol] value token is
\r
504 + * comprised of the type and sub-type tokens of the Content-Type".
\r
505 + * As per RFC 1521 section 2: "Content-Type values, subtypes, and
\r
506 + * parameter names as defined in this document are
\r
507 + * case-insensitive." Thus, we use strcasecmp for the protocol.
\r
509 + for (i = 0; i < ARRAY_SIZE (protocols); i++) {
\r
510 + if (strcasecmp (protocol, protocols[i].protocol) == 0)
\r
511 + return protocols[i].get_context (crypto);
\r
514 + fprintf (stderr, "Unknown or unsupported cryptographic protocol %s.\n",
\r
521 +_notmuch_crypto_cleanup (_notmuch_crypto_t *crypto)
\r
523 + if (crypto->gpgctx) {
\r
524 + g_object_unref (crypto->gpgctx);
\r
525 + crypto->gpgctx = NULL;
\r
528 + if (crypto->pkcs7ctx) {
\r
529 + g_object_unref (crypto->pkcs7ctx);
\r
530 + crypto->pkcs7ctx = NULL;
\r
533 diff --git a/util/crypto.h b/util/crypto.h
\r
534 new file mode 100644
\r
535 index 0000000..d4a51e8
\r
537 +++ b/util/crypto.h
\r
542 +#include "notmuch.h"
\r
543 +#include <gmime/gmime.h>
\r
544 +/* This is automatically included only since gmime 2.6.10 */
\r
545 +#include <gmime/gmime-pkcs7-context.h>
\r
547 +typedef struct _notmuch_crypto {
\r
548 + GMimeCryptoContext* gpgctx;
\r
549 + GMimeCryptoContext* pkcs7ctx;
\r
550 + notmuch_bool_t verify;
\r
551 + notmuch_bool_t decrypt;
\r
552 + const char *gpgpath;
\r
553 +} _notmuch_crypto_t;
\r
556 +GMimeCryptoContext *
\r
557 +_notmuch_crypto_get_gmime_context (_notmuch_crypto_t *crypto, const char *protocol);
\r
560 +_notmuch_crypto_cleanup (_notmuch_crypto_t *crypto);
\r