Files
Warp/server/internal/federation/userkeys.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

110 lines
4.3 KiB
Go

package federation
import (
"context"
"encoding/json"
"net/http"
)
// The end-to-end encryption half of the Server-Server API: asking another
// server for its users' device keys, claiming their one-time keys, and the
// EDUs that say a user's keys have changed.
//
// Keys travel as json.RawMessage. They are signed objects, and the only
// safe thing to do with a signed object on its way through is not to
// re-encode it.
// UserKeysQueryRequest is the body of POST /user/keys/query: user ID to the
// device IDs wanted, where an empty list asks for every device.
type UserKeysQueryRequest struct {
DeviceKeys map[string][]string `json:"device_keys"`
}
// UserKeysQueryResponse is the answer to POST /user/keys/query.
type UserKeysQueryResponse struct {
DeviceKeys map[string]map[string]json.RawMessage `json:"device_keys"`
MasterKeys map[string]json.RawMessage `json:"master_keys,omitempty"`
SelfSigningKeys map[string]json.RawMessage `json:"self_signing_keys,omitempty"`
}
// UserKeysClaimRequest is the body of POST /user/keys/claim: user ID, then
// device ID, then the algorithm of the key wanted.
type UserKeysClaimRequest struct {
OneTimeKeys map[string]map[string]string `json:"one_time_keys"`
}
// UserKeysClaimResponse is the answer to POST /user/keys/claim: user ID,
// then device ID, then key ID, then the key.
type UserKeysClaimResponse struct {
OneTimeKeys map[string]map[string]map[string]json.RawMessage `json:"one_time_keys"`
}
// UserDevicesResponse is the answer to GET /user/devices/{userId}: the
// whole of a user's device list, which a server asks for when the updates
// it was sent do not add up.
type UserDevicesResponse struct {
UserID string `json:"user_id"`
StreamID int64 `json:"stream_id"`
Devices []UserDevice `json:"devices"`
MasterKey json.RawMessage `json:"master_key,omitempty"`
SelfSigningKey json.RawMessage `json:"self_signing_key,omitempty"`
}
// UserDevice is one device in a UserDevicesResponse.
type UserDevice struct {
DeviceID string `json:"device_id"`
Keys json.RawMessage `json:"keys"`
DeviceDisplayName string `json:"device_display_name,omitempty"`
}
// DeviceListUpdateContent is the content of an m.device_list_update EDU:
// one device of one user changed, or went away.
type DeviceListUpdateContent struct {
UserID string `json:"user_id"`
DeviceID string `json:"device_id"`
DeviceDisplayName string `json:"device_display_name,omitempty"`
StreamID int64 `json:"stream_id"`
PrevID []int64 `json:"prev_id,omitempty"`
Deleted bool `json:"deleted,omitempty"`
Keys json.RawMessage `json:"keys,omitempty"`
}
// SigningKeyUpdateContent is the content of an m.signing_key_update EDU: a
// user's cross-signing keys changed.
type SigningKeyUpdateContent struct {
UserID string `json:"user_id"`
MasterKey json.RawMessage `json:"master_key,omitempty"`
SelfSigningKey json.RawMessage `json:"self_signing_key,omitempty"`
}
// QueryUserKeys asks a server for its users' device and cross-signing
// keys.
func (c *Client) QueryUserKeys(ctx context.Context, destination string, req UserKeysQueryRequest) (UserKeysQueryResponse, error) {
var out UserKeysQueryResponse
err := c.do(ctx, request{
method: http.MethodPost, destination: destination, signed: true,
path: "/_matrix/federation/v1/user/keys/query", body: req,
}, &out)
return out, err
}
// ClaimUserKeys claims one-time keys of a server's users' devices.
func (c *Client) ClaimUserKeys(ctx context.Context, destination string, req UserKeysClaimRequest) (UserKeysClaimResponse, error) {
var out UserKeysClaimResponse
err := c.do(ctx, request{
method: http.MethodPost, destination: destination, signed: true,
path: "/_matrix/federation/v1/user/keys/claim", body: req,
}, &out)
return out, err
}
// UserDevices asks a server for one of its users' whole device list.
func (c *Client) UserDevices(ctx context.Context, destination, userID string) (UserDevicesResponse, error) {
var out UserDevicesResponse
err := c.do(ctx, request{
method: http.MethodGet, destination: destination, signed: true,
path: "/_matrix/federation/v1/user/devices/" + esc(userID),
}, &out)
return out, err
}