2017-12-04 18:29:25 -07:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2017 New Vector Ltd
|
|
|
|
#
|
|
|
|
# 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.
|
2017-12-05 10:54:48 -07:00
|
|
|
|
2018-10-12 04:48:56 -06:00
|
|
|
import json
|
2018-09-06 09:23:16 -06:00
|
|
|
|
2017-12-04 18:29:25 -07:00
|
|
|
from twisted.internet import defer
|
2018-09-06 09:23:16 -06:00
|
|
|
|
2018-01-07 16:58:32 -07:00
|
|
|
from synapse.api.errors import StoreError
|
2019-08-22 04:28:12 -06:00
|
|
|
from synapse.logging.opentracing import log_kv, trace
|
2017-12-04 18:29:25 -07:00
|
|
|
|
|
|
|
from ._base import SQLBaseStore
|
|
|
|
|
|
|
|
|
|
|
|
class EndToEndRoomKeyStore(SQLBaseStore):
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def get_e2e_room_key(self, user_id, version, room_id, session_id):
|
2017-12-24 09:44:18 -07:00
|
|
|
"""Get the encrypted E2E room key for a given session from a given
|
|
|
|
backup version of room_keys. We only store the 'best' room key for a given
|
|
|
|
session at a given time, as determined by the handler.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id(str): the user whose backup we're querying
|
|
|
|
version(str): the version ID of the backup for the set of keys we're querying
|
|
|
|
room_id(str): the ID of the room whose keys we're querying.
|
|
|
|
This is a bit redundant as it's implied by the session_id, but
|
|
|
|
we include for consistency with the rest of the API.
|
|
|
|
session_id(str): the session whose room_key we're querying.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A deferred dict giving the session_data and message metadata for
|
|
|
|
this room key.
|
|
|
|
"""
|
2017-12-04 18:29:25 -07:00
|
|
|
|
|
|
|
row = yield self._simple_select_one(
|
|
|
|
table="e2e_room_keys",
|
|
|
|
keyvalues={
|
|
|
|
"user_id": user_id,
|
|
|
|
"version": version,
|
|
|
|
"room_id": room_id,
|
|
|
|
"session_id": session_id,
|
|
|
|
},
|
|
|
|
retcols=(
|
|
|
|
"first_message_index",
|
|
|
|
"forwarded_count",
|
|
|
|
"is_verified",
|
|
|
|
"session_data",
|
|
|
|
),
|
|
|
|
desc="get_e2e_room_key",
|
|
|
|
)
|
|
|
|
|
2018-09-06 09:23:16 -06:00
|
|
|
row["session_data"] = json.loads(row["session_data"])
|
2018-08-21 12:51:34 -06:00
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return row
|
2017-12-04 18:29:25 -07:00
|
|
|
|
2017-12-31 10:47:11 -07:00
|
|
|
@defer.inlineCallbacks
|
2017-12-04 18:29:25 -07:00
|
|
|
def set_e2e_room_key(self, user_id, version, room_id, session_id, room_key):
|
2017-12-24 09:44:18 -07:00
|
|
|
"""Replaces or inserts the encrypted E2E room key for a given session in
|
|
|
|
a given backup
|
|
|
|
|
|
|
|
Args:
|
|
|
|
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
|
2017-12-27 16:35:10 -07:00
|
|
|
room_key(dict): the room_key being set
|
2017-12-24 09:44:18 -07:00
|
|
|
Raises:
|
2018-10-12 04:48:56 -06:00
|
|
|
StoreError
|
2017-12-24 09:44:18 -07:00
|
|
|
"""
|
2017-12-04 18:29:25 -07:00
|
|
|
|
2017-12-17 18:52:46 -07:00
|
|
|
yield self._simple_upsert(
|
|
|
|
table="e2e_room_keys",
|
|
|
|
keyvalues={
|
|
|
|
"user_id": user_id,
|
2019-08-14 19:02:58 -06:00
|
|
|
"version": version,
|
2017-12-17 18:52:46 -07:00
|
|
|
"room_id": room_id,
|
|
|
|
"session_id": session_id,
|
|
|
|
},
|
|
|
|
values={
|
2019-06-20 03:32:02 -06:00
|
|
|
"first_message_index": room_key["first_message_index"],
|
|
|
|
"forwarded_count": room_key["forwarded_count"],
|
|
|
|
"is_verified": room_key["is_verified"],
|
|
|
|
"session_data": json.dumps(room_key["session_data"]),
|
2017-12-17 18:52:46 -07:00
|
|
|
},
|
|
|
|
lock=False,
|
2017-12-04 18:29:25 -07:00
|
|
|
)
|
2019-08-22 04:28:12 -06:00
|
|
|
log_kv(
|
|
|
|
{
|
|
|
|
"message": "Set room key",
|
|
|
|
"room_id": room_id,
|
|
|
|
"session_id": session_id,
|
|
|
|
"room_key": room_key,
|
|
|
|
}
|
|
|
|
)
|
2017-12-04 18:29:25 -07:00
|
|
|
|
2019-08-22 04:28:12 -06:00
|
|
|
@trace
|
2017-12-04 18:29:25 -07:00
|
|
|
@defer.inlineCallbacks
|
2019-04-03 03:07:29 -06:00
|
|
|
def get_e2e_room_keys(self, user_id, version, room_id=None, session_id=None):
|
2017-12-24 09:44:18 -07:00
|
|
|
"""Bulk get the E2E room keys for a given backup, optionally filtered to a given
|
|
|
|
room, or a given session.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id(str): the user whose backup we're querying
|
|
|
|
version(str): the version ID of the backup for the set of keys we're querying
|
|
|
|
room_id(str): Optional. the ID of the room whose keys we're querying, if any.
|
|
|
|
If not specified, we return the keys for all the rooms in the backup.
|
|
|
|
session_id(str): Optional. the session whose room_key we're querying, if any.
|
|
|
|
If specified, we also require the room_id to be specified.
|
|
|
|
If not specified, we return all the keys in this version of
|
|
|
|
the backup (or for the specified room)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A deferred list of dicts giving the session_data and message metadata for
|
|
|
|
these room keys.
|
|
|
|
"""
|
2017-12-04 18:29:25 -07:00
|
|
|
|
2018-10-30 05:12:23 -06:00
|
|
|
try:
|
|
|
|
version = int(version)
|
|
|
|
except ValueError:
|
2019-07-23 07:00:55 -06:00
|
|
|
return {"rooms": {}}
|
2018-10-30 05:12:23 -06:00
|
|
|
|
2019-04-03 03:07:29 -06:00
|
|
|
keyvalues = {"user_id": user_id, "version": version}
|
2017-12-05 14:44:25 -07:00
|
|
|
if room_id:
|
2019-06-20 03:32:02 -06:00
|
|
|
keyvalues["room_id"] = room_id
|
2017-12-17 18:52:46 -07:00
|
|
|
if session_id:
|
2019-06-20 03:32:02 -06:00
|
|
|
keyvalues["session_id"] = session_id
|
2017-12-04 18:29:25 -07:00
|
|
|
|
|
|
|
rows = yield self._simple_select_list(
|
|
|
|
table="e2e_room_keys",
|
|
|
|
keyvalues=keyvalues,
|
|
|
|
retcols=(
|
2017-12-05 14:44:25 -07:00
|
|
|
"user_id",
|
|
|
|
"room_id",
|
|
|
|
"session_id",
|
2017-12-04 18:29:25 -07:00
|
|
|
"first_message_index",
|
|
|
|
"forwarded_count",
|
|
|
|
"is_verified",
|
|
|
|
"session_data",
|
|
|
|
),
|
|
|
|
desc="get_e2e_room_keys",
|
|
|
|
)
|
|
|
|
|
2019-06-20 03:32:02 -06:00
|
|
|
sessions = {"rooms": {}}
|
2017-12-05 14:44:25 -07:00
|
|
|
for row in rows:
|
2019-06-20 03:32:02 -06:00
|
|
|
room_entry = sessions["rooms"].setdefault(row["room_id"], {"sessions": {}})
|
|
|
|
room_entry["sessions"][row["session_id"]] = {
|
2017-12-05 14:44:25 -07:00
|
|
|
"first_message_index": row["first_message_index"],
|
|
|
|
"forwarded_count": row["forwarded_count"],
|
|
|
|
"is_verified": row["is_verified"],
|
2018-08-21 12:51:34 -06:00
|
|
|
"session_data": json.loads(row["session_data"]),
|
2017-12-05 14:44:25 -07:00
|
|
|
}
|
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return sessions
|
2017-12-05 10:54:48 -07:00
|
|
|
|
2019-08-22 04:28:12 -06:00
|
|
|
@trace
|
2017-12-05 10:54:48 -07:00
|
|
|
@defer.inlineCallbacks
|
2019-04-03 03:07:29 -06:00
|
|
|
def delete_e2e_room_keys(self, user_id, version, room_id=None, session_id=None):
|
2017-12-24 09:44:18 -07:00
|
|
|
"""Bulk delete the E2E room keys for a given backup, optionally filtered to a given
|
|
|
|
room or a given session.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id(str): the user whose backup we're deleting from
|
|
|
|
version(str): the version ID of the backup for the set of keys we're deleting
|
|
|
|
room_id(str): Optional. the ID of the room whose keys we're deleting, if any.
|
|
|
|
If not specified, we delete the keys for all the rooms in the backup.
|
|
|
|
session_id(str): Optional. the session whose room_key we're querying, if any.
|
|
|
|
If specified, we also require the room_id to be specified.
|
|
|
|
If not specified, we delete all the keys in this version of
|
|
|
|
the backup (or for the specified room)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A deferred of the deletion transaction
|
|
|
|
"""
|
2017-12-05 10:54:48 -07:00
|
|
|
|
2019-04-03 03:07:29 -06:00
|
|
|
keyvalues = {"user_id": user_id, "version": int(version)}
|
2017-12-05 14:44:25 -07:00
|
|
|
if room_id:
|
2019-06-20 03:32:02 -06:00
|
|
|
keyvalues["room_id"] = room_id
|
2017-12-17 18:52:46 -07:00
|
|
|
if session_id:
|
2019-06-20 03:32:02 -06:00
|
|
|
keyvalues["session_id"] = session_id
|
2017-12-05 10:54:48 -07:00
|
|
|
|
|
|
|
yield self._simple_delete(
|
2019-04-03 03:07:29 -06:00
|
|
|
table="e2e_room_keys", keyvalues=keyvalues, desc="delete_e2e_room_keys"
|
2017-12-05 10:54:48 -07:00
|
|
|
)
|
2017-12-05 18:02:57 -07:00
|
|
|
|
2018-01-07 16:45:55 -07:00
|
|
|
@staticmethod
|
|
|
|
def _get_current_version(txn, user_id):
|
|
|
|
txn.execute(
|
2018-10-05 08:08:36 -06:00
|
|
|
"SELECT MAX(version) FROM e2e_room_keys_versions "
|
|
|
|
"WHERE user_id=? AND deleted=0",
|
2019-04-03 03:07:29 -06:00
|
|
|
(user_id,),
|
2018-01-07 16:45:55 -07:00
|
|
|
)
|
|
|
|
row = txn.fetchone()
|
|
|
|
if not row:
|
2019-06-20 03:32:02 -06:00
|
|
|
raise StoreError(404, "No current backup version")
|
2018-01-07 16:45:55 -07:00
|
|
|
return row[0]
|
|
|
|
|
2017-12-27 16:35:10 -07:00
|
|
|
def get_e2e_room_keys_version_info(self, user_id, version=None):
|
|
|
|
"""Get info metadata about a version of our room_keys backup.
|
2017-12-24 09:44:18 -07:00
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id(str): the user whose backup we're querying
|
2017-12-27 16:35:10 -07:00
|
|
|
version(str): Optional. the version ID of the backup we're querying about
|
|
|
|
If missing, we return the information about the current version.
|
|
|
|
Raises:
|
|
|
|
StoreError: with code 404 if there are no e2e_room_keys_versions present
|
2017-12-24 09:44:18 -07:00
|
|
|
Returns:
|
2018-11-09 07:38:31 -07:00
|
|
|
A deferred dict giving the info metadata for this backup version, with
|
|
|
|
fields including:
|
2018-11-09 06:25:38 -07:00
|
|
|
version(str)
|
|
|
|
algorithm(str)
|
|
|
|
auth_data(object): opaque dict supplied by the client
|
2017-12-24 09:44:18 -07:00
|
|
|
"""
|
2017-12-05 18:02:57 -07:00
|
|
|
|
2017-12-27 16:35:10 -07:00
|
|
|
def _get_e2e_room_keys_version_info_txn(txn):
|
|
|
|
if version is None:
|
2018-01-07 16:45:55 -07:00
|
|
|
this_version = self._get_current_version(txn, user_id)
|
2017-12-27 16:58:51 -07:00
|
|
|
else:
|
2018-10-30 05:01:07 -06:00
|
|
|
try:
|
|
|
|
this_version = int(version)
|
|
|
|
except ValueError:
|
|
|
|
# Our versions are all ints so if we can't convert it to an integer,
|
|
|
|
# it isn't there.
|
|
|
|
raise StoreError(404, "No row found")
|
2017-12-27 16:35:10 -07:00
|
|
|
|
2018-08-21 08:38:00 -06:00
|
|
|
result = self._simple_select_one_txn(
|
2017-12-31 07:35:25 -07:00
|
|
|
txn,
|
2017-12-27 16:35:10 -07:00
|
|
|
table="e2e_room_keys_versions",
|
2019-04-03 03:07:29 -06:00
|
|
|
keyvalues={"user_id": user_id, "version": this_version, "deleted": 0},
|
|
|
|
retcols=("version", "algorithm", "auth_data"),
|
2017-12-27 16:35:10 -07:00
|
|
|
)
|
2018-08-21 08:38:00 -06:00
|
|
|
result["auth_data"] = json.loads(result["auth_data"])
|
2018-10-30 04:35:18 -06:00
|
|
|
result["version"] = str(result["version"])
|
2018-08-21 08:38:00 -06:00
|
|
|
return result
|
2017-12-27 16:35:10 -07:00
|
|
|
|
|
|
|
return self.runInteraction(
|
2019-04-03 03:07:29 -06:00
|
|
|
"get_e2e_room_keys_version_info", _get_e2e_room_keys_version_info_txn
|
2017-12-05 18:02:57 -07:00
|
|
|
)
|
|
|
|
|
2019-08-22 04:28:12 -06:00
|
|
|
@trace
|
2017-12-17 18:58:53 -07:00
|
|
|
def create_e2e_room_keys_version(self, user_id, info):
|
2017-12-06 02:02:49 -07:00
|
|
|
"""Atomically creates a new version of this user's e2e_room_keys store
|
|
|
|
with the given version info.
|
2017-12-24 09:44:18 -07:00
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id(str): the user whose backup we're creating a version
|
|
|
|
info(dict): the info about the backup version to be created
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A deferred string for the newly created version ID
|
2017-12-06 02:02:49 -07:00
|
|
|
"""
|
2017-12-05 18:02:57 -07:00
|
|
|
|
2017-12-17 18:58:53 -07:00
|
|
|
def _create_e2e_room_keys_version_txn(txn):
|
2017-12-06 02:02:49 -07:00
|
|
|
txn.execute(
|
2017-12-17 18:58:53 -07:00
|
|
|
"SELECT MAX(version) FROM e2e_room_keys_versions WHERE user_id=?",
|
2019-04-03 03:07:29 -06:00
|
|
|
(user_id,),
|
2017-12-06 02:02:49 -07:00
|
|
|
)
|
|
|
|
current_version = txn.fetchone()[0]
|
|
|
|
if current_version is None:
|
2019-06-20 03:32:02 -06:00
|
|
|
current_version = "0"
|
2017-12-06 02:02:49 -07:00
|
|
|
|
2017-12-31 07:35:25 -07:00
|
|
|
new_version = str(int(current_version) + 1)
|
2017-12-06 02:02:49 -07:00
|
|
|
|
2017-12-05 18:02:57 -07:00
|
|
|
self._simple_insert_txn(
|
|
|
|
txn,
|
2017-12-17 18:58:53 -07:00
|
|
|
table="e2e_room_keys_versions",
|
2017-12-05 18:02:57 -07:00
|
|
|
values={
|
|
|
|
"user_id": user_id,
|
2017-12-06 02:02:49 -07:00
|
|
|
"version": new_version,
|
2017-12-05 18:02:57 -07:00
|
|
|
"algorithm": info["algorithm"],
|
2018-08-21 08:38:00 -06:00
|
|
|
"auth_data": json.dumps(info["auth_data"]),
|
2017-12-05 18:02:57 -07:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2017-12-06 02:02:49 -07:00
|
|
|
return new_version
|
2017-12-05 18:02:57 -07:00
|
|
|
|
|
|
|
return self.runInteraction(
|
2017-12-17 18:58:53 -07:00
|
|
|
"create_e2e_room_keys_version_txn", _create_e2e_room_keys_version_txn
|
2017-12-05 18:02:57 -07:00
|
|
|
)
|
|
|
|
|
2019-08-22 04:28:12 -06:00
|
|
|
@trace
|
2019-02-06 15:57:10 -07:00
|
|
|
def update_e2e_room_keys_version(self, user_id, version, info):
|
|
|
|
"""Update a given backup version
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id(str): the user whose backup version we're updating
|
|
|
|
version(str): the version ID of the backup version we're updating
|
|
|
|
info(dict): the new backup version info to store
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._simple_update(
|
|
|
|
table="e2e_room_keys_versions",
|
2019-04-03 03:07:29 -06:00
|
|
|
keyvalues={"user_id": user_id, "version": version},
|
|
|
|
updatevalues={"auth_data": json.dumps(info["auth_data"])},
|
|
|
|
desc="update_e2e_room_keys_version",
|
2019-02-06 15:57:10 -07:00
|
|
|
)
|
|
|
|
|
2019-08-22 04:28:12 -06:00
|
|
|
@trace
|
2018-01-07 16:45:55 -07:00
|
|
|
def delete_e2e_room_keys_version(self, user_id, version=None):
|
2017-12-24 09:44:18 -07:00
|
|
|
"""Delete a given backup version of the user's room keys.
|
|
|
|
Doesn't delete their actual key data.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id(str): the user whose backup version we're deleting
|
2018-01-07 16:45:55 -07:00
|
|
|
version(str): Optional. the version ID of the backup version we're deleting
|
|
|
|
If missing, we delete the current backup version info.
|
|
|
|
Raises:
|
|
|
|
StoreError: with code 404 if there are no e2e_room_keys_versions present,
|
|
|
|
or if the version requested doesn't exist.
|
2017-12-24 09:44:18 -07:00
|
|
|
"""
|
2017-12-05 18:02:57 -07:00
|
|
|
|
2018-01-07 16:45:55 -07:00
|
|
|
def _delete_e2e_room_keys_version_txn(txn):
|
|
|
|
if version is None:
|
|
|
|
this_version = self._get_current_version(txn, user_id)
|
|
|
|
else:
|
|
|
|
this_version = version
|
2017-12-05 18:02:57 -07:00
|
|
|
|
2018-10-05 08:08:36 -06:00
|
|
|
return self._simple_update_one_txn(
|
2018-01-07 16:45:55 -07:00
|
|
|
txn,
|
|
|
|
table="e2e_room_keys_versions",
|
2019-04-03 03:07:29 -06:00
|
|
|
keyvalues={"user_id": user_id, "version": this_version},
|
|
|
|
updatevalues={"deleted": 1},
|
2018-01-07 16:45:55 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
return self.runInteraction(
|
2019-04-03 03:07:29 -06:00
|
|
|
"delete_e2e_room_keys_version", _delete_e2e_room_keys_version_txn
|
2017-12-05 18:02:57 -07:00
|
|
|
)
|