local_key_resource.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket 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. import logging
  16. from canonicaljson import encode_canonical_json
  17. from signedjson.sign import sign_json
  18. from unpaddedbase64 import encode_base64
  19. from twisted.web.resource import Resource
  20. from synapse.http.server import respond_with_json_bytes
  21. logger = logging.getLogger(__name__)
  22. class LocalKey(Resource):
  23. """HTTP resource containing encoding the TLS X.509 certificate and NACL
  24. signature verification keys for this server::
  25. GET /_matrix/key/v2/server/a.key.id HTTP/1.1
  26. HTTP/1.1 200 OK
  27. Content-Type: application/json
  28. {
  29. "valid_until_ts": # integer posix timestamp when this result expires.
  30. "server_name": "this.server.example.com"
  31. "verify_keys": {
  32. "algorithm:version": {
  33. "key": # base64 encoded NACL verification key.
  34. }
  35. },
  36. "old_verify_keys": {
  37. "algorithm:version": {
  38. "expired_ts": # integer posix timestamp when the key expired.
  39. "key": # base64 encoded NACL verification key.
  40. }
  41. },
  42. "tls_fingerprints": [ # Fingerprints of the TLS certs this server uses.
  43. {
  44. "sha256": # base64 encoded sha256 fingerprint of the X509 cert
  45. },
  46. ],
  47. "signatures": {
  48. "this.server.example.com": {
  49. "algorithm:version": # NACL signature for this server
  50. }
  51. }
  52. }
  53. """
  54. isLeaf = True
  55. def __init__(self, hs):
  56. self.config = hs.config
  57. self.clock = hs.get_clock()
  58. self.update_response_body(self.clock.time_msec())
  59. Resource.__init__(self)
  60. def update_response_body(self, time_now_msec):
  61. refresh_interval = self.config.key_refresh_interval
  62. self.valid_until_ts = int(time_now_msec + refresh_interval)
  63. self.response_body = encode_canonical_json(self.response_json_object())
  64. def response_json_object(self):
  65. verify_keys = {}
  66. for key in self.config.signing_key:
  67. verify_key_bytes = key.verify_key.encode()
  68. key_id = "%s:%s" % (key.alg, key.version)
  69. verify_keys[key_id] = {"key": encode_base64(verify_key_bytes)}
  70. old_verify_keys = {}
  71. for key_id, key in self.config.old_signing_keys.items():
  72. verify_key_bytes = key.encode()
  73. old_verify_keys[key_id] = {
  74. "key": encode_base64(verify_key_bytes),
  75. "expired_ts": key.expired_ts,
  76. }
  77. tls_fingerprints = self.config.tls_fingerprints
  78. json_object = {
  79. "valid_until_ts": self.valid_until_ts,
  80. "server_name": self.config.server_name,
  81. "verify_keys": verify_keys,
  82. "old_verify_keys": old_verify_keys,
  83. "tls_fingerprints": tls_fingerprints,
  84. }
  85. for key in self.config.signing_key:
  86. json_object = sign_json(json_object, self.config.server_name, key)
  87. return json_object
  88. def render_GET(self, request):
  89. time_now = self.clock.time_msec()
  90. # Update the expiry time if less than half the interval remains.
  91. if time_now + self.config.key_refresh_interval / 2 > self.valid_until_ts:
  92. self.update_response_body(time_now)
  93. return respond_with_json_bytes(request, 200, self.response_body)