Files
Warp/server/internal/matrix/signing/key.go
T
tajniak81andClaude Opus 5.5 6aaf554336
CI / Test (ubuntu-latest) (push) Canceled after 0s
CI / Test (windows-latest) (push) Canceled after 0s
CI / Storage tests on PostgreSQL (push) Canceled after 0s
CI / Complement (federation off) (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
CI / Smoke test the binary (push) Canceled after 0s
Phase 5: federation
Implements PLAN.md Phase 5: the Server-Server API. Warp joins rooms on
other servers and lets their users join its rooms, invites across servers
with the invitee's server countersigning, rejects remote invites (locally
if the inviter is unreachable), and exchanges events, typing, receipts and
to-device messages with other homeservers — all behind the runtime switch,
which now starts and stops real outbound traffic.

The protocol lives in internal/federation with no knowledge of rooms:
X-Matrix signing (fuzzed parser), the spec's discovery algorithm keeping
the dial address and the TLS name apart, a signed client that refuses
every request while the switch is off or the policy denies the
destination, and a key ring that caches other servers' keys for at most a
week, with optional notaries.

The roomserver takes events from elsewhere through the spec's intake:
signatures and hashes (a hash mismatch keeps the redacted form), the rules
against the event's own auth events, against the state before it — with
/get_missing_events for small gaps and /state_ids for wide ones — and
against the current state, where a failure is a soft failure. Events held
without their state are outliers: a column on roomserver_events, because
the current state of a room joined over federation is almost all
outliers, and excluded from every timeline query. Forks are resolved at
the room's head. Membership across servers is make_join/send_join,
make_leave/send_leave and countersigned invites, in both directions;
m.room.server_acl is enforced on everything another server sends.
History before a remote join is backfilled when /messages pages past it,
stored at negative stream positions.

internal/federationapi is the running component: the inbound endpoints,
and an outbound queue that persists what it owes each destination, sends
it in transactions of 50 PDUs and 100 EDUs, backs off from ten seconds to
six hours, survives restarts, and drops events persisted while federation
is off rather than delivering a backlog in one burst. Its Module is what
the switch starts and stops.

Also: a TLS listener for port 8448, trusted key servers, key validity and
an outbound CA bundle in the config; per-destination health and backoff
reset in the admin API; remote profiles, aliases and directories in the
Client-Server API; a Complement federation profile. See
docs/federation.md, including its known gaps (no partial-state joins, no
key rotation).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 17:14:20 +02:00

192 lines
6.0 KiB
Go

