2019-01-29 07:09:10 -07:00
|
|
|
#
|
2023-11-21 13:29:58 -07:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2019-01-29 07:09:10 -07:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2022-12-16 06:53:28 -07:00
|
|
|
from typing import cast
|
|
|
|
|
2019-09-13 12:58:38 -06:00
|
|
|
import idna
|
2019-08-28 06:12:22 -06:00
|
|
|
|
2019-06-28 02:19:09 -06:00
|
|
|
from OpenSSL import SSL
|
|
|
|
|
2019-10-10 02:39:35 -06:00
|
|
|
from synapse.config._base import Config, RootConfig
|
2022-12-16 06:53:28 -07:00
|
|
|
from synapse.config.homeserver import HomeServerConfig
|
2019-06-28 02:19:09 -06:00
|
|
|
from synapse.config.tls import ConfigError, TlsConfig
|
2022-12-16 06:53:28 -07:00
|
|
|
from synapse.crypto.context_factory import (
|
|
|
|
FederationPolicyForHTTPS,
|
|
|
|
SSLClientConnectionCreator,
|
|
|
|
)
|
|
|
|
from synapse.types import JsonDict
|
2019-01-29 07:09:10 -07:00
|
|
|
|
|
|
|
from tests.unittest import TestCase
|
|
|
|
|
|
|
|
|
2019-10-10 02:39:35 -06:00
|
|
|
class FakeServer(Config):
|
|
|
|
section = "server"
|
|
|
|
|
2022-12-16 06:53:28 -07:00
|
|
|
def has_tls_listener(self) -> bool:
|
2019-02-11 15:01:27 -07:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
2019-10-10 02:39:35 -06:00
|
|
|
class TestConfig(RootConfig):
|
|
|
|
config_classes = [FakeServer, TlsConfig]
|
|
|
|
|
|
|
|
|
2019-01-29 07:09:10 -07:00
|
|
|
class TLSConfigTests(TestCase):
|
2022-12-16 06:53:28 -07:00
|
|
|
def test_tls_client_minimum_default(self) -> None:
|
2019-06-28 02:19:09 -06:00
|
|
|
"""
|
|
|
|
The default client TLS version is 1.0.
|
|
|
|
"""
|
2022-12-16 06:53:28 -07:00
|
|
|
config: JsonDict = {}
|
2019-06-28 02:19:09 -06:00
|
|
|
t = TestConfig()
|
2021-10-06 08:47:41 -06:00
|
|
|
t.tls.read_config(config, config_dir_path="", data_dir_path="")
|
2019-06-28 02:19:09 -06:00
|
|
|
|
2021-10-06 08:47:41 -06:00
|
|
|
self.assertEqual(t.tls.federation_client_minimum_tls_version, "1")
|
2019-06-28 02:19:09 -06:00
|
|
|
|
2022-12-16 06:53:28 -07:00
|
|
|
def test_tls_client_minimum_set(self) -> None:
|
2019-06-28 02:19:09 -06:00
|
|
|
"""
|
|
|
|
The default client TLS version can be set to 1.0, 1.1, and 1.2.
|
|
|
|
"""
|
2022-12-16 06:53:28 -07:00
|
|
|
config: JsonDict = {"federation_client_minimum_tls_version": 1}
|
2019-06-28 02:19:09 -06:00
|
|
|
t = TestConfig()
|
2021-10-06 08:47:41 -06:00
|
|
|
t.tls.read_config(config, config_dir_path="", data_dir_path="")
|
|
|
|
self.assertEqual(t.tls.federation_client_minimum_tls_version, "1")
|
2019-06-28 02:19:09 -06:00
|
|
|
|
|
|
|
config = {"federation_client_minimum_tls_version": 1.1}
|
|
|
|
t = TestConfig()
|
2021-10-06 08:47:41 -06:00
|
|
|
t.tls.read_config(config, config_dir_path="", data_dir_path="")
|
|
|
|
self.assertEqual(t.tls.federation_client_minimum_tls_version, "1.1")
|
2019-06-28 02:19:09 -06:00
|
|
|
|
|
|
|
config = {"federation_client_minimum_tls_version": 1.2}
|
|
|
|
t = TestConfig()
|
2021-10-06 08:47:41 -06:00
|
|
|
t.tls.read_config(config, config_dir_path="", data_dir_path="")
|
|
|
|
self.assertEqual(t.tls.federation_client_minimum_tls_version, "1.2")
|
2019-06-28 02:19:09 -06:00
|
|
|
|
|
|
|
# Also test a string version
|
|
|
|
config = {"federation_client_minimum_tls_version": "1"}
|
|
|
|
t = TestConfig()
|
2021-10-06 08:47:41 -06:00
|
|
|
t.tls.read_config(config, config_dir_path="", data_dir_path="")
|
|
|
|
self.assertEqual(t.tls.federation_client_minimum_tls_version, "1")
|
2019-06-28 02:19:09 -06:00
|
|
|
|
|
|
|
config = {"federation_client_minimum_tls_version": "1.2"}
|
|
|
|
t = TestConfig()
|
2021-10-06 08:47:41 -06:00
|
|
|
t.tls.read_config(config, config_dir_path="", data_dir_path="")
|
|
|
|
self.assertEqual(t.tls.federation_client_minimum_tls_version, "1.2")
|
2019-06-28 02:19:09 -06:00
|
|
|
|
2022-12-16 06:53:28 -07:00
|
|
|
def test_tls_client_minimum_1_point_3_missing(self) -> None:
|
2019-06-28 02:19:09 -06:00
|
|
|
"""
|
|
|
|
If TLS 1.3 support is missing and it's configured, it will raise a
|
|
|
|
ConfigError.
|
|
|
|
"""
|
|
|
|
# thanks i hate it
|
|
|
|
if hasattr(SSL, "OP_NO_TLSv1_3"):
|
|
|
|
OP_NO_TLSv1_3 = SSL.OP_NO_TLSv1_3
|
|
|
|
delattr(SSL, "OP_NO_TLSv1_3")
|
|
|
|
self.addCleanup(setattr, SSL, "SSL.OP_NO_TLSv1_3", OP_NO_TLSv1_3)
|
|
|
|
assert not hasattr(SSL, "OP_NO_TLSv1_3")
|
|
|
|
|
2022-12-16 06:53:28 -07:00
|
|
|
config: JsonDict = {"federation_client_minimum_tls_version": 1.3}
|
2019-06-28 02:19:09 -06:00
|
|
|
t = TestConfig()
|
|
|
|
with self.assertRaises(ConfigError) as e:
|
2021-10-06 08:47:41 -06:00
|
|
|
t.tls.read_config(config, config_dir_path="", data_dir_path="")
|
2019-06-28 02:19:09 -06:00
|
|
|
self.assertEqual(
|
|
|
|
e.exception.args[0],
|
|
|
|
(
|
|
|
|
"federation_client_minimum_tls_version cannot be 1.3, "
|
|
|
|
"your OpenSSL does not support it"
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2022-12-16 06:53:28 -07:00
|
|
|
def test_tls_client_minimum_1_point_3_exists(self) -> None:
|
2019-06-28 02:19:09 -06:00
|
|
|
"""
|
|
|
|
If TLS 1.3 support exists and it's configured, it will be settable.
|
|
|
|
"""
|
|
|
|
# thanks i hate it, still
|
|
|
|
if not hasattr(SSL, "OP_NO_TLSv1_3"):
|
|
|
|
SSL.OP_NO_TLSv1_3 = 0x00
|
|
|
|
self.addCleanup(lambda: delattr(SSL, "OP_NO_TLSv1_3"))
|
|
|
|
assert hasattr(SSL, "OP_NO_TLSv1_3")
|
|
|
|
|
2022-12-16 06:53:28 -07:00
|
|
|
config: JsonDict = {"federation_client_minimum_tls_version": 1.3}
|
2019-06-28 02:19:09 -06:00
|
|
|
t = TestConfig()
|
2021-10-06 08:47:41 -06:00
|
|
|
t.tls.read_config(config, config_dir_path="", data_dir_path="")
|
|
|
|
self.assertEqual(t.tls.federation_client_minimum_tls_version, "1.3")
|
2019-06-28 02:19:09 -06:00
|
|
|
|
2022-12-16 06:53:28 -07:00
|
|
|
def test_tls_client_minimum_set_passed_through_1_2(self) -> None:
|
2019-06-28 02:19:09 -06:00
|
|
|
"""
|
|
|
|
The configured TLS version is correctly configured by the ContextFactory.
|
|
|
|
"""
|
2022-12-16 06:53:28 -07:00
|
|
|
config: JsonDict = {"federation_client_minimum_tls_version": 1.2}
|
2019-06-28 02:19:09 -06:00
|
|
|
t = TestConfig()
|
2021-10-06 08:47:41 -06:00
|
|
|
t.tls.read_config(config, config_dir_path="", data_dir_path="")
|
2019-06-28 02:19:09 -06:00
|
|
|
|
2022-12-16 06:53:28 -07:00
|
|
|
cf = FederationPolicyForHTTPS(cast(HomeServerConfig, t))
|
2020-03-17 15:32:25 -06:00
|
|
|
options = _get_ssl_context_options(cf._verify_ssl_context)
|
2019-06-28 02:19:09 -06:00
|
|
|
|
|
|
|
# The context has had NO_TLSv1_1 and NO_TLSv1_0 set, but not NO_TLSv1_2
|
2020-03-17 15:32:25 -06:00
|
|
|
self.assertNotEqual(options & SSL.OP_NO_TLSv1, 0)
|
|
|
|
self.assertNotEqual(options & SSL.OP_NO_TLSv1_1, 0)
|
|
|
|
self.assertEqual(options & SSL.OP_NO_TLSv1_2, 0)
|
2019-06-28 02:19:09 -06:00
|
|
|
|
2022-12-16 06:53:28 -07:00
|
|
|
def test_tls_client_minimum_set_passed_through_1_0(self) -> None:
|
2019-06-28 02:19:09 -06:00
|
|
|
"""
|
|
|
|
The configured TLS version is correctly configured by the ContextFactory.
|
|
|
|
"""
|
2022-12-16 06:53:28 -07:00
|
|
|
config: JsonDict = {"federation_client_minimum_tls_version": 1}
|
2019-06-28 02:19:09 -06:00
|
|
|
t = TestConfig()
|
2021-10-06 08:47:41 -06:00
|
|
|
t.tls.read_config(config, config_dir_path="", data_dir_path="")
|
2019-06-28 02:19:09 -06:00
|
|
|
|
2022-12-16 06:53:28 -07:00
|
|
|
cf = FederationPolicyForHTTPS(cast(HomeServerConfig, t))
|
2020-03-17 15:32:25 -06:00
|
|
|
options = _get_ssl_context_options(cf._verify_ssl_context)
|
2019-06-28 02:19:09 -06:00
|
|
|
|
|
|
|
# The context has not had any of the NO_TLS set.
|
2020-03-17 15:32:25 -06:00
|
|
|
self.assertEqual(options & SSL.OP_NO_TLSv1, 0)
|
|
|
|
self.assertEqual(options & SSL.OP_NO_TLSv1_1, 0)
|
|
|
|
self.assertEqual(options & SSL.OP_NO_TLSv1_2, 0)
|
2019-08-28 06:12:22 -06:00
|
|
|
|
2022-12-16 06:53:28 -07:00
|
|
|
def test_whitelist_idna_failure(self) -> None:
|
2019-09-13 12:58:38 -06:00
|
|
|
"""
|
|
|
|
The federation certificate whitelist will not allow IDNA domain names.
|
|
|
|
"""
|
2022-12-16 06:53:28 -07:00
|
|
|
config: JsonDict = {
|
2019-09-13 12:58:38 -06:00
|
|
|
"federation_certificate_verification_whitelist": [
|
|
|
|
"example.com",
|
|
|
|
"*.ドメイン.テスト",
|
|
|
|
]
|
|
|
|
}
|
|
|
|
t = TestConfig()
|
|
|
|
e = self.assertRaises(
|
2021-10-06 08:47:41 -06:00
|
|
|
ConfigError, t.tls.read_config, config, config_dir_path="", data_dir_path=""
|
2019-09-13 12:58:38 -06:00
|
|
|
)
|
|
|
|
self.assertIn("IDNA domain names", str(e))
|
|
|
|
|
2022-12-16 06:53:28 -07:00
|
|
|
def test_whitelist_idna_result(self) -> None:
|
2019-09-13 12:58:38 -06:00
|
|
|
"""
|
|
|
|
The federation certificate whitelist will match on IDNA encoded names.
|
|
|
|
"""
|
2022-12-16 06:53:28 -07:00
|
|
|
config: JsonDict = {
|
2019-09-13 12:58:38 -06:00
|
|
|
"federation_certificate_verification_whitelist": [
|
|
|
|
"example.com",
|
|
|
|
"*.xn--eckwd4c7c.xn--zckzah",
|
|
|
|
]
|
|
|
|
}
|
|
|
|
t = TestConfig()
|
2021-10-06 08:47:41 -06:00
|
|
|
t.tls.read_config(config, config_dir_path="", data_dir_path="")
|
2019-09-13 12:58:38 -06:00
|
|
|
|
2022-12-16 06:53:28 -07:00
|
|
|
cf = FederationPolicyForHTTPS(cast(HomeServerConfig, t))
|
2019-09-13 12:58:38 -06:00
|
|
|
|
|
|
|
# Not in the whitelist
|
|
|
|
opts = cf.get_options(b"notexample.com")
|
2022-12-16 06:53:28 -07:00
|
|
|
assert isinstance(opts, SSLClientConnectionCreator)
|
2019-09-13 12:58:38 -06:00
|
|
|
self.assertTrue(opts._verifier._verify_certs)
|
|
|
|
|
|
|
|
# Caught by the wildcard
|
|
|
|
opts = cf.get_options(idna.encode("テスト.ドメイン.テスト"))
|
2022-12-16 06:53:28 -07:00
|
|
|
assert isinstance(opts, SSLClientConnectionCreator)
|
2019-09-13 12:58:38 -06:00
|
|
|
self.assertFalse(opts._verifier._verify_certs)
|
2020-03-17 15:32:25 -06:00
|
|
|
|
|
|
|
|
|
|
|
def _get_ssl_context_options(ssl_context: SSL.Context) -> int:
|
|
|
|
"""get the options bits from an openssl context object"""
|
|
|
|
# the OpenSSL.SSL.Context wrapper doesn't expose get_options, so we have to
|
|
|
|
# use the low-level interface
|
2022-12-16 06:53:28 -07:00
|
|
|
return SSL._lib.SSL_CTX_get_options(ssl_context._context) # type: ignore[attr-defined]
|