test_event_signing.py 3.8 KB

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