1
0

test_event_signing.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # Copyright 2015, 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 signedjson.key import decode_signing_key_base64
  15. from signedjson.types import SigningKey
  16. from synapse.api.room_versions import RoomVersions
  17. from synapse.crypto.event_signing import add_hashes_and_signatures
  18. from synapse.events import make_event_from_dict
  19. from tests import unittest
  20. # Perform these tests using given secret key so we get entirely deterministic
  21. # signatures output that we can test against.
  22. SIGNING_KEY_SEED = "YJDBA9Xnr2sVqXD9Vj7XVUnmFZcZrlw8Md7kMW+3XA1"
  23. KEY_ALG = "ed25519"
  24. KEY_VER = "1"
  25. KEY_NAME = "%s:%s" % (KEY_ALG, KEY_VER)
  26. HOSTNAME = "domain"
  27. class EventSigningTestCase(unittest.TestCase):
  28. def setUp(self) -> None:
  29. self.signing_key: SigningKey = decode_signing_key_base64(
  30. KEY_ALG, KEY_VER, SIGNING_KEY_SEED
  31. )
  32. def test_sign_minimal(self) -> None:
  33. event_dict = {
  34. "event_id": "$0:domain",
  35. "origin": "domain",
  36. "origin_server_ts": 1000000,
  37. "signatures": {},
  38. "type": "X",
  39. "unsigned": {"age_ts": 1000000},
  40. }
  41. add_hashes_and_signatures(
  42. RoomVersions.V1, event_dict, HOSTNAME, self.signing_key
  43. )
  44. event = make_event_from_dict(event_dict)
  45. self.assertTrue(hasattr(event, "hashes"))
  46. self.assertIn("sha256", event.hashes)
  47. self.assertEqual(
  48. event.hashes["sha256"], "6tJjLpXtggfke8UxFhAKg82QVkJzvKOVOOSjUDK4ZSI"
  49. )
  50. self.assertTrue(hasattr(event, "signatures"))
  51. self.assertIn(HOSTNAME, event.signatures)
  52. self.assertIn(KEY_NAME, event.signatures["domain"])
  53. self.assertEqual(
  54. event.signatures[HOSTNAME][KEY_NAME],
  55. "2Wptgo4CwmLo/Y8B8qinxApKaCkBG2fjTWB7AbP5Uy+"
  56. "aIbygsSdLOFzvdDjww8zUVKCmI02eP9xtyJxc/cLiBA",
  57. )
  58. def test_sign_message(self) -> None:
  59. event_dict = {
  60. "content": {"body": "Here is the message content"},
  61. "event_id": "$0:domain",
  62. "origin": "domain",
  63. "origin_server_ts": 1000000,
  64. "type": "m.room.message",
  65. "room_id": "!r:domain",
  66. "sender": "@u:domain",
  67. "signatures": {},
  68. "unsigned": {"age_ts": 1000000},
  69. }
  70. add_hashes_and_signatures(
  71. RoomVersions.V1, event_dict, HOSTNAME, self.signing_key
  72. )
  73. event = make_event_from_dict(event_dict)
  74. self.assertTrue(hasattr(event, "hashes"))
  75. self.assertIn("sha256", event.hashes)
  76. self.assertEqual(
  77. event.hashes["sha256"], "onLKD1bGljeBWQhWZ1kaP9SorVmRQNdN5aM2JYU2n/g"
  78. )
  79. self.assertTrue(hasattr(event, "signatures"))
  80. self.assertIn(HOSTNAME, event.signatures)
  81. self.assertIn(KEY_NAME, event.signatures["domain"])
  82. self.assertEqual(
  83. event.signatures[HOSTNAME][KEY_NAME],
  84. "Wm+VzmOUOz08Ds+0NTWb1d4CZrVsJSikkeRxh6aCcUw"
  85. "u6pNC78FunoD7KNWzqFn241eYHYMGCA5McEiVPdhzBA",
  86. )