server_key_resource.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 /key HTTP/1.1
  27. HTTP/1.1 200 OK
  28. Content-Type: application/json
  29. {
  30. "server_name": "this.server.example.com"
  31. "verify_keys": {
  32. "algorithm:version": # base64 encoded NACL verification key.
  33. },
  34. "tls_certificate": # base64 ASN.1 DER encoded X.509 tls cert.
  35. "signatures": {
  36. "this.server.example.com": {
  37. "algorithm:version": # NACL signature for this server.
  38. }
  39. }
  40. }
  41. """
  42. def __init__(self, hs):
  43. self.hs = hs
  44. self.version_string = hs.version_string
  45. self.response_body = encode_canonical_json(
  46. self.response_json_object(hs.config)
  47. )
  48. Resource.__init__(self)
  49. @staticmethod
  50. def response_json_object(server_config):
  51. verify_keys = {}
  52. for key in server_config.signing_key:
  53. verify_key_bytes = key.verify_key.encode()
  54. key_id = "%s:%s" % (key.alg, key.version)
  55. verify_keys[key_id] = encode_base64(verify_key_bytes)
  56. x509_certificate_bytes = crypto.dump_certificate(
  57. crypto.FILETYPE_ASN1,
  58. server_config.tls_certificate
  59. )
  60. json_object = {
  61. u"server_name": server_config.server_name,
  62. u"verify_keys": verify_keys,
  63. u"tls_certificate": encode_base64(x509_certificate_bytes)
  64. }
  65. for key in server_config.signing_key:
  66. json_object = sign_json(
  67. json_object,
  68. server_config.server_name,
  69. key,
  70. )
  71. return json_object
  72. def render_GET(self, request):
  73. return respond_with_json_bytes(
  74. request, 200, self.response_body,
  75. version_string=self.version_string
  76. )
  77. def getChild(self, name, request):
  78. if name == '':
  79. return self