test_tls.py 11 KB

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