Compare commits

...

29 Commits

Author SHA1 Message Date
Andrew Morgan
18cf53376d ree 2020-04-20 18:05:36 +01:00
Andrew Morgan
e49a90899b another 2020-04-20 17:59:46 +01:00
Andrew Morgan
6317eba770 ahh 2020-04-20 17:17:11 +01:00
Andrew Morgan
da51afdc6b whoops 2020-04-20 17:07:35 +01:00
Andrew Morgan
2ccad9a1b6 add debug logging 2020-04-20 16:50:17 +01:00
Andrew Morgan
714e75dc1b lint 2020-04-20 16:07:00 +01:00
Andrew Morgan
0c1b27ecd0 Resolve review comments 2020-04-20 16:05:44 +01:00
Andrew Morgan
ac1bbfdd2b Update changelog 2020-04-20 15:35:22 +01:00
Andrew Morgan
7dd06332a9 Update changelog 2020-04-20 11:58:35 +01:00
Andrew Morgan
76f15f4bf2 Remove extraneous key_id and verify_key 2020-04-20 11:57:13 +01:00
Andrew Morgan
887ec58556 Update method docstring 2020-04-17 12:54:55 +01:00
Andrew Morgan
b3b2da56b3 Send device updates, modeled after SigningKeyEduUpdater._handle_signing_key_updates 2020-04-17 12:30:54 +01:00
Andrew Morgan
cb56a51ada Factor key retrieval out into a separate function 2020-04-17 12:08:09 +01:00
Andrew Morgan
40042dec0d lint 2020-04-17 11:39:30 +01:00
Andrew Morgan
2c881bf8a4 Remove extraneous items from remote query try/except 2020-04-17 11:36:08 +01:00
Andrew Morgan
667e9ca5be Fix log statements, docstrings 2020-04-17 11:33:43 +01:00
Andrew Morgan
8490a8793c Only fetch master and self_signing key types 2020-04-16 20:01:34 +01:00
Andrew Morgan
2ff55e02c1 Add comment explaining why this is useful 2020-04-16 17:59:47 +01:00
Andrew Morgan
0ac339cfe6 lint 2020-04-16 17:55:59 +01:00
Andrew Morgan
f4064862b2 Note that _get_e2e_cross_signing_verify_key can raise a SynapseError 2020-04-16 17:54:34 +01:00
Andrew Morgan
67851671e5 Wrap get_verify_key_from_cross_signing_key in a try/except 2020-04-16 17:51:00 +01:00
Andrew Morgan
a7dadf87be Remove very specific exception handling 2020-04-16 17:48:52 +01:00
Andrew Morgan
c215378411 Make changelog more useful 2020-04-16 17:23:28 +01:00
Andrew Morgan
e91373c1fa Use query_user_devices instead, assume only master, self_signing key types 2020-04-16 17:13:57 +01:00
Andrew Morgan
4e48515bbf Fix and de-brittle remote result dict processing 2020-04-16 16:14:01 +01:00
Andrew Morgan
70807c83c6 lint 2020-04-16 13:58:29 +01:00
Andrew Morgan
44cf7cf56e Save retrieved keys to the db 2020-04-16 13:54:59 +01:00
Andrew Morgan
d2a9b45df0 Add changelog 2020-04-16 12:50:34 +01:00
Andrew Morgan
81e74fbae5 Query missing cross-signing keys on local sig upload 2020-04-16 12:49:00 +01:00
3 changed files with 153 additions and 12 deletions

1
changelog.d/7289.bugfix Normal file
View File

@@ -0,0 +1 @@
Fix a bug with cross-signing devices with remote users when they did not share a room with any user on the local homeserver.

View File

@@ -406,13 +406,19 @@ class TransportLayerClient(object):
"device_keys": {
"<user_id>": {
"<device_id>": {...}
} }
"master_keys": {
"<user_id>": {...}
} }
"self_signing_keys": {
"<user_id>": {...}
} } }
Args:
destination(str): The server to query.
query_content(dict): The user ids to query.
Returns:
A dict containg the device keys.
A dict containing device and cross-signing keys.
"""
path = _create_v1_path("/user/keys/query")
@@ -429,14 +435,16 @@ class TransportLayerClient(object):
Response:
{
"stream_id": "...",
"devices": [ { ... } ]
"devices": [ { ... } ],
"master_key": { ... },
"self_signing_key: { ... }
}
Args:
destination(str): The server to query.
query_content(dict): The user ids to query.
Returns:
A dict containg the device keys.
A dict containing device and cross-signing keys.
"""
path = _create_v1_path("/user/devices/%s", user_id)

View File

