@@ -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