Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions ssh/agent/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ func parseRSAKey(req []byte) (*AddedKey, error) {
D: k.D,
Primes: []*big.Int{k.P, k.Q},
}
if err := priv.Validate(); err != nil {
return nil, fmt.Errorf("agent: invalid RSA key: %v", err)
}
priv.Precompute()

addedKey := &AddedKey{PrivateKey: priv, Comment: k.Comments}
Expand Down Expand Up @@ -324,6 +327,15 @@ func unmarshalECDSA(curveName string, keyBytes []byte, privScalar *big.Int) (pri
return nil, errors.New("agent: point not on curve")
}

// Mirror the checks in parseOpenSSHPrivateKey: the scalar must be in
// [1, N-1] and must match the public point.
if privScalar.Sign() <= 0 || privScalar.Cmp(priv.Curve.Params().N) >= 0 {
return nil, errors.New("agent: scalar is out of range")
}
if x, y := priv.Curve.ScalarBaseMult(privScalar.Bytes()); x.Cmp(priv.X) != 0 || y.Cmp(priv.Y) != 0 {
return nil, errors.New("agent: public key does not match private key")
}

return priv, nil
}

Expand Down Expand Up @@ -408,6 +420,9 @@ func parseRSACert(req []byte) (*AddedKey, error) {
D: k.D,
Primes: []*big.Int{k.Q, k.P},
}
if err := priv.Validate(); err != nil {
return nil, fmt.Errorf("agent: invalid RSA key: %v", err)
}
priv.Precompute()

addedKey := &AddedKey{PrivateKey: &priv, Certificate: cert, Comment: k.Comments}
Expand Down
157 changes: 157 additions & 0 deletions ssh/agent/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ package agent
import (
"crypto"
"crypto/dsa"
"crypto/ecdsa"
"crypto/rand"
"crypto/rsa"
"fmt"
"io"
"math/big"
pseudorand "math/rand"
"reflect"
"strings"
Expand Down Expand Up @@ -419,3 +422,157 @@ func TestParseEd25519KeyShortPanic(t *testing.T) {
t.Error("short ed25519 key was accepted into keyring")
}
}

// signedTestCert returns the wire format of a certificate for pub, signed by
// the test CA.
func signedTestCert(t *testing.T, pub ssh.PublicKey) []byte {
t.Helper()
cert := &ssh.Certificate{
ValidPrincipals: []string{"gopher"},
Key: pub,
CertType: ssh.UserCert,
ValidBefore: ssh.CertTimeInfinity,
SignatureKey: testPublicKeys["rsa"],
}
if err := cert.SignCert(rand.Reader, testSigners["rsa"]); err != nil {
t.Fatalf("SignCert: %v", err)
}
return cert.Marshal()
}

