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

Commit fd8d154

Browse files
committed
Use query_user_devices instead, assume only master, self_signing key types
1 parent 39ed9f6 commit fd8d154

File tree

2 files changed

+42
-41
lines changed

2 files changed

+42
-41
lines changed

synapse/federation/transport/client.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,13 +406,19 @@ def query_client_keys(self, destination, query_content, timeout):
406406
"device_keys": {
407407
"<user_id>": {
408408
"<device_id>": {...}
409+
} }
410+
"master_keys": {
411+
"<user_id>": {...}
412+
} }
413+
"self_signing_keys": {
414+
"<user_id>": {...}
409415
} } }
410416
411417
Args:
412418
destination(str): The server to query.
413419
query_content(dict): The user ids to query.
414420
Returns:
415-
A dict containing the device keys.
421+
A dict containing device and cross-signing keys.
416422
"""
417423
path = _create_v1_path("/user/keys/query")
418424

@@ -429,14 +435,16 @@ def query_user_devices(self, destination, user_id, timeout):
429435
Response:
430436
{
431437
"stream_id": "...",
432-
"devices": [ { ... } ]
438+
"devices": [ { ... } ],
439+
"master_key": { ... },
440+
"self_signing_key: { ... }
433441
}
434442
435443
Args:
436444
destination(str): The server to query.
437445
query_content(dict): The user ids to query.
438446
Returns:
439-
A dict containg the device keys.
447+
A dict containing device and cross-signing keys.
440448
"""
441449
path = _create_v1_path("/user/devices/%s", user_id)
442450

synapse/handlers/e2e_keys.py

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -969,12 +969,14 @@ def _process_other_signatures(self, user_id, signatures):
969969
return signature_list, failures
970970

971971
@defer.inlineCallbacks
972-
def _get_e2e_cross_signing_verify_key(self, user_id, key_type, from_user_id=None):
972+
def _get_e2e_cross_signing_verify_key(
973+
self, user_id, desired_key_type, from_user_id=None
974+
):
973975
"""Fetch the cross-signing public key from storage and interpret it.
974976
975977
Args:
976978
user_id (str): the user whose key should be fetched
977-
key_type (str): the type of key to fetch
979+
desired_key_type (str): the type of key to fetch
978980
from_user_id (str): the user that we are fetching the keys for.
979981
This affects what signatures are fetched.
980982
@@ -987,47 +989,38 @@ def _get_e2e_cross_signing_verify_key(self, user_id, key_type, from_user_id=None
987989
"""
988990
user = UserID.from_string(user_id)
989991
key = yield self.store.get_e2e_cross_signing_key(
990-
user_id, key_type, from_user_id
992+
user_id, desired_key_type, from_user_id
991993
)
992994

993-
if key is None and self.is_mine(user):
994-
# Attempt to fetch the missing key from the remote user's server
995+
# If we still can't find the key, and we're looking for keys of another user,
996+
# then attempt to fetch the missing key from the remote user's server.
997+
#
998+
# We don't get "user_signing" keys from remote servers, so disallow that here
999+
if (
1000+
key is None
1001+
and not self.is_mine(user)
1002+
and desired_key_type != "user_signing"
1003+
):
9951004
try:
996-
remote_result = yield self.federation.query_client_keys(
997-
user.domain, {"device_keys": {user_id: []}}, timeout=10 * 1000
1005+
remote_result = yield self.federation.query_user_devices(
1006+
user.domain, user_id
9981007
)
9991008

1000-
# Process the result
1001-
for remote_key_type, remote_user_dict in remote_result.items():
1002-
# The key_type variable passed to this function is in the form
1003-
# "self_signing","master" etc. whereas the results returned from
1004-
# the remote server use "self_signing_keys", "master_keys" etc.
1005-
# Remove the "_keys" from the key type
1006-
if remote_key_type.endswith("_keys"):
1007-
remote_key_type = remote_key_type[:-5]
1008-
1009-
# remote_user_dict is a dictionary in the form of
1010-
# {
1011-
# "user_id": {
1012-
# "master_keys": ...
1013-
# },
1014-
# ...
1015-
# }
1016-
1017-
# Only extract the keys that pertain to the requested user
1018-
key_content_list = remote_user_dict.get(user_id, {}).values()
1019-
1020-
for key_content in key_content_list:
1021-
# If the key_type here matches the key we're requesting,
1022-
# then this is the key we want to return
1023-
if remote_key_type == key_type:
1024-
key = key_content
1025-
1026-
# At the same time, save the key to the database for subsequent
1027-
# queries
1028-
yield self.store.set_e2e_cross_signing_key(
1029-
user_id, remote_key_type, key_content
1030-
)
1009+
# Process each of the retrieved cross-signing keys
1010+
for key_type in ["master", "self_signing"]:
1011+
key_content = remote_result.get(key_type + "_key")
1012+
if not key_content:
1013+
continue
1014+
1015+
# If this is the desired key type, return it
1016+
if key_type == desired_key_type:
1017+
key = key_content
1018+
1019+
# At the same time, store this key in the db for
1020+
# subsequent queries
1021+
yield self.store.set_e2e_cross_signing_key(
1022+
user_id, key_type, key_content
1023+
)
10311024
except (
10321025
HttpResponseException,
10331026
NotRetryingDestination,

0 commit comments

Comments
 (0)