Skip to content

Commit ee1dc49

Browse files
committed
Merge branch 'fs/ssh-signing-other-keytypes'
The cryptographic signing using ssh keys can specify literal keys for keytypes whose name do not begin with the "ssh-" prefix by using the "key::" prefix mechanism (e.g. "key::ecdsa-sha2-nistp256"). * fs/ssh-signing-other-keytypes: ssh signing: make sign/amend test more resilient ssh signing: support non ssh-* keytypes
2 parents d2f0b72 + 3b4b5a7 commit ee1dc49

File tree

5 files changed

+67
-17
lines changed

5 files changed

+67
-17
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
@@ -757,22 +757,38 @@ int git_gpg_config(const char *var, const char *value, void *cb)
757757
return 0;
758758
}
759759

760+
/*
761+
* Returns 1 if `string` contains a literal ssh key, 0 otherwise
762+
* `key` will be set to the start of the actual key if a prefix is present.
763+
*/
764+
static int is_literal_ssh_key(const char *string, const char **key)
765+
{
766+
if (skip_prefix(string, "key::", key))
767+
return 1;
768+
if (starts_with(string, "ssh-")) {
769+
*key = string;
770+
return 1;
771+
}
772+
return 0;
773+
}
774+
760775
static char *get_ssh_key_fingerprint(const char *signing_key)
761776
{
762777
struct child_process ssh_keygen = CHILD_PROCESS_INIT;
763778
int ret = -1;
764779
struct strbuf fingerprint_stdout = STRBUF_INIT;
765780
struct strbuf **fingerprint;
766781
char *fingerprint_ret;
782+
const char *literal_key = NULL;
767783

768784
/*
769785
* With SSH Signing this can contain a filename or a public key
770786
* For textual representation we usually want a fingerprint
771787
*/
772-
if (starts_with(signing_key, "ssh-")) {
788+
if (is_literal_ssh_key(signing_key, &literal_key)) {
773789
strvec_pushl(&ssh_keygen.args, "ssh-keygen", "-lf", "-", NULL);
774-
ret = pipe_command(&ssh_keygen, signing_key,
775-
strlen(signing_key), &fingerprint_stdout, 0,
790+
ret = pipe_command(&ssh_keygen, literal_key,
791+
strlen(literal_key), &fingerprint_stdout, 0,
776792
NULL, 0);
777793
} else {
778794
strvec_pushl(&ssh_keygen.args, "ssh-keygen", "-lf",
@@ -807,6 +823,7 @@ static const char *get_default_ssh_signing_key(void)
807823
const char **argv;
808824
int n;
809825
char *default_key = NULL;
826+
const char *literal_key = NULL;
810827

811828
if (!ssh_default_key_command)
812829
die(_("either user.signingkey or gpg.ssh.defaultKeyCommand needs to be configured"));
@@ -824,7 +841,11 @@ static const char *get_default_ssh_signing_key(void)
824841

825842
if (!ret) {
826843
keys = strbuf_split_max(&key_stdout, '\n', 2);
827-
if (keys[0] && starts_with(keys[0]->buf, "ssh-")) {
844+
if (keys[0] && is_literal_ssh_key(keys[0]->buf, &literal_key)) {
845+
/*
846+
* We only use `is_literal_ssh_key` here to check validity
847+
* The prefix will be stripped when the key is used.
848+
*/
828849
default_key = strbuf_detach(keys[0], NULL);
829850
} else {
830851
warning(_("gpg.ssh.defaultKeyCommand succeeded but returned no keys: %s %s"),
@@ -939,19 +960,20 @@ static int sign_buffer_ssh(struct strbuf *buffer, struct strbuf *signature,
939960
struct tempfile *key_file = NULL, *buffer_file = NULL;
940961
char *ssh_signing_key_file = NULL;
941962
struct strbuf ssh_signature_filename = STRBUF_INIT;
963+
const char *literal_key = NULL;
942964

943965
if (!signing_key || signing_key[0] == '\0')
944966
return error(
945967
_("user.signingkey needs to be set for ssh signing"));
946968

947-
if (starts_with(signing_key, "ssh-")) {
969+
if (is_literal_ssh_key(signing_key, &literal_key)) {
948970
/* A literal ssh key */
949971
key_file = mks_tempfile_t(".git_signing_key_tmpXXXXXX");
950972
if (!key_file)
951973
return error_errno(
952974
_("could not create temporary file"));
953-
keylen = strlen(signing_key);
954-
if (write_in_full(key_file->fd, signing_key, keylen) < 0 ||
975+
keylen = strlen(literal_key);
976+
if (write_in_full(key_file->fd, literal_key, keylen) < 0 ||
955977
close_tempfile_gently(key_file) < 0) {
956978
error_errno(_("failed writing ssh signing key to '%s'"),
957979
key_file->filename.buf);

t/lib-gpg.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ GPGSSH_KEY_NOTYETVALID="${GNUPGHOME}/notyetvalid_ssh_signing_key"
9595
GPGSSH_KEY_TIMEBOXEDVALID="${GNUPGHOME}/timeboxed_valid_ssh_signing_key"
9696
GPGSSH_KEY_TIMEBOXEDINVALID="${GNUPGHOME}/timeboxed_invalid_ssh_signing_key"
9797
GPGSSH_KEY_WITH_PASSPHRASE="${GNUPGHOME}/protected_ssh_signing_key"
98+
GPGSSH_KEY_ECDSA="${GNUPGHOME}/ecdsa_ssh_signing_key"
9899
GPGSSH_KEY_PASSPHRASE="super_secret"
99100
GPGSSH_ALLOWED_SIGNERS="${GNUPGHOME}/ssh.all_valid.allowedSignersFile"
100101

@@ -116,12 +117,14 @@ test_lazy_prereq GPGSSH '
116117
ssh-keygen -t ed25519 -N "" -C "git ed25519 key" -f "${GPGSSH_KEY_PRIMARY}" >/dev/null &&
117118
ssh-keygen -t rsa -b 2048 -N "" -C "git rsa2048 key" -f "${GPGSSH_KEY_SECONDARY}" >/dev/null &&
118119
ssh-keygen -t ed25519 -N "${GPGSSH_KEY_PASSPHRASE}" -C "git ed25519 encrypted key" -f "${GPGSSH_KEY_WITH_PASSPHRASE}" >/dev/null &&
120+
ssh-keygen -t ecdsa -N "" -f "${GPGSSH_KEY_ECDSA}" >/dev/null &&
119121
ssh-keygen -t ed25519 -N "" -C "git ed25519 key" -f "${GPGSSH_KEY_UNTRUSTED}" >/dev/null &&
120122
121123
cat >"${GPGSSH_ALLOWED_SIGNERS}" <<-EOF &&
122124
"principal with number 1" $(cat "${GPGSSH_KEY_PRIMARY}.pub")"
123125
"principal with number 2" $(cat "${GPGSSH_KEY_SECONDARY}.pub")"
124126
"principal with number 3" $(cat "${GPGSSH_KEY_WITH_PASSPHRASE}.pub")"
127+
"principal with number 4" $(cat "${GPGSSH_KEY_ECDSA}.pub")"
125128
EOF
126129
127130
# Verify if at least one key and ssh-keygen works as expected

t/t7510-signed-commit.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ test_expect_success GPG 'detect fudged signature with NUL' '
228228
'
229229

230230
test_expect_success GPG 'amending already signed commit' '
231-
git checkout fourth-signed^0 &&
231+
git checkout -f fourth-signed^0 &&
232232
git commit --amend -S --no-edit &&
233233
git verify-commit HEAD &&
234234
git show -s --show-signature HEAD >actual &&

t/t7528-signed-commit-ssh.sh

Lines changed: 24 additions & 2 deletions
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,GPGSSH_VERIFYTIME 'create signed commits with keys having defined lifetimes' '
@@ -259,7 +281,7 @@ test_expect_success GPGSSH 'amending already signed commit' '
259281
test_config gpg.format ssh &&
260282
test_config user.signingkey "${GPGSSH_KEY_PRIMARY}" &&
261283
test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
262-
git checkout fourth-signed^0 &&
284+
git checkout -f fourth-signed^0 &&
263285
git commit --amend -S --no-edit &&
264286
git verify-commit HEAD &&
265287
git show -s --show-signature HEAD >actual &&

0 commit comments

Comments
 (0)