git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 16/18] receive-pack: GPG-validate push certificates

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 19, 2014, 22:06 UTC
Message-ID
<1408485987-3590-17-git-send-email-gitster@pobox.com>
In-Reply-To
<1408485987-3590-1-git-send-email-gitster@pobox.com>

Reusing the GPG signature check helpers we already have, verify the signature in receive-pack and give the results to the hooks via GIT_PUSH_CERT_{SIGNER,KEY,STATUS} environment variables.

Policy decisions, such as accepting or rejecting a good signature by a key that is not fully trusted, is left to the hook and kept outside of the core.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 Documentation/git-receive-pack.txt | 22 ++++++++++++++++++----
 builtin/receive-pack.c             | 29 +++++++++++++++++++++++++++++
 t/t5534-push-signed.sh             | 18 ++++++++++++++++--
 3 files changed, 63 insertions(+), 6 deletions(-)
diff --git a/Documentation/git-receive-pack.txt b/Documentation/git-receive-pack.txt
index 6c458af..a66884c 100644
--- a/Documentation/git-receive-pack.txt
+++ b/Documentation/git-receive-pack.txt
@@ -56,7 +56,21 @@ sha1-old and sha1-new should be valid objects in the repository.
 When accepting a signed push (see linkgit:git-push[1]), the signed
 push certificate is stored in a blob and an environment variable
 `GIT_PUSH_CERT` can be consulted for its object name.  See the
-description of `post-receive` hook for an example.
+description of `post-receive` hook for an example.  In addition, the
+certificate is verified using GPG and the result is exported with
+the following environment variables:
+
+GIT_PUSH_CERT_SIGNER::
+	The name and the e-mail address of the owner of the key that
+	signed the push certificate.
+
+GIT_PUSH_CERT_KEY::
+	The GPG key ID of the key that signed the push certificate.
+
+GIT_PUSH_CERT_STATUS::
+	The status of GPG verification of the push certificate,
+	using the same mnemonic as used in `%G?` format of `git log`
+	family of commands (see linkgit:git-log[1]).
 
 This hook is called before any refname is updated and before any
 fast-forward checks are performed.
@@ -106,13 +120,13 @@ the update.  Refs that were created will have sha1-old equal to
 0\{40}, otherwise sha1-old and sha1-new should be valid objects in
 the repository.
 
-The `GIT_PUSH_CERT` environment variable can be inspected, just as
+The `GIT_PUSH_CERT*` environment variables can be inspected, just as
 in `pre-receive` hook, after accepting a signed push.
 
 Using this hook, it is easy to generate mails describing the updates
 to the repository.  This example script sends one mail message per
 ref listing the commits pushed to the repository, and logs the push
-certificates of signed pushes to a file:
+certificates of signed pushes with good signatures to a file:
 
 	#!/bin/sh
 	# mail out commit update information.
@@ -129,7 +143,7 @@ certificates of signed pushes to a file:
 		mail -s "Changes to ref $ref" commit-list@mydomain
 	done
 	# log signed push certificate, if any
-	if test -n "${GIT_PUSH_CERT-}"
+	if test -n "${GIT_PUSH_CERT-}" && test ${GIT_PUSH_CERT_STATUS} = G
 	then
 		git cat-file blob ${GIT_PUSH_CERT} >>/var/log/push-log
 	fi
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index f30df8a..abdc296 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -15,6 +15,8 @@
 #include "connected.h"
 #include "argv-array.h"
 #include "version.h"
+#include "tag.h"
+#include "gpg-interface.h"
 
 static const char receive_pack_usage[] = "git receive-pack <git-dir>";
 
@@ -48,6 +50,7 @@ static int shallow_update;
 static const char *alt_shallow_file;
 static struct strbuf push_cert = STRBUF_INIT;
 static unsigned char push_cert_sha1[20];
+static struct signature_check sigcheck;
 
 static enum deny_action parse_deny_action(const char *var, const char *value)
 {
@@ -260,12 +263,38 @@ static void prepare_push_cert_sha1(struct child_process *proc)
 	struct argv_array env = ARGV_ARRAY_INIT;
 
 	if (!already_done) {
+		struct strbuf gpg_output = STRBUF_INIT;
+		struct strbuf gpg_status = STRBUF_INIT;
+		int bogs /* beginning_of_gpg_sig */;
+
 		already_done = 1;
 		if (write_sha1_file(push_cert.buf, push_cert.len, "blob", push_cert_sha1))
 			hashclr(push_cert_sha1);
+
+		memset(&sigcheck, '\0', sizeof(sigcheck));
+		sigcheck.result = 'N';
+
+		bogs = parse_signature(push_cert.buf, push_cert.len);
+		if (verify_signed_buffer(push_cert.buf, bogs,
+					 push_cert.buf + bogs, push_cert.len - bogs,
+					 &gpg_output, &gpg_status) < 0) {
+			; /* error running gpg */
+		} else {
+			sigcheck.payload = push_cert.buf;
+			sigcheck.gpg_output = gpg_output.buf;
+			sigcheck.gpg_status = gpg_status.buf;
+			parse_gpg_output(&sigcheck);
+		}
+
+		strbuf_release(&gpg_output);
+		strbuf_release(&gpg_status);
 	}
 	if (!is_null_sha1(push_cert_sha1)) {
 		argv_array_pushf(&env, "GIT_PUSH_CERT=%s", sha1_to_hex(push_cert_sha1));
+		argv_array_pushf(&env, "GIT_PUSH_CERT_SIGNER=%s", sigcheck.signer);
+		argv_array_pushf(&env, "GIT_PUSH_CERT_KEY=%s", sigcheck.key);
+		argv_array_pushf(&env, "GIT_PUSH_CERT_STATUS=%c", sigcheck.result);
+
 		proc->env = env.argv;
 	}
 }
