2014-08-31 09:06:39 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 21:26:29 -07:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2014-08-31 09:06:39 -06: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 01:39:06 -07:00
|
|
|
import logging
|
2014-08-31 09:06:39 -06:00
|
|
|
import os
|
2019-01-29 07:09:10 -07:00
|
|
|
import warnings
|
2019-01-23 01:39:06 -07:00
|
|
|
from datetime import datetime
|
2016-10-11 12:14:46 -06:00
|
|
|
from hashlib import sha256
|
2020-10-01 06:09:18 -06:00
|
|
|
from typing import List, Optional
|
2018-07-09 00:09:20 -06:00
|
|
|
|
2016-10-11 12:14:46 -06:00
|
|
|
from unpaddedbase64 import encode_base64
|
|
|
|
|
2019-06-28 02:19:09 -06:00
|
|
|
from OpenSSL import SSL, crypto
|
2019-04-25 07:22:49 -06:00
|
|
|
from twisted.internet._sslverify import Certificate, trustRootFromCertificates
|
2018-07-09 00:09:20 -06:00
|
|
|
|
2019-02-12 03:51:31 -07:00
|
|
|
from synapse.config._base import Config, ConfigError
|
2019-04-25 07:22:49 -06:00
|
|
|
from synapse.util import glob_to_regex
|
2019-01-23 01:39:06 -07:00
|
|
|
|
2019-02-11 14:00:41 -07:00
|
|
|
logger = logging.getLogger(__name__)
|
2018-07-09 00:09:20 -06:00
|
|
|
|
2020-02-13 07:20:08 -07:00
|
|
|
ACME_SUPPORT_ENABLED_WARN = """\
|
|
|
|
This server uses Synapse's built-in ACME support. Note that ACME v1 has been
|
|
|
|
deprecated by Let's Encrypt, and that Synapse doesn't currently support ACME v2,
|
|
|
|
which means that this feature will not work with Synapse installs set up after
|
|
|
|
November 2019, and that it may stop working on June 2020 for installs set up
|
|
|
|
before that date.
|
|
|
|
|
|
|
|
For more info and alternative solutions, see
|
2020-02-13 10:03:41 -07:00
|
|
|
https://github.com/matrix-org/synapse/blob/master/docs/ACME.md#deprecation-of-acme-v1
|
2020-02-13 08:44:14 -07:00
|
|
|
--------------------------------------------------------------------------------"""
|
2020-02-13 07:20:08 -07:00
|
|
|
|
2014-09-01 13:35:10 -06:00
|
|
|
|
2014-08-31 09:06:39 -06:00
|
|
|
class TlsConfig(Config):
|
2019-10-10 02:39:35 -06:00
|
|
|
section = "tls"
|
|
|
|
|
|
|
|
def read_config(self, config: dict, config_dir_path: str, **kwargs):
|
2015-07-08 11:20:02 -06:00
|
|
|
|
2019-01-30 07:17:55 -07:00
|
|
|
acme_config = config.get("acme", None)
|
|
|
|
if acme_config is None:
|
|
|
|
acme_config = {}
|
|
|
|
|
2019-01-23 01:39:06 -07:00
|
|
|
self.acme_enabled = acme_config.get("enabled", False)
|
2019-02-25 12:16:33 -07:00
|
|
|
|
2020-02-13 07:20:08 -07:00
|
|
|
if self.acme_enabled:
|
|
|
|
logger.warning(ACME_SUPPORT_ENABLED_WARN)
|
|
|
|
|
2019-02-25 12:16:33 -07:00
|
|
|
# hyperlink complains on py2 if this is not a Unicode
|
2020-06-16 06:51:47 -06:00
|
|
|
self.acme_url = str(
|
2019-02-05 04:37:33 -07:00
|
|
|
acme_config.get("url", "https://acme-v01.api.letsencrypt.org/directory")
|
2019-02-25 12:16:33 -07:00
|
|
|
)
|
2019-01-30 07:17:55 -07:00
|
|
|
self.acme_port = acme_config.get("port", 80)
|
|
|
|
self.acme_bind_addresses = acme_config.get("bind_addresses", ["::", "0.0.0.0"])
|
2019-01-23 01:39:06 -07:00
|
|
|
self.acme_reprovision_threshold = acme_config.get("reprovision_threshold", 30)
|
2019-02-18 08:46:23 -07:00
|
|
|
self.acme_domain = acme_config.get("domain", config.get("server_name"))
|
2019-01-23 01:39:06 -07:00
|
|
|
|
2019-06-21 08:27:41 -06:00
|
|
|
self.acme_account_key_file = self.abspath(
|
|
|
|
acme_config.get("account_key_file", config_dir_path + "/client.key")
|
|
|
|
)
|
|
|
|
|
2019-01-29 07:09:10 -07: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 03:51:31 -07:00
|
|
|
|
2019-10-10 02:39:35 -06:00
|
|
|
if self.root.server.has_tls_listener():
|
2019-02-12 03:51:31 -07: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 12:16:33 -07:00
|
|
|
"tls_private_key_path must be specified if TLS-enabled listeners are "
|
2019-02-12 03:51:31 -07:00
|
|
|
"configured."
|
|
|
|
)
|
|
|
|
|
2019-02-11 03:04:27 -07:00
|
|
|
self._original_tls_fingerprints = config.get("tls_fingerprints", [])
|
|
|
|
|
|
|
|
if self._original_tls_fingerprints is None:
|
|
|
|
self._original_tls_fingerprints = []
|
|
|
|
|
2019-01-23 01:39:06 -07:00
|
|
|
self.tls_fingerprints = list(self._original_tls_fingerprints)
|
2015-03-06 04:34:06 -07:00
|
|
|
|
2019-04-25 07:22:49 -06:00
|
|
|
# Whether to verify certificates on outbound federation traffic
|
|
|
|
self.federation_verify_certificates = config.get(
|
2019-06-05 07:16:07 -06:00
|
|
|
"federation_verify_certificates", True
|
2019-04-25 07:22:49 -06:00
|
|
|
)
|
|
|
|
|
2019-06-28 02:19:09 -06: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(
|
|
|
|
(
|
|
|
|
"federation_client_minimum_tls_version cannot be 1.3, "
|
|
|
|
"your OpenSSL does not support it"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2019-04-25 07:22:49 -06:00
|
|
|
# Whitelist of domains to not verify certificates for
|
|
|
|
fed_whitelist_entries = config.get(
|
|
|
|
"federation_certificate_verification_whitelist", []
|
|
|
|
)
|
2020-02-06 07:45:01 -07:00
|
|
|
if fed_whitelist_entries is None:
|
|
|
|
fed_whitelist_entries = []
|
2019-04-25 07:22:49 -06:00
|
|
|
|
|
|
|
# Support globs (*) in whitelist values
|
2019-10-10 02:39:35 -06:00
|
|
|
self.federation_certificate_verification_whitelist = [] # type: List[str]
|
2019-04-25 07:22:49 -06:00
|
|
|
for entry in fed_whitelist_entries:
|
2019-09-13 12:58:38 -06: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 07:22:49 -06:00
|
|
|
# Convert globs to regex
|
|
|
|
self.federation_certificate_verification_whitelist.append(entry_regex)
|
|
|
|
|
|
|
|
# List of custom certificate authorities for federation traffic validation
|
|
|
|
custom_ca_list = config.get("federation_custom_ca_list", None)
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
raise ConfigError(
|
|
|
|
"federation_custom_ca_list specified without "
|
|
|
|
"any certificate files"
|
|
|
|
)
|
|
|
|
|
|
|
|
certs = []
|
|
|
|
for ca_file in custom_ca_list:
|
|
|
|
logger.debug("Reading custom CA certificate file: %s", ca_file)
|
2019-06-05 09:16:33 -06:00
|
|
|
content = self.read_file(ca_file, "federation_custom_ca_list")
|
2019-04-25 07:22:49 -06:00
|
|
|
|
|
|
|
# Parse the CA certificates
|
|
|
|
try:
|
|
|
|
cert_base = Certificate.loadPEM(content)
|
|
|
|
certs.append(cert_base)
|
|
|
|
except Exception as e:
|
|
|
|
raise ConfigError(
|
|
|
|
"Error parsing custom CA certificate file %s: %s" % (ca_file, e)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.federation_ca_trust_root = trustRootFromCertificates(certs)
|
|
|
|
|
2019-01-23 01:39:06 -07: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"
|
|
|
|
)
|
|
|
|
|
2020-10-01 06:09:18 -06:00
|
|
|
self.tls_certificate = None # type: Optional[crypto.X509]
|
|
|
|
self.tls_private_key = None # type: Optional[crypto.PKey]
|
2019-01-23 01:39:06 -07:00
|
|
|
|
2019-02-11 03:36:26 -07:00
|
|
|
def is_disk_cert_valid(self, allow_self_signed=True):
|
2019-01-23 01:39:06 -07:00
|
|
|
"""
|
|
|
|
Is the certificate we have on disk valid, and if so, for how long?
|
|
|
|
|
2019-02-11 03:36:26 -07:00
|
|
|
Args:
|
|
|
|
allow_self_signed (bool): Should we allow the certificate we
|
|
|
|
read to be self signed?
|
|
|
|
|
2019-01-23 01:39:06 -07:00
|
|
|
Returns:
|
|
|
|
int: Days remaining of certificate validity.
|
|
|
|
None: No certificate exists.
|
|
|
|
"""
|
|
|
|
if not os.path.exists(self.tls_certificate_file):
|
|
|
|
return None
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(self.tls_certificate_file, "rb") as f:
|
|
|
|
cert_pem = f.read()
|
2019-04-25 07:22:49 -06:00
|
|
|
except Exception as e:
|
|
|
|
raise ConfigError(
|
|
|
|
"Failed to read existing certificate file %s: %s"
|
|
|
|
% (self.tls_certificate_file, e)
|
2019-06-20 03:32:02 -06:00
|
|
|
)
|
2019-01-23 01:39:06 -07:00
|
|
|
|
|
|
|
try:
|
|
|
|
tls_certificate = crypto.load_certificate(crypto.FILETYPE_PEM, cert_pem)
|
2019-04-25 07:22:49 -06:00
|
|
|
except Exception as e:
|
|
|
|
raise ConfigError(
|
|
|
|
"Failed to parse existing certificate file %s: %s"
|
|
|
|
% (self.tls_certificate_file, e)
|
2019-06-20 03:32:02 -06:00
|
|
|
)
|
2019-01-23 01:39:06 -07:00
|
|
|
|
2019-02-11 03:36:26 -07:00
|
|
|
if not allow_self_signed:
|
|
|
|
if tls_certificate.get_subject() == tls_certificate.get_issuer():
|
|
|
|
raise ValueError(
|
|
|
|
"TLS Certificate is self signed, and this is not permitted"
|
|
|
|
)
|
|
|
|
|
2019-01-23 01:39:06 -07:00
|
|
|
# YYYYMMDDhhmmssZ -- in UTC
|
|
|
|
expires_on = datetime.strptime(
|
|
|
|
tls_certificate.get_notAfter().decode("ascii"), "%Y%m%d%H%M%SZ"
|
|
|
|
)
|
|
|
|
now = datetime.utcnow()
|
|
|
|
days_remaining = (expires_on - now).days
|
|
|
|
return days_remaining
|
|
|
|
|
2020-10-01 06:09:18 -06:00
|
|
|
def read_certificate_from_disk(self, require_cert_and_key: bool):
|
2019-01-23 01:39:06 -07:00
|
|
|
"""
|
2019-02-12 03:51:31 -07:00
|
|
|
Read the certificates and private key from disk.
|
2019-01-29 07:09:10 -07:00
|
|
|
|
2019-02-12 03:51:31 -07:00
|
|
|
Args:
|
2020-10-01 06:09:18 -06:00
|
|
|
require_cert_and_key: set to True to throw an error if the certificate
|
2019-02-12 03:51:31 -07:00
|
|
|
and key file are not given
|
|
|
|
"""
|
|
|
|
if require_cert_and_key:
|
2019-02-11 14:00:41 -07:00
|
|
|
self.tls_private_key = self.read_tls_private_key()
|
2019-02-12 03:51:31 -07:00
|
|
|
self.tls_certificate = self.read_tls_certificate()
|
|
|
|
elif self.tls_certificate_file:
|
|
|
|
# we only need the certificate for the tls_fingerprints. Reload it if we
|
|
|
|
# can, but it's not a fatal error if we can't.
|
|
|
|
try:
|
|
|
|
self.tls_certificate = self.read_tls_certificate()
|
|
|
|
except Exception as e:
|
|
|
|
logger.info(
|
|
|
|
"Unable to read TLS certificate (%s). Ignoring as no "
|
|
|
|
"tls listeners enabled.",
|
|
|
|
e,
|
|
|
|
)
|
2015-03-06 04:34:06 -07:00
|
|
|
|
2019-01-23 01:39:06 -07:00
|
|
|
self.tls_fingerprints = list(self._original_tls_fingerprints)
|
2016-10-11 12:14:46 -06:00
|
|
|
|
2019-02-12 03:51:31 -07:00
|
|
|
if self.tls_certificate:
|
|
|
|
# Check that our own certificate is included in the list of fingerprints
|
|
|
|
# and include it if it is not.
|
|
|
|
x509_certificate_bytes = crypto.dump_certificate(
|
|
|
|
crypto.FILETYPE_ASN1, self.tls_certificate
|
|
|
|
)
|
|
|
|
sha256_fingerprint = encode_base64(sha256(x509_certificate_bytes).digest())
|
2020-02-21 05:15:07 -07:00
|
|
|
sha256_fingerprints = {f["sha256"] for f in self.tls_fingerprints}
|
2019-02-12 03:51:31 -07:00
|
|
|
if sha256_fingerprint not in sha256_fingerprints:
|
|
|
|
self.tls_fingerprints.append({"sha256": sha256_fingerprint})
|
2016-10-11 12:14:46 -06:00
|
|
|
|
2019-06-21 17:00:20 -06:00
|
|
|
def generate_config_section(
|
2019-08-28 06:12:22 -06:00
|
|
|
self,
|
|
|
|
config_dir_path,
|
|
|
|
server_name,
|
|
|
|
data_dir_path,
|
|
|
|
tls_certificate_path,
|
|
|
|
tls_private_key_path,
|
|
|
|
acme_domain,
|
2021-04-13 03:41:34 -06:00
|
|
|
**kwargs,
|
2019-06-21 17:00:20 -06:00
|
|
|
):
|
2019-08-28 06:12:22 -06:00
|
|
|
"""If the acme_domain is specified acme will be enabled.
|
|
|
|
If the TLS paths are not specified the default will be certs in the
|
|
|
|
config directory"""
|
|
|
|
|
2015-04-29 21:24:44 -06:00
|
|
|
base_key_name = os.path.join(config_dir_path, server_name)
|
|
|
|
|
2019-08-28 06:12:22 -06: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."
|
|
|
|
)
|
|
|
|
|
|
|
|
tls_enabled = (
|
|
|
|
"" if tls_certificate_path and tls_private_key_path or acme_domain else "#"
|
|
|
|
)
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
acme_enabled = bool(acme_domain)
|
|
|
|
acme_domain = "matrix.example.com"
|
|
|
|
|
2019-06-21 08:27:41 -06:00
|
|
|
default_acme_account_file = os.path.join(data_dir_path, "acme_account.key")
|
2015-04-29 21:24:44 -06:00
|
|
|
|
2019-01-30 07:17:55 -07:00
|
|
|
# this is to avoid the max line length. Sorrynotsorry
|
|
|
|
proxypassline = (
|
|
|
|
"ProxyPass /.well-known/acme-challenge "
|
|
|
|
"http://localhost:8009/.well-known/acme-challenge"
|
|
|
|
)
|
|
|
|
|
2019-10-23 06:22:54 -06:00
|
|
|
# flake8 doesn't recognise that variables are used in the below string
|
|
|
|
_ = tls_enabled, proxypassline, acme_enabled, default_acme_account_file
|
|
|
|
|
2019-01-23 01:39:06 -07:00
|
|
|
return (
|
|
|
|
"""\
|
2019-02-11 10:57:58 -07:00
|
|
|
## TLS ##
|
|
|
|
|
2019-01-30 07:17:55 -07: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.
|
|
|
|
#
|
|
|
|
# See 'ACME support' below to enable auto-provisioning this certificate via
|
|
|
|
# Let's Encrypt.
|
|
|
|
#
|
2019-03-13 09:26:29 -06:00
|
|
|
# If supplying your own, 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-08-28 06:12:22 -06:00
|
|
|
%(tls_enabled)stls_certificate_path: "%(tls_certificate_path)s"
|
2015-04-29 21:24:44 -06:00
|
|
|
|
2019-01-30 07:17:55 -07:00
|
|
|
# PEM-encoded private key for TLS
|
2019-02-19 06:54:29 -07:00
|
|
|
#
|
2019-08-28 06:12:22 -06:00
|
|
|
%(tls_enabled)stls_private_key_path: "%(tls_private_key_path)s"
|
2015-04-29 21:24:44 -06:00
|
|
|
|
2019-06-05 07:16:07 -06:00
|
|
|
# Whether to verify TLS server certificates for outbound federation requests.
|
2019-04-25 07:22:49 -06:00
|
|
|
#
|
2019-06-05 07:16:07 -06:00
|
|
|
# Defaults to `true`. To disable certificate verification, uncomment the
|
|
|
|
# following line.
|
2019-04-25 07:22:49 -06:00
|
|
|
#
|
2019-06-05 07:16:07 -06:00
|
|
|
#federation_verify_certificates: false
|
2019-04-25 07:22:49 -06:00
|
|
|
|
2019-06-28 02:19:09 -06: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 07:22:49 -06: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
|
|
|
|
# - *.domain.com
|
|
|
|
# - *.onion
|
|
|
|
|
|
|
|
# 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-30 07:17:55 -07:00
|
|
|
# ACME support: This will configure Synapse to request a valid TLS certificate
|
|
|
|
# for your configured `server_name` via Let's Encrypt.
|
|
|
|
#
|
2020-02-13 07:20:08 -07:00
|
|
|
# Note that ACME v1 is now deprecated, and Synapse currently doesn't support
|
|
|
|
# ACME v2. This means that this feature currently won't work with installs set
|
|
|
|
# up after November 2019. For more info, and alternative solutions, see
|
|
|
|
# https://github.com/matrix-org/synapse/blob/master/docs/ACME.md#deprecation-of-acme-v1
|
|
|
|
#
|
2019-01-30 07:17:55 -07:00
|
|
|
# Note that provisioning a certificate in this way requires port 80 to be
|
|
|
|
# routed to Synapse so that it can complete the http-01 ACME challenge.
|
|
|
|
# By default, if you enable ACME support, Synapse will attempt to listen on
|
|
|
|
# port 80 for incoming http-01 challenges - however, this will likely fail
|
|
|
|
# with 'Permission denied' or a similar error.
|
|
|
|
#
|
|
|
|
# There are a couple of potential solutions to this:
|
|
|
|
#
|
|
|
|
# * If you already have an Apache, Nginx, or similar listening on port 80,
|
|
|
|
# you can configure Synapse to use an alternate port, and have your web
|
|
|
|
# server forward the requests. For example, assuming you set 'port: 8009'
|
|
|
|
# below, on Apache, you would write:
|
|
|
|
#
|
|
|
|
# %(proxypassline)s
|
|
|
|
#
|
|
|
|
# * Alternatively, you can use something like `authbind` to give Synapse
|
|
|
|
# permission to listen on port 80.
|
|
|
|
#
|
|
|
|
acme:
|
2019-08-28 06:12:22 -06:00
|
|
|
# ACME support is disabled by default. Set this to `true` and uncomment
|
|
|
|
# tls_certificate_path and tls_private_key_path above to enable it.
|
2019-01-30 07:17:55 -07:00
|
|
|
#
|
2019-08-28 06:12:22 -06:00
|
|
|
enabled: %(acme_enabled)s
|
2019-01-30 07:17:55 -07:00
|
|
|
|
|
|
|
# Endpoint to use to request certificates. If you only want to test,
|
|
|
|
# use Let's Encrypt's staging url:
|
|
|
|
# https://acme-staging.api.letsencrypt.org/directory
|
|
|
|
#
|
2019-02-19 06:54:29 -07:00
|
|
|
#url: https://acme-v01.api.letsencrypt.org/directory
|
2019-01-30 07:17:55 -07:00
|
|
|
|
|
|
|
# Port number to listen on for the HTTP-01 challenge. Change this if
|
|
|
|
# you are forwarding connections through Apache/Nginx/etc.
|
|
|
|
#
|
2019-08-28 06:12:22 -06:00
|
|
|
port: 80
|
2019-01-30 07:17:55 -07:00
|
|
|
|
|
|
|
# Local addresses to listen on for incoming connections.
|
|
|
|
# Again, you may want to change this if you are forwarding connections
|
|
|
|
# through Apache/Nginx/etc.
|
|
|
|
#
|
2019-08-28 06:12:22 -06:00
|
|
|
bind_addresses: ['::', '0.0.0.0']
|
2019-01-30 07:17:55 -07:00
|
|
|
|
|
|
|
# How many days remaining on a certificate before it is renewed.
|
|
|
|
#
|
2019-08-28 06:12:22 -06:00
|
|
|
reprovision_threshold: 30
|
2019-01-30 07:17:55 -07:00
|
|
|
|
2019-02-19 03:59:26 -07:00
|
|
|
# The domain that the certificate should be for. Normally this
|
|
|
|
# should be the same as your Matrix domain (i.e., 'server_name'), but,
|
|
|
|
# by putting a file at 'https://<server_name>/.well-known/matrix/server',
|
|
|
|
# you can delegate incoming traffic to another server. If you do that,
|
|
|
|
# you should give the target of the delegation here.
|
|
|
|
#
|
|
|
|
# For example: if your 'server_name' is 'example.com', but
|
|
|
|
# 'https://example.com/.well-known/matrix/server' delegates to
|
|
|
|
# 'matrix.example.com', you should put 'matrix.example.com' here.
|
|
|
|
#
|
|
|
|
# If not set, defaults to your 'server_name'.
|
2019-02-18 08:46:23 -07:00
|
|
|
#
|
2019-08-28 06:12:22 -06:00
|
|
|
domain: %(acme_domain)s
|
2019-02-18 08:46:23 -07:00
|
|
|
|
2019-06-21 08:27:41 -06:00
|
|
|
# file to use for the account key. This will be generated if it doesn't
|
|
|
|
# exist.
|
|
|
|
#
|
|
|
|
# If unspecified, we will use CONFDIR/client.key.
|
|
|
|
#
|
|
|
|
account_key_file: %(default_acme_account_file)s
|
|
|
|
|
2016-10-11 12:14:46 -06:00
|
|
|
# List of allowed TLS fingerprints for this server to publish along
|
|
|
|
# with the signing keys for this server. Other matrix servers that
|
|
|
|
# make HTTPS requests to this server will check that the TLS
|
|
|
|
# certificates returned by this server match one of the fingerprints.
|
|
|
|
#
|
2017-02-12 00:02:57 -07:00
|
|
|
# Synapse automatically adds the fingerprint of its own certificate
|
2018-01-16 09:52:31 -07:00
|
|
|
# to the list. So if federation traffic is handled directly by synapse
|
2016-10-11 12:14:46 -06:00
|
|
|
# then no modification to the list is required.
|
|
|
|
#
|
|
|
|
# If synapse is run behind a load balancer that handles the TLS then it
|
|
|
|
# will be necessary to add the fingerprints of the certificates used by
|
|
|
|
# the loadbalancers to this list if they are different to the one
|
|
|
|
# synapse is using.
|
|
|
|
#
|
|
|
|
# Homeservers are permitted to cache the list of TLS fingerprints
|
2016-10-12 07:48:24 -06:00
|
|
|
# returned in the key responses up to the "valid_until_ts" returned in
|
|
|
|
# key. It may be necessary to publish the fingerprints of a new
|
|
|
|
# certificate and wait until the "valid_until_ts" of the previous key
|
|
|
|
# responses have passed before deploying it.
|
2017-10-24 11:49:44 -06:00
|
|
|
#
|
|
|
|
# You can calculate a fingerprint from a given TLS listener via:
|
|
|
|
# openssl s_client -connect $host:$port < /dev/null 2> /dev/null |
|
|
|
|
# openssl x509 -outform DER | openssl sha256 -binary | base64 | tr -d '='
|
|
|
|
# or by checking matrix.org/federationtester/api/report?server_name=$host
|
|
|
|
#
|
2019-02-19 06:54:29 -07:00
|
|
|
#tls_fingerprints: [{"sha256": "<base64_encoded_sha256_fingerprint>"}]
|
2019-01-23 01:39:06 -07:00
|
|
|
"""
|
2019-10-23 06:22:54 -06: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 01:39:06 -07:00
|
|
|
)
|
2014-08-31 09:06:39 -06:00
|
|
|
|
2020-10-01 06:09:18 -06:00
|
|
|
def read_tls_certificate(self) -> crypto.X509:
|
2019-02-11 14:00:41 -07:00
|
|
|
"""Reads the TLS certificate from the configured file, and returns it
|
|
|
|
|
|
|
|
Also checks if it is self-signed, and warns if so
|
|
|
|
|
|
|
|
Returns:
|
2020-10-01 06:09:18 -06:00
|
|
|
The certificate
|
2019-02-11 14:00:41 -07: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")
|
|
|
|
cert = crypto.load_certificate(crypto.FILETYPE_PEM, cert_pem)
|
|
|
|
|
|
|
|
# Check if it is self-signed, and issue a warning if so.
|
|
|
|
if cert.get_issuer() == cert.get_subject():
|
|
|
|
warnings.warn(
|
|
|
|
(
|
|
|
|
"Self-signed TLS certificates will not be accepted by Synapse 1.0. "
|
|
|
|
"Please either provide a valid certificate, or use Synapse's ACME "
|
|
|
|
"support to provision one."
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return cert
|
2014-08-31 09:06:39 -06:00
|
|
|
|
2020-10-01 06:09:18 -06:00
|
|
|
def read_tls_private_key(self) -> crypto.PKey:
|
2019-02-11 14:00:41 -07:00
|
|
|
"""Reads the TLS private key from the configured file, and returns it
|
|
|
|
|
|
|
|
Returns:
|
2020-10-01 06:09:18 -06:00
|
|
|
The private key
|
2019-02-11 14:00:41 -07: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 09:06:39 -06:00
|
|
|
return crypto.load_privatekey(crypto.FILETYPE_PEM, private_key_pem)
|