ssh: verify declared key type matches decoded key in authorized_keys
ParseAuthorizedKey and ParseKnownHosts previously ignored the key type field (e.g. "ssh-rsa") in each entry, relying solely on the type information embedded within the base64-encoded key blob. For ParseAuthorizedKey this also caused a single-token option to be silently dropped: a line such as "restrict <key>" (with the key type omitted) was parsed as an unrestricted key, because the option token landed in the key type position and was discarded together with its meaning. The same happens for no-pty, no-port-forwarding and the other single-word options. OpenSSH's sshkey_read rejects such lines outright, as the option token cannot be read as a key type. OpenSSH's sshkey_read also explicitly verifies that the key type declared in the text matches the type of the parsed key, returning SSH_ERR_KEY_TYPE_MISMATCH if they differ. This change adds, in both functions, a check that the declared key type matches the decoded key's type, returning an error for malformed lines where they diverge. This mirrors the fix already applied to the ssh/knownhosts package in CL 782427. Change-Id: I9163108d964de4be8a415fe0efd68e6b71f77990 Reviewed-on: https://go-review.googlesource.com/c/crypto/+/792840 Reviewed-by: Filippo Valsorda <filippo@golang.org> LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Nicola Murino <nicola.murino@gmail.com> Reviewed-by: Neal Patel <nealpatel@google.com> Reviewed-by: Junyang Shao <shaojunyang@google.com>
This commit is contained in:
committed by
Gopher Robot
parent
0471e7969e
commit
7626c50256
@@ -848,6 +848,65 @@ func TestInvalidEntry(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthorizedKeyOptionWithoutKeyType(t *testing.T) {
|
||||
_, pubSerialized := getTestKey()
|
||||
for _, opt := range []string{"restrict", "no-pty", "no-port-forwarding", "no-agent-forwarding"} {
|
||||
// The key type ("ssh-rsa") is intentionally omitted.
|
||||
line := opt + " " + pubSerialized
|
||||
testAuthorizedKeys(t, []byte(line), []testAuthResult{
|
||||
{nil, nil, "", "", false},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthorizedKeyTypeMismatch(t *testing.T) {
|
||||
_, pubSerialized := getTestKey() // an ssh-rsa key
|
||||
lines := []string{
|
||||
"ssh-ed25519 " + pubSerialized + " user@host",
|
||||
"restrict ssh-ed25519 " + pubSerialized + " user@host",
|
||||
"rsa-sha2-512 " + pubSerialized + " user@host",
|
||||
}
|
||||
for _, line := range lines {
|
||||
testAuthorizedKeys(t, []byte(line), []testAuthResult{
|
||||
{nil, nil, "", "", false},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthorizedKeyCertificate(t *testing.T) {
|
||||
certLine := testdata.SSHCertificates["rsa-user-testcertificate"]
|
||||
|
||||
key, _, options, _, err := ParseAuthorizedKey(certLine)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseAuthorizedKey on certificate: %v", err)
|
||||
}
|
||||
if _, ok := key.(*Certificate); !ok {
|
||||
t.Fatalf("got %T, want *Certificate", key)
|
||||
}
|
||||
if len(options) != 0 {
|
||||
t.Errorf("got options %v, want none", options)
|
||||
}
|
||||
|
||||
key, _, options, _, err = ParseAuthorizedKey(append([]byte("cert-authority "), certLine...))
|
||||
if err != nil {
|
||||
t.Fatalf("ParseAuthorizedKey on cert-authority certificate: %v", err)
|
||||
}
|
||||
if _, ok := key.(*Certificate); !ok {
|
||||
t.Fatalf("got %T, want *Certificate", key)
|
||||
}
|
||||
if !reflect.DeepEqual(options, []string{"cert-authority"}) {
|
||||
t.Errorf("got options %v, want [cert-authority]", options)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthorizedKeyOptionWithKeyType(t *testing.T) {
|
||||
pub, pubSerialized := getTestKey()
|
||||
line := "restrict ssh-rsa " + pubSerialized + " user@host"
|
||||
testAuthorizedKeys(t, []byte(line), []testAuthResult{
|
||||
{pub, []string{"restrict"}, "user@host", "", true},
|
||||
})
|
||||
}
|
||||
|
||||
var knownHostsParseTests = []struct {
|
||||
input string
|
||||
err string
|
||||
@@ -927,6 +986,13 @@ var knownHostsParseTests = []struct {
|
||||
"@marker \tlocalhost,[host2:123]\tssh-rsa aabbccdd",
|
||||
"short read",
|
||||
|
||||
"", "", nil, "",
|
||||
},
|
||||
{
|
||||
// Declared key type does not match the type embedded in the blob.
|
||||
"localhost ssh-ed25519 {RSAPUB}",
|
||||
"key type mismatch",
|
||||
|
||||
"", "", nil, "",
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user