test_tls.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 idna
  18. import yaml
  19. from OpenSSL import SSL
  20. from synapse.config.tls import ConfigError, TlsConfig
  21. from synapse.crypto.context_factory import ClientTLSOptionsFactory
  22. from tests.unittest import TestCase
  23. class TestConfig(TlsConfig):
  24. def has_tls_listener(self):
  25. return False
  26. class TLSConfigTests(TestCase):
  27. def test_warn_self_signed(self):
  28. """
  29. Synapse will give a warning when it loads a self-signed certificate.
  30. """
  31. config_dir = self.mktemp()
  32. os.mkdir(config_dir)
  33. with open(os.path.join(config_dir, "cert.pem"), "w") as f:
  34. f.write(
  35. """-----BEGIN CERTIFICATE-----
  36. MIID6DCCAtACAws9CjANBgkqhkiG9w0BAQUFADCBtzELMAkGA1UEBhMCVFIxDzAN
  37. BgNVBAgMBsOHb3J1bTEUMBIGA1UEBwwLQmHFn21ha8OnxLExEjAQBgNVBAMMCWxv
  38. Y2FsaG9zdDEcMBoGA1UECgwT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  54. iZupikC5MT1LQaRwidkSNxCku1TfAyueiBwhLnFwTmIGNnhuDCutEVAD9kFmcJN2
  55. SznugAcPk4doX2+rL+ila+ThqgPzIkwTUHtnmjI0TI6xsDUlXz5S3UyudrE2Qsfz
  56. s4niecZKPBizL6aucT59CsunNmmb5Glq8rlAcU+1ZTZZzGYqVYhF6axB9Qg=
  57. -----END CERTIFICATE-----"""
  58. )
  59. config = {
  60. "tls_certificate_path": os.path.join(config_dir, "cert.pem"),
  61. "tls_fingerprints": [],
  62. }
  63. t = TestConfig()
  64. t.read_config(config, config_dir_path="", data_dir_path="")
  65. t.read_certificate_from_disk(require_cert_and_key=False)
  66. warnings = self.flushWarnings()
  67. self.assertEqual(len(warnings), 1)
  68. self.assertEqual(
  69. warnings[0]["message"],
  70. (
  71. "Self-signed TLS certificates will not be accepted by "
  72. "Synapse 1.0. Please either provide a valid certificate, "
  73. "or use Synapse's ACME support to provision one."
  74. ),
  75. )
  76. def test_tls_client_minimum_default(self):
  77. """
  78. The default client TLS version is 1.0.
  79. """
  80. config = {}
  81. t = TestConfig()
  82. t.read_config(config, config_dir_path="", data_dir_path="")
  83. self.assertEqual(t.federation_client_minimum_tls_version, "1")
  84. def test_tls_client_minimum_set(self):
  85. """
  86. The default client TLS version can be set to 1.0, 1.1, and 1.2.
  87. """
  88. config = {"federation_client_minimum_tls_version": 1}
  89. t = TestConfig()
  90. t.read_config(config, config_dir_path="", data_dir_path="")
  91. self.assertEqual(t.federation_client_minimum_tls_version, "1")
  92. config = {"federation_client_minimum_tls_version": 1.1}
  93. t = TestConfig()
  94. t.read_config(config, config_dir_path="", data_dir_path="")
  95. self.assertEqual(t.federation_client_minimum_tls_version, "1.1")
  96. config = {"federation_client_minimum_tls_version": 1.2}
  97. t = TestConfig()
  98. t.read_config(config, config_dir_path="", data_dir_path="")
  99. self.assertEqual(t.federation_client_minimum_tls_version, "1.2")
  100. # Also test a string version
  101. config = {"federation_client_minimum_tls_version": "1"}
  102. t = TestConfig()
  103. t.read_config(config, config_dir_path="", data_dir_path="")
  104. self.assertEqual(t.federation_client_minimum_tls_version, "1")
  105. config = {"federation_client_minimum_tls_version": "1.2"}
  106. t = TestConfig()
  107. t.read_config(config, config_dir_path="", data_dir_path="")
  108. self.assertEqual(t.federation_client_minimum_tls_version, "1.2")
  109. def test_tls_client_minimum_1_point_3_missing(self):
  110. """
  111. If TLS 1.3 support is missing and it's configured, it will raise a
  112. ConfigError.
  113. """
  114. # thanks i hate it
  115. if hasattr(SSL, "OP_NO_TLSv1_3"):
  116. OP_NO_TLSv1_3 = SSL.OP_NO_TLSv1_3
  117. delattr(SSL, "OP_NO_TLSv1_3")
  118. self.addCleanup(setattr, SSL, "SSL.OP_NO_TLSv1_3", OP_NO_TLSv1_3)
  119. assert not hasattr(SSL, "OP_NO_TLSv1_3")
  120. config = {"federation_client_minimum_tls_version": 1.3}
  121. t = TestConfig()
  122. with self.assertRaises(ConfigError) as e:
  123. t.read_config(config, config_dir_path="", data_dir_path="")
  124. self.assertEqual(
  125. e.exception.args[0],
  126. (
  127. "federation_client_minimum_tls_version cannot be 1.3, "
  128. "your OpenSSL does not support it"
  129. ),
  130. )
  131. def test_tls_client_minimum_1_point_3_exists(self):
  132. """
  133. If TLS 1.3 support exists and it's configured, it will be settable.
  134. """
  135. # thanks i hate it, still
  136. if not hasattr(SSL, "OP_NO_TLSv1_3"):
  137. SSL.OP_NO_TLSv1_3 = 0x00
  138. self.addCleanup(lambda: delattr(SSL, "OP_NO_TLSv1_3"))
  139. assert hasattr(SSL, "OP_NO_TLSv1_3")
  140. config = {"federation_client_minimum_tls_version": 1.3}
  141. t = TestConfig()
  142. t.read_config(config, config_dir_path="", data_dir_path="")
  143. self.assertEqual(t.federation_client_minimum_tls_version, "1.3")
  144. def test_tls_client_minimum_set_passed_through_1_2(self):
  145. """
  146. The configured TLS version is correctly configured by the ContextFactory.
  147. """
  148. config = {"federation_client_minimum_tls_version": 1.2}
  149. t = TestConfig()
  150. t.read_config(config, config_dir_path="", data_dir_path="")
  151. cf = ClientTLSOptionsFactory(t)
  152. # The context has had NO_TLSv1_1 and NO_TLSv1_0 set, but not NO_TLSv1_2
  153. self.assertNotEqual(cf._verify_ssl._options & SSL.OP_NO_TLSv1, 0)
  154. self.assertNotEqual(cf._verify_ssl._options & SSL.OP_NO_TLSv1_1, 0)
  155. self.assertEqual(cf._verify_ssl._options & SSL.OP_NO_TLSv1_2, 0)
  156. def test_tls_client_minimum_set_passed_through_1_0(self):
  157. """
  158. The configured TLS version is correctly configured by the ContextFactory.
  159. """
  160. config = {"federation_client_minimum_tls_version": 1}
  161. t = TestConfig()
  162. t.read_config(config, config_dir_path="", data_dir_path="")
  163. cf = ClientTLSOptionsFactory(t)
  164. # The context has not had any of the NO_TLS set.
  165. self.assertEqual(cf._verify_ssl._options & SSL.OP_NO_TLSv1, 0)
  166. self.assertEqual(cf._verify_ssl._options & SSL.OP_NO_TLSv1_1, 0)
  167. self.assertEqual(cf._verify_ssl._options & SSL.OP_NO_TLSv1_2, 0)
  168. def test_acme_disabled_in_generated_config_no_acme_domain_provied(self):
  169. """
  170. Checks acme is disabled by default.
  171. """
  172. conf = TestConfig()
  173. conf.read_config(
  174. yaml.safe_load(
  175. TestConfig().generate_config_section(
  176. "/config_dir_path",
  177. "my_super_secure_server",
  178. "/data_dir_path",
  179. "/tls_cert_path",
  180. "tls_private_key",
  181. None, # This is the acme_domain
  182. )
  183. ),
  184. "/config_dir_path",
  185. )
  186. self.assertFalse(conf.acme_enabled)
  187. def test_acme_enabled_in_generated_config_domain_provided(self):
  188. """
  189. Checks acme is enabled if the acme_domain arg is set to some string.
  190. """
  191. conf = TestConfig()
  192. conf.read_config(
  193. yaml.safe_load(
  194. TestConfig().generate_config_section(
  195. "/config_dir_path",
  196. "my_super_secure_server",
  197. "/data_dir_path",
  198. "/tls_cert_path",
  199. "tls_private_key",
  200. "my_supe_secure_server", # This is the acme_domain
  201. )
  202. ),
  203. "/config_dir_path",
  204. )
  205. self.assertTrue(conf.acme_enabled)
  206. def test_whitelist_idna_failure(self):
  207. """
  208. The federation certificate whitelist will not allow IDNA domain names.
  209. """
  210. config = {
  211. "federation_certificate_verification_whitelist": [
  212. "example.com",
  213. "*.ドメイン.テスト",
  214. ]
  215. }
  216. t = TestConfig()
  217. e = self.assertRaises(
  218. ConfigError, t.read_config, config, config_dir_path="", data_dir_path=""
  219. )
  220. self.assertIn("IDNA domain names", str(e))
  221. def test_whitelist_idna_result(self):
  222. """
  223. The federation certificate whitelist will match on IDNA encoded names.
  224. """
  225. config = {
  226. "federation_certificate_verification_whitelist": [
  227. "example.com",
  228. "*.xn--eckwd4c7c.xn--zckzah",
  229. ]
  230. }
  231. t = TestConfig()
  232. t.read_config(config, config_dir_path="", data_dir_path="")
  233. cf = ClientTLSOptionsFactory(t)
  234. # Not in the whitelist
  235. opts = cf.get_options(b"notexample.com")
  236. self.assertTrue(opts._verifier._verify_certs)
  237. # Caught by the wildcard
  238. opts = cf.get_options(idna.encode("テスト.ドメイン.テスト"))
  239. self.assertFalse(opts._verifier._verify_certs)