[PATCH v4 2/7] cli: new crypto structure to store crypto contexts and parameters...
authorJameson Graef Rollins <jrollins@finestructure.net>
Wed, 23 May 2012 22:40:38 +0000 (15:40 +1700)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:47:17 +0000 (09:47 -0800)
af/3fa117f420b366807167e4acb844d7f2ccd95c [new file with mode: 0644]

diff --git a/af/3fa117f420b366807167e4acb844d7f2ccd95c b/af/3fa117f420b366807167e4acb844d7f2ccd95c
new file mode 100644 (file)
index 0000000..0e0cc24
--- /dev/null
@@ -0,0 +1,195 @@
+Return-Path: <jrollins@finestructure.net>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id AAAD5431FB6\r
+       for <notmuch@notmuchmail.org>; Wed, 23 May 2012 15:40:54 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.3\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id 0aV0+jFu-QwN for <notmuch@notmuchmail.org>;\r
+       Wed, 23 May 2012 15:40:53 -0700 (PDT)\r
+Received: from outgoing-mail.its.caltech.edu (outgoing-mail.its.caltech.edu\r
+       [131.215.239.19])\r
+       by olra.theworths.org (Postfix) with ESMTP id 77223431FC2\r
+       for <notmuch@notmuchmail.org>; Wed, 23 May 2012 15:40:50 -0700 (PDT)\r
+Received: from fire-doxen.imss.caltech.edu (localhost [127.0.0.1])\r
+       by fire-doxen-postvirus (Postfix) with ESMTP id 9C0ED32807F\r
+       for <notmuch@notmuchmail.org>; Wed, 23 May 2012 15:40:49 -0700 (PDT)\r
+X-Spam-Scanned: at Caltech-IMSS on fire-doxen by amavisd-new\r
+Received: from finestructure.net (unknown [137.151.175.56])\r
+       (Authenticated sender: jrollins)\r
+       by fire-doxen-submit (Postfix) with ESMTP id 4CD91328080\r
+       for <notmuch@notmuchmail.org>; Wed, 23 May 2012 15:40:46 -0700 (PDT)\r
+Received: by finestructure.net (Postfix, from userid 1000)\r
+       id F2C471E7; Wed, 23 May 2012 15:40:45 -0700 (PDT)\r
+From: Jameson Graef Rollins <jrollins@finestructure.net>\r
+To: Notmuch Mail <notmuch@notmuchmail.org>\r
+Subject: [PATCH v4 2/7] cli: new crypto structure to store crypto contexts and parameters, and functions to support it\r
+Date: Wed, 23 May 2012 15:40:38 -0700\r
+Message-Id: <1337812843-14986-3-git-send-email-jrollins@finestructure.net>\r
+X-Mailer: git-send-email 1.7.10\r
+In-Reply-To: <1337812843-14986-2-git-send-email-jrollins@finestructure.net>\r
+References: <1337812843-14986-1-git-send-email-jrollins@finestructure.net>\r
+       <1337812843-14986-2-git-send-email-jrollins@finestructure.net>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=UTF-8\r
+Content-Transfer-Encoding: 8bit\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Wed, 23 May 2012 22:40:55 -0000\r
+\r
+This new structure, notmuch_crypto_t, keeps all relevant crypto\r
+contexts and parameters together, and will make it easier to pass the\r
+stuff around and clean it up.  The name of the crypto context inside\r
+this new struct will change, to reflect that it is actually a GPG\r
+context, which is a sub type of Crypto context.  There are other types\r
+of Crypto contexts (Pkcs7 in particular, which we hope to support) so\r
+we want to be clear.\r
+\r
+The new crypto.c contains functions to return the proper context from\r
+the struct for a given protocol (and initialize it if needed), and to\r
+cleanup a struct by releasing the crypto contexts.\r
+---\r
+ Makefile.local   |    1 +\r
+ crypto.c         |   71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ notmuch-client.h |   11 +++++++++\r
+ 3 files changed, 83 insertions(+)\r
+ create mode 100644 crypto.c\r
+\r
+diff --git a/Makefile.local b/Makefile.local\r
+index 53b4a0d..a890df2 100644\r
+--- a/Makefile.local\r
++++ b/Makefile.local\r
+@@ -292,6 +292,7 @@ notmuch_client_srcs =              \\r
+       notmuch-time.c          \\r
+       query-string.c          \\r
+       mime-node.c             \\r
++      crypto.c                \\r
+       json.c\r
\r
+ notmuch_client_modules = $(notmuch_client_srcs:.c=.o)\r
+diff --git a/crypto.c b/crypto.c\r
+new file mode 100644\r
+index 0000000..25c2d10\r
+--- /dev/null\r
++++ b/crypto.c\r
+@@ -0,0 +1,71 @@\r
++/* notmuch - Not much of an email program, (just index and search)\r
++ *\r
++ * Copyright © 2012 Jameson Rollins\r
++ *\r
++ * This program is free software: you can redistribute it and/or modify\r
++ * it under the terms of the GNU General Public License as published by\r
++ * the Free Software Foundation, either version 3 of the License, or\r
++ * (at your option) any later version.\r
++ *\r
++ * This program is distributed in the hope that it will be useful,\r
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
++ * GNU General Public License for more details.\r
++ *\r
++ * You should have received a copy of the GNU General Public License\r
++ * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
++ *\r
++ * Authors: Jameson Rollins <jrollins@finestructure.net>\r
++ */\r
++\r
++#include "notmuch-client.h"\r
++\r
++/* for the specified protocol return the context pointer (initializing\r
++ * if needed) */\r
++GMimeCryptoContext *\r
++notmuch_crypto_get_context (notmuch_crypto_t *crypto, const char *protocol)\r
++{\r
++    GMimeCryptoContext *cryptoctx = NULL;\r
++\r
++    /* As per RFC 1847 section 2.1: "the [protocol] value token is\r
++     * comprised of the type and sub-type tokens of the Content-Type".\r
++     * As per RFC 1521 section 2: "Content-Type values, subtypes, and\r
++     * parameter names as defined in this document are\r
++     * case-insensitive."  Thus, we use strcasecmp for the protocol.\r
++     */\r
++    if ((strcasecmp (protocol, "application/pgp-signature") == 0)\r
++      || (strcasecmp (protocol, "application/pgp-encrypted") == 0)) {\r
++      if (!crypto->gpgctx) {\r
++#ifdef GMIME_ATLEAST_26\r
++          /* TODO: GMimePasswordRequestFunc */\r
++          crypto->gpgctx = g_mime_gpg_context_new (NULL, "gpg");\r
++#else\r
++          GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);\r
++          crypto->gpgctx = g_mime_gpg_context_new (session, "gpg");\r
++          g_object_unref (session);\r
++#endif\r
++          if (crypto->gpgctx) {\r
++              g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) crypto->gpgctx, FALSE);\r
++          } else {\r
++              fprintf (stderr, "Failed to construct gpg context.\n");\r
++          }\r
++      }\r
++      cryptoctx = crypto->gpgctx;\r
++\r
++    } else {\r
++      fprintf (stderr, "Unknown or unsupported cryptographic protocol.\n");\r
++    }\r
++\r
++    return cryptoctx;\r
++}\r
++\r
++int\r
++notmuch_crypto_cleanup (notmuch_crypto_t *crypto)\r
++{\r
++    if (crypto->gpgctx) {\r
++      g_object_unref(crypto->gpgctx);\r
++      crypto->gpgctx = NULL;\r
++    }\r
++\r
++    return 0;\r
++}\r
+diff --git a/notmuch-client.h b/notmuch-client.h\r
+index 337409f..a8b00ab 100644\r
+--- a/notmuch-client.h\r
++++ b/notmuch-client.h\r
+@@ -76,6 +76,11 @@ typedef struct notmuch_show_format {\r
+     const char *message_set_end;\r
+ } notmuch_show_format_t;\r
\r
++typedef struct notmuch_crypto {\r
++    GMimeCryptoContext* gpgctx;\r
++    notmuch_bool_t decrypt;\r
++} notmuch_crypto_t;\r
++\r
+ typedef struct notmuch_show_params {\r
+     notmuch_bool_t entire_thread;\r
+     notmuch_bool_t omit_excluded;\r
+@@ -111,6 +116,12 @@ chomp_newline (char *str)\r
+       str[strlen(str)-1] = '\0';\r
+ }\r
\r
++GMimeCryptoContext *\r
++notmuch_crypto_get_context (notmuch_crypto_t *crypto, const char *protocol);\r
++\r
++int\r
++notmuch_crypto_cleanup (notmuch_crypto_t *crypto);\r
++\r
+ int\r
+ notmuch_count_command (void *ctx, int argc, char *argv[]);\r
\r
+-- \r
+1.7.10\r
+\r