test_event_signing.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 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. import nacl.signing
  16. from unpaddedbase64 import decode_base64
  17. from synapse.crypto.event_signing import add_hashes_and_signatures
  18. from synapse.events.builder import EventBuilder
  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 = decode_base64(
  23. "YJDBA9Xnr2sVqXD9Vj7XVUnmFZcZrlw8Md7kMW+3XA1"
  24. )
  25. KEY_ALG = "ed25519"
  26. KEY_VER = 1
  27. KEY_NAME = "%s:%d" % (KEY_ALG, KEY_VER)
  28. HOSTNAME = "domain"
  29. class EventSigningTestCase(unittest.TestCase):
  30. def setUp(self):
  31. self.signing_key = nacl.signing.SigningKey(SIGNING_KEY_SEED)
  32. self.signing_key.alg = KEY_ALG
  33. self.signing_key.version = KEY_VER
  34. def test_sign_minimal(self):
  35. builder = EventBuilder(
  36. {
  37. 'event_id': "$0:domain",
  38. 'origin': "domain",
  39. 'origin_server_ts': 1000000,
  40. 'signatures': {},
  41. 'type': "X",
  42. 'unsigned': {'age_ts': 1000000},
  43. },
  44. )
  45. add_hashes_and_signatures(builder, HOSTNAME, self.signing_key)
  46. event = builder.build()
  47. self.assertTrue(hasattr(event, 'hashes'))
  48. self.assertIn('sha256', event.hashes)
  49. self.assertEquals(
  50. event.hashes['sha256'],
  51. "6tJjLpXtggfke8UxFhAKg82QVkJzvKOVOOSjUDK4ZSI",
  52. )
  53. self.assertTrue(hasattr(event, 'signatures'))
  54. self.assertIn(HOSTNAME, event.signatures)
  55. self.assertIn(KEY_NAME, event.signatures["domain"])
  56. self.assertEquals(
  57. event.signatures[HOSTNAME][KEY_NAME],
  58. "2Wptgo4CwmLo/Y8B8qinxApKaCkBG2fjTWB7AbP5Uy+"
  59. "aIbygsSdLOFzvdDjww8zUVKCmI02eP9xtyJxc/cLiBA",
  60. )
  61. def test_sign_message(self):
  62. builder = EventBuilder(
  63. {
  64. 'content': {
  65. 'body': "Here is the message content",
  66. },
  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. )
  77. add_hashes_and_signatures(builder, HOSTNAME, self.signing_key)
  78. event = builder.build()
  79. self.assertTrue(hasattr(event, 'hashes'))
  80. self.assertIn('sha256', event.hashes)
  81. self.assertEquals(
  82. event.hashes['sha256'],
  83. "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.assertEquals(
  89. event.signatures[HOSTNAME][KEY_NAME],
  90. "Wm+VzmOUOz08Ds+0NTWb1d4CZrVsJSikkeRxh6aCcUw"
  91. "u6pNC78FunoD7KNWzqFn241eYHYMGCA5McEiVPdhzBA"
  92. )