You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

88 lines
2.9 KiB

  1. # Copyright 2014-2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Collection, Dict, List, Tuple
  15. from unpaddedbase64 import encode_base64
  16. from synapse.crypto.event_signing import compute_event_reference_hash
  17. from synapse.storage.databases.main.events_worker import (
  18. EventRedactBehaviour,
  19. EventsWorkerStore,
  20. )
  21. from synapse.util.caches.descriptors import cached, cachedList
  22. class SignatureWorkerStore(EventsWorkerStore):
  23. @cached()
  24. def get_event_reference_hash(self, event_id: str) -> Dict[str, Dict[str, bytes]]:
  25. # This is a dummy function to allow get_event_reference_hashes
  26. # to use its cache
  27. raise NotImplementedError()
  28. @cachedList(
  29. cached_method_name="get_event_reference_hash", list_name="event_ids", num_args=1
  30. )
  31. async def get_event_reference_hashes(
  32. self, event_ids: Collection[str]
  33. ) -> Dict[str, Dict[str, bytes]]:
  34. """Get all hashes for given events.
  35. Args:
  36. event_ids: The event IDs to get hashes for.
  37. Returns:
  38. A mapping of event ID to a mapping of algorithm to hash.
  39. Returns an empty dict for a given event id if that event is unknown.
  40. """
  41. events = await self.get_events(
  42. event_ids,
  43. redact_behaviour=EventRedactBehaviour.as_is,
  44. allow_rejected=True,
  45. )
  46. hashes: Dict[str, Dict[str, bytes]] = {}
  47. for event_id in event_ids:
  48. event = events.get(event_id)
  49. if event is None:
  50. hashes[event_id] = {}
  51. else:
  52. ref_alg, ref_hash_bytes = compute_event_reference_hash(event)
  53. hashes[event_id] = {ref_alg: ref_hash_bytes}
  54. return hashes
  55. async def add_event_hashes(
  56. self, event_ids: Collection[str]
  57. ) -> List[Tuple[str, Dict[str, str]]]:
  58. """
  59. Args:
  60. event_ids: The event IDs
  61. Returns:
  62. A list of tuples of event ID and a mapping of algorithm to base-64 encoded hash.
  63. """
  64. hashes = await self.get_event_reference_hashes(event_ids)
  65. encoded_hashes = {
  66. e_id: {k: encode_base64(v) for k, v in h.items() if k == "sha256"}
  67. for e_id, h in hashes.items()
  68. }
  69. return list(encoded_hashes.items())
  70. class SignatureStore(SignatureWorkerStore):
  71. """Persistence for event signatures and hashes"""