Skip to content

Commit 350a251

Browse files
FStelzergitster
authored andcommitted
ssh signing: support non ssh-* keytypes
The user.signingKey config for ssh signing supports either a path to a file containing the key or for the sake of convenience a literal string with the ssh public key. To differentiate between those two cases we check if the first few characters contain "ssh-" which is unlikely to be the start of a path. ssh supports other key types which are not prefixed with "ssh-" and will currently be treated as a file path and therefore fail to load. To remedy this we move the prefix check into its own function and introduce the prefix `key::` for literal ssh keys. This way we don't need to add new key types when they become available. The existing `ssh-` prefix is retained for compatibility with current user configs but removed from the official documentation to discourage its use. Signed-off-by: Fabian Stelzer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cd3e606 commit 350a251

File tree

4 files changed

+65
-15
lines changed

4 files changed

+65
-15
lines changed

Documentation/config/user.txt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,13 @@ user.signingKey::
3636
commit, you can override the default selection with this variable.
3737
This option is passed unchanged to gpg's --local-user parameter,
3838
so you may specify a key using any method that gpg supports.
39-
If gpg.format is set to "ssh" this can contain the literal ssh public
40-
key (e.g.: "ssh-rsa XXXXXX identifier") or a file which contains it and
41-
corresponds to the private key used for signing. The private key
42-
needs to be available via ssh-agent. Alternatively it can be set to
43-
a file containing a private key directly. If not set git will call
44-
gpg.ssh.defaultKeyCommand (e.g.: "ssh-add -L") and try to use the first
45-
key available.
39+
If gpg.format is set to `ssh` this can contain the path to either
40+
your private ssh key or the public key when ssh-agent is used.
41+
Alternatively it can contain a public key prefixed with `key::`
42+
directly (e.g.: "key::ssh-rsa XXXXXX identifier"). The private key
43+
needs to be available via ssh-agent. If not set git will call
44+
gpg.ssh.defaultKeyCommand (e.g.: "ssh-add -L") and try to use the
45+
first key available. For backward compatibility, a raw key which
46+
begins with "ssh-", such as "ssh-rsa XXXXXX identifier", is treated
47+
as "key::ssh-rsa XXXXXX identifier", but this form is deprecated;
48+
use the `key::` form instead.

gpg-interface.c

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -707,22 +707,38 @@ int git_gpg_config(const char *var, const char *value, void *cb)
707707
return 0;
708708
}
709709

