local_key_resource.py 4.1 KB

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