end_to_end_keys.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket Ltd
  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 ._base import SQLBaseStore
  16. class EndToEndKeyStore(SQLBaseStore):
  17. def set_e2e_device_keys(self, user_id, device_id, time_now, json_bytes):
  18. return self._simple_upsert(
  19. table="e2e_device_keys_json",
  20. keyvalues={
  21. "user_id": user_id,
  22. "device_id": device_id,
  23. },
  24. values={
  25. "ts_added_ms": time_now,
  26. "key_json": json_bytes,
  27. }
  28. )
  29. def get_e2e_device_keys(self, query_list):
  30. """Fetch a list of device keys.
  31. Args:
  32. query_list(list): List of pairs of user_ids and device_ids.
  33. Returns:
  34. Dict mapping from user-id to dict mapping from device_id to
  35. key json byte strings.
  36. """
  37. def _get_e2e_device_keys(txn):
  38. result = {}
  39. for user_id, device_id in query_list:
  40. user_result = result.setdefault(user_id, {})
  41. keyvalues = {"user_id": user_id}
  42. if device_id:
  43. keyvalues["device_id"] = device_id
  44. rows = self._simple_select_list_txn(
  45. txn, table="e2e_device_keys_json",
  46. keyvalues=keyvalues,
  47. retcols=["device_id", "key_json"]
  48. )
  49. for row in rows:
  50. user_result[row["device_id"]] = row["key_json"]
  51. return result
  52. return self.runInteraction("get_e2e_device_keys", _get_e2e_device_keys)
  53. def add_e2e_one_time_keys(self, user_id, device_id, time_now, key_list):
  54. def _add_e2e_one_time_keys(txn):
  55. for (algorithm, key_id, json_bytes) in key_list:
  56. self._simple_upsert_txn(
  57. txn, table="e2e_one_time_keys_json",
  58. keyvalues={
  59. "user_id": user_id,
  60. "device_id": device_id,
  61. "algorithm": algorithm,
  62. "key_id": key_id,
  63. },
  64. values={
  65. "ts_added_ms": time_now,
  66. "key_json": json_bytes,
  67. }
  68. )
  69. return self.runInteraction(
  70. "add_e2e_one_time_keys", _add_e2e_one_time_keys
  71. )
  72. def count_e2e_one_time_keys(self, user_id, device_id):
  73. """ Count the number of one time keys the server has for a device
  74. Returns:
  75. Dict mapping from algorithm to number of keys for that algorithm.
  76. """
  77. def _count_e2e_one_time_keys(txn):
  78. sql = (
  79. "SELECT algorithm, COUNT(key_id) FROM e2e_one_time_keys_json"
  80. " WHERE user_id = ? AND device_id = ?"
  81. " GROUP BY algorithm"
  82. )
  83. txn.execute(sql, (user_id, device_id))
  84. result = {}
  85. for algorithm, key_count in txn.fetchall():
  86. result[algorithm] = key_count
  87. return result
  88. return self.runInteraction(
  89. "count_e2e_one_time_keys", _count_e2e_one_time_keys
  90. )
  91. def claim_e2e_one_time_keys(self, query_list):
  92. """Take a list of one time keys out of the database"""
  93. def _claim_e2e_one_time_keys(txn):
  94. sql = (
  95. "SELECT key_id, key_json FROM e2e_one_time_keys_json"
  96. " WHERE user_id = ? AND device_id = ? AND algorithm = ?"
  97. " LIMIT 1"
  98. )
  99. result = {}
  100. delete = []
  101. for user_id, device_id, algorithm in query_list:
  102. user_result = result.setdefault(user_id, {})
  103. device_result = user_result.setdefault(device_id, {})
  104. txn.execute(sql, (user_id, device_id, algorithm))
  105. for key_id, key_json in txn.fetchall():
  106. device_result[algorithm + ":" + key_id] = key_json
  107. delete.append((user_id, device_id, algorithm, key_id))
  108. sql = (
  109. "DELETE FROM e2e_one_time_keys_json"
  110. " WHERE user_id = ? AND device_id = ? AND algorithm = ?"
  111. " AND key_id = ?"
  112. )
  113. for user_id, device_id, algorithm, key_id in delete:
  114. txn.execute(sql, (user_id, device_id, algorithm, key_id))
  115. return result
  116. return self.runInteraction(
  117. "claim_e2e_one_time_keys", _claim_e2e_one_time_keys
  118. )