git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Henning Schild <henning.schild@siemens.com>
To: git@vger.kernel.org
Cc: Ben Toews <mastahyeti@gmail.com>, Jeff King <peff@peff.net>,
	Junio C Hamano <gitster@pobox.com>, Taylor Blau <me@ttaylorr.com>,
	"brian m . carlson" <sandals@crustytoothpaste.net>,
	Eric Sunshine <sunshine@sunshineco.com>,
	Henning Schild <henning.schild@siemens.com>
Subject: [PATCH 4/8] gpg-interface: introduce an abstraction for multiple gpg formats
Date: Tue,  3 Jul 2018 14:38:16 +0200	[thread overview]
Message-ID: <db46732d1cd09a71200e00c2d09605d5beaff364.1530616446.git.henning.schild@siemens.com> (raw)
In-Reply-To: <cover.1530616446.git.henning.schild@siemens.com>
In-Reply-To: <cover.1530616446.git.henning.schild@siemens.com>

Create a struct that holds the format details for the supported formats.
At the moment that is still just "PGP". This commit prepares for the
introduction of more formats, that might use other programs and match
other signatures.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
---
 gpg-interface.c | 80 +++++++++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 64 insertions(+), 16 deletions(-)

diff --git a/gpg-interface.c b/gpg-interface.c
index 1def1f131..cd3b1b568 100644
--- a/gpg-interface.c
+++ b/gpg-interface.c
@@ -7,12 +7,46 @@
 #include "tempfile.h"
 
 static char *configured_signing_key;
-static const char *gpg_format = "PGP";
-static const char *gpg_program = "gpg";
+struct gpg_format_data {
+	const char *format;
+	const char *program;
+	const char *extra_args_verify[1];
+	const char *sigs[2];
+};
 
 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
 #define PGP_MESSAGE "-----BEGIN PGP MESSAGE-----"
 
