diff --git a/AUTHORIZED_KEYS.md b/AUTHORIZED_KEYS.md index 2c0f3ba..6a15b94 100644 --- a/AUTHORIZED_KEYS.md +++ b/AUTHORIZED_KEYS.md @@ -72,7 +72,7 @@ All option values (`principals="..."`, `permitopen="..."`) must be enclosed in d > - **User Certificate Validation:** Certificates must be SSH user certificates (`ssh.UserCert`) within their `ValidAfter`/`ValidBefore` window, signed with a supported algorithm (`ssh-ed25519`, `ecdsa-sha2-*`, `rsa-sha2-256`, or `rsa-sha2-512`; `ssh-dss` and SHA-1 `ssh-rsa` are rejected). > - **Critical Options:** `source-address` is verified against the client's IP address—either the direct TCP peer of `sshterm-server`, or the client IP extracted from the `X-Forwarded-For` header when the connection arrives from a proxy configured via `--trusted-proxies`. Any other critical option (such as `force-command`) is rejected. > - **Extensions:** User certificates must include the `permit-port-forwarding` extension (included by default in both OpenSSH `ssh-keygen` and `tlsproxy` user certificates). -> - **Security Keys (`sk-ecdsa-sha2-nistp256@openssh.com` / `sk-ssh-ed25519@openssh.com`):** Native FIDO2/CTAP2 signatures (`sk-ecdsa-sha2-nistp256@openssh.com` and `sk-ssh-ed25519@openssh.com`) are supported. Browser-generated WebAuthn (`ecdsa-sk`) signatures use `webauthn-sk-ecdsa-sha2-nistp256@openssh.com`, which is not yet supported by `golang.org/x/crypto/ssh` on the server side (see [WEBAUTHN-GO.md](WEBAUTHN-GO.md)). +> - **Security Keys (`sk-ecdsa-sha2-nistp256@openssh.com` / `sk-ssh-ed25519@openssh.com`):** Native FIDO2/CTAP2 signatures (`sk-ecdsa-sha2-nistp256@openssh.com` and `sk-ssh-ed25519@openssh.com`) are supported, as well as browser-generated WebAuthn (`ecdsa-sk`) signatures, which use `webauthn-sk-ecdsa-sha2-nistp256@openssh.com` (see [WEBAUTHN-GO.md](WEBAUTHN-GO.md)). Note that because the jump host and the downstream target each establish their own SSH session, using a WebAuthn key for both hops requires authenticating with the security key or passkey twice per connection (once for the jump host and once for the target host). --- diff --git a/README.md b/README.md index c00a4e6..445b5b6 100644 --- a/README.md +++ b/README.md @@ -243,6 +243,8 @@ For this to work, you only need a configured endpoint for the *first* jump host. The hostname `internal-host.example.com` does not need its own endpoint; it is resolved from the jump host. + > **Note:** Because each hop establishes an independent SSH session with its own session ID, authenticating with a WebAuthn (`ecdsa-sk`) key requires a separate security key touch or passkey verification for each hop that uses the key (e.g., once for the jump host and once for the destination host). + ### Command Reference Here is a list of all available commands. Most commands follow a `command [arguments]` pattern. diff --git a/go/go.mod b/go/go.mod index f40c21e..2692070 100644 --- a/go/go.mod +++ b/go/go.mod @@ -5,7 +5,7 @@ go 1.27.4 godebug multipathtcp=0 require ( - github.com/c2FmZQ/webauthn v0.0.0-20261117022304-9643eefd1e03 + github.com/c2FmZQ/webauthn v0.1.0 github.com/chromedp/cdproto v0.160.3 github.com/chromedp/chromedp v0.23.1 github.com/gorilla/websocket v1.5.3 diff --git a/go/go.sum b/go/go.sum index 3808e37..e9871bb 100644 --- a/go/go.sum +++ b/go/go.sum @@ -1,5 +1,5 @@ -github.com/c2FmZQ/webauthn v0.0.0-20261117022304-9643eefd1e03 h1:vlBCrniYp+f995sGXTkJQl5353dX+JY5xbjTyukb4xc= -github.com/c2FmZQ/webauthn v0.0.0-20261117022304-9643eefd1e03/go.mod h1:wVitmVNeHvULhGba0yfU3DJUKh6u6c/6xnNpDfVfaF0= +github.com/c2FmZQ/webauthn v0.1.0 h1:Eg3YiQk+/gH2T1jPyw/hlaVK4WxLYhqTDdBwe2EKq5w= +github.com/c2FmZQ/webauthn v0.1.0/go.mod h1:wVitmVNeHvULhGba0yfU3DJUKh6u6c/6xnNpDfVfaF0= github.com/chromedp/cdproto v0.160.3 h1:IpCgfMavg4heyk4detaGEUneXjAZ7hZDwqWwYMSgaVQ= github.com/chromedp/cdproto v0.160.3/go.mod h1:h68JaIuFr9o3yiayxEYhBdvaxgYO3shDrPSYhNlyfu0= github.com/chromedp/chromedp v0.23.1 h1:CbK1Knbhe9H/m/KT3qfmuSG1c6ErR8ZjmmpHb4Fadz8= diff --git a/go/internal/app/cert_util.go b/go/internal/app/cert_util.go index 34185c1..7db471b 100644 --- a/go/internal/app/cert_util.go +++ b/go/internal/app/cert_util.go @@ -73,13 +73,15 @@ func checkCertificate(cert *ssh.Certificate, certType uint32) error { slices.Sort(opts) errs = append(errs, fmt.Errorf("certificate has critical options: %v", opts)) } - c2 := *cert - c2.Signature = nil - signBytes := c2.Marshal() - if cert.Signature == nil || cert.Signature.Format == ssh.KeyAlgoRSA || cert.Signature.Format == ssh.KeyAlgoDSA { - errs = append(errs, fmt.Errorf("certificate signature is invalid")) - } else if err := cert.SignatureKey.Verify(signBytes[:len(signBytes)-4], cert.Signature); err != nil { + if cert.Signature == nil || cert.SignatureKey == nil || cert.Signature.Format == ssh.KeyAlgoRSA || cert.Signature.Format == ssh.KeyAlgoDSA { errs = append(errs, fmt.Errorf("certificate signature is invalid")) + } else { + c2 := *cert + c2.Signature = nil + signBytes := c2.Marshal() + if err := cert.SignatureKey.Verify(signBytes[:len(signBytes)-4], cert.Signature); err != nil { + errs = append(errs, fmt.Errorf("certificate signature is invalid")) + } } return errors.Join(errs...) } diff --git a/go/internal/app/cert_util_test.go b/go/internal/app/cert_util_test.go index ef7dcc0..5dbf773 100644 --- a/go/internal/app/cert_util_test.go +++ b/go/internal/app/cert_util_test.go @@ -59,22 +59,19 @@ func TestCheckCertificate(t *testing.T) { badSig bool expectErr bool }{ - {cert: &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidBefore: ssh.CertTimeInfinity}}, - {cert: &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidAfter: earlier, ValidBefore: later}}, - {cert: &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidAfter: earlier, ValidBefore: ssh.CertTimeInfinity}}, - {cert: &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidBefore: later}}, - {cert: &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidBefore: 0}, expectErr: true}, - {cert: &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidAfter: later, ValidBefore: ssh.CertTimeInfinity}, expectErr: true}, - {cert: &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidBefore: earlier}, expectErr: true}, - {cert: &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidBefore: ssh.CertTimeInfinity}, badSig: true, expectErr: true}, - {cert: &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidBefore: ssh.CertTimeInfinity, Permissions: ssh.Permissions{CriticalOptions: map[string]string{"foo\x1b[31m": "bar\x1b[32m"}}}, expectErr: true}, - {cert: &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidBefore: ssh.CertTimeInfinity}, certType: ssh.UserCert, expectErr: true}, - {cert: &ssh.Certificate{Key: sshPub, CertType: ssh.UserCert, ValidBefore: ssh.CertTimeInfinity}, certType: ssh.HostCert, expectErr: true}, - {cert: &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidAfter: later, ValidBefore: earlier}, badSig: true, expectErr: true}, + {cert: &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidPrincipals: []string{"host.example.com"}, ValidBefore: ssh.CertTimeInfinity}}, + {cert: &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidPrincipals: []string{"host.example.com"}, ValidAfter: earlier, ValidBefore: later}}, + {cert: &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidPrincipals: []string{"host.example.com"}, ValidAfter: earlier, ValidBefore: ssh.CertTimeInfinity}}, + {cert: &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidPrincipals: []string{"host.example.com"}, ValidBefore: later}}, + {cert: &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidPrincipals: []string{"host.example.com"}, ValidBefore: 0}, expectErr: true}, + {cert: &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidPrincipals: []string{"host.example.com"}, ValidAfter: later, ValidBefore: ssh.CertTimeInfinity}, expectErr: true}, + {cert: &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidPrincipals: []string{"host.example.com"}, ValidBefore: earlier}, expectErr: true}, + {cert: &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidPrincipals: []string{"host.example.com"}, ValidBefore: ssh.CertTimeInfinity}, badSig: true, expectErr: true}, + {cert: &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidPrincipals: []string{"host.example.com"}, ValidBefore: ssh.CertTimeInfinity, Permissions: ssh.Permissions{CriticalOptions: map[string]string{"foo\x1b[31m": "bar\x1b[32m"}}}, expectErr: true}, + {cert: &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidPrincipals: []string{"host.example.com"}, ValidBefore: ssh.CertTimeInfinity}, certType: ssh.UserCert, expectErr: true}, + {cert: &ssh.Certificate{Key: sshPub, CertType: ssh.UserCert, ValidPrincipals: []string{"user"}, ValidBefore: ssh.CertTimeInfinity}, certType: ssh.HostCert, expectErr: true}, + {cert: &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidPrincipals: []string{"host.example.com"}, ValidAfter: later, ValidBefore: earlier}, badSig: true, expectErr: true}, } { - if len(tc.cert.ValidPrincipals) == 0 { - tc.cert.ValidPrincipals = []string{"test"} - } if err := tc.cert.SignCert(rand.Reader, signer); err != nil { t.Fatalf("[#%d] SignCert: %v", i, err) } @@ -106,7 +103,7 @@ func TestCheckCertificate(t *testing.T) { } algSigner := rsaSigner.(ssh.AlgorithmSigner) - sha1Cert := &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidPrincipals: []string{"test"}, ValidBefore: ssh.CertTimeInfinity} + sha1Cert := &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidPrincipals: []string{"host.example.com"}, ValidBefore: ssh.CertTimeInfinity} if err := sha1Cert.SignCert(rand.Reader, &fixedAlgoSigner{AlgorithmSigner: algSigner, algo: ssh.KeyAlgoRSA}); err != nil { t.Fatalf("SignCert(ssh-rsa): %v", err) } @@ -114,7 +111,7 @@ func TestCheckCertificate(t *testing.T) { t.Errorf("expected checkCertificate to reject ssh-rsa (SHA-1) CA signature") } - sha512Cert := &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidPrincipals: []string{"test"}, ValidBefore: ssh.CertTimeInfinity} + sha512Cert := &ssh.Certificate{Key: sshPub, CertType: ssh.HostCert, ValidPrincipals: []string{"host.example.com"}, ValidBefore: ssh.CertTimeInfinity} if err := sha512Cert.SignCert(rand.Reader, &fixedAlgoSigner{AlgorithmSigner: algSigner, algo: ssh.KeyAlgoRSASHA512}); err != nil { t.Fatalf("SignCert(rsa-sha2-512): %v", err) } diff --git a/go/sshterm-server/internal/jumphost.go b/go/sshterm-server/internal/jumphost.go index 32466e0..c327f57 100644 --- a/go/sshterm-server/internal/jumphost.go +++ b/go/sshterm-server/internal/jumphost.go @@ -58,6 +58,9 @@ var jumpPubKeyAuthAlgorithms = []string{ ssh.KeyAlgoED25519, ssh.KeyAlgoSKED25519, ssh.KeyAlgoSKECDSA256, + // Browser-generated WebAuthn signatures for sk-ecdsa keys. + //ssh.SigAlgoWebAuthnSKECDSA256, + "webauthn-sk-ecdsa-sha2-nistp256@openssh.com", ssh.KeyAlgoECDSA256, ssh.KeyAlgoECDSA384, ssh.KeyAlgoECDSA521, diff --git a/go/sshterm-server/internal/jumphost_test.go b/go/sshterm-server/internal/jumphost_test.go index 7f07550..3e7fb3d 100644 --- a/go/sshterm-server/internal/jumphost_test.go +++ b/go/sshterm-server/internal/jumphost_test.go @@ -26,14 +26,19 @@ package internal import ( "bytes" "context" + "crypto/ecdsa" "crypto/ed25519" + "crypto/elliptic" "crypto/rand" "crypto/rsa" + "crypto/sha256" + "encoding/base64" "encoding/json" "encoding/pem" "fmt" "io" "log" + "math/big" "net" "net/http" "net/http/httptest" @@ -709,7 +714,7 @@ func TestValidateUserCertRejectsSHA1RSA(t *testing.T) { cert := &ssh.Certificate{ Key: uPub, CertType: ssh.UserCert, - ValidPrincipals: []string{"alice"}, + ValidPrincipals: []string{"alice@example.com"}, ValidBefore: ssh.CertTimeInfinity, Permissions: ssh.Permissions{ Extensions: map[string]string{"permit-port-forwarding": ""}, @@ -718,14 +723,14 @@ func TestValidateUserCertRejectsSHA1RSA(t *testing.T) { if err := cert.SignCert(rand.Reader, &fixedAlgoTestSigner{AlgorithmSigner: algSigner, algo: ssh.KeyAlgoRSA}); err != nil { t.Fatal(err) } - if err := validateUserCert(cert, "alice", nil); err == nil { + if err := validateUserCert(cert, "alice@example.com", nil); err == nil { t.Error("expected validateUserCert to reject ssh-rsa (SHA-1) signature") } if err := cert.SignCert(rand.Reader, &fixedAlgoTestSigner{AlgorithmSigner: algSigner, algo: ssh.KeyAlgoRSASHA512}); err != nil { t.Fatal(err) } - if err := validateUserCert(cert, "alice", nil); err != nil { + if err := validateUserCert(cert, "alice@example.com", nil); err != nil { t.Errorf("expected validateUserCert to accept rsa-sha2-512 signature, got: %v", err) } } @@ -1032,3 +1037,136 @@ func TestJumpHostTrustedProxiesXForwardedFor(t *testing.T) { t.Error("expected malformed XFF to fail closed") } } + +// webAuthnTestSigner is a Signer for an sk-ecdsa-sha2-nistp256@openssh.com +// key that produces webauthn-sk-ecdsa-sha2-nistp256@openssh.com signatures, +// like the browser app does with an ecdsa-sk key. +type webAuthnTestSigner struct { + priv *ecdsa.PrivateKey + pub ssh.PublicKey +} + +func newWebAuthnTestSigner(t *testing.T) *webAuthnTestSigner { + t.Helper() + priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + t.Fatal(err) + } + pub, err := ssh.ParsePublicKey(ssh.Marshal(struct { + Name string + ID string + Key []byte + Application string + }{ + ssh.KeyAlgoSKECDSA256, + "nistp256", + elliptic.Marshal(elliptic.P256(), priv.PublicKey.X, priv.PublicKey.Y), + "ssh:", + })) + if err != nil { + t.Fatal(err) + } + return &webAuthnTestSigner{priv: priv, pub: pub} +} + +func (s *webAuthnTestSigner) PublicKey() ssh.PublicKey { return s.pub } + +func (s *webAuthnTestSigner) Sign(rand io.Reader, data []byte) (*ssh.Signature, error) { + const origin = "https://sshterm.example.com" + clientData := fmt.Sprintf(`{"type":"webauthn.get","challenge":%q,"origin":%q,"crossOrigin":false}`, + base64.RawURLEncoding.EncodeToString(data), origin) + appHash := sha256.Sum256([]byte("ssh:")) + clientDataHash := sha256.Sum256([]byte(clientData)) + const flags byte = 0x05 // UP | UV + const counter uint32 = 1 + signed := ssh.Marshal(struct { + ApplicationDigest []byte `ssh:"rest"` + Flags byte + Counter uint32 + MessageDigest []byte `ssh:"rest"` + }{appHash[:], flags, counter, clientDataHash[:]}) + digest := sha256.Sum256(signed) + r, ss, err := ecdsa.Sign(rand, s.priv, digest[:]) + if err != nil { + return nil, err + } + return &ssh.Signature{ + //Format: ssh.SigAlgoWebAuthnSKECDSA256, + Format: "webauthn-sk-ecdsa-sha2-nistp256@openssh.com", + Blob: ssh.Marshal(struct{ R, S *big.Int }{r, ss}), + Rest: ssh.Marshal(struct { + Flags byte + Counter uint32 + Origin string + ClientData string + Extensions string + }{flags, counter, origin, clientData, ""}), + }, nil +} + +func TestJumpHostWebAuthnSignature(t *testing.T) { + t.Skip() + signer := newWebAuthnTestSigner(t) + keyLine := strings.TrimSpace(string(ssh.MarshalAuthorizedKey(signer.PublicKey()))) + + targetAddr, targetHostPub := startMockTargetSSHServer(t, "targetuser", signer.PublicKey()) + + akFile := filepath.Join(t.TempDir(), "authorized_keys") + akContent := fmt.Sprintf("permitopen=\"internal.example.com:22\" %s webauthn-key\n", keyLine) + if err := os.WriteFile(akFile, []byte(akContent), 0o600); err != nil { + t.Fatal(err) + } + + srv, err := New(Options{ + AuthorizedKeysFile: akFile, + DialContext: func(ctx context.Context, network, address string) (net.Conn, error) { + if address != "internal.example.com:22" { + return nil, fmt.Errorf("unexpected dial to %q", address) + } + var d net.Dialer + return d.DialContext(ctx, "tcp", targetAddr) + }, + Logger: log.New(io.Discard, "", 0), + }) + if err != nil { + t.Fatalf("New: %v", err) + } + ts := httptest.NewServer(srv.Handler()) + defer ts.Close() + wsJumpURL := "ws" + strings.TrimPrefix(ts.URL, "http") + "/_ws/" + DefaultJumpEndpointName + + jumpClient, err := dialJumpSSHClient(t, wsJumpURL, "anyuser", srv.jumpHost.signer.PublicKey(), ssh.PublicKeys(signer)) + if err != nil { + t.Fatalf("dialJumpSSHClient(webauthn key): %v", err) + } + defer jumpClient.Close() + + // The same key also authenticates to the target through the tunnel. + tunConn, err := jumpClient.Dial("tcp", "internal.example.com:22") + if err != nil { + t.Fatalf("jumpClient.Dial: %v", err) + } + tc, tchans, treqs, err := ssh.NewClientConn(tunConn, "internal.example.com:22", &ssh.ClientConfig{ + User: "targetuser", + Auth: []ssh.AuthMethod{ssh.PublicKeys(signer)}, + HostKeyCallback: ssh.FixedHostKey(targetHostPub), + }) + if err != nil { + tunConn.Close() + t.Fatalf("NewClientConn over tunnel: %v", err) + } + targetClient := ssh.NewClient(tc, tchans, treqs) + defer targetClient.Close() + sess, err := targetClient.NewSession() + if err != nil { + t.Fatalf("targetClient.NewSession: %v", err) + } + defer sess.Close() + out, err := sess.CombinedOutput("hello") + if err != nil { + t.Fatalf("CombinedOutput: %v", err) + } + if string(out) != "target-exec:hello" { + t.Errorf("output = %q, want %q", out, "target-exec:hello") + } +}