2017-12-04 20:29:25 -05:00
|
|
|
# Copyright 2017 New Vector Ltd
|
2019-11-27 16:14:44 -05:00
|
|
|
# Copyright 2019 Matrix.org Foundation C.I.C.
|
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.
|
2017-12-05 12:54:48 -05:00
|
|
|
|
2021-12-14 12:46:47 -05:00
|
|
|
from typing import Dict, Iterable, Mapping, Optional, Tuple, cast
|
|
|
|
|
|
|
|
from typing_extensions import Literal, TypedDict
|
2020-08-27 07:08:38 -04:00
|
|
|
|
2018-01-07 18:58:32 -05:00
|
|
|
from synapse.api.errors import StoreError
|
2019-08-22 06:28:12 -04:00
|
|
|
from synapse.logging.opentracing import log_kv, trace
|
2020-07-16 11:32:19 -04:00
|
|
|
from synapse.storage._base import SQLBaseStore, db_to_json
|
2021-12-14 12:46:47 -05:00
|
|
|
from synapse.storage.database import LoggingTransaction
|
2022-05-16 11:35:31 -04:00
|
|
|
from synapse.types import JsonDict, JsonSerializable, StreamKeyType
|
2020-08-07 08:02:55 -04:00
|
|
|
from synapse.util import json_encoder
|
2017-12-04 20:29:25 -05:00
|
|
|
|
|
|
|
|
2021-12-14 12:46:47 -05:00
|
|
|
class RoomKey(TypedDict):
|
|
|
|
"""`KeyBackupData` in the Matrix spec.
|
|
|
|
|
|
|
|
https://spec.matrix.org/v1.1/client-server-api/#get_matrixclientv3room_keyskeysroomidsessionid
|
|
|
|
"""
|
|
|
|
|
|
|
|
first_message_index: int
|
|
|
|
forwarded_count: int
|
|
|
|
is_verified: bool
|
|
|
|
session_data: JsonSerializable
|
|
|
|
|
|
|
|
|
2017-12-04 20:29:25 -05:00
|
|
|
class EndToEndRoomKeyStore(SQLBaseStore):
|
2021-12-14 12:46:47 -05:00
|
|
|
"""The store for end to end room key backups.
|
|
|
|
|
|
|
|
See https://spec.matrix.org/v1.1/client-server-api/#server-side-key-backups
|
|
|
|
|
|
|
|
As per the spec, backups are identified by an opaque version string. Internally,
|
|
|
|
version identifiers are assigned using incrementing integers. Non-numeric version
|
|
|
|
strings are treated as if they do not exist, since we would have never issued them.
|
|
|
|
"""
|
|
|
|
|
2020-08-07 13:36:29 -04:00
|
|
|
async def update_e2e_room_key(
|
2021-12-14 12:46:47 -05:00
|
|
|
self,
|
|
|
|
user_id: str,
|
|
|
|
version: str,
|
|
|
|
room_id: str,
|
|
|
|
session_id: str,
|
|
|
|
room_key: RoomKey,
|
|
|
|
) -> None:
|
2019-11-27 16:14:44 -05:00
|
|
|
"""Replaces the encrypted E2E room key for a given session in a given backup
|
2017-12-24 11:44:18 -05:00
|
|
|
|
|
|
|
Args:
|
2021-12-14 12:46:47 -05:00
|
|
|
user_id: the user whose backup we're setting
|
|
|
|
version: the version ID of the backup we're updating
|
|
|
|
room_id: the ID of the room whose keys we're setting
|
|
|
|
session_id: the session whose room_key we're setting
|
|
|
|
room_key: the room_key being set
|
2017-12-24 11:44:18 -05:00
|
|
|
Raises:
|
2018-10-12 06:48:56 -04:00
|
|
|
StoreError
|
2017-12-24 11:44:18 -05:00
|
|
|
"""
|
2021-12-14 12:46:47 -05:00
|
|
|
try:
|
|
|
|
version_int = int(version)
|
|
|
|
except ValueError:
|
|
|
|
# Our versions are all ints so if we can't convert it to an integer,
|
|
|
|
# it doesn't exist.
|
|
|
|
raise StoreError(404, "No backup with that version exists")
|
2017-12-04 20:29:25 -05:00
|
|
|
|
2020-08-07 13:36:29 -04:00
|
|
|
await self.db_pool.simple_update_one(
|
2017-12-17 20:52:46 -05:00
|
|
|
table="e2e_room_keys",
|
|
|
|
keyvalues={
|
|
|
|
"user_id": user_id,
|
2021-12-14 12:46:47 -05:00
|
|
|
"version": version_int,
|
2017-12-17 20:52:46 -05:00
|
|
|
"room_id": room_id,
|
|
|
|
"session_id": session_id,
|
|
|
|
},
|
2019-11-27 16:14:44 -05:00
|
|
|
updatevalues={
|
2017-12-17 20:52:46 -05:00
|
|
|
"first_message_index": room_key["first_message_index"],
|
|
|
|
"forwarded_count": room_key["forwarded_count"],
|
|
|
|
"is_verified": room_key["is_verified"],
|
2020-08-07 08:02:55 -04:00
|
|
|
"session_data": json_encoder.encode(room_key["session_data"]),
|
2017-12-17 20:52:46 -05:00
|
|
|
},
|
2019-11-27 16:14:44 -05:00
|
|
|
desc="update_e2e_room_key",
|
2017-12-04 20:29:25 -05:00
|
|
|
)
|
2019-11-27 16:14:44 -05:00
|
|
|
|
2021-12-14 12:46:47 -05:00
|
|
|
async def add_e2e_room_keys(
|
|
|
|
self, user_id: str, version: str, room_keys: Iterable[Tuple[str, str, RoomKey]]
|
|
|
|
) -> None:
|
2019-11-27 16:14:44 -05:00
|
|
|
"""Bulk add room keys to a given backup.
|
|
|
|
|
|
|
|
Args:
|
2021-12-14 12:46:47 -05:00
|
|
|
user_id: the user whose backup we're adding to
|
|
|
|
version: the version ID of the backup for the set of keys we're adding to
|
|
|
|
room_keys: the keys to add, in the form (roomID, sessionID, keyData)
|
2019-11-27 16:14:44 -05:00
|
|
|
"""
|
2021-12-14 12:46:47 -05:00
|
|
|
try:
|
|
|
|
version_int = int(version)
|
|
|
|
except ValueError:
|
|
|
|
# Our versions are all ints so if we can't convert it to an integer,
|
|
|
|
# it doesn't exist.
|
|
|
|
raise StoreError(404, "No backup with that version exists")
|
2019-11-27 16:14:44 -05:00
|
|
|
|
|
|
|
values = []
|
|
|
|
for room_id, session_id, room_key in room_keys:
|
|
|
|
values.append(
|
2022-01-13 19:44:18 -05:00
|
|
|
(
|
|
|
|
user_id,
|
|
|
|
version_int,
|
|
|
|
room_id,
|
|
|
|
session_id,
|
|
|
|
room_key["first_message_index"],
|
|
|
|
room_key["forwarded_count"],
|
|
|
|
room_key["is_verified"],
|
|
|
|
json_encoder.encode(room_key["session_data"]),
|
|
|
|
)
|
2019-11-27 16:14:44 -05:00
|
|
|
)
|
|
|
|
log_kv(
|
|
|
|
{
|
|
|
|
"message": "Set room key",
|
|
|
|
"room_id": room_id,
|
|
|
|
"session_id": session_id,
|
2022-05-16 11:35:31 -04:00
|
|
|
StreamKeyType.ROOM: room_key,
|
2019-11-27 16:14:44 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-08-07 13:36:29 -04:00
|
|
|
await self.db_pool.simple_insert_many(
|
2022-01-13 19:44:18 -05:00
|
|
|
table="e2e_room_keys",
|
|
|
|
keys=(
|
|
|
|
"user_id",
|
|
|
|
"version",
|
|
|
|
"room_id",
|
|
|
|
"session_id",
|
|
|
|
"first_message_index",
|
|
|
|
"forwarded_count",
|
|
|
|
"is_verified",
|
|
|
|
"session_data",
|
|
|
|
),
|
|
|
|
values=values,
|
|
|
|
desc="add_e2e_room_keys",
|
2019-08-22 06:28:12 -04:00
|
|
|
)
|
2017-12-04 20:29:25 -05:00
|
|
|
|
2019-08-22 06:28:12 -04:00
|
|
|
@trace
|
2021-12-14 12:46:47 -05:00
|
|
|
async def get_e2e_room_keys(
|
|
|
|
self,
|
|
|
|
user_id: str,
|
|
|
|
version: str,
|
|
|
|
room_id: Optional[str] = None,
|
|
|
|
session_id: Optional[str] = None,
|
|
|
|
) -> Dict[
|
|
|
|
Literal["rooms"], Dict[str, Dict[Literal["sessions"], Dict[str, RoomKey]]]
|
|
|
|
]:
|
2017-12-24 11:44:18 -05:00
|
|
|
"""Bulk get the E2E room keys for a given backup, optionally filtered to a given
|
|
|
|
room, or a given session.
|
|
|
|
|
|
|
|
Args:
|
2021-12-14 12:46:47 -05:00
|
|
|
user_id: the user whose backup we're querying
|
|
|
|
version: the version ID of the backup for the set of keys we're querying
|
|
|
|
room_id: Optional. the ID of the room whose keys we're querying, if any.
|
2017-12-24 11:44:18 -05:00
|
|
|
If not specified, we return the keys for all the rooms in the backup.
|
2021-12-14 12:46:47 -05:00
|
|
|
session_id: Optional. the session whose room_key we're querying, if any.
|
2017-12-24 11:44:18 -05:00
|
|
|
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:
|
2021-12-14 12:46:47 -05:00
|
|
|
A dict giving the session_data and message metadata for these room keys.
|
|
|
|
`{"rooms": {room_id: {"sessions": {session_id: room_key}}}}`
|
2017-12-24 11:44:18 -05:00
|
|
|
"""
|
2017-12-04 20:29:25 -05:00
|
|
|
|
2018-10-30 07:12:23 -04:00
|
|
|
try:
|
2021-12-14 12:46:47 -05:00
|
|
|
version_int = int(version)
|
2018-10-30 07:12:23 -04:00
|
|
|
except ValueError:
|
2019-07-23 09:00:55 -04:00
|
|
|
return {"rooms": {}}
|
2018-10-30 07:12:23 -04:00
|
|
|
|
2021-12-14 12:46:47 -05:00
|
|
|
keyvalues = {"user_id": user_id, "version": version_int}
|
2017-12-05 16:44:25 -05:00
|
|
|
if room_id:
|
|
|
|
keyvalues["room_id"] = room_id
|
2017-12-17 20:52:46 -05:00
|
|
|
if session_id:
|
|
|
|
keyvalues["session_id"] = session_id
|
2017-12-04 20:29:25 -05:00
|
|
|
|
2020-08-07 13:36:29 -04:00
|
|
|
rows = await self.db_pool.simple_select_list(
|
2017-12-04 20:29:25 -05:00
|
|
|
table="e2e_room_keys",
|
|
|
|
keyvalues=keyvalues,
|
|
|
|
retcols=(
|
2017-12-05 16:44:25 -05:00
|
|
|
"user_id",
|
|
|
|
"room_id",
|
|
|
|
"session_id",
|
2017-12-04 20:29:25 -05:00
|
|
|
"first_message_index",
|
|
|
|
"forwarded_count",
|
|
|
|
"is_verified",
|
|
|
|
"session_data",
|
|
|
|
),
|
|
|
|
desc="get_e2e_room_keys",
|
|
|
|
)
|
|
|
|
|
2021-12-14 12:46:47 -05:00
|
|
|
sessions: Dict[
|
|
|
|
Literal["rooms"], Dict[str, Dict[Literal["sessions"], Dict[str, RoomKey]]]
|
|
|
|
] = {"rooms": {}}
|
2017-12-05 16:44:25 -05:00
|
|
|
for row in rows:
|
2017-12-17 20:52:46 -05:00
|
|
|
room_entry = sessions["rooms"].setdefault(row["room_id"], {"sessions": {}})
|
|
|
|
room_entry["sessions"][row["session_id"]] = {
|
2017-12-05 16:44:25 -05:00
|
|
|
"first_message_index": row["first_message_index"],
|
|
|
|
"forwarded_count": row["forwarded_count"],
|
2020-03-27 09:30:22 -04:00
|
|
|
# is_verified must be returned to the client as a boolean
|
|
|
|
"is_verified": bool(row["is_verified"]),
|
2020-07-16 11:32:19 -04:00
|
|
|
"session_data": db_to_json(row["session_data"]),
|
2017-12-05 16:44:25 -05:00
|
|
|
}
|
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return sessions
|
2017-12-05 12:54:48 -05:00
|
|
|
|
2021-12-14 12:46:47 -05:00
|
|
|
async def get_e2e_room_keys_multi(
|
|
|
|
self,
|
|
|
|
user_id: str,
|
|
|
|
version: str,
|
|
|
|
room_keys: Mapping[str, Mapping[Literal["sessions"], Iterable[str]]],
|
|
|
|
) -> Dict[str, Dict[str, RoomKey]]:
|
2019-11-27 16:14:44 -05:00
|
|
|
"""Get multiple room keys at a time. The difference between this function and
|
|
|
|
get_e2e_room_keys is that this function can be used to retrieve
|
|
|
|
multiple specific keys at a time, whereas get_e2e_room_keys is used for
|
|
|
|
getting all the keys in a backup version, all the keys for a room, or a
|
|
|
|
specific key.
|
|
|
|
|
|
|
|
Args:
|
2021-12-14 12:46:47 -05:00
|
|
|
user_id: the user whose backup we're querying
|
|
|
|
version: the version ID of the backup we're querying about
|
|
|
|
room_keys: a map from room ID -> {"sessions": [session ids]}
|
|
|
|
indicating the session IDs that we want to query
|
2019-11-27 16:14:44 -05:00
|
|
|
|
|
|
|
Returns:
|
2021-12-14 12:46:47 -05:00
|
|
|
A map of room IDs to session IDs to room key
|
2019-11-27 16:14:44 -05:00
|
|
|
"""
|
2021-12-14 12:46:47 -05:00
|
|
|
try:
|
|
|
|
version_int = int(version)
|
|
|
|
except ValueError:
|
|
|
|
# Our versions are all ints so if we can't convert it to an integer,
|
|
|
|
# it doesn't exist.
|
|
|
|
return {}
|
2019-11-27 16:14:44 -05:00
|
|
|
|
2020-08-28 07:54:27 -04:00
|
|
|
return await self.db_pool.runInteraction(
|
2019-11-27 16:14:44 -05:00
|
|
|
"get_e2e_room_keys_multi",
|
|
|
|
self._get_e2e_room_keys_multi_txn,
|
|
|
|
user_id,
|
2021-12-14 12:46:47 -05:00
|
|
|
version_int,
|
2019-11-27 16:14:44 -05:00
|
|
|
room_keys,
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
2021-12-14 12:46:47 -05:00
|
|
|
def _get_e2e_room_keys_multi_txn(
|
|
|
|
txn: LoggingTransaction,
|
|
|
|
user_id: str,
|
|
|
|
version: int,
|
|
|
|
room_keys: Mapping[str, Mapping[Literal["sessions"], Iterable[str]]],
|
|
|
|
) -> Dict[str, Dict[str, RoomKey]]:
|
2019-11-27 16:14:44 -05:00
|
|
|
if not room_keys:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
where_clauses = []
|
|
|
|
params = [user_id, version]
|
|
|
|
for room_id, room in room_keys.items():
|
|
|
|
sessions = list(room["sessions"])
|
|
|
|
if not sessions:
|
|
|
|
continue
|
|
|
|
params.append(room_id)
|
|
|
|
params.extend(sessions)
|
|
|
|
where_clauses.append(
|
|
|
|
"(room_id = ? AND session_id IN (%s))"
|
|
|
|
% (",".join(["?" for _ in sessions]),)
|
|
|
|
)
|
|
|
|
|
|
|
|
# check if we're actually querying something
|
|
|
|
if not where_clauses:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
sql = """
|
|
|
|
SELECT room_id, session_id, first_message_index, forwarded_count,
|
|
|
|
is_verified, session_data
|
|
|
|
FROM e2e_room_keys
|
|
|
|
WHERE user_id = ? AND version = ? AND (%s)
|
|
|
|
""" % (
|
|
|
|
" OR ".join(where_clauses)
|
|
|
|
)
|
|
|
|
|
|
|
|
txn.execute(sql, params)
|
|
|
|
|
2021-12-14 12:46:47 -05:00
|
|
|
ret: Dict[str, Dict[str, RoomKey]] = {}
|
2019-11-27 16:14:44 -05:00
|
|
|
|
|
|
|
for row in txn:
|
|
|
|
room_id = row[0]
|
|
|
|
session_id = row[1]
|
|
|
|
ret.setdefault(room_id, {})
|
|
|
|
ret[room_id][session_id] = {
|
|
|
|
"first_message_index": row[2],
|
|
|
|
"forwarded_count": row[3],
|
|
|
|
"is_verified": row[4],
|
2020-07-16 11:32:19 -04:00
|
|
|
"session_data": db_to_json(row[5]),
|
2019-11-27 16:14:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
2020-08-26 07:19:32 -04:00
|
|
|
async def count_e2e_room_keys(self, user_id: str, version: str) -> int:
|
2019-11-27 16:14:44 -05:00
|
|
|
"""Get the number of keys in a backup version.
|
|
|
|
|
|
|
|
Args:
|
2020-08-26 07:19:32 -04:00
|
|
|
user_id: the user whose backup we're querying
|
|
|
|
version: the version ID of the backup we're querying about
|
2019-11-27 16:14:44 -05:00
|
|
|
"""
|
2021-12-14 12:46:47 -05:00
|
|
|
try:
|
|
|
|
version_int = int(version)
|
|
|
|
except ValueError:
|
|
|
|
# Our versions are all ints so if we can't convert it to an integer,
|
|
|
|
# it doesn't exist.
|
|
|
|
return 0
|
2019-11-27 16:14:44 -05:00
|
|
|
|
2020-08-26 07:19:32 -04:00
|
|
|
return await self.db_pool.simple_select_one_onecol(
|
2019-11-27 16:14:44 -05:00
|
|
|
table="e2e_room_keys",
|
2021-12-14 12:46:47 -05:00
|
|
|
keyvalues={"user_id": user_id, "version": version_int},
|
2019-11-27 16:14:44 -05:00
|
|
|
retcol="COUNT(*)",
|
|
|
|
desc="count_e2e_room_keys",
|
|
|
|
)
|
|
|
|
|
2019-08-22 06:28:12 -04:00
|
|
|
@trace
|
2020-08-07 13:36:29 -04:00
|
|
|
async def delete_e2e_room_keys(
|
2021-12-14 12:46:47 -05:00
|
|
|
self,
|
|
|
|
user_id: str,
|
|
|
|
version: str,
|
|
|
|
room_id: Optional[str] = None,
|
|
|
|
session_id: Optional[str] = None,
|
|
|
|
) -> None:
|
2017-12-24 11:44:18 -05:00
|
|
|
"""Bulk delete the E2E room keys for a given backup, optionally filtered to a given
|
|
|
|
room or a given session.
|
|
|
|
|
|
|
|
Args:
|
2021-12-14 12:46:47 -05:00
|
|
|
user_id: the user whose backup we're deleting from
|
|
|
|
version: the version ID of the backup for the set of keys we're deleting
|
|
|
|
room_id: Optional. the ID of the room whose keys we're deleting, if any.
|
2017-12-24 11:44:18 -05:00
|
|
|
If not specified, we delete the keys for all the rooms in the backup.
|
2021-12-14 12:46:47 -05:00
|
|
|
session_id: Optional. the session whose room_key we're querying, if any.
|
2017-12-24 11:44:18 -05:00
|
|
|
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)
|
|
|
|
"""
|
2021-12-14 12:46:47 -05:00
|
|
|
try:
|
|
|
|
version_int = int(version)
|
|
|
|
except ValueError:
|
|
|
|
# Our versions are all ints so if we can't convert it to an integer,
|
|
|
|
# it doesn't exist.
|
|
|
|
return
|
2017-12-05 12:54:48 -05:00
|
|
|
|
2021-12-14 12:46:47 -05:00
|
|
|
keyvalues = {"user_id": user_id, "version": version_int}
|
2017-12-05 16:44:25 -05:00
|
|
|
if room_id:
|
|
|
|
keyvalues["room_id"] = room_id
|
2017-12-17 20:52:46 -05:00
|
|
|
if session_id:
|
|
|
|
keyvalues["session_id"] = session_id
|
2017-12-05 12:54:48 -05:00
|
|
|
|
2020-08-07 13:36:29 -04:00
|
|
|
await self.db_pool.simple_delete(
|
2019-04-03 05:07:29 -04:00
|
|
|
table="e2e_room_keys", keyvalues=keyvalues, desc="delete_e2e_room_keys"
|
2017-12-05 12:54:48 -05:00
|
|
|
)
|
2017-12-05 20:02:57 -05:00
|
|
|
|
2018-01-07 18:45:55 -05:00
|
|
|
@staticmethod
|
2021-12-14 12:46:47 -05:00
|
|
|
def _get_current_version(txn: LoggingTransaction, user_id: str) -> int:
|
2018-01-07 18:45:55 -05:00
|
|
|
txn.execute(
|
2018-10-05 10:08:36 -04:00
|
|
|
"SELECT MAX(version) FROM e2e_room_keys_versions "
|
|
|
|
"WHERE user_id=? AND deleted=0",
|
2019-04-03 05:07:29 -04:00
|
|
|
(user_id,),
|
2018-01-07 18:45:55 -05:00
|
|
|
)
|
2021-12-14 12:46:47 -05:00
|
|
|
# `SELECT MAX() FROM ...` will always return 1 row. The value in that row will
|
|
|
|
# be `NULL` when there are no available versions.
|
|
|
|
row = cast(Tuple[Optional[int]], txn.fetchone())
|
|
|
|
if row[0] is None:
|
2018-01-07 18:45:55 -05:00
|
|
|
raise StoreError(404, "No current backup version")
|
|
|
|
return row[0]
|
|
|
|
|
2021-12-14 12:46:47 -05:00
|
|
|
async def get_e2e_room_keys_version_info(
|
|
|
|
self, user_id: str, version: Optional[str] = None
|
|
|
|
) -> JsonDict:
|
2017-12-27 18:35:10 -05:00
|
|
|
"""Get info metadata about a version of our room_keys backup.
|
2017-12-24 11:44:18 -05:00
|
|
|
|
|
|
|
Args:
|
2021-12-14 12:46:47 -05:00
|
|
|
user_id: the user whose backup we're querying
|
|
|
|
version: Optional. the version ID of the backup we're querying about
|
2017-12-27 18:35:10 -05:00
|
|
|
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 11:44:18 -05:00
|
|
|
Returns:
|
2020-08-28 07:54:27 -04:00
|
|
|
A dict giving the info metadata for this backup version, with
|
2018-11-09 09:38:31 -05:00
|
|
|
fields including:
|
2022-11-16 10:25:24 -05:00
|
|
|
version (str)
|
|
|
|
algorithm (str)
|
|
|
|
auth_data (object): opaque dict supplied by the client
|
|
|
|
etag (int): tag of the keys in the backup
|
2017-12-24 11:44:18 -05:00
|
|
|
"""
|
2017-12-05 20:02:57 -05:00
|
|
|
|
2021-12-14 12:46:47 -05:00
|
|
|
def _get_e2e_room_keys_version_info_txn(txn: LoggingTransaction) -> JsonDict:
|
2017-12-27 18:35:10 -05:00
|
|
|
if version is None:
|
2018-01-07 18:45:55 -05:00
|
|
|
this_version = self._get_current_version(txn, user_id)
|
2017-12-27 18:58:51 -05:00
|
|
|
else:
|
2018-10-30 07:01:07 -04: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.
|
2021-12-14 12:46:47 -05:00
|
|
|
raise StoreError(404, "No backup with that version exists")
|
2017-12-27 18:35:10 -05:00
|
|
|
|
2020-08-05 16:38:57 -04:00
|
|
|
result = self.db_pool.simple_select_one_txn(
|
2017-12-31 09:35:25 -05:00
|
|
|
txn,
|
2017-12-27 18:35:10 -05:00
|
|
|
table="e2e_room_keys_versions",
|
2019-04-03 05:07:29 -04:00
|
|
|
keyvalues={"user_id": user_id, "version": this_version, "deleted": 0},
|
2019-11-27 16:14:44 -05:00
|
|
|
retcols=("version", "algorithm", "auth_data", "etag"),
|
2021-12-14 12:46:47 -05:00
|
|
|
allow_none=False,
|
2017-12-27 18:35:10 -05:00
|
|
|
)
|
2021-12-14 12:46:47 -05:00
|
|
|
assert result is not None # see comment on `simple_select_one_txn`
|
2020-07-16 11:32:19 -04:00
|
|
|
result["auth_data"] = db_to_json(result["auth_data"])
|
2018-10-30 06:35:18 -04:00
|
|
|
result["version"] = str(result["version"])
|
2019-11-27 16:14:44 -05:00
|
|
|
if result["etag"] is None:
|
|
|
|
result["etag"] = 0
|
2018-08-21 10:38:00 -04:00
|
|
|
return result
|
2017-12-27 18:35:10 -05:00
|
|
|
|
2020-08-28 07:54:27 -04:00
|
|
|
return await self.db_pool.runInteraction(
|
2019-04-03 05:07:29 -04:00
|
|
|
"get_e2e_room_keys_version_info", _get_e2e_room_keys_version_info_txn
|
2017-12-05 20:02:57 -05:00
|
|
|
)
|
|
|
|
|
2019-08-22 06:28:12 -04:00
|
|
|
@trace
|
2021-12-14 12:46:47 -05:00
|
|
|
async def create_e2e_room_keys_version(self, user_id: str, info: JsonDict) -> str:
|
2017-12-06 04:02:49 -05:00
|
|
|
"""Atomically creates a new version of this user's e2e_room_keys store
|
|
|
|
with the given version info.
|
2017-12-24 11:44:18 -05:00
|
|
|
|
|
|
|
Args:
|
2021-12-14 12:46:47 -05:00
|
|
|
user_id: the user whose backup we're creating a version
|
|
|
|
info: the info about the backup version to be created
|
2017-12-24 11:44:18 -05:00
|
|
|
|
|
|
|
Returns:
|
2020-08-28 07:54:27 -04:00
|
|
|
The newly created version ID
|
2017-12-06 04:02:49 -05:00
|
|
|
"""
|
2017-12-05 20:02:57 -05:00
|
|
|
|
2021-12-14 12:46:47 -05:00
|
|
|
def _create_e2e_room_keys_version_txn(txn: LoggingTransaction) -> str:
|
2017-12-06 04:02:49 -05:00
|
|
|
txn.execute(
|
2017-12-17 20:58:53 -05:00
|
|
|
"SELECT MAX(version) FROM e2e_room_keys_versions WHERE user_id=?",
|
2019-04-03 05:07:29 -04:00
|
|
|
(user_id,),
|
2017-12-06 04:02:49 -05:00
|
|
|
)
|
2021-12-14 12:46:47 -05:00
|
|
|
current_version = cast(Tuple[Optional[int]], txn.fetchone())[0]
|
2017-12-06 04:02:49 -05:00
|
|
|
if current_version is None:
|
2021-12-14 12:46:47 -05:00
|
|
|
current_version = 0
|
2017-12-06 04:02:49 -05:00
|
|
|
|
2021-12-14 12:46:47 -05:00
|
|
|
new_version = current_version + 1
|
2017-12-06 04:02:49 -05:00
|
|
|
|
2020-08-05 16:38:57 -04:00
|
|
|
self.db_pool.simple_insert_txn(
|
2017-12-05 20:02:57 -05:00
|
|
|
txn,
|
2017-12-17 20:58:53 -05:00
|
|
|
table="e2e_room_keys_versions",
|
2017-12-05 20:02:57 -05:00
|
|
|
values={
|
|
|
|
"user_id": user_id,
|
2017-12-06 04:02:49 -05:00
|
|
|
"version": new_version,
|
2017-12-05 20:02:57 -05:00
|
|
|
"algorithm": info["algorithm"],
|
2020-08-07 08:02:55 -04:00
|
|
|
"auth_data": json_encoder.encode(info["auth_data"]),
|
2017-12-05 20:02:57 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-12-14 12:46:47 -05:00
|
|
|
return str(new_version)
|
2017-12-05 20:02:57 -05:00
|
|
|
|
2020-08-28 07:54:27 -04:00
|
|
|
return await self.db_pool.runInteraction(
|
2017-12-17 20:58:53 -05:00
|
|
|
"create_e2e_room_keys_version_txn", _create_e2e_room_keys_version_txn
|
2017-12-05 20:02:57 -05:00
|
|
|
)
|
|
|
|
|
2019-08-22 06:28:12 -04:00
|
|
|
@trace
|
2020-08-27 07:08:38 -04:00
|
|
|
async def update_e2e_room_keys_version(
|
|
|
|
self,
|
|
|
|
user_id: str,
|
|
|
|
version: str,
|
2021-12-14 12:46:47 -05:00
|
|
|
info: Optional[JsonDict] = None,
|
2020-08-27 07:08:38 -04:00
|
|
|
version_etag: Optional[int] = None,
|
|
|
|
) -> None:
|
2019-02-06 17:57:10 -05:00
|
|
|
"""Update a given backup version
|
|
|
|
|
|
|
|
Args:
|
2020-08-27 07:08:38 -04:00
|
|
|
user_id: the user whose backup version we're updating
|
|
|
|
version: the version ID of the backup version we're updating
|
|
|
|
info: the new backup version info to store. If None, then the backup
|
|
|
|
version info is not updated.
|
|
|
|
version_etag: etag of the keys in the backup. If None, then the etag
|
|
|
|
is not updated.
|
2019-02-06 17:57:10 -05:00
|
|
|
"""
|
2021-12-14 12:46:47 -05:00
|
|
|
updatevalues: Dict[str, object] = {}
|
2019-02-06 17:57:10 -05:00
|
|
|
|
2019-11-27 16:14:44 -05:00
|
|
|
if info is not None and "auth_data" in info:
|
2020-08-07 08:02:55 -04:00
|
|
|
updatevalues["auth_data"] = json_encoder.encode(info["auth_data"])
|
2019-11-27 16:14:44 -05:00
|
|
|
if version_etag is not None:
|
|
|
|
updatevalues["etag"] = version_etag
|
|
|
|
|
|
|
|
if updatevalues:
|
2021-12-14 12:46:47 -05:00
|
|
|
try:
|
|
|
|
version_int = int(version)
|
|
|
|
except ValueError:
|
|
|
|
# Our versions are all ints so if we can't convert it to an integer,
|
|
|
|
# it doesn't exist.
|
|
|
|
raise StoreError(404, "No backup with that version exists")
|
|
|
|
|
|
|
|
await self.db_pool.simple_update_one(
|
2019-11-27 16:14:44 -05:00
|
|
|
table="e2e_room_keys_versions",
|
2021-12-14 12:46:47 -05:00
|
|
|
keyvalues={"user_id": user_id, "version": version_int},
|
2019-11-27 16:14:44 -05:00
|
|
|
updatevalues=updatevalues,
|
|
|
|
desc="update_e2e_room_keys_version",
|
|
|
|
)
|
2019-02-06 17:57:10 -05:00
|
|
|
|
2019-08-22 06:28:12 -04:00
|
|
|
@trace
|
2020-08-28 07:54:27 -04:00
|
|
|
async def delete_e2e_room_keys_version(
|
|
|
|
self, user_id: str, version: Optional[str] = None
|
|
|
|
) -> None:
|
2017-12-24 11:44:18 -05:00
|
|
|
"""Delete a given backup version of the user's room keys.
|
|
|
|
Doesn't delete their actual key data.
|
|
|
|
|
|
|
|
Args:
|
2020-08-28 07:54:27 -04:00
|
|
|
user_id: the user whose backup version we're deleting
|
|
|
|
version: Optional. the version ID of the backup version we're deleting
|
2018-01-07 18:45:55 -05:00
|
|
|
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 11:44:18 -05:00
|
|
|
"""
|
2017-12-05 20:02:57 -05:00
|
|
|
|
2021-12-14 12:46:47 -05:00
|
|
|
def _delete_e2e_room_keys_version_txn(txn: LoggingTransaction) -> None:
|
2018-01-07 18:45:55 -05:00
|
|
|
if version is None:
|
|
|
|
this_version = self._get_current_version(txn, user_id)
|
|
|
|
else:
|
2021-12-14 12:46:47 -05: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 backup with that version exists")
|
2017-12-05 20:02:57 -05:00
|
|
|
|
2020-08-05 16:38:57 -04:00
|
|
|
self.db_pool.simple_delete_txn(
|
2019-10-24 21:13:01 -04:00
|
|
|
txn,
|
|
|
|
table="e2e_room_keys",
|
|
|
|
keyvalues={"user_id": user_id, "version": this_version},
|
|
|
|
)
|
|
|
|
|
2020-08-28 07:54:27 -04:00
|
|
|
self.db_pool.simple_update_one_txn(
|
2018-01-07 18:45:55 -05:00
|
|
|
txn,
|
|
|
|
table="e2e_room_keys_versions",
|
2019-04-03 05:07:29 -04:00
|
|
|
keyvalues={"user_id": user_id, "version": this_version},
|
|
|
|
updatevalues={"deleted": 1},
|
2018-01-07 18:45:55 -05:00
|
|
|
)
|
|
|
|
|
2020-08-28 07:54:27 -04:00
|
|
|
await self.db_pool.runInteraction(
|
2019-04-03 05:07:29 -04:00
|
|
|
"delete_e2e_room_keys_version", _delete_e2e_room_keys_version_txn
|
2017-12-05 20:02:57 -05:00
|
|
|
)
|