+enum gpgformats { PGP_FMT };
+struct gpg_format_data gpg_formats[] = {
+	{ .format = "PGP", .program = "gpg",
+	  .extra_args_verify = { "--keyid-format=long", },
+	  .sigs = { PGP_SIGNATURE, PGP_MESSAGE, },
+	},
+};
+static const char *gpg_format = "PGP";
+
+static struct gpg_format_data *get_format_data(void)
+{
+	int i;
+	for (i = 0; i < ARRAY_SIZE(gpg_formats); i++)
+		if (!strcmp(gpg_formats[i].format, gpg_format))
+			return gpg_formats + i;
+	assert(0);
+}
+
+static struct gpg_format_data *get_format_data_by_sig(const char *sig)
+{
+	int i, j;
+	for (i = 0; i < ARRAY_SIZE(gpg_formats); i++)
+		for (j = 0; j < ARRAY_SIZE(gpg_formats[i].sigs); j++)
+			if (gpg_formats[i].sigs[j] && 
+			    !strncmp(gpg_formats[i].sigs[j], sig,
+				     strlen(gpg_formats[i].sigs[j])))
+				return gpg_formats + i;
+	return NULL;
+}
+
 void signature_check_clear(struct signature_check *sigc)
 {
 	FREE_AND_NULL(sigc->payload);
@@ -104,8 +138,7 @@ void print_signature_buffer(const struct signature_check *sigc, unsigned flags)
 
 static int is_gpg_start(const char *line)
 {
-	return starts_with(line, PGP_SIGNATURE) ||
-		starts_with(line, PGP_MESSAGE);
+	return (get_format_data_by_sig(line) != NULL);
 }
 
 size_t parse_signature(const char *buf, size_t size)
@@ -132,6 +165,8 @@ void set_signing_key(const char *key)
 
 int git_gpg_config(const char *var, const char *value, void *cb)
 {
+	int i, j;
+
 	if (!strcmp(var, "user.signingkey")) {
 		if (!value)
 			return config_error_nonbool(var);
@@ -140,18 +175,20 @@ int git_gpg_config(const char *var, const char *value, void *cb)
 	}
 
 	if (!strcmp(var, "gpg.format")) {
-		if (!strcmp(value, "PGP"))
+		j = 0;
+		for (i = 0; i < ARRAY_SIZE(gpg_formats); i++)
+			if (!strcmp(value, gpg_formats[i].format)) {
+				j++;
+				break;
+			}
+		if (!j)
 			return error("malformed value for %s: %s", var, value);
 		return git_config_string(&gpg_format, var, value);
 	}
 
-	if (!strcmp(var, "gpg.program")) {
-		if (!value)
-			return config_error_nonbool(var);
-		gpg_program = xstrdup(value);
-		return 0;
-	}
-
+	if (!strcmp(var, "gpg.program"))
+		return git_config_string(&gpg_formats[PGP_FMT].program, var,
+					 value);
 	return 0;
 }
 
@@ -165,12 +202,14 @@ const char *get_signing_key(void)
 int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
 {
 	struct child_process gpg = CHILD_PROCESS_INIT;
+	struct gpg_format_data *fmt;
 	int ret;
 	size_t i, j, bottom;
 	struct strbuf gpg_status = STRBUF_INIT;
 
+	fmt = get_format_data();
 	argv_array_pushl(&gpg.args,
-			 gpg_program,
+			 fmt->program,
 			 "--status-fd=2",
 			 "-bsau", signing_key,
 			 NULL);
@@ -208,8 +247,9 @@ int verify_signed_buffer(const char *payload, size_t payload_size,
 			 struct strbuf *gpg_output, struct strbuf *gpg_status)
 {
 	struct child_process gpg = CHILD_PROCESS_INIT;
+	struct gpg_format_data *fmt;
 	struct tempfile *temp;
-	int ret;
+	int ret, i;
 	struct strbuf buf = STRBUF_INIT;
 
 	temp = mks_tempfile_t(".git_vtag_tmpXXXXXX");
@@ -223,10 +263,18 @@ int verify_signed_buffer(const char *payload, size_t payload_size,
 		return -1;
 	}
 
+	fmt = get_format_data_by_sig(signature);
+	assert(fmt);
+
+	argv_array_pushl(&gpg.args,
+			 fmt->program, NULL);
+	for (i = 0; i < ARRAY_SIZE(fmt->extra_args_verify); i++)
+		if (fmt->extra_args_verify[i])
+			argv_array_pushl(&gpg.args,
+					 fmt->extra_args_verify[i], NULL);
+
 	argv_array_pushl(&gpg.args,
-			 gpg_program,
 			 "--status-fd=1",
-			 "--keyid-format=long",
 			 "--verify", temp->filename.buf, "-",
 			 NULL);
 
-- 
2.16.4


  parent reply	other threads:[~2018-07-03 12:57 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-03 12:38 [PATCH 0/8] X509 (gpgsm) commit signing support Henning Schild
2018-07-03 12:38 ` [PATCH 1/8] builtin/receive-pack: use check_signature from gpg-interface Henning Schild
2018-07-06 19:51   ` Junio C Hamano
2018-07-06 21:35     ` Junio C Hamano
2018-07-09  8:18       ` Henning Schild
2018-07-09 15:55         ` Junio C Hamano
2018-07-03 12:38 ` [PATCH 2/8] gpg-interface: make parse_gpg_output static and remove from interface header Henning Schild
2018-07-03 12:38 ` [PATCH 3/8] gpg-interface: add new config to select how to sign a commit Henning Schild
2018-07-06  1:01   ` brian m. carlson
2018-07-06  8:02     ` Henning Schild
2018-07-06 19:58     ` Junio C Hamano
2018-07-03 12:38 ` Henning Schild [this message]
2018-07-04  7:10   ` [PATCH 4/8] gpg-interface: introduce an abstraction for multiple gpg formats Martin Ågren
2018-07-05 13:21     ` Henning Schild
2018-07-06 17:24     ` Junio C Hamano
2018-07-09  8:21       ` Henning Schild
2018-07-09  8:44         ` Eric Sunshine
2018-07-09 15:47           ` Junio C Hamano
2018-07-10 15:37       ` Jeff King
2018-07-10 15:51         ` Junio C Hamano
2018-07-10 15:58         ` Junio C Hamano
2018-07-10 17:15           ` Jeff King
2018-07-03 12:38 ` [PATCH 5/8] t/t7510: check the validation of the new config gpg.format Henning Schild
2018-07-06 20:21   ` Junio C Hamano
2018-07-09  8:27     ` Henning Schild
2018-07-03 12:38 ` [PATCH 6/8] gpg-interface: do not hardcode the key string len anymore Henning Schild
2018-07-06 20:22   ` Junio C Hamano
2018-07-03 12:38 ` [PATCH 7/8] gpg-interface: introduce new signature format "X509" using gpgsm Henning Schild
2018-07-06  1:10   ` brian m. carlson
2018-07-06  8:01     ` Henning Schild
2018-07-06 20:34   ` Junio C Hamano
2018-07-03 12:38 ` [PATCH 8/8] gpg-interface t: extend the existing GPG tests with GPGSM Henning Schild
2018-07-06  1:14   ` brian m. carlson
2018-07-06  8:01     ` Henning Schild
2018-07-06  1:18 ` [PATCH 0/8] X509 (gpgsm) commit signing support brian m. carlson
2018-07-06  8:01   ` Henning Schild

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=db46732d1cd09a71200e00c2d09605d5beaff364.1530616446.git.henning.schild@siemens.com \
    --to=henning.schild@siemens.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=mastahyeti@gmail.com \
    --cc=me@ttaylorr.com \
    --cc=peff@peff.net \
    --cc=sandals@crustytoothpaste.net \
    --cc=sunshine@sunshineco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).