Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit bd18b8f

Browse files
committed
Add cross-signing sigs to the keys object
All the callers want this info in the same place, so let's reduce the duplication by doing it here.
1 parent bbb0cfd commit bd18b8f

File tree

3 files changed

+18
-33
lines changed

3 files changed

+18
-33
lines changed

changelog.d/8234.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Refactor queries for device keys and cross-signatures.

synapse/storage/databases/main/devices.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,9 @@ async def _get_device_update_edus_by_remote(
291291
prev_id = stream_id
292292

293293
if device is not None:
294-
key_json = device.key_json
295-
if key_json:
296-
result["keys"] = db_to_json(key_json)
297-
298-
if device.signatures:
299-
for sig_user_id, sigs in device.signatures.items():
300-
result["keys"].setdefault("signatures", {}).setdefault(
301-
sig_user_id, {}
302-
).update(sigs)
294+
keys = device.keys
295+
if keys:
296+
result["keys"] = keys
303297

304298
device_display_name = device.display_name
305299
if device_display_name:

synapse/storage/databases/main/end_to_end_keys.py

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,8 @@ class DeviceKeyLookupResult:
4343

4444
# the key data from e2e_device_keys_json. Typically includes fields like
4545
# "algorithm", "keys" (including the curve25519 identity key and the ed25519 signing
46-
# key) and "signatures" (a signature of the structure by the ed25519 key)
47-
key_json = attr.ib(type=Optional[str])
48-
49-
# cross-signing sigs on this device.
50-
# dict from (signing user_id)->(signing device_id)->sig
51-
signatures = attr.ib(type=Optional[Dict[str, Dict[str, str]]], factory=dict)
46+
# key) and signatures" (a map from (user id) to (key id/device_id) to signature.)
47+
keys = attr.ib(type=Optional[JsonDict])
5248

5349

5450
class EndToEndKeyWorkerStore(SQLBaseStore):
@@ -70,15 +66,9 @@ async def get_e2e_device_keys_for_federation_query(
7066
for device_id, device in user_devices.items():
7167
result = {"device_id": device_id}
7268

73-
key_json = device.key_json
74-
if key_json:
75-
result["keys"] = db_to_json(key_json)
76-
77-
if device.signatures:
78-
for sig_user_id, sigs in device.signatures.items():
79-
result["keys"].setdefault("signatures", {}).setdefault(
80-
sig_user_id, {}
81-
).update(sigs)
69+
keys = device.keys
70+
if keys:
71+
result["keys"] = keys
8272

8373
device_display_name = device.display_name
8474
if device_display_name:
@@ -114,16 +104,11 @@ async def get_e2e_device_keys_for_cs_api(
114104
for user_id, device_keys in results.items():
115105
rv[user_id] = {}
116106
for device_id, device_info in device_keys.items():
117-
r = db_to_json(device_info.key_json)
107+
r = device_info.keys
118108
r["unsigned"] = {}
119109
display_name = device_info.display_name
120110
if display_name is not None:
121111
r["unsigned"]["device_display_name"] = display_name
122-
if device_info.signatures:
123-
for sig_user_id, sigs in device_info.signatures.items():
124-
r.setdefault("signatures", {}).setdefault(
125-
sig_user_id, {}
126-
).update(sigs)
127112
rv[user_id][device_id] = r
128113

129114
return rv
@@ -137,6 +122,9 @@ async def get_e2e_device_keys_and_signatures(
137122
) -> Dict[str, Dict[str, Optional[DeviceKeyLookupResult]]]:
138123
"""Fetch a list of device keys, together with their cross-signatures.
139124
125+
The cross-signatures are added to the `signatures` field within the `keys`
126+
object in the response.
127+
140128
Args:
141129
query_list: List of pairs of user_ids and device_ids. Device id can be None
142130
to indicate "all devices for this user"
@@ -167,7 +155,7 @@ async def get_e2e_device_keys_and_signatures(
167155
(user_id, device_id)
168156
for user_id, dev in result.items()
169157
for device_id, d in dev.items()
170-
if d is not None
158+
if d is not None and d.keys is not None
171159
)
172160

173161
for batch in batch_iter(signature_query, 50):
@@ -186,7 +174,9 @@ async def get_e2e_device_keys_and_signatures(
186174
signature = row["signature"]
187175

188176
target_device_result = result[target_user_id][target_device_id]
189-
target_device_signatures = target_device_result.signatures
177+
target_device_signatures = target_device_result.keys.setdefault(
178+
"signatures", {}
179+
)
190180

191181
signing_user_signatures = target_device_signatures.setdefault(
192182
signing_user_id, {}
@@ -243,7 +233,7 @@ def _get_e2e_device_keys_txn(
243233
if include_deleted_devices:
244234
deleted_devices.remove((user_id, device_id))
245235
result.setdefault(user_id, {})[device_id] = DeviceKeyLookupResult(
246-
display_name, key_json
236+
display_name, db_to_json(key_json) if key_json else None
247237
)
248238

249239
if include_deleted_devices:

0 commit comments

Comments
 (0)