2014-08-12 16:10:52 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-01-06 14:21:39 +01:00
|
|
|
# Copyright 2014, 2015 OpenMarket Ltd
|
2014-08-12 16:10:52 +02: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.
|
|
|
|
|
2014-08-13 04:14:34 +02:00
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
from twisted.web.http import HTTPClient
|
2014-09-30 16:15:10 +02:00
|
|
|
from twisted.internet.protocol import Factory
|
2014-08-12 16:10:52 +02:00
|
|
|
from twisted.internet import defer, reactor
|
2014-11-20 18:41:56 +01:00
|
|
|
from synapse.http.endpoint import matrix_federation_endpoint
|
2014-10-30 02:21:33 +01:00
|
|
|
from synapse.util.logcontext import PreserveLoggingContext
|
2014-08-12 16:10:52 +02:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def fetch_server_key(server_name, ssl_context_factory):
|
|
|
|
"""Fetch the keys for a remote server."""
|
|
|
|
|
|
|
|
factory = SynapseKeyClientFactory()
|
2014-11-20 18:41:56 +01:00
|
|
|
endpoint = matrix_federation_endpoint(
|
2014-09-30 16:15:10 +02:00
|
|
|
reactor, server_name, ssl_context_factory, timeout=30
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2014-09-30 16:15:10 +02:00
|
|
|
for i in range(5):
|
|
|
|
try:
|
2014-10-30 02:21:33 +01:00
|
|
|
with PreserveLoggingContext():
|
|
|
|
protocol = yield endpoint.connect(factory)
|
|
|
|
server_response, server_certificate = yield protocol.remote_key
|
|
|
|
defer.returnValue((server_response, server_certificate))
|
|
|
|
return
|
2014-09-30 16:15:10 +02:00
|
|
|
except Exception as e:
|
|
|
|
logger.exception(e)
|
2014-10-13 17:39:15 +02:00
|
|
|
raise IOError("Cannot get key for %s" % server_name)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
class SynapseKeyClientError(Exception):
|
2014-11-20 18:41:56 +01:00
|
|
|
"""The key wasn't retrieved from the remote server."""
|
2014-08-12 16:10:52 +02: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 16:15:10 +02:00
|
|
|
timeout = 30
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.remote_key = defer.Deferred()
|
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
def connectionMade(self):
|
|
|
|
logger.debug("Connected to %s", self.transport.getHost())
|
2014-09-30 16:15:10 +02:00
|
|
|
self.sendCommand(b"GET", b"/_matrix/key/v1/")
|
2014-08-12 16:10:52 +02:00
|
|
|
self.endHeaders()
|
|
|
|
self.timer = reactor.callLater(
|
2014-09-30 16:15:10 +02:00
|
|
|
self.timeout,
|
2014-08-12 16:10:52 +02:00
|
|
|
self.on_timeout
|
|
|
|
)
|
|
|
|
|
|
|
|
def handleStatus(self, version, status, message):
|
|
|
|
if status != b"200":
|
2014-09-30 16:15:10 +02:00
|
|
|
#logger.info("Non-200 response from %s: %s %s",
|
|
|
|
# self.transport.getHost(), status, message)
|
2014-08-12 16:10:52 +02:00
|
|
|
self.transport.abortConnection()
|
|
|
|
|
|
|
|
def handleResponse(self, response_body_bytes):
|
|
|
|
try:
|
|
|
|
json_response = json.loads(response_body_bytes)
|
|
|
|
except ValueError:
|
2014-09-30 16:15:10 +02:00
|
|
|
#logger.info("Invalid JSON response from %s",
|
|
|
|
# self.transport.getHost())
|
2014-08-12 16:10:52 +02:00
|
|
|
self.transport.abortConnection()
|
|
|
|
return
|
|
|
|
|
|
|
|
certificate = self.transport.getPeerCertificate()
|
2014-09-30 16:15:10 +02:00
|
|
|
self.remote_key.callback((json_response, certificate))
|
2014-08-12 16:10:52 +02:00
|
|
|
self.transport.abortConnection()
|
|
|
|
self.timer.cancel()
|
|
|
|
|
|
|
|
def on_timeout(self):
|
|
|
|
logger.debug("Timeout waiting for response from %s",
|
|
|
|
self.transport.getHost())
|
2014-10-13 17:39:15 +02:00
|
|
|
self.remote_key.errback(IOError("Timeout waiting for response"))
|
2014-08-12 16:10:52 +02:00
|
|
|
self.transport.abortConnection()
|
|
|
|
|
|
|
|
|
2014-09-30 16:15:10 +02:00
|
|
|
class SynapseKeyClientFactory(Factory):
|
2014-08-12 16:10:52 +02:00
|
|
|
protocol = SynapseKeyClientProtocol
|