key.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 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. from ._base import Config, ConfigError
  16. from synapse.util.stringutils import random_string
  17. from signedjson.key import (
  18. generate_signing_key, is_signing_algorithm_supported,
  19. decode_signing_key_base64, decode_verify_key_bytes,
  20. read_signing_keys, write_signing_keys, NACL_ED25519
  21. )
  22. from unpaddedbase64 import decode_base64
  23. from synapse.util.stringutils import random_string_with_symbols
  24. import os
  25. import hashlib
  26. import logging
  27. logger = logging.getLogger(__name__)
  28. class KeyConfig(Config):
  29. def read_config(self, config):
  30. self.signing_key = self.read_signing_key(config["signing_key_path"])
  31. self.old_signing_keys = self.read_old_signing_keys(
  32. config["old_signing_keys"]
  33. )
  34. self.key_refresh_interval = self.parse_duration(
  35. config["key_refresh_interval"]
  36. )
  37. self.perspectives = self.read_perspectives(
  38. config["perspectives"]
  39. )
  40. self.macaroon_secret_key = config.get(
  41. "macaroon_secret_key", self.registration_shared_secret
  42. )
  43. if not self.macaroon_secret_key:
  44. # Unfortunately, there are people out there that don't have this
  45. # set. Lets just be "nice" and derive one from their secret key.
  46. logger.warn("Config is missing missing macaroon_secret_key")
  47. seed = self.signing_key[0].seed
  48. self.macaroon_secret_key = hashlib.sha256(seed)
  49. def default_config(self, config_dir_path, server_name, is_generating_file=False,
  50. **kwargs):
  51. base_key_name = os.path.join(config_dir_path, server_name)
  52. if is_generating_file:
  53. macaroon_secret_key = random_string_with_symbols(50)
  54. else:
  55. macaroon_secret_key = None
  56. return """\
  57. macaroon_secret_key: "%(macaroon_secret_key)s"
  58. ## Signing Keys ##
  59. # Path to the signing key to sign messages with
  60. signing_key_path: "%(base_key_name)s.signing.key"
  61. # The keys that the server used to sign messages with but won't use
  62. # to sign new messages. E.g. it has lost its private key
  63. old_signing_keys: {}
  64. # "ed25519:auto":
  65. # # Base64 encoded public key
  66. # key: "The public part of your old signing key."
  67. # # Millisecond POSIX timestamp when the key expired.
  68. # expired_ts: 123456789123
  69. # How long key response published by this server is valid for.
  70. # Used to set the valid_until_ts in /key/v2 APIs.
  71. # Determines how quickly servers will query to check which keys
  72. # are still valid.
  73. key_refresh_interval: "1d" # 1 Day.
  74. # The trusted servers to download signing keys from.
  75. perspectives:
  76. servers:
  77. "matrix.org":
  78. verify_keys:
  79. "ed25519:auto":
  80. key: "Noi6WqcDj0QmPxCNQqgezwTlBKrfqehY1u2FyWP9uYw"
  81. """ % locals()
  82. def read_perspectives(self, perspectives_config):
  83. servers = {}
  84. for server_name, server_config in perspectives_config["servers"].items():
  85. for key_id, key_data in server_config["verify_keys"].items():
  86. if is_signing_algorithm_supported(key_id):
  87. key_base64 = key_data["key"]
  88. key_bytes = decode_base64(key_base64)
  89. verify_key = decode_verify_key_bytes(key_id, key_bytes)
  90. servers.setdefault(server_name, {})[key_id] = verify_key
  91. return servers
  92. def read_signing_key(self, signing_key_path):
  93. signing_keys = self.read_file(signing_key_path, "signing_key")
  94. try:
  95. return read_signing_keys(signing_keys.splitlines(True))
  96. except Exception:
  97. raise ConfigError(
  98. "Error reading signing_key."
  99. " Try running again with --generate-config"
  100. )
  101. def read_old_signing_keys(self, old_signing_keys):
  102. keys = {}
  103. for key_id, key_data in old_signing_keys.items():
  104. if is_signing_algorithm_supported(key_id):
  105. key_base64 = key_data["key"]
  106. key_bytes = decode_base64(key_base64)
  107. verify_key = decode_verify_key_bytes(key_id, key_bytes)
  108. verify_key.expired_ts = key_data["expired_ts"]
  109. keys[key_id] = verify_key
  110. else:
  111. raise ConfigError(
  112. "Unsupported signing algorithm for old key: %r" % (key_id,)
  113. )
  114. return keys
  115. def generate_files(self, config):
  116. signing_key_path = config["signing_key_path"]
  117. if not os.path.exists(signing_key_path):
  118. with open(signing_key_path, "w") as signing_key_file:
  119. key_id = "a_" + random_string(4)
  120. write_signing_keys(
  121. signing_key_file, (generate_signing_key(key_id),),
  122. )
  123. else:
  124. signing_keys = self.read_file(signing_key_path, "signing_key")
  125. if len(signing_keys.split("\n")[0].split()) == 1:
  126. # handle keys in the old format.
  127. key_id = "a_" + random_string(4)
  128. key = decode_signing_key_base64(
  129. NACL_ED25519, key_id, signing_keys.split("\n")[0]
  130. )
  131. with open(signing_key_path, "w") as signing_key_file:
  132. write_signing_keys(
  133. signing_key_file, (key,),
  134. )