func TestParseRSAKeyValidation(t *testing.T) {
key := testPrivateKeys["rsa"].(*rsa.PrivateKey)
other := testPrivateKeys["rsa-openssh-format"].(*rsa.PrivateKey)
huge := new(big.Int).Lsh(big.NewInt(1), 1<<20)

tests := []struct {
name string
// edit modifies a message describing a valid key.
edit func(m *rsaKeyMsg)
wantErr bool
}{
{"valid", func(m *rsaKeyMsg) {}, false},
{"P is one", func(m *rsaKeyMsg) {
m.P, m.Q = big.NewInt(1), new(big.Int).Set(m.N)
}, true},
{"N is not P*Q", func(m *rsaKeyMsg) { m.N = new(big.Int).Add(m.N, big.NewInt(2)) }, true},
{"mismatched D", func(m *rsaKeyMsg) { m.D = other.D }, true},
{"D is zero", func(m *rsaKeyMsg) { m.D = new(big.Int) }, true},
{"D exceeds N", func(m *rsaKeyMsg) { m.D = huge }, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := rsaKeyMsg{
Type: ssh.KeyAlgoRSA,
N: new(big.Int).Set(key.N),
E: big.NewInt(int64(key.E)),
D: new(big.Int).Set(key.D),
Iqmp: key.Precomputed.Qinv,
P: key.Primes[0],
Q: key.Primes[1],
Comments: "test",
}
tt.edit(&m)
_, err := parseRSAKey(ssh.Marshal(m))
if (err != nil) != tt.wantErr {
t.Errorf("parseRSAKey: err = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestParseRSACertValidation(t *testing.T) {
key := testPrivateKeys["rsa"].(*rsa.PrivateKey)
other := testPrivateKeys["rsa-openssh-format"].(*rsa.PrivateKey)
certBytes := signedTestCert(t, testPublicKeys["rsa"])

tests := []struct {
name string
certBytes []byte
edit func(m *rsaCertMsg)
wantErr bool
}{
{"valid", certBytes, func(m *rsaCertMsg) {}, false},
{"P is one", certBytes, func(m *rsaCertMsg) {
m.P, m.Q = big.NewInt(1), big.NewInt(15)
}, true},
{"private key of another RSA key", certBytes, func(m *rsaCertMsg) {
m.D, m.P, m.Q = other.D, other.Primes[0], other.Primes[1]
}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := rsaCertMsg{
Type: ssh.CertAlgoRSAv01,
CertBytes: tt.certBytes,
D: new(big.Int).Set(key.D),
Iqmp: key.Precomputed.Qinv,
P: key.Primes[0],
Q: key.Primes[1],
Comments: "test",
}
tt.edit(&m)
_, err := parseRSACert(ssh.Marshal(m))
if (err != nil) != tt.wantErr {
t.Errorf("parseRSACert: err = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestParseECDSAKeyValidation(t *testing.T) {
for _, name := range []string{"ecdsap256", "ecdsap384", "ecdsap521"} {
t.Run(name, func(t *testing.T) {
key := testPrivateKeys[name].(*ecdsa.PrivateKey)
pub, err := key.PublicKey.ECDH()
if err != nil {
t.Fatal(err)
}
sshPub, err := ssh.NewPublicKey(&key.PublicKey)
if err != nil {
t.Fatal(err)
}
curve := strings.TrimPrefix(sshPub.Type(), "ecdsa-sha2-")
n := key.Curve.Params().N
certBytes := signedTestCert(t, sshPub)

tests := []struct {
name string
d *big.Int
wantErr bool
}{
{"valid", key.D, false},
{"zero", new(big.Int), true},
{"negative", new(big.Int).Neg(key.D), true},
{"order", n, true},
{"above order", new(big.Int).Add(key.D, n), true},
{"huge", new(big.Int).Lsh(big.NewInt(1), 1<<20), true},
{"does not match public key", new(big.Int).Add(key.D, big.NewInt(1)), true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := parseECDSAKey(ssh.Marshal(ecdsaKeyMsg{
Type: sshPub.Type(),
Curve: curve,
KeyBytes: pub.Bytes(),
D: tt.d,
Comments: "test",
}))
if (err != nil) != tt.wantErr {
t.Errorf("parseECDSAKey: err = %v, wantErr %v", err, tt.wantErr)
}

_, err = parseECDSACert(ssh.Marshal(ecdsaCertMsg{
Type: sshPub.Type() + "-cert-v01@openssh.com",
CertBytes: certBytes,
D: tt.d,
Comments: "test",
}))
if (err != nil) != tt.wantErr {
t.Errorf("parseECDSACert: err = %v, wantErr %v", err, tt.wantErr)
}
})
}
})
}
}
2 changes: 1 addition & 1 deletion ssh/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -1556,7 +1556,7 @@ func parseOpenSSHPrivateKey(key []byte, decrypt openSSHDecryptFunc) (crypto.Priv
return nil, errors.New("ssh: failed to unmarshal public key")
}

if key.D.Cmp(curve.Params().N) >= 0 {
if key.D.Sign() <= 0 || key.D.Cmp(curve.Params().N) >= 0 {
return nil, errors.New("ssh: scalar is out of range")
}

Expand Down
28 changes: 28 additions & 0 deletions ssh/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,34 @@ func TestMarshalPrivateKey(t *testing.T) {
}
}

func TestParseOpenSSHECDSAScalarRange(t *testing.T) {
for _, name := range []string{"p256-openssh-format", "p384-openssh-format", "p521-openssh-format"} {
t.Run(name, func(t *testing.T) {
good := testPrivateKeys[name].(*ecdsa.PrivateKey)
n := good.Curve.Params().N

for _, tc := range []struct {
name string
d *big.Int
}{
{"negative", new(big.Int).Neg(good.D)},
{"zero", new(big.Int)},
{"order", new(big.Int).Set(n)},
{"above order", new(big.Int).Add(good.D, n)},
} {
bad := &ecdsa.PrivateKey{PublicKey: good.PublicKey, D: tc.d}
block, err := MarshalPrivateKey(bad, "")
if err != nil {
t.Fatalf("%s: MarshalPrivateKey: %v", tc.name, err)
}
if _, err := ParseRawPrivateKey(pem.EncodeToMemory(block)); err == nil {
t.Errorf("%s: ParseRawPrivateKey accepted an ECDSA key with D = %v", tc.name, tc.d)
}
}
})
}
}

func TestMarshalPrivateKeyWithPassphrase(t *testing.T) {
tests := []struct {
name string
Expand Down