Files
Warp/server/internal/federationapi/keys.go
T
tajniak81andClaude Opus 5.5 fc49f56339 Phase 7: E2EE support
Implements PLAN.md Phase 7: the server half of end-to-end encryption.
Devices upload identity, one-time and fallback keys; other devices query
and claim them, across servers too, and every one-time key is handed out
exactly once. Cross-signing, signature uploads, device list tracking and
server-side key backup complete the set.

internal/keyserver is the component. Keys are stored in canonical JSON
so that re-uploading the same key is a byte comparison, and signatures
uploaded later are kept apart from the key they sign and merged in on
read, so a key's own JSON stays what its owner uploaded. Device keys must
be for the caller's own device; cross-signing keys must be the
uploader's, carry the right usage and, for self- and user-signing keys,
be signed by the master key. Signatures are verified against the key as
stored. Replacing a master key asks for the password; a first upload or
an upload of the keys already stored does not, as the spec allows. A
signature of another user's master key is shown only to the user who
made it and never federated.

Every write that changes what /keys/query answers moves the device list
stream in the same transaction, one position per device, and wakes the
syncs of everyone sharing a room. The stream is the sixth field of a sync
token; older tokens still parse. The syncapi works out device_lists
changed and left from the stream and from membership moves in the
window, skipping invites and knocks, and reports
device_one_time_keys_count and device_unused_fallback_key_types, the
latter as [] when empty. GET /keys/changes uses the same logic.

Over federation: /user/keys/query, /user/keys/claim and
/user/devices/{userId}, and m.device_list_update and m.signing_key_update
both ways. Remote keys are not cached; an update about a remote user
sharing a room moves them in the stream so local clients re-query.
Device display names are not sent to other servers. With federation off,
remote users are reported in failures at once.

Deleting a device deletes its keys, through a new userapi
DeviceObserver. Key backup keeps the better copy of each session by the
spec's rule, never reuses a deleted version's number, and answers writes
to a stale version with M_WRONG_ROOM_KEYS_VERSION.

Storage is migration 0008 for sqlite and postgres, with a PocketBase
stub. The Complement target sets gain the key, device list, fallback,
cross-signing and backup tests.

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

97 lines
3.3 KiB
Go

package federationapi
import (
"context"
"encoding/json"
"net/http"
"github.com/dariuszjaniak/warp/server/internal/federation"
"github.com/dariuszjaniak/warp/server/internal/httputil"
"github.com/dariuszjaniak/warp/server/internal/matrix/id"
)
// The end-to-end encryption endpoints other servers call: the keys of this
// server's users, their one-time keys, and their whole device lists.
// KeyService answers other servers' questions about this server's users'
// keys, and takes in their news about their own users'. *keyserver.API
// satisfies it.
type KeyService interface {
QueryLocalKeys(ctx context.Context, req federation.UserKeysQueryRequest) (federation.UserKeysQueryResponse, error)
ClaimLocalKeys(ctx context.Context, want map[string]map[string]string) (map[string]map[string]map[string]json.RawMessage, error)
UserDevices(ctx context.Context, userID string) (federation.UserDevicesResponse, error)
RemoteKeysChanged(ctx context.Context, userID string) error
}
// registerKeyRoutes adds the key endpoints, if there is a keyserver.
func (s *Server) registerKeyRoutes(authed func(string, authedHandler)) {
if s.keyService == nil {
return
}
const v1 = "/_matrix/federation/v1"
authed("POST "+v1+"/user/keys/query", s.handleUserKeysQuery)
authed("POST "+v1+"/user/keys/claim", s.handleUserKeysClaim)
authed("GET "+v1+"/user/devices/{userID}", s.handleUserDevices)
}
func (s *Server) handleUserKeysQuery(w http.ResponseWriter, r *http.Request, _ string, body []byte) error {
var req federation.UserKeysQueryRequest
if err := decode(body, &req); err != nil {
return err
}
resp, err := s.keyService.QueryLocalKeys(r.Context(), req)
if err != nil {
return err
}
httputil.WriteJSON(w, http.StatusOK, resp)
return nil
}
func (s *Server) handleUserKeysClaim(w http.ResponseWriter, r *http.Request, _ string, body []byte) error {
var req federation.UserKeysClaimRequest
if err := decode(body, &req); err != nil {
return err
}
claimed, err := s.keyService.ClaimLocalKeys(r.Context(), req.OneTimeKeys)
if err != nil {
return err
}
httputil.WriteJSON(w, http.StatusOK, federation.UserKeysClaimResponse{OneTimeKeys: claimed})
return nil
}
func (s *Server) handleUserDevices(w http.ResponseWriter, r *http.Request, _ string, _ []byte) error {
userID := r.PathValue("userID")
server, err := id.ServerNameOf(userID)
if err != nil {
return httputil.InvalidParam("%s is not a user ID", userID)
}
if server != s.serverName {
return httputil.NotFound("%s is not a user of this server", userID)
}
resp, err := s.keyService.UserDevices(r.Context(), userID)
if err != nil {
return err
}
httputil.WriteJSON(w, http.StatusOK, resp)
return nil
}
// receiveKeyEDU takes in an m.device_list_update or m.signing_key_update.
// Either says only that a user of the sending server changed their keys;
// the keyserver passes that on to the local clients that care.
func (s *Server) receiveKeyEDU(ctx context.Context, origin string, edu federation.EDU) {
if s.keyService == nil {
return
}
var c struct {
UserID string `json:"user_id"`
}
if json.Unmarshal(edu.Content, &c) != nil || !sameServer(c.UserID, origin) {
return
}
if err := s.keyService.RemoteKeysChanged(ctx, c.UserID); err != nil {
s.log.Debug("ignored a key update EDU", "origin", origin, "type", edu.Type, "error", err)
}
}