2017-12-04 20:29:25 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-10-12 06:26:18 -04:00
|
|
|
# Copyright 2017, 2018 New Vector Ltd
|
2017-12-04 20:29:25 -05:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
2018-10-09 04:39:13 -04:00
|
|
|
from six import iteritems
|
2018-10-09 04:47:04 -04:00
|
|
|
|
2017-12-04 20:29:25 -05:00
|
|
|
from twisted.internet import defer
|
|
|
|
|
2019-02-06 17:57:10 -05:00
|
|
|
from synapse.api.errors import Codes, NotFoundError, RoomKeysVersionError, \
|
|
|
|
StoreError, SynapseError
|
2018-08-24 22:51:25 -04:00
|
|
|
from synapse.util.async_helpers import Linearizer
|
2017-12-04 20:29:25 -05:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class E2eRoomKeysHandler(object):
|
2017-12-17 20:52:46 -05:00
|
|
|
"""
|
|
|
|
Implements an optional realtime backup mechanism for encrypted E2E megolm room keys.
|
|
|
|
This gives a way for users to store and recover their megolm keys if they lose all
|
|
|
|
their clients. It should also extend easily to future room key mechanisms.
|
|
|
|
The actual payload of the encrypted keys is completely opaque to the handler.
|
|
|
|
"""
|
|
|
|
|
2017-12-04 20:29:25 -05:00
|
|
|
def __init__(self, hs):
|
|
|
|
self.store = hs.get_datastore()
|
2017-12-17 20:52:46 -05:00
|
|
|
|
|
|
|
# Used to lock whenever a client is uploading key data. This prevents collisions
|
|
|
|
# between clients trying to upload the details of a new session, given all
|
|
|
|
# clients belonging to a user will receive and try to upload a new session at
|
|
|
|
# roughly the same time. Also used to lock out uploads when the key is being
|
|
|
|
# changed.
|
2017-12-05 16:44:25 -05:00
|
|
|
self._upload_linearizer = Linearizer("upload_room_keys_lock")
|
2017-12-04 20:29:25 -05:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2017-12-27 18:37:44 -05:00
|
|
|
def get_room_keys(self, user_id, version, room_id=None, session_id=None):
|
|
|
|
"""Bulk get the E2E room keys for a given backup, optionally filtered to a given
|
|
|
|
room, or a given session.
|
|
|
|
See EndToEndRoomKeyStore.get_e2e_room_keys for full details.
|
|
|
|
|
2018-10-12 06:26:18 -04:00
|
|
|
Args:
|
|
|
|
user_id(str): the user whose keys we're getting
|
|
|
|
version(str): the version ID of the backup we're getting keys from
|
|
|
|
room_id(string): room ID to get keys for, for None to get keys for all rooms
|
2018-10-12 08:47:43 -04:00
|
|
|
session_id(string): session ID to get keys for, for None to get keys for all
|
|
|
|
sessions
|
2018-11-05 17:59:29 -05:00
|
|
|
Raises:
|
|
|
|
NotFoundError: if the backup version does not exist
|
2017-12-27 18:37:44 -05:00
|
|
|
Returns:
|
|
|
|
A deferred list of dicts giving the session_data and message metadata for
|
|
|
|
these room keys.
|
|
|
|
"""
|
|
|
|
|
2017-12-05 20:02:57 -05:00
|
|
|
# we deliberately take the lock to get keys so that changing the version
|
|
|
|
# works atomically
|
|
|
|
with (yield self._upload_linearizer.queue(user_id)):
|
2018-11-05 17:59:29 -05:00
|
|
|
# make sure the backup version exists
|
|
|
|
try:
|
|
|
|
yield self.store.get_e2e_room_keys_version_info(user_id, version)
|
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
raise NotFoundError("Unknown backup version")
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2017-12-05 20:02:57 -05:00
|
|
|
results = yield self.store.get_e2e_room_keys(
|
|
|
|
user_id, version, room_id, session_id
|
|
|
|
)
|
2017-12-31 12:47:11 -05:00
|
|
|
|
2017-12-05 20:02:57 -05:00
|
|
|
defer.returnValue(results)
|
2017-12-04 20:29:25 -05:00
|
|
|
|
2017-12-05 12:54:48 -05:00
|
|
|
@defer.inlineCallbacks
|
2017-12-27 18:37:44 -05:00
|
|
|
def delete_room_keys(self, user_id, version, room_id=None, session_id=None):
|
|
|
|
"""Bulk delete the E2E room keys for a given backup, optionally filtered to a given
|
|
|
|
room or a given session.
|
|
|
|
See EndToEndRoomKeyStore.delete_e2e_room_keys for full details.
|
|
|
|
|
2018-10-12 06:26:18 -04:00
|
|
|
Args:
|
|
|
|
user_id(str): the user whose backup we're deleting
|
|
|
|
version(str): the version ID of the backup we're deleting
|
2018-10-12 08:47:43 -04:00
|
|
|
room_id(string): room ID to delete keys for, for None to delete keys for all
|
|
|
|
rooms
|
|
|
|
session_id(string): session ID to delete keys for, for None to delete keys
|
|
|
|
for all sessions
|
2017-12-27 18:37:44 -05:00
|
|
|
Returns:
|
|
|
|
A deferred of the deletion transaction
|
|
|
|
"""
|
|
|
|
|
2017-12-17 20:52:46 -05:00
|
|
|
# lock for consistency with uploading
|
|
|
|
with (yield self._upload_linearizer.queue(user_id)):
|
|
|
|
yield self.store.delete_e2e_room_keys(user_id, version, room_id, session_id)
|
2017-12-05 12:54:48 -05:00
|
|
|
|
2017-12-04 20:29:25 -05:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def upload_room_keys(self, user_id, version, room_keys):
|
2017-12-27 18:37:44 -05:00
|
|
|
"""Bulk upload a list of room keys into a given backup version, asserting
|
|
|
|
that the given version is the current backup version. room_keys are merged
|
|
|
|
into the current backup as described in RoomKeysServlet.on_PUT().
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id(str): the user whose backup we're setting
|
|
|
|
version(str): the version ID of the backup we're updating
|
|
|
|
room_keys(dict): a nested dict describing the room_keys we're setting:
|
|
|
|
|
|
|
|
{
|
|
|
|
"rooms": {
|
|
|
|
"!abc:matrix.org": {
|
|
|
|
"sessions": {
|
|
|
|
"c0ff33": {
|
|
|
|
"first_message_index": 1,
|
|
|
|
"forwarded_count": 1,
|
|
|
|
"is_verified": false,
|
|
|
|
"session_data": "SSBBTSBBIEZJU0gK"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Raises:
|
2018-11-05 17:59:29 -05:00
|
|
|
NotFoundError: if there are no versions defined
|
2017-12-27 18:37:44 -05:00
|
|
|
RoomKeysVersionError: if the uploaded version is not the current version
|
|
|
|
"""
|
|
|
|
|
2017-12-04 20:29:25 -05:00
|
|
|
# TODO: Validate the JSON to make sure it has the right keys.
|
|
|
|
|
2017-12-05 12:54:48 -05:00
|
|
|
# XXX: perhaps we should use a finer grained lock here?
|
2017-12-05 16:44:25 -05:00
|
|
|
with (yield self._upload_linearizer.queue(user_id)):
|
2017-12-31 12:47:11 -05:00
|
|
|
|
2017-12-17 20:52:46 -05:00
|
|
|
# Check that the version we're trying to upload is the current version
|
2017-12-27 18:42:08 -05:00
|
|
|
try:
|
2018-10-12 06:26:18 -04:00
|
|
|
version_info = yield self.store.get_e2e_room_keys_version_info(user_id)
|
2017-12-17 20:52:46 -05:00
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
2018-11-05 17:59:29 -05:00
|
|
|
raise NotFoundError("Version '%s' not found" % (version,))
|
2017-12-17 20:52:46 -05:00
|
|
|
else:
|
2018-10-12 06:26:18 -04:00
|
|
|
raise
|
2017-12-17 20:52:46 -05:00
|
|
|
|
|
|
|
if version_info['version'] != version:
|
2017-12-31 12:47:11 -05:00
|
|
|
# Check that the version we're trying to upload actually exists
|
|
|
|
try:
|
2018-10-12 08:47:43 -04:00
|
|
|
version_info = yield self.store.get_e2e_room_keys_version_info(
|
|
|
|
user_id, version,
|
|
|
|
)
|
2017-12-31 12:47:11 -05:00
|
|
|
# if we get this far, the version must exist
|
|
|
|
raise RoomKeysVersionError(current_version=version_info['version'])
|
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
2018-11-05 17:59:29 -05:00
|
|
|
raise NotFoundError("Version '%s' not found" % (version,))
|
2017-12-31 12:47:11 -05:00
|
|
|
else:
|
2018-10-12 06:26:18 -04:00
|
|
|
raise
|
2017-12-17 20:52:46 -05:00
|
|
|
|
|
|
|
# go through the room_keys.
|
|
|
|
# XXX: this should/could be done concurrently, given we're in a lock.
|
2018-10-09 04:39:13 -04:00
|
|
|
for room_id, room in iteritems(room_keys['rooms']):
|
|
|
|
for session_id, session in iteritems(room['sessions']):
|
2017-12-05 16:44:25 -05:00
|
|
|
yield self._upload_room_key(
|
2017-12-31 12:47:11 -05:00
|
|
|
user_id, version, room_id, session_id, session
|
2017-12-05 12:54:48 -05:00
|
|
|
)
|
2017-12-04 20:29:25 -05:00
|
|
|
|
2017-12-05 16:44:25 -05:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _upload_room_key(self, user_id, version, room_id, session_id, room_key):
|
2017-12-27 18:37:44 -05:00
|
|
|
"""Upload a given room_key for a given room and session into a given
|
|
|
|
version of the backup. Merges the key with any which might already exist.
|
|
|
|
|
2017-12-27 18:58:51 -05:00
|
|
|
Args:
|
2017-12-27 18:37:44 -05:00
|
|
|
user_id(str): the user whose backup we're setting
|
|
|
|
version(str): the version ID of the backup we're updating
|
|
|
|
room_id(str): the ID of the room whose keys we're setting
|
|
|
|
session_id(str): the session whose room_key we're setting
|
|
|
|
room_key(dict): the room_key being set
|
|
|
|
"""
|
|
|
|
|
2017-12-05 16:44:25 -05:00
|
|
|
# get the room_key for this particular row
|
|
|
|
current_room_key = None
|
|
|
|
try:
|
|
|
|
current_room_key = yield self.store.get_e2e_room_key(
|
|
|
|
user_id, version, room_id, session_id
|
|
|
|
)
|
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
pass
|
|
|
|
else:
|
2018-10-12 06:26:18 -04:00
|
|
|
raise
|
2017-12-05 16:44:25 -05:00
|
|
|
|
2018-10-12 06:26:18 -04:00
|
|
|
if self._should_replace_room_key(current_room_key, room_key):
|
2017-12-17 20:52:46 -05:00
|
|
|
yield self.store.set_e2e_room_key(
|
|
|
|
user_id, version, room_id, session_id, room_key
|
|
|
|
)
|
|
|
|
|
2017-12-24 10:03:44 -05:00
|
|
|
@staticmethod
|
2017-12-17 20:52:46 -05:00
|
|
|
def _should_replace_room_key(current_room_key, room_key):
|
|
|
|
"""
|
2017-12-24 10:03:44 -05:00
|
|
|
Determine whether to replace a given current_room_key (if any)
|
|
|
|
with a newly uploaded room_key backup
|
2017-12-17 20:52:46 -05:00
|
|
|
|
|
|
|
Args:
|
|
|
|
current_room_key (dict): Optional, the current room_key dict if any
|
|
|
|
room_key (dict): The new room_key dict which may or may not be fit to
|
|
|
|
replace the current_room_key
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if current_room_key should be replaced by room_key in the backup
|
|
|
|
"""
|
|
|
|
|
2017-12-05 16:44:25 -05:00
|
|
|
if current_room_key:
|
2017-12-17 20:52:46 -05:00
|
|
|
# spelt out with if/elifs rather than nested boolean expressions
|
|
|
|
# purely for legibility.
|
|
|
|
|
2017-12-05 16:44:25 -05:00
|
|
|
if room_key['is_verified'] and not current_room_key['is_verified']:
|
2018-10-12 06:26:18 -04:00
|
|
|
return True
|
2017-12-05 16:44:25 -05:00
|
|
|
elif (
|
|
|
|
room_key['first_message_index'] <
|
|
|
|
current_room_key['first_message_index']
|
|
|
|
):
|
2018-10-12 06:26:18 -04:00
|
|
|
return True
|
2017-12-17 20:52:46 -05:00
|
|
|
elif room_key['forwarded_count'] < current_room_key['forwarded_count']:
|
2018-10-12 06:26:18 -04:00
|
|
|
return True
|
2017-12-05 16:44:25 -05:00
|
|
|
else:
|
2017-12-17 20:52:46 -05:00
|
|
|
return False
|
|
|
|
return True
|
2017-12-05 20:02:57 -05:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2017-12-06 04:02:49 -05:00
|
|
|
def create_version(self, user_id, version_info):
|
2017-12-27 18:37:44 -05:00
|
|
|
"""Create a new backup version. This automatically becomes the new
|
|
|
|
backup version for the user's keys; previous backups will no longer be
|
|
|
|
writeable to.
|
|
|
|
|
2017-12-27 18:58:51 -05:00
|
|
|
Args:
|
2017-12-27 18:37:44 -05:00
|
|
|
user_id(str): the user whose backup version we're creating
|
|
|
|
version_info(dict): metadata about the new version being created
|
|
|
|
|
|
|
|
{
|
|
|
|
"algorithm": "m.megolm_backup.v1",
|
|
|
|
"auth_data": "dGhpcyBzaG91bGQgYWN0dWFsbHkgYmUgZW5jcnlwdGVkIGpzb24K"
|
|
|
|
}
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A deferred of a string that gives the new version number.
|
|
|
|
"""
|
|
|
|
|
2017-12-05 20:02:57 -05:00
|
|
|
# TODO: Validate the JSON to make sure it has the right keys.
|
|
|
|
|
|
|
|
# lock everyone out until we've switched version
|
|
|
|
with (yield self._upload_linearizer.queue(user_id)):
|
2017-12-17 20:58:53 -05:00
|
|
|
new_version = yield self.store.create_e2e_room_keys_version(
|
2017-12-06 04:02:49 -05:00
|
|
|
user_id, version_info
|
2017-12-05 20:02:57 -05:00
|
|
|
)
|
2017-12-06 04:02:49 -05:00
|
|
|
defer.returnValue(new_version)
|
2017-12-05 20:02:57 -05:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2017-12-31 09:10:31 -05:00
|
|
|
def get_version_info(self, user_id, version=None):
|
|
|
|
"""Get the info about a given version of the user's backup
|
2017-12-27 18:37:44 -05:00
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id(str): the user whose current backup version we're querying
|
2017-12-31 09:10:31 -05:00
|
|
|
version(str): Optional; if None gives the most recent version
|
|
|
|
otherwise a historical one.
|
2017-12-27 18:37:44 -05:00
|
|
|
Raises:
|
2019-02-06 17:47:22 -05:00
|
|
|
NotFoundError: if the requested backup version doesn't exist
|
2017-12-27 18:37:44 -05:00
|
|
|
Returns:
|
|
|
|
A deferred of a info dict that gives the info about the new version.
|
|
|
|
|
|
|
|
{
|
2017-12-31 09:10:31 -05:00
|
|
|
"version": "1234",
|
2017-12-27 18:37:44 -05:00
|
|
|
"algorithm": "m.megolm_backup.v1",
|
|
|
|
"auth_data": "dGhpcyBzaG91bGQgYWN0dWFsbHkgYmUgZW5jcnlwdGVkIGpzb24K"
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2017-12-05 20:02:57 -05:00
|
|
|
with (yield self._upload_linearizer.queue(user_id)):
|
2019-02-06 17:47:22 -05:00
|
|
|
try:
|
|
|
|
res = yield self.store.get_e2e_room_keys_version_info(user_id, version)
|
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
raise NotFoundError("Unknown backup version")
|
|
|
|
else:
|
|
|
|
raise
|
2017-12-31 12:47:11 -05:00
|
|
|
defer.returnValue(res)
|
|
|
|
|
2017-12-05 20:02:57 -05:00
|
|
|
@defer.inlineCallbacks
|
2018-01-07 18:45:26 -05:00
|
|
|
def delete_version(self, user_id, version=None):
|
2017-12-27 18:37:44 -05:00
|
|
|
"""Deletes a given version of the user's e2e_room_keys backup
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id(str): the user whose current backup version we're deleting
|
|
|
|
version(str): the version id of the backup being deleted
|
|
|
|
Raises:
|
2019-02-06 17:47:22 -05:00
|
|
|
NotFoundError: if this backup version doesn't exist
|
2017-12-27 18:37:44 -05:00
|
|
|
"""
|
|
|
|
|
2017-12-05 20:02:57 -05:00
|
|
|
with (yield self._upload_linearizer.queue(user_id)):
|
2019-02-06 17:47:22 -05:00
|
|
|
try:
|
|
|
|
yield self.store.delete_e2e_room_keys_version(user_id, version)
|
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
raise NotFoundError("Unknown backup version")
|
|
|
|
else:
|
|
|
|
raise
|
2019-02-06 17:57:10 -05:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def update_version(self, user_id, version, version_info):
|
|
|
|
"""Update the info about a given version of the user's backup
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id(str): the user whose current backup version we're updating
|
|
|
|
version(str): the backup version we're updating
|
|
|
|
version_info(dict): the new information about the backup
|
|
|
|
Raises:
|
|
|
|
NotFoundError: if the requested backup version doesn't exist
|
|
|
|
Returns:
|
|
|
|
A deferred of an empty dict.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
old_info = yield self.store.get_e2e_room_keys_version_info(user_id, version)
|
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
raise NotFoundError("Unknown backup version")
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
if old_info["algorithm"] != version_info["algorithm"]:
|
|
|
|
raise SynapseError(
|
|
|
|
400,
|
|
|
|
"Algorithm does not match",
|
|
|
|
Codes.INVALID_PARAM
|
|
|
|
)
|
|
|
|
|
|
|
|
yield self.store.update_e2e_room_keys_version(user_id, version, version_info)
|
|
|
|
|
|
|
|
defer.returnValue({})
|