signatures.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. import six
  16. from unpaddedbase64 import encode_base64
  17. from twisted.internet import defer
  18. from synapse.crypto.event_signing import compute_event_reference_hash
  19. from synapse.storage._base import SQLBaseStore
  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 = six.moves.builtins.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(
  34. cached_method_name="get_event_reference_hash", list_name="event_ids", num_args=1
  35. )
  36. def get_event_reference_hashes(self, event_ids):
  37. def f(txn):
  38. return {
  39. event_id: self._get_event_reference_hashes_txn(txn, event_id)
  40. for event_id in event_ids
  41. }
  42. return self.db.runInteraction("get_event_reference_hashes", f)
  43. @defer.inlineCallbacks
  44. def add_event_hashes(self, event_ids):
  45. hashes = yield self.get_event_reference_hashes(event_ids)
  46. hashes = {
  47. e_id: {k: encode_base64(v) for k, v in h.items() if k == "sha256"}
  48. for e_id, h in hashes.items()
  49. }
  50. return list(hashes.items())
  51. def _get_event_reference_hashes_txn(self, txn, event_id):
  52. """Get all the hashes for a given PDU.
  53. Args:
  54. txn (cursor):
  55. event_id (str): Id for the Event.
  56. Returns:
  57. A dict[unicode, bytes] of algorithm -> hash.
  58. """
  59. query = (
  60. "SELECT algorithm, hash"
  61. " FROM event_reference_hashes"
  62. " WHERE event_id = ?"
  63. )
  64. txn.execute(query, (event_id,))
  65. return {k: v for k, v in txn}
  66. class SignatureStore(SignatureWorkerStore):
  67. """Persistence for event signatures and hashes"""
  68. def _store_event_reference_hashes_txn(self, txn, events):
  69. """Store a hash for a PDU
  70. Args:
  71. txn (cursor):
  72. events (list): list of Events.
  73. """
  74. vals = []
  75. for event in events:
  76. ref_alg, ref_hash_bytes = compute_event_reference_hash(event)
  77. vals.append(
  78. {
  79. "event_id": event.event_id,
  80. "algorithm": ref_alg,
  81. "hash": db_binary_type(ref_hash_bytes),
  82. }
  83. )
  84. self.db.simple_insert_many_txn(txn, table="event_reference_hashes", values=vals)