// Package signing handles the server's ed25519 signing keys: generating
// them, reading and writing the on-disk format, and naming them.
//
// Event signing itself (canonical JSON, hashes, signature blocks) arrives
// with the event core in Phase 2; this package deliberately stops at key
// material so that `warp generate-keys` can exist from day one.
package signing
import (
"crypto/ed25519"
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
// KeyIDPrefix is the only signing algorithm Matrix defines today.
const KeyIDPrefix = "ed25519"
// FileMode is the permission a freshly written key file gets. On Windows it
// is advisory; the warning in Load covers the Unix case.
const FileMode os.FileMode = 0o600
// KeyPair is a server signing key and the key ID that names it in a
// signature block ("ed25519:abc123").
type KeyPair struct {
// ID is the part after the algorithm, e.g. "a_RVfN".
ID string
Private ed25519.PrivateKey
Public ed25519.PublicKey
}
// KeyID returns the full "ed25519:<id>" identifier.
func (k *KeyPair) KeyID() string { return KeyIDPrefix + ":" + k.ID }
// PublicBase64 returns the unpadded base64 public key, the encoding Matrix
// uses on the wire.
func (k *KeyPair) PublicBase64() string {
return base64.RawStdEncoding.EncodeToString(k.Public)
}
// Generate creates a new key. An empty id gets a random one.
func Generate(id string) (*KeyPair, error) {
if id == "" {
var err error
if id, err = RandomKeyID(); err != nil {
return nil, err
}
}
if err := ValidateKeyID(id); err != nil {
return nil, err
}
pub, priv, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
return nil, fmt.Errorf("generate ed25519 key: %w", err)
}
return &KeyPair{ID: id, Private: priv, Public: pub}, nil
}
// keyIDAlphabet is deliberately narrow: the spec allows [a-zA-Z0-9_] and
// mixed case key IDs are a nuisance to type.
const keyIDAlphabet = "abcdefghijklmnopqrstuvwxyz0123456789"
// RandomKeyID returns an identifier of the "a_xxxxx" shape Synapse uses, so
// operators recognise it.
func RandomKeyID() (string, error) {
buf := make([]byte, 5)
if _, err := io.ReadFull(rand.Reader, buf); err != nil {
return "", fmt.Errorf("read randomness: %w", err)
}
out := make([]byte, 0, len(buf)+2)
out = append(out, 'a', '_')
for _, b := range buf {
out = append(out, keyIDAlphabet[int(b)%len(keyIDAlphabet)])
}
return string(out), nil
}
// ValidateKeyID checks the identifier against the grammar in the spec.
func ValidateKeyID(id string) error {
if id == "" {
return errors.New("key ID must not be empty")
}
if strings.Contains(id, ":") {
return fmt.Errorf("key ID %q must not contain a colon; pass the part after %q", id, KeyIDPrefix+":")
}
for _, r := range id {
switch {
case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z', r >= '0' && r <= '9', r == '_':
default:
return fmt.Errorf("key ID %q contains %q; only [a-zA-Z0-9_] are allowed", id, r)
}
}
return nil
}
// Save writes the key to path in the one-line interchange format shared with
// Synapse:
//
// ed25519 <key-id> <unpadded base64 seed>
//
// It refuses to clobber an existing file: a lost signing key means every
// event this server ever signed stops verifying.
func (k *KeyPair) Save(path string) error {
if dir := filepath.Dir(path); dir != "" && dir != "." {
if err := os.MkdirAll(dir, 0o700); err != nil {
return fmt.Errorf("create key directory: %w", err)
}
}
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, FileMode)
if err != nil {
if errors.Is(err, os.ErrExist) {
return fmt.Errorf("%s already exists; refusing to overwrite a signing key", path)
}
return err
}
defer f.Close()
seed := base64.RawStdEncoding.EncodeToString(k.Private.Seed())
if _, err := fmt.Fprintf(f, "%s %s %s\n", KeyIDPrefix, k.ID, seed); err != nil {
return fmt.Errorf("write key: %w", err)
}
return f.Close()
}
// Load reads a key file written by Save. Lines starting with '#' and blank
// lines are ignored; the first key line wins. Older keys (a file with
// several entries) will matter once key rotation exists, which it does not
// yet: see the known gaps in docs/federation.md.
func Load(path string) (*KeyPair, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read signing key: %w", err)
}
for _, line := range strings.Split(string(data), "\n") {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
return parseKeyLine(line, path)
}
return nil, fmt.Errorf("%s contains no signing key", path)
}
func parseKeyLine(line, path string) (*KeyPair, error) {
parts := strings.Fields(line)
if len(parts) != 3 {
return nil, fmt.Errorf("%s: malformed key line, want \"ed25519 <key-id> <base64-seed>\"", path)
}
if parts[0] != KeyIDPrefix {
return nil, fmt.Errorf("%s: unsupported key algorithm %q", path, parts[0])
}
if err := ValidateKeyID(parts[1]); err != nil {
return nil, fmt.Errorf("%s: %w", path, err)
}
seed, err := decodeBase64(parts[2])
if err != nil {
return nil, fmt.Errorf("%s: decode seed: %w", path, err)
}
if len(seed) != ed25519.SeedSize {
return nil, fmt.Errorf("%s: seed is %d bytes, want %d", path, len(seed), ed25519.SeedSize)
}
priv := ed25519.NewKeyFromSeed(seed)
pub, ok := priv.Public().(ed25519.PublicKey)
if !ok {
return nil, fmt.Errorf("%s: unexpected public key type", path)
}
return &KeyPair{ID: parts[1], Private: priv, Public: pub}, nil
}
// DecodeBase64 decodes a base64 value from the Matrix wire format.
//
// Matrix writes these unpadded, but the spec asks decoders to accept
// padding too wherever they can: keys, hashes and signatures get copied
// between implementations by hand, and rejecting a trailing "=" would turn
// a harmless transcription into a verification failure.
func DecodeBase64(s string) ([]byte, error) {
return decodeBase64(s)
}
func decodeBase64(s string) ([]byte, error) {
if strings.HasSuffix(s, "=") {
return base64.StdEncoding.DecodeString(s)
}
return base64.RawStdEncoding.DecodeString(s)
}