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

[PATCH v8 3/9] ssh signing: add ssh key format and signing code

From
Fabian Stelzer via GitGitGadget <gitgitgadget@gmail.com>
Date
Sep 10, 2021, 20:07 UTC
Message-ID
<c1e9bba8da09d464739e3a1e192d1d9c4cd29e24.1631304462.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1041.v8.git.git.1631304462.gitgitgadget@gmail.com>
From: Fabian Stelzer <fs@gigacodes.de>

Implements the actual sign_buffer_ssh operation and move some shared cleanup code into a strbuf function

Set gpg.format = ssh and user.signingkey to either a ssh public key string (like from an authorized_keys file), or a ssh key file. If the key file or the config value itself contains only a public key then the private key needs to be available via ssh-agent.

gpg.ssh.program can be set to an alternative location of ssh-keygen. A somewhat recent openssh version (8.2p1+) of ssh-keygen is needed for this feature. Since only ssh-keygen is needed it can this way be installed seperately without upgrading your system openssh packages.

Signed-off-by: Fabian Stelzer <fs@gigacodes.de>
---
 Documentation/config/gpg.txt  |   4 +-
 Documentation/config/user.txt |   5 ++
 gpg-interface.c               | 138 ++++++++++++++++++++++++++++++++--
 3 files changed, 137 insertions(+), 10 deletions(-)
diff --git a/Documentation/config/gpg.txt b/Documentation/config/gpg.txt
index d94025cb368..88531b15f0f 100644
--- a/Documentation/config/gpg.txt
+++ b/Documentation/config/gpg.txt
@@ -11,13 +11,13 @@ gpg.program::
 
 gpg.format::
 	Specifies which key format to use when signing with `--gpg-sign`.
-	Default is "openpgp" and another possible value is "x509".
+	Default is "openpgp". Other possible values are "x509", "ssh".
 
 gpg.<format>.program::
 	Use this to customize the program used for the signing format you
 	chose. (see `gpg.program` and `gpg.format`) `gpg.program` can still
 	be used as a legacy synonym for `gpg.openpgp.program`. The default
-	value for `gpg.x509.program` is "gpgsm".
+	value for `gpg.x509.program` is "gpgsm" and `gpg.ssh.program` is "ssh-keygen".
 
 gpg.minTrustLevel::
 	Specifies a minimum trust level for signature verification.  If
diff --git a/Documentation/config/user.txt b/Documentation/config/user.txt
index 59aec7c3aed..2155128957c 100644
--- a/Documentation/config/user.txt
+++ b/Documentation/config/user.txt
@@ -36,3 +36,8 @@ user.signingKey::
 	commit, you can override the default selection with this variable.
 	This option is passed unchanged to gpg's --local-user parameter,
 	so you may specify a key using any method that gpg supports.
+	If gpg.format is set to "ssh" this can contain the literal ssh public
+	key (e.g.: "ssh-rsa XXXXXX identifier") or a file which contains it and
+	corresponds to the private key used for signing. The private key
+	needs to be available via ssh-agent. Alternatively it can be set to
+	a file containing a private key directly.
diff --git a/gpg-interface.c b/gpg-interface.c
index db54b054162..7ca682ac6d6 100644
--- a/gpg-interface.c
+++ b/gpg-interface.c
@@ -41,12 +41,20 @@ static const char *x509_sigs[] = {
 	NULL
 };
 
+static const char *ssh_verify_args[] = { NULL };
+static const char *ssh_sigs[] = {
+	"-----BEGIN SSH SIGNATURE-----",
+	NULL
+};
+
 static int verify_gpg_signed_buffer(struct signature_check *sigc,
 				    struct gpg_format *fmt, const char *payload,
 				    size_t payload_size, const char *signature,
 				    size_t signature_size);
 static int sign_buffer_gpg(struct strbuf *buffer, struct strbuf *signature,
 			   const char *signing_key);
