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>
This commit is contained in:
1 parent
92836f8dcd
commit
fc49f56339
45 files changed
+6050
-63
No files matched your search
@@ -61,11 +61,22 @@ type Response struct {
|
||||
ToDevice ToDeviceList `json:"to_device"`
|
||||
|
||||
// DeviceLists reports whose device list changed, which a client needs
|
||||
// in order to re-fetch their keys. The keyserver is PLAN.md phase 7,
|
||||
// so both lists are always empty; the block is here all the same,
|
||||
// because a client that finds no device_lists treats the response as
|
||||
// malformed rather than as "nothing changed".
|
||||
// in order to re-fetch their keys, and whose it can stop tracking.
|
||||
// Both lists are present even when empty: a client that finds no
|
||||
// device_lists treats the response as malformed rather than as
|
||||
// "nothing changed".
|
||||
DeviceLists DeviceLists `json:"device_lists"`
|
||||
|
||||
// DeviceOneTimeKeysCount is how many one-time keys the syncing device
|
||||
// has left on the server, by algorithm, which is how it knows to
|
||||
// upload more.
|
||||
DeviceOneTimeKeysCount map[string]int `json:"device_one_time_keys_count,omitempty"`
|
||||
|
||||
// DeviceUnusedFallbackKeyTypes lists the algorithms the device has an
|
||||
// unused fallback key for. It is a pointer so that an empty list goes
|
||||
// out as [] rather than not at all: a client reads the field's
|
||||
// absence as a server without fallback keys, and never uploads one.
|
||||
DeviceUnusedFallbackKeyTypes *[]string `json:"device_unused_fallback_key_types,omitempty"`
|
||||
}
|
||||
|
||||
// Rooms is the room half of a sync, split by the user's membership.
|
||||
@@ -302,7 +313,6 @@ func (s *API) build(ctx context.Context, req SyncRequest, filter Filter, since T
|
||||
Presence: PresenceList{Events: []presenceEvent{}},
|
||||
AccountData: AccountDataList{Events: []AccountDataEvent{}},
|
||||
ToDevice: ToDeviceList{Events: []toDeviceEvent{}},
|
||||
// Both lists are empty until Phase 7 and both have to be present.
|
||||
DeviceLists: DeviceLists{Changed: []string{}, Left: []string{}},
|
||||
}
|
||||
|
||||
@@ -346,6 +356,13 @@ func (s *API) build(ctx context.Context, req SyncRequest, filter Filter, since T
|
||||
}
|
||||
resp.Presence.Events = presence
|
||||
|
||||
if resp.DeviceLists, err = s.deviceLists(ctx, req.User, since, to, rooms); err != nil {
|
||||
return Response{}, false, err
|
||||
}
|
||||
if err := s.deviceKeyCounts(ctx, req, &resp); err != nil {
|
||||
return Response{}, false, err
|
||||
}
|
||||
|
||||
resp.NextBatch = next.String()
|
||||
return resp, isInteresting(resp, since), nil
|
||||
}
|
||||
@@ -369,7 +386,9 @@ func isInteresting(resp Response, since Token) bool {
|
||||
len(resp.Rooms.Knock) > 0 ||
|
||||
len(resp.AccountData.Events) > 0 ||
|
||||
len(resp.ToDevice.Events) > 0 ||
|
||||
len(resp.Presence.Events) > 0
|
||||
len(resp.Presence.Events) > 0 ||
|
||||
len(resp.DeviceLists.Changed) > 0 ||
|
||||
len(resp.DeviceLists.Left) > 0
|
||||
}
|
||||
|
||||
// buildRooms fills the four room sections.
|
||||
|
||||
Reference in new issue
Block a user