2020-01-06 13:28:58 +01:00
|
|
|
# Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
import urllib.parse
|
2020-01-08 14:24:10 +01:00
|
|
|
from io import BytesIO, StringIO
|
2022-03-03 17:05:44 +01:00
|
|
|
from typing import Any, Dict, Optional, Union
|
2021-04-09 19:44:38 +02:00
|
|
|
from unittest.mock import Mock
|
2020-01-06 13:28:58 +01:00
|
|
|
|
|
|
|
import signedjson.key
|
2020-01-08 14:24:10 +01:00
|
|
|
from canonicaljson import encode_canonical_json
|
2020-01-06 13:28:58 +01:00
|
|
|
from signedjson.sign import sign_json
|
2022-03-03 17:05:44 +01:00
|
|
|
from signedjson.types import SigningKey
|
2020-01-06 13:28:58 +01:00
|
|
|
|
2022-03-03 17:05:44 +01:00
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
|
|
|
from twisted.web.resource import NoResource, Resource
|
2020-01-06 13:28:58 +01:00
|
|
|
|
2020-01-08 14:24:10 +01:00
|
|
|
from synapse.crypto.keyring import PerspectivesKeyFetcher
|
2020-01-06 13:28:58 +01:00
|
|
|
from synapse.http.site import SynapseRequest
|
|
|
|
from synapse.rest.key.v2 import KeyApiV2Resource
|
2022-03-03 17:05:44 +01:00
|
|
|
from synapse.server import HomeServer
|
2020-01-08 14:24:10 +01:00
|
|
|
from synapse.storage.keys import FetchKeyResult
|
2022-03-03 17:05:44 +01:00
|
|
|
from synapse.types import JsonDict
|
|
|
|
from synapse.util import Clock
|
2020-01-06 13:28:58 +01:00
|
|
|
from synapse.util.httpresourcetree import create_resource_tree
|
2020-01-08 14:24:10 +01:00
|
|
|
from synapse.util.stringutils import random_string
|
2020-01-06 13:28:58 +01:00
|
|
|
|
|
|
|
from tests import unittest
|
2020-11-16 19:21:47 +01:00
|
|
|
from tests.server import FakeChannel
|
2020-01-08 14:24:10 +01:00
|
|
|
from tests.utils import default_config
|
2020-01-06 13:28:58 +01:00
|
|
|
|
|
|
|
|
2020-01-08 14:24:10 +01:00
|
|
|
class BaseRemoteKeyResourceTestCase(unittest.HomeserverTestCase):
|
2022-03-03 17:05:44 +01:00
|
|
|
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
|
2020-01-06 13:28:58 +01:00
|
|
|
self.http_client = Mock()
|
2020-12-02 17:09:24 +01:00
|
|
|
return self.setup_test_homeserver(federation_http_client=self.http_client)
|
2020-01-06 13:28:58 +01:00
|
|
|
|
2022-03-03 17:05:44 +01:00
|
|
|
def create_test_resource(self) -> Resource:
|
2020-01-06 13:28:58 +01:00
|
|
|
return create_resource_tree(
|
|
|
|
{"/_matrix/key/v2": KeyApiV2Resource(self.hs)}, root_resource=NoResource()
|
|
|
|
)
|
|
|
|
|
|
|
|
def expect_outgoing_key_request(
|
|
|
|
self, server_name: str, signing_key: SigningKey
|
|
|
|
) -> None:
|
|
|
|
"""
|
|
|
|
Tell the mock http client to expect an outgoing GET request for the given key
|
|
|
|
"""
|
|
|
|
|
2022-03-03 17:05:44 +01:00
|
|
|
async def get_json(
|
|
|
|
destination: str,
|
|
|
|
path: str,
|
|
|
|
ignore_backoff: bool = False,
|
|
|
|
**kwargs: Any,
|
|
|
|
) -> Union[JsonDict, list]:
|
2020-01-06 13:28:58 +01:00
|
|
|
self.assertTrue(ignore_backoff)
|
|
|
|
self.assertEqual(destination, server_name)
|
|
|
|
key_id = "%s:%s" % (signing_key.alg, signing_key.version)
|
|
|
|
self.assertEqual(
|
|
|
|
path, "/_matrix/key/v2/server/%s" % (urllib.parse.quote(key_id),)
|
|
|
|
)
|
|
|
|
|
|
|
|
response = {
|
|
|
|
"server_name": server_name,
|
|
|
|
"old_verify_keys": {},
|
|
|
|
"valid_until_ts": 200 * 1000,
|
|
|
|
"verify_keys": {
|
|
|
|
key_id: {
|
|
|
|
"key": signedjson.key.encode_verify_key_base64(
|
|
|
|
signing_key.verify_key
|
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
sign_json(response, server_name, signing_key)
|
|
|
|
return response
|
|
|
|
|
|
|
|
self.http_client.get_json.side_effect = get_json
|
|
|
|
|
2020-01-08 14:24:10 +01:00
|
|
|
|
|
|
|
class RemoteKeyResourceTestCase(BaseRemoteKeyResourceTestCase):
|
2020-01-06 13:28:58 +01:00
|
|
|
def make_notary_request(self, server_name: str, key_id: str) -> dict:
|
|
|
|
"""Send a GET request to the test server requesting the given key.
|
|
|
|
|
|
|
|
Checks that the response is a 200 and returns the decoded json body.
|
|
|
|
"""
|
|
|
|
channel = FakeChannel(self.site, self.reactor)
|
2022-03-03 17:05:44 +01:00
|
|
|
# channel is a `FakeChannel` but `HTTPChannel` is expected
|
|
|
|
req = SynapseRequest(channel, self.site) # type: ignore[arg-type]
|
2020-01-06 13:28:58 +01:00
|
|
|
req.content = BytesIO(b"")
|
|
|
|
req.requestReceived(
|
|
|
|
b"GET",
|
|
|
|
b"/_matrix/key/v2/query/%s/%s"
|
|
|
|
% (server_name.encode("utf-8"), key_id.encode("utf-8")),
|
|
|
|
b"1.1",
|
|
|
|
)
|
2020-11-16 19:21:47 +01:00
|
|
|
channel.await_result()
|
2020-01-06 13:28:58 +01:00
|
|
|
self.assertEqual(channel.code, 200)
|
|
|
|
resp = channel.json_body
|
|
|
|
return resp
|
|
|
|
|
2022-03-03 17:05:44 +01:00
|
|
|
def test_get_key(self) -> None:
|
2020-01-06 13:28:58 +01:00
|
|
|
"""Fetch a remote key"""
|
|
|
|
SERVER_NAME = "remote.server"
|
|
|
|
testkey = signedjson.key.generate_signing_key("ver1")
|
|
|
|
self.expect_outgoing_key_request(SERVER_NAME, testkey)
|
|
|
|
|
|
|
|
resp = self.make_notary_request(SERVER_NAME, "ed25519:ver1")
|
|
|
|
keys = resp["server_keys"]
|
|
|
|
self.assertEqual(len(keys), 1)
|
|
|
|
|
|
|
|
self.assertIn("ed25519:ver1", keys[0]["verify_keys"])
|
|
|
|
self.assertEqual(len(keys[0]["verify_keys"]), 1)
|
|
|
|
|
|
|
|
# it should be signed by both the origin server and the notary
|
|
|
|
self.assertIn(SERVER_NAME, keys[0]["signatures"])
|
|
|
|
self.assertIn(self.hs.hostname, keys[0]["signatures"])
|
|
|
|
|
2022-03-03 17:05:44 +01:00
|
|
|
def test_get_own_key(self) -> None:
|
2020-01-06 13:28:58 +01:00
|
|
|
"""Fetch our own key"""
|
|
|
|
testkey = signedjson.key.generate_signing_key("ver1")
|
|
|
|
self.expect_outgoing_key_request(self.hs.hostname, testkey)
|
|
|
|
|
|
|
|
resp = self.make_notary_request(self.hs.hostname, "ed25519:ver1")
|
|
|
|
keys = resp["server_keys"]
|
|
|
|
self.assertEqual(len(keys), 1)
|
|
|
|
|
|
|
|
# it should be signed by both itself, and the notary signing key
|
|
|
|
sigs = keys[0]["signatures"]
|
|
|
|
self.assertEqual(len(sigs), 1)
|
|
|
|
self.assertIn(self.hs.hostname, sigs)
|
|
|
|
oursigs = sigs[self.hs.hostname]
|
|
|
|
self.assertEqual(len(oursigs), 2)
|
|
|
|
|
2020-01-08 14:24:10 +01:00
|
|
|
# the requested key should be present in the verify_keys section
|
2020-01-06 13:28:58 +01:00
|
|
|
self.assertIn("ed25519:ver1", keys[0]["verify_keys"])
|
2020-01-08 14:24:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
class EndToEndPerspectivesTests(BaseRemoteKeyResourceTestCase):
|
|
|
|
"""End-to-end tests of the perspectives fetch case
|
|
|
|
|
|
|
|
The idea here is to actually wire up a PerspectivesKeyFetcher to the notary
|
|
|
|
endpoint, to check that the two implementations are compatible.
|
|
|
|
"""
|
|
|
|
|
2022-03-03 17:05:44 +01:00
|
|
|
def default_config(self) -> Dict[str, Any]:
|
2020-03-24 19:33:49 +01:00
|
|
|
config = super().default_config()
|
2020-01-08 14:24:10 +01:00
|
|
|
|
|
|
|
# replace the signing key with our own
|
|
|
|
self.hs_signing_key = signedjson.key.generate_signing_key("kssk")
|
|
|
|
strm = StringIO()
|
|
|
|
signedjson.key.write_signing_keys(strm, [self.hs_signing_key])
|
|
|
|
config["signing_key"] = strm.getvalue()
|
|
|
|
|
|
|
|
return config
|
|
|
|
|
2022-03-03 17:05:44 +01:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2020-01-08 14:24:10 +01:00
|
|
|
# make a second homeserver, configured to use the first one as a key notary
|
|
|
|
self.http_client2 = Mock()
|
|
|
|
config = default_config(name="keyclient")
|
|
|
|
config["trusted_key_servers"] = [
|
|
|
|
{
|
|
|
|
"server_name": self.hs.hostname,
|
|
|
|
"verify_keys": {
|
|
|
|
"ed25519:%s"
|
|
|
|
% (
|
|
|
|
self.hs_signing_key.version,
|
|
|
|
): signedjson.key.encode_verify_key_base64(
|
|
|
|
self.hs_signing_key.verify_key
|
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
]
|
|
|
|
self.hs2 = self.setup_test_homeserver(
|
2020-12-02 17:09:24 +01:00
|
|
|
federation_http_client=self.http_client2, config=config
|
2020-01-08 14:24:10 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# wire up outbound POST /key/v2/query requests from hs2 so that they
|
|
|
|
# will be forwarded to hs1
|
2022-03-03 17:05:44 +01:00
|
|
|
async def post_json(
|
|
|
|
destination: str, path: str, data: Optional[JsonDict] = None
|
|
|
|
) -> Union[JsonDict, list]:
|
2020-01-08 14:24:10 +01:00
|
|
|
self.assertEqual(destination, self.hs.hostname)
|
|
|
|
self.assertEqual(
|
|
|
|
path,
|
|
|
|
"/_matrix/key/v2/query",
|
|
|
|
)
|
|
|
|
|
|
|
|
channel = FakeChannel(self.site, self.reactor)
|
2022-03-03 17:05:44 +01:00
|
|
|
# channel is a `FakeChannel` but `HTTPChannel` is expected
|
|
|
|
req = SynapseRequest(channel, self.site) # type: ignore[arg-type]
|
2020-01-08 14:24:10 +01:00
|
|
|
req.content = BytesIO(encode_canonical_json(data))
|
|
|
|
|
|
|
|
req.requestReceived(
|
|
|
|
b"POST",
|
|
|
|
path.encode("utf-8"),
|
|
|
|
b"1.1",
|
|
|
|
)
|
2020-11-16 19:21:47 +01:00
|
|
|
channel.await_result()
|
2020-01-08 14:24:10 +01:00
|
|
|
self.assertEqual(channel.code, 200)
|
|
|
|
resp = channel.json_body
|
|
|
|
return resp
|
|
|
|
|
|
|
|
self.http_client2.post_json.side_effect = post_json
|
|
|
|
|
2022-03-03 17:05:44 +01:00
|
|
|
def test_get_key(self) -> None:
|
2020-01-08 14:24:10 +01:00
|
|
|
"""Fetch a key belonging to a random server"""
|
|
|
|
# make up a key to be fetched.
|
|
|
|
testkey = signedjson.key.generate_signing_key("abc")
|
|
|
|
|
|
|
|
# we expect hs1 to make a regular key request to the target server
|
|
|
|
self.expect_outgoing_key_request("targetserver", testkey)
|
|
|
|
keyid = "ed25519:%s" % (testkey.version,)
|
|
|
|
|
|
|
|
fetcher = PerspectivesKeyFetcher(self.hs2)
|
2021-06-02 17:37:59 +02:00
|
|
|
d = fetcher.get_keys("targetserver", [keyid], 1000)
|
2020-01-08 14:24:10 +01:00
|
|
|
res = self.get_success(d)
|
2021-06-02 17:37:59 +02:00
|
|
|
self.assertIn(keyid, res)
|
|
|
|
keyres = res[keyid]
|
2020-01-08 14:24:10 +01:00
|
|
|
assert isinstance(keyres, FetchKeyResult)
|
|
|
|
self.assertEqual(
|
|
|
|
signedjson.key.encode_verify_key_base64(keyres.verify_key),
|
|
|
|
signedjson.key.encode_verify_key_base64(testkey.verify_key),
|
|
|
|
)
|
|
|
|
|
2022-03-03 17:05:44 +01:00
|
|
|
def test_get_notary_key(self) -> None:
|
2020-01-08 14:24:10 +01:00
|
|
|
"""Fetch a key belonging to the notary server"""
|
|
|
|
# make up a key to be fetched. We randomise the keyid to try to get it to
|
|
|
|
# appear before the key server signing key sometimes (otherwise we bail out
|
|
|
|
# before fetching its signature)
|
|
|
|
testkey = signedjson.key.generate_signing_key(random_string(5))
|
|
|
|
|
|
|
|
# we expect hs1 to make a regular key request to itself
|
|
|
|
self.expect_outgoing_key_request(self.hs.hostname, testkey)
|
|
|
|
keyid = "ed25519:%s" % (testkey.version,)
|
|
|
|
|
|
|
|
fetcher = PerspectivesKeyFetcher(self.hs2)
|
2021-06-02 17:37:59 +02:00
|
|
|
d = fetcher.get_keys(self.hs.hostname, [keyid], 1000)
|
2020-01-08 14:24:10 +01:00
|
|
|
res = self.get_success(d)
|
2021-06-02 17:37:59 +02:00
|
|
|
self.assertIn(keyid, res)
|
|
|
|
keyres = res[keyid]
|
2020-01-08 14:24:10 +01:00
|
|
|
assert isinstance(keyres, FetchKeyResult)
|
|
|
|
self.assertEqual(
|
|
|
|
signedjson.key.encode_verify_key_base64(keyres.verify_key),
|
|
|
|
signedjson.key.encode_verify_key_base64(testkey.verify_key),
|
|
|
|
)
|
|
|
|
|
2022-03-03 17:05:44 +01:00
|
|
|
def test_get_notary_keyserver_key(self) -> None:
|
2020-01-08 14:24:10 +01:00
|
|
|
"""Fetch the notary's keyserver key"""
|
|
|
|
# we expect hs1 to make a regular key request to itself
|
|
|
|
self.expect_outgoing_key_request(self.hs.hostname, self.hs_signing_key)
|
|
|
|
keyid = "ed25519:%s" % (self.hs_signing_key.version,)
|
|
|
|
|
|
|
|
fetcher = PerspectivesKeyFetcher(self.hs2)
|
2021-06-02 17:37:59 +02:00
|
|
|
d = fetcher.get_keys(self.hs.hostname, [keyid], 1000)
|
2020-01-08 14:24:10 +01:00
|
|
|
res = self.get_success(d)
|
2021-06-02 17:37:59 +02:00
|
|
|
self.assertIn(keyid, res)
|
|
|
|
keyres = res[keyid]
|
2020-01-08 14:24:10 +01:00
|
|
|
assert isinstance(keyres, FetchKeyResult)
|
|
|
|
self.assertEqual(
|
|
|
|
signedjson.key.encode_verify_key_base64(keyres.verify_key),
|
|
|
|
signedjson.key.encode_verify_key_base64(self.hs_signing_key.verify_key),
|
|
|
|
)
|