test_tls.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. from OpenSSL import SSL
  18. from synapse.config.tls import ConfigError, TlsConfig
  19. from synapse.crypto.context_factory import ClientTLSOptionsFactory
  20. from tests.unittest import TestCase
  21. class TestConfig(TlsConfig):
  22. def has_tls_listener(self):
  23. return False
  24. class TLSConfigTests(TestCase):
  25. def test_warn_self_signed(self):
  26. """
  27. Synapse will give a warning when it loads a self-signed certificate.
  28. """
  29. config_dir = self.mktemp()
  30. os.mkdir(config_dir)
  31. with open(os.path.join(config_dir, "cert.pem"), "w") as f:
  32. f.write(
  33. """-----BEGIN CERTIFICATE-----
  34. MIID6DCCAtACAws9CjANBgkqhkiG9w0BAQUFADCBtzELMAkGA1UEBhMCVFIxDzAN
  35. BgNVBAgMBsOHb3J1bTEUMBIGA1UEBwwLQmHFn21ha8OnxLExEjAQBgNVBAMMCWxv
  36. Y2FsaG9zdDEcMBoGA1UECgwTVHdpc3RlZCBNYXRyaXggTGFiczEkMCIGA1UECwwb
  37. QXV0b21hdGVkIFRlc3RpbmcgQXV0aG9yaXR5MSkwJwYJKoZIhvcNAQkBF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mjI0TI6xsDUlXz5S3UyudrE2Qsfz
  54. s4niecZKPBizL6aucT59CsunNmmb5Glq8rlAcU+1ZTZZzGYqVYhF6axB9Qg=
  55. -----END CERTIFICATE-----"""
  56. )
  57. config = {
  58. "tls_certificate_path": os.path.join(config_dir, "cert.pem"),
  59. "tls_fingerprints": [],
  60. }
  61. t = TestConfig()
  62. t.read_config(config, config_dir_path="", data_dir_path="")
  63. t.read_certificate_from_disk(require_cert_and_key=False)
  64. warnings = self.flushWarnings()
  65. self.assertEqual(len(warnings), 1)
  66. self.assertEqual(
  67. warnings[0]["message"],
  68. (
  69. "Self-signed TLS certificates will not be accepted by "
  70. "Synapse 1.0. Please either provide a valid certificate, "
  71. "or use Synapse's ACME support to provision one."
  72. ),
  73. )
  74. def test_tls_client_minimum_default(self):
  75. """
  76. The default client TLS version is 1.0.
  77. """
  78. config = {}
  79. t = TestConfig()
  80. t.read_config(config, config_dir_path="", data_dir_path="")
  81. self.assertEqual(t.federation_client_minimum_tls_version, "1")
  82. def test_tls_client_minimum_set(self):
  83. """
  84. The default client TLS version can be set to 1.0, 1.1, and 1.2.
  85. """
  86. config = {"federation_client_minimum_tls_version": 1}
  87. t = TestConfig()
  88. t.read_config(config, config_dir_path="", data_dir_path="")
  89. self.assertEqual(t.federation_client_minimum_tls_version, "1")
  90. config = {"federation_client_minimum_tls_version": 1.1}
  91. t = TestConfig()
  92. t.read_config(config, config_dir_path="", data_dir_path="")
  93. self.assertEqual(t.federation_client_minimum_tls_version, "1.1")
  94. config = {"federation_client_minimum_tls_version": 1.2}
  95. t = TestConfig()
  96. t.read_config(config, config_dir_path="", data_dir_path="")
  97. self.assertEqual(t.federation_client_minimum_tls_version, "1.2")
  98. # Also test a string version
  99. config = {"federation_client_minimum_tls_version": "1"}
  100. t = TestConfig()
  101. t.read_config(config, config_dir_path="", data_dir_path="")
  102. self.assertEqual(t.federation_client_minimum_tls_version, "1")
  103. config = {"federation_client_minimum_tls_version": "1.2"}
  104. t = TestConfig()
  105. t.read_config(config, config_dir_path="", data_dir_path="")
  106. self.assertEqual(t.federation_client_minimum_tls_version, "1.2")
  107. def test_tls_client_minimum_1_point_3_missing(self):
  108. """
  109. If TLS 1.3 support is missing and it's configured, it will raise a
  110. ConfigError.
  111. """
  112. # thanks i hate it
  113. if hasattr(SSL, "OP_NO_TLSv1_3"):
  114. OP_NO_TLSv1_3 = SSL.OP_NO_TLSv1_3
  115. delattr(SSL, "OP_NO_TLSv1_3")
  116. self.addCleanup(setattr, SSL, "SSL.OP_NO_TLSv1_3", OP_NO_TLSv1_3)
  117. assert not hasattr(SSL, "OP_NO_TLSv1_3")
  118. config = {"federation_client_minimum_tls_version": 1.3}
  119. t = TestConfig()
  120. with self.assertRaises(ConfigError) as e:
  121. t.read_config(config, config_dir_path="", data_dir_path="")
  122. self.assertEqual(
  123. e.exception.args[0],
  124. (
  125. "federation_client_minimum_tls_version cannot be 1.3, "
  126. "your OpenSSL does not support it"
  127. ),
  128. )
  129. def test_tls_client_minimum_1_point_3_exists(self):
  130. """
  131. If TLS 1.3 support exists and it's configured, it will be settable.
  132. """
  133. # thanks i hate it, still
  134. if not hasattr(SSL, "OP_NO_TLSv1_3"):
  135. SSL.OP_NO_TLSv1_3 = 0x00
  136. self.addCleanup(lambda: delattr(SSL, "OP_NO_TLSv1_3"))
  137. assert hasattr(SSL, "OP_NO_TLSv1_3")
  138. config = {"federation_client_minimum_tls_version": 1.3}
  139. t = TestConfig()
  140. t.read_config(config, config_dir_path="", data_dir_path="")
  141. self.assertEqual(t.federation_client_minimum_tls_version, "1.3")
  142. def test_tls_client_minimum_set_passed_through_1_2(self):
  143. """
  144. The configured TLS version is correctly configured by the ContextFactory.
  145. """
  146. config = {"federation_client_minimum_tls_version": 1.2}
  147. t = TestConfig()
  148. t.read_config(config, config_dir_path="", data_dir_path="")
  149. cf = ClientTLSOptionsFactory(t)
  150. # The context has had NO_TLSv1_1 and NO_TLSv1_0 set, but not NO_TLSv1_2
  151. self.assertNotEqual(cf._verify_ssl._options & SSL.OP_NO_TLSv1, 0)
  152. self.assertNotEqual(cf._verify_ssl._options & SSL.OP_NO_TLSv1_1, 0)
  153. self.assertEqual(cf._verify_ssl._options & SSL.OP_NO_TLSv1_2, 0)
  154. def test_tls_client_minimum_set_passed_through_1_0(self):
  155. """
  156. The configured TLS version is correctly configured by the ContextFactory.
  157. """
  158. config = {"federation_client_minimum_tls_version": 1}
  159. t = TestConfig()
  160. t.read_config(config, config_dir_path="", data_dir_path="")
  161. cf = ClientTLSOptionsFactory(t)
  162. # The context has not had any of the NO_TLS set.
  163. self.assertEqual(cf._verify_ssl._options & SSL.OP_NO_TLSv1, 0)
  164. self.assertEqual(cf._verify_ssl._options & SSL.OP_NO_TLSv1_1, 0)
  165. self.assertEqual(cf._verify_ssl._options & SSL.OP_NO_TLSv1_2, 0)