You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

553 lines
20 KiB

  1. # Copyright 2017 New Vector Ltd
  2. # Copyright 2019 Matrix.org Foundation C.I.C.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from typing import Dict, Iterable, Mapping, Optional, Tuple, cast
  16. from typing_extensions import Literal, TypedDict
  17. from synapse.api.errors import StoreError
  18. from synapse.logging.opentracing import log_kv, trace
  19. from synapse.storage._base import SQLBaseStore, db_to_json
  20. from synapse.storage.database import LoggingTransaction
  21. from synapse.types import JsonDict, JsonSerializable, StreamKeyType
  22. from synapse.util import json_encoder
  23. class RoomKey(TypedDict):
  24. """`KeyBackupData` in the Matrix spec.
  25. https://spec.matrix.org/v1.1/client-server-api/#get_matrixclientv3room_keyskeysroomidsessionid
  26. """
  27. first_message_index: int
  28. forwarded_count: int
  29. is_verified: bool
  30. session_data: JsonSerializable
  31. class EndToEndRoomKeyStore(SQLBaseStore):
  32. """The store for end to end room key backups.
  33. See https://spec.matrix.org/v1.1/client-server-api/#server-side-key-backups
  34. As per the spec, backups are identified by an opaque version string. Internally,
  35. version identifiers are assigned using incrementing integers. Non-numeric version
  36. strings are treated as if they do not exist, since we would have never issued them.
  37. """
  38. async def update_e2e_room_key(
  39. self,
  40. user_id: str,
  41. version: str,
  42. room_id: str,
  43. session_id: str,
  44. room_key: RoomKey,
  45. ) -> None:
  46. """Replaces the encrypted E2E room key for a given session in a given backup
  47. Args:
  48. user_id: the user whose backup we're setting
  49. version: the version ID of the backup we're updating
  50. room_id: the ID of the room whose keys we're setting
  51. session_id: the session whose room_key we're setting
  52. room_key: the room_key being set
  53. Raises:
  54. StoreError
  55. """
  56. try:
  57. version_int = int(version)
  58. except ValueError:
  59. # Our versions are all ints so if we can't convert it to an integer,
  60. # it doesn't exist.
  61. raise StoreError(404, "No backup with that version exists")
  62. await self.db_pool.simple_update_one(
  63. table="e2e_room_keys",
  64. keyvalues={
  65. "user_id": user_id,
  66. "version": version_int,
  67. "room_id": room_id,
  68. "session_id": session_id,
  69. },
  70. updatevalues={
  71. "first_message_index": room_key["first_message_index"],
  72. "forwarded_count": room_key["forwarded_count"],
  73. "is_verified": room_key["is_verified"],
  74. "session_data": json_encoder.encode(room_key["session_data"]),
  75. },
  76. desc="update_e2e_room_key",
  77. )
  78. async def add_e2e_room_keys(
  79. self, user_id: str, version: str, room_keys: Iterable[Tuple[str, str, RoomKey]]
  80. ) -> None:
  81. """Bulk add room keys to a given backup.
  82. Args:
  83. user_id: the user whose backup we're adding to
  84. version: the version ID of the backup for the set of keys we're adding to
  85. room_keys: the keys to add, in the form (roomID, sessionID, keyData)
  86. """
  87. try:
  88. version_int = int(version)
  89. except ValueError:
  90. # Our versions are all ints so if we can't convert it to an integer,
  91. # it doesn't exist.
  92. raise StoreError(404, "No backup with that version exists")
  93. values = []
  94. for room_id, session_id, room_key in room_keys:
  95. values.append(
  96. (
  97. user_id,
  98. version_int,
  99. room_id,
  100. session_id,
  101. room_key["first_message_index"],
  102. room_key["forwarded_count"],
  103. room_key["is_verified"],
  104. json_encoder.encode(room_key["session_data"]),
  105. )
  106. )
  107. log_kv(
  108. {
  109. "message": "Set room key",
  110. "room_id": room_id,
  111. "session_id": session_id,
  112. StreamKeyType.ROOM: room_key,
  113. }
  114. )
  115. await self.db_pool.simple_insert_many(
  116. table="e2e_room_keys",
  117. keys=(
  118. "user_id",
  119. "version",
  120. "room_id",
  121. "session_id",
  122. "first_message_index",
  123. "forwarded_count",
  124. "is_verified",
  125. "session_data",
  126. ),
  127. values=values,
  128. desc="add_e2e_room_keys",
  129. )
  130. @trace
  131. async def get_e2e_room_keys(
  132. self,
  133. user_id: str,
  134. version: str,
  135. room_id: Optional[str] = None,
  136. session_id: Optional[str] = None,
  137. ) -> Dict[
  138. Literal["rooms"], Dict[str, Dict[Literal["sessions"], Dict[str, RoomKey]]]
  139. ]:
  140. """Bulk get the E2E room keys for a given backup, optionally filtered to a given
  141. room, or a given session.
  142. Args:
  143. user_id: the user whose backup we're querying
  144. version: the version ID of the backup for the set of keys we're querying
  145. room_id: Optional. the ID of the room whose keys we're querying, if any.
  146. If not specified, we return the keys for all the rooms in the backup.
  147. session_id: Optional. the session whose room_key we're querying, if any.
  148. If specified, we also require the room_id to be specified.
  149. If not specified, we return all the keys in this version of
  150. the backup (or for the specified room)
  151. Returns:
  152. A dict giving the session_data and message metadata for these room keys.
  153. `{"rooms": {room_id: {"sessions": {session_id: room_key}}}}`
  154. """
  155. try:
  156. version_int = int(version)
  157. except ValueError:
  158. return {"rooms": {}}
  159. keyvalues = {"user_id": user_id, "version": version_int}
  160. if room_id:
  161. keyvalues["room_id"] = room_id
  162. if session_id:
  163. keyvalues["session_id"] = session_id
  164. rows = await self.db_pool.simple_select_list(
  165. table="e2e_room_keys",
  166. keyvalues=keyvalues,
  167. retcols=(
  168. "user_id",
  169. "room_id",
  170. "session_id",
  171. "first_message_index",
  172. "forwarded_count",
  173. "is_verified",
  174. "session_data",
  175. ),
  176. desc="get_e2e_room_keys",
  177. )
  178. sessions: Dict[
  179. Literal["rooms"], Dict[str, Dict[Literal["sessions"], Dict[str, RoomKey]]]
  180. ] = {"rooms": {}}
  181. for row in rows:
  182. room_entry = sessions["rooms"].setdefault(row["room_id"], {"sessions": {}})
  183. room_entry["sessions"][row["session_id"]] = {
  184. "first_message_index": row["first_message_index"],
  185. "forwarded_count": row["forwarded_count"],
  186. # is_verified must be returned to the client as a boolean
  187. "is_verified": bool(row["is_verified"]),
  188. "session_data": db_to_json(row["session_data"]),
  189. }
  190. return sessions
  191. async def get_e2e_room_keys_multi(
  192. self,
  193. user_id: str,
  194. version: str,
  195. room_keys: Mapping[str, Mapping[Literal["sessions"], Iterable[str]]],
  196. ) -> Dict[str, Dict[str, RoomKey]]:
  197. """Get multiple room keys at a time. The difference between this function and
  198. get_e2e_room_keys is that this function can be used to retrieve
  199. multiple specific keys at a time, whereas get_e2e_room_keys is used for
  200. getting all the keys in a backup version, all the keys for a room, or a
  201. specific key.
  202. Args:
  203. user_id: the user whose backup we're querying
  204. version: the version ID of the backup we're querying about
  205. room_keys: a map from room ID -> {"sessions": [session ids]}
  206. indicating the session IDs that we want to query
  207. Returns:
  208. A map of room IDs to session IDs to room key
  209. """
  210. try:
  211. version_int = int(version)
  212. except ValueError:
  213. # Our versions are all ints so if we can't convert it to an integer,
  214. # it doesn't exist.
  215. return {}
  216. return await self.db_pool.runInteraction(
  217. "get_e2e_room_keys_multi",
  218. self._get_e2e_room_keys_multi_txn,
  219. user_id,
  220. version_int,
  221. room_keys,
  222. )
  223. @staticmethod
  224. def _get_e2e_room_keys_multi_txn(
  225. txn: LoggingTransaction,
  226. user_id: str,
  227. version: int,
  228. room_keys: Mapping[str, Mapping[Literal["sessions"], Iterable[str]]],
  229. ) -> Dict[str, Dict[str, RoomKey]]:
  230. if not room_keys:
  231. return {}
  232. where_clauses = []
  233. params = [user_id, version]
  234. for room_id, room in room_keys.items():
  235. sessions = list(room["sessions"])
  236. if not sessions:
  237. continue
  238. params.append(room_id)
  239. params.extend(sessions)
  240. where_clauses.append(
  241. "(room_id = ? AND session_id IN (%s))"
  242. % (",".join(["?" for _ in sessions]),)
  243. )
  244. # check if we're actually querying something
  245. if not where_clauses:
  246. return {}
  247. sql = """
  248. SELECT room_id, session_id, first_message_index, forwarded_count,
  249. is_verified, session_data
  250. FROM e2e_room_keys
  251. WHERE user_id = ? AND version = ? AND (%s)
  252. """ % (
  253. " OR ".join(where_clauses)
  254. )
  255. txn.execute(sql, params)
  256. ret: Dict[str, Dict[str, RoomKey]] = {}
  257. for row in txn:
  258. room_id = row[0]
  259. session_id = row[1]
  260. ret.setdefault(room_id, {})
  261. ret[room_id][session_id] = {
  262. "first_message_index": row[2],
  263. "forwarded_count": row[3],
  264. "is_verified": row[4],
  265. "session_data": db_to_json(row[5]),
  266. }
  267. return ret
  268. async def count_e2e_room_keys(self, user_id: str, version: str) -> int:
  269. """Get the number of keys in a backup version.
  270. Args:
  271. user_id: the user whose backup we're querying
  272. version: the version ID of the backup we're querying about
  273. """
  274. try:
  275. version_int = int(version)
  276. except ValueError:
  277. # Our versions are all ints so if we can't convert it to an integer,
  278. # it doesn't exist.
  279. return 0
  280. return await self.db_pool.simple_select_one_onecol(
  281. table="e2e_room_keys",
  282. keyvalues={"user_id": user_id, "version": version_int},
  283. retcol="COUNT(*)",
  284. desc="count_e2e_room_keys",
  285. )
  286. @trace
  287. async def delete_e2e_room_keys(
  288. self,
  289. user_id: str,
  290. version: str,
  291. room_id: Optional[str] = None,
  292. session_id: Optional[str] = None,
  293. ) -> None:
  294. """Bulk delete the E2E room keys for a given backup, optionally filtered to a given
  295. room or a given session.
  296. Args:
  297. user_id: the user whose backup we're deleting from
  298. version: the version ID of the backup for the set of keys we're deleting
  299. room_id: Optional. the ID of the room whose keys we're deleting, if any.
  300. If not specified, we delete the keys for all the rooms in the backup.
  301. session_id: Optional. the session whose room_key we're querying, if any.
  302. If specified, we also require the room_id to be specified.
  303. If not specified, we delete all the keys in this version of
  304. the backup (or for the specified room)
  305. """
  306. try:
  307. version_int = int(version)
  308. except ValueError:
  309. # Our versions are all ints so if we can't convert it to an integer,
  310. # it doesn't exist.
  311. return
  312. keyvalues = {"user_id": user_id, "version": version_int}
  313. if room_id:
  314. keyvalues["room_id"] = room_id
  315. if session_id:
  316. keyvalues["session_id"] = session_id
  317. await self.db_pool.simple_delete(
  318. table="e2e_room_keys", keyvalues=keyvalues, desc="delete_e2e_room_keys"
  319. )
  320. @staticmethod
  321. def _get_current_version(txn: LoggingTransaction, user_id: str) -> int:
  322. txn.execute(
  323. "SELECT MAX(version) FROM e2e_room_keys_versions "
  324. "WHERE user_id=? AND deleted=0",
  325. (user_id,),
  326. )
  327. # `SELECT MAX() FROM ...` will always return 1 row. The value in that row will
  328. # be `NULL` when there are no available versions.
  329. row = cast(Tuple[Optional[int]], txn.fetchone())
  330. if row[0] is None:
  331. raise StoreError(404, "No current backup version")
  332. return row[0]
  333. async def get_e2e_room_keys_version_info(
  334. self, user_id: str, version: Optional[str] = None
  335. ) -> JsonDict:
  336. """Get info metadata about a version of our room_keys backup.
  337. Args:
  338. user_id: the user whose backup we're querying
  339. version: Optional. the version ID of the backup we're querying about
  340. If missing, we return the information about the current version.
  341. Raises:
  342. StoreError: with code 404 if there are no e2e_room_keys_versions present
  343. Returns:
  344. A dict giving the info metadata for this backup version, with
  345. fields including:
  346. version (str)
  347. algorithm (str)
  348. auth_data (object): opaque dict supplied by the client
  349. etag (int): tag of the keys in the backup
  350. """
  351. def _get_e2e_room_keys_version_info_txn(txn: LoggingTransaction) -> JsonDict:
  352. if version is None:
  353. this_version = self._get_current_version(txn, user_id)
  354. else:
  355. try:
  356. this_version = int(version)
  357. except ValueError:
  358. # Our versions are all ints so if we can't convert it to an integer,
  359. # it isn't there.
  360. raise StoreError(404, "No backup with that version exists")
  361. result = self.db_pool.simple_select_one_txn(
  362. txn,
  363. table="e2e_room_keys_versions",
  364. keyvalues={"user_id": user_id, "version": this_version, "deleted": 0},
  365. retcols=("version", "algorithm", "auth_data", "etag"),
  366. allow_none=False,
  367. )
  368. assert result is not None # see comment on `simple_select_one_txn`
  369. result["auth_data"] = db_to_json(result["auth_data"])
  370. result["version"] = str(result["version"])
  371. if result["etag"] is None:
  372. result["etag"] = 0
  373. return result
  374. return await self.db_pool.runInteraction(
  375. "get_e2e_room_keys_version_info", _get_e2e_room_keys_version_info_txn
  376. )
  377. @trace
  378. async def create_e2e_room_keys_version(self, user_id: str, info: JsonDict) -> str:
  379. """Atomically creates a new version of this user's e2e_room_keys store
  380. with the given version info.
  381. Args:
  382. user_id: the user whose backup we're creating a version
  383. info: the info about the backup version to be created
  384. Returns:
  385. The newly created version ID
  386. """
  387. def _create_e2e_room_keys_version_txn(txn: LoggingTransaction) -> str:
  388. txn.execute(
  389. "SELECT MAX(version) FROM e2e_room_keys_versions WHERE user_id=?",
  390. (user_id,),
  391. )
  392. current_version = cast(Tuple[Optional[int]], txn.fetchone())[0]
  393. if current_version is None:
  394. current_version = 0
  395. new_version = current_version + 1
  396. self.db_pool.simple_insert_txn(
  397. txn,
  398. table="e2e_room_keys_versions",
  399. values={
  400. "user_id": user_id,
  401. "version": new_version,
  402. "algorithm": info["algorithm"],
  403. "auth_data": json_encoder.encode(info["auth_data"]),
  404. },
  405. )
  406. return str(new_version)
  407. return await self.db_pool.runInteraction(
  408. "create_e2e_room_keys_version_txn", _create_e2e_room_keys_version_txn
  409. )
  410. @trace
  411. async def update_e2e_room_keys_version(
  412. self,
  413. user_id: str,
  414. version: str,
  415. info: Optional[JsonDict] = None,
  416. version_etag: Optional[int] = None,
  417. ) -> None:
  418. """Update a given backup version
  419. Args:
  420. user_id: the user whose backup version we're updating
  421. version: the version ID of the backup version we're updating
  422. info: the new backup version info to store. If None, then the backup
  423. version info is not updated.
  424. version_etag: etag of the keys in the backup. If None, then the etag
  425. is not updated.
  426. """
  427. updatevalues: Dict[str, object] = {}
  428. if info is not None and "auth_data" in info:
  429. updatevalues["auth_data"] = json_encoder.encode(info["auth_data"])
  430. if version_etag is not None:
  431. updatevalues["etag"] = version_etag
  432. if updatevalues:
  433. try:
  434. version_int = int(version)
  435. except ValueError:
  436. # Our versions are all ints so if we can't convert it to an integer,
  437. # it doesn't exist.
  438. raise StoreError(404, "No backup with that version exists")
  439. await self.db_pool.simple_update_one(
  440. table="e2e_room_keys_versions",
  441. keyvalues={"user_id": user_id, "version": version_int},
  442. updatevalues=updatevalues,
  443. desc="update_e2e_room_keys_version",
  444. )
  445. @trace
  446. async def delete_e2e_room_keys_version(
  447. self, user_id: str, version: Optional[str] = None
  448. ) -> None:
  449. """Delete a given backup version of the user's room keys.
  450. Doesn't delete their actual key data.
  451. Args:
  452. user_id: the user whose backup version we're deleting
  453. version: Optional. the version ID of the backup version we're deleting
  454. If missing, we delete the current backup version info.
  455. Raises:
  456. StoreError: with code 404 if there are no e2e_room_keys_versions present,
  457. or if the version requested doesn't exist.
  458. """
  459. def _delete_e2e_room_keys_version_txn(txn: LoggingTransaction) -> None:
  460. if version is None:
  461. this_version = self._get_current_version(txn, user_id)
  462. else:
  463. try:
  464. this_version = int(version)
  465. except ValueError:
  466. # Our versions are all ints so if we can't convert it to an integer,
  467. # it isn't there.
  468. raise StoreError(404, "No backup with that version exists")
  469. self.db_pool.simple_delete_txn(
  470. txn,
  471. table="e2e_room_keys",
  472. keyvalues={"user_id": user_id, "version": this_version},
  473. )
  474. self.db_pool.simple_update_one_txn(
  475. txn,
  476. table="e2e_room_keys_versions",
  477. keyvalues={"user_id": user_id, "version": this_version},
  478. updatevalues={"deleted": 1},
  479. )
  480. await self.db_pool.runInteraction(
  481. "delete_e2e_room_keys_version", _delete_e2e_room_keys_version_txn
  482. )