Documentation
¶
Overview ¶
Package ssh implements OpenSSH FIDO2/WebAuthn ECDSA P-256 security keys (sk-ecdsa-sha2-nistp256@openssh.com) and browser WebAuthn SSH signatures (webauthn-sk-ecdsa-sha2-nistp256@openssh.com) per OpenSSH PROTOCOL.u2f.
Key implements golang.org/x/crypto/ssh.PublicKey on all platforms and golang.org/x/crypto/ssh.Signer when built for browser WebAssembly (GOOS=js GOARCH=wasm).
Key Registration ¶
A Key can be registered either on a server from a standard WebAuthn registration ceremony or directly in a browser WebAssembly client:
- Server-driven registration: run a WebAuthn registration ceremony using github.com/c2FmZQ/webauthn.NewAttestationOptions (restricted to github.com/c2FmZQ/webauthn.AlgES256) and github.com/c2FmZQ/webauthn.VerifyAttestation, then construct a Key with NewKey.
- Browser WASM registration (GOOS=js GOARCH=wasm): call Create(name, resident) to invoke navigator.credentials.create() and verify the resulting P-256 credential locally.
Once created, the public key can be formatted for OpenSSH authorized_keys with golang.org/x/crypto/ssh.MarshalAuthorizedKey or signed into an OpenSSH certificate (see CertType). The key handle and metadata can be serialized with Key.MarshalPrivate as a PEM-encoded WEBAUTHN KEY or passphrase-protected WEBAUTHN ENCRYPTED KEY.
Authentication and Signature Verification ¶
Stored keys are loaded with Unmarshal (from PEM) or UnmarshalPublic (from OpenSSH wire format, e.g. after golang.org/x/crypto/ssh.ParseAuthorizedKey).
In a browser WebAssembly client (GOOS=js GOARCH=wasm), (*Key).Sign produces an OpenSSH signature in SignatureFormat via navigator.credentials.get(), and Discover recovers a resident ECDSA P-256 key from two assertions.
On any platform, Key.Verify verifies both hardware security key signatures (KeyType) and browser WebAuthn signatures (SignatureFormat).
Example (Authentication) ¶
package main
import (
"crypto/ecdsa"
"crypto/rand"
"encoding/asn1"
"encoding/pem"
"fmt"
"log"
"math/big"
gossh "golang.org/x/crypto/ssh"
"github.com/c2FmZQ/webauthn"
"github.com/c2FmZQ/webauthn/ssh"
)
func main() {
// Placeholder storage for pending challenges, OpenSSH authorized_keys, and
// PEM-encoded key handles.
pendingChallenges := make(map[string][]byte)
authorizedKeys := make(map[string][]byte)
privateKeyPEMs := make(map[string][]byte)
// Setup: populate storage with an existing registered key for "alice".
auth, err := webauthn.NewFakeAuthenticator()
if err != nil {
log.Fatal(err)
}
auth.SetOrigin("https://example.com")
regOpts, err := webauthn.NewAttestationOptions()
if err != nil {
log.Fatal(err)
}
regOpts.RelyingParty = webauthn.RelyingPartyEntity{Name: "Example Corp", ID: "example.com"}
regOpts.User = webauthn.UserEntity{ID: webauthn.Bytes("alice-uid"), Name: "alice", DisplayName: "Alice"}
regOpts.PubKeyCredParams = []webauthn.PubKeyCredParam{{Type: "public-key", Alg: webauthn.AlgES256}}
regCD, regAtt, err := auth.Create(regOpts)
if err != nil {
log.Fatal(err)
}
regCred, err := webauthn.VerifyAttestation(&webauthn.AttestationResponse{
ClientDataJSON: regCD,
AttestationObject: regAtt,
}, webauthn.VerifyAttestationOptions{
Challenge: regOpts.Challenge,
Origin: "https://example.com",
RPID: "example.com",
})
if err != nil {
log.Fatal(err)
}
regKey, err := ssh.NewKey(regCred.ID, regCred.PublicKey.(*ecdsa.PublicKey), []byte("example.com"))
if err != nil {
log.Fatal(err)
}
authorizedKeys["alice"] = gossh.MarshalAuthorizedKey(regKey)
pemBlock, err := regKey.MarshalPrivate("passphrase")
if err != nil {
log.Fatal(err)
}
privateKeyPEMs["alice"] = pem.EncodeToMemory(pemBlock)
// Placeholder for server <-> browser communication (e.g. HTTP/JSON or WebSocket).
// In production, the browser loads its key via ssh.Unmarshal (or ssh.Discover)
// and calls (*ssh.Key).Sign(rand.Reader, challenge) in GOOS=js GOARCH=wasm,
// returning the marshaled *gossh.Signature to the server.
browserSign := func(challenge []byte) ([]byte, error) {
clientKey, err := ssh.Unmarshal(privateKeyPEMs["alice"], "alice", func(string) (string, error) {
return "passphrase", nil
})
if err != nil {
return nil, err
}
_, cd, adBytes, rawSig, _, err := auth.Get(&webauthn.AssertionOptions{
Challenge: challenge,
RPID: string(clientKey.RPID()),
AllowCredentials: []webauthn.CredentialID{
{Type: "public-key", ID: clientKey.ID()},
},
})
if err != nil {
return nil, err
}
var ad webauthn.AuthenticatorData
if err := webauthn.ParseAuthenticatorData(adBytes, &ad); err != nil {
return nil, err
}
parsedCD, err := webauthn.ParseClientData(cd)
if err != nil {
return nil, err
}
var ecSig struct{ R, S *big.Int }
if _, err := asn1.Unmarshal(rawSig, &ecSig); err != nil {
return nil, err
}
sshSig := gossh.Signature{
Format: ssh.SignatureFormat,
Blob: gossh.Marshal(ecSig),
Rest: gossh.Marshal(struct {
Flags byte
Counter uint32
Origin string
ClientData string
Extensions string
}{
Flags: ad.Flags,
Counter: ad.SignCount,
Origin: parsedCD.Origin,
ClientData: string(cd),
Extensions: string(ad.Extensions),
}),
}
return gossh.Marshal(sshSig), nil
}
// Step 1 (Server): Load the user's public key and generate a challenge.
pub, _, _, _, err := gossh.ParseAuthorizedKey(authorizedKeys["alice"])
if err != nil {
log.Fatal(err)
}
serverKey, err := ssh.UnmarshalPublic(pub.Marshal())
if err != nil {
log.Fatal(err)
}
challenge := make([]byte, 32)
if _, err := rand.Read(challenge); err != nil {
log.Fatal(err)
}
pendingChallenges["session-2"] = challenge
// Step 2 (Browser): Sign the challenge and send the SSH signature back.
sigBytes, err := browserSign(challenge)
if err != nil {
log.Fatal(err)
}
// Step 3 (Server): Load the expected challenge and verify the signature.
wantChallenge := pendingChallenges["session-2"]
delete(pendingChallenges, "session-2")
var sig gossh.Signature
if err := gossh.Unmarshal(sigBytes, &sig); err != nil {
log.Fatal(err)
}
if err := serverKey.Verify(wantChallenge, &sig); err != nil {
log.Fatal(err)
}
fmt.Printf("verified: format=%s\n", sig.Format)
}
Output: verified: format=webauthn-sk-ecdsa-sha2-nistp256@openssh.com
Example (Registration) ¶
package main
import (
"crypto/ecdsa"
"encoding/json"
"encoding/pem"
"fmt"
"log"
gossh "golang.org/x/crypto/ssh"
"github.com/c2FmZQ/webauthn"
"github.com/c2FmZQ/webauthn/ssh"
)
func main() {
// Placeholder storage for pending challenges, OpenSSH authorized_keys, and
// PEM-encoded private key handles.
pendingChallenges := make(map[string][]byte)
authorizedKeys := make(map[string][]byte)
privateKeyPEMs := make(map[string][]byte)
// Placeholder for server <-> browser communication (e.g. HTTP/JSON).
// In production, the browser calls navigator.credentials.create() (or
// ssh.Create in a GOOS=js GOARCH=wasm client) and sends the JSON response
// back to the server.
auth, err := webauthn.NewFakeAuthenticator()
if err != nil {
log.Fatal(err)
}
auth.SetOrigin("https://example.com")
browserCreate := func(optionsJSON []byte) (responseJSON []byte, err error) {
var opts webauthn.AttestationOptions
if err := json.Unmarshal(optionsJSON, &opts); err != nil {
return nil, err
}
clientDataJSON, attestationObject, err := auth.Create(&opts)
if err != nil {
return nil, err
}
att, err := webauthn.ParseAttestationObject(attestationObject)
if err != nil {
return nil, err
}
credID := att.AuthData.AttestedCredentials.ID
return json.Marshal(map[string]any{
"id": credID.String(),
"rawId": credID,
"type": "public-key",
"response": map[string]any{
"clientDataJSON": webauthn.Bytes(clientDataJSON),
"attestationObject": webauthn.Bytes(attestationObject),
},
})
}
// Step 1 (Server): Request an ES256 (ECDSA P-256) WebAuthn credential.
opts, err := webauthn.NewAttestationOptions()
if err != nil {
log.Fatal(err)
}
opts.RelyingParty = webauthn.RelyingPartyEntity{Name: "Example Corp", ID: "example.com"}
opts.User = webauthn.UserEntity{ID: webauthn.Bytes("alice-uid"), Name: "alice", DisplayName: "Alice"}
opts.PubKeyCredParams = []webauthn.PubKeyCredParam{{Type: "public-key", Alg: webauthn.AlgES256}}
pendingChallenges["session-1"] = opts.Challenge
optionsJSON, err := json.Marshal(opts)
if err != nil {
log.Fatal(err)
}
// Step 2 (Browser): Create the credential and return the attestation JSON.
responseJSON, err := browserCreate(optionsJSON)
if err != nil {
log.Fatal(err)
}
// Step 3 (Server): Verify the attestation, build an *ssh.Key, and store both
// the OpenSSH authorized_keys entry and the encrypted PEM key handle.
challenge := pendingChallenges["session-1"]
delete(pendingChallenges, "session-1")
var resp webauthn.AttestationResponse
if err := json.Unmarshal(responseJSON, &resp); err != nil {
log.Fatal(err)
}
cred, err := webauthn.VerifyAttestation(&resp, webauthn.VerifyAttestationOptions{
Challenge: challenge,
Origin: "https://example.com",
RPID: "example.com",
})
if err != nil {
log.Fatal(err)
}
pubKey, ok := cred.PublicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatalf("unexpected public key type %T", cred.PublicKey)
}
key, err := ssh.NewKey(cred.ID, pubKey, []byte("example.com"))
if err != nil {
log.Fatal(err)
}
authorizedKeys["alice"] = gossh.MarshalAuthorizedKey(key)
block, err := key.MarshalPrivate("passphrase")
if err != nil {
log.Fatal(err)
}
privateKeyPEMs["alice"] = pem.EncodeToMemory(block)
fmt.Printf("registered: type=%s hasAuthKey=%v hasPEM=%v\n",
key.Type(), len(authorizedKeys["alice"]) > 0, len(privateKeyPEMs["alice"]) > 0)
}
Output: registered: type=sk-ecdsa-sha2-nistp256@openssh.com hasAuthKey=true hasPEM=true
Index ¶
Examples ¶
Constants ¶
const ( // KeyType is the OpenSSH public key identifier for ECDSA P-256 security keys. KeyType = gossh.KeyAlgoSKECDSA256 // CertType is the OpenSSH certificate identifier for ECDSA P-256 security keys. CertType = gossh.CertAlgoSKECDSA256v01 // SignatureFormat is the OpenSSH signature format identifier for browser // WebAuthn assertions with an ECDSA P-256 security key. SignatureFormat = "webauthn-sk-ecdsa-sha2-nistp256@openssh.com" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Key ¶
type Key struct {
// contains filtered or unexported fields
}
Key represents an OpenSSH ECDSA P-256 WebAuthn security key.
func NewKey ¶
NewKey constructs a Key from a credential ID, P-256 public key, and Relying Party ID (application string).
func Unmarshal ¶
Unmarshal parses a PEM-encoded WEBAUTHN KEY or WEBAUTHN ENCRYPTED KEY block. If the key is encrypted, rp is called with a prompt string containing name to obtain the passphrase.
func UnmarshalPublic ¶
UnmarshalPublic parses an OpenSSH wire-encoded sk-ecdsa-sha2-nistp256@openssh.com (or legacy webauthn-sk-ecdsa-sha2-nistp256@openssh.com) public key blob.
func (*Key) MarshalPrivate ¶
MarshalPrivate serializes the key into a PEM block. When passphrase is empty, it produces an unencrypted WEBAUTHN KEY block; otherwise, it encrypts the credential ID using AES-256-GCM with a PBKDF2-HMAC-SHA256 key derived from passphrase and produces a WEBAUTHN ENCRYPTED KEY block.