diff --git a/t/t5534-push-signed.sh b/t/t5534-push-signed.sh
index 0eb5004..659bca0 100755
--- a/t/t5534-push-signed.sh
+++ b/t/t5534-push-signed.sh
@@ -52,12 +52,26 @@ test_expect_success GPG 'signed push sends push certificate' '
 	if test -n "${GIT_PUSH_CERT-}"
 	then
 		git cat-file blob $GIT_PUSH_CERT >../push-cert
-	fi
+	fi &&
+
+	cat >../push-cert-status <<E_O_F
+	SIGNER=${GIT_PUSH_CERT_SIGNER-nobody}
+	KEY=${GIT_PUSH_CERT_KEY-nokey}
+	STATUS=${GIT_PUSH_CERT_STATUS-nostatus}
+	E_O_F
+
+	EOF
+
+	cat >expect <<-\EOF &&
+	SIGNER=C O Mitter <committer@example.com>
+	KEY=13B6F51ECDDE430D
+	STATUS=G
 	EOF
 
 	git push --signed dst noop ff +noff &&
 	grep "$(git rev-parse noop ff) refs/heads/ff" dst/push-cert &&
-	grep "$(git rev-parse noop noff) refs/heads/noff" dst/push-cert
+	grep "$(git rev-parse noop noff) refs/heads/noff" dst/push-cert &&
+	test_cmp expect dst/push-cert-status
 '
 
 test_done
-- 
2.1.0-301-g54593e2
Previous: Junio C HamanoNext: David Turner
Message 22 of 59 in “Signed push”
  1. 00/18 Signed pushJunio C Hamano, Aug 19, 2014
  2. 01/18 receive-pack: do not overallocate command structureJunio C Hamano, Aug 19, 2014
  3. 02/18 receive-pack: parse feature request a bit earlierJunio C Hamano, Aug 19, 2014
  4. Junio C HamanoAug 19, 2014
  5. 03/18 receive-pack: do not reuse old_sha1[] to other thingsJunio C Hamano, Aug 19, 2014
  6. Junio C HamanoAug 19, 2014
  7. 04/18 receive-pack: factor out queueing of commandJunio C Hamano, Aug 19, 2014
  8. 05/18 send-pack: move REF_STATUS_REJECT_NODELETE logic a bit higherJunio C Hamano, Aug 19, 2014
  9. 06/18 send-pack: refactor decision to send update per refJunio C Hamano, Aug 19, 2014
  10. 07/18 send-pack: always send capabilitiesJunio C Hamano, Aug 19, 2014
  11. 08/18 send-pack: factor out capability string generationJunio C Hamano, Aug 19, 2014
  12. 09/18 send-pack: rename "new_refs" to "need_pack_data"Junio C Hamano, Aug 19, 2014
  13. 10/18 send-pack: refactor inspecting and resetting status and sending commandsJunio C Hamano, Aug 19, 2014
  14. 11/18 send-pack: clarify that cmds_sent is a booleanJunio C Hamano, Aug 19, 2014
  15. 12/18 gpg-interface: move parse_gpg_output() to where it should beJunio C Hamano, Aug 19, 2014
  16. 13/18 gpg-interface: move parse_signature() to where it should beJunio C Hamano, Aug 19, 2014
  17. 14/18 pack-protocol doc: typofix for PKT-LINEJunio C Hamano, Aug 19, 2014
  18. 15/18 the beginning of the signed pushJunio C Hamano, Aug 19, 2014
  19. brian m. carlsonAug 20, 2014
  20. Bert WesargAug 20, 2014
  21. Junio C HamanoAug 20, 2014
  22. 16/18 receive-pack: GPG-validate push certificatesJunio C Hamano, Aug 19, 2014
  23. David TurnerAug 20, 2014
  24. Junio C HamanoAug 20, 2014
  25. David TurnerAug 20, 2014
  26. Junio C HamanoAug 20, 2014
  27. David TurnerAug 21, 2014
  28. Junio C HamanoAug 22, 2014
  29. 17/18 send-pack: send feature request on push-cert packetJunio C Hamano, Aug 19, 2014
  30. 18/18 signed push: final protocol updateJunio C Hamano, Aug 19, 2014
  31. Shawn PearceAug 21, 2014
  32. Junio C HamanoAug 21, 2014
  33. Kyle J. McKayAug 22, 2014
  34. Junio C HamanoAug 22, 2014
  35. Shawn PearceAug 22, 2014
  36. Junio C HamanoAug 25, 2014
  37. Shawn PearceAug 26, 2014
  38. Junio C HamanoAug 26, 2014
  39. Junio C HamanoAug 26, 2014
  40. Junio C HamanoSep 4, 2014
  41. Shawn PearceSep 5, 2014
  42. Junio C HamanoAug 22, 2014
  43. David TurnerAug 22, 2014
  44. Duy NguyenAug 19, 2014
  45. Junio C HamanoAug 19, 2014
  46. Nico WilliamsAug 20, 2014
  47. Junio C HamanoAug 20, 2014
  48. Junio C HamanoAug 20, 2014
  49. Junio C HamanoAug 20, 2014
  50. Nico WilliamsAug 20, 2014
  51. Stefan BellerAug 22, 2014
  52. Junio C HamanoAug 22, 2014
  53. Stefan BellerAug 22, 2014
  54. Junio C HamanoAug 22, 2014
  55. Stefan BellerAug 22, 2014
  56. Junio C HamanoAug 22, 2014
  57. Stefan BellerAug 22, 2014
  58. Junio C HamanoAug 25, 2014
  59. Jason PyeronAug 25, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.