test_tls.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 New Vector Ltd
  3. # Copyright 2019 Matrix.org Foundation C.I.C.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import os
  17. import yaml
  18. from OpenSSL import SSL
  19. from synapse.config.tls import ConfigError, TlsConfig
  20. from synapse.crypto.context_factory import ClientTLSOptionsFactory
  21. from tests.unittest import TestCase
  22. class TestConfig(TlsConfig):
  23. def has_tls_listener(self):
  24. return False
  25. class TLSConfigTests(TestCase):
  26. def test_warn_self_signed(self):
  27. """
  28. Synapse will give a warning when it loads a self-signed certificate.
  29. """
  30. config_dir = self.mktemp()
  31. os.mkdir(config_dir)
  32. with open(os.path.join(config_dir, "cert.pem"), "w") as f:
  33. f.write(
  34. """-----BEGIN CERTIFICATE-----
  35. MIID6DCCAtACAws9CjANBgkqhkiG9w0BAQUFADCBtzELMAkGA1UEBhMCVFIxDzAN
  36. BgNVBAgMBsOHb3J1bTEUMBIGA1UEBwwLQmHFn21ha8OnxLExEjAQBgNVBAMMCWxv
  37. Y2FsaG9zdDEcMBoGA1UECgwTVHdpc3RlZCBNYXRyaXggTGFiczEkMCIGA1UECwwb
  38. QXV0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uDCutEVAD9kFmcJN2
  54. SznugAcPk4doX2+rL+ila+ThqgPzIkwTUHtnmjI0TI6xsDUlXz5S3UyudrE2Qsfz
  55. s4niecZKPBizL6aucT59CsunNmmb5Glq8rlAcU+1ZTZZzGYqVYhF6axB9Qg=
  56. -----END CERTIFICATE-----"""
  57. )
  58. config = {
  59. "tls_certificate_path": os.path.join(config_dir, "cert.pem"),
  60. "tls_fingerprints": [],
  61. }
  62. t = TestConfig()
  63. t.read_config(config, config_dir_path="", data_dir_path="")
  64. t.read_certificate_from_disk(require_cert_and_key=False)
  65. warnings = self.flushWarnings()
  66. self.assertEqual(len(warnings), 1)
  67. self.assertEqual(
  68. warnings[0]["message"],
  69. (
  70. "Self-signed TLS certificates will not be accepted by "
  71. "Synapse 1.0. Please either provide a valid certificate, "
  72. "or use Synapse's ACME support to provision one."
  73. ),
  74. )
  75. def test_tls_client_minimum_default(self):
  76. """
  77. The default client TLS version is 1.0.
  78. """
  79. config = {}
  80. t = TestConfig()
  81. t.read_config(config, config_dir_path="", data_dir_path="")
  82. self.assertEqual(t.federation_client_minimum_tls_version, "1")
  83. def test_tls_client_minimum_set(self):
  84. """
  85. The default client TLS version can be set to 1.0, 1.1, and 1.2.
  86. """
  87. config = {"federation_client_minimum_tls_version": 1}
  88. t = TestConfig()
  89. t.read_config(config, config_dir_path="", data_dir_path="")
  90. self.assertEqual(t.federation_client_minimum_tls_version, "1")
  91. config = {"federation_client_minimum_tls_version": 1.1}
  92. t = TestConfig()
  93. t.read_config(config, config_dir_path="", data_dir_path="")
  94. self.assertEqual(t.federation_client_minimum_tls_version, "1.1")
  95. config = {"federation_client_minimum_tls_version": 1.2}
  96. t = TestConfig()
  97. t.read_config(config, config_dir_path="", data_dir_path="")
  98. self.assertEqual(t.federation_client_minimum_tls_version, "1.2")
  99. # Also test a string version
  100. config = {"federation_client_minimum_tls_version": "1"}
  101. t = TestConfig()
  102. t.read_config(config, config_dir_path="", data_dir_path="")
  103. self.assertEqual(t.federation_client_minimum_tls_version, "1")
  104. config = {"federation_client_minimum_tls_version": "1.2"}
  105. t = TestConfig()
  106. t.read_config(config, config_dir_path="", data_dir_path="")
  107. self.assertEqual(t.federation_client_minimum_tls_version, "1.2")
  108. def test_tls_client_minimum_1_point_3_missing(self):
  109. """
  110. If TLS 1.3 support is missing and it's configured, it will raise a
  111. ConfigError.
  112. """
  113. # thanks i hate it
  114. if hasattr(SSL, "OP_NO_TLSv1_3"):
  115. OP_NO_TLSv1_3 = SSL.OP_NO_TLSv1_3
  116. delattr(SSL, "OP_NO_TLSv1_3")
  117. self.addCleanup(setattr, SSL, "SSL.OP_NO_TLSv1_3", OP_NO_TLSv1_3)
  118. assert not hasattr(SSL, "OP_NO_TLSv1_3")
  119. config = {"federation_client_minimum_tls_version": 1.3}
  120. t = TestConfig()
  121. with self.assertRaises(ConfigError) as e:
  122. t.read_config(config, config_dir_path="", data_dir_path="")
  123. self.assertEqual(
  124. e.exception.args[0],
  125. (
  126. "federation_client_minimum_tls_version cannot be 1.3, "
  127. "your OpenSSL does not support it"
  128. ),
  129. )
  130. def test_tls_client_minimum_1_point_3_exists(self):
  131. """
  132. If TLS 1.3 support exists and it's configured, it will be settable.
  133. """
  134. # thanks i hate it, still
  135. if not hasattr(SSL, "OP_NO_TLSv1_3"):
  136. SSL.OP_NO_TLSv1_3 = 0x00
  137. self.addCleanup(lambda: delattr(SSL, "OP_NO_TLSv1_3"))
  138. assert hasattr(SSL, "OP_NO_TLSv1_3")
  139. config = {"federation_client_minimum_tls_version": 1.3}
  140. t = TestConfig()
  141. t.read_config(config, config_dir_path="", data_dir_path="")
  142. self.assertEqual(t.federation_client_minimum_tls_version, "1.3")
  143. def test_tls_client_minimum_set_passed_through_1_2(self):
  144. """
  145. The configured TLS version is correctly configured by the ContextFactory.
  146. """
  147. config = {"federation_client_minimum_tls_version": 1.2}
  148. t = TestConfig()
  149. t.read_config(config, config_dir_path="", data_dir_path="")
  150. cf = ClientTLSOptionsFactory(t)
  151. # The context has had NO_TLSv1_1 and NO_TLSv1_0 set, but not NO_TLSv1_2
  152. self.assertNotEqual(cf._verify_ssl._options & SSL.OP_NO_TLSv1, 0)
  153. self.assertNotEqual(cf._verify_ssl._options & SSL.OP_NO_TLSv1_1, 0)
  154. self.assertEqual(cf._verify_ssl._options & SSL.OP_NO_TLSv1_2, 0)
  155. def test_tls_client_minimum_set_passed_through_1_0(self):
  156. """
  157. The configured TLS version is correctly configured by the ContextFactory.
  158. """
  159. config = {"federation_client_minimum_tls_version": 1}
  160. t = TestConfig()
  161. t.read_config(config, config_dir_path="", data_dir_path="")
  162. cf = ClientTLSOptionsFactory(t)
  163. # The context has not had any of the NO_TLS set.
  164. self.assertEqual(cf._verify_ssl._options & SSL.OP_NO_TLSv1, 0)
  165. self.assertEqual(cf._verify_ssl._options & SSL.OP_NO_TLSv1_1, 0)
  166. self.assertEqual(cf._verify_ssl._options & SSL.OP_NO_TLSv1_2, 0)
  167. def test_acme_disabled_in_generated_config_no_acme_domain_provied(self):
  168. """
  169. Checks acme is disabled by default.
  170. """
  171. conf = TestConfig()
  172. conf.read_config(
  173. yaml.safe_load(
  174. TestConfig().generate_config_section(
  175. "/config_dir_path",
  176. "my_super_secure_server",
  177. "/data_dir_path",
  178. "/tls_cert_path",
  179. "tls_private_key",
  180. None, # This is the acme_domain
  181. )
  182. ),
  183. "/config_dir_path",
  184. )
  185. self.assertFalse(conf.acme_enabled)
  186. def test_acme_enabled_in_generated_config_domain_provided(self):
  187. """
  188. Checks acme is enabled if the acme_domain arg is set to some string.
  189. """
  190. conf = TestConfig()
  191. conf.read_config(
  192. yaml.safe_load(
  193. TestConfig().generate_config_section(
  194. "/config_dir_path",
  195. "my_super_secure_server",
  196. "/data_dir_path",
  197. "/tls_cert_path",
  198. "tls_private_key",
  199. "my_supe_secure_server", # This is the acme_domain
  200. )
  201. ),
  202. "/config_dir_path",
  203. )
  204. self.assertTrue(conf.acme_enabled)