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 Signed-off-by: Adam Szkoda <adaszko@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Adam Szkoda committed Jan 25, 2023 at 12:40 UTC dce7b311266951fc2fb7a1dcbe89416ba8cebbb5
1 file changed +5 -1
gpg-interface.c
+5 -1
@@ -998,6 +998,7 @@ static int sign_buffer_ssh(struct strbuf *buffer, struct strbuf *signature,
998 char *ssh_signing_key_file = NULL;
999 struct strbuf ssh_signature_filename = STRBUF_INIT;
1000 const char *literal_key = NULL;
1001 + int literal_ssh_key = 0;
1002
1003 if (!signing_key || signing_key[0] == '\0')
1004 return error(
@@ -1005,6 +1006,7 @@ static int sign_buffer_ssh(struct strbuf *buffer, struct strbuf *signature,
1006
1007 if (is_literal_ssh_key(signing_key, &literal_key)) {
1008 /* A literal ssh key */
1009 + literal_ssh_key = 1;
1010 key_file = mks_tempfile_t(".git_signing_key_tmpXXXXXX");
1011 if (!key_file)
1012 return error_errno(
@@ -1039,8 +1041,10 @@ static int sign_buffer_ssh(struct strbuf *buffer, struct strbuf *signature,
1041 "-Y", "sign",
1042 "-n", "git",
1043 "-f", ssh_signing_key_file,
1042 - buffer_file->filename.buf,
1044 NULL);
1045 + if (literal_ssh_key)
1046 + strvec_push(&signer.args, "-U");
1047 + strvec_push(&signer.args, buffer_file->filename.buf);
1048
1049 sigchain_push(SIGPIPE, SIG_IGN);
1050 ret = pipe_command(&signer, NULL, 0, NULL, 0, &signer_stderr, 0);