2016-01-06 23:26:29 -05:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2014-08-31 11:06:39 -04:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2019-01-23 03:39:06 -05:00
|
|
|
import logging
|
2014-08-31 11:06:39 -04:00
|
|
|
import os
|
2021-05-11 05:47:23 -04:00
|
|
|
from typing import List, Optional, Pattern
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2022-01-05 06:41:49 -05:00
|
|
|
from matrix_common.regex import glob_to_regex
|
|
|
|
|
2019-06-28 04:19:09 -04:00
|
|
|
from OpenSSL import SSL, crypto
|
2019-04-25 09:22:49 -04:00
|
|
|
from twisted.internet._sslverify import Certificate, trustRootFromCertificates
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2019-02-12 05:51:31 -05:00
|
|
|
from synapse.config._base import Config, ConfigError
|
2019-01-23 03:39:06 -05:00
|
|
|
|
2019-02-11 16:00:41 -05:00
|
|
|
logger = logging.getLogger(__name__)
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2014-09-01 15:35:10 -04:00
|
|
|
|
2014-08-31 11:06:39 -04:00
|
|
|
class TlsConfig(Config):
|
2019-10-10 04:39:35 -04:00
|
|
|
section = "tls"
|
|
|
|
|
|
|
|
def read_config(self, config: dict, config_dir_path: str, **kwargs):
|
2015-07-08 13:20:02 -04:00
|
|
|
|
2019-01-29 09:09:10 -05:00
|
|
|
self.tls_certificate_file = self.abspath(config.get("tls_certificate_path"))
|
|
|
|
self.tls_private_key_file = self.abspath(config.get("tls_private_key_path"))
|
2019-02-12 05:51:31 -05:00
|
|
|
|
2019-10-10 04:39:35 -04:00
|
|
|
if self.root.server.has_tls_listener():
|
2019-02-12 05:51:31 -05:00
|
|
|
if not self.tls_certificate_file:
|
|
|
|
raise ConfigError(
|
|
|
|
"tls_certificate_path must be specified if TLS-enabled listeners are "
|
|
|
|
"configured."
|
|
|
|
)
|
|
|
|
if not self.tls_private_key_file:
|
|
|
|
raise ConfigError(
|
2019-02-25 14:16:33 -05:00
|
|
|
"tls_private_key_path must be specified if TLS-enabled listeners are "
|
2019-02-12 05:51:31 -05:00
|
|
|
"configured."
|
|
|
|
)
|
|
|
|
|
2019-04-25 09:22:49 -04:00
|
|
|
# Whether to verify certificates on outbound federation traffic
|
|
|
|
self.federation_verify_certificates = config.get(
|
2019-06-20 05:32:02 -04:00
|
|
|
"federation_verify_certificates", True
|
2019-04-25 09:22:49 -04:00
|
|
|
)
|
|
|
|
|
2019-06-28 04:19:09 -04:00
|
|
|
# Minimum TLS version to use for outbound federation traffic
|
|
|
|
self.federation_client_minimum_tls_version = str(
|
|
|
|
config.get("federation_client_minimum_tls_version", 1)
|
|
|
|
)
|
|
|
|
|
|
|
|
if self.federation_client_minimum_tls_version not in ["1", "1.1", "1.2", "1.3"]:
|
|
|
|
raise ConfigError(
|
|
|
|
"federation_client_minimum_tls_version must be one of: 1, 1.1, 1.2, 1.3"
|
|
|
|
)
|
|
|
|
|
|
|
|
# Prevent people shooting themselves in the foot here by setting it to
|
|
|
|
# the biggest number blindly
|
|
|
|
if self.federation_client_minimum_tls_version == "1.3":
|
|
|
|
if getattr(SSL, "OP_NO_TLSv1_3", None) is None:
|
|
|
|
raise ConfigError(
|
2021-07-19 10:28:05 -04:00
|
|
|
"federation_client_minimum_tls_version cannot be 1.3, "
|
|
|
|
"your OpenSSL does not support it"
|
2019-06-28 04:19:09 -04:00
|
|
|
)
|
|
|
|
|
2019-04-25 09:22:49 -04:00
|
|
|
# Whitelist of domains to not verify certificates for
|
|
|
|
fed_whitelist_entries = config.get(
|
2019-06-20 05:32:02 -04:00
|
|
|
"federation_certificate_verification_whitelist", []
|
2019-04-25 09:22:49 -04:00
|
|
|
)
|
2020-02-06 09:45:01 -05:00
|
|
|
if fed_whitelist_entries is None:
|
|
|
|
fed_whitelist_entries = []
|
2019-04-25 09:22:49 -04:00
|
|
|
|
|
|
|
# Support globs (*) in whitelist values
|
2021-07-15 06:02:43 -04:00
|
|
|
self.federation_certificate_verification_whitelist: List[Pattern] = []
|
2019-04-25 09:22:49 -04:00
|
|
|
for entry in fed_whitelist_entries:
|
2019-09-13 14:58:38 -04:00
|
|
|
try:
|
|
|
|
entry_regex = glob_to_regex(entry.encode("ascii").decode("ascii"))
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
raise ConfigError(
|
|
|
|
"IDNA domain names are not allowed in the "
|
|
|
|
"federation_certificate_verification_whitelist: %s" % (entry,)
|
|
|
|
)
|
|
|
|
|
2019-04-25 09:22:49 -04:00
|
|
|
# Convert globs to regex
|
|
|
|
self.federation_certificate_verification_whitelist.append(entry_regex)
|
|
|
|
|
|
|
|
# List of custom certificate authorities for federation traffic validation
|
2019-06-20 05:32:02 -04:00
|
|
|
custom_ca_list = config.get("federation_custom_ca_list", None)
|
2019-04-25 09:22:49 -04:00
|
|
|
|
|
|
|
# Read in and parse custom CA certificates
|
|
|
|
self.federation_ca_trust_root = None
|
|
|
|
if custom_ca_list is not None:
|
|
|
|
if len(custom_ca_list) == 0:
|
|
|
|
# A trustroot cannot be generated without any CA certificates.
|
|
|
|
# Raise an error if this option has been specified without any
|
|
|
|
# corresponding certificates.
|
2019-06-20 05:32:02 -04:00
|
|
|
raise ConfigError(
|
|
|
|
"federation_custom_ca_list specified without "
|
|
|
|
"any certificate files"
|
|
|
|
)
|
2019-04-25 09:22:49 -04:00
|
|
|
|
|
|
|
certs = []
|
|
|
|
for ca_file in custom_ca_list:
|
|
|
|
logger.debug("Reading custom CA certificate file: %s", ca_file)
|
2019-06-05 11:16:33 -04:00
|
|
|
content = self.read_file(ca_file, "federation_custom_ca_list")
|
2019-04-25 09:22:49 -04:00
|
|
|
|
|
|
|
# Parse the CA certificates
|
|
|
|
try:
|
|
|
|
cert_base = Certificate.loadPEM(content)
|
|
|
|
certs.append(cert_base)
|
|
|
|
except Exception as e:
|
2019-06-20 05:32:02 -04:00
|
|
|
raise ConfigError(
|
|
|
|
"Error parsing custom CA certificate file %s: %s" % (ca_file, e)
|
|
|
|
)
|
2019-04-25 09:22:49 -04:00
|
|
|
|
|
|
|
self.federation_ca_trust_root = trustRootFromCertificates(certs)
|
|
|
|
|
2019-01-23 03:39:06 -05:00
|
|
|
# This config option applies to non-federation HTTP clients
|
|
|
|
# (e.g. for talking to recaptcha, identity servers, and such)
|
|
|
|
# It should never be used in production, and is intended for
|
|
|
|
# use only when running tests.
|
|
|
|
self.use_insecure_ssl_client_just_for_testing_do_not_use = config.get(
|
|
|
|
"use_insecure_ssl_client_just_for_testing_do_not_use"
|
|
|
|
)
|
|
|
|
|
2021-07-15 06:02:43 -04:00
|
|
|
self.tls_certificate: Optional[crypto.X509] = None
|
|
|
|
self.tls_private_key: Optional[crypto.PKey] = None
|
2019-01-23 03:39:06 -05:00
|
|
|
|
2021-12-09 11:15:46 -05:00
|
|
|
def read_certificate_from_disk(self) -> None:
|
2019-01-23 03:39:06 -05:00
|
|
|
"""
|
2019-02-12 05:51:31 -05:00
|
|
|
Read the certificates and private key from disk.
|
|
|
|
"""
|
2021-05-27 05:34:24 -04:00
|
|
|
self.tls_private_key = self.read_tls_private_key()
|
|
|
|
self.tls_certificate = self.read_tls_certificate()
|
2015-03-06 06:34:06 -05:00
|
|
|
|
2019-06-21 19:00:20 -04:00
|
|
|
def generate_config_section(
|
2019-08-28 08:12:22 -04:00
|
|
|
self,
|
|
|
|
config_dir_path,
|
|
|
|
server_name,
|
|
|
|
data_dir_path,
|
|
|
|
tls_certificate_path,
|
|
|
|
tls_private_key_path,
|
2021-04-13 05:41:34 -04:00
|
|
|
**kwargs,
|
2019-06-21 19:00:20 -04:00
|
|
|
):
|
2021-06-17 13:56:48 -04:00
|
|
|
"""If the TLS paths are not specified the default will be certs in the
|
2019-08-28 08:12:22 -04:00
|
|
|
config directory"""
|
|
|
|
|
2015-04-29 23:24:44 -04:00
|
|
|
base_key_name = os.path.join(config_dir_path, server_name)
|
|
|
|
|
2019-08-28 08:12:22 -04:00
|
|
|
if bool(tls_certificate_path) != bool(tls_private_key_path):
|
|
|
|
raise ConfigError(
|
|
|
|
"Please specify both a cert path and a key path or neither."
|
|
|
|
)
|
|
|
|
|
2021-06-17 13:56:48 -04:00
|
|
|
tls_enabled = "" if tls_certificate_path and tls_private_key_path else "#"
|
2019-08-28 08:12:22 -04:00
|
|
|
|
|
|
|
if not tls_certificate_path:
|
|
|
|
tls_certificate_path = base_key_name + ".tls.crt"
|
|
|
|
if not tls_private_key_path:
|
|
|
|
tls_private_key_path = base_key_name + ".tls.key"
|
|
|
|
|
2019-10-23 08:22:54 -04:00
|
|
|
# flake8 doesn't recognise that variables are used in the below string
|
2021-06-17 13:56:48 -04:00
|
|
|
_ = tls_enabled
|
2019-10-23 08:22:54 -04:00
|
|
|
|
2019-01-23 03:39:06 -05:00
|
|
|
return (
|
|
|
|
"""\
|
2019-02-11 12:57:58 -05:00
|
|
|
## TLS ##
|
|
|
|
|
2019-01-30 09:17:55 -05:00
|
|
|
# PEM-encoded X509 certificate for TLS.
|
|
|
|
# This certificate, as of Synapse 1.0, will need to be a valid and verifiable
|
|
|
|
# certificate, signed by a recognised Certificate Authority.
|
|
|
|
#
|
2021-06-17 13:56:48 -04:00
|
|
|
# Be sure to use a `.pem` file that includes the full certificate chain including
|
|
|
|
# any intermediate certificates (for instance, if using certbot, use
|
|
|
|
# `fullchain.pem` as your certificate, not `cert.pem`).
|
2019-03-13 11:26:29 -04:00
|
|
|
#
|
2019-08-28 08:12:22 -04:00
|
|
|
%(tls_enabled)stls_certificate_path: "%(tls_certificate_path)s"
|
2015-04-29 23:24:44 -04:00
|
|
|
|
2019-01-30 09:17:55 -05:00
|
|
|
# PEM-encoded private key for TLS
|
2019-02-19 08:54:29 -05:00
|
|
|
#
|
2019-08-28 08:12:22 -04:00
|
|
|
%(tls_enabled)stls_private_key_path: "%(tls_private_key_path)s"
|
2015-04-29 23:24:44 -04:00
|
|
|
|
2019-06-05 09:16:07 -04:00
|
|
|
# Whether to verify TLS server certificates for outbound federation requests.
|
2019-04-25 09:22:49 -04:00
|
|
|
#
|
2019-06-05 09:16:07 -04:00
|
|
|
# Defaults to `true`. To disable certificate verification, uncomment the
|
|
|
|
# following line.
|
2019-04-25 09:22:49 -04:00
|
|
|
#
|
2019-06-05 09:16:07 -04:00
|
|
|
#federation_verify_certificates: false
|
2019-04-25 09:22:49 -04:00
|
|
|
|
2019-06-28 04:19:09 -04:00
|
|
|
# The minimum TLS version that will be used for outbound federation requests.
|
|
|
|
#
|
|
|
|
# Defaults to `1`. Configurable to `1`, `1.1`, `1.2`, or `1.3`. Note
|
|
|
|
# that setting this value higher than `1.2` will prevent federation to most
|
|
|
|
# of the public Matrix network: only configure it to `1.3` if you have an
|
|
|
|
# entirely private federation setup and you can ensure TLS 1.3 support.
|
|
|
|
#
|
|
|
|
#federation_client_minimum_tls_version: 1.2
|
|
|
|
|
2019-04-25 09:22:49 -04:00
|
|
|
# Skip federation certificate verification on the following whitelist
|
|
|
|
# of domains.
|
|
|
|
#
|
|
|
|
# This setting should only be used in very specific cases, such as
|
|
|
|
# federation over Tor hidden services and similar. For private networks
|
|
|
|
# of homeservers, you likely want to use a private CA instead.
|
|
|
|
#
|
|
|
|
# Only effective if federation_verify_certicates is `true`.
|
|
|
|
#
|
|
|
|
#federation_certificate_verification_whitelist:
|
|
|
|
# - lon.example.com
|
Docs: Quote wildcard `federation_certificate_verification_whitelist` (#11381)
Otherwise I get this beautiful stacktrace:
```
python3 -m synapse.app.homeserver --config-path /etc/matrix/homeserver.yaml
Traceback (most recent call last):
File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/root/synapse/synapse/app/homeserver.py", line 455, in <module>
main()
File "/root/synapse/synapse/app/homeserver.py", line 445, in main
hs = setup(sys.argv[1:])
File "/root/synapse/synapse/app/homeserver.py", line 345, in setup
config = HomeServerConfig.load_or_generate_config(
File "/root/synapse/synapse/config/_base.py", line 671, in load_or_generate_config
config_dict = read_config_files(config_files)
File "/root/synapse/synapse/config/_base.py", line 717, in read_config_files
yaml_config = yaml.safe_load(file_stream)
File "/root/synapse/env/lib/python3.8/site-packages/yaml/__init__.py", line 125, in safe_load
return load(stream, SafeLoader)
File "/root/synapse/env/lib/python3.8/site-packages/yaml/__init__.py", line 81, in load
return loader.get_single_data()
File "/root/synapse/env/lib/python3.8/site-packages/yaml/constructor.py", line 49, in get_single_data
node = self.get_single_node()
File "/root/synapse/env/lib/python3.8/site-packages/yaml/composer.py", line 36, in get_single_node
document = self.compose_document()
File "/root/synapse/env/lib/python3.8/site-packages/yaml/composer.py", line 55, in compose_document
node = self.compose_node(None, None)
File "/root/synapse/env/lib/python3.8/site-packages/yaml/composer.py", line 84, in compose_node
node = self.compose_mapping_node(anchor)
File "/root/synapse/env/lib/python3.8/site-packages/yaml/composer.py", line 133, in compose_mapping_node
item_value = self.compose_node(node, item_key)
File "/root/synapse/env/lib/python3.8/site-packages/yaml/composer.py", line 82, in compose_node
node = self.compose_sequence_node(anchor)
File "/root/synapse/env/lib/python3.8/site-packages/yaml/composer.py", line 110, in compose_sequence_node
while not self.check_event(SequenceEndEvent):
File "/root/synapse/env/lib/python3.8/site-packages/yaml/parser.py", line 98, in check_event
self.current_event = self.state()
File "/root/synapse/env/lib/python3.8/site-packages/yaml/parser.py", line 379, in parse_block_sequence_first_entry
return self.parse_block_sequence_entry()
File "/root/synapse/env/lib/python3.8/site-packages/yaml/parser.py", line 384, in parse_block_sequence_entry
if not self.check_token(BlockEntryToken, BlockEndToken):
File "/root/synapse/env/lib/python3.8/site-packages/yaml/scanner.py", line 116, in check_token
self.fetch_more_tokens()
File "/root/synapse/env/lib/python3.8/site-packages/yaml/scanner.py", line 227, in fetch_more_tokens
return self.fetch_alias()
File "/root/synapse/env/lib/python3.8/site-packages/yaml/scanner.py", line 610, in fetch_alias
self.tokens.append(self.scan_anchor(AliasToken))
File "/root/synapse/env/lib/python3.8/site-packages/yaml/scanner.py", line 922, in scan_anchor
raise ScannerError("while scanning an %s" % name, start_mark,
yaml.scanner.ScannerError: while scanning an alias
in "/etc/matrix/homeserver.yaml", line 614, column 5
expected alphabetic or numeric character, but found '.'
in "/etc/matrix/homeserver.yaml", line 614, column 6
```
Signed-off-by: Nicolai Søborg <git@xn--sb-lka.org>
2021-11-18 07:24:40 -05:00
|
|
|
# - "*.domain.com"
|
|
|
|
# - "*.onion"
|
2019-04-25 09:22:49 -04:00
|
|
|
|
|
|
|
# List of custom certificate authorities for federation traffic.
|
|
|
|
#
|
|
|
|
# This setting should only normally be used within a private network of
|
|
|
|
# homeservers.
|
|
|
|
#
|
|
|
|
# Note that this list will replace those that are provided by your
|
|
|
|
# operating environment. Certificates must be in PEM format.
|
|
|
|
#
|
|
|
|
#federation_custom_ca_list:
|
|
|
|
# - myCA1.pem
|
|
|
|
# - myCA2.pem
|
|
|
|
# - myCA3.pem
|
2019-01-23 03:39:06 -05:00
|
|
|
"""
|
2019-10-23 08:22:54 -04:00
|
|
|
# Lowercase the string representation of boolean values
|
|
|
|
% {
|
|
|
|
x[0]: str(x[1]).lower() if isinstance(x[1], bool) else x[1]
|
|
|
|
for x in locals().items()
|
|
|
|
}
|
2019-01-23 03:39:06 -05:00
|
|
|
)
|
2014-08-31 11:06:39 -04:00
|
|
|
|
2020-10-01 08:09:18 -04:00
|
|
|
def read_tls_certificate(self) -> crypto.X509:
|
2019-02-11 16:00:41 -05:00
|
|
|
"""Reads the TLS certificate from the configured file, and returns it
|
|
|
|
|
|
|
|
Returns:
|
2020-10-01 08:09:18 -04:00
|
|
|
The certificate
|
2019-02-11 16:00:41 -05:00
|
|
|
"""
|
|
|
|
cert_path = self.tls_certificate_file
|
|
|
|
logger.info("Loading TLS certificate from %s", cert_path)
|
|
|
|
cert_pem = self.read_file(cert_path, "tls_certificate_path")
|
2021-11-23 10:21:19 -05:00
|
|
|
cert = crypto.load_certificate(crypto.FILETYPE_PEM, cert_pem.encode())
|
2019-02-11 16:00:41 -05:00
|
|
|
|
|
|
|
return cert
|
2014-08-31 11:06:39 -04:00
|
|
|
|
2020-10-01 08:09:18 -04:00
|
|
|
def read_tls_private_key(self) -> crypto.PKey:
|
2019-02-11 16:00:41 -05:00
|
|
|
"""Reads the TLS private key from the configured file, and returns it
|
|
|
|
|
|
|
|
Returns:
|
2020-10-01 08:09:18 -04:00
|
|
|
The private key
|
2019-02-11 16:00:41 -05:00
|
|
|
"""
|
|
|
|
private_key_path = self.tls_private_key_file
|
|
|
|
logger.info("Loading TLS key from %s", private_key_path)
|
|
|
|
private_key_pem = self.read_file(private_key_path, "tls_private_key_path")
|
2014-08-31 11:06:39 -04:00
|
|
|
return crypto.load_privatekey(crypto.FILETYPE_PEM, private_key_pem)
|