@@ -174,8 +174,8 @@ class E2eKeysHandler(object):
"""This is called when we are querying the device list of a user on
a remote homeserver and their device list is not in the device list
cache. If we share a room with this user and we're not querying for
specific user we will update the cache
with their device list."""
specific user we will update the cache with their device list.
"""
destination_query = remote_queries_not_in_cache[destination]
@@ -880,6 +880,7 @@ class E2eKeysHandler(object):
try:
# get our user-signing key to verify the signatures
logger.info("***Getting the user_signing")
(
user_signing_key,
user_signing_key_id,
@@ -903,6 +904,7 @@ class E2eKeysHandler(object):
try:
# get the target user's master key, to make sure it matches
# what was sent
logger.info("***Getting the master")
(
master_key,
master_key_id,
@@ -961,13 +963,19 @@ class E2eKeysHandler(object):
return signature_list, failures
@defer.inlineCallbacks
def _get_e2e_cross_signing_verify_key(self, user_id, key_type, from_user_id=None):
"""Fetch the cross-signing public key from storage and interpret it.
def _get_e2e_cross_signing_verify_key(
self, user_id: str, key_type: str, from_user_id: str = None
):
"""Fetch locally or remotely query for a cross-signing public key.
First, attempt to fetch the cross-signing public key from storage.
If that fails, query the keys from the homeserver they belong to
and update our local copy.
Args:
user_id (str): the user whose key should be fetched
key_type (str): the type of key to fetch
from_user_id (str): the user that we are fetching the keys for.
user_id: the user whose key should be fetched
key_type: the type of key to fetch
from_user_id: the user that we are fetching the keys for.
This affects what signatures are fetched.
Returns:
@@ -976,16 +984,140 @@ class E2eKeysHandler(object):
Raises:
NotFoundError: if the key is not found
SynapseError: if `user_id` is invalid
"""
user = UserID.from_string(user_id)
logger.info("***Trying to get a %s key for %s from storage...", key_type, user_id)
key = yield self.store.get_e2e_cross_signing_key(
user_id, key_type, from_user_id
)
logger.info("***Well, we got this: %s", key)
# If we couldn't find the key locally, and we're looking for keys of
# another user then attempt to fetch the missing key from the remote
# user's server.
#
# We may run into this in possible edge cases where a user tries to
# cross-sign a remote user, but does not share any rooms with them yet.
# Thus, we would not have their key list yet. We fetch the key here,
# store it and notify clients of new, associated device IDs.
logger.info("***Checking if we'll do our thingy")
if (
key is None
and not self.is_mine(user)
# We only get "master" and "self_signing" keys from remote servers
and key_type in ["master", "self_signing"]
):
logger.info("***Doing our thingy")
(
key,
key_id,
verify_key,
) = yield self._retrieve_cross_signing_keys_for_remote_user(user, key_type)
if key is None:
logger.debug("no %s key found for %s", key_type, user_id)
logger.warning("No %s key found for %s", key_type, user_id)
raise NotFoundError("No %s key found for %s" % (key_type, user_id))
key_id, verify_key = get_verify_key_from_cross_signing_key(key)
try:
key_id, verify_key = get_verify_key_from_cross_signing_key(key)
except ValueError as e:
logger.warning(
"Invalid %s key retrieved: %s - %s %s", key_type, key, type(e), e,
)
raise SynapseError(
502, "Invalid %s key retrieved from remote server" % (key_type,)
)
logger.info("***Finally returning %s - %s - %s", key, key_id, verify_key)
return key, key_id, verify_key
@defer.inlineCallbacks
def _retrieve_cross_signing_keys_for_remote_user(
self, user: UserID, desired_key_type: str,
):
"""Queries cross-signing keys for a remote user and saves them to the database
Only the key specified by `key_type` will be returned, while all retrieved keys
will be saved regardless
Args:
user: The user to query remote keys for
desired_key_type: The type of key to receive. One of "master", "self_signing"
Returns:
Deferred[Tuple[Optional[Dict], Optional[str], Optional[VerifyKey]]]: A tuple
of the retrieved key content, the key's ID and the matching VerifyKey.
If the key cannot be retrieved, all values in the tuple will instead be None.
"""
try:
remote_result = yield self.federation.query_user_devices(
user.domain, user.to_string()
)
logger.info("***Got our remote_result: %s", remote_result)
except Exception as e:
logger.warning(
"Unable to query %s for cross-signing keys of user %s: %s %s",
user.domain,
user.to_string(),
type(e),
e,
)
return None, None, None
# Process each of the retrieved cross-signing keys
final_key = None
final_key_id = None
final_verify_key = None
device_ids = []
for key_type in ["master", "self_signing"]:
logger.info("***Processing retrieved key type: %s", key_type)
key_content = remote_result.get(key_type + "_key")
logger.info("***Got key_content: %s", key_content)
if not key_content:
continue
# At the same time, store this key in the db for
# subsequent queries
yield self.store.set_e2e_cross_signing_key(
user.to_string(), key_type, key_content
)
logger.info("***Stored key")
# Note down the device ID attached to this key
try:
# verify_key is a VerifyKey from signedjson, which uses
# .version to denote the portion of the key ID after the
# algorithm and colon, which is the device ID
key_id, verify_key = get_verify_key_from_cross_signing_key(key_content)
logger.info("***Verified key: %s - %s", key_id, verify_key)
except ValueError as e:
logger.warning(
"Invalid %s key retrieved: %s - %s %s",
key_type,
key_content,
type(e),
e,
)
continue
logger.info("***Appending device id: %s - %s", verify_key, verify_key.version)
device_ids.append(verify_key.version)
# If this is the desired key type, save it and its ID/VerifyKey
if key_type == desired_key_type:
logger.info("***We found our desired key type, %s!", key_type)
final_key = key_content
final_verify_key = verify_key
final_key_id = key_id
# Notify clients that new devices for this user have been discovered
if device_ids:
logger.info("***Updating clients with devices: %s", device_ids)
yield self.device_handler.notify_device_update(user.to_string(), device_ids)
logger.info("***Returning %s - %s - %s", final_key, final_key_id, final_verify_key)
return final_key, final_key_id, final_verify_key
def _check_cross_signing_key(key, user_id, key_type, signing_key=None):
"""Check a cross-signing key uploaded by a user. Performs some basic sanity