710+
/*
711+
* Returns 1 if `string` contains a literal ssh key, 0 otherwise
712+
* `key` will be set to the start of the actual key if a prefix is present.
713+
*/
714+
static int is_literal_ssh_key(const char *string, const char **key)
715+
{
716+
if (skip_prefix(string, "key::", key))
717+
return 1;
718+
if (starts_with(string, "ssh-")) {
719+
*key = string;
720+
return 1;
721+
}
722+
return 0;
723+
}
724+
710725
static char *get_ssh_key_fingerprint(const char *signing_key)
711726
{
712727
struct child_process ssh_keygen = CHILD_PROCESS_INIT;
713728
int ret = -1;
714729
struct strbuf fingerprint_stdout = STRBUF_INIT;
715730
struct strbuf **fingerprint;
716731
char *fingerprint_ret;
732+
const char *literal_key = NULL;
717733

718734
/*
719735
* With SSH Signing this can contain a filename or a public key
720736
* For textual representation we usually want a fingerprint
721737
*/
722-
if (starts_with(signing_key, "ssh-")) {
738+
if (is_literal_ssh_key(signing_key, &literal_key)) {
723739
strvec_pushl(&ssh_keygen.args, "ssh-keygen", "-lf", "-", NULL);
724-
ret = pipe_command(&ssh_keygen, signing_key,
725-
strlen(signing_key), &fingerprint_stdout, 0,
740+
ret = pipe_command(&ssh_keygen, literal_key,
741+
strlen(literal_key), &fingerprint_stdout, 0,
726742
NULL, 0);
727743
} else {
728744
strvec_pushl(&ssh_keygen.args, "ssh-keygen", "-lf",
@@ -757,6 +773,7 @@ static const char *get_default_ssh_signing_key(void)
757773
const char **argv;
758774
int n;
759775
char *default_key = NULL;
776+
const char *literal_key = NULL;
760777

761778
if (!ssh_default_key_command)
762779
die(_("either user.signingkey or gpg.ssh.defaultKeyCommand needs to be configured"));
@@ -774,7 +791,11 @@ static const char *get_default_ssh_signing_key(void)
774791

775792
if (!ret) {
776793
keys = strbuf_split_max(&key_stdout, '\n', 2);
777-
if (keys[0] && starts_with(keys[0]->buf, "ssh-")) {
794+
if (keys[0] && is_literal_ssh_key(keys[0]->buf, &literal_key)) {
795+
/*
796+
* We only use `is_literal_ssh_key` here to check validity
797+
* The prefix will be stripped when the key is used.
798+
*/
778799
default_key = strbuf_detach(keys[0], NULL);
779800
} else {
780801
warning(_("gpg.ssh.defaultKeyCommand succeeded but returned no keys: %s %s"),
@@ -889,19 +910,20 @@ static int sign_buffer_ssh(struct strbuf *buffer, struct strbuf *signature,
889910
struct tempfile *key_file = NULL, *buffer_file = NULL;
890911
char *ssh_signing_key_file = NULL;
891912
struct strbuf ssh_signature_filename = STRBUF_INIT;
913+
const char *literal_key = NULL;
892914

893915
if (!signing_key || signing_key[0] == '\0')
894916
return error(
895917
_("user.signingkey needs to be set for ssh signing"));
896918

897-
if (starts_with(signing_key, "ssh-")) {
919+
if (is_literal_ssh_key(signing_key, &literal_key)) {
898920
/* A literal ssh key */
899921
key_file = mks_tempfile_t(".git_signing_key_tmpXXXXXX");
900922
if (!key_file)
901923
return error_errno(
902924
_("could not create temporary file"));
903-
keylen = strlen(signing_key);
904-
if (write_in_full(key_file->fd, signing_key, keylen) < 0 ||
925+
keylen = strlen(literal_key);
926+
if (write_in_full(key_file->fd, literal_key, keylen) < 0 ||
905927
close_tempfile_gently(key_file) < 0) {
906928
error_errno(_("failed writing ssh signing key to '%s'"),
907929
key_file->filename.buf);

t/lib-gpg.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ GPGSSH_KEY_PRIMARY="${GNUPGHOME}/ed25519_ssh_signing_key"
9191
GPGSSH_KEY_SECONDARY="${GNUPGHOME}/rsa_2048_ssh_signing_key"
9292
GPGSSH_KEY_UNTRUSTED="${GNUPGHOME}/untrusted_ssh_signing_key"
9393
GPGSSH_KEY_WITH_PASSPHRASE="${GNUPGHOME}/protected_ssh_signing_key"
94+
GPGSSH_KEY_ECDSA="${GNUPGHOME}/ecdsa_ssh_signing_key"
9495
GPGSSH_KEY_PASSPHRASE="super_secret"
9596
GPGSSH_ALLOWED_SIGNERS="${GNUPGHOME}/ssh.all_valid.allowedSignersFile"
9697

@@ -119,6 +120,8 @@ test_lazy_prereq GPGSSH '
119120
echo "\"principal with number 2\" $(cat "${GPGSSH_KEY_SECONDARY}.pub")" >> "${GPGSSH_ALLOWED_SIGNERS}" &&
120121
ssh-keygen -t ed25519 -N "${GPGSSH_KEY_PASSPHRASE}" -C "git ed25519 encrypted key" -f "${GPGSSH_KEY_WITH_PASSPHRASE}" >/dev/null &&
121122
echo "\"principal with number 3\" $(cat "${GPGSSH_KEY_WITH_PASSPHRASE}.pub")" >> "${GPGSSH_ALLOWED_SIGNERS}" &&
123+
ssh-keygen -t ecdsa -N "" -f "${GPGSSH_KEY_ECDSA}" >/dev/null
124+
echo "\"principal with number 4\" $(cat "${GPGSSH_KEY_ECDSA}.pub")" >> "${GPGSSH_ALLOWED_SIGNERS}" &&
122125
ssh-keygen -t ed25519 -N "" -f "${GPGSSH_KEY_UNTRUSTED}" >/dev/null
123126
'
124127

t/t7528-signed-commit-ssh.sh

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,29 @@ test_expect_success GPGSSH 'create signed commits' '
7373
git tag eleventh-signed $(cat oid) &&
7474
echo 12 | git commit-tree --gpg-sign="${GPGSSH_KEY_UNTRUSTED}" HEAD^{tree} >oid &&
7575
test_line_count = 1 oid &&
76-
git tag twelfth-signed-alt $(cat oid)
76+
git tag twelfth-signed-alt $(cat oid) &&
77+
78+
echo 13>file && test_tick && git commit -a -m thirteenth -S"${GPGSSH_KEY_ECDSA}" &&
79+
git tag thirteenth-signed-ecdsa
80+
'
81+
82+
test_expect_success GPGSSH 'sign commits using literal public keys with ssh-agent' '
83+
test_when_finished "test_unconfig commit.gpgsign" &&
84+
test_config gpg.format ssh &&
85+
eval $(ssh-agent) &&
86+
test_when_finished "kill ${SSH_AGENT_PID}" &&
87+
ssh-add "${GPGSSH_KEY_PRIMARY}" &&
88+
echo 1 >file && git add file &&
89+
git commit -a -m rsa-inline -S"$(cat "${GPGSSH_KEY_PRIMARY}.pub")" &&
90+
echo 2 >file &&
91+
test_config user.signingkey "$(cat "${GPGSSH_KEY_PRIMARY}.pub")" &&
92+
git commit -a -m rsa-config -S &&
93+
ssh-add "${GPGSSH_KEY_ECDSA}" &&
94+
echo 3 >file &&
95+
git commit -a -m ecdsa-inline -S"key::$(cat "${GPGSSH_KEY_ECDSA}.pub")" &&
96+
echo 4 >file &&
97+
test_config user.signingkey "key::$(cat "${GPGSSH_KEY_ECDSA}.pub")" &&
98+
git commit -a -m ecdsa-config -S
7799
'
78100

79101
test_expect_success GPGSSH 'verify and show signatures' '

0 commit comments

Comments
 (0)