event_signing.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 synapse.api.errors import SynapseError, Codes
  16. from synapse.events.utils import prune_event
  17. from canonicaljson import encode_canonical_json
  18. from unpaddedbase64 import encode_base64, decode_base64
  19. from signedjson.sign import sign_json
  20. import hashlib
  21. import logging
  22. logger = logging.getLogger(__name__)
  23. def check_event_content_hash(event, hash_algorithm=hashlib.sha256):
  24. """Check whether the hash for this PDU matches the contents"""
  25. name, expected_hash = compute_content_hash(event, hash_algorithm)
  26. logger.debug("Expecting hash: %s", encode_base64(expected_hash))
  27. if name not in event.hashes:
  28. raise SynapseError(
  29. 400,
  30. "Algorithm %s not in hashes %s" % (
  31. name, list(event.hashes),
  32. ),
  33. Codes.UNAUTHORIZED,
  34. )
  35. message_hash_base64 = event.hashes[name]
  36. try:
  37. message_hash_bytes = decode_base64(message_hash_base64)
  38. except:
  39. raise SynapseError(
  40. 400,
  41. "Invalid base64: %s" % (message_hash_base64,),
  42. Codes.UNAUTHORIZED,
  43. )
  44. return message_hash_bytes == expected_hash
  45. def compute_content_hash(event, hash_algorithm):
  46. event_json = event.get_pdu_json()
  47. event_json.pop("age_ts", None)
  48. event_json.pop("unsigned", None)
  49. event_json.pop("signatures", None)
  50. event_json.pop("hashes", None)
  51. event_json.pop("outlier", None)
  52. event_json.pop("destinations", None)
  53. event_json_bytes = encode_canonical_json(event_json)
  54. hashed = hash_algorithm(event_json_bytes)
  55. return (hashed.name, hashed.digest())
  56. def compute_event_reference_hash(event, hash_algorithm=hashlib.sha256):
  57. tmp_event = prune_event(event)
  58. event_json = tmp_event.get_pdu_json()
  59. event_json.pop("signatures", None)
  60. event_json.pop("age_ts", None)
  61. event_json.pop("unsigned", None)
  62. event_json_bytes = encode_canonical_json(event_json)
  63. hashed = hash_algorithm(event_json_bytes)
  64. return (hashed.name, hashed.digest())
  65. def compute_event_signature(event, signature_name, signing_key):
  66. tmp_event = prune_event(event)
  67. redact_json = tmp_event.get_pdu_json()
  68. redact_json.pop("age_ts", None)
  69. redact_json.pop("unsigned", None)
  70. logger.debug("Signing event: %s", encode_canonical_json(redact_json))
  71. redact_json = sign_json(redact_json, signature_name, signing_key)
  72. logger.debug("Signed event: %s", encode_canonical_json(redact_json))
  73. return redact_json["signatures"]
  74. def add_hashes_and_signatures(event, signature_name, signing_key,
  75. hash_algorithm=hashlib.sha256):
  76. # if hasattr(event, "old_state_events"):
  77. # state_json_bytes = encode_canonical_json(
  78. # [e.event_id for e in event.old_state_events.values()]
  79. # )
  80. # hashed = hash_algorithm(state_json_bytes)
  81. # event.state_hash = {
  82. # hashed.name: encode_base64(hashed.digest())
  83. # }
  84. name, digest = compute_content_hash(event, hash_algorithm=hash_algorithm)
  85. if not hasattr(event, "hashes"):
  86. event.hashes = {}
  87. event.hashes[name] = encode_base64(digest)
  88. event.signatures = compute_event_signature(
  89. event,
  90. signature_name=signature_name,
  91. signing_key=signing_key,
  92. )