|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | # Copyright 2019 New Vector Ltd |
| 3 | +# Copyright 2019 Matrix.org Foundation C.I.C. |
3 | 4 | # |
4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 6 | # you may not use this file except in compliance with the License. |
|
15 | 16 |
|
16 | 17 | import os |
17 | 18 |
|
18 | | -from synapse.config.tls import TlsConfig |
| 19 | +from OpenSSL import SSL |
| 20 | + |
| 21 | +from synapse.config.tls import ConfigError, TlsConfig |
| 22 | +from synapse.crypto.context_factory import ClientTLSOptionsFactory |
19 | 23 |
|
20 | 24 | from tests.unittest import TestCase |
21 | 25 |
|
@@ -78,3 +82,112 @@ def test_warn_self_signed(self): |
78 | 82 | "or use Synapse's ACME support to provision one." |
79 | 83 | ), |
80 | 84 | ) |
| 85 | + |
| 86 | + def test_tls_client_minimum_default(self): |
| 87 | + """ |
| 88 | + The default client TLS version is 1.0. |
| 89 | + """ |
| 90 | + config = {} |
| 91 | + t = TestConfig() |
| 92 | + t.read_config(config, config_dir_path="", data_dir_path="") |
| 93 | + |
| 94 | + self.assertEqual(t.federation_client_minimum_tls_version, "1") |
| 95 | + |
| 96 | + def test_tls_client_minimum_set(self): |
| 97 | + """ |
| 98 | + The default client TLS version can be set to 1.0, 1.1, and 1.2. |
| 99 | + """ |
| 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 | + |
| 105 | + config = {"federation_client_minimum_tls_version": 1.1} |
| 106 | + t = TestConfig() |
| 107 | + t.read_config(config, config_dir_path="", data_dir_path="") |
| 108 | + self.assertEqual(t.federation_client_minimum_tls_version, "1.1") |
| 109 | + |
| 110 | + config = {"federation_client_minimum_tls_version": 1.2} |
| 111 | + t = TestConfig() |
| 112 | + t.read_config(config, config_dir_path="", data_dir_path="") |
| 113 | + self.assertEqual(t.federation_client_minimum_tls_version, "1.2") |
| 114 | + |
| 115 | + # Also test a string version |
| 116 | + config = {"federation_client_minimum_tls_version": "1"} |
| 117 | + t = TestConfig() |
| 118 | + t.read_config(config, config_dir_path="", data_dir_path="") |
| 119 | + self.assertEqual(t.federation_client_minimum_tls_version, "1") |
| 120 | + |
| 121 | + config = {"federation_client_minimum_tls_version": "1.2"} |
| 122 | + t = TestConfig() |
| 123 | + t.read_config(config, config_dir_path="", data_dir_path="") |
| 124 | + self.assertEqual(t.federation_client_minimum_tls_version, "1.2") |
| 125 | + |
| 126 | + def test_tls_client_minimum_1_point_3_missing(self): |
| 127 | + """ |
| 128 | + If TLS 1.3 support is missing and it's configured, it will raise a |
| 129 | + ConfigError. |
| 130 | + """ |
| 131 | + # thanks i hate it |
| 132 | + if hasattr(SSL, "OP_NO_TLSv1_3"): |
| 133 | + OP_NO_TLSv1_3 = SSL.OP_NO_TLSv1_3 |
| 134 | + delattr(SSL, "OP_NO_TLSv1_3") |
| 135 | + self.addCleanup(setattr, SSL, "SSL.OP_NO_TLSv1_3", OP_NO_TLSv1_3) |
| 136 | + assert not hasattr(SSL, "OP_NO_TLSv1_3") |
| 137 | + |
| 138 | + config = {"federation_client_minimum_tls_version": 1.3} |
| 139 | + t = TestConfig() |
| 140 | + with self.assertRaises(ConfigError) as e: |
| 141 | + t.read_config(config, config_dir_path="", data_dir_path="") |
| 142 | + self.assertEqual( |
| 143 | + e.exception.args[0], |
| 144 | + ( |
| 145 | + "federation_client_minimum_tls_version cannot be 1.3, " |
| 146 | + "your OpenSSL does not support it" |
| 147 | + ), |
| 148 | + ) |
| 149 | + |
| 150 | + def test_tls_client_minimum_1_point_3_exists(self): |
| 151 | + """ |
| 152 | + If TLS 1.3 support exists and it's configured, it will be settable. |
| 153 | + """ |
| 154 | + # thanks i hate it, still |
| 155 | + if not hasattr(SSL, "OP_NO_TLSv1_3"): |
| 156 | + SSL.OP_NO_TLSv1_3 = 0x00 |
| 157 | + self.addCleanup(lambda: delattr(SSL, "OP_NO_TLSv1_3")) |
| 158 | + assert hasattr(SSL, "OP_NO_TLSv1_3") |
| 159 | + |
| 160 | + config = {"federation_client_minimum_tls_version": 1.3} |
| 161 | + t = TestConfig() |
| 162 | + t.read_config(config, config_dir_path="", data_dir_path="") |
| 163 | + self.assertEqual(t.federation_client_minimum_tls_version, "1.3") |
| 164 | + |
| 165 | + def test_tls_client_minimum_set_passed_through_1_2(self): |
| 166 | + """ |
| 167 | + The configured TLS version is correctly configured by the ContextFactory. |
| 168 | + """ |
| 169 | + config = {"federation_client_minimum_tls_version": 1.2} |
| 170 | + t = TestConfig() |
| 171 | + t.read_config(config, config_dir_path="", data_dir_path="") |
| 172 | + |
| 173 | + cf = ClientTLSOptionsFactory(t) |
| 174 | + |
| 175 | + # The context has had NO_TLSv1_1 and NO_TLSv1_0 set, but not NO_TLSv1_2 |
| 176 | + self.assertNotEqual(cf._verify_ssl._options & SSL.OP_NO_TLSv1, 0) |
| 177 | + self.assertNotEqual(cf._verify_ssl._options & SSL.OP_NO_TLSv1_1, 0) |
| 178 | + self.assertEqual(cf._verify_ssl._options & SSL.OP_NO_TLSv1_2, 0) |
| 179 | + |
| 180 | + def test_tls_client_minimum_set_passed_through_1_0(self): |
| 181 | + """ |
| 182 | + The configured TLS version is correctly configured by the ContextFactory. |
| 183 | + """ |
| 184 | + config = {"federation_client_minimum_tls_version": 1} |
| 185 | + t = TestConfig() |
| 186 | + t.read_config(config, config_dir_path="", data_dir_path="") |
| 187 | + |
| 188 | + cf = ClientTLSOptionsFactory(t) |
| 189 | + |
| 190 | + # The context has not had any of the NO_TLS set. |
| 191 | + self.assertEqual(cf._verify_ssl._options & SSL.OP_NO_TLSv1, 0) |
| 192 | + self.assertEqual(cf._verify_ssl._options & SSL.OP_NO_TLSv1_1, 0) |
| 193 | + self.assertEqual(cf._verify_ssl._options & SSL.OP_NO_TLSv1_2, 0) |
0 commit comments