diff --git a/ssh/agent/server.go b/ssh/agent/server.go index 1be61e0268..30f2637cca 100644 --- a/ssh/agent/server.go +++ b/ssh/agent/server.go @@ -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} @@ -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 } @@ -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} diff --git a/ssh/agent/server_test.go b/ssh/agent/server_test.go index 3d4ef9275f..90a0ca695b 100644 --- a/ssh/agent/server_test.go +++ b/ssh/agent/server_test.go @@ -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" @@ -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) + } + }) + } + }) + } +} diff --git a/ssh/keys.go b/ssh/keys.go index bfcba7ecdc..18eabd801c 100644 --- a/ssh/keys.go +++ b/ssh/keys.go @@ -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") } diff --git a/ssh/keys_test.go b/ssh/keys_test.go index 20e9886fd4..223b51dbde 100644 --- a/ssh/keys_test.go +++ b/ssh/keys_test.go @@ -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