2019-06-24 06:33:56 -04:00
|
|
|
# Copyright 2019 New Vector Ltd
|
|
|
|
# Copyright 2019 The Matrix.org Foundation C.I.C.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Utility function to create an ACME issuing service.
|
|
|
|
|
|
|
|
This file contains the unconditional imports on the acme and cryptography bits that we
|
|
|
|
only need (and may only have available) if we are doing ACME, so is designed to be
|
|
|
|
imported conditionally.
|
|
|
|
"""
|
2019-06-21 10:27:41 -04:00
|
|
|
import logging
|
2021-01-26 10:50:21 -05:00
|
|
|
from typing import Dict, Iterable, List
|
2019-06-24 06:33:56 -04:00
|
|
|
|
|
|
|
import attr
|
2021-01-26 10:50:21 -05:00
|
|
|
import pem
|
2019-06-21 10:27:41 -04:00
|
|
|
from cryptography.hazmat.backends import default_backend
|
|
|
|
from cryptography.hazmat.primitives import serialization
|
|
|
|
from josepy import JWKRSA
|
2019-06-24 06:33:56 -04:00
|
|
|
from josepy.jwa import RS256
|
|
|
|
from txacme.challenges import HTTP01Responder
|
|
|
|
from txacme.client import Client
|
|
|
|
from txacme.interfaces import ICertificateStore
|
|
|
|
from txacme.service import AcmeIssuingService
|
2019-06-21 10:27:41 -04:00
|
|
|
from txacme.util import generate_private_key
|
2019-06-24 06:33:56 -04:00
|
|
|
from zope.interface import implementer
|
|
|
|
|
|
|
|
from twisted.internet import defer
|
2021-01-26 10:50:21 -05:00
|
|
|
from twisted.internet.interfaces import IReactorTCP
|
2019-06-24 06:33:56 -04:00
|
|
|
from twisted.python.filepath import FilePath
|
|
|
|
from twisted.python.url import URL
|
2021-01-26 10:50:21 -05:00
|
|
|
from twisted.web.resource import IResource
|
2019-06-24 06:33:56 -04:00
|
|
|
|
2019-06-21 10:27:41 -04:00
|
|
|
logger = logging.getLogger(__name__)
|
2019-06-24 06:33:56 -04:00
|
|
|
|
2019-06-21 10:27:41 -04:00
|
|
|
|
2021-01-26 10:50:21 -05:00
|
|
|
def create_issuing_service(
|
|
|
|
reactor: IReactorTCP,
|
|
|
|
acme_url: str,
|
|
|
|
account_key_file: str,
|
|
|
|
well_known_resource: IResource,
|
|
|
|
) -> AcmeIssuingService:
|
2019-06-24 06:33:56 -04:00
|
|
|
"""Create an ACME issuing service, and attach it to a web Resource
|
|
|
|
|
|
|
|
Args:
|
|
|
|
reactor: twisted reactor
|
2021-01-26 10:50:21 -05:00
|
|
|
acme_url: URL to use to request certificates
|
|
|
|
account_key_file: where to store the account key
|
|
|
|
well_known_resource: web resource for .well-known.
|
2019-06-24 06:33:56 -04:00
|
|
|
we will attach a child resource for "acme-challenge".
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
AcmeIssuingService
|
|
|
|
"""
|
|
|
|
responder = HTTP01Responder()
|
|
|
|
|
|
|
|
well_known_resource.putChild(b"acme-challenge", responder.resource)
|
|
|
|
|
|
|
|
store = ErsatzStore()
|
|
|
|
|
|
|
|
return AcmeIssuingService(
|
|
|
|
cert_store=store,
|
|
|
|
client_creator=(
|
|
|
|
lambda: Client.from_url(
|
|
|
|
reactor=reactor,
|
|
|
|
url=URL.from_text(acme_url),
|
2019-06-21 10:27:41 -04:00
|
|
|
key=load_or_create_client_key(account_key_file),
|
2019-06-24 06:33:56 -04:00
|
|
|
alg=RS256,
|
|
|
|
)
|
|
|
|
),
|
|
|
|
clock=reactor,
|
|
|
|
responders=[responder],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-09-14 12:50:06 -04:00
|
|
|
@attr.s(slots=True)
|
2019-06-24 06:33:56 -04:00
|
|
|
@implementer(ICertificateStore)
|
2020-09-04 06:54:56 -04:00
|
|
|
class ErsatzStore:
|
2019-06-24 06:33:56 -04:00
|
|
|
"""
|
|
|
|
A store that only stores in memory.
|
|
|
|
"""
|
|
|
|
|
2021-01-26 10:50:21 -05:00
|
|
|
certs = attr.ib(type=Dict[bytes, List[bytes]], default=attr.Factory(dict))
|
2019-06-24 06:33:56 -04:00
|
|
|
|
2021-01-26 10:50:21 -05:00
|
|
|
def store(
|
|
|
|
self, server_name: bytes, pem_objects: Iterable[pem.AbstractPEMObject]
|
|
|
|
) -> defer.Deferred:
|
2019-06-24 06:33:56 -04:00
|
|
|
self.certs[server_name] = [o.as_bytes() for o in pem_objects]
|
|
|
|
return defer.succeed(None)
|
2019-06-21 10:27:41 -04:00
|
|
|
|
|
|
|
|
2021-01-26 10:50:21 -05:00
|
|
|
def load_or_create_client_key(key_file: str) -> JWKRSA:
|
2019-06-21 10:27:41 -04:00
|
|
|
"""Load the ACME account key from a file, creating it if it does not exist.
|
|
|
|
|
|
|
|
Args:
|
2021-01-26 10:50:21 -05:00
|
|
|
key_file: name of the file to use as the account key
|
2019-06-21 10:27:41 -04:00
|
|
|
"""
|
|
|
|
# this is based on txacme.endpoint.load_or_create_client_key, but doesn't
|
|
|
|
# hardcode the 'client.key' filename
|
|
|
|
acme_key_file = FilePath(key_file)
|
|
|
|
if acme_key_file.exists():
|
|
|
|
logger.info("Loading ACME account key from '%s'", acme_key_file)
|
|
|
|
key = serialization.load_pem_private_key(
|
|
|
|
acme_key_file.getContent(), password=None, backend=default_backend()
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
logger.info("Saving new ACME account key to '%s'", acme_key_file)
|
|
|
|
key = generate_private_key("rsa")
|
|
|
|
acme_key_file.setContent(
|
|
|
|
key.private_bytes(
|
|
|
|
encoding=serialization.Encoding.PEM,
|
|
|
|
format=serialization.PrivateFormat.TraditionalOpenSSL,
|
|
|
|
encryption_algorithm=serialization.NoEncryption(),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return JWKRSA(key=key)
|