+static int sign_buffer_ssh(struct strbuf *buffer, struct strbuf *signature,
+			   const char *signing_key);
 
 static struct gpg_format gpg_format[] = {
 	{
@@ -65,6 +73,14 @@ static struct gpg_format gpg_format[] = {
 		.verify_signed_buffer = verify_gpg_signed_buffer,
 		.sign_buffer = sign_buffer_gpg,
 	},
+	{
+		.name = "ssh",
+		.program = "ssh-keygen",
+		.verify_args = ssh_verify_args,
+		.sigs = ssh_sigs,
+		.verify_signed_buffer = NULL, /* TODO */
+		.sign_buffer = sign_buffer_ssh
+	},
 };
 
 static struct gpg_format *use_format = &gpg_format[0];
@@ -443,6 +459,9 @@ int git_gpg_config(const char *var, const char *value, void *cb)
 	if (!strcmp(var, "gpg.x509.program"))
 		fmtname = "x509";
 
+	if (!strcmp(var, "gpg.ssh.program"))
+		fmtname = "ssh";
+
 	if (fmtname) {
 		fmt = get_format_by_name(fmtname);
 		return git_config_string(&fmt->program, var, value);
@@ -463,12 +482,30 @@ int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *sig
 	return use_format->sign_buffer(buffer, signature, signing_key);
 }
 
+/*
+ * Strip CR from the line endings, in case we are on Windows.
+ * NEEDSWORK: make it trim only CRs before LFs and rename
+ */
+static void remove_cr_after(struct strbuf *buffer, size_t offset)
+{
+	size_t i, j;
+
+	for (i = j = offset; i < buffer->len; i++) {
+		if (buffer->buf[i] != '\r') {
+			if (i != j)
+				buffer->buf[j] = buffer->buf[i];
+			j++;
+		}
+	}
+	strbuf_setlen(buffer, j);
+}
+
 static int sign_buffer_gpg(struct strbuf *buffer, struct strbuf *signature,
 			  const char *signing_key)
 {
 	struct child_process gpg = CHILD_PROCESS_INIT;
 	int ret;
-	size_t i, j, bottom;
+	size_t bottom;
 	struct strbuf gpg_status = STRBUF_INIT;
 
 	strvec_pushl(&gpg.args,
@@ -494,13 +531,98 @@ static int sign_buffer_gpg(struct strbuf *buffer, struct strbuf *signature,
 		return error(_("gpg failed to sign the data"));
 
 	/* Strip CR from the line endings, in case we are on Windows. */
-	for (i = j = bottom; i < signature->len; i++)
-		if (signature->buf[i] != '\r') {
-			if (i != j)
-				signature->buf[j] = signature->buf[i];
-			j++;
-		}
-	strbuf_setlen(signature, j);
+	remove_cr_after(signature, bottom);
 
 	return 0;
 }
+
+static int sign_buffer_ssh(struct strbuf *buffer, struct strbuf *signature,
+			   const char *signing_key)
+{
+	struct child_process signer = CHILD_PROCESS_INIT;
+	int ret = -1;
+	size_t bottom, keylen;
+	struct strbuf signer_stderr = STRBUF_INIT;
+	struct tempfile *key_file = NULL, *buffer_file = NULL;
+	char *ssh_signing_key_file = NULL;
+	struct strbuf ssh_signature_filename = STRBUF_INIT;
+
+	if (!signing_key || signing_key[0] == '\0')
+		return error(
+			_("user.signingkey needs to be set for ssh signing"));
+
+	if (starts_with(signing_key, "ssh-")) {
+		/* A literal ssh key */
+		key_file = mks_tempfile_t(".git_signing_key_tmpXXXXXX");
+		if (!key_file)
+			return error_errno(
+				_("could not create temporary file"));
+		keylen = strlen(signing_key);
+		if (write_in_full(key_file->fd, signing_key, keylen) < 0 ||
+		    close_tempfile_gently(key_file) < 0) {
+			error_errno(_("failed writing ssh signing key to '%s'"),
+				    key_file->filename.buf);
+			goto out;
+		}
+		ssh_signing_key_file = strbuf_detach(&key_file->filename, NULL);
+	} else {
+		/* We assume a file */
+		ssh_signing_key_file = expand_user_path(signing_key, 1);
+	}
+
+	buffer_file = mks_tempfile_t(".git_signing_buffer_tmpXXXXXX");
+	if (!buffer_file) {
+		error_errno(_("could not create temporary file"));
+		goto out;
+	}
+
+	if (write_in_full(buffer_file->fd, buffer->buf, buffer->len) < 0 ||
+	    close_tempfile_gently(buffer_file) < 0) {
+		error_errno(_("failed writing ssh signing key buffer to '%s'"),
+			    buffer_file->filename.buf);
+		goto out;
+	}
+
+	strvec_pushl(&signer.args, use_format->program,
+		     "-Y", "sign",
+		     "-n", "git",
+		     "-f", ssh_signing_key_file,
+		     buffer_file->filename.buf,
+		     NULL);
+
+	sigchain_push(SIGPIPE, SIG_IGN);
+	ret = pipe_command(&signer, NULL, 0, NULL, 0, &signer_stderr, 0);
+	sigchain_pop(SIGPIPE);
+
+	if (ret) {
+		if (strstr(signer_stderr.buf, "usage:"))
+			error(_("ssh-keygen -Y sign is needed for ssh signing (available in openssh version 8.2p1+)"));
+
+		error("%s", signer_stderr.buf);
+		goto out;
+	}
+
+	bottom = signature->len;
+
+	strbuf_addbuf(&ssh_signature_filename, &buffer_file->filename);
+	strbuf_addstr(&ssh_signature_filename, ".sig");
+	if (strbuf_read_file(signature, ssh_signature_filename.buf, 0) < 0) {
+		error_errno(
+			_("failed reading ssh signing data buffer from '%s'"),
+			ssh_signature_filename.buf);
+	}
+	unlink_or_warn(ssh_signature_filename.buf);
+
+	/* Strip CR from the line endings, in case we are on Windows. */
+	remove_cr_after(signature, bottom);
+
+out:
+	if (key_file)
+		delete_tempfile(&key_file);
+	if (buffer_file)
+		delete_tempfile(&buffer_file);
+	strbuf_release(&signer_stderr);
+	strbuf_release(&ssh_signature_filename);
+	FREE_AND_NULL(ssh_signing_key_file);
+	return ret;
+}
-- 
gitgitgadget
Previous: Fabian Stelzer via GitGitGadgetNext: Fabian Stelzer via GitGitGadget
Message 139 of 153 in “Add commit & tag signing/verification via SSH keys using ssh-keygen”
  1. Add commit & tag signing/verification via SSH keys using ssh-keygenFabian Stelzer via GitGitGadget, Jul 6, 2021
  2. Han-Wen NienhuysJul 6, 2021
  3. Fabian StelzerJul 6, 2021
  4. brian m. carlsonJul 6, 2021
  5. Fabian StelzerJul 6, 2021
  6. Junio C HamanoJul 6, 2021
  7. Fabian StelzerJul 6, 2021
  8. Junio C HamanoJul 6, 2021
  9. Randall S. BeckerJul 6, 2021
  10. Bagas SanjayaJul 7, 2021
  11. Fabian StelzerJul 7, 2021
  12. Add commit, tag & push signing/verification via SSH keys using ssh-keygenFabian Stelzer via GitGitGadget, Jul 12, 2021
  13. Ævar Arnfjörð BjarmasonJul 12, 2021
  14. Fabian StelzerJul 12, 2021
  15. Felipe ContrerasJul 12, 2021
  16. 0/9 RFC: Add commit & tag signing/verification via SSH keys using ssh-keygenFabian Stelzer via GitGitGadget, Jul 14, 2021
  17. 2/9 ssh signing: add documentationFabian Stelzer via GitGitGadget, Jul 14, 2021
  18. Junio C HamanoJul 14, 2021
  19. Fabian StelzerJul 15, 2021
  20. Bagas SanjayaJul 15, 2021
  21. Junio C HamanoJul 15, 2021
  22. 1/9 Add commit, tag & push signing via SSH keysFabian Stelzer via GitGitGadget, Jul 14, 2021
  23. Junio C HamanoJul 14, 2021
  24. Eric SunshineJul 14, 2021
  25. Fabian StelzerJul 15, 2021
  26. 3/9 ssh signing: retrieve a default key from ssh-agentFabian Stelzer via GitGitGadget, Jul 14, 2021
  27. Junio C HamanoJul 14, 2021
  28. Han-Wen NienhuysJul 15, 2021
  29. Fabian StelzerJul 15, 2021
  30. Fabian StelzerJul 15, 2021
  31. 5/9 ssh signing: provide a textual representation of the signing keyFabian Stelzer via GitGitGadget, Jul 14, 2021
  32. 4/9 ssh signing: sign using either gpg or ssh keysFabian Stelzer via GitGitGadget, Jul 14, 2021
  33. Junio C HamanoJul 14, 2021
  34. Fabian StelzerJul 15, 2021
  35. 6/9 ssh signing: parse ssh-keygen output and verify signaturesFabian Stelzer via GitGitGadget, Jul 14, 2021
  36. Gwyneth MorganJul 16, 2021
  37. Fabian StelzerJul 16, 2021
  38. 7/9 ssh signing: add test prereqsFabian Stelzer via GitGitGadget, Jul 14, 2021
  39. 8/9 ssh signing: duplicate t7510 tests for commitsFabian Stelzer via GitGitGadget, Jul 14, 2021
  40. 9/9 ssh signing: add more tests for logs, tags & push certsFabian Stelzer via GitGitGadget, Jul 14, 2021
  41. 0/9 ssh signing: Add commit & tag signing/verification via SSH keys using ssh-keygenFabian Stelzer via GitGitGadget, Jul 19, 2021
  42. 1/9 ssh signing: preliminary refactoring and clean-upFabian Stelzer via GitGitGadget, Jul 19, 2021
  43. Junio C HamanoJul 19, 2021
  44. 2/9 ssh signing: add ssh signature format and signing using ssh keysFabian Stelzer via GitGitGadget, Jul 19, 2021
  45. Junio C HamanoJul 19, 2021
  46. Fabian StelzerJul 20, 2021
  47. 3/9 ssh signing: retrieve a default key from ssh-agentFabian Stelzer via GitGitGadget, Jul 19, 2021
  48. 4/9 ssh signing: provide a textual representation of the signing keyFabian Stelzer via GitGitGadget, Jul 19, 2021
  49. 5/9 ssh signing: parse ssh-keygen output and verify signaturesFabian Stelzer via GitGitGadget, Jul 19, 2021
  50. 6/9 ssh signing: add test prereqsFabian Stelzer via GitGitGadget, Jul 19, 2021
  51. 7/9 ssh signing: duplicate t7510 tests for commitsFabian Stelzer via GitGitGadget, Jul 19, 2021
  52. 8/9 ssh signing: add more tests for logs, tags & push certsFabian Stelzer via GitGitGadget, Jul 19, 2021
  53. 9/9 ssh signing: add documentationFabian Stelzer via GitGitGadget, Jul 19, 2021
  54. Junio C HamanoJul 20, 2021
  55. 0/9 ssh signing: Add commit & tag signing/verification via SSH keys using ssh-keygenFabian Stelzer via GitGitGadget, Jul 27, 2021
  56. 1/9 ssh signing: preliminary refactoring and clean-upFabian Stelzer via GitGitGadget, Jul 27, 2021
  57. 2/9 ssh signing: add ssh signature format and signing using ssh keysFabian Stelzer via GitGitGadget, Jul 27, 2021
  58. 4/9 ssh signing: provide a textual representation of the signing keyFabian Stelzer via GitGitGadget, Jul 27, 2021
  59. 3/9 ssh signing: retrieve a default key from ssh-agentFabian Stelzer via GitGitGadget, Jul 27, 2021
  60. 5/9 ssh signing: parse ssh-keygen output and verify signaturesFabian Stelzer via GitGitGadget, Jul 27, 2021
  61. 6/9 ssh signing: add test prereqsFabian Stelzer via GitGitGadget, Jul 27, 2021
  62. 7/9 ssh signing: duplicate t7510 tests for commitsFabian Stelzer via GitGitGadget, Jul 27, 2021
  63. 8/9 ssh signing: add more tests for logs, tags & push certsFabian Stelzer via GitGitGadget, Jul 27, 2021
  64. 9/9 ssh signing: add documentationFabian Stelzer via GitGitGadget, Jul 27, 2021
  65. 0/9 ssh signing: Add commit & tag signing/verification via SSH keys using ssh-keygenFabian Stelzer via GitGitGadget, Jul 28, 2021
  66. 1/9 ssh signing: preliminary refactoring and clean-upFabian Stelzer via GitGitGadget, Jul 28, 2021
  67. Jonathan TanJul 28, 2021
  68. Junio C HamanoJul 29, 2021
  69. Fabian StelzerJul 29, 2021
  70. Fabian StelzerJul 29, 2021
  71. 2/9 ssh signing: add ssh signature format and signing using ssh keysFabian Stelzer via GitGitGadget, Jul 28, 2021
  72. Jonathan TanJul 28, 2021
  73. Junio C HamanoJul 29, 2021
  74. Fabian StelzerJul 29, 2021
  75. Josh SteadmonJul 29, 2021
  76. Fabian StelzerJul 29, 2021
  77. 3/9 ssh signing: retrieve a default key from ssh-agentFabian Stelzer via GitGitGadget, Jul 28, 2021
  78. Junio C HamanoJul 28, 2021
  79. Jonathan TanJul 28, 2021
  80. Fabian StelzerJul 29, 2021
  81. Josh SteadmonJul 29, 2021
  82. Junio C HamanoJul 29, 2021
  83. Fabian StelzerJul 29, 2021
  84. 5/9 ssh signing: parse ssh-keygen output and verify signaturesFabian Stelzer via GitGitGadget, Jul 28, 2021
  85. Junio C HamanoJul 28, 2021
  86. Fabian StelzerJul 29, 2021
  87. Junio C HamanoJul 29, 2021
  88. Jonathan TanJul 28, 2021
  89. Fabian StelzerJul 29, 2021
  90. Fabian StelzerJul 29, 2021
  91. Fabian StelzerAug 3, 2021
  92. Fabian StelzerAug 3, 2021
  93. Junio C HamanoJul 29, 2021
  94. Randall S. BeckerJul 29, 2021
  95. Fabian StelzerJul 29, 2021
  96. Randall S. BeckerJul 29, 2021
  97. Fabian StelzerJul 29, 2021
  98. Randall S. BeckerJul 29, 2021
  99. Fabian StelzerJul 30, 2021
  100. Randall S. BeckerJul 30, 2021
  101. Fabian StelzerJul 30, 2021
  102. Randall S. BeckerJul 30, 2021
  103. 6/9 ssh signing: add test prereqsFabian Stelzer via GitGitGadget, Jul 28, 2021
  104. Josh SteadmonJul 29, 2021
  105. Junio C HamanoJul 29, 2021
  106. Fabian StelzerJul 30, 2021
  107. 4/9 ssh signing: provide a textual representation of the signing keyFabian Stelzer via GitGitGadget, Jul 28, 2021
  108. Junio C HamanoJul 28, 2021
  109. Fabian StelzerJul 29, 2021
  110. 8/9 ssh signing: add more tests for logs, tags & push certsFabian Stelzer via GitGitGadget, Jul 28, 2021
  111. 9/9 ssh signing: add documentationFabian Stelzer via GitGitGadget, Jul 28, 2021
  112. 7/9 ssh signing: duplicate t7510 tests for commitsFabian Stelzer via GitGitGadget, Jul 28, 2021
  113. Bagas SanjayaJul 29, 2021
  114. Fabian StelzerJul 29, 2021
  115. 0/9 ssh signing: Add commit & tag signing/verification via SSH keys using ssh-keygenFabian Stelzer via GitGitGadget, Aug 3, 2021
  116. 1/9 ssh signing: preliminary refactoring and clean-upFabian Stelzer via GitGitGadget, Aug 3, 2021
  117. 2/9 ssh signing: add test prereqsFabian Stelzer via GitGitGadget, Aug 3, 2021
  118. 3/9 ssh signing: add ssh key format and signing codeFabian Stelzer via GitGitGadget, Aug 3, 2021
  119. 4/9 ssh signing: retrieve a default key from ssh-agentFabian Stelzer via GitGitGadget, Aug 3, 2021
  120. 5/9 ssh signing: provide a textual signing_key_idFabian Stelzer via GitGitGadget, Aug 3, 2021
  121. 7/9 ssh signing: duplicate t7510 tests for commitsFabian Stelzer via GitGitGadget, Aug 3, 2021
  122. 6/9 ssh signing: verify signatures using ssh-keygenFabian Stelzer via GitGitGadget, Aug 3, 2021
  123. Junio C HamanoAug 3, 2021
  124. Fabian StelzerAug 4, 2021
  125. Junio C HamanoAug 4, 2021
  126. 8/9 ssh signing: tests for logs, tags & push certsFabian Stelzer via GitGitGadget, Aug 3, 2021
  127. 9/9 ssh signing: test that gpg fails for unkown keysFabian Stelzer via GitGitGadget, Aug 3, 2021
  128. Junio C HamanoAug 29, 2021
  129. Gwyneth MorganAug 29, 2021
  130. Fabian StelzerAug 30, 2021
  131. Junio C HamanoSep 7, 2021
  132. Fabian StelzerSep 10, 2021
  133. Junio C HamanoSep 10, 2021
  134. Fabian StelzerSep 10, 2021
  135. Carlo ArenasSep 10, 2021
  136. 0/9 ssh signing: Add commit & tag signing/verification via SSH keys using ssh-keygenFabian Stelzer via GitGitGadget, Sep 10, 2021
  137. 1/9 ssh signing: preliminary refactoring and clean-upFabian Stelzer via GitGitGadget, Sep 10, 2021
  138. 2/9 ssh signing: add test prereqsFabian Stelzer via GitGitGadget, Sep 10, 2021
  139. 3/9 ssh signing: add ssh key format and signing codeFabian Stelzer via GitGitGadget, Sep 10, 2021
  140. 4/9 ssh signing: retrieve a default key from ssh-agentFabian Stelzer via GitGitGadget, Sep 10, 2021
  141. 5/9 ssh signing: provide a textual signing_key_idFabian Stelzer via GitGitGadget, Sep 10, 2021
  142. 6/9 ssh signing: verify signatures using ssh-keygenFabian Stelzer via GitGitGadget, Sep 10, 2021
  143. 7/9 ssh signing: duplicate t7510 tests for commitsFabian Stelzer via GitGitGadget, Sep 10, 2021
  144. 8/9 ssh signing: tests for logs, tags & push certsFabian Stelzer via GitGitGadget, Sep 10, 2021
  145. 9/9 ssh signing: test that gpg fails for unknown keysFabian Stelzer via GitGitGadget, Sep 10, 2021
  146. t7510-signed-commit.sh hangs on old gpg, regression in 1bfb57f642d (was: [PATCH v8 9/9] ssh signing: test that gpg fails for unknown keys)Ævar Arnfjörð Bjarmason, Dec 22, 2021
  147. Fabian StelzerDec 22, 2021
  148. brian m. carlsonDec 22, 2021
  149. Ævar Arnfjörð BjarmasonDec 26, 2021
  150. Fabian StelzerDec 30, 2021
  151. Junio C HamanoSep 10, 2021
  152. Fabian StelzerSep 10, 2021
  153. Junio C HamanoSep 10, 2021

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.