1 Return-Path: <jani@nikula.org>
\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 olra.theworths.org (Postfix) with ESMTP id 826FD431FD0
\r
6 for <notmuch@notmuchmail.org>; Sun, 3 Mar 2013 06:10:24 -0800 (PST)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"
\r
12 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5
\r
13 tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled
\r
14 Received: from olra.theworths.org ([127.0.0.1])
\r
15 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
16 with ESMTP id RYEOZXK7aNPN for <notmuch@notmuchmail.org>;
\r
17 Sun, 3 Mar 2013 06:10:22 -0800 (PST)
\r
18 Received: from mail-lb0-f175.google.com (mail-lb0-f175.google.com
\r
19 [209.85.217.175]) (using TLSv1 with cipher RC4-SHA (128/128 bits))
\r
20 (No client certificate requested)
\r
21 by olra.theworths.org (Postfix) with ESMTPS id 92605431FBF
\r
22 for <notmuch@notmuchmail.org>; Sun, 3 Mar 2013 06:10:18 -0800 (PST)
\r
23 Received: by mail-lb0-f175.google.com with SMTP id n3so3268560lbo.6
\r
24 for <notmuch@notmuchmail.org>; Sun, 03 Mar 2013 06:10:17 -0800 (PST)
\r
25 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
\r
26 d=google.com; s=20120113;
\r
27 h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to
\r
28 :references:in-reply-to:references:x-gm-message-state;
\r
29 bh=u5nYV6lPQeWzSD0ohAho2tviQoGMdjNz+oNnkID1qjE=;
\r
30 b=QaPpZMsfDZDvt2WokOapjegeIO37dWo0G0s4t6hihiw7af6MoRhOAxg2viyA8hBTu1
\r
31 KcZao7CnLoVrbPGj5uUouFsgsATr0sQwLkoqS/Wxs8mHk5YimalaFFKAg5/PuGf3Zkmi
\r
32 x+c2IEdn5VIhbBWiaJEQ7GTEzEKulcw5z8YmZmiHMidm/1xmjiBYKdtjlfpiO/C1E2WY
\r
33 e4oonriXNxei+IAjrdWw6Qta5yeL3LFsM+NHQt3lNV/QuFgCCdVI4Zjflg1NGg/af9Tg
\r
34 77TFAJsOPoAePPPLG53hG5oKqkfHqGs2maBfcfeh2gr32jFJ8FoHrbJYx9YvvuLqw50I
\r
36 X-Received: by 10.112.10.138 with SMTP id i10mr3278034lbb.24.1362319816975;
\r
37 Sun, 03 Mar 2013 06:10:16 -0800 (PST)
\r
38 Received: from localhost (dsl-hkibrasgw4-50df51-27.dhcp.inet.fi.
\r
40 by mx.google.com with ESMTPS id fz16sm10111409lab.5.2013.03.03.06.10.15
\r
41 (version=TLSv1.2 cipher=RC4-SHA bits=128/128);
\r
42 Sun, 03 Mar 2013 06:10:16 -0800 (PST)
\r
43 From: Jani Nikula <jani@nikula.org>
\r
44 To: notmuch@notmuchmail.org
\r
45 Subject: [PATCH 2/2] cli: mime node: abstract decryption and signature
\r
47 Date: Sun, 3 Mar 2013 16:10:11 +0200
\r
49 <d250faaac8aae1f6383b7a77e84e4b4144791688.1362319765.git.jani@nikula.org>
\r
50 X-Mailer: git-send-email 1.7.10.4
\r
52 <a9f12ba474fc51df71e2fd2b7a20a8d101729c6e.1362319765.git.jani@nikula.org>
\r
54 <a9f12ba474fc51df71e2fd2b7a20a8d101729c6e.1362319765.git.jani@nikula.org>
\r
56 <a9f12ba474fc51df71e2fd2b7a20a8d101729c6e.1362319765.git.jani@nikula.org>
\r
58 <a9f12ba474fc51df71e2fd2b7a20a8d101729c6e.1362319765.git.jani@nikula.org>
\r
60 ALoCoQlIbNTVORhZRgaPwImmcXNybkmI21zLix5fPWHMku2hhN+YinpCRaCTlRXXff3XTmWjQuvP
\r
61 X-BeenThere: notmuch@notmuchmail.org
\r
62 X-Mailman-Version: 2.1.13
\r
64 List-Id: "Use and development of the notmuch mail system."
\r
65 <notmuch.notmuchmail.org>
\r
66 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
67 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
68 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
69 List-Post: <mailto:notmuch@notmuchmail.org>
\r
70 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
71 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
72 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
73 X-List-Received-Date: Sun, 03 Mar 2013 14:10:24 -0000
\r
75 The code filled with #ifdef GMIME_ATLEAST_26 is difficult to
\r
76 read. Abstract the decryption and signature verification into
\r
77 functions, with separate implementations for GMime 2.4 and 2.6, to
\r
80 There should be no functional changes.
\r
82 mime-node.c | 210 ++++++++++++++++++++++++++++++++++++++---------------------
\r
83 1 file changed, 134 insertions(+), 76 deletions(-)
\r
85 diff --git a/mime-node.c b/mime-node.c
\r
86 index 839737a..ba7f709 100644
\r
89 @@ -130,26 +130,155 @@ DONE:
\r
92 #ifdef GMIME_ATLEAST_26
\r
95 _signature_list_free (GMimeSignatureList **proxy)
\r
97 g_object_unref (*proxy);
\r
103 +set_signature_list_destructor (mime_node_t *node)
\r
105 + GMimeSignatureList **proxy = talloc (node, GMimeSignatureList *);
\r
107 + *proxy = node->sig_list;
\r
108 + talloc_set_destructor (proxy, _signature_list_free);
\r
113 +node_verify (mime_node_t *node, GMimeObject *part,
\r
114 + notmuch_crypto_context_t *cryptoctx)
\r
116 + GError *err = NULL;
\r
118 + node->verify_attempted = TRUE;
\r
119 + node->sig_list = g_mime_multipart_signed_verify
\r
120 + (GMIME_MULTIPART_SIGNED (part), cryptoctx, &err);
\r
122 + if (node->sig_list)
\r
123 + set_signature_list_destructor (node);
\r
125 + fprintf (stderr, "Failed to verify signed part: %s\n",
\r
126 + err ? err->message : "no error explanation given");
\r
129 + g_error_free (err);
\r
133 +node_decrypt_and_verify (mime_node_t *node, GMimeObject *part,
\r
134 + notmuch_crypto_context_t *cryptoctx)
\r
136 + GError *err = NULL;
\r
137 + GMimeDecryptResult *decrypt_result = NULL;
\r
138 + GMimeMultipartEncrypted *encrypteddata = GMIME_MULTIPART_ENCRYPTED (part);
\r
140 + node->decrypt_attempted = TRUE;
\r
141 + node->decrypted_child = g_mime_multipart_encrypted_decrypt
\r
142 + (encrypteddata, cryptoctx, &decrypt_result, &err);
\r
143 + if (! node->decrypted_child) {
\r
144 + fprintf (stderr, "Failed to decrypt part: %s\n",
\r
145 + err ? err->message : "no error explanation given");
\r
149 + node->decrypt_success = TRUE;
\r
150 + node->verify_attempted = TRUE;
\r
152 + /* This may be NULL if the part is not signed. */
\r
153 + node->sig_list = g_mime_decrypt_result_get_signatures (decrypt_result);
\r
154 + if (node->sig_list) {
\r
155 + g_object_ref (node->sig_list);
\r
156 + set_signature_list_destructor (node);
\r
158 + g_object_unref (decrypt_result);
\r
162 + g_error_free (err);
\r
165 +#else /* GMIME_ATLEAST_26 */
\r
168 _signature_validity_free (GMimeSignatureValidity **proxy)
\r
170 g_mime_signature_validity_free (*proxy);
\r
176 +set_signature_validity_destructor (mime_node_t *node)
\r
178 + GMimeSignatureValidity **proxy = talloc (node, GMimeSignatureValidity *);
\r
180 + *proxy = node->sig_validity;
\r
181 + talloc_set_destructor (proxy, _signature_validity_free);
\r
186 +node_verify (mime_node_t *node, GMimeObject *part,
\r
187 + notmuch_crypto_context_t *cryptoctx)
\r
189 + GError *err = NULL;
\r
191 + node->verify_attempted = TRUE;
\r
192 + node->sig_validity = g_mime_multipart_signed_verify
\r
193 + (GMIME_MULTIPART_SIGNED (part), cryptoctx, &err);
\r
194 + if (node->sig_validity) {
\r
195 + set_signature_validity_destructor (node);
\r
197 + fprintf (stderr, "Failed to verify signed part: %s\n",
\r
198 + err ? err->message : "no error explanation given");
\r
202 + g_error_free (err);
\r
206 +node_decrypt_and_verify (mime_node_t *node, GMimeObject *part,
\r
207 + notmuch_crypto_context_t *cryptoctx)
\r
209 + GError *err = NULL;
\r
210 + GMimeMultipartEncrypted *encrypteddata = GMIME_MULTIPART_ENCRYPTED (part);
\r
212 + node->decrypt_attempted = TRUE;
\r
213 + node->decrypted_child = g_mime_multipart_encrypted_decrypt
\r
214 + (encrypteddata, cryptoctx, &err);
\r
215 + if (! node->decrypted_child) {
\r
216 + fprintf (stderr, "Failed to decrypt part: %s\n",
\r
217 + err ? err->message : "no error explanation given");
\r
221 + node->decrypt_success = TRUE;
\r
222 + node->verify_attempted = TRUE;
\r
224 + /* The GMimeSignatureValidity returned here is a const, unlike the
\r
225 + * one returned by g_mime_multipart_signed_verify() in
\r
226 + * node_verify() above, so the destructor is not needed.
\r
228 + node->sig_validity = g_mime_multipart_encrypted_get_signature_validity (encrypteddata);
\r
229 + if (! node->sig_validity)
\r
230 + fprintf (stderr, "Failed to verify encrypted signed part: %s\n",
\r
231 + err ? err->message : "no error explanation given");
\r
235 + g_error_free (err);
\r
238 +#endif /* GMIME_ATLEAST_26 */
\r
240 static mime_node_t *
\r
241 _mime_node_create (mime_node_t *parent, GMimeObject *part)
\r
243 mime_node_t *node = talloc_zero (parent, mime_node_t);
\r
244 - GError *err = NULL;
\r
245 notmuch_crypto_context_t *cryptoctx = NULL;
\r
247 /* Set basic node properties */
\r
248 @@ -198,32 +327,7 @@ _mime_node_create (mime_node_t *parent, GMimeObject *part)
\r
249 "message (must be exactly 2)\n",
\r
252 - GMimeMultipartEncrypted *encrypteddata =
\r
253 - GMIME_MULTIPART_ENCRYPTED (part);
\r
254 - node->decrypt_attempted = TRUE;
\r
255 -#ifdef GMIME_ATLEAST_26
\r
256 - GMimeDecryptResult *decrypt_result = NULL;
\r
257 - node->decrypted_child = g_mime_multipart_encrypted_decrypt
\r
258 - (encrypteddata, cryptoctx, &decrypt_result, &err);
\r
260 - node->decrypted_child = g_mime_multipart_encrypted_decrypt
\r
261 - (encrypteddata, cryptoctx, &err);
\r
263 - if (node->decrypted_child) {
\r
264 - node->decrypt_success = node->verify_attempted = TRUE;
\r
265 -#ifdef GMIME_ATLEAST_26
\r
266 - /* This may be NULL if the part is not signed. */
\r
267 - node->sig_list = g_mime_decrypt_result_get_signatures (decrypt_result);
\r
268 - if (node->sig_list)
\r
269 - g_object_ref (node->sig_list);
\r
270 - g_object_unref (decrypt_result);
\r
272 - node->sig_validity = g_mime_multipart_encrypted_get_signature_validity (encrypteddata);
\r
275 - fprintf (stderr, "Failed to decrypt part: %s\n",
\r
276 - (err ? err->message : "no error explanation given"));
\r
278 + node_decrypt_and_verify (node, part, cryptoctx);
\r
280 } else if (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->crypto->verify && cryptoctx) {
\r
281 if (node->nchildren != 2) {
\r
282 @@ -232,56 +336,10 @@ _mime_node_create (mime_node_t *parent, GMimeObject *part)
\r
283 "(must be exactly 2)\n",
\r
286 -#ifdef GMIME_ATLEAST_26
\r
287 - node->sig_list = g_mime_multipart_signed_verify
\r
288 - (GMIME_MULTIPART_SIGNED (part), cryptoctx, &err);
\r
289 - node->verify_attempted = TRUE;
\r
291 - if (!node->sig_list)
\r
292 - fprintf (stderr, "Failed to verify signed part: %s\n",
\r
293 - (err ? err->message : "no error explanation given"));
\r
295 - /* For some reason the GMimeSignatureValidity returned
\r
296 - * here is not a const (inconsistent with that
\r
298 - * g_mime_multipart_encrypted_get_signature_validity,
\r
299 - * and therefore needs to be properly disposed of.
\r
301 - * In GMime 2.6, they're both non-const, so we'll be able
\r
302 - * to clean up this asymmetry. */
\r
303 - GMimeSignatureValidity *sig_validity = g_mime_multipart_signed_verify
\r
304 - (GMIME_MULTIPART_SIGNED (part), cryptoctx, &err);
\r
305 - node->verify_attempted = TRUE;
\r
306 - node->sig_validity = sig_validity;
\r
307 - if (sig_validity) {
\r
308 - GMimeSignatureValidity **proxy =
\r
309 - talloc (node, GMimeSignatureValidity *);
\r
310 - *proxy = sig_validity;
\r
311 - talloc_set_destructor (proxy, _signature_validity_free);
\r
314 + node_verify (node, part, cryptoctx);
\r
318 -#ifdef GMIME_ATLEAST_26
\r
319 - /* sig_list may be created in both above cases, so we need to
\r
320 - * cleanly handle it here. */
\r
321 - if (node->sig_list) {
\r
322 - GMimeSignatureList **proxy = talloc (node, GMimeSignatureList *);
\r
323 - *proxy = node->sig_list;
\r
324 - talloc_set_destructor (proxy, _signature_list_free);
\r
328 -#ifndef GMIME_ATLEAST_26
\r
329 - if (node->verify_attempted && !node->sig_validity)
\r
330 - fprintf (stderr, "Failed to verify signed part: %s\n",
\r
331 - (err ? err->message : "no error explanation given"));
\r
335 - g_error_free (err);
\r