verifier.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector 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. from __future__ import absolute_import
  16. import logging
  17. import time
  18. from twisted.internet import defer
  19. from unpaddedbase64 import decode_base64
  20. import signedjson.sign
  21. import signedjson.key
  22. from signedjson.sign import SignatureVerifyException
  23. from sydent.http.httpclient import FederationHttpClient
  24. from sydent.util.stringutils import is_valid_matrix_server_name
  25. logger = logging.getLogger(__name__)
  26. class NoAuthenticationError(Exception):
  27. """
  28. Raised when no signature is provided that could be authenticated
  29. """
  30. pass
  31. class InvalidServerName(Exception):
  32. """
  33. Raised when the provided origin parameter is not a valid hostname (plus optional port).
  34. """
  35. pass
  36. class Verifier(object):
  37. """
  38. Verifies signed json blobs from Matrix Homeservers by finding the
  39. homeserver's address, contacting it, requesting its keys and
  40. verifying that the signature on the json blob matches.
  41. """
  42. def __init__(self, sydent):
  43. self.sydent = sydent
  44. # Cache of server keys. These are cached until the 'valid_until_ts' time
  45. # in the result.
  46. self.cache = {
  47. # server_name: <result from keys query>,
  48. }
  49. @defer.inlineCallbacks
  50. def _getKeysForServer(self, server_name):
  51. """Get the signing key data from a homeserver.
  52. :param server_name: The name of the server to request the keys from.
  53. :type server_name: unicode
  54. :return: The verification keys returned by the server.
  55. :rtype: twisted.internet.defer.Deferred[dict[unicode, dict[unicode, unicode]]]
  56. """
  57. if server_name in self.cache:
  58. cached = self.cache[server_name]
  59. now = int(time.time() * 1000)
  60. if cached['valid_until_ts'] > now:
  61. defer.returnValue(self.cache[server_name]['verify_keys'])
  62. client = FederationHttpClient(self.sydent)
  63. result = yield client.get_json("matrix://%s/_matrix/key/v2/server/" % server_name, 1024 * 50)
  64. if 'verify_keys' not in result:
  65. raise SignatureVerifyException("No key found in response")
  66. if 'valid_until_ts' in result:
  67. if not isinstance(result['valid_until_ts'], int):
  68. raise SignatureVerifyException("Invalid valid_until_ts received, must be an integer")
  69. # Don't cache anything without a valid_until_ts or we wouldn't
  70. # know when to expire it.
  71. logger.info("Got keys for %s: caching until %d", server_name, result['valid_until_ts'])
  72. self.cache[server_name] = result
  73. defer.returnValue(result['verify_keys'])
  74. @defer.inlineCallbacks
  75. def verifyServerSignedJson(self, signed_json, acceptable_server_names=None):
  76. """Given a signed json object, try to verify any one
  77. of the signatures on it
  78. XXX: This contains a fairly noddy version of the home server
  79. SRV lookup and signature verification. It does no caching (just
  80. fetches the signature each time and does not contact any other
  81. servers to do perspective checks).
  82. :param acceptable_server_names: If provided and not None,
  83. only signatures from servers in this list will be accepted.
  84. :type acceptable_server_names: list[unicode] or None
  85. :return a tuple of the server name and key name that was
  86. successfully verified.
  87. :rtype: twisted.internet.defer.Deferred[tuple[unicode]]
  88. :raise SignatureVerifyException: The json cannot be verified.
  89. """
  90. if 'signatures' not in signed_json:
  91. raise SignatureVerifyException("Signature missing")
  92. for server_name, sigs in signed_json['signatures'].items():
  93. if acceptable_server_names is not None:
  94. if server_name not in acceptable_server_names:
  95. continue
  96. server_keys = yield self._getKeysForServer(server_name)
  97. for key_name, sig in sigs.items():
  98. if key_name in server_keys:
  99. if 'key' not in server_keys[key_name]:
  100. logger.warn("Ignoring key %s with no 'key'")
  101. continue
  102. key_bytes = decode_base64(server_keys[key_name]['key'])
  103. verify_key = signedjson.key.decode_verify_key_bytes(key_name, key_bytes)
  104. logger.info("verifying sig from key %r", key_name)
  105. signedjson.sign.verify_signed_json(signed_json, server_name, verify_key)
  106. logger.info("Verified signature with key %s from %s", key_name, server_name)
  107. defer.returnValue((server_name, key_name))
  108. logger.warn(
  109. "No matching key found for signature block %r in server keys %r",
  110. signed_json['signatures'], server_keys,
  111. )
  112. logger.warn(
  113. "Unable to verify any signatures from block %r. Acceptable server names: %r",
  114. signed_json['signatures'], acceptable_server_names,
  115. )
  116. raise SignatureVerifyException("No matching signature found")
  117. @defer.inlineCallbacks
  118. def authenticate_request(self, request, content):
  119. """Authenticates a Matrix federation request based on the X-Matrix header
  120. XXX: Copied largely from synapse
  121. :param request: The request object to authenticate
  122. :type request: twisted.web.server.Request
  123. :param content: The content of the request, if any
  124. :type content: bytes or None
  125. :return: The origin of the server whose signature was validated
  126. :rtype: twisted.internet.defer.Deferred[unicode]
  127. """
  128. json_request = {
  129. "method": request.method,
  130. "uri": request.uri,
  131. "destination_is": self.sydent.server_name,
  132. "signatures": {},
  133. }
  134. if content is not None:
  135. json_request["content"] = content
  136. origin = None
  137. def parse_auth_header(header_str):
  138. """
  139. Extracts a server name, signing key and payload signature from an
  140. authentication header.
  141. :param header_str: The content of the header
  142. :type header_str: unicode
  143. :return: The server name, the signing key, and the payload signature.
  144. :rtype: tuple[unicode]
  145. """
  146. try:
  147. params = header_str.split(u" ")[1].split(u",")
  148. param_dict = dict(kv.split(u"=") for kv in params)
  149. def strip_quotes(value):
  150. if value.startswith(u"\""):
  151. return value[1:-1]
  152. else:
  153. return value
  154. origin = strip_quotes(param_dict["origin"])
  155. key = strip_quotes(param_dict["key"])
  156. sig = strip_quotes(param_dict["sig"])
  157. return origin, key, sig
  158. except Exception:
  159. raise SignatureVerifyException("Malformed Authorization header")
  160. auth_headers = request.requestHeaders.getRawHeaders(u"Authorization")
  161. if not auth_headers:
  162. raise NoAuthenticationError("Missing Authorization headers")
  163. for auth in auth_headers:
  164. if auth.startswith(u"X-Matrix"):
  165. (origin, key, sig) = parse_auth_header(auth)
  166. json_request["origin"] = origin
  167. json_request["signatures"].setdefault(origin, {})[key] = sig
  168. if not json_request["signatures"]:
  169. raise NoAuthenticationError("Missing X-Matrix Authorization header")
  170. if not is_valid_matrix_server_name(json_request["origin"]):
  171. raise InvalidServerName("X-Matrix header's origin parameter must be a valid Matrix server name")
  172. yield self.verifyServerSignedJson(json_request, [origin])
  173. logger.info("Verified request from HS %s", origin)
  174. defer.returnValue(origin)