Skip to content

Commit e04e721

Browse files
committed
ssh/agent: validate ed25519 private key length in Add
insertKey and insertCert assume an ed25519.PrivateKey is exactly 64 bytes long: a 32-byte seed followed by a 32-byte public key. The expression []byte(k)[32:] panics with "slice bounds out of range" when a shorter slice is passed, which is reachable through the exported Add API and crashes the caller process. Reject ed25519 keys whose length is not ed25519.PrivateKeySize in all four branches (value and pointer variants of insertKey and insertCert), using the same error wording already used by parseEd25519Cert in server.go. This issue was found during a security audit by NCC Group Cryptography Services, sponsored by Teleport, and was assessed and is being fixed as a non-security bug. Change-Id: I4a053835588aad4c3482fe1ca8045542cd960500 Reviewed-on: https://go-review.googlesource.com/c/crypto/+/782423 Reviewed-by: David Chase <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]> Reviewed-by: Junyang Shao <[email protected]> LUCI-TryBot-Result: [email protected] <[email protected]>
1 parent b315afd commit e04e721

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

‎ssh/agent/client.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,9 @@ func (c *client) insertKey(s interface{}, comment string, constraints []byte) er
621621
Constraints: constraints,
622622
})
623623
case ed25519.PrivateKey:
624+
if len(k) != ed25519.PrivateKeySize {
625+
return fmt.Errorf("agent: bad ED25519 key size: %d", len(k))
626+
}
624627
req = ssh.Marshal(ed25519KeyMsg{
625628
Type: ssh.KeyAlgoED25519,
626629
Pub: []byte(k)[32:],
@@ -632,6 +635,9 @@ func (c *client) insertKey(s interface{}, comment string, constraints []byte) er
632635
// general idiom is to pass ed25519.PrivateKey by value, not by pointer.
633636
// We still support the pointer variant for backwards compatibility.
634637
case *ed25519.PrivateKey:
638+
if len(*k) != ed25519.PrivateKeySize {
639+
return fmt.Errorf("agent: bad ED25519 key size: %d", len(*k))
640+
}
635641
req = ssh.Marshal(ed25519KeyMsg{
636642
Type: ssh.KeyAlgoED25519,
637643
Pub: []byte(*k)[32:],
@@ -756,6 +762,9 @@ func (c *client) insertCert(s interface{}, cert *ssh.Certificate, comment string
756762
Constraints: constraints,
757763
})
758764
case ed25519.PrivateKey:
765+
if len(k) != ed25519.PrivateKeySize {
766+
return fmt.Errorf("agent: bad ED25519 key size: %d", len(k))
767+
}
759768
req = ssh.Marshal(ed25519CertMsg{
760769
Type: cert.Type(),
761770
CertBytes: cert.Marshal(),
@@ -768,6 +777,9 @@ func (c *client) insertCert(s interface{}, cert *ssh.Certificate, comment string
768777
// general idiom is to pass ed25519.PrivateKey by value, not by pointer.
769778
// We still support the pointer variant for backwards compatibility.
770779
case *ed25519.PrivateKey:
780+
if len(*k) != ed25519.PrivateKeySize {
781+
return fmt.Errorf("agent: bad ED25519 key size: %d", len(*k))
782+
}
771783
req = ssh.Marshal(ed25519CertMsg{
772784
Type: cert.Type(),
773785
CertBytes: cert.Marshal(),

‎ssh/agent/client_test.go‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package agent
66

77
import (
88
"bytes"
9+
"crypto/ed25519"
910
"crypto/rand"
1011
"encoding/binary"
1112
"errors"
@@ -654,3 +655,36 @@ func TestAddConstraintExtensionsWireFormat(t *testing.T) {
654655
}
655656
}
656657
}
658+
659+
func TestAddRejectsShortEd25519Key(t *testing.T) {
660+
short := ed25519.PrivateKey(make([]byte, 16))
661+
// insertCert reaches its ed25519 branch only when AddedKey.Certificate
662+
// is non-nil. The length check returns before cert.Type/Marshal run, so
663+
// an empty certificate is sufficient to drive the path.
664+
emptyCert := &ssh.Certificate{}
665+
666+
cases := []struct {
667+
name string
668+
add AddedKey
669+
}{
670+
{"insertKey value", AddedKey{PrivateKey: short}},
671+
{"insertKey pointer", AddedKey{PrivateKey: &short}},
672+
{"insertCert value", AddedKey{PrivateKey: short, Certificate: emptyCert}},
673+
{"insertCert pointer", AddedKey{PrivateKey: &short, Certificate: emptyCert}},
674+
}
675+
676+
for _, tc := range cases {
677+
t.Run(tc.name, func(t *testing.T) {
678+
client, cleanup := startKeyringAgent(t)
679+
defer cleanup()
680+
681+
err := client.Add(tc.add)
682+
if err == nil {
683+
t.Fatal("Add accepted ed25519 key shorter than 64 bytes")
684+
}
685+
if !strings.Contains(err.Error(), "bad ED25519 key size") {
686+
t.Errorf("got error %q, want substring %q", err.Error(), "bad ED25519 key size")
687+
}
688+
})
689+
}
690+
}

0 commit comments

Comments
 (0)