signatures.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. from ._base import SQLBaseStore
  17. from unpaddedbase64 import encode_base64
  18. from synapse.crypto.event_signing import compute_event_reference_hash
  19. from synapse.util.caches.descriptors import cached, cachedList
  20. class SignatureStore(SQLBaseStore):
  21. """Persistence for event signatures and hashes"""
  22. @cached()
  23. def get_event_reference_hash(self, event_id):
  24. return self._get_event_reference_hashes_txn(event_id)
  25. @cachedList(cached_method_name="get_event_reference_hash",
  26. list_name="event_ids", num_args=1)
  27. def get_event_reference_hashes(self, event_ids):
  28. def f(txn):
  29. return {
  30. event_id: self._get_event_reference_hashes_txn(txn, event_id)
  31. for event_id in event_ids
  32. }
  33. return self.runInteraction(
  34. "get_event_reference_hashes",
  35. f
  36. )
  37. @defer.inlineCallbacks
  38. def add_event_hashes(self, event_ids):
  39. hashes = yield self.get_event_reference_hashes(
  40. event_ids
  41. )
  42. hashes = {
  43. e_id: {
  44. k: encode_base64(v) for k, v in h.items()
  45. if k == "sha256"
  46. }
  47. for e_id, h in hashes.items()
  48. }
  49. defer.returnValue(hashes.items())
  50. def _get_event_reference_hashes_txn(self, txn, event_id):
  51. """Get all the hashes for a given PDU.
  52. Args:
  53. txn (cursor):
  54. event_id (str): Id for the Event.
  55. Returns:
  56. A dict of algorithm -> hash.
  57. """
  58. query = (
  59. "SELECT algorithm, hash"
  60. " FROM event_reference_hashes"
  61. " WHERE event_id = ?"
  62. )
  63. txn.execute(query, (event_id, ))
  64. return {k: v for k, v in txn.fetchall()}
  65. def _store_event_reference_hashes_txn(self, txn, events):
  66. """Store a hash for a PDU
  67. Args:
  68. txn (cursor):
  69. events (list): list of Events.
  70. """
  71. vals = []
  72. for event in events:
  73. ref_alg, ref_hash_bytes = compute_event_reference_hash(event)
  74. vals.append({
  75. "event_id": event.event_id,
  76. "algorithm": ref_alg,
  77. "hash": buffer(ref_hash_bytes),
  78. })
  79. self._simple_insert_many_txn(
  80. txn,
  81. table="event_reference_hashes",
  82. values=vals,
  83. )