signatures.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-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 twisted.internet import defer
  16. import six
  17. from ._base import SQLBaseStore
  18. from unpaddedbase64 import encode_base64
  19. from synapse.crypto.event_signing import compute_event_reference_hash
  20. from synapse.util.caches.descriptors import cached, cachedList
  21. # py2 sqlite has buffer hardcoded as only binary type, so we must use it,
  22. # despite being deprecated and removed in favor of memoryview
  23. if six.PY2:
  24. db_binary_type = buffer
  25. else:
  26. db_binary_type = memoryview
  27. class SignatureWorkerStore(SQLBaseStore):
  28. @cached()
  29. def get_event_reference_hash(self, event_id):
  30. # This is a dummy function to allow get_event_reference_hashes
  31. # to use its cache
  32. raise NotImplementedError()
  33. @cachedList(cached_method_name="get_event_reference_hash",
  34. list_name="event_ids", num_args=1)
  35. def get_event_reference_hashes(self, event_ids):
  36. def f(txn):
  37. return {
  38. event_id: self._get_event_reference_hashes_txn(txn, event_id)
  39. for event_id in event_ids
  40. }
  41. return self.runInteraction(
  42. "get_event_reference_hashes",
  43. f
  44. )
  45. @defer.inlineCallbacks
  46. def add_event_hashes(self, event_ids):
  47. hashes = yield self.get_event_reference_hashes(
  48. event_ids
  49. )
  50. hashes = {
  51. e_id: {
  52. k: encode_base64(v) for k, v in h.items()
  53. if k == "sha256"
  54. }
  55. for e_id, h in hashes.items()
  56. }
  57. defer.returnValue(list(hashes.items()))
  58. def _get_event_reference_hashes_txn(self, txn, event_id):
  59. """Get all the hashes for a given PDU.
  60. Args:
  61. txn (cursor):
  62. event_id (str): Id for the Event.
  63. Returns:
  64. A dict of algorithm -> hash.
  65. """
  66. query = (
  67. "SELECT algorithm, hash"
  68. " FROM event_reference_hashes"
  69. " WHERE event_id = ?"
  70. )
  71. txn.execute(query, (event_id, ))
  72. return {k: v for k, v in txn}
  73. class SignatureStore(SignatureWorkerStore):
  74. """Persistence for event signatures and hashes"""
  75. def _store_event_reference_hashes_txn(self, txn, events):
  76. """Store a hash for a PDU
  77. Args:
  78. txn (cursor):
  79. events (list): list of Events.
  80. """
  81. vals = []
  82. for event in events:
  83. ref_alg, ref_hash_bytes = compute_event_reference_hash(event)
  84. vals.append({
  85. "event_id": event.event_id,
  86. "algorithm": ref_alg,
  87. "hash": db_binary_type(ref_hash_bytes),
  88. })
  89. self._simple_insert_many_txn(
  90. txn,
  91. table="event_reference_hashes",
  92. values=vals,
  93. )