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

[PATCH] ssh signing: better error message when key not in agent

From
Adam Szkoda via GitGitGadget <gitgitgadget@gmail.com>
Date
Jan 18, 2023, 08:17 UTC
Message-ID
<pull.1270.git.git.1674029874363.gitgitgadget@gmail.com>
From: Adam Szkoda <adaszko@gmail.com>

When signing a commit with a SSH key, with the private key missing from ssh-agent, a confusing error message is produced:

    error: Load key
    "/var/folders/t5/cscwwl_n3n1_8_5j_00x_3t40000gn/T//.git_signing_key_tmpkArSj7":
    invalid format? fatal: failed to write commit object

The temporary file .git_signing_key_tmpkArSj7 created by git contains a valid *public* key. The error message comes from `ssh-keygen -Y sign' and is caused by a fallback mechanism in ssh-keygen whereby it tries to interpret .git_signing_key_tmpkArSj7 as a *private* key if it can't find in the agent [1]. A fix is scheduled to be released in OpenSSH 9.1. All that needs to be done is to pass an additional backward-compatible option -U to 'ssh-keygen -Y sign' call. With '-U', ssh-keygen always interprets the file as public key and expects to find the private key in the agent.

As a result, when the private key is missing from the agent, a more accurate error message gets produced:

    error: Couldn't find key in agent
[1] https://bugzilla.mindrot.org/show_bug.cgi?id=3429
Signed-off-by: Adam Szkoda <adaszko@gmail.com>
---
    ssh signing: better error message when key not in agent
    
    When signing a commit with a SSH key, with the private key missing from
    ssh-agent, a confusing error message is produced:
    
    error: Load key "/var/folders/t5/cscwwl_n3n1_8_5j_00x_3t40000gn/T//.git_signing_key_tmpkArSj7": invalid format?
    fatal: failed to write commit object
    
    
    The temporary file .git_signing_key_tmpkArSj7 created by git contains a
    valid public key. The error message comes from `ssh-keygen -Y sign' and
    is caused by a fallback mechanism in ssh-keygen whereby it tries to
    interpret .git_signing_key_tmpkArSj7 as a private key if it can't find
    in the agent [1]. A fix is scheduled to be released in OpenSSH 9.1. All
    that needs to be done is to pass an additional backward-compatible
    option -U to 'ssh-keygen -Y sign' call. With '-U', ssh-keygen always
    interprets the file as public key and expects to find the private key in
    the agent.
    
    As a result, when the private key is missing from the agent, a more
    accurate error message gets produced:
    
    error: Couldn't find key in agent
    
    
    [1] https://bugzilla.mindrot.org/show_bug.cgi?id=3429
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1270%2Fradicle-dev%2Fmaint-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1270/radicle-dev/maint-v1
Pull-Request: https://github.com/git/git/pull/1270
 gpg-interface.c | 1 +
 1 file changed, 1 insertion(+)
diff --git a/gpg-interface.c b/gpg-interface.c
index 280f1fa1a58..4a5913ae942 100644
--- a/gpg-interface.c
+++ b/gpg-interface.c
@@ -1022,6 +1022,7 @@ static int sign_buffer_ssh(struct strbuf *buffer, struct strbuf *signature,
 	strvec_pushl(&signer.args, use_format->program,
 		     "-Y", "sign",
 		     "-n", "git",
+		     "-U",
 		     "-f", ssh_signing_key_file,
 		     buffer_file->filename.buf,
 		     NULL);

base-commit: e54793a95afeea1e10de1e5ad7eab914e7416250
-- 
gitgitgadget
Next: Phillip Wood
Message 1 of 18 in “ssh signing: better error message when key not in agent”
  1. ssh signing: better error message when key not in agentAdam Szkoda via GitGitGadget, Jan 18, 2023
  2. Phillip WoodJan 18, 2023
  3. Phillip WoodJan 18, 2023
  4. Adam SzkodaJan 18, 2023
  5. Phillip WoodJan 18, 2023
  6. Fabian StelzerJan 20, 2023
  7. Phillip WoodJan 23, 2023
  8. Fabian StelzerJan 23, 2023
  9. Adam SzkodaJan 23, 2023
  10. ssh signing: better error message when key not in agentAdam Szkoda via GitGitGadget, Jan 24, 2023
  11. Junio C HamanoJan 24, 2023
  12. Adam SzkodaJan 25, 2023
  13. Junio C HamanoJan 25, 2023
  14. Junio C HamanoJan 25, 2023
  15. Eric SunshineJan 25, 2023
  16. Junio C HamanoJan 25, 2023
  17. Eric SunshineFeb 15, 2023
  18. ssh signing: better error message when key not in agentAdam Szkoda via GitGitGadget, Jan 25, 2023

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.