tls.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. from ._base import Config
  16. from OpenSSL import crypto
  17. import subprocess
  18. import os
  19. GENERATE_DH_PARAMS = False
  20. class TlsConfig(Config):
  21. def read_config(self, config):
  22. self.tls_certificate = self.read_tls_certificate(
  23. config.get("tls_certificate_path")
  24. )
  25. self.tls_certificate_file = config.get("tls_certificate_path")
  26. self.no_tls = config.get("no_tls", False)
  27. if self.no_tls:
  28. self.tls_private_key = None
  29. else:
  30. self.tls_private_key = self.read_tls_private_key(
  31. config.get("tls_private_key_path")
  32. )
  33. self.tls_dh_params_path = self.check_file(
  34. config.get("tls_dh_params_path"), "tls_dh_params"
  35. )
  36. # This config option applies to non-federation HTTP clients
  37. # (e.g. for talking to recaptcha, identity servers, and such)
  38. # It should never be used in production, and is intended for
  39. # use only when running tests.
  40. self.use_insecure_ssl_client_just_for_testing_do_not_use = config.get(
  41. "use_insecure_ssl_client_just_for_testing_do_not_use"
  42. )
  43. def default_config(self, config_dir_path, server_name, **kwargs):
  44. base_key_name = os.path.join(config_dir_path, server_name)
  45. tls_certificate_path = base_key_name + ".tls.crt"
  46. tls_private_key_path = base_key_name + ".tls.key"
  47. tls_dh_params_path = base_key_name + ".tls.dh"
  48. return """\
  49. # PEM encoded X509 certificate for TLS.
  50. # You can replace the self-signed certificate that synapse
  51. # autogenerates on launch with your own SSL certificate + key pair
  52. # if you like. Any required intermediary certificates can be
  53. # appended after the primary certificate in hierarchical order.
  54. tls_certificate_path: "%(tls_certificate_path)s"
  55. # PEM encoded private key for TLS
  56. tls_private_key_path: "%(tls_private_key_path)s"
  57. # PEM dh parameters for ephemeral keys
  58. tls_dh_params_path: "%(tls_dh_params_path)s"
  59. # Don't bind to the https port
  60. no_tls: False
  61. """ % locals()
  62. def read_tls_certificate(self, cert_path):
  63. cert_pem = self.read_file(cert_path, "tls_certificate")
  64. return crypto.load_certificate(crypto.FILETYPE_PEM, cert_pem)
  65. def read_tls_private_key(self, private_key_path):
  66. private_key_pem = self.read_file(private_key_path, "tls_private_key")
  67. return crypto.load_privatekey(crypto.FILETYPE_PEM, private_key_pem)
  68. def generate_files(self, config):
  69. tls_certificate_path = config["tls_certificate_path"]
  70. tls_private_key_path = config["tls_private_key_path"]
  71. tls_dh_params_path = config["tls_dh_params_path"]
  72. if not os.path.exists(tls_private_key_path):
  73. with open(tls_private_key_path, "w") as private_key_file:
  74. tls_private_key = crypto.PKey()
  75. tls_private_key.generate_key(crypto.TYPE_RSA, 2048)
  76. private_key_pem = crypto.dump_privatekey(
  77. crypto.FILETYPE_PEM, tls_private_key
  78. )
  79. private_key_file.write(private_key_pem)
  80. else:
  81. with open(tls_private_key_path) as private_key_file:
  82. private_key_pem = private_key_file.read()
  83. tls_private_key = crypto.load_privatekey(
  84. crypto.FILETYPE_PEM, private_key_pem
  85. )
  86. if not os.path.exists(tls_certificate_path):
  87. with open(tls_certificate_path, "w") as certificate_file:
  88. cert = crypto.X509()
  89. subject = cert.get_subject()
  90. subject.CN = config["server_name"]
  91. cert.set_serial_number(1000)
  92. cert.gmtime_adj_notBefore(0)
  93. cert.gmtime_adj_notAfter(10 * 365 * 24 * 60 * 60)
  94. cert.set_issuer(cert.get_subject())
  95. cert.set_pubkey(tls_private_key)
  96. cert.sign(tls_private_key, 'sha256')
  97. cert_pem = crypto.dump_certificate(crypto.FILETYPE_PEM, cert)
  98. certificate_file.write(cert_pem)
  99. if not os.path.exists(tls_dh_params_path):
  100. if GENERATE_DH_PARAMS:
  101. subprocess.check_call([
  102. "openssl", "dhparam",
  103. "-outform", "PEM",
  104. "-out", tls_dh_params_path,
  105. "2048"
  106. ])
  107. else:
  108. with open(tls_dh_params_path, "w") as dh_params_file:
  109. dh_params_file.write(
  110. "2048-bit DH parameters taken from rfc3526\n"
  111. "-----BEGIN DH PARAMETERS-----\n"
  112. "MIIBCAKCAQEA///////////JD9qiIWjC"
  113. "NMTGYouA3BzRKQJOCIpnzHQCC76mOxOb\n"
  114. "IlFKCHmONATd75UZs806QxswKwpt8l8U"
  115. "N0/hNW1tUcJF5IW1dmJefsb0TELppjft\n"
  116. "awv/XLb0Brft7jhr+1qJn6WunyQRfEsf"
  117. "5kkoZlHs5Fs9wgB8uKFjvwWY2kg2HFXT\n"
  118. "mmkWP6j9JM9fg2VdI9yjrZYcYvNWIIVS"
  119. "u57VKQdwlpZtZww1Tkq8mATxdGwIyhgh\n"
  120. "fDKQXkYuNs474553LBgOhgObJ4Oi7Aei"
  121. "j7XFXfBvTFLJ3ivL9pVYFxg5lUl86pVq\n"
  122. "5RXSJhiY+gUQFXKOWoqsqmj/////////"
  123. "/wIBAg==\n"
  124. "-----END DH PARAMETERS-----\n"
  125. )