Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

749 Zeilen
29 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket Ltd
  3. # Copyright 2019 New Vector Ltd
  4. # Copyright 2019 The Matrix.org Foundation C.I.C.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. from typing import Dict, List, Tuple
  18. from canonicaljson import encode_canonical_json, json
  19. from twisted.enterprise.adbapi import Connection
  20. from twisted.internet import defer
  21. from synapse.logging.opentracing import log_kv, set_tag, trace
  22. from synapse.storage._base import SQLBaseStore, db_to_json
  23. from synapse.storage.database import make_in_list_sql_clause
  24. from synapse.util.caches.descriptors import cached, cachedList
  25. from synapse.util.iterutils import batch_iter
  26. class EndToEndKeyWorkerStore(SQLBaseStore):
  27. @trace
  28. @defer.inlineCallbacks
  29. def get_e2e_device_keys(
  30. self, query_list, include_all_devices=False, include_deleted_devices=False
  31. ):
  32. """Fetch a list of device keys.
  33. Args:
  34. query_list(list): List of pairs of user_ids and device_ids.
  35. include_all_devices (bool): whether to include entries for devices
  36. that don't have device keys
  37. include_deleted_devices (bool): whether to include null entries for
  38. devices which no longer exist (but were in the query_list).
  39. This option only takes effect if include_all_devices is true.
  40. Returns:
  41. Dict mapping from user-id to dict mapping from device_id to
  42. key data. The key data will be a dict in the same format as the
  43. DeviceKeys type returned by POST /_matrix/client/r0/keys/query.
  44. """
  45. set_tag("query_list", query_list)
  46. if not query_list:
  47. return {}
  48. results = yield self.db_pool.runInteraction(
  49. "get_e2e_device_keys",
  50. self._get_e2e_device_keys_txn,
  51. query_list,
  52. include_all_devices,
  53. include_deleted_devices,
  54. )
  55. # Build the result structure, un-jsonify the results, and add the
  56. # "unsigned" section
  57. rv = {}
  58. for user_id, device_keys in results.items():
  59. rv[user_id] = {}
  60. for device_id, device_info in device_keys.items():
  61. r = db_to_json(device_info.pop("key_json"))
  62. r["unsigned"] = {}
  63. display_name = device_info["device_display_name"]
  64. if display_name is not None:
  65. r["unsigned"]["device_display_name"] = display_name
  66. if "signatures" in device_info:
  67. for sig_user_id, sigs in device_info["signatures"].items():
  68. r.setdefault("signatures", {}).setdefault(
  69. sig_user_id, {}
  70. ).update(sigs)
  71. rv[user_id][device_id] = r
  72. return rv
  73. @trace
  74. def _get_e2e_device_keys_txn(
  75. self, txn, query_list, include_all_devices=False, include_deleted_devices=False
  76. ):
  77. set_tag("include_all_devices", include_all_devices)
  78. set_tag("include_deleted_devices", include_deleted_devices)
  79. query_clauses = []
  80. query_params = []
  81. signature_query_clauses = []
  82. signature_query_params = []
  83. if include_all_devices is False:
  84. include_deleted_devices = False
  85. if include_deleted_devices:
  86. deleted_devices = set(query_list)
  87. for (user_id, device_id) in query_list:
  88. query_clause = "user_id = ?"
  89. query_params.append(user_id)
  90. signature_query_clause = "target_user_id = ?"
  91. signature_query_params.append(user_id)
  92. if device_id is not None:
  93. query_clause += " AND device_id = ?"
  94. query_params.append(device_id)
  95. signature_query_clause += " AND target_device_id = ?"
  96. signature_query_params.append(device_id)
  97. signature_query_clause += " AND user_id = ?"
  98. signature_query_params.append(user_id)
  99. query_clauses.append(query_clause)
  100. signature_query_clauses.append(signature_query_clause)
  101. sql = (
  102. "SELECT user_id, device_id, "
  103. " d.display_name AS device_display_name, "
  104. " k.key_json"
  105. " FROM devices d"
  106. " %s JOIN e2e_device_keys_json k USING (user_id, device_id)"
  107. " WHERE %s AND NOT d.hidden"
  108. ) % (
  109. "LEFT" if include_all_devices else "INNER",
  110. " OR ".join("(" + q + ")" for q in query_clauses),
  111. )
  112. txn.execute(sql, query_params)
  113. rows = self.db_pool.cursor_to_dict(txn)
  114. result = {}
  115. for row in rows:
  116. if include_deleted_devices:
  117. deleted_devices.remove((row["user_id"], row["device_id"]))
  118. result.setdefault(row["user_id"], {})[row["device_id"]] = row
  119. if include_deleted_devices:
  120. for user_id, device_id in deleted_devices:
  121. result.setdefault(user_id, {})[device_id] = None
  122. # get signatures on the device
  123. signature_sql = ("SELECT * FROM e2e_cross_signing_signatures WHERE %s") % (
  124. " OR ".join("(" + q + ")" for q in signature_query_clauses)
  125. )
  126. txn.execute(signature_sql, signature_query_params)
  127. rows = self.db_pool.cursor_to_dict(txn)
  128. # add each cross-signing signature to the correct device in the result dict.
  129. for row in rows:
  130. signing_user_id = row["user_id"]
  131. signing_key_id = row["key_id"]
  132. target_user_id = row["target_user_id"]
  133. target_device_id = row["target_device_id"]
  134. signature = row["signature"]
  135. target_user_result = result.get(target_user_id)
  136. if not target_user_result:
  137. continue
  138. target_device_result = target_user_result.get(target_device_id)
  139. if not target_device_result:
  140. # note that target_device_result will be None for deleted devices.
  141. continue
  142. target_device_signatures = target_device_result.setdefault("signatures", {})
  143. signing_user_signatures = target_device_signatures.setdefault(
  144. signing_user_id, {}
  145. )
  146. signing_user_signatures[signing_key_id] = signature
  147. log_kv(result)
  148. return result
  149. @defer.inlineCallbacks
  150. def get_e2e_one_time_keys(self, user_id, device_id, key_ids):
  151. """Retrieve a number of one-time keys for a user
  152. Args:
  153. user_id(str): id of user to get keys for
  154. device_id(str): id of device to get keys for
  155. key_ids(list[str]): list of key ids (excluding algorithm) to
  156. retrieve
  157. Returns:
  158. deferred resolving to Dict[(str, str), str]: map from (algorithm,
  159. key_id) to json string for key
  160. """
  161. rows = yield self.db_pool.simple_select_many_batch(
  162. table="e2e_one_time_keys_json",
  163. column="key_id",
  164. iterable=key_ids,
  165. retcols=("algorithm", "key_id", "key_json"),
  166. keyvalues={"user_id": user_id, "device_id": device_id},
  167. desc="add_e2e_one_time_keys_check",
  168. )
  169. result = {(row["algorithm"], row["key_id"]): row["key_json"] for row in rows}
  170. log_kv({"message": "Fetched one time keys for user", "one_time_keys": result})
  171. return result
  172. @defer.inlineCallbacks
  173. def add_e2e_one_time_keys(self, user_id, device_id, time_now, new_keys):
  174. """Insert some new one time keys for a device. Errors if any of the
  175. keys already exist.
  176. Args:
  177. user_id(str): id of user to get keys for
  178. device_id(str): id of device to get keys for
  179. time_now(long): insertion time to record (ms since epoch)
  180. new_keys(iterable[(str, str, str)]: keys to add - each a tuple of
  181. (algorithm, key_id, key json)
  182. """
  183. def _add_e2e_one_time_keys(txn):
  184. set_tag("user_id", user_id)
  185. set_tag("device_id", device_id)
  186. set_tag("new_keys", new_keys)
  187. # We are protected from race between lookup and insertion due to
  188. # a unique constraint. If there is a race of two calls to
  189. # `add_e2e_one_time_keys` then they'll conflict and we will only
  190. # insert one set.
  191. self.db_pool.simple_insert_many_txn(
  192. txn,
  193. table="e2e_one_time_keys_json",
  194. values=[
  195. {
  196. "user_id": user_id,
  197. "device_id": device_id,
  198. "algorithm": algorithm,
  199. "key_id": key_id,
  200. "ts_added_ms": time_now,
  201. "key_json": json_bytes,
  202. }
  203. for algorithm, key_id, json_bytes in new_keys
  204. ],
  205. )
  206. self._invalidate_cache_and_stream(
  207. txn, self.count_e2e_one_time_keys, (user_id, device_id)
  208. )
  209. yield self.db_pool.runInteraction(
  210. "add_e2e_one_time_keys_insert", _add_e2e_one_time_keys
  211. )
  212. @cached(max_entries=10000)
  213. def count_e2e_one_time_keys(self, user_id, device_id):
  214. """ Count the number of one time keys the server has for a device
  215. Returns:
  216. Dict mapping from algorithm to number of keys for that algorithm.
  217. """
  218. def _count_e2e_one_time_keys(txn):
  219. sql = (
  220. "SELECT algorithm, COUNT(key_id) FROM e2e_one_time_keys_json"
  221. " WHERE user_id = ? AND device_id = ?"
  222. " GROUP BY algorithm"
  223. )
  224. txn.execute(sql, (user_id, device_id))
  225. result = {}
  226. for algorithm, key_count in txn:
  227. result[algorithm] = key_count
  228. return result
  229. return self.db_pool.runInteraction(
  230. "count_e2e_one_time_keys", _count_e2e_one_time_keys
  231. )
  232. @defer.inlineCallbacks
  233. def get_e2e_cross_signing_key(self, user_id, key_type, from_user_id=None):
  234. """Returns a user's cross-signing key.
  235. Args:
  236. user_id (str): the user whose key is being requested
  237. key_type (str): the type of key that is being requested: either 'master'
  238. for a master key, 'self_signing' for a self-signing key, or
  239. 'user_signing' for a user-signing key
  240. from_user_id (str): if specified, signatures made by this user on
  241. the self-signing key will be included in the result
  242. Returns:
  243. dict of the key data or None if not found
  244. """
  245. res = yield self.get_e2e_cross_signing_keys_bulk([user_id], from_user_id)
  246. user_keys = res.get(user_id)
  247. if not user_keys:
  248. return None
  249. return user_keys.get(key_type)
  250. @cached(num_args=1)
  251. def _get_bare_e2e_cross_signing_keys(self, user_id):
  252. """Dummy function. Only used to make a cache for
  253. _get_bare_e2e_cross_signing_keys_bulk.
  254. """
  255. raise NotImplementedError()
  256. @cachedList(
  257. cached_method_name="_get_bare_e2e_cross_signing_keys",
  258. list_name="user_ids",
  259. num_args=1,
  260. )
  261. def _get_bare_e2e_cross_signing_keys_bulk(
  262. self, user_ids: List[str]
  263. ) -> Dict[str, Dict[str, dict]]:
  264. """Returns the cross-signing keys for a set of users. The output of this
  265. function should be passed to _get_e2e_cross_signing_signatures_txn if
  266. the signatures for the calling user need to be fetched.
  267. Args:
  268. user_ids (list[str]): the users whose keys are being requested
  269. Returns:
  270. dict[str, dict[str, dict]]: mapping from user ID to key type to key
  271. data. If a user's cross-signing keys were not found, either
  272. their user ID will not be in the dict, or their user ID will map
  273. to None.
  274. """
  275. return self.db_pool.runInteraction(
  276. "get_bare_e2e_cross_signing_keys_bulk",
  277. self._get_bare_e2e_cross_signing_keys_bulk_txn,
  278. user_ids,
  279. )
  280. def _get_bare_e2e_cross_signing_keys_bulk_txn(
  281. self, txn: Connection, user_ids: List[str],
  282. ) -> Dict[str, Dict[str, dict]]:
  283. """Returns the cross-signing keys for a set of users. The output of this
  284. function should be passed to _get_e2e_cross_signing_signatures_txn if
  285. the signatures for the calling user need to be fetched.
  286. Args:
  287. txn (twisted.enterprise.adbapi.Connection): db connection
  288. user_ids (list[str]): the users whose keys are being requested
  289. Returns:
  290. dict[str, dict[str, dict]]: mapping from user ID to key type to key
  291. data. If a user's cross-signing keys were not found, their user
  292. ID will not be in the dict.
  293. """
  294. result = {}
  295. for user_chunk in batch_iter(user_ids, 100):
  296. clause, params = make_in_list_sql_clause(
  297. txn.database_engine, "k.user_id", user_chunk
  298. )
  299. sql = (
  300. """
  301. SELECT k.user_id, k.keytype, k.keydata, k.stream_id
  302. FROM e2e_cross_signing_keys k
  303. INNER JOIN (SELECT user_id, keytype, MAX(stream_id) AS stream_id
  304. FROM e2e_cross_signing_keys
  305. GROUP BY user_id, keytype) s
  306. USING (user_id, stream_id, keytype)
  307. WHERE
  308. """
  309. + clause
  310. )
  311. txn.execute(sql, params)
  312. rows = self.db_pool.cursor_to_dict(txn)
  313. for row in rows:
  314. user_id = row["user_id"]
  315. key_type = row["keytype"]
  316. key = db_to_json(row["keydata"])
  317. user_info = result.setdefault(user_id, {})
  318. user_info[key_type] = key
  319. return result
  320. def _get_e2e_cross_signing_signatures_txn(
  321. self, txn: Connection, keys: Dict[str, Dict[str, dict]], from_user_id: str,
  322. ) -> Dict[str, Dict[str, dict]]:
  323. """Returns the cross-signing signatures made by a user on a set of keys.
  324. Args:
  325. txn (twisted.enterprise.adbapi.Connection): db connection
  326. keys (dict[str, dict[str, dict]]): a map of user ID to key type to
  327. key data. This dict will be modified to add signatures.
  328. from_user_id (str): fetch the signatures made by this user
  329. Returns:
  330. dict[str, dict[str, dict]]: mapping from user ID to key type to key
  331. data. The return value will be the same as the keys argument,
  332. with the modifications included.
  333. """
  334. # find out what cross-signing keys (a.k.a. devices) we need to get
  335. # signatures for. This is a map of (user_id, device_id) to key type
  336. # (device_id is the key's public part).
  337. devices = {}
  338. for user_id, user_info in keys.items():
  339. if user_info is None:
  340. continue
  341. for key_type, key in user_info.items():
  342. device_id = None
  343. for k in key["keys"].values():
  344. device_id = k
  345. devices[(user_id, device_id)] = key_type
  346. for batch in batch_iter(devices.keys(), size=100):
  347. sql = """
  348. SELECT target_user_id, target_device_id, key_id, signature
  349. FROM e2e_cross_signing_signatures
  350. WHERE user_id = ?
  351. AND (%s)
  352. """ % (
  353. " OR ".join(
  354. "(target_user_id = ? AND target_device_id = ?)" for _ in batch
  355. )
  356. )
  357. query_params = [from_user_id]
  358. for item in batch:
  359. # item is a (user_id, device_id) tuple
  360. query_params.extend(item)
  361. txn.execute(sql, query_params)
  362. rows = self.db_pool.cursor_to_dict(txn)
  363. # and add the signatures to the appropriate keys
  364. for row in rows:
  365. key_id = row["key_id"]
  366. target_user_id = row["target_user_id"]
  367. target_device_id = row["target_device_id"]
  368. key_type = devices[(target_user_id, target_device_id)]
  369. # We need to copy everything, because the result may have come
  370. # from the cache. dict.copy only does a shallow copy, so we
  371. # need to recursively copy the dicts that will be modified.
  372. user_info = keys[target_user_id] = keys[target_user_id].copy()
  373. target_user_key = user_info[key_type] = user_info[key_type].copy()
  374. if "signatures" in target_user_key:
  375. signatures = target_user_key["signatures"] = target_user_key[
  376. "signatures"
  377. ].copy()
  378. if from_user_id in signatures:
  379. user_sigs = signatures[from_user_id] = signatures[from_user_id]
  380. user_sigs[key_id] = row["signature"]
  381. else:
  382. signatures[from_user_id] = {key_id: row["signature"]}
  383. else:
  384. target_user_key["signatures"] = {
  385. from_user_id: {key_id: row["signature"]}
  386. }
  387. return keys
  388. @defer.inlineCallbacks
  389. def get_e2e_cross_signing_keys_bulk(
  390. self, user_ids: List[str], from_user_id: str = None
  391. ) -> defer.Deferred:
  392. """Returns the cross-signing keys for a set of users.
  393. Args:
  394. user_ids (list[str]): the users whose keys are being requested
  395. from_user_id (str): if specified, signatures made by this user on
  396. the self-signing keys will be included in the result
  397. Returns:
  398. Deferred[dict[str, dict[str, dict]]]: map of user ID to key type to
  399. key data. If a user's cross-signing keys were not found, either
  400. their user ID will not be in the dict, or their user ID will map
  401. to None.
  402. """
  403. result = yield self._get_bare_e2e_cross_signing_keys_bulk(user_ids)
  404. if from_user_id:
  405. result = yield self.db_pool.runInteraction(
  406. "get_e2e_cross_signing_signatures",
  407. self._get_e2e_cross_signing_signatures_txn,
  408. result,
  409. from_user_id,
  410. )
  411. return result
  412. async def get_all_user_signature_changes_for_remotes(
  413. self, instance_name: str, last_id: int, current_id: int, limit: int
  414. ) -> Tuple[List[Tuple[int, tuple]], int, bool]:
  415. """Get updates for groups replication stream.
  416. Note that the user signature stream represents when a user signs their
  417. device with their user-signing key, which is not published to other
  418. users or servers, so no `destination` is needed in the returned
  419. list. However, this is needed to poke workers.
  420. Args:
  421. instance_name: The writer we want to fetch updates from. Unused
  422. here since there is only ever one writer.
  423. last_id: The token to fetch updates from. Exclusive.
  424. current_id: The token to fetch updates up to. Inclusive.
  425. limit: The requested limit for the number of rows to return. The
  426. function may return more or fewer rows.
  427. Returns:
  428. A tuple consisting of: the updates, a token to use to fetch
  429. subsequent updates, and whether we returned fewer rows than exists
  430. between the requested tokens due to the limit.
  431. The token returned can be used in a subsequent call to this
  432. function to get further updatees.
  433. The updates are a list of 2-tuples of stream ID and the row data
  434. """
  435. if last_id == current_id:
  436. return [], current_id, False
  437. def _get_all_user_signature_changes_for_remotes_txn(txn):
  438. sql = """
  439. SELECT stream_id, from_user_id AS user_id
  440. FROM user_signature_stream
  441. WHERE ? < stream_id AND stream_id <= ?
  442. ORDER BY stream_id ASC
  443. LIMIT ?
  444. """
  445. txn.execute(sql, (last_id, current_id, limit))
  446. updates = [(row[0], (row[1:])) for row in txn]
  447. limited = False
  448. upto_token = current_id
  449. if len(updates) >= limit:
  450. upto_token = updates[-1][0]
  451. limited = True
  452. return updates, upto_token, limited
  453. return await self.db_pool.runInteraction(
  454. "get_all_user_signature_changes_for_remotes",
  455. _get_all_user_signature_changes_for_remotes_txn,
  456. )
  457. class EndToEndKeyStore(EndToEndKeyWorkerStore, SQLBaseStore):
  458. def set_e2e_device_keys(self, user_id, device_id, time_now, device_keys):
  459. """Stores device keys for a device. Returns whether there was a change
  460. or the keys were already in the database.
  461. """
  462. def _set_e2e_device_keys_txn(txn):
  463. set_tag("user_id", user_id)
  464. set_tag("device_id", device_id)
  465. set_tag("time_now", time_now)
  466. set_tag("device_keys", device_keys)
  467. old_key_json = self.db_pool.simple_select_one_onecol_txn(
  468. txn,
  469. table="e2e_device_keys_json",
  470. keyvalues={"user_id": user_id, "device_id": device_id},
  471. retcol="key_json",
  472. allow_none=True,
  473. )
  474. # In py3 we need old_key_json to match new_key_json type. The DB
  475. # returns unicode while encode_canonical_json returns bytes.
  476. new_key_json = encode_canonical_json(device_keys).decode("utf-8")
  477. if old_key_json == new_key_json:
  478. log_kv({"Message": "Device key already stored."})
  479. return False
  480. self.db_pool.simple_upsert_txn(
  481. txn,
  482. table="e2e_device_keys_json",
  483. keyvalues={"user_id": user_id, "device_id": device_id},
  484. values={"ts_added_ms": time_now, "key_json": new_key_json},
  485. )
  486. log_kv({"message": "Device keys stored."})
  487. return True
  488. return self.db_pool.runInteraction(
  489. "set_e2e_device_keys", _set_e2e_device_keys_txn
  490. )
  491. def claim_e2e_one_time_keys(self, query_list):
  492. """Take a list of one time keys out of the database"""
  493. @trace
  494. def _claim_e2e_one_time_keys(txn):
  495. sql = (
  496. "SELECT key_id, key_json FROM e2e_one_time_keys_json"
  497. " WHERE user_id = ? AND device_id = ? AND algorithm = ?"
  498. " LIMIT 1"
  499. )
  500. result = {}
  501. delete = []
  502. for user_id, device_id, algorithm in query_list:
  503. user_result = result.setdefault(user_id, {})
  504. device_result = user_result.setdefault(device_id, {})
  505. txn.execute(sql, (user_id, device_id, algorithm))
  506. for key_id, key_json in txn:
  507. device_result[algorithm + ":" + key_id] = key_json
  508. delete.append((user_id, device_id, algorithm, key_id))
  509. sql = (
  510. "DELETE FROM e2e_one_time_keys_json"
  511. " WHERE user_id = ? AND device_id = ? AND algorithm = ?"
  512. " AND key_id = ?"
  513. )
  514. for user_id, device_id, algorithm, key_id in delete:
  515. log_kv(
  516. {
  517. "message": "Executing claim e2e_one_time_keys transaction on database."
  518. }
  519. )
  520. txn.execute(sql, (user_id, device_id, algorithm, key_id))
  521. log_kv({"message": "finished executing and invalidating cache"})
  522. self._invalidate_cache_and_stream(
  523. txn, self.count_e2e_one_time_keys, (user_id, device_id)
  524. )
  525. return result
  526. return self.db_pool.runInteraction(
  527. "claim_e2e_one_time_keys", _claim_e2e_one_time_keys
  528. )
  529. def delete_e2e_keys_by_device(self, user_id, device_id):
  530. def delete_e2e_keys_by_device_txn(txn):
  531. log_kv(
  532. {
  533. "message": "Deleting keys for device",
  534. "device_id": device_id,
  535. "user_id": user_id,
  536. }
  537. )
  538. self.db_pool.simple_delete_txn(
  539. txn,
  540. table="e2e_device_keys_json",
  541. keyvalues={"user_id": user_id, "device_id": device_id},
  542. )
  543. self.db_pool.simple_delete_txn(
  544. txn,
  545. table="e2e_one_time_keys_json",
  546. keyvalues={"user_id": user_id, "device_id": device_id},
  547. )
  548. self._invalidate_cache_and_stream(
  549. txn, self.count_e2e_one_time_keys, (user_id, device_id)
  550. )
  551. return self.db_pool.runInteraction(
  552. "delete_e2e_keys_by_device", delete_e2e_keys_by_device_txn
  553. )
  554. def _set_e2e_cross_signing_key_txn(self, txn, user_id, key_type, key):
  555. """Set a user's cross-signing key.
  556. Args:
  557. txn (twisted.enterprise.adbapi.Connection): db connection
  558. user_id (str): the user to set the signing key for
  559. key_type (str): the type of key that is being set: either 'master'
  560. for a master key, 'self_signing' for a self-signing key, or
  561. 'user_signing' for a user-signing key
  562. key (dict): the key data
  563. """
  564. # the 'key' dict will look something like:
  565. # {
  566. # "user_id": "@alice:example.com",
  567. # "usage": ["self_signing"],
  568. # "keys": {
  569. # "ed25519:base64+self+signing+public+key": "base64+self+signing+public+key",
  570. # },
  571. # "signatures": {
  572. # "@alice:example.com": {
  573. # "ed25519:base64+master+public+key": "base64+signature"
  574. # }
  575. # }
  576. # }
  577. # The "keys" property must only have one entry, which will be the public
  578. # key, so we just grab the first value in there
  579. pubkey = next(iter(key["keys"].values()))
  580. # The cross-signing keys need to occupy the same namespace as devices,
  581. # since signatures are identified by device ID. So add an entry to the
  582. # device table to make sure that we don't have a collision with device
  583. # IDs.
  584. # We only need to do this for local users, since remote servers should be
  585. # responsible for checking this for their own users.
  586. if self.hs.is_mine_id(user_id):
  587. self.db_pool.simple_insert_txn(
  588. txn,
  589. "devices",
  590. values={
  591. "user_id": user_id,
  592. "device_id": pubkey,
  593. "display_name": key_type + " signing key",
  594. "hidden": True,
  595. },
  596. )
  597. # and finally, store the key itself
  598. with self._cross_signing_id_gen.get_next() as stream_id:
  599. self.db_pool.simple_insert_txn(
  600. txn,
  601. "e2e_cross_signing_keys",
  602. values={
  603. "user_id": user_id,
  604. "keytype": key_type,
  605. "keydata": json.dumps(key),
  606. "stream_id": stream_id,
  607. },
  608. )
  609. self._invalidate_cache_and_stream(
  610. txn, self._get_bare_e2e_cross_signing_keys, (user_id,)
  611. )
  612. def set_e2e_cross_signing_key(self, user_id, key_type, key):
  613. """Set a user's cross-signing key.
  614. Args:
  615. user_id (str): the user to set the user-signing key for
  616. key_type (str): the type of cross-signing key to set
  617. key (dict): the key data
  618. """
  619. return self.db_pool.runInteraction(
  620. "add_e2e_cross_signing_key",
  621. self._set_e2e_cross_signing_key_txn,
  622. user_id,
  623. key_type,
  624. key,
  625. )
  626. def store_e2e_cross_signing_signatures(self, user_id, signatures):
  627. """Stores cross-signing signatures.
  628. Args:
  629. user_id (str): the user who made the signatures
  630. signatures (iterable[SignatureListItem]): signatures to add
  631. """
  632. return self.db_pool.simple_insert_many(
  633. "e2e_cross_signing_signatures",
  634. [
  635. {
  636. "user_id": user_id,
  637. "key_id": item.signing_key_id,
  638. "target_user_id": item.target_user_id,
  639. "target_device_id": item.target_device_id,
  640. "signature": item.signature,
  641. }
  642. for item in signatures
  643. ],
  644. "add_e2e_signing_key",
  645. )