2014-08-12 08:10:52 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 21:26:29 -07:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2014-08-12 08:10:52 -06:00
|
|
|
#
|
|
|
|
# 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 logging
|
|
|
|
|
2018-10-31 06:14:39 -06:00
|
|
|
from six.moves import urllib
|
|
|
|
|
2018-07-09 00:09:20 -06:00
|
|
|
from canonicaljson import json
|
|
|
|
|
|
|
|
from twisted.internet import defer, reactor
|
2018-08-21 04:09:46 -06:00
|
|
|
from twisted.internet.error import ConnectError
|
2018-07-09 00:09:20 -06:00
|
|
|
from twisted.internet.protocol import Factory
|
2018-08-21 04:09:46 -06:00
|
|
|
from twisted.names.error import DomainError
|
2018-07-09 00:09:20 -06:00
|
|
|
from twisted.web.http import HTTPClient
|
|
|
|
|
|
|
|
from synapse.http.endpoint import matrix_federation_endpoint
|
|
|
|
from synapse.util import logcontext
|
2014-08-12 08:10:52 -06:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2018-10-31 06:14:39 -06:00
|
|
|
KEY_API_V2 = "/_matrix/key/v2/server/%s"
|
2015-04-22 07:21:08 -06:00
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2018-10-31 06:14:39 -06:00
|
|
|
def fetch_server_key(server_name, tls_client_options_factory, key_id):
|
2014-08-12 08:10:52 -06:00
|
|
|
"""Fetch the keys for a remote server."""
|
|
|
|
|
|
|
|
factory = SynapseKeyClientFactory()
|
2018-10-31 06:14:39 -06:00
|
|
|
factory.path = KEY_API_V2 % (urllib.parse.quote(key_id), )
|
2016-03-11 03:29:05 -07:00
|
|
|
factory.host = server_name
|
2014-11-20 10:41:56 -07:00
|
|
|
endpoint = matrix_federation_endpoint(
|
2018-06-24 14:38:43 -06:00
|
|
|
reactor, server_name, tls_client_options_factory, timeout=30
|
2014-09-30 08:15:10 -06:00
|
|
|
)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2014-09-30 08:15:10 -06:00
|
|
|
for i in range(5):
|
|
|
|
try:
|
2017-09-25 02:51:39 -06:00
|
|
|
with logcontext.PreserveLoggingContext():
|
|
|
|
protocol = yield endpoint.connect(factory)
|
|
|
|
server_response, server_certificate = yield protocol.remote_key
|
|
|
|
defer.returnValue((server_response, server_certificate))
|
2015-04-15 09:57:58 -06:00
|
|
|
except SynapseKeyClientError as e:
|
2018-08-21 04:09:46 -06:00
|
|
|
logger.warn("Error getting key for %r: %s", server_name, e)
|
2018-09-12 04:16:31 -06:00
|
|
|
if e.status.startswith(b"4"):
|
2015-04-15 09:57:58 -06:00
|
|
|
# Don't retry for 4xx responses.
|
|
|
|
raise IOError("Cannot get key for %r" % server_name)
|
2018-08-21 04:09:46 -06:00
|
|
|
except (ConnectError, DomainError) as e:
|
|
|
|
logger.warn("Error getting key for %r: %s", server_name, e)
|
2018-10-24 03:35:01 -06:00
|
|
|
except Exception:
|
2018-08-21 04:09:46 -06:00
|
|
|
logger.exception("Error getting key for %r", server_name)
|
2015-04-15 09:57:58 -06:00
|
|
|
raise IOError("Cannot get key for %r" % server_name)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
|
|
|
|
|
|
|
class SynapseKeyClientError(Exception):
|
2014-11-20 10:41:56 -07:00
|
|
|
"""The key wasn't retrieved from the remote server."""
|
2015-04-15 09:57:58 -06:00
|
|
|
status = None
|
2014-08-12 08:10:52 -06:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class SynapseKeyClientProtocol(HTTPClient):
|
|
|
|
"""Low level HTTPS client which retrieves an application/json response from
|
|
|
|
the server and extracts the X.509 certificate for the remote peer from the
|
|
|
|
SSL connection."""
|
|
|
|
|
2014-09-30 08:15:10 -06:00
|
|
|
timeout = 30
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.remote_key = defer.Deferred()
|
2015-01-30 04:07:57 -07:00
|
|
|
self.host = None
|
2016-07-21 10:38:51 -06:00
|
|
|
self._peer = None
|
2014-09-30 08:15:10 -06:00
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
def connectionMade(self):
|
2016-07-21 10:38:51 -06:00
|
|
|
self._peer = self.transport.getPeer()
|
|
|
|
logger.debug("Connected to %s", self._peer)
|
|
|
|
|
2018-09-12 04:16:31 -06:00
|
|
|
if not isinstance(self.path, bytes):
|
|
|
|
self.path = self.path.encode('ascii')
|
|
|
|
|
|
|
|
if not isinstance(self.host, bytes):
|
|
|
|
self.host = self.host.encode('ascii')
|
|
|
|
|
2015-04-15 09:57:58 -06:00
|
|
|
self.sendCommand(b"GET", self.path)
|
2016-03-11 03:29:05 -07:00
|
|
|
if self.host:
|
|
|
|
self.sendHeader(b"Host", self.host)
|
2014-08-12 08:10:52 -06:00
|
|
|
self.endHeaders()
|
|
|
|
self.timer = reactor.callLater(
|
2014-09-30 08:15:10 -06:00
|
|
|
self.timeout,
|
2014-08-12 08:10:52 -06:00
|
|
|
self.on_timeout
|
|
|
|
)
|
|
|
|
|
2015-04-15 09:57:58 -06:00
|
|
|
def errback(self, error):
|
|
|
|
if not self.remote_key.called:
|
|
|
|
self.remote_key.errback(error)
|
|
|
|
|
|
|
|
def callback(self, result):
|
|
|
|
if not self.remote_key.called:
|
|
|
|
self.remote_key.callback(result)
|
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
def handleStatus(self, version, status, message):
|
|
|
|
if status != b"200":
|
2015-02-10 10:58:36 -07:00
|
|
|
# logger.info("Non-200 response from %s: %s %s",
|
2014-09-30 08:15:10 -06:00
|
|
|
# self.transport.getHost(), status, message)
|
2015-04-22 07:21:08 -06:00
|
|
|
error = SynapseKeyClientError(
|
|
|
|
"Non-200 response %r from %r" % (status, self.host)
|
2015-04-15 09:57:58 -06:00
|
|
|
)
|
|
|
|
error.status = status
|
|
|
|
self.errback(error)
|
2014-08-12 08:10:52 -06:00
|
|
|
self.transport.abortConnection()
|
|
|
|
|
|
|
|
def handleResponse(self, response_body_bytes):
|
|
|
|
try:
|
|
|
|
json_response = json.loads(response_body_bytes)
|
|
|
|
except ValueError:
|
2015-02-10 10:58:36 -07:00
|
|
|
# logger.info("Invalid JSON response from %s",
|
2014-09-30 08:15:10 -06:00
|
|
|
# self.transport.getHost())
|
2014-08-12 08:10:52 -06:00
|
|
|
self.transport.abortConnection()
|
|
|
|
return
|
|
|
|
|
|
|
|
certificate = self.transport.getPeerCertificate()
|
2015-04-15 09:57:58 -06:00
|
|
|
self.callback((json_response, certificate))
|
2014-08-12 08:10:52 -06:00
|
|
|
self.transport.abortConnection()
|
|
|
|
self.timer.cancel()
|
|
|
|
|
|
|
|
def on_timeout(self):
|
2016-07-21 04:14:54 -06:00
|
|
|
logger.debug(
|
|
|
|
"Timeout waiting for response from %s: %s",
|
2016-07-21 10:38:51 -06:00
|
|
|
self.host, self._peer,
|
2016-07-21 04:14:54 -06:00
|
|
|
)
|
2015-04-15 09:57:58 -06:00
|
|
|
self.errback(IOError("Timeout waiting for response"))
|
2014-08-12 08:10:52 -06:00
|
|
|
self.transport.abortConnection()
|
|
|
|
|
|
|
|
|
2014-09-30 08:15:10 -06:00
|
|
|
class SynapseKeyClientFactory(Factory):
|
2015-04-15 09:57:58 -06:00
|
|
|
def protocol(self):
|
|
|
|
protocol = SynapseKeyClientProtocol()
|
|
|
|
protocol.path = self.path
|
2016-07-21 04:45:53 -06:00
|
|
|
protocol.host = self.host
|
2015-04-15 09:57:58 -06:00
|
|
|
return protocol
|