test_remote_key_resource.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. # Copyright 2020 The Matrix.org Foundation C.I.C.
  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 io import BytesIO, StringIO
  15. from typing import Any, Dict, Optional, Union
  16. from unittest.mock import Mock
  17. import signedjson.key
  18. from canonicaljson import encode_canonical_json
  19. from signedjson.sign import sign_json
  20. from signedjson.types import SigningKey
  21. from twisted.test.proto_helpers import MemoryReactor
  22. from twisted.web.resource import NoResource, Resource
  23. from synapse.crypto.keyring import PerspectivesKeyFetcher
  24. from synapse.http.site import SynapseRequest
  25. from synapse.rest.key.v2 import KeyResource
  26. from synapse.server import HomeServer
  27. from synapse.storage.keys import FetchKeyResult
  28. from synapse.types import JsonDict
  29. from synapse.util import Clock
  30. from synapse.util.httpresourcetree import create_resource_tree
  31. from synapse.util.stringutils import random_string
  32. from tests import unittest
  33. from tests.server import FakeChannel
  34. from tests.utils import default_config
  35. class BaseRemoteKeyResourceTestCase(unittest.HomeserverTestCase):
  36. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  37. self.http_client = Mock()
  38. return self.setup_test_homeserver(federation_http_client=self.http_client)
  39. def create_test_resource(self) -> Resource:
  40. return create_resource_tree(
  41. {"/_matrix/key/v2": KeyResource(self.hs)}, root_resource=NoResource()
  42. )
  43. def expect_outgoing_key_request(
  44. self, server_name: str, signing_key: SigningKey
  45. ) -> None:
  46. """
  47. Tell the mock http client to expect an outgoing GET request for the given key
  48. """
  49. async def get_json(
  50. destination: str,
  51. path: str,
  52. ignore_backoff: bool = False,
  53. **kwargs: Any,
  54. ) -> Union[JsonDict, list]:
  55. self.assertTrue(ignore_backoff)
  56. self.assertEqual(destination, server_name)
  57. key_id = "%s:%s" % (signing_key.alg, signing_key.version)
  58. self.assertEqual(path, "/_matrix/key/v2/server")
  59. response = {
  60. "server_name": server_name,
  61. "old_verify_keys": {},
  62. "valid_until_ts": 200 * 1000,
  63. "verify_keys": {
  64. key_id: {
  65. "key": signedjson.key.encode_verify_key_base64(
  66. signedjson.key.get_verify_key(signing_key)
  67. )
  68. }
  69. },
  70. }
  71. sign_json(response, server_name, signing_key)
  72. return response
  73. self.http_client.get_json.side_effect = get_json
  74. class RemoteKeyResourceTestCase(BaseRemoteKeyResourceTestCase):
  75. def make_notary_request(self, server_name: str, key_id: str) -> dict:
  76. """Send a GET request to the test server requesting the given key.
  77. Checks that the response is a 200 and returns the decoded json body.
  78. """
  79. channel = FakeChannel(self.site, self.reactor)
  80. # channel is a `FakeChannel` but `HTTPChannel` is expected
  81. req = SynapseRequest(channel, self.site) # type: ignore[arg-type]
  82. req.content = BytesIO(b"")
  83. req.requestReceived(
  84. b"GET",
  85. b"/_matrix/key/v2/query/%s/%s"
  86. % (server_name.encode("utf-8"), key_id.encode("utf-8")),
  87. b"1.1",
  88. )
  89. channel.await_result()
  90. self.assertEqual(channel.code, 200)
  91. resp = channel.json_body
  92. return resp
  93. def test_get_key(self) -> None:
  94. """Fetch a remote key"""
  95. SERVER_NAME = "remote.server"
  96. testkey = signedjson.key.generate_signing_key("ver1")
  97. self.expect_outgoing_key_request(SERVER_NAME, testkey)
  98. resp = self.make_notary_request(SERVER_NAME, "ed25519:ver1")
  99. keys = resp["server_keys"]
  100. self.assertEqual(len(keys), 1)
  101. self.assertIn("ed25519:ver1", keys[0]["verify_keys"])
  102. self.assertEqual(len(keys[0]["verify_keys"]), 1)
  103. # it should be signed by both the origin server and the notary
  104. self.assertIn(SERVER_NAME, keys[0]["signatures"])
  105. self.assertIn(self.hs.hostname, keys[0]["signatures"])
  106. def test_get_own_key(self) -> None:
  107. """Fetch our own key"""
  108. testkey = signedjson.key.generate_signing_key("ver1")
  109. self.expect_outgoing_key_request(self.hs.hostname, testkey)
  110. resp = self.make_notary_request(self.hs.hostname, "ed25519:ver1")
  111. keys = resp["server_keys"]
  112. self.assertEqual(len(keys), 1)
  113. # it should be signed by both itself, and the notary signing key
  114. sigs = keys[0]["signatures"]
  115. self.assertEqual(len(sigs), 1)
  116. self.assertIn(self.hs.hostname, sigs)
  117. oursigs = sigs[self.hs.hostname]
  118. self.assertEqual(len(oursigs), 2)
  119. # the requested key should be present in the verify_keys section
  120. self.assertIn("ed25519:ver1", keys[0]["verify_keys"])
  121. class EndToEndPerspectivesTests(BaseRemoteKeyResourceTestCase):
  122. """End-to-end tests of the perspectives fetch case
  123. The idea here is to actually wire up a PerspectivesKeyFetcher to the notary
  124. endpoint, to check that the two implementations are compatible.
  125. """
  126. def default_config(self) -> Dict[str, Any]:
  127. config = super().default_config()
  128. # replace the signing key with our own
  129. self.hs_signing_key = signedjson.key.generate_signing_key("kssk")
  130. strm = StringIO()
  131. signedjson.key.write_signing_keys(strm, [self.hs_signing_key])
  132. config["signing_key"] = strm.getvalue()
  133. return config
  134. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  135. # make a second homeserver, configured to use the first one as a key notary
  136. self.http_client2 = Mock()
  137. config = default_config(name="keyclient")
  138. config["trusted_key_servers"] = [
  139. {
  140. "server_name": self.hs.hostname,
  141. "verify_keys": {
  142. "ed25519:%s"
  143. % (
  144. self.hs_signing_key.version,
  145. ): signedjson.key.encode_verify_key_base64(
  146. signedjson.key.get_verify_key(self.hs_signing_key)
  147. )
  148. },
  149. }
  150. ]
  151. self.hs2 = self.setup_test_homeserver(
  152. federation_http_client=self.http_client2, config=config
  153. )
  154. # wire up outbound POST /key/v2/query requests from hs2 so that they
  155. # will be forwarded to hs1
  156. async def post_json(
  157. destination: str, path: str, data: Optional[JsonDict] = None
  158. ) -> Union[JsonDict, list]:
  159. self.assertEqual(destination, self.hs.hostname)
  160. self.assertEqual(
  161. path,
  162. "/_matrix/key/v2/query",
  163. )
  164. channel = FakeChannel(self.site, self.reactor)
  165. # channel is a `FakeChannel` but `HTTPChannel` is expected
  166. req = SynapseRequest(channel, self.site) # type: ignore[arg-type]
  167. req.content = BytesIO(encode_canonical_json(data))
  168. req.requestReceived(
  169. b"POST",
  170. path.encode("utf-8"),
  171. b"1.1",
  172. )
  173. channel.await_result()
  174. self.assertEqual(channel.code, 200)
  175. resp = channel.json_body
  176. return resp
  177. self.http_client2.post_json.side_effect = post_json
  178. def test_get_key(self) -> None:
  179. """Fetch a key belonging to a random server"""
  180. # make up a key to be fetched.
  181. testkey = signedjson.key.generate_signing_key("abc")
  182. # we expect hs1 to make a regular key request to the target server
  183. self.expect_outgoing_key_request("targetserver", testkey)
  184. keyid = "ed25519:%s" % (testkey.version,)
  185. fetcher = PerspectivesKeyFetcher(self.hs2)
  186. d = fetcher.get_keys("targetserver", [keyid], 1000)
  187. res = self.get_success(d)
  188. self.assertIn(keyid, res)
  189. keyres = res[keyid]
  190. assert isinstance(keyres, FetchKeyResult)
  191. self.assertEqual(
  192. signedjson.key.encode_verify_key_base64(keyres.verify_key),
  193. signedjson.key.encode_verify_key_base64(
  194. signedjson.key.get_verify_key(testkey)
  195. ),
  196. )
  197. def test_get_notary_key(self) -> None:
  198. """Fetch a key belonging to the notary server"""
  199. # make up a key to be fetched. We randomise the keyid to try to get it to
  200. # appear before the key server signing key sometimes (otherwise we bail out
  201. # before fetching its signature)
  202. testkey = signedjson.key.generate_signing_key(random_string(5))
  203. # we expect hs1 to make a regular key request to itself
  204. self.expect_outgoing_key_request(self.hs.hostname, testkey)
  205. keyid = "ed25519:%s" % (testkey.version,)
  206. fetcher = PerspectivesKeyFetcher(self.hs2)
  207. d = fetcher.get_keys(self.hs.hostname, [keyid], 1000)
  208. res = self.get_success(d)
  209. self.assertIn(keyid, res)
  210. keyres = res[keyid]
  211. assert isinstance(keyres, FetchKeyResult)
  212. self.assertEqual(
  213. signedjson.key.encode_verify_key_base64(keyres.verify_key),
  214. signedjson.key.encode_verify_key_base64(
  215. signedjson.key.get_verify_key(testkey)
  216. ),
  217. )
  218. def test_get_notary_keyserver_key(self) -> None:
  219. """Fetch the notary's keyserver key"""
  220. # we expect hs1 to make a regular key request to itself
  221. self.expect_outgoing_key_request(self.hs.hostname, self.hs_signing_key)
  222. keyid = "ed25519:%s" % (self.hs_signing_key.version,)
  223. fetcher = PerspectivesKeyFetcher(self.hs2)
  224. d = fetcher.get_keys(self.hs.hostname, [keyid], 1000)
  225. res = self.get_success(d)
  226. self.assertIn(keyid, res)
  227. keyres = res[keyid]
  228. assert isinstance(keyres, FetchKeyResult)
  229. self.assertEqual(
  230. signedjson.key.encode_verify_key_base64(keyres.verify_key),
  231. signedjson.key.encode_verify_key_base64(
  232. signedjson.key.get_verify_key(self.hs_signing_key)
  233. ),
  234. )