112 lines
2.8 KiB
Go
112 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"crypto/ecdsa"
|
|
"crypto/ed25519"
|
|
"crypto/elliptic"
|
|
"crypto/rand"
|
|
"net"
|
|
"testing"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
)
|
|
|
|
// TestHostKeyPinHoldsOnAHostWithSeveralKeys reproduces the pin failing on a
|
|
// host that also holds a key the client prefers (a typical host offers ECDSA before
|
|
// ed25519): the pinned key must be accepted, and a key that isn't pinned
|
|
// must still be refused.
|
|
func TestHostKeyPinHoldsOnAHostWithSeveralKeys(t *testing.T) {
|
|
edHost := signer(t, newEd25519(t))
|
|
ecHost := signer(t, newECDSA(t))
|
|
stranger := signer(t, newEd25519(t))
|
|
client := signer(t, newEd25519(t))
|
|
|
|
tests := []struct {
|
|
name string
|
|
pin ssh.PublicKey
|
|
wantErr bool
|
|
}{
|
|
{name: "pinned ed25519 key among several", pin: edHost.PublicKey()},
|
|
{name: "pinned ecdsa key among several", pin: ecHost.PublicKey()},
|
|
{name: "a key the host doesn't hold", pin: stranger.PublicKey(), wantErr: true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
addr := serve(t, client.PublicKey(), ecHost, edHost)
|
|
h := &host{username: "archie", signer: client}
|
|
conn, err := (&net.Dialer{}).DialContext(t.Context(), "tcp", addr)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer func() { _ = conn.Close() }()
|
|
cc, _, _, err := ssh.NewClientConn(conn, addr, h.clientConfig(Target{Name: "box", Address: addr, key: tt.pin}))
|
|
if tt.wantErr {
|
|
if err == nil {
|
|
t.Fatal("handshake accepted a host key that wasn't pinned")
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("handshake: %v", err)
|
|
}
|
|
_ = cc.Close()
|
|
})
|
|
}
|
|
}
|
|
|
|
// serve runs a one-connection SSH server holding hostKeys that accepts only
|
|
// the client key.
|
|
func serve(t *testing.T, clientKey ssh.PublicKey, hostKeys ...ssh.Signer) string {
|
|
t.Helper()
|
|
cfg := &ssh.ServerConfig{PublicKeyCallback: func(_ ssh.ConnMetadata, k ssh.PublicKey) (*ssh.Permissions, error) {
|
|
if string(k.Marshal()) == string(clientKey.Marshal()) {
|
|
return &ssh.Permissions{}, nil
|
|
}
|
|
return nil, net.UnknownNetworkError("unknown client key")
|
|
}}
|
|
for _, k := range hostKeys {
|
|
cfg.AddHostKey(k)
|
|
}
|
|
ln, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = ln.Close() })
|
|
go func() {
|
|
c, err := ln.Accept()
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer func() { _ = c.Close() }()
|
|
_, _, _, _ = ssh.NewServerConn(c, cfg)
|
|
}()
|
|
return ln.Addr().String()
|
|
}
|
|
|
|
func newEd25519(t *testing.T) ed25519.PrivateKey {
|
|
t.Helper()
|
|
_, k, err := ed25519.GenerateKey(rand.Reader)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return k
|
|
}
|
|
|
|
func newECDSA(t *testing.T) *ecdsa.PrivateKey {
|
|
t.Helper()
|
|
k, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return k
|
|
}
|
|
|
|
func signer(t *testing.T, key any) ssh.Signer {
|
|
t.Helper()
|
|
s, err := ssh.NewSignerFromKey(key)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return s
|
|
}
|